| 1 | # Copyright (c) 2000-2005 Graham Barr <gbarr@pobox.com>. 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 | 24 | $Convert::ASN1::VERSION = '0.36'; | ||||||||||||||||||||||||||||||||
| 7 | 24 | ▶ | use strict; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 8 | 24 | ▶ | use warnings; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 9 | ||||||||||||||||||||||||||||||||||
| 10 | 24 | ▶ | BEGIN { | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 11 | 24 | unless (CHECK_UTF8) { | ||||||||||||||||||||||||||||||||
| 12 | local $SIG{__DIE__}; | |||||||||||||||||||||||||||||||||
| 13 | eval { require bytes } and 'bytes'->import | |||||||||||||||||||||||||||||||||
| 14 | } | |||||||||||||||||||||||||||||||||
| 15 | } | |||||||||||||||||||||||||||||||||
| 16 | ||||||||||||||||||||||||||||||||||
| 17 | # These are the subs which do the encoding, they are called with | |||||||||||||||||||||||||||||||||
| 18 | # 0 1 2 3 4 5 | |||||||||||||||||||||||||||||||||
| 19 | # $opt, $op, $stash, $var, $buf, $loop | |||||||||||||||||||||||||||||||||
| 20 | # The order in the array must match the op definitions above | |||||||||||||||||||||||||||||||||
| 21 | ||||||||||||||||||||||||||||||||||
| 22 | my @encode = ( | |||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub { die "internal error\n" }, | |||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||
| 24 | 24 | \&_enc_boolean, | ||||||||||||||||||||||||||||||||
| 25 | \&_enc_integer, | |||||||||||||||||||||||||||||||||
| 26 | \&_enc_bitstring, | |||||||||||||||||||||||||||||||||
| 27 | \&_enc_string, | |||||||||||||||||||||||||||||||||
| 28 | \&_enc_null, | |||||||||||||||||||||||||||||||||
| 29 | \&_enc_object_id, | |||||||||||||||||||||||||||||||||
| 30 | \&_enc_real, | |||||||||||||||||||||||||||||||||
| 31 | \&_enc_sequence, | |||||||||||||||||||||||||||||||||
| 32 | \&_enc_sequence, # EXPLICIT is the same encoding as sequence | |||||||||||||||||||||||||||||||||
| 33 | \&_enc_sequence, # SET is the same encoding as sequence | |||||||||||||||||||||||||||||||||
| 34 | \&_enc_time, | |||||||||||||||||||||||||||||||||
| 35 | \&_enc_time, | |||||||||||||||||||||||||||||||||
| 36 | \&_enc_utf8, | |||||||||||||||||||||||||||||||||
| 37 | \&_enc_any, | |||||||||||||||||||||||||||||||||
| 38 | \&_enc_choice, | |||||||||||||||||||||||||||||||||
| 39 | \&_enc_object_id, | |||||||||||||||||||||||||||||||||
| 40 | \&_enc_bcd, | |||||||||||||||||||||||||||||||||
| 41 | ); | |||||||||||||||||||||||||||||||||
| 42 | ||||||||||||||||||||||||||||||||||
| 43 | ||||||||||||||||||||||||||||||||||
| 44 | 150 | ▶ | sub _encode { | |||||||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=10 SCAR=23.0
| ||||||||||||||||||||||||||||||||||
| 45 | 150 | my ($optn, $ops, $stash, $path) = @_; | ||||||||||||||||||||||||||||||||
| 46 | 150 | my $var; | ||||||||||||||||||||||||||||||||
| 47 | ||||||||||||||||||||||||||||||||||
| 48 | 150 | foreach my $op (@{$ops}) { | ||||||||||||||||||||||||||||||||
| 49 | 197 | ▶ | next if $op->[cTYPE] == opEXTENSIONS; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 50 | 192 | ▶ | if (defined(my $opt = $op->[cOPT])) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 51 | 5 | ▶ | next unless defined $stash->{$opt}; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 52 | } | |||||||||||||||||||||||||||||||||
| 53 | 190 | ▶ | if (defined($var = $op->[cVAR])) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 54 | 153 | push @$path, $var; | ||||||||||||||||||||||||||||||||
| 55 | 153 | ▶ | require Carp, Carp::croak(join(".", @$path)," is undefined") unless defined $stash->{$var}; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 56 | } | |||||||||||||||||||||||||||||||||
| 57 | 187 | $_[4] .= $op->[cTAG]; | ||||||||||||||||||||||||||||||||
| 58 | ||||||||||||||||||||||||||||||||||
| 59 | 187 | &{$encode[$op->[cTYPE]]}( | ||||||||||||||||||||||||||||||||
| 60 | $optn, | |||||||||||||||||||||||||||||||||
| 61 | $op, | |||||||||||||||||||||||||||||||||
| 62 | (UNIVERSAL::isa($stash, 'HASH') | |||||||||||||||||||||||||||||||||
| 63 | 187 | ▶ | ? ($stash, defined($var) ? $stash->{$var} : undef) | |||||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||||
| 64 | : ({}, $stash)), | |||||||||||||||||||||||||||||||||
| 65 | $_[4], | |||||||||||||||||||||||||||||||||
| 66 | $op->[cLOOP], | |||||||||||||||||||||||||||||||||
| 67 | $path, | |||||||||||||||||||||||||||||||||
| 68 | ); | |||||||||||||||||||||||||||||||||
| 69 | ||||||||||||||||||||||||||||||||||
| 70 | 185 | ▶ | pop @$path if defined $var; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 71 | } | |||||||||||||||||||||||||||||||||
| 72 | ||||||||||||||||||||||||||||||||||
| 73 | 145 | $_[4]; | ||||||||||||||||||||||||||||||||
| 74 | } | |||||||||||||||||||||||||||||||||
| 75 | ||||||||||||||||||||||||||||||||||
| 76 | ||||||||||||||||||||||||||||||||||
| 77 | 15 | ▶ | sub _enc_boolean { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_boolean: called CC=2 SCAR=6.9
| ||||||||||||||||||||||||||||||||||
| 78 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 79 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 80 | ||||||||||||||||||||||||||||||||||
| 81 | 15 | ▶ | $_[4] .= pack("CC",1, $_[3] ? 0xff : 0); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 82 | } | |||||||||||||||||||||||||||||||||
| 83 | ||||||||||||||||||||||||||||||||||
| 84 | ||||||||||||||||||||||||||||||||||
| 85 | 72 | ▶ | sub _enc_integer { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_integer: called CC=10 SCAR=23.0
| ||||||||||||||||||||||||||||||||||
| 86 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 87 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 88 | 72 | ▶ | if (abs($_[3]) >= 2**31) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 89 | 12 | ▶ | my $os = i2osp($_[3], ref($_[3]) || $_[0]->{encode_bigint} || 'Math::BigInt'); | |||||||||||||||||||||||||||||||
|
Condition80%4/5
ref $_[3] || $_[0]{'encode_bigint'} || 'Math::BigInt'
ref $_[3] or $_[0]{"encode_bigint"}
MC/DC50%1/2
ref $_[3] || $_[0]{'encode_bigint'} || 'Math::BigInt'
ref $_[3]
$_[0]{"encode_bigint"}
Truth table66%2/3
A or B
A= ref $_[3]B= $_[0]{"encode_bigint"}
| ||||||||||||||||||||||||||||||||||
| 90 | 12 | my $len = length $os; | ||||||||||||||||||||||||||||||||
| 91 | 12 | ▶ | my $msb = (vec($os, 0, 8) & 0x80) ? 0 : 255; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 92 | 12 | ▶ | $len++, $os = pack("C",$msb) . $os if $msb xor $_[3] > 0; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
$msb xor $_[3] > 0
MC/DC100%2/2
$msb xor $_[3] > 0
$msb
$_[3] > 0
Truth table100%4/4
A xor B
A= $msbB= $_[3] > 0
| ||||||||||||||||||||||||||||||||||
| 93 | 12 | $_[4] .= asn_encode_length($len); | ||||||||||||||||||||||||||||||||
| 94 | 12 | $_[4] .= $os; | ||||||||||||||||||||||||||||||||
| 95 | } | |||||||||||||||||||||||||||||||||
| 96 | else { | |||||||||||||||||||||||||||||||||
| 97 | 60 | my $val = int($_[3]); | ||||||||||||||||||||||||||||||||
| 98 | 60 | my $neg = ($val < 0); | ||||||||||||||||||||||||||||||||
| 99 | 60 | ▶ | my $len = num_length($neg ? ~$val : $val); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 100 | 60 | my $msb = $val & (0x80 << (($len - 1) * 8)); | ||||||||||||||||||||||||||||||||
| 101 | ||||||||||||||||||||||||||||||||||
| 102 | 60 | ▶ | $len++ if $neg ? !$msb : $msb; | |||||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||||
| 103 | ||||||||||||||||||||||||||||||||||
| 104 | 60 | $_[4] .= asn_encode_length($len); | ||||||||||||||||||||||||||||||||
| 105 | 60 | $_[4] .= substr(pack("N",$val), -$len); | ||||||||||||||||||||||||||||||||
| 106 | } | |||||||||||||||||||||||||||||||||
| 107 | } | |||||||||||||||||||||||||||||||||
| 108 | ||||||||||||||||||||||||||||||||||
| 109 | ||||||||||||||||||||||||||||||||||
| 110 | 4 | ▶ | sub _enc_bitstring { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_bitstring: called CC=6 SCAR=18.6
| ||||||||||||||||||||||||||||||||||
| 111 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 112 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 113 | 4 | ▶ | my $vref = ref($_[3]) ? \($_[3]->[0]) : \$_[3]; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 114 | ||||||||||||||||||||||||||||||||||
| 115 | 4 | ▶ | if (CHECK_UTF8 and Encode::is_utf8($$vref)) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 116 | 0 | utf8::encode(my $tmp = $$vref); | ||||||||||||||||||||||||||||||||
| 117 | 0 | $vref = \$tmp; | ||||||||||||||||||||||||||||||||
| 118 | } | |||||||||||||||||||||||||||||||||
| 119 | ||||||||||||||||||||||||||||||||||
| 120 | 4 | ▶ | if (ref($_[3])) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 121 | 3 | my $less = (8 - ($_[3]->[1] & 7)) & 7; | ||||||||||||||||||||||||||||||||
| 122 | 3 | my $len = ($_[3]->[1] + 7) >> 3; | ||||||||||||||||||||||||||||||||
| 123 | 3 | $_[4] .= asn_encode_length(1+$len); | ||||||||||||||||||||||||||||||||
| 124 | 3 | $_[4] .= pack("C",$less); | ||||||||||||||||||||||||||||||||
| 125 | 3 | $_[4] .= substr($$vref, 0, $len); | ||||||||||||||||||||||||||||||||
| 126 | 3 | ▶ | if ($less && $len) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$less and $len
MC/DC0%0/2
$less and $len
$less
$len
Truth table33%1/3
A and B
A= $lessB= $len
| ||||||||||||||||||||||||||||||||||
| 127 | 3 | substr($_[4],-1) &= pack("C",(0xff << $less) & 0xff); | ||||||||||||||||||||||||||||||||
| 128 | } | |||||||||||||||||||||||||||||||||
| 129 | } | |||||||||||||||||||||||||||||||||
| 130 | else { | |||||||||||||||||||||||||||||||||
| 131 | 1 | $_[4] .= asn_encode_length(1+length $$vref); | ||||||||||||||||||||||||||||||||
| 132 | 1 | $_[4] .= pack("C",0); | ||||||||||||||||||||||||||||||||
| 133 | 1 | $_[4] .= $$vref; | ||||||||||||||||||||||||||||||||
| 134 | } | |||||||||||||||||||||||||||||||||
| 135 | } | |||||||||||||||||||||||||||||||||
| 136 | ||||||||||||||||||||||||||||||||||
| 137 | ||||||||||||||||||||||||||||||||||
| 138 | 41 | ▶ | sub _enc_string { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_string: called CC=2 SCAR=6.9
| ||||||||||||||||||||||||||||||||||
| 139 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 140 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 141 | ||||||||||||||||||||||||||||||||||
| 142 | 41 | ▶ | if (CHECK_UTF8 and Encode::is_utf8($_[3])) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 143 | 1 | utf8::encode(my $tmp = $_[3]); | ||||||||||||||||||||||||||||||||
| 144 | 1 | $_[4] .= asn_encode_length(length $tmp); | ||||||||||||||||||||||||||||||||
| 145 | 1 | $_[4] .= $tmp; | ||||||||||||||||||||||||||||||||
| 146 | } | |||||||||||||||||||||||||||||||||
| 147 | else { | |||||||||||||||||||||||||||||||||
| 148 | 40 | $_[4] .= asn_encode_length(length $_[3]); | ||||||||||||||||||||||||||||||||
| 149 | 40 | $_[4] .= $_[3]; | ||||||||||||||||||||||||||||||||
| 150 | } | |||||||||||||||||||||||||||||||||
| 151 | } | |||||||||||||||||||||||||||||||||
| 152 | ||||||||||||||||||||||||||||||||||
| 153 | ||||||||||||||||||||||||||||||||||
| 154 | 2 | ▶ | sub _enc_null { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_null: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 155 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 156 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 157 | ||||||||||||||||||||||||||||||||||
| 158 | 2 | $_[4] .= pack("C",0); | ||||||||||||||||||||||||||||||||
| 159 | } | |||||||||||||||||||||||||||||||||
| 160 | ||||||||||||||||||||||||||||||||||
| 161 | ||||||||||||||||||||||||||||||||||
| 162 | 9 | ▶ | sub _enc_object_id { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_object_id: called CC=3 SCAR=11.1
| ||||||||||||||||||||||||||||||||||
| 163 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 164 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 165 | ||||||||||||||||||||||||||||||||||
| 166 | 9 | my @data = ($_[3] =~ /(\d+)/g); | ||||||||||||||||||||||||||||||||
| 167 | ||||||||||||||||||||||||||||||||||
| 168 | 9 | ▶ | if ($_[1]->[cTYPE] == opOBJID) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 169 | 6 | ▶ | if(@data < 2) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 170 | 0 | @data = (0); | ||||||||||||||||||||||||||||||||
| 171 | } | |||||||||||||||||||||||||||||||||
| 172 | else { | |||||||||||||||||||||||||||||||||
| 173 | 6 | my $first = $data[1] + ($data[0] * 40); | ||||||||||||||||||||||||||||||||
| 174 | 6 | splice(@data,0,2,$first); | ||||||||||||||||||||||||||||||||
| 175 | } | |||||||||||||||||||||||||||||||||
| 176 | } | |||||||||||||||||||||||||||||||||
| 177 | ||||||||||||||||||||||||||||||||||
| 178 | 9 | my $l = length $_[4]; | ||||||||||||||||||||||||||||||||
| 179 | 9 | $_[4] .= pack("cw*", 0, @data); | ||||||||||||||||||||||||||||||||
| 180 | 9 | substr($_[4],$l,1) = asn_encode_length(length($_[4]) - $l - 1); | ||||||||||||||||||||||||||||||||
| 181 | } | |||||||||||||||||||||||||||||||||
| 182 | ||||||||||||||||||||||||||||||||||
| 183 | ||||||||||||||||||||||||||||||||||
| 184 | 7 | ▶ | sub _enc_real { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_real: called CC=9 SCAR=22.6
| ||||||||||||||||||||||||||||||||||
| 185 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 186 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 187 | ||||||||||||||||||||||||||||||||||
| 188 | # Zero | |||||||||||||||||||||||||||||||||
| 189 | 7 | ▶ | unless ($_[3]) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 190 | 1 | $_[4] .= pack("C",0); | ||||||||||||||||||||||||||||||||
| 191 | 1 | return; | ||||||||||||||||||||||||||||||||
| 192 | } | |||||||||||||||||||||||||||||||||
| 193 | ||||||||||||||||||||||||||||||||||
| 194 | 6 | require POSIX; | ||||||||||||||||||||||||||||||||
| 195 | ||||||||||||||||||||||||||||||||||
| 196 | # +oo (well we use HUGE_VAL as Infinity is not available to perl) | |||||||||||||||||||||||||||||||||
| 197 | 6 | ▶ | if ($_[3] >= POSIX::HUGE_VAL()) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 198 | 1 | $_[4] .= pack("C*",0x01,0x40); | ||||||||||||||||||||||||||||||||
| 199 | 1 | return; | ||||||||||||||||||||||||||||||||
| 200 | } | |||||||||||||||||||||||||||||||||
| 201 | ||||||||||||||||||||||||||||||||||
| 202 | # -oo (well we use HUGE_VAL as Infinity is not available to perl) | |||||||||||||||||||||||||||||||||
| 203 | 5 | ▶ | if ($_[3] <= - POSIX::HUGE_VAL()) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 204 | 1 | $_[4] .= pack("C*",0x01,0x41); | ||||||||||||||||||||||||||||||||
| 205 | 1 | return; | ||||||||||||||||||||||||||||||||
| 206 | } | |||||||||||||||||||||||||||||||||
| 207 | ||||||||||||||||||||||||||||||||||
| 208 | 4 | ▶ | if (exists $_[0]->{'encode_real'} && $_[0]->{'encode_real'} ne 'binary') { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $_[0]{"encode_real"} and $_[0]{"encode_real"} ne "binary"
MC/DC0%0/2
exists $_[0]{"encode_real"} and $_[0]{"encode_real"} ne "binary"
exists $_[0]{"encode_real"}
$_[0]{"encode_real"} ne "binary"
Truth table33%1/3
A and B
A= exists $_[0]{"encode_real"}B= $_[0]{"encode_real"} ne "binary"
| ||||||||||||||||||||||||||||||||||
| 209 | 0 | my $tmp = sprintf("%g",$_[3]); | ||||||||||||||||||||||||||||||||
| 210 | 0 | $_[4] .= asn_encode_length(1+length $tmp); | ||||||||||||||||||||||||||||||||
| 211 | 0 | $_[4] .= pack("C",1); # NR1? | ||||||||||||||||||||||||||||||||
| 212 | 0 | $_[4] .= $tmp; | ||||||||||||||||||||||||||||||||
| 213 | 0 | return; | ||||||||||||||||||||||||||||||||
| 214 | } | |||||||||||||||||||||||||||||||||
| 215 | ||||||||||||||||||||||||||||||||||
| 216 | # We have a real number. | |||||||||||||||||||||||||||||||||
| 217 | 4 | my $first = 0x80; | ||||||||||||||||||||||||||||||||
| 218 | 4 | my($mantissa, $exponent) = POSIX::frexp($_[3]); | ||||||||||||||||||||||||||||||||
| 219 | ||||||||||||||||||||||||||||||||||
| 220 | 4 | ▶ | if ($mantissa < 0.0) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 221 | 1 | $mantissa = -$mantissa; | ||||||||||||||||||||||||||||||||
| 222 | 1 | $first |= 0x40; | ||||||||||||||||||||||||||||||||
| 223 | } | |||||||||||||||||||||||||||||||||
| 224 | 4 | my($eMant,$eExp); | ||||||||||||||||||||||||||||||||
| 225 | ||||||||||||||||||||||||||||||||||
| 226 | 4 | ▶ | while($mantissa > 0.0) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 227 | 5 | ($mantissa, my $int) = POSIX::modf($mantissa * (1<<8)); | ||||||||||||||||||||||||||||||||
| 228 | 5 | $eMant .= pack("C",$int); | ||||||||||||||||||||||||||||||||
| 229 | } | |||||||||||||||||||||||||||||||||
| 230 | 4 | $exponent -= 8 * length $eMant; | ||||||||||||||||||||||||||||||||
| 231 | ||||||||||||||||||||||||||||||||||
| 232 | 4 | _enc_integer(undef, undef, undef, $exponent, $eExp); | ||||||||||||||||||||||||||||||||
| 233 | ||||||||||||||||||||||||||||||||||
| 234 | # $eExp will be prefixed by a length byte | |||||||||||||||||||||||||||||||||
| 235 | ||||||||||||||||||||||||||||||||||
| 236 | 4 | ▶ | if (5 > length $eExp) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 237 | 4 | $eExp =~ s/\A.//s; | ||||||||||||||||||||||||||||||||
| 238 | 4 | $first |= length($eExp)-1; | ||||||||||||||||||||||||||||||||
| 239 | } | |||||||||||||||||||||||||||||||||
| 240 | else { | |||||||||||||||||||||||||||||||||
| 241 | 0 | $first |= 0x3; | ||||||||||||||||||||||||||||||||
| 242 | } | |||||||||||||||||||||||||||||||||
| 243 | ||||||||||||||||||||||||||||||||||
| 244 | 4 | $_[4] .= asn_encode_length(1 + length($eMant) + length($eExp)); | ||||||||||||||||||||||||||||||||
| 245 | 4 | $_[4] .= pack("C",$first); | ||||||||||||||||||||||||||||||||
| 246 | 4 | $_[4] .= $eExp; | ||||||||||||||||||||||||||||||||
| 247 | 4 | $_[4] .= $eMant; | ||||||||||||||||||||||||||||||||
| 248 | } | |||||||||||||||||||||||||||||||||
| 249 | ||||||||||||||||||||||||||||||||||
| 250 | ||||||||||||||||||||||||||||||||||
| 251 | 45 | ▶ | sub _enc_sequence { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_sequence: called CC=5 SCAR=16.2
| ||||||||||||||||||||||||||||||||||
| 252 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 253 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 254 | ||||||||||||||||||||||||||||||||||
| 255 | 45 | ▶ | if (my $ops = $_[1]->[cCHILD]) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 256 | 45 | my $l = length $_[4]; | ||||||||||||||||||||||||||||||||
| 257 | 45 | $_[4] .= "\0\0"; # guess | ||||||||||||||||||||||||||||||||
| 258 | 45 | ▶ | if (defined $_[5]) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 259 | 11 | my $op = $ops->[0]; # there should only be one | ||||||||||||||||||||||||||||||||
| 260 | 11 | my $enc = $encode[$op->[cTYPE]]; | ||||||||||||||||||||||||||||||||
| 261 | 11 | my $tag = $op->[cTAG]; | ||||||||||||||||||||||||||||||||
| 262 | 11 | my $loop = $op->[cLOOP]; | ||||||||||||||||||||||||||||||||
| 263 | ||||||||||||||||||||||||||||||||||
| 264 | 11 | push @{$_[6]}, -1; | ||||||||||||||||||||||||||||||||
| 265 | ||||||||||||||||||||||||||||||||||
| 266 | 11 | foreach my $var (@{$_[3]}) { | ||||||||||||||||||||||||||||||||
| 267 | 32 | $_[6]->[-1]++; | ||||||||||||||||||||||||||||||||
| 268 | 32 | $_[4] .= $tag; | ||||||||||||||||||||||||||||||||
| 269 | ||||||||||||||||||||||||||||||||||
| 270 | 32 | &{$enc}( | ||||||||||||||||||||||||||||||||
| 271 | $_[0], # $optn | |||||||||||||||||||||||||||||||||
| 272 | $op, # $op | |||||||||||||||||||||||||||||||||
| 273 | $_[2], # $stash | |||||||||||||||||||||||||||||||||
| 274 | $var, # $var | |||||||||||||||||||||||||||||||||
| 275 | $_[4], # $buf | |||||||||||||||||||||||||||||||||
| 276 | $loop, # $loop | |||||||||||||||||||||||||||||||||
| 277 | $_[6], # $path | |||||||||||||||||||||||||||||||||
| 278 | ); | |||||||||||||||||||||||||||||||||
| 279 | } | |||||||||||||||||||||||||||||||||
| 280 | 11 | pop @{$_[6]}; | ||||||||||||||||||||||||||||||||
| 281 | } | |||||||||||||||||||||||||||||||||
| 282 | else { | |||||||||||||||||||||||||||||||||
| 283 | 34 | ▶ | _encode($_[0],$_[1]->[cCHILD], defined($_[3]) ? $_[3] : $_[2], $_[6], $_[4]); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 284 | } | |||||||||||||||||||||||||||||||||
| 285 | 43 | substr($_[4],$l,2) = asn_encode_length(length($_[4]) - $l - 2); | ||||||||||||||||||||||||||||||||
| 286 | } | |||||||||||||||||||||||||||||||||
| 287 | else { | |||||||||||||||||||||||||||||||||
| 288 | 0 | $_[4] .= asn_encode_length(length $_[3]); | ||||||||||||||||||||||||||||||||
| 289 | 0 | $_[4] .= $_[3]; | ||||||||||||||||||||||||||||||||
| 290 | } | |||||||||||||||||||||||||||||||||
| 291 | } | |||||||||||||||||||||||||||||||||
| 292 | ||||||||||||||||||||||||||||||||||
| 293 | ||||||||||||||||||||||||||||||||||
| 294 | 24 | my %_enc_time_opt = ( utctime => 1, withzone => 0, raw => 2); | ||||||||||||||||||||||||||||||||
| 295 | ||||||||||||||||||||||||||||||||||
| 296 | 6 | ▶ | sub _enc_time { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_time: called CC=11 SCAR=25.5
| ||||||||||||||||||||||||||||||||||
| 297 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 298 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 299 | ||||||||||||||||||||||||||||||||||
| 300 | 6 | ▶ | my $mode = $_enc_time_opt{$_[0]->{'encode_time'} || ''} || 0; | |||||||||||||||||||||||||||||||
|
Condition100%4/4
$_enc_time_opt{$_[0]{'encode_time'} or ''} || 0
$_[0]{'encode_time'} || ''
MC/DC100%1/1
$_enc_time_opt{$_[0]{'encode_time'} or ''} || 0
$_enc_time_opt{$_[0]{'encode_time'} or ''}
MC/DC100%1/1
$_[0]{'encode_time'} || ''
$_[0]{'encode_time'}
Truth table100%2/2
A
A= $_enc_time_opt{$_[0]{'encode_time'} or ''}
Truth table100%2/2
A
A= $_[0]{'encode_time'}
| ||||||||||||||||||||||||||||||||||
| 301 | ||||||||||||||||||||||||||||||||||
| 302 | 6 | ▶ | if ($mode == 2) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 303 | 0 | $_[4] .= asn_encode_length(length $_[3]); | ||||||||||||||||||||||||||||||||
| 304 | 0 | $_[4] .= $_[3]; | ||||||||||||||||||||||||||||||||
| 305 | 0 | return; | ||||||||||||||||||||||||||||||||
| 306 | } | |||||||||||||||||||||||||||||||||
| 307 | ||||||||||||||||||||||||||||||||||
| 308 | 6 | my $time; | ||||||||||||||||||||||||||||||||
| 309 | my @time; | |||||||||||||||||||||||||||||||||
| 310 | 6 | my $offset; | ||||||||||||||||||||||||||||||||
| 311 | 6 | my $isgen = $_[1]->[cTYPE] == opGTIME; | ||||||||||||||||||||||||||||||||
| 312 | ||||||||||||||||||||||||||||||||||
| 313 | 6 | ▶ | if (ref($_[3])) { | |||||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||||
| 314 | 0 | $offset = int($_[3]->[1] / 60); | ||||||||||||||||||||||||||||||||
| 315 | 0 | $time = $_[3]->[0] + $_[3]->[1]; | ||||||||||||||||||||||||||||||||
| 316 | } | |||||||||||||||||||||||||||||||||
| 317 | elsif ($mode == 0) { | |||||||||||||||||||||||||||||||||
| 318 | 5 | ▶ | if (exists $_[0]->{'encode_timezone'}) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 319 | 5 | $offset = int($_[0]->{'encode_timezone'} / 60); | ||||||||||||||||||||||||||||||||
| 320 | 5 | $time = $_[3] + $_[0]->{'encode_timezone'}; | ||||||||||||||||||||||||||||||||
| 321 | } | |||||||||||||||||||||||||||||||||
| 322 | else { | |||||||||||||||||||||||||||||||||
| 323 | 0 | @time = localtime($_[3]); | ||||||||||||||||||||||||||||||||
| 324 | 0 | my @g = gmtime($_[3]); | ||||||||||||||||||||||||||||||||
| 325 | ||||||||||||||||||||||||||||||||||
| 326 | 0 | $offset = ($time[1] - $g[1]) + ($time[2] - $g[2]) * 60; | ||||||||||||||||||||||||||||||||
| 327 | 0 | $time = $_[3] + $offset*60; | ||||||||||||||||||||||||||||||||
| 328 | } | |||||||||||||||||||||||||||||||||
| 329 | } | |||||||||||||||||||||||||||||||||
| 330 | else { | |||||||||||||||||||||||||||||||||
| 331 | 1 | $time = $_[3]; | ||||||||||||||||||||||||||||||||
| 332 | } | |||||||||||||||||||||||||||||||||
| 333 | 6 | @time = gmtime($time); | ||||||||||||||||||||||||||||||||
| 334 | 6 | $time[4] += 1; | ||||||||||||||||||||||||||||||||
| 335 | 6 | ▶ | $time[5] = $isgen ? ($time[5] + 1900) : ($time[5] % 100); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 336 | ||||||||||||||||||||||||||||||||||
| 337 | 6 | my $tmp = sprintf("%02d"x6, @time[5,4,3,2,1,0]); | ||||||||||||||||||||||||||||||||
| 338 | 6 | ▶ | if ($isgen) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 339 | 3 | my $sp = sprintf("%.03f",$time); | ||||||||||||||||||||||||||||||||
| 340 | 3 | ▶ | $tmp .= substr($sp,-4) unless $sp =~ /\.000$/; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 341 | } | |||||||||||||||||||||||||||||||||
| 342 | 6 | ▶ | $tmp .= $offset ? sprintf("%+03d%02d",$offset / 60, abs($offset % 60)) : 'Z'; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 343 | 6 | $_[4] .= asn_encode_length(length $tmp); | ||||||||||||||||||||||||||||||||
| 344 | 6 | $_[4] .= $tmp; | ||||||||||||||||||||||||||||||||
| 345 | } | |||||||||||||||||||||||||||||||||
| 346 | ||||||||||||||||||||||||||||||||||
| 347 | ||||||||||||||||||||||||||||||||||
| 348 | 2 | ▶ | sub _enc_utf8 { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_utf8: called CC=2 SCAR=6.9
| ||||||||||||||||||||||||||||||||||
| 349 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 350 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 351 | ||||||||||||||||||||||||||||||||||
| 352 | 2 | if (CHECK_UTF8) { | ||||||||||||||||||||||||||||||||
| 353 | 2 | my $tmp = $_[3]; | ||||||||||||||||||||||||||||||||
| 354 | 2 | ▶ | utf8::upgrade($tmp) unless Encode::is_utf8($tmp); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 355 | 2 | utf8::encode($tmp); | ||||||||||||||||||||||||||||||||
| 356 | 2 | $_[4] .= asn_encode_length(length $tmp); | ||||||||||||||||||||||||||||||||
| 357 | 2 | $_[4] .= $tmp; | ||||||||||||||||||||||||||||||||
| 358 | } | |||||||||||||||||||||||||||||||||
| 359 | else { | |||||||||||||||||||||||||||||||||
| 360 | $_[4] .= asn_encode_length(length $_[3]); | |||||||||||||||||||||||||||||||||
| 361 | $_[4] .= $_[3]; | |||||||||||||||||||||||||||||||||
| 362 | } | |||||||||||||||||||||||||||||||||
| 363 | } | |||||||||||||||||||||||||||||||||
| 364 | ||||||||||||||||||||||||||||||||||
| 365 | ||||||||||||||||||||||||||||||||||
| 366 | 2 | ▶ | sub _enc_any { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_any: called CC=5 SCAR=18.4
| ||||||||||||||||||||||||||||||||||
| 367 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 368 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 369 | ||||||||||||||||||||||||||||||||||
| 370 | 2 | my $handler; | ||||||||||||||||||||||||||||||||
| 371 | 2 | ▶ | if ($_[1]->[cDEFINE] && $_[2]->{$_[1]->[cDEFINE]}) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[1][7] and $_[2]{$_[1][7]}
MC/DC0%0/2
$_[1][7] and $_[2]{$_[1][7]}
$_[1][7]
$_[2]{$_[1][7]}
Truth table33%1/3
A and B
A= $_[1][7]B= $_[2]{$_[1][7]}
| ||||||||||||||||||||||||||||||||||
| 372 | 2 | $handler=$_[0]->{oidtable}{$_[2]->{$_[1]->[cDEFINE]}}; | ||||||||||||||||||||||||||||||||
| 373 | 2 | ▶ | $handler=$_[0]->{handlers}{$_[1]->[cVAR]}{$_[2]->{$_[1]->[cDEFINE]}} unless $handler; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 374 | } | |||||||||||||||||||||||||||||||||
| 375 | 2 | ▶ | if ($handler) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 376 | 2 | $_[4] .= $handler->encode($_[3]); | ||||||||||||||||||||||||||||||||
| 377 | } else { | |||||||||||||||||||||||||||||||||
| 378 | 0 | $_[4] .= $_[3]; | ||||||||||||||||||||||||||||||||
| 379 | } | |||||||||||||||||||||||||||||||||
| 380 | } | |||||||||||||||||||||||||||||||||
| 381 | ||||||||||||||||||||||||||||||||||
| 382 | ||||||||||||||||||||||||||||||||||
| 383 | 10 | ▶ | sub _enc_choice { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_choice: called CC=6 SCAR=18.2
| ||||||||||||||||||||||||||||||||||
| 384 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 385 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 386 | ||||||||||||||||||||||||||||||||||
| 387 | 10 | ▶ | my $stash = defined($_[3]) ? $_[3] : $_[2]; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 388 | 10 | for my $op (@{$_[1]->[cCHILD]}) { | ||||||||||||||||||||||||||||||||
| 389 | 16 | ▶ | next if $op->[cTYPE] == opEXTENSIONS; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 390 | 15 | ▶ | my $var = defined $op->[cVAR] ? $op->[cVAR] : $op->[cCHILD]->[0]->[cVAR]; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 391 | ||||||||||||||||||||||||||||||||||
| 392 | 15 | ▶ | if (exists $stash->{$var}) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 393 | 10 | push @{$_[6]}, $var; | ||||||||||||||||||||||||||||||||
| 394 | 10 | _encode($_[0],[$op], $stash, $_[6], $_[4]); | ||||||||||||||||||||||||||||||||
| 395 | 10 | pop @{$_[6]}; | ||||||||||||||||||||||||||||||||
| 396 | 10 | return; | ||||||||||||||||||||||||||||||||
| 397 | } | |||||||||||||||||||||||||||||||||
| 398 | } | |||||||||||||||||||||||||||||||||
| 399 | 0 | require Carp; | ||||||||||||||||||||||||||||||||
| 400 | 0 | Carp::croak("No value found for CHOICE " . join(".", @{$_[6]})); | ||||||||||||||||||||||||||||||||
| 401 | } | |||||||||||||||||||||||||||||||||
| 402 | ||||||||||||||||||||||||||||||||||
| 403 | ||||||||||||||||||||||||||||||||||
| 404 | 8 | ▶ | sub _enc_bcd { | |||||||||||||||||||||||||||||||
|
Subroutine
_enc_bcd: called CC=3 SCAR=11.0
| ||||||||||||||||||||||||||||||||||
| 405 | # 0 1 2 3 4 5 6 | |||||||||||||||||||||||||||||||||
| 406 | # $optn, $op, $stash, $var, $buf, $loop, $path | |||||||||||||||||||||||||||||||||
| 407 | 8 | ▶ | my $str = ("$_[3]" =~ /^(\d+)/) ? $1 : ""; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 408 | 8 | ▶ | $str .= "F" if length($str) & 1; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 409 | 8 | $_[4] .= asn_encode_length(length($str) / 2); | ||||||||||||||||||||||||||||||||
| 410 | 8 | $_[4] .= pack("H*", $str); | ||||||||||||||||||||||||||||||||
| 411 | } | |||||||||||||||||||||||||||||||||
| 412 | 24 | 1; | ||||||||||||||||||||||||||||||||
| 413 | ||||||||||||||||||||||||||||||||||