<commit_before>import ptypes
from ptypes import ptype,parray,pstruct,pint,pstr,dyn,pbinary
ptypes.setbyteorder(ptypes.config.byteorder.littleendian)

### base
class uchar(pint.uint8_t): pass
class ElfXX_Off(ptype.rpointer_t):
    _object_ = ptype.undefined
    def _baseobject_(self):
        return self.getparent(ElfXX_File)
    @classmethod
    def typename(cls):
        return cls.__name__
    def classname(self):
        try: type = self.d.classname() if self.initializedQ() else self._object_().classname()
        except: pass
        else: return "{:s}<{:s}>".format(self.typename(), type)

        try: type = self._object_.typename() if ptype.istype(self._object_) else self._object_().classname()
        except: pass
        else: return "{:s}<{:s}>".format(self.typename(), type)

        type = self._object_.__name__
        return "{:s}<{:s}>".format(self.typename(), type)

class ULEB128(pbinary.terminatedarray):
    class septet(pbinary.struct):
        _fields_ = [
            (1, 'more'),
            (7, 'value'),
        ]
    _object_ = septet
    def isTerminator(self, value):
        return not bool(value['more'])

    def int(self): return self.get()
    def get(self):
        res = 0
        for n in reversed(self):
            res = (res << 7) | n['value']
        return res
    def set(self, value):
        result = []
        while value > 0:
            result.append( self.new(self.septet).set((1, value & (2**7-1))) )
            value //= 2**7
        result[-1].set(more=0)
        self.value[:] = result[:]
        return self

    def summary(self):
        res = self.int()
        return "{:s} : {:d} : ({:#x}, {:d})".format(self.__element__(), res, res, 7*len(self))

class ElfXX_File(ptype.boundary): pass
class ElfXX_Header(ptype.boundary): pass
class ElfXX_Ehdr(ElfXX_Header): pass
class ElfXX_Phdr(ElfXX_Header): pass
class ElfXX_Shdr(ElfXX_Header): pass

### elf32
class Elf32_Addr(pint.uint32_t): pass
class Elf32_Half(pint.uint16_t): pass
class Elf32_Off(ElfXX_Off):
    _value_ = Elf32_Addr
class Elf32_Sword(pint.int32_t): pass
class Elf32_Word(pint.uint32_t): pass

### elf64
class Elf64_Addr(pint.uint64_t): pass
class Elf64_Off(ElfXX_Off):
    _value_ = Elf64_Addr
class Elf64_Half(Elf32_Half): pass
class Elf64_Word(Elf32_Word): pass
class Elf64_Sword(Elf32_Sword): pass
class Elf64_Xword(pint.uint64_t): pass
class Elf64_Sxword(pint.int64_t): pass

### elf general
EI_NIDENT = 16

class EV_(pint.enum):
    _values_ = [
        ('EV_NONE', 0),
        ('EV_CURRENT', 1),
    ]

class EI_MAG(ptype.block):
    length = 4

class EI_CLASS(pint.enum, uchar):
    _values_ = [
        ('ELFCLASSNONE', 0),
        ('ELFCLASS32', 1),
        ('ELFCLASS64', 2),
    ]

class EI_DATA(pint.enum, uchar):
    # FIXME: switch the byteorder of everything based on this value
    _values_ = [
        ('ELFDATANONE', 0),
        ('ELFDATA2LSB', 1),
        ('ELFDATA2MSB', 2),
    ]
    def order(self):
        if self['ELFDATA2LSB']:
            return ptypes.config.byteorder.littleendian
        elif self['ELFDATA2MSB']:
            return ptypes.config.byteorder.bigendian
        return ptypes.config.defaults.integer.order

class EI_VERSION(EV_, uchar):
    pass

class EI_OSABI(pint.enum, uchar):
    _values_ = [
        ('ELFOSABI_SYSV', 0),
        ('ELFOSABI_HPUX', 1),
        ('ELFOSABI_ARM_EABI', 64),
        ('ELFOSABI_STANDALONE', 255),
    ]

class EI_ABIVERSION(uchar):
    pass

class EI_PAD(ptype.block):
    length = EI_NIDENT - 9

class e_ident(pstruct.type):
    _fields_ = [
        (EI_MAG, 'EI_MAG'),
        (EI_CLASS, 'EI_CLASS'),
        (EI_DATA, 'EI_DATA'),
        (EI_VERSION, 'EI_VERSION'),
        (EI_OSABI, 'EI_OSABI'),
        (EI_ABIVERSION, 'EI_ABIVERSION'),
        (EI_PAD, 'EI_PAD'),
    ]

    def valid(self):
        return self.initializedQ() and self['EI_MAG'].serialize() == b'\x7fELF'
    def properties(self):
        res = super(e_ident, self).properties()
        if self.initializedQ():
            res['valid'] = self.valid()
        return res

class e_type(pint.enum, Elf32_Half):
    ET_LOOS, ET_HIOS = 0xfe00, 0xfeff
    ET_LOPROC, ET_HIPROC = 0xff00, 0xffff
    _values_ = [
        ('ET_NONE', 0),
        ('ET_REL', 1),
        ('ET_EXEC', 2),
        ('ET_DYN', 3),
        ('ET_CORE', 4),
    ]

