diff options
author | pacien | 2018-11-23 12:57:00 +0100 |
---|---|---|
committer | pacien | 2018-11-23 13:00:53 +0100 |
commit | d9768cd0315b0415d20818de9c897c6168c95b78 (patch) | |
tree | 41987740d02f689550436384dd7d2b755aa85869 /src/bitreader.nim | |
parent | c87ea5c5d11bffbe84acf66215e6bf5a5e047a50 (diff) | |
download | gziplike-d9768cd0315b0415d20818de9c897c6168c95b78.tar.gz |
Split bitstream into bitreader and bitwriter
Diffstat (limited to 'src/bitreader.nim')
-rw-r--r-- | src/bitreader.nim | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/bitreader.nim b/src/bitreader.nim new file mode 100644 index 0000000..35e9d57 --- /dev/null +++ b/src/bitreader.nim | |||
@@ -0,0 +1,72 @@ | |||
1 | # "à-la-gzip" 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 | # Stream functions | ||
21 | |||
22 | proc newEIO(msg: string): ref IOError = | ||
23 | new(result) | ||
24 | result.msg = msg | ||
25 | |||
26 | proc read[T](s: Stream, t: typedesc[T]): T = | ||
27 | if readData(s, addr(result), sizeof(T)) != sizeof(T): | ||
28 | raise newEIO("cannot read from stream") | ||
29 | |||
30 | proc peek[T](s: Stream, t: typedesc[T]): T = | ||
31 | if peekData(s, addr(result), sizeof(T)) != sizeof(T): | ||
32 | raise newEIO("cannot read from stream") | ||
33 | |||
34 | # BitReader | ||
35 | |||
36 | type BitReader* = ref object | ||
37 | stream: Stream | ||
38 | bitOffset: int | ||
39 | |||
40 | proc bitReader*(stream: Stream): BitReader = | ||
41 | BitReader(stream: stream, bitOffset: 0) | ||
42 | |||
43 | proc atEnd*(bitReader: BitReader): bool = | ||
44 | bitReader.stream.atEnd() | ||
45 | |||
46 | proc readBits*[T: SomeUnsignedInt](bitReader: BitReader, bits: int, to: typedesc[T]): T = | ||
47 | let targetBitLength = sizeof(T) * wordBitLength | ||
48 | if bits < 0 or bits > targetBitLength: | ||
49 | raise newException(RangeError, "invalid bit length") | ||
50 | elif bits == 0: | ||
51 | result = 0 | ||
52 | elif bits < targetBitLength - bitReader.bitOffset: | ||
53 | result = bitReader.stream.peek(T) shl (targetBitLength - bits - bitReader.bitOffset) shr (targetBitLength - bits) | ||
54 | elif bits == targetBitLength - bitReader.bitOffset: | ||
55 | result = bitReader.stream.read(T) shl (targetBitLength - bits - bitReader.bitOffset) shr (targetBitLength - bits) | ||
56 | else: | ||
57 | let rightBits = targetBitLength - bitReader.bitOffset | ||
58 | let leftBits = bits - rightBits | ||
59 | let right = bitReader.stream.read(T) shr bitReader.bitOffset | ||
60 | let left = bitReader.stream.peek(T) shl (targetBitLength - leftBits) shr (targetBitLength - bits) | ||
61 | result = left or right | ||
62 | bitReader.bitOffset = (bitReader.bitOffset + bits) mod wordBitLength | ||
63 | |||
64 | proc readBool*(bitReader: BitReader): bool = | ||
65 | bitReader.readBits(1, uint8) != 0 | ||
66 | |||
67 | proc readSeq*[T: SomeUnsignedInt](bitReader: BitReader, bitLength: int, to: typedesc[T]): tuple[bitLength: int, data: seq[T]] = | ||
68 | result = (0, newSeqOfCap[T](bitLength /^ (sizeof(T) * wordBitLength))) | ||
69 | for _, chunkBitLength in chunks(bitLength, T): | ||
70 | if bitReader.atEnd(): return | ||
71 | result.bitLength += chunkBitLength | ||
72 | result.data.add(bitReader.readBits(chunkBitLength, T)) | ||