aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpacien2018-11-23 12:31:59 +0100
committerpacien2018-11-23 12:31:59 +0100
commitc87ea5c5d11bffbe84acf66215e6bf5a5e047a50 (patch)
tree7dc5834dc6df09198b68a0fc7f8a447e7fd3944c /src
parent05490b39111169c32446e079de90d45d9fafa8dd (diff)
downloadgziplike-c87ea5c5d11bffbe84acf66215e6bf5a5e047a50.tar.gz
Partial implementation of streamblock
Diffstat (limited to 'src')
-rw-r--r--src/lzssblock.nim32
-rw-r--r--src/streamblock.nim64
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
17import bitstream
18
19type LzssBlock* = object
20 discard
21
22proc readSerialised*(bitStream: BitStream): LzssBlock =
23 discard
24
25proc writeSerialisedTo*(lzssBlock: LzssBlock, bitStream: BitStream) =
26 discard
27
28proc readRaw*(bitStream: BitStream): LzssBlock =
29 discard
30
31proc 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
17import sequtils
18import integers, bitstream, rawblock, lzssblock
19
20type BlockKind* = enum
21 uncompressed = 0b00'u8,
22 lzss = 0b01,
23 reserved1 = 0b10,
24 reserved2 = 0b11
25
26type 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
36proc 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
44proc 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
52proc 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
60proc 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")