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,10 +521,15 @@ 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;
if E::is_le() {
while byte_left > USIZE_SIZE - 1 { while byte_left > USIZE_SIZE - 1 {
let bytes = self let raw = self.read_shifted_usize(read_pos, shift, false);
.read_shifted_usize(read_pos, shift, false) let bytes = if E::is_le() {
.to_le_bytes(); raw.to_le_bytes()
} else {
raw.to_be_bytes()
};
let read_bytes = USIZE_SIZE - 1; let read_bytes = USIZE_SIZE - 1;
let usable_bytes = &bytes[0..read_bytes]; let usable_bytes = &bytes[0..read_bytes];
data.extend_from_slice(usable_bytes); data.extend_from_slice(usable_bytes);
@ -536,6 +541,14 @@ where
let bytes = self.read_shifted_usize(read_pos, shift, true).to_le_bytes(); let bytes = self.read_shifted_usize(read_pos, shift, true).to_le_bytes();
let usable_bytes = &bytes[0..byte_left]; let usable_bytes = &bytes[0..byte_left];
data.extend_from_slice(usable_bytes); data.extend_from_slice(usable_bytes);
} else {
let mut pos = position;
while byte_left > 0 {
data.push(self.read_int_unchecked::<u8>(pos, 8, true));
byte_left -= 1;
pos += 8;
}
}
Cow::Owned(data) Cow::Owned(data)
} }
@ -626,6 +639,7 @@ where
)) ))
} else { } else {
let mut acc = Vec::with_capacity(32); let mut acc = Vec::with_capacity(32);
if E::is_le() {
let mut byte_index = position / 8; let mut byte_index = position / 8;
loop { loop {
// note: if less then a usize worth of data is left in the buffer, read_usize_bytes // note: if less then a usize worth of data is left in the buffer, read_usize_bytes
@ -653,6 +667,18 @@ where
byte_index += 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);
}
}
}
} }
} }

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]);
} }