madler / zlib

A massively spiffy yet delicately unobtrusive compression library.
http://zlib.net/
Other
5.58k stars 2.43k forks source link

Unused code part in inftrees.c? #850

Closed bastie closed 1 year ago

bastie commented 1 year ago

Hello, can someone explained, in wich case the while loop is executed?

            /* determine length of next table */
            curr = len - drop;
            left = (int)(1 << curr);
            while (curr + drop < max) {
                left -= count[curr + drop];
                if (left <= 0) break;
                curr++;
                left <<= 1;
            }

In result of transpiled to Swift, swiftc means curr+drop < max is never ever true, because curr = len-drop before. But it was a test translation from KI and so I asked.

If I starts make test with printf in the while loop no output is displayed.

thanks Sebastian

madler commented 1 year ago

Happens all the time. You must not have tested it with your printf very much. It happens when len < max, which is when the next sub-table is shorter than the last sub-table.

Here is a short zlib stream that does it:

78 5e 05 c1 c1 81 24 c9 11 04 31 59 79 3b 5d 19
6e d0 ff 4f e0 7f ff fd fb f7 ef ef ef ef ef f7
fb fd 7e bf df f7 7d df f7 7d df f7 7d ef bd f7
de 7b ef bd f7 de 7b ef dd dd dd dd dd dd dd dd
dd dd dd dd dd dd dd b6 6d db b6 6d db b6 6d db
b6 6d db b6 6d db b6 6d ab aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa 02 00 00 00 00
00 00 00 00 00 00 00 00 00 00 c0 ff 01 eb d6 83
d0