Jump to content

Trong
 Share

Recommended Posts

In my code optimization work, I only work with these 2 functions so I share it:

; # Dao Van Trong - TRONG.LIVE # =======================================================
; Title .........: Resource UDF for RCDATA
;
;    _RCDATA_GetResAsBytes($sResNameOrID, $iResLang = 0)
;    _RCDATA_GetResAsString($sResNameOrID, $iResLang = 0)
;
; ================================================================

#Region - # FUNCTION #
Func _RCDATA_GetResAsBytes($sResNameOrID, $iResLang = 0)
    Local $hRes, $hInstance = __WinAPI_GetModuleHandle(Null)
    If ($iResLang <> 0) Then
        $hRes = __WinAPI_FindResourceEx($hInstance, 10, $sResNameOrID, $iResLang) ;$RT_RCDATA = 10
    Else
        $hRes = __WinAPI_FindResource($hInstance, 10, $sResNameOrID)
    EndIf
    If @error Or Not $hRes Then Return SetError(1, 0, 0)
    Local $dSize = __WinAPI_SizeOfResource($hInstance, $hRes)
    If @error Or Not $dSize Then Return SetError(2, 0, 0)
    Local $hLoad = __WinAPI_LoadResource($hInstance, $hRes)
    If @error Or Not $hLoad Then Return SetError(3, 0, 0)
    Local $pData = __WinAPI_LockResource($hLoad)
    If @error Or Not $pData Then Return SetError(4, 0, 0)
    Local $tBuffer = DllStructCreate("byte[" & $dSize & "]")
    __WinAPI_MoveMemory(DllStructGetPtr($tBuffer), $pData, $dSize)
    Return DllStructGetData($tBuffer, 1)
EndFunc   ;==>_RCDATA_GetResAsBytes

Func _RCDATA_GetResAsString($sResNameOrID, $iResLang = 0)
    Local $pResource = _RCDATA_GetResAsBytes($sResNameOrID, $iResLang)
    If @error Then Return SetError(1, 0, 0)
    Local $iError = @error, $iLength = @extended
    Local Enum $BINARYTOSTRING_NONE, $BINARYTOSTRING_ANSI, $BINARYTOSTRING_UTF16LE, $BINARYTOSTRING_UTF16BE, $BINARYTOSTRING_UTF8
    Local $iStart = $BINARYTOSTRING_NONE, $iUTFEncoding = $BINARYTOSTRING_ANSI
    Local Const $sUTF8 = '0xEFBBBF', $sUTF16BE = '0xFEFF', $sUTF16LE = '0xFFFE', $sUTF32BE = '0x0000FEFF', $sUTF32LE = '0xFFFE0000'
    Local $iUTF8 = BinaryLen($sUTF8), $iUTF16BE = BinaryLen($sUTF16BE), $iUTF16LE = BinaryLen($sUTF16LE), $iUTF32BE = BinaryLen($sUTF32BE), $iUTF32LE = BinaryLen($sUTF32LE)
    Select
        Case BinaryMid($pResource, 1, $iUTF32BE) = $sUTF32BE
            $iStart = $iUTF32BE
            $iUTFEncoding = $BINARYTOSTRING_ANSI
        Case BinaryMid($pResource, 1, $iUTF32LE) = $sUTF32LE
            $iStart = $iUTF32LE
            $iUTFEncoding = $BINARYTOSTRING_ANSI
        Case BinaryMid($pResource, 1, $iUTF16BE) = $sUTF16BE
            $iStart = $iUTF16BE
            $iUTFEncoding = $BINARYTOSTRING_UTF16BE
        Case BinaryMid($pResource, 1, $iUTF16LE) = $sUTF16LE
            $iStart = $iUTF16LE
            $iUTFEncoding = $BINARYTOSTRING_UTF16LE
        Case BinaryMid($pResource, 1, $iUTF8) = $sUTF8
            $iStart = $iUTF8
            $iUTFEncoding = $BINARYTOSTRING_UTF8
    EndSelect
    $iStart += 1
    $iLength = $iLength + 1 - $iStart
    Local $sString = BinaryToString(BinaryMid($pResource, $iStart), $iUTFEncoding)
    $pResource = 0
    Return SetError($iError, $iLength, $sString)
EndFunc   ;==>_RCDATA_GetResAsString
#EndRegion - # FUNCTION #
#Region ; #INTERNAL_USE_ONLY# # Dao Van Trong - TRONG.LIVE
;#include <WinAPIRes.au3>
;#include <WinAPIInternals.au3>
Func __WinAPI_GetModuleHandle($sModuleName)
    If $sModuleName = "" Then $sModuleName = Null
    Local $aCall = DllCall("kernel32.dll", "handle", "GetModuleHandleW", "wstr", $sModuleName)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_GetModuleHandle
