blib/lib/CBOR/Free.pm

filter: Statementstmt 100% 31 / 31 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 33.3% 1 / 3 totaltotal 95.6% 44 / 46 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« cbor_free_boolean.c ↑ Summary cbor_free_encode.c »
1 package CBOR::Free;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use CBOR::Free::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use CBOR::Free::Tagged;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 21 our ($VERSION);
10  
11 21 ▶use XSLoader ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 BEGIN {
14 21 ▶ $VERSION = '0.15';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 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<CBOR::Free::Decoder> for an object-oriented interface
35 to the decoder.
36  
37 =head1 DESCRIPTION
38  
39 This library implements L<CBOR|https://tools.ietf.org/html/rfc7049>
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<CBOR::Free::Tagged> instances,
56 L<Types::Serialiser> 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<canonical> - A boolean that makes the encoder output
65 CBOR in L<canonical form|https://tools.ietf.org/html/rfc7049#section-3.9>.
66  
67 =item * C<preserve_references> - A boolean that makes the encoder encode
68 multi-referenced values via L<CBOR’s “shared references” tags|https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>. This allows encoding of shared
69 and circular references. It also incurs a performance penalty.
70  
71 (Take care that any circular references in your application don’t cause
72 memory leaks!)
73  
74 =item * C<scalar_references> - A boolean that makes the encoder accept
75 scalar references
76 (rather than reject them) and encode them via
77 L<CBOR’s “indirection” tag|https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>.
78 Most languages don’t use references as Perl does, so this option seems of
79 little use outside all-Perl IPC contexts; it is arguably more useful, then,
80 for general use to have the encoder reject data structures that most other
81 languages cannot represent.
82  
83 =back
84  
85 Notes on mapping Perl to CBOR:
86  
87 =over
88  
89 =item * The internal state of a defined Perl scalar (e.g., whether it’s an
90 integer, float, byte string, or character string) determines its CBOR
91 encoding.
92  
93 =item * Perl hash keys become CBOR binary strings.
94  
95 =item * L<Types::Serialiser> booleans are encoded as CBOR booleans.
96 Perl undef is encoded as CBOR null. (NB: No Perl value encodes as CBOR
97 undefined.)
98  
99 =item * Scalar references (including references to other references) are
100 unhandled by default, which makes them trigger an exception. You can
101 optionally tell CBOR::Free to encode them via the C<scalar_references> flag.
102  
103 =item * Via the optional C<preserve_references> flag, circular and shared
104 references may be preserved. Without this flag, circular references cause an
105 exception, and other shared references are not preserved.
106  
107 =item * Instances of L<CBOR::Free::Tagged> are encoded as tagged values.
108  
109 =back
110  
111 An error is thrown on excess recursion or an unrecognized object.
112  
113 =head2 $data = decode( $CBOR )
114  
115 Decodes a data structure from CBOR. Errors are thrown to indicate
116 invalid CBOR. A warning is thrown if $CBOR is longer than is needed
117 for $data.
118  
119 Notes on mapping CBOR to Perl:
120  
121 =over
122  
123 =item * CBOR text strings become Perl character strings. CBOR binary strings
124 become Perl byte strings. (This may become configurable later.)
125  
126 Note that invalid UTF-8 in a CBOR text string is considered
127 invalid input and will thus prompt a thrown exception.
128  
129 =item * The only map keys that C<decode()> accepts are integers and strings.
130 An exception is thrown if the decoder finds anything else as a map key.
131  
132 =item * CBOR booleans become the corresponding L<Types::Serialiser> values.
133 Both CBOR null and undefined become Perl undef.
134  
135 =item * L<CBOR’s “indirection” tag|https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml> is interpreted as a scalar reference. This behavior is always
136 active; unlike with the encoder, there is no need to enable it manually.
137  
138 =item * C<preserve_references()> mode complements the same flag
139 given to the encoder.
140  
141 =item * This function does not interpret any other tags. If you need to
142 decode other tags, look at L<CBOR::Free::Decoder>. Any unhandled tags that
143 this function sees prompt a warning but are otherwise ignored.
144  
145 =back
146  
147 =head2 $obj = tag( $NUMBER, $DATA )
148  
149 Tags an item for encoding so that its CBOR encoding will preserve the
150 tag number. (Include $obj, not $DATA, in the data structure that
151 C<encode()> receives.)
152  
153 =head1 BOOLEANS
154  
155 C<CBOR::Free::true()> and C<CBOR::Free::false()> are defined as
156 convenience aliases for the equivalent L<Types::Serialiser> functions.
157 (Note that there are no equivalent scalar aliases.)
158  
159 =head1 FRACTIONAL (FLOATING-POINT) NUMBERS
160  
161 Floating-point numbers are encoded in CBOR as IEEE 754 half-, single-,
162 or double-precision. If your Perl is compiled to use anything besides
163 IEEE 754 double-precision to represent floating-point values (e.g.,
164 “long double” or “quadmath” compilation options), you may see rounding
165 errors when converting to/from CBOR. If that’s a problem for you, append
166 an empty string to your floating-point numbers, which will cause CBOR::Free
167 to encode them as strings.
168  
169 =head1 INTEGER LIMITS
170  
171 CBOR handles up to 64-bit positive and negative integers. Most Perls
172 nowadays can handle 64-bit integers, but if yours can’t then you’ll
173 get an exception whenever trying to parse an integer that can’t be
174 represented with 32 bits. This means:
175  
176 =over
177  
178 =item * Anything greater than 0xffff_ffff (4,294,967,295)
179  
180 =item * Anything less than -0x8000_0000 (2,147,483,648)
181  
182 =back
183  
184 Note that even 64-bit Perls can’t parse negatives that are less than
185 -0x8000_0000_0000_0000 (-9,223,372,036,854,775,808); these also prompt an
186 exception since Perl can’t handle them. (It would be possible to load
187 L<Math::BigInt> to handle these; if that’s desirable for you,
188 file a feature request.)
189  
190 =head1 ERROR HANDLING
191  
192 Most errors are represented via instances of subclasses of
193 L<CBOR::Free::X>, which subclasses L<X::Tiny::Base>.
194  
195 =head1 SPEED
196  
197 CBOR::Free is pretty snappy. I find that it keeps pace with or
198 surpasses L<CBOR::XS>, L<Cpanel::JSON::XS>, L<JSON::XS>, L<Sereal>,
199 and L<Data::MessagePack>.
200  
201 It’s also quite light. Its only “heavy” dependency is
202 L<Types::Serialiser>, which is only loaded when you actually need it.
203 This keeps memory usage low for when, e.g., you’re using CBOR for
204 IPC between Perl processes and have no need for true booleans.
205  
206 =head1 TODO
207  
208 Some of the Windows tests fail regularly in older Perl versions.
209 It would be nice to have those fixed.
210  
211 =head1 AUTHOR
212  
213 L<Gasper Software Consulting|http://gaspersoftware.com> (FELIPE)
214  
215 =head1 LICENSE
216  
217 This code is licensed under the same license as Perl itself.
218  
219 =head1 SEE ALSO
220  
221 L<CBOR::PP> is a pure-Perl CBOR library.
222  
223 L<CBOR::XS> is an older CBOR module on CPAN. It’s got more bells and
224 whistles, so check it out if CBOR::Free lacks a feature you’d like.
225 Note that L<its maintainer has abandoned support for Perl versions from 5.22
226 onward|http://blog.schmorp.de/2015-06-06-stableperl-faq.html>, though,
227 and its GPL license limits its usefulness in
228 commercial L<perlcc|https://metacpan.org/pod/distribution/B-C/script/perlcc.PL>
229 applications.
230  
231 =cut
232  
233 #----------------------------------------------------------------------
234  
235 sub true {
236 2 ▶ require Types::Serialiser;
Subroutine true: called CC=1 SCAR=0.0
Pod undocumented
237 2 *true = *Types::Serialiser::true;
238 2 goto &true;
239 }
240  
241 sub false {
242 1 ▶ require Types::Serialiser;
Subroutine false: called CC=1 SCAR=0.0
Pod undocumented
243 1 *false = *Types::Serialiser::false;
244 1 goto &false;
245 }
246  
247 sub tag {
248 104 ▶ return CBOR::Free::Tagged->new(@_);
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
249 }
250  
251 #----------------------------------------------------------------------
252  
253 sub _die_recursion {
254 2 ▶ die CBOR::Free::X->create( 'Recursion', _MAX_RECURSION());
Subroutine _die_recursion: called CC=1 SCAR=0.0
255 }
256  
257 sub _die {
258 184 ▶ my ($subclass, @args) = @_;
Subroutine _die: called CC=1 SCAR=0.0
259  
260 184 die CBOR::Free::X->create($subclass, @args);
261 }
262  
263 sub _warn_decode_leftover {
264 1 ▶ my ($count) = @_;
Subroutine _warn_decode_leftover: called CC=1 SCAR=0.0
265  
266 1 warn "CBOR buffer contained $count excess bytes";
267 }
268  
269 21 1;
« cbor_free_boolean.c ↑ Summary cbor_free_encode.c »