Code:
./a.out aba.gz
path = 'aba.gz'
PrintStreamDetails( 0x7fff6097e150 ): ptr = 0x55af47400480, pos = 0, num = 0, max = 328, fed = 0
PrintBytes( 0x55af47400480, 41, 16 )
1F 8B 0 0 0 0 0 0 0 3 1D C6 49 1 0 0
40 C0 AC A3 7F 88 3D 3C 20 2A 97 9D 37 5E 1D C
29 34 94 23 0 0 0
PrintStreamDetails( 0x7fff6097e150 ): ptr = 0x55af47400480, pos = 136, num = 80, max = 328, fed = 56
gzip.magic = 1F8B, gzip.format = 0, gzip.flags = 0, gzip.mtime = 0, zlib.xflags = 0, zlib.system = 3
gzip.flag_TEXT = false, gzip.flag_HCRC = false, gzip.flag_MORE = false, gzip.flag_NAME = false, gzip.flag_NOTE = false, gzip.flag_RESERVED = 0
PrintStreamDetails( 0x7fff6097e150 ): ptr = 0x55af47400480, pos = 136, num = 80, max = 328, fed = 56
last = true, type = 2
lengc = 260, distc = 7, codec = 18, left = 60
pos = 212, max = 328, count = 267
left = 61, byte = 18, bit = 7
Code Table:
main.c:647: _list[ 0]: from = 2, more = 0, copy = 0, leng = 1, fill = 'ÿ', code = 0
main.c:647: _list[ 1]: from = 18, more = 7, copy = 11, leng = 2, fill = 'ÿ', code = 10
main.c:647: _list[ 2]: from = 1, more = 0, copy = 0, leng = 4, fill = 'ÿ', code = 1100
main.c:647: _list[ 3]: from = 4, more = 0, copy = 0, leng = 4, fill = 'ÿ', code = 1101
main.c:647: _list[ 4]: from = 16, more = 2, copy = 3, leng = 4, fill = 'ÿ', code = 1110
main.c:647: _list[ 5]: from = 17, more = 3, copy = 3, leng = 4, fill = 'ÿ', code = 1111
main.c:742: _list[ 1]: from = 18, more = 7, copy = 11, leng = 2, fill = 'ÿ', code = 10
i = 0010 j = 1, copy = 97
lit = 'ÿ' (0), sym = 'ÿ' (0)
...
lit = '`' (96), sym = 'ÿ' (0)
main.c:742: _list[ 2]: from = 1, more = 0, copy = 0, leng = 4, fill = 'ÿ', code = 1100
lit = 'a' (97), sym = 'a' (97)
main.c:742: _list[ 0]: from = 2, more = 0, copy = 0, leng = 1, fill = 'ÿ', code = 0
lit = 'b' (98), sym = 'b' (98)
main.c:742: _list[ 1]: from = 18, more = 7, copy = 11, leng = 2, fill = 'ÿ', code = 10
i = 0010 j = 1, copy = 138
lit = 'c' (99), sym = 'ÿ' (0)
...
lit = 'ì' (236), sym = 'ÿ' (0)
main.c:742: _list[ 1]: from = 18, more = 7, copy = 11, leng = 2, fill = 'ÿ', code = 10
i = 0010 j = 1, copy = 19
lit = 'í' (237), sym = 'ÿ' (0)
...
lit = 'ÿ' (255), sym = 'ÿ' (0)
main.c:742: _list[ 3]: from = 4, more = 0, copy = 0, leng = 4, fill = 'ÿ', code = 1101
lit = '
' (256), sym = '
' (256)
main.c:742: _list[ 4]: from = 16, more = 2, copy = 3, leng = 4, fill = 'ÿ', code = 1110
i = 1110 j = 4, copy = 3
lit = '' (257), sym = '' (257)
lit = '' (258), sym = '' (257)
lit = '' (259), sym = '' (257)
main.c:742: _list[ 0]: from = 2, more = 0, copy = 0, leng = 1, fill = 'ÿ', code = 0
lit = '' (260), sym = '' (260)
main.c:742: _list[ 5]: from = 17, more = 3, copy = 3, leng = 4, fill = 'ÿ', code = 1111
i = 1111 j = 5, copy = 3
lit = '' (261), sym = 'ÿ' (0)
lit = '' (262), sym = 'ÿ' (0)
lit = '' (263), sym = 'ÿ' (0)
main.c:742: _list[ 0]: from = 2, more = 0, copy = 0, leng = 1, fill = 'ÿ', code = 0
lit = '' (264), sym = '' (264)
main.c:742: _list[ 0]: from = 2, more = 0, copy = 0, leng = 1, fill = 'ÿ', code = 0
lit = ' ' (265), sym = ' ' (265)
main.c:742: _list[ 0]: from = 2, more = 0, copy = 0, leng = 1, fill = 'ÿ', code = 0
lit = '
' (266), sym = '
' (266)
PrintBytes( 0x55af474034a0, 0, 16 )
Handler:
Code:
void * RdHuffmanLengDistCodes
(
STREAM *stream,
BUFF *CodeWords,
BUFF *Symbols,
uint count,
uint max_leng,
uint start_with_literal
)
{
uint prv = 0, lit = start_with_literal, pos = 0;
CODEWORD *codewords = CodeWords->addr;
HUFFMAN_SYMBOL *symbols, *symbol;
symbols = Symbols->addr = calloc( count + 1, sizeof(HUFFMAN_SYMBOL) );
if ( !symbols )
return NULL;
while ( pos < count && stream->num < stream->max )
{
CODEWORD *word = NULL;
uint i = 0, j = 0, cur_leng = 1, copy = 1;
uintmax_t val;
for ( cur_leng = 1; cur_leng <= max_leng; ++cur_leng )
{
i = RevBits( CopyStreamBits( stream, cur_leng, false ), cur_leng );
for ( j = 0; j < list.used; ++j )
{
word = codewords + j;
if ( word->leng == cur_leng && word->code == i )
{
IncStreamCount( stream, cur_leng );
break;
}
}
if ( j < list.used )
break;
}
if ( cur_leng > max_leng )
{
printf
(
"i = %u, j = %u, list.used = %u, cur_leng = %u\n",
i, j, CodeWords->used, cur_leng
);
return NULL;
}
val = CopyStreamBits( stream, word->more, true );
PrintCodeWord( word, __LINE__, j );
if ( word->from >= 16 )
{
copy = (uint)(word->copy + val);
printf( "i = " );
SeeBits( &i, max_leng );
printf( " j = %u, copy = %u\n", j, copy );
}
for ( uint s = 0; s < copy; ++s, ++pos, ++lit )
{
uint sym = 0;
if ( word->from == 16 )
sym = prv;
else if ( word->from < 16 )
sym = lit;
symbol = symbols + pos;
symbol->len = word->leng;
symbol->sym = sym;
symbol->val = pos;
printf
(
"lit = '%c' (%u), sym = '%c' (%u)\n",
lit ? lit : -1, lit,
sym ? sym : -1, sym
);
}
prv = lit;
}
return symbols;
}
How it's called: