HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/pymacaroons/serializers/__pycache__/binary_serializer.cpython-38.pyc
U

�%�Y01�@s�ddlmZddlZddlmZddlZddlZddlZddlm	Z	ddl
mZmZm
Z
ddlmZddlmZedd	d
g�ZGdd�de�Zd
d�Zejjdkr�dd�Zndd�Zdd�ZdS)�)�unicode_literalsN)�
namedtuple)�urlsafe_b64encode)�convert_to_bytes�convert_to_string�
raw_b64decode)�BaseSerializer)�MacaroonSerializationException�PacketV2�
field_type�datac@s�eZdZdZdZdZdZdZdZdd�Z	dd	�Z
d
d�Zdd
�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd!dd�Zdd�Zdd �ZdS)"�BinarySerializer����rcCst|�|���d��d�S)N�ascii�=)r�
serialize_raw�decode�rstrip)�self�macaroon�r�K/usr/lib/python3/dist-packages/pymacaroons/serializers/binary_serializer.py�	serializes���zBinarySerializer.serializecCs*ddlm}|j|kr |�|�S|�|�S)Nr)�MACAROON_V1)�pymacaroons.macaroonr�version�
_serialize_v1�
_serialize_v2)rrrrrrr#s

zBinarySerializer.serialize_rawcCs�|�d|j�}||�d|j�7}|jD]F}||�d|j�7}|jr&|jr&||�d|j�7}||�d|j�7}q&||�dt�|j	��7}|S)N�location�
identifier�cid�vid�cl�	signature)
�
_packetize�location�
identifier�caveatsZ
_caveat_idZ_verification_key_idZ	_location�binascii�	unhexlify�signature_bytes)rrZcombinedZcaveatrrrr)s
�
�zBinarySerializer._serialize_v1cCs�ddlm}t�}|�|�|jdk	rN|�||jt|j��|�||j|j	�|�||j
�|jD]d}|jdk	r�|�||jt|j��|�||j|j�|j
dk	r�|�||jt|j
��|�||j
�qb|�||j
�|�||jt�|j��t|�S)Nr��MACAROON_V2)rr/�	bytearray�appendr(�_append_packet�	_LOCATIONr�_IDENTIFIERZidentifier_bytes�_EOSr*Zcaveat_id_bytes�verification_key_id�_VID�
_SIGNATUREr+r,r-�bytes)rrr/r�crrrr ;s6

�
�


�
��zBinarySerializer._serialize_v2cCs.t|�dkrtd��t|�}t|�}|�|�S)Nrzempty macaroon)�len�
ValueErrorrr�deserialize_raw)r�
serialized�decodedrrr�deserializeUs
zBinarySerializer.deserializecCsZddlm}ddlm}t�|dd��}||kr<|�|�St|�rN|�|�S|d��dS)Nrr.�� MacaroonDeserializationExceptionrz7cannot determine data format of binary-encoded macaroon)	rr/�pymacaroons.exceptionsrB�sixZbyte2int�_deserialize_v2�
_is_ascii_hex�_deserialize_v1)rr>r/rB�firstrrrr=\s

�z BinarySerializer.deserialize_rawcCs.ddlm}m}ddlm}ddlm}||d�}d}|t|�k�r*tt	�
d||||j��dd�}|||j||�}	|�|	�\}
}|
dkr�||_
n�|
d	kr�||_nt|
d
kr�|j�|||d��nV|
dkr�||jd
_n@|
dkr�||jd
_
n*|
dk�rt�|�|_n|dj|
|d���||}q6|S)Nr)�Macaroonr��CaveatrA�rs4s�r!r"r#)�	caveat_idrr$���r%r&z7Key {key} not valid key for this format. Value: {value})�key�value)rrIr�pymacaroons.caveatrKrCrBr;�int�structZunpack�PACKET_PREFIX_LENGTH�_depacketizer(r)r*r1r6r+�hexlify�	signature�format)rr?rIrrKrBr�indexZ
packet_length�packetrPrQrrrrGhsT
����
�

