diff options
-rw-r--r-- | src/lzssblock.nim | 32 | ||||
-rw-r--r-- | src/streamblock.nim | 64 |
2 files changed, 96 insertions, 0 deletions
diff --git a/src/lzssblock.nim b/src/lzssblock.nim new file mode 100644 index 0000000..87b62a0 --- /dev/null +++ b/src/lzssblock.nim | |||
@@ -0,0 +1,32 @@ | |||
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 bitstream | ||
18 | |||
19 | type LzssBlock* = object | ||
20 | discard | ||
21 | |||
22 | proc readSerialised*(bitStream: BitStream): LzssBlock = | ||
23 | discard | ||
24 | |||
25 | proc writeSerialisedTo*(lzssBlock: LzssBlock, bitStream: BitStream) = | ||
26 | discard | ||
27 | |||
28 | proc readRaw*(bitStream: BitStream): LzssBlock = | ||
29 | discard | ||
30 | |||
31 | proc writeRawTo*(lzssBlock: LzssBlock, bitStream: BitStream) = | ||
32 | discard | ||
diff --git a/src/streamblock.nim b/src/streamblock.nim new file mode 100644 index 0000000..8d2b4b1 --- /dev/null +++ b/src/streamblock.nim | |||
@@ -0,0 +1,64 @@ | |||
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 sequtils | ||
18 | import integers, bitstream, rawblock, lzssblock | ||
19 | |||
20 | type BlockKind* = enum | ||
21 | uncompressed = 0b00'u8, | ||
22 | lzss = 0b01, | ||
23 | reserved1 = 0b10, | ||
24 | reserved2 = 0b11 | ||
25 | |||
26 | type StreamBlock* = object | ||
27 | last: bool | ||
28 | case kind: BlockKind | ||
29 | of uncompressed: | ||
30 | rawBlock: RawBlock | ||
31 | of lzss: | ||
32 | lzssBlock: LzssBlock | ||
33 | else: | ||
34 | discard | ||
35 | |||
36 | proc readSerialised*(bitStream: BitStream): StreamBlock = | ||
37 | result.last = bitStream.readBool() | ||
38 | result.kind = bitStream.readBits(2, uint8).BlockKind | ||
39 | case result.kind: | ||
40 | of uncompressed: result.rawBlock = rawblock.readRaw(bitStream) | ||
41 | of lzss: result.lzssBlock = lzssblock.readRaw(bitStream) | ||
42 | else: raise newException(ValueError, "unhandled block type") | ||
43 | |||
44 | proc writeSerialisedTo*(streamBlock: StreamBlock, bitStream: BitStream) = | ||
45 | bitStream.writeBool(streamBlock.last) | ||
46 | bitStream.writeBits(2, streamBlock.kind.uint8) | ||
47 | case streamBlock.kind: | ||
48 | of uncompressed: streamBlock.rawBlock.writeSerialisedTo(bitStream) | ||
49 | of lzss: streamBlock.lzssBlock.writeSerialisedTo(bitStream) | ||
50 | else: raise newException(ValueError, "unhandled block type") | ||
51 | |||
52 | proc readRaw*(bitStream: BitStream, kind: BlockKind = uncompressed): StreamBlock = | ||
53 | result.kind = kind | ||
54 | case kind: | ||
55 | of uncompressed: result.rawBlock = rawblock.readRaw(bitStream) | ||
56 | of lzss: result.lzssBlock = lzssblock.readRaw(bitStream) | ||
57 | else: raise newException(ValueError, "unhandled block type") | ||
58 | result.last = bitStream.atEnd() | ||
59 | |||
60 | proc writeRawTo*(streamBlock: StreamBlock, bitStream: BitStream) = | ||
61 | case streamBlock.kind: | ||
62 | of uncompressed: streamBlock.rawBlock.writeRawTo(bitStream) | ||
63 | of lzss: streamBlock.lzssBlock.writeRawTo(bitStream) | ||
64 | else: raise newException(ValueError, "unhandled block type") | ||