
    A                     d   d Z dj                         Z	 ddlmc mZ dZddlZddlZ G d d      Zdd	Zd
 Zd Zd Zd Zd Zd Zej*                  ej,                  gej.                  ej0                  gej2                  ej4                  gej6                  ej8                  gej:                  ej<                  gdZi Z dj                         D ]9  Z!dddddjE                   ejF                  e!      d      Z$e$-e$e vs2de!z  e e$<   ; e d   e d<   [![$d Z%d Z&dZ'y# e$ r ddl	m
Z dZY w xY w)ay  crcmod is a Python module for gererating objects that compute the Cyclic
Redundancy Check.  Any 8, 16, 24, 32, or 64 bit polynomial can be used.  

The following are the public components of this module.

Crc -- a class that creates instances providing the same interface as the
algorithms in the hashlib module in the Python standard library.  These
instances also provide a method for generating a C/C++ function to compute
the CRC.

mkCrcFun -- create a Python function to compute the CRC using the specified
polynomial and initial value.  This provides a much simpler interface if
all you need is a function for CRC calculation.
zmkCrcFun Crc
    NT)	_crcfunpyFc                   F    e Zd ZdZddZd ZddZd Zd Zd Z	d	 Z
dd
Zy)Crca  Compute a Cyclic Redundancy Check (CRC) using the specified polynomial.

    Instances of this class have the same interface as the algorithms in the
    hashlib module in the Python standard library.  See the documentation of
    this module for examples of how to use a Crc instance.

    The string representation of a Crc instance identifies the polynomial,
    initial value, XOR out value, and the current CRC value.  The print
    statement can be used to output this information.

    If you need to generate a C/C++ function for use in another application,
    use the generateCode method.  If you need to generate code for another
    language, subclass Crc and override the generateCode method.

    The following are the parameters supplied to the constructor.

    poly -- The generator polynomial to use in calculating the CRC.  The value
    is specified as a Python integer.  The bits in this integer are the
    coefficients of the polynomial.  The only polynomials allowed are those
    that generate 8, 16, 24, 32, or 64 bit CRCs.

    initCrc -- Initial value used to start the CRC calculation.  This initial
    value should be the initial shift register value XORed with the final XOR
    value.  That is equivalent to the CRC result the algorithm should return for
    a zero-length string.  Defaults to all bits set because that starting value
    will take leading zero bytes into account.  Starting with zero will ignore
    all leading zero bytes.

    rev -- A flag that selects a bit reversed algorithm when True.  Defaults to
    True because the bit reversed algorithms are more efficient.

    xorOut -- Final value to XOR with the calculated CRC value.  Used by some
    CRC algorithms.  Defaults to zero.
    c                     |sy t        |||      \  }}}|dz  | _        || _        || _        || _        || _        t        |||||      \  }}|| _        || _        | j                  | _	        y )N   )
_verifyParamsdigest_sizeinitCrcxorOutpolyreverse	_mkCrcFun_crctablecrcValue)	selfr   r
   revr   
initializesizeBitscrcfunr   s	            /lib/third_party/gcloud_crcmod/python3/crcmod.py__init__zCrc.__init__Y   sv     &3D'6&J#7F#Q;	#D(GS&I	
    c                    g }|j                  d| j                  z         |j                  d| j                  z         d| j                  dz  z  }|j                  d|| j                  z  z         |j                  d|| j
                  z  z         |j                  d|| j                  z  z         dj                  |      S )	Nzpoly = 0x%Xzreverse = %s0x%%0%dX   zinitCrc  = %szxorOut   = %szcrcValue = %s
)appendr   r   r	   r
   r   r   join)r   lstfmts      r   __str__zCrc.__str__m   s    

=499,-

>DLL01D,,Q./

?cDLL&89:

?cDKK&789

?cDMM&9:;yy~r   Nc                 V   t        dd      }| j                  |_        | j                  |_        | j                  |_        | j                  |_        | j
                  |_        | j                  |_        | j                  |_        | j                  |_        ||j                  |       |S )a	  Create a new instance of the Crc class initialized to the same
        values as the original instance.  The current CRC is set to the initial
        value.  If a string is provided in the optional arg parameter, it is
        passed to the update method.
        NF)r   r   )
