nihav_core/deflate: fix handling large buffers
authorKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 27 Jun 2020 12:26:31 +0000 (14:26 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 27 Jun 2020 12:26:31 +0000 (14:26 +0200)
nihav-core/src/compr/deflate.rs

index fa318a473e84647e9ca281cfa6fc868c243c812a..da37fde37df255faa698b82bd66597a8347f8d05 100644 (file)
@@ -320,19 +320,19 @@ impl Inflate {
     }
     fn put_literal(&mut self, val: u8) {
         self.buf[self.bpos] = val;
-        self.bpos += 1;
+        self.bpos = (self.bpos + 1) & (self.buf.len() - 1);
     }
     fn lz_copy(&mut self, offset: usize, len: usize, dst: &mut [u8]) -> DecompressResult<()> {
         let mask = self.buf.len() - 1;
-        if self.bpos < offset {
+        if offset > self.output_idx {
             return Err(DecompressError::InvalidData);
         }
-        let cstart = (self.bpos - offset) & mask;
+        let cstart = (self.bpos.wrapping_sub(offset)) & mask;
         for i in 0..len {
             self.buf[(self.bpos + i) & mask] = self.buf[(cstart + i) & mask];
             dst[i] = self.buf[(cstart + i) & mask];
         }
-        self.bpos += len;
+        self.bpos = (self.bpos + len) & mask;
         Ok(())
     }
     ///! Reports whether decoder has finished decoding the input.