
    @OOf"              
           d Z ddlZddlmZ ddlmZ edeeeee	e
eef	   Zdede	fdZdededdfd	Zd
ej                   dededefdZde	defdZdedefdZdede	defdZde	deee	f   fdZg dZy)aY  
tnetstring:  data serialization using typed netstrings
======================================================

This is a custom Python 3 implementation of tnetstrings.
Compared to other implementations, the main difference
is that this implementation supports a custom unicode datatype.

An ordinary tnetstring is a blob of data prefixed with its length and postfixed
with its type. Here are some examples:

    >>> tnetstring.dumps("hello world")
    11:hello world,
    >>> tnetstring.dumps(12345)
    5:12345#
    >>> tnetstring.dumps([12345, True, 0])
    19:5:12345#4:true!1:0#]

This module gives you the following functions:

    :dump:    dump an object as a tnetstring to a file
    :dumps:   dump an object as a tnetstring to a string
    :load:    load a tnetstring-encoded object from a file
    :loads:   load a tnetstring-encoded object from a string

Note that since parsing a tnetstring requires reading all the data into memory
at once, there's no efficiency gain from using the file-based versions of these
functions.  They're only here so you can use load() to read precisely one
item from a file or socket without consuming any extra data.

The tnetstrings specification explicitly states that strings are binary blobs
and forbids the use of unicode at the protocol level.
**This implementation decodes dictionary keys as surrogate-escaped ASCII**,
all other strings are returned as plain bytes.

:Copyright: (c) 2012-2013 by Ryan Kelly <ryan@rfk.id.au>.
:Copyright: (c) 2014 by Carlo Pires <carlopires@gmail.com>.
:Copyright: (c) 2016 by Maximilian Hils <tnetstring3@maximilianhils.com>.

:License: MIT
    N)BinaryIO)Unionvaluereturnc                 f    t        j                         }t        |d|        dj                  |      S )z>
    This function dumps a python object as a tnetstring.
    r       )collectionsdeque_rdumpqjoin)r   qs     X/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/mitmproxy/io/tnetstring.pydumpsr   2   s,     ',,.AAq%88A;r   file_handlec                 8    |j                  t        |              y)zb
    This function dumps a python object as a tnetstring and
    writes it to the given file.
    N)writer   )r   r   s     r   dumpr   >   s    
 eEl#r   r   sizec                 J   | j                   }| |d       |dz   S |du r |d       |dz   S |du r |d       |dz   S t        |t              r^t        |      j	                         }t        |      }t        |      j	                         } |d	||fz         |d
z   t        |      z   |z   S t        |t              r^t        |      j	                         }t        |      }t        |      j	                         } |d||fz         |d
z   t        |      z   |z   S t        |t              rZ|}t        |      }t        |      j	                         } |d        ||        |d        ||       |d
z   t        |      z   |z   S t        |t              ri|j	                  d      }t        |      }t        |      j	                         } |d        ||        |d        ||       |d
z   t        |      z   |z   S t        |t        t        f      ri |d       |dz   x}}t        |      D ]  }t        | ||      } t        ||z
        j	                         } |d        ||       |dz   t        |      z   S t        |t              r~ |d       |dz   x}}|j                         D ]  \  }	}
t        | ||
      }t        | ||	      }! t        ||z
        j	                         } |d        ||       |dz   t        |      z   S t        d| dt!        |       d      )a  
    Dump value as a tnetstring, to a deque instance, last chunks first.

    This function generates the tnetstring representation of the given value,
    pushing chunks of the output onto the given deque instance.  It pushes
    the last chunk first, then recursively generates more chunks.

    When passed in the current size of the string in the queue, it will return
    the new size of the string in the queue.

    Operating last-chunk-first makes it easy to calculate the size written
    for recursive structures without having to build their representation as
    a string.  This is measurably faster than generating the intermediate
    strings, especially on deeply nested structures.
    s   0:~   Ts   4:true!   Fs   5:false!   s   %s:%s#   s   %s:%s^   ,   :utf8   ;   ]      }zunserializable object: z ())
