Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How to use Convert::ASN1 ?

by jmcnamara (Monsignor)
on Sep 17, 2008 at 20:03 UTC ( [id://712111]=note: print w/replies, xml ) Need Help??


in reply to How to use Convert::ASN1 ?

If the data was in ASN.1 format you would need an ASN.1 grammar to parse it. However, that doesn't look like ASN.1 data.

Here is a simple (non-ASN.1) decoder based on your specification:

#!/usr/bin/perl -w use strict; my $message = pack 'H*', join '', qw( 9f a1 0b 00 9f a1 0b 00 9f be 00 02 00 15 9f be 01 81 0f 01 55 18 f3 0b 24 99 20 10 00 10 00 00 00 01 9f be 19 81 09 00 01 10 60 18 00 38 38 11 9f be 1b 08 02 01 10 ff ff ff ff ff a1 06 82 04 04 00 00 00 ); _unpack_message($message); sub _unpack_message { my $message = shift; print "Tag Length Data\n"; while ($message) { my $tag = unpack 'H6', substr $message, 0, 3, ''; my $length = unpack 'C', substr $message, 0, 1, ''; if ($length == 0x81) { $length = unpack 'C', substr $message, 0, 1, ''; } my $template = 'H' . $length * 2; my $data = unpack $template, substr $message, 0, $leng +th, ''; printf "%s %-6d %s\n", $tag, $length, $data; } } __END__ Prints: Tag Length Data 9fa10b 0 9fa10b 0 9fbe00 2 0015 9fbe01 15 015518f30b24992010001000000001 9fbe19 9 000110601800383811 9fbe1b 8 020110ffffffffff a10682 4 04000000

--
John.

Replies are listed 'Best First'.
Re^2: How to use Convert::ASN1 ?
by bh_perl (Monk) on Jun 25, 2010 at 04:46 UTC
    Hi..
    This is my program but I have seen that the substr is not subtring the data correctly. Please help me
    #!/usr/bin/perl -w use Cwd; use warnings; use strict; use Getopt::Long; use constant FILEHDR => 4; use constant CDRLEN => 286; my ($trace, $help, $infile); my $swap = ''; my $indir = getcwd; my $outdir = getcwd; GetOptions ( "h|help" => \$help, "filename|f=s" => \$infile, "swap|s" => \$swap, "input|i=s" => \$indir, "output|o=s" => \$outdir, "trace|t" => \$trace ) or usage(); sub usage { exit; } my $outfile = $infile; my $data; if ($infile) { #open (OUTPUT, ">$outdir/$outfile"); open (DATA, "$indir/$infile"); binmode DATA; while ($data = <DATA>) { my $tag = unpack "H2", substr $data,0,1,''; my $length = unpack "C", substr $data,0,1,''; if ($length == 0x81) { $length = unpack "C", substr $data,0,1,''; } # Problem is here.. data not substr correctly my $template = 'H' . $length*2; my $rec = unpack $template, substr $data,0,$length,''; printf ("RECORD TAG : %s\n", $tag); printf ("RECORD LENGTH : %s\n", $length); printf ("RECORD : %s\n", $rec); } close(DATA); #close(OUTPUT); }

    This is sample file
    84 47 00 0c 00 00 11 0a 03 50 35 04 00 64 0a 04 16 12 00 1e 00 1d 00 00 65 09 08 54 52 03 2f 82 05 10 00 02 6e 06 0b a8 53 11 67 00 00 00 7a 7f 00 69 4a 42 47 48 4a 41 0c 00 6a 53 54 4d 44 54 4f 1c 00 66 04 00 00

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://712111]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found