add-determinism 0.7.3

RPM buildroot helper to strip nondeterministic bits in files
Documentation
�

��e�@��0�ddlmZmZmZddlmZmZmZddlmZm	Z	ddl
mZmZGd�de�Z
Gd�de�ZGd	�d
e�ZGd�de�ZGd
�de�ZGd�de�ZGd�de�ZGd�de�ZGd�de�ZGd�de�ZGd�de�ZGd�de�ZGd�de�ZGd�d e�ZGd!�d"e�ZGd#�d$e�ZGd%�d&e�ZGd'�d(e�ZGd)�d*e�ZGd+�d,e�Z Gd-�d.e �Z!Gd/�d0e �Z"y1)2�)�Adapter�AdaptationError�Pass)�
int_to_bin�
bin_to_int�
swap_bytes)�FlagsContainer�	HexString)�BytesIO�decodebytesc��eZdZgZy)�BitIntegerErrorN��__name__�
__module__�__qualname__�	__slots__���@/usr/lib/python3.12/site-packages/elftools/construct/adapters.pyrr
����Irrc��eZdZgZy)�MappingErrorNrrrrrrrrrc��eZdZgZy)�
ConstErrorNrrrrrrrrrc��eZdZgZy)�ValidationErrorNrrrrrrrrrc��eZdZgZy)�PaddingErrorNrrrrrrrrrc�0�eZdZdZgd�Z		dd�Zd�Zd�Zy)�BitIntegerAdaptera�
    Adapter for bit-integers (converts bitstrings to integers, and vice versa).
    See BitField.

    Parameters:
    * subcon - the subcon to adapt
    * width - the size of the subcon, in bits
    * swapped - whether to swap byte order (little endian/big endian).
      default is False (big endian)
    * signed - whether the value is signed (two's complement). the default
      is False (unsigned)
    * bytesize - number of bits per byte, used for byte-swapping (if swapped).
      default is 8.
    )�width�swapped�signed�bytesizec�h�tj||�||_||_||_||_y�N)r�__init__r"r#r$r%)�self�subconr"r#r$r%s      rr(zBitIntegerAdapter.__init__(s.������v�&���
������� ��
rc��|dkr|jstd|��t||j��}|jrt||j��}|S)N�z+object is negative, but field is not signed)r"�r%)r$rrr"r#rr%)r)�obj�context�obj2s    r�_encodezBitIntegerAdapter._encode/sN����7�4�;�;�!�"O���
��#�t�z�z�2���<�<��d�t�}�}�=�D��rc�v�|jrt||j��}t||j��S)Nr-)r$)r#rr%rr$�r)r.r/s   r�_decodezBitIntegerAdapter._decode7s*���<�<��S�T�]�]�;�C��#����4�4rN)FF��rrr�__doc__rr(r1r4rrrr!r!s#��
�;�I�@E��!��5rr!c�0�eZdZdZgd�Zeefd�Zd�Zd�Zy)�MappingAdaptera�
    Adapter that maps objects to other objects.
    See SymmetricMapping and Enum.

    Parameters:
    * subcon - the subcon to map
    * decoding - the decoding (parsing) mapping (a dict)
    * encoding - the encoding (building) mapping (a dict)
    * decdefault - the default return value when the object is not found
      in the decoding mapping. if no object is given, an exception is raised.
      if `Pass` is used, the unmapped object will be passed as-is
    * encdefault - the default return value when the object is not found
      in the encoding mapping. if no object is given, an exception is raised.
      if `Pass` is used, the unmapped object will be passed as-is
    )�encoding�decoding�
