1#!/usr/bin/perl
2#
3# This script extracts the ASN1 definition from TS 36.331/36.355/25.331/38.331/37.355/36.413/38.413/36.423/38.423
4# /38.463/38.473 , 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
19use warnings;
20$input_file = $ARGV[0];
21$version = 0;
22
23sub extract_spec_version;
24sub extract_asn1;
25
26open(INPUT_FILE, "< $input_file") or die "Can not open file $input_file";
27
28extract_spec_version();
29
30extract_asn1();
31
32close(INPUT_FILE);
33
34# This subroutine extracts the version of the specification
35sub extract_spec_version {
36  my $line;
37  while($line = <INPUT_FILE>){
38    if($line =~ m/3GPP TS ((25|36|38)\.331|(36|37)\.355|(36|38)\.413|(36|38)\.423|(36|38)\.455|38\.463|38\.473) 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"
49sub extract_asn1 {
50  my $line;
51  my $prev_line;
52  my $is_asn1 = 0;
53  my $output_file_name = 0;
54  my $file_name_found = 0;
55
56  while($line = <INPUT_FILE>){
57    if ($line =~ m/-- ASN1STOP/) {
58      $is_asn1 = 0;
59    }
60
61    if(($file_name_found == 0) && ($line =~ m/^LPP-Broadcast-Definitions/)){
62      $output_file_name = "LPP-Broadcast-Definitions.asn";
63      print  "generating $output_file_name\n";
64      open(OUTPUT_FILE, "> $output_file_name") or die "Can not open file $output_file_name";
65      $file_name_found = 1;
66      syswrite OUTPUT_FILE,"-- "."$version"."\n";
67    }
68
69    if(($file_name_found == 0) && ($line =~ m/SonTransfer-IEs/)){
70      $output_file_name = "S1AP-SonTransfer-IEs.asn";
71      print  "generating $output_file_name\n";
72      open(OUTPUT_FILE, "> $output_file_name") or die "Can not open file $output_file_name";
73      $is_asn1 = 1;
74      $file_name_found = 1;
75      syswrite OUTPUT_FILE,"-- "."$version"."\n";
76    }
77
78    if(($file_name_found == 0) && ($line =~ m/itu-t \(0\) identified-organization \(4\) etsi \(0\) mobileDomain \(0\)/)){
79      ($output_file_name) = ($prev_line =~ m/^([a-zA-Z0-9\-]+)\s/);
80      $output_file_name = "$output_file_name".".asn";
81      print  "generating $output_file_name\n";
82      open(OUTPUT_FILE, "> $output_file_name") or die "Can not open file $output_file_name";
83      $is_asn1 = 1;
84      $file_name_found = 1;
85      syswrite OUTPUT_FILE,"-- "."$version"."\n";
86      syswrite OUTPUT_FILE,"$prev_line";
87    }
88
89    if(($file_name_found == 0) && ($line =~ m/DEFINITIONS AUTOMATIC TAGS ::=/)){
90      ($output_file_name) = ($line =~ m/^([a-zA-Z0-9\-]+)\s+DEFINITIONS AUTOMATIC TAGS ::=/);
91      $output_file_name = "$output_file_name".".asn";
92      print  "generating $output_file_name\n";
93      open(OUTPUT_FILE, "> $output_file_name") or die "Can not open file $output_file_name";
94      $is_asn1 = 1;
95      $file_name_found = 1;
96      syswrite OUTPUT_FILE,"-- "."$version"."\n";
97    }
98
99    if (($line =~ /^END[\r\n]/) && (defined fileno OUTPUT_FILE)){
100      syswrite OUTPUT_FILE,"$line";
101      close(OUTPUT_FILE);
102      $is_asn1 = 0;
103      $file_name_found = 0;
104    }
105
106    if (($is_asn1 == 1) && (defined fileno OUTPUT_FILE)){
107      syswrite OUTPUT_FILE,"$line";
108    }
109
110    if ($line =~ m/-- ASN1START/) {
111      $is_asn1 = 1;
112    }
113
114    $prev_line = $line;
115  }
116}
117
118