Jump to content

Inline binary files


willichan
 Share

Recommended Posts

This is a script I banged out for a project where, for some reason, my client's anti-virus program detected any compiled script that used FileInstall() as infected. (They were using some program from China I was not familiar with, nor could I read the messages.) It creates an include file patterned somewhat after the SQLite.dll.au3 that is included with AutoIt. This makes the resulting EXE file a little larger than FileInstall() does (even after /striponly), but it solved my particular problem.

It is intended to be compiled, and then drag/drop the binary file onto it to do the conversion.

If the dropped file is a DLL file, it will add the DllOpen() and DllClose() commands to the startup and shutdown functions.

InlineMe.au3

Const $blocksize = 512
Dim $fromfile, $fromname, $curpos, $tag, $tofile, $toname, $ext, $name
If $CmdLine[0] = 0 Then
    Exit
Else
    $fromname = $CmdLine[1]
EndIf
If Not FileExists($fromname) Then Exit
$toname = $fromname & ".au3"
$fromfile = StringToBinary(BinaryToString(FileRead($fromname)))
$curpos = 0

$tofile = FileOpen($toname, 2)
If $tofile = -1 Then Exit
If StringInStr($fromname, ".") Then
    $ext = StringUpper(StringRight($fromname, StringLen($fromname) - StringInStr($fromname, ".", 0, -1)))
Else
    $ext = ""
