Strings can now be transmuted or converted into binary data.

This also added the scribe library so that this library can use
the logging system.
This commit is contained in:
Jason Travis Smith
2016-04-13 23:57:40 -04:00
parent d1c4b3e053
commit e9ab0bdece
7 changed files with 225 additions and 1 deletions

View File

@ -74,7 +74,8 @@ macro_rules! read_bytes
/// and writing them to a buffer.
macro_rules! write_bytes
{
($buffer: expr, $valueType: ident, $numBytes: expr, $num: expr, $convertFunc: ident) =>
($buffer: expr, $valueType: ident, $numBytes: expr,
$num: expr, $convertFunc: ident) =>
({
assert!($buffer.len() >= $valueType::BYTES,
"Not enough room in the buffer to write to.");
@ -166,6 +167,67 @@ impl Converter for BigEndian
buffer.as_mut_ptr(), num_bytes as usize);
}
}
fn bytes_to_string(buffer: &[u8]) -> String
{
let byte_count: u64;
let new_string: String;
// A string array should have atleast a u64 size byte count.
assert!(buffer.len() >= U64_BYTES);
// Strings start with the size of bytes to read as
// a u64. So read that in and then we know how many
// bytes make up the string.
byte_count = BigEndian::bytes_to_u64(buffer);
if byte_count > 0
{
match String::from_utf8(buffer[U64_BYTES..(buffer.len()-1)].to_vec())
{
Ok(string) =>
{
new_string = string;
}
Err(error) =>
{
error!("{}", error);
}
}
}
else
{
new_string = String::new();
}
new_string
}
fn string_to_bytes(buffer: &mut [u8], string: String)
{
let bytes: &[u8];
let byte_count: u64;
// Turn the string into a byte array.
bytes = string.as_bytes();
// Determine how many bytes will be written
// for this string.
byte_count = bytes.len() as u64;
// Make sure the buffer has enough space for this string.
assert!(buffer.len() as u64 >= byte_count + U64_BYTES as u64);
// Add the count to the buffer.
BigEndian::u64_to_bytes(&mut buffer[0..U64_BYTES], byte_count);
// Add each byte of the string to the buffer.
for (i, byte) in bytes.iter().enumerate()
{
buffer[U64_BYTES + i] = byte.clone();
}
}
}
impl Converter for LittleEndian
@ -234,6 +296,67 @@ impl Converter for LittleEndian
num_bytes as usize);
}
}
fn bytes_to_string(buffer: &[u8]) -> String
{
let byte_count: u64;
let new_string: String;
// A string array should have atleast a u64 size byte count.
assert!(buffer.len() >= U64_BYTES);
// Strings start with the size of bytes to read as
// a u64. So read that in and then we know how many
// bytes make up the string.
byte_count = BigEndian::bytes_to_u64(buffer);
if byte_count > 0
{
match String::from_utf8(buffer[U64_BYTES..(buffer.len()-1)].to_vec())
{
Ok(string) =>
{
new_string = string;
}
Err(error) =>
{
error!("{}", error);
}
}
}
else
{
new_string = String::new();
}
new_string
}
fn string_to_bytes(buffer: &mut [u8], string: String)
{
let bytes: &[u8];
let byte_count: u64;
// Turn the string into a byte array.
bytes = string.as_bytes();
// Determine how many bytes will be written
// for this string.
byte_count = bytes.len() as u64 + U64_BYTES as u64;
// Make sure the buffer has enough space for this string.
assert!(buffer.len() as u64 >= byte_count);
// Add the count to the buffer.
LittleEndian::u64_to_bytes(&mut buffer[0..U64_BYTES], byte_count);
// Add each byte of the string to the buffer.
for (i, byte) in bytes.iter().enumerate()
{
buffer[U64_BYTES + i] = byte.clone();
}
}
}