
    C                        S r SSKrSSKrSR                  5       r SSKrSr\R                  R                  S:  a  \r\" S5      ) r " S S5      r\SS4S	 jrS
 rS rS rS rS rS r\R0                  \R2                  /\R4                  \R6                  /\R8                  \R:                  /\R<                  \R>                  /\R@                  \RB                  /S.r"0 r#SR                  5        H?  r$SSSSS.RK                  \RL                  " \$5      S5      r'\'c  M/  \'\#;  d  M7  S\$-  \#\''   MA     \#S   \#S'   C$C'S r(S r)Sr*g! \ a+    \R                  R                  S:X  a  SSKrOSSKJr  Sr GNWf = f)al  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
md5 and sha modules 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.
    NzmkCrcFun Crc
T   Fc                   \    \ rS rSrSr\SSS4S jrS rSS jrS	 r	S
 r
S rS rSS jrSrg)Crc>   aq  Compute a Cyclic Redundancy Check (CRC) using the specified polynomial.

Instances of this class have the same interface as the md5 and sha modules
in the Python standard library.  See the documentation for these modules
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 or long 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.
Tr   c                     U(       d  g [        XU5      u  pbnUS-  U l        X l        X@l        Xl        X0l        [        XX#U5      u  pxXpl        Xl        U R                  U l	        g )N   )
_verifyParamsdigest_sizeinitCrcxorOutpolyreverse	_mkCrcFun_crctablecrcValue)	selfr   r   revr   
initializesizeBitscrcfunr   s	            7platform/gsutil/third_party/crcmod_osx/crcmod/crcmod.py__init__Crc.__init__a   s`     &3D6&J#F#Q;	#DG&I	
    c                    / nUR                  SU R                  -  5        UR                  SU R                  -  5        SU R                  S-  -  nUR                  SX R                  -  -  5        UR                  SX R
                  -  -  5        UR                  SX R                  -  -  5        SR                  U5      $ )	Nzpoly = 0x%Xzreverse = %s0x%%0%dXr   zinitCrc  = %szxorOut   = %szcrcValue = %s
)appendr   r   r
   r   r   r   join)r   lstfmts      r   __str__Crc.__str__u   s    

=499,-

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

?cLL&89:

?cKK&789

?cMM&9:;yy~r   Nc                 R   [        SSS9nU R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        U R
                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        Ub  UR                  U5        U$ )zCreate 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   newCrc.new   s     Te,((LL	;;**\\
LL	?HHSMr   c                 H    U R                  5       nU R                  Ul        U$ )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   copyCrc.copy   s     HHJ]]
r   c                 D    U R                  XR                  5      U l        g)zOUpdate the current CRC value using the string specified as the data
parameter.
N)r   r   )r   datas     r   r&   
Crc.update   s     		$6r   c                     U R                   nU R                  n/ nUS:  a/  UR                  [        US-  5      5        US-	  nUS-  nUS:  a  M/  UR	                  5         SR                  U5      $ )zyReturn 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   chrr   r    r   r(   crcr!   s       r   digest
Crc.digest   sl     mm!eJJs3:'(CFA !e 	wws|r   c                     U R                   nU R                  n/ nUS:  a)  UR                  SUS-  -  5        US-	  nUS-  nUS:  a  M)  UR                  5         SR	                  U5      $ )zwReturn the current CRC value as a string of hex digits.  The length
of this string is twice the digest_size attribute.
r   z%02Xr3   r   r4   r5   )r
   r   r   r   r    r7   s       r   	hexdigestCrc.hexdigest   sm     mm!eJJvt,-(CFA !e 	wws|r   c           	      H   Uc  SnUc  SU R                   -  nUS:X  a  SnSU-  nU R                   S:X  a  SnO+U R                  (       a  S	nOSU R                   S-
  -  nS
U-  nSSU R                   -  -  nU R                   S::  a  US-   nOUS-   nSSSSSS.U R                      n	/ n
[        U R                  5       H1  u  pX-  S:X  a  U
R	                  S5        U
R	                  X-  5        M3     SU R
                  -  nU R                  (       a  US-   nU R                  (       a  SUSS U R                  -  -  nUnOSnSnU R                   S:X  a  U R                  (       a  US-  nOUS-  nUUUXc-  SR                  U
5      UUUS.nUR                  [        U-  5        g)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%dr4   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,)r4   r      rB   r   r   z
    zpolynomial: 0x%Xz, bit reverse algorithmz
    crc = crc ^ %s;r5   rC   z
    crc = crc & 0xFFFFFFU;)dataTypecrcTypenamecrcAlgorcrcTabler   preConditionpostCondition)
r
   r   	enumerater   r   r   r   r    write_codeTemplate)r   functionNameoutrE   rF   sizerH   shiftr"   r(   r!   ivalr   rJ   rK   parmss                    r   generateCode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

+FA!|

8$JJsy! ,
 "DII-<<33D;;2c#2h6LML(MLMq  || >>!?? "!!,)+	
 			-%'(r   )r   r   r
   r   r   r   r   r   N)NN)__name__
__module____qualname____firstlineno____doc__	_CRC_INITr   r#   r)   r-   r&   r9   r<   rV   __static_attributes__ r   r   r   r   >   s<    !D &/Dt %(&7T)r   r   c                 @    [        XU5      u  pAn[        XXU5      S   $ )ac  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   mkCrcFunrb     s)     #0v"FXTW6:1==r   c                     Sn[        U 5      n S H*  n[        S5      U-  nUS-  nX0s=::  a  U:  d  M$   Us  $   M,     [        U5      e)Nz8The degree of the polynomial must be 8, 16, 24, 32 or 64r      r@   rA   @   r4   r   )long
ValueError)r   msgr(   lowhighs        r   _verifyPolyrl   /  sQ    
DC:D1gqj1uH  
 S/r   c                     [        U 5      n [        S5      n[        U5       H  nUS-  U [        S5      -  -  nU S-	  n M     [        S5      U-  S-
  [        R                  ::  a  [	        U5      $ U$ )Nr   r4   rg   xrangesysmaxintint)xr(   yrS   s       r   _bitrevru   <  si    QAQAAY!VDG$F  
a!Q3::%1vHr   c                    [        U 5      n [        U5      n[        S5      US-
  -  n[        S5       H  nX-  (       a
  U S-  U-  n M  U S-  n M     [        S5      U-  S-
  nX-  n U[        R                  ::  a  [	        U 5      $ U $ )Nr4   r   rn   )r8   r   r(   maskrS   s        r   _bytecrcrx   K  s    
s)C:D7QqS>DAY:!8t#C(C	 
 GQJ!D
*Cszz3xJr   c                    [        U 5      n [        U5      n[        S5       H%  nU [        S5      -  (       a
  U S-	  U-  n M   U S-	  n M'     [        S5      U-  S-
  nX-  n U[        R                  ::  a  [	        U 5      $ U $ )Nr   r4   rn   )r8   r   r(   rS   rw   s        r   
_bytecrc_rrz   Z  sy    
s)C:DAYa=!8t#C(C	 
 GQJ!D
*Cszz3xJr   c                     [        S5      U-  S-
  n[        U 5      U-  n [        S5       Vs/ s H  n[        [        U5      US-
  -  X5      PM      nnU$ s  snf )Nr4      r   )rg   ro   rx   r   r(   rw   rS   r   s        r   _mkTabler~   p  sX    GQJ!D:D6<SkBkXd1g!nT,kEBL Cs   %Ac                     [        S5      U-  S-
  n[        [        U 5      U-  U5      n [        S5       Vs/ s H  n[        [        U5      X5      PM     nnU$ s  snf )Nr4   r|   )rg   ru   ro   rz   r}   s        r   
_mkTable_rr   v  sV    GQJ!D4:$a(D17=AZQ'E=L >s   Ard   z	B H I L Qr   re   rA   rf   )r4   r   rB   r   z256%sr@   c                     [        U 5      n[        S5      U-  S-
  n[        U5      U-  nU[        R                  ::  a  [	        U5      n[        U5      U-  nU[        R                  ::  a  [	        U5      nX1U4$ )Nr4   )rl   rg   rp   rq   rr   )r   r   r   r   rw   s        r   r	   r	     st    4 HGX"D 7md"Gszzg, &\D FszzVv&&r   c                    ^ U(       a  [        X5      n[        U   S   nO[        X5      n[        U   S   nUn[        (       a  [        R
                  " [        U   /UQ76 nTS:X  a
  X'U4S jnX4$ X'U4U4S jjnX4$ )Nr4   r   c                     U" XU5      $ rX   r`   )r0   r8   r   funs       r   r   _mkCrcFun.<locals>.crcfun  s    t%((r   c                 $   > TU" U TU-  U5      -  $ rX   r`   )r0   r8   r   r   r   s       r   r   r     s    CfslE:::r   )r   _sizeMapr~   _usingExtensionstructpack_sizeToTypeCode)	r   r   r   r   r   	tableList_fun_tabler   s	       `    r   r   r     s    
t.	!!$T,	!!$F_X6CC{$ 	)  % 	; 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]   r   rp   split__all__
_crcfunext_crcfunr   ImportErrorversion_infomajor	_crcfunpycrcmod._crcfunpyrr   rg   r^   r   rb   rl   ru   rx   rz   r~   r   _crc8_crc8r_crc16_crc16r_crc24_crc24r_crc32_crc32r_crc64_crc64rr   r   typeCodegetcalcsizerQ   r	   r   rN   r`   r   r   <module>r      s  .  
	EG  O A	$1gJ	T) T)n %$q >4, --	(
..'//	*
..'//	*
..'//	*
..'//	* !!#H2r"&&vx'@FDD7 '( 2 $
 &b) d':,  
"!(Os   E	 	-E:9E: