Jump to content

Hex Packet UDF


Luig
 Share

Recommended Posts

This my first UDF that I release. Thanks Matt for helping me on this. Basically this is a hex packet UDF. The most common use for this is to make Server/Client emulators for things such as but limited to MMO Games to things like MSN Messenger and so on.

UDF:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.1.6 (beta)
 Author:         Luig

 Script Function:
    A simple to use Hex Packet UDF.

#ce ----------------------------------------------------------------------------

#include-once

Global $BufferData[1]
Global $Index = 0
Global $Length = 0

; Creates a new buffer
; Size - Size of buffer
Func NewPacket()
    ReDim $BufferData[1]
    $Length = 0
    $Index = 0
EndFunc

; Creates a new buffer
; ArrayOfBytes - The byte array the buffer should use
Func LoadPacket($ArrayOfBytes)
    $BufferData = $ArrayOfBytes
    $Length = UBound($BufferData)
    $Index = 0
EndFunc

; Resizes the buffer
; Deltasize -
Func Resize($Deltasize)
    ReDim $BufferData[$Length + $Deltasize]
    $Length += $Deltasize
EndFunc

; Returns the buffer data
; Returns - Buffer data
Func GetBuffer()
    return $BufferData
EndFunc

; Returns the buffer data in a byte array
; Returns - The buffer data
Func GetWrittenBuffer()
    Local $result = $BufferData

    If ($Index > $Length) Then
        ReDim $result[$Index - 1]
    Else
        ReDim $result[$Index]
    EndIf

    Return $result
EndFunc

; Converts a packet to a binary string
; Returns - The Packet in binary string form
Func PacketToString($Array = $BufferData)
    Local $String = ""
    For $i = 0 to UBound($Array) - 1
        $String &= StringTrimLeft($Array[$i], 2)
    Next
    Return "0x" & $String
EndFunc

; Converts a binary string to a packet
; Returns the binary string in packet form
Func StringToPacket($String)
    Local $PacketArray[1], $StringLen
    $String = StringTrimLeft($String, 2)
    $StringLen = StringLen($String)
    Redim $PacketArray[($StringLen / 2)]
    For $i = 0 to ($StringLen / 2) - 1
        $PacketArray[$i] = "0x" & StringLeft($String, 2)
        $String = StringTrimLeft($String, 2)
    Next
    Return $PacketArray
EndFunc

; Sets the index to the given position
; nIndex - New index
; Returns - True If the operation completed
Func setIndex($nIndex)
    If ($Index > $Length) Then Return False
    $Index = $nIndex
    Return True
EndFunc

; Sets the index to current pos + new index
; nIndex - New index
; Returns - True If operation completed
Func addIndex($nIndex)
    return setIndex($Index + $nIndex)
EndFunc

; Sets the index to 0
Func resetIndex()
    setIndex(0)
EndFunc

; Writes a byte to the buffer
; Byte - The byte to write
Func WriteByte($Byte)
    If ($Index >= $Length) Then Resize(($Index - $Length) + 1)
    $BufferData[$Index] = $Byte
    $Index += 1
EndFunc

; Writes a short (Int16) to the buffer
; ShortInteger - The short to write
Func WriteShort($ShortInteger)
    If ($Index + 2 >= $Length) Then Resize(($Index - $Length) + 2)
    WriteByte("0x" & Hex(BitAND($ShortInteger, 0xFF), 2))
    WriteByte("0x" & Hex(BitAND(BitShift($ShortInteger, 8), 0xFF), 2))
EndFunc


; Writes an integer to the buffer
; Integer - The int you want to write
Func WriteInt($Integer)
    If ($Index + 4 >= $Length) Then Resize(($Index - $Length) + 4)
    WriteByte("0x" & Hex(BitAND($Integer, 0xFF), 2))
    WriteByte("0x" & Hex(BitAND(BitShift($Integer,  8), 0xFF), 2))
    WriteByte("0x" & Hex(BitAND(BitShift($Integer, 16), 0xFF), 2))
    WriteByte("0x" & Hex(BitAND(BitShift($Integer, 24), 0xFF), 2))
EndFunc