EndIf
$name = StringRight($fromname, StringLen($fromname) - StringInStr($fromname, "\", 0, -1))
FileWriteLine($tofile, '#include-once')
FileWriteLine($tofile, '#include <file.au3>')
FileWriteLine($tofile, '')
FileWriteLine($tofile, 'Func _' & CleanName($name) & '_Startup()')
FileWriteLine($tofile, '    Local $Inline_Filename = _TempFile(@TempDir, "~", ".' & $ext & '")')
FileWriteLine($tofile, '    Local $InlineOutFile = FileOpen($Inline_Filename, 2)')
FileWriteLine($tofile, '    If $InlineOutFile = -1 Then Return SetError(1, 0, "")')
FileWriteLine($tofile, '')
FileWriteLine($tofile, '    FileWrite($InlineOutFile, _' & CleanName($name) & '_Inline())')
FileWriteLine($tofile, '    FileClose($InlineOutFile)')
If $ext = "DLL" Then
    FileWriteLine($tofile, '    If DllOpen($Inline_Filename) = -1 Then')
    FileWriteLine($tofile, '        Return SetError(1, 0, "")')
    FileWriteLine($tofile, '    Else')
    FileWriteLine($tofile, '        Return $Inline_Filename')
    FileWriteLine($tofile, '    EndIf')
Else
    FileWriteLine($tofile, '    Return $Inline_Filename')
EndIf
FileWriteLine($tofile, 'EndFunc   ;==>_' & CleanName($name) & '_Startup')
FileWriteLine($tofile, '')
FileWriteLine($tofile, 'Func _' & CleanName($name) & '_Shutdown($Inline_Filename)')
If $ext = "DLL" Then
    FileWriteLine($tofile, '    DllClose($Inline_Filename)')
EndIf
FileWriteLine($tofile, '    FileDelete($Inline_Filename)')
FileWriteLine($tofile, 'EndFunc   ;==>_' & CleanName($name) & '_Shutdown')
FileWriteLine($tofile, '')
FileWriteLine($tofile, 'Func _' & CleanName($name) & '_Inline()')
FileWriteLine($tofile, '    Local $sData')
FileWriteLine($tofile, "    #region    ;" & $name)
While $curpos < StringLen($fromfile)
    If $curpos = 0 Then
        $curpos = 1
        $tag = '    $sData  = "'
    Else
        $tag = '    $sData &= "'
    EndIf
    FileWriteLine($tofile, $tag & StringMid($fromfile, $curpos, $blocksize) & '"')
    $curpos += $blocksize
WEnd
FileWriteLine($tofile, "    #endregion ;" & StringRight($fromname, StringLen($fromname) - StringInStr($fromname, "\", 0, -1)))
FileWriteLine($tofile, '    Return Binary($sData)')
FileWriteLine($tofile, 'EndFunc   ;==>_' & CleanName($name) & '_Inline')
FileClose($tofile)

Func CleanName($name)
    $name = StringReplace($name, ".", "")
    $name = StringReplace($name, " ", "")
    $name = StringReplace($name, "[", "")
    $name = StringReplace($name, "]", "")
    $name = StringReplace($name, "(", "")
    $name = StringReplace($name, ")", "")
    $name = StringReplace($name, "{", "")
    $name = StringReplace($name, "}", "")
    Return $name
EndFunc   ;==>CleanName
Link to comment
Share on other sites

but why use a random file name

I generally try to make my includes as general use as possible. Creating the file as a temp file allows for it to be used where you may not intend for the file to be permanently installed on the system the script runs on. Since I don't want to stomp on temp files being used by another script/app, I use the _TempFile() function to ensure a unique name. Since the filename is made available to you, you can move/copy/rename as you need to, if you do want it kept.

and don't put an autostart to the new script for get the file ?

Since I don't know what the file is, how it will be used, or even if it is executable; it does not make sense to autostart it here. It would be up to the script author to determine when and how the file is used.

Link to comment
Share on other sites

@Manadar

You are probably right. I can make my way around Japanese screens, but Chinese was a bit beyond me, so I had no idea what it was.

@AdmiralAlkex

Thank you. I appreciate it.

Link to comment
Share on other sites

  • 2 years later...
  • 3 years later...
On 12/10/2010 at 4:02 AM, willichan said:

This is a script I banged out for a project where, for some reason, my client's anti-virus program detected any compiled script that used FileInstall() as infected. (They were using some program from China I was not familiar with, nor could I read the messages.) It creates an include file patterned somewhat after the SQLite.dll.au3 that is included with AutoIt. This makes the resulting EXE file a little larger than FileInstall() does (even after /striponly), but it solved my particular problem.

 

It is intended to be compiled, and then drag/drop the binary file onto it to do the conversion.

If the dropped file is a DLL file, it will add the DllOpen() and DllClose() commands to the startup and shutdown functions.

 

InlineMe.au3

 

Const $blocksize = 512
Dim $fromfile, $fromname, $curpos, $tag, $tofile, $toname, $ext, $name
If $CmdLine[0] = 0 Then
    Exit
Else
    $fromname = $CmdLine[1]
EndIf
If Not FileExists($fromname) Then Exit
$toname = $fromname & ".au3"
$fromfile = StringToBinary(BinaryToString(FileRead($fromname)))
$curpos = 0

$tofile = FileOpen($toname, 2)
If $tofile = -1 Then Exit
If StringInStr($fromname, ".") Then
    $ext = StringUpper(StringRight($fromname, StringLen($fromname) - StringInStr($fromname, ".", 0, -1)))
Else
    $ext = ""
EndIf
$name = StringRight($fromname, StringLen($fromname) - StringInStr($fromname, "\", 0, -1))
FileWriteLine($tofile, '#include-once')
FileWriteLine($tofile, '#include <file.au3>')
FileWriteLine($tofile, '')
FileWriteLine($tofile, 'Func _' & CleanName($name) & '_Startup()')
FileWriteLine($tofile, '    Local $Inline_Filename = _TempFile(@TempDir, "~", ".' & $ext & '")')
FileWriteLine($tofile, '    Local $InlineOutFile = FileOpen($Inline_Filename, 2)')
FileWriteLine($tofile, '    If $InlineOutFile = -1 Then Return SetError(1, 0, "")')
FileWriteLine($tofile, '')
FileWriteLine($tofile, '    FileWrite($InlineOutFile, _' & CleanName($name) & '_Inline())')
FileWriteLine($tofile, '    FileClose($InlineOutFile)')
If $ext = "DLL" Then
    FileWriteLine($tofile, '    If DllOpen($Inline_Filename) = -1 Then')
    FileWriteLine($tofile, '        Return SetError(1, 0, "")')
    FileWriteLine($tofile, '    Else')
    FileWriteLine($tofile, '        Return $Inline_Filename')
    FileWriteLine($tofile, '    EndIf')
Else
    FileWriteLine($tofile, '    Return $Inline_Filename')
EndIf
FileWriteLine($tofile, 'EndFunc   ;==>_' & CleanName($name) & '_Startup')
FileWriteLine($tofile, '')
FileWriteLine($tofile, 'Func _' & CleanName($name) & '_Shutdown($Inline_Filename)')
If $ext = "DLL" Then
    FileWriteLine($tofile, '    DllClose($Inline_Filename)')
EndIf
FileWriteLine($tofile, '    FileDelete($Inline_Filename)')
FileWriteLine($tofile, 'EndFunc   ;==>_' & CleanName($name) & '_Shutdown')
FileWriteLine($tofile, '')
FileWriteLine($tofile, 'Func _' & CleanName($name) & '_Inline()')
FileWriteLine($tofile, '    Local $sData')
FileWriteLine($tofile, "    #region    ;" & $name)
While $curpos < StringLen($fromfile)
    If $curpos = 0 Then
        $curpos = 1
        $tag = '    $sData  = "'
    Else
        $tag = '    $sData &= "'
    EndIf
    FileWriteLine($tofile, $tag & StringMid($fromfile, $curpos, $blocksize) & '"')
    $curpos += $blocksize
WEnd
FileWriteLine($tofile, "    #endregion ;" & StringRight($fromname, StringLen($fromname) - StringInStr($fromname, "\", 0, -1)))
FileWriteLine($tofile, '    Return Binary($sData)')
FileWriteLine($tofile, 'EndFunc   ;==>_' & CleanName($name) & '_Inline')
FileClose($tofile)

Func CleanName($name)
    $name = StringReplace($name, ".", "")
    $name = StringReplace($name, " ", "")
    $name = StringReplace($name, "[", "")
    $name = StringReplace($name, "]", "")
    $name = StringReplace($name, "(", "")
    $name = StringReplace($name, ")", "")
    $name = StringReplace($name, "{", "")
    $name = StringReplace($name, "}", "")
    Return $name
EndFunc   ;==>CleanName

Dear..

My Exe File 289 kb.. Drop to inlineme

Execute file.. Display command Prompt.. File to big to fit in memory..

How to change blocksize .. ?

 

Thank you..

Link to comment
Share on other sites

On Monday, October 24, 2016 at 3:32 PM, Sudiro said:

My Exe File 289 kb.. Drop to inlineme

Execute file.. Display command Prompt.. File to big to fit in memory..

How to change blocksize .. ?

blocksize is set in the first line of the script.  It won't change much as far as the final size goes though.

Link to comment
Share on other sites

  • 3 months later...

Hi,

Great script , thanks for sharing :)

I ran the script and created the SQLite.dll.au3 file , and included it in my script

but im still getting error "SQLite3.dll Can't be Loaded!"

I will really appreciate it if you took a look at my code below

Thanks

#include <SQLite.au3>
#include <SQLite.dll.au3>


_SQLite_Startup()

If @error Then
    MsgBox($MB_SYSTEMMODAL, "SQLite Error", "SQLite3.dll Can't be Loaded!")
    Exit -1
EndIf

$DB = _SQLite_Open("C:\Temp\Test.db")
$Action = "TestAction"
$Time = @HOUR&":"&@MIN&":"&@SEC
$Date = @YEAR&"-"&@MON&"-"&@MDAY
$User = @UserName
$Computer = @ComputerName
$DC = @LogonServer
If @error Then
    MsgBox($MB_SYSTEMMODAL, "SQLite Error", "Couldnt open Database")
    Exit -1
EndIf
_SQLite_Exec($DB,"INSERT INTO QLogs (Action,Date,Time,User,Computer,DC) " & _
                              "VALUES ("& _SQLite_FastEscape($Action) & "," & _
                                          _SQLite_FastEscape($Date) & "," & _
                                          _SQLite_FastEscape($Time) & "," & _
                                          _SQLite_FastEscape($User) & "," & _
                                          _SQLite_FastEscape($Computer) & "," & _
                                          _SQLite_FastEscape($DC) & ");")

If @error Then
    MsgBox($MB_SYSTEMMODAL, "SQLite Error", "Couldnt insert!")
    Exit -1
EndIf
_SQLite_Shutdown()

 

Edited by BisherSH
Link to comment
Share on other sites

  • 1 year later...

I know this is an old post, but I wanted to give a solution to the problem for anyone that comes up on this topic.  The _Starup and _Shutdown functions created by the Inline script are not being called.  The updated script is below.  

#include <SQLite.au3>
#include "SQLite.dll.au3" ;Created by InlineMe.au3

Global $sSQLiteDLL = _SQLitedll_Startup()

_SQLite_Startup($sSQLiteDLL)

If @error Then
    MsgBox($MB_SYSTEMMODAL, "SQLite Error", "SQLite3.dll Can't be Loaded!")
    Exit -1
EndIf

Global $DB = _SQLite_Open("C:\Temp\Test.db")
Global $Action = "TestAction"
Global $Time = @HOUR&":"&@MIN&":"&@SEC
Global $Date = @YEAR&"-"&@MON&"-"&@MDAY
Global $User = @UserName
Global $Computer = @ComputerName
Global $DC = @LogonServer
If @error Then
    MsgBox($MB_SYSTEMMODAL, "SQLite Error", "Couldnt open Database")
    Exit -1
EndIf
_SQLite_Exec($DB,"INSERT INTO QLogs (Action,Date,Time,User,Computer,DC) " & _
                              "VALUES ("& _SQLite_FastEscape($Action) & "," & _
                                          _SQLite_FastEscape($Date) & "," & _
                                          _SQLite_FastEscape($Time) & "," & _
                                          _SQLite_FastEscape($User) & "," & _
                                          _SQLite_FastEscape($Computer) & "," & _
                                          _SQLite_FastEscape($DC) & ");")

If @error Then
    MsgBox($MB_SYSTEMMODAL, "SQLite Error", "Couldnt insert!")
    Exit -1
EndIf
_SQLite_Shutdown()

_SQLitedll_Shutdown($sSQLiteDLL)

 

Adam

 

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