diff options
author | pacien | 2018-11-25 22:34:31 +0100 |
---|---|---|
committer | pacien | 2018-11-25 22:46:42 +0100 |
commit | d353e8312b59818cdae5771549c92c1dc6427c71 (patch) | |
tree | 78d997e3a107810eccd946020fc30bfd11dfb665 | |
parent | 8328b00bbe8660d9265de10daf3c840001260ef4 (diff) | |
download | gziplike-d353e8312b59818cdae5771549c92c1dc6427c71.tar.gz |
fix bit sequence read at end of stream
-rw-r--r-- | src/bitreader.nim | 11 | ||||
-rw-r--r-- | src/rawblock.nim | 2 |
2 files changed, 7 insertions, 6 deletions
diff --git a/src/bitreader.nim b/src/bitreader.nim index 7afb13d..baa8bf8 100644 --- a/src/bitreader.nim +++ b/src/bitreader.nim | |||
@@ -48,9 +48,10 @@ proc readBits*[T: SomeUnsignedInt](bitReader: BitReader, bits: int, to: typedesc | |||
48 | proc readBool*(bitReader: BitReader): bool = | 48 | proc readBool*(bitReader: BitReader): bool = |
49 | bitReader.readBits(1, uint8) != 0 | 49 | bitReader.readBits(1, uint8) != 0 |
50 | 50 | ||
51 | proc readSeq*[T: SomeUnsignedInt](bitReader: BitReader, bitLength: int, to: typedesc[T]): tuple[bitLength: int, data: seq[T]] = | 51 | proc readSeq*[T: SomeUnsignedInt](bitReader: BitReader, maxBitLength: int, to: typedesc[T]): tuple[bitLength: int, data: seq[T]] = |
52 | result = (0, newSeqOfCap[T](bitLength /^ (sizeof(T) * wordBitLength))) | 52 | result = (0, newSeqOfCap[T](maxBitLength /^ (sizeof(T) * wordBitLength))) |
53 | for _, chunkBitLength in chunks(bitLength, T): | 53 | for _, chunkBitLength in chunks(maxBitLength, T): |
54 | if bitReader.atEnd(): return | 54 | if bitReader.atEnd(): return |
55 | result.bitLength += chunkBitLength | 55 | let bitsToRead = if bitReader.stream.atEnd(): sizeof(T) * wordBitLength - bitReader.bitOffset else: chunkBitLength |
56 | result.data.add(bitReader.readBits(chunkBitLength, T)) | 56 | result.bitLength += bitsToRead |
57 | result.data.add(bitReader.readBits(bitsToRead, T)) | ||
diff --git a/src/rawblock.nim b/src/rawblock.nim index 0a44550..4a83b1d 100644 --- a/src/rawblock.nim +++ b/src/rawblock.nim | |||
@@ -16,7 +16,7 @@ | |||
16 | 16 | ||
17 | import integers, bitreader, bitwriter | 17 | import integers, bitreader, bitwriter |
18 | 18 | ||
19 | const maxDataBitLength = high(uint16).int - 1 | 19 | const maxDataBitLength = high(uint16).int |
20 | const bitLengthFieldBitLength = 2 * wordBitLength | 20 | const bitLengthFieldBitLength = 2 * wordBitLength |
21 | 21 | ||
22 | type RawBlock* = object | 22 | type RawBlock* = object |