HEX
Server: LiteSpeed
System: Linux br-asc-web1845.main-hosting.eu 5.14.0-611.42.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Mar 24 05:30:20 EDT 2026 x86_64
User: u790421558 (790421558)
PHP: 8.2.30
Disabled: system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //lib/python3.9/site-packages/elftools/construct/__pycache__/core.cpython-39.pyc
a

�#�_��@sxddlmZddlmZmZmZddlmZm	Z	m
Z
Gdd�de�ZGdd�de�Z
Gd	d
�d
e�ZGdd�de�ZGd
d�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Zdd�Zdd �ZGd!d"�d"e�ZGd#d$�d$e�ZGd%d&�d&e�ZGd'd(�d(e�ZGd)d*�d*e�ZGd+d,�d,e�Z Gd-d.�d.e�ZGd/d0�d0e�Z!Gd1d2�d2e�Z"Gd3d4�d4e�Z#Gd5d6�d6e�Z$Gd7d8�d8e�Z%Gd9d:�d:e�Z&Gd;d<�d<e�Z'Gd=d>�d>e�Z(Gd?d@�d@e�Z)GdAdB�dBe�Z*GdCdD�dDe�Z+GdEdF�dFe�Z,GdGdH�dHe�Z-GdIdJ�dJe�Z.e.dK�Z.GdLdM�dMe�Z/e/dK�Z/dKS)N�)�Struct�)�BytesIO�advance_iterator�bchr)�	Container�
ListContainer�
LazyContainerc@seZdZgZdS)�ConstructErrorN��__name__�
__module__�__qualname__�	__slots__�rr�;/usr/lib/python3.9/site-packages/elftools/construct/core.pyr

sr
c@seZdZgZdS)�
FieldErrorNrrrrrrsrc@seZdZgZdS)�SizeofErrorNrrrrrrsrc@seZdZgZdS)�AdaptationErrorNrrrrrrsrc@seZdZgZdS)�
ArrayErrorNrrrrrrsrc@seZdZgZdS)�
RangeErrorNrrrrrrsrc@seZdZgZdS)�SwitchErrorNrrrrrrsrc@seZdZgZdS)�SelectErrorNrrrrrrsrc@seZdZgZdS)�TerminatorErrorNrrrrrrsrc@s�eZdZdZdZdZdZdZddgZd,d	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd-d(d)�Zd*d+�Zd'S).�	Constructax
    The mother of all constructs.

    This object is generally not directly instantiated, and it does not
    directly implement parsing and building, so it is largely only of interest
    to subclass implementors.

    The external user API:

     * parse()
     * parse_stream()
     * build()
     * build_stream()
     * sizeof()

    Subclass authors should not override the external methods. Instead,
    another API is available:

     * _parse()
     * _build()
     * _sizeof()

    There is also a flag API:

     * _set_flag()
     * _clear_flag()
     * _inherit_flags()
     * _is_flag()

    And stateful copying:

     * __getstate__()
     * __setstate__()

    Attributes and Inheritance
    ==========================

    All constructs have a name and flags. The name is used for naming struct
    members and context dictionaries. Note that the name can either be a
    string, or None if the name is not needed. A single underscore ("_") is a
    reserved name, and so are names starting with a less-than character ("<").
    The name should be descriptive, short, and valid as a Python identifier,
    although these rules are not enforced.

    The flags specify additional behavioral information about this construct.
    Flags are used by enclosing constructs to determine a proper course of
    action. Flags are inherited by default, from inner subconstructs to outer
    constructs. The enclosing construct may set new flags or clear existing
    ones, as necessary.

    For example, if FLAG_COPY_CONTEXT is set, repeaters will pass a copy of
    the context for each iteration, which is necessary for OnDemand parsing.
    r����name�conflagsrcCsJ|dur:t|�turtd|��|dks0|�d�r:td|��||_||_dS)Nzname must be a string or None�_�<z
reserved name)�type�str�	TypeError�
startswith�
ValueErrorrr)�selfr�flagsrrr�__init__]s

zConstruct.__init__cCsd|jj|jfS)Nz%s(%r))�	__class__rr�r'rrr�__repr__fszConstruct.__repr__cCs|j|O_dS)zv
        Set the given flag or flags.

        :param int flag: flag to set; may be OR'd combination of flags
        N�r�r'�flagrrr�	_set_flagiszConstruct._set_flagcCs|j|M_dS)zz
        Clear the given flag or flags.

        :param int flag: flag to clear; may be OR'd combination of flags
        Nr-r.rrr�_clear_flagrszConstruct._clear_flagcGs|D]}|�|j�qdS)z0
        Pull flags from subconstructs.
        N)r0r)r'�subcons�scrrr�_inherit_flags{szConstruct._inherit_flagscCst|j|@�S)z\
        Check whether a given flag is set.

        :param int flag: flag to check
        )�boolrr.rrr�_is_flag�szConstruct._is_flagcCspi}t|d�r|�|j�g}|j}|durJt|d�rB|�|j�|j}q$|D]}t||�rNt||�||<qN|S)zJ
        Obtain a dictionary representing this construct's state.
        �__dict__Nr)�hasattr�updater7r*�extendr�__base__�getattr)r'�attrs�slots�crrrr�__getstate__�s


