diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bitreader.nim | 72 | ||||
-rw-r--r-- | src/bitstream.nim | 112 | ||||
-rw-r--r-- | src/bitwriter.nim | 59 | ||||
-rw-r--r-- | src/integers.nim | 7 | ||||
-rw-r--r-- | src/lzssblock.nim | 10 | ||||
-rw-r--r-- | src/rawblock.nim | 22 | ||||
-rw-r--r-- | src/streamblock.nim | 37 |
7 files changed, 173 insertions, 146 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)) | ||
diff --git a/src/bitstream.nim b/src/bitstream.nim deleted file mode 100644 index 81401ce..0000000 --- a/src/bitstream.nim +++ /dev/null | |||
@@ -1,112 +0,0 @@ | |||
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/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 | ||