Jump to content

Gzip to text


Recommended Posts

Dlls for windows are here and here, for Windows CE here, as detailed on the very zlib homepage you referred to (and apparently didn't read; there are also .NET implementations, including dll wrapper in C# if that floats your boat).

Since zlib is (and I quote)

Quote

for use on virtually any computer hardware and operating system

it would make sense that the authors provided the source code first and foremost, not a particular platform-specific implementation. You could just compile it as a dll yourself; I did this a few months ago for my Eigen4AutoIt environment; works great.

Link to comment
Share on other sites

Successfully decompress Zlib string using @ProgAndy code
https://www.autoitscript.com/forum/topic/131184-solved-flatedecode-zlib-pdf/?do=findComment&comment=913425
but it fail to decompress Gzip string. So what changes should be made to make it  work with Gzip.

Zlib code: ( working )

; #FUNCTION# =============================================================================
; Name...........: __ExampleA( Zlib string )
; ========================================================================================
Func __ExampleA()
    ConsoleWrite('>  __ExampleA( Zlib string: Welcome to Autoit )' & @CRLF)
    Local $s_String = '0x789C0B4FCD49CECF4D5528C957702C2DC9CF2C010038DA0666'                            ;~

    Local $o_ZlibFPath = @ScriptDir & '\zlib1.dll'
    $Zlib_Dll = DllOpen($o_ZlibFPath)

    ConsoleWrite('+> ' & BinaryToString(_ZLib_UncompressBinary(Binary($s_String))) & @CRLF)

    DllClose($Zlib_Dll)
    ConsoleWrite('-> Done' & @CRLF)
EndFunc

 

Gzip code (not working) [ returning -3 (DATA_ERROR) ]

from https://stackoverflow.com/a/1838702

To decompress a gzip format file with zlib, call inflateInit2 with the windowBits parameter as 16+MAX_WBITS, like this:
inflateInit2(&stream, 16+MAX_WBITS);

so added:
_ZLib_inflateinit
_ZLib_inflateinit2
_ZLib_InflateEnd
_ZLib_inflate
_ZLib_inflateGetHeader

using this site:
https://www.zlib.net/manual.html
https://zlib.net/zlib_how.html
https://github.com/madler/zlib/blob/master/zlib.h

#include <Array.au3>

Global $Zlib_Dll = Null

__ExampleB()

; #FUNCTION# =============================================================================
; Name...........: __ExampleB( Gzip string )
; ========================================================================================
Func __ExampleB()
    ConsoleWrite('>  __ExampleA( Gzip string: Welcome to Autoit )' & @CRLF)

    Local Const $Z_MAX_WBITS = 15
    Local Const $tagz_stream_s = _
    "ptr next_in;" & _      ; /* next input byte */
    "uInt avail_in;" & _    ; /* number of bytes available at next_in */
    "uLong total_in;" & _   ; /* total nb of input bytes read so far */
    "ptr next_out;" & _     ; /* next output byte should be put there */
    "uInt avail_out;" & _   ; /* remaining free space at next_out */
    "uLong total_out;" & _  ; /* total nb of bytes output so far */
    "ptr msg;" & _          ; /* last error message, NULL if no error */
    "ptr state;" & _        ;
    "ptr zalloc;" & _       ; /* used to allocate the internal state */
    "ptr zfree;" & _        ; /* used to free the internal state */
    "ptr opaque;" & _       ; /* private data object passed to zalloc and zfree */
    "int data_type;" & _    ; /* best guess about the data type: binary or text */
    "uLong adler;" & _      ; /* adler32 value of the uncompressed data */
    "uLong reserved;"       ; /* reserved for future use */
;   ConsoleWrite($tagz_stream_s & @CRLF)

    Local $s_String = '0x1F8B080000000000000B0B4FCD49CECF4D5528C957702C2DC9CF2C0100054D2B3C11000000'    ;~ Welcome to Autoit

    Local $o_ZlibFPath = @ScriptDir & '\zlib1.dll'
    $Zlib_Dll = DllOpen($o_ZlibFPath)

    Local $Stream = DllStructCreate($tagz_stream_s)
    Local $strmPtr = DllStructGetPtr($Stream)
    Local $windowBits = $Z_MAX_WBITS + 16
    Local $ZLIB_VER = _Zlib_Version()
    Local $StrmSize = DllStructGetSize($Stream)

;   _ZLib_inflateinit($strmPtr, $ZLIB_VER, $StrmSize)
;   _ZLib_inflate($strmPtr, 4)

    _ZLib_inflateinit2($strmPtr, $windowBits, $ZLIB_VER, $StrmSize)
    _ZLib_inflateGetHeader($strmPtr)

    ;~ Z_NO_FLUSH(0) | Z_PARTIAL_FLUSH(1) | Z_SYNC_FLUSH(2) | Z_FULL_FLUSH(3) | Z_FINISH(4) |
;   _ZLib_inflate($strmPtr, 4)

    ConsoleWrite('+> ' & BinaryToString(_ZLib_UncompressBinary(Binary($s_String))) & @CRLF)

    _ZLib_InflateEnd($strmPtr)

    DllClose($Zlib_Dll)
    ConsoleWrite('-> Done' & @CRLF)
EndFunc



; #FUNCTION# =============================================================================
; Name...........: _Zlib_Version
; ========================================================================================
Func _Zlib_Version()
    Local $call = DllCall($Zlib_Dll, "str:cdecl", "zlibVersion")
    Return $call[0]
EndFunc   ;==>_Zlib_Version

; #FUNCTION# =============================================================================
; Name...........: _Zlib_Uncompress
; modified by ProgAndy
; ========================================================================================
Func _Zlib_Uncompress($CompressedPtr, ByRef $CompressedSize, $UncompressedPtr, $UncompressedSize)
    Local $call = DllCall($Zlib_Dll, "int:cdecl", "uncompress", "ptr", $UncompressedPtr, "long*", $UncompressedSize, "ptr", $CompressedPtr, "long", $CompressedSize)
    If @error Then Return SetError(1,0,-7)
    $CompressedSize = $call[2]
    ConsoleWrite('>  _Zlib_Uncompress( @error: ' & $call[0] & ' )' & @CRLF)
    Return $call[0]
EndFunc   ;==>_Zlib_Uncompress

; #FUNCTION# =============================================================================
; Name...........: _ZLib_UncompressBinary
; ProgAndy
; ========================================================================================
Func _ZLib_UncompressBinary($bBinary, $iLength = 0)
    Local $tBuf, $iSize, $iRes
    Local $tBin = DllStructCreate("byte[" & BinaryLen($bBinary) & "]")
    DllStructSetData($tBin, 1, $bBinary)
    If $iLength < 1 Then $iLength = DllStructGetSize($tBin) * 2
    $bBinary = 0

    Local $i = 1
    Do
        $tBuf = DllStructCreate("byte[" & ($iLength * $i) & "]")
        $iSize = DllStructGetSize($tBin)
        $iRes = _Zlib_Uncompress(DllStructGetPtr($tBin), $iSize, DllStructGetPtr($tBuf), DllStructGetSize($tBuf))
        $i += 1
    Until $iRes <> -5

    ConsoleWrite('>  _ZLib_UncompressBinary( @error: ' & $iRes & ' )' & @CRLF)
    If $iRes <> 0 Then Return SetError($iRes, 0, "")
    $tBin = 0
    Return DllStructGetData(DllStructCreate("byte[" & $iSize & "]", DllStructGetPtr($tBuf)), 1)
EndFunc

; #FUNCTION# =============================================================================
; Name...........: _ZLib_inflateinit
; ========================================================================================
Func _ZLib_inflateinit(Byref $strmPtr, $ZLIB_VER, $StrmSize)
    ;~ ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, const char *version, int stream_size));

    Local $call = DllCall($Zlib_Dll, "int:cdecl", "inflateInit_", "ptr", $strmPtr, "STR", $ZLIB_VER, "long", $StrmSize)
    If @error Then Return SetError(1,0,-7)
    ConsoleWrite('>  _ZLib_inflateinit( @error: ' & $call[0] & ' )' & @CRLF)
