aboutsummaryrefslogtreecommitdiff
path: root/src/bitreader.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/bitreader.nim')
-rw-r--r--src/bitreader.nim72
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
17import streams
18import integers
19
20# Stream functions
21
22proc newEIO(msg: string): ref IOError =
23 new(result)
24 result.msg = msg
25
26proc 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
30proc 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
36type BitReader* = ref object
37 stream: Stream
38 bitOffset: int
39
40proc bitReader*(stream: Stream): BitReader =
41 BitReader(stream: stream, bitOffset: 0)
42
43proc atEnd*(bitReader: BitReader): bool =
44 bitReader.stream.atEnd()
45
46proc 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
64proc readBool*(bitReader: BitReader): bool =
65 bitReader.readBits(1, uint8) != 0
66
67proc 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))