��
z BinarySerializer._deserialize_v1cCsddlm}m}ddlm}ddlm}|dd�}|�|�\}}d}t|�dkr~|dj	|j
kr~|dj�d�}|dd�}t|�dks�|dj	|j
kr�|d��||dj||d	�}|�|�\}	}|	}t|�dkrؐq�||d
�}
t|�dk�r |dj	|j
k�r |dj�d�|
_|dd�}t|�dk�s@|dj	|j
k�rH|d��|dj|
_|dd�}t|�dk�r�|
jdk	�r�|d��|j�|
�q�t|�dk�r�|d
��|dj	|jk�r�|d��|dj|
_|j�|
�q�|�|�\}}|j	|jk�r�|d��t�|j�|_|S)Nr)rIr/rJrAr�zutf-8zinvalid macaroon header)r)r(rrLzno identifier in caveatz*location not allowed in first party caveatzextra fields found in caveatzinvalid field found in caveatz+unexpected field found instead of signature)rrIr/rRrKrCrB�_parse_section_v2r;rr3rrr4r(rNr*r1r7r6�_parse_packet_v2r8r+rWrX)rr>rIr/rKrBZsectionZlocr�restZcavr[rrrrE�sl�
  �����z BinarySerializer._deserialize_v2cCs�|jdt|�t|�}t|�dd�}|dkrFtdj|||d���|�d��d�}|dt|�d}t�	td	t|��||�}|S)
Nri��ztPacket too long for serialization. Max length is 0xFFFF (65535). Packet length: 0x{hex_length} ({length}) Key: {key})rPZ
hex_lengthZlengthrr� �
z4s%ds)
rUr;�hexr	rY�zfill�encoderrTZpack)rrPrZpacket_sizeZpacket_size_hex�headerZpacket_contentr[rrrr'�s$���zBinarySerializer._packetizecCs*|�d�d}|t|�dd�}||fS)Nr`rrrO)�splitr;)rr[rPrQrrrrV�szBinarySerializer._depacketizeNcCs0t||�||jkr,t|t|��|�|�dS)N)�_encode_uvarintr5r;�extend)rrrZpacket_datarrrr2�s

zBinarySerializer._append_packetcCsvddlm}d}g}t|�dkr(|d��|�|�\}}|j|jkrJ||fS|j|kr\|d��|�|�|j}|}qdS)z� Parses a sequence of packets in data.

        The sequence is terminated by a packet with a field type of EOS
        :param data bytes to be deserialized.
        :return: the rest of data and an array of packet V2
        rrArOz"section extends past end of bufferzfields out of orderN)rCrBr;r^rr5r1)rrrBZprev_field_typeZpacketsr_r[rrrr]�s�

z"BinarySerializer._parse_section_v2cCs�ddlm}t|�\}}||d�}||jkr<|t|d�fSt|�\}}||d�}|t|�krh|d��||d�t||d|��fS)aQ Parses a V2 data packet at the start of the given data.

        The format of a packet is as follows:

        field_type(varint) payload_len(varint) data[payload_len bytes]

        apart from EOS which has no payload_en or data (it's a single zero
        byte).

        :param data:
        :return: rest of data, PacketV2
        rrANz%field data extends past end of buffer)rCrB�_decode_uvarintr5r
r;)rrrBZft�nZpayload_lenrrrr^s

�z!BinarySerializer._parse_packet_v2)N)�__name__�
__module__�__qualname__rUr3r4r7r8r5rrrr r@r=rGrEr'rVr2r]r^rrrrr
s&0;
r
cCsH|dkrtd��|d@}|dL}|dkr4|�|�qD|�|dB�qdS)z7 Encodes integer into variable-length format into data.rzonly support positive integer���N)r<r1)rrjZ	this_byterrrrg-s
rgrcCsfd}d}d}|D]H}t|�}|d7}|dkr@|||>B|fS||d@|>O}|d7}qtd��dS�z� Decode a variable -length integer.

        Reads a sequence of unsigned integer byte and decodes them into an
        integer in variable-length format and returns it and the length read.
        rrrprnrozcannot read uvarint from bufferN)�ord�	Exception�rrj�shift�i�brrrri;s
ricCs^d}d}d}|D]@}|d7}|dkr8|||>B|fS||d@|>O}|d7}qtd��dSrq)rsrtrrrriMs
cCsLtd�|krtd�kr$nndStd�|kr@td�krHnndSdS)N�0�9T�a�fF)rr)rwrrrrF_s
  rF)Z
__future__rr+�collectionsrrDrT�sys�base64rZpymacaroons.utilsrrrZ'pymacaroons.serializers.base_serializerrrCr	r
r
rg�version_info�majorrirFrrrr�<module>s$