zConstruct.__getstate__cCs"|��D]\}}t|||�qdS)z>
        Set this construct's state to a given state.
        N)�items�setattr)r'r=r�valuerrr�__setstate__�szConstruct.__setstate__cCst�|j�}|�|���|S)z returns a copy of this construct)�object�__new__r*rDr@)r'Zself2rrr�__copy__�szConstruct.__copy__cCs|�t|��S)z�
        Parse an in-memory buffer.

        Strings, buffers, memoryviews, and other complete buffers can be
        parsed with this method.
        )�parse_streamr)r'�datarrr�parse�szConstruct.parsecCs|�|t��S)z�
        Parse a stream.

        Files, pipes, sockets, and other streaming sources of data are handled
        by this method.
        )�_parser)r'�streamrrrrH�szConstruct.parse_streamcCs
t��dS��/
        Override me in your subclass.
        N��NotImplementedError�r'rL�contextrrrrK�szConstruct._parsecCst�}|�||�|��S)z,
        Build an object in memory.
        )r�build_stream�getvalue�r'�objrLrrr�build�szConstruct.buildcCs|�||t��dS)z9
        Build an object directly into a stream.
        N)�_buildrrUrrrrS�szConstruct.build_streamcCs
t��dSrMrO�r'rVrLrRrrrrX�szConstruct._buildNc
CsL|durt�}z|�|�WStyF}zt|��WYd}~n
d}~00dS)a�
        Calculate the size of this object, optionally using a context.

        Some constructs have no fixed size and can only know their size for a
        given hunk of data; these constructs will raise an error if they are
        not passed a context.

        :param ``Container`` context: contextual data

        :returns: int of the length of this construct
        :raises SizeofError: the size could not be determined
        N)r�_sizeof�	Exceptionr)r'rR�errr�sizeof�szConstruct.sizeofcCstd��dS)rNzRaw Constructs have no size!N�r�r'rRrrrrZ�szConstruct._sizeof)r)N)rr
r�__doc__�FLAG_COPY_CONTEXT�FLAG_DYNAMIC�
FLAG_EMBEDZFLAG_NESTINGrr)r,r0r1r4r6r@rDrGrJrHrKrWrSrXr]rZrrrrr s.6
				


rc@s6eZdZdZdgZdd�Zdd�Zdd�Zd	d
�ZdS)�Subconstructz�
    Abstract subconstruct (wraps an inner construct, inheriting its
    name and flags).

    Parameters:
    * subcon - the construct to wrap
    �subconcCst�||j|j�||_dS�N)rr)rrre)r'rerrrr)szSubconstruct.__init__cCs|j�||�Srf)rerKrQrrrrKszSubconstruct._parsecCs|j�|||�dSrf)rerXrYrrrrXszSubconstruct._buildcCs|j�|�Srf)rerZr_rrrrZszSubconstruct._sizeofN�	rr
rr`rr)rKrXrZrrrrrd�srdc@s4eZdZdZgZdd�Zdd�Zdd�Zdd	�Zd
S)�Adapterz�
    Abstract adapter: calls _decode for parsing and _encode for building.

    Parameters:
    * subcon - the construct to wrap
    cCs|�|j�||�|�Srf)�_decodererKrQrrrrKszAdapter._parsecCs|j�|�||�||�dSrf)rerX�_encoderYrrrrXszAdapter._buildcCs
t��dSrfrO�r'rVrRrrrriszAdapter._decodecCs
t��dSrfrOrkrrrrjszAdapter._encodeN)	rr
rr`rrKrXrirjrrrrrhsrhcCs@|dkrtd|��|�|�}t|�|kr<td|t|�f��|S�Nrzlength must be >= 0�expected %d, found %d)r&�read�lenr�rL�lengthrIrrr�_read_stream s

rrcCs@|dkrtd|��t|�|kr2td|t|�f��|�|�dSrl)r&ror�writerprrr�
_write_stream(s

rtc@s6eZdZdZdgZdd�Zdd�Zdd�Zd	d
�ZdS)�StaticFieldzw
    A fixed-size byte field.

    :param str name: field name
    :param int length: number of bytes in the field
    rqcCst�||�||_dSrf)rr)rq)r'rrqrrrr)8szStaticField.__init__cCst||j�Srf)rrrqrQrrrrK;szStaticField._parsecCst||j|�dSrf)rtrqrYrrrrX=szStaticField._buildcCs|jSrf)rqr_rrrrZ?szStaticField._sizeofNrgrrrrru/sruc@s>eZdZdZdgZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
