Tool to (re)configure the sysmoUSIM and sysmoISIM cards
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.8 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. A collection of useful routines to make this tool work
  5. (C) 2017 by Sysmocom s.f.m.c. GmbH
  6. All Rights Reserved
  7. Author: Philipp Maier
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. # Convert list to an printable ascii hex string
  20. def hexdump(array, multilne = False, width = 30, prefix = " "):
  21. if array == None:
  22. return "(no data)"
  23. if multilne:
  24. result = ""
  25. for i in range(0, len(array), width):
  26. buf = array[i:i + width]
  27. result += prefix
  28. result += ''.join('{:02x}'.format(x) for x in buf)
  29. result += "\n"
  30. return result.rstrip()
  31. else:
  32. return ''.join('{:02x}'.format(x) for x in array)
  33. # Convert ascii string with decimal numbers to numeric ascii-code list
  34. def ascii_to_list(string):
  35. rc = []
  36. for c in string:
  37. rc.append(ord(c))
  38. return rc
  39. # Convert an ascii hex string to numeric list
  40. def asciihex_to_list(string):
  41. string = string.translate(None, ':')
  42. try:
  43. return map(ord, string.decode("hex"))
  44. except:
  45. print("Warning: Invalid hex string -- ignored!")
  46. return []
  47. # Pad an ascihex string with a nibble in case it is "incomplete"
  48. def pad_asciihex(string, front=False, padding='f'):
  49. if front and len(string) % 2 != 0:
  50. return padding + string
  51. elif len(string) % 2 != 0:
  52. return string + padding
  53. return string
  54. # Swap nibbles of each byte in an array
  55. def swap_nibbles(array):
  56. rc = []
  57. for a in array:
  58. rc.append(((a & 0xf0) >> 4) | ((a & 0x0f) << 4))
  59. return rc
  60. # Convert from list of bytes to big-endian integer
  61. def list_to_int(arr):
  62. return int(hexdump(arr), 16)
  63. # Encode an integer number into list of bytes (e.g. 1025 becomes [4, 1])
  64. def int_to_list(inp, num_bytes):
  65. out = []
  66. for i in range(0, num_bytes):
  67. shift_bits = ((num_bytes-1-i) * 8)
  68. out.append((inp >> shift_bits) & 0xFF)
  69. return out
  70. # Lookup a string in a given table by its ID
  71. def id_to_str(table, nr):
  72. dict_by_nr = dict(table)
  73. return dict_by_nr.get(nr) or '(invalid)'
  74. # Convert a string back to its ID by looking it up in a given table
  75. def str_to_id(table, string):
  76. dict_by_name = dict([(name.upper(), nr) for nr, name in table])
  77. id = dict_by_name.get(string.upper())
  78. if id is None:
  79. raise ValueError('identifier (\"%s\") not in table %s' % (string, str(table)))
  80. return id