From f69226a67782a87ccfc4743624527cd6b6263ea5 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Sun, 1 Nov 2020 13:35:57 -0600 Subject: [PATCH] it works --- src/zippy/compress.nim | 18 ++++++++++++++---- src/zippy/uncompress.nim | 4 ++-- tests/test.nim | 4 ++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/zippy/compress.nim b/src/zippy/compress.nim index a83fbc1..da7de8e 100644 --- a/src/zippy/compress.nim +++ b/src/zippy/compress.nim @@ -281,13 +281,23 @@ func compress*(src: seq[uint8]): seq[uint8] = b.addBits(bitLensRle[k], 7) inc k + for i in 0 ..< encoded.len: + let symbol = encoded[i] + b.addBitsReverse(codesLitLen[symbol], depthsLitLen[symbol]) + if depthsLitLen[256] == 0: failCompress() - b.addBits(0, 8) - b.addBits(0, 8) - b.addBits(0, 8) - b.addBits(0, 8) + b.addBitsReverse(codesLitLen[256], depthsLitLen[256]) # End of block + + b.skipRemainingBitsInCurrentByte() + b.data.setLen(b.data.len + 1) + + let checksum = cast[array[4, uint8]](adler32(src)) + b.addBits(checkSum[3], 8) + b.addBits(checkSum[2], 8) + b.addBits(checkSum[1], 8) + b.addBits(checkSum[0], 8) b.data diff --git a/src/zippy/uncompress.nim b/src/zippy/uncompress.nim index d4879ce..349c4c6 100644 --- a/src/zippy/uncompress.nim +++ b/src/zippy/uncompress.nim @@ -138,8 +138,8 @@ func decodeSymbol(b: var BitStream, h: Huffman): uint16 {.inline.} = code = code or (bits and 1).int bits = bits shr 1 count = h.counts[len].int - debugEcho code, " ", toBin(code.int, 8), " ", len, " ", count, " ", first, " ", index - debugEcho code - count + # debugEcho code, " ", toBin(code.int, 8), " ", len, " ", count, " ", first, " ", index + # debugEcho code - count if code - count < first: fastSkip(i) return h.symbols[index + (code - first)] diff --git a/tests/test.nim b/tests/test.nim index ebd0939..fce94c3 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -38,8 +38,8 @@ const golds = [ let c = cast[seq[uint8]](compress("A_DEAD_DAD_CEDED_A_BAD_BABE_A_BEADED_ABACA_BED")) # let c = cast[seq[uint8]](compress("aaaaaaaaaabcccccccccccccccddddddd")) -# echo c -echo uncompress(c) +echo c +echo cast[string](uncompress(c)) # import random, fidget/opengl/perf, algorithm # include zippy/compress