
    vd#                         d Z ddlmZ 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dZd Zd Zd ZddZdS )z
Utilities
    )print_functionFN   )struct_unpackc                    	 t          | t                    r| S t          |           }|dk    r| d         S |dk    r!| dd|z
  z  z   }t          d|          d         S | dd|z
  z  z   }t          d|          d         S #   xY w)z&Unpacks the given buffer to an integerr   r          <I   <Q)
isinstanceintlenr   )buflengthtmps      Q/var/www/html/t/fyr/venv311/lib/python3.11/site-packages/mysql/connector/utils.pyintreadr   #   s    c3 	JSQ;;q6Mq[[6**C s++A..6**C s++A..s   A> A> &A>  A> >Bc                 |    | dk     s| dk    rt          d          t          t          j        d|                     S )z^
    Takes an unsigned byte (1 byte) and packs it as a bytes-object.

    Returns string.
    r      z int1store requires 0 <= i <= 255<B
ValueError	bytearraystructpackis    r   	int1storer   5   s>     	1uuC;<<<T1--...    c                 |    | dk     s| dk    rt          d          t          t          j        d|                     S )z`
    Takes an unsigned short (2 bytes) and packs it as a bytes-object.

    Returns string.
    r     z"int2store requires 0 <= i <= 65535<Hr   r   s    r   	int2storer#   A   s>     	1uuE		=>>>T1--...r   c                     | dk     s| dk    rt          d          t          t          j        d|           dd                   S )zb
    Takes an unsigned integer (3 bytes) and packs it as a bytes-object.

    Returns string.
    r    z%int3store requires 0 <= i <= 16777215r	      r   r   s    r   	int3storer'   M   sG     	1uuH@AAAT1--ac2333r   c                 |    | dk     s| dk    rt          d          t          t          j        d|                     S )zb
    Takes an unsigned integer (4 bytes) and packs it as a bytes-object.

    Returns string.
    r       z'int4store requires 0 <= i <= 4294967295r	   r   r   s    r   	int4storer*   Y   s>     	1uuJBCCCT1--...r   c                 |    | dk     s| dk    rt          d          t          t          j        d|                     S )zZ
    Takes an unsigned integer (8 bytes) and packs it as string.

    Returns string.
    r               z!int8store requires 0 <= i <= 2^64r   r   r   s    r   	int8storer-   e   sA     	1uu(((<===T1--...r   c                     | dk     s| dk    rt          d          | dk    rt          }n1| dk    rt          }n#| dk    rt          }n| dk    rt          }nt
          } ||           S )z
    Takes an unsigned integers and packs it as a bytes-object.

    This function uses int1store, int2store, int3store,
    int4store or int8store depending on the integer value.

    returns string.
    r   r,   z!intstore requires 0 <= i <=  2^64r   r!   r%   r)   )r   r   r#   r'   r*   r-   )r   formed_strings     r   intstorer0   q   s|     	1uu(((<===Cxx!	
e!	
h!	
j!!=r   c                    | dk     s| dk    rt          d          | dk     r"t          t          j        d|                     S | dk    r%dt          t          j        d|                     z   S | d	k    r-d
t          t          j        d|           dd                   z   S dt          t          j        d|                     z   S )z|
    Takes an unsigned integer and packs it as bytes,
    with the information of how much bytes the encoded int takes.
    r   r,   zRequires 0 <= i <= 2^64   r   r!      r"   r%      r	   r&      r   r   r   s    r   lc_intr6      s    
 	1uu(((23333wwT1--...	
e6;tQ#7#78888	
h6;tQ#7#7!#<====6;tQ#7#78888r   c                 .    | d|         }| |d         |fS )zi
    Reads bytes from a buffer.

    Returns a tuple with buffer less the read bytes, and the bytes.
    r   N r   sizeress      r   
read_bytesr<      s$     af+CJr   c                 :   | d         dk    r| dd         dfS dx}}| d         }|dk    r|}| d|z   d         | d|dz            fS |dk    rd}n|dk    rd	}|d
k    rd}t          | d|dz                      }| ||z   dz   d         | |dz   ||z   dz            fS )u  
    Takes a buffer and reads a length coded string from the start.

    This is how Length coded strings work

    If the string is 250 bytes long or smaller, then it looks like this:

      <-- 1b  -->
      +----------+-------------------------
      |  length  | a string goes here
      +----------+-------------------------

    If the string is bigger than 250, then it looks like this:

      <- 1b -><- 2/3/8 ->
      +------+-----------+-------------------------
      | type |  length   | a string goes here
      +------+-----------+-------------------------

      if type == ü:
          length is code in next 2 bytes
      elif type == ý:
          length is code in next 3 bytes
      elif type == þ:
          length is code in next 8 bytes

    NULL has a special value. If the buffer starts with û then
    it's a NULL and we return None as value.

    Returns a tuple (trucated buffer, bytes).
    r   r2   r   N            r&      r
   r   )r   r   lsizefsts       r   read_lc_stringrF      s    @ 1v}}ABBFU
a&C
czzAJKK #a
l"344		
czzS519%&&F"##$c%!)FUNQ4F*F&GHHr   c                 "   g }dddd}t          |           }d}||k     r| |         }|dk    rdS |dk    r|                    d           |d	z  }n|d
k    r1|}|                    | |d	z   ||d	z   z                       |d	|z   z  }ntd}	 ||         }n# t          $ r Y dS w xY wt          | |d	z   ||d	z   z                      }|                    | |d	z   |z   ||z   |d	z   z                       |d	|z   |z   z  }||k     t	          |          S )zXReads all length encoded strings from the given buffer

    Returns a list of bytes
    r@   r&   r
   )r?   rA   rB   r   r   Nr2   r   r>   )r   appendKeyErrorr   tuple)r   byteslstsizesbuf_lenposfirstr   rD   s           r   read_lc_string_listrP      sy   
 H!!$$E#hhG
C
--CC<< 4C<<OOD!!!1HCC||S1Wfa.@$@ ABBBq6z! !%LEE      44  cAgua/@%@!ABBa%#'(BBCE E Eq5y6))1 --4 ??s   B 
B"!B"c                    ||t          d          |K	 |                     |          }n# t           $ r t          d          w xY w| |dz   d         | d|         fS |t          | |          S t          d          )zr
    Reads a string up until a character or for a given size.

    Returns a tuple (trucated buffer, string).
    Nz&read_string() needs either end or sizezend byte not present in bufferr   r   z.read_string() needs either end or size (weird))r   indexr<   )r   endr:   idxs       r   read_stringrU     s     {t|ABBB
	?))C..CC 	? 	? 	?=>>>	?C!GHHs1S5z**		#t$$$
E
F
FFs	   - Ac                 X    	 t          | d|                   }n#   xY w| |d         |fS )zMRead an integer from buffer

    Returns a tuple (truncated buffer, int)
    r   NrC   r9   s      r   read_intrW     s=    c!D&k""Js    c                    | st          d          | d         }|dk    r| dd         dfS |dk     r| dd         t          |          fS |dk    r(| dd         t          d| dd                   d         fS |d	k    r+| d
d         t          d| dd
         dz             d         fS |dk    r(| dd         t          d| dd                   d         fS t          d          )z
    Takes a buffer and reads an length code string from the start.

    Returns a tuple with buffer less the integer and the integer read.
    zEmpty buffer.r   r2   r   Nr?   r&   z<xHrA   r   r	   r   rB   	   z<xQz%Failed reading length encoded integer)r   r   r   )r   lcbytes     r   read_lc_intr[   (  s     *)))VF}}ABB	#ABBV%%	3ABBuc!A#h77:;;	3ABBtS1X-?@@CDD	3ABBuc!A#h77:;;@AAAr   c                     t          | t                    sd                    d | D                       S d                    d | D                       S )z"Debug function for showing buffers c                     g | ]}d |z  S z\x%02xr8   .0cs     r   
<listcomp>z"_digest_buffer.<locals>.<listcomp>F  s    333!	A333r   c                 2    g | ]}d t          |          z  S r_   )ordr`   s     r   rc   z"_digest_buffer.<locals>.<listcomp>G  s#    4441IA&444r   )r   strjoin)r   s    r   _digest_bufferrh   C  sV    c3 5ww33s3334447744444555r      c                     |rF|r|dk    rt          | d|                   }nt          |           }t          |dz   |z              dS t          t          |                      dS )z2Debug function printing output of _digest_buffer()r   z: N)rh   print)abufferprefixlimitdigests       r   print_bufferrp   I  sx     ' 	-UQYY#GAeG$455FF#G,,Fftmf$%%%%%nW%%&&&&&r   )NN)Nri   )__doc__
__future__r   __MYSQL_DEBUG__r   catch23r   r   r   r#   r'   r*   r-   r0   r6   r<   rF   rP   rU   rW   r[   rh   rp   r8   r   r   <module>ru      sS  0  & % % % % %  " " " " " "  $	/ 	/ 	/	/ 	/ 	/	4 	4 	4	/ 	/ 	/	/ 	/ 	/  49 9 9$  2I 2I 2Ij& & &RG G G G*  B B B66 6 6	' 	' 	' 	' 	' 	'r   