ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.pm
Revision: 1.20
Committed: Wed Nov 20 02:03:08 2013 UTC (10 years, 5 months ago) by root
Branch: MAIN
Changes since 1.19: +10 -11 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.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 root 1.6 $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 root 1.1
29     =head1 DESCRIPTION
30    
31 root 1.9 WARNING! This module is very new, and not very well tested (that's up to
32     you to do). Furthermore, details of the implementation might change freely
33     before version 1.0. And lastly, the object serialisation protocol depends
34     on a pending IANA assignment, and until that assignment is official, this
35     implementation is not interoperable with other implementations (even
36     future versions of this module) until the assignment is done.
37    
38     You are still invited to try out CBOR, and this module.
39 root 1.5
40     This module converts Perl data structures to the Concise Binary Object
41     Representation (CBOR) and vice versa. CBOR is a fast binary serialisation
42     format that aims to use a superset of the JSON data model, i.e. when you
43     can represent something in JSON, you should be able to represent it in
44     CBOR.
45 root 1.1
46 root 1.9 In short, CBOR is a faster and very compact binary alternative to JSON,
47 root 1.10 with the added ability of supporting serialisation of Perl objects. (JSON
48     often compresses better than CBOR though, so if you plan to compress the
49     data later you might want to compare both formats first).
50 root 1.5
51 root 1.15 To give you a general idea about speed, with texts in the megabyte range,
52     C<CBOR::XS> usually encodes roughly twice as fast as L<Storable> or
53     L<JSON::XS> and decodes about 15%-30% faster than those. The shorter the
54     data, the worse L<Storable> performs in comparison.
55    
56     As for compactness, C<CBOR::XS> encoded data structures are usually about
57     20% smaller than the same data encoded as (compact) JSON or L<Storable>.
58 root 1.14
59 root 1.5 The primary goal of this module is to be I<correct> and the secondary goal
60     is to be I<fast>. To reach the latter goal it was written in C.
61 root 1.1
62     See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and
63     vice versa.
64    
65     =cut
66    
67     package CBOR::XS;
68    
69     use common::sense;
70    
71 root 1.17 our $VERSION = 0.08;
72 root 1.1 our @ISA = qw(Exporter);
73    
74     our @EXPORT = qw(encode_cbor decode_cbor);
75    
76     use Exporter;
77     use XSLoader;
78    
79 root 1.6 use Types::Serialiser;
80    
81 root 1.3 our $MAGIC = "\xd9\xd9\xf7";
82    
83 root 1.1 =head1 FUNCTIONAL INTERFACE
84    
85     The following convenience methods are provided by this module. They are
86     exported by default:
87    
88     =over 4
89    
90     =item $cbor_data = encode_cbor $perl_scalar
91    
92     Converts the given Perl data structure to CBOR representation. Croaks on
93     error.
94    
95     =item $perl_scalar = decode_cbor $cbor_data
96    
97     The opposite of C<encode_cbor>: expects a valid CBOR string to parse,
98     returning the resulting perl scalar. Croaks on error.
99    
100     =back
101    
102    
103     =head1 OBJECT-ORIENTED INTERFACE
104    
105     The object oriented interface lets you configure your own encoding or
106     decoding style, within the limits of supported formats.
107    
108     =over 4
109    
110     =item $cbor = new CBOR::XS
111    
112     Creates a new CBOR::XS object that can be used to de/encode CBOR
113     strings. All boolean flags described below are by default I<disabled>.
114    
115     The mutators for flags all return the CBOR object again and thus calls can
116     be chained:
117    
118     #TODO
119     my $cbor = CBOR::XS->new->encode ({a => [1,2]});
120    
121     =item $cbor = $cbor->max_depth ([$maximum_nesting_depth])
122    
123     =item $max_depth = $cbor->get_max_depth
124    
125     Sets the maximum nesting level (default C<512>) accepted while encoding
126     or decoding. If a higher nesting level is detected in CBOR data or a Perl
127     data structure, then the encoder and decoder will stop and croak at that
128     point.
129    
130     Nesting level is defined by number of hash- or arrayrefs that the encoder
131     needs to traverse to reach a given point or the number of C<{> or C<[>
132     characters without their matching closing parenthesis crossed to reach a
133     given character in a string.
134    
135     Setting the maximum depth to one disallows any nesting, so that ensures
136     that the object is only a single hash/object or array.
137    
138     If no argument is given, the highest possible setting will be used, which
139     is rarely useful.
140    
141     Note that nesting is implemented by recursion in C. The default value has
142     been chosen to be as large as typical operating systems allow without
143     crashing.
144    
145     See SECURITY CONSIDERATIONS, below, for more info on why this is useful.
146    
147     =item $cbor = $cbor->max_size ([$maximum_string_size])
148    
149     =item $max_size = $cbor->get_max_size
150    
151     Set the maximum length a CBOR string may have (in bytes) where decoding
152     is being attempted. The default is C<0>, meaning no limit. When C<decode>
153     is called on a string that is longer then this many bytes, it will not
154     attempt to decode the string but throw an exception. This setting has no
155     effect on C<encode> (yet).
156    
157     If no argument is given, the limit check will be deactivated (same as when
158     C<0> is specified).
159    
160     See SECURITY CONSIDERATIONS, below, for more info on why this is useful.
161    
162 root 1.19 =item $cbor = $cbor->allow_unknown ([$enable])
163    
164     =item $enabled = $cbor->get_allow_unknown
165    
166     If C<$enable> is true (or missing), then C<encode> will I<not> throw an
167     exception when it encounters values it cannot represent in CBOR (for
168     example, filehandles) but instead will encode a CBOR C<error> value.
169    
170     If C<$enable> is false (the default), then C<encode> will throw an
171     exception when it encounters anything it cannot encode as CBOR.
172    
173     This option does not affect C<decode> in any way, and it is recommended to
174     leave it off unless you know your communications partner.
175    
176 root 1.20 =item $cbor = $cbor->allow_sharing ([$enable])
177 root 1.19
178 root 1.20 =item $enabled = $cbor->get_allow_sharing
179 root 1.19
180     If C<$enable> is true (or missing), then C<encode> will not double-encode
181 root 1.20 values that have been referenced before (e.g. when the same object, such
182     as an array, is referenced multiple times), but instead will emit a
183     reference to the earlier value.
184 root 1.19
185     This means that such values will only be encoded once, and will not result
186     in a deep cloning of the value on decode, in decoders supporting the value
187     sharing extension.
188    
189     Detecting shared values incurs a runtime overhead when values are encoded
190     that have a reference counter large than one, and might unnecessarily
191     increase the encoded size, as potentially shared values are encode as
192     sharable whether or not they are actually shared.
193    
194 root 1.20 At the moment, only targets of references can be shared (e.g. scalars,
195     arrays or hashes pointed to by a reference). Weirder constructs, such as
196     an array with multiple "copies" of the I<same> string, which are hard but
197     not impossible to create in Perl, are not supported (this is the same as
198     for L<Storable>).
199 root 1.19
200     If C<$enable> is false (the default), then C<encode> will encode
201     exception when it encounters anything it cannot encode as CBOR.
202    
203     This option does not affect C<decode> in any way - shared values and
204     references will always be decoded properly if present. It is recommended
205     to leave it off unless you know your communications partner supports the
206     value sharing extensions to CBOR (http://cbor.schmorp.de/value-sharing).
207    
208 root 1.1 =item $cbor_data = $cbor->encode ($perl_scalar)
209    
210     Converts the given Perl data structure (a scalar value) to its CBOR
211     representation.
212    
213     =item $perl_scalar = $cbor->decode ($cbor_data)
214    
215     The opposite of C<encode>: expects CBOR data and tries to parse it,
216     returning the resulting simple scalar or reference. Croaks on error.
217    
218     =item ($perl_scalar, $octets) = $cbor->decode_prefix ($cbor_data)
219    
220     This works like the C<decode> method, but instead of raising an exception
221     when there is trailing garbage after the CBOR string, it will silently
222     stop parsing there and return the number of characters consumed so far.
223    
224     This is useful if your CBOR texts are not delimited by an outer protocol
225     and you need to know where the first CBOR string ends amd the next one
226     starts.
227    
228     CBOR::XS->new->decode_prefix ("......")
229     => ("...", 3)
230    
231     =back
232    
233    
234     =head1 MAPPING
235    
236     This section describes how CBOR::XS maps Perl values to CBOR values and
237     vice versa. These mappings are designed to "do the right thing" in most
238     circumstances automatically, preserving round-tripping characteristics
239     (what you put in comes out as something equivalent).
240    
241     For the more enlightened: note that in the following descriptions,
242     lowercase I<perl> refers to the Perl interpreter, while uppercase I<Perl>
243     refers to the abstract Perl language itself.
244    
245    
246     =head2 CBOR -> PERL
247    
248     =over 4
249    
250 root 1.4 =item integers
251    
252     CBOR integers become (numeric) perl scalars. On perls without 64 bit
253     support, 64 bit integers will be truncated or otherwise corrupted.
254    
255     =item byte strings
256    
257     Byte strings will become octet strings in Perl (the byte values 0..255
258     will simply become characters of the same value in Perl).
259    
260     =item UTF-8 strings
261    
262     UTF-8 strings in CBOR will be decoded, i.e. the UTF-8 octets will be
263     decoded into proper Unicode code points. At the moment, the validity of
264     the UTF-8 octets will not be validated - corrupt input will result in
265     corrupted Perl strings.
266    
267     =item arrays, maps
268    
269     CBOR arrays and CBOR maps will be converted into references to a Perl
270     array or hash, respectively. The keys of the map will be stringified
271     during this process.
272    
273 root 1.6 =item null
274    
275     CBOR null becomes C<undef> in Perl.
276    
277     =item true, false, undefined
278 root 1.1
279 root 1.6 These CBOR values become C<Types:Serialiser::true>,
280     C<Types:Serialiser::false> and C<Types::Serialiser::error>,
281 root 1.1 respectively. They are overloaded to act almost exactly like the numbers
282 root 1.6 C<1> and C<0> (for true and false) or to throw an exception on access (for
283     error). See the L<Types::Serialiser> manpage for details.
284    
285     =item CBOR tag 256 (perl object)
286    
287 root 1.7 The tag value C<256> (TODO: pending iana registration) will be used
288 root 1.11 to deserialise a Perl object serialised with C<FREEZE>. See L<OBJECT
289     SERIALISATION>, below, for details.
290 root 1.1
291 root 1.6 =item CBOR tag 55799 (magic header)
292 root 1.4
293 root 1.6 The tag 55799 is ignored (this tag implements the magic header).
294 root 1.1
295 root 1.6 =item other CBOR tags
296 root 1.4
297 root 1.6 Tagged items consists of a numeric tag and another CBOR value. Tags not
298     handled internally are currently converted into a L<CBOR::XS::Tagged>
299     object, which is simply a blessed array reference consisting of the
300     numeric tag value followed by the (decoded) CBOR value.
301 root 1.4
302 root 1.6 In the future, support for user-supplied conversions might get added.
303 root 1.4
304     =item anything else
305    
306     Anything else (e.g. unsupported simple values) will raise a decoding
307     error.
308 root 1.1
309     =back
310    
311    
312     =head2 PERL -> CBOR
313    
314     The mapping from Perl to CBOR is slightly more difficult, as Perl is a
315     truly typeless language, so we can only guess which CBOR type is meant by
316     a Perl value.
317    
318     =over 4
319    
320     =item hash references
321    
322 root 1.4 Perl hash references become CBOR maps. As there is no inherent ordering in
323     hash keys (or CBOR maps), they will usually be encoded in a pseudo-random
324     order.
325    
326     Currently, tied hashes will use the indefinite-length format, while normal
327     hashes will use the fixed-length format.
328 root 1.1
329     =item array references
330    
331 root 1.4 Perl array references become fixed-length CBOR arrays.
332 root 1.1
333     =item other references
334    
335     Other unblessed references are generally not allowed and will cause an
336     exception to be thrown, except for references to the integers C<0> and
337 root 1.4 C<1>, which get turned into false and true in CBOR.
338    
339     =item CBOR::XS::Tagged objects
340    
341     Objects of this type must be arrays consisting of a single C<[tag, value]>
342 root 1.13 pair. The (numerical) tag will be encoded as a CBOR tag, the value will
343     be encoded as appropriate for the value. You cna use C<CBOR::XS::tag> to
344     create such objects.
345 root 1.1
346 root 1.6 =item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error
347 root 1.1
348 root 1.6 These special values become CBOR true, CBOR false and CBOR undefined
349     values, respectively. You can also use C<\1>, C<\0> and C<\undef> directly
350     if you want.
351 root 1.1
352 root 1.7 =item other blessed objects
353 root 1.1
354 root 1.7 Other blessed objects are serialised via C<TO_CBOR> or C<FREEZE>. See
355 root 1.11 L<OBJECT SERIALISATION>, below, for details.
356 root 1.1
357     =item simple scalars
358    
359     TODO
360     Simple Perl scalars (any scalar that is not a reference) are the most
361     difficult objects to encode: CBOR::XS will encode undefined scalars as
362 root 1.4 CBOR null values, scalars that have last been used in a string context
363 root 1.1 before encoding as CBOR strings, and anything else as number value:
364    
365     # dump as number
366     encode_cbor [2] # yields [2]
367     encode_cbor [-3.0e17] # yields [-3e+17]
368     my $value = 5; encode_cbor [$value] # yields [5]
369    
370     # used as string, so dump as string
371     print $value;
372     encode_cbor [$value] # yields ["5"]
373    
374     # undef becomes null
375     encode_cbor [undef] # yields [null]
376    
377     You can force the type to be a CBOR string by stringifying it:
378    
379     my $x = 3.1; # some variable containing a number
380     "$x"; # stringified
381     $x .= ""; # another, more awkward way to stringify
382     print $x; # perl does it for you, too, quite often
383    
384     You can force the type to be a CBOR number by numifying it:
385    
386     my $x = "3"; # some variable containing a string
387     $x += 0; # numify it, ensuring it will be dumped as a number
388     $x *= 1; # same thing, the choice is yours.
389    
390     You can not currently force the type in other, less obscure, ways. Tell me
391     if you need this capability (but don't forget to explain why it's needed
392     :).
393    
394 root 1.4 Perl values that seem to be integers generally use the shortest possible
395     representation. Floating-point values will use either the IEEE single
396     format if possible without loss of precision, otherwise the IEEE double
397     format will be used. Perls that use formats other than IEEE double to
398     represent numerical values are supported, but might suffer loss of
399     precision.
400 root 1.1
401     =back
402    
403 root 1.7 =head2 OBJECT SERIALISATION
404    
405     This module knows two way to serialise a Perl object: The CBOR-specific
406     way, and the generic way.
407    
408     Whenever the encoder encounters a Perl object that it cnanot serialise
409     directly (most of them), it will first look up the C<TO_CBOR> method on
410     it.
411    
412     If it has a C<TO_CBOR> method, it will call it with the object as only
413     argument, and expects exactly one return value, which it will then
414     substitute and encode it in the place of the object.
415    
416     Otherwise, it will look up the C<FREEZE> method. If it exists, it will
417     call it with the object as first argument, and the constant string C<CBOR>
418     as the second argument, to distinguish it from other serialisers.
419    
420     The C<FREEZE> method can return any number of values (i.e. zero or
421     more). These will be encoded as CBOR perl object, together with the
422     classname.
423    
424     If an object supports neither C<TO_CBOR> nor C<FREEZE>, encoding will fail
425     with an error.
426    
427     Objects encoded via C<TO_CBOR> cannot be automatically decoded, but
428     objects encoded via C<FREEZE> can be decoded using the following protocol:
429    
430     When an encoded CBOR perl object is encountered by the decoder, it will
431     look up the C<THAW> method, by using the stored classname, and will fail
432     if the method cannot be found.
433    
434     After the lookup it will call the C<THAW> method with the stored classname
435     as first argument, the constant string C<CBOR> as second argument, and all
436     values returned by C<FREEZE> as remaining arguments.
437    
438     =head4 EXAMPLES
439    
440     Here is an example C<TO_CBOR> method:
441    
442     sub My::Object::TO_CBOR {
443     my ($obj) = @_;
444    
445     ["this is a serialised My::Object object", $obj->{id}]
446     }
447    
448     When a C<My::Object> is encoded to CBOR, it will instead encode a simple
449     array with two members: a string, and the "object id". Decoding this CBOR
450     string will yield a normal perl array reference in place of the object.
451    
452     A more useful and practical example would be a serialisation method for
453     the URI module. CBOR has a custom tag value for URIs, namely 32:
454    
455     sub URI::TO_CBOR {
456     my ($self) = @_;
457     my $uri = "$self"; # stringify uri
458     utf8::upgrade $uri; # make sure it will be encoded as UTF-8 string
459     CBOR::XS::tagged 32, "$_[0]"
460     }
461    
462     This will encode URIs as a UTF-8 string with tag 32, which indicates an
463     URI.
464    
465     Decoding such an URI will not (currently) give you an URI object, but
466     instead a CBOR::XS::Tagged object with tag number 32 and the string -
467     exactly what was returned by C<TO_CBOR>.
468    
469     To serialise an object so it can automatically be deserialised, you need
470     to use C<FREEZE> and C<THAW>. To take the URI module as example, this
471     would be a possible implementation:
472    
473     sub URI::FREEZE {
474     my ($self, $serialiser) = @_;
475     "$self" # encode url string
476     }
477    
478     sub URI::THAW {
479     my ($class, $serialiser, $uri) = @_;
480    
481     $class->new ($uri)
482     }
483    
484     Unlike C<TO_CBOR>, multiple values can be returned by C<FREEZE>. For
485     example, a C<FREEZE> method that returns "type", "id" and "variant" values
486     would cause an invocation of C<THAW> with 5 arguments:
487    
488     sub My::Object::FREEZE {
489     my ($self, $serialiser) = @_;
490    
491     ($self->{type}, $self->{id}, $self->{variant})
492     }
493    
494     sub My::Object::THAW {
495     my ($class, $serialiser, $type, $id, $variant) = @_;
496    
497     $class-<new (type => $type, id => $id, variant => $variant)
498     }
499    
500 root 1.1
501 root 1.7 =head1 MAGIC HEADER
502 root 1.3
503     There is no way to distinguish CBOR from other formats
504     programmatically. To make it easier to distinguish CBOR from other
505     formats, the CBOR specification has a special "magic string" that can be
506 root 1.18 prepended to any CBOR string without changing its meaning.
507 root 1.3
508     This string is available as C<$CBOR::XS::MAGIC>. This module does not
509 root 1.18 prepend this string to the CBOR data it generates, but it will ignore it
510 root 1.3 if present, so users can prepend this string as a "file type" indicator as
511     required.
512    
513    
514 root 1.12 =head1 THE CBOR::XS::Tagged CLASS
515    
516     CBOR has the concept of tagged values - any CBOR value can be tagged with
517     a numeric 64 bit number, which are centrally administered.
518    
519     C<CBOR::XS> handles a few tags internally when en- or decoding. You can
520     also create tags yourself by encoding C<CBOR::XS::Tagged> objects, and the
521     decoder will create C<CBOR::XS::Tagged> objects itself when it hits an
522     unknown tag.
523    
524     These objects are simply blessed array references - the first member of
525     the array being the numerical tag, the second being the value.
526    
527     You can interact with C<CBOR::XS::Tagged> objects in the following ways:
528    
529     =over 4
530    
531     =item $tagged = CBOR::XS::tag $tag, $value
532    
533     This function(!) creates a new C<CBOR::XS::Tagged> object using the given
534     C<$tag> (0..2**64-1) to tag the given C<$value> (which can be any Perl
535     value that can be encoded in CBOR, including serialisable Perl objects and
536     C<CBOR::XS::Tagged> objects).
537    
538     =item $tagged->[0]
539    
540     =item $tagged->[0] = $new_tag
541    
542     =item $tag = $tagged->tag
543    
544     =item $new_tag = $tagged->tag ($new_tag)
545    
546     Access/mutate the tag.
547    
548     =item $tagged->[1]
549    
550     =item $tagged->[1] = $new_value
551    
552     =item $value = $tagged->value
553    
554     =item $new_value = $tagged->value ($new_value)
555    
556     Access/mutate the tagged value.
557    
558     =back
559    
560     =cut
561    
562     sub tag($$) {
563     bless [@_], CBOR::XS::Tagged::;
564     }
565    
566     sub CBOR::XS::Tagged::tag {
567     $_[0][0] = $_[1] if $#_;
568     $_[0][0]
569     }
570    
571     sub CBOR::XS::Tagged::value {
572     $_[0][1] = $_[1] if $#_;
573     $_[0][1]
574     }
575    
576 root 1.13 =head2 EXAMPLES
577    
578     Here are some examples of C<CBOR::XS::Tagged> uses to tag objects.
579    
580     You can look up CBOR tag value and emanings in the IANA registry at
581     L<http://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>.
582    
583     Prepend a magic header (C<$CBOR::XS::MAGIC>):
584    
585     my $cbor = encode_cbor CBOR::XS::tag 55799, $value;
586     # same as:
587     my $cbor = $CBOR::XS::MAGIC . encode_cbor $value;
588    
589     Serialise some URIs and a regex in an array:
590    
591     my $cbor = encode_cbor [
592     (CBOR::XS::tag 32, "http://www.nethype.de/"),
593     (CBOR::XS::tag 32, "http://software.schmorp.de/"),
594     (CBOR::XS::tag 35, "^[Pp][Ee][Rr][lL]\$"),
595     ];
596    
597     Wrap CBOR data in CBOR:
598    
599     my $cbor_cbor = encode_cbor
600     CBOR::XS::tag 24,
601     encode_cbor [1, 2, 3];
602    
603 root 1.19 =head1 TAG HANDLING AND EXTENSIONS
604    
605     This section describes how this module handles specific tagged values and
606     extensions. If a tag is not mentioned here, then the default handling
607     applies (creating a CBOR::XS::Tagged object on decoding, and only encoding
608     the tag when explicitly requested).
609    
610     Future versions of this module reserve the right to special case
611     additional tags (such as bigfloat or base64url).
612    
613     =over 4
614    
615     =item <unassigned> (perl-object, L<http://cbor.schmorp.de/perl-object>)
616    
617     These tags are automatically created for serialisable objects using the
618     C<FREEZE/THAW> methods (the L<Types::Serialier> object serialisation
619     protocol).
620    
621     =item <unassigned>, <unassigned> (sharable, sharedref, L <http://cbor.schmorp.de/value-sharing>)
622    
623     These tags are automatically decoded when encountered, resulting in
624     shared values in the decoded object. They are only encoded, however, when
625     C<allow_sharable> is enabled.
626    
627     =item 22098 (indirection, L<http://cbor.schmorp.de/indirection>)
628    
629     This tag is automatically generated when a reference are encountered (with
630     the exception of hash and array refernces). It is converted to a reference
631     when decoding.
632    
633     =item 55799 (self-describe CBOR, RFC 7049)
634    
635     This value is not generated on encoding (unless explicitly requested by
636     the user), and is simply ignored when decoding.
637    
638     =back
639    
640    
641 root 1.7 =head1 CBOR and JSON
642 root 1.1
643 root 1.4 CBOR is supposed to implement a superset of the JSON data model, and is,
644     with some coercion, able to represent all JSON texts (something that other
645     "binary JSON" formats such as BSON generally do not support).
646    
647     CBOR implements some extra hints and support for JSON interoperability,
648     and the spec offers further guidance for conversion between CBOR and
649     JSON. None of this is currently implemented in CBOR, and the guidelines
650     in the spec do not result in correct round-tripping of data. If JSON
651     interoperability is improved in the future, then the goal will be to
652     ensure that decoded JSON data will round-trip encoding and decoding to
653     CBOR intact.
654 root 1.1
655    
656     =head1 SECURITY CONSIDERATIONS
657    
658     When you are using CBOR in a protocol, talking to untrusted potentially
659     hostile creatures requires relatively few measures.
660    
661     First of all, your CBOR decoder should be secure, that is, should not have
662     any buffer overflows. Obviously, this module should ensure that and I am
663     trying hard on making that true, but you never know.
664    
665     Second, you need to avoid resource-starving attacks. That means you should
666     limit the size of CBOR data you accept, or make sure then when your
667     resources run out, that's just fine (e.g. by using a separate process that
668     can crash safely). The size of a CBOR string in octets is usually a good
669     indication of the size of the resources required to decode it into a Perl
670     structure. While CBOR::XS can check the size of the CBOR text, it might be
671     too late when you already have it in memory, so you might want to check
672     the size before you accept the string.
673    
674     Third, CBOR::XS recurses using the C stack when decoding objects and
675     arrays. The C stack is a limited resource: for instance, on my amd64
676     machine with 8MB of stack size I can decode around 180k nested arrays but
677     only 14k nested CBOR objects (due to perl itself recursing deeply on croak
678     to free the temporary). If that is exceeded, the program crashes. To be
679     conservative, the default nesting limit is set to 512. If your process
680     has a smaller stack, you should adjust this setting accordingly with the
681     C<max_depth> method.
682    
683     Something else could bomb you, too, that I forgot to think of. In that
684     case, you get to keep the pieces. I am always open for hints, though...
685    
686     Also keep in mind that CBOR::XS might leak contents of your Perl data
687     structures in its error messages, so when you serialise sensitive
688     information you might want to make sure that exceptions thrown by CBOR::XS
689     will not end up in front of untrusted eyes.
690    
691     =head1 CBOR IMPLEMENTATION NOTES
692    
693     This section contains some random implementation notes. They do not
694     describe guaranteed behaviour, but merely behaviour as-is implemented
695     right now.
696    
697     64 bit integers are only properly decoded when Perl was built with 64 bit
698     support.
699    
700     Strings and arrays are encoded with a definite length. Hashes as well,
701     unless they are tied (or otherwise magical).
702    
703     Only the double data type is supported for NV data types - when Perl uses
704     long double to represent floating point values, they might not be encoded
705     properly. Half precision types are accepted, but not encoded.
706    
707     Strict mode and canonical mode are not implemented.
708    
709    
710     =head1 THREADS
711    
712     This module is I<not> guaranteed to be thread safe and there are no
713     plans to change this until Perl gets thread support (as opposed to the
714     horribly slow so-called "threads" which are simply slow and bloated
715     process simulations - use fork, it's I<much> faster, cheaper, better).
716    
717     (It might actually work, but you have been warned).
718    
719    
720     =head1 BUGS
721    
722     While the goal of this module is to be correct, that unfortunately does
723     not mean it's bug-free, only that I think its design is bug-free. If you
724     keep reporting bugs they will be fixed swiftly, though.
725    
726     Please refrain from using rt.cpan.org or any other bug reporting
727     service. I put the contact address into my modules for a reason.
728    
729     =cut
730    
731     XSLoader::load "CBOR::XS", $VERSION;
732    
733     =head1 SEE ALSO
734    
735     The L<JSON> and L<JSON::XS> modules that do similar, but human-readable,
736     serialisation.
737    
738 root 1.6 The L<Types::Serialiser> module provides the data model for true, false
739     and error values.
740    
741 root 1.1 =head1 AUTHOR
742    
743     Marc Lehmann <schmorp@schmorp.de>
744     http://home.schmorp.de/
745    
746     =cut
747    
748 root 1.6 1
749