#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_icon=analyze.ico #AutoIt3Wrapper_outfile=Traffic_Analyze.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #cs ---------------------------------------------------------------------------- AutoIt Version: 3.2.x Version: 1.0 Script Function: Interactive bi-directional manual TCP/IP traffic tool. Revisions : #ce ---------------------------------------------------------------------------- #include #include #include #include #include #include "Traffic_Analyze_GUI.au3" Opt( "GUICloseOnESC", 0 ) Opt( "GUIOnEventMode", 1 ) Opt( "MustDeclareVars", 1 ) Opt( "TrayIconHide", 1 ) While 1 While $listening If $connection == -1 Then $connection = TCPAccept( $socket ) Else If BitAND( GUICtrlGetState( $obSendBTN ), $GUI_DISABLE ) == $GUI_DISABLE Then GUICtrlSetState( $obSendBTN, $GUI_ENABLE ) EndIf Local $data = TCPRecv( $connection, 2048 ) If @error > 0 Then $connection = -1 TCPCloseSocket ( $connection ) CheckConnectionState() EndIf If $data <> "" Then GUICtrlSetData( $ibResultsFIELD, GUICtrlRead( $ibResultsFIELD ) & $data ) EndIf EndIf WEnd ; This stuff helps with dynamic features like enabling the ; server stop/start button when a port is entered If GUICtrlRead( $modeServerRADIO ) == $GUI_CHECKED Then CheckIbState() ElseIf GUICtrlRead( $modeClientRADIO ) == $GUI_CHECKED Then CheckObState() Else MsgBox( 48, "ERROR", "Illegal State" ) EndIf $FOCUS = ControlGetFocus($TITLE) If $FOCUS <> $OLD_FOCUS Then Local $curr_handle = ControlGetHandle($TITLE, "", $FOCUS) If $curr_handle == $obEditHANDLE Then HotKeySet( "^a", "IbSelectAllHandler" ) ElseIf $curr_handle == $ibResultHANDLE Then HotKeySet( "^a", "ObSelectAllHandler" ) Else HotKeySet( "^a" ) EndIf EndIf $OLD_FOCUS = $FOCUS Sleep( 100 ) WEnd ;=============================================================================== ; Description: Establishes a connection with a server ; Parameter(s): none ; Requirement(s): Application should be ready to make a connection (i.e. not ; be in "server mode". ; Return Values(s): none ; Note(s): None ;=============================================================================== Func ConHandler() TCPStartup() Local $ip = TCPNameToIP( GUICtrlRead( $obTargetFIELD ) ) If @error Then MsgBox( 48, "IP Error", @error ) Return EndIf $connection = TCPConnect( $ip, GUICtrlRead( $obPortFIELD ) ) If @error Then Local $msg If @error == 1 Then $msg = "Unable to resolve IP Address" ElseIf @error == 2 Then $msg = "Port error" Else $msg = "Could not connect for an unknown reason" EndIf MsgBox( 48, "Connection Error", $msg ) Else GUICtrlSetData( $obConDisconBTN, "Discon" ) GuiCtrlSetOnEvent( $obConDisconBTN, "DisconHandler" ) GUICtrlSetState( $obSendBTN, $GUI_ENABLE ) GUICtrlSetState( $obTargetFIELD, $GUI_DISABLE ) GUICtrlSetState( $obPortFIELD, $GUI_DISABLE ) GUICtrlSetState( $modeServerRADIO, $GUI_DISABLE ) GUICtrlSetState( $modeClientRADIO, $GUI_DISABLE ) $port = GUICtrlRead( $obPortFIELD ) EndIf EndFunc ;=============================================================================== ; Description: Disconnects a connection with a remote server ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Shuts down TCP - so will work without error even if the ; connection has already failed. ;=============================================================================== Func DisconHandler() TCPShutdown() GUICtrlSetData( $obConDisconBTN, "Con" ) GuiCtrlSetOnEvent( $obConDisconBTN, "ConHandler" ) GUICtrlSetState( $obSendBTN, $GUI_DISABLE ) GUICtrlSetState( $obTargetFIELD, $GUI_ENABLE ) GUICtrlSetState( $obPortFIELD, $GUI_ENABLE ) GUICtrlSetState( $modeServerRADIO, $GUI_ENABLE ) GUICtrlSetState( $modeClientRADIO, $GUI_ENABLE ) $listening = FALSE $connection = -1 $socket = -1 EndFunc ;=============================================================================== ; Description: Sends data to a remote server ; Parameter(s): none ; Requirement(s): A connection should be up and ready to go ; Return Values(s): none ; Note(s): Puts the program in "listening" mode so that any response ; from the remote server will be captured ;=============================================================================== Func SendHandler() If $connection == -1 Then ; Something must have happened to the connection... MsgBox( 0, "SendHandler", "TODO" ) Return EndIf Local $data = GUICtrlRead( $obEditFIELD ) Local $count = TCPSend( $connection, $data ) If @error Then MsgBox( 48, "Send Error", @error ) $connection = -1 $listening = FALSE Else $listening = TRUE EndIf EndFunc ;=============================================================================== ; Description: Starts the server port on the ip address provided ; Parameter(s): none ; Requirement(s): Widget states should be such that this is only called when ; it will work correctly. ; Return Values(s): none ; Note(s): Starts the TCP service and sets the socket to waiting ;=============================================================================== Func IbStartHandler() TCPStartup() $listening = TRUE $port = GUICtrlRead( $ibPortFIELD ) $socket = TCPListen( GUICtrlRead( $ibIpCOMBO), $port ) If $socket = -1 Then MsgBox( 0 + 16 + 8192, "Unable to open socket", "Unable to open Socket - it may be in use" ) $listening = FALSE EndIf If $listening Then GUICtrlSetState( $ibIpCOMBO, $GUI_DISABLE ) GUICtrlSetState( $ibPortFIELD, $GUI_DISABLE ) GUICtrlSetState( $modeServerRADIO, $GUI_DISABLE ) GUICtrlSetState( $modeClientRADIO, $GUI_DISABLE ) GUICtrlSetData( $ibStartStopBTN, "Stop" ) GuiCtrlSetOnEvent( $ibStartStopBTN, "IbStopHandler" ) EndIf EndFunc ;=============================================================================== ; Description: Stops the server listening ; Parameter(s): none ; Requirement(s): Widget states should be such that this is only called when ; it will work correctly. ; Return Values(s): none ; Note(s): Stops the server and tcp service, clears the socket and ; connection ;=============================================================================== Func IbStopHandler() TCPShutdown() $listening = FALSE $socket = -1 $connection = -1 GUICtrlSetState( $ibIpCOMBO, $GUI_ENABLE ) GUICtrlSetState( $ibPortFIELD, $GUI_ENABLE ) GUICtrlSetState( $obSendBTN, $GUI_DISABLE ) GUICtrlSetState( $modeServerRADIO, $GUI_ENABLE ) GUICtrlSetState( $modeClientRADIO, $GUI_ENABLE ) GUICtrlSetData( $ibStartStopBTN, "Start" ) GuiCtrlSetOnEvent( $ibStartStopBTN, "IbStartHandler" ) EndFunc ;=============================================================================== ; Description: Monitors the state of the application while it's in ; "inbound" mode. ; Parameter(s): none ; Requirement(s): Should only be called while in inbound mode ; Return Values(s): none ; Note(s): Manipulates the whole program, but from the perspective of ; expecting data to be sent to the program. ;=============================================================================== Func CheckIbState() If $connection <> -1 Then If BitAND( GUICtrlGetState( $obSendBTN ), $GUI_DISABLE ) == $GUI_DISABLE Then GUICtrlSetState( $obSendBTN, $GUI_ENABLE ) EndIf Return EndIf Local $port = GUICtrlRead( $ibPortFIELD ) If $port > 0 And $port < 65536 Then If BitAND( GUICtrlGetState( $ibIpCOMBO ), $GUI_ENABLE ) == $GUI_ENABLE _ And BitAND( GUICtrlGetState( $ibStartStopBTN ), $GUI_DISABLE ) == $GUI_DISABLE Then GUICtrlSetState( $ibStartStopBTN, $GUI_ENABLE ) If Not $listening Then GUICtrlSetData( $ibStartStopBTN, "Start" ) EndIf EndIf Else If BitAND( GUICtrlGetState( $ibStartStopBTN ), $GUI_ENABLE ) == $GUI_ENABLE Then GUICtrlSetState( $ibStartStopBTN, $GUI_DISABLE ) If $listening Then GUICtrlSetData( $ibStartStopBTN, "Stop" ) EndIf EndIf EndIf EndFunc ;=============================================================================== ; Description: Monitors the state of the application while it's in ; "outbound" mode. ; Parameter(s): none ; Requirement(s): Should only be called while in outbound mode ; Return Values(s): none ; Note(s): Manipulates the whole program, but from the perspective of ; expecting data to be sent from the program to a server. ;=============================================================================== Func CheckObState() If $connection <> -1 Then Return EndIf Local $port = GUICtrlRead( $obPortFIELD ) Local $target = GUICtrlRead( $obTargetFIELD ) If $port > 0 And $port < 65536 And StringLen( $target ) > 0 Then If BitAND( GUICtrlGetState( $obTargetFIELD ), $GUI_ENABLE ) == $GUI_ENABLE _ And BitAND( GUICtrlGetState( $obConDisconBTN ), $GUI_DISABLE ) == $GUI_DISABLE Then GUICtrlSetState( $obConDisconBTN, $GUI_ENABLE ) EndIf Else If BitAND( GUICtrlGetState( $obTargetFIELD ), $GUI_ENABLE ) == $GUI_ENABLE _ And BitAND( GUICtrlGetState( $obConDisconBTN ), $GUI_ENABLE ) == $GUI_ENABLE Then GUICtrlSetState( $obConDisconBTN, $GUI_DISABLE ) EndIf EndIf EndFunc ;=============================================================================== ; Description: Enables client functionality - which means we can initiate ; a connection. ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Changes what is enabled and disabled. ;=============================================================================== Func EnableClient() GUICtrlSetState( $ibIpCOMBO, $GUI_DISABLE ) GUICtrlSetState( $ibPortFIELD, $GUI_DISABLE ) GUICtrlSetState( $obTargetFIELD, $GUI_ENABLE ) GUICtrlSetState( $obPortFIELD, $GUI_ENABLE ) GUICtrlSetState( $ibStartStopBTN, $GUI_DISABLE ) GUICtrlSetData( $obConDisconBTN, "Con" ) EndFunc ;=============================================================================== ; Description: Enables server functionality - which means we open a port ; and wait for a connection ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Changes what is enabled and disabled ;=============================================================================== Func EnableServer() GUICtrlSetState( $ibIpCOMBO, $GUI_ENABLE ) GUICtrlSetState( $ibPortFIELD, $GUI_ENABLE ) GUICtrlSetState( $obConDisconBTN, $GUI_DISABLE ) GUICtrlSetState( $obTargetFIELD, $GUI_DISABLE ) GUICtrlSetState( $obPortFIELD, $GUI_DISABLE ) EndFunc ;=============================================================================== ; Description: Called when a save button is clicked ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Has hard-coded if/else switch of fields - will complain ; if an unsupported button is clicked ;=============================================================================== Func SaveHandler() Local $save_title Local $data If @GUI_CtrlId == $obSaveBTN Then $save_title = "Save Sent Data" $data = GUICtrlRead( $obEditFIELD ) ElseIf @GUI_CtrlId == $ibSaveBTN Then $save_title = "Save Received Data" $data = GUICtrlRead( $ibResultsFIELD ) Else MsgBox( 48, "ERROR", "Unknown widget called SaveHandler" ) Return EndIf Local $path = FileSaveDialog( $save_title, "", "Text files (*.txt)|All (*.*)", 18 ) If @error Then Return ; Cancelled EndIf Local $fh = FileOpen( $path, 2 ) FileWrite( $fh, $data ) EndFunc ;=============================================================================== ; Description: Called when a load button is clicked ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Has hard-coded if/else switch of fields - will complain ; if an unsupported button is clicked ;=============================================================================== Func LoadHandler() Local $load_title If @GUI_CtrlId == $obLoadBTN Then $load_title = "Load data into outbound field" Else MsgBox( 48, "ERROR", "Unknown widget called SaveHandler" ) Return EndIf Local $path = FileOpenDialog($load_title, "", "Text files (*.txt)|All (*.*)", 1) If @error Then Return GUICtrlSetData( $obEditFIELD, FileRead($path) ) EndFunc ;=============================================================================== ; Description: Called when a clear button is clicked ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Has hard-coded if/else switch of fields - will complain ; if an unsupported button is clicked ;=============================================================================== Func ClearHandler() If @GUI_CTRLID == $obClearBTN Then GUICtrlSetData( $obEditFIELD, "" ) ElseIf @GUI_CtrlId == $ibClearBTN Then GUICtrlSetData( $ibResultsFIELD, "" ) Else MsgBox( 48, "ERROR", "Unknown widget called ClearHandler" ) EndIf EndFunc ;=============================================================================== ; Description: Called when a text conversion button is clicked ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Has hard-coded if/else switch of fields - will complain ; if an unsupported button is clicked ;=============================================================================== Func CvtTextHandler() Local $editor If @GUI_CtrlId == $obTextBTN Then $editor = $obEditFIELD ElseIf @GUI_CtrlId == $ibTextBTN Then $editor = $ibResultsFIELD Else MsgBox( 48, "ERROR", "Unknown widget called CvtTextHandler" ) Return EndIf Local $text = GetSelectedText( $editor ) Local $converted If IsHexContent( $text ) Then $converted = HexToStr( $text ) ElseIf IsB64Content( $text ) Then $converted = Base64_DecodeString( $text ) Else MsgBox( 48, "ERROR", "Unknown content type selected" ) Return EndIf Local $surround = GetBoundedText( $editor ) GUICtrlSetData( $editor, $surround[0] & $converted & $surround[1] ) EndFunc ;=============================================================================== ; Description: Called when a Base64 conversion button is clicked ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Has hard-coded if/else switch of fields - will complain ; if an unsupported button is clicked ;=============================================================================== Func CvtBase64Handler() Local $editor If @GUI_CtrlId == $obBase64BTN Then $editor = $obEditFIELD ElseIf @GUI_CtrlId == $ibBase64BTN Then $editor = $ibResultsFIELD Else MsgBox( 48, "ERROR", "Unknown widget called from CvtBase64Handler" ) Return EndIf Local $text = GetSelectedText( $editor ) Local $converted = Base64_EncodeString( $text ) Local $surround = GetBoundedText( $editor ) GUICtrlSetData( $editor, $surround[0] & $converted & $surround[1] ) EndFunc ;=============================================================================== ; Description: Called when a Hex conversion button is clicked ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Has hard-coded if/else switch of fields - will complain ; if an unsupported button is clicked ;=============================================================================== Func CvtHexHandler() Local $editor If @GUI_CtrlId == $obHexBTN Then $editor = $obEditFIELD ElseIf @GUI_CtrlId == $ibHexBTN Then $editor = $ibResultsFIELD Else MsgBox( 48, "ERROR", "Unknown widget called from CvtHexHandler" ) Return EndIf Local $text = GetSelectedText( $editor ) Local $converted = StrToHex( $text ) Local $surround = GetBoundedText( $editor ) GUICtrlSetData( $editor, $surround[0] & $converted & $surround[1] ) EndFunc ;=============================================================================== ; Description: Called when a CRLF conversion button is clicked ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Has hard-coded if/else switch of fields - will complain ; if an unsupported button is clicked ;=============================================================================== Func CvtCrlfHandler() Local $editor If @GUI_CtrlId == $obCrlfBTN Then $editor = $obEditFIELD ElseIf @GUI_CtrlId == $ibCrlfBTN Then $editor = $ibResultsFIELD Else MsgBox( 48, "ERROR", "Unknown widget called from CvtHexHandler" ) Return EndIf Local $text = GetSelectedText( $editor ) Local $converted = StringRegExpReplace( $text, "(\r\n|\n)", @CRLF ) Local $surround = GetBoundedText( $editor ) GUICtrlSetData( $editor, $surround[0] & $converted & $surround[1] ) EndFunc ;=============================================================================== ; Description: Called when size button is clicked ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Has hard-coded if/else switch of fields - will complain ; if an unsupported button is clicked ;=============================================================================== Func SizeHandler() Local $text = GetSelectedText( $obEditFIELD ) Local $size = StringLen( $text ) MsgBox( 0, "Size", "The selected text is " & $size & " characters" ) EndFunc ;=============================================================================== ; Description: Exits the program ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): none ;=============================================================================== Func ExitHandler() Exit EndFunc ;=============================================================================== ; Description: Called when a copy button is clicked ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Has hard-coded if/else switch of fields - will complain ; if an unsupported button is clicked ;=============================================================================== Func CopyHandler() Local $editor If @GUI_CtrlId == $obCopyBTN Then $editor = $obEditFIELD ElseIf @GUI_CtrlId == $ibCopyBTN Then $editor = $ibResultsFIELD Else MsgBox( 48, "ERROR", "Unknown widget called from CopyHandler" ) Return EndIf Local $text = GetSelectedText( $editor ) ClipPut( $text ) EndFunc ;=============================================================================== ; Description: Called when a paste button is clicked ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): Has hard-coded if/else switch of fields - will complain ; if an unsupported button is clicked ;=============================================================================== Func PasteHandler() Local $editor If @GUI_CtrlId == $obPasteBTN Then $editor = $obEditFIELD ElseIf @GUI_CtrlId == $ibPasteBTN Then $editor = $ibResultsFIELD Else MsgBox( 48, "ERROR", "Unknown widget called from PasteHandler" ) Return EndIf Local $clip = ClipGet() Switch @error Case 0 Local $selection = _GUICtrlEdit_GetSel( $editor ) If $selection[0] <> $selection[1] Then Local $surround = GetBoundedText( $editor ) GUICtrlSetData( $editor, $surround[0] & $clip & $surround[1] ) Else _GUICtrlEdit_InsertText( $editor, $clip, $selection[0] ) EndIf Case 1 MsgBox( 48, "ERROR", "Clipboard is empty" ) Case 2 MsgBox( 48, "ERROR", "Clipboard content is non-text" ) Case 3 To 4 MsgBox( 48, "ERROR", "Unable to access the clipboard" ) EndSwitch EndFunc ;=============================================================================== ; Description: Moves text from the Inbound editor to the outbound editor ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): none ;=============================================================================== Func MoveHandler() GUICtrlSetData( $obEditFIELD, GUICtrlRead( $ibResultsFIELD ) ) GUICtrlSetData( $ibResultsFIELD, "" ) EndFunc ;=============================================================================== ; Description: Selects all text in the inbound field ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): none ;=============================================================================== Func IbSelectAllHandler() _GUICtrlEdit_SetSel( $obEditHANDLE, 0, -1 ) EndFunc ;=============================================================================== ; Description: Selects all text in the outbound field ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): none ;=============================================================================== Func ObSelectAllHandler() _GUICtrlEdit_SetSel( $ibResultHANDLE, 0, -1 ) EndFunc ;=============================================================================== ; Description: Returns the network adaptors associated with the machine ; Parameter(s): none ; Requirement(s): none ; Return Values(s): A pipe separated list of network adaptors ; Note(s): Ready to be directly into a drop-down list ;=============================================================================== Func GetAdaptors() Local $adaptors = "" Local $pipe = "" Local $local_needed = True If @IPAddress1 = "127.0.0.1" Then $local_needed = False EndIf If @IPAddress2 <> "0.0.0.0" Then If @IPAddress2 = "127.0.0.1" Then $local_needed = False EndIf $adaptors = $adaptors & $pipe & @IPAddress2 $pipe = "|" EndIf If @IPAddress3 <> "0.0.0.0" Then If @IPAddress3 = "127.0.0.1" Then $local_needed = False EndIf $adaptors = $adaptors & $pipe & @IPAddress3 $pipe = "|" EndIf If @IPAddress4 <> "0.0.0.0" Then If @IPAddress4 = "127.0.0.1" Then $local_needed = False EndIf $adaptors = $adaptors & $pipe & @IPAddress4 $pipe = "|" EndIf If $local_needed Then $adaptors = $adaptors & $pipe & "127.0.0.1" EndIf Return $adaptors EndFunc ;=============================================================================== ; Description: Displays a simple help message. ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): none ;=============================================================================== Func HelpHandler() MsgBox( 0, "Program Use", $HELP_MESSAGE ) EndFunc ;=============================================================================== ; Description: Turns the string passed into a hex representation ; Parameter(s): $str The string to convert ; Requirement(s): none ; Return Values(s): A hex representation of the string ; Note(s): Each byte is separated by a space. So for example, 0123 ; would come out as 30 31 32 33 ; ; Adds a carriage return and line feed every 16 bytes. ;=============================================================================== Func StrToHex( $str ) Local $chars = StringSplit( $str, "" ) Local $result = "" Local $space = "" For $i = 1 To $chars[0] $result &= $space & Hex( Asc( $chars[$i] ), 2 ) If Mod( $i, 16 ) == 0 Then $space = @CRLF Else $space = " " EndIf Next Return $result EndFunc ;=============================================================================== ; Description: Turns a sequence of hex data into a string representation ; Parameter(s): $data The data to convert ; Requirement(s): Data must be in discrete bytes, separated by spaces. ; Return Values(s): A String made up of the bytes ; Note(s): Example: the following 30 31 32 33 would be returned as 0123 ; ; The return value is a "String", but there's nothing ; preventing the String from containing unprintable bytes. ; The bytes passed are faithfully recreated such that multi- ; byte data will be recreated - even if it's not "text". ; Which means that binary data could be sent to a server if it ; is first converted to a proper format - i.e. hex bytes ; with each byte separated by spaces. ;=============================================================================== Func HexToStr( $data ) If StringRight( $data, 1 ) = " " Then $data = StringLeft( $data, StringLen( $data ) - 1 ) EndIf Local $noCR = StringStripCR( $data ) Local $noLF = StringReplace( $noCR, @LF, " " ) Local $bytes = StringSplit( $noLF, " " ) Local $result = "" For $i = 1 To $bytes[0] $result &= Chr( Dec( $bytes[$i] ) ) Next Return $result EndFunc ;=============================================================================== ; Description: Checks connection status, and possibly resets gui elements. ; Parameter(s): none ; Requirement(s): Should only be called when a previously established ; connection breaks. Assumes that any gui elements ; which show a connection should be reset. ; Return Values(s): non ; Note(s): Only has an affect while the program is in "client mode". ; Resets gui elements when the program looks like there is ; a connection, when in fact there is not. This can happen ; if a connection was previously established, and then the ; server disconnects. ;=============================================================================== Func CheckConnectionState() If GUICtrlRead( $modeClientRADIO ) == $GUI_CHECKED Then DisconHandler() EndIf EndFunc ;=============================================================================== ; Description: Called when the window is resized. Enforces minimum size. ; Parameter(s): $hWind - Handle to window (supplied by Windows) ; $Msg - The message sent by Windows ; $wParam - Window Parameters ; $lParam - More Window Parameters ; Requirement(s): (none) ; Return Values(s): Always zero ; Note(s): Commented out is a way to ensure a maximum size as well ; minimum size. Simply define the two maximum values and ; uncomment the section to enable this feature ;=============================================================================== Func ResizeHandler( $hWnd, $Msg, $wParam, $lParam ) Local $minmaxinfo = DllStructCreate( "int;int;int;int;int;int;int;int;int;int", $lParam ) DllStructSetData( $minmaxinfo, 7, $MINIMUM_WIDTH ) DllStructSetData( $minmaxinfo, 8, $MINIMUM_HEIGHT ) ; Use these if you want a maximum window width and height ; DllStructSetData( $minmaxinfo, 9, $MAXIMUM_WIDTH ) ; DllStructSetData( $minmaxinfo, 10, $MAXIMUM_HEIGHT ) Return 0 EndFunc ;=============================================================================== ; Description: Extracts the IP Address from the remote side of a connection ; Parameter(s): $connection - the connection to get the address from ; Requirement(s): Connection must be active ; Return Values(s): IP Address ; Note(s): Returns zero in the event of a failure ;=============================================================================== Func SocketToIP( $conection ) Local $address = 0 ;Default value in case the call fails Local $sockaddr = DLLStructCreate( "short;ushort;uint;char[8]" ) Local $info = DLLCall( "Ws2_32.dll", "int", "getpeername", "int", $connection, _ "ptr", DLLStructGetPtr($sockaddr), "int_ptr", DLLStructGetSize( $sockaddr )) If Not @error And $info[0] = 0 Then $info = DLLCall( "Ws2_32.dll", "str", "inet_ntoa", "int", DLLStructGetData( $sockaddr, 3 )) If Not @error Then $address = $info[0] EndIf EndIf Return $address EndFunc ;=============================================================================== ; Description: Checks to see if the content passed is Hex ; Parameter(s): $text the text to check ; Requirement(s): none ; Return Values(s): True if the text matches the Hex pattern ; Note(s): The pattern is two hex characters bounded by space and ; wrapped every 16 bytes ;=============================================================================== Func IsHexContent( $text ) Local $lines = StringSplit( $text, @CRLF ) For $i = 1 To $lines[0] If StringLen( $lines[$i] ) > 47 Then Return False EndIf Local $chars = StringSplit( $lines[$i], "" ) If Mod( $chars[0], 3 ) == 1 Then Return False EndIf For $j = 1 To $chars[0] If Mod( $j, 3 ) Then ; We'll allow lower case letters even though we don't use them If Not StringRegExp( $chars[$j], "[[:xdigit:]]" ) Then Return False EndIf Else If Not $chars[$j] = ' ' Then Return False EndIf EndIf Next Next Return True EndFunc ;=============================================================================== ; Description: Checks to see if the content passed is Base64 ; Parameter(s): $text the text to check ; Requirement(s): none ; Return Values(s): True if the text matches the Base64 pattern ; Note(s): The pattern is the following: ; * Only characters 'A->Z', 'a->z', '0->9', '+' and '/' are ; allowed. ; * The last one or two characters can be '=', but no others. ; * The number of characters must be evenly divisible by 4. ; * \r and \n are removed from the string before checking ; the rules above. ;=============================================================================== Func IsB64Content( $text ) Local $ret Local $data = StringReplace( StringReplace( $text, @CR, "" ), @LF, "" ) If Mod( StringLen( $data ), 4 ) Then $ret = False Else For $i = 1 To 2 If StringRight( $data, 1 ) = "=" Then $data = StringLeft( $data, StringLen( $data ) - 1 ) EndIf Next If StringRegExp( $data, "[^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/]" ) Then ; Found something outside the normal allowed characters $ret = False Else $ret = True EndIf EndIf Return $ret EndFunc ;=============================================================================== ; Description: Gets either the selected text or the whole text of the field ; Parameter(s): $field the field to read ; Requirement(s): none ; Return Values(s): the text ; Note(s): If there is NO selection, then the entire text is returned, ; otherwise, just the part that is selected is returned. ;=============================================================================== Func GetSelectedText( $field ) Local $selection = _GUICtrlEdit_GetSel( $field ) Local $content = GUICtrlRead( $field ) If $selection[0] <> $selection[1] Then $content = StringMid($content, $selection[0] + 1, $selection[1] - $selection[0]) EndIf Return $content EndFunc ;=============================================================================== ; Description: Gets the text before and after the selection (if any) ; Parameter(s): $text the text to check ; Requirement(s): none ; Return Values(s): a two-element array ; Note(s): The return will always be a two-element array. ; * If there is NO selection, then both elements will be empty ; ; * If the selection starts at the first character, then the ; first element will be empty, and the last will contain ; everything after the selection. ; ; * If the selection ends at the last character, then the ; first element will contain everything before the selection ; and the last will be empty. ; ; * If entire text is selected, then both elements will be ; empty same as if there is NO selection ;=============================================================================== Func GetBoundedText( $field ) Local $text = GUICtrlRead( $field ) Local $surround[2] = ["", ""] Local $selection = _GUICtrlEdit_GetSel( $field ) If $selection[0] <> $selection[1] Then If $selection[0] > 0 Then $surround[0] = StringLeft( $text, $selection[0] ) EndIf If $selection[1] < StringLen( $text ) Then $surround[1] = StringRight( $text, StringLen( $text) - $selection[1] ) EndIf EndIf Return $surround EndFunc ;=============================================================================== ; Description: Encodes a string into an RFC 2045 compliant Base64 string. ; Parameter(s): $str the string to encode ; $encoding the byte encoding ; $split the number of characters to split on ; Requirement(s): none ; Return Values(s): The string re-encoded into Base64 ; Note(s): The default encoding is UTF-8 (possibly the most common ; non-ASCII encoding), and the default line-split is 76 ; characters (in accordance with RFC 2045). ;=============================================================================== Func Base64_EncodeString($str, $encoding = 4, $split = 76) If StringLen( $str ) == 0 Then Return "" EndIf Local $data = StringToBinary($str, $encoding) Local $encoded = Base64_EncodeBytes($data, $split) Return $encoded EndFunc ;=============================================================================== ; Description: Encodes bytes into an RFC 2045 compliant Base64 string. ; Parameter(s): $data the bytes of data (in a string) to encode ; Requirement(s): none ; Return Values(s): the bytes re-encoded into Base64 ; Note(s): The default encoding is UTF-8 (possibly the most common ; non-ASCII encoding), and the default line-split is 76 ; characters (in accordance with RFC 2045). ;=============================================================================== Func Base64_EncodeBytes($data, $split = 76) If StringLen($data) == 0 Then Return "" EndIf If StringLeft($data, 2) = "0x" Then $data = StringTrimLeft($data, 2) EndIf Local $hex = StringSplit($data, "") Local $len = $hex[0] / 2 Local $pad = Mod((3 - Mod($len, 3)), 3) Local $bytes[$len + $pad + 1] ; Extra space so the loop doesn't barf $bytes[0] = $len Local $index = 1 For $i = 1 To $hex[0] Step 2 Local $int = Dec($hex[$i] & $hex[$i + 1]) $bytes[$index] = $int $index += 1 Next Local $ret = "" Local $mask = Dec("3F") For $i = 1 To $bytes[0] Step 3 Local $packed = BitShift($bytes[$i ], -16) + _ BitShift($bytes[$i + 1], -8) + _ BitShift($bytes[$i + 2], 0) $ret &= $BASE64_CHAR_ARRAY[BitAND( BitShift($packed, 18), $mask )] $ret &= $BASE64_CHAR_ARRAY[BitAND( BitShift($packed, 12), $mask )] $ret &= $BASE64_CHAR_ARRAY[BitAND( BitShift($packed, 6), $mask )] $ret &= $BASE64_CHAR_ARRAY[BitAND( BitShift($packed, 0), $mask )] Next If $pad > 0 Then $ret = StringTrimRight($ret, $pad) $ret &= $BASE64_PADDING[$pad] EndIf If $split Then Return SplitLines($ret, $split) Else Return $ret EndIf EndFunc ;=============================================================================== ; Description: Decodes a Base64 string into text. ; Parameter(s): $str the string of bytes to decode ; Requirement(s): none ; Return Values(s): the original string ; Note(s): None ;=============================================================================== Func Base64_DecodeString($str, $encoding = 0) Local $bin = Base64_DecodeBytes($str) If $encoding == 0 Then ; Set a default $encoding = 4 ; Now try to guess. These are only an educated guess. This works only ; if the absolute first character is not outside the ASCII range. If StringLeft($bin, 4) == "0x00" Then $encoding = 3 ; UTF16 Big Endian ElseIf StringMid($bin, 5, 2) == "00" Then $encoding = 2 ; UTF16 Little Endian EndIf EndIf Local $ret = BinaryToString($bin, $encoding) Return $ret EndFunc ;=============================================================================== ; Description: Decodes a Base64 into stringified bytes. ; Parameter(s): $data the bytes of data (in a string) to encode ; Requirement(s): none ; Return Values(s): the stringified bytes (e.g. "0x02718215") ; Note(s): none ;=============================================================================== Func Base64_DecodeBytes($str) If StringLen($str) == 0 Then Return "" EndIf Local $data = StringReplace(StringReplace($str, @CR, ""), @LF, "") Local $extra = 0 For $i = 0 To 2 Local $char = StringMid($data, StringLen($data) - $i, 1) If $char == "=" Then $extra += 1 EndIf Next $data = StringReplace($data, "=", "A") Local $len = StringLen($data) Local $bin = "" For $i = 1 To $len Step 4 Local $packed = 0 Local $pos $pos = StringInStr($BASE64_CHARS, StringMid($data, $i , 1), 1) - 1 $packed += BitShift($pos, -18) $pos = StringInStr($BASE64_CHARS, StringMid($data, $i + 1, 1), 1) - 1 $packed += BitShift($pos, -12) $pos = StringInStr($BASE64_CHARS, StringMid($data, $i + 2, 1), 1) - 1 $packed += BitShift($pos, -6) $pos = StringInStr($BASE64_CHARS, StringMid($data, $i + 3, 1), 1) - 1 $packed += BitShift($pos, 0) $bin &= Hex(BitAND(BitShift($packed, 16), 255), 2) $bin &= Hex(BitAND(BitShift($packed, 8), 255), 2) $bin &= Hex(BitAND(BitShift($packed, 0), 255), 2) Next If $extra > 0 Then $bin = StringTrimRight($bin, ($extra * 2)) EndIf Return "0x" & $bin EndFunc ;=============================================================================== ; Description: Splits a string based on size ; Parameter(s): $encoded the bytes of data (in a string) to encode ; $split the number of characters before a split ; Requirement(s): Best used on Base64 content ; Return Values(s): the string split every ($split) characters ; Note(s): $split defaults to 76 ;=============================================================================== Func SplitLines($encoded, $split = 76) Local $ret = "" Local $len = StringLen($encoded) For $i = 1 To $len Step $split If $len > ($i + $split) Then $ret &= StringMid($encoded, $i, $split) $ret &= @CRLF Else $ret &= StringMid($encoded, $i) EndIf Next Return $ret EndFunc ;=============================================================================== ; Description: Plays the default error sound ; Parameter(s): none ; Requirement(s): none ; Return Values(s): none ; Note(s): none ;=============================================================================== Func PlayErrorSound() Local $sound = RegRead("HKEY_CURRENT_USER\AppEvents\Schemes\Apps\.Default\.Default\.Current", "") AutoItSetOption("ExpandEnvStrings", 1) SoundPlay($sound) AutoItSetOption("ExpandEnvStrings", 0) EndFunc