S)�FormatFielda4
    A field that uses ``struct`` to pack and unpack data.

    See ``struct`` documentation for instructions on crafting format strings.

    :param str name: name of the field
    :param str endianness: format endianness string; one of "<", ">", or "="
    :param str format: a single format character
    �packercCsJ|dvrtd|��t|�dkr&td��t||�|_t�|||jj�dS)N)�>r!�=z%endianity must be be '=', '<', or '>'rz)must specify one and only one format char)r&ro�Packerrwrur)�size)r'rZ	endianity�formatrrrr)Ns�zFormatField.__init__cCst�|�}|dj|d<|S�Nrw)rur@r|�r'r=rrrr@Vs
zFormatField.__getstate__cCst|d�|d<t�||�Sr})rzrurDr~rrrrDZszFormatField.__setstate__c
CsLz|j�t||j��dWStyF}zt|��WYd}~n
d}~00dS�Nr)rw�unpackrrrqr[r)r'rLrR�exrrrrK]szFormatField._parsec
CsLzt||j|j�|��Wn,tyF}zt|��WYd}~n
d}~00dSrf)rtrqrw�packr[r)r'rVrLrRr�rrrrXbszFormatField._buildN)
rr
rr`rr)r@rDrKrXrrrrrvBs
rvc@s6eZdZdZdgZdd�Zdd�Zdd�Zd	d
�ZdS)�	MetaFielda
    A variable-length field. The length is obtained at runtime from a
    function.

    :param str name: name of the field
    :param callable lengthfunc: callable that takes a context and returns
                                length as an int

    >>> foo = Struct("foo",
    ...     Byte("length"),
    ...     MetaField("data", lambda ctx: ctx["length"])
    ... )
    >>> foo.parse("\x03ABC")
    Container(data = 'ABC', length = 3)
    >>> foo.parse("\x04ABCD")
    Container(data = 'ABCD', length = 4)
    �
lengthfunccCs"t�||�||_|�|j�dSrf)rr)r�r0rb)r'rr�rrrr)|szMetaField.__init__cCst||�|��Srf)rrr�rQrrrrK�szMetaField._parsecCst||�|�|�dSrf)rtr�rYrrrrX�szMetaField._buildcCs
|�|�Srf)r�r_rrrrZ�szMetaField._sizeofNrgrrrrr�hsr�c@s6eZdZdZdgZdd�Zdd�Zdd�Zd	d
�ZdS)�	MetaArraya�
    An array (repeater) of a meta-count. The array will iterate exactly
    `countfunc()` times. Will raise ArrayError if less elements are found.
    See also Array, Range and RepeatUntil.

    Parameters:
    * countfunc - a function that takes the context as a parameter and returns
      the number of elements of the array (count)
    * subcon - the subcon to repeat `countfunc()` times

    Example:
    MetaArray(lambda ctx: 5, UBInt8("foo"))
    �	countfunccCs.t�||�||_|�|j�|�|j�dSrf)rdr)r�r1rar0rb)r'r�rerrrr)�szMetaArray.__init__c
Cs�t�}d}|�|�}zd|jj|j@rP||krv|�|j�||����|d7}q$n&||krv|�|j�||��|d7}qPWn6ty�}zt	d||f|��WYd}~n
d}~00|S)Nrrrm)
rr�rerra�appendrKrGr
r)r'rLrRrVr?�countr�rrrrK�s
(zMetaArray._parsecCsv|�|�}t|�|kr*td|t|�f��|jj|j@rX|D]}|j�|||���q<n|D]}|j�|||�q\dS)Nrm)r�rorrerrarXrG)r'rVrLrRr��subobjrrrrX�s
zMetaArray._buildcCs|j�|�|�|�Srf)rerZr�r_rrrrZ�szMetaArray._sizeofNrgrrrrr��s

r�c@s8eZdZdZddgZdd�Zdd�Zdd	�Zd
d�ZdS)
�Rangea�
    A range-array. The subcon will iterate between `mincount` to `maxcount`
    times. If less than `mincount` elements are found, raises RangeError.
    See also GreedyRange and OptionalGreedyRange.

    The general-case repeater. Repeats the given unit for at least mincount
    times, and up to maxcount times. If an exception occurs (EOF, validation
    error), the repeater exits. If less than mincount units have been
    successfully parsed, a RangeError is raised.

    .. note::
       This object requires a seekable stream for parsing.

    :param int mincount: the minimal count
    :param int maxcount: the maximal count
    :param Construct subcon: the subcon to repeat

    >>> c = Range(3, 7, UBInt8("foo"))
    >>> c.parse("\x01\x02")
    Traceback (most recent call last):
      ...
    construct.core.RangeError: expected 3..7, found 2
    >>> c.parse("\x01\x02\x03")
    [1, 2, 3]
    >>> c.parse("\x01\x02\x03\x04\x05\x06")
    [1, 2, 3, 4, 5, 6]
    >>> c.parse("\x01\x02\x03\x04\x05\x06\x07")
    [1, 2, 3, 4, 5, 6, 7]
    >>> c.parse("\x01\x02\x03\x04\x05\x06\x07\x08\x09")
    [1, 2, 3, 4, 5, 6, 7]
    >>> c.build([1,2])
    Traceback (most recent call last):
      ...
    construct.core.RangeError: expected 3..7, found 2
    >>> c.build([1,2,3,4])
    '\x01\x02\x03\x04'
    >>> c.build([1,2,3,4,5,6,7,8])
    Traceback (most recent call last):
      ...
    construct.core.RangeError: expected 3..7, found 8
    �mincount�maxcoutcCs4t�||�||_||_|�|j�|�|j�dSrf)rdr)r�r�r1rar0rb)r'r�r�rerrrr)�s
zRange.__init__c
Cs�t�}d}zx|jj|j@rP||jkr�|��}|�|j�||����|d7}qn0||jkr�|��}|�|j�||��|d7}qPWnPt	y�}z8||j
kr�td|j
|j|f|��|�|�WYd}~n
d}~00|S)Nrr�expected %d to %d, found %d)
rrerrar��tellr�rKrGr
r�r�seek)r'rLrRrVr?�posr�rrrrK�s*


�� zRange._parsec
Cst|�|jkst|�|jkr6td|j|jt|�f��d}z�|jj|j@r�|D]2}t|t�rdt	|�}|j�
|||���|d7}qNn4|D].}t|t�r�t	|�}|j�
|||�|d7}q�WnLt�y}z2||jkr�td|j|jt|�f|��WYd}~n
d}~00dS)Nr�rr)
ror�r�rrerra�
isinstance�bytesrrXrGr
)r'rVrLrRZcntr�r�rrrrXs0�


��zRange._buildcCstd��dS�Nzcan't calculate sizer^r_rrrrZsz
Range._sizeofNrgrrrrr��s*r�c@s6eZdZdZdgZdd�Zdd�Zdd�Zd	d
�ZdS)�RepeatUntila�
    An array that repeats until the predicate indicates it to stop. Note that
    the last element (which caused the repeat to exit) is included in the
    return value.

    Parameters:
    * predicate - a predicate function that takes (obj, context) and returns
      True if the stop-condition is met, or False to continue.
    * subcon - the subcon to repeat.

    Example:
    # will read chars until b (inclusive)
    RepeatUntil(lambda obj, ctx: obj == b"",
        Field("chars", 1)
    )
    �	predicatecCs.t�||�||_|�|j�|�|j�dSrf)rdr)r�r1rar0rb)r'r�rerrrr)-szRepeatUntil.__init__c
Cs�g}zh|jj|j@rB|j�||���}|�|�|�||�rqjqn(|j�||�}|�|�|�||�rBqjqBWn.ty�}ztd|��WYd}~n
d}~00|S)N�missing terminator)	rerrarKrGr�r�r
r)r'rLrRrVr�r�rrrrK2s

 zRepeatUntil._parsecCs�d}|jj|j@rF|D],}|j�|||���|�||�rd}q�qn:|D]4}t|�}|j�|||���|�||�rJd}q�qJ|s�td��dS)NFTr�)rerrarXrGr�rr)r'rVrLrRZ
terminatedr�rrrrXDszRepeatUntil._buildcCstd��dSr�r^r_rrrrZUszRepeatUntil._sizeofNrgrrrrr�sr�c@s8eZdZdZddgZdd�Zdd�Zdd	�Zd
d�ZdS)
ra�
    A sequence of named constructs, similar to structs in C. The elements are
    parsed and built in the order they are defined.
    See also Embedded.

    Parameters:
    * name - the name of the structure
    * subcons - a sequence of subconstructs that make up this structure.
    * nested - a keyword-only argument that indicates whether this struct
      creates a nested context. The default is True. This parameter is
      considered "advanced usage", and may be removed in the future.

    Example:
    Struct("foo",
        UBInt8("first_element"),
        UBInt16("second_element"),
        Padding(2),
        UBInt8("third_element"),
    )
    r2�nestedcOsH|�dd�|_|rtd|��t�||�||_|j|�|�|j�dS)Nr�Tz.the only keyword argument accepted is 'nested')	�popr�r$rr)r2r4r1rc)r'rr2�kwrrrr)rs

zStruct.__init__cCs�d|vr|d}|d=nt�}|jr.t|d�}|jD]P}|j|j@rZ||d<|�||�q4|�||�}|jdur4|||j<|||j<q4|S�Nz<obj>�r )rr�r2rrcrKr�r'rLrRrVr3r�rrrrKzs



z
Struct._parsecCs~d|vr|d=n|jr t|d�}|jD]R}|j|j@rDd|d<|}n&|jdurTd}nt||j�}|||j<|�|||�q&dS�Nz
<unnested>r�T)r�rr2rrcrr<rX)r'rVrLrRr3r�rrrrX�s



z
Struct._buildcs(|jrt�d��t�fdd�|jD��S)Nr�c3s|]}|���VqdSrf�rZ��.0r3�rRrr�	<genexpr>��z!Struct._sizeof.<locals>.<genexpr>)r�r�sumr2r_rr�rrZ�s
zStruct._sizeofNrgrrrrr\src@s$eZdZdZgZdd�Zdd�ZdS)�Sequencea
    A sequence of unnamed constructs. The elements are parsed and built in the
    order they are defined.
    See also Embedded.

    Parameters:
    * name - the name of the structure
    * subcons - a sequence of subconstructs that make up this structure.
    * nested - a keyword-only argument that indicates whether this struct
      creates a nested context. The default is True. This parameter is
      considered "advanced usage", and may be removed in the future.

    Example:
    Sequence("foo",
        UBInt8("first_element"),
        UBInt16("second_element"),
        Padding(2),
        UBInt8("third_element"),
    )
    cCs�d|vr|d}|d=nt�}|jr.t|d�}|jD]P}|j|j@rZ||d<|�||�q4|�||�}|jdur4|�|�|||j<q4|Sr�)	rr�rr2rrcrKrr�r�rrrrK�s



zSequence._parsecCs�d|vr|d=n|jr t|d�}t|�}|jD]N}|j|j@rLd|d<|}n"|jdur\d}nt|�}|||j<|�|||�q.dSr�)	r�r�iterr2rrcrrrX)r'rVrLrRZobjiterr3r�rrrrX�s



zSequence._buildN)rr
rr`rrKrXrrrrr��sr�c@s8eZdZdZddgZdd�Zdd�Zdd	�Zd
d�ZdS)
�Uniona�
    a set of overlapping fields (like unions in C). when parsing,
    all fields read the same data; when building, only the first subcon
    (called "master") is used.

    Parameters:
    * name - the name of the union
    * master - the master subcon, i.e., the subcon used for building and
      calculating the total size
    * subcons - additional subcons

    Example:
    Union("what_are_four_bytes",
        UBInt32("one_dword"),
        Struct("two_words", UBInt16("first"), UBInt16("second")),
        Struct("four_bytes",
            UBInt8("a"),
            UBInt8("b"),
            UBInt8("c"),
            UBInt8("d")
        ),
    )
    �parser�buildercs^t�||�dd�|D�}|�td�fdd���t|t�dd�g|�R�|_t|��|_dS)NcSsg|]}t|��qSr)�Peekr�rrr�
