Skip to content

Fixed Length Opaque

Ryan Durham edited this page Nov 27, 2021 · 2 revisions

An 'opaque' encoding allows you to pass bytes directly into an XDR buffer without having to interpret them. A fixed length opaque allows you to allocate a known number of bytes for the value.

Internally, a fixed length opaque value will always be allocated a length of bytes that is divisible by four, as per the spec. If you specify a length that is not divisible by four the extra padding will be applied invisibly behind the scenes.

To encode a fixed length opaque value:

use StageRightLabs\PhpXdr\XDR;

// Encode
$xdr = XDR::fresh()->write(hex2bin('12345678'), XDR::OPAQUE_FIXED, 4);
// Note that we are specifying that we want to use 4 bytes to store this value. 

To decode a fixed length opaque value:

use StageRightLabs\PhpXdr\XDR;
$xdr = XDR::fresh()->write(hex2bin('12345678'), XDR::OPAQUE_FIXED, 4);

// Decode
$opaque = $xdr->read(XDR::OPAQUE_FIXED, length: 4);
$opaque == hex2bin('12345678'); // true
Clone this wiki locally