
    T,3ikL                     ^   S SK r S SKrSSKJrJr   " S S\5      r\r " S S\R                  5      r	\	" \R                  5      r\	R                  r\	R                  r\	R                   rS r\\R&                  \R(                  S 4S	 jrS
 rS rS rS rS r " S S\5      r " S S\5      rg)    N   )ffilibc                       \ rS rSrSrSrg)error   z{
Raised whenever an error is encountered with compressing or decompressing
data using brotlicffi.

.. versionadded:: 0.5.1
 N)__name__
__module____qualname____firstlineno____doc____static_attributes__r	       /lib/python3.13/site-packages/brotlicffi/_api.pyr   r      s     	r   r   c                   `    \ rS rSrSr\R                  r\R                  r	\R                  rSrg)BrotliEncoderMode   zD
Compression modes for the Brotli encoder.

.. versionadded:: 0.5.0
r	   N)r
   r   r   r   r   r   BROTLI_MODE_GENERICGENERICBROTLI_MODE_TEXTTEXTBROTLI_MODE_FONTFONTr   r	   r   r   r   r      s.     %%G D Dr   r   c                 \    [        5       nUR                  U 5      n UR                  5         U $ )zo
Decompress a complete Brotli-compressed string.

:param data: A bytestring containing Brotli-compressed data.
)Decompressor
decompressfinish)datads     r   r   r   R   s'     	A<<DHHJKr   c                 .   [        UUUUS9nUR                  U [        R                  5      n[        R                  " UR
                  5      [        R                  :X  d   e[        R                  " UR
                  5      [        R                  :X  d   eU$ )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   compressr0   ^   s    P 	J !**41L1LMO&&z':':;sNNN&&z':':;s?O?OOOr   c                 T     [        U 5      n g! [         a    [        SU -  5      ef = f)z"
Validate that the mode is valid.
z%s is not a valid encoder modeN)r   
ValueErrorr   vals    r   _validate_moder5      s1    <$ <4s:;;<s    'c                 >    SU s=::  a  S::  d  O  [        SU -  5      eg)z-
Validate that the quality setting is valid.
r      z3%d is not a valid quality, must be between 0 and 11Nr   r3   s    r   _validate_qualityr9      s)     NNACG
 	
 r   c                 >    SU s=::  a  S::  d  O  [        SU -  5      eg)z+
Validate that the lgwin setting is valid.

      z2%d is not a valid lgwin, must be between 10 and 24Nr8   r3   s    r   _validate_lgwinr=      s%     #OOH3NOO r   c                 L    U S:w  a  SU s=::  a  S::  d  O  [        SU -  5      egg)z-
Validate that the lgblock setting is valid.
r      r<   z@%d is not a valid lgblock, must be either 0 or between 16 and 24Nr8   r3   s    r   _validate_lgblockr@      s4     	q2??N
 	
 +r   c                    [         R                  " XU5      nU[         R                  :X  a  [        U5        OkU[         R                  :X  a  [        U5        OKU[         R                  :X  a  [        U5        O+U[         R                  :X  a  [        U5        O[        S5      eU[         R                  :w  a  [        SX#4-  5      eg)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_MODEr5   BROTLI_PARAM_QUALITYr9   BROTLI_PARAM_LGWINr=   BROTLI_PARAM_LGBLOCKr@   RuntimeErrorr+   r   )encoder	parameterparameter_namer4   rcs        r   _set_parameterrL      s     
	&	&w3	?BC)))s	c..	.#	c,,	,	c..	.#233
 
S__,/DD
 	
 r   c                   v    \ rS rSrSrSrSr\\R                  \R                  S4S jrS rS r\rS rS	 rS
rg)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                    [         R                  " [        R                  [        R                  [        R                  5      nU(       d  [	        S5      e[        R
                  " U[         R                  5      n[        U[         R                  SU5        [        U[         R                  SU5        [        U[         R                  SU5        [        U[         R                  SU5        XPl        g )Nz"Unable to allocate Brotli encoder!r"   r#   r$   r%   )r   BrotliEncoderCreateInstancer   NULLrG   gcBrotliEncoderDestroyInstancerL   rC   rD   rE   rF   r*   )selfr"   r#   r$   r%   encs         r   __init__Compressor.__init__   s    
 --HHchh
 CDDffS#::; 	sC1164@sC44iIsC22GUCsC44iIr   c           	         [        [        R                  " [        U5      [        U5      S-	  -   S-   5      5      n[        R
                  " S5      nX4S'   [        R
                  " SUS   5      n[        R
                  " SU5      n[        R
                  " S[        U5      5      n[        R
                  " SU5      n[        R
                  " SU5      n	[        R                  " U R                  UUU	UU[        R                  5      n
U
[        R                  :w  a  [        S5      eUS   (       a   eX4S   -
  n[        R                  " X[5      S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.
   i (  size_t *r   
uint8_t []
uint8_t **z#Error encountered compressing data.N)intmathceillenr   newr   BrotliEncoderCompressStreamr*   rQ   r+   r   buffer)rT   r   	operationoriginal_output_sizeavailable_outoutput_bufferptr_to_output_buffer
input_sizeinput_bufferptr_to_input_bufferrK   size_of_outputs               r   r'   Compressor._compress  s     #IIc$i3t9>2U:; 
 
+/amA.>?"ww|]CWWZT3
ww|T2!gglLA,,MM HH
  =>>a=  -a0@@zz-8;;r   c                 B    U R                  U[        R                  5      $ )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)rT   r   s     r   r0   Compressor.compress0  s     ~~dC$@$@AAr   c                    U R                  S[        R                  5      /n[        R                  " U R                  5      [        R
                  :X  ac  UR                  U R                  S[        R                  5      5        [        R                  " U R                  5      [        R
                  :X  a  Mc  SR                  U5      $ )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.
r   )r'   r   BROTLI_OPERATION_FLUSHr,   r*   r+   appendjoinrT   chunkss     r   flushCompressor.flush=  s     ..c&@&@AB,,T]];sNMM$..c.H.HIJ ,,T]];sN xxr   c                 R   / n[         R                  " U R                  5      [         R                  :X  ac  UR	                  U R                  S[         R                  5      5        [         R                  " U R                  5      [         R                  :X  a  Mc  SR                  U5      $ )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.
r   )r   r)   r*   r-   rs   r'   r(   rt   ru   s     r   r   Compressor.finishJ  ss     ))$--8C<L<LLMM$..c.I.IJK ))$--8C<L<LL xxr   )r*   )r
   r   r   r   r   _dictionary_dictionary_sizeDEFAULT_MODEr   BROTLI_DEFAULT_QUALITYBROTLI_DEFAULT_WINDOWrV   r'   r0   processrw   r   r   r	   r   r   r&   r&      sP    8 K #3300	*"<H	B G 
 r   r&   c                   d    \ rS rSrSrSrSrSrSS jr\	S 5       r
SS jr\rS rS rS	 rS
 rSrg)r   iW  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``
Nc                    [         R                  " [        R                  [        R                  [        R                  5      n[        R                  " U[         R
                  5      U l        SU l        U(       ac  [        R                  " SU5      U l	        [        U5      U l        [         R                  " U R                  U R                  U R                  5        g g )Nr   r[   )r   BrotliDecoderCreateInstancer   rQ   rR   BrotliDecoderDestroyInstance_decoder_unconsumed_datara   r{   r`   r|    BrotliDecoderSetCustomDictionary)rT   
