o
    çûõjI  ã                   @   sb  U d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZmZ ejdkr9d dlmZ nd dlmZ eeedd	f Zzzd dlZW n ey[   d dlZY nw d
ZW n eyj   dZY nw zejdkrxd dlmZ nd dlmZ d
ZW n eyŒ   dZY nw dZd ZdZ dZ!dZ"dZ#G dd„ de$ƒZ%G dd„ deƒZ&G dd„ deƒZ'G dd„ deƒZ(G dd„ dedd�Z)G dd„ dƒZ*e*eƒZ+e*e,d < d!e(d"dfd#d$„Z-		d:d%e.dB d&e/d"e0fd'd(„Z1G d)d*„ d*eƒZ2ed+e2d,�Z3G d-d.„ d.eƒZ4G d/d0„ d0e4ee3 ƒZ5G d1d2„ d2ƒZ6G d3d4„ d4e5e' ƒZ7G d5d6„ d6e4ƒZ8G d7d8„ d8e5d9 ƒZ9dS );é    N)ÚABCÚabstractmethod)ÚExecutor)ÚAnyÚFinalÚGenericÚProtocolÚ	TypedDictÚTypeVarÚcast)é   é   )ÚBuffer)ÚUnionzmemoryview[int]zmemoryview[bytes]TF)r   é   ©ÚZstdDecompressori   éÿÿÿÿé@   i   i   c                   @   s   e Zd ZdZdS )ÚTooManyMembersErrorz:A stream concatenated more members than the caller allows.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úL/opt/steambot/venv/lib/python3.10/site-packages/aiohttp/compression_utils.pyr   6   s    r   c                   @   s2   e Zd Zdedefdd„Zd
dedefdd„Zd	S )ÚZLibCompressObjProtocolÚdataÚreturnc                 C   ó   d S ©Nr   ©Úselfr   r   r   r   Úcompress;   ó    z ZLibCompressObjProtocol.compress.Úmodec                C   r   r    r   ©r"   r%   r   r   r   Úflush<   r$   zZLibCompressObjProtocol.flushN©.)r   r   r   r   Úbytesr#   Úintr'   r   r   r   r   r   :   s    r   c                   @   sn   e Zd Zddededefdd„Zddedefdd	„Zede	fd
d„ƒZ
edefdd„ƒZedefdd„ƒZdS )ÚZLibDecompressObjProtocol.r   Ú
max_lengthr   c                 C   r   r    r   ©r"   r   r,   r   r   r   Ú
decompress@   r$   z$ZLibDecompressObjProtocol.decompressÚlengthc                C   r   r    r   ©r"   r/   r   r   r   r'   A   r$   zZLibDecompressObjProtocol.flushc                 C   r   r    r   ©r"   r   r   r   ÚeofC   ó   zZLibDecompressObjProtocol.eofc                 C   r   r    r   r1   r   r   r   Úunconsumed_tailF   r3   z)ZLibDecompressObjProtocol.unconsumed_tailc                 C   r   r    r   r1   r   r   r   Úunused_dataI   r3   z%ZLibDecompressObjProtocol.unused_dataNr(   )r   r   r   r   r*   r)   r.   r'   ÚpropertyÚboolr2   r4   r5   r   r   r   r   r+   ?   s    r+   c                   @   sÄ   e Zd ZU eed< eed< eed< eed< eed< 						ddeded	ed
edededB defdd„Z	dd	ededefdd„Z		dded	edede
fdd„Z	dd	ededede
fdd„ZdS )ÚZLibBackendProtocolÚ	MAX_WBITSÚZ_FULL_FLUSHÚZ_SYNC_FLUSHÚZ_BEST_SPEEDÚZ_FINISH.ÚlevelÚmethodÚwbitsÚmemLevelÚstrategyÚzdictNr   c                 C   r   r    r   )r"   r>   r?   r@   rA   rB   rC   r   r   r   ÚcompressobjT   s   zZLibBackendProtocol.compressobjc                 C   r   r    r   )r"   r@   rC   r   r   r   Údecompressobj]   ó   z!ZLibBackendProtocol.decompressobjr   c                C   r   r    r   )r"   r   r>   r@   r   r   r   r#   a   rF   zZLibBackendProtocol.compressÚbufsizec                C   r   r    r   )r"   r   r@   rG   r   r   r   r.   d   rF   zZLibBackendProtocol.decompress)......)..)r   r   r   r*   Ú__annotations__r   r   rD   r+   rE   r)   r#   r.   r   r   r   r   r8   M   sn   
 ùþýüûúù