EndFunc

; #FUNCTION# =============================================================================
; Name...........: _ZLib_inflateinit2
; ========================================================================================
Func _ZLib_inflateinit2(Byref $strmPtr, $windowBits, $ZLIB_VER, $StrmSize)
    ;~ ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int  windowBits, const char *version, int stream_size));

    Local $call = DllCall($Zlib_Dll, "int:cdecl", "inflateInit2_", "ptr", $strmPtr, "long", $windowBits, "STR", $ZLIB_VER, "long", $StrmSize)
    If @error Then Return SetError(1,0,-7)
    ConsoleWrite('>  _ZLib_inflateinit2( @error: ' & $call[0] & ' )' & @CRLF)
EndFunc

; #FUNCTION# =============================================================================
; Name...........: _ZLib_InflateEnd
; ========================================================================================
Func _ZLib_InflateEnd(Byref $strmPtr)
    ;~ ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
    Local $call = DllCall($Zlib_Dll, "int:cdecl", "inflateEnd", "ptr", $strmPtr)
    If @error Then Return SetError(1,0,-7)
    ConsoleWrite('>  _ZLib_InflateEnd( @error: ' & $call[0] & ' )' & @CRLF)
EndFunc

; #FUNCTION# =============================================================================
; Name...........: _ZLib_inflate
; ========================================================================================
Func _ZLib_inflate(Byref $strmPtr, $Z_FULL_Param = 0)
    ;~ ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
    Local $call = DllCall($Zlib_Dll, "int:cdecl", "inflate", "ptr", $strmPtr, "long", $Z_FULL_Param)
    If @error Then Return SetError(1,0,-7)
    ConsoleWrite('>  _ZLib_inflate( @error: ' & $call[0] & ' )' & @CRLF)