<listcomp>�r�z"Union.__init__.<locals>.<listcomp>cs
��|�Srfr�)�ctx��masterrr�<lambda>�r�z Union.__init__.<locals>.<lambda>T)�
perform_build)rr)r�r�rr�r�r�)r'rr�r2r��argsrr�rr)�s
zUnion.__init__cCs|j�||�Srf)r�rKrQrrrrK�szUnion._parsecCs|j�|||�Srf)r�rXrYrrrrX�szUnion._buildcCs|j�|�Srf)r�rZr_rrrrZ�sz
Union._sizeofNrgrrrrr��sr�c@sVeZdZdZGdd�de�Zed�Zgd�Zedfdd�Zd	d
�Zdd�Z	d
d�Z
dS)�Switchah
    A conditional branch. Switch will choose the case to follow based on
    the return value of keyfunc. If no case is matched, and no default value
    is given, SwitchError will be raised.
    See also Pass.

    Parameters:
    * name - the name of the construct
    * keyfunc - a function that takes the context and returns a key, which
      will ne used to choose the relevant case.
    * cases - a dictionary mapping keys to constructs. the keys can be any
      values that may be returned by keyfunc.
    * default - a default value to use when the key is not found in the cases.
      if not supplied, an exception will be raised when the key is not found.
      You can use the builtin construct Pass for 'do-nothing'.
    * include_key - whether or not to include the key in the return value
      of parsing. defualt is False.

    Example:
    Struct("foo",
        UBInt8("type"),
        Switch("value", lambda ctx: ctx.type, {
                1 : UBInt8("spam"),
                2 : UBInt16("spam"),
                3 : UBInt32("spam"),
                4 : UBInt64("spam"),
            }
        ),
    )
    c@s$eZdZdd�Zdd�Zdd�ZdS)zSwitch.NoDefaultcCstd��dS�Nzno default case defined�rrQrrrrK#szSwitch.NoDefault._parsecCstd��dSr�r�rYrrrrX%szSwitch.NoDefault._buildcCstd��dSr�r�r_rrrrZ'szSwitch.NoDefault._sizeofN)rr
