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   1680096 use strict;
  26         222  
  26         843  
4 26     26   143 use warnings;
  26         46  
  26         689  
5              
6 26     26   11030 use CBOR::Free::X;
  26         75  
  26         795  
7 26     26   10642 use CBOR::Free::Tagged;
  26         73  
  26         980  
8              
9             our ($VERSION);
10              
11 26     26   169 use XSLoader ();
  26         53  
  26         732  
12              
13             BEGIN {
14 26     26   78 $VERSION = '0.31_01';
15 26         20931 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 is (currently) the only way to output text and binary strings in a
82             single CBOR document. Unfortunately, because Perl itself doesn’t reliably
83             distinguish between text and binary strings, neither can CBOR::Free. If you
84             want to try, though:
85              
86             =over
87              
88             =item * Be sure to use character-decoding logic that always
89             sets the string’s UTF8 flag, even if the input is plain ASCII.
90             (As of this writing, L and L work this way.)
91              
92             =item * Whatever consumes your Perl-sourced CBOR should probably accept
93             “mis-typed” strings.
94              
95             =back
96              
97             =item * C: Treats all strings as unencoded characters.
98             All CBOR strings will be text.
99              
100             This is probably what you want if you
101             follow the receive-decode-process-encode-output workflow that
102             L recommends (which you might be doing via C)
103             B if you intend for your CBOR to contain exclusively text.
104              
105             Think of this option as: “All my strings are decoded.”
106              
107             (Perl internals note: if !SvUTF8, the CBOR will be the UTF8-upgraded
108             version.)
109              
110             =item * C: Treats all strings as octets of UTF-8.
111             Wide characters (i.e., code points above 255) are thus invalid input.
112             All CBOR strings will be text.
113              
114             This is probably what you want if you forgo character decoding (and encoding),
115             treating all input as octets, B you still intend for your CBOR to
116             contain exclusively text.
117              
118             Think of this option as: “I’ve encoded all my strings as UTF-8.”
119              
120             (Perl internals note: if SvUTF8, the CBOR will be the downgraded version.)
121              
122             =item * C: Like C, but outputs CBOR binary
123             instead of text.
124              
125             This is probably what you want if your application is “all binary,
126             all the time”.
127              
128             Think of this option as: “Just the bytes, ma’am.”
129              
130             =back
131              
132             =item * C - A boolean that makes the encoder encode
133             multi-referenced values via L. This allows encoding of shared
134             and circular references. It also incurs a performance penalty.
135              
136             (Take care that any circular references in your application don’t cause
137             memory leaks!)
138              
139             =item * C - A boolean that makes the encoder accept
140             scalar references
141             (rather than reject them) and encode them via
142             L.
143             Most languages don’t use references as Perl does, so this option seems of
144             little use outside all-Perl IPC contexts; it is arguably more useful, then,
145             for general use to have the encoder reject data structures that most other
146             languages cannot represent.
147              
148             =back
149              
150             Notes on mapping Perl to CBOR:
151              
152             =over
153              
154             =item * The internal state of a Perl scalar (e.g., whether it’s an
155             integer, float, string, etc.) determines its CBOR encoding.
156              
157             =item * Perl doesn’t currently provide reliable binary/character string types.
158             The various C options (described above) provide ways to
159             deal with this problem.
160              
161             =item * The above applies also to strings vs. numbers: whatever consumes
162             your Perl-sourced CBOR B account for the prospect of numbers that
163             are in CBOR as strings, or vice-versa.
164              
165             =item * Perl hash keys are serialized as strings, either binary or text
166             (according to the C).
167              
168             =item * L booleans are encoded as CBOR booleans.
169             Perl undef is encoded as CBOR null. (NB: No Perl value encodes as CBOR
170             undefined.)
171              
172             =item * Scalar references (including references to other references) are
173             unhandled by default, which makes them trigger an exception. You can
174             optionally tell CBOR::Free to encode them via the C flag.
175              
176             =item * Via the optional C flag, circular and shared
177             references may be preserved. Without this flag, circular references cause an
178             exception, and other shared references are not preserved.
179              
180             =item * Instances of L are encoded as tagged values.
181              
182             =back
183              
184             An error is thrown on excess recursion or an unrecognized object.
185              
186             =head2 $data = decode( $CBOR )
187              
188             Decodes a data structure from CBOR. Errors are thrown to indicate
189             invalid CBOR. A warning is thrown if $CBOR is longer than is needed
190             for $data.
191              
192             Notes on mapping CBOR to Perl:
193              
194             =over
195              
196             =item * C decodes CBOR text strings as UTF-8-decoded Perl strings.
197             CBOR binary strings become undecoded Perl strings.
198              
199             (See L and L for more
200             character-decoding options.)
201              
202             Notes:
203              
204             =over
205              
206             =item * Invalid UTF-8 in a CBOR text string is usually considered
207             invalid input and will thus prompt a thrown exception. (See
208             L and L if you want
209             to tolerate invalid UTF-8.)
210              
211             =item * You can reliably use C to determine if a given Perl
212             string came from CBOR text or binary, but B if you test the scalar as
213             it appears in the newly-decoded data structure itself. Generally Perl code
214             should avoid C, but with CBOR::Free-created strings this limited
215             use case is legitimate and potentially gainful.
216              
217             =back
218              
219             =item * The only map keys that C accepts are integers and strings.
220             An exception is thrown if the decoder finds anything else as a map key.
221             Note that, because Perl does not distinguish between binary and text strings,
222             if two keys of the same map contain the same bytes, Perl will consider these
223             a duplicate key and prefer the latter.
224              
225             =item * CBOR booleans become the corresponding L values.
226             Both CBOR null and undefined become Perl undef.
227              
228             =item * L is interpreted as a scalar reference. This behavior is always
229             active; unlike with the encoder, there is no need to enable it manually.
230              
231             =item * C mode complements the same flag
232             given to the encoder.
233              
234             =item * This function does not interpret any other tags. If you need to
235             decode other tags, look at L. Any unhandled tags that
236             this function sees prompt a warning but are otherwise ignored.
237              
238             =back
239              
240             =head2 $obj = tag( $NUMBER, $DATA )
241              
242             Tags an item for encoding so that its CBOR encoding will preserve the
243             tag number. (Include $obj, not $DATA, in the data structure that
244             C receives.)
245              
246             =head1 BOOLEANS
247              
248             C and C are defined as
249             convenience aliases for the equivalent L functions.
250             (Note that there are no equivalent scalar aliases.)
251              
252             =head1 FRACTIONAL (FLOATING-POINT) NUMBERS
253              
254             Floating-point numbers are encoded in CBOR as IEEE 754 half-, single-,
255             or double-precision. If your Perl is compiled to use anything besides
256             IEEE 754 double-precision to represent floating-point values (e.g.,
257             “long double” or “quadmath” compilation options), you may see rounding
258             errors when converting to/from CBOR. If that’s a problem for you, append
259             an empty string to your floating-point numbers, which will cause CBOR::Free
260             to encode them as strings.
261              
262             =head1 INTEGER LIMITS
263              
264             CBOR handles up to 64-bit positive and negative integers. Most Perls
265             nowadays can handle 64-bit integers, but if yours can’t then you’ll
266             get an exception whenever trying to parse an integer that can’t be
267             represented with 32 bits. This means:
268              
269             =over
270              
271             =item * Anything greater than 0xffff_ffff (4,294,967,295)
272              
273             =item * Anything less than -0x8000_0000 (2,147,483,648)
274              
275             =back
276              
277             Note that even 64-bit Perls can’t parse negatives that are less than
278             -0x8000_0000_0000_0000 (-9,223,372,036,854,775,808); these also prompt an
279             exception since Perl can’t handle them. (It would be possible to load
280             L to handle these; if that’s desirable for you,
281             file a feature request.)
282              
283             =head1 ERROR HANDLING
284              
285             Most errors are represented via instances of subclasses of
286             L, which subclasses L.
287              
288             =head1 SPEED
289              
290             CBOR::Free is pretty snappy. I find that it keeps pace with or
291             surpasses L, L, L, L,
292             and L.
293              
294             It’s also quite light. Its only “heavy” dependency is
295             L, which is only loaded when you actually need it.
296             This keeps memory usage low for when, e.g., you’re using CBOR for
297             IPC between Perl processes and have no need for true booleans.
298              
299             =head1 AUTHOR
300              
301             L (FELIPE)
302              
303             =head1 LICENSE
304              
305             This code is licensed under the same license as Perl itself.
306              
307             =head1 SEE ALSO
308              
309             L is a pure-Perl CBOR library.
310              
311             L is an older CBOR module on CPAN. It’s got more bells and
312             whistles, so check it out if CBOR::Free lacks a feature you’d like.
313             Note that L
314             onward|http://blog.schmorp.de/2015-06-06-stableperl-faq.html>, though,
315             and its GPL license limits its usefulness in
316             commercial L
317             applications.
318              
319             =cut
320              
321             #----------------------------------------------------------------------
322              
323             sub true {
324 1     1 0 496 require Types::Serialiser;
325 1         1451 *true = *Types::Serialiser::true;
326 1         5 goto &true;
327             }
328              
329             sub false {
330 0     0 0 0 require Types::Serialiser;
331 0         0 *false = *Types::Serialiser::false;
332 0         0 goto &false;
333             }
334              
335             sub tag {
336 105     105 1 696 return CBOR::Free::Tagged->new(@_);
337             }
338              
339             #----------------------------------------------------------------------
340              
341             sub _die_recursion {
342 2     2   1457 die CBOR::Free::X->create( 'Recursion', _MAX_RECURSION());
343             }
344              
345             sub _die {
346 220     220   416212 my ($subclass, @args) = @_;
347              
348 220         865 die CBOR::Free::X->create($subclass, @args);
349             }
350              
351             sub _warn_decode_leftover {
352 1     1   1601 my ($count) = @_;
353              
354 1         18 warn "CBOR buffer contained $count excess bytes";
355             }
356              
357             1;