How to use the hachoir.field.Bytes function in hachoir

To help you get started, we’ve selected a few hachoir examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vstinner / hachoir / hachoir / parser / file_system / linux_swap.py View on Github external
PAGE_SIZE = 4096

# Definition of MAX_SWAP_BADPAGES in Linux kernel:
#  (__swapoffset(magic.magic) - __swapoffset(info.badpages)) / sizeof(int)
MAX_SWAP_BADPAGES = ((PAGE_SIZE - 10) - 1536) // 4


class Page(RawBytes):
    static_size = PAGE_SIZE * 8

    def __init__(self, parent, name):
        RawBytes.__init__(self, parent, name, PAGE_SIZE)


class UUID(Bytes):
    static_size = 16 * 8

    def __init__(self, parent, name, *args, **kwargs):
        Bytes.__init__(self, parent, name, 16, *args, **kwargs)

    def createDisplay(self):
        text = str2hex(self.value, format=r"%02x")
        return "%s-%s-%s-%s-%s" % (
            text[:8], text[8:12], text[12:16], text[16:20], text[20:])


class LinuxSwapFile(Parser):
    PARSER_TAGS = {
        "id": "linux_swap",
        "file_ext": ("",),
        "category": "file_system",
github vstinner / hachoir / hachoir / parser / file_system / fat.py View on Github external
yield textHandler(UInt32(self, "serial", "ID (serial number)"), hexadecimal)
        yield String(self, "label", 11, "Volume Label", strip=' ', charset="ASCII")
        yield String(self, "fs_type", 8, "FAT file system type", strip=' ', charset="ASCII")
        yield Bytes(self, "code", 510 - self.current_size // 8, "Operating system boot code")
        yield Bytes(self, "trail_sig", 2, "Signature (0x55 0xAA)")


class FSInfo(StaticFieldSet):
    format = (
        (String, "lead_sig", 4, 'Signature ("RRaA")'),
        (NullBytes, "reserved[]", 480),
        (String, "struct_sig", 4, 'Signature ("rrAa")'),
        (UInt32, "free_count", "Last known free cluster count on the volume"),
        (UInt32, "nxt_free",),
        (NullBytes, "reserved[]", 12),
        (Bytes, "trail_sig", 4, "Signature (0x00 0x00 0x55 0xAA)")
    )


class FAT(FieldSet):

    def createFields(self):
        version = self.parent.version
        max_entry = 1 << min(28, version)

        def FatEntry(chunk):
            i = chunk.value
            j = (1 - i) % max_entry
            if j == 0:
                return "reserved cluster"
            elif j == 1:
                return "free cluster"
github vstinner / hachoir / hachoir / parser / file_system / ntfs.py View on Github external
def parseData(self):
        size = (self.size - self.current_size) // 8
        if size:
            yield Bytes(self, "data", size)
github vstinner / hachoir / hachoir / parser / file_system / ntfs.py View on Github external
padding = self.seekByte(self["usa_offset"].value, relative=True)
            if padding:
                yield padding

            yield UInt16(self, "usa_number")
            for i in range(self["usa_count"].value):
                yield UInt16(self, "usa_value[]")

        padding = self.seekByte(self["attrs_offset"].value, relative=True)
        if padding:
            yield padding

        while not self.eof:
            addr = self.absolute_address + self.current_size
            if self.stream.readBytes(addr, 4) == b"\xFF\xFF\xFF\xFF":
                yield Bytes(self, "attr_end_marker", 8)
                break
            yield Attribute(self, "attr[]")

        size = self["bytes_in_use"].value - self.current_size // 8
        if size:
            yield RawBytes(self, "end_rawdata", size)

        size = (self.size - self.current_size) // 8
        if size:
            yield RawBytes(self, "end_padding", size, "Unused but allocated bytes")
github vstinner / hachoir / hachoir / parser / container / swf.py View on Github external
yield Tag(self, "tag[]")
        else:
            size = (self.size - self.current_size) // 8
            if has_deflate:
                data = Deflate(Bytes(self, "compressed_data", size), False)

                def createInputStream(cis, source=None, **args):
                    stream = cis(source=source)
                    header = StringInputStream(
                        b"FWS" + self.stream.readBytes(3 * 8, 5))
                    args.setdefault("tags", []).append(("class", SwfFile))
                    return ConcatStream((header, stream), source=stream.source, **args)
                data.setSubIStream(createInputStream)
                yield data
            else:
                yield Bytes(self, "compressed_data", size)
github vstinner / hachoir / hachoir / parser / image / tiff.py View on Github external
def createFields(self):
        for off, byte in getStrips(self._ifd):
            self.seekByte(off, relative=False)
            field = Bytes(self, "strip[]", byte)
            yield field
github vstinner / hachoir / hachoir / parser / archive / sevenzip.py View on Github external
def __init__(self, parent, name, length, decompressor, description=None,
                 parser=None, filename=None, mime_type=None, parser_class=None):
        if filename:
            if not isinstance(filename, str):
                filename = makePrintable(filename, "ISO-8859-1")
            if not description:
                description = 'File "%s" (%s)' % (
                    filename, humanFilesize(length))
        Bytes.__init__(self, parent, name, length, description)
        self.setupInputStream(decompressor, parser,
                              filename, mime_type, parser_class)
github vstinner / hachoir / hachoir / parser / program / nds.py View on Github external
def createFields(self):
        yield textHandler(UInt64(self, "id"), hexadecimal)
        if self["id"].value == 0xe7ffdeffe7ffdeff:  # indicates that secure area is decrypted
            yield Bytes(self, "fixed[]", 6)  # always \xff\xde\xff\xe7\xff\xde
            yield Crc16(self, "header_crc16", self.stream.readBytes(self.absolute_address + (16 * 8), 2048 - 16))
            yield RawBytes(self, "unknown[]", 2048 - 16 - 2)
            yield Bytes(self, "fixed[]", 2)  # always \0\0
        else:
            yield RawBytes(self, "encrypted[]", 2048 - 8)
github vstinner / hachoir / hachoir / parser / video / mpeg_video.py View on Github external
def createFields(self):
        yield Bytes(self, "sync", 3)
        yield textHandler(UInt8(self, "tag"), hexadecimal)
        if self.parser and self['tag'].value != 0xb7:
            yield self.parser(self, "content")
github vstinner / hachoir / hachoir / parser / misc / word_doc.py View on Github external
def __init__(self, parent, name, description=None,
                 strip=None, nbytes=None, truncate=None):
        Bytes.__init__(self, parent, name, 1, description)

        self._format = "WidePascalString16"
        self._strip = strip
        self._truncate = truncate
        self._character_size = 2
        self._charset = "UTF-16-LE"
        self._content_offset = 2
        self._content_size = self._character_size * self._parent.stream.readBits(
            self.absolute_address, self._content_offset * 8, self._parent.endian)
        self._size = (self._content_size + self.content_offset) * 8