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 http://expath.org/modules/binary/.
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 [[expath-file]], which defines
functions in the namespace 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
xs:hexBinary() and
xs:base64Binary().
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 empty, i.e. contain no
data, (in the same way that items of type xs:string can contain no
characters.) Where 'data' arguments to functions that return binary data are optional
(i.e. $arg as type?) 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 [[qt3]] format, is defined at [[tests.binary]].
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.)
[[X690]] defines several formats for identifying and encoding arbitrary-sized telecommunications data as streams of octets. Many of these forms specify the length of data as part of their encoding. For example, in the Basic Encoding Rules, an integer is represented as the following series of octets:
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 [[expath-file]] provides three suitable functions:
file:append-binary($file as xs:string, $value as
xs:base64Binary) as empty-sequence(). Appends a
Base64 item as binary to a file.
file:read-binary($file as xs:string) as
xs:base64Binary. Returns the content of a file in its Base64
representation.
file:write-binary($file as xs:string, $value as
xs:base64Binary) as empty-sequence(). Writes a
Base64 item as binary to a file.
There may be an argument for a positioned file:read-binary($file as
xs:string,$offset as xs:integer), for access into
large files without total read.
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]).
bin:hex($in as xs:string?) as xs:base64Binary?
$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.
[=bin:non-numeric-character=] is raised if $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]).
bin:bin($in as xs:string?) as xs:base64Binary?
$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.
[=bin:non-numeric-character=] is raised if $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]).
bin:octal($in as xs:string?) as xs:base64Binary?
$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.
[=bin:non-numeric-character=] is raised if $in cannot be parsed as an
octal number.
Returns binary data as a sequence of octets.
bin:to-octets($in as xs:base64Binary) as xs:integer*
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.
bin:from-octets($in as xs:integer*) as xs:base64Binary
Octets are integers from 0 to 255.
If the value of $in is the empty sequence, the function returns zero-sized
binary data.
[=bin:octet-out-of-range=] is raised if one of the octets lies outside the range 0 - 255.
The bin:length function returns the size of binary data in octets.
bin:length($in as xs:base64Binary) as xs:integer
Returns the size of binary data in octets.
The bin:part function returns a specified part of binary data.
bin:part($in as xs:base64Binary?, $offset as xs:integer) as xs:base64Binary?
bin:part($in as xs:base64Binary?, $offset as xs:integer, $size as xs:integer) as xs:base64Binary?
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
MUST be non-negative integers.
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.
[=bin:negative-offset=] is raised if $offset is negative.
[=bin:negative-size=] is raised if $size is negative.
[=bin:offset-beyond-end=] is raised if $offset + $size extends
beyond the binary data of $in.
Note that fn:subsequence() and fn:substring()
[[xpath-functions-11-20091215]] both use 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.
bin:join($in as xs:base64Binary*) as xs:base64Binary
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.
bin:insert-before($in as xs:base64Binary?, $offset as xs:integer, $extra as xs:base64Binary?) as xs:base64Binary?
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
MUST be a non-negative integer.
It is a dynamic error if $offset 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.
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)).
[=bin:negative-offset=] is raised if $offset is negative.
[=bin:offset-beyond-end=] is raised if $offset extends beyond the
binary data of $in.
Note that under non-error conditions and when $offset gt 0 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.
bin:pad-left($in as xs:base64Binary?, $size as xs:integer) as xs:base64Binary?
bin:pad-left($in as xs:base64Binary?, $size as xs:integer, $octet as xs:integer) as xs:base64Binary?
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 blank.
$size
MUST be a non-negative integer.
If the value of $in is the empty sequence, the function returns an empty
sequence.
[=bin:negative-size=] is raised if $size is negative.
[=bin:octet-out-of-range=] is raised if $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.
bin:pad-right($in as xs:base64Binary?, $size as xs:integer) as xs:base64Binary?
bin:pad-right($in as xs:base64Binary?, $size as xs:integer, $octet as xs:integer) as xs:base64Binary?
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 blank.
$size
MUST be a non-negative integer.
If the value of $in is the empty sequence, the function returns an empty
sequence.
[=bin:negative-size=] is raised if $size is negative.
[=bin:octet-out-of-range=] is raised if $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.
bin:find($in as xs:base64Binary?, $offset as xs:integer, $search as xs:base64Binary) as xs:integer?
The function returns the first location of the binary search sequence in the input, or if not found, the empty sequence.
The search sequence cannot be 'empty'.
The value of $offset
MUST be a non-negative integer.
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.
[=bin:negative-offset=] is raised if $offset is negative.
[=bin:offset-beyond-end=] is raised if $offset extends beyond the
binary data of $input.
[=bin:empty-search-item=] is raised if $search is empty binary
data.
Finding all the matches can be accomplished with simple recursive application:
Decodes binary data as a string in a given encoding.
bin:decode-string($in as xs:base64Binary?, $encoding as xs:string) as xs:string?
bin:decode-string($in as xs:base64Binary?, $encoding as xs:string, $offset as xs:integer) as xs:string?
bin:decode-string($in as xs:base64Binary?, $encoding as xs:string, $offset as xs:integer, $size as xs:integer) as xs:string?
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 REQUIRED to
recognize are utf-8 and utf-16.
The values of $offset and $size
MUST be non-negative integers.
If the value of $in is the empty sequence, the function returns an empty
sequence.
$offset is zero based.
[=bin:negative-offset=] is raised if $offset is negative.
[=bin:negative-size=] is raised if $size is negative.
[=bin:offset-beyond-end=] is raised if $offset +
$size - 1 extends beyond the binary data of $in.
[=bin:unknown-encoding=] is raised if $encoding is invalid or not
supported by the implementation.
[=bin:decoding-error=] is raised if there is an error or malformed input during decoding the string. Additional information about the error may be passed though suitable error reporting mechanisms - this is implementation-dependant.
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.
bin:encode-string($in as xs:string?, $encoding as xs:string) as xs:base64Binary?
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 REQUIRED to
recognize are utf-8 and utf-16.
If the value of $in is the empty sequence, the function returns an empty
sequence.
[=bin:decoding-error=] is raised if $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
'most-significant-first'. The functions have an optional parameter
$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 [[IEEE-754-1985]]. Consequently they will occupy 4 or 8 octets when
packed.
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.
[[xmlschema11-2]] provides only one form of NaN which corresponds to a 'quiet'
NaN with zero payload of [[IEEE-754-1985]] with forms 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 double value.
bin:pack-double($in as xs:double) as xs:base64Binary
bin:pack-double($in as xs:double, $octet-order as xs:string) as xs:base64Binary
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 [[IEEE-754-1985]]. For more details see .
[=bin:unknown-significance-order=] is raised if the value $octet-order is
unrecognized.
Returns the 4-octet binary representation of a float value.
bin:pack-float($in as xs:float) as xs:base64Binary
bin:pack-float($in as xs:float, $octet-order as xs:string) as xs:base64Binary
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 [[IEEE-754-1985]]. For more details see .
[=bin:unknown-significance-order=] is raised if the value $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.
bin:pack-integer($in as xs:integer, $size as xs:integer) as xs:base64Binary
bin:pack-integer($in as xs:integer, $size as xs:integer, $octet-order as xs:string) as xs:base64Binary
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.
[=bin:unknown-significance-order=] is raised if the value $octet-order is
unrecognized.
[=bin:negative-size=] is raised if $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 double value stored at the particular offset in binary data.
bin:unpack-double($in as xs:base64Binary, $offset as xs:integer) as xs:double
bin:unpack-double($in as xs:base64Binary, $offset as xs:integer, $octet-order as xs:string) as xs:double
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
MUST be a non-negative integer.
The $offset is zero based.
The binary representation is expected to correspond with that of the IEEE double-precision 64-bit floating point type [[IEEE-754-1985]]. For more details see .
[=bin:negative-offset=] is raised if $offset is negative.
[=bin:offset-beyond-end=] is raised if $offset + 7
(octet-length of xs:double - 1) extends beyond the binary data of
$in.
[=bin:unknown-significance-order=] is raised if the value $octet-order is
unrecognized.
Extract float value stored at the particular offset in binary data.
bin:unpack-float($in as xs:base64Binary, $offset as xs:integer) as xs:float
bin:unpack-float($in as xs:base64Binary, $offset as xs:integer, $octet-order as xs:string) as xs:float
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
MUST be a non-negative integer.
The $offset is zero based.
The binary representation is expected to correspond with that of the IEEE single-precision 32-bit floating point type [[IEEE-754-1985]]. For more details see .
[=bin:negative-offset=] is raised if $offset is negative.
[=bin:offset-beyond-end=] is raised if $offset + 3
(octet-length of xs:float - 1) extends beyond the binary data of
$in.
[=bin:unknown-significance-order=] is raised if the value $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').
bin:unpack-integer($in as xs:base64Binary, $offset as xs:integer, $size as xs:integer) as xs:integer
bin:unpack-integer($in as xs:base64Binary, $offset as xs:integer, $size as xs:integer, $octet-order as xs:string) as xs:integer
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
MUST be non-negative integers.
$offset is zero based.
Specifying a $size of zero yields the integer 0.
[=bin:negative-offset=] is raised if $offset is negative.
[=bin:negative-size=] is raised if $size is negative.
[=bin:offset-beyond-end=] is raised if $offset +
$size - 1 extends beyond the binary data of $in.
[=bin:unknown-significance-order=] is raised if the value $octet-order is
unrecognized.
Returns an unsigned integer value represented by the $size octets starting
from $offset in the input binary representation.
bin:unpack-unsigned-integer($in as xs:base64Binary, $offset as xs:integer, $size as xs:integer) as xs:integer
bin:unpack-unsigned-integer($in as xs:base64Binary, $offset as xs:integer, $size as xs:integer, $octet-order as xs:string) as xs:integer
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
MUST be non-negative integers.
The $offset is zero based.
Specifying a $size of zero yields the integer 0.
[=bin:negative-offset=] is raised if $offset is negative.
[=bin:negative-size=] is raised if $size is negative.
[=bin:offset-beyond-end=] is raised if $offset +
$size - 1 extends beyond the binary data of $in.
[=bin:unknown-significance-order=] is raised if the value $octet-order is
unrecognized.
Returns the "bitwise or" of two binary arguments.
bin:or($a as xs:base64Binary?, $b as xs:base64Binary?) as xs:base64Binary?
Returns "bitwise or" applied between $a and $b.
If either argument is the empty sequence, an empty sequence is returned.
[=bin:differing-length-arguments=] is raised if the input arguments are of differing length.
Returns the "bitwise xor" of two binary arguments.
bin:xor($a as xs:base64Binary?, $b as xs:base64Binary?) as xs:base64Binary?
Returns "bitwise exclusive or" applied between $a and $b.
If either argument is the empty sequence, an empty sequence is returned.
[=bin:differing-length-arguments=] is raised if the input arguments are of differing length.
Returns the "bitwise and" of two binary arguments.
bin:and($a as xs:base64Binary?, $b as xs:base64Binary?) as xs:base64Binary?
Returns "bitwise and" applied between $a and $b.
If either argument is the empty sequence, an empty sequence is returned.
[=bin:differing-length-arguments=] is raised if the input arguments are of differing length.
Returns the "bitwise not" of a binary argument.
bin:not($in as xs:base64Binary?) as xs:base64Binary?
Returns "bitwise not" applied to $in.
If the argument is the empty sequence, an empty sequence is returned.
Shift bits in binary data.
bin:shift($in as xs:base64Binary?, $by as xs:integer) as xs:base64Binary?
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.
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.