EoWriter
in package
A class for writing EO data to a sequence of bytes.
EoWriter enables serialization of different data types into a binary format, essential for networking applications or data storage.
Table of Contents
Properties
- $data : SplFixedArray<string|int, int>
- $stringSanitizationMode : bool
Methods
- __construct() : mixed
- Constructor initializes the internal data structure for storing bytes.
- addByte() : void
- Adds a single byte to the data array.
- addBytes() : void
- Adds an array of bytes to the data array.
- addChar() : void
- Adds an encoded character to the data array as a 1-byte integer.
- addEncodedString() : void
- Adds a string to the data array, encoded in ANSI format and then encoded with the EO encryption scheme.
- addFixedEncodedString() : void
- Adds a fixed-length string to the data array, optionally padded to length with `0xFF` and then encoded with the EO encryption scheme.
- addFixedString() : void
- Adds a fixed-length string to the data array, optionally padded to length with `0xFF`.
- addInt() : void
- Adds an encoded integer to the data array as a 4-byte integer.
- addShort() : void
- Adds an encoded short integer to the data array as a 2-byte integer.
- addString() : void
- Adds a string to the data array, encoded in ANSI format.
- addThree() : void
- Adds an encoded integer to the data array as a 3-byte integer.
- getLength() : int
- Returns the current length of the internal data array.
- getStringSanitizationMode() : bool
- Returns the current state of string sanitization mode.
- setStringSanitizationMode() : void
- Enables or disables string sanitization mode.
- toByteArray() : array<string|int, int>
- Converts the internal data array to a standard PHP array of bytes.
- toByteString() : string
- Converts the internal data array to a string of bytes.
- addBytesWithLength() : void
- Adds an array of bytes to the data array with a specified length.
- addPadding() : array<string|int, int>
- Adds padding to a byte array up to a specified length.
- checkNumberSize() : void
- Checks that a number is within the specified maximum value.
- checkStringLength() : void
- Checks the length of a string against a specified length for fixed-length strings.
- encodeAnsi() : array<string|int, int>
- Encodes a string from UTF-8 to ANSI.
- sanitizeString() : void
- Sanitizes a string by replacing `0xFF` bytes with `0x79` ('y').
Properties
$data
private
SplFixedArray<string|int, int>
$data
The internal data structure for storing bytes.
$stringSanitizationMode
private
bool
$stringSanitizationMode
= false
Indicates whether string sanitization is enabled.
Methods
__construct()
Constructor initializes the internal data structure for storing bytes.
public
__construct() : mixed
addByte()
Adds a single byte to the data array.
public
addByte(int $value) : void
Parameters
- $value : int
-
The byte value to add (must be between 0 and 255).
Tags
addBytes()
Adds an array of bytes to the data array.
public
addBytes(array<string|int, int> $bytes) : void
Parameters
- $bytes : array<string|int, int>
-
The array of bytes to add.
addChar()
Adds an encoded character to the data array as a 1-byte integer.
public
addChar(int|null $number) : void
Parameters
- $number : int|null
-
The character code to add (must be within EO_CHAR_MAX - 1).
Tags
addEncodedString()
Adds a string to the data array, encoded in ANSI format and then encoded with the EO encryption scheme.
public
addEncodedString(string $string) : void
Parameters
- $string : string
-
The string to encode and add.
addFixedEncodedString()
Adds a fixed-length string to the data array, optionally padded to length with `0xFF` and then encoded with the EO encryption scheme.
public
addFixedEncodedString(string $string, int $length[, bool $padded = false ]) : void
Parameters
- $string : string
-
The string to encode and add.
- $length : int
-
The fixed length of the string.
- $padded : bool = false
-
Whether to pad the string with
0xFF
to the fixed length.
Tags
addFixedString()
Adds a fixed-length string to the data array, optionally padded to length with `0xFF`.
public
addFixedString(string $string, int $length[, bool $padded = false ]) : void
Parameters
- $string : string
-
The string to encode and add.
- $length : int
-
The fixed length of the string.
- $padded : bool = false
-
Whether to pad the string with
0xFF
to the fixed length.
Tags
addInt()
Adds an encoded integer to the data array as a 4-byte integer.
public
addInt(int|null $number) : void
Parameters
- $number : int|null
-
The integer to add (must be within EO_INT_MAX - 1).
Tags
addShort()
Adds an encoded short integer to the data array as a 2-byte integer.
public
addShort(int|null $number) : void
Parameters
- $number : int|null
-
The short to add (must be within EO_SHORT_MAX - 1).
Tags
addString()
Adds a string to the data array, encoded in ANSI format.
public
addString(string $string) : void
Parameters
- $string : string
-
The string to encode and add.
addThree()
Adds an encoded integer to the data array as a 3-byte integer.
public
addThree(int|null $number) : void
Parameters
- $number : int|null
-
The integer to add (must be within EO_THREE_MAX - 1).
Tags
getLength()
Returns the current length of the internal data array.
public
getLength() : int
Return values
int —The number of bytes in the internal data array.
getStringSanitizationMode()
Returns the current state of string sanitization mode.
public
getStringSanitizationMode() : bool
Return values
bool —True if sanitization is enabled, otherwise false.
setStringSanitizationMode()
Enables or disables string sanitization mode.
public
setStringSanitizationMode(bool $mode) : void
Parameters
- $mode : bool
-
True to enable sanitization, false to disable.
toByteArray()
Converts the internal data array to a standard PHP array of bytes.
public
toByteArray() : array<string|int, int>
Return values
array<string|int, int> —An array of byte values.
toByteString()
Converts the internal data array to a string of bytes.
public
toByteString() : string
Return values
string —A string of bytes.
addBytesWithLength()
Adds an array of bytes to the data array with a specified length.
private
addBytesWithLength(array<string|int, int> $bytes, int $bytesLength) : void
Parameters
- $bytes : array<string|int, int>
-
The bytes to add.
- $bytesLength : int
-
The length of bytes to add.
addPadding()
Adds padding to a byte array up to a specified length.
private
static addPadding(array<string|int, int> $bytes, int $length) : array<string|int, int>
Parameters
- $bytes : array<string|int, int>
-
The byte array to pad.
- $length : int
-
The length to pad to.
Return values
array<string|int, int> —The padded byte array.
checkNumberSize()
Checks that a number is within the specified maximum value.
private
static checkNumberSize(int $number, int $maxValue) : void
Parameters
- $number : int
-
The number to check.
- $maxValue : int
-
The maximum value the number can have.
Tags
checkStringLength()
Checks the length of a string against a specified length for fixed-length strings.
private
static checkStringLength(string $string, int $length, bool $padded) : void
Parameters
- $string : string
-
The string whose length is to be checked.
- $length : int
-
The expected length of the string.
- $padded : bool
-
Whether the string should be padded.
Tags
encodeAnsi()
Encodes a string from UTF-8 to ANSI.
private
static encodeAnsi(string $string) : array<string|int, int>
Parameters
- $string : string
-
The UTF-8 string to encode.
Return values
array<string|int, int> —The ANSI-encoded string as an array of bytes.
sanitizeString()
Sanitizes a string by replacing `0xFF` bytes with `0x79` ('y').
private
sanitizeString(array<string|int, int> &$bytes) : void
Parameters
- $bytes : array<string|int, int>
-
The array of bytes representing the string to sanitize.