ø
ÿÿÿ
þÿÿÿÿ
þÿÿÿÿþr8   c                   @   s&   e Zd ZU eed< eed< eed< dS )ÚCompressObjArgsr@   rB   r>   N)r   r   r   r*   rH   r   r   r   r   rI   i   s   
 rI   )Útotalc                   @   sø   e Zd Zdefdd„Zedefdd„ƒZedefdd„ƒZ	edefd	d
„ƒZ
edefdd„ƒZedefdd„ƒZedefdd„ƒZdededefdd„Zdededefdd„Zdedededefdd„Zdedededefdd„Zdedefdd„ZdS ) ÚZLibBackendWrapperÚ_zlib_backendc                 C   s
   || _ d S r    )rL   )r"   rL   r   r   r   Ú__init__p   ó   
zZLibBackendWrapper.__init__r   c                 C   s   t | jddƒS )Nr   Ú	undefined©ÚgetattrrL   r1   r   r   r   Únames   s   zZLibBackendWrapper.namec                 C   ó   | j jS r    )rL   r9   r1   r   r   r   r9   w   ó   zZLibBackendWrapper.MAX_WBITSc                 C   rS   r    )rL   r:   r1   r   r   r   r:   {   rT   zZLibBackendWrapper.Z_FULL_FLUSHc                 C   rS   r    )rL   r;   r1   r   r   r   r;      rT   zZLibBackendWrapper.Z_SYNC_FLUSHc                 C   rS   r    )rL   r<   r1   r   r   r   r<   ƒ   rT   zZLibBackendWrapper.Z_BEST_SPEEDc                 C   rS   r    )rL   r=   r1   r   r   r   r=   ‡   rT   zZLibBackendWrapper.Z_FINISHÚargsÚkwargsc                 O   ó   | j j|i |¤ŽS r    )rL   rD   ©r"   rU   rV   r   r   r   rD   ‹   ó   zZLibBackendWrapper.compressobjc                 O   rW   r    )rL   rE   rX   r   r   r   rE   Ž   rY   z ZLibBackendWrapper.decompressobjr   c                 O   ó   | j j|g|¢R i |¤ŽS r    )rL   r#   ©r"   r   rU   rV   r   r   r   r#   ‘   ó   zZLibBackendWrapper.compressc                 O   rZ   r    )rL   r.   r[   r   r   r   r.   ”   r\   zZLibBackendWrapper.decompressÚattrnamec                 C   s   t | j|ƒS r    rP   )r"   r]   r   r   r   Ú__getattr__˜   ó   zZLibBackendWrapper.__getattr__N)r   r   r   r8   rM   r6   ÚstrrR   r*   r9   r:   r;   r<   r=   r   r   rD   r+   rE   r   r)   r#   r.   r^   r   r   r   r   rK   o   s&    rK   ÚZLibBackendÚnew_zlib_backendr   c                 C   s
   | t _d S r    )ra   rL   )rb   r   r   r   Úset_zlib_backendŸ   rN   rc   ÚencodingÚsuppress_deflate_headerc                 C   s$   | dkr	dt j S |rt j S t jS )NÚgzipé   )ra   r9   )rd   re   r   r   r   Úencoding_to_mode£   s   
