lib/Encoding/BER.pm

filter: Statementstmt 77.6% 521 / 671 Branchbran 64.9% 165 / 254 Conditioncond 56.8% 54 / 95 MC/DCmcdc 32.7% 18 / 55 Subroutinesub 85.4% 53 / 62 Podpod 12.2% 7 / 57 totaltotal 68.5% 818 / 1194 CC 172 SCAR 25.0
CC
172
Cov
73%
decode_item
37.5
encode_length
22.9
encode_real
20.2
CRAP 12.2
« 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.9 2007/03/06 02:50:10 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.00';
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 sub new {
204 94 ▶ my $cl = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
205 94 my $me = bless { @_ }, $cl;
206  
207 94 $me;
208 }
209  
210 sub error {
211 0 ▶ my $me = shift;
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
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 sub warn {
223 1 ▶ my $me = shift;
Subroutine warn: called CC=2 SCAR=7.2
Pod documented
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 sub debug {
235 642 ▶ my $me = shift;
Subroutine debug: called CC=2 SCAR=8.4
Pod documented
236 642 my $msg = shift;
237  
238 642 ▶ return unless $me->{debug};
Branch: 1 branch
BranchTrueFalseTotal
unless $me->{"debug"} 642 0 642
239 0 print STDERR " " x $me->{level}, $msg, "\n";
240 0 undef;
241 }
242  
243 ################################################################
244  
245 sub add_tag_hash {
246 139 ▶ my $me = shift;
Subroutine add_tag_hash: called CC=3 SCAR=11.0
Pod undocumented
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 sub add_implicit_tag {
284 138 ▶ my $me = shift;
Subroutine add_implicit_tag: called CC=2 SCAR=7.0
Pod documented
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 sub add_tag {
300 1 ▶ my $me = shift;
Subroutine add_tag: called CC=1 SCAR=0.0
Pod undocumented
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 sub init_tag_lookups {
320 3 ▶ my $TAG = shift;
Subroutine init_tag_lookups: called CC=3 SCAR=11.0
Pod undocumented
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 sub encode {
349 115 ▶ my $me = shift;
Subroutine encode: called CC=14 SCAR=26.6
Pod documented
350 115 my $data = shift;
351 115 my $levl = shift;
352
353 115 ▶ $me->{level} = $levl || 0;
Condition100%2/2
$levl || 0
l!l
2293
MC/DC100%1/1
$levl || 0
$levl
Truth table100%2/2
A
A= $levl
Aresult
11
00
354 115 ▶ $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) 66 49 115
Condition100%3/3
$me->{"acanonical"} or not $me->behaves_like_a_hash($data)
l!l&&r!l&&!r
115549
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 115 ▶ if( $data->{type} eq 'BER_preencoded' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"type"} eq "BER_preencoded") 0 115 115
358 0 return $data->{value};
359 }
360
361 115 ▶ $data = $me->rule_check_and_apply($data) || $data;
Condition33%1/3
$me->rule_check_and_apply($data) || $data
l!l&&r!l&&!r
01150
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 115 my($typeval, $tagnum, $encfnc) = $me->ident_data_and_efunc($data->{type});
363 115 my $value;
364  
365 115 ▶ if( $typeval & 0x20 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($typeval & 32) { } 5 110 115
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 22 $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 110 $me->debug( "encode primitive ($typeval/$tagnum)" );
376
377 110 ▶ unless( $encfnc ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($encfnc) 1 109 110
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 110 $value = $encfnc->($me, $data);
384 }
385  
386 115 my $defp = $me->use_definite_form($typeval, $data);
387 115 my $leng = $me->encode_length(length($value));
388  
389 115 my $res;
390 115 ▶ if( $defp && defined($leng) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($defp and defined $leng) { } 114 1 115
Condition66%2/3
$defp and defined $leng
!ll&&!rl&&r
10114
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 114 $me->debug("encode definite form");
392 114 $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 115 $data->{dlen} = length($value);
400 115 $data->{tlen} = length($res);
401  
402 115 $res;
403 }
404  
405 sub encode_null {
406 2 ▶ my $me = shift;
Subroutine encode_null: called CC=1 SCAR=0.0
Pod undocumented
407 2 $me->debug('encode null');
408 2 '';
409 }
410  
411 sub encode_unknown {
412 1 ▶ my $me = shift;
Subroutine encode_unknown: called CC=1 SCAR=0.0
Pod undocumented
413 1 my $data = shift;
414  
415 1 $me->debug('encode unknown');
416 1 '' . $data->{value};
417 }
418  
419 sub encode_string {
420 11 ▶ my $me = shift;
Subroutine encode_string: called CC=1 SCAR=0.0
Pod undocumented
421 11 my $data = shift;
422  
423 # CER splitting of long strings is handled in CER subclass
424 11 $me->debug('encode string');
425 11 '' . $data->{value};
426 }
427  
428 sub encode_bits {
429 1 ▶ my $me = shift;
Subroutine encode_bits: called CC=1 SCAR=0.0
Pod undocumented
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 sub encode_bool {
439 2 ▶ my $me = shift;
Subroutine encode_bool: called CC=2 SCAR=6.9
Pod undocumented
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 sub encode_int {
448 48 ▶ my $me = shift;
Subroutine encode_int: called CC=15 SCAR=27.1
Pod undocumented
449 48 my $data = shift;
450 48 my $val = $data->{value};
451  
452 48 my @i;
453 my $big;
454  
455 48 ▶ if( defined &Math::BigInt::new ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Math::BigInt::new) 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 sub encode_uint {
493 12 ▶ my $me = shift;
Subroutine encode_uint: called CC=8 SCAR=20.9
Pod undocumented
494 12 my $data = shift;
495 12 my $val = $data->{value};
496
497 12 my @i;
498 my $big;
499
500 12 ▶ if( defined &Math::BigInt::new ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Math::BigInt::new) 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 sub encode_uint32 {
523 2 ▶ my $me = shift;
Subroutine encode_uint32: called CC=1 SCAR=0.0
Pod undocumented
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 sub encode_real {
533 29 ▶ my $me = shift;
Subroutine encode_real: called CC=19 SCAR=30.1
Pod undocumented
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 sub encode_oid {
614 1 ▶ my $me = shift;
Subroutine encode_oid: called CC=4 SCAR=14.1
Pod undocumented
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 sub encode_roid {
634 1 ▶ my $me = shift;
Subroutine encode_roid: called CC=3 SCAR=11.2
Pod undocumented
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 sub encode_ident {
652 115 ▶ my $me = shift;
Subroutine encode_ident: called CC=2 SCAR=6.9
Pod undocumented
653 115 my $type = shift;
654 115 my $tnum = shift;
655  
656 115 ▶ if( $tnum < 31 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($tnum < 31) 114 1 115
657 114 return pack('C', $type|$tnum);
658 }
659 1 $type |= 0x1f;
660 1 pack('Cw', $type, $tnum);
661 }
662  
663 sub encode_length {
664 115 ▶ my $me = shift;
Subroutine encode_length: called CC=6 SCAR=31.3
Pod undocumented
665 115 my $len = shift;
666  
667 115 ▶ return pack('C', $len) if $len < 128; # x.690 8.1.3.4
Branch: 1 branch
BranchTrueFalseTotal
if $len < 128 115 0 115
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 sub rule_check_and_apply {
679 115 ▶ my $me = shift;
Subroutine rule_check_and_apply: called CC=1 SCAR=0.0
Pod undocumented
680 115 my $data = shift;
681  
682 115 undef;
683 }
684  
685 # convert DWIM values => canonical form
686 sub canonicalize {
687 66 ▶ my $me = shift;
Subroutine canonicalize: called CC=6 SCAR=17.9
Pod undocumented
688 66 my $data = shift;
689
690 # arrayref | int | float | string | undef
691  
692 66 ▶ unless( defined $data ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 2 64 66
693 return {
694 2 type => 'null',
695 value => undef,
696 };
697 }
698
699 64 ▶ if( $me->behaves_like_an_array($data) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($me->behaves_like_an_array($data)) 3 61 64
700 return {
701 3 type => 'sequence',
702 value => $data,
703 };
704 }
705  
706 61 ▶ if( $me->behaves_like_a_hash($data) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($me->behaves_like_a_hash($data)) 1 60 61
707 return {
708 1 type => ['application', 'constructed', 3],
709 value => [ %$data ],
710 };
711 }
712
713 60 ▶ if( $me->smells_like_a_number($data) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($me->smells_like_a_number($data)) 51 9 60
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 9 type => 'octet_string',
723 value => $data,
724 };
725 }
726  
727 # tags added via add_tag method
728 sub app_tag_data_byname {
729 266 ▶ my $me = shift;
Subroutine app_tag_data_byname: called CC=1 SCAR=0.0
Pod undocumented
730 266 my $name = shift;
731  
732 266 $me->{tags}{$name};
733 }
734  
735 # override me in subclass
736 sub subclass_tag_data_byname {
737 252 ▶ my $me = shift;
Subroutine subclass_tag_data_byname: called CC=1 SCAR=0.0
Pod undocumented
738 252 my $name = shift;
739  
740 252 undef;
741 }
742  
743 # from the table up top
744 sub univ_tag_data_byname {
745 252 ▶ my $me = shift;
Subroutine univ_tag_data_byname: called CC=3 SCAR=11.6
Pod undocumented
746 252 my $name = shift;
747  
748 252 ▶ $ALLTAG{$name} || ($AKATAG{$name} && $ALLTAG{$AKATAG{$name}});
Branch: 1 branch
BranchTrueFalseTotal
$ALLTAG{$name} or $AKATAG{$name} and $ALLTAG{$AKATAG{$name}} 157 95 252
Condition50%3/6
$ALLTAG{$name} || $AKATAG{$name} && $ALLTAG{$AKATAG{$name}}
l!l&&r!l&&!r
951570
$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 sub tag_data_byname {
752 266 ▶ my $me = shift;
Subroutine tag_data_byname: called CC=3 SCAR=11.0
Pod undocumented
753 266 my $name = shift;
754  
755 266 my $th;
756 # application specific tag name
757 266 $th = $me->app_tag_data_byname($name);
758
759 # subclass specific tag name
760 266 ▶ $th = $me->subclass_tag_data_byname($name) unless $th;
Branch: 1 branch
BranchTrueFalseTotal
unless $th 252 14 266
761
762 # universal tag name
763 266 ▶ $th = $me->univ_tag_data_byname($name) unless $th;
Branch: 1 branch
BranchTrueFalseTotal
unless $th 252 14 266
764  
765 266 $th;
766 }
767  
768 sub class_and_type_from_speclist {
769 132 ▶ my $me = shift;
Subroutine class_and_type_from_speclist: called CC=4 SCAR=14.0
Pod undocumented
770 132 my($class, $type);
771 132 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 132 ($class, $type);
777 }
778  
779 sub ident_data_and_efunc {
780 115 ▶ my $me = shift;
Subroutine ident_data_and_efunc: called CC=17 SCAR=29.7
Pod undocumented
781 115 my $typd = shift;
782 115 my $func = shift;
783  
784 115 ▶ $func ||= 'e';
Condition50%1/2
$func ||= "e"
l!l
0115
MC/DC0%0/1
$func ||= "e"
$func
Truth table50%1/2
A
A= $func
Aresult
11
00
785 115 ▶ my @t = ref($typd) ? @$typd : ($typd);
Branch: 1 branch
BranchTrueFalseTotal
ref $typd ? : 3 112 115
786
787 # type: name | [class, type, name] | [class, type, num]
788 # if name resolves, specified class+type for validation only
789  
790 115 my $tname = pop @t;
791 115 ▶ if( $me->smells_like_a_number($tname) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($me->smells_like_a_number($tname)) 2 113 115
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 113 my $th = $me->tag_data_byname($tname);
802  
803 113 ▶ unless( $th ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($th) 0 113 113
804 0 $me->error("unknown type [$tname]");
805 }
806 113 ▶ unless( ref $th ){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $th) 0 113 113
807 0 $me->error("programmer botch. tag data should be hashref: [$tname] => $th");
808 0 $th = undef;
809 }
810  
811 113 my( $class, $type, $rclass, $rtype, $tnum, $encf );
812  
813 # parse request
814 113 ($rclass, $rtype) = $me->class_and_type_from_speclist( @t );
815 # parse spec
816 113 ▶ if( my $ts = $th->{type} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $ts = $th->{"type"}) 17 96 113
817 17 ($class, $type) = $me->class_and_type_from_speclist( @$ts );
818 }
819  
820 # use these values for identifier-value
821 113 ▶ $class ||= 'universal';
Condition100%2/2
$class ||= "universal"
l!l
1499
MC/DC100%1/1
$class ||= "universal"
$class
Truth table100%2/2
A
A= $class
Aresult
11
00
822 113 ▶ $type = $rtype || $type || 'primitive';
Condition100%5/5
$rtype || $type || 'primitive'
l!l
1895
$rtype or $type
l!l&&r!l&&!r
11795
MC/DC100%2/2
$rtype || $type || 'primitive'
$rtype $type
Truth table100%3/3
A or B
A= $rtypeB= $type
ABresult
1X1
011
000
823 113 $tnum = $th->{v};
824  
825 113 $me->debug("specificication [@t $tname] resolved to [$class $type $tname($tnum)]");
826 # warn if mismatched
827 113 ▶ $me->warn("specificication [$rclass $tname] resolved to [$class $tname]")
Branch: 1 branch
BranchTrueFalseTotal
if $rclass and $rclass ne $class 0 113 113
Condition33%1/3
$rclass and $rclass ne $class
!ll&&!rl&&r
11300
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 113 $encf = $th->{$func};
832 113 ▶ if( my $impl = $th->{implicit} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $impl = $th->{"implicit"}) 15 98 113
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 113 my $tv = $CLASS{$class}{v} | $TYPE{$type}{v};
845 113 return( $tv, $tnum, $encf );
846 }
847  
848 sub use_definite_form {
849 115 ▶ my $me = shift;
Subroutine use_definite_form: called CC=6 SCAR=18.4
Pod undocumented
850 115 my $type = shift;
851 115 my $data = shift;
852
853 115 ▶ return 1 unless $type & 0x20; # x.690 8.1.3.2 - primitive - always definite
Branch: 1 branch
BranchTrueFalseTotal
unless $type & 32 110 5 115
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 sub behaves_like_an_array {
865 64 ▶ my $me = shift;
Subroutine behaves_like_an_array: called CC=2 SCAR=6.9
Pod undocumented
866 64 my $d = shift;
867  
868 64 ▶ return unless ref $d;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $d 39 25 64
869 25 return UNIVERSAL::isa($d, 'ARRAY');
870 }
871  
872 sub behaves_like_a_hash {
873 165 ▶ my $me = shift;
Subroutine behaves_like_a_hash: called CC=3 SCAR=11.0
Pod undocumented
874 165 my $d = shift;
875  
876 165 ▶ return unless ref $d;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $d 71 94 165
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 sub smells_like_a_number {
884 175 ▶ my $me = shift;
Subroutine smells_like_a_number: called CC=3 SCAR=11.0
Pod undocumented
885 175 my $d = shift;
886  
887 175 ▶ return 1 if ref $d && UNIVERSAL::isa($d, 'Math::BigInt');
Branch: 1 branch
BranchTrueFalseTotal
if ref $d and UNIVERSAL::isa($d, "Math::BigInt") 21 154 175
Condition66%2/3
ref $d and UNIVERSAL::isa($d, "Math::BigInt")
!ll&&!rl&&r
154021
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 154 local $^W = 0;
890 154 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 sub decode {
907 24 ▶ my $me = shift;
Subroutine decode: called CC=1 SCAR=0.0
Pod documented
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 sub decode_items {
916 2 ▶ my $me = shift;
Subroutine decode_items: called CC=5 SCAR=16.8
Pod undocumented
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) 10 2 12
926 10 my($val, $len) = $me->decode_item($data, $levl+1);
927 10 $tlen += $len;
928 10 ▶ unless( $val && defined $val->{type} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($val and defined $val->{"type"}) 0 10 10
Condition33%1/3
$val and defined $val->{"type"}
!ll&&!rl&&r
0010
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 10 push @v, $val;
935 10 $data = substr($data, $len);
936 }
937  
938 2 $me->{level} = $levl;
939 2 $me->debug(']');
940 2 return (\@v, $tlen);
941 }
942  
943 sub decode_item {
944 34 ▶ my $me = shift;
Subroutine decode_item: called CC=16 SCAR=36.3
Pod undocumented
945 34 my $data = shift;
946 34 my $levl = shift;
947
948 # hexdump($data, 'di:');
949 34 $me->{level} = $levl;
950 34 my($typval, $typlen, $typmore) = $me->decode_ident($data);
951 34 my($typdat, $decfnc, $pretty, $tagnum) = $me->ident_descr_and_dfuncs($typval, $typmore);
952 34 my($datlen, $lenlen) = $me->decode_length(substr($data,$typlen));
953 34 my $havlen = length($data);
954 34 ▶ my $tlen = $typlen + $lenlen + ($datlen || 0);
Condition100%2/2
$datlen || 0
l!l
331
MC/DC100%1/1
$datlen || 0
$datlen
Truth table100%2/2
A
A= $datlen
Aresult
11
00
955 34 my $doff = $typlen + $lenlen;
956 34 my $result;
957
958 34 ▶ $me->error("corrupt data? data appears truncated")
Branch: 1 branch
BranchTrueFalseTotal
if $havlen < $tlen 0 34 34
959 if $havlen < $tlen;
960  
961 34 ▶ if( $typval & 0x20 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($typval & 32) { } 2 32 34
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 32 my $ndat;
993 32 ▶ if( defined $datlen ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $datlen) { } 32 0 32
994 # definite
995 32 $me->debug("decode item: primitive definite [@$typdat($tagnum)]");
996 32 $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 32 ▶ unless( $typval || $typmore ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($typval or $typmore) 0 32 32
Condition33%1/3
$typval or $typmore
l!l&&r!l&&!r
3200
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 32 ▶ $decfnc ||= \&decode_unknown;
Condition50%1/2
$decfnc ||= \&Encoding::BER::decode_unknown
l!l
320
MC/DC0%0/1
$decfnc ||= \&Encoding::BER::decode_unknown
$decfnc
Truth table50%1/2
A
A= $decfnc
Aresult
11
00
1023 32 my $val = $decfnc->( $me, $ndat, $typdat );
1024
1025 # format value in a special pretty way?
1026 32 ▶ if( $pretty ){
Branch: 1 branch
BranchTrueFalseTotal
if ($pretty) 0 32 32
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 32 $result = $val;
1030 }
1031  
1032 34 $result->{type} = $typdat;
1033 34 $result->{tagnum} = $tagnum;
1034 34 $result->{identval} = $typval;
1035
1036 34 ▶ if( my $c = $me->{decoded_callback} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $me->{"decoded_callback"}) 0 34 34
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 34 return( $result, $tlen );
1040 }
1041  
1042 sub app_tag_data_bynumber {
1043 34 ▶ my $me = shift;
Subroutine app_tag_data_bynumber: called CC=2 SCAR=6.9
Pod undocumented
1044 34 my $class = shift;
1045 34 my $tnum = shift;
1046
1047 34 my $name = $me->{revtags}{$class}{$tnum};
1048 34 ▶ return unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 33 1 34
1049  
1050 1 $me->{tags}{$name};
1051 }
1052  
1053 # override me in subclass
1054 sub subclass_tag_data_bynumber {
1055 33 ▶ my $me = shift;
Subroutine subclass_tag_data_bynumber: called CC=1 SCAR=0.0
Pod undocumented
1056 33 my $class = shift;
1057 33 my $tnum = shift;
1058  
1059 33 undef;
1060 }
1061  
1062 sub univ_tag_data_bynumber {
1063 33 ▶ my $me = shift;
Subroutine univ_tag_data_bynumber: called CC=1 SCAR=0.0
Pod undocumented
1064 33 my $class = shift;
1065 33 my $tnum = shift;
1066  
1067 33 $TAG{$class}{ $REVTAG{$class}{$tnum} };
1068 }
1069  
1070 sub tag_data_bynumber {
1071 34 ▶ my $me = shift;
Subroutine tag_data_bynumber: called CC=3 SCAR=11.0
Pod undocumented
1072 34 my $class = shift;
1073 34 my $tnum = shift;
1074  
1075 34 my $th;
1076 # application specific tag name
1077 34 $th = $me->app_tag_data_bynumber($class, $tnum);
1078
1079 # subclass specific tag name
1080 34 ▶ $th = $me->subclass_tag_data_bynumber($class, $tnum) unless $th;
Branch: 1 branch
BranchTrueFalseTotal
unless $th 33 1 34
1081  
1082 # from universal
1083 34 ▶ $th = $me->univ_tag_data_bynumber($class, $tnum) unless $th;
Branch: 1 branch
BranchTrueFalseTotal
unless $th 33 1 34
1084  
1085 34 $th;
1086 }
1087  
1088 sub ident_descr_and_dfuncs {
1089 34 ▶ my $me = shift;
Subroutine ident_descr_and_dfuncs: called CC=10 SCAR=28.0
Pod undocumented
1090 34 my $tval = shift;
1091 34 my $more = shift;
1092  
1093 34 ▶ my $tag = $more || ($tval & 0x1f) || 0;
Condition40%2/5
$more || $tval & 31 || 0
l!l
340
$more or $tval & 31
l!l&&r!l&&!r
0340
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 34 my $cl = $tval & 0xC0;
1095 34 my $ty = $tval & 0x20;
1096 34 my $class = $REVCLASS{$cl};
1097 34 my $pctyp = $REVTYPE{$ty};
1098  
1099 34 my( $th, $tn, $tf, $tp );
1100  
1101 34 $th = $me->tag_data_bynumber($class, $tag);
1102  
1103 34 ▶ if( ref $th ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $th) { } 34 0 34
elsif ($th) { } 0 0 0
1104 34 $tn = $th->{n};
1105 34 $tp = $th->{pretty};
1106
1107 34 ▶ if( my $impl = $th->{implicit} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $impl = $th->{"implicit"}) 0 34 34
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 34 ▶ $tp ||= $th->{pretty};
Condition33%1/3
$tp ||= $th->{"pretty"}
l!l&&r!l&&!r
0034
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 34 ▶ $tf = $ty ? $th->{dc} : $th->{d};
Branch: 1 branch
BranchTrueFalseTotal
$ty ? : 2 32 34
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 34 ▶ $tn = $tag unless defined $tn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tn 0 34 34
1126  
1127 34 $me->debug("identifier $tval/$tag resolved to [$class $pctyp $tn]");
1128 # [class, type, tagname], decodefunc, tagnumber
1129 34 ([$class, $pctyp, $tn], $tf, $tp, $tag);
1130 }
1131  
1132 sub decode_length {
1133 34 ▶ my $me = shift;
Subroutine decode_length: called CC=4 SCAR=21.7
Pod undocumented
1134 34 my $data = shift;
1135  
1136 34 my($l1) = unpack('C', $data);
1137  
1138 34 ▶ unless( $l1 & 0x80 ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($l1 & 128) 34 0 34
1139 # x.690 8.1.3.4 - short form
1140 34 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 sub decode_ident {
1161 34 ▶ my $me = shift;
Subroutine decode_ident: called CC=3 SCAR=18.2
Pod undocumented
1162 34 my $data = shift;
1163  
1164 34 my($tag) = unpack('C', $data);
1165 34 ▶ return ($tag, 1) unless ($tag & 0x1f) == 0x1f; # x.690 8.1.2.3
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag & 31) == 31 34 0 34
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 sub decode_bool {
1182 0 ▶ my $me = shift;
Subroutine decode_bool: not called CC=1 SCAR=6.9
Pod undocumented
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 sub decode_null {
1194 1 ▶ my $me = shift;
Subroutine decode_null: called CC=1 SCAR=0.0
Pod undocumented
1195 1 my $data = shift;
1196 1 my $type = shift;
1197  
1198 {
1199 1 value => undef,
1200 };
1201 }
1202  
1203 # reassemble constructed string
1204 sub reass_string {
1205 0 ▶ my $me = shift;
Subroutine reass_string: not called CC=2 SCAR=17.9
Pod undocumented
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 sub decode_string {
1223 4 ▶ my $me = shift;
Subroutine decode_string: called CC=1 SCAR=0.0
Pod undocumented
1224 4 my $data = shift;
1225 4 my $type = shift;
1226  
1227 {
1228 4 value => $data,
1229 };
1230 }
1231  
1232 sub decode_bits {
1233 0 ▶ my $me = shift;
Subroutine decode_bits: not called CC=1 SCAR=6.9
Pod undocumented
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 sub decode_int {
1248 4 ▶ my $me = shift;
Subroutine decode_int: called CC=1 SCAR=0.0
Pod undocumented
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 sub decode_uint {
1260 0 ▶ my $me = shift;
Subroutine decode_uint: not called CC=1 SCAR=6.9
Pod undocumented
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 sub part_decode_int {
1272 27 ▶ my $me = shift;
Subroutine part_decode_int: called CC=8 SCAR=27.3
Pod undocumented
1273 27 my $data = shift;
1274 27 my $sgnd = shift;
1275  
1276 27 my $val;
1277 my $big;
1278 27 ▶ $big = 1 if defined &Math::BigInt::new && length($data) > 4;
Branch: 1 branch
BranchTrueFalseTotal
if defined &Math::BigInt::new and length $data > 4 0 27 27
Condition33%1/3
defined &Math::BigInt::new and length $data > 4
!ll&&!rl&&r
2700
MC/DC0%0/2
defined &Math::BigInt::new and length $data > 4
defined &Math::BigInt::new length $data > 4
Truth table33%1/3
A and B
A= defined &Math::BigInt::newB= 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 sub decode_real {
1304 23 ▶ my $me = shift;
Subroutine decode_real: called CC=12 SCAR=27.1
Pod undocumented
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 sub decode_oid {
1374 0 ▶ my $me = shift;
Subroutine decode_oid: not called CC=3 SCAR=24.8
Pod undocumented
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 sub decode_roid {
1399 0 ▶ my $me = shift;
Subroutine decode_roid: not called CC=1 SCAR=6.9
Pod undocumented
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 sub decode_unknown {
1414 0 ▶ my $me = shift;
Subroutine decode_unknown: not called CC=1 SCAR=6.9
Pod undocumented
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 ################################################################
1425  
1426 sub hexdump {
1427 0 ▶ my $b = shift;
Subroutine hexdump: not called CC=3 SCAR=24.8
Pod undocumented
1428 0 my $tag = shift;
1429 0 my( $l, $t );
1430  
1431 0 ▶ print STDERR "$tag:\n" if $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $tag 0 0 0
1432 0 ▶ while( $b ){
Branch: 1 branch
BranchTrueFalseTotal
if ($b) 0 0 0
1433 0 $t = $l = substr($b, 0, 16, '');
1434 0 $l =~ s/(.)/sprintf('%0.2X ',ord($1))/ges;
1435 0 $l =~ s/(.{24})/$1 /;
1436 0 $t =~ s/[[:^print:]]/./gs;
1437 0 my $p = ' ' x (49 - (length $l));
1438 0 print STDERR " $l $p$t\n";
1439 }
1440 }
1441  
1442 sub import {
1443 4 ▶ my $pkg = shift;
Subroutine import: called CC=3 SCAR=14.2
1444 4 my $caller = caller;
1445  
1446 4 for my $f (@_){
1447 3 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1448 0 my $fnc = $pkg->can($f);
1449 0 ▶ next unless $fnc;
Branch: 1 branch
BranchTrueFalseTotal
unless $fnc 0 0 0
1450 0 *{$caller . '::' . $f} = $fnc;
1451 }
1452 }
1453  
1454 =back
1455  
1456 =head1 ENCODING DATA
1457  
1458 You can give data to the encoder in either of two ways (or mix and match).
1459  
1460 You can specify simple values directly, and the module will guess the
1461 correct tags to use. Things that look like integers will be encoded as
1462 C<integer>, things that look like floating-point numbers will be encoded
1463 as C<real>, things that look like strings, will be encoded as C<octet_string>.
1464 Arrayrefs will be encoded as C<sequence>.
1465  
1466 example:
1467 $enc->encode( [0, 1.2, "foobar", [ "baz", 37.94 ]] );
1468  
1469 Alternatively, you can explicity specify the type using a hashref
1470 containing C<type> and C<value> keys.
1471  
1472 example:
1473 $enc->encode( { type => 'sequence',
1474 value => [
1475 { type => 'integer',
1476 value => 37 } ] } );
1477  
1478 The type may be specfied as either a string containg the tag-name, or
1479 as an arryref containing the class, type, and tag-name.
1480  
1481 example:
1482 type => 'octet_string'
1483 type => ['universal', 'primitive', 'octet_string']
1484  
1485 Note: using the second form above, you can create wacky encodings
1486 that no one will be able to decode.
1487
1488 The value should be a scalar value for primitive types, and an
1489 arrayref for constructed types.
1490  
1491 example:
1492 { type => 'octet_string', value => 'foobar' }
1493 { type => 'set', value => [ 1, 2, 3 ] }
1494  
1495 { type => ['universal', 'constructed', 'octet_string'],
1496 value => [ 'foo', 'bar' ] }
1497  
1498 =head1 DECODED DATA
1499  
1500 The values returned from decoding will be similar to the way data to
1501 be encoded is specified, in the full long form. Additionally, the hashref
1502 will contain: C<identval> the numeric value representing the class+type+tag
1503 and C<tagnum> the numeric tag number.
1504  
1505 example:
1506 a string might be returned as:
1507 { type => ['universal', 'primitive', 'octet_string'],
1508 identval => 4,
1509 tagnum => 4,
1510 value => 'foobar',
1511 }
1512  
1513  
1514 =head1 TAG NAMES
1515  
1516 The following are recognized as valid names of tags:
1517  
1518 bit_string bmp_string bool boolean character_string embedded_pdv
1519 enum enumerated external float general_string generalized_time
1520 graphic_string ia5_string int int32 integer integer32 iso646_string
1521 null numeric_string object_descriptor object_identifier octet_string
1522 oid printable_string real relative_object_identifier relative_oid
1523 roid sequence sequence_of set set_of string t61_string teletex_string
1524 uint uint32 universal_string universal_time unsigned_int unsigned_int32
1525 unsigned_integer utf8_string videotex_string visible_string
1526  
1527 =head1 Math::BigInt
1528  
1529 If you have Math::BigInt, it can be used for large integers. If you want it used,
1530 you must load it yourself:
1531  
1532 use Math::BigInt;
1533 use Encoding::BER;
1534  
1535 It can be used for both encoding and decoding. The encoder can be handed either
1536 a Math::BigInt object, or a "big string of digits" marked as an integer:
1537  
1538 use math::BigInt;
1539  
1540 my $x = Math::BigInt->new( '12345678901234567890' );
1541 $enc->encode( $x )
1542  
1543 $enc->encode( { type => 'integer', '12345678901234567890' } );
1544  
1545 During decoding, a Math::BigInt object will be created if the value "looks big".
1546  
1547
1548 =head1 EXPORTS
1549  
1550 By default, this module exports nothing. This can be overridden by specifying
1551 something else:
1552  
1553 use Encoding::BER ('import', 'hexdump');
1554  
1555 =head1 LIMITATIONS
1556  
1557 If your application uses the same tag-number for more than one type of implicitly
1558 tagged primitive, the decoder will not be able to distinguish between them, and will
1559 not be able to decode them both correctly. eg:
1560  
1561 width ::= [context 12] implicit integer
1562 girth ::= [context 12] implicit real
1563  
1564 If you specify data to be encoded using the "short form", the module may
1565 guess the type differently than you expect. If it matters, be explicit.
1566  
1567 This module does not do data validation. It will happily let you encode
1568 a non-ascii string as a C<ia5_string>, etc.
1569  
1570
1571 =head1 PREREQUISITES
1572  
1573 If you wish to use C<real>s, the POSIX module is required. It will be loaded
1574 automatically, if needed.
1575  
1576 Familiarity with ASN.1 and BER encoding is probably required to take
1577 advantage of this module.
1578  
1579 =head1 SEE ALSO
1580
1581 Yellowstone National Park
1582 Encoding::BER::CER, Encoding::BER::DER
1583 Encoding::BER::SNMP, Encoding::BER::Dumper
1584 ITU-T x.690
1585
1586 =head1 AUTHOR
1587  
1588 Jeff Weisberg - http://www.tcp4me.com
1589  
1590 =cut
1591 ;
1592  
1593 ################################################################
1594 3 1;
1595  
« blib/lib/Encoding/BER/CER.pm ↑ Summary blib/lib/Encoding/BER/SNMP.pm »