This commit is contained in:
Ryan Oldenburg 2020-11-01 13:35:57 -06:00
parent d39d1f83de
commit f69226a677
3 changed files with 18 additions and 8 deletions

View File

@ -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

View File

@ -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)]

View File

@ -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