
    Ii4'                        S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SKJ	r	  Sr
SrSr S SKr S SKr S SKJr  S r S SKr S SKr S SKr\" \R2                  " 5       S	:H  5      rS
 rS rS rS rSS jrS r SS jr!S r"S r#\(       a  \r$O*\(       a  \RJ                  r$O\(       a  \RL                  r$OSr$S r'\(       a  \RP                  r)O \(       a  \'r)O\(       a  \RP                  r)OSr)S r*S r+S r,S r-g! \ a    Sr Nf = f! \ a    Sr Nf = f! \ a    Sr Nf = f! \ a    Sr Nf = f! \ a    Sr Nf = f! \ a    Sr GNf = f)    )absolute_importN)six)range)
i   S   N   A   Pr	      Yr      r   
bccccccBiii   c                      UR                  SS 5        [        R                  " U 4SS0UD6$ ! [         a.    UR                  SS 5        [        R                  " U 4SS0UD6s $ f = f)Nblock_linkedF
block_moder   )poplz4compress	TypeError)payloadkwargss     1/venv/lib/python3.13/site-packages/kafka/codec.py_lz4_compressr      sg    	AJJ~t,<<FeFvFF 	AJJ|T*<<@A@@@	As   *- 5A%$A%PyPyc                      g)NT r       r   has_gzipr   ;   s    r   c                      [         S L$ N)snappyr   r   r   
has_snappyr    ?   s    r   c                      [         S L$ r   )zstdr   r   r   has_zstdr#   C   s    tr   c                  4    [         b  g[        b  g[        b  gg)NTF)r   lz4f	lz4framedr   r   r   has_lz4r'   G   s    
r   c                     U(       d  Sn[         R                  " 5       n[        R                  " USUS9n UR	                  U 5        UR                  5         UR                  5       $ ! UR                  5         f = f)N	   w)fileobjmodecompresslevel)ioBytesIOgzipGzipFilewriteclosegetvalue)r   r-   bufgzippers       r   gzip_encoder7   Q   s\    
**,C mmCcOGg<<> 	s   A' 'A9c                     [         R                  " U 5      n[        R                  " USS9n UR	                  5       UR                  5         $ ! UR                  5         f = f)Nr)r+   r,   )r.   r/   r0   r1   readr3   )r   r5   r6   s      r   gzip_decoder;   b   sC    
**W
C mmCc2G||~s   A Ac                   ^ ^^	 [        5       (       d  [        S5      eU(       d  [        R                  " T 5      $ [        R
                  " 5       n[        [        [        5       H.  u  pEUR                  [        R                  " SU-   U5      5        M0     [        (       a  S m	O[        R                  (       a  S m	OS m	U	U U4S j[        S[!        T 5      T5       5        H[  n[        R                  " U5      n[!        U5      nUR                  [        R                  " SU5      5        UR                  U5        M]     UR#                  5       $ )	a  Encodes the given data with snappy compression.

If xerial_compatible is set then the stream is encoded in a fashion
compatible with the xerial snappy library.

The block size (xerial_blocksize) controls how frequent the blocking occurs
32k is the default in the xerial library.

The format winds up being:


    +-------------+------------+--------------+------------+--------------+
    |   Header    | Block1 len | Block1 data  | Blockn len | Blockn data  |
    +-------------+------------+--------------+------------+--------------+
    |  16 bytes   |  BE int32  | snappy bytes |  BE int32  | snappy bytes |
    +-------------+------------+--------------+------------+--------------+


It is important to note that the blocksize is the amount of uncompressed
data presented to snappy at each block, whereas the blocklen is the number
of bytes that will be present in the stream; so the length will always be
<= blocksize.

Snappy codec is not available!c                     XX!-    $ r   r   r   isizes      r   <lambda>snappy_encode.<locals>.<lambda>   s    7TV+<r   c                     [        XU5      $ r   )bufferr@   s      r   rC   rD      s    6'd+Cr   c                 >    [        U 5      XU-    R                  5       $ r   )
memoryviewtobytesr@   s      r   rC   rD      s    :g+>qa+H+P+P+Rr   c              3   8   >#    U  H  nT" TUT5      v   M     g 7fr   r   ).0rA   chunkerr   xerial_blocksizes     r   	<genexpr> snappy_encode.<locals>.<genexpr>   s&      ECa '1&677Cs   r   !i)r    NotImplementedErrorr   r   r.   r/   zip_XERIAL_V1_FORMAT_XERIAL_V1_HEADERr2   structpackPYPYr   PY2r   lenr4   )
r   xerial_compatiblerM   outfmtdatchunkblock
block_sizerL   s
   ` `      @r   snappy_encodera   n   s    4 <<!"ABBw''
**,C)+<=		&++cCi-. > t =	 D
 SE CL2BCE &Z
		&++dJ/0		%E <<>r   c                     [        U 5      S:  aj  [        R                  " S[        SS -   [	        U 5      SS 5      n[        R                  " S[        SS -   [	        U 5      SS 5      u  p#U[
        SS :X  a  gg)a  Detects if the data given might have been encoded with the blocking mode
of the xerial snappy library.

This mode writes a magic header of the format:
    +--------+--------------+------------+---------+--------+
    | Marker | Magic String | Null / Pad | Version | Compat |
    +--------+--------------+------------+---------+--------+
    |  byte  |   c-string   |    byte    |  int32  | int32  |
    +--------+--------------+------------+---------+--------+
    |  -126  |   'SNAPPY'   |           |         |        |
    +--------+--------------+------------+---------+--------+

The pad appears to be to ensure that SNAPPY is a valid cstring
The version is the version of this format as written by xerial,
in the wild this is currently 1 as such we only support v1.

Compat is there to claim the minimum supported version that
can read a xerial block stream, presently in the wild this is
1.
   r>   N   TF)rY   rU   unpackrS   bytesrT   )r   magicversioncompats       r   _detect_xerial_streamrj      s|    , 7|bc$5bq$995>"1;MN --.?.C(CU7^TUVXEYZ %bq))r   c                    [        5       (       d  [        S5      e[        U 5      (       a  [        R                  " 5       nU SS  n[        U5      nSnXC:  aU  [        R                  " SX$S  5      S   nUS-  nXE-   nUR                  [        R                  " X$U 5      5        UnXC:  a  MU  UR                  S5        UR                  5       $ [        R                  " U 5      $ )Nr=   rc   r   rP      )r    rQ   rj   r.   r/   rY   rU   unpack_fromr2   r   
decompressseekr:   )r   r[   bytlengthcursorr`   ends          r   snappy_decodert      s    <<!"ABBW%%jjlbclSo++D#g,?BJaKF%CIIf''389F o 	xxz  ))r   c                     [         R                  " 5       n[         R                  " X5      n[         R                  " U5        US   S:w  a  [	        S5      eUS   $ )zFDecode payload using interoperable LZ4 framing. Requires Kafka >= 0.10nextr   z&lz4f unable to decompress full payloaddecomp)r%   createDecompContextdecompressFramefreeDecompContextRuntimeError)r   ctxdatas      r   lz4f_decoder~      sT     
"
"
$C-D3 F|qCDD>r   c                 z   [         c   e[        U 5      nSnUS   n[        U[        5      (       d  [	        U5      nUS-	  S-  nU(       a'  US-  n[        U5      nX1S'   [        U5      nXS-   S n OXS n [         R                  " USUS-
   5      R                  5       SS	 nS
R                  USUS-
   UU /5      $ )zLEncode payload for 0.8/0.9 brokers -- requires an incorrect header checksum.N   rl      r   rd   r   r   )
xxhash
lz4_encode
isinstanceintord	bytearrayrf   xxh32digestjoin)r   r}   header_sizeflgcontent_size_bithcs         r   lz4_encode_old_kafkar     s    gDK
q'Cc3#hQ 	qQT{1}~&|$ 
d1[]+	,	3	3	5b	<B88Q{1}
  r   c                 @   [         c   eSn[        U S   [        5      (       a  U S   nO[        U S   5      nUS-	  S-  nU(       a  US-  n[         R                  " U SUS-
   5      R                  5       SS nSR                  U S	US-
   UXS  /5      n[        U5      $ )
Nr   rl   r   r   rd   r   r   r   r   )r   r   r   r   r   r   r   
lz4_decode)r   r   r   r   r   munged_payloads         r   lz4_decode_old_kafkar   *  s    K'!*c""aj'!*oQq 
gaA.	/	6	6	8B	?BXX+a- 
 N
 n%%r   c                 v    [         (       d  [        S5      e[         R                  " 5       R                  U 5      $ )NZstd codec is not available)r"   rQ   ZstdCompressorr   r   s    r   zstd_encoder   A  s-    4!"?@@ ))'22r   c                     [         (       d  [        S5      e [         R                  " 5       R                  U 5      $ ! [         R                   a*    [         R                  " 5       R                  U [
        S9s $ f = f)Nr   )max_output_size)r"   rQ   ZstdDecompressorrn   	ZstdErrorZSTD_MAX_OUTPUT_SIZEr   s    r   zstd_decoder   G  sh    4!"?@@a$$&11'::>> a$$&11'K_1``as   #< ;A:9A:r   )Ti   ).
__future__r   r0   r.   platformrU   kafka.vendorr   kafka.vendor.six.movesr   rT   rS   r   r   ImportError	zstandardr"   	lz4.frameframer   r   r%   r&   r   boolpython_implementationrW   r   r    r#   r'   r7   r;   ra   rj   rt   r   compressFramer   r~   rn   r   r   r   r   r   r   r   r   <module>r      s   &  	    (G   " 	A H**,67"	<~ F*2 J	##J##JJ J	J%%JJ>&.3am	  F
  D"  
C
  D
  I
  Fsh   C= D 	D D' D5 E =DDDDD$#D$'D21D25E ?E EE