; Writes a Long to the buffer
Func WriteLong($Integer)
    WriteByte(BitAND($Integer, 0xFF))
    WriteByte(BitAND(BitShift($Integer, 8), 0xFF))
    WriteByte(BitAND(BitShift($Integer, 16), 0xFF))
    WriteByte(BitAND(BitShift($Integer, 24), 0xFF))
    WriteByte(BitAND(BitShift($Integer, 32), 0xFF))
    WriteByte(BitAND(BitShift($Integer, 40), 0xFF))
    WriteByte(BitAND(BitShift($Integer, 48), 0xFF))
    WriteByte(BitAND(BitShift($Integer, 56), 0xFF))
EndFunc

; Writes a byte n number of times to the buffer
; Pad - The byte you want to pad
; nLength - The number of bytes to write
Func BytePad($Pad, $nLength)
    If (($Index + $nLength) >= $Length) Then Resize($nLength)
    For $i = 0 To $nLength - 1
        $BufferData[$Index + $i] = $Pad
    Next
    $Index += $nLength
EndFunc

; Writes a string to the buffer
; String - The string you want to write
Func WriteString($String)
    If (($Index + StringLen($String)) >= $Length) Then Resize(StringLen($String))
    For $i = 0 To StringLen($String) - 1
        $BufferData[$Index + $i] = "0x" & Hex(Asc(StringMid($String, $i + 1, 1)), 2)
    Next
    $Index += StringLen($String)
EndFunc

; Writes a string preceeded by a short containing its length
; String - The string you want to write
Func WriteStringLen($String)
    If (($Index + StringLen($String) + 2) >= $Length) Then Resize(StringLen($String) + 2)
    WriteShort("0x" & Hex(StringLen($String), 4))
    WriteString($String)
EndFunc

; Writes a padded left string to the buffer
; String - The string you want to write
; Pad - The char you want to pad
; PadLength - The number of chars you want to pad
Func WriteStringPaddedLeft($String, $Pad, $PadLength)
    $String = _StringRepeat($Pad, $PadLength) & $String
    If (($Index + StringLen($String)) >= $Length) Then Resize(StringLen($String))
    WriteString($String)
EndFunc

; Writes a padded right string to the buffer
; String - The string you want to write
; Pad - The char you want to pad
; PadLength - The number of chars you want to pad
Func WriteStringPaddedRight($String, $Pad, $PadLength)
    $String &= _StringRepeat($Pad, $PadLength)
    If (($Index + StringLen($String)) >= $Length) Then Resize(StringLen($String))
    WriteString($String)
EndFunc

; Writes an array of bytes to the buffer
; Array - The array you want to write
Func WriteArray($Array)
    If (($Index + UBound($Array)) >= $Length) Then Resize(UBound($Array))
    For $i = 0 To UBound($Array) - 1
        $BufferData[$Index + $i] = $Array[$i]
    Next
    $Index += UBound($Array)
EndFunc

; Write a hex string to the buffer
; HexString - The string containing the hex chain
Func WriteHexString($HexString)
    Local $_byte = HexEncodingGetBytes($HexString)
    WriteArray($_byte)
EndFunc

; Reads a byte from the buffer
; Returns - The read byte or 0 If failed
Func ReadByte()
    Local $result = $BufferData[$Index];
    $Index += 1
    return $result
EndFunc

; Reads a byte at a certain position in the buffer
; pos - The position to read from
; Returns - The read byte or 0 If failed
Func ReadByteAtPos($pos)
    Local $TempIndex = $Index
    $Index = $pos
    Local $result = ReadByte()
    $Index = $TempIndex
    return $result
EndFunc

; Reads an unsigned short from the buffer
; Returns - The read ushort
Func ReadUShort()
    Local $result = $BufferData[$Index] + ($BufferData[$Index + 1] * 256)
    $Index += 2
    return $result
EndFunc

; Reads a short from the buffer
; Returns - The read short
Func ReadShort()
    Local $byte1, $byte2
    $byte1 = ReadByte()
    $byte2 = ReadByte()
    return BitShift($byte2, -8) + $byte1
EndFunc

; Reads a short from the buffer at the given position
; Returns - The read
; pos - Position to read atshort
Func ReadShortAtPos($pos)
    Local $TempIndex = $Index
    $Index = $pos
    Local $result = ReadShort()
    $Index = $TempIndex
    Return $result
EndFunc

; Reads an integer from the buffer
; Returns - The read int
Func ReadInt()
    Local $byte1 = ReadByte()
    Local $byte2 = ReadByte()
    Local $byte3 = ReadByte()
    Local $byte4 = ReadByte()

    Return BitShift($byte4, -24) + BitShift($byte3, -16) + BitShift($byte2, -8) + $byte1
