
    &jNS                     >   d dl Z d dlZd dlZddlmZmZ  G d de          ZeZ G d dej	                  Z
 e
ej                  Ze
j        Ze
j        Ze
j        Zd Zeej        ej        d fd	Zd
 Zd Zd Zd Zd Z G d de          Z G d de          ZdS )    N   )ffilibc                       e Zd ZdZdS )errorz
    Raised whenever an error is encountered with compressing or decompressing
    data using brotlicffi.

    .. versionadded:: 0.5.1
    N)__name__
__module____qualname____doc__     P/usr/local/lib/hermes-agent/venv/lib/python3.11/site-packages/brotlicffi/_api.pyr   r   	   s          	Dr   r   c                   <    e Zd ZdZej        Zej        Zej	        Z
dS )BrotliEncoderModezP
    Compression modes for the Brotli encoder.

    .. versionadded:: 0.5.0
    N)r   r	   r
   r   r   BROTLI_MODE_GENERICGENERICBROTLI_MODE_TEXTTEXTBROTLI_MODE_FONTFONTr   r   r   r   r      s6          %G D DDDr   r   c                 t    t                      }|                    |           } |                                 | S )z{
    Decompress a complete Brotli-compressed string.

    :param data: A bytestring containing Brotli-compressed data.
    )Decompressor
