Jump to content

Write to file in binary


Recommended Posts

Hi, so im making a file transfer script that uses tcp, and it was almost perfect at one point, the issue is that somewhere along the line, i made something that broke the downloaded files, what happens now is that it seems like the data is being written to file as a string, where the information should be text, it's in hex, i tried many ways to put it back together, but i think i've been looking at this for too long, and so i ask for help, and i hope someone can spot the issue.

Thanks in advance.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Version=Beta
#AutoIt3Wrapper_Icon=.ico
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Res_Comment=By: careca
#AutoIt3Wrapper_Res_Description=TCP Transfer
#AutoIt3Wrapper_Res_SaveSource=y
#AutoIt3Wrapper_Res_Icon_Add=.ico
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Run_Debug_Mode=n
#pragma compile(CompanyName, 'careca')
#pragma compile(x64, false)
#pragma compile(UPX, False)
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;=============================================================================
#include <File.au3>
#include <Array.au3>
#include <String.au3>
#include <FileConstants.au3>
#include <TrayConstants.au3>
#include <GUIConstantsEx.au3>
#include <AutoItConstants.au3>
#include <WindowsConstants.au3>
;=============================================================================
Opt("TrayMenuMode", 1)
Opt("TrayIconHide", 0)
Opt("GUIResizeMode", 1)
Opt("TrayIconDebug", 1)
Opt("TrayAutoPause", 0)
Opt("MouseCoordMode", 2)
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)
Opt("GUIEventOptions", 1)
Opt("TrayOnEventMode", 1)
Opt("ExpandEnvStrings", 1)
Opt("WinDetectHiddenText", 1)
;=============================================================================
Global $CurrentPath = @ScriptDir
Global $ServerIp = @IPAddress1
Global $ServerPort = '50911'
Global $iOffset = 0
Global $NotFirst = 0
Global $i4KiB = 2048
Global $iFileSize, $Split
Global $UpdateDir_MainSocket = -1
Global $UpdateDir_Ret = -1
Global $UpdateDir_Buffer = ''
Global $UpdateDir_Bytes = 0
Global $FO, $FolderSel, $SelectFile, $iError, $Socket, $SocketA, $FileDataList, $hFile, $File, $SecSplit, $TCPRecv
Global $ChkUpd, $ExitItem, $ServerIpInpt, $Perc, $Label1, $Label2, $Confirm, $ConfirmFE, $FileSize, $StrLeft, $b2Str, $bData2File
Global $GUI = GUICreate('TCP Transfer', 200, 150, -1, -1, $WS_SYSMENU)
$ServerIpInpt = GUICtrlCreateInput($ServerIp, 10, 10, 100, 20)
$SelectFile = GUICtrlCreateButton('FileToSend', 10, 40, 100, 20)
GUICtrlSetOnEvent($SelectFile, "FileSel")
$Label1 = GUICtrlCreateLabel('TCPListen Socket', 10, 70, 100, 20)
$Label2 = GUICtrlCreateLabel('TCPAccept Socket', 10, 90, 100, 20)
$ChkUpd = TrayCreateItem("Check Update")
TrayItemSetOnEvent(-1, "Update")
$ExitItem = TrayCreateItem("Close")
TrayItemSetOnEvent(-1, "Quit")
HotKeySet('{END}', 'Quit')
TraySetState(1)
TraySetClick(8)
;GUISetIcon($InstallDir & '\1.ico', -1, $GUI)
;TraySetIcon($InstallDir & '\1.ico')
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
;GUISetOnEvent($GUI_EVENT_MINIMIZE, "Minimize")
GUISetState(@SW_SHOW, $GUI)
;=============================================================================
TCPStartup()
$UpdateDir_MainSocket = TCPListen($ServerIp, $ServerPort)
ConsoleWrite('TCPListen: ' & $UpdateDir_MainSocket & @CRLF)
GUICtrlSetData($Label1, $UpdateDir_MainSocket)
If $UpdateDir_MainSocket = -1 Then
    ConsoleWrite('Invalid Socket' & @CRLF)
    Quit()
EndIf
;=============================================================================
While 1
    $ServerIp = GUICtrlRead($ServerIpInpt)
    $SocketA = TCPAccept($UpdateDir_MainSocket)
    GUICtrlSetData($Label2, $SocketA)
    If $SocketA <> -1 Then
        Receive()
        $NotFirst = 0
        Quit()
    EndIf
    Sleep(50)
WEnd
;=============================================================================
Func FileSel()
    $FolderSel = FileSelectFolder('Select Folder to Upload', @ScriptDir, 6, @ScriptDir)
    If $FolderSel <> '' Then
        Upload()
    EndIf
