
                         V   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZ	  ej                         5   ej                  d       ddl
Z
ddd       e
j                  dv rdZne	 d
ZdZg dZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zy# 1 sw Y   ExY w# e$ r	 ddlZd	ZY Aw xY w)z)Helpers for calculating CRC32C checksums.    )absolute_import)division)unicode_literalsN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                     t         rt        j                         }nt        j                  j                  d      }| r|j                  |        |S )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   @   s?    0 %

 
 
"C




	
*CJJ|	*    c                 F    t               }t        r	| |_        |S | |_        |S )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   c   s*     	#$CH 
* CL	*r   c                 .    t        |       }t        |      S )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#   w   s     (1*	*	r   c                 6    t        | j                         d      S )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                 f    t        j                  | j                               j                  d      S )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                 z    t               }|j                  t        j                  |              t	        |      |k(  S )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_checksumr4      s2     	#**St$%	c	o	--r   c                 H    t        dj                  | d      d d d   d      S )Nz{0:032b}r	   )widthr
   )r%   format)crc_checksums    r   _reverse_32_bitsr:      s(    	Z|26tt<a	@@r   c                     d}dt         z  }t        t               D ]$  }| dz  r||z  }|dz  }||z  r	|t        z  }| dz  } & |S )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_polynomialsrE      sZ     &K'=a1ukf!GA7{?a!GA  
-r   c                     t        |       }d}|dk7  r8|dz  r#t        |t        |t        t              z           }|dz  }|dz  }|dk7  r8t        |      }|S )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   r<   )r:   rE   X_POW_2K_TABLElen)r9   	bit_countupdated_crc_checksumis       r    _extend_crc32c_checksum_by_zerosrL      sq     *,7!Q1}6
q3~3F/F GIFA!OI 	Q **>?	r   c                 4    |s| S d|z  }t        | |      |z  S )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).
     )rI   )rL   )crc_acrc_bb_byte_countb_bit_counts       r   concat_checksumsrS      s(     
LL +	)%;	G%	OOr   )r   )__doc__
__future__r   r   r   r+   warningsr0   catch_warningssimplefilterr   implementationr   ImportErrorgcloud_crcmodr   r?   r=   rG   r   r   r#   r(   r!   r4   r:   rE   rL   rS    r   r   <module>r]      s    0 &  '   
* x H(# ! !!]2 '+#
 $ 	
 F(

"
8. A00PG !   * !$)!	*s(   B B
B BB B('B(