#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_AU3Check_Parameters=-d -w 3 -w 4 -w 5 -w 6 #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ; Version 1.0.0 ; _BinaryStream_Create() ; _BinaryStream_Available($aStream) ; _BinaryStream_Clear($aStream) ; _BinaryStream_GetLength($aStream) ; _BinaryStream_SetLength($aStream, $iLength) ; _BinaryStream_GetPosition($aStream) ; _BinaryStream_SetPosition($aStream, $iPosition) ; _BinaryStream_Write($aStream, $bData [, $iLength]) ; _BinaryStream_Read($aStream [, $iLength]) ; _BinaryStream_Peek($aStream [, $iLength]) ; _BinaryStream_ToBinary($aStream [, $iPosition [, $iLength]) ; #FUNCTION# ==================================================================================================================== ; Name ..........: _BinaryStream_Create ; Description ...: Creates a new stream which is represented as a 1 dimensional array with 2 values: ; [0] = The binary data of the stream. ; [1] = The current position of the stream. ; Syntax ........: _BinaryStream_Create([$bData = Default]) ; Parameters ....: $bData - [optional] The initial value of the stream, empty by default. ; Return values .: On success: The stream represented as an array. ; Author ........: Tim Krueger ; =============================================================================================================================== Func _BinaryStream_Create($bData = Default) Local $aStream[2] = [BinaryMid(Null, 1, 0), 0] If ($bData <> Default) And (BinaryLen($bData) > 0) Then $aStream[0] &= Binary($bData) EndIf Return $aStream EndFunc ;==>_BinaryStream_Create ; #FUNCTION# ==================================================================================================================== ; Name ..........: _BinaryStream_Available ; Description ...: Returns the count of bytes available to read from the current stream position. ; Syntax ........: _BinaryStream_Available(Byref $aStream) ; Parameters ....: $aStream - [in/out] The array that represents the stream which was created by _BinaryStream_Create(). ; Return values .: On success: Returns the count of bytes available to read from the current stream position. ; On error: ; -1 : The passed stream is not a valid array! ; -2 : The passed stream has invalid dimensions, it must be a 1 dimensional array with a length of 2! ; -3 : The current position of the stream is outside the stream data boundaries! ; Author ........: Tim Krueger ; =============================================================================================================================== Func _BinaryStream_Available(ByRef $aStream) If Not IsArray($aStream) Then Return SetError(-1, Default, -1) If (UBound($aStream, 0) <> 1) Or (UBound($aStream, 1) > 2) Then Return SetError(-2, Default, -2) Local $iStreamLength = BinaryLen($aStream[0]) If ($aStream[1] < 0) Or ($aStream[1] > $iStreamLength) Then Return SetError(-3, Default, -3) ; Broken stream Return $iStreamLength - $aStream[1] EndFunc ;==>_BinaryStream_Available ; #FUNCTION# ==================================================================================================================== ; Name ..........: _BinaryStream_Clear ; Description ...: Clears the specified stream. This will set its length and position to 0. ; Syntax ........: _BinaryStream_Clear(Byref $aStream) ; Parameters ....: $aStream - [in/out] The array that represents the stream which was created by _BinaryStream_Create(). ; Return values .: On success: Returns 1. ; On error: ; -1 : The passed stream is not a valid array! ; -2 : The passed stream has invalid dimensions, it must be a 1 dimensional array with a length of 2! ; Author ........: Tim Krueger ; =============================================================================================================================== Func _BinaryStream_Clear(ByRef $aStream) If Not IsArray($aStream) Then Return SetError(-1, Default, -1) If (UBound($aStream, 0) <> 1) Or (UBound($aStream, 1) > 2) Then Return SetError(-2, Default, -2) $aStream[0] = BinaryMid(Null, 1, 0) $aStream[1] = 0 Return 1 EndFunc ;==>_BinaryStream_Clear ; #FUNCTION# ==================================================================================================================== ; Name ..........: _BinaryStream_GetLength ; Description ...: Returns the length of the stream in count of bytes. ; Syntax ........: _BinaryStream_GetLength(Byref $aStream) ; Parameters ....: $aStream - [in/out] The array that represents the stream which was created by _BinaryStream_Create(). ; Return values .: On success: Returns the length of the stream in count of bytes. ; On error: ; -1 : The passed stream is not a valid array! ; -2 : The passed stream has invalid dimensions, it must be a 1 dimensional array with a length of 2! ; Author ........: Tim Krueger ; =============================================================================================================================== Func _BinaryStream_GetLength(ByRef $aStream) If Not IsArray($aStream) Then Return SetError(-1, Default, -1) If (UBound($aStream, 0) <> 1) Or (UBound($aStream, 1) > 2) Then Return SetError(-2, Default, -2) Return BinaryLen($aStream[0]) EndFunc ;==>_BinaryStream_GetLength ; #FUNCTION# ==================================================================================================================== ; Name ..........: _BinaryStream_SetLength ; Description ...: Adjusts the length of the stream to the specified value. If the new length is lower than the previous length ; the stream position will be adjusted to the last byte of the new stream length. If the new stream length is ; greater than the previous length, the stream position will remain to its current value. The stream will get ; bytes of value 00 appended to its end until the length fits the new value. ; If the new length equals the old length the whole stream will remain to its current values. ; Syntax ........: _BinaryStream_SetLength(Byref $aStream, $iLength) ; Parameters ....: $aStream - [in/out] The array that represents the stream which was created by _BinaryStream_Create(). ; $iLength - The new length of the stream. ; Return values .: On success: Returns the new length of the stream in count of bytes. ; On error: ; -1 : The passed stream is not a valid array! ; -2 : The passed stream has invalid dimensions, it must be a 1 dimensional array with a length of 2! ; -3 : The passed length is lower than 0! ; Author ........: Tim Krueger ; =============================================================================================================================== Func _BinaryStream_SetLength(ByRef $aStream, $iLength) If Not IsArray($aStream) Then Return SetError(-1, Default, -1) If (UBound($aStream, 0) <> 1) Or (UBound($aStream, 1) > 2) Then Return SetError(-2, Default, -2) If $iLength < 0 Then Return SetError(-3, Default, -3) Local $iStreamLength = BinaryLen($aStream[0]) If $iLength > $iStreamLength Then Local $bPadding = BinaryMid(0, 1, 0) Local $iPaddingLength = 0 ; The following code creates a series of 00-bytes ; It will try to append 1 byte, if that fits into the specified length, it will double the 1 byte ... ; then it will retry to append 2 bytes, if that also fits into the length, it will double the 2 bytes ... ; then it will retry to append 4 bytes, if that ... etc. ; It will re-start at 1 byte again when the doubled data does not fit into the final series anymore. ; this will be continued until the series of n-bytes is finally created. While $iStreamLength < $iLength $bPadding = BinaryMid(0, 1, 1) $iPaddingLength = 1 While ($iStreamLength + $iPaddingLength * 2) < $iLength $bPadding &= $bPadding $iPaddingLength *= 2 WEnd $aStream[0] &= $bPadding $iStreamLength += $iPaddingLength WEnd ElseIf $iLength < $iStreamLength Then $aStream[0] = BinaryMid($aStream[0], 1, $iLength) If $aStream[1] > $iLength Then $aStream[1] = $iLength ; If the stream position would be outside the data it ... ; ... will be set to the end position within the new stream data - in any other ... ; ... case the stream position will remain to its value EndIf EndIf Return $iLength EndFunc ;==>_BinaryStream_SetLength ; #FUNCTION# ==================================================================================================================== ; Name ..........: _BinaryStream_GetPosition ; Description ...: Returns the current position of the specified stream. ; Syntax ........: _BinaryStream_GetPosition(Byref $aStream) ; Parameters ....: $aStream - [in/out] The array that represents the stream which was created by _BinaryStream_Create(). ; Return values .: On success: Returns the current position of the specified stream. ; On error: ; -1 : The passed stream is not a valid array! ; -2 : The passed stream has invalid dimensions, it must be a 1 dimensional array with a length of 2! ; Author ........: Tim Krueger ; =============================================================================================================================== Func _BinaryStream_GetPosition(ByRef $aStream) If Not IsArray($aStream) Then Return SetError(-1, Default, -1) If (UBound($aStream, 0) <> 1) Or (UBound($aStream, 1) > 2) Then Return SetError(-2, Default, -2) Return $aStream[1] EndFunc ;==>_BinaryStream_GetPosition ; #FUNCTION# ==================================================================================================================== ; Name ..........: _BinaryStream_SetPosition ; Description ...: Sets the stream position to the specified value related to an origin. ; Syntax ........: _BinaryStream_SetPosition(Byref $aStream, $iPosition[, $iOrigin = Default]) ; Parameters ....: $aStream - [in/out] The array that represents the stream which was created by _BinaryStream_Create(). ; $iPosition - The position to set the stream to related to the origin. ; $iOrigin - [optional] The origin where to set the position from, valid values are: ; 0 : From the begin of the stream (0) ; 1 : From the current position of the stream ; 2 : From the end of the stream (new position = length - offset) ; The default origin is 0 - from the begin of the stream. ; Return values .: On success: A value greater or equal to 0, representing the new position of the stream. ; On error: ; -1 : The passed stream is not a valid array! ; -2 : The passed stream has invalid dimensions, it must be a 1 dimensional array with a length of 2! ; -3 : The passed position is lower than 0! ; -4 : The passed origin value is invalid! Valid values are: 0, 1 and 2. (see parameter description) ; -5 : The passed position related to the passed origin is resulting in a position outside the stream data! ; Author ........: Tim Krueger ; =============================================================================================================================== Func _BinaryStream_SetPosition(ByRef $aStream, $iPosition, $iOrigin = Default) If Not IsArray($aStream) Then Return SetError(-1, Default, -1) If (UBound($aStream, 0) <> 1) Or (UBound($aStream, 1) > 2) Then Return SetError(-2, Default, -2) If $iPosition < 0 Then Return SetError(-3, Default, -3) ; Note No max validation here ... ; ... later on the calculated value will be validated against the max If $iOrigin = Default Then $iOrigin = 0 ; The origin is the start of the stream by default ElseIf ($iOrigin < 0) Or ($iOrigin > 2) Then Return SetError(-4, Default, -4) EndIf Local $iNewPosition = -1 Local $iStreamLength = BinaryLen($aStream[0]) Switch Int($iOrigin) Case 2 ; End $iNewPosition = $iStreamLength - $iPosition Case 1 ; Current $iNewPosition = $aStream[1] + $iPosition Case Else ; Begin $iNewPosition = $iPosition EndSwitch If $iNewPosition > $iStreamLength Then Return SetError(-5, Default, -5) $aStream[1] = $iNewPosition Return $aStream[1] EndFunc ;==>_BinaryStream_SetPosition ; #FUNCTION# ==================================================================================================================== ; Name ..........: _BinaryStream_Write ; Description ...: Writes the specified binary data to the current stream position. ; Syntax ........: _BinaryStream_Write(Byref $aStream, $bData[, $iLength = Default]) ; Parameters ....: $aStream - [in/out] The array that represents the stream which was created by _BinaryStream_Create(). ; $bData - The binary data to write to the stream. ; $iLength - [optional] The length of the passed binary data to write to the stream. (The whole data ; by default) ; Return values .: On success: Returns the new position of the stream after the bytes were written. ; On error: ; -1 : The passed stream is not a valid array! ; -2 : The passed stream has invalid dimensions, it must be a 1 dimensional array with a length of 2! ; -3 : The current position of the stream is outside the stream data boundaries! ; Author ........: Tim Krueger ; =============================================================================================================================== Func _BinaryStream_Write(ByRef $aStream, $bData, $iLength = Default) If Not IsArray($aStream) Then Return SetError(-1, Default, -1) If (UBound($aStream, 0) <> 1) Or (UBound($aStream, 1) > 2) Then Return SetError(-2, Default, -2) Local $iStreamLength = BinaryLen($aStream[0]) ; To avoid further BinaryLen() calls If ($aStream[1] < 0) Or ($aStream[1] > $iStreamLength) Then Return SetError(-3, Default, -3) ; Broken stream Local $iDataLength = BinaryLen($bData) If $iLength = Default Then $iLength = $iDataLength ; Write the whole input data ElseIf ($iLength <= 0) Or ($iDataLength <= 0) Or ($iLength > $iDataLength) Then Return SetError(-4, Default, -4) ; The length or the length of the input data is 0 ... ; ... or the length exceeds the length of the input data EndIf Local $bNewStreamData If $aStream[1] = $iStreamLength Then $bNewStreamData = $aStream[1] Else $bNewStreamData = BinaryMid($aStream[0], 1, $aStream[1]) EndIf ; The user must already pass binary to avoid unexpected serialization! ; See parameters notes. If $iLength = $iDataLength Then $bNewStreamData &= Binary($bData) Else $bNewStreamData &= BinaryMid($bData, 1, $iLength) EndIf Local $iTempLength = BinaryLen($bNewStreamData) If $iStreamLength > $iTempLength Then $bNewStreamData &= BinaryMid($aStream[0], ($iTempLength + 1)) EndIf $aStream[0] = $bNewStreamData $aStream[1] += $iLength Return $aStream[1] EndFunc ;==>_BinaryStream_Write ; #FUNCTION# ==================================================================================================================== ; Name ..........: _BinaryStream_Read ; Description ...: Reads a specified amount of bytes from the given stream and increments the stream position. ; Syntax ........: _BinaryStream_Read(Byref $aStream[, $iLength = Default]) ; Parameters ....: $aStream - [in/out] The array that represents the stream which was created by _BinaryStream_Create(). ; $iLength - [optional] an integer value. Default is Default. ; Return values .: On success: The bytes that have been read from the stream as binary. ; On error: ; -1 : The passed stream is not an array. ; -2 : The dimensions of the passed stream array do not represent a valid stream. ; -3 : The stream position is 0 but the stream already contains data. ; -4 : The stream position is outside the stream boundaries. ; Author ........: Tim Krueger ; =============================================================================================================================== Func _BinaryStream_Read(ByRef $aStream, $iLength = Default) If Not IsArray($aStream) Then Return SetError(-1, Default, -1) If (UBound($aStream, 0) <> 1) Or (UBound($aStream, 1) > 2) Then Return SetError(-2, Default, -2) Local $iStreamLength = BinaryLen($aStream[0]) ; To avoid multiple BinaryLen() calls If ($aStream[1] < 0) Or ($aStream[1] > $iStreamLength) Then Return SetError(-3, Default, -3) ; Broken stream If $iLength = Default Then $iLength = 1 ; Read 1 byte by default ElseIf ($iLength <= 0) Or ($iLength > ($iStreamLength - $aStream[1])) Then Return SetError(-4, Default, -4) ; The length is 0 or exceeds the length of the stream EndIf Local $bReadData = BinaryMid($aStream[0], ($aStream[1] + 1), $iLength) $aStream[1] += $iLength ; This is the difference to peek Return $bReadData EndFunc ;==>_BinaryStream_Read ; #FUNCTION# ==================================================================================================================== ; Name ..........: _BinaryStream_Peek ; Description ...: Peeks a specified length of bytes from the given stream. The return value behaves like a read, but the stream ; position will not be incremented. ; Syntax ........: _BinaryStream_Peek(Byref $aStream[, $iLength = Default]) ; Parameters ....: $aStream - [in/out] The array that represents the stream which was created by _BinaryStream_Create(). ; $iLength - [optional] The count of bytes to peek from the stream. Default = 1. ; Return values .: On success: The n-bytes read from the current position. ; On error: ; -1 : The passed stream is not a valid array! ; -2 : The passed stream has invalid dimensions, it must be a 1 dimensional array with a length of 2! ; -3 : The current position of the stream is outside the stream data boundaries! ; Author ........: Tim Krueger ; =============================================================================================================================== Func _BinaryStream_Peek(ByRef $aStream, $iLength = Default) If Not IsArray($aStream) Then Return SetError(-1, Default, -1) If (UBound($aStream, 0) <> 1) Or (UBound($aStream, 1) > 2) Then Return SetError(-2, Default, -2) Local $iStreamLength = BinaryLen($aStream[0]) ; To avoid multiple BinaryLen() calls If ($aStream[1] < 0) Or ($aStream[1] > $iStreamLength) Then Return SetError(-3, Default, -3) ; Broken stream If $iLength = Default Then $iLength = 1 ; Peek 1 byte by default ElseIf ($iLength <= 0) Or ($iLength > ($iStreamLength - $aStream[1])) Then Return SetError(-4, Default, -4) ; The length is 0 or exceeds the length of the stream EndIf Return BinaryMid($aStream[0], ($aStream[1] + 1), $iLength) EndFunc ;==>_BinaryStream_Peek ; #FUNCTION# ==================================================================================================================== ; Name ..........: _BinaryStream_ToBinary ; Description ...: Returns the specified data as binary from the stream. This will not affect the stream position nor the length. ; Syntax ........: _BinaryStream_ToBinary(Byref $aStream[, $iPosition = Default[, $iLength = Default]]) ; Parameters ....: $aStream - [in/out] The array that represents the stream which was created by _BinaryStream_Create(). ; $iPosition - [optional] The start position of the data to return from the stream. (0 by default) ; $iLength - [optional] The length of data to return from the specified position of the stream. (The ; whole data by default) ; Return values .: On success: The data of the specified length from the given position of the stream. ; On error: ; -1 : The passed stream is not a valid array! ; -2 : The passed stream has invalid dimensions, it must be a 1 dimensional array with a length of 2! ; -3 : The specified position is outside the stream boundaries! ; -4 : The passed length of data is not available from the specified position in the stream! ; Author ........: Tim Krueger ; =============================================================================================================================== Func _BinaryStream_ToBinary(ByRef $aStream, $iPosition = Default, $iLength = Default) If Not IsArray($aStream) Then Return SetError(-1, Default, -1) If (UBound($aStream, 0) <> 1) Or (UBound($aStream, 1) > 2) Then Return SetError(-2, Default, -2) Local $iStreamLength = BinaryLen($aStream[0]) If $iPosition = Default Then $iPosition = 0 ElseIf ($iPosition < 0) Or ($iPosition > $iStreamLength) Then Return SetError(-3, Default, -3) ; The position is out of range EndIf If $iLength = Default Then ; Everything from the specified position until the end of the stream $iLength = $iStreamLength - $iPosition ElseIf ($iLength <= 0) Or ($iLength > ($iStreamLength - $iPosition)) Then Return SetError(-4, Default, -4) ; The length is 0 or exceeds the length of the stream EndIf Return BinaryMid($aStream[0], $iPosition, $iLength) EndFunc ;==>_BinaryStream_ToBinary