aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpacien2018-11-23 12:57:00 +0100
committerpacien2018-11-23 13:00:53 +0100
commitd9768cd0315b0415d20818de9c897c6168c95b78 (patch)
tree41987740d02f689550436384dd7d2b755aa85869 /src
parentc87ea5c5d11bffbe84acf66215e6bf5a5e047a50 (diff)
downloadgziplike-d9768cd0315b0415d20818de9c897c6168c95b78.tar.gz
Split bitstream into bitreader and bitwriter
Diffstat (limited to 'src')
-rw-r--r--src/bitreader.nim72
-rw-r--r--src/bitstream.nim112
-rw-r--r--src/bitwriter.nim59
-rw-r--r--src/integers.nim7
-rw-r--r--src/lzssblock.nim10
-rw-r--r--src/rawblock.nim22
-rw-r--r--src/streamblock.nim37
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
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# BitReader
35
36type BitReader* = ref object
37 stream: Stream
38 bitOffset: int
39
40proc bitReader*(stream: Stream): BitReader =
41 BitReader(stream: stream, bitOffset: 0)
42
43proc atEnd*(bitReader: BitReader): bool =
44 bitReader.stream.atEnd()
45
46proc 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
64proc readBool*(bitReader: BitReader): bool =
65 bitReader.readBits(1, uint8) != 0
66
67proc 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
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/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
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 >= wordBit