diff options
Diffstat (limited to 'src/bitwriter.nim')
-rw-r--r-- | src/bitwriter.nim | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/bitwriter.nim b/src/bitwriter.nim new file mode 100644 index 0000000..aac96f9 --- /dev/null +++ b/src/bitwriter.nim | |||
@@ -0,0 +1,59 @@ | |||
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 | type BitWriter* = ref object | ||
21 | stream: Stream | ||
22 | bitOffset: int | ||
23 | writeBuffer: uint8 | ||
24 | |||
25 | proc bitWriter*(stream: Stream): BitWriter = | ||
26 | BitWriter(stream: stream, bitOffset: 0, writeBuffer: 0) | ||
27 | |||
28 | proc 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 | |||
34 | proc atEnd*(bitWriter: BitWriter): bool = | ||
35 | bitWriter.stream.atEnd() | ||
36 | |||
37 | proc 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 | |||
54 | proc writeBool*(bitWriter: BitWriter, value: bool) = | ||
55 | bitWriter.writeBits(1, value.uint8) | ||
56 | |||
57 | proc writeSeq*[T: SomeUnsignedInt](bitWriter: BitWriter, bitLength: int, data: seq[T]) = | ||
58 | for i, chunkBitLength in chunks(bitLength, T): | ||
59 | bitWriter.writeBits(chunkBitLength, data[i]) | ||