def unpack_number(self): # variable length numbers, MSB first, 7 bits per byte, last byte is # flagged by MSB set raw = self.buffer[self.offset:self.offset + 10] number, consumed = decint(raw) self.advance(consumed) return number
def unpack_number(self): # variable length numbers, MSB first, 7 bits per byte, last byte is # flagged by MSB set raw = self.buffer[self.offset:self.offset+10] number, consumed = decint(raw) self.advance(consumed) return number
def __init__(self, records, codec): self.records = OrderedDict() record_offset = 0 for raw in records: pos = 0 while pos < len(raw): length, consumed = decint(raw[pos:]) if length > 0: try: self.records[pos+record_offset] = raw[ pos+consumed:pos+consumed+length].decode(codec) except: byts = raw[pos:] r = format_bytes(byts) print ('CNCX entry at offset %d has unknown format %s'%( pos+record_offset, r)) self.records[pos+record_offset] = r pos = len(raw) pos += consumed+length record_offset += 0x10000
def get_tag_map(control_byte_count, tagx, data, strict=False): ptags = [] ans = {} control_bytes = list(bytearray(data[:control_byte_count])) data = data[control_byte_count:] for x in tagx: if x.eof == 0x01: control_bytes = control_bytes[1:] continue value = control_bytes[0] & x.bitmask if value != 0: value_count = value_bytes = None if value == x.bitmask: if count_set_bits(x.bitmask) > 1: # If all bits of masked value are set and the mask has more # than one bit, a variable width value will follow after # the control bytes which defines the length of bytes (NOT # the value count!) which will contain the corresponding # variable width values. value_bytes, consumed = decint(data) data = data[consumed:] else: value_count = 1 else: # Shift bits to get the masked value. mask = x.bitmask while mask & 0b1 == 0: mask >>= 1 value >>= 1 value_count = value ptags.append( PTagX(x.tag, value_count, value_bytes, x.num_of_values)) for x in ptags: values = [] if x.value_count is not None: # Read value_count * values_per_entry variable width values. for _ in xrange(x.value_count * x.num_of_values): byts, consumed = decint(data) data = data[consumed:] values.append(byts) else: # value_bytes is not None # Convert value_bytes to variable width values. total_consumed = 0 while total_consumed < x.value_bytes: # Does this work for values_per_entry != 1? byts, consumed = decint(data) data = data[consumed:] total_consumed += consumed values.append(byts) if total_consumed != x.value_bytes: err = ("Error: Should consume %s bytes, but consumed %s" % (x.value_bytes, total_consumed)) if strict: raise ValueError(err) else: print(err) ans[x.tag] = values # Test that all bytes have been processed if data.replace(b'\0', b''): err = ("Warning: There are unprocessed index bytes left: %s" % format_bytes(data)) if strict: raise ValueError(err) else: print(err) return ans
def get_tag_map(control_byte_count, tagx, data, strict=False): ptags = [] ans = {} control_bytes = list(bytearray(data[:control_byte_count])) data = data[control_byte_count:] for x in tagx: if x.eof == 0x01: control_bytes = control_bytes[1:] continue value = control_bytes[0] & x.bitmask if value != 0: value_count = value_bytes = None if value == x.bitmask: if count_set_bits(x.bitmask) > 1: # If all bits of masked value are set and the mask has more # than one bit, a variable width value will follow after # the control bytes which defines the length of bytes (NOT # the value count!) which will contain the corresponding # variable width values. value_bytes, consumed = decint(data) data = data[consumed:] else: value_count = 1 else: # Shift bits to get the masked value. mask = x.bitmask while mask & 0b1 == 0: mask >>= 1 value >>= 1 value_count = value ptags.append(PTagX(x.tag, value_count, value_bytes, x.num_of_values)) for x in ptags: values = [] if x.value_count is not None: # Read value_count * values_per_entry variable width values. for _ in xrange(x.value_count * x.num_of_values): byts, consumed = decint(data) data = data[consumed:] values.append(byts) else: # value_bytes is not None # Convert value_bytes to variable width values. total_consumed = 0 while total_consumed < x.value_bytes: # Does this work for values_per_entry != 1? byts, consumed = decint(data) data = data[consumed:] total_consumed += consumed values.append(byts) if total_consumed != x.value_bytes: err = ("Error: Should consume %s bytes, but consumed %s" % (x.value_bytes, total_consumed)) if strict: raise ValueError(err) else: print(err) ans[x.tag] = values # Test that all bytes have been processed if data.replace(b'\0', b''): err = ("Warning: There are unprocessed index bytes left: %s" % format_bytes(data)) if strict: raise ValueError(err) else: print(err) return ans