class e_machine(pint.enum, Elf32_Half):
    _values_ = [
        ('ET_NONE', 0),<commit_msg>Added the ability for the elf.base library module to validate the ELF header.<commit_after>import ptypes
from ptypes import ptype,parray,pstruct,pint,pstr,dyn,pbinary
ptypes.setbyteorder(ptypes.config.byteorder.littleendian)

### base
class uchar(pint.uint8_t): pass
class ElfXX_Off(ptype.rpointer_t):
    _object_ = ptype.undefined
    def _baseobject_(self):
        return self.getparent(ElfXX_File)
    @classmethod
    def typename(cls):
        return cls.__name__
    def classname(self):
        try: type = self.d.classname() if self.initializedQ() else self._object_().classname()
        except: pass
        else: return "{:s}<{:s}>".format(self.typename(), type)

        try: type = self._object_.typename() if ptype.istype(self._object_) else self._object_().classname()
        except: pass
        else: return "{:s}<{:s}>".format(self.typename(), type)

        type = self._object_.__name__
        return "{:s}<{:s}>".format(self.typename(), type)

class ULEB128(pbinary.terminatedarray):
    class septet(pbinary.struct):
        _fields_ = [
            (1, 'more'),
            (7, 'value'),
        ]
    _object_ = septet
    def isTerminator(self, value):
        return not bool(value['more'])

    def int(self): return self.get()
    def get(self):
        res = 0
        for n in reversed(self):
            res = (res << 7) | n['value']
        return res
    def set(self, value):
        result = []
        while value > 0:
            result.append( self.new(self.septet).set((1, value & (2**7-1))) )
            value //= 2**7
        result[-1].set(more=0)
        self.value[:] = result[:]
        return self

    def summary(self):
        res = self.int()
        return "{:s} : {:d} : ({:#x}, {:d})".format(self.__element__(), res, res, 7*len(self))

class ElfXX_File(ptype.boundary): pass
class ElfXX_Header(ptype.boundary): pass
class ElfXX_Ehdr(ElfXX_Header): pass
class ElfXX_Phdr(ElfXX_Header): pass
class ElfXX_Shdr(ElfXX_Header): pass

### elf32
class Elf32_Addr(pint.uint32_t): pass
class Elf32_Half(pint.uint16_t): pass
class Elf32_Off(ElfXX_Off):
    _value_ = Elf32_Addr
class Elf32_Sword(pint.int32_t): pass
class Elf32_Word(pint.uint32_t): pass

### elf64
class Elf64_Addr(pint.uint64_t): pass
class Elf64_Off(ElfXX_Off):
    _value_ = Elf64_Addr
class Elf64_Half(Elf32_Half): pass
class Elf64_Word(Elf32_Word): pass
class Elf64_Sword(Elf32_Sword): pass
class Elf64_Xword(pint.uint64_t): pass
class Elf64_Sxword(pint.int64_t): pass

### elf general
EI_NIDENT = 16

class EV_(pint.enum):
    _values_ = [
        ('EV_NONE', 0),
        ('EV_CURRENT', 1),
    ]

class EI_MAG(ptype.block):
    length = 4

    def default(self):
        return self.set(b'\x7fELF')

    def valid(self):
        res = self.copy().default()
        return res.serialize() == self.serialize()

    def properties(self):
        res = super(EI_MAG, self).properties()
        if self.initializedQ():
            res['valid'] = self.valid()
        return res

class EI_CLASS(pint.enum, uchar):
    _values_ = [
        ('ELFCLASSNONE', 0),
        ('ELFCLASS32', 1),
        ('ELFCLASS64', 2),
    ]

class EI_DATA(pint.enum, uchar):
    # FIXME: switch the byteorder of everything based on this value
    _values_ = [
        ('ELFDATANONE', 0),
        ('ELFDATA2LSB', 1),
        ('ELFDATA2MSB', 2),
    ]
    def order(self):
        if self['ELFDATA2LSB']:
            return ptypes.config.byteorder.littleendian
        elif self['ELFDATA2MSB']:
            return ptypes.config.byteorder.bigendian
        return ptypes.config.defaults.integer.order

class EI_VERSION(EV_, uchar):
    pass

class EI_OSABI(pint.enum, uchar):
    _values_ = [
        ('ELFOSABI_SYSV', 0),
        ('ELFOSABI_HPUX', 1),
        ('ELFOSABI_ARM_EABI', 64),
        ('ELFOSABI_STANDALONE', 255),
    ]

class EI_ABIVERSION(uchar):
    pass

class EI_PAD(ptype.block):
    length = EI_NIDENT - 9

class e_ident(pstruct.type):
    _fields_ = [
        (EI_MAG, 'EI_MAG'),
        (EI_CLASS, 'EI_CLASS'),
        (EI_DATA, 'EI_DATA'),
        (EI_VERSION, 'EI_VERSION'),
        (EI_OSABI, 'EI_OSABI'),
        (EI_ABIVERSION, 'EI_ABIVERSION'),
        (EI_PAD, 'EI_PAD'),
    ]

    def valid(self):
        return self.initializedQ() and self['EI_MAG'].valid()
    def properties(self):
        res = super(e_ident, self).properties()
        if self.initializedQ():
            res['valid'] = self.valid()
        return res

class e_type(pint.enum, Elf32_Half):
    ET_LOOS, ET_HIOS = 0xfe00, 0xfeff
    ET_LOPROC, ET_HIPROC = 0xff00, 0xffff
    _values_ = [
        ('ET_NONE', 0),
        ('ET_REL', 1),
        ('ET_EXEC', 2),
        ('ET_DYN', 3),
        ('ET_CORE', 4),
    ]

class e_machine(pint.enum, Elf32_Half):
    _values_ = [
        ('ET_NONE', 0),
{'fraction_non_alphanumeric': 0.14403829416884248, 'fraction_numerical': 0.025239338555265448, 'mean_word_length': 2.75531045751634, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '297424', 'n_tokens_mistral': 3580, 'n_tokens_neox': 3417, 'n_words': 674}