Jump to content
Sign in to follow this  
Nighter

zlib dllcall with autoit

Recommended Posts

Nighter

Hi all

I've got the following problem:

I've got a file in an unkown file format. I know after serveral days, that the file contains a file header and a lot of zlib compress files. (tga, xml .. )

I've got the filenumber of all files in this file, the table of content, the offset of every file beginning, the filename, and a lot of other things.

One of these other things is the binary data of the compressed file. it always starts with 78 9C which indicates a zlib stream ..

I've tried to make a dllcall with zlib to uncompress or whatever this zlibstream data.

I've tried with the uncompress function from zlib, but this works for textfiles only. (txt, xml..)

and then the binary data is directly translated into plain text.

here is my dllcall working for textfiles

$InBufferSize=$fileinfos[$m][5] ;$fileinfos[$m][5] contains the bytelength of the zlib datastream which begins with 78 9C (read from file) 
logtofile("InBufferSize: " & $InBufferSize) ;just a log funtion
$InBuffer=DllStructCreate("ubyte["& $InBufferSize  &"]") 
DllStructSetData($InBuffer,1,$fileinfos[$m][18]) ;databuffer which now contains the zlib datastream (hopefully) starting with 78 9C 
                                        
                        
$OutBufferSize=$fileinfos[$m][4] ;outputbuffer size, was read from file before
logtofile("OutBufferSize: " & $OutBufferSize)
$OutBufferSizePtr=DllStructCreate("ulong")
DllStructSetData($OutBufferSizePtr,1,$OutBufferSize)
                
