File Coverage

blib/lib/Convert/ASN1/_decode.pm
Criterion Covered Total %
statement 229 281 81.4
branch 155 234 66.2
condition 48 98 48.9
subroutine 21 22 95.4
pod n/a
total 453 635 71.3


line stmt bran cond sub pod time code
1             # Copyright (c) 2000-2005 Graham Barr . All rights reserved.
2             # This program is free software; you can redistribute it and/or
3             # modify it under the same terms as Perl itself.
4              
5             package Convert::ASN1;
6             $Convert::ASN1::VERSION = '0.34';
7 23     23   162 use strict;
  23         71  
  23         718  
8 23     23   116 use warnings;
  23         42  
  23         1206  
9              
10             BEGIN {
11 23     23   128 local $SIG{__DIE__};
12 23 50       45 eval { require bytes and 'bytes'->import };
  23         202  
13             }
14              
15             # These are the subs that do the decode, they are called with
16             # 0 1 2 3 4
17             # $optn, $op, $stash, $var, $buf
18             # The order must be the same as the op definitions above
19              
20             my @decode = (
21             sub { die "internal error\n" },
22             \&_dec_boolean,
23             \&_dec_integer,
24             \&_dec_bitstring,
25             \&_dec_string,
26             \&_dec_null,
27             \&_dec_object_id,
28             \&_dec_real,
29             \&_dec_sequence,
30             \&_dec_explicit,
31             \&_dec_set,
32             \&_dec_time,
33             \&_dec_time,
34             \&_dec_utf8,
35             undef, # ANY
36             undef, # CHOICE
37             \&_dec_object_id,
38             \&_dec_bcd,
39             );
40              
41             my @ctr;
42             @ctr[opBITSTR, opSTRING, opUTF8] = (\&_ctr_bitstring,\&_ctr_string,\&_ctr_string);
43              
44              
45             sub _decode {
46 516     516   1314 my ($optn, $ops, $stash, $pos, $end, $seqof, $larr) = @_;
47 516         786 my $idx = 0;
48              
49             # we try not to copy the input buffer at any time
50 516         935 foreach my $buf ($_[-1]) {
51             OP:
52 516         681 foreach my $op (@{$ops}) {
  516         907  
53 898         1390 my $var = $op->[cVAR];
54              
55 898 100       1596 if (length $op->[cTAG]) {
56              
57             TAGLOOP: {
58 724         917 my($tag,$len,$npos,$indef) = _decode_tl($buf,$pos,$end,$larr)
59 843 100       1492 or do {
60 25 50 33     137 next OP if $pos==$end and ($seqof || defined $op->[cEXT]);
      66        
61 2         28 die "decode error";
62             };
63              
64 818 100       1839 if ($tag eq $op->[cTAG]) {
65              
66 752         1933 &{$decode[$op->[cTYPE]]}(
67             $optn,
68             $op,
69             $stash,
70             # We send 1 if there is not var as if there is the decode
71             # should be getting undef. So if it does not get undef
72             # it knows it has no variable
73 752 100       2122 ($seqof ? $seqof->[$idx++] : defined($var) ? $stash->{$var} : ref($stash) eq 'SCALAR' ? $$stash : 1),
    100          
    100          
74             $buf,$npos,$len, $larr
75             );
76              
77 752         1226 $pos = $npos+$len+$indef;
78              
79 752 100 100     1790 redo TAGLOOP if $seqof && $pos < $end;
80 633         1362 next OP;
81             }
82              
83 66 100 66     223 if ($tag eq ($op->[cTAG] | pack("C",ASN_CONSTRUCTOR))
84             and my $ctr = $ctr[$op->[cTYPE]])
85             {
86 1         21 _decode(
87             $optn,
88             [$op],
89             undef,
90             $npos,
91             $npos+$len,
92             (\my @ctrlist),
93             $larr,
94             $buf,
95             );
96              
97             ($seqof
98             ? $seqof->[$idx++] # = &{$ctr}(@ctrlist);
99             : defined($var)
100             ? $stash->{$var} # = &{$ctr}(@ctrlist);
101             : ref($stash) eq 'SCALAR'
102             ? $$stash # = &{$ctr}(@ctrlist);
103             : my $any ) # = &{$ctr}(@ctrlist) FIX #43
104 1 0       3 = &{$ctr}(@ctrlist);
  1 50       3  
    50          
105              
106 1         2 $pos = $npos+$len+$indef;
107              
108 1 50 33     4 redo TAGLOOP if $seqof && $pos < $end;
109 1         3 next OP;
110              
111             }
112              
113 65 50 33     217 if ($seqof || defined $op->[cEXT]) {
114 65         123 next OP;
115             }
116              
117 0   0     0 die "decode error " . unpack("H*",$tag) ."<=>" . unpack("H*",$op->[cTAG]), " ",$pos," ",$op->[cTYPE]," ",$op->[cVAR]||'';
118             }
119             }
120             else { # opTag length is zero, so it must be an ANY, CHOICE or EXTENSIONS
121            
122 174 100       393 if ($op->[cTYPE] == opANY) {
    100          
    50          
123              
124             ANYLOOP: {
125              
126 33         44 my($tag,$len,$npos,$indef) = _decode_tl($buf,$pos,$end,$larr)
127 34 100       71 or do {
128 2 50 33     14 next OP if $pos==$end and ($seqof || defined $op->[cEXT]);
      33        
129 0         0 die "decode error";
130             };
131              
132 32         76 $len += $npos - $pos + $indef;
133              
134 32         43 my $handler;
135 32 100       74 if ($op->[cDEFINE]) {
136 2   33     11 $handler = $optn->{oidtable} && $optn->{oidtable}{$stash->{$op->[cDEFINE]}};
137 2   33     13 $handler ||= $optn->{handlers}{$op->[cVAR]}{$stash->{$op->[cDEFINE]}};
138             }
139              
140 32 100       141 ($seqof ? $seqof->[$idx++] : ref($stash) eq 'SCALAR' ? $$stash : $stash->{$var})
    100          
    100          
141             = $handler ? $handler->decode(substr($buf,$pos,$len)) : substr($buf,$pos,$len);
142              
143 32         59 $pos += $len;
144              
145 32 100 100     114 redo ANYLOOP if $seqof && $pos < $end;
146             }
147             }
148             elsif ($op->[cTYPE] == opCHOICE) {
149              
150             CHOICELOOP: {
151 137         177 my($tag,$len,$npos,$indef) = _decode_tl($buf,$pos,$end,$larr)
152 140 50       259 or do {
153 0 0 0     0 next OP if $pos==$end and ($seqof || defined $op->[cEXT]);
      0        
154 0         0 die "decode error";
155             };
156 140         245 my $extensions;
157 140         201 foreach my $cop (@{$op->[cCHILD]}) {
  140         294  
158              
159 273 100       533 if ($tag eq $cop->[cTAG]) {
160              
161             my $nstash = $seqof
162             ? ($seqof->[$idx++]={})
163             : defined($var)
164 137 100       438 ? ($stash->{$var}={})
    100          
    100          
165             : ref($stash) eq 'SCALAR'
166             ? ($$stash={}) : $stash;
167              
168 137         346 &{$decode[$cop->[cTYPE]]}(
169             $optn,
170             $cop,
171             $nstash,
172 137 50       383 ($cop->[cVAR] ? $nstash->{$cop->[cVAR]} : undef),
173             $buf,$npos,$len,$larr,
174             );
175              
176 137         232 $pos = $npos+$len+$indef;
177              
178 137 100 100     271 redo CHOICELOOP if $seqof && $pos < $end;
179 135         317 next OP;
180             }
181              
182 136 100       245 if ($cop->[cTYPE] == opEXTENSIONS) {
183 2         4 $extensions = 1;
184 2         14 next;
185             }
186              
187 134 100       230 unless (length $cop->[cTAG]) {
188 2 50       4 eval {
189 2         44 _decode(
190             $optn,
191             [$cop],
192             (\my %tmp_stash),
193             $pos,
194             $npos+$len+$indef,
195             undef,
196             $larr,
197             $buf,
198             );
199              
200             my $nstash = $seqof
201             ? ($seqof->[$idx++]={})
202             : defined($var)
203 2 0       9 ? ($stash->{$var}={})
    0          
    50          
204             : ref($stash) eq 'SCALAR'
205             ? ($$stash={}) : $stash;
206              
207 2         7 @{$nstash}{keys %tmp_stash} = values %tmp_stash;
  2         9  
208              
209             } or next;
210              
211 2         4 $pos = $npos+$len+$indef;
212              
213 2 100 66     9 redo CHOICELOOP if $seqof && $pos < $end;
214 1         3 next OP;
215             }
216              
217 132 50 33     325 if ($tag eq ($cop->[cTAG] | pack("C",ASN_CONSTRUCTOR))
218             and my $ctr = $ctr[$cop->[cTYPE]])
219             {
220             my $nstash = $seqof
221             ? ($seqof->[$idx++]={})
222             : defined($var)
223 0 0       0 ? ($stash->{$var}={})
    0          
    0          
224             : ref($stash) eq 'SCALAR'
225             ? ($$stash={}) : $stash;
226              
227 0         0 _decode(
228             $optn,
229             [$cop],
230             undef,
231             $npos,
232             $npos+$len,
233             (\my @ctrlist),
234             $larr,
235             $buf,
236             );
237              
238 0         0 $nstash->{$cop->[cVAR]} = &{$ctr}(@ctrlist);
  0         0  
239 0         0 $pos = $npos+$len+$indef;
240              
241 0 0 0     0 redo CHOICELOOP if $seqof && $pos < $end;
242 0         0 next OP;
243             }
244             }
245              
246 1 50 33     6 if ($pos < $end && $extensions) {
247 1         2 $pos = $npos+$len+$indef;
248              
249 1 50 33     21 redo CHOICELOOP if $seqof && $pos < $end;
250 1         5 next OP;
251             }
252             }
253 0 0       0 die "decode error" unless $op->[cEXT];
254             }
255             elsif ($op->[cTYPE] == opEXTENSIONS) {
256 4         10 $pos = $end; # Skip over the rest
257             }
258             else {
259 0         0 die "this point should never be reached";
260             }
261             }
262             }
263             }
264 514 100       1206 die "decode error $pos $end" unless $pos == $end;
265             }
266              
267              
268             sub _dec_boolean {
269             # 0 1 2 3 4 5 6
270             # $optn, $op, $stash, $var, $buf, $pos, $len
271              
272 26 100   26   202 $_[3] = unpack("C",substr($_[4],$_[5],1)) ? 1 : 0;
273 26         47 1;
274             }
275              
276              
277             sub _dec_integer {
278             # 0 1 2 3 4 5 6
279             # $optn, $op, $stash, $var, $buf, $pos, $len
280              
281 89     89   224 my $buf = substr($_[4],$_[5],$_[6]);
282 89 100       293 my $tmp = unpack("C",$buf) & 0x80 ? pack("C",255) : pack("C",0);
283 89 100       240 if ($_[6] > 4) {
284 16   100     100 $_[3] = os2ip($buf, $_[0]->{decode_bigint} || 'Math::BigInt');
285             } else {
286             # N unpacks an unsigned value
287 73         481 $_[3] = unpack("l",pack("l",unpack("N", $tmp x (4-$_[6]) . $buf)));
288             }
289 89         1674 1;
290             }
291              
292              
293             sub _dec_bitstring {
294             # 0 1 2 3 4 5 6
295             # $optn, $op, $stash, $var, $buf, $pos, $len
296              
297 22     22   120 $_[3] = [ substr($_[4],$_[5]+1,$_[6]-1), ($_[6]-1)*8-unpack("C",substr($_[4],$_[5],1)) ];
298 22         41 1;
299             }
300              
301              
302             sub _dec_string {
303             # 0 1 2 3 4 5 6
304             # $optn, $op, $stash, $var, $buf, $pos, $len
305              
306 187     187   534 $_[3] = substr($_[4],$_[5],$_[6]);
307 187         282 1;
308             }
309              
310              
311             sub _dec_null {
312             # 0 1 2 3 4 5 6
313             # $optn, $op, $stash, $var, $buf, $pos, $len
314              
315 2 50   2   18 $_[3] = exists($_[0]->{decode_null}) ? $_[0]->{decode_null} : 1;
316 2         4 1;
317             }
318              
319              
320             sub _dec_object_id {
321             # 0 1 2 3 4 5 6
322             # $optn, $op, $stash, $var, $buf, $pos, $len
323              
324 162     162   460 my @data = unpack("w*",substr($_[4],$_[5],$_[6]));
325 162 100 66     621 if ($_[1]->[cTYPE] == opOBJID and @data > 1) {
326 159 100       369 if ($data[0] < 40) {
    100          
327 4         12 splice(@data, 0, 1, 0, $data[0]);
328             }
329             elsif ($data[0] < 80) {
330 36         82 splice(@data, 0, 1, 1, $data[0] - 40);
331             }
332             else {
333 119         244 splice(@data, 0, 1, 2, $data[0] - 80);
334             }
335             }
336 162         663 $_[3] = join(".", @data);
337 162         285 1;
338             }
339              
340              
341             my @_dec_real_base = (2,8,16);
342              
343             sub _dec_real {
344             # 0 1 2 3 4 5 6
345             # $optn, $op, $stash, $var, $buf, $pos, $len
346              
347 7 100   7   28 $_[3] = 0.0, return unless $_[6];
348              
349 6         17 my $first = unpack("C",substr($_[4],$_[5],1));
350 6 100       21 if ($first & 0x80) {
    50          
    0          
351             # A real number
352              
353 4         19 require POSIX;
354              
355 4         10 my $exp;
356 4         11 my $expLen = $first & 0x3;
357 4         9 my $estart = $_[5]+1;
358              
359 4 50       8 if($expLen == 3) {
360 0         0 $estart++;
361 0         0 $expLen = unpack("C",substr($_[4],$_[5]+1,1));
362             }
363             else {
364 4         9 $expLen++;
365             }
366 4         13 _dec_integer(undef, undef, undef, $exp, $_[4],$estart,$expLen);
367              
368 4         8 my $mant = 0.0;
369 4         16 for (reverse unpack("C*",substr($_[4],$estart+$expLen,$_[6]-1-$expLen))) {
370 5         17 $exp +=8, $mant = (($mant+$_) / 256) ;
371             }
372              
373 4         12 $mant *= 1 << (($first >> 2) & 0x3);
374 4 100       12 $mant = - $mant if $first & 0x40;
375              
376 4         68 $_[3] = $mant * POSIX::pow($_dec_real_base[($first >> 4) & 0x3], $exp);
377 4         522 return;
378             }
379             elsif($first & 0x40) {
380 2 100       7 $_[3] = POSIX::HUGE_VAL(),return if $first == 0x40;
381 1 50       18 $_[3] = - POSIX::HUGE_VAL(),return if $first == 0x41;
382             }
383             elsif(substr($_[4],$_[5],$_[6]) =~ /^.([-+]?)0*(\d+(?:\.\d+(?:[Ee][-+]?\d+)?)?)$/s) {
384 0         0 $_[3] = eval "$1$2";
385 0         0 return;
386             }
387              
388 0         0 die "REAL decode error\n";
389             }
390              
391              
392             sub _dec_explicit {
393             # 0 1 2 3 4 5 6 7
394             # $optn, $op, $stash, $var, $buf, $pos, $len, $larr
395              
396 20 100   20   67 local $_[1][cCHILD][0][cVAR] = $_[1][cVAR] unless $_[1][cCHILD][0][cVAR];
397              
398 20         109 _decode(
399             $_[0], #optn
400             $_[1]->[cCHILD], #ops
401             $_[2], #stash
402             $_[5], #pos
403             $_[5]+$_[6], #end
404             undef, #loop
405             $_[7],
406             $_[4], #buf
407             );
408 20         30 1;
409             }
410             sub _dec_sequence {
411             # 0 1 2 3 4 5 6 7
412             # $optn, $op, $stash, $var, $buf, $pos, $len, $larr
413              
414 361 50   361   676 if (defined( my $ch = $_[1]->[cCHILD])) {
415 361 100 100     2558 _decode(
      100        
416             $_[0], #optn
417             $ch, #ops
418             (defined($_[3]) || $_[1]->[cLOOP]) ? $_[2] : ($_[3]= {}), #stash
419             $_[5], #pos
420             $_[5]+$_[6], #end
421             $_[1]->[cLOOP] && ($_[3]=[]), #loop
422             $_[7],
423             $_[4], #buf
424             );
425             }
426             else {
427 0         0 $_[3] = substr($_[4],$_[5],$_[6]);
428             }
429 361         555 1;
430             }
431              
432              
433             sub _dec_set {
434             # 0 1 2 3 4 5 6 7
435             # $optn, $op, $stash, $var, $buf, $pos, $len, $larr
436              
437             # decode SET OF the same as SEQUENCE OF
438 8     8   29 my $ch = $_[1]->[cCHILD];
439 8 50 33     35 goto &_dec_sequence if $_[1]->[cLOOP] or !defined($ch);
440              
441 8         33 my ($optn, $pos, $larr) = @_[0,5,7];
442 8 50       22 my $stash = defined($_[3]) ? $_[2] : ($_[3]={});
443 8         12 my $end = $pos + $_[6];
444 8         23 my @done;
445             my $extensions;
446              
447 8         16 while ($pos < $end) {
448 26 50       51 my($tag,$len,$npos,$indef) = _decode_tl($_[4],$pos,$end,$larr)
449             or die "decode error";
450              
451 26         62 my ($idx, $any, $done) = (-1);
452              
453             SET_OP:
454 26         56 foreach my $op (@$ch) {
455 69         95 $idx++;
456 69 100       195 if (length($op->[cTAG])) {
    50          
    50          
    50          
457 55 100       116 if ($tag eq $op->[cTAG]) {
458 25         44 my $var = $op->[cVAR];
459 25         70 &{$decode[$op->[cTYPE]]}(
460             $optn,
461             $op,
462             $stash,
463             # We send 1 if there is not var as if there is the decode
464             # should be getting undef. So if it does not get undef
465             # it knows it has no variable
466 25 50       73 (defined($var) ? $stash->{$var} : 1),
467             $_[4],$npos,$len,$larr,
468             );
469 25         41 $done = $idx;
470 25         49 last SET_OP;
471             }
472 30 50 33     94 if ($tag eq ($op->[cTAG] | pack("C",ASN_CONSTRUCTOR))
473             and my $ctr = $ctr[$op->[cTYPE]])
474             {
475 0         0 _decode(
476             $optn,
477             [$op],
478             undef,
479             $npos,
480             $npos+$len,
481             (\my @ctrlist),
482             $larr,
483             $_[4],
484             );
485              
486 0 0       0 $stash->{$op->[cVAR]} = &{$ctr}(@ctrlist)
  0         0  
487             if defined $op->[cVAR];
488 0         0 $done = $idx;
489 0         0 last SET_OP;
490             }
491 30         49 next SET_OP;
492             }
493             elsif ($op->[cTYPE] == opANY) {
494 0         0 $any = $idx;
495             }
496             elsif ($op->[cTYPE] == opCHOICE) {
497 0         0 my $var = $op->[cVAR];
498 0         0 foreach my $cop (@{$op->[cCHILD]}) {
  0         0  
499 0 0       0 if ($tag eq $cop->[cTAG]) {
500 0 0       0 my $nstash = defined($var) ? ($stash->{$var}={}) : $stash;
501              
502 0         0 &{$decode[$cop->[cTYPE]]}(
503             $optn,
504             $cop,
505             $nstash,
506 0         0 $nstash->{$cop->[cVAR]},
507             $_[4],$npos,$len,$larr,
508             );
509 0         0 $done = $idx;
510 0         0 last SET_OP;
511             }
512 0 0 0     0 if ($tag eq ($cop->[cTAG] | pack("C",ASN_CONSTRUCTOR))
513             and my $ctr = $ctr[$cop->[cTYPE]])
514             {
515 0 0       0 my $nstash = defined($var) ? ($stash->{$var}={}) : $stash;
516              
517 0         0 _decode(
518             $optn,
519             [$cop],
520             undef,
521             $npos,
522             $npos+$len,
523             (\my @ctrlist),
524             $larr,
525             $_[4],
526             );
527              
528 0         0 $nstash->{$cop->[cVAR]} = &{$ctr}(@ctrlist);
  0         0  
529 0         0 $done = $idx;
530 0         0 last SET_OP;
531             }
532             }
533             }
534             elsif ($op->[cTYPE] == opEXTENSIONS) {
535 14         24 $extensions = $idx;
536             }
537             else {
538 0         0 die "internal error";
539             }
540             }
541              
542 26 50 66     76 if (!defined($done) and defined($any)) {
543 0         0 my $var = $ch->[$any][cVAR];
544 0 0       0 $stash->{$var} = substr($_[4],$pos,$len+$npos-$pos) if defined $var;
545 0         0 $done = $any;
546             }
547              
548 26 100 66     58 if( !defined($done) && defined($extensions) ) {
549 1         3 $done = $extensions;
550             }
551              
552 26 50 33     108 die "decode error" if !defined($done) or $done[$done]++;
553              
554 26         59 $pos = $npos + $len + $indef;
555             }
556              
557 8 50       24 die "decode error" unless $end == $pos;
558              
559 8         17 foreach my $idx (0..$#{$ch}) {
  8         27  
560 30 50 100     97 die "decode error" unless $done[$idx] or $ch->[$idx][cEXT] or $ch->[$idx][cTYPE] == opEXTENSIONS;
      66        
561             }
562              
563 8         17 1;
564             }
565              
566              
567             my %_dec_time_opt = ( unixtime => 0, withzone => 1, raw => 2);
568              
569             sub _dec_time {
570             # 0 1 2 3 4 5 6
571             # $optn, $op, $stash, $var, $buf, $pos, $len
572              
573 24   50 24   123 my $mode = $_dec_time_opt{$_[0]->{'decode_time'} || ''} || 0;
574              
575 24 50 33     86 if ($mode == 2 or $_[6] == 0) {
576 0         0 $_[3] = substr($_[4],$_[5],$_[6]);
577 0         0 return;
578             }
579              
580 24 50       254 my @bits = (substr($_[4],$_[5],$_[6])
581             =~ /^((?:\d\d)?\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)((?:\.\d{1,3})?)(([-+])(\d\d)(\d\d)|Z)/)
582             or die "bad time format";
583              
584 24 100       85 if ($bits[0] < 100) {
585 19 100       48 $bits[0] += 100 if $bits[0] < 50;
586             }
587             else {
588 5         48 $bits[0] -= 1900;
589             }
590 24         47 $bits[1] -= 1;
591 24         1407 require Time::Local;
592 24         5110 my $time = Time::Local::timegm(@bits[5,4,3,2,1,0]);
593 24 100       785 $time += $bits[6] if length $bits[6];
594 24         37 my $offset = 0;
595 24 100       49 if ($bits[7] ne 'Z') {
596 5         14 $offset = $bits[9] * 3600 + $bits[10] * 60;
597 5 100       12 $offset = -$offset if $bits[8] eq '-';
598 5         7 $time -= $offset;
599             }
600 24 50       104 $_[3] = $mode ? [$time,$offset] : $time;
601             }
602              
603              
604             sub _dec_utf8 {
605             # 0 1 2 3 4 5 6
606             # $optn, $op, $stash, $var, $buf, $pos, $len
607              
608             BEGIN {
609 23     23   98772 unless (CHECK_UTF8) {
610             local $SIG{__DIE__};
611             eval { require bytes } and 'bytes'->unimport;
612             eval { require utf8 } and 'utf8'->import;
613             }
614             }
615              
616 2     2   3 if (CHECK_UTF8) {
617 2         10 $_[3] = Encode::decode('utf8', substr($_[4],$_[5],$_[6]));
618             }
619             else {
620             $_[3] = (substr($_[4],$_[5],$_[6]) =~ /(.*)/s)[0];
621             }
622              
623 2         121 1;
624             }
625              
626              
627             sub _decode_tl {
628 1043     1043   2025 my($pos,$end,$larr) = @_[1,2,3];
629              
630 1043 100       1931 return if $pos >= $end;
631              
632 1018         1360 my $indef = 0;
633              
634 1018         1752 my $tag = substr($_[0], $pos++, 1);
635              
636 1018 100       2458 if((unpack("C",$tag) & 0x1f) == 0x1f) {
637 1         2 my $b;
638 1         1 my $n=1;
639 1         2 do {
640 5 100       14 return if $pos >= $end;
641 4         7 $tag .= substr($_[0],$pos++,1);
642 4         16 $b = ord substr($tag,-1);
643             } while($b & 0x80);
644             }
645 1017 50       1854 return if $pos >= $end;
646              
647 1017         1614 my $len = ord substr($_[0],$pos++,1);
648              
649 1017 100       1775 if($len & 0x80) {
650 112         142 $len &= 0x7f;
651              
652 112 100       205 if ($len) {
653 102 50       179 return if $pos+$len > $end ;
654              
655 102 50       213 my $padding = $len < 4 ? "\0" x (4 - $len) : "";
656 102         250 ($len,$pos) = (unpack("N", $padding . substr($_[0],$pos,$len)), $pos+$len);
657             }
658             else {
659 10 100       38 unless (exists $larr->{$pos}) {
660 5 100       54 _scan_indef($_[0],$pos,$end,$larr) or return;
661             }
662 9         21 $indef = 2;
663 9         18 $len = $larr->{$pos};
664             }
665             }
666              
667 1016 50       1954 return if $pos+$len+$indef > $end;
668              
669             # return the tag, the length of the data, the position of the data
670             # and the number of extra bytes for indefinite encoding
671              
672 1016         3292 ($tag, $len, $pos, $indef);
673             }
674              
675             sub _scan_indef {
676 5     5   33 my($pos,$end,$larr) = @_[1,2,3];
677 5         29 my @depth = ( $pos );
678              
679 5         16 while(@depth) {
680 27 50       60 return if $pos+2 > $end;
681              
682 27 100       66 if (substr($_[0],$pos,2) eq "\0\0") {
683 10         16 my $end = $pos;
684 10         17 my $stref = shift @depth;
685             # replace pos with length = end - pos
686 10         25 $larr->{$stref} = $end - $stref;
687 10         13 $pos += 2;
688 10         23 next;
689             }
690 17 50       70 return if $pos >= $end;
691              
692 17         39 my $tag = substr($_[0], $pos++, 1);
693              
694 17 100       55 if((unpack("C",$tag) & 0x1f) == 0x1f) {
695 1         3 my $b;
696 1         7 do {
697 3 100       13 return if $pos >= $end;
698 2         6 $tag .= substr($_[0],$pos++,1);
699 2         5 $b = ord substr($tag,-1);
700             } while($b & 0x80);
701             }
702 16 50       71 return if $pos >= $end;
703              
704 16         59 my $len = ord substr($_[0],$pos++,1);
705              
706 16 100       37 if($len & 0x80) {
707 6 50       21 if ($len &= 0x7f) {
708 0 0       0 return if $pos+$len > $end ;
709              
710 0 0       0 my $padding = $len < 4 ? "\0" x (4 - $len) : "";
711 0         0 $pos += $len + unpack("N", $padding . substr($_[0],$pos,$len));
712             }
713             else {
714             # reserve another list element
715 6         19 unshift @depth, $pos;
716             }
717             }
718             else {
719 10         20 $pos += $len;
720             }
721             }
722              
723 4         20 1;
724             }
725              
726 1     1   7 sub _ctr_string { join '', @_ }
727              
728             sub _ctr_bitstring {
729 0     0   0 [ join('', map { $_->[0] } @_), $_[-1]->[1] ]
  0         0  
730             }
731              
732             sub _dec_bcd {
733             # 0 1 2 3 4 5 6
734             # $optn, $op, $stash, $var, $buf, $pos, $len
735              
736 8     8   57 ($_[3] = unpack("H*", substr($_[4],$_[5],$_[6]))) =~ s/[fF]$//;
737 8         16 1;
738             }
739             1;
740