XdrBuffer
in package
Enables easy iteration through a blob of XDR data.
Provides an optional recursion-depth guard for XDR types that decode themselves recursively (e.g. XdrSorobanDelegateSignature). Call enterRecursion() at the start of each recursive call and leaveRecursion() on return. The guard throws InvalidArgumentException when the depth exceeds DECODE_DEPTH_LIMIT (500), preventing stack exhaustion from hostile deep-nesting in data received from the network.
The guard counts true nesting depth, not array width or sequential fields, so wide transactions and large maps are unaffected.
Table of Contents
Constants
- DECODE_DEPTH_LIMIT : mixed = 500
- Maximum active recursive type decodes on this buffer.
- RECURSION_LIMIT : mixed = 128
- Maximum nested Soroban delegate signatures.
Properties
Methods
- __construct() : mixed
- enterRecursion() : void
- Signals entry into one level of recursive XDR decoding.
- getRecursionDepth() : int
- Returns the current recursion depth.
- leaveRecursion() : void
- Signals exit from one level of recursive XDR decoding.
- readArrayLength() : int
- Reads the element count of a variable-length XDR array and validates it against the remaining bytes before the caller decodes any element.
- readBigInteger64() : BigInteger
- readBoolean() : bool
- readExtensionPoint() : void
- Reads a void-only extension point that the SDK stores as a plain integer field.
- readInteger32() : int
- readInteger64() : int
- readOpaqueFixed() : string
- readOpaqueFixedString() : string
- readOpaqueVariable() : string
- readOptionalPresence() : bool
- Reads the presence flag of an optional XDR value.
- readString() : string
- readUnsignedInteger256() : string
- readUnsignedInteger32() : int
- readUnsignedInteger64() : int
- assertBytesRemaining() : void
- roundTo4() : int
- rounds $number up to the nearest value that's a multiple of 4
Constants
DECODE_DEPTH_LIMIT
Maximum active recursive type decodes on this buffer.
public
mixed
DECODE_DEPTH_LIMIT
= 500
RECURSION_LIMIT
Maximum nested Soroban delegate signatures.
public
mixed
RECURSION_LIMIT
= 128
Properties
$position
protected
int
$position
$size
protected
int
$size
$xdrBytes
protected
string
$xdrBytes
Methods
__construct()
public
__construct(string $xdrBytes) : mixed
Parameters
- $xdrBytes : string
enterRecursion()
Signals entry into one level of recursive XDR decoding.
public
enterRecursion() : void
Checks the limit before incrementing. Pair every successful call with leaveRecursion(), including when decoding throws.
Tags
getRecursionDepth()
Returns the current recursion depth.
public
getRecursionDepth() : int
Return values
int —current depth (0 at top level)
leaveRecursion()
Signals exit from one level of recursive XDR decoding.
public
leaveRecursion() : void
Decrements the depth counter. Must be called once for every successful enterRecursion() call, including on exception paths (use try/finally).
readArrayLength()
Reads the element count of a variable-length XDR array and validates it against the remaining bytes before the caller decodes any element.
public
readArrayLength() : int
Every XDR array element occupies at least 4 bytes (scalars, enum and union discriminants, optional flags and length prefixes are 4 bytes; fixed opaque data is padded to 4), so a count above a quarter of the remaining bytes cannot be satisfied by the buffer. The count is read as a signed int32, so a value with the high bit set is negative on 32-bit and 64-bit PHP alike and is rejected as negative.
Tags
Return values
int —the element count, between 0 and a quarter of the remaining bytes (inclusive)
readBigInteger64()
public
readBigInteger64() : BigInteger
Return values
BigIntegerreadBoolean()
public
readBoolean() : bool
Return values
boolreadExtensionPoint()
Reads a void-only extension point that the SDK stores as a plain integer field.
public
readExtensionPoint(string $typeName) : void
The XDR union behind such a field has a single arm, case 0: void, so any other
discriminant is not decodable.
Parameters
- $typeName : string
-
name of the type that holds the extension point, used in the error message
Tags
readInteger32()
public
readInteger32() : int
Return values
intreadInteger64()
public
readInteger64() : int
Return values
intreadOpaqueFixed()
public
readOpaqueFixed(int $length) : string
Parameters
- $length : int
Return values
stringreadOpaqueFixedString()
public
readOpaqueFixedString(int $length) : string
Parameters
- $length : int
Return values
stringreadOpaqueVariable()
public
readOpaqueVariable([int|null $maxLength = null ]) : string
Parameters
- $maxLength : int|null = null
-
maximum accepted length in bytes, or null for no limit
Return values
stringreadOptionalPresence()
Reads the presence flag of an optional XDR value.
public
readOptionalPresence(string $fieldName) : bool
XDR encodes an optional value as a bool followed by the value when the bool is
- A bool is 0 or 1, so any other word is not decodable.
Parameters
- $fieldName : string
-
type and field name of the optional value, used in the error message
Tags
Return values
bool —true if the value follows, false if it is absent
readString()
public
readString([int|null $maxLength = null ]) : string
Parameters
- $maxLength : int|null = null
-
maximum accepted length in bytes, or null for no limit
Return values
stringreadUnsignedInteger256()
public
readUnsignedInteger256() : string
Return values
stringreadUnsignedInteger32()
public
readUnsignedInteger32() : int
Return values
intreadUnsignedInteger64()
public
readUnsignedInteger64() : int
Return values
intassertBytesRemaining()
protected
assertBytesRemaining(int $numBytes) : void
Parameters
- $numBytes : int
roundTo4()
rounds $number up to the nearest value that's a multiple of 4
protected
roundTo4(int $number) : int
Parameters
- $number : int