-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbinary.xml
More file actions
475 lines (461 loc) · 23.2 KB
/
binary.xml
File metadata and controls
475 lines (461 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?xml version="1.0" encoding="UTF-8"?>
<spec role="editors-copy" xmlns:ex="http://expath.org/ns/xmlspec" ex:w3c="true">
<header>
<title>Binary Module 1.0</title>
<w3c-designation>w3c-designation</w3c-designation>
<w3c-doctype>EXPath Module</w3c-doctype>
<pubdate>
<day>3</day>
<month>December</month>
<year>2013</year>
</pubdate>
<publoc>
<loc href="http://expath.org/spec/binary/1.0"/>
</publoc>
<altlocs>
<loc href="http://expath.org/spec/binary/1,0.xml">XML</loc>
<loc href="http://expath.org/spec/binary/1.0/diff">Revision Markup</loc>
</altlocs>
<latestloc>
<loc href="http://expath.org/spec/binary"/>
</latestloc>
<prevlocs>
<loc href="http://expath.org/spec/binary/20131113"/>
<loc href="http://expath.org/spec/binary/20130920"/>
<loc href="http://expath.org/spec/binary/20130731"/>
<loc href="http://expath.org/spec/binary/20130312"/>
</prevlocs>
<authlist>
<author role="editor">
<name>Jirka Kosek</name>
<email href="mailto:jirka@kosek.cz">jirka@kosek.cz</email>
</author>
<author role="editor">
<name>John Lumley</name>
<email href="mailto:john@saxonica.com">john@saxonica.com</email>
</author>
</authlist>
<copyright>
<p>Copyright © 2013 Jirka Kosek and John Lumley, published by the <loc
href="http://w3.org/community/expath/">EXPath Community Group</loc> under the <loc
href="https://www.w3.org/community/about/agreements/final/">W3C Community Final
Specification Agreement (FSA)</loc>. A human-readable <loc
href="http://www.w3.org/community/about/agreements/fsa-deed/">summary</loc> is
available.</p>
<p>This specification was published by the <loc href="http://www.w3.org/community/expath/"
>EXPath Community Group</loc>. It is not a W3C Standard nor is it on the W3C Standards
Track. Please note that under the <loc
href="http://www.w3.org/community/about/agreements/final/">W3C Community Final Specification
Agreement (FSA)</loc> other conditions apply. Learn more
about <loc href="http://www.w3.org/community/">W3C Community and Business Groups</loc>.</p>
</copyright>
<abstract>
<p>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.</p>
<p>The module homepage, with more information, is on the EXPath website at <loc
href="http://expath.org/modules/binary/">http://expath.org/modules/binary/</loc>.</p>
</abstract>
<status>
<p/>
</status>
<langusage>
<language>en-US</language>
</langusage>
<revisiondesc>
<p>revisiondesc</p>
</revisiondesc>
</header>
<body>
<div1 id="status">
<head>Status of this document</head>
<p>This document is a final specification.</p>
</div1>
<div1 id="introduction">
<head>Introduction</head>
<div2 id="namespaces">
<head>Namespace conventions</head>
<p>The module defined by this document defines several functions, all contained in the
namespace <code>http://expath.org/ns/binary</code>. In this document, the <code>bin</code>
prefix, when used, is bound to this namespace URI.</p>
<p>Error codes are defined in the same namespace (<code>http://expath.org/ns/binary</code>),
and in this document are displayed with the same prefix, <code>bin</code>.</p>
<p>Binary file I/O uses facilities defined in <bibref ref="expathfile"/>, which defines
functions in the namespace <code>http://expath.org/ns/file</code>. In this document, the
<code>file</code> prefix, when used, is bound to this namespace URI.</p>
</div2>
<div2 id="error.management">
<head>Error management</head>
<p>Error conditions are identified by a code (a <code>QName</code>.) 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 <code>error()</code> had
been called.)</p>
</div2>
<div2 id="type">
<head>Binary type</head>
<p>The principal binary type within this module is <code>xs:base64Binary</code>.</p>
<p>Conversion to and from <code>xs:hexBinary</code> can be performed by casting with
<ex:function>xs:hexBinary()</ex:function> and
<ex:function>xs:base64Binary()</ex:function>.</p>
<note>
<p>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.</p>
</note>
<p>An item of type <code>xs:base64Binary</code> can be <emph>empty</emph>, i.e. contain no
data, (in the same way that items of type <code>xs:string</code> can contain no
characters.) Where 'data' arguments to functions that return binary data are optional
(i.e. <code><emph>$arg as type</emph>?</code>) 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 <code>xs:base64Binary</code>. </p>
</div2>
<div2 id="testing">
<head>Test suite</head>
<p>A suite of test-cases for all the functions defined in this module, in <bibref ref="qt3"
/> format, is defined at <bibref ref="tests.binary"/>.</p>
</div2>
</div1>
<div1 id="use-cases">
<head>Use cases</head>
<p>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:</p>
<ulist>
<item>
<p>Getting the dimensions of an image file.</p>
</item>
<item>
<p>Extracting image metadata.</p>
</item>
<item>
<p>Processing images embedded as base64 encodings within a SOAP message.</p>
</item>
<item>
<p>Processing legacy text files which use different encodings in separate sections.</p>
</item>
<item>
<p>Generating PDF files from SVG graphical data.</p>
</item>
</ulist>
<div2 id="example–JPEG">
<head>Example – finding JPEG size</head>
<p>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:</p>
<eg xml:space="preserve"><![CDATA[
<xsl:variable name="binary" select="file:read-binary(@href)" as="xs:base64Binary"/>
<xsl:variable name="location" select="bin:find($binary,0,bin:hex('FFC0'))"/>
<size width="{bin:unpack-unsigned-integer($binary,$location+5,2,'most-significant-first')}"
height="{bin:unpack-unsigned-integer($binary,$location+7,2,'most-significant-first')}"/>
=> <size width="377" height="327"/>]]></eg>
<p>(The <code>'most-significant-first'()</code> argument ensures the numeric conversion is
'big-endian', which is the format in JPEG.)</p>
</div2>
<div2 id="example-ASN1">
<head>Example – reading and writing variable length ASN.1 integers</head>
<p><bibref ref="asn1"/> 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:</p>
<ulist>
<item>
<p>Type – 1 octet – in this case the value <code>0x02</code></p>
</item>
<item>
<p>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.)</p>
</item>
<item>
<p>Payload – >=0 octets – the octets of the integer value in most-significant-first
order.</p>
</item>
</ulist>
<p>To generate such a representation for an integer from XSLT/XPath, the following code
might be used:</p>
<eg xml:space="preserve"><![CDATA[
<xsl:function name="bin:int-octets" as="xs:integer*">
<xsl:param name="value" as="xs:integer"/>
<xsl:sequence
select="if($value ne 0) then (bin:int-octets($value idiv 256),$value mod 256) else ()"/>
</xsl:function>
<xsl:function name="bin:encode-ASN-integer" as="xs:base64Binary">
<xsl:param name="int" as="xs:integer"/>
<xsl:variable name="octets" select="bin:int-octets($int)"/>
<xsl:variable name="length-octets"
select="let $l := count($octets) return
(if($l le 127) then $l
else (let $lo := bin:int-octets($l) return (128+count($lo),$lo)))"/>
<xsl:sequence select="bin:from-octets((2,$length-octets,$octets))"/>
</xsl:function>]]></eg>
<p>The function <code>bin:int-octets()</code> 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: </p>
<eg xml:space="preserve"><![CDATA[
bin:encode-ASN-integer(0) => "AgA="
bin:encode-ASN-integer(1234) => "AgIE0g=="
bin:encode-ASN-integer(123456789123456789123456789123456789) => "Ag8XxuPAMviQRa10ZoQEXxU="
bin:encode-ASN-integer(123456789.. 900 digits... 123456789) => "AoIBdgaTo....EBF8V"]]></eg>
<p>The first example requires no octets to encode zero, hence its octets are
<code>2,0</code>. 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:
<code>2,130,1</code> indicating that the data is represented by (130-128) * 256 + 1 =
513 octets and the length required two octets to encode.</p>
<p>Decoding is a matter of compound use of the integer decoding function:</p>
<eg xml:space="preserve"><![CDATA[
<xsl:function name="bin:decode-ASN-integer" as="xs:integer">
<xsl:param name="in" as="xs:base64Binary"/>
<xsl:sequence
select="let $lo := bin:unpack-unsigned-integer($in,1,1,'BE') return (
if($lo le 127) then bin:unpack-unsigned-integer($in,2,$lo,'BE')
else (let $lo2 := $lo - 128, $lo3 := bin:unpack-unsigned-integer($in,2,$lo2,'BE') return
bin:unpack-unsigned-integer($in,2+$lo2,$lo3,'BE')))"
/>
</xsl:function> ]]></eg>
<p>(all numbers in ASN are 'big-endian') and the examples from above reverse:</p>
<eg xml:space="preserve"><![CDATA[
bin:decode-ASN-integer(xs:base64Binary("AgA=")) => 0
bin:decode-ASN-integer(xs:base64Binary("AgIE0g==")) => 1234
bin:encode-ASN-integer(xs:base64Binary("Ag8XxuPAMviQRa10ZoQEXxU="))
=> 123456789123456789123456789123456789
bin:encode-ASN-integer(xs:base64Binary("AoIBdgaTo....EBF8V"))
=> 123456789.. 900 digits... 123456789 ]]></eg>
</div2>
</div1>
<div1 id="loading">
<head>Loading and saving binary data</head>
<p>This module defines no specific functions for reading and writing binary data from files.
The EXPath File Module <bibref ref="expathfile"/> provides three suitable functions:</p>
<ulist>
<item>
<p>
<ex:function><a href="http://expath.org/spec/file#d3e541"
>file:append-binary</a></ex:function>($file as <ex:type>xs:string</ex:type>, $value as
<ex:type>xs:base64Binary</ex:type>) as <ex:type>empty-sequence()</ex:type>. Appends a
Base64 item as binary to a file.</p>
</item>
<item>
<p>
<ex:function><a href="http://expath.org/spec/file#d3e954"
>file:read-binary</a></ex:function>($file as <ex:type>xs:string</ex:type>) as
<ex:type>xs:base64Binary</ex:type>. Returns the content of a file in its Base64
representation. A function signature with an offset and size is available to read part
of a file.</p>
</item>
<item>
<p>
<ex:function><a href="http://expath.org/spec/file#d3e1359"
>file:write-binary</a></ex:function>($file as <ex:type>xs:string</ex:type>, $value as
<ex:type>xs:base64Binary</ex:type>) as <ex:type>empty-sequence()</ex:type>. Writes a
Base64 item as binary to a file. A function signature with an offset is available to write part
of a file. </p>
</item>
</ulist>
<!-- <p>There may be an argument for a positioned file:read-binary($file as
<ex:type>xs:string</ex:type>,$offset as <ex:type>xs:integer</ex:type>), for
access into large files without total read.</p>-->
</div1>
<div1 id="constants">
<head>Defining 'constants' and conversions</head>
<p>Users of the package may need to define binary 'constants' within their code or examine the
basic octets. The following functions support these:</p>
<div2 id="hex">
<head><?function bin:hex?></head>
</div2>
<div2 id="binary">
<head><?function bin:bin?></head>
</div2>
<div2 id="octal">
<head><?function bin:octal?></head>
</div2>
<div2 id="to-octets">
<head><?function bin:to-octets?></head>
</div2>
<div2 id="from-octets">
<head><?function bin:from-octets?></head>
</div2>
</div1>
<div1 id="basic-operations">
<head>Basic operations</head>
<div2 id="length">
<head><?function bin:length?></head>
</div2>
<div2 id="part">
<head><?function bin:part?></head>
</div2>
<div2 id="join">
<head><?function bin:join?></head>
</div2>
<div2 id="insert-before">
<head><?function bin:insert-before?></head>
</div2>
<div2 id="pad-left">
<head><?function bin:pad-left?></head>
</div2>
<div2 id="pad-right">
<head><?function bin:pad-right?></head>
</div2>
<div2 id="index-of">
<head><?function bin:find?></head>
</div2>
</div1>
<div1 id="text-encoding">
<head>Text decoding and encoding</head>
<div2 id="decode-string">
<head><?function bin:decode-string?></head>
</div2>
<div2 id="encode-string">
<head><?function bin:encode-string?></head>
</div2>
</div1>
<div1 id="numeric-packing">
<head>Packing and unpacking of encoded numeric values</head>
<div2 id="endianness">
<head>Number 'endianness'</head>
<p>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
<emph>'most-significant-first'</emph>. The functions have an optional parameter
<code>$octet-order</code> whose string value controls the order. Least-significant-first
order is indicated by any of the values <code>least-significant-first</code>,
<code>little-endian</code> or <code>LE</code>. Most-significant-first order is indicated
by any of the values <code>most-significant-first</code>, <code>big-endian</code> or
<code>BE</code>.</p>
</div2>
<div2 id="integer">
<head>Integer representation</head>
<p/>
<p>Integers within binary data are represented, or assumed to be represented, as an integral
number of octets. Integers where <code>$length</code> is greater than 8 octets (and thus
not representable as a <code>long</code>) might be expected in some situations, e.g.
encryption. Whether the range of integers is limited to <code>±2^63</code> may be
implementation-dependant.</p>
</div2>
<div2 id="floating">
<head>Representation of floating point numbers</head>
<p>Care should be taken with the packing and unpacking of floating point numbers
(<code>xs:float</code> and <code>xs:double</code>). The binary representations are
expected to correspond with those of the IEEE single/double-precision 32/64-bit floating
point types <bibref ref="ieee754"/>. Consequently they will occupy 4 or 8 octets when
packed.</p>
<p>Positive and negative infinities are supported. <code>INF</code> maps to <code>0x7f80
0000</code> (float), <code>0x7ff0 0000 0000 0000</code> (double). <code>-INF</code> maps
to <code>0xff80 0000</code> (float), <code>0xfff0 0000 0000 0000</code> (double).</p>
<p>Negative zero (<code>0x8000 0000 0000 0000</code> double, <code>0x8000 0000</code> float)
encountered during unpacking will yield negative zero forms (e.g.
<code>-xs:double(0.0)</code>) and negative zeros will be written as a result of
packing.</p>
<p><bibref ref="xmlschema1.1"/> provides only one form of NaN which corresponds to a 'quiet'
NaN with zero payload of <bibref ref="ieee754"/> with forms <code>0x7fc0 0000</code>
(float), <code>0x7ff8 0000 0000 0000</code> (double). These are the bit forms that will be
packed. 'Signalling' NaN values (<code>0x7f80 0001</code> -> <code>0x7fbf ffff</code>
or <code>0xff80 0001</code> -> <code>0xffbf ffff</code>, <code>0x7ff0 0000 0000
0001</code> -> <code>0x7ff7 ffff ffff ffff</code> or <code>0xfff0 0000 0000
0001</code> -> <code>0xfff7 ffff ffff ffff</code>) encountered during unpacking will
be replaced by 'quiet' NaN. Any low-order payload in a unpacked quiet NaN is also zeroed.
</p>
</div2>
<div2 id="pack-double">
<head><?function bin:pack-double?></head>
</div2>
<div2 id="pack-float">
<head><?function bin:pack-float?></head>
</div2>
<div2 id="pack-integer">
<head><?function bin:pack-integer?></head>
</div2>
<div2 id="unpack-double">
<head><?function bin:unpack-double?></head>
</div2>
<div2 id="unpack-float">
<head><?function bin:unpack-float?></head>
</div2>
<div2 id="unpack-integer">
<head><?function bin:unpack-integer?></head>
</div2>
<div2 id="unpack-unsigned-integer">
<head><?function bin:unpack-unsigned-integer?></head>
</div2>
</div1>
<div1 id="bitwise">
<head>Bitwise operations</head>
<div2 id="or">
<head><?function bin:or?></head>
</div2>
<div2 id="xor">
<head><?function bin:xor?></head>
</div2>
<div2 id="and">
<head><?function bin:and?></head>
</div2>
<div2 id="not">
<head><?function bin:not?></head>
</div2>
<div2 id="shift">
<head><?function bin:shift?></head>
</div2>
</div1>
</body>
<back>
<div1 id="references">
<head>References</head>
<blist>
<bibl id="asn1" key="ASN.1"> OSI networking and system aspects – Abstract Syntax Notation
One (ASN.1) – see <loc
href="http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf">ASN.1
encoding rules: Specification of Basic Encoding Rules (BER), Canonical Encoding Rules
(CER) and Distinguished Encoding Rules (DER) </loc>. ITU-T X.690 (07/2002) </bibl>
<bibl id="expathfile" key="EXPath File">
<loc href="http://expath.org/spec/file">File Module</loc>. Christian Grün and Matthias
Brantner, editors. EXPath Candidate Module. 14 June 2012.</bibl>
<bibl id="fo30" key="F&O 3.0">
<loc href="http://www.w3.org/TR/xpath-functions-30/">XPath and XQuery Functions and
Operators 3.0</loc>. Michael Kay, editor. W3C Candidate Recommendation 21 May
2013.</bibl>
<bibl id="ieee754" key="IEEE 754-1985">IEEE Standard for Binary Floating-Point Arithmetic.
See <loc
href="http://standards.ieee.org/reading/ieee/std_public/description/busarch/754-1985_desc.html"
>http://standards.ieee.org/reading/ieee/std_public/description/busarch/754-1985_desc.html</loc></bibl>
<bibl id="qt3" key="QT3">
<loc href="http://dev.w3.org/2011/QT3-test-suite/">XML Query Test Suite</loc>. W3C 21 June
2013. </bibl>
<bibl id="tests.binary" key="Test-suite">The test suite for this module, using QT3 format,
is in the EXPath repository <loc
href="http://github.com/expath/expath-cg/tree/master/tests/qt3/binary"
>http://github.com/expath/expath-cg</loc> in the directory tests/qt3/binary/</bibl>
<bibl id="xmlschema1.1" key="XML Schema 1.1 Part 2">
<loc href="http://www.w3.org/TR/xmlschema11-2/">W3C XML Schema Definition Language (XSD)
1.1 Part 2: Datatypes</loc>. David Peterson et al, editors. W3C Recommendation 5 April
2012.</bibl>
</blist>
</div1>
<div1 id="errors">
<head>Summary of error conditions</head>
<blist>
<bibl id="error.differentLengthArguments" key="bin:differing-length-arguments">The arguments
to a bitwise operation are of differing length.</bibl>
<bibl id="error.indexOutOfRange" key="bin:index-out-of-range">Attempting to retrieve data
outside the meaningful range of a binary data type.</bibl>
<!--<bibl id="error.indexBeforeStart" key="bin:index-before-start">Attempting to
retrieve data before the start of a binary data type.</bibl>
<bibl id="error.indexAfterEnd" key="bin:index-after-end">Attempting to retrieve data
beyond the end of a binary data type.</bibl>-->
<bibl id="error.sizeNegative" key="bin:negative-size">Size of binary portion, required
numeric size or padding is negative.</bibl>
<!-- <bibl id="error.emptySearch" key="bin:empty-search-item">Binary search item is
empty.</bibl>-->
<bibl id="error.largeOctet" key="bin:octet-out-of-range">Attempting to pack binary value
with octet outside range.</bibl>
<bibl id="error.nonNumberChar" key="bin:non-numeric-character">Wrong character in binary
'numeric constructor' string.</bibl>
<bibl id="error.unknownEncoding" key="bin:unknown-encoding">The specified encoding is not
supported.</bibl>
<bibl id="error.encoding" key="bin:conversion-error">Error in converting to/from a
string.</bibl>
<bibl id="error.endianness" key="bin:unknown-significance-order">Unknown octet-order
value.</bibl>
</blist>
</div1>
</back>
</spec>