blob: 9df65b5a18776f360be0732e5cefbdb3cdf32d8f [file] [log] [blame]
Oliver Smith276c5a72019-12-10 14:37:25 +01001#!/usr/bin/env python3
Neels Hofmeyr6b883f72017-01-31 16:40:28 +01002# Convert test sets pasted from 3GPP TS 55.205 to C code.
3
4# (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
5#
6# All Rights Reserved
7#
8# Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU Affero General Public License as published by
12# the Free Software Foundation; either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU Affero General Public License for more details.
19#
20# You should have received a copy of the GNU Affero General Public License
21# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23import sys, os
24
25script_dir = sys.path[0]
26
27fields = (
28 'Ki',
29 'RAND',
30 'OP',
31 'OPc',
32 'MIL3G-RES',
33 'SRES#1',
34 'SRES#2',
35 'MIL3G-CK',
36 'MIL3G-IK',
37 'Kc',
38)
39
40test_sets_lines = []
41test_set_lines = None
42
43for line in [l.strip() for l in open(os.path.join(script_dir, 'ts55_205_test_sets.txt'), 'r')]:
44 if line.startswith('Test Set'):
45 if test_set_lines:
46 test_sets_lines.append(test_set_lines)
47 test_set_lines = []
48 elif len(line) == 8:
49 try:
50 is_hex = int(line, 16)
51 test_set_lines.append(line)
52 except ValueError:
53 pass
54
55if test_set_lines:
56 test_sets_lines.append(test_set_lines)
57
58# Magic fixups for PDF-to-text uselessness
59idx = (( 0, 10, 15, 19),
60 ( 1, 11, 16, 20),
61 ( 2, 12, 17, 21),
62 ( 3, 13, 18, 22),
63 ( 4, 14),
64 ( 5, ),
65 ( 6, ),
66 ( 7, 23, 26, 28),
67 ( 8, 24, 27, 29),
68 ( 9, 25 ),
69 )
70
71test_sets = []
72for l in test_sets_lines:
73 test_sets.append( [ ''.join([l[i] for i in li]) for li in idx ] )
74
75func_templ = open(os.path.join(script_dir, 'func_template.c'), 'r').read()
76
77funcs = []
78func_calls = []
79nr = 0
80for test_set in test_sets:
81 nr += 1
82 func_name = 'test_set_%d' % nr
83 kwargs = dict(zip(fields, test_set))
84 kwargs['func_name'] = func_name
85
86 func_calls.append('\t%s();' % func_name)
87 funcs.append(func_templ.format(**kwargs))
88
89templ = open(os.path.join(script_dir, 'main_template.c')).read()
90
91code = templ.replace('FUNCTIONS', '\n'.join(funcs)).replace('FUNCTION_CALLS', '\n'.join(func_calls))
92
93print('''
94/***** DO NOT EDIT THIS FILE -- THIS CODE IS GENERATED *****
95 ***** by gen_ts_55_205_test_sets/pdftxt_2_c.py *****/
96''')
97print(code)
98