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