2022-03-30 03:27:58 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-02-23 13:47:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
A collection of useful routines to make this tool work
|
|
|
|
|
|
|
|
(C) 2017 by Sysmocom s.f.m.c. GmbH
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Author: Philipp Maier
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Convert list to an printable ascii hex string
|
2019-11-08 11:54:47 +00:00
|
|
|
def hexdump(array, multilne = False, width = 30, prefix = " "):
|
|
|
|
|
|
|
|
if array == None:
|
2017-02-23 13:47:38 +00:00
|
|
|
return "(no data)"
|
|
|
|
|
2019-11-08 11:54:47 +00:00
|
|
|
if multilne:
|
|
|
|
result = ""
|
|
|
|
for i in range(0, len(array), width):
|
|
|
|
buf = array[i:i + width]
|
|
|
|
result += prefix
|
|
|
|
result += ''.join('{:02x}'.format(x) for x in buf)
|
|
|
|
result += "\n"
|
2019-11-07 13:51:41 +00:00
|
|
|
return result.rstrip()
|
2019-11-08 11:54:47 +00:00
|
|
|
else:
|
|
|
|
return ''.join('{:02x}'.format(x) for x in array)
|
|
|
|
|
2017-02-23 13:47:38 +00:00
|
|
|
|
|
|
|
# Convert ascii string with decimal numbers to numeric ascii-code list
|
|
|
|
def ascii_to_list(string):
|
|
|
|
rc = []
|
|
|
|
for c in string:
|
|
|
|
rc.append(ord(c))
|
|
|
|
return rc
|
|
|
|
|
|
|
|
|
|
|
|
# Convert an ascii hex string to numeric list
|
|
|
|
def asciihex_to_list(string):
|
|
|
|
|
2022-03-30 03:27:58 +00:00
|
|
|
string = string.translate(str.maketrans('','',':'))
|
2017-02-23 13:47:38 +00:00
|
|
|
try:
|
2022-03-30 03:27:58 +00:00
|
|
|
return list(map(ord,''.join(['%c' % b for b in bytearray.fromhex(string)])))
|
2017-02-23 13:47:38 +00:00
|
|
|
except:
|
2020-08-31 12:22:27 +00:00
|
|
|
print("Warning: Invalid hex string -- ignored!")
|
2017-02-23 13:47:38 +00:00
|
|
|
return []
|
2017-03-20 23:30:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Pad an ascihex string with a nibble in case it is "incomplete"
|
2018-08-08 08:49:26 +00:00
|
|
|
def pad_asciihex(string, front=False, padding='f'):
|
2017-03-20 23:30:34 +00:00
|
|
|
|
2018-08-08 08:49:26 +00:00
|
|
|
if front and len(string) % 2 != 0:
|
|
|
|
return padding + string
|
|
|
|
elif len(string) % 2 != 0:
|
|
|
|
return string + padding
|
2017-03-20 23:30:34 +00:00
|
|
|
return string
|
|
|
|
|
|
|
|
|
|
|
|
# Swap nibbles of each byte in an array
|
|
|
|
def swap_nibbles(array):
|
|
|
|
|
|
|
|
rc = []
|
|
|
|
for a in array:
|
|
|
|
rc.append(((a & 0xf0) >> 4) | ((a & 0x0f) << 4))
|
|
|
|
return rc
|
2017-08-16 15:20:26 +00:00
|
|
|
|
2019-11-07 13:12:41 +00:00
|
|
|
|
2017-08-16 15:20:26 +00:00
|
|
|
# Convert from list of bytes to big-endian integer
|
|
|
|
def list_to_int(arr):
|
|
|
|
return int(hexdump(arr), 16)
|
2017-08-17 12:36:19 +00:00
|
|
|
|
2019-11-07 13:21:08 +00:00
|
|
|
|
|
|
|
# Encode an integer number into list of bytes (e.g. 1025 becomes [4, 1])
|
2017-08-17 12:36:19 +00:00
|
|
|
def int_to_list(inp, num_bytes):
|
|
|
|
out = []
|
|
|
|
for i in range(0, num_bytes):
|
|
|
|
shift_bits = ((num_bytes-1-i) * 8)
|
|
|
|
out.append((inp >> shift_bits) & 0xFF)
|
|
|
|
return out
|
2019-11-07 13:09:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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
|