def build_packet(typeid, target=None): """Build a new editable packet, based off the given Message type""" # Allow passing in payload classes if hasattr(typeid, 'typeid'): typeid = getattr(typeid, 'typeid') # Allow passing in names if isinstance(typeid, str): typeid = TYPE_IDS_BY_NAME.get(typeid) if typeid not in VALID_TYPE_IDS: raise ValueError("Unknown `typeid`!") def trim(pkt): fields = [f.name for f in pkt] assert fields[-1] == 'raw[]' del pkt['raw[]'] pkt['size'].value = int(pkt.size / 8) # bits! # Hachoir does not really support "building", so we instead # use a empty buffer, parse it, then convert that to an editable. buffer = bytes(1024) stream = StringInputStream(buffer) ro_packet = DeviceFrameParser(stream) packet = createEditor(ro_packet) packet['type'].value = typeid packet['protocol'].value = 1024 if target: packet['target'].value = target packet['addressable'].value = 1 packet['tagged'].value = (typeid == 2) trim(packet) if typeid in EMPTY_REQUEST_PAYLOADS_IDS: packet.proto = ro_packet return packet # Here's where the real fun begins! # We need to re-hydrate the packet, so that all the fields are known # This all feels quite dodgy, though... temp_data = serialise_packet(packet) new_ro_packet = parse_packet(temp_data + bytes(1024 - len(temp_data))) new_ro_packet.walk() new_packet = createEditor(new_ro_packet) new_packet.proto = new_ro_packet trim(new_packet) return new_packet
def main(): # Parse arguments and read filenames values, filenames = parseOptions() level = 0 for item in values.strip.split(","): item = item.strip() if item == "useless": level |= STRIP_USELESS elif item == "metadata": level |= STRIP_METADATA elif item == "index": level |= STRIP_INDEX if not level: print("Nothing to do, exit") sys.exit(0) ok = True for filename in filenames: print("[+]", ("Process file %s" % filename)) parser = createParser(filename) if parser: editor = createEditor(parser) ok &= stripEditor(editor, filename + ".new", level, not (values.quiet)) else: ok = False if ok: sys.exit(0) else: sys.exit(1)
def __init__(self, packet): if hasattr(packet, 'proto'): self._proto = packet.proto self._packet = packet else: self._proto = packet walk_packet(self._proto) self._packet = createEditor(packet) self._packet.proto = self.proto self._payload_name = self._packet['type'].display if self._payload_name in self._packet: self._payload_field = self._packet[self._payload_name] payload_fields = [f.name for f in self._payload_field if not f.name.startswith("reserved")] self._payload_fields = tuple(payload_fields) else: self._payload_field = None self._payload_fields = tuple() header_fields = [f.name for f in self._packet if not f.name.startswith("reserved")] if self._payload_name in header_fields: header_fields.remove(self._payload_name) self._header_fields = tuple(header_fields)
def __init__(self, input_filename, **kw): Fuse.__init__(self, **kw) log.setFilename("/home/haypo/fuse_log") self.hachoir = createParser(input_filename) if True: self.hachoir = createEditor(self.hachoir) self.readonly = False else: self.readonly = True self.fs_charset = "ASCII"
def strip_webm(filename): parser = createParser(filename) editor = createEditor(parser) output = stripEditor(editor) # print(output.filename) if output: return output.filename else: return filename
from hachoir.parser import createParser from hachoir.editor import createEditor from hachoir.field import writeIntoFile parser = createParser("file.zip") with parser: editor = createEditor(parser) editor["end_central_directory/comment"].value = "new comment" writeIntoFile(editor, "file_comment.zip")