utils.py: use generic functions to lookup strings in a table
To find out which algorithm ID corresponds to which string and vice versa we currently use tables and dictionaries. However, this way can be generalized and other parts of the program can profit from that Change-Id: I68388ae27d05fcd1bc9de100e040da0c2bb8e799 Related: SYS#4466
This commit is contained in:
parent
6fa296a800
commit
8e427d0b68
|
@ -272,17 +272,10 @@ sysmo_usim_algorithms = (
|
||||||
(9, 'CIS-B'),
|
(9, 'CIS-B'),
|
||||||
)
|
)
|
||||||
|
|
||||||
sysmo_usim_algorithms_dict_by_nr = dict(sysmo_usim_algorithms)
|
sysmo_usim_opcmodes = (
|
||||||
sysmo_usim_algorithms_dict_by_name = dict([(name.upper(), nr) for nr, name in sysmo_usim_algorithms])
|
(0, 'OP'),
|
||||||
|
(1, 'OPc'),
|
||||||
def sysmo_usim_algo_to_str(alg_nr):
|
)
|
||||||
return sysmo_usim_algorithms_dict_by_nr.get(alg_nr) or 'INVALID'
|
|
||||||
|
|
||||||
def sysmo_usim_str_to_algo(alg_str):
|
|
||||||
alg_nr = sysmo_usim_algorithms_dict_by_name.get(alg_str.upper())
|
|
||||||
if alg_nr is None:
|
|
||||||
raise ValueError('Unknown Algorithm %s' % alg_str)
|
|
||||||
return alg_nr
|
|
||||||
|
|
||||||
# Show current athentication parameters
|
# Show current athentication parameters
|
||||||
# (Which algorithim is used for which rat?)
|
# (Which algorithim is used for which rat?)
|
||||||
|
@ -297,8 +290,8 @@ def sysmo_usim_show_auth_params(sim):
|
||||||
algo_2g, algo_3g = res.apdu[:2]
|
algo_2g, algo_3g = res.apdu[:2]
|
||||||
|
|
||||||
print " * Current algorithm setting:"
|
print " * Current algorithm setting:"
|
||||||
print " 2G: %d=%s" % (algo_2g, sysmo_usim_algo_to_str(algo_2g))
|
print " 2G: %d=%s" % (algo_2g, id_to_str(sysmo_usim_algorithms, algo_2g))
|
||||||
print " 3G: %d=%s" % (algo_3g, sysmo_usim_algo_to_str(algo_3g))
|
print " 3G: %d=%s" % (algo_3g, id_to_str(sysmo_usim_algorithms, algo_3g))
|
||||||
|
|
||||||
|
|
||||||
# Program new authentication parameters
|
# Program new authentication parameters
|
||||||
|
@ -307,16 +300,16 @@ def sysmo_usim_write_auth_params(sim, algo_2g_str, algo_3g_str):
|
||||||
if algo_2g_str.isdigit():
|
if algo_2g_str.isdigit():
|
||||||
algo_2g = int(algo_2g_str)
|
algo_2g = int(algo_2g_str)
|
||||||
else:
|
else:
|
||||||
algo_2g = sysmo_usim_str_to_algo(algo_2g_str)
|
algo_2g = str_to_id(sysmo_usim_algorithms, algo_2g_str)
|
||||||
|
|
||||||
if algo_3g_str.isdigit():
|
if algo_3g_str.isdigit():
|
||||||
algo_3g = int(algo_3g_str)
|
algo_3g = int(algo_3g_str)
|
||||||
else:
|
else:
|
||||||
algo_3g = sysmo_usim_str_to_algo(algo_3g_str)
|
algo_3g = str_to_id(sysmo_usim_algorithms, algo_3g_str)
|
||||||
|
|
||||||
print " * New algorithm setting:"
|
print " * New algorithm setting:"
|
||||||
print " 2G: %d=%s" % (algo_2g, sysmo_usim_algo_to_str(algo_2g))
|
print " 2G: %d=%s" % (algo_2g, id_to_str(sysmo_usim_algorithms, algo_2g))
|
||||||
print " 3G: %d=%s" % (algo_3g, sysmo_usim_algo_to_str(algo_3g))
|
print " 3G: %d=%s" % (algo_3g, id_to_str(sysmo_usim_algorithms, algo_3g))
|
||||||
|
|
||||||
sysmo_usim_init(sim)
|
sysmo_usim_init(sim)
|
||||||
|
|
||||||
|
@ -418,15 +411,6 @@ def sysmo_usim_write_milenage_params(sim, ef_mlngc):
|
||||||
sim.update_binary(ef_mlngc.encode())
|
sim.update_binary(ef_mlngc.encode())
|
||||||
|
|
||||||
|
|
||||||
def sysmo_usim_opcmode2str(mode):
|
|
||||||
if mode == 1:
|
|
||||||
return 'OPc'
|
|
||||||
elif mode == 0:
|
|
||||||
return 'OP'
|
|
||||||
else:
|
|
||||||
raise ValueError('Unknown Mode ', mode)
|
|
||||||
|
|
||||||
|
|
||||||
# Show current OPc value
|
# Show current OPc value
|
||||||
def sysmo_usim_show_opc_params(sim):
|
def sysmo_usim_show_opc_params(sim):
|
||||||
sysmo_usim_init(sim)
|
sysmo_usim_init(sim)
|
||||||
|
@ -436,7 +420,7 @@ def sysmo_usim_show_opc_params(sim):
|
||||||
sim.select(SYSMO_USIMSJS1_EF_OPC)
|
sim.select(SYSMO_USIMSJS1_EF_OPC)
|
||||||
res = sim.read_binary(17)
|
res = sim.read_binary(17)
|
||||||
|
|
||||||
mode_str = sysmo_usim_opcmode2str(res.apdu[0])
|
mode_str = id_to_str(sysmo_usim_opcmodes, res.apdu[0])
|
||||||
|
|
||||||
print " * Current OP/OPc setting:"
|
print " * Current OP/OPc setting:"
|
||||||
print " %s: %s" % (mode_str, hexdump(res.apdu[1:]))
|
print " %s: %s" % (mode_str, hexdump(res.apdu[1:]))
|
||||||
|
@ -445,7 +429,7 @@ def sysmo_usim_show_opc_params(sim):
|
||||||
# Program new OPc value
|
# Program new OPc value
|
||||||
def sysmo_usim_write_opc_params(sim, select, op):
|
def sysmo_usim_write_opc_params(sim, select, op):
|
||||||
print " * New OPc setting:"
|
print " * New OPc setting:"
|
||||||
print " %s: %s" % (sysmo_usim_opcmode2str(select), hexdump(op))
|
print " %s: %s" % (id_to_str(sysmo_usim_opcmodes, select), hexdump(op))
|
||||||
print " OP/OPc: " + hexdump(op)
|
print " OP/OPc: " + hexdump(op)
|
||||||
|
|
||||||
sysmo_usim_init(sim)
|
sysmo_usim_init(sim)
|
||||||
|
|
16
utils.py
16
utils.py
|
@ -78,3 +78,19 @@ def int_to_list(inp, num_bytes):
|
||||||
shift_bits = ((num_bytes-1-i) * 8)
|
shift_bits = ((num_bytes-1-i) * 8)
|
||||||
out.append((inp >> shift_bits) & 0xFF)
|
out.append((inp >> shift_bits) & 0xFF)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
# Lookup a string in a given table by its ID
|
||||||
|
def id_to_str(table, nr):
|
||||||
|
dict_by_nr = dict(table)
|
||||||
|
return dict_by_nr.get(nr) or '(invalid)'
|
||||||
|
|
||||||
|
|
||||||
|
# Convert a string back to its ID by looking it up in a given table
|
||||||
|
def str_to_id(table, string):
|
||||||
|
dict_by_name = dict([(name.upper(), nr) for nr, name in table])
|
||||||
|
id = dict_by_name.get(string.upper())
|
||||||
|
|
||||||
|
if id is None:
|
||||||
|
raise ValueError('identifier (\"%s\") not in table %s' % (string, str(table)))
|
||||||
|
return id
|
||||||
|
|
Loading…
Reference in New Issue