aboutsummaryrefslogtreecommitdiff
path: root/src/blocks
diff options
context:
space:
mode:
authorpacien2018-11-30 21:06:39 +0100
committerpacien2018-11-30 21:06:39 +0100
commit1f0c6a7638353d1ebbbc4ba1a8de0887d5f68e98 (patch)
treeed9867c9997e9ceee750194fe8aaaa6f067a93f1 /src/blocks
parent3d5a87a5879aa724e47546d1bdbb7f6c9466cf94 (diff)
downloadgziplike-1f0c6a7638353d1ebbbc4ba1a8de0887d5f68e98.tar.gz
isolate blocks
Diffstat (limited to 'src/blocks')
-rw-r--r--src/blocks/lzssblock.nim32
-rw-r--r--src/blocks/rawblock.nim40
-rw-r--r--src/blocks/streamblock.nim68
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
17import ../bitio/bitreader, ../bitio/bitwriter
18
19type LzssBlock* = object
20 discard
21
22proc readSerialised*(bitReader: BitReader): LzssBlock =
23 discard
24
25proc writeSerialisedTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) =
26 discard
27
28proc readRaw*(bitReader: BitReader): LzssBlock =
29 discard
30
31proc 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
17import ../bitio/integers, ../bitio/bitreader, ../bitio/bitwriter
18
19const maxDataBitLength = high(uint16).int
20const bitLengthFieldBitLength = 2 * wordBitLength
21
22type RawBlock* = object
23 bitLength: int
24 data: seq[uint8]
25
26proc 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
31proc writeSerialisedTo*(rawBlock: RawBlock, bitWriter: BitWriter) =
32 bitWriter.writeBits(bitLengthFieldBitLength, rawBlock.bitLength.uint16)
33 bitWriter.writeSeq(rawBlock.bitLength, rawBlock.data)
34
35proc readRaw*(bitReader: BitReader, bits: int = maxDataBitLength): RawBlock =
36 let data = bitReader.readSeq(bits, uint8)
37 RawBlock(bitLength: data.bitLength, data: data.data)
38
39proc 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
17import sequtils
18import ../bitio/integers, ../bitio/bitreader, ../bitio/bitwriter
19import rawblock, lzssblock
20
21type BlockKind* = enum
22 uncompressed = 0b00'u8,
23 lzss = 0b01,
24 reserved1 = 0b10,
25 reserved2 = 0b11
26
27type 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
37proc isLast*(streamBlock: StreamBlock): bool =
38 streamBlock.last
39
40proc 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
48proc 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
56proc 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
64proc 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")