folding (contracting) for this function doesn't work properly.
Code: Select all
Private Function lzfx_decompress(Byval ibuf As Ubyte Ptr , Byval ilen As UInteger , Byval obuf As Ubyte Ptr , Byref olen As UInteger) As Long
Dim As Ubyte Ptr ip = ibuf
Dim As Ubyte Ptr in_end = ip + ilen
Dim As Ubyte Ptr op = obuf
Dim As Ubyte Ptr out_end = op + olen
Dim As UInteger remain_len = 0
Dim As Long rc
If(olen = 0) Then Return LZFX_EARGS
If(ibuf = NULL) Then
If(ilen <> 0) Then Return LZFX_EARGS
olen = 0
Return 0
End If
If(obuf = NULL)Then
If(olen <> 0) Then Return LZFX_EARGS
Return lzfx_getsize(ibuf, ilen, olen)
End If
#Macro my_guess() 'used by lzfx_decompress (better than Gosub)
rc = lzfx_getsize(ip, ilen - (ip-ibuf), remain_len)
If rc>=0 Then olen = remain_len + (op - obuf)
Return rc
#Endmacro
Do
Dim As UInteger ctrl = *ip
ip+=1
/' Format 000LLLLL: a literal Byte String follows, of length L+1 '/
If(ctrl < (1 Shl 5)) Then
ctrl+=1
If(op + ctrl > out_end) Then
ip -=1 /' Rewind To control Byte '/
my_guess()
End If
If(ip + ctrl > in_end) Then Return LZFX_ECORRUPT
Do
*op= *ip : op+=1 : ip+=1
ctrl -= 1
Loop While(ctrl <> 0)
/' Format #1 [LLLooooo oooooooo]: backref of length L+1+2
^^^^^ ^^^^^^^^
A B
#2 [111ooooo LLLLLLLL oooooooo] backref of length L+7+2
^^^^^ ^^^^^^^^
A B
In both cases the location of the backref Is computed from the
remaining part of the Data As follows:
location = op - A*256 - B - 1
'/
Else
Dim As UInteger len1 = (ctrl Shr 5)
Dim As Ubyte Ptr ref = op - ((ctrl And &h1f) Shl 8) -1
If(len1=7) Then
len1 += *ip
ip+=1 /' i.e. Format #2 '/
End If
len1 += 2 /' Len Is Now #octets '/
If(op + len1 > out_end)Then
ip -= Iif(len1 >= 9, 2 , 1) /' Rewind To control Byte '/
my_guess()
End If
If(ip >= in_end) Then Return LZFX_ECORRUPT
ref -= *ip : ip += 1
If(ref < obuf) Then Return LZFX_ECORRUPT
Do
*op = *ref : op+= 1 : ref+=1
len1 -=1
Loop While (len1 <> 0 )
End If
Loop While (ip < in_end)
olen = op - obuf
Return 0
End Function