diff options
author | pacien | 2018-11-30 16:57:32 +0100 |
---|---|---|
committer | pacien | 2018-11-30 17:03:23 +0100 |
commit | 8af38da097b8358cb273baa37c748120461c718e (patch) | |
tree | 403397acf75540e76cbed619deaaa027da33a91f /src/bitreader.nim | |
parent | b03508ea5e20370de26c6faf23bbbdd4e89ab1a9 (diff) | |
download | gziplike-8af38da097b8358cb273baa37c748120461c718e.tar.gz |
isolate bit IO
Diffstat (limited to 'src/bitreader.nim')
-rw-r--r-- | src/bitreader.nim | 57 |
1 files changed, 0 insertions, 57 deletions
diff --git a/src/bitreader.nim b/src/bitreader.nim deleted file mode 100644 index baa8bf8..0000000 --- a/src/bitreader.nim +++ /dev/null | |||
@@ -1,57 +0,0 @@ | |||
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)) | ||