Func __WinAPI_IsBadReadPtr($pAddress, $iLength)
    Local $aCall = DllCall('kernel32.dll', 'bool', 'IsBadReadPtr', 'struct*', $pAddress, 'uint_ptr', $iLength)
    If @error Then Return SetError(@error, @extended, False)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_IsBadReadPtr
Func __WinAPI_IsBadWritePtr($pAddress, $iLength)
    Local $aCall = DllCall('kernel32.dll', 'bool', 'IsBadWritePtr', 'struct*', $pAddress, 'uint_ptr', $iLength)
    If @error Then Return SetError(@error, @extended, False)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_IsBadWritePtr
Func __WinAPI_MoveMemory($pDestination, $pSource, $iLength)
    If __WinAPI_IsBadReadPtr($pSource, $iLength) Then Return SetError(10, @extended, 0)
    If __WinAPI_IsBadWritePtr($pDestination, $iLength) Then Return SetError(11, @extended, 0)
    DllCall('ntdll.dll', 'none', 'RtlMoveMemory', 'struct*', $pDestination, 'struct*', $pSource, 'ulong_ptr', $iLength)
    If @error Then Return SetError(@error, @extended, 0)
    Return 1
EndFunc   ;==>__WinAPI_MoveMemory
Func __WinAPI_FindResource($hInstance, $sType, $sName)
    Local $sTypeOfType = 'int', $sTypeOfName = 'int'
    If IsString($sType) Then
        $sTypeOfType = 'wstr'
    EndIf
    If IsString($sName) Then
        $sTypeOfName = 'wstr'
    EndIf
    Local $aCall = DllCall('kernel32.dll', 'handle', 'FindResourceW', 'handle', $hInstance, $sTypeOfName, $sName, $sTypeOfType, $sType)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_FindResource
Func __WinAPI_FindResourceEx($hInstance, $sType, $sName, $iLanguage)
    Local $sTypeOfType = 'int', $sTypeOfName = 'int'
    If IsString($sType) Then
        $sTypeOfType = 'wstr'
    EndIf
    If IsString($sName) Then
        $sTypeOfName = 'wstr'
    EndIf
    Local $aCall = DllCall('kernel32.dll', 'handle', 'FindResourceExW', 'handle', $hInstance, $sTypeOfType, $sType, $sTypeOfName, $sName, 'ushort', $iLanguage)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_FindResourceEx
Func __WinAPI_LoadResource($hInstance, $hResource)
    Local $aCall = DllCall('kernel32.dll', 'handle', 'LoadResource', 'handle', $hInstance, 'handle', $hResource)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_LoadResource
Func __WinAPI_LockResource($hData)
    Local $aCall = DllCall('kernel32.dll', 'ptr', 'LockResource', 'handle', $hData)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_LockResource
Func __WinAPI_SizeOfResource($hInstance, $hResource)
    Local $aCall = DllCall('kernel32.dll', 'dword', 'SizeofResource', 'handle', $hInstance, 'handle', $hResource)
    If @error Or Not $aCall[0] Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_SizeOfResource
#EndRegion ; #INTERNAL_USE_ONLY# # Dao Van Trong - TRONG.LIVE
;# Dao Van Trong - TRONG.LIVE

 

Regards,
 

Link to comment
Share on other sites

I use it for source code protection.
By saving the data to Rescource RCDATA and encrypting and packaging the exe file.
Decompiled Autoit source code doesn't seem too difficult, but Unpack technique is not everyone can do!

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Res_File_Add=FileNameInDisk.ext, RT_RCDATA, FileNameInRes, 0
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

