From ef81427b6241894eda58f3458883b56c0c848a36 Mon Sep 17 00:00:00 2001 From: zypeh Date: Thu, 25 Aug 2022 21:13:29 +0800 Subject: [PATCH] Fixed bug if byte array exceed 255 bytes --- crates/flat/src/encode/encoder.rs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/crates/flat/src/encode/encoder.rs b/crates/flat/src/encode/encoder.rs index 43773817..65fad039 100644 --- a/crates/flat/src/encode/encoder.rs +++ b/crates/flat/src/encode/encoder.rs @@ -80,7 +80,7 @@ impl Encoder { return Err(Error::BufferNotByteAligned); } - self.write_blk(arr, &mut 0); + self.write_blk(arr); Ok(self) } @@ -277,20 +277,13 @@ impl Encoder { /// Following that it writes the next 255 bytes from the array. /// After reaching the end of the buffer we write a 0 byte. Only write 0 if the byte array is empty. /// This is byte alignment agnostic. - fn write_blk(&mut self, arr: &[u8], src_ptr: &mut usize) { - loop { - let src_len = arr.len() - *src_ptr; - let blk_len = src_len.min(255); + fn write_blk(&mut self, arr: &[u8]) { + let chunks = arr.chunks(255); - self.buffer.push(blk_len as u8); - - if blk_len == 0 { - return; - } - - self.buffer.extend(&arr[*src_ptr..blk_len]); - - *src_ptr += blk_len; + for chunk in chunks { + self.buffer.push(chunk.len() as u8); + self.buffer.extend(chunk); } + self.buffer.push(0); } }