diff options
author | Sylvain Munaut <tnt@246tNt.com> | 2010-12-23 20:28:24 +0100 |
---|---|---|
committer | Sylvain Munaut <tnt@246tNt.com> | 2010-12-23 20:28:24 +0100 |
commit | 9f120e01696e05cfc5b5603deed60c073b9bee64 (patch) | |
tree | 7c6d73b9c1ecb2775bb70303cddcf6228ba05d95 | |
parent | 8d243e884757744af8c4708a1f8b30c2b30d9da8 (diff) |
pySim-prog: Fix insertion of Ki in HLR. Need to escape like DBI does it
Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
-rwxr-xr-x | pySim-prog.py | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/pySim-prog.py b/pySim-prog.py index f599cb7..04fb7c9 100755 --- a/pySim-prog.py +++ b/pySim-prog.py @@ -156,6 +156,39 @@ def _isnum(s, l=-1): return s.isdigit() and ((l== -1) or (len(s) == l)) +def _dbi_binary_quote(s): + # Count usage of each char + cnt = {} + for c in s: + cnt[c] = cnt.get(c, 0) + 1 + + # Find best offset + e = 0 + m = len(s) + for i in range(1, 256): + if i == 39: + continue + sum_ = cnt.get(i, 0) + cnt.get((i+1)&0xff, 0) + cnt.get((i+39)&0xff, 0) + if sum_ < m: + m = sum_ + e = i + if m == 0: # No overhead ? use this ! + break; + + # Generate output + out = [] + out.append( chr(e) ) # Offset + for c in s: + x = (256 + ord(c) - e) % 256 + if x in (0, 1, 39): + out.append('\x01') + out.append(chr(x+1)) + else: + out.append(chr(x)) + + return ''.join(out) + + def gen_parameters(opts): """Generates Name, ICCID, MCC, MNC, IMSI, SMSP, Ki from the options given by the user""" @@ -298,7 +331,7 @@ def write_parameters(opts, params): '(subscriber_id, algorithm_id, a3a8_ki)' + 'VALUES ' + '(?,?,?)', - [ sub_id, 2, sqlite3.Binary(h2b(params['ki'])) ], + [ sub_id, 2, sqlite3.Binary(_dbi_binary_quote(h2b(params['ki']))) ], ) conn.commit() |