r   r   r	   r
   r   r   r   r   r   update)r   argns      r   newzCrc.neww   s     Te,((LL	;;**\\
LL	?HHSMr   c                 H    | j                         }| j                  |_        |S )zCreate a new instance of the Crc class initialized to the same
        values as the original instance.  The current CRC is set to the current
        value.  This allows multiple CRC calculations using a common initial
        string.
        )r'   r   )r   cs     r   copyzCrc.copy   s     HHJ]]
r   c                 F    | j                  || j                        | _        y)z_Update the current CRC value using the string specified as the data
        parameter.
        N)r   r   )r   datas     r   r$   z
Crc.update   s     		$6r   c                     | j                   }| j                  }g }|dkD  r$|j                  |dz         |dz	  }|dz  }|dkD  r$|j                          t	        |      S )zReturn the current CRC value as a string of bytes.  The length of
        this string is specified in the digest_size attribute.
        r      r      )r	   r   r   r   bytesr   r&   crcr    s       r   digestz
Crc.digest   sd     mm!eJJsTz"(CFA !e 	Szr   c                     | j                   }| j                  }g }|dkD  r'|j                  d|dz  z         |dz	  }|dz  }|dkD  r'|j                          dj	                  |      S )zReturn the current CRC value as a string of hex digits.  The length
        of this string is twice the digest_size attribute.
        r   z%02Xr.   r   r/    )r	   r   r   r   r   r1   s       r   	hexdigestzCrc.hexdigest   sm     mm!eJJvt,-(CFA !e 	wws|r   c           	         |d}|d| j                   z  }|dk(  rd}d|z  }| j                   dk(  rd}n&| j                  rd	}nd| j                   dz
  z  }d
|z  }dd| j                   z  z  }| j                   dk  r|dz   }n|dz   }dddddd| j                      }	g }
t        | j                        D ]2  \  }}||	z  dk(  r|
j	                  d       |
j	                  ||z         4 d| j
                  z  }| j                  r|dz   }| j                  rd|dd | j                  z  z  }|}nd}d}| j                   dk(  r| j                  r|dz  }n|dz  }|||||z  dj                  |
      |||d}|j                  t        |z         y)a(  Generate a C/C++ function.

        functionName -- String specifying the name of the function.

        out -- An open file-like object with a write method.  This specifies
        where the generated code is written.

        dataType -- An optional parameter specifying the data type of the input
        data to the function.  Defaults to UINT8.

        crcType -- An optional parameter specifying the data type of the CRC
        value.  Defaults to one of UINT8, UINT16, UINT32, or UINT64 depending
        on the size of the CRC value.
        NUINT8r          zUINT%dr/   ztable[*data ^ (%s)crc]z#table[*data ^ (%s)crc] ^ (crc >> 8)z,table[*data ^ (%%s)(crc >> %d)] ^ (crc << 8)r   r      zU,zULL,)r/   r      r;   r   r   z
    zpolynomial: 0x%Xz, bit reverse algorithmz
    crc = crc ^ %s;r5   r<   z
    crc = crc & 0xFFFFFFU;)dataTypecrcTypenamecrcAlgorcrcTabler   preConditionpostCondition)
r	   r   	enumerater   r   r   r   r   write_codeTemplate)r   functionNameoutr>   r?   sizerA   shiftr!   r&   r    ivalr   rC   rD   parmss                    r   generateCodezCrc.generateCode   s    H?T%%%DrzoGq /H\\ =H t''!+,EEMHAd.../q *C ,C Aa1%d&6&67

+FAsA!|

8$JJsSy! ,
 "DII-<<33D;;2c#2h6LML(MLMq  || >>!?? "!!H,)+	
 			-%'(r   )r=   Tr   TN)NN)__name__
__module____qualname____doc__r   r"   r'   r*   r$   r3   r6   rO    r   r   r   r   6   s1    !D%(&7T)r   r   c                 H    t        | ||      \  }}}t        | ||||      d   S )a  Return a function that computes the CRC using the specified polynomial.

    poly -- integer representation of the generator polynomial
    initCrc -- default initial CRC value
    rev -- when true, indicates that the data is processed bit reversed.
    xorOut -- the final XOR value

    The returned function has the following user interface
    def crcfun(data, crc=initCrc):
    r   )r   r   )r   r
   r   r   r   s        r   mkCrcFunrW     s1     #0gv"FXwT8Wc6:1==r   c                 `    d}dD ]  }d|z  }|dz  }|| cxk  r	|k  s|c S   t        |      )Nz8The degree of the polynomial must be 8, 16, 24, 32 or 64r      r9   r:   @   r/   r   )
