B
    Ñš[É#  ã               @   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‚   ynt | tƒr| S t| ƒ}|dkr(| d S |dkrN| dd|   }td|ƒd S | dd|   }td|ƒd S W n   ‚ Y nX dS )	z&Unpacks the given buffer to an integerr   r   é   ó    z<Ié   z<QN)Ú
isinstanceÚintÚlenr   )ÚbufÚlengthÚtmp© r   ú™C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-ioykiiwb\mysql-connector-python-rf\mysql_connector_python_rf-2.2.2.data\purelib\mysql\connector\utils.pyÚintread#   s    
r   c             C   s.   | dk s| dkrt dƒ‚ntt d| ¡ƒS dS )z^
    Takes an unsigned byte (1 byte) and packs it as a bytes-object.

    Returns string.
    r   éÿ   z int1store requires 0 <= i <= 255z<BN)Ú
ValueErrorÚ	bytearrayÚstructÚpack)Úir   r   r   Ú	int1store5   s    
r   c             C   s.   | dk s| dkrt dƒ‚ntt d| ¡ƒS dS )z`
    Takes an unsigned short (2 bytes) and packs it as a bytes-object.

    Returns string.
    r   iÿÿ  z"int2store requires 0 <= i <= 65535z<HN)r   r   r   r   )r   r   r   r   Ú	int2storeA   s    
r   c             C   s6   | dk s| dkrt dƒ‚ntt d| ¡dd… ƒS dS )zb
    Takes an unsigned integer (3 bytes) and packs it as a bytes-object.

    Returns string.
    r   iÿÿÿ z%int3store requires 0 <= i <= 16777215z<Ié   N)r   r   r   r   )r   r   r   r   Ú	int3storeM   s    
r   c             C   s.   | dk s| dkrt dƒ‚ntt d| ¡ƒS dS )zb
    Takes an unsigned integer (4 bytes) and packs it as a bytes-object.

    Returns string.
    r   l   ÿÿ z'int4store requires 0 <= i <= 4294967295z<IN)r   r   r   r   )r   r   r   r   Ú	int4storeY   s    
r   c             C   s.   | dk s| dkrt dƒ‚ntt d| ¡ƒS dS )zZ
    Takes an unsigned integer (8 bytes) and packs it as string.

    Returns string.
    r   l            z!int8store requires 0 <= i <= 2^64z<QN)r   r   r   r   )r   r   r   r   Ú	int8storee   s    
r   c             C   s\   | dk s| dkrt dƒ‚| dkr&t}n.| dkr4t}n | dkrBt}n| dkrPt}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   l            z!intstore requires 0 <= i <=  2^64r   iÿÿ  iÿÿÿ l   ÿÿ )r   r   r   r   r   r   )r   Zformed_stringr   r   r   Úintstoreq   s    	r   c             C   sˆ   | dk s| dkrt dƒ‚| dk r0tt d| ¡ƒS | dkrLdtt d| ¡ƒ S | d	krpd
tt d| ¡dd… ƒ S dtt d| ¡ƒ S dS )z|
    Takes an unsigned integer and packs it as bytes,
    with the information of how much bytes the encoded int takes.
    r   l            zRequires 0 <= i <= 2^64éû   z<Biÿÿ  ó   üz<Hiÿÿÿ ó   ýz<Ir   ó   þz<QN)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    r%   c             C   s¾   | d dkr| dd… dfS d }}| d }|dkrX|}| d| d… | d|d … fS |dkrfd}n|dkrrd	}|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   ÚlsizeZfstr   r   r   Úread_lc_string§   s      r,   c             C   s  g }ddddœ}t | ƒ}d}xî||k �r
| | }|dkr<dS |dkrX| d¡ |d	7 }q|d
kr�|}| | |d	 ||d	  … ¡ |d	| 7 }qd}y|| }W n tk
r´   dS X t| |d	 ||d	  … ƒ}| | |d	 | || |d	  … ¡ |d	| | 7 }qW t|ƒS )zXReads all length encoded strings from the given buffer

    Returns a list of bytes
    r(   r   r   )r'   r)   r*   r   r   Nr   r   r&   )r
   ÚappendÚKeyErrorr   Útuple)r   ZbyteslstÚsizesZbuf_lenÚposÚfirstr   r+   r   r   r   Úread_lc_string_listÜ   s2    

"r3   c             C   s†   |dkr|dkrt dƒ‚|dk	rhy|  |¡}W n t k
rJ   t dƒ‚Y nX | |d d… | d|… fS |dk	rzt| |ƒS t dƒ‚dS )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#   Úidxr   r   r   Úread_string  s    
r7   c             C   s4   yt | d|… ƒ}W n   ‚ Y nX | |d… |fS )zMRead an integer from buffer

    Returns a tuple (truncated buffer, int)
    r   N)r   )r   r#   r$   r   r   r   Úread_int  s
    r8   c             C   sÖ   | st dƒ‚| d }|dkr,| dd… dfS |dk rH| dd… t|ƒfS |dkrr| dd… td| dd… ƒd fS |d	kr | d
d… td| dd
… d ƒd fS |dkrÊ| dd… td| dd… ƒd fS t dƒ‚dS )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   Nr'   r   z<xHr)   r   z<Ir   r*   é	   z<xQz%Failed reading length encoded integer)r   r	   r   )r   Zlcbyter   r   r   Úread_lc_int(  s    "&"r:   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>F  s    z"_digest_buffer.<locals>.<listcomp>c             S   s   g | ]}d t |ƒ ‘qS )z\x%02x)Úord)r<   r=   r   r   r   r>   G  s    )r   ÚstrÚjoin)r   r   r   r   Ú_digest_bufferC  s    
rB   é   c             C   sL   |r<|r"|dkr"t | d|… ƒ}nt | ƒ}t|d | ƒ ntt | ƒƒ dS )z2Debug function printing output of _digest_buffer()r   z: N)rB   Úprint)ZabufferÚprefixÚlimitÚdigestr   r   r   Úprint_bufferI  s    rH   )NN)NrC   )Ú__doc__Ú
__future__r   Z__MYSQL_DEBUG__r   Zcatch23r   r   r   r   r   r   r   r   r"   r%   r,   r3   r7   r8   r:   rB   rH   r   r   r   r   Ú<module>   s(   
5)
