Skip to content

Helpers

Ryan Durham edited this page Nov 26, 2021 · 8 revisions

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.

Instantiation

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 instance from a base 16 string:

use StageRightLabs\PhpXdr\XDR;

$xdr = XDR::fromBase16('0000002a');

String Representation

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.

Buffer Traversal

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);

// 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();
Clone this wiki locally