]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Educational decoder: Clarify IO_rewind_bits
authorSean Purcell <me@seanp.xyz>
Tue, 21 Mar 2017 22:03:23 +0000 (15:03 -0700)
committerSean Purcell <me@seanp.xyz>
Tue, 21 Mar 2017 22:05:40 +0000 (15:05 -0700)
doc/educational_decoder/zstd_decompress.c

index ae4eaa81c6aec9173cc13ee223ee9df85980d3bc..5a94fc4aa0315e7f31d4c4d334eac0ee0c6c9ac9 100644 (file)
@@ -1598,11 +1598,16 @@ static inline void IO_rewind_bits(istream_t *const in, int num) {
         ERROR("Attempting to rewind stream by a negative number of bits");
     }
 
+    /* move the offset back by `num` bits */
     const int new_offset = in->bit_offset - num;
-    const i64 bytes = (new_offset - 7) / 8;
-
-    in->ptr += bytes;
-    in->len -= bytes;
+    /* determine the number of whole bytes we have to rewind, rounding up to an
+     * integer number (e.g. if `new_offset == -5`, `bytes == 1`) */
+    const i64 bytes = -(new_offset - 7) / 8;
+
+    in->ptr -= bytes;
+    in->len += bytes;
+    /* make sure the resulting `bit_offset` is positive, as mod in C does not
+     * convert numbers from negative to positive (e.g. -22 % 8 == -6) */
     in->bit_offset = ((new_offset % 8) + 8) % 8;
 }