Jump to content

DllCall to BZIP2.dll method fails when I try to use function


Recommended Posts

Hi guys.

I try to choose an algorithm to compress some string data in-memory, without file medium to store in sqlite as blob. I plan to store about 100k blobs each 80k symbols (uncompressed). First opinion was LZMA udf by Ward, but udf file doesn't exist.

Then i tried to use BZIP2 dll in DllCall, but it simply doesn't work and throws @error = 1.

 

Here is an example:

Spoiler
$hBZDll = DllOpen(@ScriptDir & "\bzip2.dll") ; obtained at http://gnuwin32.sourceforge.net/packages/bzip2.htm

$data = "binbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbinbin"

$Ret = _DB_CompressData($data)
MsgBox(0, $Ret[2], Binary($Ret[0]))

Func _DB_CompressData($data)

    Local $Size_O, $Size_C, $Size_R, $a_pBuffer, $ret[3]
    $Size_O = BinaryLen($data)
    $Size_R = Ceiling(($Size_O*1.01)+600) ; reserve some bytes accordingly to BZIP2 documentation
    
    $a_pBuffer = DllStructCreate('source byte[' & $Size_O & '];sourceLen uint;dest byte[' & $Size_R & '];destLen uint;blockSize100k int;verbosity int;workFactor int;')
    DllStructSetData($a_pBuffer, "source", $data)
    DllStructSetData($a_pBuffer, "sourceLen", $Size_O)
    DllStructSetData($a_pBuffer, "destLen", $Size_R)
    DllStructSetData($a_pBuffer, "blockSize100k", 9) ; this and below are default values in BZIP2 docs
    DllStructSetData($a_pBuffer, "verbosity", 0)
    DllStructSetData($a_pBuffer, "workFactor", 30)
    
    $dllres = DllCall($hBZDll, "int", "BZ2_bzBuffToBuffCompress", _
    "dest", DllStructGetPtr($a_pBuffer, "dest"), _
    "destLen", DllStructGetPtr($a_pBuffer, "destLen"), _ ; destLen is uint * type, so we need a pointer
    "source", DllStructGetPtr($a_pBuffer, "source"), _
    "sourceLen", DllStructGetData($a_pBuffer, "sourceLen"), _
    "blockSize100k", DllStructGetData($a_pBuffer, "blockSize100k"), _
    "verbosity", DllStructGetData($a_pBuffer, "verbosity"), _
    "workFactor", DllStructGetData($a_pBuffer, "workFactor"))
    
    MsgBox(0, "Error", "@error = " & @error & @CRLF & "@extended = " & @extended)
    
    $Size_C = DllStructGetData($a_pBuffer, "destLen")
    
    $Ret[0] = DllStructGetData($a_pBuffer, "dest")
    $Ret[1] = $Size_O
    $Ret[2] = $Size_C

    Return $ret

EndFunc   ;==>_CompressData


#cs
BZ2_bzBuffToBuffCompress function prototype on C++

int BZ2_bzBuffToBuffCompress( char*         dest,
                              unsigned int* destLen,
                              char*         source,
                              unsigned int  sourceLen,
                              int           blockSize100k,
                              int           verbosity,
                              int           workFactor );
#ce

 

 

What should I do with this error? Where am I wrong?

Thanks.

Edited by Hammerfist
Link to comment
Share on other sites

And trancexx , I know about your Native API Compression =) Didn't use it due to low compression ratio - it's 34%. Btw $COMPRESSION_FORMAT_XPRESS_HUFF shows awesome 11% of compression, but I could not make it decompress the data.

_WinAPI_DecompressBuffer($a_pBuffer[0], $Size_O, $a_pBuffer[1], $Size_C, $COMPRESSION_FORMAT_XPRESS_HUFF) shows @error = 10 and @extended = c00000e8.

_WinAPI_CompressBuffer and _WinAPI_DecompressBuffer using the engine $COMPRESSION_FORMAT_XPRESS work very well, but if we add "_HUFF" it throws error.

Link to comment
Share on other sites

If the use is for compression of blob in SQLite storage and if you don't have concurency timing requirement, I'd develop an SQLite extension to compress/decompress data on the fly, keeping the applicative layer clean, simple, blindly ignorant of what's being done under its feet and avoiding manipulating data several times within AutoIt (forcibly slower than within C code).

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Can I write proper subroutines for SQLite using AutoIt? I think no.

And my app is kinda archive. It keeps a database in compressed state and extracts one blob of data on demand, one, two or ten times per day. So i can sacrifice some processor time at the import stage to reach maximum compression.

Applicative layer remains clean enough. I'll simply change the "$data = $uncompressed_blob" to "$data = _DB_UnpackBlob($compressed_blob)".

Link to comment
Share on other sites

Yes you can (create SQLite functions written in AutoIt) but it isn't worth the burden, especially in you "archive" use case.

When the expected benefit is large enough it's much better to write directly an extension function in C and load it at connection time, or auto-load it for all subsequent connections in the same run, or statically include it in a homebrew SQLite.dll build.

If ever you're interessed to add AutoIt functions as extensions to SQLite you can use this (posted on the French forum): https://autoitscript.fr/forum/viewtopic.php?f=21&t=13556&p=94618

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

×
×
  • Create New...