First public contribution.
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 # This component and the accompanying materials are made available
5 # under the terms of the License "Eclipse Public License v1.0"
6 # which accompanies this distribution, and is available
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 # Initial Contributors:
10 # Nokia Corporation - initial contribution.
23 my $OUTPUT_BUFFER = "";
34 GetOptions('debug=i' => \$DEBUG,
38 if (! defined $in && defined $ARGV[0]) {
43 @lines = decompile($in);
46 die "No input file specified.\n";
49 if (! defined $out && defined $ARGV[1]) {
53 open $outFh, ">$out" || die "Cannot open output file $out";
59 print $outFh $OUTPUT_BUFFER;
64 my $lineCount = scalar(@$lines);
66 for (my $i = 0; $i < $lineCount; ++$i) {
75 if ( /^OBJECT\s+IDENTIFIER\s*$/ ) {
77 print "reading OID value from next line\n";
79 if (defined @$lines[$i+1]) {
84 if ( /BIT\s+STRING,\s+encapsulates/i ) {
85 addToOutput("BITSTRING_WRAPPER");
88 elsif ( /^\s*BIT\s+STRING/i ) {
89 # bitstring defined in binary
90 if ( $$lines[$i+1] =~ /\'([01]+)\'/ ) {
92 addToOutput("BITSTRING=$1");
94 # bit string defined in hex
95 elsif ( /^\s*BIT\s+STRING\s+(([A-F0-9][A-F0-9]\s*)+)/i ) {
96 my $bitStr = toBitString($1);
97 addToOutput("BITSTRING=$bitStr");
101 addToOutput("BITSTRING_WRAPPER");
103 addToOutput("RAW \{");
106 addToOutput(getRawHex($lines,\$i));
113 elsif ( /^(BMPSTRING\s+)\'(.*)\'/i ) {
114 addToOutput("BMPSTRING=$2");
116 elsif ( /^(BOOLEAN\s+)(.*)/i ) {
117 addToOutput("BOOLEAN=$2");
119 elsif ( /(^ENUMERATED\s+)(\d+)*$/i ) {
120 # small integer - non hex incoded
121 addToOutput("ENUMERATED=$2");
123 elsif ( /^\[(\d+)\]\s*\'(.*)\'/ ) {
124 addToOutput("IMPLICIT=$1");
126 addToOutput("PRINTABLESTRING=$2");
130 elsif ( /^\[(\d+)\]/ ) {
131 # general case for implicit & explicit tags
133 if (defined @$lines[$i+1] && isRawData(@$lines[$i+1])) {
134 # if there is only raw data assume implicit
135 addToOutput("IMPLICIT=$tag");
137 addToOutput("OCTETSTRING");
139 addToOutput("RAW \{");
140 while (isRawData(@$lines[++$i])) {
141 addToOutput("" . @$lines[$i] . "");
152 # otherwise assume explicit
153 addToOutput("EXPLICIT=$tag");
157 elsif ( /^(IA5STRING\s+)\'(.*)\'/i ) {
158 addToOutput("IA5STRING=$2");
160 elsif ( /(^INTEGER\s+)(\d+)*$/i ) {
161 # small integer - non hex incoded
162 addToOutput("INTEGER=$2");
166 addToOutput("BIGINTEGER {");
168 $tmp =~ s/.*INTEGER\s+//g;
170 if (isRawData($tmp)) {
175 addToOutput(getRawHex($lines,\$i));
182 elsif ( /^OCTET STRING\s*$/i ) {
184 addToOutput("OCTETSTRING");
186 addToOutput("RAW \{");
188 addToOutput(getRawHex($lines,\$i));
194 elsif ( /^OCTET\s+STRING.*encapsulates/i ) {
195 addToOutput("OCTETSTRING");
198 elsif ( /^OCTET\s+STRING/i ) {
199 addToOutput("OCTETSTRING");
202 $hex =~ s/OCTET\s+STRING\s+//g;
203 addToOutput("RAW=$hex");
207 elsif ( /^OBJECT\s+IDENTIFIER\s+\'([\d ]+)\'/i ) {
211 addToOutput("OID=$oid");
213 elsif ( /(^OBJECT\s+IDENTIFIER.*\()([\d ]+)/i ) {
214 # extra information printed with oid
217 addToOutput("OID=$oid");
219 elsif ( /(^PRINTABLESTRING\s*\')([^\']*)/i ) {
220 addToOutput("PRINTABLESTRING=$2");
222 elsif ( /^SEQUENCE/i ) {
223 addToOutput("SEQUENCE");
230 elsif (/^(UTCTIME\s+\')([^\']+)/i) {
231 addToOutput("UTCTIME=$2");
233 elsif ( /^(UTF8STRING\s+)\'(.*)\'/i ) {
234 addToOutput("UTF8STRING=$2");
240 elsif (isRawData($_)) {
242 addToOutput("RAW=$_");
252 print "+${TABS}$text\n";
254 $OUTPUT_BUFFER .= "${TABS}$text\n";
258 my ($lines,$index) = @_;
263 my $line = $$lines[$$index];
264 last if (!defined $line);
267 if (isRawData($line)) {
269 addToOutput("$line");
282 my $retVal = ($line =~ /^\s*([A-F0-9][A-F0-9]\s?)+$/i);
283 if ($DEBUG >= 3 && $retVal) {
294 for (my $i=0; $i < length($hex); $i+=2) {
295 my $num = hex(substr($hex, $i, 2));
297 for (my $j=0; $j < 8; $j++) {
298 $bitStr .= ($num & 0x80) ? '1' : '0';
303 print "\nbitStr: $hex = $bitStr\n";
308 # increment debug tabbing level
313 # decrement debug tabbing level
323 my @command = ("cmd",
324 "/C \"dumpasn1 -apruz $inFile > _dump.tmp\"");
326 if ((my $err = system(@command)) != 0) {
327 die "decode: " . join(" ", @command) . "\nreturned error $err";
331 open $dumpFh, "_dump.tmp";
332 my @lines = <$dumpFh>;