appendleft
isinstanceintstrencodelenfloatreprbyteslisttuplereversedr   dictitems
ValueErrortype)r   r   r   r   dataldataspan	init_sizeitemkvs              r   r   r   F   s     LLE}fax	$jax	%kax	E3	5z  "D	5z  "i4,&'ax#d)#e++	E5	!
 E{!!#D	5z  "i4,&'ax#d)#e++	E5	!D	5z  "ddddax#d)#e++	E3	||F#D	5z  "ddddax#d)#e++	ED%=	)d!8#	DUO 	*D1dD)D	*4)#$++-ddax#d)##	E4	 d!8#	DKKM 	'DAq1dA&D1dA&D	' 4)#$++-ddax#d)##25'DK=JKKr   stringc                     t        |       d   S )zA
    This function parses a tnetstring into a python object.
    r   )pop)r9   s    r   loadsr<      s     v;q>r   c                 |   | j                  d      }|dk(  rt        d      d}|j                         r@||z  }t        |      dkD  rt        d      | j                  d      }|j                         r@|dk7  rt        d      | j                  t	        |            }| j                  d      d   }t        ||      S )	zload(file) -> object

    This function reads a tnetstring from a file and parses it into a
    python object.  The file must support the read() method, and this
    function promises not to read more data than necessary.
    r   r   znot a tnetstring: empty file   z.not a tnetstring: absurdly large length prefixr   z2not a tnetstring: missing or invalid length prefixr   )readr0   isdigitr'   r$   parse)r   cdata_lengthr2   	data_types        r   loadrE      s     	ACx788K
))+q{b MNNQ	 ))+
 	DyMNNC,-D  #A&ID!!r   rD   r2   c                    | t        d      k(  r|S | t        d      k(  r|j                  d      S | t        d      k(  r	 t        |      S | t        d      k(  r	 t	        |      S | t        d      k(  r|d	k(  ry
|dk(  ryt        d|      | t        d      k(  r|rt        d|      y | t        d      k(  r(g }|r"t        |      \  }}|j                  |       |r"|S | t        d      k(  r*i }|r$t        |      \  }}t        |      \  }}|||<   |r$|S t        d|        # t        $ r t        d|      w xY w# t        $ r t        d|      w xY w)Nr   r   r      #z+not a tnetstring: invalid integer literal:    ^z)not a tnetstring: invalid float literal:    !s   trueTs   falseFz+not a tnetstring: invalid boolean literal:    ~z(not a tnetstring: invalid null literal: r   r    zunknown type tag: )orddecoder$   r0   r(   r;   append)rD   r2   lstr6   dkeyvals          r   rA   rA      s   CICI{{6""CI	Ut9 CI	S; CI7?XJ4(STTCIGxPQQCITJD$JJt  
CID	ICD	ICAcF  
))5
66?  	UJ4(STT	U
  	SHQRR	Ss   
D' 
E 'D?Ec                     	 | j                  dd      \  }} t        |      }	 | d| | |   | |dz   d }}} t	        ||       |fS # t        $ r t        d|       w xY w# t        $ r t        d|       w xY w)z
    This function parses a tnetstring into a python object.
    It returns a tuple giving the parsed object and a string
    containing any unparsed data from the end of the string.
    r   r   z4not a tnetstring: missing or invalid length prefix: Nz)not a tnetstring: invalid length prefix: )splitr$   r0   
IndexErrorrA   )r2   blengthlengthrD   remains        r   r;   r;      s    


4+W
O"&w-ftFQJL?Qi D!6))  
B4(K
 	

  O DVHMNNOs    A A AA6)r   r   rE   r<   r;   )__doc__r	   typingr   r   r%   boolr$   r(   r*   r+   r,   r.   TSerializabler   r   r
   r   r<   rE   rA   r;   __all__ r   r   <module>r^      s   (T   dCsE5$tKL	 	5 	$ $H $ $PL{   PL PLM PLc PLf% M "h "= "6'7S '7 '7- '7T*e *mU23 *0 4r   