encdefault�
decdefaultc�h�tj||�||_||_||_||_yr')rr(r;r:r=r<)r)r*r;r:r=r<s      rr(zMappingAdapter.__init__Ms.������v�&� ��
� ��
�$���$��rc��	|j|S#ttf$r]|jtur&td|�d|jj�d���|jtur|cYS|jcYSwxYw)Nzno encoding mapping for � [�])	r:�KeyError�	TypeErrorr<�NotImplementedrr*�namerr3s   rr1zMappingAdapter._encodeT�r��	#��=�=��%�%���)�$�	#����.�0�"�����)�)�$+�,�,����$�&��
��?�?�"�
	#����AA=�.A=�<A=c��	|j|S#ttf$r]|jtur&td|�d|jj�d���|jtur|cYS|jcYSwxYw)Nzno decoding mapping for r@rA)	r;rBrCr=rDrr*rErr3s   rr4zMappingAdapter._decode^rFrGN)	rrrr7rrDr(r1r4rrrr9r9<s#���E�I�,�>�%�	#�	#rr9c�(�eZdZdZdgZd�Zd�Zd�Zy)�FlagsAdaptera
    Adapter for flag fields. Each flag is extracted from the number, resulting
    in a FlagsContainer object. Not intended for direct usage.
    See FlagsEnum.

    Parameters
    * subcon - the subcon to extract
    * flags - a dictionary mapping flag-names to their value
    �flagsc�>�tj||�||_yr')rr(rK)r)r*rKs   rr(zFlagsAdapter.__init__t�������v�&���
rc�t�d}|jj�D]\}}t||d�s�||z}�|S)Nr,F)rK�items�getattr)r)r.r/rKrE�values      rr1zFlagsAdapter._encodewsC�����:�:�+�+�-�	�K�D�%��s�D�%�(�����	��rc	��t�}|jj�D]\}}t||t	||z��� |Sr')r	rKrO�setattr�bool)r)r.r/r0rErQs      rr4zFlagsAdapter._decode}sD������:�:�+�+�-�	3�K�D�%��D�$��S�5�[� 1�2�	3��rNr6rrrrJrJis����	�I���rrJc�*�eZdZdZdgZdd�Zd�Zd�Zy)�
StringAdapteraA
    Adapter for strings. Converts a sequence of characters into a python
    string, and optionally handles character encoding.
    See String.

    Parameters:
    * subcon - the subcon to convert
    * encoding - the character encoding name (e.g., "utf8"), or None to
      return raw bytes (usually 8-bit ASCII).
    r:Nc�>�tj||�||_yr')rr(r:)r)r*r:s   rr(zStringAdapter.__init__��������v�&� ��
rc�T�|jr|j|j�}|Sr')r:�encoder3s   rr1zStringAdapter._encode�� ���=�=��*�*�T�]�]�+�C��
rc�T�|jr|j|j�}|Sr')r:�decoder3s   rr4zStringAdapter._decode�r[rr'r6rrrrVrV�s��	���I�!��rrVc�0�eZdZdZgd�Z		dd�Zd�Zd�Zy)�PaddedStringAdaptera�
    Adapter for padded strings.
    See String.

    Parameters:
    * subcon - the subcon to adapt
    * padchar - the padding character. default is b"\x00".
    * paddir - the direction where padding is placed ("right", "left", or
      "center"). the default is "right".
    * trimdir - the direction where trimming will take place ("right" or
      "left"). the default is "right". trimming is only meaningful for
      building, when the given string is too long.
    )�padchar�paddir�trimdirc��|dvrtd|��|dvrtd|��tj||�||_||_||_y)N)�right�left�centerz*paddir must be 'right', 'left' or 'center')rdrez!trimdir must be 'right' or 'left')�
