Sign in to follow this  
Followers 0
Rux

File Bin Maker

8 posts in this topic

Hi, I made a UDF called AUBinary.au3, and with it you can make "file bins" which combine files into one, kinda like a .zip/.rar file, but easier to use with AutoIT.

HERE ARE THE FUNCTIONS

----------------------

_binary_createBin() - Starts a bin file.

_binary_addBin() - Adds a file to a bin.

_binary_saveBin() - Finalizes a bin and saves it.

_binary_extractBin() - Extracts a specific file or all files from a bin file.

_binary_getBinNames() - Retrieves how many files in a bin file, and the names of them in an array.

_binary_getBinNamesHandle() - Retrieves how many files in a bin handle, and the names of them in an array.

_binary_getBinData() - Retrieves how many files in a bin file, and the actual data inside the files in an array.

_binary_getBinDataHandle() - Retrieves how many files in a bin handle, and the actual data inside the files in an array.

_binary_lockBin() - Locks a bin file with a password with RC4 encryption.

_binary_unlockBin() - Unlocks a bin file.

_binary_binIsLocked() - Checks whether a bin file is locked or not.

Here is the UDF and all the details you need to know.

;AUTOIT Binary Packer UDF - BY: JDM STUDIOS
;------------------------------------------
;This UDF is used to create custom binary files.
;Combine and encrypt several files into one with a custom file format.
;---------------------------------------------------------------------
;
;FUNCTIONS
;---------
;CREATION FUNCTIONS
;-------------------
;_binary_createBin($iFile1, $iMODE = 0) - Create the file in memory.
;$iFile1 = This is the path to first file in the binary. (Warning: When adding files BE SURE to include the FULL file path, not just for example: "file.txt". Make it an exact path or files won't extract correctly and may delete files thanks to the overwriter. Use @ScriptDir or ".\" in the filepath to specify files.)
;$iMODE (optional) = If 0, original files will not be deleted when imported, if 1 they will be.
;RETURN: SUCCESS = The handle to the binary. FAIL = 0
;
;_binary_addBin($iHANDLE, $iFile) - Add a file to a binary.
;$iHANDLE = The handle as returned by _binary_createBin().
;$iFile = The file to add. (See the warning in _binary_createBin())
;RETURN: SUCCESS = Returns a new, updated handle, making the previous one obsolete (you can use the same variable), FAIL = 0
;
;_binary_saveBin($iHANDLE, $iPATH) - Saves a binary file.
;$iHANDLE = The handle as returned by _binary_createBin()/_binary_addBin().
;$iPATH = The file path to save the binary.
;RETURN: SUCCESS = 1, FAIL = 0
;
;_binary_extractBin($iPATH, $iEXTRACT = "", $iFBIN = 0, $iMODE = 0) - Extracts a file or all files from a bin created with this UDF.
;$iPATH = The path to the binary file.
;$iEXTRACT (optional) = The dir to extract the files to. If none specified, relative path is used.
;$iFBIN (optional) = The file to extract (type the name and file extension of the file in a string), or 0 to extract all files.
;$iMODE (optional) = If 0, original bin will not be deleted, if 1, it will be.
;RETURN: SUCCESS = The number of files extracted (returns 0 if no files extracted), FAIL = -1
;
;MANAGEMENT FUNCTIONS
;---------------------
;_binary_getBinNames($iPATH) - Returns an array that includes all the file names in a bin file.
;$iPATH = The path to the bin.
;RETURN: SUCCESS = An array containing the names. [0] is how many files that are in the bin, FAIL = -1
;
;_binary_getBinNamesHandle($iHANDLE) - Returns an array that includes all the file names in a binary handle.
;$iHANDLE = The handle as returned by a previous _binary_addBin() or _binary_createBin().
;RETURN: SUCCESS = An array containing the names. [0] is how many files that are in the bin handle, FAIL = -1
;
;_binary_getBinData($iPATH) - Returns an array that contains the file data in order.
;$iPATH = The path to the binary file.
;RETURN: SUCCESS = An array containing the data in the file order when created. [0] is how many files that are in the bin file, FAIL = -1
;
;_binary_getBinDataHandle($iHANDLE) - Returns an array that contains the file data in order from a handle.
;$iHANDLE = The handle as returned by _binary_createBin()/_binary_addBin().
;RETURN: SUCCESS = An array containing only the data from the specified handle. [0] is how many files that are in the bin handle, FAIL = -1
;
;_binary_lockBin($iPATH, $iPASSWORD) - Encrypts a bin file with a password. When locked, the only functions that will work properly are _binary_unlockBin(), and _binary_binIsLocked().
;$iPATH = The path to the bin file.
;$iPASSWORD = The password to use to lock the bin file.
;RETURN: SUCCESS = 1, FAIL = 0
;
;_binary_unlockBin($iPATH, $iPASSWORD) - Decrypts a bin file with a password.
;$iPATH = The path to the bin file.
;$iPASSWORD = The password to unlock the file with.
;RETURN: SUCCESS = 1, FAIL = 0
;
;_binary_binIsLocked($iPATH) - Determins if the bin file is locked.
;$iPATH = The path to the bin file.
;RETURN: IS LOCKED = 1, IS NOT LOCKED = 0
;-----------------------------------------------------------------------------
#include <String.au3>
#include <Crypt.au3>
#include <File.au3>

Func _binary_createBin($iFile1, $iMODE = 0)
    Local $handle[2][2]
    if $iFile1 = "" OR FileExists($iFile1) = 0 Then
        Return 0
    EndIf
    if $iMODE <> 0 AND $iMODE <> 1 Then
        Return 0
    EndIf
    $handle[0][0] = 1
    $handle[0][1] = $iMODE
    $handle[1][0] = _getFilename($iFile1)
    $handle[1][1] = FileRead($iFile1)
    if $iMODE = 1 Then
        FileDelete($iFile1)
    EndIf
    Return $handle
EndFunc

Func _binary_addBin($iHANDLE, $iFile)
    if $iFile = "" OR FileExists($iFile) = 0 OR IsArray($iHANDLE) = 0 Then
        Return 0
    EndIf
    Local $handle[Ubound($iHANDLE, 1) + 1][2]
    $handle[0][0] = $iHANDLE[0][0] + 1
    $handle[0][1] = $iHANDLE[0][1]
    For $count = 1 To Ubound($handle, 1) - 2
        $handle[$count][0] = $iHANDLE[$count][0]
        $handle[$count][1] = $iHANDLE[$count][1]
    Next
    $handle[Ubound($iHANDLE, 1)][0] = _getFilename(FileGetLongName($iFile))
    $handle[Ubound($iHANDLE, 1)][1] = FileRead($iFile)
    if $handle[0][1] = 1 Then
        FileDelete($iFile)
    EndIf
    Return $handle
EndFunc

Func _binary_saveBin($iHANDLE, $iPATH)
    Local $encrypter
    if $iPATH = "" OR FileExists($iPATH) = 1 OR IsArray($iHANDLE) = 0 Then
        Return 0
    EndIf
    
    Local $handle[Ubound($iHANDLE, 1)][2]
    $handle[0][0] = $iHANDLE[0][0]
    For $count = 1 To Ubound($handle, 1) - 1
        $handle[$count][0] = $iHANDLE[$count][0]
        $handle[$count][1] = $iHANDLE[$count][1]
    Next
    
    FileWrite(@ScriptDir &"\bin.tmp", $handle[0][0] & @CRLF)
    FileWrite(@ScriptDir &"\bin.tmp", $handle[1][0])
    FileWrite(@ScriptDir &"\bin.tmp", "|BREAK|" & $handle[1][1])
    For $count = 2 To Ubound($handle, 1) - 1
        FileWrite(@ScriptDir &"\bin.tmp", "|BREAK|"& $handle[$count][0])
        FileWrite(@ScriptDir &"\bin.tmp", "|BREAK|"& $handle[$count][1])
    Next
    
    $encrypter = _StringToHex(FileRead(@ScriptDir &"\bin.tmp"))
    FileWrite($iPATH, $encrypter)
    FileDelete(@ScriptDir &"\bin.tmp")
    Return 1
EndFunc

Func _binary_extractBin($iPATH, $iEXTRACT = "", $iFBIN = 0, $iMODE = 0)
    if $iPATH = "" OR FileExists($iPATH) = 0 Then
        Return -1
    EndIf
    if FileExists($iEXTRACT) = 0 AND $iEXTRACT <> "" Then
        Return -1
    EndIf
    if $iMODE <> 0 AND $iMODE <> 1 Then
        Return -1
    EndIf
    
    Local $iEdir, $filestract = 0
    if $iEXTRACT = "" Then
        $iEdir = @ScriptDir &"\"
    Else
        $iEdir = $iEXTRACT &"\"
    EndIf
    Local $split1
    
    FileWrite(@ScriptDir &"\bin.tmp", _HexToString(FileRead($iPATH)))
    $split1 = StringInStr(FileRead(@ScriptDir &"\bin.tmp"), @CRLF, Default, 1)
    FileWrite(@ScriptDir &"\bin2.tmp", StringTrimLeft(FileRead(@ScriptDir &"\bin.tmp"), $split1 + 1))
    $data = StringSplit(FileRead(@ScriptDir &"\bin2.tmp"), "|BREAK|", 3)
    
    if $iFBIN = "0" Then
        if FileExists($iEdir & $data[0]) = 1 Then
            FileDelete($iEdir & $data[0])
        EndIf
        FileWrite($iEdir & $data[0], $data[1])
        $filestract += 1
        if FileReadLine(@ScriptDir &"\bin.tmp", 1) > 1 Then
            For $count = 2 To Ubound($data) - 2
                if FileExists($iEdir & $data[$count]) = 1 Then
                    FileDelete($iEdir & $data[$count])
                EndIf
                FileWrite($iEdir & $data[$count], $data[$count + 1])
                $filestract += 1
                $count += 1
            Next
        EndIf
    Elseif $iFBIN <> "0" Then
        For $count = 0 To Ubound($data) - 1
            if $data[$count] = $iFBIN Then
                if FileExists($iEdir &"\"& $iFBIN) = 1 Then
                    FileDelete($iEdir &"\"& $iFBIN)
                EndIf
                FileWrite($iEdir &"\"& $iFBIN, $data[$count + 1])
                $filestract += 1
            EndIf
        Next
    EndIf
    FileDelete(@ScriptDir &"\bin.tmp")
    FileDelete(@ScriptDir &"\bin2.tmp")
    if $iMODE = 1 Then
        FileDelete($iPATH)
    EndIf
    Return $filestract
EndFunc

Func _binary_getBinNames($iPATH)
    if FileExists($iPATH) = 0 OR $iPATH = "" Then
        Return -1
    EndIf
    FileWrite(@ScriptDir &"\bin.tmp", _HexToString(FileRead($iPATH)))
    $split1 = StringInStr(FileRead(@ScriptDir &"\bin.tmp"), @CRLF, Default, 1)
    FileWrite(@ScriptDir &"\bin2.tmp", StringTrimLeft(FileRead(@ScriptDir &"\bin.tmp"), $split1 + 1))
    $data = StringSplit(FileRead(@ScriptDir &"\bin2.tmp"), "|BREAK|", 3)
    Local $result[Ubound($data) / 2 + 1], $filenumb = 0
    $result[0] = FileReadLine(@ScriptDir &"\bin.tmp", 1)
    For $count = 1 To Ubound($result) - 1
        $result[$count] = $data[$filenumb]
        $filenumb += 2
    Next
    FileDelete(@ScriptDir &"\bin.tmp")
    FileDelete(@ScriptDir &"\bin2.tmp")
    Return $result
EndFunc

Func _binary_getBinNamesHandle($iHANDLE)
    if IsArray($iHANDLE) = 0 OR $iHANDLE = "" Then
        Return -1
    EndIf
    Local $result[Ubound($iHANDLE, 1)]
    
    $result[0] = $iHANDLE[0][0]
    
    For $count = 1 To Ubound($result) - 1
        $result[$count] = $iHANDLE[$count][0]
    Next
    Return $result
EndFunc

Func _binary_getBinData($iPATH)
    if FileExists($iPATH) = 0 OR $iPATH = "" Then
        Return -1
    EndIf
    FileWrite(@ScriptDir &"\bin.tmp", _HexToString(FileRead($iPATH)))
    $split1 = StringInStr(FileRead(@ScriptDir &"\bin.tmp"), @CRLF, Default, 1)
    FileWrite(@ScriptDir &"\bin2.tmp", StringTrimLeft(FileRead(@ScriptDir &"\bin.tmp"), $split1 + 1))
    $data = StringSplit(FileRead(@ScriptDir &"\bin2.tmp"), "|BREAK|", 3)
    Local $result[Ubound($data) / 2 + 1], $filenumb = 1
    $result[0] = FileReadLine(@ScriptDir &"\bin.tmp", 1)
    For $count = 1 To Ubound($result) - 1
        $result[$count] = $data[$filenumb]
        $filenumb += 2
    Next
    FileDelete(@ScriptDir &"\bin.tmp")
    FileDelete(@ScriptDir &"\bin2.tmp")
    Return $result
EndFunc

Func _binary_getBinDataHandle($iHANDLE)
    if IsArray($iHANDLE) = 0 OR $iHANDLE = "" Then
        Return -1
    EndIf
    Local $result[Ubound($iHANDLE, 1)]
    
    $result[0] = $iHANDLE[0][0]
    
    For $count = 1 To Ubound($result) - 1
        $result[$count] = $iHANDLE[$count][1]
    Next
    Return $result
EndFunc

Func _binary_lockBin($iPATH, $iPASSWORD)
    if FileExists($iPATH) = 0 OR $iPATH = "" OR $iPASSWORD = "" Then
        Return 0
    EndIf
    Local $key = _Crypt_DeriveKey($iPASSWORD, $CALG_RC4)
    $data1 = _Crypt_EncryptData(FileRead($iPATH), $key, $CALG_USERKEY)
    if @error <> 0 Then
        Return 0
    EndIf
    _Crypt_DestroyKey($key)
    FileDelete($iPATH)
    FileWrite($iPATH, $data1)
    Return 1
EndFunc

Func _binary_unlockBin($iPATH, $iPASSWORD)
    if FileExists($iPATH) = 0 OR $iPATH = "" OR $iPASSWORD = "" Then
        Return 0
    EndIf
    Local $key = _Crypt_DeriveKey($iPASSWORD, $CALG_RC4)
    $data1 = _Crypt_DecryptData(FileRead($iPATH), $key, $CALG_USERKEY)
    if @error <> 0 Then
        Return 0
    EndIf
    _Crypt_DestroyKey($key)
    FileDelete($iPATH)
    FileWrite($iPATH, $data1)
    Return 1
EndFunc

Func _binary_binIsLocked($iPATH)
    if FileExists($iPATH) = 0 OR $iPATH = "" Then
        Return 0
    EndIf
    $result = StringInStr(_HexToString(FileRead($iPATH)), "|BREAK|")
    if $result = 0 Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc
    
;INTERNAL FUNCTIONS
Func _getFilename($iDir, $iExt = 1)
    if $iExt <> 0 AND $iExt <> 1 Then
        Return 0
    EndIf
    if $iExt = 1 Then
        $result = StringRight($iDir, StringinStr(_StringReverse($iDir), "\")-1)
        if @error = 0 Then
            Return $result
        Else
            Return 0
        EndIf
    Elseif $iExt = 0 Then
        $result = StringRight($iDir, StringinStr(_StringReverse($iDir), "\")-1)
        $result2 = StringTrimRight($result, StringInStr(_StringReverse($iDir), "."))
        if @error = 0 Then
            Return $result2
        Else
            Return 0
        EndIf
    EndIf
EndFunc

Enjoy! Be careful with the addbin/createbin function, if you do NOT provide a complete path, it will delete all the files in a relative path to the UDF when extracting, because the program is set to delete already existing files, but the filenames won't save correctly in the bin file, thus causing it to delete the directory instead. I will try to work this out later.

HERE ARE SOME EXAMPLES:

CREATE THE BIN:

#include <AUBinary.au3>

$binhandle = _binary_createBin('.\file1.jpg', 1)
$binhandle = _binary_addBin($binhandle, '.\file2.txt')
$binhandle = _binary_addBin($binhandle, '.\file3.exe')
MsgBox(64, "Created", _binary_saveBin($binhandle, 'test.jbin'))

EXTRACT ALL FILES FROM BIN:

#include <AUBinary.au3>

MsgBox(64, "Extracted", _binary_extractBin('test.jbin', "", 0, 0))

EXTRACT 1 FILE FROM BIN, AND TO C:\Windows, AND DELETING THE ORIGINAL BIN:

#include <AUBinary.au3>

MsgBox(64, "Extracted", _binary_extractBin('test.jbin', "C:\Windows", "file1.jpg", 1))

GETTING STATISTICS:

#include <AUBinary.au3>

$results1 = _binary_getBinNames('test.jbin')
$results2 = _binary_getBinData('test.jbin')

FileWrite(@ScriptDir &"\log.txt", "# of Files:"& $results1[0] & @CRLF &"File Names:" & @CRLF)

For $count = 1 To Ubound($results1) - 1
    FileWrite(@ScriptDir &"\log.txt", $results1[$count] & @CRLF)
Next

FileWrite(@ScriptDir &"\log.txt", "File Data:" & @CRLF)

For $count = 1 To Ubound($results2) - 1
    FileWrite(@ScriptDir &"\log.txt", $results2[$count] & @CRLF)
Next

MsgBox(64, "LOGGED", "Logged stats.")
Exit

LOCKING BIN:

#include <AUBinary.au3>

MsgBox(64, "LOCKED", _binary_lockBin('test.jbin', "password"))
MsgBox(64, "ISLOCKED?", _binary_binIsLocked('test.jbin'))

UNLOCKING BIN:

#include <AUBinary.au3>

MsgBox(64, "LOCKED", _binary_unlockBin('test.jbin', "password"))
MsgBox(64, "ISLOCKED?", _binary_binIsLocked('test.jbin'))

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi, I made a UDF called AUBinary.au3, and with it you can make "file bins" which combine files into one, kinda like a .zip/.rar file, but easier to use with AutoIT.

Very interesting!

It would however, be better if you said -

and with it you can make "file bins" which collect files into one package, kinda like a .zip/.rar file

less misleading that way.

Combine is too much like Join or Merge.

Collect or Gather are a better decription.

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

#include <AUBinary.au3>

$binhandle = _binary_createBin('.\file1.jpg', 1)

$binhandle = _binary_addBin($binhandle, '.\file2.txt')

$binhandle = _binary_addBin($binhandle, '.\file3.exe')

MsgBox(64, "Created", _binary_saveBin($binhandle, 'test.jbin'))

Not created, return 0.

Share this post


Link to post
Share on other sites

Not created, return 0.

Share this post


Link to post
Share on other sites

Not created, return 0.

That's because the files don't actually exist... you need to create them. Make sure the file extensions are correct too. Also make sure you keep .\ in the string or use @ScriptDir, for now, I can't stress that enough.

Share this post


Link to post
Share on other sites

_binary_lockBin and _binary_unlockBin why the two functions are the same?

Share this post


Link to post
Share on other sites

_binary_lockBin and _binary_unlockBin why the two functions are the same?

They aren't the same, very similar, but NOT the same. The actual encryption functions are different 1 is _encryptdata, the other is _decryptdata.

Share this post


Link to post
Share on other sites

This is very interesting and I was looking at creating something like this today, but thought I would search just in case. I will test the UDF and might add extra features.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
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
Sign in to follow this  
Followers 0