Page 1 of 1

zlib.lib - Decompression! Grrrrr! lol.

Posted: Tue Oct 08, 2013 4:10 am
by Thunder93
Hi all. I'm puzzled beyond imagination! I have a problem and only speculations and I've exhausted all other options before bothering for aid.

When using uncompress() it returns -3 Data Error. Is it the wrong command to decompress HTTP/1.1 gzip page with zlib? Any aid would be much appreciated..., like always I'm very grateful for any and all support.

In UnCompressedLength: 559903
CompressedLength - ReadLong: 0
CompressedLength Lof(): 3941
*UnCompressed MemorySize: 559903
*Compressed MemorySize: 3941
PackStatus: -3 ....0 is Good, -3 is Data Error
Anything stored in *UnCompressed?:
CRC32 from File: 1541735168
CRC32Fingerprint: 786209370
UnpackedMemory: 5369017152

Code: Select all

ImportC "zlib.lib"  
  uncompress(*UnCompressed, *UnCompressedLength, *Compressed, CompressedLength)  
EndImport

ReadOK = ReadFile(#PB_Any, "gzip-webpage.gz")
If ReadOK
  UnCompressedLength = ReadLong(ReadOK)  ; g15765

  If UnCompressedLength : Debug "In UnCompressedLength: "+Str(UnCompressedLength)
    CompressedLength.l = ReadLong(ReadOK) : Debug "CompressedLength - ReadLong: "+Str(CompressedLength)
    If CompressedLength = 0 : CompressedLength = Lof(ReadOK) : EndIf
    CRC32.l = ReadLong(ReadOK)
    
    Debug "CompressedLength Lof(): "+CompressedLength
  
    *UnCompressed = AllocateMemory(UnCompressedLength) : Debug "*UnCompressed MemorySize: "+MemorySize(*UnCompressed)
    *Compressed = AllocateMemory(CompressedLength) : Debug "*Compressed MemorySize: "+MemorySize(*Compressed)
    ReadData(ReadOK, *Compressed, CompressedLength)
    CloseFile(ReadOK)    
    
    PackStatus.b = uncompress(*UnCompressed, @UnCompressedLength, *Compressed, CompressedLength)
    Debug "PackStatus: "+PackStatus + " ....0 is Good, -3 is Data Error"
    
    If PackStatus = 0 : Result.l = MemorySize(*UnCompressed) : Else : Result = 0 : EndIf    
    
    If *Compressed : FreeMemory(*Compressed) : EndIf
    UnpackedMemory = @*UnCompressed    
    Debug "Any from *UnCompressed?: "+PeekS(*UnCompressed, UnCompressedLength)
    
    Debug "CRC32 from File: "+CRC32
    Debug "CRC32Fingerprint: "+CRC32Fingerprint(*UnCompressed, UnCompressedLength)   
    Debug "UnpackedMemory: "+Str(UnpackedMemory)
    
;     If CreateFile(0, "MeLikeMeSave.U")
;       WriteData(0, *UnCompressed, MemorySize(*UnCompressed))
;       CloseFile(0)
;     EndIf
  
    If *UnCompressed : FreeMemory(*UnCompressed) : EndIf
  EndIf  
EndIf

Re: zlib.lib - Decompression! Grrrrr! lol.

Posted: Tue Oct 08, 2013 4:38 am
by Thunder93
I think I'm suppose to be using inflateInit2 and inflateEnd... Little tricky

Re: zlib.lib - Decompression! Grrrrr! lol.

Posted: Tue Oct 08, 2013 8:34 am
by JHPJHP
Here is a dll version that only works in the 32bit version of Purebasic (lib version - 32 / 64bit to follow):
- dll downloaded from here: http://zlib.net/

Code: Select all

#ZLIB_VERSION = "1.2.8"
#WANT_GZIP = 16
#Z_FINISH = 4

Structure Z_STREAM
  *next_in.Byte
  avail_in.l
  total_in.l
  *next_out.Byte
  avail_out.l
  total_out.l
  *msg.Byte
  *state
  zalloc.l
  zfree.l
  opaque.l
  data_type.i
  adler.l
  reserved.l
EndStructure

Prototype.i protoInflateInit2(*strm, windowBits.i, version.s, strm_size)
Global InflateInit2.protoInflateInit2

Prototype.i protoInflate(*strm, flush.i)
Global Inflate.protoInflate

Prototype.i protoInflateEnd(*strm)
Global InflateEnd.protoInflateEnd

zlib1 = OpenLibrary(#PB_Any, "zlib1.dll")

If IsLibrary(zlib1)
  InflateInit2 = GetFunction(zlib1, "inflateInit2_")
  Inflate = GetFunction(zlib1, "inflate")
  InflateEnd = GetFunction(zlib1, "inflateEnd")
  gzFile.s = "gzPacket.gz"
  LengthToRead = FileSize(gzFile)
  *Payload = AllocateMemory(LengthToRead)
  LengthToWrite = LengthToRead * 8
  *Output = AllocateMemory(LengthToWrite)
  OpenFile(0, gzFile)
  ReadData(0, *Payload, LengthToRead)
  CloseFile(0)
  strm.Z_STREAM
  strm\next_in = *Payload
  strm\avail_in = LengthToRead
  strm\next_out = *Output
  strm\avail_out = LengthToWrite
  InflateInit2(@strm, #WANT_GZIP, #ZLIB_VERSION, SizeOf(Z_STREAM))
  Inflate(@strm, #Z_FINISH)
  InflateEnd(@strm)
  Debug PeekS(*Output)
  FreeMemory(*Output)
  FreeMemory(*Payload)
  CloseLibrary(zlib1)
EndIf

Re: zlib.lib - Decompression! Grrrrr! lol.

Posted: Tue Oct 08, 2013 4:08 pm
by JHPJHP
I had some trouble with the structure until I found this post: http://www.purebasic.fr/english/viewtop ... +zlib.lib+
- to expand on this (compression / header), read the following sources
-- http://zlib.net/manual.html
-- http://zlib.net/zlib_how.html

Code: Select all

#ZLIB_VERSION = "1.2.8"
#WANT_GZIP = 16
#Z_FINISH = 4

Structure Z_STREAM Align #PB_Structure_AlignC
  *next_in.Byte
  avail_in.l
  total_in.l
  *next_out.Byte
  avail_out.l
  total_out.l
  *msg.Byte
  *state
  zalloc.l
  zfree.l
  opaque.l
  data_type.i
  adler.l
  reserved.l
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    alignment.l
  CompilerEndIf
EndStructure

ImportC "zlib.lib"
  inflateInit2_(*strm, windowBits.i, version.s, strm_size)
  inflate(*strm, flush.i)
  inflateEnd(*strm)
EndImport
gzFile.s = "gzPacket.gz"
LengthToRead = FileSize(gzFile)
*Payload = AllocateMemory(LengthToRead)
LengthToWrite = LengthToRead * 8
*Output = AllocateMemory(LengthToWrite)
OpenFile(0, gzFile)
ReadData(0, *Payload, LengthToRead)
CloseFile(0)
strm.Z_STREAM
strm\next_in = *Payload
strm\avail_in = LengthToRead
strm\next_out = *Output
strm\avail_out = LengthToWrite
inflateInit2_(@strm, #WANT_GZIP, #ZLIB_VERSION, SizeOf(Z_STREAM))
inflate(@strm, #Z_FINISH)
inflateEnd(@strm)
Debug PeekS(*Output)
FreeMemory(*Output)
FreeMemory(*Payload)

Re: zlib.lib - Decompression! Grrrrr! lol.

Posted: Tue Oct 08, 2013 7:53 pm
by Thunder93
Hot damn! Excellent! :D

Code: Select all

#ZLIB_VERSION = "1.2.8"
#WANT_GZIP = 16

#Z_BUF_ERROR     = -5
#Z_FINISH        =  4

Structure Z_STREAM Align #PB_Structure_AlignC
  *next_in.Byte
  avail_in.l
  total_in.l
  *next_out.Byte
  avail_out.l
  total_out.l
  *msg.Byte
  *state
  zalloc.l
  zfree.l
  opaque.l
  data_type.i
  adler.l
  reserved.l
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    alignment.l
  CompilerEndIf
EndStructure

ImportC "zlib.lib"
  inflateInit2_(*strm, windowBits.i, version.s, strm_size)
  inflate(*strm, flush.i)
  inflateEnd(*strm)
EndImport

Procedure.l zlib_inflateInit2(*dest, *destLen, *source, sourceLen.l)
  Static strm.Z_STREAM
  With strm
    \zalloc = #Null
    \zfree  = #Null
    \opaque = #Null
    
    \next_in  = *source
    \avail_in = sourceLen
    \total_in = #Null
    
    \next_out   = *dest
    \avail_out  = *destLen
    \total_out  = #Null    
  EndWith
  
  Static err.l
  
  err.l = inflateInit2_(@strm, #WANT_GZIP, #ZLIB_VERSION, SizeOf(Z_STREAM))
  If err <> 0 : ProcedureReturn err : EndIf
  
  err = inflate(@strm,#Z_FINISH)
  
  If err <> 1
    inflateEnd(@strm)
    If err = 0
      ProcedureReturn #Z_BUF_ERROR
    Else
      ProcedureReturn err
    EndIf
  EndIf
  PokeL(*destLen,strm\total_out)
  ProcedureReturn inflateEnd(@strm)  
EndProcedure

Procedure.s ExplainIt(val)
  Str.s
  If val = 0 : Str = "#Z_OK" : EndIf
  If val = 1 : Str = "#Z_STREAM_END" : EndIf
  If val = 2 : Str = "#Z_NEED_DICT" : EndIf
  If val = -1 : Str = "#Z_ERRNO" : EndIf
  If val = -2 : Str = "#Z_STREAM_ERROR" : EndIf
  If val = -3 : Str = "#Z_DATA_ERROR" : EndIf
  If val = -4 : Str = "#Z_MEM_ERROR" : EndIf
  If val = -5 : Str = "#Z_BUF_ERROR" : EndIf
  If val = -6 : Str = "#Z_VERSION_ERROR" : EndIf
  ProcedureReturn Str
EndProcedure

ReadOK = ReadFile(#PB_Any, "tPacket.gz")
If ReadOK
  CompressedLength = Lof(ReadOK) : UnCompressedLength = CompressedLength * 4.0003
  
  If UnCompressedLength    
    *Compressed = AllocateMemory(CompressedLength)
    If Not *Compressed : Debug "Memory Allocation for Compressed has failed." : Goto Cleanup  : EndIf     
    If Not ReadData(ReadOK, *Compressed, CompressedLength) : Debug "Error reading file data." : EndIf         
    CloseFile(ReadOK)        
    
    *UnCompressed = AllocateMemory(UnCompressedLength)
    If Not *UnCompressed : Debug "Memory Allocation for *UnCompressed has failed." : Goto Cleanup : EndIf 
    Z_Error.b = zlib_inflateInit2(*UnCompressed, @UnCompressedLength, *Compressed, CompressedLength)
    If Not Z_Error
      Debug PeekS(*UnCompressed, UnCompressedLength)
    Else : 
      Debug "zlib_inflateInit2_(): Error "+ExplainIt(Z_Error)
      FakeReturn    
    EndIf    
    
    Cleanup:
    If *Compressed : FreeMemory(*Compressed) : EndIf
    If *UnCompressed : FreeMemory(*UnCompressed) : EndIf
  EndIf
EndIf

Re: zlib.lib - Decompression! Grrrrr! lol.

Posted: Tue Oct 08, 2013 7:55 pm
by Thunder93
Oooh Noooo... I used Goto! Where's the PB police? :lol:

Re: zlib.lib - Decompression! Grrrrr! lol.

Posted: Tue Oct 08, 2013 8:13 pm
by Kiffi
Thunder93 wrote:Oooh Noooo... I used Goto! Where's the PB police? :lol:
Image
You're under arrest!

Re: zlib.lib - Decompression! Grrrrr! lol.

Posted: Tue Oct 08, 2013 8:31 pm
by Thunder93
lol! I knew it was just a matter of time before my PB coding gets me into troubles... :twisted:

Re: zlib.lib - Decompression! Grrrrr! lol.

Posted: Wed Oct 09, 2013 10:36 pm
by JHPJHP
Here is an update to my previous posts:
- the example below will take a gzip page from memory (need to supply that part yourself), inflate it, deflate it, and inflate it again
- GetHeader & AddHeader still need some work

Invaluable Document: http://tools.ietf.org/html/rfc1952

Code: Select all

#ZLIB_VERSION = "1.2.8"
#ENABLE_GZIP = 16
#ENABLE_ZLIB_GZIP = 32
#Z_FINISH = 4
#Z_DEFAULT_COMPRESSION = -1
#Z_DEFLATED = 8
#MAX_MEM_LEVEL = 9
#Z_DEFAULT_STRATEGY = 0
#Z_NULL = 0
#OS_NTFS = 11

Structure Z_STREAM Align #PB_Structure_AlignC
  *next_in.Byte
  avail_in.l
  total_in.l
  *next_out.Byte
  avail_out.l
  total_out.l
  *msg.Byte
  *state
  zalloc.l
  zfree.l
  opaque.l
  data_type.i
  adler.l
  reserved.l
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    alignment.l
  CompilerEndIf
EndStructure

Structure GZ_HEADER Align #PB_Structure_AlignC
  text.i
  time.l
  xflags.i
  os.i
  *extra.Byte
  extra_len.l
  extra_max.l
  *name.Byte
  name_max.l
  *comment.Byte
  comm_max.l
  hcrc.i
  done.i
EndStructure

ImportC "zlib.lib"
  inflateInit2_(*strm, windowBits.i, Version.s, strm_size)
  inflateGetHeader(*strm, *head)
  inflate(*strm, flush.i)
  inflateEnd(*strm)
  deflateInit2_(*strm, level.i, method.i, windowBits.i, memlevel.i, strategy.i, Version.s, strm_size)
  deflateBound(*strm, sourceLen)
  deflateSetHeader(*strm, *head)
  Deflate(*strm, flush.i)
  deflateEnd(*strm)
EndImport

Procedure.s InflatePayload(*Payload, windowBits.i, GetHeader.b = #False)
  LengthToRead = MemorySize(*Payload)
  LengthToWrite = LengthToRead * 8
  *Output = AllocateMemory(LengthToWrite)
  strm.Z_STREAM
  strm\next_in = *Payload
  strm\avail_in = LengthToRead
  strm\next_out = *Output
  strm\avail_out = LengthToWrite
  strm\zalloc = #Z_NULL
  strm\zfree = #Z_NULL
  strm\opaque = #Z_NULL
  inflateInit2_(@strm, windowBits, #ZLIB_VERSION, SizeOf(Z_STREAM))
  
  If GetHeader
    head.GZ_HEADER
    inflateGetHeader(@strm, @head)
  EndIf
  inflate(@strm, #Z_FINISH)
  inflateEnd(@strm)
  sOutput.s = PeekS(*Output, -1, #PB_UTF8)
  FreeMemory(*Output)
  ProcedureReturn sOutput
EndProcedure

Procedure DeflatePayload(sInput.s, windowBits.i, AddHeader.b = #False)
  LengthToRead = StringByteLength(sInput)
  strm.Z_STREAM
  strm\next_in = @sInput
  strm\avail_in = LengthToRead
  strm\zalloc = #Z_NULL
  strm\zfree = #Z_NULL
  strm\opaque = #Z_NULL
  deflateInit2_(@strm, #Z_DEFAULT_COMPRESSION, #Z_DEFLATED, windowBits, #MAX_MEM_LEVEL, #Z_DEFAULT_STRATEGY, #ZLIB_VERSION, SizeOf(Z_STREAM))
  LengthToWrite = deflateBound(@strm, LengthToRead)
  *Payload = AllocateMemory(LengthToWrite)
  strm\next_out = *Payload
  strm\avail_out = LengthToWrite
  
  If AddHeader
    sysTime.SYSTEMTIME
    head.GZ_HEADER
    head\text = #True
    head\time = GetSystemTime_(sysTime)
    head\os = #OS_NTFS
    head\extra = #Z_NULL
    head\name = #Z_NULL
    head\comment = #Z_NULL
    head\hcrc = #False
    deflateSetHeader(@strm, @head)
  EndIf
  Deflate(@strm, #Z_FINISH)
  deflateEnd(@strm)
  ProcedureReturn *Payload
EndProcedure

;...
sOutput.s = InflatePayload(*Payload, 15 | #ENABLE_GZIP)
FreeMemory(*Payload)
*Payload = AllocateMemory(Len(sOutput))
*Payload = DeflatePayload(sOutput, 15)
Debug InflatePayload(*Payload, 15)
FreeMemory(*Payload)

Re: zlib.lib - Decompression! Grrrrr! lol.

Posted: Thu Oct 10, 2013 5:10 am
by JHPJHP
A small update has been applied to the previous post, based on an email response I received from Mark Adler (co-creator of zlib).

I only mention this because he was responding to a question I had about inflateGetHeader & deflateSetHeader. He sent me an example of each function in use, and if anyone is interested in the original files - I'll temporarily post a link.

I will at some point be converting it to PureBasic, but I don't have a guaranteed timeline. :)

Cheers!

Re: zlib.lib - Decompression! Grrrrr! lol.

Posted: Fri Oct 11, 2013 7:04 pm
by RichAlgeni
Thunder93 wrote:lol! I knew it was just a matter of time before my PB coding gets me into troubles... :twisted:
You have the right to remain silent. :shock:

I also have the right to remain silent.

I just don't have the ability. :mrgreen: