
    ]                     B   S r SSKrSSKrSSKr \R                  " 5          \R
                  " S5        SSKrSSS5        \R                  S;   a  SrO\	eSrSr/ S	QrSS
 jrS rS rS rS rS rS rS rS rS rg! , (       d  f       NL= f! \	 a	    SSK
rSr NHf = f)z)Helpers for calculating CRC32C checksums.    Nignore)ccffiTFl   Ao=     )            i   iAoivE:iWiPYl   T/U l   ?O iNi.%i'hinTi\Fib&l   ", iGWL;igl   b! l   <c l   ! l   N,Q l   2! l   =X iD3iKS}i2=l   my ixBc                     [         (       a  [        R                  " 5       nO[        R                  R                  S5      nU (       a  UR                  U 5        U$ )a  Returns an instance of Hashlib-like helper for CRC32C operations.

Args:
  initial_data (bytes): The CRC32C object will be initialized with the
    checksum of the data.

Returns:
  The google_crc32c.Checksum instance
  if google-crc32c (https://github.com/googleapis/python-crc32c) is
  available. If not, returns the predefined.Crc instance from crcmod library.

Usage:
  # Get the instance.
  crc = get_crc32c()
  # Update the instance with data. If your data is available in chunks,
  # you can update each chunk so that you don't have to keep everything in
  # memory.
  for chunk in chunks:
    crc.update(data)
  # Get the digest.
  crc_digest = crc.digest()

zcrc-32c)IS_FAST_GOOGLE_CRC32C_AVAILABLEgoogle_crc32cChecksumcrcmod
predefinedCrcupdate)initial_datacrcs     -lib/googlecloudsdk/command_lib/util/crc32c.py
get_crc32cr   =   sB    0 %$

 
 
"C




	
*CJJ|	*    c                 L    [        5       n[        (       a  Xl        U$ Xl        U$ )a#  Returns Hashlib-like CRC32C object with a starting checksum.

Args:
  checksum (int): CRC32C checksum representing the hash of processed data.

Returns:
  google_crc32c.Checksum if google-crc32c is available or predefined.Crc
 instance from crcmod library. Both set to use initial checksum.
)r   r   _crccrcValue)checksumr   s     r   get_crc32c_from_checksumr   `   s)     	#$$H 
* L	*r   c                 .    [        U 5      n[        U5      $ )zReturns base64-encoded hash from the checksum.

Args:
  checksum (int): CRC32C checksum representing the hash of processed data.

Returns:
  A string representing the base64 encoded CRC32C hash.
)r   get_hash)r   
crc_objects     r   $get_crc32c_hash_string_from_checksumr    t   s     (1*	*	r   c                 6    [        U R                  5       S5      $ )zGets the hex checksum from a CRC32C object.

Args:
  crc (google_crc32c.Checksum|predefined.Crc): CRC32C object from
    google-crc32c or crcmod package.

Returns:
  An int representing the CRC32C checksum of the provided object.
r	   )int	hexdigestr   s    r   get_checksumr%      s     
S]]_b	!!r   c                 h    [         R                  " U R                  5       5      R                  S5      $ )zGets the base64-encoded hash from a CRC32C object.

Args:
  crc (google_crc32c.Checksum|predefined.Crc): CRC32C object from
    google-crc32c or crcmod package.

Returns:
  A string representing the base64 encoded CRC32C hash.
ascii)base64	b64encodedigestdecoder$   s    r   r   r      s&     
		#**,	'	.	.w	77r   c                 |    [        5       nUR                  [        R                  " U 5      5        [	        U5      U:H  $ )a  Checks if checksum for the data matches the supplied checksum.

Args:
  data (bytes): Bytes over which the checksum should be calculated.
  crc32c_checksum (int): Checksum against which data's checksum will be
    compared.

Returns:
  True iff both checksums match.
)r   r   sixensure_binaryr%   )datacrc32c_checksumr   s      r   does_data_match_checksumr1      s2     	#**St$%	c	o	--r   c                 B    [        SR                  U SS9S S S2   S5      $ )Nz{0:032b}r   )widthr   )r"   format)crc_checksums    r   _reverse_32_bitsr7      s(    	Z|26tt<a	@@r   c                     SnS[         -  n[        [         5       H-  nU S-  (       a  X!-  nUS-  nX-  (       a	  U[        -  nU S-  n M/     U$ )zMultiplies two polynomials together modulo CASTAGNOLI_POLY.

Args:
  p (int): The first polynomial.
  q (int): The second polynomial.

Returns:
  Int result of the multiplication.
r      )DEGREErangeCASTAGNOLI_POLY)pqresulttop_bit_s        r   _multiply_crc_polynomialsrB      sV     &K'=a1ukf!GA{?a!GA  
-r   c                     [        U 5      nSnUS:w  a?  US-  (       a#  [        U[        U[        [        5      -     5      nUS-  nUS-  nUS:w  a  M?  [        U5      nU$ )zGiven crc_checksum representing polynomial P(x), compute P(x)*x^bit_count.

Args:
  crc_checksum (int): crc respresenting polynomial P(x).
  bit_count (int): number of bits in crc.

Returns:
  P(x)*x^bit_count (int).
r   r9   )r7   rB   X_POW_2K_TABLElen)r6   	bit_countupdated_crc_checksumis       r    _extend_crc32c_checksum_by_zerosrI      sq     *,7!Q1}6
q3~3F/F GIFA!OI 	Q **>?	r   c                 6    U(       d  U $ SU-  n[        XS9U-  $ )a  Computes CRC32C for concat(A, B) given crc(A), crc(B),and len(B).

An explanation of the algorithm can be found at
https://code.google.com/archive/p/crcutil/downloads.

Args:
  crc_a (int): Represents the CRC32C checksum of object A.
  crc_b (int): Represents the CRC32C checksum of object B.
  b_byte_count (int): Length of data covered by crc_b in bytes.

Returns:
  CRC32C checksum representing the data covered by crc_a and crc_b (int).
   )rF   )rI   )crc_acrc_bb_byte_countb_bit_counts       r   concat_checksumsrP      s&     
LL +	)%	G%	OOr   )r   )__doc__r(   warningsr-   catch_warningssimplefilterr   implementationr   ImportErrorgcloud_crcmodr   r<   r:   rD   r   r   r    r%   r   r1   r7   rB   rI   rP    r   r   <module>rY      s    0   
*  (# ! !!]2 '+#
 	
 F(

"
8. A00PG !   * !$)!	*s-   B A>B B >
BB BB