rh   c                   @   sH   e Zd Zddededefdd„Zedefdd„ƒZ	edefd	d
„ƒZ
dS )ÚMemberDecompressObjProtocol.r   r,   r   c                 C   r   r    r   r-   r   r   r   r.   ®   r$   z&MemberDecompressObjProtocol.decompressc                 C   r   r    r   r1   r   r   r   r2   °   r3   zMemberDecompressObjProtocol.eofc                 C   r   r    r   r1   r   r   r   r5   ³   r3   z'MemberDecompressObjProtocol.unused_dataNr(   )r   r   r   r   r*   r)   r.   r6   r7   r2   r5   r   r   r   r   ri   ­   s    ri   Ú_DecompressObjT)Úboundc                   @   sz   e Zd ZdefdedB dedB fdd„Zeefde	dede
fd	d
„ƒZefde	dede
fdd„Zeedefdd„ƒƒZdS )ÚDecompressionBaseHandlerNÚexecutorÚmax_sync_chunk_sizec                 C   s   || _ || _dS )z&Base class for decompression handlers.N)Ú	_executorÚ_max_sync_chunk_size©r"   rm   rn   r   r   r   rM   »   s   
z!DecompressionBaseHandler.__init__r   r,   r   c                 C   ó   dS ©úDecompress the given data.Nr   r-   r   r   r   Údecompress_syncÄ   r$   z(DecompressionBaseHandler.decompress_syncc                 Ã   sD   �| j durt|ƒ| j krt ¡  | j| j||¡I dH S |  ||¡S rs   )rp   ÚlenÚasyncioÚget_event_loopÚrun_in_executorro   ru   r-   r   r   r   r.   Ê   s   €

ÿz#DecompressionBaseHandler.decompressc                 C   rr   )z7Return True if more output is available by passing b"".Nr   r1   r   r   r   Údata_available×   r$   z'DecompressionBaseHandler.data_available)r   r   r   ÚMAX_SYNC_CHUNK_SIZEr   r*   rM   r   ÚZLIB_MAX_LENGTH_UNLIMITEDr   r)   ru   r.   r6   r7   rz   r   r   r   r   rl   º   s8    ýþ
ý	ÿÿÿþÿÿÿ
þrl   c                   @   sZ   e Zd ZU dZeed< eed< dZedB ed< e	defdd„ƒZ
d	ed
edefdd„ZdS )ÚConcatDecompressionHandleray  Handler for a codec whose streams may concatenate independent members.

    Concatenated gzip/deflate members and multi-frame zstd
    (https://datatracker.ietf.org/doc/html/rfc8878#section-3.1.1) decode the
    same way: a decompressor handles one member, then flags eof and leaves the
    rest of the input in unused_data, so every member after it needs a fresh
    one.
    Ú
_unlimitedÚ_decompressorNÚ_pending_unused_datar   c                 C   rr   )z*Return a decompressor for the next member.Nr   r1   r   r   r   Ú_new_decompressorí   r$   z,ConcatDecompressionHandler._new_decompressorÚfirstr,   c                 C   s  t | jjƒ}|g}t|ƒ}d}t}|}d}	|t|ƒk rŠ| jjr6|	d7 }	|	tkr/tdt› d�ƒ‚|  ¡ | _t}|| j	krM|| }|dkrMt
||d… ƒ| _n=t|| t|ƒƒ}
| j |||
… |¡}|rn| |¡ |t|ƒ7 }| jjr{|
t| jjƒ }n	|
}t|d tƒ}|t|ƒk sd |¡S )z9Decode the members following the one ``first`` came from.r   é   z Compressed stream has more than z membersNé   ó    )Ú
memoryviewr   r5   rv   ÚMEMBER_WINDOW_MINr2   ÚMAX_DECOMPRESS_MEMBERSr   r�   r~   r)   r€   Úminr.   ÚappendÚMEMBER_WINDOW_MAXÚjoin)r"   r‚   r,   Ú	remainingÚpartsÚproducedÚposÚwindowÚbudgetÚmembersÚendÚchunkr   r   r   Ú_decompress_membersñ   sD   ÿÿ


à
"z.ConcatDecompressionHandler._decompress_members)r   r   r   r   r*   rH   rj   r€   r)   r   r�   r–   r   r   r   r   r}   Ý   s   
 
r}   c                   @   s–   e Zd ZddddddefdedB dededB dedB dedB dedB d	edB fd
d„Zde	de
fdd„Zde	de
fdd„ZddedB de
fdd„ZdS )ÚZLibCompressorNFrd   re   r>   r@   rB   rm   rn   c           	      C   sv   || _ || _|d u rt||ƒn|| _ttjƒ| _i }| j|d< |d ur'||d< |d ur/||d< | jjdi |¤Ž| _d S )Nr@   rB   r>   r   )	ro   rp   rh   Ú_moderK   ra   rL   rD   Ú_compressor)	r"   rd   re   r>   r@   rB   rm   rn   rV   r   r   r   rM   !  s   

