Jump to content

wcslen alternative for Binary string length from pointer


jugador
 Share

Recommended Posts

That's impossible to determine: binary data may contain NULL byte at any place, so contrary to strings, there is no stop sign. Only the allocator of the binary data knows how large it is. And you never know if the pointer is pointing to the base or in-between the data.

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

To retrieve Binary Data (VT_ARRAY + VT_UI1 = 8029)
you need @LarsJ   Variants and Safearrays UDF

; #FUNCTION# =============================================================================
; Name...........: __SafeArrayBinary_toByte
; ========================================================================================
Func __SafeArrayBinary_toByte( $pSafeArray, ByRef $v_GetData)
    If Not IsPtr( $pSafeArray ) Then Return SetError(1,0,0)
    If Not SafeArrayGetDim( $pSafeArray ) = 1 Then Return SetError(2,0,0)
    Local $Total_Byte, $pSafeArrayData
    SafeArrayGetUBound( $pSafeArray, 1, $Total_Byte )
    SafeArrayAccessData( $pSafeArray, $pSafeArrayData )
    $v_GetData = DllStructGetData( DllStructCreate( "Byte[" & $Total_Byte + 1 &"]", $pSafeArrayData), 1 )
    SafeArrayUnaccessData( $pSafeArray )
EndFunc

 

Edited by jugador
Link to comment
Share on other sites

Yes, in a very specific case where the binary has been created the way you need it to access it. But of course this won't work in the general case.

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

21 hours ago, jchd said:

Only the allocator of the binary data knows how large it is.

 

On 9/28/2023 at 8:59 AM, jugador said:

Any alternative of wcslen to get Binary data length from pointer.

Yes, use the appropriate string managemennt system. The pointer points to a memoryblock where the first 4 bytes (UINT/DWORD) contains the string length and the rest of the memory contains the string/data. Then the data format is irrelevant. 

Link to comment
Share on other sites

@AndyG  thanks for the hint.
But still it will be easy for me if you show how to get the Binary String length from $pVariant.

__ExampleA()
Func __ExampleA()
    Local $BinaryString = StringToBinary("Hello @World")
    Local $Bin_Len = BinaryLen( $BinaryString )
    ConsoleWrite( 'BinaryLen: ' & $Bin_Len & @TAB & 'BinaryString: ' & $BinaryString & @CRLF)

    Local $tSTRUCT1 = DllStructCreate('Byte[' & $Bin_Len & ']')
    DllStructSetData($tSTRUCT1, 1, $BinaryString)
    ConsoleWrite('[Data]: ' & DllStructGetData($tSTRUCT1, 1) & @CRLF)

    Local $pVariant = DllStructGetPtr($tSTRUCT1)
    ConsoleWrite('[$pVariant]: ' & $pVariant & @CRLF)
EndFunc

 

Edited by jugador
Link to comment
Share on other sites

__ExampleA()
Func __ExampleA()
    Local $BinaryString = StringToBinary("Hello @World")
    Local $Bin_Len = BinaryLen($BinaryString)
    ConsoleWrite('BinaryLen: ' & $Bin_Len & @TAB & 'BinaryString: ' & $BinaryString & @CRLF)

    Local $tSTRUCT1 = DllStructCreate('uint; Byte[' & $Bin_Len & ']')

    DllStructSetData($tSTRUCT1, 1, $Bin_Len, 1)

    For $i = 1 To $Bin_Len                                                      ;you have to write bytes...because it is not a string! or you can make a memcpy
        DllStructSetData($tSTRUCT1, 2, Dec(StringMid($BinaryString, $i * 2 + 1, 2)), $i)
;~         ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : stringmid($BinaryString,$i*2+1,2) = ' & StringMid($BinaryString, $i * 2 + 1, 2) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    Next

    ConsoleWrite('[Data uint   ]: ' & DllStructGetData($tSTRUCT1, 1) & @CRLF)
    ConsoleWrite('[Data String ]: ' & DllStructGetData($tSTRUCT1, 2) & @CRLF)

    Local $pVariant = DllStructGetPtr($tSTRUCT1)
    ConsoleWrite('[$pVariant   ]: ' & $pVariant & @CRLF & @CRLF & @CRLF)

    ;end of writing data into memory



    ;start reading data from memory with known pointer

    $struct_stringlen = DllStructCreate("uint", $pVariant)                      ;get first 4 bytes = uint =stringlen
    $stringlen = DllStructGetData($struct_stringlen, 1)

    ;lets get the binarystring
    $struct_binarystring = DllStructCreate("byte[" & $stringlen & "]", $pVariant + 4) ;place the struct at the pointer +4 because uint is 4 bytes
    $binary = DllStructGetData($struct_binarystring, 1)
    $string = BinaryToString($binary)

    ConsoleWrite(" Stringlen = " & $stringlen  & @CRLF & " Binary    = " & $binary & @CRLF & " String    = " & $string & @crlf & @crlf)

EndFunc                                                                         ;==>__ExampleA

If you have an ANSI/ASCII String, it is much easier because the transformation to binary data is omitted. You can write the (ANSI/ASCII) string directly into the memory. This works with char and wchar (16 bit unicode).

__ExampleB()
Func __ExampleB()
    Local $String = "Hello @World"
    Local $Len = StringLen($String)
    ConsoleWrite('StringLen: ' & $Len & @TAB & 'String: ' & $String & @CRLF)

    Local $tSTRUCT1 = DllStructCreate('uint; char[' & $Len & ']')         ;or wchar if unicode!!!

    DllStructSetData($tSTRUCT1, 1, $Len)
    DllStructSetData($tSTRUCT1, 2, $String)

    ConsoleWrite('[Data uint   ]: ' & DllStructGetData($tSTRUCT1, 1) & @CRLF)
    ConsoleWrite('[Data String ]: ' & DllStructGetData($tSTRUCT1, 2) & @CRLF)

    Local $pVariant = DllStructGetPtr($tSTRUCT1)
    ConsoleWrite('[$pVariant   ]: ' & $pVariant & @CRLF & @CRLF & @CRLF)

    ;end of writing data into memory



    ;start reading data from memory with known pointer

    $struct_stringlen = DllStructCreate("uint", $pVariant)                ;get first 4 bytes = uint =stringlen
    $stringlen = DllStructGetData($struct_stringlen, 1)

    ;lets get the string  char or wchar if unicode
    $struct_string = DllStructCreate("char[" & $stringlen & "]", $pVariant + 4) ;place the struct at the pointer +4 because uint is 4 bytes
    $String = DllStructGetData($struct_string, 1)


    ConsoleWrite(" Stringlen = " & $stringlen & @CRLF & " String    = " & $String & @CRLF & @CRLF)

EndFunc                                                                   ;==>__ExampleB

 

Edited by AndyG
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...