Copyright © 2013 Jirka Kosek and John Lumley, published by the
This specification was published by the
This proposal provides an API for XPath 2.0 to handle binary data. It defines extension functions to process data from binary files, including extracting subparts, searching, basic binary operations and conversion between binary and structured forms. It has been designed to be compatible with XQuery 1.0 and XSLT 2.0, as well as any other XPath 2.0 usage.
The module homepage, with more information, is on the EXPath website at
revisiondesc
This document is a final specification.
The module defined by this document defines several functions, all contained in the
namespace http://expath.org/ns/binary
. In this document, the bin
prefix, when used, is bound to this namespace URI.
Error codes are defined in the same namespace (http://expath.org/ns/binary
),
and in this document are displayed with the same prefix, bin
.
Binary file I/O uses facilities defined in http://expath.org/ns/file
. In this document, the
file
prefix, when used, is bound to this namespace URI.
Error conditions are identified by a code (a QName
.) When such an error
condition is reached in the evaluation of an expression, a dynamic error is thrown, with
the corresponding error code (as if the standard XPath function error()
had
been called.)
The principal binary type within this module is xs:base64Binary
.
Conversion to and from xs:hexBinary
can be performed by casting with
As these types are normally implemented as wrappers around byte array structures containing the data, and differ only when being serialized to or parsed from text, such casting in-process should not involve data copying.
An item of type xs:base64Binary
can be xs:string
can contain no
characters.) Where 'data' arguments to functions that return binary data are optional
(i.e.
) and any of those optional arguments is set
to the empty sequence, in general an empty sequence is returned, rather than an empty item
of type xs:base64Binary
.
A suite of test-cases for all the functions defined in this module, in
Development of this specification was driven by requirements which some XML developers regularly encounter in examining or generating data which is presented in binary, or other non-textual forms. Some typical use cases include:
Getting the dimensions of an image file.
Extracting image metadata.
Processing images embedded as base64 encodings within a SOAP message.
Processing legacy text files which use different encodings in separate sections.
Generating PDF files from SVG graphical data.
As an example, the following code reads the binary form of a JPEG image file, searches for the 'Start of Frame/DCT' segment, and unpacks the relevant binary sections to integers of height and width:
(The 'most-significant-first'()
argument ensures the numeric conversion is
'big-endian', which is the format in JPEG.)
Type – 1 octet – in this case the value 0x02
Length – >=1 octet – the number of octets in the integer value. The length field itself can be variable in length – to accomodate VERY large integers (requiring more than 127 octets to represent, e.g. 2048-bit crypto keys.)
Payload – >=0 octets – the octets of the integer value in most-significant-first order.
To generate such a representation for an integer from XSLT/XPath, the following code might be used:
The function bin:int-octets()
returns a sequence of all the 'significant'
octets of the integer (i.e. eliminating leading 'zeroes') in most-significant order.
Examples of the encoding are:
The first example requires no octets to encode zero, hence its octets are
2,0
. Both the second and third examples can be represented in less than 128
octets (2 and 15 respectively), so length is encoded as a single octet. The first three
octets of the result for the last example, which encodes a 900-digit integer, are:
2,130,1
indicating that the data is represented by (130-128) * 256 + 1 =
513 octets and the length required two octets to encode.
Decoding is a matter of compound use of the integer decoding function:
(all numbers in ASN are 'big-endian') and the examples from above reverse:
This module defines no specific functions for reading and writing binary data from files.
The EXPath File Module
Users of the package may need to define binary 'constants' within their code or examine the basic octets. The following functions support these:
Returns the binary form of the set of octets written as a sequence of (ASCII) hex digits ([0-9A-Fa-f]).
$in
will be effectively zero-padded from the left to generate an integral
number of octets, i.e. an even number of hexadecimal digits. If $in
is an empty
string, then the result will be a xs:base64Binary
with no embedded data.
Byte order in the result follows (per-octet) character order in the string.
If the value of $in
is the empty sequence, the function returns an empty
sequence.
$in
cannot be parsed as a
hexadecimal number.
When the input string has an even number of characters, this function behaves similarly to
the double cast xs:base64Binary(xs:hexBinary($string))
.
Returns the binary form of the set of octets written as a sequence of (8-wise) (ASCII) binary digits ([01]).
$in
will be effectively zero-padded from the left to generate an integral
number of octets. If $in
is an empty string, then the result will be a
xs:base64Binary
with no embedded data.
Byte order in the result follows (per-octet) character order in the string.
If the value of $in
is the empty sequence, the function returns an empty
sequence.
$in
cannot be parsed as a
binary number.
Returns the binary form of the set of octets written as a sequence of (ASCII) octal digits ([0-7]).
$in
will be effectively zero-padded from the left to generate an integral
number of octets. If $in
is an empty string, then the result will be a
xs:base64Binary
with no embedded data.
Byte order in the result follows (per-octet) character order in the string.
If the value of $in
is the empty sequence, the function returns an empty
sequence.
$in
cannot be parsed as an
octal number.
Returns binary data as a sequence of octets.
If $in
is a zero length binary data then the empty sequence is returned.
Octets are returned as integers from 0 to 255.
Converts a sequence of octets into binary data.
Octets are integers from 0 to 255.
If the value of $in
is the empty sequence, the function returns zero-sized
binary data.
The bin:length
function returns the size of binary data in octets.
Returns the size of binary data in octets.
The bin:part
function returns a specified part of binary data.
Returns a section of binary data starting at the $offset
octet. If
$size
is defined, the size of the returned binary data is $size
octets. If $size
is absent, all remaining data from $offset
is
returned.
The $offset
is zero based.
The values of $offset
and $size
It is a dynamic error if $offset
+ $size
is larger than the size
of the binary data in $in
.
If the value of $in
is the empty sequence, the function returns an empty
sequence.
$offset
is negative or
$offset + $size
is larger than the size of the binary data of
$in
.
$size
is negative.
Note that fn:subsequence()
and fn:substring()
xs:double
for offset and size – this is a legacy
from XPath 1.0.
Testing whether $data
variable starts with binary content consistent with a
PDF file:
25504446
is the magic number for PDF files: it is the US-ASCII encoded
hexadecimal value for %PDF
.
Returns the binary data created by concatenating the binary data items in a sequence.
The function returns an xs:base64Binary
created by concatenating the items in
the sequence $in
, in order.
If the value of $in
is the empty sequence, the function returns a binary item
containing no data bytes.
The bin:insert-before
function inserts additional binary data at a given point
in other binary data.
Returns binary data consisting sequentially of the data from $in
upto and
including the $offset - 1
octet, followed by all the data from
$extra
, and then the remaining data from $in
.
The $offset
is zero based.
The value of $offset
If the value of $in
is the empty sequence, the function returns an empty
sequence.
If the value of $extra
is the empty sequence, the function returns
$in
.
If $offset eq 0
the result is the binary concatenation of $extra
and $in
, i.e. equivalent to bin:join(($extra,$in))
.
$offset
is negative or
$offset
is larger than the size of the binary data of $in
.
Note that when $offset gt 0 and $offset lt bin:size($in)
the function is
equivalent to:
Returns the binary data created by padding $in
with $size
octets
from the left. The padding octet values are $octet
or zero if omitted.
The function returns an xs:base64Binary
created by padding the input with
$size
octets in front of the input. If $octet
is
specified, the padding octets each have that value, otherwise they are initialized to 0.
$size
If the value of $in
is the empty sequence, the function returns an empty
sequence.
$size
is negative.
$octet
lies outside the range 0
– 255.
Padding with a non-zero octet value can also be accomplished by the XPath expressions:
Returns the binary data created by padding $in
with $size
blank
octets from the right. The padding octet values are $octet
or zero if
omitted.
The function returns an xs:base64Binary
created by padding the input with
$size
blank octets after the input. If $octet
is
specified, the padding octets each have that value, otherwise they are initialized to 0.
$size
If the value of $in
is the empty sequence, the function returns an empty
sequence.
$size
is negative.
$octet
lies outside the range 0
– 255.
Padding with a non-zero octet value can also be accomplished by the XPath expressions:
Returns the first location in $in
of $search
, starting at the
$offset
octet.
The function returns the first location of the binary search sequence in the input, or if not found, the empty sequence.
If $search
is empty $offset
is returned.
The value of $offset
The $offset
is zero based.
The returned location is zero based.
If the value of $in
is the empty sequence, the function returns an empty
sequence.
$offset
is negative or
$offset
is larger than the size of the binary data of $in
.
Finding all the matches can be accomplished with simple recursive application:
Decodes binary data as a string in a given encoding.
If $offset
and $size
are provided, the $size
octets
from $offset
are decoded. If $offset
alone is provided, octets
from $offset
to the end are decoded, otherwise the entire octet sequence is
used.
The $encoding
argument is the name of an encoding. The values for this
attribute follow the same rules as for the encoding
attribute in an XML
declaration. The only values which every implementation is utf-8
and utf-16
.
If $encoding
is ommitted, utf-8
encoding is assumed.
The values of $offset
and $size
If the value of $in
is the empty sequence, the function returns an empty
sequence.
$offset
is zero based.
$offset
is negative or
$offset + $size
is larger than the size of the binary data of
$in
.
$size
is negative.
$encoding
is invalid or not
supported by the implementation.
Testing whether $data
variable starts with binary content consistent with a
PDF file:
The first four characters of a PDF file are '%PDF'
.
Encodes a string into binary data using a given encoding.
The $encoding
argument is the name of an encoding. The values for this
attribute follow the same rules as for the encoding
attribute in an XML
declaration. The only values which every implementation is utf-8
and utf-16
.
If $encoding
is ommitted, utf-8
encoding is assumed.
If the value of $in
is the empty sequence, the function returns an empty
sequence.
$encoding
is invalid or not
supported by the implementation.
Packing and unpacking numeric values can be performed in 'most-significant-first'
('big-endian') or 'least-significant-first' ('little-endian') octet order. The default is
$octet-order
whose string value controls the order. Least-significant-first
order is indicated by any of the values least-significant-first
,
little-endian
or LE
. Most-significant-first order is indicated
by any of the values most-significant-first
, big-endian
or
BE
.
Integers within binary data are represented, or assumed to be represented, as an integral
number of octets. Integers where $length
is greater than 8 octets (and thus
not representable as a long
) might be expected in some situations, e.g.
encryption. Whether the range of integers is limited to ±2^63
may be
implementation-dependant.
Care should be taken with the packing and unpacking of floating point numbers
(xs:float
and xs:double
). The binary representations are
expected to correspond with those of the IEEE single/double-precision 32/64-bit floating
point types
Positive and negative infinities are supported. INF
maps to 0x7f80
0000
(float), 0x7ff0 0000 0000 0000
(double). -INF
maps
to 0xff80 0000
(float), 0xfff0 0000 0000 0000
(double).
Negative zero (0x8000 0000 0000 0000
double, 0x8000 0000
float)
encountered during unpacking will yield negative zero forms (e.g.
-xs:double(0.0)
) and negative zeros will be written as a result of
packing.
0x7fc0 0000
(float), 0x7ff8 0000 0000 0000
(double). These are the bit forms that will be
packed. 'Signalling' NaN values (0x7f80 0001
-> 0x7fbf ffff
or 0xff80 0001
-> 0xffbf ffff
, 0x7ff0 0000 0000
0001
-> 0x7ff7 ffff ffff ffff
or 0xfff0 0000 0000
0001
-> 0xfff7 ffff ffff ffff
) encountered during unpacking will
be replaced by 'quiet' NaN. Any low-order payload in a unpacked quiet NaN is also zeroed.
Returns the 8-octet binary representation of a
Most-significant-octet-first number representation is assumed unless the
$octet-order
parameter is specified. Acceptable values for
$octet-order
are described in
The binary representation will correspond with that of the IEEE double-precision 64-bit
floating point type
$octet-order
is
unrecognized.
Returns the 4-octet binary representation of a
Most-significant-octet-first number representation is assumed unless the
$octet-order
parameter is specified. Acceptable values for
$octet-order
are described in
The binary representation will correspond with that of the IEEE single-precision 32-bit
floating point type
$octet-order
is
unrecognized.
Returns the twos-complement binary representation of an integer value treated as
$size
octets long. Any 'excess' high-order bits are discarded.
Most-significant-octet-first number representation is assumed unless the
$octet-order
parameter is specified. Acceptable values for
$octet-order
are described in
Specifying a $size
of zero yields an empty binary data.
$octet-order
is
unrecognized.
$size
is negative.
If the integer being packed has a maximum precision of $size
octets, then
signed/unsigned versions are not necessary. If the data is considered unsigned, then the
most significant bit of the bottom $size
octets has a normal positive
(2^(8 *$size - 1)
) meaning. If it is considered to be a signed value, then
the MSB and all the higher order, discarded bits will be '1' for a negative value and '0'
for a positive or zero. If this function were to check the 'sizing' of the supplied integer
against the packing size, then any values of MSB and the discarded higher order bits other
than 'all 1' or 'all 0' would constitute an error. This function does not perfom such
checking.
Extract
Extract the $offset
octet of the binary data of
$in
.
Most-significant-octet-first number representation is assumed unless the
$octet-order
parameter is specified. Acceptable values for
$octet-order
are described in
The value of $offset
The $offset
is zero based.
The binary representation is expected to correspond with that of the IEEE double-precision
64-bit floating point type
$offset
is negative or
$offset + 8
(octet-length of $in
.
$octet-order
is
unrecognized.
Extract
Extract the $offset
octet of the binary data of
$in
.
Most-significant-octet-first number representation is assumed unless the
$octet-order
parameter is specified. Acceptable values for
$octet-order
are described in
The value of $offset
The $offset
is zero based.
The binary representation is expected to correspond with that of the IEEE single-precision
32-bit floating point type
$offset
is negative or
$offset + 4
(octet-length of $in
.
$octet-order
is
unrecognized.
Returns a signed integer value represented by the $size
octets starting from
$offset
in the input binary representation. Necessary sign extension is
performed (i.e. the result is negative if the high order bit is '1').
Most-significant-octet-first number representation is assumed unless the
$octet-order
parameter is specified. Acceptable values for
$octet-order
are described in
The values of $offset
and $size
$offset
is zero based.
Specifying a $size
of zero yields the integer 0
.
$offset
is negative or
$offset + $size
is larger than the size of the binary data of
$in
.
$size
is negative.
$octet-order
is
unrecognized.
For discussion on integer range see
Returns an unsigned integer value represented by the $size
octets starting
from $offset
in the input binary representation.
Most-significant-octet-first number representation is assumed unless the
$octet-order
parameter is specified. Acceptable values for
$octet-order
are described in
The values of $offset
and $size
The $offset
is zero based.
Specifying a $size
of zero yields the integer 0
.
$offset
is negative or
$offset + $size
is larger than the size of the binary data of
$in
.
$size
is negative.
$octet-order
is
unrecognized.
For discussion on integer range see
Returns the "bitwise or" of two binary arguments.
Returns "bitwise or" applied between $a
and $b
.
If either argument is the empty sequence, an empty sequence is returned.
Returns the "bitwise xor" of two binary arguments.
Returns "bitwise exclusive or" applied between $a
and $b
.
If either argument is the empty sequence, an empty sequence is returned.
Returns the "bitwise and" of two binary arguments.
Returns "bitwise and" applied between $a
and $b
.
If either argument is the empty sequence, an empty sequence is returned.
Returns the "bitwise not" of a binary argument.
Returns "bitwise not" applied to $in
.
If the argument is the empty sequence, an empty sequence is returned.
Shift bits in binary data.
If $by
is positive then bits are shifted $by
times to the
left.
If $by
is negative then bits are shifted -$by
times to the
right.
If $by
is zero, the result is identical to $in
.
If |$by|
is greater than the bit-length of $in
then an all-zeros
result, of the same length as $in
, is returned.
|$by|
can be greater than 8, implying multi-byte shifts.
The result always has the same size as $in
.
The shifting is logical: zeros are placed into discarded bits.
If the value of $in
is the empty sequence, the function returns an empty
sequence.
Bit shifting across byte boundaries implies 'big-endian' treatment, i.e. the leftmost (high-order) bit when shifted left becomes the low-order bit of the preceding byte.