diff options
Diffstat (limited to 'src/lzsschain.nim')
-rw-r--r-- | src/lzsschain.nim | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/lzsschain.nim b/src/lzsschain.nim new file mode 100644 index 0000000..8203cb8 --- /dev/null +++ b/src/lzsschain.nim | |||
@@ -0,0 +1,36 @@ | |||
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 lists, tables, sugar | ||
18 | import polyfill, integers, lzssnode | ||
19 | |||
20 | const maxChainByteLength = 32_000 * wordBitLength | ||
21 | |||
22 | type LzssChain* = | ||
23 | SinglyLinkedList[LzssNode] | ||
24 | |||
25 | proc lzssChain*(): LzssChain = | ||
26 | initSinglyLinkedList[LzssNode]() | ||
27 | |||
28 | proc decode*(lzssChain: LzssChain): seq[uint8] = | ||
29 | result = newSeqOfCap[uint8](maxChainByteLength) | ||
30 | for node in lzssChain.items: | ||
31 | case node.kind: | ||
32 | of character: | ||
33 | result.add(node.character) | ||
34 | of reference: | ||
35 | let absolutePos = result.len - node.relativePos | ||
36 | result.add(result.toOpenArray(absolutePos, absolutePos + node.length - 1)) | ||