File Coverage

blib/lib/CBOR/Free.pm
Criterion Covered Total %
statement 26 29 89.6
branch n/a
condition n/a
subroutine 11 12 91.6
pod 1 3 33.3
total 38 44 86.3


line stmt bran cond sub pod time code
1             package CBOR::Free;
2              
3 26     26   1707157 use strict;
  26         185  
  26         784  
4 26     26   147 use warnings;
  26         53  
  26         688  
5              
6 26     26   11056 use CBOR::Free::X;
  26         79  
  26         814  
7 26     26   10612 use CBOR::Free::Tagged;
  26         73  
  26         961  
8              
9             our ($VERSION);
10              
11 26     26   226 use XSLoader ();
  26         51  
  26         749  
12              
13             BEGIN {
14 26     26   78 $VERSION = '0.30_05';
15 26         21674 XSLoader::load();
16             }
17              
18             #----------------------------------------------------------------------
19              
20             =encoding utf-8
21              
22             =head1 NAME
23              
24             CBOR::Free - Fast CBOR for everyone
25              
26             =head1 SYNOPSIS
27              
28             $cbor = CBOR::Free::encode( $some_data_structure );
29              
30             $thing = CBOR::Free::decode( $cbor )
31              
32             my $tagged = CBOR::Free::tag( 1, '2019-01-02T00:01:02Z' );
33              
34             Also see L for an object-oriented interface
35             to the decoder.
36              
37             =head1 DESCRIPTION
38              
39             This library implements L
40             via XS under a license that permits commercial usage with no “strings
41             attached”.
42              
43             =head1 STATUS
44              
45             This distribution is an experimental effort. Its interface is still
46             subject to change. If you decide to use CBOR::Free in your project,
47             please always check the changelog before upgrading.
48              
49             =head1 FUNCTIONS
50              
51             =head2 $cbor = encode( $DATA, %OPTS )
52              
53             Encodes a data structure or non-reference scalar to CBOR.
54             The encoder recognizes and encodes integers, floats, byte and character
55             strings, array and hash references, L instances,
56             L booleans, and undef (encoded as null).
57              
58             The encoder currently does not handle any other blessed references.
59              
60             %OPTS may be:
61              
62             =over
63              
64             =item * C - A boolean that makes the encoder output
65             CBOR in L.
66              
67             =item * C - Decides the logic to use for
68             CBOR encoding of strings and hash keys. (The word “string”
69             in the below descriptions applies equally to hash keys.)
70              
71             Takes one of:
72              
73             =over
74              
75             =item * C: The default mode of operation. If the string’s internal
76             UTF8 flag is set, it will become a CBOR text string; otherwise, it will be
77             CBOR binary. This is good for IPC with other Perl code but isn’t a very
78             friendly default for working with other languages that probably expect more
79             reliably-typed strings.
80              
81             This configuration is B recommended; it’s the default behavior because
82             it’s the only configuration that can reasonably fulfill that role. This is
83             also the only way to output text and binary strings in a single CBOR document.
84              
85             =item * C: Treats all strings as unencoded characters.
86             All CBOR strings will be text.
87              
88             This is probably what you want if you’re
89             following the receive-decode-process-encode-output workflow that
90             L recommends (which you might be doing via C)
91             B if you intend for your CBOR to contain exclusively text.
92              
93             Think of this option as: “All my strings are decoded.”
94              
95             (Perl internals note: if !SvUTF8, the CBOR will be the UTF8-upgraded
96             version.)
97              
98             =item * C: Treats all strings as octets of UTF-8.
99             Wide characters are thus invalid input. All CBOR strings will be text.
100              
101             This is probably what you want if you forgo character decoding (and encoding),
102             treating all input as octets, B you still intend for your CBOR to
103             contain exclusively text.
104              
105             Think of this option as: “I’ve encoded all my strings as UTF-8.”
106              
107             (Perl internals note: if SvUTF8, the CBOR will be the downgraded version.)
108              
109             =item * C: It’s like C, but outputs CBOR binary
110             instead of text.
111              
112             This is probably what you want if your application is “all binary,
113             all the time”.
114              
115             Think of this option as: “Just the bytes, ma’am.”
116              
117             =back
118              
119             =item * C - EXPERIMENTAL. Encodes all Perl hash keys as CBOR text.
120             If you use this mode then your strings B be properly decoded, or else
121             the output CBOR may mangle your string.
122              
123             For example, this:
124              
125             CBOR::Free::encode( { "\xc3\xa9" => 1 }, text_keys => 1 )
126              
127             … will create a CBOR map with key C<"\xc3\x83\xc2\xa9"> because the key
128             in the hash that was sent to C was not properly decoded.
129              
130             =item * C - A boolean that makes the encoder encode
131             multi-referenced values via L. This allows encoding of shared
132             and circular references. It also incurs a performance penalty.
133              
134             (Take care that any circular references in your application don’t cause
135             memory leaks!)
136              
137             =item * C - A boolean that makes the encoder accept
138             scalar references
139             (rather than reject them) and encode them via
140             L.
141             Most languages don’t use references as Perl does, so this option seems of
142             little use outside all-Perl IPC contexts; it is arguably more useful, then,
143             for general use to have the encoder reject data structures that most other
144             languages cannot represent.
145              
146             =back
147              
148             Notes on mapping Perl to CBOR:
149              
150             =over
151              
152             =item * The internal state of a defined Perl scalar (e.g., whether it’s an
153             integer, float, string, etc.) determines its CBOR encoding.
154              
155             =item * Perl doesn’t currently provide reliable binary/character string types.
156             CBOR::Free, in its default configuration, tries to distinguish anyway by
157             looking at a string’s UTF8 flag: if
158             set, then the string becomes CBOR text; otherwise, it’ll be CBOR binary.
159             That’s not always going to work, though. A trivial example:
160              
161             perl -MCBOR::Free -e'my $str = "abc"; utf8::decode($str); print CBOR::Free::encode($str)'
162              
163             Since C doesn’t set the UTF8 flag unless it “has to”
164             (see L), that function is a no-op in the above.
165              
166             The above I produce a CBOR text string, though, if you use
167             L instead of L:
168              
169             perl -MUnicode::UTF8 -MCBOR::Free -e'print CBOR::Free::encode(Unicode::UTF8::decode_utf8("abc"))'
170              
171             The crucial point, though, is that, because Perl itself doesn’t guarantee
172             the reliable string types that CBOR recognizes, any heuristics we apply
173             to distinguish one from the other are a “best-guess” merely.
174              
175             B If you use the default encoding configuration, whatever
176             consumes your Perl-sourced CBOR B account for the prospect of an
177             incorrectly-typed string.
178              
179             =item * The above applies also to strings vs. numbers: whatever consumes
180             your Perl-sourced CBOR B account for the prospect of numbers that
181             are in CBOR as strings, or vice-versa.
182              
183             =item * Perl hash keys are serialized as strings, either binary or text
184             (following the algorithm described above).
185              
186             =item * L booleans are encoded as CBOR booleans.
187             Perl undef is encoded as CBOR null. (NB: No Perl value encodes as CBOR
188             undefined.)
189              
190             =item * Scalar references (including references to other references) are
191             unhandled by default, which makes them trigger an exception. You can
192             optionally tell CBOR::Free to encode them via the C flag.
193              
194             =item * Via the optional C flag, circular and shared
195             references may be preserved. Without this flag, circular references cause an
196             exception, and other shared references are not preserved.
197              
198             =item * Instances of L are encoded as tagged values.
199              
200             =back
201              
202             An error is thrown on excess recursion or an unrecognized object.
203              
204             =head2 $data = decode( $CBOR )
205              
206             Decodes a data structure from CBOR. Errors are thrown to indicate
207             invalid CBOR. A warning is thrown if $CBOR is longer than is needed
208             for $data.
209              
210             Notes on mapping CBOR to Perl:
211              
212             =over
213              
214             =item * C decodes CBOR text strings as UTF-8-decoded Perl strings.
215             CBOR binary strings become undecoded Perl strings.
216              
217             (See L and L for more
218             character-decoding options.)
219              
220             Notes:
221              
222             =over
223              
224             =item * Invalid UTF-8 in a CBOR text string is considered
225             invalid input and will thus prompt a thrown exception.
226              
227             =item * You can reliably use C to determine if a given Perl
228             string came from CBOR text or binary, but B if you test the scalar as
229             it appears in the newly-decoded data structure itself. Generally Perl code
230             should avoid C, but with CBOR::Free-created strings this limited
231             use case is legitimate and potentially gainful.
232              
233             =back
234              
235             =item * The only map keys that C accepts are integers and strings.
236             An exception is thrown if the decoder finds anything else as a map key.
237             Note that, because Perl does not distinguish between binary and text strings,
238             if two keys of the same map contain the same bytes, Perl will consider these
239             a duplicate key and prefer the latter.
240              
241             =item * CBOR booleans become the corresponding L values.
242             Both CBOR null and undefined become Perl undef.
243              
244             =item * L is interpreted as a scalar reference. This behavior is always
245             active; unlike with the encoder, there is no need to enable it manually.
246              
247             =item * C mode complements the same flag
248             given to the encoder.
249              
250             =item * This function does not interpret any other tags. If you need to
251             decode other tags, look at L. Any unhandled tags that
252             this function sees prompt a warning but are otherwise ignored.
253              
254             =back
255              
256             =head2 $obj = tag( $NUMBER, $DATA )
257              
258             Tags an item for encoding so that its CBOR encoding will preserve the
259             tag number. (Include $obj, not $DATA, in the data structure that
260             C receives.)
261              
262             =head1 BOOLEANS
263              
264             C and C are defined as
265             convenience aliases for the equivalent L functions.
266             (Note that there are no equivalent scalar aliases.)
267              
268             =head1 FRACTIONAL (FLOATING-POINT) NUMBERS
269              
270             Floating-point numbers are encoded in CBOR as IEEE 754 half-, single-,
271             or double-precision. If your Perl is compiled to use anything besides
272             IEEE 754 double-precision to represent floating-point values (e.g.,
273             “long double” or “quadmath” compilation options), you may see rounding
274             errors when converting to/from CBOR. If that’s a problem for you, append
275             an empty string to your floating-point numbers, which will cause CBOR::Free
276             to encode them as strings.
277              
278             =head1 INTEGER LIMITS
279              
280             CBOR handles up to 64-bit positive and negative integers. Most Perls
281             nowadays can handle 64-bit integers, but if yours can’t then you’ll
282             get an exception whenever trying to parse an integer that can’t be
283             represented with 32 bits. This means:
284              
285             =over
286              
287             =item * Anything greater than 0xffff_ffff (4,294,967,295)
288              
289             =item * Anything less than -0x8000_0000 (2,147,483,648)
290              
291             =back
292              
293             Note that even 64-bit Perls can’t parse negatives that are less than
294             -0x8000_0000_0000_0000 (-9,223,372,036,854,775,808); these also prompt an
295             exception since Perl can’t handle them. (It would be possible to load
296             L to handle these; if that’s desirable for you,
297             file a feature request.)
298              
299             =head1 ERROR HANDLING
300              
301             Most errors are represented via instances of subclasses of
302             L, which subclasses L.
303              
304             =head1 SPEED
305              
306             CBOR::Free is pretty snappy. I find that it keeps pace with or
307             surpasses L, L, L, L,
308             and L.
309              
310             It’s also quite light. Its only “heavy” dependency is
311             L, which is only loaded when you actually need it.
312             This keeps memory usage low for when, e.g., you’re using CBOR for
313             IPC between Perl processes and have no need for true booleans.
314              
315             =head1 AUTHOR
316              
317             L (FELIPE)
318              
319             =head1 LICENSE
320              
321             This code is licensed under the same license as Perl itself.
322              
323             =head1 SEE ALSO
324              
325             L is a pure-Perl CBOR library.
326              
327             L is an older CBOR module on CPAN. It’s got more bells and
328             whistles, so check it out if CBOR::Free lacks a feature you’d like.
329             Note that L
330             onward|http://blog.schmorp.de/2015-06-06-stableperl-faq.html>, though,
331             and its GPL license limits its usefulness in
332             commercial L
333             applications.
334              
335             =cut
336              
337             #----------------------------------------------------------------------
338              
339             sub true {
340 1     1 0 507 require Types::Serialiser;
341 1         1488 *true = *Types::Serialiser::true;
342 1         5 goto &true;
343             }
344              
345             sub false {
346 0     0 0 0 require Types::Serialiser;
347 0         0 *false = *Types::Serialiser::false;
348 0         0 goto &false;
349             }
350              
351             sub tag {
352 105     105 1 672 return CBOR::Free::Tagged->new(@_);
353             }
354              
355             #----------------------------------------------------------------------
356              
357             sub _die_recursion {
358 2     2   1450 die CBOR::Free::X->create( 'Recursion', _MAX_RECURSION());
359             }
360              
361             sub _die {
362 220     220   412388 my ($subclass, @args) = @_;
363              
364 220         808 die CBOR::Free::X->create($subclass, @args);
365             }
366              
367             sub _warn_decode_leftover {
368 1     1   1536 my ($count) = @_;
369              
370 1         17 warn "CBOR buffer contained $count excess bytes";
371             }
372              
373             1;