Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to use Convert::ASN1 ?

by bh_perl (Monk)
on Sep 16, 2008 at 07:46 UTC ( [id://711618]=perlquestion: print w/replies, xml ) Need Help??

bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

hi

Actually, I am trying to write a program to decode/encode ASN.1 file. But, i still do not understand how to use Convert::ASN or Convert::BER libraries.

Could somebody explain to me how to use it. I have read the manual but its so short and I still do not understand. Hopefully, somebody could advise me using my sample data as below:

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
bh_perl

Replies are listed 'Best First'.
Re: How to use Convert::ASN1 ?
by Corion (Patriarch) on Sep 16, 2008 at 07:50 UTC

    I find the synopsis of Convert::ASN1 quite clear:

    use Convert::ASN1; $asn = Convert::ASN1->new; $asn->prepare(q< [APPLICATION 7] SEQUENCE { int INTEGER, str OCTET STRING } >); $pdu = $asn->encode( int => 7, str => "string"); $out = $asn->decode($pdu); print $out->{int}," ",$out->{str},"\n";

    Where exactly do you have problems?

      Actually, I have asn.1 file and the sample as below.

      My problem is how could i read ASN.1 data and display the value ? I did not know how to do that and I could not macthed your program with my data.

      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

        I guess that ASN1 expects you to supply the data description. So you would have to have the description of your data somewhere in your documentation. You don't show your data description at all here, so I don't know what it is.

        After you've created the ASN decoder using the description, you pass it data to decode. That's it.

        You've posted a hexdump of your data for the second time. What do you expect us to do with that hexdump?

        Where exactly do you have problems? You haven't shown a single line of code, so that makes it kinda hard for me to give you more concrete help than pointing you to the module documentation.

Re: How to use Convert::ASN1 ?
by BrowserUk (Patriarch) on Sep 16, 2008 at 11:07 UTC
      This is file description:
      The format of a Tag is as follow: Identifr Filler Length Data Where Identifier : A 3 bytes long number uniquely identifies the Tag Filler : A filler of with value H’81 is inserted at position + on condition number of data bytes sent in the AXE signal is odd. Length : is the number of taaged data in bytes. Data : is the actual data in the tag Identifier Filler Length_of_data <data> 9FA10B - 00 <data> 9FA001 81 03 <data> 9FBF7B 81 0F <data> 9FBF7D 81 00-0F <data> 9FBE00 - 0E <data> 9FBE00 - 0E <data> 9FBE1A 81 00-0F <data> 9FBE1B 81 00-0F <data> 9FBE1C 81 00-0F <data> 9FBE1D 81 00-0F <data> 9FA016 81 00-02 <data> 9FBE19 81 00-0E <data> 9FA10B - 00 <data> 9FBE00 - 02 <data> 9FBE01 81 0F <data> 9FBE19 81 01-0E <data> 9FBE1B 81 01-11 <data> 9FA10B - 00 <data> 9FA001 81 03 <data>

        Wherever you are obtaining your ASN.1 dataset, should also supply you with or make available to you, an ASN.1 description (template) of the format of the data within that dataset.

        For example, if you are downloading .aso files from NCBI, then somewhere within their site/ftp there will be a file containing the ASN.1 description that looks something like this one. In that same directory there are several other .prt files, one of which would form the the input to Convert::ASN1 (or possibly better, Bio::ASN1::Sequence), if they are the source of your dataset.

        Reverse engineering an ASN.1 description based upon the limited information you've provided above (where did that come from?) is iffy at best.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to use Convert::ASN1 ?
by jmcnamara (Monsignor) on Sep 17, 2008 at 20:03 UTC
    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.

      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: perlquestion [id://711618]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-26 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found