o
    –¨ÊhÉ$  ã                   @   s°   d Z ddlmZ dZddlZddlmZ dd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd(dd„Zdd „ Zd!d"„ Zd#d$„ Zd)d&d'„ZdS )*z
Utilities
é    )Úprint_functionFNé   )Ústruct_unpackc                 C   s|   z9t | tƒr	| W S t| ƒ}|dkr| d W S |dkr*| dd|   }td|ƒd W S | dd|   }td|ƒd W S    ‚ )z&Unpacks the given buffer to an integerr   r   é   ó    ú<Ié   ú<Q)Ú
isinstanceÚintÚlenr   )ÚbufÚlengthÚtmp© r   úG/var/www/html/env/lib/python3.10/site-packages/mysql/connector/utils.pyÚintread(   s   

r   c                 C   ó(   | dk s| dkrt dƒ‚tt 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©Úir   r   r   Ú	int1store9   ó   r   c                 C   r   )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   r   r   r   Ú	int2storeE   r   r!   c                 C   s0   | dk s| dkrt dƒ‚tt 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   r   r   r   Ú	int3storeQ   s   r$   c                 C   r   )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   r   r   r   Ú	int4store]   r   r&   c                 C   r   )zZ
    Takes an unsigned integer (8 bytes) and packs it as string.

    Returns string.
    r   ì            z!int8store requires 0 <= i <= 2^64r	   r   r   r   r   r   Ú	int8storei   r   r(   c                 C   st   | dk s| dkrt dƒ‚| dkrt}|| ƒS | dkr t}|| ƒS | dkr*t}|| ƒS | dkr4t}|| ƒS 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_stringr   r   r   Úintstoreu   s    	
÷ùûþr*   c                 C   s„   | dk s| dkrt dƒ‚| dk rtt d| ¡ƒS | dkr&dtt d| ¡ƒ S | d	kr8d
tt d| ¡dd… ƒ S dtt d| ¡ƒ 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   r   r   r   Úlc_int�   s   r/   c                 C   s   | d|… }| |d… |fS )zi
    Reads bytes from a buffer.

    Returns a tuple with buffer less the read bytes, and the bytes.
    r   Nr   ©r   ÚsizeÚresr   r   r   Ú
read_bytes¡   s   r3   c                 C   s¾   | d dkr| dd… dfS d }}| d }|dkr,|}| d| d… | d|d … fS |dkr3d}n|dkr9d	}|d
kr?d}t | d|d … ƒ}| || d d… | |d || d … 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   r+   r   Néú   éü   é   éý   r#   éþ   r   ©r   )r   r   ÚlsizeÚfstr   r   r   Úread_lc_string«   s     ,r<   c                 C   s  g }ddddœ}t | ƒ}d}||k r†| | }|dkrdS |dkr*| d¡ |d	7 }nX|d
krF|}| | |d	 ||d	  … ¡ |d	| 7 }n<d}z|| }W n
 tyX   Y dS w t| |d	 ||d	  … ƒ}| | |d	 | || |d	  … ¡ |d	| | 7 }||k st|ƒS )zXReads all length encoded strings from the given buffer

    Returns a list of bytes
    r6   r#   r   )r5   r7   r8   r   r   Nr+   r   r4   )r   ÚappendÚKeyErrorr   Útuple)r   ÚbyteslstÚsizesÚbuf_lenÚposÚfirstr   r:   r   r   r   Úread_lc_string_listà   s8   

ÿÿèrE   c                 C   s|   |du r|du rt dƒ‚|dur1z|  |¡}W n t y"   t dƒ‚w | |d d… | d|… fS |dur: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   Úindexr3   )r   Úendr1   Úidxr   r   r   Úread_string	  s   ÿ
rI   c                 C   s.   z
t | d|… ƒ}W n   ‚ | |d… |fS )zMRead an integer from buffer

    Returns a tuple (truncated buffer, int)
    r   Nr9   r0   r   r   r   Úread_int  s
   rJ   c                 C   sÒ   | st dƒ‚| d }|dkr| dd… dfS |dk r$| dd… t|ƒfS |dkr9| dd… td| dd… ƒd fS |d	krP| d
d… td| dd
… d ƒd fS |dkre| 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   r+   r   Nr5   r#   z<xHr7   r   r   r   r8   é	   z<xQz%Failed reading length encoded integer)r   r   r   )r   Úlcbyter   r   r   Úread_lc_int,  s   "&"rM   c                 C   s2   t | tƒsd dd„ | D ƒ¡S d dd„ | D ƒ¡S )z"Debug function for showing buffersÚ c                 S   s   g | ]}d | ‘qS ©z\x%02xr   ©Ú.0Úcr   r   r   Ú
<listcomp>J  s    z"_digest_buffer.<locals>.<listcomp>c                 S   s   g | ]}d t |ƒ ‘qS rO   )ÚordrP   r   r   r   rS   K  s    )r
   ÚstrÚjoin)r   r   r   r   Ú_digest_bufferG  s   
rW   é   c                 C   sN   |r|r|dkrt | d|… ƒ}nt | ƒ}t|d | ƒ dS tt | ƒƒ dS )z2Debug function printing output of _digest_buffer()r   z: N)rW   Úprint)ÚabufferÚprefixÚlimitÚdigestr   r   r   Úprint_bufferM  s   r^   )NN)NrX   )Ú__doc__Ú
__future__r   Ú__MYSQL_DEBUG__r   Úcatch23r   r   r   r!   r$   r&   r(   r*   r/   r3   r<   rE   rI   rJ   rM   rW   r^   r   r   r   r   Ú<module>   s*   
5
)