rrKrXrZrrrr�	NoDefault"sr�zNo default value specified)r2�keyfunc�cases�default�include_keyFcCsPt�||�|j|���||_||_||_||_|j|���|�|j	�dSrf)
rr)r4�valuesr�r�r�r�r0rb)r'rr�r�r�r�rrrr)-szSwitch.__init__cCs8|�|�}|j�||j��||�}|jr0||fS|SdSrf)r�r��getr�rKr�)r'rLrR�keyrVrrrrK7s

z
Switch._parsecCs<|jr|\}}n
|�|�}|j�||j�}|�|||�dSrf)r�r�r�r�r�rX)r'rVrLrRr��caserrrrX>s


z
Switch._buildcCs |j�|�|�|j�}|�|�Srf)r�r�r�r�rZ)r'rRr�rrrrZEszSwitch._sizeofN)rr
rr`rr�rr)rKrXrZrrrrr�s�

r�c@s8eZdZdZddgZdd�Zdd�Zdd	�Zd
d�ZdS)
�SelectaL
    Selects the first matching subconstruct. It will literally try each of
    the subconstructs, until one matches.

    Notes:
    * requires a seekable stream.

    Parameters:
    * name - the name of the construct
    * subcons - the subcons to try (order-sensitive)
    * include_name - a keyword only argument, indicating whether to include
      the name of the selected subcon in the return value of parsing. default
      is false.

    Example:
    Select("foo",
        UBInt64("large"),
        UBInt32("medium"),
        UBInt16("small"),
        UBInt8("tiny"),
    )
    r2�include_namecOsL|�dd�}|rtd|��t�||�||_||_|j|�|�|j�dS)Nr�Fz4the only keyword argument accepted is 'include_name')	r�r$rr)r2r�r4r0rb)r'rr2r�r�rrrr)as�
zSelect.__init__c	Cs||jD]h}|��}|��}z|�||�}WntyF|�|�Yq0|�|�|jrf|j|fS|Sqt	d��dS�Nzno subconstruct matched)
r2r�rGrKr
r��
__update__r�rr)r'rLrRr3r��context2rVrrrrKks


z
Select._parsec	Cs�|jr:|\}}|jD]"}|j|kr|�|||�dSqn^|jD]V}t�}|��}z|�|||�WntyvYq@0|�|�|�|�	��dSq@t
d|��dSr�)r�r2rrXrrGr[r�rsrTr)r'rVrLrRrr3�stream2r�rrrrXzs"




z
Select._buildcCstd��dSr�r^r_rrrrZ�szSelect._sizeofNrgrrrrr�Is
r�c@s6eZdZdZdgZdd�Zdd�Zdd�Zd	d
�ZdS)�Pointeraj
    Changes the stream position to a given offset, where the construction
    should take place, and restores the stream position when finished.
    See also Anchor, OnDemand and OnDemandPointer.

    Notes:
    * requires a seekable stream.

    Parameters:
    * offsetfunc: a function that takes the context and returns an absolute
      stream position, where the construction would take place
    * subcon - the subcon to use at `offsetfunc()`

    Example:
    Struct("foo",
        UBInt32("spam_pointer"),
        Pointer(lambda ctx: ctx.spam_pointer,
            Array(5, UBInt8("spam"))
        )
    )
    �
offsetfunccCst�||�||_dSrf)rdr)r�)r'r�rerrrr)�szPointer.__init__cCs8|�|�}|��}|�|�|j�||�}|�|�|Srf)r�r�r�rerK)r'rLrR�newpos�origposrVrrrrK�s


zPointer._parsecCs:|�|�}|��}|�|�|j�|||�|�|�dSrf)r�r�r�rerX)r'rVrLrRr�r�rrrrX�s


zPointer._buildcCsdSrrr_rrrrZ�szPointer._sizeofNrgrrrrr��sr�c@s8eZdZdZdgZd
dd�Zdd�Zdd	�Zd
d�ZdS)r�a�
    Peeks at the stream: parses without changing the stream position.
    See also Union. If the end of the stream is reached when peeking,
    returns None.

    Notes:
    * requires a seekable stream.

    Parameters:
    * subcon - the subcon to peek at
    * perform_build - whether or not to perform building. by default this
      parameter is set to False, meaning building is a no-op.

    Example:
    Peek(UBInt8("foo"))
    r�FcCst�||�||_dSrf)rdr)r�)r'rer�rrrr)�sz
Peek.__init__c	CsX|��}z>z|j�||�WW|�|�Sty8Yn0W|�|�n|�|�0dSrf)r�rerKr�r)r'rLrRr�rrrrK�s
�zPeek._parsecCs|jr|j�|||�dSrf)r�rerXrYrrrrX�szPeek._buildcCsdSrrr_rrrrZ�szPeek._sizeofN)Frgrrrrr��s
r�c@s2eZdZdZddgZddd�Zdd�Zd	d
�ZdS)
�OnDemanda�
    Allows for on-demand (lazy) parsing. When parsing, it will return a
    LazyContainer that represents a pointer to the data, but does not actually
    parses it from stream until it's "demanded".
    By accessing the 'value' property of LazyContainers, you will demand the
    data from the stream. The data will be parsed and cached for later use.
    You can use the 'has_value' property to know whether the data has already
    been demanded.
    See also OnDemandPointer.

    Notes:
    * requires a seekable stream.

    Parameters:
    * subcon -
    * advance_stream - whether or not to advance the stream position. by
      default this is True, but if subcon is a pointer, this should be False.
    * force_build - whether or not to force build. If set to False, and the
      LazyContainer has not been demaned, building is a no-op.

    Example:
    OnDemand(Array(10000, UBInt8("foo"))
    �advance_stream�force_buildTcCst�||�||_||_dSrf)rdr)r�r�)r'rer�r�rrrr)�szOnDemand.__init__cCs2t|j||��|�}|jr.|�|j�|�d�|S�Nr)r	rer�r�r�rZ)r'rLrRrVrrrrK�szOnDemand._parsecCsZt|t�s|j�|||�n:|js(|jr<|j�|j||�n|jrV|�|j�	|�d�dSr�)
r�r	rerXr�Z	has_valuerCr�r�rZrYrrrrXs
zOnDemand._buildN)TT)rr
rr`rr)rKrXrrrrr��s