EndFunc

; #FUNCTION# =============================================================================
; Name...........: _ZLib_inflateGetHeader
; ========================================================================================
Func _ZLib_inflateGetHeader(Byref $strmPtr)
    ;~ ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, gz_headerp head));

    Local Const $tagz_gz_header_s = _
    "int text;" & _         ; /* true if compressed data believed to be text */
    "uLong time;" & _       ; /* modification time */
    "int xflags;" & _       ; /* extra flags (not used when writing a gzip file) */
    "int os;" & _           ; /* operating system */
    "ptr extra;" & _        ; /* pointer to extra field or Z_NULL if none */
    "uInt extra_len;" & _   ; /* extra field length (valid if extra != Z_NULL) */
    "uInt extra_max;" & _   ; /* space at extra (only when reading header) */
    "ptr name;" & _         ; /* pointer to zero-terminated file name or Z_NULL */
    "uInt name_max;" & _    ; /* space at name (only when reading header) */
    "ptr comment;" & _      ; /* pointer to zero-terminated comment or Z_NULL */
    "uInt comm_max;" & _    ; /* space at comment (only when reading header) */
    "int hcrc; " & _        ; /* true if there was or will be a header crc */
    "int done;"             ; /* true when done reading gzip header (not used when writing a gzip file) */
    ;ConsoleWrite($tagz_gz_header_s & @CRLF)

    Local $gz_header = DllStructCreate($tagz_gz_header_s)
    Local $gz_headerPtr = DllStructGetPtr($gz_header)

    Local $call = DllCall($Zlib_Dll, "int:cdecl", "inflateGetHeader", "ptr", $strmPtr, "ptr", $gz_headerPtr)
    If @error Then Return SetError(1,0,-7)
    ConsoleWrite('>  _ZLib_inflateGetHeader( @error: ' & $call[0] & ' )' & @CRLF)
EndFunc

I try but not good with this dll stuff. so it would be great if someone rectify the above code :D

Link to comment
Share on other sites

You need to do it like this:

#include <Array.au3>

Global $Zlib_Dll = Null

__ExampleB()

