-
Notifications
You must be signed in to change notification settings - Fork 0
Helpers
An XDR object is a wrapper that manages a binary string buffer. There are several utility methods available for working with the underlying buffer directly.
To create a new XDR instance with an empty buffer, use the fresh()
method:
use StageRightLabs\PhpXdr\XDR;
$xdr = XDR::fresh()
To create a new XDR instane from an existing string of bytes:
use StageRightLabs\PhpXdr\XDR;
$xdr = XDR::fromBytes($buffer);
To create a new XDR instance from a base 16 string:
use StageRightLabs\PhpXdr\XDR;
$xdr = XDR::fromBase16('0000002a');
To create a new XDR intance from a base 64 string:
use StageRightLabs\PhpXdr\XDR;
$xdr = XDR::fromBase64('AAAAKg==');
To transmit the binary value you can either send it as is, or use a utility method to reveal the base 16 or base 64 representations of that binary string.
use StageRightLabs\PhpXdr\XDR;
$xdr = XDR::fresh()->write(42, XDR::INT);
// Hex / Base 16
$xdr->toBase16(); // '0000002a'
// Base 64
$xdr->toBase64(); // 'AAAAKg=='
// You can also access the underlying buffer directly:
$xdr->buffer(); // a string containing the bytes of the buffer.
As values are encoded the binary representation of those values is appended to the end of the buffer. To decode the buffer, an internal cursor is used to keep track of which bytes have been read and which still remain. The cursor always starts at index 0, and then moves forward each time you call a read()
method. Occasionally you may find it desirable to understand or manipulate the position of the cursor:
use StageRightLabs\PhpXdr\XDR;
$xdr = XDR::fresh()->write(42, XDR::INT); // cursor is now at position 0.
// Move the cursor forward 4 bytes
$xdr->advance(4);
// Move the cursor back 4 bytes
$xdr->rewind(4);
// Return to the beginning of the buffer
$xdr->rewind();
// Reveal the number of bytes remaining in the buffer after the current cursor position
$remaining = $xdr->remaining();
// Determine whether or not the buffer is empty
$isEmpty = $xdr->isEmpty();
Attempting to traverse beyond the end of the buffer will keep the cursor at the final location in the buffer. Likewise, attempting to move the cursor back below the beginning of the buffer will keep the cursor at position 0.