EndFunc

; Reads an unsigned int from the buffer at the given position
; Pos - The position to read at
; Returns - The read uint
Func ReadIntAtPos($Pos)
    Local $TempIndex = $Index
    $Index = $Pos
    Local $result = ReadInt()
    $Index = $TempIndex
    Return $result
EndFunc

; Reads a string that is as long as the provided length from the buffer
; _length - Length of the string to be read
; Returns - The read string
Func ReadString($_length)
    Local $ret
    For $i = 0 To $_length - 1
        $ret &= Chr($BufferData[$Index + $i])
    Next
    $Index += $_length
    Return $ret
EndFunc

; Reads a string that is as long as the provided length from the buffer
; _length - Length of the string to be read
; Returns - The read string
Func ReadStringFrom0($_length)
    Local $ret
    For $i = 0 To $_length - 1
        If ($BufferData[$Index + $i] <> 0) Then $ret &= Chr($BufferData[$Index + $i])
    Next
    $Index += $_length
    Return $ret
EndFunc

; Reads a string using the short length preceding the string from the buffer
; Returns - The read string
Func ReadStringFromLength()
    Local $_length = ReadShort()
    Return ReadString($_length)
EndFunc

; Reads a string using the short length preceding the string from the buffer at the given position
; Pos - The position to read at
; Returns - The read string
Func ReadStringFromLengthAtPos($Pos)
    Local $TempIndex = $Index
    $Index = $Pos
    Local $result = ReadStringFromLength()
    $Index = $TempIndex
    Return $result
EndFunc

; Reads a string that is as long as the provided length from the buffer at the given position
; Pos - The position to read at
; nLength - The length of the string to be read
; Returns - The read string
Func ReadStringSpecIfy($Pos, $nLength)
    Local $TempIndex = $Index
    $Index = $Pos
    Local $result = ReadString($nLength)
    $Index = $TempIndex
    Return $result
EndFunc

; Pads the packet
; b -
; amount -
Func Pad($b, $amount)
    For $i = 0 To $amount - 1
        WriteByte($<img src='http://www.autoitscript.com/forum/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' />
    Next
EndFunc

; Read an amount of bytes from the buffer
Func ReadBytes($amt)
    Local $buffer[$amt]
    _Array_Copy($BufferData, $Index, $buffer, 0, $amt)
    $Index += $amt
    return $buffer
EndFunc

; Skip a specified amount of bytes in the buffer
Func Skip($amt)
    $Index += $amt
EndFunc

; Skip a single byte in the buffer
Func Skip1()
    $Index += 1;
EndFunc

; Misc Functions
; ---------------------------------------------------------
Func HexEncodingGetBytes($HexString)
 $SplitArray = StringSplit($HexString, " ", 2)
 For $i = 0 to UBound($SplitArray) - 1
     $SplitArray[$i] = "0x"&$SplitArray[$i]
 Next
 Return $SplitArray
EndFunc

Func _Array_Copy(ByRef $Source, $SourceIndex, ByRef $DestArray, $DestIndex, $Length)
    Local $TempIndex = $DestIndex
    For $i = $SourceIndex to ($Length + $SourceIndex) - 1
        $DestArray[$TempIndex] = $Source[$i]
        $TempIndex += 1
    Next
EndFunc

Func _Array_Copy1(ByRef $Source, ByRef $DestArray, $Length)
    Local $TempIndex = 0
    For $i = 0 to ($Length) - 1
        $DestArray[$TempIndex] = $Source[$i]
        $TempIndex += 1
    Next
EndFunc

Func GetBytes($int)
    $result = Hex($int)
    If Not IsInt(StringLen($result)/2) Then $result = StringLeft($result,StringLen($result)-1)&"0"&StringRight($result, 1)
;~  ConsoleWrite($result)
    Dim $byteArray[StringLen($result)/2]
    For $i = 0 To StringLen($result)/2-1
        $byteArray[$i] = Dec(StringMid($result, (StringLen($result)/2-$i)*2-1, 2))
    Next
    Return $byteArray
EndFunc

Please leave feedback.

Note you will need to include String.au3

Link to comment
Share on other sites

  • 2 weeks later...
  • 4 weeks later...

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