How to use the hachoir.field.FieldSet 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 / audio / id3.py View on Github external
class ID3_TrackLength(FieldSet):

    def createFields(self):
        yield NullBytes(self, "zero", 1)
        yield textHandler(String(self, "length", self._size // 8 - 1,
                                 "Length in ms", charset="ASCII"), self.computeLength)

    def computeLength(self, field):
        try:
            ms = int(field.value)
            return humanDuration(ms)
        except Exception:
            return field.value


class ID3_Picture23(FieldSet):
    pict_type_name = {
        0x00: "Other",
        0x01: "32x32 pixels 'file icon' (PNG only)",
        0x02: "Other file icon",
        0x03: "Cover (front)",
        0x04: "Cover (back)",
        0x05: "Leaflet page",
        0x06: "Media (e.g. lable side of CD)",
        0x07: "Lead artist/lead performer/soloist",
        0x08: "Artist/performer",
        0x09: "Conductor",
        0x0A: "Band/Orchestra",
        0x0B: "Composer",
        0x0C: "Lyricist/text writer",
        0x0D: "Recording Location",
        0x0E: "During recording",
github vstinner / hachoir / hachoir / parser / archive / bzip2_parser.py View on Github external
addr += 1
            self._size += 1
            if not bit:
                break
            value += 1
        self._value = value

    def createValue(self):
        return self._value


def move_to_front(l, c):
    l[:] = l[c:c + 1] + l[0:c] + l[c + 1:]


class Bzip2Bitmap(FieldSet):

    def __init__(self, parent, name, nb_items, start_index, *args, **kwargs):
        FieldSet.__init__(self, parent, name, *args, **kwargs)
        self.nb_items = nb_items
        self.start_index = start_index

    def createFields(self):
        for i in range(self.start_index, self.start_index + self.nb_items):
            yield Bit(self, "symbol_used[%i]" % i, "Is the symbol %i (%r) used?" % (i, chr(i)))


class Bzip2Lengths(FieldSet):

    def __init__(self, parent, name, symbols, *args, **kwargs):
        FieldSet.__init__(self, parent, name, *args, **kwargs)
        self.symbols = symbols
github vstinner / hachoir / hachoir / parser / program / java.py View on Github external
raise ParserError("Not a valid constant pool element type: "
                              + self["tag"].value)

    def createDescription(self):
        return self.constant_type + "(%s)" % str(self)


###############################################################################
# field_info {
#        u2 access_flags;
#        u2 name_index;
#        u2 descriptor_index;
#        u2 attributes_count;
#        attribute_info attributes[attributes_count];
# }
class FieldInfo(FieldSet):

    def createFields(self):
        # Access flags (16 bits)
        yield NullBits(self, "reserved[]", 1)
        yield Bit(self, "enum", description='Declared as an element of an enum.')
        yield NullBits(self, "reserved[]", 1)
        yield Bit(self, "synthetic", description='Declared synthetic; not present in the source code.')
        yield NullBits(self, "reserved[]", 4)
        yield Bit(self, "transient", description='Declared transient; not written or read by a persistent object manager.')
        yield Bit(self, "volatile", description='Declared volatile; cannot be cached.')
        yield NullBits(self, "reserved[]", 1)
        yield Bit(self, "final", description='Declared final; never directly assigned to after object construction.')
        yield Bit(self, "static", description='Declared static.')
        yield Bit(self, "protected", description='Declared protected; may be accessed within subclasses.')
        yield Bit(self, "private", description='Declared private; accessible only within the defining class.')
        yield Bit(self, "public", description='Declared public; may be accessed from outside its package.')
github vstinner / hachoir / hachoir / parser / misc / pdf.py View on Github external
class PDFDictionary(FieldSet):

    def createFields(self):
        yield String(self, "dict_start", 2)
        while not self.eof:
            addr = self.absolute_address + self.current_size
            if self.stream.readBytes(addr, 2) != b'>>':
                yield from parsePDFType(self)
            else:
                break
        yield String(self, "dict_end", 2)


class PDFArray(FieldSet):
    """
    Array of possibly non-homogeneous elements, starting with '[' and ending
    with ']'
    """

    def createFields(self):
        yield String(self, "array_start", 1)
        while self.stream.readBytes(self.absolute_address + self.current_size, 1) != b']':
            yield from parsePDFType(self)
        yield String(self, "array_end", 1)


def parsePDFType(s):
    addr = s.absolute_address + s.current_size
    char = s.stream.readBytes(addr, 1)
    if char == b'/':
github vstinner / hachoir / hachoir / parser / misc / lnk.py View on Github external
if padding:
            yield padding
        yield CString(self, "final_pathname", "Final component of the pathname")

        if has_unicode_paths:
            padding = self.seekByte(self["pathname_unicode_offset"].value)
            if padding:
                yield padding
            yield CString(self, "final_pathname_unicode", "Final component of the pathname in Unicode", charset="UTF-16-LE")

        padding = self.seekByte(self["length"].value)
        if padding:
            yield padding


class LnkString(FieldSet):

    def createFields(self):
        yield UInt16(self, "length", "Length of this string")
        if self["length"].value:
            if self.root.hasUnicodeNames():
                yield String(self, "data", self["length"].value * 2, charset="UTF-16-LE")
            else:
                yield String(self, "data", self["length"].value, charset="ASCII")

    def createValue(self):
        if self["length"].value:
            return self["data"].value
        else:
            return ""
github vstinner / hachoir / hachoir / parser / misc / lnk.py View on Github external
def createFields(self):
        yield UInt16(self, "length", "Length of this string")
        if self["length"].value:
            if self.root.hasUnicodeNames():
                yield String(self, "data", self["length"].value * 2, charset="UTF-16-LE")
            else:
                yield String(self, "data", self["length"].value, charset="ASCII")

    def createValue(self):
        if self["length"].value:
            return self["data"].value
        else:
            return ""


class ColorRef(FieldSet):
    ''' COLORREF struct, 0x00bbggrr '''
    static_size = 32

    def createFields(self):
        yield UInt8(self, "red", "Red")
        yield UInt8(self, "green", "Green")
        yield UInt8(self, "blue", "Blue")
        yield PaddingBytes(self, "pad", 1, "Padding (must be 0)")

    def createDescription(self):
        rgb = self["red"].value, self["green"].value, self["blue"].value
        return "RGB Color: #%02X%02X%02X" % rgb


class ColorTableIndex(Bits):
github vstinner / hachoir / hachoir / parser / game / laf.py View on Github external
def __init__(self, chars, *args):
        FieldSet.__init__(self, *args)
        self.chars = chars
github vstinner / hachoir / hachoir / parser / video / asf.py View on Github external
yield Object(self, "object[]")


class Metadata(FieldSet):
    guid = "75B22633-668E-11CF-A6D9-00AA0062CE6C"
    names = ("title", "author", "copyright", "xxx", "yyy")

    def createFields(self):
        for index in range(5):
            yield UInt16(self, "size[]")
        for name, size in zip(self.names, self.array("size")):
            if size.value:
                yield String(self, name, size.value, charset="UTF-16-LE", strip=" \0")


class Descriptor(FieldSet):
    """
    See ExtendedContentDescription class.
    """
    TYPE_BYTE_ARRAY = 1
    TYPE_NAME = {
        0: "Unicode",
        1: "Byte array",
        2: "BOOL (32 bits)",
        3: "DWORD (32 bits)",
        4: "QWORD (64 bits)",
        5: "WORD (16 bits)"
    }

    def createFields(self):
        yield PascalString16(self, "name", "Name", charset="UTF-16-LE", strip="\0")
        yield Enum(UInt16(self, "type"), self.TYPE_NAME)
github vstinner / hachoir / hachoir / parser / program / java_serialized.py View on Github external
def createFields(self):
        yield Enum(UInt8(self, "typecode"), TYPECODE_NAMES)
        self.root.resetHandles()


class BlockDataLong(FieldSet):

    def createFields(self):
        yield Enum(UInt8(self, "typecode"), TYPECODE_NAMES)
        yield UInt32(self, "size")
        if self['size'].value:
            yield RawBytes(self, "data", self['size'].value)


class SerializedException(FieldSet):

    def createFields(self):
        yield Enum(UInt8(self, "typecode"), TYPECODE_NAMES)
        self.root.resetHandles()
        yield SerializedObject(self, "object")
        self.root.resetHandles()


class SerializedLongString(FieldSet):

    def createFields(self):
        yield Enum(UInt8(self, "typecode"), TYPECODE_NAMES)
        self.root.newHandle(self)
        yield LongString(self, "value")

    def createValue(self):