| 1 | package CBOR::Free; | ||
| 2 | |||
| 3 | 27 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 27 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 27 | ▶ | use CBOR::Free::X; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 27 | ▶ | use CBOR::Free::Tagged; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 27 | our ($VERSION); | |
| 10 | |||
| 11 | 27 | ▶ | use XSLoader (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 27 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 27 | $VERSION = '0.36'; | |
| 15 | 27 | 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 | =begin html | ||
| 40 | |||
| 41 | <a href='https://coveralls.io/github/FGasper/p5-CBOR-Free?branch=master'><img src='https://coveralls.io/repos/github/FGasper/p5-CBOR-Free/badge.svg?branch=master' alt='Coverage Status' /></a> | ||
| 42 | |||
| 43 | =end html | ||
| 44 | |||
| 45 | This library implements L<CBOR|https://tools.ietf.org/html/rfc7049> | ||
| 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<CBOR::Free::Tagged> instances, | ||
| 62 | L<Types::Serialiser> 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<canonical> - A boolean that makes the encoder output | ||
| 71 | CBOR in L<canonical form|https://tools.ietf.org/html/rfc7049#section-3.9>. | ||
| 72 | |||
| 73 | =item * C<string_encode_mode> - 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<sv>: 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<Encode> and L<Unicode::UTF8> 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<encode_text>: 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<perlunitut> recommends (which you might be doing via C<use utf8>) | ||
| 109 | B<AND> 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<as_text>: 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<BUT> 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<as_binary>: Like C<as_text>, 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<preserve_references> - A boolean that makes the encoder encode | ||
| 139 | 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 | ||
| 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<scalar_references> - A boolean that makes the encoder accept | ||
| 146 | scalar references | ||
| 147 | (rather than reject them) and encode them via | ||
| 148 | L<CBOR’s “indirection” tag|https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>. | ||
| 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<string_encode_mode> 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<MUST> 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<string_encode_mode>). | ||
| 173 | |||
| 174 | =item * L<Types::Serialiser> 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<scalar_references> flag. | ||
| 181 | |||
| 182 | =item * Via the optional C<preserve_references> 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<CBOR::Free::Tagged> 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<decode()> decodes CBOR text strings as UTF-8-decoded Perl strings. | ||
| 203 | CBOR binary strings become undecoded Perl strings. | ||
| 204 | |||
| 205 | (See L<CBOR::Free::Decoder> and L<CBOR::Free::SequenceDecoder> 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<CBOR::Free::Decoder> and L<CBOR::Free::SequenceDecoder> if you want | ||
| 215 | to tolerate invalid UTF-8.) | ||
| 216 | |||
| 217 | =item * You can reliably use C<utf8::is_utf8()> to determine if a given Perl | ||
| 218 | string came from CBOR text or binary, but B<ONLY> if you test the scalar as | ||
| 219 | it appears in the newly-decoded data structure itself. Generally Perl code | ||
| 220 | should avoid C<is_utf8()>, 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<decode()> 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. Use C<reject_duplicate_keys()> on | ||
| 230 | L<CBOR::Free::Decoder> to reject map keys that would overwrite an earlier | ||
| 231 | key. | ||
| 232 | |||
| 233 | =item * CBOR booleans become the corresponding L<Types::Serialiser> values. | ||
| 234 | Both CBOR null and undefined become Perl undef. | ||
| 235 | |||
| 236 | =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 | ||
| 237 | active; unlike with the encoder, there is no need to enable it manually. | ||
| 238 | |||
| 239 | =item * C<preserve_references()> mode complements the same flag | ||
| 240 | given to the encoder. | ||
| 241 | |||
| 242 | =item * This function does not interpret any other tags. If you need to | ||
| 243 | decode other tags, look at L<CBOR::Free::Decoder>. Any unhandled tags that | ||
| 244 | this function sees prompt a warning but are otherwise ignored. | ||
| 245 | |||
| 246 | =back | ||
| 247 | |||
| 248 | =head2 $obj = tag( $NUMBER, $DATA ) | ||
| 249 | |||
| 250 | Tags an item for encoding so that its CBOR encoding will preserve the | ||
| 251 | tag number. (Include $obj, not $DATA, in the data structure that | ||
| 252 | C<encode()> receives.) | ||
| 253 | |||
| 254 | =head1 BOOLEANS | ||
| 255 | |||
| 256 | C<CBOR::Free::true()> and C<CBOR::Free::false()> are defined as | ||
| 257 | convenience aliases for the equivalent L<Types::Serialiser> functions. | ||
| 258 | (Note that there are no equivalent scalar aliases.) | ||
| 259 | |||
| 260 | =head1 FRACTIONAL (FLOATING-POINT) NUMBERS | ||
| 261 | |||
| 262 | Floating-point numbers are encoded in CBOR as IEEE 754 half-, single-, | ||
| 263 | or double-precision. If your Perl is compiled to use anything besides | ||
| 264 | IEEE 754 double-precision to represent floating-point values (e.g., | ||
| 265 | “long double” or “quadmath” compilation options), you may see rounding | ||
| 266 | errors when converting to/from CBOR. If that’s a problem for you, append | ||
| 267 | an empty string to your floating-point numbers, which will cause CBOR::Free | ||
| 268 | to encode them as strings. | ||
| 269 | |||
| 270 | =head1 INTEGER LIMITS | ||
| 271 | |||
| 272 | CBOR handles up to 64-bit positive and negative integers. Most Perls | ||
| 273 | nowadays can handle 64-bit integers, but if yours can’t then you’ll | ||
| 274 | get an exception whenever trying to parse an integer that can’t be | ||
| 275 | represented with 32 bits. This means: | ||
| 276 | |||
| 277 | =over | ||
| 278 | |||
| 279 | =item * Anything greater than 0xffff_ffff (4,294,967,295) | ||
| 280 | |||
| 281 | =item * Anything less than -0x8000_0000 (2,147,483,648) | ||
| 282 | |||
| 283 | =back | ||
| 284 | |||
| 285 | Note that even 64-bit Perls can’t parse negatives that are less than | ||
| 286 | -0x8000_0000_0000_0000 (-9,223,372,036,854,775,808); these also prompt an | ||
| 287 | exception since Perl can’t handle them. (It would be possible to load | ||
| 288 | L<Math::BigInt> to handle these; if that’s desirable for you, | ||
| 289 | file a feature request.) | ||
| 290 | |||
| 291 | =head1 ERROR HANDLING | ||
| 292 | |||
| 293 | Most errors are represented via instances of subclasses of | ||
| 294 | L<CBOR::Free::X>, which subclasses L<X::Tiny::Base>. | ||
| 295 | |||
| 296 | =head1 SPEED | ||
| 297 | |||
| 298 | CBOR::Free is pretty snappy. I find that it keeps pace with or | ||
| 299 | surpasses L<CBOR::XS>, L<Cpanel::JSON::XS>, L<JSON::XS>, L<Sereal>, | ||
| 300 | and L<Data::MessagePack>. | ||
| 301 | |||
| 302 | It’s also quite light. Its only “heavy” dependency is | ||
| 303 | L<Types::Serialiser>, which is only loaded when you actually need it. | ||
| 304 | This keeps memory usage low for when, e.g., you’re using CBOR for | ||
| 305 | IPC between Perl processes and have no need for true booleans. | ||
| 306 | |||
| 307 | =head1 AUTHOR | ||
| 308 | |||
| 309 | L<Gasper Software Consulting|http://gaspersoftware.com> (FELIPE) | ||
| 310 | |||
| 311 | =head1 LICENSE | ||
| 312 | |||
| 313 | This code is licensed under the same license as Perl itself. | ||
| 314 | |||
| 315 | =head1 SEE ALSO | ||
| 316 | |||
| 317 | L<CBOR::PP> is a pure-Perl CBOR library. | ||
| 318 | |||
| 319 | L<CBOR::XS> is an older CBOR module on CPAN. It’s got more bells and | ||
| 320 | whistles, so check it out if CBOR::Free lacks a feature you’d like. | ||
| 321 | Note that L<its maintainer has abandoned support for Perl versions from 5.22 | ||
| 322 | onward|http://blog.schmorp.de/2015-06-06-stableperl-faq.html>, though, | ||
| 323 | and its GPL license limits its usefulness in | ||
| 324 | commercial L<perlcc|https://metacpan.org/pod/distribution/B-C/script/perlcc.PL> | ||
| 325 | applications. | ||
| 326 | |||
| 327 | =cut | ||
| 328 | |||
| 329 | #---------------------------------------------------------------------- | ||
| 330 | |||
| 331 | 2 | ▶ | sub true { |
|
Subroutine
true: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 332 | 2 | require Types::Serialiser; | |
| 333 | 2 | *true = *Types::Serialiser::true; | |
| 334 | 2 | goto &true; | |
| 335 | } | ||
| 336 | |||
| 337 | 1 | ▶ | sub false { |
|
Subroutine
false: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 338 | 1 | require Types::Serialiser; | |
| 339 | 1 | *false = *Types::Serialiser::false; | |
| 340 | 1 | goto &false; | |
| 341 | } | ||
| 342 | |||
| 343 | 105 | ▶ | sub tag { |
|
Subroutine
tag: called CC=1 SCAR=0.0
Pod
documented
| |||
| 344 | 105 | return CBOR::Free::Tagged->new(@_); | |
| 345 | } | ||
| 346 | |||
| 347 | #---------------------------------------------------------------------- | ||
| 348 | |||
| 349 | 2 | ▶ | sub _die_recursion { |
|
Subroutine
_die_recursion: called CC=1 SCAR=0.0
| |||
| 350 | 2 | die CBOR::Free::X->create( 'Recursion', _MAX_RECURSION()); | |
| 351 | } | ||
| 352 | |||
| 353 | 228 | ▶ | sub _die { |
|
Subroutine
_die: called CC=1 SCAR=0.0
| |||
| 354 | 228 | my ($subclass, @args) = @_; | |
| 355 | |||
| 356 | 228 | die CBOR::Free::X->create($subclass, @args); | |
| 357 | } | ||
| 358 | |||
| 359 | 1 | ▶ | sub _warn_decode_leftover { |
|
Subroutine
_warn_decode_leftover: called CC=1 SCAR=0.0
| |||
| 360 | 1 | my ($count) = @_; | |
| 361 | |||
| 362 | 1 | warn "CBOR buffer contained $count excess bytes"; | |
| 363 | } | ||
| 364 | |||
| 365 | 27 | 1; | |