extract_asn1_from_spec.pl: fix parsing of END tag
[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/38.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 # SPDX-License-Identifier: GPL-2.0-or-later
18
19 use warnings;
20 $input_file = $ARGV[0];
21 $version = 0;
22
23 sub extract_spec_version;
24 sub extract_asn1;
25
26 open(INPUT_FILE, "< $input_file") or die "Can not open file $input_file";
27
28 extract_spec_version();
29
30 extract_asn1();
31
32 close(INPUT_FILE);
33
34 # This subroutine extracts the version of the specification
35 sub extract_spec_version {
36   my $line;
37   while($line = <INPUT_FILE>){
38     if($line =~ m/3GPP TS ((25|36|38)\.331|36\.355) V/){
39       $version = $line;
40       return;
41     }
42   }
43 }
44
45 # This subroutine copies the text delimited by -- ASN1START and -- ASN1STOP in INPUT_FILE
46 # and copies it into OUTPUT_FILE.
47 # The OUTPUT_FILE is opened on encounter of the keyword "DEFINITIONS AUTOMATIC TAGS"
48 # and closed on encounter of the keyword "END"
49 sub extract_asn1 {
50   my $line;
51   my $is_asn1 = 0;
52   my $output_file_name = 0;
53   my $file_name_found = 0;
54
55   while($line = <INPUT_FILE>){
56     if ($line =~ m/-- ASN1STOP/) {
57       $is_asn1 = 0;
58     }
59
60     if($line =~ m/      LPP-PDU-Definitions/){
61       $output_file_name = "LPP.asn";
62       print  "generating $output_file_name\n";
63       open(OUTPUT_FILE, "> $output_file_name") or die "Can not open file $output_file_name";
64       $file_name_found = 1;
65     }
66
67     if(($file_name_found == 0) && ($line =~ m/DEFINITIONS AUTOMATIC TAGS ::=/)){
68       ($output_file_name) = ($line =~ m/^([a-zA-Z0-9\-]+)\s+DEFINITIONS AUTOMATIC TAGS ::=/);
69       $output_file_name = "$output_file_name".".asn";
70       print  "generating $output_file_name\n";
71       open(OUTPUT_FILE, "> $output_file_name") or die "Can not open file $output_file_name";
72       $is_asn1 = 1;
73       $file_name_found = 1;
74     }
75
76     if (($line =~ /^END/) && (defined fileno OUTPUT_FILE)){
77       syswrite OUTPUT_FILE,"$line";
78       close(OUTPUT_FILE);
79       $is_asn1 = 0;
80       $file_name_found = 0;
81     }
82
83     if (($is_asn1 == 1) && (defined fileno OUTPUT_FILE)){
84       syswrite OUTPUT_FILE,"$line";
85     }
86
87     if ($line =~ m/-- ASN1START/) {
88       $is_asn1 = 1;
89     }
90   }
91 }
92