dictionarydecs      r   rV   Decompressor.__init__l  s    --chh#((KsC$D$DE #"ww|Z@D$'
OD!00%%   r   c                 J    Ub  X-
  $ U S:X  a  S[        US-   S5      -  $ SU -  $ )Nr   r      r<      )mininput_data_lenoutput_buffer_limit
chunks_len
chunks_nums       r   _calculate_buffer_size#Decompressor._calculate_buffer_sizez  s@     *&33q  JOR000 ~%%r   c                    U R                   (       a  U(       a  [        S5      eUb  US::  a  gU R                   (       a  U R                   nSU l         OUn/ nSn[        R                  " S[	        U5      5      n[        R                  " SU5      n[        R                  " SU5      n U R                  [	        U5      UU[	        U5      S9n	[        R                  " SU	5      n
[        R                  " SU	5      n[        R                  " SU5      n[        R                  " U R                  UUU
U[        R                  5      nU[        R                  :X  aX  [        R                  " U R                  5      n[        R                  " U5      n[        S	[        R                  " U5      -  5      e[        R                  " XU
S   -
  5      SS nUR                  U5        U[	        U5      -  nUS   S:  a'  [        R                  " US   US   5      SS nUU l         Ub  XR:  a  ONU[        R                   :X  a  US   S:X  d   eO.U[        R"                  :X  a  OU[        R$                  :X  d   eGM  SR'                  U5      $ )
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.
zObrotli: decoder process called with data when 'can_accept_more_data()' is FalseNr   r   rZ   z	uint8_t[]r\   r   s   Decompression error: %s)r   r   r   ra   r`   r   r   BrotliDecoderDecompressStreamr   rQ   BROTLI_DECODER_RESULT_ERRORBrotliDecoderGetErrorCodeBrotliDecoderErrorStringstringrc   rs   &BROTLI_DECODER_RESULT_NEEDS_MORE_INPUTBROTLI_DECODER_RESULT_SUCCESS'BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUTrt   )rT   r   r   
input_datarv   r   available_in	in_buffernext_inbuffer_sizerf   
out_buffernext_outrK   
error_codeerror_messagechunkremaining_inputs                     r   r   Decompressor.decompress  sM   "   T4  */Ba/G   ..J$'D!J
wwz3z?;GGK4	'',	255":$7%v;	 6 K  GGJ<Mk:Jww|Z8H224==3?3:3@3;3688=B S444 ::4==I
 # < <Z H.M1JJ 
 JJzq9I+IJ1MEMM% #e*$J A""%**WQZa"I!"L(7% $/5S???#A!+++s888 SHHHHHg j xxr   c                     g)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	   rT   s    r   rw   Decompressor.flush  s     r   c                     [         R                  " U R                  5      [         R                  :X  d   eU R	                  5       (       d  [        S5      eg)a?  
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   Decompressor.finish  sG     **4==9S=M=MM	
M!!LMMr   c                 d    [         R                  " U R                  5      [         R                  :H  $ )zO
Returns ``True`` if the decompression stream
is complete, ``False`` otherwise
)r   BrotliDecoderIsFinishedr   r+   r   s    r   r   Decompressor.is_finished  s"    
 **4==9S__LLr   c                     [        U R                  5      S:  a  g[        R                  " U R                  5      [        R
                  :X  a  gg)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``
r   FT)r`   r   r   r   r   r+   r   s    r   can_accept_more_data!Decompressor.can_accept_more_data  s;    * t$$%)))$--8COOKr   )r   r{   r|   r   )r   )N)r
   r   r   r   r   r{   r|   r   rV   staticmethodr   r   r   rw   r   r   r   r   r	   r   r   r   r   W  sV      K & & _ B G"Mr   r   )r^   enum_brotlicffir   r   	Exceptionr   ErrorIntEnumr   BROTLI_DEFAULT_MODEr}   r   MODE_GENERICr   	MODE_TEXTr   	MODE_FONTr   r~   r   r0   r5   r9   r=   r@   rL   objectr&   r   r	   r   r   <module>r      s      !	I 	 	   ( !!8!89 !(( ""	 ""		 //,,	3l<
P

8}  } @V6 Vr   