
    Dh#                         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y)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      U/var/www/html/civilisation/venv/lib/python3.12/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" "A%c                 j    | dk  s| dkD  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   s3     	1uC;<<T1-..    c                 j    | dk  s| dkD  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   s3     	1uE	=>>T1-..r   c                 p    | dk  s| dkD  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   s:     	1uH@AAT1-a233r   c                 j    | dk  s| dkD  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   s3     	1uJBCCT1-..r   c                 j    | dk  s| dkD  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   s5     	1u((<==T1-..r   c                     | dk  s| dkD  rt        d      | dk  rt        }n*| 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_     	1u((<==Cx!	
e!	
h!	
j!!r   c                 Z   | dk  s| dkD  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    
 	1u((2333wT1-..	
e6;;tQ#7888	
h6;;tQ#7!#<===6;;tQ#7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+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}ABFU
a&C
czAJK #a
"344		
czS519%&F"#$c%!)FUNQ4F&GHHr   c                    g }dddd}t        |       }d}||k  r| |   }|dk(  ry|dk(  r|j                  d       |d	z  }nz|d
k  r(|}|j                  | |d	z   ||d	z   z           |d	|z   z  }nMd}	 ||   }t        | |d	z   ||d	z   z          }|j                  | |d	z   |z   ||z   |d	z   z           |d	|z   |z   z  }||k  rt	        |      S # t        $ r Y yw xY w)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      s0   
 H!!$E#hG
C
-CC< C<OOD!1HC|S1Wfa.@ ABq6z! !%LE !cAgua/@!ABa%#'(BCEq5y6))1 -4 ?     s   2C 	CCc                     ||t        d      |	 | j                  |      }| |dz   d | d| fS |t        | |      S t        d      # t         $ r t        d      w xY w)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
	?))C.C C!GHs1Sz**		#t$$
E
FF  	?=>>	?s   A
 
Ac                 :    	 t        | d|       }| |d |fS #   xY w)zMRead an integer from buffer

    Returns a tuple (truncated buffer, int)
    r   NrC   r9   s      r   read_intrW     s4    c!Dk" 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}AB	#ABV%%	3ABuc!Ah7:;;	3ABtS1X-?@CDD	3ABuc!Ah7:;;@AAr   c           	          t        | t              s"dj                  | D cg c]  }d|z  	 c}      S dj                  | D cg c]  }dt        |      z   c}      S c c}w c c}w )z"Debug function for showing buffers z\x%02x)r   strjoinord)r   cs     r   _digest_bufferrb   C  sU    c3wws3!	A3447741IA&455 44s   AA#c                     |r3|r|dkD  rt        | d|       }nt        |       }t        |dz   |z          yt        t        |              y)z2Debug function printing output of _digest_buffer()r   z: N)rb   print)abufferprefixlimitdigests       r   print_bufferri   I  sG    UQY#GAe$45F#G,Fftmf$%nW%&r   )NN)N   )__doc__
__future__r   __MYSQL_DEBUG__r   catch23r   r   r   r#   r'   r*   r-   r0   r6   r<   rF   rP   rU   rW   r[   rb   ri   r8   r   r   <module>ro      so   0 &  "$	/	/	4	/	/49$2Ij&RG*B66	'r   