EndFunc   ;==>FileSel
;=============================================================================
Func Upload()
    $FileDataList = _FileListToArrayRec($FolderSel, Default, $FLTAR_FILES, 1, 0, 1)
    For $i = 1 To $FileDataList[0]
        Do
            $Socket = TCPConnect($ServerIp, $ServerPort)
            Sleep(100)
        Until $Socket <> -1
        ConsoleWrite('Connected! = ' & $Socket & @CRLF)
        ConsoleWrite('Sent File: ' & $FileDataList[$i] & @CRLF)
        ConsoleWrite('FullFilePath: ' & $FolderSel & '\' & $FileDataList[$i] & @CRLF)
        $iFileSize = FileGetSize($FolderSel & '\' & $FileDataList[$i])
        ConsoleWrite('FileSize: ' & $iFileSize & @CRLF)
        TCPSend($Socket, $FileDataList[$i] & '<FileSize>' & FileGetSize($FolderSel & '\' & $FileDataList[$i]) & '<StartOfFile>')
        Sleep(1000)
        ;=============================================================================
        $hFile = FileOpen($FolderSel & '\' & $FileDataList[$i], $FO_BINARY)
        $iOffset = 0
        Do
            FileSetPos($hFile, $iOffset, $FILE_BEGIN)
            TCPSend($Socket, FileRead($hFile, $i4KiB))
            If @error Then
                $iError = @error
                MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not send the data, Error code: " & $iError)
                TCPCloseSocket($Socket)
                Return False
            EndIf
            $iOffset += $i4KiB
            $Perc = Round($iOffset / $iFileSize * 100, 1)
            If $Perc > 100 Then
                ConsoleWrite('Progress: 100%' & @CRLF)
            Else
                ConsoleWrite('Progress: ' & $Perc & '%' & @CRLF)
            EndIf
        Until $iOffset >= $iFileSize
        FileClose($hFile)
        ;=============================================================================
        Do
            TCPSend($Socket, "{EndOfFile}")
            $File = TCPRecv($Socket, $i4KiB, 0)
            ConsoleWrite('FileEndNotification: ' & $File & @CRLF)
        Until StringInStr($File, 'FileEndNotification') <> 0
        ConsoleWrite('FileEndNotification! DoNE!!! ' & $File & @CRLF)
        TCPSend($Socket, "FileEndNotificationConfirm")
        ;=============================================================================
        If $i = $FileDataList[0] Then
            TCPSend($Socket, "{EndOfTheTransfers}")
        EndIf
        ;=============================================================================
        TCPCloseSocket($Socket)
        Sleep(1500)
    Next
    Quit()
EndFunc   ;==>Upload
;=============================================================================
Func Receive()
    Do
        If $NotFirst = 1 Then
            Do
                $Socket = TCPAccept($UpdateDir_MainSocket)
                Sleep(100)
            Until $Socket <> -1
            ConsoleWrite('Connected: ' & $Socket & @CRLF)
        EndIf
        Sleep(500)
        Do
            $TCPRecv = TCPRecv($SocketA, $i4KiB, 1)
            ConsoleWrite('TCPRecv: ' & $TCPRecv & @CRLF) ;BinaryToString()
        Until $TCPRecv <> ''
        ;$File = BinaryToString($File)
        $Split = StringSplit(BinaryToString($TCPRecv), '<FileSize>', 1)
        ;$Split = StringSplit($File, '<FileSize>', 1)
        If $Split[0] <> 0 Then
            $File = $Split[1]
            ConsoleWrite('FileName: ' & $File & @CRLF)
            $SecSplit = StringSplit($Split[2], '<StartOfFile>', 1)
            $FileSize = $SecSplit[1]
            ConsoleWrite('FileSize: ' & $FileSize & @CRLF)
            ;$bData &= StringToBinary($SecSplit[2])
            ;ConsoleWrite('$SecSplit[2]:' & $bData & @CRLF)
            ;Sleep(2000)
        Else
            MsgBox(64 + 262144, '', 'Split Failed')
            Quit()
        EndIf
        ;=============================================================================
        Local $bData = Binary("")
        Local $iDataLen = 0
        Local $bEOFReached = False
        $bData2File = Binary("")
        Do
            $bData &= TCPRecv($SocketA, $i4KiB, 1)
            If @error Then
                $iError = @error
                MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Connection lost, Error code: " & $iError)
                Exit
                Return False
            EndIf
            ConsoleWrite('$bData <> ' & $bData & @CRLF)
            $iDataLen = BinaryLen($bData)
            ConsoleWrite('$iDataLen: ' & $iDataLen & ' > TotalSize: ' & $FileSize & @CRLF)
            $Perc = Round($iDataLen / $FileSize * 100, 1)
            If $Perc > 100 Then
                ConsoleWrite('Progress: 100%' & @CRLF)
            Else
                ConsoleWrite('Progress: ' & $Perc & '%' & @CRLF)
            EndIf
            If $iDataLen >= $FileSize Then
                ;$bData = '0x' & StringToBinary($bData)
                ;$bData = Binary("0x" & $bData)
                ;$bData = BinaryMid($bData, 1, $FileSize - $iDataLen)
                ;$bData2File = BinaryToString($bData)
                ;$StrLeft = StringLeft($b2Str, StringInStr($bData, '{EndOfFile}') - 1)
                ;$bData2File = BinaryToString($bData)
                $bData = StringTrimLeft($bData, 2)
                ;$bData = Binary("0x" & $bData)
                ;$bData2File = _HexToString($bData)
                ;MsgBox(64 + 262144, '_HexToString', $bData2File)
                ;$bData2File = BinaryToString($bData2File)
                ;MsgBox(64 + 262144, 'BinaryToString', $bData2File)
                $bData2File = $bData
                ;=============================================================================
                $FO = FileOpen('1.txt', 26)
                FileWrite($FO, "0x" & $bData)
                FileClose($FO)
                ;=============================================================================
                $bEOFReached = True
                ConsoleWrite('$bEOFReached = True !!' & @CRLF)
            EndIf
        Until $bEOFReached = True
        DirCreate(@ScriptDir & '\Downloaded\')
        $FO = FileOpen('Downloaded\' & $File, 26)
        FileWrite($FO, "0x" & $bData2File)
        FileClose($FO)
        ;=============================================================================
        Do
            TCPSend($SocketA, 'FileEndNotification')
            $Confirm = TCPRecv($SocketA, $i4KiB, 0)
            Sleep(100)
            If StringInStr($Confirm, '{EndOfTheTransfers}') <> 0 Then ExitLoop
        Until StringInStr($Confirm, 'FileEndNotificationConfirm') <> 0
        ;=============================================================================
        ;$bData = ''
        ;$iDataLen = 0
        Sleep(500)
        ;$Confirm = TCPRecv($SocketA, $i4KiB, 0)
        TCPCloseSocket($SocketA)
        $NotFirst = 1
    Until StringInStr($Confirm, '{EndOfTheTransfers}') <> 0
    ConsoleWrite('{EndOfTheTransfers} DoNE!!! ' & $Confirm & @CRLF)
EndFunc   ;==>Receive
;======================================================================================
Func Update()
    TrayItemSetState($ChkUpd, $TRAY_UNCHECKED)
    IniWrite('TCP.url', 'InternetShortcut', 'URL', 'https://drive.google.com/drive/folders/1o_S_gOIoQJ04LWszi51MIu9rx1l2L1PU')
    ShellExecute('TCP.url')
    FileDelete('TCP.url')
EndFunc   ;==>Update
;=============================================================================
Func Quit()
    TCPCloseSocket($Socket)
    TCPShutdown()
    Exit
EndFunc   ;==>Quit
;=============================================================================

In the screenshot you can see that in the hex editor the original has text to the right, and hex in the left, but in the downloaded file, the hex is in the right and in the left is what i think is binary, this is confusing.

screenshot - 02022019-1045.jpg

screenshot - 02022019-1042.jpg

TCP Standalone - test.au3

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to post
Share on other sites

As you can see in the script:

$FO = FileOpen('Downloaded\' & $File, 26)

and in the upload part:

$hFile = FileOpen($FolderSel & '\' & $FileDataList[$i], $FO_BINARY)

 

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to post
Share on other sites

Hi careca,

I have struggled a few hours on a similar problem.  So to solve my problem I decided to terribly simplify my code by implementing 2 things.

1- sender will create a new tcp session for each file.  A bit of overhead I know but I can now send .exe, .jpg, .txt, etc flawlessly without informing receiver of the type of file.

2- I had problem with mixing ASCII part with binary part, so I removed all of it except filename that I send in the first 32 bytes of the session.  So receiver read first the 32 bytes, decode the filename then read the rest till no more data is received.

Now it works like a charm.

Link to post
Share on other sites

That's exacly what i did, if you take a look at the code, you'll see the only difference is that i send both the filename and the file size, then i start sending the file itself, and it's weird because i have an older script that can send one file and it's successful, but i cannot see any particular difference between the 2 scripts that justify this newer version not working propperly.

This one does work...

TCP Standalone.au3

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...