
    AҐi(                     `    d Z ddlZddlmZ dZdZdZdZdZdZ	d	Z
d
ZdZdZdZdZ G d d      Zy)a  
TLV (Type-Length-Value) Binary Encoder/Decoder

Provides efficient binary serialization for dirty worker protocol messages.
Inspired by OpenBSD msgctl/msgsnd message format.

Type Codes:
    0x00: None (no value bytes)
    0x01: bool (1 byte: 0x00 or 0x01)
    0x05: int64 (8 bytes big-endian signed)
    0x06: float64 (8 bytes IEEE 754)
    0x10: bytes (4-byte length + raw bytes)
    0x11: string (4-byte length + UTF-8 encoded)
    0x20: list (4-byte count + encoded elements)
    0x21: dict (4-byte count + encoded key-value pairs)
    N   )DirtyProtocolError                !   i   i   c                   \    e Zd ZdZedefd       Zed	dededefd       Z	edefd       Z
y)

TLVEncoderz
    TLV binary encoder/decoder.

    Encodes Python values to binary TLV format and decodes back.
    Supports: None, bool, int, float, bytes, str, list, dict.
    returnc                    | t        t        g      S t        | t              rt        t        | rdg      S dg      S t        | t
              r't        t        g      t        j                  d|       z   S t        | t              r't        t        g      t        j                  d|       z   S t        | t               rdt        |       t        kD  rt        dt        |        dt         d      t        t        g      t        j                  dt        |             z   | z   S t        | t              ru| j!                  d	      }t        |      t"        kD  rt        d
t        |       dt"         d      t        t$        g      t        j                  dt        |            z   |z   S t        | t&        t(        f      rt        |       t*        kD  rt        dt        |        dt*         d      t        t,        g      t        j                  dt        |             g}| D ]&  }|j/                  t0        j!                  |             ( dj3                  |      S t        | t4              rt        |       t6        kD  rt        dt        |        dt6         d      t        t8        g      t        j                  dt        |             g}| j;                         D ]h  \  }}t        |t              st        |      }|j/                  t0        j!                  |             |j/                  t0        j!                  |             j dj3                  |      S t        dt=        |       j>                         )aJ  
        Encode a Python value to TLV binary format.

        Args:
            value: Python value to encode (None, bool, int, float,
                   bytes, str, list, or dict)

        Returns:
            bytes: TLV-encoded binary data

        Raises:
            DirtyProtocolError: If value type is not supported
        r   r   >q>dBytes too large:  bytes (max: )>Iutf-8String too large: List too large:  items (max:     Dict too large: z#Unsupported type for TLV encoding: ) bytes	TYPE_NONE
isinstancebool	TYPE_BOOLint
TYPE_INT64structpackfloatTYPE_FLOAT64lenMAX_BYTES_SIZEr   
TYPE_BYTESstrencodeMAX_STRING_SIZETYPE_STRINGlisttupleMAX_LIST_SIZE	TYPE_LISTappendr   joindictMAX_DICT_SIZE	TYPE_DICTitemstype__name__)valueencodedpartsitemkvs         K/var/www/descvideos/venv/lib/python3.12/site-packages/gunicorn/dirty/tlv.pyr*   zTLVEncoder.encode4   s    =)%%eT")UT=>>=>>eS!*&T5)AAAeU#,(6;;tU+CCCeU#5zN*('E
| 4+,A/  *&T3u:)FFNNeS!ll7+G7|o-((W 7,-Q0  +'&++dCL*IIGSSedE]+5zM)(&s5zl 3*O1.  I;'T3u:)FGE 6Z..t45688E?"eT"5zM)(&s5zl 3*O1.  I;'T3u:)FGE 31!!S)AAZ..q12Z..q123 88E?" 1$u+2F2F1GH
 	
r   dataoffsetc                 ,	   |t        |       k\  rt        d| ||dz          | |   }|dz  }|t        k(  rd|fS |t        k(  r3|t        |       k\  rt        d| |dz
  |dz          | |   dk7  }||dz   fS |t        k(  rM|dz   t        |       kD  rt        d	| |dz
  |dz          t        j                  d
| ||dz          d   }||dz   fS |t        k(  rM|dz   t        |       kD  rt        d| |dz
  |dz          t        j                  d| ||dz          d   }||dz   fS |t        k(  r|dz   t        |       kD  rt        d| |dz
  |dz          t        j                  d| ||dz          d   }|dz  }|t        kD  rt        d| dt         d      ||z   t        |       kD  r(t        d| dt        |       |z
   | |dz
  |dz          | |||z    }|||z   fS |t        k(  r|dz   t        |       kD  rt        d| |dz
  |dz          t        j                  d| ||dz          d   }|dz  }|t        kD  rt        d| dt         d      ||z   t        |       kD  r(t        d| dt        |       |z
   | |dz
  |dz          	 | |||z    j                  d      }|||z   fS |t        k(  r|dz   t        |       kD  rt        d| |dz
  |dz          t        j                  d| ||dz          d   }|dz  }|t         kD  rt        d| dt          d      g }t#        |      D ],  }t$        j                  | |      \  }	}|j'                  |	       . ||fS |t(        k(  r|dz   t        |       kD  rt        d| |dz
  |dz          t        j                  d| ||dz          d   }|dz  }|t*        kD  rt        d| dt*         d      i }
t#        |      D ]j  }t$        j                  | |      \  }}t-        |t.              s!t        d t1        |      j2                         t$        j                  | |      \  }}||
|<   l |
|fS t        d!|d"| |dz
  |dz          # t        $ r%}t        d| | ||t        |d      z          d}~ww xY w)#a9  
        Decode a TLV-encoded value from binary data.

        Args:
            data: Binary data to decode
            offset: Starting offset in the data

        Returns:
            tuple: (decoded_value, new_offset)

        Raises:
            DirtyProtocolError: If data is malformed or truncated
        z Truncated TLV data: no type byte   raw_datar   Nz&Truncated TLV data: missing bool valuer      z$Truncated TLV data: incomplete int64r   z&Truncated TLV data: incomplete float64r      z+Truncated TLV data: incomplete bytes lengthr   r   r   r   zTruncated TLV data: expected z bytes, got r   z,Truncated TLV data: incomplete string lengthr   z bytes for string, got r   zInvalid UTF-8 in string: z)Truncated TLV data: incomplete list countr   r   z)Truncated TLV data: incomplete dict countr   zDict key must be string, got zUnknown TLV type code: 0x02x)r&   r   r   r   r!   r"   unpackr%   r(   r'   r,   r+   decodeUnicodeDecodeErrorminr0   r/   ranger   r1   r5   r4   r   r)   r7   r8   )r@   rA   	type_coder9   lengthecountr6   _r<   resultkeys               r?   rJ   zTLVEncoder.decode   s    SY$2fVb[1 
 L	!	!<	!T"(<!&1*Vb[9  LD(E&1*$$
"zCI%(:!&1*Vb[9  MM$VFQJ(?@CE&1*$$$zCI%(<!&1*Vb[9  MM$VFQJ(?@CE&1*$$
"zCI%(A!&1*Vb[9  ]]4fVaZ)@A!DFaKF&('x}^<LAN  T*(3F8 <t9v-.0!&1*Vb[9 
 0E&6/))#zCI%(B!&1*Vb[9  ]]4fVaZ)@A!DFaKF'((o=NaP  T*(3F8 <t9v-.0!&1*Vb[9 
VFVO4;;GD &6/))	!zCI%(?!&1*Vb[9  MM$VFQJ(?@CEaKF}$(&ug]=/K  E5\ #)00v>fT"# &= 	!zCI%(?!&1*Vb[9  MM$VFQJ(?@CEaKF}$(&ug]=/K  F5\ $(//f=V!#s+,7S	8J8J7KL  !+ 1 1$ ?v#s$ 6>! '	#7&1*Vb[1
 	
i & (/s3!&#fb/)AB s   	Q% %	R. RRc                     t         j                  | d      \  }}|t        |       k7  r#t        dt        |       |z
   d| ||dz          |S )a  
        Decode a complete TLV-encoded value, ensuring all data is consumed.

        Args:
            data: Binary data to decode

        Returns:
            Decoded Python value

        Raises:
            DirtyProtocolError: If data is malformed or has trailing bytes
        r   zTrailing data after TLV: z bytesrC   rD   )r   rJ   r&   r   )r@   r9   rA   s      r?   decode_fullzTLVEncoder.decode_full  s^     #))$2vSY$+CI,>+?vFfVb[1  r   N)r   )r8   
__module____qualname____doc__staticmethodr   r*   r    r.   rJ   rV    r   r?   r   r   ,   sp     H
 H
 H
T Y
U Y
C Y
 Y
 Y
v %  r   r   )rY   r"   errorsr   r   r   r!   r%   r(   r,   r0   r5   r+   r'   r/   r4   r   r[   r   r?   <module>r]      s^   
"  & 		

		 #!C Cr   