1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 16:44:06 +02:00

fix reading offset string in big endian

This commit is contained in:
Robin Appelman 2021-07-12 20:04:49 +02:00
commit dddb91fd6b
2 changed files with 62 additions and 36 deletions

View file

@ -521,22 +521,35 @@ where
let mut data = Vec::with_capacity(byte_count); let mut data = Vec::with_capacity(byte_count);
let mut byte_left = byte_count; let mut byte_left = byte_count;
let mut read_pos = position / 8; let mut read_pos = position / 8;
while byte_left > USIZE_SIZE - 1 {
let bytes = self if E::is_le() {
.read_shifted_usize(read_pos, shift, false) while byte_left > USIZE_SIZE - 1 {
.to_le_bytes(); let raw = self.read_shifted_usize(read_pos, shift, false);
let read_bytes = USIZE_SIZE - 1; let bytes = if E::is_le() {
let usable_bytes = &bytes[0..read_bytes]; raw.to_le_bytes()
} else {
raw.to_be_bytes()
};
let read_bytes = USIZE_SIZE - 1;
let usable_bytes = &bytes[0..read_bytes];
data.extend_from_slice(usable_bytes);
read_pos += read_bytes;
byte_left -= read_bytes;
}
let bytes = self.read_shifted_usize(read_pos, shift, true).to_le_bytes();
let usable_bytes = &bytes[0..byte_left];
data.extend_from_slice(usable_bytes); data.extend_from_slice(usable_bytes);
} else {
read_pos += read_bytes; let mut pos = position;
byte_left -= read_bytes; while byte_left > 0 {
data.push(self.read_int_unchecked::<u8>(pos, 8, true));
byte_left -= 1;
pos += 8;
}
} }
let bytes = self.read_shifted_usize(read_pos, shift, true).to_le_bytes();
let usable_bytes = &bytes[0..byte_left];
data.extend_from_slice(usable_bytes);
Cow::Owned(data) Cow::Owned(data)
} }
@ -626,32 +639,45 @@ where
)) ))
} else { } else {
let mut acc = Vec::with_capacity(32); let mut acc = Vec::with_capacity(32);
let mut byte_index = position / 8; if E::is_le() {
loop { let mut byte_index = position / 8;
// note: if less then a usize worth of data is left in the buffer, read_usize_bytes loop {
// will automatically pad with null bytes, triggering the loop termination // note: if less then a usize worth of data is left in the buffer, read_usize_bytes
// thus no separate logic for dealing with the end of the bytes is required // will automatically pad with null bytes, triggering the loop termination
// // thus no separate logic for dealing with the end of the bytes is required
// This is safe because the final usize is filled with 0's, thus triggering the exit clause //
// before reading any out of bounds // This is safe because the final usize is filled with 0's, thus triggering the exit clause
let shifted = unsafe { self.read_shifted_usize(byte_index, shift, true) }; // before reading any out of bounds
let shifted = unsafe { self.read_shifted_usize(byte_index, shift, true) };
let has_null = contains_zero_byte_non_top(shifted); let has_null = contains_zero_byte_non_top(shifted);
let bytes: [u8; USIZE_SIZE] = shifted.to_le_bytes(); let bytes: [u8; USIZE_SIZE] = shifted.to_le_bytes();
let usable_bytes = &bytes[0..USIZE_SIZE - 1]; let usable_bytes = &bytes[0..USIZE_SIZE - 1];
if has_null { if has_null {
for i in 0..USIZE_SIZE - 1 { for i in 0..USIZE_SIZE - 1 {
if usable_bytes[i] == 0 { if usable_bytes[i] == 0 {
acc.extend_from_slice(&usable_bytes[0..i]); acc.extend_from_slice(&usable_bytes[0..i]);
return Ok(Cow::Owned(acc)); return Ok(Cow::Owned(acc));
}
} }
} }
acc.extend_from_slice(&usable_bytes[0..USIZE_SIZE - 1]);
byte_index += USIZE_SIZE - 1;
}
} else {
let mut pos = position;
loop {
let byte = self.read_int::<u8>(pos, 8)?;
pos += 8;
if byte == 0 {
return Ok(Cow::Owned(acc));
} else {
acc.push(byte);
}
} }
acc.extend_from_slice(&usable_bytes[0..USIZE_SIZE - 1]);
byte_index += USIZE_SIZE - 1;
} }
} }
} }

View file

@ -119,7 +119,7 @@ where
.extend_from_slice(&merged.to_le_bytes()[0..merged_byte_count]); .extend_from_slice(&merged.to_le_bytes()[0..merged_byte_count]);
} else { } else {
let merged = ((last_written_byte as usize) << (USIZE_BITS - 8)) let merged = ((last_written_byte as usize) << (USIZE_BITS - 8))
| bits << (USIZE_BITS - bit_offset - count); | (bits << (USIZE_BITS - bit_offset - count));
self.bytes self.bytes
.extend_from_slice(&merged.to_be_bytes()[0..merged_byte_count]); .extend_from_slice(&merged.to_be_bytes()[0..merged_byte_count]);
} }