aboutsummaryrefslogtreecommitdiff
path: root/src/blocks/lzssblock.nim
diff options
context:
space:
mode:
authorpacien2018-12-02 00:22:56 +0100
committerpacien2018-12-02 00:22:56 +0100
commit5cc4256a931b98ea167291397421d0db60c5d40c (patch)
tree54c7ba39e69fb31322db431a82dbf31aedbb53b9 /src/blocks/lzssblock.nim
parent1850acb5b77aabbf4e9ba24ae6d5314c3d4d896a (diff)
downloadgziplike-5cc4256a931b98ea167291397421d0db60c5d40c.tar.gz
implement lzss block
Diffstat (limited to 'src/blocks/lzssblock.nim')
-rw-r--r--src/blocks/lzssblock.nim31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/blocks/lzssblock.nim b/src/blocks/lzssblock.nim
index f68f665..b23cee2 100644
--- a/src/blocks/lzssblock.nim
+++ b/src/blocks/lzssblock.nim
@@ -14,19 +14,38 @@
14# You should have received a copy of the GNU Affero General Public License 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/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import ../bitio/bitreader, ../bitio/bitwriter 17import lists
18import ../bitio/integers, ../bitio/bitreader, ../bitio/bitwriter
19import ../lzss/lzsschain, ../lzss/lzssencoder
20import ../huffman/huffmantree, ../huffman/huffmantreebuilder, ../huffman/huffmanencoder, ../huffman/huffmandecoder
21import ../lzsshuffman/lzsshuffmanstats, ../lzsshuffman/lzsshuffmandecoder, ../lzsshuffman/lzsshuffmanencoder
22
23const maxDataByteLength = 32_000
18 24
19type LzssBlock* = object 25type LzssBlock* = object
20 discard 26 lzssChain: LzssChain
21 27
22proc readSerialised*(bitReader: BitReader): LzssBlock = 28proc readSerialised*(bitReader: BitReader): LzssBlock =
23 discard 29 let symbolHuffmanTree = huffmantree.deserialise(bitReader, uint16)
30 let positionHuffmanTree = huffmantree.deserialise(bitReader, uint16)
31 let symbolDecoder = symbolHuffmanTree.decoder()
32 let positionDecoder = positionHuffmanTree.decoder()
33 LzssBlock(lzssChain: readChain(bitReader, symbolDecoder, positionDecoder, maxDataByteLength))
24 34
25proc writeSerialisedTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) = 35proc writeSerialisedTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) =
26 discard 36 let (symbolStats, positionStats) = aggregateStats(lzssBlock.lzssChain)
37 let symbolHuffmanTree = buildHuffmanTree(symbolStats)
38 let positionHuffmanTree = buildHuffmanTree(positionStats)
39 let symbolEncoder = symbolHuffmanTree.encoder(uint16)
40 let positionEncoder = positionHuffmanTree.encoder(uint16)
41 symbolHuffmanTree.serialise(bitWriter)
42 positionHuffmanTree.serialise(bitWriter)
43 lzssBlock.lzssChain.writeChain(symbolEncoder, positionEncoder, bitWriter)
27 44
28proc readRaw*(bitReader: BitReader): LzssBlock = 45proc readRaw*(bitReader: BitReader): LzssBlock =
29 discard 46 let byteBuf = bitReader.readSeq(maxDataByteLength, uint8)
47 LzssBlock(lzssChain: lzssEncode(byteBuf.data))
30 48
31proc writeRawTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) = 49proc writeRawTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) =
32 discard 50 let byteSeq = lzssBlock.lzssChain.decode()
51 bitWriter.writeSeq(byteSeq.len * wordBitLength, byteSeq)