decompressfinish)datads     r   r   r   S   s0     	A<<DHHJJJKr   c                    t          ||||          }|                    | t          j                  }t          j        |j                  t          j        k    sJ t          j        |j                  t          j        k    sJ |S )a  
    Compress a string using Brotli.

    .. versionchanged:: 0.5.0
       Added ``mode``, ``quality``, `lgwin``, ``lgblock``, and ``dictionary``
       parameters.

    :param data: A bytestring containing the data to compress.
    :type data: ``bytes``

    :param mode: The encoder mode.
    :type mode: :class:`BrotliEncoderMode` or ``int``

    :param quality: Controls the compression-speed vs compression-density
        tradeoffs. The higher the quality, the slower the compression. The
        range of this value is 0 to 11.
    :type quality: ``int``

    :param lgwin: The base-2 logarithm of the sliding window size. The range of
        this value is 10 to 24.
    :type lgwin: ``int``

    :param lgblock: The base-2 logarithm of the maximum input block size. The
        range of this value is 16 to 24. If set to 0, the value will be set
        based on ``quality``.
    :type lgblock: ``int``

    :returns: The compressed bytestring.
    :rtype: ``bytes``
    )modequalitylgwinlgblock)	
Compressor	_compressr   BROTLI_OPERATION_FINISHBrotliEncoderIsFinished_encoderBROTLI_TRUEBrotliEncoderHasMoreOutputBROTLI_FALSE)r   r   r   r    r!   
compressorcompressed_datas          r   compressr,   _   s    P 	  J !**41LMMO&z':;;sNNNN&z':;;s?OOOOOr   c                 f    	 t          |           } dS # t          $ r t          d| z            w xY w)z*
    Validate that the mode is valid.
    z%s is not a valid encoder modeN)r   
ValueErrorr   vals    r   _validate_moder1      sG    <$$ < < <4s:;;;<s    0c                 H    d| cxk    rdk    sn t          d| z            dS )z5
    Validate that the quality setting is valid.
    r      z3%d is not a valid quality, must be between 0 and 11Nr   r/   s    r   _validate_qualityr5      s<     NNNNNNNNACG
 
 	
 Nr   c                 H    d| cxk    rdk    sn t          d| z            dS )z3
    Validate that the lgwin setting is valid.
    
      z2%d is not a valid lgwin, must be between 10 and 24Nr4   r/   s    r   _validate_lgwinr9      s6     #OOOOOOOOH3NOOO Or   c                 X    | dk    r!d| cxk    rdk    sn t          d| z            dS dS )z5
    Validate that the lgblock setting is valid.
    r      r8   z@%d is not a valid lgblock, must be either 0 or between 16 and 24Nr4   r/   s    r   _validate_lgblockr<      sK     	q2????????N
 
 	
 	??r   c                    t          j        | ||          }|t           j        k    rt          |           no|t           j        k    rt          |           nO|t           j        k    rt          |           n/|t           j        k    rt          |           nt          d          |t           j        k    rt          d||fz            dS )z
    This helper function sets a specific Brotli encoder parameter, checking
    the return code and raising :class:`Error <brotlicffi.Error>` if it is
    invalid.
    zUnexpected parameter!zError setting parameter %s: %dN)r   BrotliEncoderSetParameterBROTLI_PARAM_MODEr1   BROTLI_PARAM_QUALITYr5   BROTLI_PARAM_LGWINr9   BROTLI_PARAM_LGBLOCKr<   RuntimeErrorr'   r   )encoder	parameterparameter_namer0   rcs        r   _set_parameterrH      s     
	&w	3	?	?BC)))s	c.	.	.#	c,	,	,	c.	.	.#2333
 
S_,/DD
 
 	
 r   c                   Z    e Zd ZdZdZdZeej        ej	        dfdZ
d Zd ZeZd Zd ZdS )	r"   a  
    An object that allows for streaming compression of data using the Brotli
    compression algorithm.

    .. versionadded:: 0.5.0

    :param mode: The encoder mode.
    :type mode: :class:`BrotliEncoderMode` or ``int``

    :param quality: Controls the compression-speed vs compression-density
        tradeoffs. The higher the quality, the slower the compression. The
        range of this value is 0 to 11.
    :type quality: ``int``

    :param lgwin: The base-2 logarithm of the sliding window size. The range of
        this value is 10 to 24.
    :type lgwin: ``int``

    :param lgblock: The base-2 logarithm of the maximum input block size. The
        range of this value is 16 to 24. If set to 0, the value will be set
        based on ``quality``.
    :type lgblock: ``int``

    :param dictionary: A pre-set dictionary for LZ77. Please use this with
        caution: if a dictionary is used for compression, the same dictionary
        **must** be used for decompression!
    :type dictionary: ``bytes``
    Nr   c                    t          j                    | _        t          j        t
          j        t
          j        t
          j                  }|st          d          t          j        |t          j	                  }t          |t          j        d|           t          |t          j        d|           t          |t          j        d|           t          |t          j        d|           || _        d S )Nz"Unable to allocate Brotli encoder!r   r   r    r!   )	threadingRLocklockr   BrotliEncoderCreateInstancer   NULLrC   gcBrotliEncoderDestroyInstancerH   r?   r@   rA   rB   r&   )selfr   r   r    r!   encs         r   __init__zCompressor.__init__   s    
 O%%	-Hch
 
  	ECDDDfS#:;; 	sC164@@@sC4iIIIsC2GUCCCsC4iIIIr   c           	      n   | j                             d          st          d          	 t          t	          j        t          |          t          |          dz	  z   dz                       }t          j        d          }||d<   t          j        d|d                   }t          j        d	|          }t          j        dt          |                    }t          j        d|          }t          j        d	|          }	t          j
        | j        |||	||t          j                  }
| j                                          n# | j                                          w xY w|
t          j        k    rt          d
          |d         rJ ||d         z
  }t          j        ||          dd         S )z
        This private method compresses some data in a given mode. This is used
        because almost all of the code uses the exact same setup. It wouldn't
        have to, but it doesn't hurt at all.
        Fblocking6Concurrently sharing Compressor objects is not allowed   i (  size_t *r   
uint8_t []
uint8_t **z#Error encountered compressing data.N)rM   acquirer   intmathceillenr   newr   BrotliEncoderCompressStreamr&   rO   releaser'   buffer)rR   r   	operationoriginal_output_sizeavailable_outoutput_bufferptr_to_output_buffer
input_sizeinput_bufferptr_to_input_bufferrG   size_of_outputs               r   r#   zCompressor._compress  s    y  % 00 	JHJ J J	  $'	#d))s4yyA~6>??$ $   GJ//M3M!GL-2BCCM#&7<#G#G SYY77J7<66L"%',"E"E0#$ B IDI  =>>>a=   -a0@@z-88;;s   DE E#c                 B    |                      |t          j                  S )a>  
        Incrementally compress more data.

        :param data: A bytestring containing data to compress.
        :returns: A bytestring containing some compressed data. May return the
            empty bytestring if not enough data has been inserted into the
            compressor to create the output yet.
        )r#   r   BROTLI_OPERATION_PROCESS)rR   r   s     r   r,   zCompressor.compress8  s     ~~dC$@AAAr   c                 6   | j                             d          st          d          	 |                     dt          j                  g}t	          j        | j                  t          j        k    rZ|	                    |                     dt          j                             t	          j        | j                  t          j        k    Z| j         
                                 n# | j         
                                 w xY wd                    |          S )z
        Flush the compressor. This will emit the remaining output data, but
        will not destroy the compressor. It can be used, for example, to ensure
        that given chunks of content will decompress immediately.
        FrV   rX   r   )rM   r]   r   r#   r   BROTLI_OPERATION_FLUSHr(   r&   r'   appendrd   joinrR   chunkss     r   flushzCompressor.flushE  s     y  % 00 	JHJ J J	 nnS#*DEEFF24=AAO$ $dnnS#2LMMNNN 24=AAO$ $ IDIxxs   B"C( (Dc                    | j                             d          st          d          	 g }t          j        | j                  t          j        k    rZ|                    |                     dt          j	                             t          j        | j                  t          j        k    Z| j         
                                 n# | j         
                                 w xY wd                    |          S )z
        Finish the compressor. This will emit the remaining output data and
        transition the compressor to a completed state. The compressor cannot
        be used again after this point, and must be replaced.
        FrV   rX   r   )rM   r]   r   r   r%   r&   r)   rs   r#   r$   rd   rt   ru   s     r   r   zCompressor.finishX  s     y  % 00 	JHJ J J	 F/>>$% %dnnS#2MNNOOO />>$% % IDIxxs   BC	 	C$)r   r	   r
   r   _dictionary_dictionary_sizeDEFAULT_MODEr   BROTLI_DEFAULT_QUALITYBROTLI_DEFAULT_WINDOWrT   r#   r,   processrw   r   r   r   r   r"   r"      s         8 K #30	   ,(< (< (<T	B 	B 	B G     &         r   r"   c                   f    e Zd ZdZdZdZdZddZed             Z	ddZ
d Ze
Zd Zd	 Zd
 Zd ZdS )r   a-  
    An object that allows for streaming decompression of Brotli-compressed
    data.

    .. versionchanged:: 0.5.0
       Added ``dictionary`` parameter.

    .. versionchanged:: 1.2.0
       Added ``can_accept_more_data()`` method and optional
       ``output_buffer_limit`` parameter to ``process()``/``decompress()``.

    :param dictionary: A pre-set dictionary for LZ77. Please use this with
        caution: if a dictionary is used for compression, the same dictionary
        **must** be used for decompression!
    :type dictionary: ``bytes``
    Nr   c                    t          j                    | _        t          j        t
          j        t
          j        t
          j                  }t          j        |t          j                  | _	        d| _
        |rUt          j        d|          | _        t          |          | _        t          j        | j	        | j        | j                   d S d S )Nr   r[   )rK   LockrM   r   BrotliDecoderCreateInstancer   rO   rP   BrotliDecoderDestroyInstance_decoder_unconsumed_datarb   ry   ra   rz    BrotliDecoderSetCustomDictionary)rR   
dictionarydecs      r   rT   zDecompressor.__init__  s    N$$	-ch#(KKsC$DEE # 	"w|Z@@D$'
OOD!0%     	 	r   c                 R    |||z
  S | dk    rdt          |dz   d          z  S d| z  S )Nr   r      r8      )mininput_data_lenoutput_buffer_limit
chunks_len
chunks_nums       r   _calculate_buffer_sizez#Decompressor._calculate_buffer_size  sH     *&33q   JOR0000 ~%%r   c                    | j                             d          st          d          	 |                     ||          }| j                                          n# | j                                          w xY wd                    |          S )a  
        Decompress part of a complete Brotli-compressed string.

        .. versionchanged:: 1.2.0
           Added ``output_buffer_limit`` parameter.

        :param data: A bytestring containing Brotli-compressed data.
        :param output_buffer_limit: Optional maximum size for the output
            buffer. If set, the output buffer will not grow once its size
            equals or exceeds this value. If the limit is reached, further
            calls to process (potentially with empty input) will continue to
            yield more data. Following process() calls must only be called
            with empty input until can_accept_more_data() returns True.
        :type output_buffer_limit: ``int`` or ``None``
        :returns: A bytestring containing the decompressed data.
        FrV   :Concurrently sharing Decompressor instances is not allowedr   )rM   r]   r   _decompressrd   rt   )rR   r   r   rv   s       r   r   zDecompressor.decompress  s    " y  % 00 	NLN N N	 %%d,?@@FIDIxxs   A A7c                    | j         r|rt          d          ||dk    rdS | j         r| j         }d| _         n|}g }d}t          j        dt	          |                    }t          j        d|          }t          j        d|          }	 |                     t	          |          ||t	          |                    }	t          j        d|	          }
t          j        d|	          }t          j        d|          }t          j        | j        |||
|t          j	                  }|t          j
        k    rQt          j        | j                  }t          j        |          }t          d	t          j        |          z            t          j        ||	|
d         z
            d d          }|                    |           |t	          |          z  }|d         dk    r0t          j        |d         |d                   d d          }|| _         |||k    rnD|t          j        k    r|d         dk    sJ n%|t          j        k    rn|t          j        k    sJ |S )
NzObrotli: decoder process called with data when 'can_accept_more_data()' is Falser   r   rZ   z	uint8_t[]r\   Tr   s   Decompression error: %s)r   r   r   rb   ra   r   r   BrotliDecoderDecompressStreamr   rO   BROTLI_DECODER_RESULT_ERRORBrotliDecoderGetErrorCodeBrotliDecoderErrorStringstringre   rs   &BROTLI_DECODER_RESULT_NEEDS_MORE_INPUTBROTLI_DECODER_RESULT_SUCCESS'BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT)rR   r   r   
input_datarv   r   available_in	in_buffernext_inbuffer_sizerh   
out_buffernext_outrG   
error_codeerror_messagechunkremaining_inputs                     r   r   zDecompressor._decompress  s     	T 	4   */Ba/G/G3   	.J$'D!!J
wz3z??;;GK44	',	223	I55":$7%v;;	 6  K  GJ<<Mk::Jw|Z88H24=3?3:3@3;368= =B S444 :4=II
 # <Z H H.M1J1JJ  
 Jz;q9I+IJJ111MEMM%   #e**$J A"""%*WQZa"I"I!!!"L(7% $/"555S???#A!++++s888 SHHHHHg3	Ih r   c                     dS )a?  
        Complete the decompression, return whatever data is remaining to be
        decompressed.

        .. deprecated:: 0.4.0

            This method is no longer required, as decompress() will now
            decompress eagerly.

        :returns: A bytestring containing the remaining decompressed data.
        r   r   rR   s    r   rw   zDecompressor.flush  s	     sr   c                     t          j        | j                  t           j        k    sJ |                                 st          d          dS )ao  
        Finish the decompressor. As the decompressor decompresses eagerly, this
        will never actually emit any data. However, it will potentially throw
        errors if a truncated or damaged data stream has been used.

        Note that, once this method is called, the decompressor is no longer
        safe for further use and must be thrown away.
        z2Decompression error: incomplete compressed stream.r   )r   BrotliDecoderHasMoreOutputr   r)   is_finishedr   r   s    r   r   zDecompressor.finish  sN     *4=99S=MMMMM!! 	NLMMMsr   c                    | j                             d          st          d          	 t          j        | j                  t          j        k    }| j                                          n# | j                                          w xY w|S )zg
        Returns ``True`` if the decompression stream
        is complete, ``False`` otherwise
        FrV   r   )rM   r]   r   r   BrotliDecoderIsFinishedr   r'   rd   rR   rets     r   r   zDecompressor.is_finished+  s    
 y  % 00 	NLN N N	 +DM::coM  IDI
s   'A- -Bc                 V   | j                             d          st          d          	 d}t          | j                  dk    rd}t          j        | j                  t
          j        k    rd}| j         	                                 n# | j         	                                 w xY w|S )a  
        Checks if the decompressor can accept more compressed data.

        If the ``output_buffer_limit`` parameter was used with
        ``decompress()`` or ``process()``, this method should be checked to
        determine if the decompressor is ready to accept new input. When the
        output buffer limit is reached, the decompressor may still have
        unconsumed input data or internal buffered output, and calling
        ``decompress(b'')`` repeatedly will continue producing output until
        this method returns ``True``.

        .. versionadded:: 1.2.0

        :returns: ``True`` if the decompressor is ready to accept more
            compressed data via ``decompress()`` or ``process()``, ``False``
            if the decompressor needs to output some data via
            ``decompress(b'')``/``process(b'')`` before being provided any
            more compressed data.
        :rtype: ``bool``
        FrV   r   Tr   )
rM   r]   r   ra   r   r   r   r   r'   rd   r   s     r   can_accept_more_dataz!Decompressor.can_accept_more_data:  s    * y  % 00 	NLN N N	 C4())A--/>>! !IDI
s   AB B&)r   )N)r   r	   r
   r   ry   rz   r   rT   staticmethodr   r   r   r~   rw   r   r   r   r   r   r   r   r   k  s           K    & & \&        4N N N` G    "  ! ! ! ! !r   r   )r_   enumrK   _brotlicffir   r   	Exceptionr   ErrorIntEnumr   BROTLI_DEFAULT_MODEr{   r   MODE_GENERICr   	MODE_TEXTr   	MODE_FONTr   r|   r}   r,   r1   r5   r9   r<   rH   objectr"   r   r   r   r   <module>r      s         ! ! ! ! ! ! ! !	 	 	 	 	I 	 	 	 	               ( ! !899 !( "	 "		 	 	 /,	3 3 3 3l< < <
 
 
P P P
 
 

 
 
8P  P  P  P  P  P  P  P fp p p p p6 p p p p pr   