Jump to content



Photo

Inline binary files


  • Please log in to reply
6 replies to this topic

#1 willichan

willichan

    Old Geek

  • Active Members
  • PipPipPipPipPipPip
  • 387 posts

Posted 11 October 2010 - 09:02 PM

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
Plain Text         
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






#2 wakillon

wakillon

    Tiny Tools Coder

  • Active Members
  • PipPipPipPipPipPip
  • 2,477 posts

Posted 12 October 2010 - 01:34 PM

It works well Posted Image
but why use a random file name, and don't put an autostart to the new script for get the file ?

Edited by wakillon, 12 October 2010 - 01:36 PM.

  AutoIt Version : 3.3.8.1/3.3.9.4 SciTE 3.3.0 Language:040C OS:WIN_7/ CPU:X64 OS:X64 

  Last updated Scripts and executables with full embedded files are available on : GoogleCode 


#3 willichan

willichan

    Old Geek

  • Active Members
  • PipPipPipPipPipPip
  • 387 posts

Posted 12 October 2010 - 02:52 PM

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.

#4 Manadar

Manadar

    Taking a REST.

  • MVPs
  • 10,714 posts

Posted 12 October 2010 - 04:16 PM

I know which anti virus you mean. It's eSafe/Aladdin with one of their many variants which all use the same definition library.

#5 AdmiralAlkex

AdmiralAlkex

    I'm on a boat

  • MVPs
  • 4,490 posts

Posted 12 October 2010 - 06:00 PM

The script works well and I can see myself using it (actually I just did). 4 stars from me! ;)

#6 willichan

willichan

    Old Geek

  • Active Members
  • PipPipPipPipPipPip
  • 387 posts

Posted 13 October 2010 - 08:30 PM

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

#7 yahaosoft

yahaosoft

    Seeker

  • Active Members
  • 18 posts

Posted 15 November 2012 - 10:07 PM

nice works! :guitar:
thanks for sharing it.
five start voted.:)
Thanksgiving...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users