r�c@s8eZdZdZgd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�Buffereda
    Creates an in-memory buffered stream, which can undergo encoding and
    decoding prior to being passed on to the subconstruct.
    See also Bitwise.

    Note:
    * Do not use pointers inside Buffered

    Parameters:
    * subcon - the subcon which will operate on the buffer
    * encoder - a function that takes a string and returns an encoded
      string (used after building)
    * decoder - a function that takes a string and returns a decoded
      string (used before parsing)
    * resizer - a function that takes the size of the subcon and "adjusts"
      or "resizes" it according to the encoding/decoding process.

    Example:
    Buffered(BitField("foo", 16),
        encoder = decode_bin,
        decoder = encode_bin,
        resizer = lambda size: size / 8,
    )
    )�encoder�decoder�resizercCs"t�||�||_||_||_dSrf)rdr)r�r�r�)r'rer�r�r�rrrr)&szBuffered.__init__cCs,t||�|��}t|�|��}|j�||�Srf)rrrZrr�rerK)r'rLrRrIr�rrrrK+szBuffered._parsecCsT|�|�}t�}|j�|||�|�|���}t|�|ks>J�t||�|�|�dSrf)rZrrerXr�rTrort)r'rVrLrRr{r�rIrrrrX/s
zBuffered._buildcCs|�|j�|��Srf�r�rerZr_rrrrZ6szBuffered._sizeofNrgrrrrr�sr�c@s8eZdZdZgd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�Restreama"
    Wraps the stream with a read-wrapper (for parsing) or a
    write-wrapper (for building). The stream wrapper can buffer the data
    internally, reading it from- or writing it to the underlying stream
    as needed. For example, BitStreamReader reads whole bytes from the
    underlying stream, but returns them as individual bits.
    See also Bitwise.

    When the parsing or building is done, the stream's close method
    will be invoked. It can perform any finalization needed for the stream
    wrapper, but it must not close the underlying stream.

    Note:
    * Do not use pointers inside Restream

    Parameters:
    * subcon - the subcon
    * stream_reader - the read-wrapper
    * stream_writer - the write wrapper
    * resizer - a function that takes the size of the subcon and "adjusts"
      or "resizes" it according to the encoding/decoding process.

    Example:
    Restream(BitField("foo", 16),
        stream_reader = BitStreamReader,
        stream_writer = BitStreamWriter,
        resizer = lambda size: size / 8,
    )
    )�
