Chips are about to burn so I'll edit this after taking them out of the oven, short of it is that on the first incrment code should be 1, not 8
Code:for ( code = 0, cur_leng = 1; cur_leng <= max_leng; ++cur_leng )
{
for ( uint j = 0; j < list.used; ++j )
{
CODEWORD *word = _list + j;
printf( "Code = " );
SeeBits( &code, max_leng );
putchar('\n');
if ( word->leng != cur_leng )
continue;
word->code = code;
printf( "_list[%u].code = ", j );
SeeBits( &code, word->leng );
putchar('\n');
++code;
}
printf( "Final code for length %u = ", cur_leng );
SeeBits( &code, max_leng );
putchar('\n');
while ( !(code >> cur_leng) )
{
uint value = code >> cur_leng;
printf( "Code " );
SeeBits( &code, max_leng );
printf( " shifted by %u results in ", cur_leng );
SeeBits( &value, max_leng );
putchar('\n');
++code;
}
printf( "Initial expected code for leng %u = ", cur_leng + 1 );
SeeBits( &code, cur_leng + 1 );
putchar('\n');
for ( i = 0; i < list.used; ++i )
{
CODEWORD *word = _list + i;
if ( word->leng >= cur_leng )
continue;
if ( code == (word->code << (cur_leng - word->code)) )
code++;
}
printf( "Adjusted initial code for leng %u = ", cur_leng + 1 );
SeeBits( &code, cur_leng + 1 );
putchar('\n');
}
Edit: Now that my chips are safe, the most relevant output is from this part:Code:./a.out aba.gz
PrintBytes( 0x559c1662c480, 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
path = 'aba.gz', max readable bits = 328
gzip.magic = 1F8B, gzip.flags = 0, gzip.mtime = 0, zlib.xflags = 0, zlib.system = 0
gzip.flag_TEXT = false
gzip.flag_HCRC = false
gzip.flag_MORE = false
gzip.flag_NAME = false
gzip.flag_NOTE = false
gzip.flag_RESERVED = 0
last = true, type = 2
lengc = 260, distc = 7, codec = 18, left = 7
0 _list[16].leng = 4, max_leng 4
1 _list[17].leng = 4, max_leng 4
2 _list[18].leng = 2, max_leng 4
3 _list[ 0].leng = 0, max_leng 4
4 _list[ 8].leng = 0, max_leng 4
5 _list[ 7].leng = 0, max_leng 4
6 _list[ 9].leng = 0, max_leng 4
7 _list[ 6].leng = 0, max_leng 4
8 _list[10].leng = 0, max_leng 4
9 _list[ 5].leng = 0, max_leng 4
10 _list[11].leng = 0, max_leng 4
11 _list[ 4].leng = 4, max_leng 4
12 _list[12].leng = 0, max_leng 4
13 _list[ 3].leng = 0, max_leng 4
14 _list[13].leng = 0, max_leng 4
15 _list[ 2].leng = 1, max_leng 4
16 _list[14].leng = 0, max_leng 4
17 _list[ 1].leng = 4, max_leng 4
i = 18
_list[ 0] = _list[ 1], code = 0, leng = 4
_list[ 1] = _list[ 2], code = 0, leng = 1
_list[ 2] = _list[ 4], code = 0, leng = 4
_list[ 3] = _list[16], code = 0, leng = 4
_list[ 4] = _list[17], code = 0, leng = 4
_list[ 5] = _list[18], code = 0, leng = 2
Code = 0000
Code = 0000
_list[1].code = 0
Code = 1000
Code = 1000
Code = 1000
Code = 1000
Final code for length 1 = 1000
Code 1000 shifted by 1 results in 0000
Initial expected code for leng 2 = 01
Adjusted initial code for leng 2 = 01
Code = 0100
Code = 0100
Code = 0100
Code = 0100
Code = 0100
Code = 0100
_list[5].code = 01
Final code for length 2 = 1100
Code 1100 shifted by 2 results in 0000
Initial expected code for leng 3 = 001
Adjusted initial code for leng 3 = 001
Code = 0010
Code = 0010
Code = 0010
Code = 0010
Code = 0010
Code = 0010
Final code for length 3 = 0010
Code 0010 shifted by 3 results in 0000
Code 1010 shifted by 3 results in 0000
Code 0110 shifted by 3 results in 0000
Code 1110 shifted by 3 results in 0000
Initial expected code for leng 4 = 0001
Adjusted initial code for leng 4 = 0001
Code = 0001
_list[0].code = 0001
Code = 1001
Code = 1001
_list[2].code = 1001
Code = 0101
_list[3].code = 0101
Code = 1101
_list[4].code = 1101
Code = 0011
Final code for length 4 = 0011
Code 0011 shifted by 4 results in 0000
Code 1011 shifted by 4 results in 0000
Code 0111 shifted by 4 results in 0000
Code 1111 shifted by 4 results in 0000
Initial expected code for leng 5 = 00001
Adjusted initial code for leng 5 = 00001
...
The rest I provided just for context, code is an unsigned integer so there's no funny pointer business going on, there's no opotunity to increment code,SeeBits() look like this:Code:Code = 0000
Code = 0000
_list[1].code = 0
Code = 1000
Code = 1000
Code = 1000
Code = 1000
Final code for length 1 = 1000
Code 1000 shifted by 1 results in 0000
Initial expected code for leng 2 = 01
Adjusted initial code for leng 2 = 01
Code = 0100
Code = 0100
Code = 0100
Code = 0100
Code = 0100
Code = 0100
_list[5].code = 01
Final code for length 2 = 1100
Code 1100 shifted by 2 results in 0000
Initial expected code for leng 3 = 001
Adjusted initial code for leng 3 = 001
Code = 0010
Code = 0010
Code = 0010
Code = 0010
Code = 0010
Code = 0010
Final code for length 3 = 0010
Code 0010 shifted by 3 results in 0000
Code 1010 shifted by 3 results in 0000
Code 0110 shifted by 3 results in 0000
Code 1110 shifted by 3 results in 0000
Initial expected code for leng 4 = 0001
Adjusted initial code for leng 4 = 0001
Code = 0001
_list[0].code = 0001
Code = 1001
Code = 1001
_list[2].code = 1001
Code = 0101
_list[3].code = 0101
Code = 1101
_list[4].code = 1101
Code = 0011
Final code for length 4 = 0011
Code 0011 shifted by 4 results in 0000
Code 1011 shifted by 4 results in 0000
Code 0111 shifted by 4 results in 0000
Code 1111 shifted by 4 results in 0000
Initial expected code for leng 5 = 00001
Adjusted initial code for leng 5 = 00001
...
And GetBits() looks like this:Code:void SeeBits( void * data, uint bits )
{
for ( uint i = 0; i < bits; i += bitsof(uintmax_t) )
{
uint left = bits - (i * bitsof(uintmax_t));
uint stop = (left < bitsof(uintmax_t)) ? left : bitsof(uintmax_t);
uintmax_t mask = 1;
uintmax_t value = GetBits
(
data,
bits,
i * bitsof(uintmax_t),
stop,
false,
false
);
for ( mask <<= (stop - 1); mask; mask >>= 1 )
putchar( '0' + !!(value & mask) );
}
}
I see no chance for the value of "code" to be corrupted, any ideas?Code:uintmax_t GetBits( void * data, uint max, uint pos, uint bits, bool backwards, bool left_shifted )
{
uchar *temp = data;
uint end, i = 0;
uintmax_t value = 0;
if ( backwards )
{
end = (bits <= pos) ? pos - bits : 0;
while ( pos > end )
{
uint B = --pos / CHAR_BIT;
uint b = pos % CHAR_BIT;
uchar bit = 1 << b;
value <<= 1;
value |= !!(temp[B] & bit);
++i;
}
}
else
{
end = pos + bits;
end = (end <= max) ? end : max;
while ( pos < end )
{
uint B = pos / CHAR_BIT;
uint b = pos++ % CHAR_BIT;
uchar bit = 1 << b;
bool one = !!(temp[B] & bit);
value <<= 1;
value |= one;
++i;
}
}
if ( left_shifted )
value <<= bits - i;
return value;
}