; #FUNCTION# =============================================================================
; Name...........: __ExampleB( Gzip string )
; ========================================================================================
Func __ExampleB()
    ConsoleWrite('>  __ExampleA( Gzip string: Welcome to Autoit )' & @CRLF)
    Global Const $Z_OK = 0
    Global Const $Z_STREAM_END = 1
    Local Const $Z_MAX_WBITS = 15
    Local Const $tagz_stream_s = _
            "ptr next_in;" & _ ; /* next input byte */
            "uInt avail_in;" & _ ; /* number of bytes available at next_in */
            "uLong total_in;" & _ ; /* total nb of input bytes read so far */
            "ptr next_out;" & _ ; /* next output byte should be put there */
            "uInt avail_out;" & _ ; /* remaining free space at next_out */
            "uLong total_out;" & _ ; /* total nb of bytes output so far */
            "ptr msg;" & _  ; /* last error message, NULL if no error */
            "ptr state;" & _ ;
            "ptr zalloc;" & _ ; /* used to allocate the internal state */
            "ptr zfree;" & _ ; /* used to free the internal state */
            "ptr opaque;" & _ ; /* private data object passed to zalloc and zfree */
            "int data_type;" & _ ; /* best guess about the data type: binary or text */
            "uLong adler;" & _ ; /* adler32 value of the uncompressed data */
            "uLong reserved;" ; /* reserved for future use */

    Local Const $tagz_gz_header_s = _
            "int text;" & _ ; /* true if compressed data believed to be text */
            "uLong time;" & _ ; /* modification time */
            "int xflags;" & _ ; /* extra flags (not used when writing a gzip file) */
            "int os;" & _   ; /* operating system */
            "ptr extra;" & _ ; /* pointer to extra field or Z_NULL if none */
            "uInt extra_len;" & _ ; /* extra field length (valid if extra != Z_NULL) */
            "uInt extra_max;" & _ ; /* space at extra (only when reading header) */
            "ptr name;" & _ ; /* pointer to zero-terminated file name or Z_NULL */
            "uInt name_max;" & _ ; /* space at name (only when reading header) */
            "ptr comment;" & _ ; /* pointer to zero-terminated comment or Z_NULL */
            "uInt comm_max;" & _ ; /* space at comment (only when reading header) */
            "int hcrc; " & _ ; /* true if there was or will be a header crc */
            "int done;"     ; /* true when done reading gzip header (not used when writing a gzip file) */

    Local $binString = '0x1F8B080000000000000B0B4FCD49CECF4D5528C957702C2DC9CF2C0100054D2B3C11000000'    ;~ Welcome to Autoit
    Local $o_ZlibFPath = @ScriptDir & '\zlib1.dll'
    Local $Zlib_Dll = DllOpen($o_ZlibFPath)

    Local $sVersion = DllCall($Zlib_Dll, "str:cdecl", "zlibVersion")[0]
    Local $iRequiredSize = 0

    For $iFlag = 1 To 2

        Local $tStream = DllStructCreate($tagz_stream_s)
        Local $iSizeStream = DllStructGetSize($tStream)

        Local $iSizeBinBuffer = BinaryLen($binString)
        Local $tInBuffer = DllStructCreate("byte[" & $iSizeBinBuffer & "]")
        DllStructSetData($tInBuffer, 1, $binString)
        $tStream.next_in = DllStructGetPtr($tInBuffer)
        $tStream.avail_in = $iSizeBinBuffer

        Local $aCall = DllCall($Zlib_Dll, "int:cdecl", "inflateInit2_", "ptr", DllStructGetPtr($tStream), "int", $Z_MAX_WBITS + 32, "str", $sVersion, "int", $iSizeStream)

        Local $tHeader = DllStructCreate($tagz_gz_header_s)
        Local $aCall = DllCall($Zlib_Dll, "int:cdecl", "inflateGetHeader", "ptr", DllStructGetPtr($tStream), "ptr", DllStructGetPtr($tHeader))


        If $iFlag = 1 Then
            Local $tTempBuffer = DllStructCreate("char[1024]")
            Local $ilenTempBuffer = DllStructGetSize($tTempBuffer)
        Else
            Local $ilenTempBuffer = $iRequiredSize
            Local $tTempBuffer = DllStructCreate("char[" & $ilenTempBuffer & "]")
        EndIf

        Local $iRet = 0
        While True
            $tStream.next_out = DllStructGetPtr($tTempBuffer)
            $tStream.avail_out = $ilenTempBuffer
            $iRet = DllCall($Zlib_Dll, "int:cdecl", "inflate", "ptr", DllStructGetPtr($tStream), "int", 0)[0]
            If Not (($tStream.avail_out = 0) And (($iRet <> $Z_STREAM_END))) Then
                ExitLoop
            EndIf
        WEnd
        If $iFlag = 1 Then
            $iRequiredSize = $tStream.total_out
        EndIf
        DllCall($Zlib_Dll, "int:cdecl", "inflateEnd", "ptr", DllStructGetPtr($tStream))
    Next

    Local $sOutputString = DllStructGetData($tTempBuffer, 1)

    MsgBox(0,"",$sOutputString)

EndFunc   ;==>__ExampleB

Saludos

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...