ÿý
zZLibCompressor.__init__r   r   c                 C   s   | j  |¡S r    )r™   r#   r!   r   r   r   Úcompress_sync<  r_   zZLibCompressor.compress_syncc                 Ã   sF   �| j duot|ƒ| j k}|rt ¡  | j| jj|¡I dH S |  |¡S )a  Compress the data and returned the compressed bytes.

        Note that flush() must be called after the last call to compress()

        If the data size is large than the max_sync_chunk_size, the compression
        will be done in the executor. Otherwise, the compression will be done
        in the event loop.

        **WARNING: This method is NOT cancellation-safe when used with flush().**
        If this operation is cancelled, the compressor state may be corrupted.
        The connection MUST be closed after cancellation to avoid data corruption
        in subsequent compress operations.

        For cancellation-safe compression (e.g., WebSocket), the caller MUST wrap
        compress() + flush() + send operations in a shield and lock to ensure atomicity.
        N)	rp   rv   rw   Úget_running_loopry   ro   r™   r#   rš   )r"   r   Úshould_use_executorr   r   r   r#   ?  s   €
þ
ÿ
zZLibCompressor.compressr%   c                 C   s   | j  |dur
|¡S | jj¡S )a	  Flush the compressor synchronously.

        **WARNING: This method is NOT cancellation-safe when called after compress().**
        The flush() operation accesses shared compressor state. If compress() was
        cancelled, calling flush() may result in corrupted data. The connection MUST
        be closed after compress() cancellation.

        For cancellation-safe compression (e.g., WebSocket), the caller MUST wrap
        compress() + flush() + send operations in a shield and lock to ensure atomicity.
        N)r™   r'   rL   r=   r&   r   r   r   r'   [  s
   
ÿÿzZLibCompressor.flushr    )r   r   r   r{   r`   r7   r*   r   rM   r   r)   rš   r#   r'   r   r   r   r   r—      s4    øþýüûúù
ør—   c                
       s¨   e Zd ZeZdddefdedB dededB de	dB f‡ fdd„Z
d	efd
d„Zefdede	d	efdd„Zdde	d	efdd„Zed	efdd„ƒZed	efdd„ƒZ‡  ZS )ÚZLibDecompressorNFrd   re   rm   rn   c                    s<   t ƒ j||d� t||ƒ| _ttjƒ| _|  ¡ | _d| _	d S )N©rm   rn   F)
ÚsuperrM   rh   r˜   rK   ra   rL   r�   r   Ú_last_empty)r"   rd   re   rm   rn   ©Ú	__class__r   r   rM   n  s
   

zZLibDecompressor.__init__r   c                 C   s   | j j| jd�S )N)r@   )rL   rE   r˜   r1   r   r   r   r�   {  s   z"ZLibDecompressor._new_decompressorr   r,   c                 C   s~   | j d ur| j t|ƒ }d | _ | j | jj| |¡}| jjr(| jjr(|  ||¡}|dk| _| jjr=| j	| j
jkr=|  ¡ | _|S ©Nr…   )r€   r)   r   r.   r4   r2   r5   r–   r    r˜   rL   r9   r�   ©r"   r   r,   Úresultr   r   r   ru   ~  s   
ÿ

z ZLibDecompressor.decompress_syncr   r/   c                 C   s   |dkr
| j  |¡S | j  ¡ S )Nr   )r   r'   r0   r   r   r   r'   ˜  s
   
ÿÿýzZLibDecompressor.flushc                 C   s   t | jjƒp| j p| jd uS r    )r7   r   r4   r    r€   r1   r   r   r   rz   Ÿ  s
   ÿýzZLibDecompressor.data_availablec                 C   rS   r    )r   r2   r1   r   r   r   r2   §  rT   zZLibDecompressor.eof)r   )r   r   r   r|   r~   r{   r`   r7   r   r*   rM   r+   r�   r   r)   ru   r'   r6   rz   r2   Ú__classcell__r   r   r¡   r   r�   k  s:    ûþýüûÿÿÿ
