o
    `js&                     @   s  d dl Z d dlZd dlZd dlZdZdZdZzd dlZW n ey'   dZY nw zd dl	Z
W n ey9   dZ
Y nw zd dlmZ dd ZW n eyQ   dZY nw zd dlZW n eyc   dZY nw zd dlZW n eyu   dZY nw zd dlZW n ey   dZY nw ee dkZdd	 Zd
d Zdd Zdd Zd&ddZdd Zd'ddZdd Zdd ZereZnerejZnerej ZndZdd Z!erej"Z#nere!Z#nerej"Z#ndZ#dd Z$d d! Z%d"d# Z&d$d% Z'dS )(    N)
i   S   N   A   Pr      Yr      r   
bccccccBiii   c                 K   sZ   z| dd  tj| fddi|W S  ty,   | dd  tj| fddi| Y S w )Nblock_linkedF
block_moder   )poplz4compress	TypeError)payloadkwargs r   I/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/codec.py_lz4_compress   s   r   PyPyc                   C   s   dS )NTr   r   r   r   r   has_gzip7   s   r   c                   C      t d uS N)snappyr   r   r   r   
has_snappy;      r   c                   C   r   r   )zstdr   r   r   r   has_zstd?   r   r   c                   C   s(   t d urdS td urdS td urdS dS )NTF)r   lz4f	lz4framedr   r   r   r   has_lz4C   s   r   c                 C   sH   |sd}t  }tj|d|d}z||  W |  | S |  w )N	   w)fileobjmodecompresslevel)ioBytesIOgzipGzipFilewriteclosegetvalue)r   r$   bufgzipperr   r   r   gzip_encodeM   s   
r.   c                 C   s6   t | }tj|dd}z	| W |  S |  w )Nr)r"   r#   )r%   r&   r'   r(   readr*   )r   r,   r-   r   r   r   gzip_decode^   s
   
r1   T   c           	         s   t  std|stS t }tttD ]\}}|	t
d| | qtr.dd  ndd   fddtdtD D ]}t|}t|}|	t
d	| |	| qC| S )
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                 S   s   | |||  S r   r   r   isizer   r   r   <lambda>   s    zsnappy_encode.<locals>.<lambda>c                 S   s   t | |||   S r   )
memoryviewtobytesr5   r   r   r   r8      s    c                 3   s    | ]	} |V  qd S r   r   ).0r6   chunkerr   xerial_blocksizer   r   	<genexpr>   s    z snappy_encode.<locals>.<genexpr>r   !i)r   NotImplementedErrorr   r   r%   r&   zip_XERIAL_V1_FORMAT_XERIAL_V1_HEADERr)   structpackPYPYrangelenr+   )	r   xerial_compatibler>   outfmtdatchunkblock
block_sizer   r<   r   snappy_encodej   s$   



rQ   c                 C   sp   t | dkr6tdtdd  t| dd }tdtdd  t| dd \}}|tdd kr6dS dS )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.
       r4   N   TF)rI   rE   unpackrC   bytesrD   )r   magicversioncompatr   r   r   _detect_xerial_stream   s   $(rY   c                 C   s   t  stdt| rNt }| dd  }t|}d}||k rEtd||d  d }|d7 }|| }|t	
|||  |}||k s|d | S t	
| S )Nr3   rR   r   r@      )r   rA   rY   r%   r&   rI   rE   unpack_fromr)   r   
decompressseekr0   )r   rK   bytlengthcursorrP   endr   r   r   snappy_decode   s"   

rb   c                 C   s:   t  }t | |}t | |d dkrtd|d 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datar   r   r   lz4f_decode   s   
rk   c                 C   s   t du rtdt| }d}|d }t|tst|}|d? d@ }|r<|d8 }t|}||d< t|}||d d } n||d } t |d|d  	 d	d
 }d
|d|d  || gS )zLEncode payload for 0.8/0.9 brokers -- requires an incorrect header checksum.N8pip install xxhash for lz4 encoding with 0.8/0.9 brokers   rZ      r   rS   r       )xxhashrh   
lz4_encode
isinstanceintord	bytearrayrU   xxh32digestjoin)r   rj   header_sizeflgcontent_size_bithcr   r   r   lz4_encode_old_kafka  s*   
"r   c                 C   s   t d u rtdd}t| d tr| d }nt| d }|d? d@ }|r(|d7 }t | d|d   dd }d	| d
|d  || |d  g}t|S )Nrl   rm   rZ   rn   r   rS   ro   rp   rq   r   )	rr   rh   rt   ru   rv   rx   ry   rz   
lz4_decode)r   r{   r|   r}   r~   munged_payloadr   r   r   lz4_decode_old_kafka#  s    
"
r   c                 C   s   t stdt  | S )NZstd codec is not available)r   rA   ZstdCompressorr   )r   r   r   r   zstd_encode;  s   r   c                 C   sP   t stdt  jt| dd}| W  d    S 1 s!w   Y  d S )Nr   T)read_across_frames)r   rA   ZstdDecompressorstream_readerr%   r&   r0   )r   readerr   r   r   zstd_decodeA  s
   $r   r   )Tr2   )(r'   r%   platformrE   rD   rC   ZSTD_MAX_OUTPUT_SIZEr   ImportError	zstandardr   	lz4.frameframer   r   r   r   rr   boolpython_implementationrG   r   r   r   r   r.   r1   rQ   rY   rb   rs   compressFramer   rk   r\   r   r   r   r   r   r   r   r   r   <module>   s    


;# 