Iczer Posted December 10, 2016 Share Posted December 10, 2016 Basically i need compress Unicode string, transfer it as ASCII string ant then decompress. But for some reason this not working... expandcollapse popup#include-once #include <WinAPISys.au3> #include <WinAPI.au3> #include <WinAPIDiag.au3> ;============================================================================================================================================================================================== $sString = 'The _WinAPI_CompressBuffer() function takes as input an uncompressed buffer and produces its compressed equivalent provided that the compressed data fits within the specified destination buffer. 和一些中文詞' ConsoleWrite("Original: "&$sString & @CRLF & @CRLF) String_CompressString($sString) ConsoleWrite("Compressed: "&$sString & @CRLF & @CRLF) String_ExtractString($sString) ConsoleWrite("Uncompressed: "&$sString & @CRLF & @CRLF) ;============================================================================================================================================================================================== Func String_CompressString(ByRef $sString) Local $bString = StringToBinary($sString,2) ;-------------------------------------------------------------------------------------- Local $iUncompressedSize = BinaryLen($bString) Local $pUncompressedBuffer = _WinAPI_CreateBuffer($iUncompressedSize,0,False) ;-------------------------------------------------------------------------------------- Local $iCompressedSize = $iUncompressedSize Local $pCompressedBuffer = _WinAPI_CreateBuffer($iCompressedSize,0,False) ;-------------------------------------------------------------------------------------- DllStructSetData(DllStructCreate('byte[' & $iUncompressedSize & ']', $pUncompressedBuffer), 1, $bString) ;-------------------------------------------------------------------------------------- $iSize = _WinAPI_CompressBuffer($pUncompressedBuffer, $iUncompressedSize, $pCompressedBuffer, $iCompressedSize, BitOR($COMPRESSION_FORMAT_LZNT1,$COMPRESSION_ENGINE_MAXIMUM)) If Not @error Then $sString = BinaryToString(DllStructGetData(DllStructCreate('byte[' & $iSize & ']', $pCompressedBuffer), 1)) Else $iext = @extended $sString = "@error = " & @error & " @extended = " & $iext & "|"& _WinAPI_GetLastErrorMessage() & "|" & _WinAPI_GetErrorMessage(_WinAPI_NtStatusToDosError ($iext)) & "|" EndIf ;-------------------------------------------------------------------------------------- _WinAPI_FreeMemory($pCompressedBuffer) _WinAPI_FreeMemory($pUncompressedBuffer) ;-------------------------------------------------------------------------------------- EndFunc ;============================================================================================================================================================================================== Func String_ExtractString(ByRef $sString) Local $bString = StringToBinary($sString) ;-------------------------------------------------------------------------------------- Local $iCompressedSize = BinaryLen($bString) Local $pCompressedBuffer = _WinAPI_CreateBuffer($iCompressedSize,0,False) ;-------------------------------------------------------------------------------------- Local $iUncompressedSize = $iCompressedSize * 8 Local $pUncompressedBuffer = _WinAPI_CreateBuffer($iUncompressedSize,0,False) ;-------------------------------------------------------------------------------------- DllStructSetData(DllStructCreate('byte[' & $iCompressedSize & ']', $pCompressedBuffer), 1, $bString) ;-------------------------------------------------------------------------------------- $iSize = _WinAPI_DecompressBuffer($pUncompressedBuffer, $iUncompressedSize, $pCompressedBuffer, $iCompressedSize, BitOR($COMPRESSION_FORMAT_LZNT1,$COMPRESSION_ENGINE_MAXIMUM)) If Not @error Then $sString = BinaryToString(DllStructGetData(DllStructCreate('byte[' & $iSize & ']', $pUncompressedBuffer), 1),4) $sString = _WinAPI_WideCharToMultiByte($sString,65001,True) Else $iext = @extended $sString = "@error = " & @error & " @extended = " & $iext & "|"& _WinAPI_GetLastErrorMessage() & "|" & _WinAPI_GetErrorMessage(_WinAPI_NtStatusToDosError ($iext)) & "|" EndIf ;-------------------------------------------------------------------------------------- _WinAPI_FreeMemory($pCompressedBuffer) _WinAPI_FreeMemory($pUncompressedBuffer) ;-------------------------------------------------------------------------------------- EndFunc ;============================================================================================================================================================================================== Func _ConsoleWrite($sString) ConsoleWrite(BinaryToString(StringToBinary($sString,4),1)) EndFunc Link to comment Share on other sites More sharing options...
j0kky Posted December 10, 2016 Share Posted December 10, 2016 (edited) Doing a quick check, I saw this: Local $bString = StringToBinary($sString,2) ;compress function $sString = BinaryToString(DllStructGetData(DllStructCreate('byte[' & $iSize & ']', $pUncompressedBuffer), 1),4) ;uncompress function You should use the same UNICODE character set. Edited December 10, 2016 by j0kky Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
Iczer Posted December 10, 2016 Author Share Posted December 10, 2016 thanks! but still - compressed string is not ASCII and original string is not equal uncompressed expandcollapse popup#include-once #include <WinAPISys.au3> #include <WinAPI.au3> #include <WinAPIDiag.au3> #include <Array.au3> ;============================================================================================================================================================================================== $sString = 'The _WinAPI_CompressBuffer() function takes as input an uncompressed buffer and produces its compressed equivalent provided that the compressed data fits within the specified destination buffer. 和一些中文詞' $sStringOrig = $sString ConsoleWrite("Original: "&$sString & @CRLF & @CRLF) String_CompressString($sString) ConsoleWrite("Compressed: "&$sString & @CRLF & @CRLF) MsgBox(64,"StringIsASCII = "&StringIsASCII($sString),$sString) String_ExtractString($sString) ConsoleWrite("Uncompressed: "&$sString & @CRLF & @CRLF) MsgBox(64,"Orig string == Uncompressed ? : "&($sStringOrig == $sString),$sStringOrig & @crlf & $sString) ;============================================================================================================================================================================================== Func String_CompressString(ByRef $sString) Local $bString = StringToBinary($sString,2) ;-------------------------------------------------------------------------------------- Local $iUncompressedSize = BinaryLen($bString) Local $pUncompressedBuffer = _WinAPI_CreateBuffer($iUncompressedSize,0,False) ;-------------------------------------------------------------------------------------- Local $iCompressedSize = $iUncompressedSize Local $pCompressedBuffer = _WinAPI_CreateBuffer($iCompressedSize,0,False) ;-------------------------------------------------------------------------------------- DllStructSetData(DllStructCreate('byte[' & $iUncompressedSize & ']', $pUncompressedBuffer), 1, $bString) ;-------------------------------------------------------------------------------------- $iSize = _WinAPI_CompressBuffer($pUncompressedBuffer, $iUncompressedSize, $pCompressedBuffer, $iCompressedSize, BitOR($COMPRESSION_FORMAT_LZNT1,$COMPRESSION_ENGINE_MAXIMUM)) If Not @error Then $sString = BinaryToString(DllStructGetData(DllStructCreate('byte[' & $iSize & ']', $pCompressedBuffer), 1)) Else $iext = @extended $sString = "@error = " & @error & " @extended = " & $iext & "|"& _WinAPI_GetLastErrorMessage() & "|" & _WinAPI_GetErrorMessage(_WinAPI_NtStatusToDosError ($iext)) & "|" EndIf ;-------------------------------------------------------------------------------------- _WinAPI_FreeMemory($pCompressedBuffer) _WinAPI_FreeMemory($pUncompressedBuffer) ;-------------------------------------------------------------------------------------- EndFunc ;============================================================================================================================================================================================== Func String_ExtractString(ByRef $sString) Local $bString = StringToBinary($sString) ;-------------------------------------------------------------------------------------- Local $iCompressedSize = BinaryLen($bString) Local $pCompressedBuffer = _WinAPI_CreateBuffer($iCompressedSize,0,False) ;-------------------------------------------------------------------------------------- Local $iUncompressedSize = $iCompressedSize * 8 Local $pUncompressedBuffer = _WinAPI_CreateBuffer($iUncompressedSize,0,False) ;-------------------------------------------------------------------------------------- DllStructSetData(DllStructCreate('byte[' & $iCompressedSize & ']', $pCompressedBuffer), 1, $bString) ;-------------------------------------------------------------------------------------- $iSize = _WinAPI_DecompressBuffer($pUncompressedBuffer, $iUncompressedSize, $pCompressedBuffer, $iCompressedSize, BitOR($COMPRESSION_FORMAT_LZNT1,$COMPRESSION_ENGINE_MAXIMUM)) If Not @error Then $sString = BinaryToString(DllStructGetData(DllStructCreate('byte[' & $iSize & ']', $pUncompressedBuffer), 1),2) $sString = _WinAPI_WideCharToMultiByte($sString,65001,True) Else $iext = @extended $sString = "@error = " & @error & " @extended = " & $iext & "|"& _WinAPI_GetLastErrorMessage() & "|" & _WinAPI_GetErrorMessage(_WinAPI_NtStatusToDosError ($iext)) & "|" EndIf ;-------------------------------------------------------------------------------------- _WinAPI_FreeMemory($pCompressedBuffer) _WinAPI_FreeMemory($pUncompressedBuffer) ;-------------------------------------------------------------------------------------- EndFunc ;============================================================================================================================================================================================== Func _ConsoleWrite($sString) ConsoleWrite(BinaryToString(StringToBinary($sString,4),1)) EndFunc Link to comment Share on other sites More sharing options...
j0kky Posted December 10, 2016 Share Posted December 10, 2016 (edited) $sString = _WinAPI_WideCharToMultiByte($sString,65001,True) Why do you add this line? Have you tried to remove it? Edited December 10, 2016 by j0kky Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
Iczer Posted December 10, 2016 Author Share Posted December 10, 2016 it's keep string size in bytes down in comparison to same string in UTF16 (on strings with mixed ascii/non-ascii letters when ascii is most of them) so now complete version: expandcollapse popup#include-once #include <WinAPISys.au3> #include <WinAPI.au3> #include <WinAPIDiag.au3> #include <Array.au3> ;============================================================================================================================================================================================== $sStringOrig = 'The _WinAPI_CompressBuffer() function takes as input an uncompressed buffer and produces its compressed equivalent provided that the compressed data fits within the specified destination buffer. 和一些中文詞' $sStringOrig = $sStringOrig&$sStringOrig&$sStringOrig&$sStringOrig&$sStringOrig&$sStringOrig&$sStringOrig&$sStringOrig&$sStringOrig&$sStringOrig&$sStringOrig&$sStringOrig $sString = $sStringOrig ConsoleWrite("Original: "&StringLen($sStringOrig)&"|"&$sString & @CRLF & @CRLF) String_CompressString($sString) ConsoleWrite("Compressed: "&StringLen($sString)&"|"&$sString & @CRLF & @CRLF) MsgBox(64,"StringIsASCII = "&StringIsASCII($sString),$sString) String_ExtractString($sString) ConsoleWrite("Uncompressed: "&$sString & @CRLF & @CRLF) MsgBox(64,"Orig string == Uncompressed ? : "&($sStringOrig == $sString),$sStringOrig & @crlf & $sString) ;============================================================================================================================================================================================== Func String_CompressString(ByRef $sString) $sString = _WinAPI_WideCharToMultiByte($sString,65001,True) Local $bString = StringToBinary($sString,4) ;-------------------------------------------------------------------------------------- Local $iUncompressedSize = BinaryLen($bString) Local $pUncompressedBuffer = _WinAPI_CreateBuffer($iUncompressedSize,0,False) ;-------------------------------------------------------------------------------------- Local $iCompressedSize = $iUncompressedSize Local $pCompressedBuffer = _WinAPI_CreateBuffer($iCompressedSize,0,False) ;-------------------------------------------------------------------------------------- DllStructSetData(DllStructCreate('byte[' & $iUncompressedSize & ']', $pUncompressedBuffer), 1, $bString) ;-------------------------------------------------------------------------------------- $iSize = _WinAPI_CompressBuffer($pUncompressedBuffer, $iUncompressedSize, $pCompressedBuffer, $iCompressedSize, BitOR($COMPRESSION_FORMAT_LZNT1,$COMPRESSION_ENGINE_MAXIMUM)) If Not @error Then $sString = String(DllStructGetData(DllStructCreate('byte[' & $iSize & ']', $pCompressedBuffer), 1)) Else $iext = @extended $sString = "@error = " & @error & " @extended = " & $iext & "|"& _WinAPI_GetLastErrorMessage() & "|" & _WinAPI_GetErrorMessage(_WinAPI_NtStatusToDosError ($iext)) & "|" EndIf ;-------------------------------------------------------------------------------------- _WinAPI_FreeMemory($pCompressedBuffer) _WinAPI_FreeMemory($pUncompressedBuffer) ;-------------------------------------------------------------------------------------- EndFunc ;============================================================================================================================================================================================== Func String_ExtractString(ByRef $sString) Local $bString = Binary($sString) ;-------------------------------------------------------------------------------------- Local $iCompressedSize = BinaryLen($bString) Local $pCompressedBuffer = _WinAPI_CreateBuffer($iCompressedSize,0,False) ;-------------------------------------------------------------------------------------- Local $iUncompressedSize = $iCompressedSize * 8 Local $pUncompressedBuffer = _WinAPI_CreateBuffer($iUncompressedSize,0,False) ;-------------------------------------------------------------------------------------- DllStructSetData(DllStructCreate('byte[' & $iCompressedSize & ']', $pCompressedBuffer), 1, $bString) ;-------------------------------------------------------------------------------------- $iSize = _WinAPI_DecompressBuffer($pUncompressedBuffer, $iUncompressedSize, $pCompressedBuffer, $iCompressedSize, BitOR($COMPRESSION_FORMAT_LZNT1,$COMPRESSION_ENGINE_MAXIMUM)) If Not @error Then $sString = BinaryToString(DllStructGetData(DllStructCreate('byte[' & $iSize & ']', $pUncompressedBuffer), 1),4) $sString = _WinAPI_MultiByteToWideChar($sString,65001,0,True) Else $iext = @extended $sString = "@error = " & @error & " @extended = " & $iext & "|"& _WinAPI_GetLastErrorMessage() & "|" & _WinAPI_GetErrorMessage(_WinAPI_NtStatusToDosError ($iext)) & "|" EndIf ;-------------------------------------------------------------------------------------- _WinAPI_FreeMemory($pCompressedBuffer) _WinAPI_FreeMemory($pUncompressedBuffer) ;-------------------------------------------------------------------------------------- EndFunc ;============================================================================================================================================================================================== Func _ConsoleWrite($sString) ConsoleWrite(BinaryToString(StringToBinary($sString,4),1)) EndFunc Link to comment Share on other sites More sharing options...
j0kky Posted December 11, 2016 Share Posted December 11, 2016 Does it work? Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
joiner Posted December 26, 2016 Share Posted December 26, 2016 BitOR($COMPRESSION_FORMAT_XPRESS_HUFF, $COMPRESSION_ENGINE_MAXIMUM);@error - 10, @extended - C00000E8 why is it problem? Hammerfist 1 Link to comment Share on other sites More sharing options...
Trong Posted April 2, 2017 Share Posted April 2, 2017 How to fix if data is ASCII or short string ? Error: The data area passed to a system call is too small. Regards, Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now