aboutsummaryrefslogtreecommitdiff
path: root/src/bitio
diff options
context:
space:
mode:
Diffstat (limited to 'src/bitio')
-rw-r--r--src/bitio/bitreader.nim57
-rw-r--r--src/bitio/bitwriter.nim59
2 files changed, 116 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
17import streams
18import ../integers
19
20type BitReader* = ref object
21 stream: Stream
22 bitOffset: int
23 overflowBuffer: uint8
24
25proc bitReader*(stream: Stream): BitReader =
26 BitReader(stream: stream, bitOffset: 0, overflowBuffer: 0)
27
28proc atEnd*(bitReader: BitReader): bool =
29 bitReader.bitOffset == 0 and bitReader.stream.atEnd()
30
31proc 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
48proc readBool*(bitReader: BitReader): bool =
49 bitReader.readBits(1, uint8) != 0
50
51proc 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))
diff --git a/src/bitio/bitwriter.nim b/src/bitio/bitwriter.nim
new file mode 100644
index 0000000..f1b44ca
--- /dev/null
+++ b/src/bitio/bitwriter.nim
@@ -0,0 +1,59 @@
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
17import streams
18import ../integers
19
20type BitWriter* = ref object
21 stream: Stream
22 bitOffset: int
23 writeBuffer: uint8
24
25proc bitWriter*(stream: Stream): BitWriter =
26 BitWriter(stream: stream, bitOffset: 0, writeBuffer: 0)
27
28proc flush*(bitWriter: BitWriter) =
29 if bitWriter.bitOffset == 0: return
30 bitWriter.stream.write(bitWriter.writeBuffer)
31 bitWriter.stream.flush()
32 (bitWriter.bitOffset, bitWriter.writeBuffer) = (0, 0'u8)
33
34proc atEnd*(bitWriter: BitWriter): bool =
35 bitWriter.stream.atEnd()
36
37proc writeBits*(bitWriter: BitWriter, bits: int, value: SomeUnsignedInt) =
38 let valueContainerBitLength = sizeof(value) * wordBitLength
39 if bits < 0 or bits > valueContainerBitLength:
40 raise newException(RangeError, "invalid bit length")
41 var bitsToWrite = bits
42 if bitsToWrite + bitWriter.bitOffset >= wordBitLength:
43 bitWriter.stream.write(truncateToUint8(value shl bitWriter.bitOffset) or bitWriter.writeBuffer)
44 bitsToWrite -= wordBitLength - bitWriter.bitOffset
45 (bitWriter.bitOffset, bitWriter.writeBuffer) = (0, 0'u8)
46 while bitsToWrite >= wordBitLength:
47 bitWriter.stream.write(truncateToUint8(value shr (bits - bitsToWrite)))
48 bitsToWrite -= wordBitLength
49 if bitsToWrite > 0:
50 let left = truncateToUint8((value shl (valueContainerBitLength - bits)) shr (valueContainerBitLength - bitsToWrite))
51 bitWriter.writeBuffer = (left shl bitWriter.bitOffset) or bitWriter.writeBuffer
52 bitWriter.bitOffset = (bitWriter.bitOffset + bitsToWrite) mod wordBitLength
53
54proc writeBool*(bitWriter: BitWriter, value: bool) =
55 bitWriter.writeBits(1, value.uint8)
56
57proc writeSeq*[T: SomeUnsignedInt](bitWriter: BitWriter, bitLength: int, data: seq[T]) =
58 for i, chunkBitLength in chunks(bitLength, T):
59 bitWriter.writeBits(chunkBitLength, data[i])