
    Q|g<                     ^   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                   F    \ rS rSrSrSrSrS
S jrS r\r	S r
S rS rS	rg)r   iW  ab  
An object that allows for streaming decompression of Brotli-compressed
data.

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

: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        U(       ac  [        R                  " SU5      U l        [        U5      U l
        [         R                  " U R                  U R                  U R                  5        g g )Nr[   )r   BrotliDecoderCreateInstancer   rQ   rR   BrotliDecoderDestroyInstance_decoderra   r{   r`   r|    BrotliDecoderSetCustomDictionary)rT   
dictionarydecs      r   rV   Decompressor.__init__g  s    --chh#((KsC$D$DE"ww|Z@D$'
OD!00%%   r   c                    / n[         R                  " S[        U5      5      n[         R                  " SU5      n[         R                  " SU5      n S[        U5      -  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
[        R                  :X  a  US   S:X  d   eO.U
[        R                  :X  a  OU
[        R                   :X  d   eGMp  SR#                  U5      $ )	z
Decompress part of a complete Brotli-compressed string.

:param data: A bytestring containing Brotli-compressed data.
:returns: A bytestring containing the decompressed data.
rZ   z	uint8_t[]r\      s   Decompression error: %sr   Nr   )r   ra   r`   r   BrotliDecoderDecompressStreamr   rQ   BROTLI_DECODER_RESULT_ERRORBrotliDecoderGetErrorCodeBrotliDecoderErrorStringr   stringrc   rs   &BROTLI_DECODER_RESULT_NEEDS_MORE_INPUTBROTLI_DECODER_RESULT_SUCCESS'BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUTrt   )rT   r   rv   available_in	in_buffernext_inbuffer_sizerf   
out_buffernext_outrK   
error_codeerror_messagechunks                 r   r   Decompressor.decompresst  s    wwz3t95GGK.	'',	2 c$i-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% S???#A!+++s888 SHHHHHE H 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   )r   r{   r|   )r   )r
   r   r   r   r   r{   r|   rV   r   r   rw   r   r   r   r	   r   r   r   r   W  s6     K1 f 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M6 vMr   