diff options
author | pacien | 2018-11-23 12:31:59 +0100 |
---|---|---|
committer | pacien | 2018-11-23 12:31:59 +0100 |
commit | c87ea5c5d11bffbe84acf66215e6bf5a5e047a50 (patch) | |
tree | 7dc5834dc6df09198b68a0fc7f8a447e7fd3944c /src/streamblock.nim | |
parent | 05490b39111169c32446e079de90d45d9fafa8dd (diff) | |
download | gziplike-c87ea5c5d11bffbe84acf66215e6bf5a5e047a50.tar.gz |
Partial implementation of streamblock
Diffstat (limited to 'src/streamblock.nim')
-rw-r--r-- | src/streamblock.nim | 64 |
1 files changed, 64 insertions, 0 deletions
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") | ||