ValueErrorrr(r`rarb)r)r*r`rarbs     rr(zPaddedStringAdapter.__init__�sZ���4�4��I���
��+�+��@�'�J�J�����v�&���������rc���|jdk(r|j|j�}|S|jdk(r|j|j�}|S|j	|j�}|S�Nrdre)ra�rstripr`�lstrip�stripr3s   rr4zPaddedStringAdapter._decode�sg���;�;�'�!��*�*�T�\�\�*�C�
�
�	�[�[�F�
"��*�*�T�\�\�*�C��
��)�)�D�L�L�)�C��
rc�d�|j|�}|jdk(r|j||j�}nH|jdk(r|j	||j�}n|j||j�}t
|�|kDr|jdk(r|d|}|S||d}|Sri)�_sizeofra�ljustr`�rjustrf�lenrb)r)r.r/�sizes    rr1zPaddedStringAdapter._encode�s����|�|�G�$���;�;�'�!��)�)�D�$�,�,�/�C�
�[�[�F�
"��)�)�D�$�,�,�/�C��*�*�T�4�<�<�0�C��s�8�d�?��|�|�w�&��%�4�j���
��4�%�&�k���
rN)�rdrd�rrrr7rr(r4r1rrrr_r_�s#���1�I�;B�"�
��
rr_c� �eZdZdZgZd�Zd�Zy)�LengthValueAdapterz�
    Adapter for length-value pairs. It extracts only the value from the
    pair, and calculates the length based on the value.
    See PrefixedArray and PascalString.

    Parameters:
    * subcon - the subcon returning a length-value pair
    c��t|�|fSr')rqr3s   rr1zLengthValueAdapter._encode�s���C��#��rc��|dS)Nrrr3s   rr4zLengthValueAdapter._decode�s���1�v�
rN)rrrr7rr1r4rrrrvrv�s����I��rrvc�*�eZdZdZdgZdd�Zd�Zd�Zy)�CStringAdapterar
    Adapter for C-style strings (strings terminated by a terminator char).

    Parameters:
    * subcon - the subcon to convert
    * terminators - a sequence of terminator chars. default is b"\x00".
    * encoding - the character encoding to use (e.g., "utf8"), or None to
      return raw-bytes. the terminator characters are not affected by the
      encoding.
    �terminatorsNc�B�tj|||��||_y)N)r:)rVr(r{)r)r*r{r:s    rr(zCStringAdapter.__init__�s�����t�V���A�&��rc�P�tj|||�|jddzS)Nr,r)rVr1r{r3s   rr1zCStringAdapter._encode�s)���$�$�T�3��8�4�;K�;K�A�a�;P�P�Prc�T�tj|dj|dd�|�S)Nr�����)rVr4�joinr3s   rr4zCStringAdapter._decode�s&���$�$�T�3�8�8�C���H�+=�w�G�Gr)rsNr6rrrrzrz�s!��	���I�'�Q�Hrrzc�(�eZdZdZdgZd�Zd�Zd�Zy)�
TunnelAdapteraC
    Adapter for tunneling (as in protocol tunneling). A tunnel is construct
    nested upon another (layering). For parsing, the lower layer first parses
    the data (note: it must return a string!), then the upper layer is called
    to parse that data (bottom-up). For building it works in a top-down manner;
    first the upper layer builds the data, then the lower layer takes it and
    writes it to the stream.

    Parameters:
    * subcon - the lower layer subcon
    * inner_subcon - the upper layer (tunneled/nested) subcon

    Example:
    # a pascal string containing compressed data (zlib encoding), so first
    # the string is read, decompressed, and finally re-parsed as an array
    # of UBInt16
    TunnelAdapter(
        PascalString("data", encoding = "zlib"),
        GreedyRange(UBInt16("elements"))
    )
    �inner_subconc�>�tj||�||_yr')rr(r�)r)r*r�s   rr(zTunnelAdapter.__init__s������v�&�(��rc�L�|jjt|�|�Sr')r��_parserr3s   rr4zTunnelAdapter._decode	s��� � �'�'����g�>�>rc�p�t�}|jj|||�|j�Sr')rr��_build�getvalue)r)r.r/�streams    rr1zTunnelAdapter._encodes.�������� � ��f�g�6���� � rNrtrrrr�r��s ���* � �I�)�?�!rr�c��eZdZdZddgZd�Zy)�ExprAdaptera>
    A generic adapter that accepts 'encoder' and 'decoder' as parameters. You
    can use ExprAdapter instead of writing a full-blown class when only a
    simple expression is needed.

    Parameters:
    * subcon - the subcon to adapt
    * encoder - a function that takes (obj, context) and returns an encoded
      version of obj
    * decoder - a function that takes (obj, context) and returns a decoded
      version of obj

    Example:
    ExprAdapter(UBInt8("foo"),
        encoder = lambda obj, ctx: obj / 4,
        decoder = lambda obj, ctx: obj * 4,
    )
    r1r4c�L�tj||�||_||_yr')rr(r1r4)r)r*�encoder�decoders    rr(zExprAdapter.__init__$s ������v�&������rN)rrrr7rr(rrrr�r�s���$�I�&�I�rr�c�*�eZdZdZdgZdd�Zd�Zd�Zy)�HexDumpAdapterzT
    Adapter for hex-dumping strings. It returns a HexString, which is a string
    �linesizec�>�tj||�||_yr')rr(r�)r)r*r�s   rr(zHexDumpAdapter.__init__.rXrc��|Sr'rr3s   rr1zHexDumpAdapter._encode1s���
rc�0�t||j��S)N)r�)r
r�r3s   rr4zHexDumpAdapter._decode3s�������7�7rN)�r6rrrr�r�)s�����I�!��8rr�c�(�eZdZdZdgZd�Zd�Zd�Zy)�ConstAdaptera,
    Adapter for enforcing a constant value ("magic numbers"). When decoding,
    the return value is checked; when building, the value is substituted in.

    Parameters:
    * subcon - the subcon to validate
    * value - the expected value

    Example:
    Const(Field("signature", 2), "MZ")
    rQc�>�tj||�||_yr')rr(rQ)r)r*rQs   rr(zConstAdapter.__init__CrMrc�r�|�||jk(r|jStd|j�d|�����Nz	expected z, found �rQrr3s   rr1zConstAdapter._encodeFs0���;�#����+��:�:����
�
�C�H�I�Irc�Z�||jk7rtd|j�d|����|Sr�r�r3s   rr4zConstAdapter._decodeKs'���$�*�*����
�
�C�H�I�I��
rNr6rrrr�r�6s ��
��	�I��J�
rr�c�,�eZdZdZgd�Zdd�Zd�Zd�Zy)�SlicingAdapterz�
    Adapter for slicing a list (getting a slice from that list)

    Parameters:
    * subcon - the subcon to slice
    * start - start index
    * stop - stop index (or None for up-to-end)
    * step - step (or None for every element)
    )�start�stop�stepNc�L�tj||�||_||_yr')rr(r�r�)r)r*r�r�s    rr(zSlicingAdapter.__init__[s ������v�&���
���	rc�D�|j�|Sdg|jz|zSr')r�r3s   rr1zSlicingAdapter._encode_s'���:�:���J��v��
�
�"�S�(�(rc�4�||j|jSr')r�r�r3s   rr4zSlicingAdapter._decodecs���4�:�:�d�i�i�(�(rr'r6rrrr�r�Ps���*�I��)�)rr�c�(�eZdZdZdgZd�Zd�Zd�Zy)�IndexingAdapterz�
    Adapter for indexing a list (getting a single item from that list)

    Parameters:
    * subcon - the subcon to index
    * index - the index of the list to get
    �indexc��tj||�t|�turt	dt|���||_y)Nzindex must be an integer)rr(�type�intrCr�)r)r*r�s   rr(zIndexingAdapter.__init__os7������v�&���;�c�!��6��U��D�D���
rc�*�dg|jz|gzSr'�r�r3s   rr1zIndexingAdapter._encodets���v��
�
�"�c�U�*�*rc� �||jSr'r�r3s   rr4zIndexingAdapter._decodevs���4�:�:��rNr6rrrr�r�fs����	�I��
+�rr�c�,�eZdZdZddgZdd�Zd�Zd�Zy)	�PaddingAdaptera+
    Adapter for padding.

    Parameters:
    * subcon - the subcon to pad
    * pattern - the padding pattern (character as byte). default is b"\x00"
    * strict - whether or not to verify, during parsing, that the given
      padding matches the padding pattern. default is False (unstrict)
    �pattern�strictc�L�tj||�||_||_yr')rr(r�r�)r)r*r�r�s    rr(zPaddingAdapter.__init__�s ������v�&������rc�>�|j|�|jzSr')rnr�r3s   rr1zPaddingAdapter._encode�s���|�|�G�$�t�|�|�3�3rc��|jr4|j|�|jz}||k7rtd|�d|����|Sr�)r�rnr�r)r)r.r/�expecteds    rr4zPaddingAdapter._decode�s<���;�;��|�|�G�,�t�|�|�;�H��h��"�h��#L�M�M��
rN)rsFr6rrrr�r�ys"����H�%�I��4�rr�c�&�eZdZdZgZd�Zd�Zd�Zy)�	Validatorz�
    Abstract class: validates a condition on the encoded/decoded object.
    Override _validate(obj, context) in deriving classes.

    Parameters:
    * subcon - the subcon to validate
    c�B�|j||�std|��|S)Nzinvalid object)�	_validaterr3s   rr4zValidator._decode�s#���~�~�c�7�+�!�"2�C�8�8��
rc�&�|j||�Sr')r4r3s   rr1zValidator._encode�s���|�|�C��)�)rc��t��r')�NotImplementedErrorr3s   rr�zValidator._validate�s��!�#�#rN)rrrr7rr4r1r�rrrr�r��s����I��*�$rr�c�"�eZdZdZdgZd�Zd�Zy)�OneOfai
    Validates that the object is one of the listed values.

    :param ``Construct`` subcon: object to validate
    :param iterable valids: a set of valid values

    >>> OneOf(UBInt8("foo"), [4,5,6,7]).parse("\x05")
    5
    >>> OneOf(UBInt8("foo"), [4,5,6,7]).parse("\x08")
    Traceback (most recent call last):
        ...
    construct.core.ValidationError: ('invalid object', 8)
    >>>
    >>> OneOf(UBInt8("foo"), [4,5,6,7]).build(5)
    '\x05'
    >>> OneOf(UBInt8("foo"), [4,5,6,7]).build(9)
    Traceback (most recent call last):
        ...
    construct.core.ValidationError: ('invalid object', 9)
    �validsc�>�tj||�||_yr')r�r(r�)r)r*r�s   rr(zOneOf.__init__�s�����4��(���rc��||jvSr')r�r3s   rr�zOneOf._validate�s���d�k�k�!�!rN�rrrr7rr(r�rrrr�r��s���(�
�I��"rr�c�"�eZdZdZdgZd�Zd�Zy)�NoneOfa�
    Validates that the object is none of the listed values.

    :param ``Construct`` subcon: object to validate
    :param iterable invalids: a set of invalid values

    >>> NoneOf(UBInt8("foo"), [4,5,6,7]).parse("\x08")
    8
    >>> NoneOf(UBInt8("foo"), [4,5,6,7]).parse("\x06")
    Traceback (most recent call last):
        ...
    construct.core.ValidationError: ('invalid object', 6)
    �invalidsc�>�tj||�||_yr')r�r(r�)r)r*r�s   rr(zNoneOf.__init__�s�����4��(� ��
rc��||jvSr')r�r3s   rr�zNoneOf._validate�s���$�-�-�'�'rNr�rrrr�r��s�����I�!�(rr�N)#�corerrr�librrrr	r
�
lib.py3compatrrrrrrrr!r9rJrVr_rvrzr�r�r�r�r�r�r�r�r�r�rrr�<module>r�s%��0�0�3�3�*�/��o���?������o���?��"5��"5�H+#�W�+#�Z�7��4�G��0/�'�/�b
��
�H�]�H�(!�G�!�B�'��28�W�8��7��4)�W�)�,�g��&�W��8$��$�$"�I�"�8(�Y�(r