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 blib/lib/CBOR/Free/Tagged.pm »
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.21';
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<text_keys> - EXPERIMENTAL. Encodes all Perl hash keys as CBOR text.
68 If you use this mode then your strings B<must> be properly decoded, or else
69 the output CBOR may mangle your string.
70  
71 For example, this:
72  
73 CBOR::Free::encode( { "\xc3\xa9" => 1 }, text_keys => 1 )
74  
75 … will create a CBOR map with key C<"\xc3\x83\xc2\xa9"> because the key
76 in the hash that was sent to C<encode()> was not properly decoded.
77  
78 =item * C<preserve_references> - A boolean that makes the encoder encode
79 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
80 and circular references. It also incurs a performance penalty.
81  
82 (Take care that any circular references in your application don’t cause
83 memory leaks!)
84  
85 =item * C<scalar_references> - A boolean that makes the encoder accept
86 scalar references
87 (rather than reject them) and encode them via
88 L<CBOR’s “indirection” tag|https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>.
89 Most languages don’t use references as Perl does, so this option seems of
90 little use outside all-Perl IPC contexts; it is arguably more useful, then,
91 for general use to have the encoder reject data structures that most other
92 languages cannot represent.
93  
94 =back
95  
96 Notes on mapping Perl to CBOR:
97  
98 =over
99  
100 =item * The internal state of a defined Perl scalar (e.g., whether it’s an
101 integer, float, string, etc.) determines its CBOR encoding.
102  
103 =item * Perl doesn’t currently provide reliable binary/character string types.
104 CBOR::Free tries to distinguish anyway by looking at a string’s UTF8 flag: if
105 set, then the string becomes CBOR text; otherwise, it’ll be CBOR binary.
106 That’s not always going to work, though. A trivial example:
107  
108 perl -MCBOR::Free -e'my $str = "abc"; utf8::decode($str); print CBOR::Free::encode($str)'
109  
110 Since C<utf8::decode()> doesn’t set the UTF8 flag unless it “has to”
111 (see L<utf8>), that function is a no-op in the above.
112  
113 The above I<will> produce a CBOR text string, though, if you use
114 L<Unicode::UTF8> instead of L<utf8>:
115  
116 perl -MUnicode::UTF8 -MCBOR::Free -e'print CBOR::Free::encode(Unicode::UTF8::decode_utf8("abc"))'
117  
118 The crucial point, though, is that, because Perl itself doesn’t guarantee
119 the reliable string types that CBOR recognizes, any heuristics we apply
120 to distinguish one from the other are a “best-guess” merely.
121  
122 B<IMPORTANT:> Whatever consumes your Perl-sourced CBOR B<MUST> account
123 for the prospect of an incorrectly-typed string.
124  
125 =item * The above applies also to strings vs. numbers: whatever consumes
126 your Perl-sourced CBOR B<MUST> account for the prospect of numbers that
127 are in CBOR as strings, or vice-versa.
128  
129 =item * Perl hash keys are serialized as strings, either binary or text
130 (following the algorithm described above).
131  
132 =item * L<Types::Serialiser> booleans are encoded as CBOR booleans.
133 Perl undef is encoded as CBOR null. (NB: No Perl value encodes as CBOR
134 undefined.)
135  
136 =item * Scalar references (including references to other references) are
137 unhandled by default, which makes them trigger an exception. You can
138 optionally tell CBOR::Free to encode them via the C<scalar_references> flag.
139  
140 =item * Via the optional C<preserve_references> flag, circular and shared
141 references may be preserved. Without this flag, circular references cause an
142 exception, and other shared references are not preserved.
143  
144 =item * Instances of L<CBOR::Free::Tagged> are encoded as tagged values.
145  
146 =back
147  
148 An error is thrown on excess recursion or an unrecognized object.
149  
150 =head2 $data = decode( $CBOR )
151  
152 Decodes a data structure from CBOR. Errors are thrown to indicate
153 invalid CBOR. A warning is thrown if $CBOR is longer than is needed
154 for $data.
155  
156 Notes on mapping CBOR to Perl:
157  
158 =over
159  
160 =item * CBOR text strings become Perl strings with the internal UTF8 flag set.
161 CBOR binary strings become Perl strings I<without> that flag set. This is
162 a mostly-internal distinction in Perl that doesn’t actually constitute
163 separate byte/character string types, but it’s at least something similar.
164  
165 Note that invalid UTF-8 in a CBOR text string is considered
166 invalid input and will thus prompt a thrown exception.
167  
168 =item * The only map keys that C<decode()> accepts are integers and strings.
169 An exception is thrown if the decoder finds anything else as a map key.
170 Note that, because Perl does not distinguish between binary and text strings,
171 if two keys of the same map contain the same bytes, Perl will consider these
172 a duplicate key and prefer the latter.
173  
174 =item * CBOR booleans become the corresponding L<Types::Serialiser> values.
175 Both CBOR null and undefined become Perl undef.
176  
177 =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
178 active; unlike with the encoder, there is no need to enable it manually.
179  
180 =item * C<preserve_references()> mode complements the same flag
181 given to the encoder.
182  
183 =item * This function does not interpret any other tags. If you need to
184 decode other tags, look at L<CBOR::Free::Decoder>. Any unhandled tags that
185 this function sees prompt a warning but are otherwise ignored.
186  
187 =back
188  
189 =head2 $obj = tag( $NUMBER, $DATA )
190  
191 Tags an item for encoding so that its CBOR encoding will preserve the
192 tag number. (Include $obj, not $DATA, in the data structure that
193 C<encode()> receives.)
194  
195 =head1 BOOLEANS
196  
197 C<CBOR::Free::true()> and C<CBOR::Free::false()> are defined as
198 convenience aliases for the equivalent L<Types::Serialiser> functions.
199 (Note that there are no equivalent scalar aliases.)
200  
201 =head1 FRACTIONAL (FLOATING-POINT) NUMBERS
202  
203 Floating-point numbers are encoded in CBOR as IEEE 754 half-, single-,
204 or double-precision. If your Perl is compiled to use anything besides
205 IEEE 754 double-precision to represent floating-point values (e.g.,
206 “long double” or “quadmath” compilation options), you may see rounding
207 errors when converting to/from CBOR. If that’s a problem for you, append
208 an empty string to your floating-point numbers, which will cause CBOR::Free
209 to encode them as strings.
210  
211 =head1 INTEGER LIMITS
212  
213 CBOR handles up to 64-bit positive and negative integers. Most Perls
214 nowadays can handle 64-bit integers, but if yours can’t then you’ll
215 get an exception whenever trying to parse an integer that can’t be
216 represented with 32 bits. This means:
217  
218 =over
219  
220 =item * Anything greater than 0xffff_ffff (4,294,967,295)
221  
222 =item * Anything less than -0x8000_0000 (2,147,483,648)
223  
224 =back
225  
226 Note that even 64-bit Perls can’t parse negatives that are less than
227 -0x8000_0000_0000_0000 (-9,223,372,036,854,775,808); these also prompt an
228 exception since Perl can’t handle them. (It would be possible to load
229 L<Math::BigInt> to handle these; if that’s desirable for you,
230 file a feature request.)
231  
232 =head1 ERROR HANDLING
233  
234 Most errors are represented via instances of subclasses of
235 L<CBOR::Free::X>, which subclasses L<X::Tiny::Base>.
236  
237 =head1 SPEED
238  
239 CBOR::Free is pretty snappy. I find that it keeps pace with or
240 surpasses L<CBOR::XS>, L<Cpanel::JSON::XS>, L<JSON::XS>, L<Sereal>,
241 and L<Data::MessagePack>.
242  
243 It’s also quite light. Its only “heavy” dependency is
244 L<Types::Serialiser>, which is only loaded when you actually need it.
245 This keeps memory usage low for when, e.g., you’re using CBOR for
246 IPC between Perl processes and have no need for true booleans.
247  
248 =head1 AUTHOR
249  
250 L<Gasper Software Consulting|http://gaspersoftware.com> (FELIPE)
251  
252 =head1 LICENSE
253  
254 This code is licensed under the same license as Perl itself.
255  
256 =head1 SEE ALSO
257  
258 L<CBOR::PP> is a pure-Perl CBOR library.
259  
260 L<CBOR::XS> is an older CBOR module on CPAN. It’s got more bells and
261 whistles, so check it out if CBOR::Free lacks a feature you’d like.
262 Note that L<its maintainer has abandoned support for Perl versions from 5.22
263 onward|http://blog.schmorp.de/2015-06-06-stableperl-faq.html>, though,
264 and its GPL license limits its usefulness in
265 commercial L<perlcc|https://metacpan.org/pod/distribution/B-C/script/perlcc.PL>
266 applications.
267  
268 =cut
269  
270 #----------------------------------------------------------------------
271  
272 sub true {
273 2 ▶ require Types::Serialiser;
Subroutine true: called CC=1 SCAR=0.0
Pod undocumented
274 2 *true = *Types::Serialiser::true;
275 2 goto &true;
276 }
277  
278 sub false {
279 1 ▶ require Types::Serialiser;
Subroutine false: called CC=1 SCAR=0.0
Pod undocumented
280 1 *false = *Types::Serialiser::false;
281 1 goto &false;
282 }
283  
284 sub tag {
285 104 ▶ return CBOR::Free::Tagged->new(@_);
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
286 }
287  
288 #----------------------------------------------------------------------
289  
290 sub _die_recursion {
291 2 ▶ die CBOR::Free::X->create( 'Recursion', _MAX_RECURSION());
Subroutine _die_recursion: called CC=1 SCAR=0.0
292 }
293  
294 sub _die {
295 185 ▶ my ($subclass, @args) = @_;
Subroutine _die: called CC=1 SCAR=0.0
296  
297 185 die CBOR::Free::X->create($subclass, @args);
298 }
299  
300 sub _warn_decode_leftover {
301 1 ▶ my ($count) = @_;
Subroutine _warn_decode_leftover: called CC=1 SCAR=0.0
302  
303 1 warn "CBOR buffer contained $count excess bytes";
304 }
305  
306 21 1;
« cbor_free_boolean.c ↑ Summary blib/lib/CBOR/Free/Tagged.pm »