-
Notifications
You must be signed in to change notification settings - Fork 0
Home
XDR, or External Data Representation, is a system for representing data structures as bytes for transmission between systems. The RFC 4506 spec defines a handful of data types that can be encoded and decoded as XDR. These data types are very flexible and can be used to compose larger complex data structures that still adhere to the spec.
XDR is convenient because it can transmit a large amout of information in a relatively small collection of bytes. However, the downside is that the sender and the receiver must agree the structure of interpretation ahead of time.
This package provides tools for reading and writing XDR data types in PHP. It offers a set of interfaces that can be used to allow your custom classes to be treated as XDR data types.
Data Types
- Integer
- Unsigned Integer
- Enumeration
- Boolean
- Hyper Integer / Hyper Unsigned Integer
- Floating Point
- Double Precision Floating Point
- Fixed Length Opaque Data
- Variable Length Opaque Data
- String
- Fixed Length Array
- Variable Length Array
- Structure
- Discriminated Union
- Void
- Optional Data
The only data type in the spec that has not been implemented is Quadruple Precision Floating Point. However, if you would like to use this type in your project you can still do that with careful use of the tools provided.
A typedef is not a data type itself, but rather a means for creating custom data types from existing primitives. This is a common practice in strongly typed languages. For our purposes it is also a very easy way to create custom data types that can be interpreted as XDR.
When reading or writing certain data types some validation will be performed. If that validation fails an exception will be thrown, either an InvalidArgumentException
or an UnexpectedValueException
. It may be beneficial to wrap your XDR calls in a try/catch block, depending on what you are doing.
The nature of the PHP language is such that there are some inherent limitations in place when handling large numbers. Internally, PHP stores all integers as signed, and the largest signed integer available to you will be dependent on the platform you are running on. To get around this you can either handle the raw binary values directly or you can make use of a package like brick/math that does this for you and provides a methodology for performing numerical operations that exceed the limitations of integers in PHP.
From the PHP manual:
PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit.
Due to limitations of the language it is not possible for this package to determine if you are attempting to encode an integer value that exceeds the applicable bounds. By default PHP will automatically convert the number into a float. If you attempt to cast it to an int, the value will wrap around: PHP_INT_MAX
+ 1 = PHP_INT_MIN
.
This package provides a reference implementation of a Brick/Math BigInteger
class that can be interpreted as XDR.