> >
> > Maybe we can use the code from UnZip if it is the Deflate algorhitm ?
>
>I tried using some of the zlib functions, but none of them worked. There's
>quite a number of custom functions that might help here, though
>(I'm not really working on SCI1 anyway, except for a separate project).
I took me all the day but I think I have figured it out how it works ...
It is quite similar to the explode algorithm (ZIP method #6 - implode ) but
there are differences.
1. the first 2 bytes are parameters: the first must be 0 or 1 (P1 - I think
this means
0=binary and 1=ascii file but in RESOURCEs I saw always 0), the 2nd byte
must be 4,5 or 6 and it is a parameter for the decompression alg. (P2)
2. after the 2 bits follows the actual bitstream. you decode it as follows
(P1 = 0 - binary ?):
LOOP:
read 1 bit (take bits from the lowest value (LSB) to the MSB i.e. bit
0, bit 1 etc ...)
- if the bit is 0 read 8 bits and write it to the output as it is.
- if the bit is 1 we have here a length/distance pair:
- decode a number with Hufmman Tree #1 (length tree);
variable bit length, result is 0x00 .. 0x0F -> L1
if L1 <= 7:
LENGTH = L1 + 2
if L1 > 7
read more (L1-7) bits -> L2
LENGTH = L2 + M[L-7] + 2
(M is a constant array defined as M[0] =
7, M[n+1] = M[n]+ 2 ^ n
that means M[1] = 8, M[2] = 0x0A, M[3] = 0x0E,
M[4] = 0x16, M[5] = 0x26 etc ...)
- decode another number with Hufmann Tree #2 (distance
tree) giving result 0x00..0x3F -> D1
if LENGTH == 2
D1 = D1 << 2
read 2 bits -> D2
else
D1 = D1 << P2 // the parameter 2
read P2 bits -> D2
DISTANCE = D1 | D2
- now copy LENGTH bytes from (output_ptr-DISTANCE) to
output_ptr
go back to LOOP
the process terminates when there are no more bits on input.
Petr