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/streamblock.nim | |
parent | 3d5a87a5879aa724e47546d1bdbb7f6c9466cf94 (diff) | |
download | gziplike-1f0c6a7638353d1ebbbc4ba1a8de0887d5f68e98.tar.gz |
isolate blocks
Diffstat (limited to 'src/streamblock.nim')
-rw-r--r-- | src/streamblock.nim | 68 |
1 files changed, 0 insertions, 68 deletions
diff --git a/src/streamblock.nim b/src/streamblock.nim deleted file mode 100644 index 489097e..0000000 --- a/src/streamblock.nim +++ /dev/null | |||
@@ -1,68 +0,0 @@ | |||
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") | ||