1
Fork 0

Fix big endian read/write

Co-authored-by: matthewjasper <mjjasper1@gmail.com>
This commit is contained in:
Jubilee Young 2020-08-22 03:54:15 -07:00
parent dc00efff9f
commit 2df552b406

View file

@ -565,7 +565,7 @@ pub fn write_target_uint(
// So we do not write all bytes of the u128, just the "payload". // So we do not write all bytes of the u128, just the "payload".
match endianness { match endianness {
Endian::Little => target.write(&data.to_le_bytes())?, Endian::Little => target.write(&data.to_le_bytes())?,
Endian::Big => target.write(&data.to_be_bytes())?, Endian::Big => target.write(&data.to_be_bytes()[16 - target.len()..])?,
}; };
debug_assert!(target.len() == 0); // We should have filled the target buffer. debug_assert!(target.len() == 0); // We should have filled the target buffer.
Ok(()) Ok(())
@ -576,12 +576,18 @@ pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, i
// This u128 holds an "any-size uint" (since smaller uints can fits in it) // This u128 holds an "any-size uint" (since smaller uints can fits in it)
let mut buf = [0u8; std::mem::size_of::<u128>()]; let mut buf = [0u8; std::mem::size_of::<u128>()];
// So we do not read exactly 16 bytes into the u128, just the "payload". // So we do not read exactly 16 bytes into the u128, just the "payload".
let uint = match endianness {
Endian::Little => {
source.read(&mut buf)?; source.read(&mut buf)?;
debug_assert!(source.len() == 0); // We should have consumed the source buffer. Ok(u128::from_le_bytes(buf))
match endianness {
Endian::Little => Ok(u128::from_le_bytes(buf)),
Endian::Big => Ok(u128::from_be_bytes(buf)),
} }
Endian::Big => {
source.read(&mut buf[16 - source.len()..])?;
Ok(u128::from_be_bytes(buf))
}
};
debug_assert!(source.len() == 0); // We should have consumed the source buffer.
uint
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////