$OutBuffer=DllStructCreate("char["& $OutBufferSize  &"]")       
$dllzlibhandle=DllOpen(@ScriptDir &'\'&"zlib1.dll")
if @error =-1 Then
msgbox(0,"Error opening", "error opening dll")
EndIf       
$result=DllCall($dllzlibhandle, "int:cdecl", "uncompress", "ptr", DllStructGetPtr($OutBuffer), "ptr", DllStructGetPtr($OutBufferSizePtr), "ptr", DllStructGetPtr($InBuffer), "udword", $InBufferSize)       
;For debug: display results
_ArrayDisplay($result, 'zlib.dll result: if unzipped correctly, first value should be 0.')
                
                
$filewritehandle=FileOpen($fileinfos[$m][9],17)
if @error=-1 Then
    msgbox(0,"error in opening file", "error in opening file " & $fileinfos[$m][9])
endif
FileWrite($filewritehandle,DllStructGetData($OutBuffer,1))
logtofile(DllStructGetData($OutBuffer,1) )

so if anyone could explain me how to work with this inflate function in autoit, please .. i've little knowledge about these kind of structs and alloc functions and so on ..

best regards

Nighter

Share this post


Link to post
Share on other sites
Nighter

hmm is my question too confusing?? or not detailed enough?? ^^ or noone with advanced dllcall,dllstruct or zlib knowledge have visited the forum today :mellow:

Share this post


Link to post
Share on other sites
ProgAndy

Just try with this struct and if it doesn't work, keave it out :mellow:

$tagInternal_state = ... Internal state struct -> http://193.95.242.3/canorusNightly/doc-0.5/deflate_8h-source.html
$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 */
    "Bytef    *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 */
    $tagInternal_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 */

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
Nighter

ok now i've got the structure, thanks :mellow: but what about the functions zalloc and zfree and the internal state struc.. must i create this structure too ond then getptr on this structure?

i'm confused .. :(

is anybody out there with autoit and zlib knowledge who could explain me how to inflate da zlib datastream with autoit and write this inflate stream to a file????

Best regards

Nighter

Share this post


Link to post
Share on other sites
Nighter

ok now i've got my struct

$tagz_stream_s ="ptr nextinput;uint inputlength;ulong totalinput;ptr nextoutput;uint outputlength;ulong totaloutput;ptr messagefromstruct;ptr internalstatefunction;ptr allocmem;ptr freemem;ptr opaquefunc;int datatype;ulong adlercheck;ulong reservedvar"        

$z_stream_struc=DllStructCreate($tagz_stream_s)oÝ÷ Ú'â[azËk¹ËpØZºÚ"µÍÝXÝÙ]]J ÌÍÞÜÝX[WÜÝXË    ][ÝÛ^[]   ][ÝËÝXÝÙ]  ÌÍÒ[YJBÝXÝÙ]]J    ÌÍÞÜÝX[WÜÝXË    ][ÝÚ[][Ý ][ÝË  ÌÍÙ[Z[ÜÖÉÌÍÛWVÍWJBÝXÝÙ]]J  ÌÍÞÜÝX[WÜÝXË    ][ÝÛ^Ý]] ][ÝËÝXÝÙ]  ÌÍÓÝ]YJBÝXÝÙ]]J  ÌÍÞÜÝX[WÜÝXË    ][ÝÛÝ]][Ý   ][ÝË  ÌÍÙ[Z[ÜÖÉÌÍÛWVÍJBÝXÝÙ]]J   ÌÍÞÜÝX[WÜÝXË    ][ÝØ[ØÛY[I][ÝË
BÝXÝÙ]]J ÌÍÞÜÝX[WÜÝXË    ][ÝÙY[Y[I][ÝË
BÝXÝÙ]]J ÌÍÞÜÝX[WÜÝXË    ][ÝÛÜ]YY[É][ÝË
oÝ÷ Úz0Úò¶[ayû§rب)ß«^"x­jëh×6$infinitreturn=DllCall(@ScriptDir &'\'&"zlib1.dll","int:cdecl", "inflateInit", "ptr",DllStructGetPtr($z_stream_struc))
If @error<>0 Then 
    MsgBox(0,"Error", "init call fails" & @error)
EndIf
_ArrayDisplay($infinitreturn, 'inflateinitreturn')

after this call @error is set to 3 which normally indicates that the function was not found in the dll file.

the dllcall returns -2 as result, which indicates Z_STREAM_ERROR, an error with the struct.

:mellow: now i'm confused ... what happend now?

inflateInit expects the following:

int inflateInit (z_streamp strm);

where z_streamp is

typedef z_stream FAR *z_streamp

so why is my dll call not correct ?? any ideas??

Share this post


Link to post
Share on other sites
ProgAndy

try to use ptr* (a reference to a pointer :mellow: )


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
rover

"inflateInit" should be "inflateInit_"

you can view DLL exports spelling and case with Dll Export Viewer

nirsoft.net


I see fascists...

Share this post


Link to post
Share on other sites
Nighter

try to use ptr* (a reference to a pointer )

ehem sorry for this dumb question, but WHERE shall I use ptr*????

:mellow:

i belive i missed the day in shool when we tlked about pointers ^^

Share this post


Link to post
Share on other sites
ProgAndy

Oh, sorry ... you don't have to use it.

I hate those typedefs with structurep = *structure

I always think this way: structurep is a pointer to a struct -> use "ptr"

then: in dllCall, you need ptr to structure

--> my wrong thought: ptr(structurep) to ptr(dllCall) is ptr*

(but acutally structurep defines already the pointer i wnat to use in DLLCall -> it's NOT a ptr to ptr but just a "ptr" :mellow: )

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
Nighter

ok step by steps its working (i think)

thanx for the hint with the dll exporter.. the function is really called inflateInit_ .. dunno why in all zlib documentations its called infalteInit without _

next step..

inflateInit_ returns -6 as i can see with my _ArrayDisplay this normally indicates a version error..

ok i tried the zlib.dll that was delivered with the files itself .. but i cant call this specific dll

everytime i try to call it with the dll my the scriptrun crashes ..

and scite shows me

AutoIT3.exe ended.rc:-1073741674

any ideas?

i'll attach the zlib file which was delivered with the file i want to "unpack". the zlib form the zlib site is called zlib1.dll (normally)

zlib.dll

Share this post


Link to post
Share on other sites
Nighter

Ok next step ...

i used the 1.2.3 dll file of zlib now .. (the 1.1.3 wont work for me donno why)

inflateInit (which is described in the documentation) is a macro for inflateinit_ .. but this macro is not exported .. :mellow:

ok i tried to use inflateinit_ ... this function needs a pointer for a structure, the char version of zlib and a int stream size.

the pointer for the structure is no problem, the version isn't also a problem, but the stream size..

because the streamsize will be compared with the streamsize of z_stream

if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
      stream_size != sizeof(z_stream))
      return Z_VERSION_ERROR;

the only point where i can find something about z_stram and not z_stramp is here

typedef struct z_stream_s {
    Bytef   *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 */

    Bytef   *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 */

    char     *msg;    /* last error message, NULL if no error */
    struct internal_state FAR *state; /* not visible by applications */

    alloc_func zalloc;  /* used to allocate the internal state */
    free_func  zfree;   /* used to free the internal state */
    voidpf   opaque;  /* private data object passed to zalloc and zfree */

    int  data_type;  /* best guess about the data type: ascii or binary */
    uLong   adler;    /* adler32 value of the uncompressed data */
    uLong   reserved;   /* reserved for future use */
} z_stream;

typedef z_stream FAR *z_streamp;

and i'm honest i don't know what it means .. :/ is z_stream now an alias or something linke that for the structure z_stream_s?

and is z_streamp just a pointer to the alias z_stream????

how shall i know how big sizeof(z_stream) is?

Share this post


Link to post
Share on other sites
ProgAndy

Yes, z_streamp is an alias for pointer to z_stream and z_stream is the struct _stream_s

the size of zStream: DllstructGetSize :mellow:

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
Nighter

:(:mellow:

thats IT ;) ;) :o thank you very much, the txtfiles works perfect (like with uncompress) next step are the imagefiles :D

maybe i'll need your help again :)

praise ProgAndy

:D

Share this post


Link to post
Share on other sites
shovetech

:(:graduated:

thats IT :D :D :D thank you very much, the txtfiles works perfect (like with uncompress) next step are the imagefiles :D

maybe i'll need your help again :D

praise ProgAndy

:D

@Nighter,

I'm having the same problem trying to use the Zlib library (v1.2.5 from zlib.net)

Could you post a working sample of code to make inflate work?

I tried using uncompress, but the destination size (decompressed size) is unknown. I'd like to just parse the stream. Here is the example I'm working from:

http://www.zlib.net/zlib_how.html

I've got a struct created, and a DllCall, but using inflateInit_ crashes, while inflateInit returns @error = 3.

Any help would be much appreciated.

Thanks

Share this post


Link to post
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
Sign in to follow this  

×