PP/Encode.pm

filter: Statementstmt 78.3% 58 / 74 Branchbran 58.8% 40 / 68 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 72.1% 119 / 165 CC 39 SCAR 44.6
CC
39
Cov
70%
encode
99.3
BEGIN
1.0
BEGIN
1.0
CRAP 86.2
« PP/Decode.pm ↑ Summary PP/Tagged.pm »
1 package CBOR::PP::Encode;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 CBOR::PP::Decode
11  
12 =head1 SYNOPSIS
13  
14 my $perlvar = CBOR::PP::Decode::decode($binary);
15  
16 =head1 DESCRIPTION
17  
18 This implements a L<CBOR|https://tools.ietf.org/html/rfc7049> encoder
19 in pure Perl.
20  
21 =head1 MAPPING PERL TO CBOR
22  
23 =over
24  
25 =item * Scalars that look like unsigned integers are encoded as such.
26 UTF-8 strings and strings that fit 7-bit ASCII (including floats and
27 negatives) are encoded as text. Any other scalar is encoded as binary.
28  
29 Note that there is no “right way” to determine whether an arbitrary
30 Perl (non-reference) scalar should be encoded as a string or as a number.
31 The above seems a reasonable enough approach.
32  
33 =item * UTF8-flagged strings are encoded as text; others are encoded as
34 binary. This is a “best-guess” merely; Perl’s UTF8 flag doesn’t reliably
35 indicate whether a given string is a text or a byte string.
36  
37 =item * undef, Types::Serialiser::true(), and Types::Serialiser::false()
38 are encoded as null, true, and false, respectively.
39  
40 =item * There is no support for streamed (i.e., indefinite-length)
41 objects.
42  
43 =item * There is no Perl value that maps to CBOR’s undefined value.
44  
45 =back
46  
47 =head1 TODO
48  
49 =over
50  
51 =item * Add canonicalization support.
52  
53 =item * Optimize as may be feasible.
54  
55 =back
56  
57 =head1 AUTHOR
58  
59 L<Gasper Software Consulting|http://gaspersoftware.com> (FELIPE)
60  
61 =head1 LICENSE
62  
63 This code is licensed under the same license as Perl itself.
64  
65 =cut
66  
67 #----------------------------------------------------------------------
68  
69 6 ▶use CBOR::PP::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 6 ▶use CBOR::PP::Tagged;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 #----------------------------------------------------------------------
73  
74 =head1 FUNCTIONS
75  
76 =head2 $obj = tag( $NUMBER, $VALUE )
77  
78 Returns an object that represents a value and its CBOR tag number.
79 For example, to encode a date/time string, you could do:
80  
81 my $tagged = tag(0, '2013-03-21T20:04:00Z')
82  
83 C<encode()> recognizes objects that this function returns and
84 turns them into tagged CBOR values.
85  
86 =cut
87  
88 sub tag {
89 1 ▶ return CBOR::PP::Tagged->new(@_);
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
90 }
91  
92 #----------------------------------------------------------------------
93  
94 =head1 METHODS
95  
96 =head2 $cbor = encode( $VALUE, \%OPTS )
97  
98 Returns a CBOR string that represents the passed $VALUE.
99  
100 For now this is only called as a static method but may eventually
101 be an instance method as well, for example, to define options like
102 canonicalization.
103  
104 =cut
105  
106 6 my ($numkeys);
107  
108 6 our $_depth = 0;
109  
110 # Avoid tripping Perl’s warning:
111 6 ▶use constant _MAX_RECURSION => 98;
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 sub encode {
114  
115 # There’s a lot of ugliness in here for the sake of speed.
116 # For example, ideally each major type would have its own function,
117 # but we realize significant savings by putting everything into
118 # one big function.
119  
120 479 ▶ local $_depth = $_depth + 1;
Subroutine encode: called CC=39 SCAR=46.0
Pod documented
121 479 ▶ die CBOR::PP::X->create('Recursion', sprintf("Refuse to encode() more than %d times at once!", _MAX_RECURSION())) if $_depth > _MAX_RECURSION();
Branch: 1 branch
BranchTrueFalseTotal
if $CBOR::PP::Encode::_depth > 98 2 477 479
122  
123 477 for ($_[0]) {
124 477 ▶ if (!ref) {
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $_) { } 230 247 477
elsif (ref $_ eq 'ARRAY') { } 227 20 247
elsif (ref $_ eq 'HASH') { } 14 6 20
elsif (ref($_)->isa('JSON::PP::Boolean')) { } 5 1 6
elsif (ref($_)->isa('CBOR::PP::Tagged')) { } 1 0 1
125  
126 # undef => null
127 230 ▶ return "\xf6" if !defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 2 228 230
128  
129 # empty string
130 228 ▶ return utf8::is_utf8($_) ? "\x60" : "\x40" if !length;
Branch: 2 branches
BranchTrueFalseTotal
unless length $_ 3 225 228
utf8::is_utf8($_) ? : 0 3 3
131  
132 # unsigned int
133 225 ▶ if (!$_ || (!tr<0-9><>c && 0 != rindex($_, 0, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_ or not tr/0-9//c and 0 != rindex($_, 0, 0)) 158 67 225
Condition100%6/6
not $_ or not tr/0-9//c and 0 != rindex($_, 0, 0)
l!l&&r!l&&!r
620811
not tr/0-9//c and 0 != rindex($_, 0, 0)
!ll&&!rl&&r
652152
MC/DC100%3/3
not $_ or not tr/0-9//c and 0 != rindex($_, 0, 0)
not $_ not tr/0-9//c 0 != rindex($_, 0, 0)
Truth table100%4/4
not(A) or not(B) and C
A= not $_B= not tr/0-9//cC= 0 != rindex($_, 0, 0)
ABCresult
1XX1
0111
00X0
0100
134 158 ▶ return chr $_ if ($_ < 24);
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 24 134 24 158
135  
136 24 ▶ return pack('CC', 0x18, $_) if $_ < 0x100;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 256 12 12 24
137  
138 12 ▶ return pack('Cn', 0x19, $_) if ($_ < 0x10000);
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 65536 2 10 12
139  
140 10 ▶ return pack('CN', 0x1a, $_) if ($_ <= 0xffffffff);
Branch: 1 branch
BranchTrueFalseTotal
if $_ <= 4294967295 4 6 10
141  
142 6 return pack('C Q>', 0x1b, $_);
143 }
144  
145 # negative int
146 # elsif ( 0 == rindex($_, '-', 0) && (substr($_, 1) !~ tr<0-9><>c) ) {
147 # return chr( 0x20 - $_ ) if ($_ > -25);
148 #
149 # return pack( 'CC', 0x38, -$_ ) if $_ >= -0x100;
150 #
151 # return pack( 'Cv', 0x39, -$_ ) if $_ >= -0x10000;
152 #
153 # return pack( 'CV', 0x3a, -$_ ) if $_ >= -0x100000000;
154 #
155 # return pack( 'C Q>', 0x3b, -$_ );
156 # }
157  
158 67 ▶ if (utf8::is_utf8($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($_)) { } 6 61 67
159  
160 # Perl doesn’t seem to have a way to pack() a
161 # a UTF-8 string directly to bytes???
162 6 utf8::encode(my $bytes = $_);
163  
164 6 ▶ return pack('Ca*', 0x60 + length($bytes), $bytes) if (length() < 24);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ < 24 6 0 6
165  
166 0 ▶ return pack('CCa*', 0x78, length($bytes), $bytes) if (length() < 0x100);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ < 256 0 0 0
167  
168 0 ▶ return pack('Cna*', 0x79, length($bytes), $bytes) if (length() < 0x10000);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ < 65536 0 0 0
169  
170 0 ▶ return pack('CNa*', 0x7a, length($bytes), $bytes) if (length() <= 0xffffffff);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ <= 4294967295 0 0 0
171  
172 0 return pack('C Q> a*', 0x7b, length($bytes), $bytes);
173 }
174 else {
175 61 ▶ return pack('Ca*', 0x40 + length, $_) if (length() < 24);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ < 24 61 0 61
176  
177 0 ▶ return pack('CCa*', 0x58, length, $_) if (length() < 0x100);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ < 256 0 0 0
178  
179 0 ▶ return pack('Cna*', 0x59, length, $_) if (length() < 0x10000);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ < 65536 0 0 0
180  
181 0 ▶ return pack('CNa*', 0x5a, length, $_) if (length() <= 0xffffffff);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ <= 4294967295 0 0 0
182  
183 0 return pack('C Q> a*', 0x5b, length, $_);
184 }
185 }
186 elsif (ref eq 'ARRAY') {
187 227 my $hdr;
188  
189 227 ▶ if (@$_ < 24) {
Branch: 4 branches
BranchTrueFalseTotal
if (@$_ < 24) { } 224 3 227
elsif (@$_ < 256) { } 3 0 3
elsif (@$_ < 65536) { } 0 0 0
elsif (@$_ <= 4294967295) { } 0 0 0
190 224 $hdr = chr( 0x80 + @$_ );
191 }
192 elsif (@$_ < 0x100) {
193 3 $hdr = pack( 'CC', 0x98, 0 + @$_ );
194 }
195 elsif (@$_ < 0x10000) {
196 0 $hdr = pack( 'Cn', 0x99, 0 + @$_ );
197 }
198 elsif (@$_ <= 0xffffffff) {
199 0 $hdr = pack( 'CN', 0x9a, 0 + @$_ );
200 }
201 else {
202 0 $hdr = pack( 'C Q>', 0x9b, 0 + @$_ );
203 }
204  
205 227 return join( q<>, $hdr, map { encode($_, $_[1]) } @$_ );
206 }
207 elsif (ref eq 'HASH') {
208 14 my $hdr;
209  
210 14 $numkeys = keys %$_;
211  
212 14 ▶ if ($numkeys < 24) {
Branch: 4 branches
BranchTrueFalseTotal
if ($numkeys < 24) { } 14 0 14
elsif ($numkeys < 256) { } 0 0 0
elsif ($numkeys < 65536) { } 0 0 0
elsif ($numkeys <= 4294967295) { } 0 0 0
213 14 $hdr = chr( 0xa0 + $numkeys );
214 }
215 elsif ($numkeys < 0x100) {
216 0 $hdr = pack( 'CC', 0xb8, $numkeys );
217 }
218 elsif ($numkeys < 0x10000) {
219 0 $hdr = pack( 'Cn', 0xb9, $numkeys );
220 }
221 elsif ($numkeys <= 0xffffffff) {
222 0 $hdr = pack( 'CN', 0xba, $numkeys );
223 }
224 else {
225 0 $hdr = pack( 'C Q>', 0xbb, $numkeys );
226 }
227  
228 14 ▶ if ($_[1] && $_[1]->{'canonical'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] and $_[1]{'canonical'}) { } 2 12 14
Condition66%2/3
$_[1] and $_[1]{"canonical"}
!ll&&!rl&&r
1202
MC/DC50%1/2
$_[1] and $_[1]{"canonical"}
$_[1] $_[1]{"canonical"}
Truth table66%2/3
A and B
A= $_[1]B= $_[1]{"canonical"}
ABresult
0X0
100
111
229 2 my $hr = $_;
230  
231 2 ▶ my @keys = sort { (length($a) <=> length($b)) || ($a cmp $b) } keys %$_;
Branch: 1 branch
BranchTrueFalseTotal
length $a <=> length $b or $a cmp $b 3 7 10
232 2 return join( q<>, $hdr, map { encode($_), encode($hr->{$_}, $_[1]) } @keys );
233 }
234 else {
235 12 return join( q<>, $hdr, map { encode($_, $_[1]) } %$_ );
236 }
237 }
238 elsif (ref()->isa('JSON::PP::Boolean')) {
239 5 ▶ return $_ ? "\xf5" : "\xf4";
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 3 2 5
240 }
241 elsif (ref()->isa('CBOR::PP::Tagged')) {
242 1 my $numstr = encode( $_->[0] );
243  
244 1 substr($numstr, 0, 1) &= "\x1f"; # zero out the first three bits
245 1 substr($numstr, 0, 1) |= "\xc0"; # now assign the first three
246  
247 1 return( $numstr . encode( $_->[1], $_[1] ) );
248 }
249  
250 0 die "Can’t encode “$_” as CBOR!";
251 }
252 }
253  
254 6 1;
« PP/Decode.pm ↑ Summary PP/Tagged.pm »