diff options
-rw-r--r-- | src/bitstream.nim | 112 | ||||
-rw-r--r-- | src/integers.nim | 24 | ||||
-rw-r--r-- | tests/tbitstream.nim | 131 | ||||
-rw-r--r-- | tests/tintegers.nim | 28 |
4 files changed, 295 insertions, 0 deletions
diff --git a/src/bitstream.nim b/src/bitstream.nim new file mode 100644 index 0000000..81401ce --- /dev/null +++ b/src/bitstream.nim | |||
@@ -0,0 +1,112 @@ | |||
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 | # Utils | ||
35 | |||
36 | iterator chunks*(totalBitLength: int, chunkType: typedesc[SomeInteger]): tuple[index: int, chunkBitLength: int] = | ||
37 | let chunkBitLength = sizeof(chunkType) * wordBitLength | ||
38 | let wordCount = totalBitLength div chunkBitLength | ||
39 | for i in 0..<(wordCount): yield (i, chunkBitLength) | ||
40 | let remainder = totalBitLength mod chunkBitLength | ||
41 | if remainder > 0: yield (wordCount, remainder) | ||
42 | |||
43 | # BitStream | ||
44 | |||
45 | type BitStream* = ref object | ||
46 | stream: Stream | ||
47 | bitOffset: int | ||
48 | writeBuffer: uint8 | ||
49 | |||
50 | proc bitStream*(stream: Stream): BitStream = | ||
51 | BitStream(stream: stream, bitOffset: 0, writeBuffer: 0) | ||
52 | |||
53 | proc flush*(bitStream: BitStream) = | ||
54 | if bitStream.bitOffset == 0: return | ||
55 | bitStream.stream.write(bitStream.writeBuffer) | ||
56 | bitStream.stream.flush() | ||
57 | (bitStream.bitOffset, bitStream.writeBuffer) = (0, 0'u8) | ||
58 | |||
59 | proc atEnd*(bitStream: BitStream): bool = | ||
60 | bitStream.stream.atEnd() | ||
61 | |||
62 | proc readBits*[T: SomeUnsignedInt](bitStream: BitStream, bits: int, to: typedesc[T]): T = | ||
63 | let targetBitLength = sizeof(T) * wordBitLength | ||
64 | if bits < 0 or bits > targetBitLength: | ||
65 | raise newException(RangeError, "invalid bit length") | ||
66 | elif bits == 0: | ||
67 | result = 0 | ||
68 | elif bits < targetBitLength - bitStream.bitOffset: | ||
69 | result = bitStream.stream.peek(T) shl (targetBitLength - bits - bitStream.bitOffset) shr (targetBitLength - bits) | ||
70 | elif bits == targetBitLength - bitStream.bitOffset: | ||
71 | result = bitStream.stream.read(T) shl (targetBitLength - bits - bitStream.bitOffset) shr (targetBitLength - bits) | ||
72 | else: | ||
73 | let rightBits = targetBitLength - bitStream.bitOffset | ||
74 | let leftBits = bits - rightBits | ||
75 | let right = bitStream.stream.read(T) shr bitStream.bitOffset | ||
76 | let left = bitStream.stream.peek(T) shl (targetBitLength - leftBits) shr (targetBitLength - bits) | ||
77 | result = left or right | ||
78 | bitStream.bitOffset = (bitStream.bitOffset + bits) mod wordBitLength | ||
79 | |||
80 | proc readBool*(bitStream: BitStream): bool = | ||
81 | bitStream.readBits(1, uint8) != 0 | ||
82 | |||
83 | proc readSeq*[T: SomeUnsignedInt](bitStream: BitStream, bitLength: int, to: typedesc[T]): tuple[bitLength: int, data: seq[T]] = | ||
84 | result = (0, newSeqOfCap[T](bitLength /^ (sizeof(T) * wordBitLength))) | ||
85 | for _, chunkBitLength in chunks(bitLength, T): | ||
86 | if bitStream.atEnd(): return | ||
87 | result.bitLength += chunkBitLength | ||
88 | result.data.add(bitStream.readBits(chunkBitLength, T)) | ||
89 | |||
90 | proc writeBits*(bitStream: BitStream, bits: int, value: SomeUnsignedInt) = | ||
91 | let valueContainerBitLength = sizeof(value) * wordBitLength | ||
92 | if bits < 0 or bits > valueContainerBitLength: | ||
93 | raise newException(RangeError, "invalid bit length") | ||
94 | var bitsToWrite = bits | ||
95 | if bitsToWrite + bitStream.bitOffset >= wordBitLength: | ||
96 | bitStream.stream.write(truncateToUint8(value shl bitStream.bitOffset) or bitStream.writeBuffer) | ||
97 | bitsToWrite -= wordBitLength - bitStream.bitOffset | ||
98 | (bitStream.bitOffset, bitStream.writeBuffer) = (0, 0'u8) | ||
99 | while bitsToWrite >= wordBitLength: | ||
100 | bitStream.stream.write(truncateToUint8(value shr (bits - bitsToWrite))) | ||
101 | bitsToWrite -= wordBitLength | ||
102 | if bitsToWrite > 0: | ||
103 | let left = truncateToUint8((value shl (valueContainerBitLength - bits)) shr (valueContainerBitLength - bitsToWrite)) | ||
104 | bitStream.writeBuffer = (left shl bitStream.bitOffset) or bitStream.writeBuffer | ||
105 | bitStream.bitOffset = (bitStream.bitOffset + bitsToWrite) mod wordBitLength | ||
106 | |||
107 | proc writeBool*(bitStream: BitStream, value: bool) = | ||
108 | bitStream.writeBits(1, value.uint8) | ||
109 | |||
110 | proc writeSeq*[T: SomeUnsignedInt](bitStream: BitStream, bitLength: int, data: seq[T]) = | ||
111 | for i, chunkBitLength in chunks(bitLength, T): | ||
112 | bitStream.writeBits(chunkBitLength, data[i]) | ||
diff --git a/src/integers.nim b/src/integers.nim new file mode 100644 index 0000000..1b9121c --- /dev/null +++ b/src/integers.nim | |||
@@ -0,0 +1,24 @@ | |||
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 | const wordBitLength* = 8 | ||
18 | const wordBitMask* = 0b1111_1111'u8 | ||
19 | |||
20 | proc `/^`*[T: Natural](x, y: T): T = | ||
21 | (x + y - 1) div y | ||
22 | |||
23 | proc truncateToUint8*(x: SomeUnsignedInt): uint8 = | ||
24 | (x and wordBitMask).uint8 | ||
diff --git a/tests/tbitstream.nim b/tests/tbitstream.nim new file mode 100644 index 0000000..2292049 --- /dev/null +++ b/tests/tbitstream.nim | |||
@@ -0,0 +1,131 @@ | |||
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 unittest, streams, sugar, sequtils | ||
18 | import integers, bitstream | ||
19 | |||
20 | suite "bitstream": | ||
21 | test "chunks iterator": | ||
22 | check toSeq(chunks(70, uint32)) == @[(0, 32), (1, 32), (2, 6)] | ||
23 | check toSeq(chunks(32, uint16)) == @[(0, 16), (1, 16)] | ||
24 | |||
25 | test "flush": | ||
26 | let stream = newStringStream() | ||
27 | defer: stream.close() | ||
28 | let bitStream = stream.bitStream() | ||
29 | |||
30 | bitStream.writeBool(true) | ||
31 | stream.setPosition(0) | ||
32 | expect IOError: discard stream.peekUint8() | ||
33 | |||
34 | bitStream.flush() | ||
35 | stream.setPosition(0) | ||
36 | check stream.readUint8() == 0x01'u8 | ||
37 | check stream.atEnd() | ||
38 | |||
39 | bitStream.flush() | ||
40 | check stream.atEnd() | ||
41 | |||
42 | test "readBool": | ||
43 | let stream = newStringStream() | ||
44 | defer: stream.close() | ||
45 | stream.write(0b1001_1111'u8) | ||
46 | stream.write(0b0110_0000'u8) | ||
47 | stream.setPosition(0) | ||
48 | |||
49 | let bitStream = stream.bitStream() | ||
50 | check lc[bitStream.readBool() | (_ <- 0..<16), bool] == @[ | ||
51 | true, true, true, true, true, false, false, true, | ||
52 | false, false, false, false, false, true, true, false] | ||
53 | |||
54 | expect IOError: discard bitStream.readBool() | ||
55 | check bitStream.atEnd() | ||
56 | |||
57 | test "readBits": | ||
58 | let stream = newStringStream() | ||
59 | defer: stream.close() | ||
60 | stream.write(0xF00F'u16) | ||
61 | stream.write(0x0FFF'u16) | ||
62 | stream.setPosition(0) | ||
63 | |||
64 | let bitStream = stream.bitStream() | ||
65 | check bitStream.readBits(8, uint8) == 0x0F'u8 | ||
66 | check bitStream.readBits(16, uint16) == 0xFFF0'u16 | ||
67 | check bitStream.readBits(8, uint8) == 0x0F'u8 | ||
68 | |||
69 | expect RangeError: discard bitStream.readBits(9, uint8) | ||
70 | expect IOError: discard bitStream.readBits(16, uint16) | ||
71 | check bitStream.atEnd() | ||
72 | |||
73 | test "readSeq": | ||
74 | let stream = newStringStream() | ||
75 | defer: stream.close() | ||
76 | stream.write(0x0F00_F0FF_F0F0_F0F0'u64) | ||
77 | stream.setPosition(0) | ||
78 | |||
79 | let bitStream = stream.bitStream() | ||
80 | check bitStream.readSeq(32, uint16) == (32, @[0xF0F0'u16, 0xF0F0]) | ||
81 | check bitStream.readSeq(40, uint8) == (32, @[0xFF'u8, 0xF0, 0x00, 0x0F]) | ||
82 | check bitStream.atEnd() | ||
83 | |||
84 | test "writeBool": | ||
85 | let stream = newStringStream() | ||
86 | defer: stream.close() | ||
87 | |||
88 | let bitStream = stream.bitStream() | ||
89 | let booleanValues = @[ | ||
90 | true, true, true, true, true, false, false, true, | ||
91 | false, false, false, false, false, true, true, false, | ||
92 | true, true, false, true] | ||
93 | for b in booleanValues: bitStream.writeBool(b) | ||
94 | bitStream.flush() | ||
95 | |||
96 | stream.setPosition(0) | ||
97 | check stream.readUint8() == 0b1001_1111'u8 | ||
98 | check stream.readUint8() == 0b0110_0000'u8 | ||
99 | check stream.readUint8() == 0b0000_1011'u8 | ||
100 | expect IOError: discard stream.readUint8() | ||
101 | check stream.atEnd() | ||
102 | |||
103 | test "writeBits": | ||
104 | let stream = newStringStream() | ||
105 | defer: stream.close() | ||
106 | |||
107 | let bitStream = stream.bitStream() | ||