From d353e8312b59818cdae5771549c92c1dc6427c71 Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 25 Nov 2018 22:34:31 +0100 Subject: fix bit sequence read at end of stream --- src/bitreader.nim | 11 ++++++----- src/rawblock.nim | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'src') 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 proc readBool*(bitReader: BitReader): bool = bitReader.readBits(1, uint8) != 0 -proc readSeq*[T: SomeUnsignedInt](bitReader: BitReader, bitLength: int, to: typedesc[T]): tuple[bitLength: int, data: seq[T]] = - result = (0, newSeqOfCap[T](bitLength /^ (sizeof(T) * wordBitLength))) - for _, chunkBitLength in chunks(bitLength, T): +proc readSeq*[T: SomeUnsignedInt](bitReader: BitReader, maxBitLength: int, to: typedesc[T]): tuple[bitLength: int, data: seq[T]] = + result = (0, newSeqOfCap[T](maxBitLength /^ (sizeof(T) * wordBitLength))) + for _, chunkBitLength in chunks(maxBitLength, T): if bitReader.atEnd(): return - result.bitLength += chunkBitLength - result.data.add(bitReader.readBits(chunkBitLength, T)) + let bitsToRead = if bitReader.stream.atEnd(): sizeof(T) * wordBitLength - bitReader.bitOffset else: chunkBitLength + result.bitLength += bitsToRead + 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 @@ import integers, bitreader, bitwriter -const maxDataBitLength = high(uint16).int - 1 +const maxDataBitLength = high(uint16).int const bitLengthFieldBitLength = 2 * wordBitLength type RawBlock* = object -- cgit v1.2.3