diff options
author | pacien | 2018-11-30 21:06:39 +0100 |
---|---|---|
committer | pacien | 2018-11-30 21:06:39 +0100 |
commit | 1f0c6a7638353d1ebbbc4ba1a8de0887d5f68e98 (patch) | |
tree | ed9867c9997e9ceee750194fe8aaaa6f067a93f1 /src/blocks | |
parent | 3d5a87a5879aa724e47546d1bdbb7f6c9466cf94 (diff) | |
download | gziplike-1f0c6a7638353d1ebbbc4ba1a8de0887d5f68e98.tar.gz |
isolate blocks
Diffstat (limited to 'src/blocks')
-rw-r--r-- | src/blocks/lzssblock.nim | 32 | ||||
-rw-r--r-- | src/blocks/rawblock.nim | 40 | ||||
-rw-r--r-- | src/blocks/streamblock.nim | 68 |
3 files changed, 140 insertions, 0 deletions
diff --git a/src/blocks/lzssblock.nim b/src/blocks/lzssblock.nim new file mode 100644 index 0000000..f68f665 --- /dev/null +++ b/src/blocks/lzssblock.nim | |||
@@ -0,0 +1,32 @@ | |||
1 | # 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 ../bitio/bitreader, ../bitio/bitwriter | ||
18 | |||
19 | type LzssBlock* = object | ||
20 | discard | ||
21 | |||
22 | proc readSerialised*(bitReader: BitReader): LzssBlock = | ||
23 | discard | ||
24 | |||
25 | proc writeSerialisedTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) = | ||
26 | discard | ||
27 | |||
28 | proc readRaw*(bitReader: BitReader): LzssBlock = | ||
29 | discard | ||
30 | |||
31 | proc writeRawTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) = | ||
32 | discard | ||
diff --git a/src/blocks/rawblock.nim b/src/blocks/rawblock.nim new file mode 100644 index 0000000..2b32831 --- /dev/null +++ b/src/blocks/rawblock.nim | |||
@@ -0,0 +1,40 @@ | |||
1 | # 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 ../bitio/integers, ../bitio/bitreader, ../bitio/bitwriter | ||
18 | |||
19 | const maxDataBitLength = high(uint16).int | ||
20 | const bitLengthFieldBitLength = 2 * wordBitLength | ||
21 | |||
22 | type RawBlock* = object | ||
23 | bitLength: int | ||
24 | data: seq[uint8] | ||
25 | |||
26 | proc readSerialised*(bitReader: BitReader): RawBlock = | ||
27 | let bitLength = bitReader.readBits(bitLengthFieldBitLength, uint16).int | ||
28 | let data = bitReader.readSeq(bitLength, uint8) | ||
29 | RawBlock(bitLength: bitLength, data: data.data) | ||
30 | |||
31 | proc writeSerialisedTo*(rawBlock: RawBlock, bitWriter: BitWriter) = | ||
32 | bitWriter.writeBits(bitLengthFieldBitLength, rawBlock.bitLength.uint16) | ||
33 | bitWriter.writeSeq(rawBlock.bitLength, rawBlock.data) | ||
34 | |||
35 | proc readRaw*(bitReader: BitReader, bits: int = maxDataBitLength): RawBlock = | ||
36 | let data = bitReader.readSeq(bits, uint8) | ||
37 | RawBlock(bitLength: data.bitLength, data: data.data) | ||
38 | |||
39 | proc writeRawTo*(rawBlock: RawBlock, bitWriter: BitWriter) = | ||
40 | bitWriter.writeSeq(rawBlock.bitLength, rawBlock.data) | ||
diff --git a/src/blocks/streamblock.nim b/src/blocks/streamblock.nim new file mode 100644 index 0000000..c904af7 --- /dev/null +++ b/src/blocks/streamblock.nim | |||
@@ -0,0 +1,68 @@ | |||
1 | # 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 sequtils | ||
18 | import ../bitio/integers, ../bitio/bitreader, ../bitio/bitwriter | ||
19 | import rawblock, lzssblock | ||
20 | |||
21 | type BlockKind* = enum | ||
22 | uncompressed = 0b00'u8, | ||
23 | lzss = 0b01, | ||
24 | reserved1 = 0b10, | ||
25 | reserved2 = 0b11 | ||
26 | |||
27 | type StreamBlock* = object | ||
28 | last: bool | ||
29 | case kind: BlockKind | ||
30 | of uncompressed: | ||
31 | rawBlock: RawBlock | ||
32 | of lzss: | ||
33 | lzssBlock: LzssBlock | ||
34 | else: | ||
35 | discard | ||
36 | |||
37 | proc isLast*(streamBlock: StreamBlock): bool = | ||
38 | streamBlock.last | ||
39 | |||
40 | proc readSerialised*(bitReader: BitReader): StreamBlock = | ||
41 | result.last = bitReader.readBool() | ||
42 | result.kind = bitReader.readBits(2, uint8).BlockKind | ||
43 | case result.kind: | ||
44 | of uncompressed: result.rawBlock = rawblock.readSerialised(bitReader) | ||
45 | of lzss: result.lzssBlock = lzssblock.readSerialised(bitReader) | ||
46 | else: raise newException(ValueError, "unhandled block type") | ||
47 | |||
48 | proc writeSerialisedTo*(streamBlock: StreamBlock, bitWriter: BitWriter) = | ||
49 | bitWriter.writeBool(streamBlock.last) | ||
50 | bitWriter.writeBits(2, streamBlock.kind.uint8) | ||
51 | case streamBlock.kind: | ||
52 | of uncompressed: streamBlock.rawBlock.writeSerialisedTo(bitWriter) | ||
53 | of lzss: streamBlock.lzssBlock.writeSerialisedTo(bitWriter) | ||
54 | else: raise newException(ValueError, "unhandled block type") | ||
55 | |||
56 | proc readRaw*(bitReader: BitReader, kind: BlockKind = uncompressed): StreamBlock = | ||
57 | result.kind = kind | ||
58 | case kind: | ||
59 | of uncompressed: result.rawBlock = rawblock.readRaw(bitReader) | ||
60 | of lzss: result.lzssBlock = lzssblock.readRaw(bitReader) | ||
61 | else: raise newException(ValueError, "unhandled block type") | ||
62 | result.last = bitReader.atEnd() | ||
63 | |||
64 | proc writeRawTo*(streamBlock: StreamBlock, bitWriter: BitWriter) = | ||
65 | case streamBlock.kind: | ||
66 | of uncompressed: streamBlock.rawBlock.writeRawTo(bitWriter) | ||
67 | of lzss: streamBlock.lzssBlock.writeRawTo(bitWriter) | ||
68 | else: raise newException(ValueError, "unhandled block type") | ||