stream_reader�
stream_writerr�cCs"t�||�||_||_||_dSrf)rdr)r�r�r�)r'rer�r�r�rrrr)XszRestream.__init__cCs$|�|�}|j�||�}|��|Srf)r�rerK�close)r'rLrRr�rVrrrrK]s
zRestream._parsecCs&|�|�}|j�|||�|��dSrf)r�rerXr�)r'rVrLrRr�rrrrXbs
zRestream._buildcCs|�|j�|��Srfr�r_rrrrZfszRestream._sizeofNrgrrrrr�9sr�c@seZdZdZgZddd�ZdS)�Reconfigaj
    Reconfigures a subconstruct. Reconfig can be used to change the name and
    set and clear flags of the inner subcon.

    Parameters:
    * name - the new name
    * subcon - the subcon to reconfigure
    * setflags - the flags to set (default is 0)
    * clearflags - the flags to clear (default is 0)

    Example:
    Reconfig("foo", UBInt8("bar"))
    rcCs.t�|||j�||_|�|�|�|�dSrf)rr)rrer0r1)r'rreZsetflagsZ
clearflagsrrrr)|s
zReconfig.__init__N)rr)rr
rr`rr)rrrrr�ms
r�c@s,eZdZdZgZdd�Zdd�Zdd�ZdS)	�AnchoraM
    Returns the "anchor" (stream position) at the point where it's inserted.
    Useful for adjusting relative offsets to absolute positions, or to measure
    sizes of constructs.
    absolute pointer = anchor + relative offset
    size = anchor_after - anchor_before
    See also Pointer.

    Notes:
    * requires a seekable stream.

    Parameters:
    * name - the name of the anchor

    Example:
    Struct("foo",
        Anchor("base"),
        UBInt8("relative_offset"),
        Pointer(lambda ctx: ctx.relative_offset + ctx.base,
            UBInt8("data")
        )
    )
    cCs|��Srf)r�rQrrrrK�sz
Anchor._parsecCs|��||j<dSrf)r�rrYrrrrX�sz
Anchor._buildcCsdSrrr_rrrrZ�szAnchor._sizeofN�rr
rr`rrKrXrZrrrrr��s
r�c@s6eZdZdZdgZdd�Zdd�Zdd�Zd	d
�ZdS)�Valuea8
    A computed value.

    Parameters:
    * name - the name of the value
    * func - a function that takes the context and return the computed value

    Example:
    Struct("foo",
        UBInt8("width"),
        UBInt8("height"),
        Value("total_pixels", lambda ctx: ctx.width * ctx.height),
    )
    �funccCs"t�||�||_|�|j�dSrf)rr)r�r0rb)r'rr�rrrr)�szValue.__init__cCs
