diff options
Diffstat (limited to 'src/bitio/bitreader.nim')
-rw-r--r-- | src/bitio/bitreader.nim | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/bitio/bitreader.nim b/src/bitio/bitreader.nim new file mode 100644 index 0000000..e4ad225 --- /dev/null +++ b/src/bitio/bitreader.nim | |||
@@ -0,0 +1,57 @@ | |||
1 | # gzip-like LZSS compressor | ||
2 | # Copyright (C) 2018 Pacien TRAN-GIRARD | ||
3 | # | ||
4 | # This program is free software: you can redistribute it and/or modify | ||
5 | # it under the terms of the GNU Affero General Public License as | ||
6 | # published by the Free Software Foundation, either version 3 of the | ||
7 | # License, or (at your option) any later version. | ||
8 | # | ||
9 | # This program is distributed in the hope that it will be useful, | ||
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | # GNU Affero General Public License for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU Affero General Public License | ||
15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
16 | |||
17 | import streams | ||
18 | import ../integers | ||
19 | |||
20 | type BitReader* = ref object | ||
21 | stream: Stream | ||
22 | bitOffset: int | ||
23 | overflowBuffer: uint8 | ||
24 | |||
25 | proc bitReader*(stream: Stream): BitReader = | ||
26 | BitReader(stream: stream, bitOffset: 0, overflowBuffer: 0) | ||
27 | |||
28 | proc atEnd*(bitReader: BitReader): bool = | ||
29 | bitReader.bitOffset == 0 and bitReader.stream.atEnd() | ||
30 | |||
31 | proc readBits*[T: SomeUnsignedInt](bitReader: BitReader, bits: int, to: typedesc[T]): T = | ||
32 | if bits < 0 or bits > sizeof(T) * wordBitLength: raise newException(RangeError, "invalid bit length") | ||
33 | if bits == 0: return 0 | ||
34 | var bitsRead = 0 | ||
35 | if bitReader.bitOffset > 0: | ||
36 | let bitsFromBuffer = min(bits, wordBitLength - bitReader.bitOffset) | ||
37 | result = (bitReader.overflowBuffer shr bitReader.bitOffset).leastSignificantBits(bitsFromBuffer) | ||
38 | bitReader.bitOffset = (bitReader.bitOffset + bitsFromBuffer) mod wordBitLength | ||
39 | bitsRead += bitsFromBuffer | ||
40 | while bits - bitsRead >= wordBitLength: | ||
41 | result = result or (bitReader.stream.readUint8().T shl bitsRead) | ||
42 | bitsRead += wordBitLength | ||
43 | if bits - bitsRead > 0: | ||
44 | bitReader.overflowBuffer = bitReader.stream.readUint8() | ||
45 | bitReader.bitOffset = bits - bitsRead | ||
46 | result = result or (bitReader.overflowBuffer.leastSignificantBits(bitReader.bitOffset).T shl bitsRead) | ||
47 | |||
48 | proc readBool*(bitReader: BitReader): bool = | ||
49 | bitReader.readBits(1, uint8) != 0 | ||
50 | |||
51 | proc readSeq*[T: SomeUnsignedInt](bitReader: BitReader, maxBitLength: int, to: typedesc[T]): tuple[bitLength: int, data: seq[T]] = | ||
52 | result = (0, newSeqOfCap[T](maxBitLength /^ (sizeof(T) * wordBitLength))) | ||
53 | for _, chunkBitLength in chunks(maxBitLength, T): | ||
54 | if bitReader.atEnd(): return | ||
55 | let bitsToRead = if bitReader.stream.atEnd(): sizeof(T) * wordBitLength - bitReader.bitOffset else: chunkBitLength | ||
56 | result.bitLength += bitsToRead | ||
57 | result.data.add(bitReader.readBits(bitsToRead, T)) | ||