Naming pass for byte order conversion
This commit is contained in:
1 parent
0a2b3f91ab
commit
6cdf3d8d22
3 files changed
+23
-19
No files matched your search
+2
-2
@@ -766,7 +766,7 @@ impl Entry {
|
|||||||
let buf_unit = usize::from(type_.byte_len());
|
let buf_unit = usize::from(type_.byte_len());
|
||||||
let mul_of_ty = buffer.len() / buf_unit * buf_unit;
|
let mul_of_ty = buffer.len() / buf_unit * buf_unit;
|
||||||
|
|
||||||
let cls = type_.byte_order_class();
|
let cls = type_.endian_bytes();
|
||||||
let native = ByteOrder::native();
|
let native = ByteOrder::native();
|
||||||
|
|
||||||
while total_bytes > 0 {
|
while total_bytes > 0 {
|
||||||
@@ -777,7 +777,7 @@ impl Entry {
|
|||||||
let buffer = &mut buffer[..now as usize];
|
let buffer = &mut buffer[..now as usize];
|
||||||
reader.inner().read_exact(buffer)?;
|
reader.inner().read_exact(buffer)?;
|
||||||
|
|
||||||
reader.byte_order.convert_class(cls, buffer, native);
|
reader.byte_order.convert_endian_bytes(cls, buffer, native);
|
||||||
collect(buffer);
|
collect(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
-5
@@ -538,13 +538,13 @@ fn fix_endianness(buf: &mut [u8], byte_order: ByteOrder, bit_depth: u8) {
|
|||||||
let host = ByteOrder::native();
|
let host = ByteOrder::native();
|
||||||
|
|
||||||
let class = match bit_depth {
|
let class = match bit_depth {
|
||||||
0..=8 => crate::tags::ByteOrderClass::One,
|
0..=8 => crate::tags::EndianBytes::One,
|
||||||
9..=16 => crate::tags::ByteOrderClass::Two,
|
9..=16 => crate::tags::EndianBytes::Two,
|
||||||
17..=32 => crate::tags::ByteOrderClass::Four,
|
17..=32 => crate::tags::EndianBytes::Four,
|
||||||
_ => crate::tags::ByteOrderClass::Eight,
|
_ => crate::tags::EndianBytes::Eight,
|
||||||
};
|
};
|
||||||
|
|
||||||
host.convert_class(class, buf, byte_order);
|
host.convert_endian_bytes(class, buf, byte_order);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: Read + Seek> Decoder<R> {
|
impl<R: Read + Seek> Decoder<R> {
|
||||||
|
|||||||
+16
-12
@@ -217,17 +217,17 @@ impl Type {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn byte_order_class(self) -> ByteOrderClass {
|
pub(crate) fn endian_bytes(self) -> EndianBytes {
|
||||||
match self {
|
match self {
|
||||||
Type::BYTE | Type::SBYTE | Type::ASCII | Type::UNDEFINED => ByteOrderClass::One,
|
Type::BYTE | Type::SBYTE | Type::ASCII | Type::UNDEFINED => EndianBytes::One,
|
||||||
Type::SHORT | Type::SSHORT => ByteOrderClass::Two,
|
Type::SHORT | Type::SSHORT => EndianBytes::Two,
|
||||||
Type::LONG
|
Type::LONG
|
||||||
| Type::SLONG
|
| Type::SLONG
|
||||||
| Type::FLOAT
|
| Type::FLOAT
|
||||||
| Type::IFD
|
| Type::IFD
|
||||||
| Type::RATIONAL
|
| Type::RATIONAL
|
||||||
| Type::SRATIONAL => ByteOrderClass::Four,
|
| Type::SRATIONAL => EndianBytes::Four,
|
||||||
Type::LONG8 | Type::SLONG8 | Type::DOUBLE | Type::IFD8 => ByteOrderClass::Eight,
|
Type::LONG8 | Type::SLONG8 | Type::DOUBLE | Type::IFD8 => EndianBytes::Eight,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -338,33 +338,37 @@ impl ByteOrder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Given a typed buffer, convert its contents to the specified byte order in-place.
|
/// Given a typed buffer, convert its contents to the specified byte order in-place.
|
||||||
|
///
|
||||||
|
/// The buffer is assumed to represent an array of the given type. If the length of the buffer
|
||||||
|
/// is not divisible into an integer number of values, the behavior for the remaining bytes it
|
||||||
|
/// not specified.
|
||||||
pub fn convert(self, ty: Type, buffer: &mut [u8], to: ByteOrder) {
|
pub fn convert(self, ty: Type, buffer: &mut [u8], to: ByteOrder) {
|
||||||
self.convert_class(ty.byte_order_class(), buffer, to)
|
self.convert_endian_bytes(ty.endian_bytes(), buffer, to)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn convert_class(self, cls: ByteOrderClass, buffer: &mut [u8], to: ByteOrder) {
|
pub(crate) fn convert_endian_bytes(self, cls: EndianBytes, buffer: &mut [u8], to: ByteOrder) {
|
||||||
if self == to {
|
if self == to {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: at MSRV 1.89 or higher use `slice::as_chunks_mut`.
|
// FIXME: at MSRV 1.89 or higher use `slice::as_chunks_mut`.
|
||||||
match cls {
|
match cls {
|
||||||
ByteOrderClass::One => {
|
EndianBytes::One => {
|
||||||
// No change needed
|
// No change needed
|
||||||
}
|
}
|
||||||
ByteOrderClass::Two => {
|
EndianBytes::Two => {
|
||||||
for chunk in buffer.chunks_exact_mut(2) {
|
for chunk in buffer.chunks_exact_mut(2) {
|
||||||
let chunk: &mut [u8; 2] = chunk.try_into().unwrap();
|
let chunk: &mut [u8; 2] = chunk.try_into().unwrap();
|
||||||
*chunk = u16::from_be_bytes(*chunk).to_le_bytes();
|
*chunk = u16::from_be_bytes(*chunk).to_le_bytes();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ByteOrderClass::Four => {
|
EndianBytes::Four => {
|
||||||
for chunk in buffer.chunks_exact_mut(4) {
|
for chunk in buffer.chunks_exact_mut(4) {
|
||||||
let chunk: &mut [u8; 4] = chunk.try_into().unwrap();
|
let chunk: &mut [u8; 4] = chunk.try_into().unwrap();
|
||||||
*chunk = u32::from_be_bytes(*chunk).to_le_bytes();
|
*chunk = u32::from_be_bytes(*chunk).to_le_bytes();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ByteOrderClass::Eight => {
|
EndianBytes::Eight => {
|
||||||
for chunk in buffer.chunks_exact_mut(8) {
|
for chunk in buffer.chunks_exact_mut(8) {
|
||||||
let chunk: &mut [u8; 8] = chunk.try_into().unwrap();
|
let chunk: &mut [u8; 8] = chunk.try_into().unwrap();
|
||||||
*chunk = u64::from_be_bytes(*chunk).to_le_bytes();
|
*chunk = u64::from_be_bytes(*chunk).to_le_bytes();
|
||||||
@@ -376,7 +380,7 @@ impl ByteOrder {
|
|||||||
|
|
||||||
/// The size of individual byte-order corrected elements.
|
/// The size of individual byte-order corrected elements.
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub(crate) enum ByteOrderClass {
|
pub(crate) enum EndianBytes {
|
||||||
One,
|
One,
|
||||||
Two,
|
Two,
|
||||||
Four,
|
Four,
|
||||||
|
|||||||
Reference in new issue
Block a user