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