ASN1/Debug.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 239 CC 23 SCAR 45.1
CC
23
Cov
0%
asn_dump
182.0
dump_op
72.0
_hexdump
12.0
CRAP 91.0
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 $Convert::ASN1::VERSION = '0.36';
7 ##
8 ## just for debug :-)
9 ##
10  
11 sub _hexdump {
12 my($fmt,$pos) = @_[1,2]; # Don't copy buffer
13  
14 $pos ||= 0;
15  
16 my $offset = 0;
17 my $cnt = 1 << 4;
18 my $len = length($_[0]);
19 my $linefmt = ("%02X " x $cnt) . "%s\n";
20  
21 print "\n";
22  
23 while ($offset < $len) {
24 my $data = substr($_[0],$offset,$cnt);
25 my @y = unpack("C*",$data);
26  
27 printf $fmt,$pos if $fmt;
28  
29 # On the last time through replace '%02X ' with '__ ' for the
30 # missing values
31 substr($linefmt, 5*@y,5*($cnt-@y)) = "__ " x ($cnt - @y)
32 if @y != $cnt;
33  
34 # Change non-printable chars to '.'
35 $data =~ s/[\x00-\x1f\x7f-\xff]/./sg;
36 printf $linefmt, @y,$data;
37  
38 $offset += $cnt;
39 $pos += $cnt;
40 }
41 }
42  
43 my %type = (
44 split(/[\t\n]\s*/,
45 q(10 SEQUENCE
46 01 BOOLEAN
47 0A ENUM
48 0D RELATIVE-OID
49 11 SET
50 02 INTEGER
51 03 BIT STRING
52 04 STRING
53 05 NULL
54 06 OBJECT ID
55 )
56 )
57 );
58  
59 my %classes = (
60 split(/[\t\n]\s*/,
61 q(C0 [PRIVATE %d]
62 40 [APPLICATION %d]
63 80 [CONTEXT %d]
64 00 [UNIVERSAL %d]
65 )
66 )
67 );
68  
69 BEGIN { undef &asn_dump }
70 sub asn_dump {
71 my $fh = @_>1 ? shift : \*STDERR;
72  
73 my $ofh = select($fh);
74  
75 my $pos = 0;
76 my $indent = "";
77 my @seqend = ();
78 my $length = length($_[0]);
79 my $fmt = $length > 0xffff ? "%08X" : "%04X";
80  
81 while(1) {
82 while (@seqend && $pos >= $seqend[0]) {
83 $indent = substr($indent,2);
84 warn "Bad sequence length " unless $pos == shift @seqend;
85 printf "$fmt : %s}\n",$pos,$indent;
86 }
87 last unless $pos < $length;
88
89 my $start = $pos;
90 my($tb,$tag,$tnum) = asn_decode_tag2(substr($_[0],$pos,10));
91 last unless defined $tb;
92 $pos += $tb;
93 my($lb,$len) = asn_decode_length(substr($_[0],$pos,10));
94 $pos += $lb;
95  
96 if($tag == 0 && $len == 0) {
97 $seqend[0] = $pos;
98 redo;
99 }
100 printf $fmt. " %4d: %s",$start,$len,$indent;
101  
102 my $label = $type{sprintf("%02X",$tag & ~0x20)}
103 || sprintf($classes{sprintf("%02X",$tag & 0xC0)}, $tag & ~0xE0);
104 print "$label: $tnum";
105  
106 if ($tag & ASN_CONSTRUCTOR) {
107 print " {\n";
108 if($len < 0) {
109 unshift(@seqend, length $_[0]);
110 }
111 else {
112 unshift(@seqend, $pos + $len);
113 }
114 $indent .= " ";
115 next;
116 }
117  
118 my $tmp;
119  
120 for ($label) { # switch
121 /^(INTEGER|ENUM)/ && do {
122 Convert::ASN1::_dec_integer({},[],{},$tmp,$_[0],$pos,$len);
123 printf " = %d\n",$tmp;
124 last;
125 };
126  
127 /^BOOLEAN/ && do {
128 Convert::ASN1::_dec_boolean({},[],{},$tmp,$_[0],$pos,$len);
129 printf " = %s\n",$tmp ? 'TRUE' : 'FALSE';
130 last;
131 };
132  
133 /^(?:(OBJECT ID)|(RELATIVE-OID))/ && do {
134 my @op; $op[cTYPE] = $1 ? opOBJID : opROID;
135 Convert::ASN1::_dec_object_id({},\@op,{},$tmp,$_[0],$pos,$len);
136 printf " = %s\n",$tmp;
137 last;
138 };
139  
140 /^NULL/ && do {
141 print "\n";
142 last;
143 };
144  
145 /^STRING/ && do {
146 Convert::ASN1::_dec_string({},[],{},$tmp,$_[0],$pos,$len);
147 if ($tmp =~ /[\x00-\x1f\x7f-\xff]/s) {
148 _hexdump($tmp,$fmt . " : ".$indent, $pos);
149 }
150 else {
151 printf " = '%s'\n",$tmp;
152 }
153 last;
154 };
155  
156 # /^BIT STRING/ && do {
157 # Convert::BER::BIT_STRING->unpack($ber,\$tmp);
158 # print " = ",$tmp,"\n";
159 # last;
160 # };
161  
162 # default -- dump hex data
163 _hexdump(substr($_[0],$pos,$len),$fmt . " : ".$indent, $pos);
164 }
165 $pos += $len;
166 }
167 printf "Buffer contains %d extra bytes\n", $length - $pos if $pos < $length;
168  
169 select($ofh);
170 }
171  
172 BEGIN { undef &asn_hexdump }
173 sub asn_hexdump {
174 my $fh = @_>1 ? shift : \*STDERR;
175 my $ofh = select($fh);
176  
177 _hexdump($_[0]);
178 print "\n";
179 select($ofh);
180 }
181  
182 BEGIN { undef &dump }
183 sub dump {
184 my $self = shift;
185
186 for (@{$self->{script}}) {
187 dump_op($_,"",{},1);
188 }
189 }
190  
191 BEGIN { undef &dump_all }
192 sub dump_all {
193 my $self = shift;
194
195 while(my($k,$v) = each %{$self->{tree}}) {
196 print STDERR "$k:\n";
197 for (@$v) {
198 dump_op($_,"",{},1);
199 }
200 }
201 }
202  
203  
204 BEGIN { undef &dump_op }
205 sub dump_op {
206 my($op,$indent,$done,$line) = @_;
207 $indent ||= "";
208 printf STDERR "%3d: ",$line;
209 if ($done->{$op}) {
210 print STDERR " $indent=",$done->{$op},"\n";
211 return ++$line;
212 }
213 $done->{$op} = $line++;
214 print STDERR $indent,"[ '",unpack("H*",$op->[cTAG]),"', ";
215 print STDERR $op->[cTYPE] =~ /\D/ ? $op->[cTYPE] : $opName[$op->[cTYPE]];
216 print STDERR ", ",defined($op->[cVAR]) ? $op->[cVAR] : "_";
217 print STDERR ", ",defined($op->[cLOOP]) ? $op->[cLOOP] : "_";
218 print STDERR ", ",defined($op->[cOPT]) ? $op->[cOPT] : "_";
219 print STDERR "]";
220 if ($op->[cCHILD]) {
221 print STDERR " ",scalar @{$op->[cCHILD]},"\n";
222 for (@{$op->[cCHILD]}) {
223 $line = dump_op($_,$indent . " ",$done,$line);
224 }
225 }
226 else {
227 print STDERR "\n";
228 }
229 print STDERR "\n" unless length $indent;
230 $line;
231 }
232  
233 1;
234