[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 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 C0 [PRIVATE %d] 53 04 STRING 54 40 [APPLICATION %d] 55 05 NULL 56 06 OBJECT ID 57 80 [CONTEXT %d] 58 ) 59 ) 60 ); 61 62 BEGIN { undef &asn_dump } 63 sub asn_dump { 64 my $fh = @_>1 ? shift : \*STDERR; 65 66 my $ofh = select($fh); 67 68 my $pos = 0; 69 my $indent = ""; 70 my @seqend = (); 71 my $length = length($_[0]); 72 my $fmt = $length > 0xffff ? "%08X" : "%04X"; 73 74 while(1) { 75 while (@seqend && $pos >= $seqend[0]) { 76 $indent = substr($indent,2); 77 warn "Bad sequence length " unless $pos == shift @seqend; 78 printf "$fmt : %s}\n",$pos,$indent; 79 } 80 last unless $pos < $length; 81 82 my $start = $pos; 83 my($tb,$tag,$tnum) = asn_decode_tag2(substr($_[0],$pos,10)); 84 $pos += $tb; 85 my($lb,$len) = asn_decode_length(substr($_[0],$pos,10)); 86 $pos += $lb; 87 88 if($tag == 0 && $len == 0) { 89 $seqend[0] = $pos; 90 redo; 91 } 92 printf $fmt. " %4d: %s",$start,$len,$indent; 93 94 my $label = $type{sprintf("%02X",$tag & ~0x20)} 95 || $type{sprintf("%02X",$tag & 0xC0)} 96 || "[UNIVERSAL %d]"; 97 printf $label, $tnum; 98 99 if ($tag & ASN_CONSTRUCTOR) { 100 print " {\n"; 101 if($len < 0) { 102 unshift(@seqend, length $_[0]); 103 } 104 else { 105 unshift(@seqend, $pos + $len); 106 } 107 $indent .= " "; 108 next; 109 } 110 111 my $tmp; 112 113 for ($label) { # switch 114 /^(INTEGER|ENUM)/ && do { 115 Convert::ASN1::_dec_integer({},[],{},$tmp,$_[0],$pos,$len); 116 printf " = %d\n",$tmp; 117 last; 118 }; 119 120 /^BOOLEAN/ && do { 121 Convert::ASN1::_dec_boolean({},[],{},$tmp,$_[0],$pos,$len); 122 printf " = %s\n",$tmp ? 'TRUE' : 'FALSE'; 123 last; 124 }; 125 126 /^(?:(OBJECT ID)|(RELATIVE-OID))/ && do { 127 my @op; $op[cTYPE] = $1 ? opOBJID : opROID; 128 Convert::ASN1::_dec_object_id({},\@op,{},$tmp,$_[0],$pos,$len); 129 printf " = %s\n",$tmp; 130 last; 131 }; 132 133 /^NULL/ && do { 134 print "\n"; 135 last; 136 }; 137 138 /^STRING/ && do { 139 Convert::ASN1::_dec_string({},[],{},$tmp,$_[0],$pos,$len); 140 if ($tmp =~ /[\x00-\x1f\x7f-\xff]/s) { 141 _hexdump($tmp,$fmt . " : ".$indent, $pos); 142 } 143 else { 144 printf " = '%s'\n",$tmp; 145 } 146 last; 147 }; 148 149 # /^BIT STRING/ && do { 150 # Convert::BER::BIT_STRING->unpack($ber,\$tmp); 151 # print " = ",$tmp,"\n"; 152 # last; 153 # }; 154 155 # default -- dump hex data 156 _hexdump(substr($_[0],$pos,$len),$fmt . " : ".$indent, $pos); 157 } 158 $pos += $len; 159 } 160 161 select($ofh); 162 } 163 164 BEGIN { undef &asn_hexdump } 165 sub asn_hexdump { 166 my $fh = @_>1 ? shift : \*STDERR; 167 my $ofh = select($fh); 168 169 _hexdump($_[0]); 170 print "\n"; 171 select($ofh); 172 } 173 174 BEGIN { undef &dump } 175 sub dump { 176 my $self = shift; 177 178 for (@{$self->{script}}) { 179 dump_op($_,"",{},1); 180 } 181 } 182 183 BEGIN { undef &dump_all } 184 sub dump_all { 185 my $self = shift; 186 187 while(my($k,$v) = each %{$self->{tree}}) { 188 print STDERR "$k:\n"; 189 for (@$v) { 190 dump_op($_,"",{},1); 191 } 192 } 193 } 194 195 196 BEGIN { undef &dump_op } 197 sub dump_op { 198 my($op,$indent,$done,$line) = @_; 199 $indent ||= ""; 200 printf STDERR "%3d: ",$line; 201 if ($done->{$op}) { 202 print STDERR " $indent=",$done->{$op},"\n"; 203 return ++$line; 204 } 205 $done->{$op} = $line++; 206 print STDERR $indent,"[ '",unpack("H*",$op->[cTAG]),"', "; 207 print STDERR $op->[cTYPE] =~ /\D/ ? $op->[cTYPE] : $opName[$op->[cTYPE]]; 208 print STDERR ", ",defined($op->[cVAR]) ? $op->[cVAR] : "_"; 209 print STDERR ", ",defined($op->[cLOOP]) ? $op->[cLOOP] : "_"; 210 print STDERR ", ",defined($op->[cOPT]) ? $op->[cOPT] : "_"; 211 print STDERR "]"; 212 if ($op->[cCHILD]) { 213 print STDERR " ",scalar @{$op->[cCHILD]},"\n"; 214 for (@{$op->[cCHILD]}) { 215 $line = dump_op($_,$indent . " ",$done,$line); 216 } 217 } 218 else { 219 print STDERR "\n"; 220 } 221 print STDERR "\n" unless length $indent; 222 $line; 223 } 224 225 1; 226
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 17 22:47:18 2015 | Cross-referenced by PHPXref 0.7.1 |