ValueError)r   msgr&   lowhighs        r   _verifyPolyr`   '  sF    
DCd1u$H  
 S/r   c                 J    d}t        |      D ]  }|dz  | dz  z  }| dz	  }  |S )Nr   r/   range)xr&   yrL   s       r   _bitrevrf   3  s7    	A1X!VAF  Hr   c                 v    d|dz
  z  }t        d      D ]  }| |z  r	| dz  |z  } | dz  }  d|z  dz
  }| |z  } | S )Nr/   r   rb   )r2   r   r&   maskrL   s        r   _bytecrcri   ?  sX    qs8D1X:!8t#C(C	 
 qDA:D
*CJr   c                 f    t        d      D ]  }| dz  r	| dz	  |z  } | dz	  }  d|z  dz
  }| |z  } | S )Nr   r/   rb   )r2   r   r&   rL   rh   s        r   
_bytecrc_rrk   J  sK    1X7!8t#C(C	 
 qDA:D
*CJr   c                 |    d|z  dz
  }| |z  } t        d      D cg c]  }t        ||dz
  z  | |       }}|S c c}w )Nr/      r   )rc   ri   r   r&   rh   rL   r   s        r   _mkTablero   \  sK    qDA:D$;D05c
;
1Xa!A#htA&
E;L <s   9c                     d|z  dz
  }t        | |z  |      } t        d      D cg c]  }t        || |       }}|S c c}w )Nr/   rm   )rf   rc   rk   rn   s        r   
_mkTable_rrq   b  sJ    qDA:D4$;"D+0:6:aZ$q!:E6L 7s   =rY   z	B H I L Qr   rZ   r:   r[   )r/   r   r;   r   z256%sr9   c                 F    t        |       }d|z  dz
  }||z  }||z  }|||fS )Nr/   )r`   )r   r
   r   r   rh   s        r   r   r     s=    4 HxK1D nG d]Fgv&&r   c                     |rt        | |      }t        |   d   }nt        | |      }t        |   d   }|}t        rt	        j
                  t        |   g| }dk(  r|||fd}||fS |||ffd	}||fS )Nr/   r   c                      || ||      S rP   rU   )r,   r2   r   funs       r   r   z_mkCrcFun.<locals>.crcfun  s    tS%((r   c                 $     || |z  |      z  S rP   rU   )r,   r2   r   ru   r   s       r   r   z_mkCrcFun.<locals>.crcfun  s    CfslE:::r   )rq   _sizeMapro   _usingExtensionstructpack_sizeToTypeCode)	r   r   r
   r   r   	tableList_fun_tabler   s	       `    r   r   r     s    
tX.	!!$T8,	!!$F_X6CC{$F 	) 9 %F 	; 9r   aK  // Automatically generated CRC function
// %(poly)s
%(crcType)s
%(name)s(%(dataType)s *data, int len, %(crcType)s crc)
{
    static const %(crcType)s table[256] = {%(crcTable)s
    };
    %(preCondition)s
    while (len > 0)
    {
        crc = %(crcAlgor)s;
        data++;
        len--;
    }%(postCondition)s
    return crc;
}
)r=   Tr   )(rT   split__all__ gcloud_crcmod.python3._crcfunextpython3
_crcfunext_crcfunrx   ImportErrorgcloud_crcmod.python3r   sysry   r   rW   r`   rf   ri   rk   ro   rq   _crc8_crc8r_crc16_crc16r_crc24_crc24r_crc32_crc32r_crc64_crc64rrw   r{   typeCodegetcalcsizerJ   r   r   rG   rU   r   r   <module>r      sV  .	EG 66O
 T) T)n>4	$ --	(
..'//	*
..'//	*
..'//	*
..'//	* !!#H2r"&&vx'@FDD7 '( 2 $
 &b) d'2,S  :Os   D D/.D/