fix: fixed edge cases to get flat encode and decode working with stress test case

This commit is contained in:
Kasey White
2022-06-08 03:33:09 -04:00
parent 21d713ece3
commit be477917f2
5 changed files with 52 additions and 45 deletions

View File

@@ -2,10 +2,11 @@ use crate::{decode::Decode, zigzag};
use super::Error;
#[derive(Debug)]
pub struct Decoder<'b> {
buffer: &'b [u8],
used_bits: i64,
pos: usize,
pub buffer: &'b [u8],
pub used_bits: i64,
pub pos: usize,
}
impl<'b> Decoder<'b> {
@@ -71,7 +72,7 @@ impl<'b> Decoder<'b> {
let mut final_word: usize = 0;
let mut shl: usize = 0;
// continue looping if lead bit is 1 otherwise exit
while leading_bit == 1 {
while leading_bit > 0 {
let word8 = self.bits8(8)?;
let word7 = word8 & 127;
final_word |= (word7 as usize) << shl;
@@ -134,6 +135,7 @@ impl<'b> Decoder<'b> {
blk_len = self.buffer[self.pos];
}
self.pos += 1;
Ok(blk_array)
}

View File

@@ -102,16 +102,17 @@ impl Encoder {
}
pub fn word(&mut self, c: usize) -> &mut Self {
let mut d = c;
loop {
let mut w = (c & 127) as u8;
let c = c >> 7;
let mut w = (d & 127) as u8;
d >>= 7;
if c != 0 {
if d != 0 {
w |= 128;
}
self.bits(8, w);
if c == 0 {
if d == 0 {
break;
}
}