aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpacien2018-11-23 12:31:11 +0100
committerpacien2018-11-23 12:31:11 +0100
commit52c08b0282aacb44c2e75cefeae5626a7aa1b21a (patch)
treedcf4df132277ecf4b12ade0464cf3175f9665dd2 /src
parent18258716b4193279e806cacbf766931d9ae3ffba (diff)
downloadgziplike-52c08b0282aacb44c2e75cefeae5626a7aa1b21a.tar.gz
Implement bitstream
Diffstat (limited to 'src')
-rw-r--r--src/bitstream.nim112
-rw-r--r--src/integers.nim24
2 files changed, 136 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
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# Utils
35
36iterator 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
45type BitStream* = ref object
46 stream: Stream
47 bitOffset: int
48 writeBuffer: uint8
49
50proc bitStream*(stream: Stream): BitStream =
51 BitStream(stream: stream, bitOffset: 0, writeBuffer: 0)
52
53proc 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
59proc atEnd*(bitStream: BitStream): bool =
60 bitStream.stream.atEnd()
61
62proc 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
80proc readBool*(bitStream: BitStream): bool =
81 bitStream.readBits(1, uint8) != 0
82
83proc 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
90proc 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
107proc writeBool*(bitStream: BitStream, value: bool) =
108 bitStream.writeBits(1, value.uint8)
109
110proc 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
17const wordBitLength* = 8
18const wordBitMask* = 0b1111_1111'u8
19
20proc `/^`*[T: Natural](x, y: T): T =
21 (x + y - 1) div y
22
23proc truncateToUint8*(x: SomeUnsignedInt): uint8 =
24 (x and wordBitMask).uint8