If Not @Compiled Then Exit MsgBox(64, "", "You need Compile this script !")
Global $FileContent = _RCDATA_GetResAsString('FileNameInRes')
ConsoleWrite("Get Resource AsString  E: " & @error & @CRLF)
ConsoleWrite("Resource Type: " & VarGetType($FileContent) & @CRLF)
ConsoleWrite("-Data (" & StringLen($FileContent) & "): " & $FileContent & @CRLF)
Sleep(1000)
_WriteToFile(@ScriptDir & "\" & "_FileNameInDisk_1.ext", $FileContent, 16)

Global $FileContent = _RCDATA_GetResAsBytes('FileNameInRes')
ConsoleWrite("Get Resource AsBytes E: " & @error & @CRLF)
ConsoleWrite("Resource Type: " & VarGetType($FileContent) & @CRLF)
ConsoleWrite("-Data (" & StringLen($FileContent) & "): " & $FileContent & @CRLF)
Sleep(1000)


_WriteToFile(@ScriptDir & "\" & "_FileNameInDisk_2.ext", $FileContent, 16)
Sleep(5000)

Func _WriteToFile($pFile, $sData = '', $Mode = 16)
    FileDelete($pFile)
    ConsoleWrite("+File: " & $pFile & @CRLF)
    ;ConsoleWrite("-Data: " & $sData & @CRLF)
    ConsoleWrite("-Data len: " & StringLen($sData) & @CRLF)
    Local $hOpen = FileOpen($pFile, 2 + 8 + $Mode)
    ConsoleWrite("-FileOpen " & $pFile & " E: " & @error & @CRLF)
    Local $FileWrite = FileWrite($hOpen, $sData)
    ConsoleWrite("-FileWrite (" & $FileWrite & ") " & $pFile & " E: " & @error & @CRLF)
    FileClose($hOpen)
    ConsoleWrite("-FileClose " & $pFile & " E: " & @error & @CRLF)
    ConsoleWrite("-FileExists: " & FileExists($pFile) & @CRLF)
    Sleep(500)
EndFunc   ;==>_WriteToFile

; # Dao Van Trong - TRONG.LIVE # =======================================================
; Title .........: Resource UDF for RCDATA
;
;    _RCDATA_GetResAsBytes($sResNameOrID, $iResLang = 0)
;    _RCDATA_GetResAsString($sResNameOrID, $iResLang = 0)
;
; ================================================================

#Region - # FUNCTION #
Func _RCDATA_GetResAsBytes($sResNameOrID, $iResLang = 0)
    Local $hRes, $hInstance = __WinAPI_GetModuleHandle(Null)
    If ($iResLang <> 0) Then
        $hRes = __WinAPI_FindResourceEx($hInstance, 10, $sResNameOrID, $iResLang) ;$RT_RCDATA = 10
    Else
        $hRes = __WinAPI_FindResource($hInstance, 10, $sResNameOrID)
    EndIf
    If @error Or Not $hRes Then Return SetError(1, 0, 0)
    Local $dSize = __WinAPI_SizeOfResource($hInstance, $hRes)
    If @error Or Not $dSize Then Return SetError(2, 0, 0)
    Local $hLoad = __WinAPI_LoadResource($hInstance, $hRes)
    If @error Or Not $hLoad Then Return SetError(3, 0, 0)
    Local $pData = __WinAPI_LockResource($hLoad)
    If @error Or Not $pData Then Return SetError(4, 0, 0)
    Local $tBuffer = DllStructCreate("byte[" & $dSize & "]")
    __WinAPI_MoveMemory(DllStructGetPtr($tBuffer), $pData, $dSize)
    Return DllStructGetData($tBuffer, 1)
EndFunc   ;==>_RCDATA_GetResAsBytes

Func _RCDATA_GetResAsString($sResNameOrID, $iResLang = 0)
    Local $pResource = _RCDATA_GetResAsBytes($sResNameOrID, $iResLang)
    If @error Then Return SetError(1, 0, 0)
    Local $iError = @error, $iLength = @extended
    Local Enum $BINARYTOSTRING_NONE, $BINARYTOSTRING_ANSI, $BINARYTOSTRING_UTF16LE, $BINARYTOSTRING_UTF16BE, $BINARYTOSTRING_UTF8
    Local $iStart = $BINARYTOSTRING_NONE, $iUTFEncoding = $BINARYTOSTRING_ANSI
    Local Const $sUTF8 = '0xEFBBBF', $sUTF16BE = '0xFEFF', $sUTF16LE = '0xFFFE', $sUTF32BE = '0x0000FEFF', $sUTF32LE = '0xFFFE0000'
    Local $iUTF8 = BinaryLen($sUTF8), $iUTF16BE = BinaryLen($sUTF16BE), $iUTF16LE = BinaryLen($sUTF16LE), $iUTF32BE = BinaryLen($sUTF32BE), $iUTF32LE = BinaryLen($sUTF32LE)
    Select
        Case BinaryMid($pResource, 1, $iUTF32BE) = $sUTF32BE
            $iStart = $iUTF32BE
            $iUTFEncoding = $BINARYTOSTRING_ANSI
        Case BinaryMid($pResource, 1, $iUTF32LE) = $sUTF32LE
            $iStart = $iUTF32LE
            $iUTFEncoding = $BINARYTOSTRING_ANSI
        Case BinaryMid($pResource, 1, $iUTF16BE) = $sUTF16BE
            $iStart = $iUTF16BE
            $iUTFEncoding = $BINARYTOSTRING_UTF16BE
        Case BinaryMid($pResource, 1, $iUTF16LE) = $sUTF16LE
            $iStart = $iUTF16LE
            $iUTFEncoding = $BINARYTOSTRING_UTF16LE
        Case BinaryMid($pResource, 1, $iUTF8) = $sUTF8
            $iStart = $iUTF8
            $iUTFEncoding = $BINARYTOSTRING_UTF8
    EndSelect
    $iStart += 1
    $iLength = $iLength + 1 - $iStart
    Local $sString = BinaryToString(BinaryMid($pResource, $iStart), $iUTFEncoding)
    $pResource = 0
    Return SetError($iError, $iLength, $sString)
EndFunc   ;==>_RCDATA_GetResAsString
#EndRegion - # FUNCTION #
#Region ; #INTERNAL_USE_ONLY# # Dao Van Trong - TRONG.LIVE
;#include <WinAPIRes.au3>
;#include <WinAPIInternals.au3>
Func __WinAPI_GetModuleHandle($sModuleName)
    If $sModuleName = "" Then $sModuleName = Null
    Local $aCall = DllCall("kernel32.dll", "handle", "GetModuleHandleW", "wstr", $sModuleName)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_GetModuleHandle
Func __WinAPI_IsBadReadPtr($pAddress, $iLength)
    Local $aCall = DllCall('kernel32.dll', 'bool', 'IsBadReadPtr', 'struct*', $pAddress, 'uint_ptr', $iLength)
    If @error Then Return SetError(@error, @extended, False)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_IsBadReadPtr
Func __WinAPI_IsBadWritePtr($pAddress, $iLength)
    Local $aCall = DllCall('kernel32.dll', 'bool', 'IsBadWritePtr', 'struct*', $pAddress, 'uint_ptr', $iLength)
    If @error Then Return SetError(@error, @extended, False)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_IsBadWritePtr
Func __WinAPI_MoveMemory($pDestination, $pSource, $iLength)
    If __WinAPI_IsBadReadPtr($pSource, $iLength) Then Return SetError(10, @extended, 0)
    If __WinAPI_IsBadWritePtr($pDestination, $iLength) Then Return SetError(11, @extended, 0)
    DllCall('ntdll.dll', 'none', 'RtlMoveMemory', 'struct*', $pDestination, 'struct*', $pSource, 'ulong_ptr', $iLength)
    If @error Then Return SetError(@error, @extended, 0)
    Return 1
EndFunc   ;==>__WinAPI_MoveMemory
Func __WinAPI_FindResource($hInstance, $sType, $sName)
    Local $sTypeOfType = 'int', $sTypeOfName = 'int'
    If IsString($sType) Then
        $sTypeOfType = 'wstr'
    EndIf
    If IsString($sName) Then
        $sTypeOfName = 'wstr'
    EndIf
    Local $aCall = DllCall('kernel32.dll', 'handle', 'FindResourceW', 'handle', $hInstance, $sTypeOfName, $sName, $sTypeOfType, $sType)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_FindResource
Func __WinAPI_FindResourceEx($hInstance, $sType, $sName, $iLanguage)
    Local $sTypeOfType = 'int', $sTypeOfName = 'int'
    If IsString($sType) Then
        $sTypeOfType = 'wstr'
    EndIf
    If IsString($sName) Then
        $sTypeOfName = 'wstr'
    EndIf
    Local $aCall = DllCall('kernel32.dll', 'handle', 'FindResourceExW', 'handle', $hInstance, $sTypeOfType, $sType, $sTypeOfName, $sName, 'ushort', $iLanguage)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_FindResourceEx
Func __WinAPI_LoadResource($hInstance, $hResource)
    Local $aCall = DllCall('kernel32.dll', 'handle', 'LoadResource', 'handle', $hInstance, 'handle', $hResource)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_LoadResource
Func __WinAPI_LockResource($hData)
    Local $aCall = DllCall('kernel32.dll', 'ptr', 'LockResource', 'handle', $hData)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_LockResource
Func __WinAPI_SizeOfResource($hInstance, $hResource)
    Local $aCall = DllCall('kernel32.dll', 'dword', 'SizeofResource', 'handle', $hInstance, 'handle', $hResource)
    If @error Or Not $aCall[0] Then Return SetError(@error, @extended, 0)
    Return $aCall[0]
EndFunc   ;==>__WinAPI_SizeOfResource
#EndRegion ; #INTERNAL_USE_ONLY# # Dao Van Trong - TRONG.LIVE
;# Dao Van Trong - TRONG.LIVE

 

Regards,
 

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...