Page 6 of 6

Re: How to compress with zlib

Posted: Tue Nov 12, 2013 5:25 pm
by Thunder93
I think your right, .. or at least with this zlib. thanks.

Re: How to compress with zlib

Posted: Thu Nov 14, 2013 12:24 am
by Thunder93
Figured out deflateBound(). Everything is working as it should. :)

Code: Select all

#ENABLE_GZIP = 16
; #ZLIB_VERSION = "1.2.8"

#Z_NULL = 0
#Z_OK = 0
#Z_STREAM_END = 1
#Z_FINISH = 4
#Z_BLOCK         =  5

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
  
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    PB_Alignment1.b[4]
  CompilerEndIf
  
  data_type.i
  adler.l
  reserved.l
  
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    PB_Alignment2.b[8]
  CompilerEndIf  
EndStructure

ImportC "zlib.lib"
  zlibVersion()
  inflateInit2_(*strm, windowBits.i, Version.s, strm_size)
  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.l)
  deflate(*strm, flush.i)
  deflateEnd(*strm)
EndImport : Global ZLIB_VERSION$ = PeekS(zlibVersion())

Procedure InflatePayload(*Payload, windowBits.i)
  LengthToRead = MemorySize(*Payload)
  LengthToWrite = FileSize("1test.dds")
  *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))
  inflate(@strm, #Z_FINISH)
  inflateEnd(@strm)
  ProcedureReturn *Output
EndProcedure

#Z_STREAM_ERROR   = -2
#Z_MEM_ERROR         = -4
#Z_VERSION_ERROR  = -6

Procedure.s deflateInitErr(Err.l)
  errorMsg.s
  Select Err
    Case #Z_STREAM_ERROR  
      errorMsg = "Invalid parameter passed in to function."
    Case #Z_MEM_ERROR
      errorMsg = "Insufficient memory."
    Case #Z_VERSION_ERROR
      errorMsg = "The version of zlib.h and the version of the library linked do not match."
    Default
      errorMsg = "Unknown error code."     
  EndSelect
  
  ProcedureReturn errorMsg
EndProcedure

#Z_ERRNO = -1
#Z_DATA_ERROR = -3
#Z_BUF_ERROR = -5

Procedure.s deflateErr(Err.l)
  errorMsg.s
  Select Err
    Case #Z_ERRNO  
      errorMsg = "Error occured while reading file."
    Case #Z_STREAM_ERROR
      errorMsg = "The stream state was inconsistent (e.g., next_in or next_out was NULL)."
    Case #Z_DATA_ERROR
      errorMsg = "The deflate data was invalid or incomplete."
    Case #Z_MEM_ERROR
      errorMsg = "Memory could not be allocated for processing."
    Case #Z_BUF_ERROR
      errorMsg = "Ran out of output buffer for writing compressed bytes."
    Case #Z_VERSION_ERROR
      errorMsg = "The version of zlib.h and the version of the library linked do not match."
    Default
      errorMsg = "Unknown error code."     
  EndSelect
  
  ProcedureReturn errorMsg
EndProcedure

; Debug "zLib Version: "+PeekS(zlibVersion())
Debug "zLib Version: "+ZLIB_VERSION$

#Z_DEFLATED = 8
#Z_DEFAULT_STRATEGY = 0

#MAX_MEM_LEVEL = 9

;Compression Levels
#Z_DEFAULT_COMPRESSION = -1
#Z_NO_COMPRESSION = 0
#Z_BEST_SPEED = 1
#Z_BEST_COMPRESSION  =  9
#Z_NORM_COMPRESSION = 6

Procedure DeflatePayload(*Input, windowBits.i)
  LengthToRead = MemorySize(*Input)
  strm.Z_STREAM
  strm\next_in = *Input
  strm\avail_in = LengthToRead
  strm\opaque = @opaque
  
  strm\zalloc = #Z_NULL
  strm\zfree = #Z_NULL
  
  deflateStatus.l = deflateInit2_(@strm, #Z_BEST_COMPRESSION, #Z_DEFLATED, windowBits | #ENABLE_GZIP, #MAX_MEM_LEVEL, #Z_DEFAULT_STRATEGY, ZLIB_VERSION$, SizeOf(Z_STREAM))
  ; deflateStatus.l = deflateInit2_(@strm, #Z_DEFAULT_COMPRESSION, #Z_DEFLATED, windowBits | #ENABLE_GZIP, #MAX_MEM_LEVEL, #Z_DEFAULT_STRATEGY, ZLIB_VERSION$, SizeOf(Z_STREAM))
  ; deflateStatus.l = deflateInit2_(@strm, #Z_NO_COMPRESSION, #Z_DEFLATED, windowBits | #ENABLE_GZIP, #MAX_MEM_LEVEL, #Z_DEFAULT_STRATEGY, ZLIB_VERSION$, SizeOf(Z_STREAM))
;   deflateStatus.l = deflateInit2_(@strm, #Z_NORM_COMPRESSION, #Z_DEFLATED, windowBits | #ENABLE_GZIP, #MAX_MEM_LEVEL, #Z_DEFAULT_STRATEGY, ZLIB_VERSION$, SizeOf(Z_STREAM))
  
  
  If deflateStatus <> #Z_OK : Debug "deflateInit2_(): "+ deflateInitErr(deflateStatus) : End : EndIf 
  
  LengthToWrite = deflateBound(@strm, LengthToRead)
  
  *data_out  = AllocateMemory(LengthToWrite)
  If *data_out =0 : Debug "data_out Memory Allocation failed." : ProcedureReturn : EndIf
  strm\next_out = *data_out 
  strm\avail_out = LengthToWrite
  
  deflateStatus.l = deflate(@strm, #Z_FINISH)
  If deflateStatus <> #Z_STREAM_END : Debug "deflate(): "+ deflateErr(deflateStatus) : End : EndIf 
  
  *Payload = AllocateMemory(strm\total_out)
  If *Payload =0 : FreeMemory(*data_out) : Debug "Payload Memory Allocation failed." : ProcedureReturn : EndIf
  CopyMemory(*data_out, *Payload, MemorySize(*Payload))
  
  ProcedureReturn *Payload
EndProcedure

If OpenFile(0, "1test.dds")
  LengthToRead = FileSize("1test.dds")
  *Input = AllocateMemory(LengthToRead)
  ReadData(0, *Input, LengthToRead)
  CloseFile(0)
  *Payload = DeflatePayload(*Input, 15)
  
  If CreateFile(0, "test.gz")
    WriteData(0, *Payload, MemorySize(*Payload))
    CloseFile(0)
  EndIf
  FreeMemory(*Payload)
EndIf

If OpenFile(0, "test.gz")
  LengthToRead = FileSize("test.gz")
  *Payload = AllocateMemory(LengthToRead)
  ReadData(0, *Payload, LengthToRead)
  CloseFile(0)
  *Output = InflatePayload(*Payload, 15 | #ENABLE_GZIP)
  
  If CreateFile(0, "test2.dds")
    WriteData(0, *Output, MemorySize(*Output))
    CloseFile(0)
  EndIf
  FreeMemory(*Output)
  FreeMemory(*Payload)
EndIf

Edited: Rev 2. Added couple of memory allocation checks and a FreeMemory() in DeflatePayload() procedure.

Re: How to compress with zlib

Posted: Thu Nov 14, 2013 3:24 am
by Thunder93
Someone owes me a beer! And I'm ready to collect! :lol:

Re: How to compress with zlib

Posted: Wed Sep 21, 2016 8:35 pm
by SeregaZ
it no have some return unpacked file sile? for sure allocate? i make big buffer, write into it, make file... and half of file is 0000000000 :)

Code: Select all

FileSeek(#File, length - 4)
unpsz = ReadLong(#File)
but it is if only for 1 file packed.

Re: How to compress with zlib

Posted: Mon Oct 24, 2016 3:15 am
by JHPJHP
Hi SeregaZ,

A full working version can be found here: Services, Stuff, and Shellhook.
- Stuff\InflateDeflate\InflateDeflate.pb

Re: How to compress with zlib

Posted: Wed Jan 17, 2018 6:12 pm
by SeregaZ
stuck again. it says:
// .DMF files are compressed using zlib, after you decompress a DMF, you will start to read the next chunk of bytes.
but my old code not want to unpack it. even last 4 bytes it is not unpacked size, as before i am unpack vgm files. it is becouse zlib is update to new one? where to get that new one? official cite have source, not dll it self.

Re: How to compress with zlib

Posted: Wed Jan 17, 2018 6:39 pm
by JHPJHP
Hi SeregaZ,

Have you recently downloaded the script from my previous post :?: There has been numerous updates from the time of that post.

NB*: Please post back whether or not the script solves the problem.

Re: How to compress with zlib

Posted: Wed Jan 17, 2018 6:54 pm
by SeregaZ
yes. i am download it, remove first part, where it creates pack and start from unpack. and nothing.

can you try to unpack it?
https://www.dropbox.com/s/3z69t1naotfy4 ... b.zip?dl=1

or it is deflemask creators screwed up?

explanation of that file:
http://www.deflemask.com/DMF_SPECS.txt

with old your code, from that times i am still succesfully unpacked another files - VGZ :) i thought this new one DMF have same system, but probably wrong.

Re: How to compress with zlib

Posted: Wed Jan 17, 2018 7:29 pm
by JHPJHP
Hi SeregaZ,

Change the Inflate_Payload Procedure...
- documentation: Method: Zlib::Inflate#initialize

From:

Code: Select all

Inflate_Initialize(*stream, 16 | #MAX_WBITS)
To:

Code: Select all

Inflate_Initialize(*stream, 15 | #MAX_WBITS)
Viewed from Notepad:
.DelekDefleMask.BReal Ghostbusters Intro
...
NB*: DefleMask was used to test the files.

Re: How to compress with zlib

Posted: Wed Jan 17, 2018 7:58 pm
by SeregaZ
thanks a lot. for my case probably i will need third parameter for Inflate_Payload(*payload, nLength, third) - where will be marker 15 or 16. becouse with 15 not work VGZ, with 16 not work DMF :)))