ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.pm
Revision: 1.58
Committed: Tue Apr 26 16:07:04 2016 UTC (8 years ago) by root
Branch: MAIN
Changes since 1.57: +49 -21 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 CBOR::XS - Concise Binary Object Representation (CBOR, RFC7049)
4
5 =encoding utf-8
6
7 =head1 SYNOPSIS
8
9 use CBOR::XS;
10
11 $binary_cbor_data = encode_cbor $perl_value;
12 $perl_value = decode_cbor $binary_cbor_data;
13
14 # OO-interface
15
16 $coder = CBOR::XS->new;
17 $binary_cbor_data = $coder->encode ($perl_value);
18 $perl_value = $coder->decode ($binary_cbor_data);
19
20 # prefix decoding
21
22 my $many_cbor_strings = ...;
23 while (length $many_cbor_strings) {
24 my ($data, $length) = $cbor->decode_prefix ($many_cbor_strings);
25 # data was decoded
26 substr $many_cbor_strings, 0, $length, ""; # remove decoded cbor string
27 }
28
29 =head1 DESCRIPTION
30
31 This module converts Perl data structures to the Concise Binary Object
32 Representation (CBOR) and vice versa. CBOR is a fast binary serialisation
33 format that aims to use an (almost) superset of the JSON data model, i.e.
34 when you can represent something useful in JSON, you should be able to
35 represent it in CBOR.
36
37 In short, CBOR is a faster and quite compact binary alternative to JSON,
38 with the added ability of supporting serialisation of Perl objects. (JSON
39 often compresses better than CBOR though, so if you plan to compress the
40 data later and speed is less important you might want to compare both
41 formats first).
42
43 To give you a general idea about speed, with texts in the megabyte range,
44 C<CBOR::XS> usually encodes roughly twice as fast as L<Storable> or
45 L<JSON::XS> and decodes about 15%-30% faster than those. The shorter the
46 data, the worse L<Storable> performs in comparison.
47
48 Regarding compactness, C<CBOR::XS>-encoded data structures are usually
49 about 20% smaller than the same data encoded as (compact) JSON or
50 L<Storable>.
51
52 In addition to the core CBOR data format, this module implements a
53 number of extensions, to support cyclic and shared data structures
54 (see C<allow_sharing> and C<allow_cycles>), string deduplication (see
55 C<pack_strings>) and scalar references (always enabled).
56
57 The primary goal of this module is to be I<correct> and the secondary goal
58 is to be I<fast>. To reach the latter goal it was written in C.
59
60 See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and
61 vice versa.
62
63 =cut
64
65 package CBOR::XS;
66
67 use common::sense;
68
69 our $VERSION = 1.5;
70 our @ISA = qw(Exporter);
71
72 our @EXPORT = qw(encode_cbor decode_cbor);
73
74 use Exporter;
75 use XSLoader;
76
77 use Types::Serialiser;
78
79 our $MAGIC = "\xd9\xd9\xf7";
80
81 =head1 FUNCTIONAL INTERFACE
82
83 The following convenience methods are provided by this module. They are
84 exported by default:
85
86 =over 4
87
88 =item $cbor_data = encode_cbor $perl_scalar
89
90 Converts the given Perl data structure to CBOR representation. Croaks on
91 error.
92
93 =item $perl_scalar = decode_cbor $cbor_data
94
95 The opposite of C<encode_cbor>: expects a valid CBOR string to parse,
96 returning the resulting perl scalar. Croaks on error.
97
98 =back
99
100
101 =head1 OBJECT-ORIENTED INTERFACE
102
103 The object oriented interface lets you configure your own encoding or
104 decoding style, within the limits of supported formats.
105
106 =over 4
107
108 =item $cbor = new CBOR::XS
109
110 Creates a new CBOR::XS object that can be used to de/encode CBOR
111 strings. All boolean flags described below are by default I<disabled>.
112
113 The mutators for flags all return the CBOR object again and thus calls can
114 be chained:
115
116 my $cbor = CBOR::XS->new->encode ({a => [1,2]});
117
118 =item $cbor = $cbor->max_depth ([$maximum_nesting_depth])
119
120 =item $max_depth = $cbor->get_max_depth
121
122 Sets the maximum nesting level (default C<512>) accepted while encoding
123 or decoding. If a higher nesting level is detected in CBOR data or a Perl
124 data structure, then the encoder and decoder will stop and croak at that
125 point.
126
127 Nesting level is defined by number of hash- or arrayrefs that the encoder
128 needs to traverse to reach a given point or the number of C<{> or C<[>
129 characters without their matching closing parenthesis crossed to reach a
130 given character in a string.
131
132 Setting the maximum depth to one disallows any nesting, so that ensures
133 that the object is only a single hash/object or array.
134
135 If no argument is given, the highest possible setting will be used, which
136 is rarely useful.
137
138 Note that nesting is implemented by recursion in C. The default value has
139 been chosen to be as large as typical operating systems allow without
140 crashing.
141
142 See SECURITY CONSIDERATIONS, below, for more info on why this is useful.
143
144 =item $cbor = $cbor->max_size ([$maximum_string_size])
145
146 =item $max_size = $cbor->get_max_size
147
148 Set the maximum length a CBOR string may have (in bytes) where decoding
149 is being attempted. The default is C<0>, meaning no limit. When C<decode>
150 is called on a string that is longer then this many bytes, it will not
151 attempt to decode the string but throw an exception. This setting has no
152 effect on C<encode> (yet).
153
154 If no argument is given, the limit check will be deactivated (same as when
155 C<0> is specified).
156
157 See SECURITY CONSIDERATIONS, below, for more info on why this is useful.
158
159 =item $cbor = $cbor->allow_unknown ([$enable])
160
161 =item $enabled = $cbor->get_allow_unknown
162
163 If C<$enable> is true (or missing), then C<encode> will I<not> throw an
164 exception when it encounters values it cannot represent in CBOR (for
165 example, filehandles) but instead will encode a CBOR C<error> value.
166
167 If C<$enable> is false (the default), then C<encode> will throw an
168 exception when it encounters anything it cannot encode as CBOR.
169
170 This option does not affect C<decode> in any way, and it is recommended to
171 leave it off unless you know your communications partner.
172
173 =item $cbor = $cbor->allow_sharing ([$enable])
174
175 =item $enabled = $cbor->get_allow_sharing
176
177 If C<$enable> is true (or missing), then C<encode> will not double-encode
178 values that have been referenced before (e.g. when the same object, such
179 as an array, is referenced multiple times), but instead will emit a
180 reference to the earlier value.
181
182 This means that such values will only be encoded once, and will not result
183 in a deep cloning of the value on decode, in decoders supporting the value
184 sharing extension. This also makes it possible to encode cyclic data
185 structures (which need C<allow_cycles> to ne enabled to be decoded by this
186 module).
187
188 It is recommended to leave it off unless you know your
189 communication partner supports the value sharing extensions to CBOR
190 (L<http://cbor.schmorp.de/value-sharing>), as without decoder support, the
191 resulting data structure might be unusable.
192
193 Detecting shared values incurs a runtime overhead when values are encoded
194 that have a reference counter large than one, and might unnecessarily
195 increase the encoded size, as potentially shared values are encode as
196 shareable whether or not they are actually shared.
197
198 At the moment, only targets of references can be shared (e.g. scalars,
199 arrays or hashes pointed to by a reference). Weirder constructs, such as
200 an array with multiple "copies" of the I<same> string, which are hard but
201 not impossible to create in Perl, are not supported (this is the same as
202 with L<Storable>).
203
204 If C<$enable> is false (the default), then C<encode> will encode shared
205 data structures repeatedly, unsharing them in the process. Cyclic data
206 structures cannot be encoded in this mode.
207
208 This option does not affect C<decode> in any way - shared values and
209 references will always be decoded properly if present.
210
211 =item $cbor = $cbor->allow_cycles ([$enable])
212
213 =item $enabled = $cbor->get_allow_cycles
214
215 If C<$enable> is true (or missing), then C<decode> will happily decode
216 self-referential (cyclic) data structures. By default these will not be
217 decoded, as they need manual cleanup to avoid memory leaks, so code that
218 isn't prepared for this will not leak memory.
219
220 If C<$enable> is false (the default), then C<decode> will throw an error
221 when it encounters a self-referential/cyclic data structure.
222
223 FUTURE DIRECTION: the motivation behind this option is to avoid I<real>
224 cycles - future versions of this module might chose to decode cyclic data
225 structures using weak references when this option is off, instead of
226 throwing an error.
227
228 This option does not affect C<encode> in any way - shared values and
229 references will always be encoded properly if present.
230
231 =item $cbor = $cbor->pack_strings ([$enable])
232
233 =item $enabled = $cbor->get_pack_strings
234
235 If C<$enable> is true (or missing), then C<encode> will try not to encode
236 the same string twice, but will instead encode a reference to the string
237 instead. Depending on your data format, this can save a lot of space, but
238 also results in a very large runtime overhead (expect encoding times to be
239 2-4 times as high as without).
240
241 It is recommended to leave it off unless you know your
242 communications partner supports the stringref extension to CBOR
243 (L<http://cbor.schmorp.de/stringref>), as without decoder support, the
244 resulting data structure might not be usable.
245
246 If C<$enable> is false (the default), then C<encode> will encode strings
247 the standard CBOR way.
248
249 This option does not affect C<decode> in any way - string references will
250 always be decoded properly if present.
251
252 =item $cbor = $cbor->text_keys ([$enable])
253
254 =item $enabled = $cbor->get_text_keys
255
256 If C<$enabled> is true (or missing), then C<encode> will encode all
257 perl hash keys as CBOR text strings/UTF-8 string, upgrading them as needed.
258
259 If C<$enable> is false (the default), then C<encode> will encode hash keys
260 normally - upgraded perl strings (strings internally encoded as UTF-8) as
261 CBOR text strings, and downgraded perl strings as CBOR byte strings.
262
263 This option does not affect C<decode> in any way.
264
265 This option is useful for interoperability with CBOR decoders that don't
266 treat byte strings as a form of text. It is especially useful as Perl
267 gives very little control over hash keys.
268
269 Enabling this option can be slow, as all downgraded hash keys that are
270 encoded need to be scanned and converted to UTF-8.
271
272 =item $cbor = $cbor->text_strings ([$enable])
273
274 =item $enabled = $cbor->get_text_strings
275
276 This option works similar to C<text_keys>, above, but works on all strings
277 (including hash keys), so C<text_keys> has no further effect after
278 enabling C<text_strings>.
279
280 If C<$enabled> is true (or missing), then C<encode> will encode all perl
281 strings as CBOR text strings/UTF-8 strings, upgrading them as needed.
282
283 If C<$enable> is false (the default), then C<encode> will encode strings
284 normally (but see C<text_keys>) - upgraded perl strings (strings
285 internally encoded as UTF-8) as CBOR text strings, and downgraded perl
286 strings as CBOR byte strings.
287
288 This option does not affect C<decode> in any way.
289
290 This option has similar advantages and disadvantages as C<text_keys>. In
291 addition, this option effectively removes the ability to encode byte
292 strings, which might break some C<FREEZE> and C<TO_CBOR> methods that rely
293 on this, such as bignum encoding, so this option is mainly useful for very
294 simple data.
295
296 =item $cbor = $cbor->validate_utf8 ([$enable])
297
298 =item $enabled = $cbor->get_validate_utf8
299
300 If C<$enable> is true (or missing), then C<decode> will validate that
301 elements (text strings) containing UTF-8 data in fact contain valid UTF-8
302 data (instead of blindly accepting it). This validation obviously takes
303 extra time during decoding.
304
305 The concept of "valid UTF-8" used is perl's concept, which is a superset
306 of the official UTF-8.
307
308 If C<$enable> is false (the default), then C<decode> will blindly accept
309 UTF-8 data, marking them as valid UTF-8 in the resulting data structure
310 regardless of whether that's true or not.
311
312 Perl isn't too happy about corrupted UTF-8 in strings, but should
313 generally not crash or do similarly evil things. Extensions might be not
314 so forgiving, so it's recommended to turn on this setting if you receive
315 untrusted CBOR.
316
317 This option does not affect C<encode> in any way - strings that are
318 supposedly valid UTF-8 will simply be dumped into the resulting CBOR
319 string without checking whether that is, in fact, true or not.
320
321 =item $cbor = $cbor->filter ([$cb->($tag, $value)])
322
323 =item $cb_or_undef = $cbor->get_filter
324
325 Sets or replaces the tagged value decoding filter (when C<$cb> is
326 specified) or clears the filter (if no argument or C<undef> is provided).
327
328 The filter callback is called only during decoding, when a non-enforced
329 tagged value has been decoded (see L<TAG HANDLING AND EXTENSIONS> for a
330 list of enforced tags). For specific tags, it's often better to provide a
331 default converter using the C<%CBOR::XS::FILTER> hash (see below).
332
333 The first argument is the numerical tag, the second is the (decoded) value
334 that has been tagged.
335
336 The filter function should return either exactly one value, which will
337 replace the tagged value in the decoded data structure, or no values,
338 which will result in default handling, which currently means the decoder
339 creates a C<CBOR::XS::Tagged> object to hold the tag and the value.
340
341 When the filter is cleared (the default state), the default filter
342 function, C<CBOR::XS::default_filter>, is used. This function simply looks
343 up the tag in the C<%CBOR::XS::FILTER> hash. If an entry exists it must be
344 a code reference that is called with tag and value, and is responsible for
345 decoding the value. If no entry exists, it returns no values.
346
347 Example: decode all tags not handled internally into C<CBOR::XS::Tagged>
348 objects, with no other special handling (useful when working with
349 potentially "unsafe" CBOR data).
350
351 CBOR::XS->new->filter (sub { })->decode ($cbor_data);
352
353 Example: provide a global filter for tag 1347375694, converting the value
354 into some string form.
355
356 $CBOR::XS::FILTER{1347375694} = sub {
357 my ($tag, $value);
358
359 "tag 1347375694 value $value"
360 };
361
362 =item $cbor_data = $cbor->encode ($perl_scalar)
363
364 Converts the given Perl data structure (a scalar value) to its CBOR
365 representation.
366
367 =item $perl_scalar = $cbor->decode ($cbor_data)
368
369 The opposite of C<encode>: expects CBOR data and tries to parse it,
370 returning the resulting simple scalar or reference. Croaks on error.
371
372 =item ($perl_scalar, $octets) = $cbor->decode_prefix ($cbor_data)
373
374 This works like the C<decode> method, but instead of raising an exception
375 when there is trailing garbage after the CBOR string, it will silently
376 stop parsing there and return the number of characters consumed so far.
377
378 This is useful if your CBOR texts are not delimited by an outer protocol
379 and you need to know where the first CBOR string ends amd the next one
380 starts.
381
382 CBOR::XS->new->decode_prefix ("......")
383 => ("...", 3)
384
385 =back
386
387 =head2 INCREMENTAL PARSING
388
389 In some cases, there is the need for incremental parsing of JSON
390 texts. While this module always has to keep both CBOR text and resulting
391 Perl data structure in memory at one time, it does allow you to parse a
392 CBOR stream incrementally, using a similar to using "decode_prefix" to see
393 if a full CBOR object is available, but is much more efficient.
394
395 It basically works by parsing as much of a CBOR string as possible - if
396 the CBOR data is not complete yet, the pasrer will remember where it was,
397 to be able to restart when more data has been accumulated. Once enough
398 data is available to either decode a complete CBOR value or raise an
399 error, a real decode will be attempted.
400
401 A typical use case would be a network protocol that consists of sending
402 and receiving CBOR-encoded messages. The solution that works with CBOR and
403 about anything else is by prepending a length to every CBOR value, so the
404 receiver knows how many octets to read. More compact (and slightly slower)
405 would be to just send CBOR values back-to-back, as C<CBOR::XS> knows where
406 a CBOR value ends, and doesn't need an explicit length.
407
408 The following methods help with this:
409
410 =over 4
411
412 =item @decoded = $cbor->incr_parse ($buffer)
413
414 This method attempts to decode exactly one CBOR value from the beginning
415 of the given C<$buffer>. The value is removed from the C<$buffer> on
416 success. When C<$buffer> doesn't contain a complete value yet, it returns
417 nothing. Finally, when the C<$buffer> doesn't start with something
418 that could ever be a valid CBOR value, it raises an exception, just as
419 C<decode> would. In the latter case the decoder state is undefined and
420 must be reset before being able to parse further.
421
422 This method modifies the C<$buffer> in place. When no CBOR value can be
423 decoded, the decoder stores the current string offset. On the next call,
424 continues decoding at the place where it stopped before. For this to make
425 sense, the C<$buffer> must begin with the same octets as on previous
426 unsuccessful calls.
427
428 You can call this method in scalar context, in which case it either
429 returns a decoded value or C<undef>. This makes it impossible to
430 distinguish between CBOR null values (which decode to C<undef>) and an
431 unsuccessful decode, which is often acceptable.
432
433 =item @decoded = $cbor->incr_parse_multiple ($buffer)
434
435 Same as C<incr_parse>, but attempts to decode as many CBOR values as
436 possible in one go, instead of at most one. Calls to C<incr_parse> and
437 C<incr_parse_multiple> can be interleaved.
438
439 =item $cbor->incr_reset
440
441 Resets the incremental decoder. This throws away any saved state, so that
442 subsequent calls to C<incr_parse> or C<incr_parse_multiple> start to parse
443 a new CBOR value from the beginning of the C<$buffer> again.
444
445 This method can be caled at any time, but it I<must> be called if you want
446 to change your C<$buffer> or there was a decoding error and you want to
447 reuse the C<$cbor> object for future incremental parsings.
448
449 =back
450
451
452 =head1 MAPPING
453
454 This section describes how CBOR::XS maps Perl values to CBOR values and
455 vice versa. These mappings are designed to "do the right thing" in most
456 circumstances automatically, preserving round-tripping characteristics
457 (what you put in comes out as something equivalent).
458
459 For the more enlightened: note that in the following descriptions,
460 lowercase I<perl> refers to the Perl interpreter, while uppercase I<Perl>
461 refers to the abstract Perl language itself.
462
463
464 =head2 CBOR -> PERL
465
466 =over 4
467
468 =item integers
469
470 CBOR integers become (numeric) perl scalars. On perls without 64 bit
471 support, 64 bit integers will be truncated or otherwise corrupted.
472
473 =item byte strings
474
475 Byte strings will become octet strings in Perl (the Byte values 0..255
476 will simply become characters of the same value in Perl).
477
478 =item UTF-8 strings
479
480 UTF-8 strings in CBOR will be decoded, i.e. the UTF-8 octets will be
481 decoded into proper Unicode code points. At the moment, the validity of
482 the UTF-8 octets will not be validated - corrupt input will result in
483 corrupted Perl strings.
484
485 =item arrays, maps
486
487 CBOR arrays and CBOR maps will be converted into references to a Perl
488 array or hash, respectively. The keys of the map will be stringified
489 during this process.
490
491 =item null
492
493 CBOR null becomes C<undef> in Perl.
494
495 =item true, false, undefined
496
497 These CBOR values become C<Types:Serialiser::true>,
498 C<Types:Serialiser::false> and C<Types::Serialiser::error>,
499 respectively. They are overloaded to act almost exactly like the numbers
500 C<1> and C<0> (for true and false) or to throw an exception on access (for
501 error). See the L<Types::Serialiser> manpage for details.
502
503 =item tagged values
504
505 Tagged items consists of a numeric tag and another CBOR value.
506
507 See L<TAG HANDLING AND EXTENSIONS> and the description of C<< ->filter >>
508 for details on which tags are handled how.
509
510 =item anything else
511
512 Anything else (e.g. unsupported simple values) will raise a decoding
513 error.
514
515 =back
516
517
518 =head2 PERL -> CBOR
519
520 The mapping from Perl to CBOR is slightly more difficult, as Perl is a
521 typeless language. That means this module can only guess which CBOR type
522 is meant by a perl value.
523
524 =over 4
525
526 =item hash references
527
528 Perl hash references become CBOR maps. As there is no inherent ordering in
529 hash keys (or CBOR maps), they will usually be encoded in a pseudo-random
530 order. This order can be different each time a hash is encoded.
531
532 Currently, tied hashes will use the indefinite-length format, while normal
533 hashes will use the fixed-length format.
534
535 =item array references
536
537 Perl array references become fixed-length CBOR arrays.
538
539 =item other references
540
541 Other unblessed references will be represented using
542 the indirection tag extension (tag value C<22098>,
543 L<http://cbor.schmorp.de/indirection>). CBOR decoders are guaranteed
544 to be able to decode these values somehow, by either "doing the right
545 thing", decoding into a generic tagged object, simply ignoring the tag, or
546 something else.
547
548 =item CBOR::XS::Tagged objects
549
550 Objects of this type must be arrays consisting of a single C<[tag, value]>
551 pair. The (numerical) tag will be encoded as a CBOR tag, the value will
552 be encoded as appropriate for the value. You must use C<CBOR::XS::tag> to
553 create such objects.
554
555 =item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error
556
557 These special values become CBOR true, CBOR false and CBOR undefined
558 values, respectively. You can also use C<\1>, C<\0> and C<\undef> directly
559 if you want.
560
561 =item other blessed objects
562
563 Other blessed objects are serialised via C<TO_CBOR> or C<FREEZE>. See
564 L<TAG HANDLING AND EXTENSIONS> for specific classes handled by this
565 module, and L<OBJECT SERIALISATION> for generic object serialisation.
566
567 =item simple scalars
568
569 Simple Perl scalars (any scalar that is not a reference) are the most
570 difficult objects to encode: CBOR::XS will encode undefined scalars as
571 CBOR null values, scalars that have last been used in a string context
572 before encoding as CBOR strings, and anything else as number value:
573
574 # dump as number
575 encode_cbor [2] # yields [2]
576 encode_cbor [-3.0e17] # yields [-3e+17]
577 my $value = 5; encode_cbor [$value] # yields [5]
578
579 # used as string, so dump as string (either byte or text)
580 print $value;
581 encode_cbor [$value] # yields ["5"]
582
583 # undef becomes null
584 encode_cbor [undef] # yields [null]
585
586 You can force the type to be a CBOR string by stringifying it:
587
588 my $x = 3.1; # some variable containing a number
589 "$x"; # stringified
590 $x .= ""; # another, more awkward way to stringify
591 print $x; # perl does it for you, too, quite often
592
593 You can force whether a string is encoded as byte or text string by using
594 C<utf8::upgrade> and C<utf8::downgrade> (if C<text_strings> is disabled):
595
596 utf8::upgrade $x; # encode $x as text string
597 utf8::downgrade $x; # encode $x as byte string
598
599 Perl doesn't define what operations up- and downgrade strings, so if the
600 difference between byte and text is important, you should up- or downgrade
601 your string as late as possible before encoding. You can also force the
602 use of CBOR text strings by using C<text_keys> or C<text_strings>.
603
604 You can force the type to be a CBOR number by numifying it:
605
606 my $x = "3"; # some variable containing a string
607 $x += 0; # numify it, ensuring it will be dumped as a number
608 $x *= 1; # same thing, the choice is yours.
609
610 You can not currently force the type in other, less obscure, ways. Tell me
611 if you need this capability (but don't forget to explain why it's needed
612 :).
613
614 Perl values that seem to be integers generally use the shortest possible
615 representation. Floating-point values will use either the IEEE single
616 format if possible without loss of precision, otherwise the IEEE double
617 format will be used. Perls that use formats other than IEEE double to
618 represent numerical values are supported, but might suffer loss of
619 precision.
620
621 =back
622
623 =head2 OBJECT SERIALISATION
624
625 This module implements both a CBOR-specific and the generic
626 L<Types::Serialier> object serialisation protocol. The following
627 subsections explain both methods.
628
629 =head3 ENCODING
630
631 This module knows two way to serialise a Perl object: The CBOR-specific
632 way, and the generic way.
633
634 Whenever the encoder encounters a Perl object that it cannot serialise
635 directly (most of them), it will first look up the C<TO_CBOR> method on
636 it.
637
638 If it has a C<TO_CBOR> method, it will call it with the object as only
639 argument, and expects exactly one return value, which it will then
640 substitute and encode it in the place of the object.
641
642 Otherwise, it will look up the C<FREEZE> method. If it exists, it will
643 call it with the object as first argument, and the constant string C<CBOR>
644 as the second argument, to distinguish it from other serialisers.
645
646 The C<FREEZE> method can return any number of values (i.e. zero or
647 more). These will be encoded as CBOR perl object, together with the
648 classname.
649
650 These methods I<MUST NOT> change the data structure that is being
651 serialised. Failure to comply to this can result in memory corruption -
652 and worse.
653
654 If an object supports neither C<TO_CBOR> nor C<FREEZE>, encoding will fail
655 with an error.
656
657 =head3 DECODING
658
659 Objects encoded via C<TO_CBOR> cannot (normally) be automatically decoded,
660 but objects encoded via C<FREEZE> can be decoded using the following
661 protocol:
662
663 When an encoded CBOR perl object is encountered by the decoder, it will
664 look up the C<THAW> method, by using the stored classname, and will fail
665 if the method cannot be found.
666
667 After the lookup it will call the C<THAW> method with the stored classname
668 as first argument, the constant string C<CBOR> as second argument, and all
669 values returned by C<FREEZE> as remaining arguments.
670
671 =head3 EXAMPLES
672
673 Here is an example C<TO_CBOR> method:
674
675 sub My::Object::TO_CBOR {
676 my ($obj) = @_;
677
678 ["this is a serialised My::Object object", $obj->{id}]
679 }
680
681 When a C<My::Object> is encoded to CBOR, it will instead encode a simple
682 array with two members: a string, and the "object id". Decoding this CBOR
683 string will yield a normal perl array reference in place of the object.
684
685 A more useful and practical example would be a serialisation method for
686 the URI module. CBOR has a custom tag value for URIs, namely 32:
687
688 sub URI::TO_CBOR {
689 my ($self) = @_;
690 my $uri = "$self"; # stringify uri
691 utf8::upgrade $uri; # make sure it will be encoded as UTF-8 string
692 CBOR::XS::tag 32, "$_[0]"
693 }
694
695 This will encode URIs as a UTF-8 string with tag 32, which indicates an
696 URI.
697
698 Decoding such an URI will not (currently) give you an URI object, but
699 instead a CBOR::XS::Tagged object with tag number 32 and the string -
700 exactly what was returned by C<TO_CBOR>.
701
702 To serialise an object so it can automatically be deserialised, you need
703 to use C<FREEZE> and C<THAW>. To take the URI module as example, this
704 would be a possible implementation:
705
706 sub URI::FREEZE {
707 my ($self, $serialiser) = @_;
708 "$self" # encode url string
709 }
710
711 sub URI::THAW {
712 my ($class, $serialiser, $uri) = @_;
713 $class->new ($uri)
714 }
715
716 Unlike C<TO_CBOR>, multiple values can be returned by C<FREEZE>. For
717 example, a C<FREEZE> method that returns "type", "id" and "variant" values
718 would cause an invocation of C<THAW> with 5 arguments:
719
720 sub My::Object::FREEZE {
721 my ($self, $serialiser) = @_;
722
723 ($self->{type}, $self->{id}, $self->{variant})
724 }
725
726 sub My::Object::THAW {
727 my ($class, $serialiser, $type, $id, $variant) = @_;
728
729 $class-<new (type => $type, id => $id, variant => $variant)
730 }
731
732
733 =head1 MAGIC HEADER
734
735 There is no way to distinguish CBOR from other formats
736 programmatically. To make it easier to distinguish CBOR from other
737 formats, the CBOR specification has a special "magic string" that can be
738 prepended to any CBOR string without changing its meaning.
739
740 This string is available as C<$CBOR::XS::MAGIC>. This module does not
741 prepend this string to the CBOR data it generates, but it will ignore it
742 if present, so users can prepend this string as a "file type" indicator as
743 required.
744
745
746 =head1 THE CBOR::XS::Tagged CLASS
747
748 CBOR has the concept of tagged values - any CBOR value can be tagged with
749 a numeric 64 bit number, which are centrally administered.
750
751 C<CBOR::XS> handles a few tags internally when en- or decoding. You can
752 also create tags yourself by encoding C<CBOR::XS::Tagged> objects, and the
753 decoder will create C<CBOR::XS::Tagged> objects itself when it hits an
754 unknown tag.
755
756 These objects are simply blessed array references - the first member of
757 the array being the numerical tag, the second being the value.
758
759 You can interact with C<CBOR::XS::Tagged> objects in the following ways:
760
761 =over 4
762
763 =item $tagged = CBOR::XS::tag $tag, $value
764
765 This function(!) creates a new C<CBOR::XS::Tagged> object using the given
766 C<$tag> (0..2**64-1) to tag the given C<$value> (which can be any Perl
767 value that can be encoded in CBOR, including serialisable Perl objects and
768 C<CBOR::XS::Tagged> objects).
769
770 =item $tagged->[0]
771
772 =item $tagged->[0] = $new_tag
773
774 =item $tag = $tagged->tag
775
776 =item $new_tag = $tagged->tag ($new_tag)
777
778 Access/mutate the tag.
779
780 =item $tagged->[1]
781
782 =item $tagged->[1] = $new_value
783
784 =item $value = $tagged->value
785
786 =item $new_value = $tagged->value ($new_value)
787
788 Access/mutate the tagged value.
789
790 =back
791
792 =cut
793
794 sub tag($$) {
795 bless [@_], CBOR::XS::Tagged::;
796 }
797
798 sub CBOR::XS::Tagged::tag {
799 $_[0][0] = $_[1] if $#_;
800 $_[0][0]
801 }
802
803 sub CBOR::XS::Tagged::value {
804 $_[0][1] = $_[1] if $#_;
805 $_[0][1]
806 }
807
808 =head2 EXAMPLES
809
810 Here are some examples of C<CBOR::XS::Tagged> uses to tag objects.
811
812 You can look up CBOR tag value and emanings in the IANA registry at
813 L<http://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>.
814
815 Prepend a magic header (C<$CBOR::XS::MAGIC>):
816
817 my $cbor = encode_cbor CBOR::XS::tag 55799, $value;
818 # same as:
819 my $cbor = $CBOR::XS::MAGIC . encode_cbor $value;
820
821 Serialise some URIs and a regex in an array:
822
823 my $cbor = encode_cbor [
824 (CBOR::XS::tag 32, "http://www.nethype.de/"),
825 (CBOR::XS::tag 32, "http://software.schmorp.de/"),
826 (CBOR::XS::tag 35, "^[Pp][Ee][Rr][lL]\$"),
827 ];
828
829 Wrap CBOR data in CBOR:
830
831 my $cbor_cbor = encode_cbor
832 CBOR::XS::tag 24,
833 encode_cbor [1, 2, 3];
834
835 =head1 TAG HANDLING AND EXTENSIONS
836
837 This section describes how this module handles specific tagged values
838 and extensions. If a tag is not mentioned here and no additional filters
839 are provided for it, then the default handling applies (creating a
840 CBOR::XS::Tagged object on decoding, and only encoding the tag when
841 explicitly requested).
842
843 Tags not handled specifically are currently converted into a
844 L<CBOR::XS::Tagged> object, which is simply a blessed array reference
845 consisting of the numeric tag value followed by the (decoded) CBOR value.
846
847 Future versions of this module reserve the right to special case
848 additional tags (such as base64url).
849
850 =head2 ENFORCED TAGS
851
852 These tags are always handled when decoding, and their handling cannot be
853 overridden by the user.
854
855 =over 4
856
857 =item 26 (perl-object, L<http://cbor.schmorp.de/perl-object>)
858
859 These tags are automatically created (and decoded) for serialisable
860 objects using the C<FREEZE/THAW> methods (the L<Types::Serialier> object
861 serialisation protocol). See L<OBJECT SERIALISATION> for details.
862
863 =item 28, 29 (shareable, sharedref, L<http://cbor.schmorp.de/value-sharing>)
864
865 These tags are automatically decoded when encountered (and they do not
866 result in a cyclic data structure, see C<allow_cycles>), resulting in
867 shared values in the decoded object. They are only encoded, however, when
868 C<allow_sharing> is enabled.
869
870 Not all shared values can be successfully decoded: values that reference
871 themselves will I<currently> decode as C<undef> (this is not the same
872 as a reference pointing to itself, which will be represented as a value
873 that contains an indirect reference to itself - these will be decoded
874 properly).
875
876 Note that considerably more shared value data structures can be decoded
877 than will be encoded - currently, only values pointed to by references
878 will be shared, others will not. While non-reference shared values can be
879 generated in Perl with some effort, they were considered too unimportant
880 to be supported in the encoder. The decoder, however, will decode these
881 values as shared values.
882
883 =item 256, 25 (stringref-namespace, stringref, L<http://cbor.schmorp.de/stringref>)
884
885 These tags are automatically decoded when encountered. They are only
886 encoded, however, when C<pack_strings> is enabled.
887
888 =item 22098 (indirection, L<http://cbor.schmorp.de/indirection>)
889
890 This tag is automatically generated when a reference are encountered (with
891 the exception of hash and array references). It is converted to a reference
892 when decoding.
893
894 =item 55799 (self-describe CBOR, RFC 7049)
895
896 This value is not generated on encoding (unless explicitly requested by
897 the user), and is simply ignored when decoding.
898
899 =back
900
901 =head2 NON-ENFORCED TAGS
902
903 These tags have default filters provided when decoding. Their handling can
904 be overridden by changing the C<%CBOR::XS::FILTER> entry for the tag, or by
905 providing a custom C<filter> callback when decoding.
906
907 When they result in decoding into a specific Perl class, the module
908 usually provides a corresponding C<TO_CBOR> method as well.
909
910 When any of these need to load additional modules that are not part of the
911 perl core distribution (e.g. L<URI>), it is (currently) up to the user to
912 provide these modules. The decoding usually fails with an exception if the
913 required module cannot be loaded.
914
915 =over 4
916
917 =item 0, 1 (date/time string, seconds since the epoch)
918
919 These tags are decoded into L<Time::Piece> objects. The corresponding
920 C<Time::Piece::TO_CBOR> method always encodes into tag 1 values currently.
921
922 The L<Time::Piece> API is generally surprisingly bad, and fractional
923 seconds are only accidentally kept intact, so watch out. On the plus side,
924 the module comes with perl since 5.10, which has to count for something.
925
926 =item 2, 3 (positive/negative bignum)
927
928 These tags are decoded into L<Math::BigInt> objects. The corresponding
929 C<Math::BigInt::TO_CBOR> method encodes "small" bigints into normal CBOR
930 integers, and others into positive/negative CBOR bignums.
931
932 =item 4, 5, 264, 265 (decimal fraction/bigfloat)
933
934 Both decimal fractions and bigfloats are decoded into L<Math::BigFloat>
935 objects. The corresponding C<Math::BigFloat::TO_CBOR> method I<always>
936 encodes into a decimal fraction (either tag 4 or 264).
937
938 NaN and infinities are not encoded properly, as they cannot be represented
939 in CBOR.
940
941 See L<BIGNUM SECURITY CONSIDERATIONS> for more info.
942
943 =item 30 (rational numbers)
944
945 These tags are decoded into L<Math::BigRat> objects. The corresponding
946 C<Math::BigRat::TO_CBOR> method encodes rational numbers with denominator
947 C<1> via their numerator only, i.e., they become normal integers or
948 C<bignums>.
949
950 See L<BIGNUM SECURITY CONSIDERATIONS> for more info.
951
952 =item 21, 22, 23 (expected later JSON conversion)
953
954 CBOR::XS is not a CBOR-to-JSON converter, and will simply ignore these
955 tags.
956
957 =item 32 (URI)
958
959 These objects decode into L<URI> objects. The corresponding
960 C<URI::TO_CBOR> method again results in a CBOR URI value.
961
962 =back
963
964 =cut
965
966 =head1 CBOR and JSON
967
968 CBOR is supposed to implement a superset of the JSON data model, and is,
969 with some coercion, able to represent all JSON texts (something that other
970 "binary JSON" formats such as BSON generally do not support).
971
972 CBOR implements some extra hints and support for JSON interoperability,
973 and the spec offers further guidance for conversion between CBOR and
974 JSON. None of this is currently implemented in CBOR, and the guidelines
975 in the spec do not result in correct round-tripping of data. If JSON
976 interoperability is improved in the future, then the goal will be to
977 ensure that decoded JSON data will round-trip encoding and decoding to
978 CBOR intact.
979
980
981 =head1 SECURITY CONSIDERATIONS
982
983 When you are using CBOR in a protocol, talking to untrusted potentially
984 hostile creatures requires relatively few measures.
985
986 First of all, your CBOR decoder should be secure, that is, should not have
987 any buffer overflows. Obviously, this module should ensure that and I am
988 trying hard on making that true, but you never know.
989
990 Second, you need to avoid resource-starving attacks. That means you should
991 limit the size of CBOR data you accept, or make sure then when your
992 resources run out, that's just fine (e.g. by using a separate process that
993 can crash safely). The size of a CBOR string in octets is usually a good
994 indication of the size of the resources required to decode it into a Perl
995 structure. While CBOR::XS can check the size of the CBOR text, it might be
996 too late when you already have it in memory, so you might want to check
997 the size before you accept the string.
998
999 Third, CBOR::XS recurses using the C stack when decoding objects and
1000 arrays. The C stack is a limited resource: for instance, on my amd64
1001 machine with 8MB of stack size I can decode around 180k nested arrays but
1002 only 14k nested CBOR objects (due to perl itself recursing deeply on croak
1003 to free the temporary). If that is exceeded, the program crashes. To be
1004 conservative, the default nesting limit is set to 512. If your process
1005 has a smaller stack, you should adjust this setting accordingly with the
1006 C<max_depth> method.
1007
1008 Something else could bomb you, too, that I forgot to think of. In that
1009 case, you get to keep the pieces. I am always open for hints, though...
1010
1011 Also keep in mind that CBOR::XS might leak contents of your Perl data
1012 structures in its error messages, so when you serialise sensitive
1013 information you might want to make sure that exceptions thrown by CBOR::XS
1014 will not end up in front of untrusted eyes.
1015
1016
1017 =head1 BIGNUM SECURITY CONSIDERATIONS
1018
1019 CBOR::XS provides a C<TO_CBOR> method for both L<Math::BigInt> and
1020 L<Math::BigFloat> that tries to encode the number in the simplest possible
1021 way, that is, either a CBOR integer, a CBOR bigint/decimal fraction (tag
1022 4) or an arbitrary-exponent decimal fraction (tag 264). Rational numbers
1023 (L<Math::BigRat>, tag 30) can also contain bignums as members.
1024
1025 CBOR::XS will also understand base-2 bigfloat or arbitrary-exponent
1026 bigfloats (tags 5 and 265), but it will never generate these on its own.
1027
1028 Using the built-in L<Math::BigInt::Calc> support, encoding and decoding
1029 decimal fractions is generally fast. Decoding bigints can be slow for very
1030 big numbers (tens of thousands of digits, something that could potentially
1031 be caught by limiting the size of CBOR texts), and decoding bigfloats or
1032 arbitrary-exponent bigfloats can be I<extremely> slow (minutes, decades)
1033 for large exponents (roughly 40 bit and longer).
1034
1035 Additionally, L<Math::BigInt> can take advantage of other bignum
1036 libraries, such as L<Math::GMP>, which cannot handle big floats with large
1037 exponents, and might simply abort or crash your program, due to their code
1038 quality.
1039
1040 This can be a concern if you want to parse untrusted CBOR. If it is, you
1041 might want to disable decoding of tag 2 (bigint) and 3 (negative bigint)
1042 types. You should also disable types 5 and 265, as these can be slow even
1043 without bigints.
1044
1045 Disabling bigints will also partially or fully disable types that rely on
1046 them, e.g. rational numbers that use bignums.
1047
1048
1049 =head1 CBOR IMPLEMENTATION NOTES
1050
1051 This section contains some random implementation notes. They do not
1052 describe guaranteed behaviour, but merely behaviour as-is implemented
1053 right now.
1054
1055 64 bit integers are only properly decoded when Perl was built with 64 bit
1056 support.
1057
1058 Strings and arrays are encoded with a definite length. Hashes as well,
1059 unless they are tied (or otherwise magical).
1060
1061 Only the double data type is supported for NV data types - when Perl uses
1062 long double to represent floating point values, they might not be encoded
1063 properly. Half precision types are accepted, but not encoded.
1064
1065 Strict mode and canonical mode are not implemented.
1066
1067
1068 =head1 LIMITATIONS ON PERLS WITHOUT 64-BIT INTEGER SUPPORT
1069
1070 On perls that were built without 64 bit integer support (these are rare
1071 nowadays, even on 32 bit architectures, as all major Perl distributions
1072 are built with 64 bit integer support), support for any kind of 64 bit
1073 integer in CBOR is very limited - most likely, these 64 bit values will
1074 be truncated, corrupted, or otherwise not decoded correctly. This also
1075 includes string, array and map sizes that are stored as 64 bit integers.
1076
1077
1078 =head1 THREADS
1079
1080 This module is I<not> guaranteed to be thread safe and there are no
1081 plans to change this until Perl gets thread support (as opposed to the
1082 horribly slow so-called "threads" which are simply slow and bloated
1083 process simulations - use fork, it's I<much> faster, cheaper, better).
1084
1085 (It might actually work, but you have been warned).
1086
1087
1088 =head1 BUGS
1089
1090 While the goal of this module is to be correct, that unfortunately does
1091 not mean it's bug-free, only that I think its design is bug-free. If you
1092 keep reporting bugs they will be fixed swiftly, though.
1093
1094 Please refrain from using rt.cpan.org or any other bug reporting
1095 service. I put the contact address into my modules for a reason.
1096
1097 =cut
1098
1099 our %FILTER = (
1100 0 => sub { # rfc4287 datetime, utf-8
1101 require Time::Piece;
1102 # Time::Piece::Strptime uses the "incredibly flexible date parsing routine"
1103 # from FreeBSD, which can't parse ISO 8601, RFC3339, RFC4287 or much of anything
1104 # else either. Whats incredibe over standard strptime totally escapes me.
1105 # doesn't do fractional times, either. sigh.
1106 # In fact, it's all a lie, it uses whatever strptime it wants, and of course,
1107 # they are all incompatible. The openbsd one simply ignores %z (but according to the
1108 # docs, it would be much more incredibly flexible indeed. If it worked, that is.).
1109 scalar eval {
1110 my $s = $_[1];
1111
1112 $s =~ s/Z$/+00:00/;
1113 $s =~ s/(\.[0-9]+)?([+-][0-9][0-9]):([0-9][0-9])$//
1114 or die;
1115
1116 my $b = $1 - ($2 * 60 + $3) * 60; # fractional part + offset. hopefully
1117 my $d = Time::Piece->strptime ($s, "%Y-%m-%dT%H:%M:%S");
1118
1119 Time::Piece::gmtime ($d->epoch + $b)
1120 } || die "corrupted CBOR date/time string ($_[0])";
1121 },
1122
1123 1 => sub { # seconds since the epoch, possibly fractional
1124 require Time::Piece;
1125 scalar Time::Piece::gmtime (pop)
1126 },
1127
1128 2 => sub { # pos bigint
1129 require Math::BigInt;
1130 Math::BigInt->new ("0x" . unpack "H*", pop)
1131 },
1132
1133 3 => sub { # neg bigint
1134 require Math::BigInt;
1135 -Math::BigInt->new ("0x" . unpack "H*", pop)
1136 },
1137
1138 4 => sub { # decimal fraction, array
1139 require Math::BigFloat;
1140 Math::BigFloat->new ($_[1][1] . "E" . $_[1][0])
1141 },
1142
1143 264 => sub { # decimal fraction with arbitrary exponent
1144 require Math::BigFloat;
1145 Math::BigFloat->new ($_[1][1] . "E" . $_[1][0])
1146 },
1147
1148 5 => sub { # bigfloat, array
1149 require Math::BigFloat;
1150 scalar Math::BigFloat->new ($_[1][1]) * Math::BigFloat->new (2)->bpow ($_[1][0])
1151 },
1152
1153 265 => sub { # bigfloat with arbitrary exponent
1154 require Math::BigFloat;
1155 scalar Math::BigFloat->new ($_[1][1]) * Math::BigFloat->new (2)->bpow ($_[1][0])
1156 },
1157
1158 30 => sub { # rational number
1159 require Math::BigRat;
1160 Math::BigRat->new ("$_[1][0]/$_[1][1]") # separate parameters only work in recent versons
1161 },
1162
1163 21 => sub { pop }, # expected conversion to base64url encoding
1164 22 => sub { pop }, # expected conversion to base64 encoding
1165 23 => sub { pop }, # expected conversion to base16 encoding
1166
1167 # 24 # embedded cbor, byte string
1168
1169 32 => sub {
1170 require URI;
1171 URI->new (pop)
1172 },
1173
1174 # 33 # base64url rfc4648, utf-8
1175 # 34 # base64 rfc46484, utf-8
1176 # 35 # regex pcre/ecma262, utf-8
1177 # 36 # mime message rfc2045, utf-8
1178 );
1179
1180 sub CBOR::XS::default_filter {
1181 &{ $FILTER{$_[0]} or return }
1182 }
1183
1184 sub URI::TO_CBOR {
1185 my $uri = $_[0]->as_string;
1186 utf8::upgrade $uri;
1187 tag 32, $uri
1188 }
1189
1190 sub Math::BigInt::TO_CBOR {
1191 if (-2147483648 <= $_[0] && $_[0] <= 2147483647) {
1192 $_[0]->numify
1193 } else {
1194 my $hex = substr $_[0]->as_hex, 2;
1195 $hex = "0$hex" if 1 & length $hex; # sigh
1196 tag $_[0] >= 0 ? 2 : 3, pack "H*", $hex
1197 }
1198 }
1199
1200 sub Math::BigFloat::TO_CBOR {
1201 my ($m, $e) = $_[0]->parts;
1202
1203 -9223372036854775808 <= $e && $e <= 18446744073709551615
1204 ? tag 4, [$e->numify, $m]
1205 : tag 264, [$e, $m]
1206 }
1207
1208 sub Math::BigRat::TO_CBOR {
1209 my ($n, $d) = $_[0]->parts;
1210
1211 $d == 1
1212 ? $n
1213 : tag 30, [$n, $d]
1214 }
1215
1216 sub Time::Piece::TO_CBOR {
1217 tag 1, 0 + $_[0]->epoch
1218 }
1219
1220 XSLoader::load "CBOR::XS", $VERSION;
1221
1222 =head1 SEE ALSO
1223
1224 The L<JSON> and L<JSON::XS> modules that do similar, but human-readable,
1225 serialisation.
1226
1227 The L<Types::Serialiser> module provides the data model for true, false
1228 and error values.
1229
1230 =head1 AUTHOR
1231
1232 Marc Lehmann <schmorp@schmorp.de>
1233 http://home.schmorp.de/
1234
1235 =cut
1236
1237 1
1238