From 0bab8734d588249a85c321f6a1e6dad096446c05 Mon Sep 17 00:00:00 2001 From: dummys Date: Wed, 29 Sep 2021 15:11:50 +0200 Subject: [PATCH 1/2] migration to python3 --- umap2/apps/list_classes.py | 4 +- umap2/dev/audio.py | 64 ++++++++++++++--------------- umap2/dev/cdc.py | 2 +- umap2/dev/cdc_acm.py | 2 +- umap2/dev/cdc_dl.py | 2 +- umap2/dev/hub.py | 4 +- umap2/dev/mass_storage.py | 4 +- umap2/fuzz/fuzz_engine.py | 2 +- umap2/fuzz/helpers.py | 10 ++--- umap2/fuzz/templates/audio.py | 6 +-- umap2/fuzz/templates/cdc.py | 2 +- umap2/fuzz/templates/enum.py | 2 +- umap2/fuzz/templates/hid.py | 8 ++-- umap2/phy/facedancer/max342x_phy.py | 2 +- 14 files changed, 57 insertions(+), 57 deletions(-) diff --git a/umap2/apps/list_classes.py b/umap2/apps/list_classes.py index cb37d31..80ff896 100644 --- a/umap2/apps/list_classes.py +++ b/umap2/apps/list_classes.py @@ -17,8 +17,8 @@ def run(self): ks = self.umap_classes verbose = self.options.get('--verbose', False) if verbose: - print '%-20s %s' % ('Device', 'Description') - print '-------------------- ----------------------------------------------------' + print('%-20s %s' % ('Device', 'Description')) + print('-------------------- ----------------------------------------------------') for k in ks: if verbose: print('%-20s %s' % (k, self.umap_class_dict[k][1])) diff --git a/umap2/dev/audio.py b/umap2/dev/audio.py index 6897b83..f917c9f 100644 --- a/umap2/dev/audio.py +++ b/umap2/dev/audio.py @@ -42,23 +42,23 @@ def setup_local_handlers(self): } self._settings = { # (val, index): [cur, min, max, res, (idle)] - (0x0100, 0x0001): ['\x44\xac\x00', '\x44\xac\x00', '\x80\xbb\x00', '\x80\xbb\x00'], + (0x0100, 0x0001): [b'\x44\xac\x00', b'\x44\xac\x00', b'\x80\xbb\x00', b'\x80\xbb\x00'], # (0x0100, 0x0002): ['\x44\xac\x00', '\x44\xac\x00', '\x80\xbb\x00', '\x80\xbb\x00'], - (0x0100, 0x0082): ['\x44\xac\x00', '\x44\xac\x00', '\x80\xbb\x00', '\x80\xbb\x00'], - (0x0100, 0x0900): ['\x00', '\x00', '\xff', '\x00'], - (0x0100, 0x0a00): ['\x01', '\x00', '\xff', '\x00'], - (0x0100, 0x0d00): ['\x01', '\x00', '\xff', '\x00'], - (0x0101, 0x0f00): ['\x01', '\x00', '\xff', '\x00'], - (0x0102, 0x0f00): ['\x01', '\x00', '\xff', '\x00'], - (0x0200, 0x0a00): ['\x00\x00', '\x00\x00', '\x55\x00', '\x30\x00', '\x00\x00'], - (0x0200, 0x0d00): ['\x80\x22', '\x00\x00', '\xd0\x00', '\x30\x00'], - (0x0201, 0x0900): ['\x80\x22', '\x20\x00', '\xa0\x00', '\x30\x00'], - (0x0201, 0x0f00): ['\x01', '\x00', '\xff', '\x00'], - (0x0202, 0x0900): ['\xcf\x00', '\x00\x00', '\xcf\x00', '\x30\x00'], - (0x0202, 0x0f00): ['\x01', '\x00', '\xff', '\x00'], - (0x0301, 0x0f00): ['\x01', '\x00', '\xff', '\x00'], - (0x0302, 0x0f00): ['\x00\x00', '\x00\x00', '\x00\x00', '\x00\x00'], - (0x0700, 0x0a00): ['\x01', '\x00', '\xff', '\x00'], + (0x0100, 0x0082): [b'\x44\xac\x00', b'\x44\xac\x00', b'\x80\xbb\x00', b'\x80\xbb\x00'], + (0x0100, 0x0900): [b'\x00', b'\x00', b'\xff', b'\x00'], + (0x0100, 0x0a00): [b'\x01', b'\x00', b'\xff', b'\x00'], + (0x0100, 0x0d00): [b'\x01', b'\x00', b'\xff', b'\x00'], + (0x0101, 0x0f00): [b'\x01', b'\x00', b'\xff', b'\x00'], + (0x0102, 0x0f00): [b'\x01', b'\x00', b'\xff', b'\x00'], + (0x0200, 0x0a00): [b'\x00\x00', b'\x00\x00', b'\x55\x00', b'\x30\x00', b'\x00\x00'], + (0x0200, 0x0d00): [b'\x80\x22', b'\x00\x00', b'\xd0\x00', b'\x30\x00'], + (0x0201, 0x0900): [b'\x80\x22', b'\x20\x00', b'\xa0\x00', b'\x30\x00'], + (0x0201, 0x0f00): [b'\x01', b'\x00', b'\xff', b'\x00'], + (0x0202, 0x0900): [b'\xcf\x00', b'\x00\x00', b'\xcf\x00', b'\x30\x00'], + (0x0202, 0x0f00): [b'\x01', b'\x00', b'\xff', b'\x00'], + (0x0301, 0x0f00): [b'\x01', b'\x00', b'\xff', b'\x00'], + (0x0302, 0x0f00): [b'\x00\x00', b'\x00\x00', b'\x00\x00', b'\x00\x00'], + (0x0700, 0x0a00): [b'\x01', b'\x00', b'\xff', b'\x00'], } self._cur = b'\x44\xac\x00' @@ -205,29 +205,29 @@ def __init__(self, app, phy, vid=0x0d8c, pid=0x000c, rev=0x0001, *args, **kwargs app=app, phy=phy, iface_num=0, iface_alt=0, iface_str_idx=0, cs_ifaces=[ # Class specific AC interface: header (4.3.2) - USBCSInterface('ACHeader', app, phy, '\x01\x00\x01\x64\x00\x02\x01\x02'), + USBCSInterface('ACHeader', app, phy, b'\x01\x00\x01\x64\x00\x02\x01\x02'), # Class specific AC interface: input terminal (Table 4.3.2.1) - USBCSInterface('ACInputTerminal0', app, phy, '\x02\x01\x01\x01\x00\x02\x03\x00\x00\x00'), - USBCSInterface('ACInputTerminal1', app, phy, '\x02\x02\x01\x02\x00\x01\x01\x00\x00\x00'), + USBCSInterface('ACInputTerminal0', app, phy, b'\x02\x01\x01\x01\x00\x02\x03\x00\x00\x00'), + USBCSInterface('ACInputTerminal1', app, phy, b'\x02\x02\x01\x02\x00\x01\x01\x00\x00\x00'), # Class specific AC interface: output terminal (Table 4.3.2.2) - USBCSInterface('ACOutputTerminal0', app, phy, '\x03\x06\x01\x03\x00\x09\x00'), - USBCSInterface('ACOutputTerminal1', app, phy, '\x03\x07\x01\x01\x00\x08\x00'), + USBCSInterface('ACOutputTerminal0', app, phy, b'\x03\x06\x01\x03\x00\x09\x00'), + USBCSInterface('ACOutputTerminal1', app, phy, b'\x03\x07\x01\x01\x00\x08\x00'), # Class specific AC interface: selector unit (Table 4.3.2.4) - USBCSInterface('ACSelectorUnit', app, phy, '\x05\x08\x01\x0a\x00'), + USBCSInterface('ACSelectorUnit', app, phy, b'\x05\x08\x01\x0a\x00'), # Class specific AC interface: feature unit (Table 4.3.2.5) - USBCSInterface('ACFeatureUnit0', app, phy, '\x06\x09\x0f\x01\x01\x02\x02\x00'), - USBCSInterface('ACFeatureUnit1', app, phy, '\x06\x0a\x02\x01\x43\x00\x00'), - USBCSInterface('ACFeatureUnit2', app, phy, '\x06\x0d\x02\x01\x03\x00\x00'), + USBCSInterface('ACFeatureUnit0', app, phy, b'\x06\x09\x0f\x01\x01\x02\x02\x00'), + USBCSInterface('ACFeatureUnit1', app, phy, b'\x06\x0a\x02\x01\x43\x00\x00'), + USBCSInterface('ACFeatureUnit2', app, phy, b'\x06\x0d\x02\x01\x03\x00\x00'), # Class specific AC interface: mixer unit (Table 4.3.2.3) - USBCSInterface('ACMixerUnit', app, phy, '\x04\x0f\x02\x01\x0d\x02\x03\x00\x00\x00\x00'), + USBCSInterface('ACMixerUnit', app, phy, b'\x04\x0f\x02\x01\x0d\x02\x03\x00\x00\x00\x00'), ], usb_class=usb_class ), USBAudioStreamingInterface( app=app, phy=phy, iface_num=1, iface_alt=0, iface_str_idx=0, cs_ifaces=[ - USBCSInterface('ASGeneral', app, phy, '\x01\x01\x01\x01\x00'), - USBCSInterface('ASFormatType', app, phy, '\x02\x01\x02\x02\x10\x02\x44\xac\x00\x44\xac\x00'), + USBCSInterface('ASGeneral', app, phy, b'\x01\x01\x01\x01\x00'), + USBCSInterface('ASFormatType', app, phy, b'\x02\x01\x02\x02\x10\x02\x44\xac\x00\x44\xac\x00'), ], endpoints=[ USBEndpoint( @@ -240,7 +240,7 @@ def __init__(self, app, phy, vid=0x0d8c, pid=0x000c, rev=0x0001, *args, **kwargs interval=1, handler=audio_streaming.data_available, cs_endpoints=[ - USBCSEndpoint('ASEndpoint', app, phy, '\x01\x01\x01\x01\x00') + USBCSEndpoint('ASEndpoint', app, phy, b'\x01\x01\x01\x01\x00') ], usb_class=usb_class, ) @@ -250,8 +250,8 @@ def __init__(self, app, phy, vid=0x0d8c, pid=0x000c, rev=0x0001, *args, **kwargs USBAudioStreamingInterface( app=app, phy=phy, iface_num=2, iface_alt=0, iface_str_idx=0, cs_ifaces=[ - USBCSInterface('ASGeneral', app, phy, '\x01\x07\x01\x01\x00'), - USBCSInterface('ASFormatType', app, phy, '\x02\x01\x01\x02\x10\x02\x44\xac\x00\x44\xac\x00'), + USBCSInterface('ASGeneral', app, phy, b'\x01\x07\x01\x01\x00'), + USBCSInterface('ASFormatType', app, phy, b'\x02\x01\x01\x02\x10\x02\x44\xac\x00\x44\xac\x00'), ], endpoints=[ USBEndpoint( @@ -264,7 +264,7 @@ def __init__(self, app, phy, vid=0x0d8c, pid=0x000c, rev=0x0001, *args, **kwargs interval=1, handler=audio_streaming.buffer_available, cs_endpoints=[ - USBCSEndpoint('ASEndpoint', app, phy, '\x01\x01\x00\x00\x00') + USBCSEndpoint('ASEndpoint', app, phy, b'\x01\x01\x00\x00\x00') ], usb_class=usb_class, ) diff --git a/umap2/dev/cdc.py b/umap2/dev/cdc.py index 16235aa..9fae8fc 100644 --- a/umap2/dev/cdc.py +++ b/umap2/dev/cdc.py @@ -191,7 +191,7 @@ def handle_getter(self, req): key = (param_id, req.value, req.index) if key in self.params: return self.params[key] - return '\x00' * req.length + return b'\x00' * req.length def handle_clear(self, req): param_id = self.get_param_id_from_request(req.request) diff --git a/umap2/dev/cdc_acm.py b/umap2/dev/cdc_acm.py index ae3f063..712d747 100644 --- a/umap2/dev/cdc_acm.py +++ b/umap2/dev/cdc_acm.py @@ -29,7 +29,7 @@ def __init__(self, app, phy, vid=0x2548, pid=0x1001, rev=0x0010, cs_interfaces=N cdc_cls = self.get_default_class(app, phy) cs_interfaces = [ # Header Functional Descriptor - FD(app, phy, FD.Header, '\x01\x01'), + FD(app, phy, FD.Header, b'\x01\x01'), # Call Management Functional Descriptor FD(app, phy, FD.CM, struct.pack('BB', bmCapabilities, USBCDCDevice.bDataInterface)), FD(app, phy, FD.ACM, struct.pack('B', bmCapabilities)), diff --git a/umap2/dev/cdc_dl.py b/umap2/dev/cdc_dl.py index 5b6af81..c823570 100644 --- a/umap2/dev/cdc_dl.py +++ b/umap2/dev/cdc_dl.py @@ -28,7 +28,7 @@ def __init__(self, app, phy, vid=0x2548, pid=0x1001, rev=0x0010, cs_interfaces=N cdc_cls = self.get_default_class(app, phy) cs_interfaces = [ # Header Functional Descriptor - FD(app, phy, FD.Header, '\x01\x01'), + FD(app, phy, FD.Header, b'\x01\x01'), # Call Management Functional Descriptor FD(app, phy, FD.CM, struct.pack('BB', bmCapabilities, USBCDCDevice.bDataInterface)), FD(app, phy, FD.DLM, struct.pack('B', bmCapabilities)), diff --git a/umap2/dev/hub.py b/umap2/dev/hub.py index 5397dbd..47dc4c7 100644 --- a/umap2/dev/hub.py +++ b/umap2/dev/hub.py @@ -54,8 +54,8 @@ def handle_get_descriptor(self, req): num_bytes = self.num_ports // 7 if self.num_ports % 7 != 0: num_bytes += 1 - d += '\x00' * num_bytes - d += '\xff' * num_bytes + d += b'\x00' * num_bytes + d += b'\xff' * num_bytes d = struct.pack('B', len(d) + 1) + d return d diff --git a/umap2/dev/mass_storage.py b/umap2/dev/mass_storage.py index 26b09c1..cf04eec 100644 --- a/umap2/dev/mass_storage.py +++ b/umap2/dev/mass_storage.py @@ -129,7 +129,7 @@ def put_sector_data(self, address, data): block_end = (address + 1) * self.block_size # slices are NON-inclusive pad_len = (self.block_size - (len(data) % self.block_size)) % self.block_size - data += '\x00' * pad_len + data += b'\x00' * pad_len self.image[block_start:block_end] = data[:self.block_size] self.image.flush() @@ -391,7 +391,7 @@ def handle_scsi_mode_sense(self, mode_type, page, subpage, alloc_len, ctrl, with if report is None: # default behaviour, taken from previous implementation # this should probably be changed ... - report = '\x07\x00\x00\x00\x00\x00\x00\x00' + report = b'\x07\x00\x00\x00\x00\x00\x00\x00' if with_header: self.debug('SCSI mode sense (%d) - adding header' % (mode_type)) report = self._report_header(mode_type, len(report)) + report diff --git a/umap2/fuzz/fuzz_engine.py b/umap2/fuzz/fuzz_engine.py index 15bcee0..6b47a08 100755 --- a/umap2/fuzz/fuzz_engine.py +++ b/umap2/fuzz/fuzz_engine.py @@ -20,7 +20,7 @@ from kitty.model import GraphModel from kitty.model import Template, Meta, String, UInt32 -from templates import audio, cdc, enum, generic, hid, hub, mass_storage +from . templates import audio, cdc, enum, generic, hid, hub, mass_storage from templates import smart_card from controller import UmapController diff --git a/umap2/fuzz/helpers.py b/umap2/fuzz/helpers.py index 8eed012..72d36f0 100644 --- a/umap2/fuzz/helpers.py +++ b/umap2/fuzz/helpers.py @@ -6,7 +6,6 @@ import binascii import inspect - class StageLogger(object): def __init__(self, filename): @@ -22,7 +21,8 @@ def stop(self): def log_stage(self, stage): if self.fd: - self.fd.write(stage + '\n') + + self.fd.write(stage.encode() + b'\n') self.fd.flush() @@ -46,8 +46,8 @@ def mutable(stage, silent=False): def wrap_f(func): func_self = None if inspect.ismethod(func): - func_self = func.im_self - func = func.im_func + func_self = func.__self__ + func = func.__func__ def wrapper(*args, **kwargs): if func_self is None: @@ -79,7 +79,7 @@ def wrapper(*args, **kwargs): self.logger.error(''.join(traceback.format_stack())) raise e if response is not None: - info('Response: %s' % binascii.hexlify(response)) + info(f'Response: {response.hex()}') return response return wrapper return wrap_f diff --git a/umap2/fuzz/templates/audio.py b/umap2/fuzz/templates/audio.py index c8502a6..1e27346 100644 --- a/umap2/fuzz/templates/audio.py +++ b/umap2/fuzz/templates/audio.py @@ -6,8 +6,8 @@ from kitty.model import Template, Repeat, List, Container, ForEach, OneOf from kitty.model import ElementCount, SizeInBytes from kitty.model import ENC_INT_LE -from hid import GenerateHidReport -from generic import Descriptor, SizedPt, DynamicInt, SubDescriptor +from . hid import GenerateHidReport +from . generic import Descriptor, SizedPt, DynamicInt, SubDescriptor class _AC_DescriptorSubTypes: # AC Interface Descriptor Subtype @@ -138,7 +138,7 @@ class _AS_DescriptorSubTypes: # AS Interface Descriptor Subtype audio_report_descriptor = Template( name='audio_report_descriptor', fields=GenerateHidReport( - '050C0901A1011500250109E909EA75019502810209E209008106050B092095018142050C09009503810226FF000900750895038102090095049102C0'.decode('hex') + bytes.fromhex('050C0901A1011500250109E909EA75019502810209E209008106050B092095018142050C09009503810226FF000900750895038102090095049102C0') ) ) diff --git a/umap2/fuzz/templates/cdc.py b/umap2/fuzz/templates/cdc.py index d455e16..c1d61ef 100644 --- a/umap2/fuzz/templates/cdc.py +++ b/umap2/fuzz/templates/cdc.py @@ -8,7 +8,7 @@ from kitty.model import Template, Repeat, List, Container, ForEach, OneOf from kitty.model import ElementCount from kitty.model import MutableField -from generic import SubDescriptor +from . generic import SubDescriptor cdc_control_interface_descriptor = Template( diff --git a/umap2/fuzz/templates/enum.py b/umap2/fuzz/templates/enum.py index 56fde91..09dddd1 100644 --- a/umap2/fuzz/templates/enum.py +++ b/umap2/fuzz/templates/enum.py @@ -10,7 +10,7 @@ from kitty.model import ElementCount, SizeInBytes # encoders from kitty.model import StrEncodeEncoder, ENC_INT_LE -from generic import Descriptor, SubDescriptor +from . generic import Descriptor, SubDescriptor # Device descriptor diff --git a/umap2/fuzz/templates/hid.py b/umap2/fuzz/templates/hid.py index 21c041c..b3eaf9d 100644 --- a/umap2/fuzz/templates/hid.py +++ b/umap2/fuzz/templates/hid.py @@ -24,7 +24,7 @@ from kitty.model import ENC_INT_LE from kitty.core import KittyException from random import Random -from generic import DynamicInt, Descriptor +from . generic import DynamicInt, Descriptor opcodes = { @@ -124,7 +124,7 @@ def GenerateHidReport(report_str, name=None): index = 0 namer = NameGen() while index < len(report_str): - opcode = ord(report_str[index]) + opcode = report_str[index] num_args = opcode & 3 if index + num_args >= len(report_str): raise KittyException('Not enough bytes in hid report for last opcode') @@ -134,7 +134,7 @@ def GenerateHidReport(report_str, name=None): fields.append(UInt8(opcode, name=cur_name)) else: args = report_str[index:index + num_args] - value = sum(ord(args[i]) << (i * 8) for i in range(len(args))) # little endian... + value = sum(args[i] << (i * 8) for i in range(len(args))) # little endian... fields.append(Container( name=cur_name, fields=[ @@ -179,6 +179,6 @@ def GenerateHidReport(report_str, name=None): hid_report_descriptor = Template( name='hid_report_descriptor', fields=GenerateHidReport( - '05010906A101050719E029E7150025017501950881029501750881011900296515002565750895018100C0'.decode('hex') + bytes.fromhex('05010906A101050719E029E7150025017501950881029501750881011900296515002565750895018100C0') ) ) diff --git a/umap2/phy/facedancer/max342x_phy.py b/umap2/phy/facedancer/max342x_phy.py index 31ee3d6..1aa9ed9 100644 --- a/umap2/phy/facedancer/max342x_phy.py +++ b/umap2/phy/facedancer/max342x_phy.py @@ -210,7 +210,7 @@ def service_irqs(self): self.clear_irq_bit(Regs.endpoint_irq, PINCTL.setup_data_avail) b = self.read_bytes(Regs.setup_data_fifo, 8) - if (irq & PINCTL.out0_data_avail) and (ord(b[0]) & 0x80 == 0x00): + if (irq & PINCTL.out0_data_avail) and (b[0] & 0x80 == 0x00): data_bytes_len = struct.unpack(' Date: Thu, 30 Sep 2021 10:50:00 +0200 Subject: [PATCH 2/2] removed useless binascii import --- umap2/fuzz/helpers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/umap2/fuzz/helpers.py b/umap2/fuzz/helpers.py index 72d36f0..277d1a2 100644 --- a/umap2/fuzz/helpers.py +++ b/umap2/fuzz/helpers.py @@ -3,7 +3,6 @@ ''' import traceback -import binascii import inspect class StageLogger(object):