… | |
… | |
32 | |
32 | |
33 | package CBOR::XS; |
33 | package CBOR::XS; |
34 | |
34 | |
35 | use common::sense; |
35 | use common::sense; |
36 | |
36 | |
37 | our $VERSION = 0.01; |
37 | our $VERSION = 0.02; |
38 | our @ISA = qw(Exporter); |
38 | our @ISA = qw(Exporter); |
39 | |
39 | |
40 | our @EXPORT = qw(encode_cbor decode_cbor); |
40 | our @EXPORT = qw(encode_cbor decode_cbor); |
41 | |
41 | |
42 | use Exporter; |
42 | use Exporter; |
43 | use XSLoader; |
43 | use XSLoader; |
|
|
44 | |
|
|
45 | our $MAGIC = "\xd9\xd9\xf7"; |
44 | |
46 | |
45 | =head1 FUNCTIONAL INTERFACE |
47 | =head1 FUNCTIONAL INTERFACE |
46 | |
48 | |
47 | The following convenience methods are provided by this module. They are |
49 | The following convenience methods are provided by this module. They are |
48 | exported by default: |
50 | exported by default: |
… | |
… | |
161 | |
163 | |
162 | =head2 CBOR -> PERL |
164 | =head2 CBOR -> PERL |
163 | |
165 | |
164 | =over 4 |
166 | =over 4 |
165 | |
167 | |
|
|
168 | =item integers |
|
|
169 | |
|
|
170 | CBOR integers become (numeric) perl scalars. On perls without 64 bit |
|
|
171 | support, 64 bit integers will be truncated or otherwise corrupted. |
|
|
172 | |
|
|
173 | =item byte strings |
|
|
174 | |
|
|
175 | Byte strings will become octet strings in Perl (the byte values 0..255 |
|
|
176 | will simply become characters of the same value in Perl). |
|
|
177 | |
|
|
178 | =item UTF-8 strings |
|
|
179 | |
|
|
180 | UTF-8 strings in CBOR will be decoded, i.e. the UTF-8 octets will be |
|
|
181 | decoded into proper Unicode code points. At the moment, the validity of |
|
|
182 | the UTF-8 octets will not be validated - corrupt input will result in |
|
|
183 | corrupted Perl strings. |
|
|
184 | |
|
|
185 | =item arrays, maps |
|
|
186 | |
|
|
187 | CBOR arrays and CBOR maps will be converted into references to a Perl |
|
|
188 | array or hash, respectively. The keys of the map will be stringified |
|
|
189 | during this process. |
|
|
190 | |
166 | =item True, False |
191 | =item true, false |
167 | |
192 | |
168 | These CBOR values become C<CBOR::XS::true> and C<CBOR::XS::false>, |
193 | These CBOR values become C<CBOR::XS::true> and C<CBOR::XS::false>, |
169 | respectively. They are overloaded to act almost exactly like the numbers |
194 | respectively. They are overloaded to act almost exactly like the numbers |
170 | C<1> and C<0>. You can check whether a scalar is a CBOR boolean by using |
195 | C<1> and C<0>. You can check whether a scalar is a CBOR boolean by using |
171 | the C<CBOR::XS::is_bool> function. |
196 | the C<CBOR::XS::is_bool> function. |
172 | |
197 | |
173 | =item null |
198 | =item null, undefined |
174 | |
199 | |
175 | A CBOR Null value becomes C<undef> in Perl. |
200 | CBOR null and undefined values becomes C<undef> in Perl (in the future, |
|
|
201 | Undefined may raise an exception or something else). |
|
|
202 | |
|
|
203 | =item tags |
|
|
204 | |
|
|
205 | Tagged items consists of a numeric tag and another CBOR value. The tag |
|
|
206 | 55799 is ignored (this tag implements the magic header). |
|
|
207 | |
|
|
208 | All other tags are currently converted into a L<CBOR::XS::Tagged> object, |
|
|
209 | which is simply a blessed array reference consistsing of the numeric tag |
|
|
210 | value followed by the (decoded) BOR value. |
|
|
211 | |
|
|
212 | =item anything else |
|
|
213 | |
|
|
214 | Anything else (e.g. unsupported simple values) will raise a decoding |
|
|
215 | error. |
176 | |
216 | |
177 | =back |
217 | =back |
178 | |
218 | |
179 | |
219 | |
180 | =head2 PERL -> CBOR |
220 | =head2 PERL -> CBOR |
… | |
… | |
185 | |
225 | |
186 | =over 4 |
226 | =over 4 |
187 | |
227 | |
188 | =item hash references |
228 | =item hash references |
189 | |
229 | |
190 | Perl hash references become CBOR maps. As there is no inherent ordering |
230 | Perl hash references become CBOR maps. As there is no inherent ordering in |
191 | in hash keys (or CBOR maps), they will usually be encoded in a |
231 | hash keys (or CBOR maps), they will usually be encoded in a pseudo-random |
192 | pseudo-random order. |
232 | order. |
|
|
233 | |
|
|
234 | Currently, tied hashes will use the indefinite-length format, while normal |
|
|
235 | hashes will use the fixed-length format. |
193 | |
236 | |
194 | =item array references |
237 | =item array references |
195 | |
238 | |
196 | Perl array references become CBOR arrays. |
239 | Perl array references become fixed-length CBOR arrays. |
197 | |
240 | |
198 | =item other references |
241 | =item other references |
199 | |
242 | |
200 | Other unblessed references are generally not allowed and will cause an |
243 | Other unblessed references are generally not allowed and will cause an |
201 | exception to be thrown, except for references to the integers C<0> and |
244 | exception to be thrown, except for references to the integers C<0> and |
202 | C<1>, which get turned into C<False> and C<True> in CBOR. |
245 | C<1>, which get turned into false and true in CBOR. |
|
|
246 | |
|
|
247 | =item CBOR::XS::Tagged objects |
|
|
248 | |
|
|
249 | Objects of this type must be arrays consisting of a single C<[tag, value]> |
|
|
250 | pair. The (numerical) tag will be encoded as a CBOR tag, the value will be |
|
|
251 | encoded as appropriate for the value. |
203 | |
252 | |
204 | =item CBOR::XS::true, CBOR::XS::false |
253 | =item CBOR::XS::true, CBOR::XS::false |
205 | |
254 | |
206 | These special values become CBOR True and CBOR False values, |
255 | These special values become CBOR true and CBOR false values, |
207 | respectively. You can also use C<\1> and C<\0> directly if you want. |
256 | respectively. You can also use C<\1> and C<\0> directly if you want. |
208 | |
257 | |
209 | =item blessed objects |
258 | =item blessed objects |
210 | |
259 | |
211 | Blessed objects are not directly representable in CBOR. TODO |
260 | Other blessed objects currently need to have a C<TO_CBOR> method. It |
212 | See the |
261 | will be called on every object that is being serialised, and must return |
213 | C<allow_blessed> and C<convert_blessed> methods on various options on |
262 | something that can be encoded in CBOR. |
214 | how to deal with this: basically, you can choose between throwing an |
|
|
215 | exception, encoding the reference as if it weren't blessed, or provide |
|
|
216 | your own serialiser method. |
|
|
217 | |
263 | |
218 | =item simple scalars |
264 | =item simple scalars |
219 | |
265 | |
220 | TODO |
266 | TODO |
221 | Simple Perl scalars (any scalar that is not a reference) are the most |
267 | Simple Perl scalars (any scalar that is not a reference) are the most |
222 | difficult objects to encode: CBOR::XS will encode undefined scalars as |
268 | difficult objects to encode: CBOR::XS will encode undefined scalars as |
223 | CBOR C<Null> values, scalars that have last been used in a string context |
269 | CBOR null values, scalars that have last been used in a string context |
224 | before encoding as CBOR strings, and anything else as number value: |
270 | before encoding as CBOR strings, and anything else as number value: |
225 | |
271 | |
226 | # dump as number |
272 | # dump as number |
227 | encode_cbor [2] # yields [2] |
273 | encode_cbor [2] # yields [2] |
228 | encode_cbor [-3.0e17] # yields [-3e+17] |
274 | encode_cbor [-3.0e17] # yields [-3e+17] |
… | |
… | |
250 | |
296 | |
251 | You can not currently force the type in other, less obscure, ways. Tell me |
297 | You can not currently force the type in other, less obscure, ways. Tell me |
252 | if you need this capability (but don't forget to explain why it's needed |
298 | if you need this capability (but don't forget to explain why it's needed |
253 | :). |
299 | :). |
254 | |
300 | |
255 | Note that numerical precision has the same meaning as under Perl (so |
301 | Perl values that seem to be integers generally use the shortest possible |
256 | binary to decimal conversion follows the same rules as in Perl, which |
302 | representation. Floating-point values will use either the IEEE single |
257 | can differ to other languages). Also, your perl interpreter might expose |
303 | format if possible without loss of precision, otherwise the IEEE double |
258 | extensions to the floating point numbers of your platform, such as |
304 | format will be used. Perls that use formats other than IEEE double to |
259 | infinities or NaN's - these cannot be represented in CBOR, and it is an |
305 | represent numerical values are supported, but might suffer loss of |
260 | error to pass those in. |
306 | precision. |
261 | |
307 | |
262 | =back |
308 | =back |
263 | |
309 | |
264 | |
310 | |
|
|
311 | =head2 MAGIC HEADER |
|
|
312 | |
|
|
313 | There is no way to distinguish CBOR from other formats |
|
|
314 | programmatically. To make it easier to distinguish CBOR from other |
|
|
315 | formats, the CBOR specification has a special "magic string" that can be |
|
|
316 | prepended to any CBOR string without changing it's meaning. |
|
|
317 | |
|
|
318 | This string is available as C<$CBOR::XS::MAGIC>. This module does not |
|
|
319 | prepend this string tot he CBOR data it generates, but it will ignroe it |
|
|
320 | if present, so users can prepend this string as a "file type" indicator as |
|
|
321 | required. |
|
|
322 | |
|
|
323 | |
265 | =head2 CBOR and JSON |
324 | =head2 CBOR and JSON |
266 | |
325 | |
267 | TODO |
326 | CBOR is supposed to implement a superset of the JSON data model, and is, |
|
|
327 | with some coercion, able to represent all JSON texts (something that other |
|
|
328 | "binary JSON" formats such as BSON generally do not support). |
|
|
329 | |
|
|
330 | CBOR implements some extra hints and support for JSON interoperability, |
|
|
331 | and the spec offers further guidance for conversion between CBOR and |
|
|
332 | JSON. None of this is currently implemented in CBOR, and the guidelines |
|
|
333 | in the spec do not result in correct round-tripping of data. If JSON |
|
|
334 | interoperability is improved in the future, then the goal will be to |
|
|
335 | ensure that decoded JSON data will round-trip encoding and decoding to |
|
|
336 | CBOR intact. |
268 | |
337 | |
269 | |
338 | |
270 | =head1 SECURITY CONSIDERATIONS |
339 | =head1 SECURITY CONSIDERATIONS |
271 | |
340 | |
272 | When you are using CBOR in a protocol, talking to untrusted potentially |
341 | When you are using CBOR in a protocol, talking to untrusted potentially |