lib/Encoding/BER.pm

filter: Statementstmt 77.7% 524 / 674 Branchbran 65.1% 168 / 258 Conditioncond 56.8% 54 / 95 MC/DCmcdc 32.7% 18 / 55 Subroutinesub 85.7% 54 / 63 Podpod 12.2% 7 / 57 totaltotal 68.6% 825 / 1202 CC 174 SCAR 24.9
CC
174
Cov
73%
decode_item
37.5
encode_length
22.9
encode_real
20.2
CRAP 12.1
« blib/lib/Encoding/BER/CER.pm ↑ Summary blib/lib/Encoding/BER/SNMP.pm »
1 # -*- perl -*-
2  
3 # Copyright (c) 2007 by Jeff Weisberg
4 # Author: Jeff Weisberg <jaw+pause @ tcp4me.com>
5 # Created: 2007-Jan-28 16:03 (EST)
6 # Function: BER encoding/decoding (also: CER and DER)
7 #
8 # $Id: BER.pm,v 1.11 2008/05/31 18:43:11 jaw Exp $
9  
10 # references: ITU-T x.680 07/2002 - ASN.1
11 # references: ITU-T x.690 07/2002 - BER
12  
13 package Encoding::BER;
14 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 $VERSION = '1.02';
16 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 # loaded on demand if needed:
19 # POSIX
20 # used if already loaded:
21 # Math::BigInt
22  
23 =head1 NAME
24  
25 Encoding::BER - Perl module for encoding/decoding data using ASN.1 Basic Encoding Rules (BER)
26  
27 =head1 SYNOPSIS
28  
29 use Encoding::BER;
30 my $enc = Encoding::BER->new();
31 my $ber = $enc->encode( $data );
32 my $xyz = $enc->decode( $ber );
33  
34 =head1 DESCRIPTION
35  
36 Unlike many other BER encoder/decoders, this module uses tree structured data
37 as the interface to/from the encoder/decoder.
38  
39 The decoder does not require any form of template or description of the
40 data to be decoded. Given arbitrary BER encoded data, the decoder produces
41 a tree shaped perl data structure from it.
42  
43 The encoder takes a perl data structure and produces a BER encoding from it.
44
45 =head1 METHODS
46  
47 =over 4
48  
49 =cut
50 ;
51  
52 ################################################################
53  
54 3 my %CLASS =
55 (
56 universal => { v => 0, },
57 application => { v => 0x40, },
58 context => { v => 0x80, },
59 private => { v => 0xC0, },
60 );
61  
62 3 my %TYPE =
63 (
64 primitive => { v => 0, },
65 constructed => { v => 0x20, },
66 );
67  
68 3 my %TAG =
69 (
70 universal => {
71 content_end => { v => 0, },
72 boolean => { v => 1, e => \&encode_bool, d => \&decode_bool },
73 integer => { v => 2, e => \&encode_int, d => \&decode_int },
74 bit_string => { v => 3, e => \&encode_bits, d => \&decode_bits, dc => \&reass_string, rule => 1 },
75 octet_string => { v => 4, e => \&encode_string, d => \&decode_string, dc => \&reass_string, rule => 1 },
76 null => { v => 5, e => \&encode_null, d => \&decode_null },
77 oid => { v => 6, e => \&encode_oid, d => \&decode_oid },
78 object_descriptor => { v => 7, implicit => 'octet_string' },
79 external => { v => 8, type => ['constructed'] },
80 real => { v => 9, e => \&encode_real, d => \&decode_real },
81 enumerated => { v => 0xA, implicit => 'integer' },
82 embedded_pdv => { v => 0xB, e => \&encode_string, d => \&decode_string, dc => \&reass_string },
83 utf8_string => { v => 0xC, implicit => 'octet_string' },
84 relative_oid => { v => 0xD, e => \&encode_roid, d => \&decode_roid },
85 # reserved
86 # reserved
87 sequence => { v => 0x10, type => ['constructed'] },
88 set => { v => 0x11, type => ['constructed'] },
89 numeric_string => { v => 0x12, implicit => 'octet_string' },
90 printable_string => { v => 0x13, implicit => 'octet_string' },
91 teletex_string => { v => 0x14, implicit => 'octet_string' },
92 videotex_string => { v => 0x15, implicit => 'octet_string' },
93 ia5_string => { v => 0x16, implicit => 'octet_string' },
94 universal_time => { v => 0x17, implicit => 'octet_string' },
95 generalized_time => { v => 0x18, implicit => 'octet_string' },
96 graphic_string => { v => 0x19, implicit => 'octet_string' },
97 visible_string => { v => 0x1a, implicit => 'octet_string' },
98 general_string => { v => 0x1b, implicit => 'octet_string' },
99 universal_string => { v => 0x1c, implicit => 'octet_string' },
100 character_string => { v => 0x1d, implicit => 'octet_string' },
101 bmp_string => { v => 0x1e, implicit => 'octet_string' },
102 },
103
104 private => {
105 # extra.
106 # no, the encode/decode functions are not mixed up.
107 # yes, this module handles large tag-numbers.
108 integer32 => { v => 0xFFF0, type => ['private'], e => \&encode_uint32, d => \&decode_int },
109 unsigned_int => { v => 0xFFF1, type => ['private'], e => \&encode_uint, d => \&decode_uint },
110 unsigned_int32 => { v => 0xFFF2, type => ['private'], e => \&encode_uint32, d => \&decode_uint },
111 },
112 );
113  
114 # synonyms
115 3 my %AKATAG =
116 (
117 bool => 'boolean',
118 int => 'integer',
119 string => 'octet_string',
120 object_identifier => 'oid',
121 relative_object_identifier => 'relative_oid',
122 roid => 'relative_oid',
123 float => 'real',
124 enum => 'enumerated',
125 sequence_of => 'sequence',
126 set_of => 'set',
127 t61_string => 'teletex_string',
128 iso646_string => 'visible_string',
129 int32 => 'integer32',
130 unsigned_integer => 'unsigned_int',
131 uint => 'unsigned_int',
132 uint32 => 'unsigned_int32',
133 # ...
134 );
135  
136 # insert name into above data
137 3 my %ALLTAG;
138 my %REVTAG;
139  
140 # insert name + class into above data
141 # build reverse map, etc.
142 3 init_tag_lookups( \%TAG, \%ALLTAG, \%REVTAG );
143  
144 my %REVCLASS = map {
145 3 ( $CLASS{$_}{v} => $_ )
146 } keys %CLASS;
147  
148 my %REVTYPE = map {
149 3 ( $TYPE{$_}{v} => $_ )
150 } keys %TYPE;
151  
152 ################################################################
153  
154 =item new(option => value, ...)
155  
156 constructor.
157  
158 example:
159 my $enc = Encoding::BER->new( error => sub{ die "$_[1]\n" } );
160  
161 the following options are available:
162  
163 =over 4
164  
165 =item error
166  
167 coderef called if there is an error. will be called with 2 parameters,
168 the Encoding::BER object, and the error message.
169  
170 # example: die on error
171 error => sub{ die "oops! $_[1]\n" }
172
173 =item warn
174  
175 coderef called if there is something to warn about. will be called with 2 parameters,
176 the Encoding::BER object, and the error message.
177  
178 # example: warn for warnings
179 warn => sub{ warn "how odd! $_[1]\n" }
180
181  
182 =item decoded_callback
183  
184 coderef called for every element decoded. will be called with 2 parameters,
185 the Encoding::BER object, and the decoded data. [see DECODED DATA]
186  
187 # example: bless decoded results into a useful class
188 decoded_callback => sub{ bless $_[1], MyBER::Result }
189
190 =item debug
191  
192 boolean. if true, large amounts of useless gibberish will be sent to stderr regarding
193 the encoding or decoding process.
194  
195 # example: enable gibberish output
196 debug => 1
197  
198 =back
199  
200 =cut
201 ;
202  
203 94 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
204 94 my $cl = shift;
205 94 my $me = bless { @_ }, $cl;
206  
207 94 $me;
208 }
209  
210 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
211 0 my $me = shift;
212 0 my $msg = shift;
213  
214 0 ▶ if( my $f = $me->{error} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $me->{'error'}) { } 0 0 0
215 0 $f->($me, $msg);
216 }else{
217 0 croak ((ref $me) . ": $msg\n");
218 }
219 0 undef;
220 }
221  
222 1 ▶sub warn {
Subroutine warn: called CC=2 SCAR=7.2
Pod documented
223 1 my $me = shift;
224 1 my $msg = shift;
225  
226 1 ▶ if( my $f = $me->{warn} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $me->{'warn'}) { } 1 0 1
227 1 $f->($me, $msg);
228 }else{
229 0 carp ((ref $me) . ": $msg\n");
230 }
231 1 undef;
232 }
233  
234 630 ▶sub debug {
Subroutine debug: called CC=2 SCAR=8.4
Pod documented
235 630 my $me = shift;
236 630 my $msg = shift;
237  
238 630 ▶ return unless $me->{debug};
Branch: 1 branch
BranchTrueFalseTotal
unless $me->{"debug"} 630 0 630
239 0 print STDERR " " x $me->{level}, $msg, "\n";
240 0 undef;
241 }
242  
243 ################################################################
244  
245 139 ▶sub add_tag_hash {
Subroutine add_tag_hash: called CC=3 SCAR=11.0
Pod undocumented
246 139 my $me = shift;
247 139 my $class = shift;
248 139 my $type = shift;
249 139 my $name = shift;
250 139 my $num = shift;
251 139 my $data = shift;
252  
253 139 ▶ return $me->error("invalid class: $class") unless $CLASS{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless $CLASS{$class} 0 139 139
254 139 ▶ return $me->error("invalid type: $type") unless $TYPE{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $TYPE{$type} 0 139 139
255  
256 139 $data->{type} = [$class, $type];
257 139 $data->{v} = $num;
258 139 $data->{n} = $name;
259
260 # install forward + reverse mappings
261 139 $me->{tags}{$name} = $data;
262 139 $me->{revtags}{$class}{$num} = $name;
263  
264 139 $me;
265 }
266  
267 =item add_implicit_tag(class, type, tag-name, tag-number, base-tag)
268  
269 add a new tag similar to another tag. class should be one of C<universal>,
270 C<application>, C<context>, or C<private>. type should be either C<primitive>
271 or C<contructed>. tag-name should specify the name of the new tag.
272 tag-number should be the numeric tag number. base-tag should specify the
273 name of the tag this is equivalent to.
274  
275 example: add a tagged integer
276 in ASN.1: width-index ::= [context 42] implicit integer
277
278 $ber->add_implicit_tag('context', 'primitive', 'width-index', 42, 'integer');
279  
280 =cut
281 ;
282  
283 138 ▶sub add_implicit_tag {
Subroutine add_implicit_tag: called CC=2 SCAR=7.0
Pod documented
284 138 my $me = shift;
285 138 my $class = shift;
286 138 my $type = shift;
287 138 my $name = shift;
288 138 my $num = shift;
289 138 my $base = shift;
290  
291 138 ▶ return $me->error("unknown base tag name: $base")
Branch: 1 branch
BranchTrueFalseTotal
unless $me->tag_data_byname($base) 0 138 138
292 unless $me->tag_data_byname($base);
293  
294 138 $me->add_tag_hash($class, $type, $name, $num, {
295 implicit => $base,
296 });
297 }
298  
299 1 ▶sub add_tag {
Subroutine add_tag: called CC=1 SCAR=0.0
Pod undocumented
300 1 my $me = shift;
301 1 my $class = shift;
302 1 my $type = shift;
303 1 my $name = shift;
304 1 my $num = shift;
305 # possibly optional:
306 1 my $encf = shift;
307 1 my $decf = shift;
308 1 my $encfc = shift;
309 1 my $decfc = shift;
310
311 1 $me->add_tag_hash($class, $type, $name, $num, {
312 e => $encf,
313 d => $decf,
314 ec => $encfc,
315 dc => $decfc,
316 });
317 }
318  
319 3 ▶sub init_tag_lookups {
Subroutine init_tag_lookups: called CC=3 SCAR=11.0
Pod undocumented
320 3 my $TAG = shift;
321 3 my $ALL = shift;
322 3 my $REV = shift;
323
324 3 for my $class (keys %$TAG){
325 6 for my $name (keys %{$TAG->{$class}}){
326 96 $TAG->{$class}{$name}{n} = $name;
327 96 $ALL->{$name} = $TAG->{$class}{$name};
328 }
329 my %d = map {
330 96 ($TAG->{$class}{$_}{v} => $_)
331 6 } keys %{$TAG->{$class}};
332 6 $REV->{$class} = \%d;
333 }
334 }
335  
336 ################################################################
337  
338 =item encode( data )
339  
340 BER encode the provided data. [see: ENCODING DATA]
341  
342 example:
343 my $ber = $enc->encode( [0, 'public', [7.3, 0, 0, ['foo', 'bar']]] );
344  
345 =cut
346 ;
347  
348 113 ▶sub encode {
Subroutine encode: called CC=14 SCAR=26.6
Pod documented
349 113 my $me = shift;
350 113 my $data = shift;
351 113 my $levl = shift;
352
353 113 ▶ $me->{level} = $levl || 0;
Condition100%2/2
$levl || 0
l!l
2093
MC/DC100%1/1
$levl || 0
$levl
Truth table100%2/2
A
A= $levl
Aresult
11
00
354 113 ▶ $data = $me->canonicalize($data) if $me->{acanonical} || !$me->behaves_like_a_hash($data);
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"acanonical"} or not $me->behaves_like_a_hash($data) 64 49 113
Condition100%3/3
$me->{"acanonical"} or not $me->behaves_like_a_hash($data)
l!l&&r!l&&!r
95549
MC/DC100%2/2
$me->{"acanonical"} or not $me->behaves_like_a_hash($data)
$me->{"acanonical"} not $me->behaves_like_a_hash($data)
Truth table100%3/3
A or not(B)
A= $me->{"acanonical"}B= not $me->behaves_like_a_hash($data)
ABresult
1X1
011
000
355  
356 # include pre-encoded data as is
357 113 ▶ if( $data->{type} eq 'BER_preencoded' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"type"} eq "BER_preencoded") 0 113 113
358 0 return $data->{value};
359 }
360
361 113 ▶ $data = $me->rule_check_and_apply($data) || $data;
Condition33%1/3
$me->rule_check_and_apply($data) || $data
l!l&&r!l&&!r
01130
MC/DC0%0/2
$me->rule_check_and_apply($data) || $data
$me->rule_check_and_apply($data) $data
Truth table33%1/3
A || B
A= $me->rule_check_and_apply($data)B= $data
ABresult
1X1
011
000
362 113 my($typeval, $tagnum, $encfnc) = $me->ident_data_and_efunc($data->{type});
363 113 my $value;
364  
365 113 ▶ if( $typeval & 0x20 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($typeval & 32) { } 5 108 113
366 5 $me->debug( "encode constructed ($typeval/$tagnum) [" );
367 # constructed - recurse
368 5 ▶ my @vs = ref($data->{value}) ? @{$data->{value}} : $data->{value};
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'value'} ? : 5 0 5
369 5 for my $e (@vs){
370 20 $value .= $me->encode( $e, $me->{level} + 1 );
371 }
372 5 ▶ $me->{level} = $levl || 0;
Condition100%2/2
$levl || 0
l!l
14
MC/DC100%1/1
$levl || 0
$levl
Truth table100%2/2
A
A= $levl
Aresult
11
00
373 5 $me->debug("]");
374 }else{
375 108 $me->debug( "encode primitive ($typeval/$tagnum)" );
376
377 108 ▶ unless( $encfnc ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($encfnc) 1 107 108
378 # try to guess encoding
379 1 ▶ my @t = ref($data->{type}) ? @{$data->{type}} : $data->{type};
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'type'} ? : 1 0 1
380 1 $me->warn("do not know how to encode identifier [@t] ($typeval/$tagnum)");
381 1 $encfnc = \&encode_unknown;
382 }
383 108 $value = $encfnc->($me, $data);
384 }
385  
386 113 my $defp = $me->use_definite_form($typeval, $data);
387 113 my $leng = $me->encode_length(length($value));
388  
389 113 my $res;
390 113 ▶ if( $defp && defined($leng) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($defp and defined $leng) { } 112 1 113
Condition66%2/3
$defp and defined $leng
!ll&&!rl&&r
10112
MC/DC50%1/2
$defp and defined $leng
$defp defined $leng
Truth table66%2/3
A and B
A= $defpB= defined $leng
ABresult
0X0
100
111
391 112 $me->debug("encode definite form");
392 112 $res = $me->encode_ident($typeval, $tagnum) . $leng . $value;
393 }else{
394 1 $me->debug("encode indefinite form");
395 1 $res = $me->encode_ident($typeval, $tagnum) . "\x80" . $value . "\x00\x00";
396 # x.690: 8.3.6.1 8.1.5
397 }
398
399 113 $data->{dlen} = length($value);
400 113 $data->{tlen} = length($res);
401  
402 113 $res;
403 }
404  
405 2 ▶sub encode_null {
Subroutine encode_null: called CC=1 SCAR=0.0
Pod undocumented
406 2 my $me = shift;
407 2 $me->debug('encode null');
408 2 '';
409 }
410  
411 1 ▶sub encode_unknown {
Subroutine encode_unknown: called CC=1 SCAR=0.0
Pod undocumented
412 1 my $me = shift;
413 1 my $data = shift;
414  
415 1 $me->debug('encode unknown');
416 1 '' . $data->{value};
417 }
418  
419 9 ▶sub encode_string {
Subroutine encode_string: called CC=1 SCAR=0.0
Pod undocumented
420 9 my $me = shift;
421 9 my $data = shift;
422  
423 # CER splitting of long strings is handled in CER subclass
424 9 $me->debug('encode string');
425 9 '' . $data->{value};
426 }
427  
428 1 ▶sub encode_bits {
Subroutine encode_bits: called CC=1 SCAR=0.0
Pod undocumented
429 1 my $me = shift;
430 1 my $data = shift;
431  
432 # x.690 8.6
433 1 $me->debug('encode bitstring');
434 1 "\0" . $data->{value};
435  
436 }
437  
438 2 ▶sub encode_bool {
Subroutine encode_bool: called CC=2 SCAR=6.9
Pod undocumented
439 2 my $me = shift;
440 2 my $data = shift;
441  
442 # x.690 11.1
443 2 $me->debug('encode boolean');
444 2 ▶ $data->{value} ? "\xFF" : "\x0";
Branch: 1 branch
BranchTrueFalseTotal
$data->{'value'} ? : 1 1 2
445 }
446  
447 48 ▶sub encode_int {
Subroutine encode_int: called CC=15 SCAR=27.1
Pod undocumented
448 48 my $me = shift;
449 48 my $data = shift;
450 48 my $val = $data->{value};
451  
452 48 my @i;
453 my $big;
454  
455 48 ▶ if( _have_math_bigint() ){
Branch: 1 branch
BranchTrueFalseTotal
if (_have_math_bigint()) 44 4 48
456 # value is a bigint or a long string
457 44 ▶ $big = 1 if (ref $val && $val->can('as_hex')) || length($val) > 8;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val and $val->can("as_hex") or length $val > 8 22 22 44
Condition83%5/6
ref $val and $val->can("as_hex") or length $val > 8
l!l&&r!l&&!r
21122
ref $val and $val->can("as_hex")
!ll&&!rl&&r
23021
MC/DC66%2/3
ref $val and $val->can("as_hex") or length $val > 8
ref $val $val->can("as_hex") length $val > 8
Truth table60%3/5
A and B or C
A= ref $valB= $val->can("as_hex")C= length $val > 8
ABCresult
11X1
0X11
1011
0X00
1000
458 }
459
460 48 ▶ if( $big ){
Branch: 1 branch
BranchTrueFalseTotal
if ($big) { } 22 26 48
461 22 my $x = Math::BigInt->new($val);
462 22 $me->debug("bigint $val => $x");
463 22 ▶ my $sign = $x->is_neg() ? 0xff : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->is_neg ? : 9 13 22
464 22 ▶ if( $sign ){
Branch: 1 branch
BranchTrueFalseTotal
if ($sign) { } 9 13 22
465 # NB: in 2s comp: -X = ~(X-1) = ~X+1
466 9 $x = $x->bneg()->bsub(1)->as_hex();
467 9 $x =~ s/^0x//;
468 9 ▶ $x = '0'.$x if length($x) & 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $x & 1 4 5 9
469 9 @i = map{ ~$_ & 0xff } unpack('C*', pack('H*', $x));
470 9 ▶ unshift @i, 0xff unless $i[0] & 0x80;
Branch: 1 branch
BranchTrueFalseTotal
unless $i[0] & 128 4 5 9
471 }else{
472 13 $x = $x->as_hex();
473 13 $x =~ s/^0x//;
474 13 ▶ $x = '0'.$x if length($x) & 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $x & 1 6 7 13
475 13 @i = unpack('C*', pack('H*', $x));
476 13 ▶ unshift @i, 0 if $i[0] & 0x80;
Branch: 1 branch
BranchTrueFalseTotal
if $i[0] & 128 5 8 13
477 }
478 22 $me->debug("encode big int [@i]");
479 }else{
480 26 ▶ my $sign = ($val < 0) ? 0xff : 0;
Branch: 1 branch
BranchTrueFalseTotal
$val < 0 ? : 5 21 26
481 26 while(1){
482 36 unshift @i, $val & 0xFF;
483 36 ▶ last if $val >= -128 && $val < 128;
Branch: 1 branch
BranchTrueFalseTotal
if $val >= -128 and $val < 128 26 10 36
Condition100%3/3
$val >= -128 and $val < 128
!ll&&!rl&&r
4626
MC/DC100%2/2
$val >= -128 and $val < 128
$val >= -128 $val < 128
Truth table100%3/3
A and B
A= $val >= -128B= $val < 128
ABresult
0X0
100
111
484 # NB: >>= does not preserve sign.
485 10 $val = int(($val - $sign)/256);
486 }
487 26 $me->debug("encode int [@i]");
488 }
489 48 pack('C*', @i);
490 }
491  
492 12 ▶sub encode_uint {
Subroutine encode_uint: called CC=8 SCAR=20.9
Pod undocumented
493 12 my $me = shift;
494 12 my $data = shift;
495 12 my $val = $data->{value};
496
497 12 my @i;
498 my $big;
499  
500 12 ▶ if( _have_math_bigint() ){
Branch: 1 branch
BranchTrueFalseTotal
if (_have_math_bigint()) 12 0 12
501 # value is a bigint or a long string
502 12 ▶ $big = 1 if (ref $val && $val->can('bcmp')) || length($val) > 8;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val and $val->can("bcmp") or length $val > 8 6 6 12
Condition66%4/6
ref $val and $val->can("bcmp") or length $val > 8
l!l&&r!l&&!r
606
ref $val and $val->can("bcmp")
!ll&&!rl&&r
606
MC/DC33%1/3
ref $val and $val->can("bcmp") or length $val > 8
ref $val $val->can("bcmp") length $val > 8
Truth table40%2/5
A and B or C
A= ref $valB= $val->can("bcmp")C= length $val > 8
ABCresult
11X1
0X11
1011
0X00
1000
503 }
504  
505 12 ▶ if( $big ){
Branch: 1 branch
BranchTrueFalseTotal
if ($big) { } 6 6 12
506 6 my $x = Math::BigInt->new($val)->as_hex();
507 6 $x =~ s/^0x//;
508 6 ▶ $x = '0' . $x if length($x) & 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $x & 1 3 3 6
509 6 $me->debug("encode big unsigned int");
510 6 pack('H*', $x);
511 }else{
512 6 ▶ while($val){
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 7 6 13
513 7 unshift @i, $val & 0xFF;
514 7 $val >>= 8;
515 }
516 6 $me->debug("encode unsigned int [@i]");
517 6 pack('C*', @i);
518 }
519 }
520  
521  
522 2 ▶sub encode_uint32 {
Subroutine encode_uint32: called CC=1 SCAR=0.0
Pod undocumented
523 2 my $me = shift;
524 2 my $data = shift;
525 2 my $val = $data->{value};
526  
527 # signed or unsigned. -1 == 0xffffffff
528 2 $me->debug("encode unsigned int32");
529 2 pack('N', $val);
530 }
531  
532 29 ▶sub encode_real {
Subroutine encode_real: called CC=19 SCAR=30.1
Pod undocumented
533 29 my $me = shift;
534 29 my $data = shift;
535 29 my $val = $data->{value};
536  
537 29 ▶ return '' unless $val; # x.690 8.5.2
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 29 29
538 29 ▶ return "\x40" if $val eq 'inf'; # x.690 8.5.8
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "inf" 0 29 29
539 29 ▶ return "\x41" if $val eq '-inf'; # x.690 8.5.8
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "-inf" 0 29 29
540  
541 # POSIX required. available?
542 29 eval {
543 29 require POSIX;
544 };
545 29 ▶ return $me->error("POSIX not available. cannot encode type real")
Branch: 1 branch
BranchTrueFalseTotal
unless defined &POSIX::frexp 0 29 29
546 unless defined &POSIX::frexp;
547  
548 29 my $sign = 0;
549 29 my($mant, $exp) = POSIX::frexp($val);
550 29 ▶ if( $mant < 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($mant < 0) 7 22 29
551 7 $sign = 1;
552 7 $mant = - $mant;
553 }
554  
555 #$me->debug("encode real: $mant ^ $exp");
556
557 # go byte-by-byte
558 29 my @mant;
559 29 ▶ while($mant > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($mant > 0) 107 29 136
560 107 my($frac, $int) = POSIX::modf(POSIX::ldexp($mant, 8));
561 107 push @mant, $int;
562 107 $mant = $frac;
563 107 $exp -= 8;
564 # $me->debug("encode real: [@mant] ^ $exp");
565 }
566 #$me->debug("encode real: [@mant] ^ $exp");
567  
568 29 ▶ if( $data->{flavor} || $me->{flavor} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"flavor"} or $me->{"flavor"}) 29 0 29
Condition33%1/3
$data->{"flavor"} or $me->{"flavor"}
l!l&&r!l&&!r
0290
MC/DC0%0/2
$data->{"flavor"} or $me->{"flavor"}
$data->{"flavor"} $me->{"flavor"}
Truth table33%1/3
A or B
A= $data->{"flavor"}B= $me->{"flavor"}
ABresult
1X1
011
000
569 # x.690 8.5.6.5, 11.3.1 - CER + DER require N has a 1 in the lsb
570 # normalize
571 29 ▶ while( ! ($mant[-1] & 1) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($mant[-1] & 1) 153 29 182
572 # shift right
573 153 my $c = 0;
574 153 for (@mant){
575 417 my $l = $_ & 1;
576 417 ▶ $_ = ($_>>1) | ($c?0x80:0);
Branch: 1 branch
BranchTrueFalseTotal
$c ? : 144 273 417
577 417 $c = $l;
578 }
579 153 $exp ++;
580 }
581 #$me->debug("encode real normalized: [@mant] ^ $exp");
582 }
583  
584 # encode exp
585 29 my @exp;
586 29 ▶ my $exps = ($exp < 0) ? 0xff : 0;
Branch: 1 branch
BranchTrueFalseTotal
$exp < 0 ? : 17 12 29
587 29 while(1){
588 32 unshift @exp, $exp & 0xFF;
589 32 ▶ last if $exp >= -128 && $exp < 128;
Branch: 1 branch
BranchTrueFalseTotal
if $exp >= -128 and $exp < 128 29 3 32
Condition100%3/3
$exp >= -128 and $exp < 128
!ll&&!rl&&r
2129
MC/DC100%2/2
$exp >= -128 and $exp < 128
$exp >= -128 $exp < 128
Truth table100%3/3
A and B
A= $exp >= -128B= $exp < 128
ABresult
0X0
100
111
590 # >>= does not preserve sign.
591 3 $exp = int(($exp - $exps)/256);
592 }
593
594 29 $me->debug("encode real: [@mant] ^ [@exp]");
595  
596 29 ▶ my $first = 0x80 | ($sign ? 0x40 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$sign ? : 7 22 29
597  
598 29 ▶ if(@exp == 2){
Branch: 1 branch
BranchTrueFalseTotal
if (@exp == 2) 3 26 29
599 3 $first |= 1;
600 }
601 29 ▶ if(@exp == 3){
Branch: 1 branch
BranchTrueFalseTotal
if (@exp == 3) 0 29 29
602 0 $first |= 2;
603 }
604 29 ▶ if(@exp > 3){
Branch: 1 branch
BranchTrueFalseTotal
if (@exp > 3) 0 29 29
605 # should not happen using ieee-754 doubles
606 0 $first |= 3;
607 0 unshift @exp, scalar(@exp);
608 }
609
610 29 pack('C*', $first, @exp, @mant);
611 }
612  
613 1 ▶sub encode_oid {
Subroutine encode_oid: called CC=4 SCAR=14.1
Pod undocumented
614 1 my $me = shift;
615 1 my $data = shift;
616 1 my $val = $data->{value};
617 # "1.3.6.1.2.0" | [1, 3, 6, 1, 2, 0]
618  
619 # x.690 8.19
620 1 ▶ my @o = ref($val) ? @$val : (split /\./, $val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 0 1 1
621 1 ▶ shift @o if $o[0] eq ''; # remove empty in case specified with leading .
Branch: 1 branch
BranchTrueFalseTotal
if $o[0] eq "" 0 1 1
622  
623 1 ▶ if( @o > 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@o > 1) 1 0 1
624 # x.690 8.19.4
625 1 my $o = shift @o;
626 1 $o[0] += $o * 40;
627 }
628  
629 1 $me->debug("encode oid [@o]");
630 1 pack('w*', @o);
631 }
632  
633 1 ▶sub encode_roid {
Subroutine encode_roid: called CC=3 SCAR=11.2
Pod undocumented
634 1 my $me = shift;
635 1 my $data = shift;
636 1 my $val = $data->{value};
637 # "1.3.6.1.2.0" | [1, 3, 6, 1, 2, 0]
638  
639 # x.690 8.20
640 1 ▶ my @o = ref($val) ? @$val : (split /\./, $val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 0 1 1
641 1 ▶ shift @o if $o[0] eq ''; # remove empty in case specified with leading .
Branch: 1 branch
BranchTrueFalseTotal
if $o[0] eq "" 0 1 1
642 # no special encoding of 1st 2
643  
644 1 $me->debug("encode relative-oid [@o]");
645 1 pack('w*', @o);
646 }
647  
648  
649 ################################################################
650  
651 113 ▶sub encode_ident {
Subroutine encode_ident: called CC=2 SCAR=6.9
Pod undocumented
652 113 my $me = shift;
653 113 my $type = shift;
654 113 my $tnum = shift;
655  
656 113 ▶ if( $tnum < 31 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($tnum < 31) 112 1 113
657 112 return pack('C', $type|$tnum);
658 }
659 1 $type |= 0x1f;
660 1 pack('Cw', $type, $tnum);
661 }
662  
663 113 ▶sub encode_length {
Subroutine encode_length: called CC=6 SCAR=31.3
Pod undocumented
664 113 my $me = shift;
665 113 my $len = shift;
666  
667 113 ▶ return pack('C', $len) if $len < 128; # x.690 8.1.3.4
Branch: 1 branch
BranchTrueFalseTotal
if $len < 128 113 0 113
668 0 ▶ return pack('CC', 0x81, $len) if $len < 1<<8; # x.690 8.1.3.5
Branch: 1 branch
BranchTrueFalseTotal
if $len < 256 0 0 0
669 0 ▶ return pack('Cn', 0x82, $len) if $len < 1<<12;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 4096 0 0 0
670 0 ▶ return pack('CCn',0x83, ($len>>16), ($len&0xFFFF)) if $len < 1<<16;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 65536 0 0 0
671 0 ▶ return pack('CN', 0x84, $len) if $len <= 0xFFFFFFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $len <= 4294967295 0 0 0
672
673 # items larger than above will be encoded in indefinite form
674 0 return;
675 }
676  
677 # override me in subclass
678 113 ▶sub rule_check_and_apply {
Subroutine rule_check_and_apply: called CC=1 SCAR=0.0
Pod undocumented
679 113 my $me = shift;
680 113 my $data = shift;
681  
682 113 undef;
683 }
684  
685 # convert DWIM values => canonical form
686 64 ▶sub canonicalize {
Subroutine canonicalize: called CC=6 SCAR=17.9
Pod undocumented
687 64 my $me = shift;
688 64 my $data = shift;
689
690 # arrayref | int | float | string | undef
691  
692 64 ▶ unless( defined $data ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 2 62 64
693 return {
694 2 type => 'null',
695 value => undef,
696 };
697 }
698
699 62 ▶ if( $me->behaves_like_an_array($data) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($me->behaves_like_an_array($data)) 3 59 62
700 return {
701 3 type => 'sequence',
702 value => $data,
703 };
704 }
705  
706 59 ▶ if( $me->behaves_like_a_hash($data) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($me->behaves_like_a_hash($data)) 1 58 59
707 return {
708 1 type => ['application', 'constructed', 3],
709 value => [ %$data ],
710 };
711 }
712
713 58 ▶ if( $me->smells_like_a_number($data) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($me->smells_like_a_number($data)) 51 7 58
714 return {
715 51 ▶ type => ( int($data) == $data ? 'integer' : 'real'),
Branch: 1 branch
BranchTrueFalseTotal
int $data == $data ? : 48 3 51
716 value => $data,
717 };
718 }
719  
720 # call it a string
721 return {
722 7 type => 'octet_string',
723 value => $data,
724 };
725 }
726  
727 # tags added via add_tag method
728 264 ▶sub app_tag_data_byname {
Subroutine app_tag_data_byname: called CC=1 SCAR=0.0
Pod undocumented
729 264 my $me = shift;
730 264 my $name = shift;
731  
732 264 $me->{tags}{$name};
733 }
734  
735 # override me in subclass
736 250 ▶sub subclass_tag_data_byname {
Subroutine subclass_tag_data_byname: called CC=1 SCAR=0.0
Pod undocumented
737 250 my $me = shift;
738 250 my $name = shift;
739  
740 250 undef;
741 }
742  
743 # from the table up top
744 250 ▶sub univ_tag_data_byname {
Subroutine univ_tag_data_byname: called CC=3 SCAR=11.6
Pod undocumented
745 250 my $me = shift;
746 250 my $name = shift;
747  
748 250 ▶ $ALLTAG{$name} || ($AKATAG{$name} && $ALLTAG{$AKATAG{$name}});
Branch: 1 branch
BranchTrueFalseTotal
$ALLTAG{$name} or $AKATAG{$name} and $ALLTAG{$AKATAG{$name}} 93 157 250
Condition50%3/6
$ALLTAG{$name} || $AKATAG{$name} && $ALLTAG{$AKATAG{$name}}
l!l&&r!l&&!r
931570
$AKATAG{$name} and $ALLTAG{$AKATAG{$name}}
!ll&&!rl&&r
00157
MC/DC0%0/3
$ALLTAG{$name} || $AKATAG{$name} && $ALLTAG{$AKATAG{$name}}
$ALLTAG{$name} $AKATAG{$name} $ALLTAG{$AKATAG{$name}}
Truth table50%2/4
A || B and C
A= $ALLTAG{$name}B= $AKATAG{$name}C= $ALLTAG{$AKATAG{$name}}
ABCresult
1XX1
0111
00X0
0100
749 }
750  
751 264 ▶sub tag_data_byname {
Subroutine tag_data_byname: called CC=3 SCAR=11.0
Pod undocumented
752 264 my $me = shift;
753 264 my $name = shift;
754  
755 264 my $th;
756 # application specific tag name
757 264 $th = $me->app_tag_data_byname($name);
758
759 # subclass specific tag name
760 264 ▶ $th = $me->subclass_tag_data_byname($name) unless $th;
Branch: 1 branch
BranchTrueFalseTotal
unless $th 250 14 264
761
762 # universal tag name
763 264 ▶ $th = $me->univ_tag_data_byname($name) unless $th;
Branch: 1 branch
BranchTrueFalseTotal
unless $th 250 14 264
764  
765 264 $th;
766 }
767  
768 130 ▶sub class_and_type_from_speclist {
Subroutine class_and_type_from_speclist: called CC=4 SCAR=14.0
Pod undocumented
769 130 my $me = shift;
770 130 my($class, $type);
771 130 for my $t (@_){
772 36 ▶ if( $CLASS{$t} ){ $class = $t; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($CLASS{$t}) 16 20 36
773 20 ▶ if( $TYPE{$t} ){ $type = $t; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($TYPE{$t}) 20 0 20
774 0 $me->error("unknown type specification [$t] not a class or type");
775 }
776 130 ($class, $type);
777 }
778  
779 113 ▶sub ident_data_and_efunc {
Subroutine ident_data_and_efunc: called CC=17 SCAR=29.7
Pod undocumented
780 113 my $me = shift;
781 113 my $typd = shift;
782 113 my $func = shift;
783  
784 113 ▶ $func ||= 'e';
Condition50%1/2
$func ||= "e"
l!l
0113
MC/DC0%0/1
$func ||= "e"
$func
Truth table50%1/2
A
A= $func
Aresult
11
00
785 113 ▶ my @t = ref($typd) ? @$typd : ($typd);
Branch: 1 branch
BranchTrueFalseTotal
ref $typd ? : 3 110 113
786
787 # type: name | [class, type, name] | [class, type, num]
788 # if name resolves, specified class+type for validation only
789  
790 113 my $tname = pop @t;
791 113 ▶ if( $me->smells_like_a_number($tname) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($me->smells_like_a_number($tname)) 2 111 113
792 2 my($class, $type) = $me->class_and_type_from_speclist( @t );
793 2 ▶ $class ||= 'universal';
Condition50%1/2
$class ||= "universal"
l!l
20
MC/DC0%0/1
$class ||= "universal"
$class
Truth table50%1/2
A
A= $class
Aresult
11
00
794 2 ▶ $type ||= 'primitive';
Condition50%1/2
$type ||= "primitive"
l!l
20
MC/DC0%0/1
$type ||= "primitive"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
795 2 my $tv = $CLASS{$class}{v} | $TYPE{$type}{v};
796 2 my $tm = $tname + 0;
797 2 $me->debug("numeric specification [@t $tname] resolved to [$class $type $tm]");
798 2 return ( $tv, $tm, undef );
799 }
800  
801 111 my $th = $me->tag_data_byname($tname);
802  
803 111 ▶ unless( $th ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($th) 0 111 111
804 0 $me->error("unknown type [$tname]");
805 }
806 111 ▶ unless( ref $th ){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $th) 0 111 111
807 0 $me->error("programmer botch. tag data should be hashref: [$tname] => $th");
808 0 $th = undef;
809 }
810  
811 111 my( $class, $type, $rclass, $rtype, $tnum, $encf );
812  
813 # parse request
814 111 ($rclass, $rtype) = $me->class_and_type_from_speclist( @t );
815 # parse spec
816 111 ▶ if( my $ts = $th->{type} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $ts = $th->{"type"}) 17 94 111
817 17 ($class, $type) = $me->class_and_type_from_speclist( @$ts );
818 }
819  
820 # use these values for identifier-value
821 111 ▶ $class ||= 'universal';
Condition100%2/2
$class ||= "universal"
l!l
1497
MC/DC100%1/1
$class ||= "universal"
$class
Truth table100%2/2
A
A= $class
Aresult
11
00
822 111 ▶ $type = $rtype || $type || 'primitive';
Condition100%5/5
$rtype || $type || 'primitive'
l!l
1893
$rtype or $type
l!l&&r!l&&!r
11793
MC/DC100%2/2
$rtype || $type || 'primitive'
$rtype $type
Truth table100%3/3
A or B
A= $rtypeB= $type
ABresult
1X1
011
000
823 111 $tnum = $th->{v};
824  
825 111 $me->debug("specificication [@t $tname] resolved to [$class $type $tname($tnum)]");
826 # warn if mismatched
827 111 ▶ $me->warn("specificication [$rclass $tname] resolved to [$class $tname]")
Branch: 1 branch
BranchTrueFalseTotal
if $rclass and $rclass ne $class 0 111 111
Condition33%1/3
$rclass and $rclass ne $class
!ll&&!rl&&r
11100
MC/DC0%0/2
$rclass and $rclass ne $class
$rclass $rclass ne $class
Truth table33%1/3
A and B
A= $rclassB= $rclass ne $class
ABresult
0X0
100
111
828 if $rclass && $rclass ne $class;
829
830 # indirect via implicit to find encoding func
831 111 $encf = $th->{$func};
832 111 ▶ if( my $impl = $th->{implicit} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $impl = $th->{"implicit"}) 15 96 111
833 # only one level of indirection
834 15 $th = $me->tag_data_byname($impl);
835  
836 15 ▶ if( ref $th ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $th) { } 15 0 15
837 15 $me->debug("specificication [$class $type $tname($tnum)] is implictly $impl ");
838 15 ▶ $encf ||= $th->{$func};
Condition33%1/3
$encf ||= $th->{$func}
l!l&&r!l&&!r
0150
MC/DC0%0/2
$encf ||= $th->{$func}
$encf $th->{$func}
Truth table33%1/3
A ||= B
A= $encfB= $th->{$func}
ABresult
1X1
011
000
839 }else{
840 0 $me->error("programmer botch. implicit indirect not found: [$class $tname] => $impl");
841 }
842 }
843  
844 111 my $tv = $CLASS{$class}{v} | $TYPE{$type}{v};
845 111 return( $tv, $tnum, $encf );
846 }
847  
848 113 ▶sub use_definite_form {
Subroutine use_definite_form: called CC=6 SCAR=18.4
Pod undocumented
849 113 my $me = shift;
850 113 my $type = shift;
851 113 my $data = shift;
852
853 113 ▶ return 1 unless $type & 0x20; # x.690 8.1.3.2 - primitive - always definite
Branch: 1 branch
BranchTrueFalseTotal
unless $type & 32 108 5 113
854  
855 5 ▶ my $fl = $data->{flavor} || $me->{flavor};
Condition66%2/3
$data->{'flavor'} || $me->{'flavor'}
l!l&&r!l&&!r
014
MC/DC50%1/2
$data->{'flavor'} || $me->{'flavor'}
$data->{'flavor'} $me->{'flavor'}
Truth table66%2/3
A || B
A= $data->{'flavor'}B= $me->{'flavor'}
ABresult
1X1
011
000
856 5 ▶ return 1 unless $fl;
Branch: 1 branch
BranchTrueFalseTotal
unless $fl 4 1 5
857 1 ▶ return 1 if $fl eq 'DER'; # x.690 10.1 - DER - always definite
Branch: 1 branch
BranchTrueFalseTotal
if $fl eq "DER" 0 1 1
858 1 ▶ return 0 if $fl eq 'CER'; # x.690 9.1 - CER + constructed - indefinite
Branch: 1 branch
BranchTrueFalseTotal
if $fl eq "CER" 1 0 1
859 0 1; # otherwise, prefer definite
860 }
861  
862 ################################################################
863  
864 62 ▶sub behaves_like_an_array {
Subroutine behaves_like_an_array: called CC=2 SCAR=6.9
Pod undocumented
865 62 my $me = shift;
866 62 my $d = shift;
867  
868 62 ▶ return unless ref $d;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $d 37 25 62
869 25 return UNIVERSAL::isa($d, 'ARRAY');
870 }
871  
872 163 ▶sub behaves_like_a_hash {
Subroutine behaves_like_a_hash: called CC=3 SCAR=11.0
Pod undocumented
873 163 my $me = shift;
874 163 my $d = shift;
875  
876 163 ▶ return unless ref $d;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $d 69 94 163
877  
878 # treat as if it is a number
879 94 ▶ return if UNIVERSAL::isa($d, 'Math::BigInt');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($d, "Math::BigInt") 42 52 94
880 52 return UNIVERSAL::isa($d, 'HASH');
881 }
882  
883 171 ▶sub smells_like_a_number {
Subroutine smells_like_a_number: called CC=3 SCAR=11.0
Pod undocumented
884 171 my $me = shift;
885 171 my $d = shift;
886  
887 171 ▶ return 1 if ref $d && UNIVERSAL::isa($d, 'Math::BigInt');
Branch: 1 branch
BranchTrueFalseTotal
if ref $d and UNIVERSAL::isa($d, "Math::BigInt") 21 150 171
Condition66%2/3
ref $d and UNIVERSAL::isa($d, "Math::BigInt")
!ll&&!rl&&r
150021
MC/DC50%1/2
ref $d and UNIVERSAL::isa($d, "Math::BigInt")
ref $d UNIVERSAL::isa($d, "Math::BigInt")
Truth table66%2/3
A and B
A= ref $dB= UNIVERSAL::isa($d, "Math::BigInt")
ABresult
0X0
100
111
888 # NB: 5.00503 does not have 'no warnings';
889 150 local $^W = 0;
890 150 return ($d + 0 eq $d);
891 }
892  
893 ################################################################
894  
895 =item decode( ber )
896  
897 Decode the provided BER encoded data. returns a perl data structure.
898 [see: DECODED DATA]
899  
900 example:
901 my $data = $enc->decode( $ber );
902  
903 =cut
904 ;
905  
906 24 ▶sub decode {
Subroutine decode: called CC=1 SCAR=0.0
Pod documented
907 24 my $me = shift;
908 24 my $data = shift;
909  
910 24 $me->{level} = 0;
911 24 my($v, $l) = $me->decode_item($data, 0);
912 24 $v;
913 }
914  
915 2 ▶sub decode_items {
Subroutine decode_items: called CC=5 SCAR=16.8
Pod undocumented
916 2 my $me = shift;
917 2 my $data = shift;
918 2 my $eocp = shift;
919 2 my $levl = shift;
920 2 my @v;
921 2 my $tlen = 0;
922  
923 2 $me->{level} = $levl;
924 2 $me->debug("decode items[");
925 2 ▶ while($data){
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 8 2 10
926 8 my($val, $len) = $me->decode_item($data, $levl+1);
927 8 $tlen += $len;
928 8 ▶ unless( $val && defined $val->{type} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($val and defined $val->{"type"}) 0 8 8
Condition33%1/3
$val and defined $val->{"type"}
!ll&&!rl&&r
008
MC/DC0%0/2
$val and defined $val->{"type"}
$val defined $val->{"type"}
Truth table33%1/3
A and B
A= $valB= defined $val->{"type"}
ABresult
0X0
100
111
929 # end-of-content
930 0 $me->debug('end of content');
931 0 ▶ last if $eocp;
Branch: 1 branch
BranchTrueFalseTotal
if $eocp 0 0 0
932 }
933  
934 8 push @v, $val;
935 8 $data = substr($data, $len);
936 }
937  
938 2 $me->{level} = $levl;
939 2 $me->debug(']');
940 2 return (\@v, $tlen);
941 }
942  
943 32 ▶sub decode_item {
Subroutine decode_item: called CC=16 SCAR=36.3
Pod undocumented
944 32 my $me = shift;
945 32 my $data = shift;
946 32 my $levl = shift;
947
948 # hexdump($data, 'di:');
949 32 $me->{level} = $levl;
950 32 my($typval, $typlen, $typmore) = $me->decode_ident($data);
951 32 my($typdat, $decfnc, $pretty, $tagnum) = $me->ident_descr_and_dfuncs($typval, $typmore);
952 32 my($datlen, $lenlen) = $me->decode_length(substr($data,$typlen));
953 32 my $havlen = length($data);
954 32 ▶ my $tlen = $typlen + $lenlen + ($datlen || 0);
Condition100%2/2
$datlen || 0
l!l
311
MC/DC100%1/1
$datlen || 0
$datlen
Truth table100%2/2
A
A= $datlen
Aresult
11
00
955 32 my $doff = $typlen + $lenlen;
956 32 my $result;
957
958 32 ▶ $me->error("corrupt data? data appears truncated")
Branch: 1 branch
BranchTrueFalseTotal
if $havlen < $tlen 0 32 32
959 if $havlen < $tlen;
960  
961 32 ▶ if( $typval & 0x20 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($typval & 32) { } 2 30 32
962 # constructed
963 2 my $vals;
964
965 2 ▶ if( defined $datlen ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $datlen) { } 2 0 2
966 # definite
967 2 $me->debug("decode item: constructed definite [@$typdat($tagnum)]");
968 2 my($v, $t) = $me->decode_items( substr($data, $doff, $datlen), 0, $levl);
969 2 $me->{level} = $levl;
970 2 ▶ $me->warn("corrupt data? item len != data len ($t, $datlen)")
Branch: 1 branch
BranchTrueFalseTotal
unless $t == $datlen 0 2 2
971 unless $t == $datlen;
972 2 $vals = $v;
973 }else{
974 # indefinite
975 0 $me->debug("decode item: constructed indefinite [@$typdat($tagnum)]");
976 0 my($v, $t) = $me->decode_items( substr($data, $doff), 1, $levl );
977 0 $me->{level} = $levl;
978 0 $tlen += $t;
979 0 $tlen += 2; # eoc
980 0 $vals = $v;
981 }
982 2 ▶ if( $decfnc ){
Branch: 1 branch
BranchTrueFalseTotal
if ($decfnc) { } 0 2 2
983 # constructed decode func: reassemble
984 0 $result = $decfnc->( $me, $vals, $typdat );
985 }else{
986 2 $result = {
987 value => $vals,
988 };
989 }
990 }else{
991 # primitive
992 30 my $ndat;
993 30 ▶ if( defined $datlen ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $datlen) { } 30 0 30
994 # definite
995 30 $me->debug("decode item: primitive definite [@$typdat($tagnum)]");
996 30 $ndat = substr($data, $doff, $datlen);
997 }else{
998 # indefinite encoding of a primitive is a violation of x.690 8.1.3.2(a)
999 # warn + parse it anyway
1000 0 $me->debug("decode item: primitive indefinite [@$typdat($tagnum)]");
1001 0 $me->warn("protocol violation - indefinite encoding of primitive. see x.690 8.1.3.2(a)");
1002 0 my $i = index($data, "\0\0", $doff);
1003 0 ▶ if( $i == -1 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i == -1) 0 0 0
1004 # invalid encoding.
1005 # no eoc found.
1006 # go back to protocol school.
1007 0 $me->error("corrupt data - content terminator not found. see x.690 8.1.3.6, 8.1.5, et al. ");
1008 0 return (undef, $tlen);
1009 }
1010 0 my $dl = $i - $doff;
1011 0 $tlen += $dl;
1012 0 $tlen += 2; # eoc
1013 0 $ndat = substr($data, $doff, $dl);
1014 }
1015  
1016 30 ▶ unless( $typval || $typmore ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($typval or $typmore) 0 30 30
Condition33%1/3
$typval or $typmore
l!l&&r!l&&!r
3000
MC/DC0%0/2
$typval or $typmore
$typval $typmore
Truth table33%1/3
A or B
A= $typvalB= $typmore
ABresult
1X1
011
000
1017 # universal-primitive-tag(0) => end-of-content
1018 0 return ( { }, $tlen );
1019 }
1020  
1021 # decode it
1022 30 ▶ $decfnc ||= \&decode_unknown;
Condition50%1/2
$decfnc ||= \&Encoding::BER::decode_unknown
l!l
300
MC/DC0%0/1
$decfnc ||= \&Encoding::BER::decode_unknown
$decfnc
Truth table50%1/2
A
A= $decfnc
Aresult
11
00
1023 30 my $val = $decfnc->( $me, $ndat, $typdat );
1024
1025 # format value in a special pretty way?
1026 30 ▶ if( $pretty ){
Branch: 1 branch
BranchTrueFalseTotal
if ($pretty) 0 30 30
1027 0 ▶ $val = $pretty->( $me, $val ) || $val;
Condition0%0/3
&$pretty($me, $val) || $val
l!l&&r!l&&!r
000
MC/DC0%0/2
&$pretty($me, $val) || $val
&$pretty($me, $val) $val
Truth table0%0/3
A || B
A= &$pretty($me, $val)B= $val
ABresult
1X1
011
000
1028 }
1029 30 $result = $val;
1030 }
1031  
1032 32 $result->{type} = $typdat;
1033 32 $result->{tagnum} = $tagnum;
1034 32 $result->{identval} = $typval;
1035
1036 32 ▶ if( my $c = $me->{decoded_callback} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $me->{"decoded_callback"}) 0 32 32
1037 0 ▶ $result = $c->( $me, $result ) || $result; # make sure the brain hasn't fallen out
Condition0%0/3
&$c($me, $result) || $result
l!l&&r!l&&!r
000
MC/DC0%0/2
&$c($me, $result) || $result
&$c($me, $result) $result
Truth table0%0/3
A || B
A= &$c($me, $result)B= $result
ABresult
1X1
011
000
1038 }
1039 32 return( $result, $tlen );
1040 }
1041  
1042 32 ▶sub app_tag_data_bynumber {
Subroutine app_tag_data_bynumber: called CC=2 SCAR=6.9
Pod undocumented
1043 32 my $me = shift;
1044 32 my $class = shift;
1045 32 my $tnum = shift;
1046
1047 32 my $name = $me->{revtags}{$class}{$tnum};
1048 32 ▶ return unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 31 1 32
1049  
1050 1 $me->{tags}{$name};
1051 }
1052  
1053 # override me in subclass
1054 31 ▶sub subclass_tag_data_bynumber {
Subroutine subclass_tag_data_bynumber: called CC=1 SCAR=0.0
Pod undocumented
1055 31 my $me = shift;
1056 31 my $class = shift;
1057 31 my $tnum = shift;
1058  
1059 31 undef;
1060 }
1061  
1062 31 ▶sub univ_tag_data_bynumber {
Subroutine univ_tag_data_bynumber: called CC=1 SCAR=0.0
Pod undocumented
1063 31 my $me = shift;
1064 31 my $class = shift;
1065 31 my $tnum = shift;
1066  
1067 31 $TAG{$class}{ $REVTAG{$class}{$tnum} };
1068 }
1069  
1070 32 ▶sub tag_data_bynumber {
Subroutine tag_data_bynumber: called CC=3 SCAR=11.0
Pod undocumented
1071 32 my $me = shift;
1072 32 my $class = shift;
1073 32 my $tnum = shift;
1074  
1075 32 my $th;
1076 # application specific tag name
1077 32 $th = $me->app_tag_data_bynumber($class, $tnum);
1078
1079 # subclass specific tag name
1080 32 ▶ $th = $me->subclass_tag_data_bynumber($class, $tnum) unless $th;
Branch: 1 branch
BranchTrueFalseTotal
unless $th 31 1 32
1081  
1082 # from universal
1083 32 ▶ $th = $me->univ_tag_data_bynumber($class, $tnum) unless $th;
Branch: 1 branch
BranchTrueFalseTotal
unless $th 31 1 32
1084  
1085 32 $th;
1086 }
1087  
1088 32 ▶sub ident_descr_and_dfuncs {
Subroutine ident_descr_and_dfuncs: called CC=10 SCAR=28.0
Pod undocumented
1089 32 my $me = shift;
1090 32 my $tval = shift;
1091 32 my $more = shift;
1092  
1093 32 ▶ my $tag = $more || ($tval & 0x1f) || 0;
Condition40%2/5
$more || $tval & 31 || 0
l!l
320
$more or $tval & 31
l!l&&r!l&&!r
0320
MC/DC0%0/2
$more || $tval & 31 || 0
$more $tval & 31
Truth table33%1/3
A or B
A= $moreB= $tval & 31
ABresult
1X1
011
000
1094 32 my $cl = $tval & 0xC0;
1095 32 my $ty = $tval & 0x20;
1096 32 my $class = $REVCLASS{$cl};
1097 32 my $pctyp = $REVTYPE{$ty};
1098  
1099 32 my( $th, $tn, $tf, $tp );
1100  
1101 32 $th = $me->tag_data_bynumber($class, $tag);
1102  
1103 32 ▶ if( ref $th ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $th) { } 32 0 32
elsif ($th) { } 0 0 0
1104 32 $tn = $th->{n};
1105 32 $tp = $th->{pretty};
1106
1107 32 ▶ if( my $impl = $th->{implicit} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $impl = $th->{"implicit"}) 0 32 32
1108 # indirect. we support only one level.
1109 0 my $h = $me->tag_data_byname($impl);
1110 0 ▶ if( ref $h ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h) { } 0 0 0
1111 0 $th = $h;
1112 }else{
1113 0 $me->error("programmer botch. implicit indirect not found: $class/$tn => $impl");
1114 }
1115 }
1116 # primitive decode func or constructed decode func?
1117 32 ▶ $tp ||= $th->{pretty};
Condition33%1/3
$tp ||= $th->{"pretty"}
l!l&&r!l&&!r
0032
MC/DC0%0/2
$tp ||= $th->{"pretty"}
$tp $th->{"pretty"}
Truth table33%1/3
A ||= B
A= $tpB= $th->{"pretty"}
ABresult
1X1
011
000
1118 32 ▶ $tf = $ty ? $th->{dc} : $th->{d};
Branch: 1 branch
BranchTrueFalseTotal
$ty ? : 2 30 32
1119 }elsif( $th ){
1120 0 $me->error("programmer botch. tag data should be hashref: $class/$tag => $th");
1121 }else{
1122 0 $me->warn("unknown type [$class $tag]");
1123 }
1124  
1125 32 ▶ $tn = $tag unless defined $tn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tn 0 32 32
1126  
1127 32 $me->debug("identifier $tval/$tag resolved to [$class $pctyp $tn]");
1128 # [class, type, tagname], decodefunc, tagnumber
1129 32 ([$class, $pctyp, $tn], $tf, $tp, $tag);
1130 }
1131  
1132 32 ▶sub decode_length {
Subroutine decode_length: called CC=4 SCAR=21.7
Pod undocumented
1133 32 my $me = shift;
1134 32 my $data = shift;
1135  
1136 32 my($l1) = unpack('C', $data);
1137  
1138 32 ▶ unless( $l1 & 0x80 ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($l1 & 128) 32 0 32
1139 # x.690 8.1.3.4 - short form
1140 32 return ($l1, 1);
1141 }
1142 0 ▶ if( $l1 == 0x80 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($l1 == 128) 0 0 0
1143 # x.690 8.1.3.6 - indefinite form
1144 0 return (undef, 1);
1145 }
1146  
1147 # x.690 8.1.3.5 - long form
1148 0 my $llen = $l1 & 0x7f;
1149 0 my @l = unpack("C$llen", substr($data, 1));
1150  
1151 0 my $len = 0;
1152 0 for my $l (@l){
1153 0 $len <<= 8;
1154 0 $len += $l;
1155 }
1156
1157 0 ($len, $llen + 1);
1158 }
1159  
1160 32 ▶sub decode_ident {
Subroutine decode_ident: called CC=3 SCAR=18.2
Pod undocumented
1161 32 my $me = shift;
1162 32 my $data = shift;
1163  
1164 32 my($tag) = unpack('C', $data);
1165 32 ▶ return ($tag, 1) unless ($tag & 0x1f) == 0x1f; # x.690 8.1.2.3
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag & 31) == 31 32 0 32
1166  
1167 # x.690 8.1.2.4 - tag numbers > 30
1168 0 my $i = 1;
1169 0 $tag &= ~0x1f;
1170 0 my $more = 0;
1171 0 while(1){
1172 0 my $c = unpack('C', substr($data,$i++,1));
1173 0 $more <<= 7;
1174 0 $more |= ($c & 0x7f);
1175 0 ▶ last unless $c & 0x80;
Branch: 1 branch
BranchTrueFalseTotal
unless $c & 128 0 0 0
1176 }
1177  
1178 0 ($tag, $i, $more);
1179 }
1180  
1181 0 ▶sub decode_bool {
Subroutine decode_bool: not called CC=1 SCAR=6.9
Pod undocumented
1182 0 my $me = shift;
1183 0 my $data = shift;
1184 0 my $type = shift;
1185  
1186 0 my $v = unpack('C', $data);
1187
1188 {
1189 0 value => $v,
1190 };
1191 }
1192  
1193 1 ▶sub decode_null {
Subroutine decode_null: called CC=1 SCAR=0.0
Pod undocumented
1194 1 my $me = shift;
1195 1 my $data = shift;
1196 1 my $type = shift;
1197  
1198 {
1199 1 value => undef,
1200 };
1201 }
1202  
1203 # reassemble constructed string
1204 0 ▶sub reass_string {
Subroutine reass_string: not called CC=2 SCAR=17.9
Pod undocumented
1205 0 my $me = shift;
1206 0 my $vals = shift;
1207 0 my $type = shift;
1208  
1209 0 my $val = '';
1210 0 for my $v (@$vals){
1211 0 $val .= $v->{value};
1212 };
1213  
1214 0 $me->debug('reassemble constructed string');
1215 return {
1216 0 type => [ $type->[0], 'primitive', $type->[2] ],
1217 value => $val,
1218 };
1219
1220 }
1221  
1222 2 ▶sub decode_string {
Subroutine decode_string: called CC=1 SCAR=0.0
Pod undocumented
1223 2 my $me = shift;
1224 2 my $data = shift;
1225 2 my $type = shift;
1226  
1227 {
1228 2 value => $data,
1229 };
1230 }
1231  
1232 0 ▶sub decode_bits {
Subroutine decode_bits: not called CC=1 SCAR=6.9
Pod undocumented
1233 0 my $me = shift;
1234 0 my $data = shift;
1235 0 my $type = shift;
1236  
1237 0 my $pad = unpack('C', $data);
1238 # QQQ - remove padding?
1239
1240 0 $data = substr($data, 1);
1241
1242 {
1243 0 value => $data,
1244 };
1245 }
1246  
1247 4 ▶sub decode_int {
Subroutine decode_int: called CC=1 SCAR=0.0
Pod undocumented
1248 4 my $me = shift;
1249 4 my $data = shift;
1250 4 my $type = shift;
1251  
1252 4 my $val = $me->part_decode_int($data, 1);
1253 4 $me->debug("decode integer: $val");
1254 {
1255 4 value => $val,
1256 };
1257 }
1258  
1259 0 ▶sub decode_uint {
Subroutine decode_uint: not called CC=1 SCAR=6.9
Pod undocumented
1260 0 my $me = shift;
1261 0 my $data = shift;
1262 0 my $type = shift;
1263  
1264 0 my $val = $me->part_decode_int($data, 0);
1265 0 $me->debug("decode unsigned integer: $val");
1266 {
1267 0 value => $val,
1268 };
1269 }
1270  
1271 27 ▶sub part_decode_int {
Subroutine part_decode_int: called CC=8 SCAR=27.3
Pod undocumented
1272 27 my $me = shift;
1273 27 my $data = shift;
1274 27 my $sgnd = shift;
1275  
1276 27 my $val;
1277 my $big;
1278 27 ▶ $big = 1 if _have_math_bigint() && length($data) > 4;
Branch: 1 branch
BranchTrueFalseTotal
if _have_math_bigint() and length $data > 4 0 27 27
Condition33%1/3
_have_math_bigint() and length $data > 4
!ll&&!rl&&r
2700
MC/DC0%0/2
_have_math_bigint() and length $data > 4
_have_math_bigint() length $data > 4
Truth table33%1/3
A and B
A= _have_math_bigint()B= length $data > 4
ABresult
0X0
100
111
1279  
1280 27 ▶ if( $big ){
Branch: 1 branch
BranchTrueFalseTotal
if ($big) { } 0 27 27
1281 0 my $sign = unpack('c', $data) < 0;
1282 0 ▶ if( $sgnd && $sign ){
Branch: 1 branch
BranchTrueFalseTotal
if ($sgnd and $sign) { } 0 0 0
Condition0%0/3
$sgnd and $sign
!ll&&!rl&&r
000
MC/DC0%0/2
$sgnd and $sign
$sgnd $sign
Truth table0%0/3
A and B
A= $sgndB= $sign
ABresult
0X0
100
111
1283 # make negative
1284 0 $val = Math::BigInt->new('0x' . unpack('H*', pack('C*', map {~$_ & 0xff} unpack('C*', $data))));
1285 0 $val->bneg()->bsub(1);
1286 }else{
1287 0 $val = Math::BigInt->new('0x' . unpack('H*', $data));
1288 }
1289
1290 }else{
1291 27 ▶ $val = unpack(($sgnd ? 'c' : 'C'), $data);
Branch: 1 branch
BranchTrueFalseTotal
$sgnd ? : 27 0 27
1292 27 my @o = unpack('C*', $data);
1293 27 shift @o;
1294 27 for my $i (@o){
1295 3 $val *= 256;
1296 3 $val += $i;
1297 }
1298 }
1299  
1300 27 $val;
1301 }
1302  
1303 23 ▶sub decode_real {
Subroutine decode_real: called CC=12 SCAR=27.1
Pod undocumented
1304 23 my $me = shift;
1305 23 my $data = shift;
1306 23 my $type = shift;
1307  
1308 23 $me->debug('decode real');
1309 23 ▶ return { value => 0.0 } unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 23 23
1310  
1311 # POSIX required. available?
1312 23 eval {
1313 23 require POSIX;
1314 };
1315 23 ▶ return $me->error("POSIX not available. cannot decode type real")
Branch: 1 branch
BranchTrueFalseTotal
unless defined &POSIX::frexp 0 23 23
1316 unless defined &POSIX::frexp;
1317  
1318 23 my $first = unpack('C', $data);
1319 23 ▶ return { value => POSIX::HUGE_VAL() } if $first == 0x40;
Branch: 1 branch
BranchTrueFalseTotal
if $first == 64 0 23 23
1320 23 ▶ return { value => - POSIX::HUGE_VAL() } if $first == 0x41;
Branch: 1 branch
BranchTrueFalseTotal
if $first == 65 0 23 23
1321  
1322 23 ▶ if( $first & 0x80 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($first & 128) { } 23 0 23
1323 # binary encoding
1324 23 ▶ my $sign = ($first & 0x40) ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$first & 64 ? : 7 16 23
1325 23 my $base = ($first & 0x30) >> 4;
1326 23 my $scal = [0, 1, -2, -1]->[($first & 0x0C) >> 2];
1327 23 my $expl = ($first & 0x03) + 1;
1328  
1329 23 $data = substr($data, 1);
1330  
1331 23 ▶ if( $expl == 4 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($expl == 4) 0 23 23
1332 0 $expl = unpack('C', $data);
1333 0 $data = substr($data, 1);
1334 }
1335  
1336 23 my $exp = $me->part_decode_int( substr($data, 0, $expl), 1 );
1337 23 $data = substr($data, $expl);
1338 23 my @mant = unpack('C*', $data);
1339 23 $me->debug("decode real: [@mant] $exp");
1340  
1341 # apply scale factor
1342 23 ▶ $exp *= 3 if $base == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 1 0 23 23
1343 23 ▶ $exp *= 4 if $base == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 2 0 23 23
1344 23 ▶ $me->error('corrupt data: invalid base for real') if $base == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 3 0 23 23
1345 23 $exp += $scal;
1346  
1347 # put it together
1348 23 my $val = 0;
1349 23 $exp += (@mant - 1) * 8;
1350 23 for my $m (@mant){
1351 101 $val += POSIX::ldexp($m, $exp);
1352 # $me->debug("decode real: $val ($m, $exp)");
1353 101 $exp -= 8;
1354 }
1355 23 $val *= $sign;
1356
1357 23 $me->debug("decode real: => $val");
1358 23 return { value => $val };
1359 }else{
1360 # decimal encoding
1361 # x.690 8.5.7 - see iso-6093
1362 0 $me->debug('decode real decimal');
1363 0 $data = substr($data, 1);
1364 0 $data =~ s/^([+-]?)0+/$1/; # remove leading 0s
1365 0 $data =~ s/\s//g; # remove spaces
1366 0 $data += 0; # make number
1367
1368 0 return { value => $data };
1369 }
1370
1371 }
1372  
1373 0 ▶sub decode_oid {
Subroutine decode_oid: not called CC=3 SCAR=24.8
Pod undocumented
1374 0 my $me = shift;
1375 0 my $data = shift;
1376 0 my $type = shift;
1377  
1378 0 my @o = unpack('w*', $data);
1379
1380 0 ▶ if( $o[0] < 40 ){
Branch: 2 branches
BranchTrueFalseTotal
if ($o[0] < 40) { } 0 0 0
elsif ($o[0] < 80) { } 0 0 0
1381 0 unshift @o, 0;
1382 }elsif( $o[0] < 80 ){
1383 0 $o[0] -= 40;
1384 0 unshift @o, 1;
1385 }else{
1386 0 $o[0] -= 80;
1387 0 unshift @o, 2;
1388 }
1389  
1390 0 my $val = join('.', @o);
1391 0 $me->debug("decode oid: $val");
1392
1393 {
1394 0 value => $val,
1395 };
1396 }
1397  
1398 0 ▶sub decode_roid {
Subroutine decode_roid: not called CC=1 SCAR=6.9
Pod undocumented
1399 0 my $me = shift;
1400 0 my $data = shift;
1401 0 my $type = shift;
1402  
1403 0 my @o = unpack('w*', $data);
1404
1405 0 my $val = join('.', @o);
1406 0 $me->debug("decode relative-oid: $val");
1407
1408 {
1409 0 value => $val,
1410 };
1411 }
1412  
1413 0 ▶sub decode_unknown {
Subroutine decode_unknown: not called CC=1 SCAR=6.9
Pod undocumented
1414 0 my $me = shift;
1415 0 my $data = shift;
1416 0 my $type = shift;
1417  
1418 0 $me->debug("decode unknown");
1419 {
1420 0 value => $data,
1421 };
1422 }
1423  
1424 87 ▶sub _have_math_bigint {
Subroutine _have_math_bigint: called CC=3 SCAR=11.1
1425  
1426 87 ▶ return unless defined &Math::BigInt::new;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Math::BigInt::new 31 56 87
1427 56 ▶ return unless defined &Math::BigInt::is_neg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Math::BigInt::is_neg 0 56 56
1428  
1429 56 1;
1430 }
1431
1432 ################################################################
1433  
1434 0 ▶sub hexdump {
Subroutine hexdump: not called CC=3 SCAR=24.8
Pod undocumented
1435 0 my $b = shift;
1436 0 my $tag = shift;
1437 0 my( $l, $t );
1438  
1439 0 ▶ print STDERR "$tag:\n" if $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $tag 0 0 0
1440 0 ▶ while( $b ){
Branch: 1 branch
BranchTrueFalseTotal
if ($b) 0 0 0
1441 0 $t = $l = substr($b, 0, 16, '');
1442 0 $l =~ s/(.)/sprintf('%0.2X ',ord($1))/ges;
1443 0 $l =~ s/(.{24})/$1 /;
1444 0 $t =~ s/[[:^print:]]/./gs;
1445 0 my $p = ' ' x (49 - (length $l));
1446 0 print STDERR " $l $p$t\n";
1447 }
1448 }
1449  
1450 4 ▶sub import {
Subroutine import: called CC=3 SCAR=14.2
1451 4 my $pkg = shift;
1452 4 my $caller = caller;
1453  
1454 4 for my $f (@_){
1455 3 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1456 0 my $fnc = $pkg->can($f);
1457 0 ▶ next unless $fnc;
Branch: 1 branch
BranchTrueFalseTotal
unless $fnc 0 0 0
1458 0 *{$caller . '::' . $f} = $fnc;
1459 }
1460 }
1461  
1462 =back
1463  
1464 =head1 ENCODING DATA
1465  
1466 You can give data to the encoder in either of two ways (or mix and match).
1467  
1468 You can specify simple values directly, and the module will guess the
1469 correct tags to use. Things that look like integers will be encoded as
1470 C<integer>, things that look like floating-point numbers will be encoded
1471 as C<real>, things that look like strings, will be encoded as C<octet_string>.
1472 Arrayrefs will be encoded as C<sequence>.
1473  
1474 example:
1475 $enc->encode( [0, 1.2, "foobar", [ "baz", 37.94 ]] );
1476  
1477 Alternatively, you can explicity specify the type using a hashref
1478 containing C<type> and C<value> keys.
1479  
1480 example:
1481 $enc->encode( { type => 'sequence',
1482 value => [
1483 { type => 'integer',
1484 value => 37 } ] } );
1485  
1486 The type may be specfied as either a string containg the tag-name, or
1487 as an arryref containing the class, type, and tag-name.
1488  
1489 example:
1490 type => 'octet_string'
1491 type => ['universal', 'primitive', 'octet_string']
1492  
1493 Note: using the second form above, you can create wacky encodings
1494 that no one will be able to decode.
1495
1496 The value should be a scalar value for primitive types, and an
1497 arrayref for constructed types.
1498  
1499 example:
1500 { type => 'octet_string', value => 'foobar' }
1501 { type => 'set', value => [ 1, 2, 3 ] }
1502  
1503 { type => ['universal', 'constructed', 'octet_string'],
1504 value => [ 'foo', 'bar' ] }
1505  
1506 =head1 DECODED DATA
1507  
1508 The values returned from decoding will be similar to the way data to
1509 be encoded is specified, in the full long form. Additionally, the hashref
1510 will contain: C<identval> the numeric value representing the class+type+tag
1511 and C<tagnum> the numeric tag number.
1512  
1513 example:
1514 a string might be returned as:
1515 { type => ['universal', 'primitive', 'octet_string'],
1516 identval => 4,
1517 tagnum => 4,
1518 value => 'foobar',
1519 }
1520  
1521  
1522 =head1 TAG NAMES
1523  
1524 The following are recognized as valid names of tags:
1525  
1526 bit_string bmp_string bool boolean character_string embedded_pdv
1527 enum enumerated external float general_string generalized_time
1528 graphic_string ia5_string int int32 integer integer32 iso646_string
1529 null numeric_string object_descriptor object_identifier octet_string
1530 oid printable_string real relative_object_identifier relative_oid
1531 roid sequence sequence_of set set_of string t61_string teletex_string
1532 uint uint32 universal_string universal_time unsigned_int unsigned_int32
1533 unsigned_integer utf8_string videotex_string visible_string
1534  
1535 =head1 Math::BigInt
1536  
1537 If you have Math::BigInt, it can be used for large integers. If you want it used,
1538 you must load it yourself:
1539  
1540 use Math::BigInt;
1541 use Encoding::BER;
1542  
1543 It can be used for both encoding and decoding. The encoder can be handed either
1544 a Math::BigInt object, or a "big string of digits" marked as an integer:
1545  
1546 use math::BigInt;
1547  
1548 my $x = Math::BigInt->new( '12345678901234567890' );
1549 $enc->encode( $x )
1550  
1551 $enc->encode( { type => 'integer', '12345678901234567890' } );
1552  
1553 During decoding, a Math::BigInt object will be created if the value "looks big".
1554  
1555
1556 =head1 EXPORTS
1557  
1558 By default, this module exports nothing. This can be overridden by specifying
1559 something else:
1560  
1561 use Encoding::BER ('import', 'hexdump');
1562  
1563 =head1 LIMITATIONS
1564  
1565 If your application uses the same tag-number for more than one type of implicitly
1566 tagged primitive, the decoder will not be able to distinguish between them, and will
1567 not be able to decode them both correctly. eg:
1568  
1569 width ::= [context 12] implicit integer
1570 girth ::= [context 12] implicit real
1571  
1572 If you specify data to be encoded using the "short form", the module may
1573 guess the type differently than you expect. If it matters, be explicit.
1574  
1575 This module does not do data validation. It will happily let you encode
1576 a non-ascii string as a C<ia5_string>, etc.
1577  
1578
1579 =head1 PREREQUISITES
1580  
1581 If you wish to use C<real>s, the POSIX module is required. It will be loaded
1582 automatically, if needed.
1583  
1584 Familiarity with ASN.1 and BER encoding is probably required to take
1585 advantage of this module.
1586  
1587 =head1 SEE ALSO
1588
1589 Yellowstone National Park
1590 Encoding::BER::CER, Encoding::BER::DER
1591 Encoding::BER::SNMP, Encoding::BER::Dumper
1592 ITU-T x.690
1593
1594 =head1 AUTHOR
1595  
1596 Jeff Weisberg - http://www.tcp4me.com
1597  
1598 =cut
1599 ;
1600  
1601 ################################################################
1602 3 1;
1603  
« blib/lib/Encoding/BER/CER.pm ↑ Summary blib/lib/Encoding/BER/SNMP.pm »