|�|�Srf)r�rQrrrrK�szValue._parsecCs|�|�||j<dSrf)r�rrYrrrrX�szValue._buildcCsdSrrr_rrrrZ�sz
Value._sizeofNrgrrrrr��sr�c@s8eZdZdZddgZdd�Zdd�Zdd	�Zd
d�ZdS)
�	LazyBounda
    Lazily bound construct, useful for constructs that need to make cyclic
    references (linked-lists, expression trees, etc.).

    Parameters:


    Example:
    foo = Struct("foo",
        UBInt8("bar"),
        LazyBound("next", lambda: foo),
    )
    �bindfunc�boundcCst�||�d|_||_dSrf)rr)r�r�)r'rr�rrrr)�szLazyBound.__init__cCs"|jdur|��|_|j�||�Srf)r�r�rKrQrrrrK�s

zLazyBound._parsecCs(|jdur|��|_|j�|||�dSrf)r�r�rXrYrrrrX�s

zLazyBound._buildcCs |jdur|��|_|j�|�Srf)r�r�rZr_rrrrZ�s

zLazyBound._sizeofNrgrrrrr��s
r�c@s4eZdZdZgZdd�Zdd�Zdd�Zdd	�Zd
S)�Passa
    A do-nothing construct, useful as the default case for Switch, or
    to indicate Enums.
    See also Switch and Enum.

    Notes:
    * this construct is a singleton. do not try to instatiate it, as it
      will not work...

    Example:
    Pass
    cCsdSrfrrQrrrrKszPass._parsecCs|dusJ�dSrfrrYrrrrXszPass._buildcCsdSrrr_rrrrZszPass._sizeofcCs|jjSrf)r*rr+rrr�
__reduce__szPass.__reduce__N)	rr
rr`rrKrXrZr�rrrrr�sr�Nc@s,eZdZdZgZdd�Zdd�Zdd�ZdS)	�
Terminatorao
    Asserts the end of the stream has been reached at the point it's placed.
    You can use this to ensure no more unparsed data follows.

    Notes:
    * this construct is only meaningful for parsing. for building, it's
      a no-op.
    * this construct is a singleton. do not try to instatiate it, as it
      will not work...

    Example:
    Terminator
    cCs|�d�rtd��dS)Nrzexpected end of stream)rnrrQrrrrK's
zTerminator._parsecCs|dusJ�dSrfrrYrrrrX*szTerminator._buildcCsdSrrr_rrrrZ,szTerminator._sizeofNr�rrrrr�s

r�)0�structrrzZ
lib.py3compatrrr�librrr	r[r
rrrrrrrrrErrdrhrrrtrurvr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr�<module>sRY&#1_AD9)GL*#*-4 >