þr�   c                       sr   e Zd ZdefdedB dedB ddf‡ fdd„Zefdedede	fd	d
„Z
de	fdd„Zedefdd„ƒZ‡  ZS )ÚBrotliDecompressorNrm   rn   r   c                    s0   t stdƒ‚t ¡ | _d| _tƒ j||d� dS )z)Decompress data using the Brotli library.zIThe brotli decompression is not available. Please install `Brotli` moduleFrž   N)Ú
HAS_BROTLIÚRuntimeErrorÚbrotliÚDecompressorÚ_objr    rŸ   rM   rq   r¡   r   r   rM   °  s   ÿ
zBrotliDecompressor.__init__r   r,   c                 C   s|   t | jdƒr|tkrtt| j |¡ƒ}n#tt| j ||¡ƒ}n|tkr-tt| j |¡ƒ}n
tt| j ||¡ƒ}|dk| _|S )rt   r.   r…   )Úhasattrr¬   r|   r   r)   r.   Úprocessr    r¤   r   r   r   ru   ¿  s   
z"BrotliDecompressor.decompress_syncc                 C   s    t | jdƒrtt| j ¡ ƒS dS )zFlush the decompressor.r'   r…   )r­   r¬   r   r)   r'   r1   r   r   r   r'   Ñ  s   zBrotliDecompressor.flushc                 C   s   | j  ¡  o	| j S r    )r¬   Úis_finishedr    r1   r   r   r   rz   ×  s   z!BrotliDecompressor.data_available)r   r   r   r{   r   r*   rM   r|   r   r)   ru   r'   r6   r7   rz   r¦   r   r   r¡   r   r§   ¬  s*    ýþýüÿÿÿ
þr§   c                       s€   e Zd ZeZdefdedB dedB ddf‡ fdd„Zddd	„Z	e
fd
ededefdd„Zdefdd„Zedefdd„ƒZ‡  ZS )ÚZSTDDecompressorNrm   rn   r   c                    s*   t stdƒ‚tƒ j||d� |  ¡ | _d S )NzOThe zstd decompression is not available. Please install `backports.zstd` modulerž   )ÚHAS_ZSTDr©   rŸ   rM   r�   r   rq   r¡   r   r   rM   ß  s   ÿzZSTDDecompressor.__init__r   c                 C   s   t ƒ S r    r   r1   r   r   r   r�   ì  s   z"ZSTDDecompressor._new_decompressorr   r,   c                 C   sj   |t krtn|}| jd ur| j| }d | _| j ||¡}| jjr*| jjr*|  ||¡}| jjr3|  ¡ | _|S r    )	r|   ÚZSTD_MAX_LENGTH_UNLIMITEDr€   r   r.   r2   r5   r–   r�   )r"   r   r,   Úzstd_max_lengthr¥   r   r   r   ru   ï  s   ÿý


z ZSTDDecompressor.decompress_syncc                 C   rr   r£   r   r1   r   r   r   r'   
  r3   zZSTDDecompressor.flushc                 C   s   | j j o	| j j p| jd uS r    )r   Úneeds_inputr2   r€   r1   r   r   r   rz     s   ÿþzZSTDDecompressor.data_available)r   r   )r   r   r   r²   r~   r{   r   r*   rM   r�   r|   r   r)   ru   r'   r6   r7   rz   r¦   r   r   r¡   r   r°   Ü  s.    ýþýü
ÿÿÿ
þr°   r   )NF):rw   ÚsysÚzlibÚabcr   r   Úconcurrent.futuresr   Útypingr   r   r   r   r	   r
   r   Úversion_infoÚcollections.abcr   r   r)   Ú	bytearrayÚ
brotlicffirª   ÚImportErrorr¨   Úcompression.zstdr   Úbackports.zstdr±   r{   r|   r²   r‡   r‹   rˆ   Ú
ValueErrorr   r   r+   r8   rI   rK   ra   rH   rc   r`   r7   r*   rh   ri   rj   rl   r}   r—   r�   r§   r°   r   r   r   r   Ú<module>   sv   
 $
ÿÿ
ÿ-þÿþ
ý

#CKA0