add9c4df38edd23553d6d1fcbae6cdab2a9c7df4
[metze/wireshark/wip.git] / tools / extract_asn1_from_spec.pl
1 #!/usr/bin/perl
2 #
3 # This script extracts the ASN1 definition from and TS 36.331/36.355/25.331
4 # and generates asn files that can be processed by asn2wrs
5 # First download the specification from 3gpp.org as a word document and open it
6 # Then in "view" menu, select normal, draft or web layout (any kind that removes page header and footers)
7 # Finally save the document as a text file
8 # Example with TS 36.331: "perl extract_asn1_from_spec.pl 36331-xxx.txt"
9 # It should generate: EUTRA-RRC-Definitions.asn, EUTRA-UE-Variables.asn and EUTRA-InterNodeDefinitions
10 #
11 # Copyright 2011 Vincent Helfre and Erwan Yvin
12 #
13 # Wireshark - Network traffic analyzer
14 # By Gerald Combs <gerald@wireshark.org>
15 # Copyright 1998 Gerald Combs
16 #
17 # This program is free software; you can redistribute it and/or
18 # modify it under the terms of the GNU General Public License
19 # as published by the Free Software Foundation; either version 2
20 # of the License, or (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 #
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, write to the Free Software
29 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30
31 use warnings;
32 $input_file = $ARGV[0];
33 $version = 0;
34
35 sub extract_spec_version;
36 sub extract_asn1;
37
38 open(INPUT_FILE, "< $input_file") or die "Can not open file $input_file";
39
40 extract_spec_version();
41
42 extract_asn1();
43
44 close(INPUT_FILE);
45
46 # This subroutine extracts the version of the specification
47 sub extract_spec_version {
48   my $line;
49   while($line = <INPUT_FILE>){
50     if($line =~ m/3GPP TS ((25|36)\.331|36\.355) V/){
51       $version = $line;
52       return;
53     }
54   }
55 }
56
57 # This subroutine copies the text delimited by -- ASN1START and -- ASN1STOP in INPUT_FILE
58 # and copies it into OUTPUT_FILE.
59 # The OUTPUT_FILE is opened on encounter of the keyword "DEFINITIONS AUTOMATIC TAGS"
60 # and closed on encounter of the keyword "END"
61 sub extract_asn1 {
62   my $line;
63   my $is_asn1 = 0;
64   my $output_file_name = 0;
65   my $file_name_found = 0;
66
67   while($line = <INPUT_FILE>){
68     if ($line =~ m/-- ASN1STOP/) {
69       $is_asn1 = 0;
70     }
71
72     if($line =~ m/\96     LPP-PDU-Definitions/){
73       $output_file_name = "LPP.asn";
74       print  "generating $output_file_name\n";
75       open(OUTPUT_FILE, "> $output_file_name") or die "Can not open file $output_file_name";
76       $file_name_found = 1;
77     }
78
79     if(($file_name_found == 0) && ($line =~ m/DEFINITIONS AUTOMATIC TAGS ::=/)){
80       ($output_file_name) = ($line =~ m/^([a-zA-Z0-9\-]+)\s+DEFINITIONS AUTOMATIC TAGS ::=/);
81       $output_file_name = "$output_file_name".".asn";
82       print  "generating $output_file_name\n";
83       open(OUTPUT_FILE, "> $output_file_name") or die "Can not open file $output_file_name";
84       $is_asn1 = 1;
85       $file_name_found = 1;
86     }
87
88     if (($line =~ /END/) && (defined fileno OUTPUT_FILE)){
89       syswrite OUTPUT_FILE,"$line";
90       close(OUTPUT_FILE);
91       $is_asn1 = 0;
92       $file_name_found = 0;
93     }
94
95     if (($is_asn1 == 1) && (defined fileno OUTPUT_FILE)){
96       syswrite OUTPUT_FILE,"$line";
97     }
98
99     if ($line =~ m/-- ASN1START/) {
100       $is_asn1 = 1;
101     }
102   }
103 }
104