
    vKgS                       S r SSKJrJr  / SQrSSKrSSKrSSKJrJ	r	  \
" 5       r\	R                  " 5       r\	R                  " 5       r\	R                   " 5       r\	R$                  " 5       r\R(                  " SS9r\	R,                  " 5       r\	R0                  rS	r\	R6                  r\	R:                  r\	R>                  \	R@                  \	RB                  4r"\	RF                  r$\	RJ                  r&\	RN                  r(\	RR                  r*\	RV                  r,\	RZ                  r.\	R^                  r0\	Rb                  r2\	Rf                  r4\	Rj                  r6\	Rn                  r8\	Rr                  r:\	Rf                  r;\	Rj                  r<\	Rz                  r>\	R~                  r@\	R                  rB\	R                  rD\	R                  rF\	R                  rH\	R                  rJ\	R                  rL\	R                  rN\	R                  rP\	R                  rR\	R                  rT\	R                  rV\	R                  rX\	R                  rZ\	R                  r\\	R                  r^\	R                  r`\	R                  rbSrcSrdSreSrfS
 rg " S S5      rh " S S5      ri " S S5      rj " S S5      rk " S S\l5      rmS rnS ro " S S\p5      rqS rrS rsS rt " S S\p5      ru " S S\p5      rv " S  S!\p5      rw " S" S#\p5      rx " S$ S%\p5      ry " S& S'\p5      rzS( r{S) r|S* r} " S+ S,\p5      r~          S6S- jr " S. S/\p5      r " S0 S1\p5      r " S2 S3\p5      r " S4 S5\p5      rg)7z=Python interface to the Zstandard (zstd) compression library.    )absolute_importunicode_literals)FBufferSegmentBufferSegmentsBufferWithSegmentsBufferWithSegmentsCollectionZstdCompressionChunkerZstdCompressionDictZstdCompressionObjZstdCompressionParametersZstdCompressionReaderZstdCompressionWriterZstdCompressorZstdDecompressionObjZstdDecompressionReaderZstdDecompressionWriterZstdDecompressor	ZstdErrorFrameParametersbackend_features#estimate_decompression_context_sizeframe_content_sizeframe_header_sizeget_frame_parameterstrain_dictionaryFLUSH_BLOCKFLUSH_FRAMECOMPRESSOBJ_FLUSH_FINISHCOMPRESSOBJ_FLUSH_BLOCKZSTD_VERSIONFRAME_HEADERCONTENTSIZE_UNKNOWNCONTENTSIZE_ERRORMAX_COMPRESSION_LEVEL"COMPRESSION_RECOMMENDED_INPUT_SIZE#COMPRESSION_RECOMMENDED_OUTPUT_SIZE$DECOMPRESSION_RECOMMENDED_INPUT_SIZE%DECOMPRESSION_RECOMMENDED_OUTPUT_SIZEMAGIC_NUMBERBLOCKSIZELOG_MAXBLOCKSIZE_MAXWINDOWLOG_MINWINDOWLOG_MAXCHAINLOG_MINCHAINLOG_MAXHASHLOG_MINHASHLOG_MAXMINMATCH_MINMINMATCH_MAXSEARCHLOG_MINSEARCHLOG_MAXSEARCHLENGTH_MINSEARCHLENGTH_MAXTARGETLENGTH_MINTARGETLENGTH_MAXLDM_MINMATCH_MINLDM_MINMATCH_MAXLDM_BUCKETSIZELOG_MAXSTRATEGY_FASTSTRATEGY_DFASTSTRATEGY_GREEDYSTRATEGY_LAZYSTRATEGY_LAZY2STRATEGY_BTLAZY2STRATEGY_BTOPTSTRATEGY_BTULTRASTRATEGY_BTULTRA2DICT_TYPE_AUTODICT_TYPE_RAWCONTENTDICT_TYPE_FULLDICTFORMAT_ZSTD1FORMAT_ZSTD1_MAGICLESSN   )ffilibF)should_clear_after_allocs   (/c                       [         R                  " 5       =(       d    S$ ! [         a     Of = f [         R                  " S5      $ ! [        [        4 a     gf = f)Nr   SC_NPROCESSORS_ONLN)os	cpu_countAttributeErrorsysconf
ValueError     V/var/www/highfloat_scraper/venv/lib/python3.13/site-packages/zstandard/backend_cffi.py
_cpu_countrY      s[    ||~"" zz/00J'  	s     
--A AAc                   4    \ rS rSrSr\S 5       rS rS rSr	g)r      zRepresents a segment within a ``BufferWithSegments``.

This type is essentially a reference to N bytes within a
``BufferWithSegments``.

The object conforms to the buffer protocol.
c                     [        5       e)z9The byte offset of this segment within its parent buffer.NotImplementedErrorselfs    rX   offsetBufferSegment.offset        "##rW   c                     [        5       e)z+Obtain the length of the segment, in bytes.r]   r_   s    rX   __len__BufferSegment.__len__       !##rW   c                     [        5       e)z"Obtain bytes copy of this segment.r]   r_   s    rX   tobytesBufferSegment.tobytes   rg   rW   rV   N)
__name__
__module____qualname____firstlineno____doc__propertyra   re   ri   __static_attributes__rV   rW   rX   r   r      s%     $ $$$rW   r   c                       \ rS rSrSrSrg)r      zRepresents an array of ``(offset, length)`` integers.

This type is effectively an index used by :py:class:`BufferWithSegments`.

The array members are 64-bit unsigned integers using host/native bit order.

Instances conform to the buffer protocol.
rV   N)rk   rl   rm   rn   ro   rq   rV   rW   rX   r   r      s    rW   r   c                   @    \ rS rSrSr\S 5       rS rS rS r	S r
Srg	)
r      a+  A memory buffer containing N discrete items of known lengths.

This type is essentially a fixed size memory address and an array
of 2-tuples of ``(offset, length)`` 64-bit unsigned native-endian
integers defining the byte offset and length of each segment within
the buffer.

Instances behave like containers.

Instances also conform to the buffer protocol. So a reference to the
backing bytes can be obtained via ``memoryview(o)``. A *copy* of the
backing bytes can be obtained via ``.tobytes()``.

This type exists to facilitate operations against N>1 items without
the overhead of Python object creation and management. Used with
APIs like :py:meth:`ZstdDecompressor.multi_decompress_to_buffer`, it
is possible to decompress many objects in parallel without the GIL
held, leading to even better performance.
c                     [        5       e)z)Total sizein bytes of the backing buffer.r]   r_   s    rX   sizeBufferWithSegments.size   rc   rW   c                     [        5       eNr]   r_   s    rX   re   BufferWithSegments.__len__   s    !##rW   c                     [        5       e)zObtains a segment within the buffer.

The returned object references memory within this buffer.

:param i:
   Integer index of segment to retrieve.
:return:
   :py:class:`BufferSegment`
r]   r`   is     rX   __getitem__BufferWithSegments.__getitem__   s     "##rW   c                     [        5       e)ziObtain the array of ``(offset, length)`` segments in the buffer.

:return:
   :py:class:`BufferSegments`
r]   r_   s    rX   segmentsBufferWithSegments.segments   s     "##rW   c                     [        5       e)z#Obtain bytes copy of this instance.r]   r_   s    rX   ri   BufferWithSegments.tobytes  rg   rW   rV   N)rk   rl   rm   rn   ro   rp   rw   re   r   r   ri   rq   rV   rW   rX   r   r      s/    ( $ $$
$$$rW   r   c                   $    \ rS rSrSrS rS rSrg)r   i	  a  A virtual spanning view over multiple BufferWithSegments.

Instances are constructed from 1 or more :py:class:`BufferWithSegments`
instances. The resulting object behaves like an ordered sequence whose
members are the segments within each ``BufferWithSegments``.

If the object is composed of 2 ``BufferWithSegments`` instances with the
first having 2 segments and the second have 3 segments, then ``b[0]``
and ``b[1]`` access segments in the first object and ``b[2]``, ``b[3]``,
and ``b[4]`` access segments from the second.
c                     [        5       e)z9The number of segments within all ``BufferWithSegments``.r]   r_   s    rX   re   $BufferWithSegmentsCollection.__len__  rg   rW   c                     [        5       e)z*Obtain the ``BufferSegment`` at an offset.r]   r}   s     rX   r   (BufferWithSegmentsCollection.__getitem__  rg   rW   rV   N)rk   rl   rm   rn   ro   re   r   rq   rV   rW   rX   r   r   	  s    
$$rW   r   c                       \ rS rSrSrg)r   i  rV   N)rk   rl   rm   rn   rq   rV   rW   rX   r   r     s    rW   r   c                 t    [         R                  " [        R                  " U 5      5      R	                  S5      $ )Nutf-8)rL   stringrM   ZSTD_getErrorNamedecode)zresults    rX   _zstd_errorr   #  s)     ::c++G45<<WEErW   c                 V   [         R                  " 5       nU[        R                  :X  a
  [	        5       e[        R
                  " U[         R                  5      n[         R                  U R                  4[         R                  U R                  4[         R                  U R                  4[         R                  U R                  4[         R                  U R                   4[         R"                  U R$                  4[         R&                  U R(                  4[         R*                  U R,                  4[         R.                  U R0                  4[         R2                  U R4                  4[         R6                  U R8                  4[         R:                  U R<                  4[         R>                  U R@                  4[         RB                  U RD                  4[         RF                  U RH                  4[         RJ                  U RL                  4[         RN                  U RP                  4[         RR                  U RT                  4[         RV                  U RX                  4[         RZ                  U R\                  4[         R^                  U R`                  4/nU H  u  p4[c        XU5        M     U$ rz   )2rM   ZSTD_createCCtxParamsrL   NULLMemoryErrorgcZSTD_freeCCtxParamsZSTD_c_formatformatZSTD_c_compressionLevelcompression_levelZSTD_c_windowLog
window_logZSTD_c_hashLoghash_logZSTD_c_chainLog	chain_logZSTD_c_searchLog
search_logZSTD_c_minMatch	min_matchZSTD_c_targetLengthtarget_lengthZSTD_c_strategystrategyZSTD_c_contentSizeFlagwrite_content_sizeZSTD_c_checksumFlagwrite_checksumZSTD_c_dictIDFlagwrite_dict_idZSTD_c_nbWorkersthreadsZSTD_c_jobSizejob_sizeZSTD_c_overlapLogoverlap_logZSTD_c_forceMaxWindowforce_max_window!ZSTD_c_enableLongDistanceMatching
enable_ldmZSTD_c_ldmHashLogldm_hash_logZSTD_c_ldmMinMatchldm_min_matchZSTD_c_ldmBucketSizeLogldm_bucket_size_logZSTD_c_ldmHashRateLogldm_hash_rate_log_set_compression_parameter)paramsresattrsparamvalues        rX   _make_cctx_paramsr   *  s   

#
#
%C
chhm
&&c--
.C 
		FMM*		$	$f&>&>?			v001			V__-			f../			v001			f../		 	 &"6"67			foo.		#	#V%>%>?		 	 &"7"78			 4 45			v~~.			V__-			 2 23		"	"F$;$;<		.	.0A0AB			 3 34			!5!56		$	$f&@&@A		"	"F$<$<=+E0 "3u5  JrW   c                      \ rS rSrSr\SS j5       r                     SS jr\S 5       r	\S 5       r
\S 5       r\S 5       r\S	 5       r\S
 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rS rSrg)r   iO  a  Low-level zstd compression parameters.

This type represents a collection of parameters to control how zstd
compression is performed.

Instances can be constructed from raw parameters or derived from a
base set of defaults specified from a compression level (recommended)
via :py:meth:`ZstdCompressionParameters.from_level`.

>>> # Derive compression settings for compression level 7.
>>> params = zstandard.ZstdCompressionParameters.from_level(7)

>>> # With an input size of 1MB
>>> params = zstandard.ZstdCompressionParameters.from_level(7, source_size=1048576)

Using ``from_level()``, it is also possible to override individual compression
parameters or to define additional settings that aren't automatically derived.
e.g.:

>>> params = zstandard.ZstdCompressionParameters.from_level(4, window_log=10)
>>> params = zstandard.ZstdCompressionParameters.from_level(5, threads=4)

Or you can define low-level compression settings directly:

>>> params = zstandard.ZstdCompressionParameters(window_log=12, enable_ldm=True)

Once a ``ZstdCompressionParameters`` instance is obtained, it can be used to
configure a compressor:

>>> cctx = zstandard.ZstdCompressor(compression_params=params)

Some of these are very low-level settings. It may help to consult the official
zstandard documentation for their behavior. Look for the ``ZSTD_p_*`` constants
in ``zstd.h`` (https://github.com/facebook/zstd/blob/dev/lib/zstd.h).
c                     [         R                  " XU5      nSSSSSSSS.nUR                  5        H  u  pgXc;  d  M  [        XG5      X6'   M     [	        S	0 UD6$ )
a-  Create compression parameters from a compression level.

:param level:
   Integer compression level.
:param source_size:
   Integer size in bytes of source to be compressed.
:param dict_size:
   Integer size in bytes of compression dictionary to use.
:return:
   :py:class:`ZstdCompressionParameters`
	windowLogchainLoghashLog	searchLogminMatchtargetLengthr   )r   r   r   r   r   r   r   rV   )rM   ZSTD_getCParamsitemsgetattrr   )levelsource_size	dict_sizekwargsr   argsargattrs           rX   
from_level$ZstdCompressionParameters.from_levelt  sj     $$UC &#!%#+"
 IC %f3 & )2622rW   c                    [         R                  " 5       nU[        R                  :X  a
  [	        5       e[        R
                  " U[         R                  5      nUU l        US:  a
  [        5       n[        U[         R                  U5        [        U[         R                  U5        [        U[         R                  U5        [        U[         R                  U5        [        U[         R                  U5        [        U[         R                  U5        [        U[         R                   U5        [        U[         R"                  U5        [        U[         R$                  U5        U	S:X  a  Sn	[        U[         R&                  U	5        [        U[         R(                  U
5        [        U[         R*                  U5        [        U[         R,                  U5        [        U[         R.                  U5        US:X  a  Sn[        U[         R0                  U5        [        U[         R2                  U5        [        U[         R4                  U5        [        U[         R6                  U5        [        U[         R8                  U5        [        U[         R:                  U5        US:X  a  Sn[        U[         R<                  U5        g )Nr   )rM   r   rL   r   r   r   r   _paramsrY   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                          rX   __init__"ZstdCompressionParameters.__init__  s$   0 **,SXX- 7 78Q; lG 	#63+?+?I"63+<+<fE"C//1B	
 	#63+?+?L"63+=+=xH"63+>+>	J"63+?+?L"63+>+>	J"C++]	
 r>H"63+>+>I"C..0B	
 	#C++^	
 	#63+@+@-P"63+=+=xH"K"63+@+@+N"C--/?	
 	#C99:	
 	#63+@+@,O"C**M	
 	#C//1D	
 " !"C--/@	
rW   c                 J    [        U R                  [        R                  5      $ rz   )_get_compression_parameterr   rM   r   r_   s    rX   r    ZstdCompressionParameters.format  s    )$,,8I8IJJrW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   +ZstdCompressionParameters.compression_level      )LL#55
 	
rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   $ZstdCompressionParameters.window_log      )$,,8L8LMMrW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   "ZstdCompressionParameters.hash_log      )$,,8J8JKKrW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   #ZstdCompressionParameters.chain_log      )$,,8K8KLLrW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   $ZstdCompressionParameters.search_log  r   rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   #ZstdCompressionParameters.min_match  r   rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   'ZstdCompressionParameters.target_length      )$,,8O8OPPrW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   "ZstdCompressionParameters.strategy  r   rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   ,ZstdCompressionParameters.write_content_size  s    )LL#44
 	
rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   (ZstdCompressionParameters.write_checksum  r   rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   'ZstdCompressionParameters.write_dict_id      )$,,8M8MNNrW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   "ZstdCompressionParameters.job_size!  r   rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   %ZstdCompressionParameters.overlap_log%  r  rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   *ZstdCompressionParameters.force_max_window)      )LL#33
 	
rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   $ZstdCompressionParameters.enable_ldm/  s    )LL#??
 	
rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   &ZstdCompressionParameters.ldm_hash_log5  r  rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   'ZstdCompressionParameters.ldm_min_match9  s    )$,,8N8NOOrW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   -ZstdCompressionParameters.ldm_bucket_size_log=  r   rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   +ZstdCompressionParameters.ldm_hash_rate_logC  r  rW   c                 J    [        U R                  [        R                  5      $ rz   )r   r   rM   r   r_   s    rX   r   !ZstdCompressionParameters.threadsI  r   rW   c                 B    [         R                  " U R                  5      $ )zAEstimated size in bytes needed to compress with these parameters.)rM   %ZSTD_estimateCCtxSize_usingCCtxParamsr   r_   s    rX   "estimated_compression_context_size<ZstdCompressionParameters.estimated_compression_context_sizeM  s    88FFrW   )r   Nr   r   )r   r   r   r   r   r   r   r   r   rK   r   r   r   r   r   r   r   r   r   r   r   ) rk   rl   rm   rn   ro   staticmethodr   r   rp   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rq   rV   rW   rX   r   r   O  s-   "H 3 3@ -X
t K K 
 

 N N L L M M N N M M Q Q M M 
 

 Q Q O O L L O O 
 

 
 

 O O P P 
 

 
 

 N NGrW   r   c                  ,    [         R                  " 5       $ )ziEstimate the memory size requirements for a decompressor instance.

:return:
   Integer number of bytes.
)rM   ZSTD_estimateDCtxSizerV   rW   rX   r   r   R  s     $$&&rW   c                     [         R                  " XU5      n[         R                  " U5      (       a  [        S[	        U5      -  5      eg )Nz/unable to set compression context parameter: %s)rM   ZSTD_CCtxParams_setParameterZSTD_isErrorr   r   )r   r   r   r   s       rX   r   r   [  sG    ..veDG
  ='"#
 	
 !rW   c                     [         R                  " S5      n[        R                  " XU5      n[        R                  " U5      (       a  [        S[        U5      -  5      eUS   $ )Nzint *z/unable to get compression context parameter: %sr   )rL   newrM   ZSTD_CCtxParams_getParameterr  r   r   )r   r   resultr   s       rX   r   r   d  s\    WWWF..vfEG
  ='"#
 	

 !9rW   c                       \ rS rSrSr SS jrS rS rS rS r	S r
S	 rS
 r\S 5       rS rS rSS jrSS jrSS jrS rSS jrS rS rSS jrS rS rS r\4S jrS rSrg) r   iq  a%  Writable compressing stream wrapper.

``ZstdCompressionWriter`` is a write-only stream interface for writing
compressed data to another stream.

This type conforms to the ``io.RawIOBase`` interface and should be usable
by any type that operates against a *file-object* (``typing.BinaryIO``
in Python type hinting speak). Only methods that involve writing will do
useful things.

As data is written to this stream (e.g. via ``write()``), that data
is sent to the compressor. As compressed data becomes available from
the compressor, it is sent to the underlying stream by calling its
``write()`` method.

Both ``write()`` and ``flush()`` return the number of bytes written to the
object's ``write()``. In many cases, small inputs do not accumulate enough
data to cause a write and ``write()`` will return ``0``.

Calling ``close()`` will mark the stream as closed and subsequent I/O
operations will raise ``ValueError`` (per the documented behavior of
``io.RawIOBase``). ``close()`` will also call ``close()`` on the underlying
stream if such a method exists and the instance was constructed with
``closefd=True``

Instances are obtained by calling :py:meth:`ZstdCompressor.stream_writer`.

Typically usage is as follows:

>>> cctx = zstandard.ZstdCompressor(level=10)
>>> compressor = cctx.stream_writer(fh)
>>> compressor.write(b"chunk 0\n")
>>> compressor.write(b"chunk 1\n")
>>> compressor.flush()
>>> # Receiver will be able to decode ``chunk 0\nchunk 1\n`` at this point.
>>> # Receiver is also expecting more data in the zstd *frame*.
>>>
>>> compressor.write(b"chunk 2\n")
>>> compressor.flush(zstandard.FLUSH_FRAME)
>>> # Receiver will be able to decode ``chunk 0\nchunk 1\nchunk 2``.
>>> # Receiver is expecting no more data, as the zstd frame is closed.
>>> # Any future calls to ``write()`` at this point will construct a new
>>> # zstd frame.

Instances can be used as context managers. Exiting the context manager is
the equivalent of calling ``close()``, which is equivalent to calling
``flush(zstandard.FLUSH_FRAME)``:

>>> cctx = zstandard.ZstdCompressor(level=10)
>>> with cctx.stream_writer(fh) as compressor:
...     compressor.write(b'chunk 0')
...     compressor.write(b'chunk 1')
...     ...

.. important::

   If ``flush(FLUSH_FRAME)`` is not called, emitted data doesn't
   constitute a full zstd *frame* and consumers of this data may complain
   about malformed input. It is recommended to use instances as a context
   manager to ensure *frames* are properly finished.

If the size of the data being fed to this streaming compressor is known,
you can declare it before compression begins:

>>> cctx = zstandard.ZstdCompressor()
>>> with cctx.stream_writer(fh, size=data_len) as compressor:
...     compressor.write(chunk0)
...     compressor.write(chunk1)
...     ...

Declaring the size of the source data allows compression parameters to
be tuned. And if ``write_content_size`` is used, it also results in the
content size being written into the frame header of the output data.

The size of chunks being ``write()`` to the destination can be specified:

>>> cctx = zstandard.ZstdCompressor()
>>> with cctx.stream_writer(fh, write_size=32768) as compressor:
...     ...

To see how much memory is being used by the streaming compressor:

>>> cctx = zstandard.ZstdCompressor()
>>> with cctx.stream_writer(fh) as compressor:
...     ...
...     byte_size = compressor.memory_size()

Thte total number of bytes written so far are exposed via ``tell()``:

>>> cctx = zstandard.ZstdCompressor()
>>> with cctx.stream_writer(fh) as compressor:
...     ...
...     total_written = compressor.tell()

``stream_writer()`` accepts a ``write_return_read`` boolean argument to
control the return value of ``write()``. When ``False`` (the default),
``write()`` returns the number of bytes that were ``write()``'en to the
underlying object. When ``True``, ``write()`` returns the number of bytes
read from the input that were subsequently written to the compressor.
``True`` is the *proper* behavior for ``write()`` as specified by the
``io.RawIOBase`` interface and will become the default value in a future
release.
c                 T   Xl         X l        X@l        [        U5      U l        [        U5      U l        SU l        SU l        SU l        SU l	        [        R                  " SU5      U l        [        R                  " S5      U l        U R                  U R                  l        [        U R                  5      U R                  l        SU R                  l        [$        R&                  " UR(                  U5      n[$        R*                  " U5      (       a  [-        S[/        U5      -  5      eg )NFr   char[]ZSTD_outBuffer *error setting source size: %s)_compressor_writer_write_sizebool_write_return_read_closefd_entered_closing_closed_bytes_compressedrL   r!  _dst_buffer_out_bufferdstlenrw   posrM   ZSTD_CCtx_setPledgedSrcSize_cctxr  r   r   )r`   
compressorwriterr   
write_sizewrite_return_readclosefdr   s           rX   r   ZstdCompressionWriter.__init__  s     &%"&'8"9W!"778Z877#56#// #D$4$4 5 11*2B2BKPG$$/+g2FF  %rW   c                     U R                   (       a  [        S5      eU R                  (       a  [        S5      eSU l        U $ Nstream is closedcannot __enter__ multiple timesTr1  rU   r/  r   r_   s    rX   	__enter__ZstdCompressionWriter.__enter__  s4    <</00===>>rW   c                 @    SU l         U R                  5         S U l        gNF)r/  closer)  r`   exc_type	exc_valueexc_tbs       rX   __exit__ZstdCompressionWriter.__exit__  s    

rW   c                 ,    [         R                  " 5       erz   ioUnsupportedOperationr_   s    rX   __iter__ZstdCompressionWriter.__iter__
      %%''rW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   __next__ZstdCompressionWriter.__next__  rV  rW   c                 V    [         R                  " U R                  R                  5      $ rz   )rM   ZSTD_sizeof_CCtxr)  r9  r_   s    rX   memory_size!ZstdCompressionWriter.memory_size  s    ##D$4$4$:$:;;rW   c                 b    [        U R                  SS 5      nU(       a  U" 5       $ [        S5      eNfilenoz)fileno not available on underlying writerr   r*  OSErrorr`   fs     rX   r`  ZstdCompressionWriter.fileno  *    DLL(D13JEFFrW   c                    U R                   (       a  g  SU l        U R                  [        5        SU l        SU l         [	        U R
                  SS 5      nU R                  (       a  U(       a  U" 5         g g g ! SU l        SU l         f = fNTFrI  )r1  r0  flushr   r   r*  r.  rc  s     rX   rI  ZstdCompressionWriter.close  sm    <<	  DMJJ{#!DMDL DLL'40==QC = "DMDLs   A7 7Bc                     U R                   $ rz   r1  r_   s    rX   closedZstdCompressionWriter.closed*      ||rW   c                     grH  rV   r_   s    rX   isattyZstdCompressionWriter.isatty.      rW   c                     grH  rV   r_   s    rX   readableZstdCompressionWriter.readable1  rs  rW   c                 ,    [         R                  " 5       erz   rQ  r`   rw   s     rX   readlineZstdCompressionWriter.readline4  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r`   hints     rX   	readlinesZstdCompressionWriter.readlines7  rV  rW   Nc                 ,    [         R                  " 5       erz   rQ  r`   ra   whences      rX   seekZstdCompressionWriter.seek:  rV  rW   c                     grH  rV   r_   s    rX   seekableZstdCompressionWriter.seekable=  rs  rW   c                 ,    [         R                  " 5       erz   rQ  rx  s     rX   truncateZstdCompressionWriter.truncate@  rV  rW   c                     gNTrV   r_   s    rX   writableZstdCompressionWriter.writableC      rW   c                     [        S5      e)Nz#writelines() is not yet implementedr]   r`   liness     rX   
writelines ZstdCompressionWriter.writelinesF  s    !"GHHrW   c                 ,    [         R                  " 5       erz   rQ  rx  s     rX   readZstdCompressionWriter.readI  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   readallZstdCompressionWriter.readallL  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r`   bs     rX   readintoZstdCompressionWriter.readintoO  rV  rW   c                    U R                   (       a  [        S5      eSn[        R                  " U5      n[        R                  " S5      nX4l        [        U5      Ul        SUl        U R                  nSUl        UR                  UR                  :  Ga  [        R                  " U R                  R                  UU[        R                  5      n[        R                  " U5      (       a  [!        S[#        U5      -  5      eUR                  (       a{  U R$                  R'                  [        R(                  " UR*                  UR                  5      SS 5        X%R                  -  nU =R,                  UR                  -  sl        SUl        UR                  UR                  :  a  GM  U R.                  (       a  UR                  $ U$ )z=Send data to the compressor and possibly to the inner stream.rB  r   ZSTD_inBuffer *zstd compress error: %sN)r1  rU   rL   from_bufferr!  srcr6  rw   r7  r4  rM   ZSTD_compressStream2r)  r9  ZSTD_e_continuer  r   r   r*  writebufferr5  r2  r-  )r`   datatotal_writedata_buffer	in_buffer
out_bufferr   s          rX   r  ZstdCompressionWriter.writeR  sY   <</00ood+GG-.	#[)		%%

mminn,..  &&##	G ((-G0DD  ~~""JJz~~z~~>qA ~~-&&*..8&!"
% mminn,( ""== rW   c                    U[         :X  a  [        R                  nO)U[        :X  a  [        R                  nO[        SU-  5      eU R                  (       a  [        S5      eSnU R                  nSUl        [        R                  " S5      n[        R                  Ul        SUl        SUl         [        R                  " U R                  R                   XEU5      n[        R"                  " U5      (       a  [%        S['        U5      -  5      eUR                  (       a{  U R(                  R+                  [        R,                  " UR.                  UR                  5      SS 5        X4R                  -  nU =R0                  UR                  -  sl        SUl        U(       d  OM  [3        U R(                  SS5      nU(       a  U R4                  (       d  U" 5         U$ )a  Evict data from compressor's internal state and write it to inner stream.

Calling this method may result in 0 or more ``write()`` calls to the
inner stream.

This method will also call ``flush()`` on the inner stream, if such a
method exists.

:param flush_mode:
   How to flush the zstd compressor.

   ``zstandard.FLUSH_BLOCK`` will flush data already sent to the
   compressor but not emitted to the inner stream. The stream is still
   writable after calling this. This is the default behavior.

   See documentation for other ``zstandard.FLUSH_*`` constants for more
   flushing options.
:return:
   Integer number of bytes written to the inner stream.
zunknown flush_mode: %rrB  r   r  r  Nri  )r   rM   ZSTD_e_flushr   
ZSTD_e_endrU   r1  r4  r7  rL   r!  r   r  rw   r  r)  r9  r  r   r   r*  r  r  r5  r2  r   r0  )r`   
flush_moderi  r  r  r  r   rd  s           rX   ri  ZstdCompressionWriter.flush|  sq   , $$$E;&NNE5
BCC<</00%%

GG-.				..  &&
uG ((-G0DD  ~~""JJz~~z~~>qA ~~-&&*..8&!"
% ( DLL'40T]]CrW   c                     U R                   $ rz   r2  r_   s    rX   tellZstdCompressionWriter.tell      %%%rW   )r2  r1  r.  r0  r)  r3  r/  r4  r-  r+  r*  Tr   rz   ) rk   rl   rm   rn   ro   r   rE  rN  rT  rX  r\  r`  rI  rp   rm  rq  ru  ry  r~  r  r  r  r  r  r  r  r  r  r   ri  r  rq   rV   rW   rX   r   r   q  s    f^ >((<G   ((((I((((T  + BH&rW   r   c                   6    \ rS rSrSr\4S jrS r\4S jr	Sr
g)r   i  a  A compressor conforming to the API in Python's standard library.

This type implements an API similar to compression types in Python's
standard library such as ``zlib.compressobj`` and ``bz2.BZ2Compressor``.
This enables existing code targeting the standard library API to swap
in this type to achieve zstd compression.

.. important::

   The design of this API is not ideal for optimal performance.

   The reason performance is not optimal is because the API is limited to
   returning a single buffer holding compressed data. When compressing
   data, we don't know how much data will be emitted. So in order to
   capture all this data in a single buffer, we need to perform buffer
   reallocations and/or extra memory copies. This can add significant
   overhead depending on the size or nature of the compressed data how
   much your application calls this type.

   If performance is critical, consider an API like
   :py:meth:`ZstdCompressor.stream_reader`,
   :py:meth:`ZstdCompressor.stream_writer`,
   :py:meth:`ZstdCompressor.chunker`, or
   :py:meth:`ZstdCompressor.read_to_iter`, which result in less overhead
   managing buffers.

Instances are obtained by calling :py:meth:`ZstdCompressor.compressobj`.

Here is how this API should be used:

>>> cctx = zstandard.ZstdCompressor()
>>> cobj = cctx.compressobj()
>>> data = cobj.compress(b"raw input 0")
>>> data = cobj.compress(b"raw input 1")
>>> data = cobj.flush()

Or to flush blocks:

>>> cctx.zstandard.ZstdCompressor()
>>> cobj = cctx.compressobj()
>>> data = cobj.compress(b"chunk in first block")
>>> data = cobj.flush(zstandard.COMPRESSOBJ_FLUSH_BLOCK)
>>> data = cobj.compress(b"chunk in second block")
>>> data = cobj.flush()

For best performance results, keep input chunks under 256KB. This avoids
extra allocations for a large output object.

It is possible to declare the input size of the data that will be fed
into the compressor:

>>> cctx = zstandard.ZstdCompressor()
>>> cobj = cctx.compressobj(size=6)
>>> data = cobj.compress(b"foobar")
>>> data = cobj.flush()
c                    Xl         [        R                  " S5      U l        [        R                  " SU5      U l        U R                  U R                  l        X R                  l        SU R                  l        SU l        g )Nr'  r&  r   F)	r)  rL   r!  _outr3  r5  rw   r7  	_finished)r`   r:  r<  s      rX   r   ZstdCompressionObj.__init__  sZ     &GG./	778Z8((		#				rW   c                 F   U R                   (       a  [        S5      e[        R                  " U5      n[        R                  " S5      nX#l        [        U5      Ul        SUl        / nUR                  [        U5      :  Ga  [        R                  " U R                  R                  U R                  U[        R                  5      n[        R                  " U5      (       a  [        S[!        U5      -  5      eU R                  R                  (       ab  UR#                  [        R$                  " U R                  R&                  U R                  R                  5      SS 5        SU R                  l        UR                  [        U5      :  a  GM  SR)                  U5      $ )a  Send data to the compressor.

This method receives bytes to feed to the compressor and returns
bytes constituting zstd compressed data.

The zstd compressor accumulates bytes and the returned bytes may be
substantially smaller or larger than the size of the input data on
any given call. The returned value may be the empty byte string
(``b""``).

:param data:
   Data to write to the compressor.
:return:
   Compressed data.
z0cannot call compress() after compressor finishedr  r   r  NrW   )r  r   rL   r  r!  r  r6  rw   r7  rM   r  r)  r9  r  r  r  r   appendr  r5  join)r`   r  r  sourcechunksr   s         rX   compressZstdCompressionObj.compress	  s$     >>NOOood+*+ 
+&
jj3t9$..  &&		63;N;NG ((-G0DD  yy}}cjj		FqIJ !		 jj3t9$ xxrW   c                    U[         [        4;  a  [        S5      eU R                  (       a  [	        S5      eU[        :X  a  [
        R                  nO-U[         :X  a  [
        R                  nSU l        O[	        S5      eU R                  R                  S:X  d   e[        R                  " S5      n[        R                  Ul        SUl        SUl	        / n [
        R                  " U R                   R"                  U R                  X25      n[
        R$                  " U5      (       a  [	        S['        U5      -  5      eU R                  R                  (       ab  UR)                  [        R*                  " U R                  R,                  U R                  R                  5      SS 5        SU R                  l	        U(       d  OM  S	R/                  U5      $ )
ai  Emit data accumulated in the compressor that hasn't been outputted yet.

The ``flush_mode`` argument controls how to end the stream.

``zstandard.COMPRESSOBJ_FLUSH_FINISH`` (the default) ends the
compression stream and finishes a zstd frame. Once this type of flush
is performed, ``compress()`` and ``flush()`` can no longer be called.
This type of flush **must** be called to end the compression context. If
not called, the emitted data may be incomplete and may not be readable
by a decompressor.

``zstandard.COMPRESSOBJ_FLUSH_BLOCK`` will flush a zstd block. This
ensures that all data fed to this instance will have been omitted and
can be decoded by a decompressor. Flushes of this type can be performed
multiple times. The next call to ``compress()`` will begin a new zstd
block.

:param flush_mode:
   How to flush the zstd compressor.
:return:
   Compressed data.
zflush mode not recognizedz"compressor object already finishedTzunhandled flush moder   r  #error ending compression stream: %sNrW   )r   r   rU   r  r   rM   r  r  r  r7  rL   r!  r   r  rw   r  r)  r9  r  r   r  r  r5  r  )r`   r  z_flush_moder  r  r   s         rX   ri  ZstdCompressionObj.flush3  sk   . $#
 
 899>>@AA00++L33>>L!DN233yy}}!!!GG-.				..  &&		9G ((9K<PP  yy}}cjj		FqIJ !		   xxrW   )r)  r3  r  r  N)rk   rl   rm   rn   ro   r&   r   r  r   ri  rq   rV   rW   rX   r   r     s$    7t &I	( T  8 A rW   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	r	   iw  a  Compress data to uniformly sized chunks.

This type allows you to iteratively feed chunks of data into a compressor
and produce output chunks of uniform size.

``compress()``, ``flush()``, and ``finish()`` all return an iterator of
``bytes`` instances holding compressed data. The iterator may be empty.
Callers MUST iterate through all elements of the returned iterator before
performing another operation on the object or else the compressor's
internal state may become confused. This can result in an exception being
raised or malformed data being emitted.

All chunks emitted by ``compress()`` will have a length of the configured
chunk size.

``flush()`` and ``finish()`` may return a final chunk smaller than
the configured chunk size.

Instances are obtained by calling :py:meth:`ZstdCompressor.chunker`.

Here is how the API should be used:

>>> cctx = zstandard.ZstdCompressor()
>>> chunker = cctx.chunker(chunk_size=32768)
>>>
>>> with open(path, 'rb') as fh:
...     while True:
...         in_chunk = fh.read(32768)
...         if not in_chunk:
...             break
...
...         for out_chunk in chunker.compress(in_chunk):
...             # Do something with output chunk of size 32768.
...
...     for out_chunk in chunker.finish():
...         # Do something with output chunks that finalize the zstd frame.

This compressor type is often a better alternative to
:py:class:`ZstdCompressor.compressobj` because it has better performance
properties.

``compressobj()`` will emit output data as it is available. This results
in a *stream* of output chunks of varying sizes. The consistency of the
output chunk size with ``chunker()`` is more appropriate for many usages,
such as sending compressed data to a socket.

``compressobj()`` may also perform extra memory reallocations in order
to dynamically adjust the sizes of the output chunks. Since ``chunker()``
output chunks are all the same size (except for flushed or final chunks),
there is less memory allocation/copying overhead.
c                    Xl         [        R                  " S5      U l        [        R                  " SU5      U l        U R                  U R                  l        X R                  l        SU R                  l        [        R                  " S5      U l        [        R                  U R                  l
        SU R                  l        SU R                  l        SU l        g )Nr'  r&  r   r  F)r)  rL   r!  r  r3  r5  rw   r7  _inr   r  r  )r`   r:  
chunk_sizes      rX   r   ZstdCompressionChunker.__init__  s    %GG./	778Z8((		#				77,-xxrW   c              #     #    U R                   (       a  [        S5      eU R                  R                  [        R
                  :w  a  [        S5      e[        R                  " U5      n[        U5      (       d  gX R                  l        [        U5      U R                  l        SU R                  l	        U R                  R                  U R                  R                  :  Ga  [        R                  " U R                  R                  U R                  U R                  [        R                  5      nU R                  R                  U R                  R                  :X  aA  [        R
                  U R                  l        SU R                  l        SU R                  l	        [        R                   " U5      (       a  [        S[#        U5      -  5      eU R                  R                  U R                  R                  :X  aU  [        R$                  " U R                  R&                  U R                  R                  5      SS v   SU R                  l	        U R                  R                  U R                  R                  :  a  GM  gg7f)zFeed new input data into the compressor.

:param data:
   Data to feed to compressor.
:return:
   Iterator of ``bytes`` representing chunks of compressed data.
z1cannot call compress() after compression finishedzHcannot perform operation before consuming output from previous operationNr   r  )r  r   r  r  rL   r   r  r6  rw   r7  rM   r  r)  r9  r  r  r  r   r  r5  )r`   r  r  r   s       rX   r  ZstdCompressionChunker.compress  s     >>OPP88<<388#* 
 ood+;"K(hhllTXX]]*..  &&		488S=P=PG xx||txx}},"xx ! ((-G0DD  yy}}		.jj		>qAA !		# hhllTXX]]**s   JJ	J	c              #     #    U R                   (       a  [        S5      eU R                  R                  [        R
                  :w  a  [        S5      e [        R                  " U R                  R                  U R                  U R                  [        R                  5      n[        R                  " U5      (       a  [        S[        U5      -  5      eU R                  R                  (       aU  [        R                  " U R                  R                   U R                  R                  5      SS v   SU R                  l        U(       d  gM  7f)zeFlushes all data currently in the compressor.

:return:
   Iterator of ``bytes`` of compressed data.
z.cannot call flush() after compression finishedzCcannot call flush() before consuming output from previous operationr  Nr   )r  r   r  r  rL   r   rM   r  r)  r9  r  r  r  r   r7  r  r5  r`   r   s     rX   ri  ZstdCompressionChunker.flush  s      >>LMM88<<388#% 
 ..  &&		488S=M=MG ((-G0DD  yy}}jj		>qAA !		 s   EEc              #     #    U R                   (       a  [        S5      eU R                  R                  [        R
                  :w  a  [        S5      e [        R                  " U R                  R                  U R                  U R                  [        R                  5      n[        R                  " U5      (       a  [        S[        U5      -  5      eU R                  R                  (       aU  [        R                  " U R                  R                   U R                  R                  5      SS v   SU R                  l        U(       d  SU l         gGM  7f)zSignals the end of input data.

No new data can be compressed after this method is called.

This method will flush buffered data and finish the zstd frame.

:return:
   Iterator of ``bytes`` of compressed data.
z/cannot call finish() after compression finishedzDcannot call finish() before consuming output from previous operationTr  Nr   )r  r   r  r  rL   r   rM   r  r)  r9  r  r  r  r   r7  r  r5  r  s     rX   finishZstdCompressionChunker.finish  s      >>MNN88<<388#% 
 ..  &&		488S^^G ((-G0DD  yy}}jj		>qAA !		!% s   EE)r)  r3  r  r  r  N)
rk   rl   rm   rn   ro   r   r  ri  r  rq   rV   rW   rX   r	   r	   w  s    2h+"Z>"rW   r	   c                       \ rS rSrSrSS jrS rS rS rS r	S r
S	 rS
 rS rS rS rS rS r\S 5       rS rS rS rS r\rS rS rSS jrSS jrS rS rSrg)r   i+  ag	  Readable compressing stream wrapper.

``ZstdCompressionReader`` is a read-only stream interface for obtaining
compressed data from a source.

This type conforms to the ``io.RawIOBase`` interface and should be usable
by any type that operates against a *file-object* (``typing.BinaryIO``
in Python type hinting speak).

Instances are neither writable nor seekable (even if the underlying
source is seekable). ``readline()`` and ``readlines()`` are not implemented
because they don't make sense for compressed data. ``tell()`` returns the
number of compressed bytes emitted so far.

Instances are obtained by calling :py:meth:`ZstdCompressor.stream_reader`.

In this example, we open a file for reading and then wrap that file
handle with a stream from which compressed data can be ``read()``.

>>> with open(path, 'rb') as fh:
...     cctx = zstandard.ZstdCompressor()
...     reader = cctx.stream_reader(fh)
...     while True:
...         chunk = reader.read(16384)
...         if not chunk:
...             break
...
...         # Do something with compressed chunk.

Instances can also be used as context managers:

>>> with open(path, 'rb') as fh:
...     cctx = zstandard.ZstdCompressor()
...     with cctx.stream_reader(fh) as reader:
...         while True:
...             chunk = reader.read(16384)
...             if not chunk:
...                 break
...
...             # Do something with compressed chunk.

When the context manager exits or ``close()`` is called, the stream is
closed, underlying resources are released, and future operations against
the compression stream will fail.

``stream_reader()`` accepts a ``size`` argument specifying how large the
input stream is. This is used to adjust compression parameters so they are
tailored to the source size. e.g.

>>> with open(path, 'rb') as fh:
...     cctx = zstandard.ZstdCompressor()
...     with cctx.stream_reader(fh, size=os.stat(path).st_size) as reader:
...         ...

If the ``source`` is a stream, you can specify how large ``read()``
requests to that stream should be via the ``read_size`` argument.
It defaults to ``zstandard.COMPRESSION_RECOMMENDED_INPUT_SIZE``. e.g.

>>> with open(path, 'rb') as fh:
...     cctx = zstandard.ZstdCompressor()
...     # Will perform fh.read(8192) when obtaining data to feed into the
...     # compressor.
...     with cctx.stream_reader(fh, read_size=8192) as reader:
...         ...
c                     Xl         X l        X0l        X@l        SU l        SU l        SU l        SU l        SU l        [        R                  " S5      U l        S U l        g NFr   r  )r)  _source
_read_sizer.  r/  r1  r2  _finished_input_finished_outputrL   r!  
_in_buffer_source_buffer)r`   r:  r  	read_sizer>  s        rX   r   ZstdCompressionReader.__init__n  sV    %#!"$ %''"34"rW   c                     U R                   (       a  [        S5      eU R                  (       a  [        S5      eSU l         U $ NrC  rB  Tr/  rU   r1  r_   s    rX   rE  ZstdCompressionReader.__enter__}  4    ==>??<</00rW   c                 N    SU l         S U l        U R                  5         S U l        grH  )r/  r)  rI  r  rJ  s       rX   rN  ZstdCompressionReader.__exit__  s$    

rW   c                     gr  rV   r_   s    rX   ru  ZstdCompressionReader.readable  r  rW   c                     grH  rV   r_   s    rX   r  ZstdCompressionReader.writable  rs  rW   c                     grH  rV   r_   s    rX   r  ZstdCompressionReader.seekable  rs  rW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   ry  ZstdCompressionReader.readline  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   r~  ZstdCompressionReader.readlines  rV  rW   c                     [        S5      eNzstream is not writablerb  r`   r  s     rX   r  ZstdCompressionReader.write      .//rW   c                     [        S5      er  r  )r`   ignoreds     rX   r   ZstdCompressionReader.writelines  r  rW   c                     grH  rV   r_   s    rX   rq  ZstdCompressionReader.isatty  rs  rW   c                     g rz   rV   r_   s    rX   ri  ZstdCompressionReader.flush  r  rW   c                     U R                   (       a  g SU l         [        U R                  SS 5      nU R                  (       a  U(       a  U" 5         g g g NTrI  r1  r   r  r.  rc  s     rX   rI  ZstdCompressionReader.close  s:    <<DLL'40==QC =rW   c                     U R                   $ rz   rl  r_   s    rX   rm  ZstdCompressionReader.closed  ro  rW   c                     U R                   $ rz   r  r_   s    rX   r  ZstdCompressionReader.tell  r  rW   c                     / n U R                  S5      nU(       d  OUR                  U5        M,  SR                  U5      $ Ni   rW   r  r  r  r`   r  chunks      rX   r  ZstdCompressionReader.readall  @    IIg&EMM%   xxrW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   rT  ZstdCompressionReader.__iter__  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   rX  ZstdCompressionReader.__next__  rV  rW   c                    U R                   (       a  g [        U R                  S5      (       a  U R                  R                  U R                  5      nU(       d  SU l         g [
        R                  " U5      U l        U R                  U R                  l	        [        U R                  5      U R                  l        SU R                  l        g [
        R                  " U R                  5      U l        U R                  U R                  l	        [        U R                  5      U R                  l        SU R                  l        g Nr  Tr   )r  hasattrr  r  r  rL   r  r  r  r  r6  rw   r7  r  s     rX   _read_input!ZstdCompressionReader._read_input  s    4<<((<<$$T__5D'+$"%//$"7D"&"5"5DOO#&t':':#;DOO "#DOO"%//$,,"?D"&"5"5DOO#&t':':#;DOO "#DOOrW   c                 .   U R                   R                  U R                   R                  :  a  g UR                  n[        R                  " U R
                  R                  UU R                   [        R                  5      nU =R                  UR                  U-
  -  sl        U R                   R                  U R                   R                  :X  aj  [        R                  U R                   l        SU R                   l        SU R                   l        S U l        [        U R                  S5      (       d  SU l        [        R                   " U5      (       a  [#        S[%        U5      5      eUR                  =(       a    UR                  UR                  :H  $ )Nr   r  Tr  )r  r7  rw   rM   r  r)  r9  r  r2  rL   r   r  r  r  r  r  r  r   r   )r`   r  old_posr   s       rX   _compress_into_buffer+ZstdCompressionReader._compress_into_buffer  s   ??$//"6"66..**""OO	
 	*..7"::??$//"6"66"%((DOO"#DOO#$DOO "&D4<<00'+$G$$5{77KLL~~C*..JOO"CCrW   c                 j   U R                   (       a  [        S5      eUS:  a  [        S5      eUS:X  a  U R                  5       $ U R                  (       d  US:X  a  g[        R
                  " SU5      n[        R
                  " S5      nX#l        Xl        SUl        U R                  U5      (       a.  [        R                  " UR                  UR                  5      S S  $ U R                  (       dg  U R                  5         U R                  U5      (       a.  [        R                  " UR                  UR                  5      S S  $ U R                  (       d  Mg  UR                  n[        R                  " U R                  R                   X0R"                  [        R$                  5      nU =R&                  UR                  U-
  -  sl        [        R(                  " U5      (       a  [+        S[-        U5      5      eUS:X  a  S	U l        [        R                  " UR                  UR                  5      S S  $ 
NrB  r   )cannot read negative amounts less than -1r   rW   r&  r'  r  T)r1  rU   r  r  rL   r!  r5  rw   r7  r  r  r  r  rM   r  r)  r9  r  r  r2  r  r   r   r`   rw   
dst_bufferr  r  r   s         rX   r  ZstdCompressionReader.read  s   <</00"9HII2:<<>!  DAI WWXt,
WW/0
#
%%j11::jnnjnn=a@@&&))*55zz*..*..A!DD	 &&& ..**""J
 	*..7"::G$$5{77K  a<$(D!zz*..*..9!<<rW   c                    U R                   (       a  [        S5      eUS:  a  [        S5      eU R                  (       d  US:X  a  gUS:X  a  [        n[        R
                  " SU5      n[        R
                  " S5      nX#l        Xl        SUl        U R                  U5        UR                  (       a.  [        R                  " UR                  UR                  5      S S  $ U R                  (       d  U R                  5         U R                  U5      (       a.  [        R                  " UR                  UR                  5      S S  $ UR                  (       a?  U R                  (       d.  [        R                  " UR                  UR                  5      S S  $ U R                  (       d  M  UR                  n[        R                  " U R                  R                   X0R"                  [        R$                  5      nU =R&                  UR                  U-
  -  sl        [        R(                  " U5      (       a  [+        S[-        U5      -  5      eUS:X  a  S	U l        [        R                  " UR                  UR                  5      S S  $ r  )r1  rU   r  r&   rL   r!  r5  rw   r7  r  r  r  r  rM   r  r)  r9  r  r  r2  r  r   r   r   s         rX   read1ZstdCompressionReader.read11  s   <</00"9HII  DAI 2:6DWWXt,
WW/0
#
 	"":.>>::jnnjnn=a@@&& ))*55zz*..*..A!DD ~~d&:&:zz*..*..A!DD &&&  ..**""J
 	*..7"::G$$5G8LL  a<$(D!zz*..*..9!<<rW   c                    U R                   (       a  [        S5      eU R                  (       a  g[        R                  " U5      n[        R
                  " USS5        [        R                  " S5      nX#l        [        U5      Ul	        SUl
        U R                  U5      (       a  UR                  $ U R                  (       dE  U R                  5         U R                  U5      (       a  UR                  $ U R                  (       d  ME  UR                  n[        R                  " U R                   R"                  X0R$                  [        R&                  5      nU =R(                  UR                  U-
  -  sl        [        R*                  " U5      (       a  [-        S[/        U5      5      eUS:X  a  SU l        UR                  $ NrB  r   rW   r'  r  Tr1  rU   r  rL   r  memmover!  r5  r6  rw   r7  r  r  r  rM   r  r)  r9  r  r  r2  r  r   r   r`   r  dest_bufferr  r  r   s         rX   r  ZstdCompressionReader.readintos  sV   <</00   ooa(AsAWW/0
$k*

%%j11>>!&&))*55!~~% &&& ..**""J
 	*..7"::G$$5{77K  a<$(D!~~rW   c                    U R                   (       a  [        S5      eU R                  (       a  g[        R                  " U5      n[        R
                  " USS5        [        R                  " S5      nX#l        [        U5      Ul	        SUl
        U R                  U5        UR                  (       a  UR                  $ U R                  (       ds  U R                  5         U R                  U5      (       a  UR                  $ UR                  (       a  U R                  (       d  UR                  $ U R                  (       d  Ms  UR                  n[        R                  " U R                   R"                  X0R$                  [        R&                  5      nU =R(                  UR                  U-
  -  sl        [        R*                  " U5      (       a  [-        S[/        U5      -  5      eUS:X  a  SU l        UR                  $ r'  r(  r*  s         rX   	readinto1ZstdCompressionReader.readinto1  sv   <</00   ooa(AsAWW/0
$k*

"":.>>>>!&&))*55!~~%~~d&:&:!~~% &&& ..**""J
 	*..7"::G$$5G8LL  a<$(D!~~rW   )r2  r1  r.  r)  r/  r  r  r  r  r  r  Nr  r  ) rk   rl   rm   rn   ro   r   rE  rN  ru  r  r  ry  r~  r  r  rq  ri  rI  rp   rm  r  r  rT  rX  nextr  r  r  r$  r  r.  rq   rV   rW   rX   r   r   +  s    @D#((00  &
 (( D$*D:.=`@=D'R.rW   r   c                       \ rS rSrSr       SS jrS rS rS rSS	 jr	S\
4S
 jrS\\
4S jrS\S4S jrS\
SS4S jrS\\
4S jrSS jrS rSrg)r   i  a  
Create an object used to perform Zstandard compression.

Each instance is essentially a wrapper around a ``ZSTD_CCtx`` from
zstd's C API.

An instance can compress data various ways. Instances can be used
multiple times. Each compression operation will use the compression
parameters defined at construction time.

.. note:

   When using a compression dictionary and multiple compression
   operations are performed, the ``ZstdCompressionParameters`` derived
   from an integer compression ``level`` and the first compressed data's
   size will be reused for all subsequent operations. This may not be
   desirable if source data sizes vary significantly.

``compression_params`` is mutually exclusive with ``level``,
``write_checksum``, ``write_content_size``, ``write_dict_id``, and
``threads``.

Assume that each ``ZstdCompressor`` instance can only handle a single
logical compression operation at the same time. i.e. if you call a method
like ``stream_reader()`` to obtain multiple objects derived from the same
``ZstdCompressor`` instance and attempt to use them simultaneously, errors
will likely occur.

If you need to perform multiple logical compression operations and you
can't guarantee those operations are temporally non-overlapping, you need
to obtain multiple ``ZstdCompressor`` instances.

Unless specified otherwise, assume that no two methods of
``ZstdCompressor`` instances can be called from multiple Python
threads simultaneously. In other words, assume instances are not thread safe
unless stated otherwise.

:param level:
   Integer compression level. Valid values are all negative integers
   through 22. Lower values generally yield faster operations with lower
   compression ratios. Higher values are generally slower but compress
   better. The default is 3, which is what the ``zstd`` CLI uses. Negative
   levels effectively engage ``--fast`` mode from the ``zstd`` CLI.
:param dict_data:
   A ``ZstdCompressionDict`` to be used to compress with dictionary
    data.
:param compression_params:
   A ``ZstdCompressionParameters`` instance defining low-level compression
   parameters. If defined, this will overwrite the ``level`` argument.
:param write_checksum:
   If True, a 4 byte content checksum will be written with the compressed
   data, allowing the decompressor to perform content verification.
:param write_content_size:
   If True (the default), the decompressed content size will be included
   in the header of the compressed data. This data will only be written if
   the compressor knows the size of the input data.
:param write_dict_id:
   Determines whether the dictionary ID will be written into the compressed
   data. Defaults to True. Only adds content to the compressed data if
   a dictionary is being used.
:param threads:
   Number of threads to use to compress data concurrently. When set,
   compression operations are performed on multiple threads. The default
   value (0) disables multi-threaded compression. A value of ``-1`` means
   to set the number of threads to the number of detected logical CPUs.
Nc           	         U[         R                  " 5       :  a!  [        S[         R                  " 5       -  5      eUS:  a
  [        5       nU(       a  Ub  [        S5      eU(       a  Ub  [        S5      eU(       a  Ub  [        S5      eU(       a  U(       a  [        S5      eU(       a  [	        U5      U l        GO9Uc  Sn[         R                  " 5       nU[        R                  :X  a
  [        5       e[        R                  " U[         R                  5      U l        [        U R
                  [         R                  U5        [        U R
                  [         R                  Ub  UOS5        [        U R
                  [         R                  U(       a  SOS5        [        U R
                  [         R                   U(       a  SOS5        U(       a%  [        U R
                  [         R"                  U5        [         R$                  " 5       n	U	[        R                  :X  a
  [        5       eXl        X l         U R+                  5         [        R                  " U	[         R,                  [         R.                  " U	5      S	9U l        g ! [        R                  " U	[         R,                  [         R.                  " U	5      S	9U l        f = f)
Nzlevel must be less than %dr   z3cannot define compression_params and write_checksumz7cannot define compression_params and write_content_sizez2cannot define compression_params and write_dict_idz,cannot define compression_params and threadsTrK   rw   )rM   ZSTD_maxCLevelrU   rY   r   r   r   rL   r   r   r   r   r   r   r   r   r   r   ZSTD_createCCtxr9  
_dict_data_setup_cctxZSTD_freeCCtxr[  )
r`   r   	dict_datacompression_paramsr   r   r   r   r   cctxs
             rX   r   ZstdCompressor.__init__  s$    3%%'',s/A/A/CC  Q; lG."<H  "4"@L  -";G  'KLL,-?@DL$ $..0F!!m#66&#*A*ABDL&c995 '**&8&D"! '''# 'c33-QQ *LL#"6"6 ""$388-
#	c''c.B.B4.HDJc''c.B.B4.HDJs   5J ?Kc                 j   [         R                  " U R                  U R                  5      n[         R                  " U5      (       a  [        S[        U5      -  5      eU R                  nU(       a  UR                  (       a,  [         R                  " U R                  UR                  5      nOS[         R                  " U R                  UR                  5       [        U5      [         R                  UR                  5      n[         R                  " U5      (       a  [        S[        U5      -  5      eg g )Nz(could not set compression parameters: %sz)could not load compression dictionary: %s)rM   &ZSTD_CCtx_setParametersUsingCCtxParamsr9  r   r  r   r   r6  _cdictZSTD_CCtx_refCDict!ZSTD_CCtx_loadDictionary_advancedas_bytesr6  ZSTD_dlm_byRef
_dict_type)r`   r   r9  s      rX   r7  ZstdCompressor._setup_cctxj  s    <<JJ
 G$$:g&' 
 OO	00Y=M=MN??JJ&&(	N&&(( ((?!'*+  ) rW   c                 B    [         R                  " U R                  5      $ )z}Obtain the memory usage of this compressor, in bytes.

>>> cctx = zstandard.ZstdCompressor()
>>> memory = cctx.memory_size()
)rM   r[  r9  r_   s    rX   r\  ZstdCompressor.memory_size       ##DJJ//rW   c                    [         R                  " U R                  [         R                  5        [        R
                  " U5      n[         R                  " [        U5      5      n[        SU5      n[         R                  " U R                  [        U5      5      n[         R                  " U5      (       a  [        S[        U5      -  5      e[        R                  " S5      n[        R                  " S5      nXFl        X6l        SUl        X'l        [        U5      Ul        SUl        [         R$                  " U R                  Xg[         R&                  5      n[         R                  " U5      (       a  [        S[        U5      -  5      eU(       a  [        S5      e[        R(                  " XFR                   5      SS $ )	a$  
Compress data in a single operation.

This is the simplest mechanism to perform compression: simply pass in a
value and get a compressed value back. It is almost the most prone to
abuse.

The input and output values must fit in memory, so passing in very large
values can result in excessive memory usage. For this reason, one of the
streaming based APIs is preferred for larger values.

:param data:
   Source data to compress
:return:
   Compressed data

>>> cctx = zstandard.ZstdCompressor()
>>> compressed = cctx.compress(b"data to compress")
r&  r(  r'  r  r   zcannot compress: %szunexpected partial frame flushN)rM   ZSTD_CCtx_resetr9  ZSTD_reset_session_onlyrL   r  ZSTD_compressBoundr6  new_nonzeror8  r  r   r   r!  r5  rw   r7  r  r  r  r  )r`   r  r  	dest_sizeoutr   r  r  s           rX   r  ZstdCompressor.compress  sK   ( 	DJJ(C(CDood+**3{+;<	(I.11$**c+>NOG$$/+g2FF  WW/0
GG-.	#
#[)		**JJ
s~~
 G$$1K4HHII<==zz#~~.q11rW   r   c                 V   [         R                  " U R                  [         R                  5        US:  a  [         R                  n[         R
                  " U R                  U5      n[         R                  " U5      (       a  [        S[        U5      -  5      e[        U [        5      nU$ )z
Obtain a compressor exposing the Python standard library compression API.

See :py:class:`ZstdCompressionObj` for the full documentation.

:param size:
   Size in bytes of data that will be compressed.
:return:
   :py:class:`ZstdCompressionObj`
r   r(  )rM   rJ  r9  rK  ZSTD_CONTENTSIZE_UNKNOWNr8  r  r   r   r   r&   )r`   rw   r   cobjs       rX   compressobjZstdCompressor.compressobj  s     	DJJ(C(CD!8//D11$**dCG$$/+g2FF  "$(KLrW   c                 D   [         R                  " U R                  [         R                  5        US:  a  [         R                  n[         R
                  " U R                  U5      n[         R                  " U5      (       a  [        S[        U5      -  5      e[        XS9$ )aG  
Create an object for iterative compressing to same-sized chunks.

This API is similar to :py:meth:`ZstdCompressor.compressobj` but has
better performance properties.

:param size:
   Size in bytes of data that will be compressed.
:param chunk_size:
   Size of compressed chunks.
:return:
   :py:class:`ZstdCompressionChunker`
r   r(  )r  )
rM   rJ  r9  rK  rR  r8  r  r   r   r	   )r`   rw   r  r   s       rX   chunkerZstdCompressor.chunker  s~     	DJJ(C(CD!8//D11$**dCG$$/+g2FF  &dBBrW   c                    [        US5      (       d  [        S5      e[        US5      (       d  [        S5      e[        R                  " U R                  [        R
                  5        US:  a  [        R                  n[        R                  " U R                  U5      n[        R                  " U5      (       a  [        S[        U5      -  5      e[        R                  " S5      n[        R                  " S5      n[        R                  " S	U5      n	Xl        XXl        SUl        S
u  p UR!                  U5      nU(       d  GO<[        R"                  " U5      nU
[%        U5      -  n
Xl        [%        U5      Ul        SUl        UR                  UR                  :  a  [        R(                  " U R                  X[        R*                  5      n[        R                  " U5      (       a  [        S[        U5      -  5      eUR                  (       aO  UR-                  [        R.                  " UR                  UR                  5      5        XR                  -  nSUl        UR                  UR                  :  a  M  GMV   [        R(                  " U R                  X[        R0                  5      n[        R                  " U5      (       a  [        S[        U5      -  5      eUR                  (       aO  UR-                  [        R.                  " UR                  UR                  5      5        XR                  -  nSUl        US:X  a   X4$ M  )a"  
Copy data between 2 streams while compressing it.

Data will be read from ``ifh``, compressed, and written to ``ofh``.
``ifh`` must have a ``read(size)`` method. ``ofh`` must have a
``write(data)``
method.

>>> cctx = zstandard.ZstdCompressor()
>>> with open(input_path, "rb") as ifh, open(output_path, "wb") as ofh:
...     cctx.copy_stream(ifh, ofh)

It is also possible to declare the size of the source stream:

>>> cctx = zstandard.ZstdCompressor()
>>> cctx.copy_stream(ifh, ofh, size=len_of_input)

You can also specify how large the chunks that are ``read()``
and ``write()`` from and to the streams:

>>> cctx = zstandard.ZstdCompressor()
>>> cctx.copy_stream(ifh, ofh, read_size=32768, write_size=16384)

The stream copier returns a 2-tuple of bytes read and written:

>>> cctx = zstandard.ZstdCompressor()
>>> read_count, write_count = cctx.copy_stream(ifh, ofh)

:param ifh:
   Source stream to read from
:param ofh:
   Destination stream to write to
:param size:
   Size in bytes of the source stream. If defined, compression
   parameters will be tuned for this size.
:param read_size:
   Chunk sizes that source stream should be ``read()`` from.
:param write_size:
   Chunk sizes that destination stream should be ``write()`` to.
:return:
   2-tuple of ints of bytes read and written, respectively.
r  (first argument must have a read() methodr  *second argument must have a write() methodr   r(  r  r'  r&  r  r  r  )r  rU   rM   rJ  r9  rK  rR  r8  r  r   r   rL   r!  r5  rw   r7  r  r  r6  r  r  r  r  r  r  )r`   ifhofhrw   r  r<  r   r  r  r!  
total_readr  r  r  s                 rX   copy_streamZstdCompressor.copy_stream  s   f sF##GHHsG$$IJJDJJ(C(CD!8//D11$**dCG$$/+g2FF  GG-.	WW/0
WWXz2
#$
"&
88I&D//$/K#k**J'M -INIM--)..022JJ
s7J7J ##G,,#1K4HH  >>IIcjjHI>>1K%&JN --)..0 4 ..

J3>>G ((9K<PP  ~~		#**Z^^Z^^DE~~-!"
!|&&# rW   Tc                 ~   [         R                  " U R                  [         R                  5         [	        U5      nUS:  a  [         R                  n[         R                  " U R                  U5      n[         R                  " U5      (       a  [        S[        U5      -  5      e[        XX4S9$ ! [
         a     Nf = f)a  
Wrap a readable source with a stream that can read compressed data.

This will produce an object conforming to the ``io.RawIOBase``
interface which can be ``read()`` from to retrieve compressed data
from a source.

The source object can be any object with a ``read(size)`` method
or an object that conforms to the buffer protocol.

See :py:class:`ZstdCompressionReader` for type documentation and usage
examples.

:param source:
   Object to read source data from
:param size:
   Size in bytes of source object.
:param read_size:
   How many bytes to request when ``read()``'ing from the source.
:param closefd:
   Whether to close the source stream when the returned stream is
   closed.
:return:
   :py:class:`ZstdCompressionReader`
r   r(  r>  )rM   rJ  r9  rK  r6  	ExceptionrR  r8  r  r   r   r   )r`   r  rw   r  r>  r   s         rX   stream_readerZstdCompressor.stream_readerv  s    @ 	DJJ(C(CD	v;D !8//D11$**dCG$$/+g2FF  %T9NN  		s   B/ /
B<;B<c           	          [        US5      (       d  [        S5      e[        R                  " U R                  [        R
                  5        US:  a  [        R                  n[        XX#XES9$ )a  
Create a stream that will write compressed data into another stream.

The argument to ``stream_writer()`` must have a ``write(data)`` method.
As compressed data is available, ``write()`` will be called with the
compressed data as its argument. Many common Python types implement
``write()``, including open file handles and ``io.BytesIO``.

See :py:class:`ZstdCompressionWriter` for more documentation, including
usage examples.

:param writer:
   Stream to write compressed data to.
:param size:
   Size in bytes of data to be compressed. If set, it will be used
   to influence compression parameter tuning and could result in the
   size being written into the header of the compressed data.
:param write_size:
   How much data to ``write()`` to ``writer`` at a time.
:param write_return_read:
   Whether ``write()`` should return the number of bytes that were
   consumed from the input.
:param closefd:
   Whether to ``close`` the ``writer`` when this stream is closed.
:return:
   :py:class:`ZstdCompressionWriter`
r  )must pass an object with a write() methodr   rb  )r  rU   rM   rJ  r9  rK  rR  r   )r`   r;  rw   r<  r=  r>  s         rX   stream_writerZstdCompressor.stream_writer  s_    F vw''HIIDJJ(C(CD!8//D$$,=
 	
rW   c              #   @  #    [        US5      (       a  SnO,[        US5      (       a  SnSn[        U5      nO[        S5      e[        R                  " U R
                  [        R                  5        US:  a  [        R                  n[        R                  " U R
                  U5      n[        R                  " U5      (       a  [        S[        U5      -  5      e[        R                  " S5      n[        R                  " S	5      n	[        R                  Ul        SUl        SUl        [        R                  " S
U5      n
Xl        XIl        SU	l         U	R"                  S:X  d   eU(       a  UR'                  U5      nO#[        U5      W-
  n[)        X5      nXXm-    nXm-  nU(       d  GO*[        R*                  " U5      nXl        [        U5      Ul        SUl        UR"                  UR                   :  a  [        R,                  " U R
                  X[        R.                  5      n[        R                  " U5      (       a  [        S[        U5      -  5      eU	R"                  (       a9  [        R0                  " U	R$                  U	R"                  5      SS nSU	l        Uv   UR"                  UR                   :  a  M  U	R"                  S:X  d   eGM   U	R"                  S:X  d   e[        R,                  " U R
                  X[        R2                  5      n[        R                  " U5      (       a  [        S[        U5      -  5      eU	R"                  (       a9  [        R0                  " U	R$                  U	R"                  5      SS nSU	l        Uv   US:X  a  gM  7f)a  
Read uncompressed data from a reader and return an iterator

Returns an iterator of compressed data produced from reading from
``reader``.

This method provides a mechanism to stream compressed data out of a
source as an iterator of data chunks.

Uncompressed data will be obtained from ``reader`` by calling the
``read(size)`` method of it or by reading a slice (if ``reader``
conforms to the *buffer protocol*). The source data will be streamed
into a compressor. As compressed data is available, it will be exposed
to the iterator.

Data is read from the source in chunks of ``read_size``. Compressed
chunks are at most ``write_size`` bytes. Both values default to the
zstd input and and output defaults, respectively.

If reading from the source via ``read()``, ``read()`` will be called
until it raises or returns an empty bytes (``b""``). It is perfectly
valid for the source to deliver fewer bytes than were what requested
by ``read(size)``.

The caller is partially in control of how fast data is fed into the
compressor by how it consumes the returned iterator. The compressor
will not consume from the reader unless the caller consumes from the
iterator.

>>> cctx = zstandard.ZstdCompressor()
>>> for chunk in cctx.read_to_iter(fh):
...     # Do something with emitted data.

``read_to_iter()`` accepts a ``size`` argument declaring the size of
the input stream:

>>> cctx = zstandard.ZstdCompressor()
>>> for chunk in cctx.read_to_iter(fh, size=some_int):
>>>     pass

You can also control the size that data is ``read()`` from the source
and the ideal size of output chunks:

>>> cctx = zstandard.ZstdCompressor()
>>> for chunk in cctx.read_to_iter(fh, read_size=16384, write_size=8192):
>>>     pass

``read_to_iter()`` does not give direct control over the sizes of chunks
fed into the compressor. Instead, chunk sizes will be whatever the object
being read from delivers. These will often be of a uniform size.

:param reader:
   Stream providing data to be compressed.
:param size:
   Size in bytes of input data.
:param read_size:
   Controls how many bytes are ``read()`` from the source.
:param write_size:
   Controls the output size of emitted chunks.
:return:
   Iterator of ``bytes``.
r  Tr   Fr   Gmust pass an object with a read() method or conforms to buffer protocolr(  r  r'  r&  r  Nr  )r  r6  rU   rM   rJ  r9  rK  rR  r8  r  r   r   rL   r!  r   r  rw   r7  r5  r  minr  r  r  r  r  )r`   readerrw   r  r<  	have_readbuffer_offsetr   r  r  r!  read_result	remaining
slice_sizeread_bufferr  s                   rX   read_to_iterZstdCompressor.read_to_iter  s    L 66""IV]++IMv;D. 
 	DJJ(C(CD!8//D11$**dCG$$/+g2FF  GG-.	WW/0
			WWXz2
#$
 >>Q&&& $kk)4K-7	 6
$]5OP+  //+6K'M -INIM--)..022JJ
s7J7J ##G,,#1K4HH  >>::jnnjnnEaHD%&JNJ --)..0 >>Q&&&  >>Q&&&..

J3>>G ((9K<PP  ~~zz*..*..A!D!"

!|! s   J=NCNc                     [        5       e)a  
Compress multiple pieces of data as a single function call.

(Experimental. Not yet supported by CFFI backend.)

This function is optimized to perform multiple compression operations
as as possible with as little overhead as possible.

Data to be compressed can be passed as a ``BufferWithSegmentsCollection``,
a ``BufferWithSegments``, or a list containing byte like objects. Each
element of the container will be compressed individually using the
configured parameters on the ``ZstdCompressor`` instance.

The ``threads`` argument controls how many threads to use for
compression. The default is ``0`` which means to use a single thread.
Negative values use the number of logical CPUs in the machine.

The function returns a ``BufferWithSegmentsCollection``. This type
represents N discrete memory allocations, each holding 1 or more
compressed frames.

Output data is written to shared memory buffers. This means that unlike
regular Python objects, a reference to *any* object within the collection
keeps the shared buffer and therefore memory backing it alive. This can
have undesirable effects on process memory usage.

The API and behavior of this function is experimental and will likely
change. Known deficiencies include:

* If asked to use multiple threads, it will always spawn that many
  threads, even if the input is too small to use them. It should
  automatically lower the thread count when the extra threads would
  just add overhead.
* The buffer allocation strategy is fixed. There is room to make it
  dynamic, perhaps even to allow one output buffer per input,
  facilitating a variation of the API to return a list without the
  adverse effects of shared memory buffers.

:param data:
   Source to read discrete pieces of data to compress.

   Can be a ``BufferWithSegmentsCollection``, a ``BufferWithSegments``,
   or a ``list[bytes]``.
:return:
   BufferWithSegmentsCollection holding compressed data.
r]   )r`   r  r   s      rX   multi_compress_to_buffer'ZstdCompressor.multi_compress_to_buffer	  s    ^ "##rW   c                     [         R                  " U R                  5      nUR                  UR                  UR
                  4$ )z
Return information on how much work the compressor has done.

Returns a 3-tuple of (ingested, consumed, produced).

>>> cctx = zstandard.ZstdCompressor()
>>> (ingested, consumed, produced) = cctx.frame_progression()
)rM   ZSTD_getFrameProgressionr9  ingestedconsumedproduced)r`   progressions     rX   frame_progression ZstdCompressor.frame_progression	  s7     224::>##[%9%9;;O;OOOrW   )r9  r6  r   )   NNNNNr   r  )rk   rl   rm   rn   ro   r   r7  r\  r  rT  r&   rW  r%   r_  rd  rh  rt  rw  r  rq   rV   rW   rX   r   r     s    AJ Wr<052n4 *M C> 46x'z 40Oj 6-
d 46gR/$bPrW   r   c                       \ rS rSrSrS rSrg)r   i	  a  Information about a zstd frame.

Instances have the following attributes:

``content_size``
   Integer size of original, uncompressed content. This will be ``0`` if the
   original content size isn't written to the frame (controlled with the
   ``write_content_size`` argument to ``ZstdCompressor``) or if the input
   content size was ``0``.

``window_size``
   Integer size of maximum back-reference distance in compressed data.

``dict_id``
   Integer of dictionary ID used for compression. ``0`` if no dictionary
   ID was used or if the dictionary ID was ``0``.

``has_checksum``
   Bool indicating whether a 4 byte content checksum is stored at the end
   of the frame.
c                     UR                   U l        UR                  U l        UR                  U l        [        UR                  5      U l        g rz   )	frameContentSizecontent_size
windowSizewindow_sizedictIDdict_idr,  checksumFlaghas_checksum)r`   fparamss     rX   r   FrameParameters.__init__	  s<    #44"--~~ !5!56rW   )r  r  r  r  N)rk   rl   rm   rn   ro   r   rq   rV   rW   rX   r   r   	  s    ,7rW   r   c                     [         R                  " U 5      n[        R                  " U[	        U5      5      nU[        R
                  :X  a  [        S5      eU[        R                  :X  a  gU$ )zObtain the decompressed size of a frame.

The returned value is usually accurate. But strictly speaking it should
not be trusted.

:return:
   ``-1`` if size unknown and a non-negative integer otherwise.
z#error when determining content sizer   )rL   r  rM   ZSTD_getFrameContentSizer6  ZSTD_CONTENTSIZE_ERRORr   rR  )r  r  rw   s      rX   r   r   	  sY     //$'K''S5EFDs)))=>>	--	-rW   c                     [         R                  " U 5      n[        R                  " U[	        U5      5      n[        R
                  " U5      (       a  [        S[        U5      -  5      eU$ )zGObtain the size of a frame header.

:return:
   Integer size in bytes.
z)could not determine frame header size: %s)rL   r  rM   ZSTD_frameHeaderSizer6  r  r   r   )r  r  r   s      rX   r   r   	  s[     //$'K&&{C4DEG
  7+g:NN
 	
 NrW   c                 D   [         R                  " S5      n[         R                  " U 5      n[        R                  " X[        U5      5      n[        R                  " U5      (       a  [        S[        U5      -  5      eU(       a  [        SU-  5      e[        US   5      $ )a  
Parse a zstd frame header into frame parameters.

Depending on which fields are present in the frame and their values, the
length of the frame parameters varies. If insufficient bytes are passed
in to fully parse the frame parameters, ``ZstdError`` is raised. To ensure
frame parameters can be parsed, pass in at least 18 bytes.

:param data:
   Data from which to read frame parameters.
:return:
   :py:class:`FrameParameters`
ZSTD_frameHeader *zcannot get frame parameters: %sz3not enough data for frame parameters; need %d bytesr   )
rL   r!  r  rM   ZSTD_getFrameHeaderr6  r  r   r   r   )r  r   r  r   s       rX   r   r   
  s     WW)*F//$'K%%f3{;KLG
  -G0DD
 	
 AGK
 	
 6!9%%rW   c                   T    \ rS rSrSr\SS4S jrS rS rS r	SS	 jr
\S
 5       rSrg)r
   i"
  a  Represents a computed compression dictionary.

Instances are obtained by calling :py:func:`train_dictionary` or by
passing bytes obtained from another source into the constructor.

Instances can be constructed from bytes:

>>> dict_data = zstandard.ZstdCompressionDict(data)

It is possible to construct a dictionary from *any* data. If the data
doesn't begin with a magic header, it will be treated as a *prefix*
dictionary. *Prefix* dictionaries allow compression operations to
reference raw data within the dictionary.

It is possible to force the use of *prefix* dictionaries or to require
a dictionary header:

>>> dict_data = zstandard.ZstdCompressionDict(data, dict_type=zstandard.DICT_TYPE_RAWCONTENT)
>>> dict_data = zstandard.ZstdCompressionDict(data, dict_type=zstandard.DICT_TYPE_FULLDICT)

You can see how many bytes are in the dictionary by calling ``len()``:

>>> dict_data = zstandard.train_dictionary(size, samples)
>>> dict_size = len(dict_data)  # will not be larger than ``size``

Once you have a dictionary, you can pass it to the objects performing
compression and decompression:

>>> dict_data = zstandard.train_dictionary(131072, samples)
>>> cctx = zstandard.ZstdCompressor(dict_data=dict_data)
>>> for source_data in input_data:
...     compressed = cctx.compress(source_data)
...     # Do something with compressed data.
...
>>> dctx = zstandard.ZstdDecompressor(dict_data=dict_data)
>>> for compressed_data in input_data:
...     buffer = io.BytesIO()
...     with dctx.stream_writer(buffer) as decompressor:
...         decompressor.write(compressed_data)
...         # Do something with raw data in ``buffer``.

Dictionaries have unique integer IDs. You can retrieve this ID via:

>>> dict_id = zstandard.dictionary_id(dict_data)

You can obtain the raw data in the dict (useful for persisting and constructing
a ``ZstdCompressionDict`` later) via ``as_bytes()``:

>>> dict_data = zstandard.train_dictionary(size, samples)
>>> raw_data = dict_data.as_bytes()

By default, when a ``ZstdCompressionDict`` is *attached* to a
``ZstdCompressor``, each ``ZstdCompressor`` performs work to prepare the
dictionary for use. This is fine if only 1 compression operation is being
performed or if the ``ZstdCompressor`` is being reused for multiple operations.
But if multiple ``ZstdCompressor`` instances are being used with the dictionary,
this can add overhead.

It is possible to *precompute* the dictionary so it can readily be consumed
by multiple ``ZstdCompressor`` instances:

>>> d = zstandard.ZstdCompressionDict(data)
>>> # Precompute for compression level 3.
>>> d.precompute_compress(level=3)
>>> # Precompute with specific compression parameters.
>>> params = zstandard.ZstdCompressionParameters(...)
>>> d.precompute_compress(compression_params=params)

.. note::

   When a dictionary is precomputed, the compression parameters used to
   precompute the dictionary overwrite some of the compression parameters
   specified to ``ZstdCompressor``.

:param data:
   Dictionary data.
:param dict_type:
   Type of dictionary. One of the ``DICT_TYPE_*`` constants.
r   c                     [        U[        5      (       d   eXl        X0l        X@l        U[
        [        [        4;  a  [        S5      eX l	        S U l
        g )Nz@invalid dictionary load mode: %d; must use DICT_TYPE_* constants)
isinstancebytes_datakdrF   rG   rH   rU   rD  r?  )r`   r  	dict_typer  r  s        rX   r   ZstdCompressionDict.__init__s
  s\    $&&&&
 
 

 ( 
 $rW   c                 ,    [        U R                  5      $ rz   )r6  r  r_   s    rX   re   ZstdCompressionDict.__len__
  s    4::rW   c                 |    [        [        R                  " U R                  [	        U R                  5      5      5      $ )z(Obtain the integer ID of the dictionary.)intrM   ZDICT_getDictIDr  r6  r_   s    rX   r  ZstdCompressionDict.dict_id
  s&    3&&tzz3tzz?CDDrW   c                     U R                   $ )z6Obtain the ``bytes`` representation of the dictionary.)r  r_   s    rX   rB  ZstdCompressionDict.as_bytes
  s    zzrW   Nc                 \   U(       a  U(       a  [        S5      eU(       d  U(       d  [        S5      eU(       a,  [        R                  " US[        U R                  5      5      nO[
        R                  " S5      nUR                  Ul        UR                  Ul
        UR                  Ul        UR                  Ul        UR                  Ul        UR                   Ul        UR$                  Ul        [        R(                  " U R                  [        U R                  5      [        R*                  U R,                  U[        R.                  5      nU[
        R0                  :X  a  [3        S5      e[
        R4                  " U[        R6                  [        R8                  " U5      S9U l        g)zPrecompute a dictionary os it can be used by multiple compressors.

Calling this method on an instance that will be used by multiple
:py:class:`ZstdCompressor` instances will improve performance.
z4must only specify one of level or compression_paramsz/must specify one of level or compression_paramsr   ZSTD_compressionParameterszunable to precompute dictionaryr3  N)rU   rM   r   r6  r  rL   r!  r   r   r   r   r   r   r   r   r   r   r   r   r   ZSTD_createCDict_advancedrC  rD  ZSTD_defaultCMemr   r   r   ZSTD_freeCDictZSTD_sizeof_CDictr?  )r`   r   r:  cparamscdicts        rX   precompute_compress'ZstdCompressionDict.precompute_compress
  s=    'I  /NOO))%C

ODGgg:;G1;;G099GO1;;G 2 = =G1::G#5#C#CG  2 = =G--JJ

OOO  
 CHH=>>ff3%%C,A,A%,H
rW   c                    [         R                  " U R                  [        U R                  5      [         R                  U R
                  [         R                  5      nU[        R                  :X  a  [        S5      e[        R                  " U[         R                  [         R                  " U5      S9nXR                  S'   U$ )Nz#could not create decompression dictr3  _ddict)rM   ZSTD_createDDict_advancedr  r6  rC  rD  r  rL   r   r   r   ZSTD_freeDDictZSTD_sizeof_DDict__dict__)r`   ddicts     rX   r  ZstdCompressionDict._ddict
  s    --JJ

OOO  
 CHHABB3%%C,A,A%,H
 #(hrW   )r?  r  rD  r  r  )r   N)rk   rl   rm   rn   ro   rF   r   re   r  rB  r  rp   r  rq   rV   rW   rX   r
   r
   "
  sA    N` (6a &E'
R  rW   r
   c           
      8   [        U[        5      (       d  [        S5      eUS:  a
  [        5       nU
(       d(  U(       d!  U=(       d    SnU
=(       d    Sn
U	=(       d    Sn	[	        [        [        U5      5      n[        SU5      n[        S[        U5      5      nSn[        U5       HU  u  nn[        U[        5      (       d  [        S5      e[        U5      n[        R                  " X-   UU5        UU-  nUUU'   MW     [        SU 5      n[        R                  " S	5      S   nUUl        UUl        UUl        U
Ul        UUl        UUl        UUl        UUR*                  l        UUR*                  l        U	UR*                  l        [2        R4                  " [        R6                  " U5      U [        R6                  " U5      [        R6                  " US5      [        U5      [        R6                  " U5      5      n[2        R8                  " U5      (       aG  [        R:                  " [2        R<                  " U5      5      R?                  S
5      n[A        SU-  5      e[C        [        RD                  " UU5      SS [F        UR                  UR                  S9$ )a  Train a dictionary from sample data using the COVER algorithm.

A compression dictionary of size ``dict_size`` will be created from the
iterable of ``samples``. The raw dictionary bytes will be returned.

The dictionary training mechanism is known as *cover*. More details about it
are available in the paper *Effective Construction of Relative Lempel-Ziv
Dictionaries* (authors: Liao, Petri, Moffat, Wirth).

The cover algorithm takes parameters ``k`` and ``d``. These are the
*segment size* and *dmer size*, respectively. The returned dictionary
instance created by this function has ``k`` and ``d`` attributes
containing the values for these parameters. If a ``ZstdCompressionDict``
is constructed from raw bytes data (a content-only dictionary), the
``k`` and ``d`` attributes will be ``0``.

The segment and dmer size parameters to the cover algorithm can either be
specified manually or ``train_dictionary()`` can try multiple values
and pick the best one, where *best* means the smallest compressed data size.
This later mode is called *optimization* mode.

Under the hood, this function always calls
``ZDICT_optimizeTrainFromBuffer_fastCover()``. See the corresponding C library
documentation for more.

If neither ``steps`` nor ``threads`` is defined, defaults for ``d``, ``steps``,
and ``level`` will be used that are equivalent with what
``ZDICT_trainFromBuffer()`` would use.


:param dict_size:
   Target size in bytes of the dictionary to generate.
:param samples:
   A list of bytes holding samples the dictionary will be trained from.
:param k:
   Segment size : constraint: 0 < k : Reasonable range [16, 2048+]
:param d:
   dmer size : constraint: 0 < d <= k : Reasonable range [6, 16]
:param f:
   log of size of frequency array : constraint: 0 < f <= 31 : 1 means
   default(20)
:param split_point:
   Percentage of samples used for training: Only used for optimization.
   The first # samples * ``split_point`` samples will be used to training.
   The last # samples * (1 - split_point) samples will be used for testing.
   0 means default (0.75), 1.0 when all samples are used for both training
   and testing.
:param accel:
   Acceleration level: constraint: 0 < accel <= 10. Higher means faster
   and less accurate, 0 means default(1).
:param dict_id:
   Integer dictionary ID for the produced dictionary. Default is 0, which uses
   a random value.
:param steps:
   Number of steps through ``k`` values to perform when trying parameter
   variations.
:param threads:
   Number of threads to use when trying parameter variations. Default is 0,
   which means to use a single thread. A negative value can be specified to
   use as many threads as there are detected logical CPUs.
:param level:
   Integer target compression level when trying parameter variations.
:param notifications:
   Controls writing of informational messages to ``stderr``. ``0`` (the
   default) means to write nothing. ``1`` writes errors. ``2`` writes
   progression info. ``3`` writes more details. And ``4`` writes all info.
zsamples must be a listr         r  r&  zsize_t[]zsamples must be byteszZDICT_fastCover_params_t *r   zcannot train dict: %sN)r  r  r  )$r  list	TypeErrorrY   summapr6  rM  	enumerater  rU   rL   r)  r!  r  r  rd  steps	nbThreads
splitPointaccelzParamsnotificationLevelr  compressionLevelrM   'ZDICT_optimizeTrainFromBuffer_fastCover	addressofZDICT_isErrorr   ZDICT_getErrorNamer   r   r
   r  rH   )r   samplesr  r  rd  split_pointr  notificationsr  r   r  r   
total_sizesamples_buffersample_sizesra   r~   samplelr9  dparamsr   msgs                          rX   r   r   
  s   d gt$$011{,F

Sg&'J :6Nz3w<8LFw'	6&%((455KN+VQ7!Q ( Hi0Igg23A6GGIGIGIGMG$GGM(5GOO%$GOO',GOO$99i n%lA&GgG !!jj//89@@I/#566

9g&q)$
))
))	 rW   c                   ^    \ rS rSrSrS rS rSS jr\S 5       r	\S 5       r
\S 5       rS	rg
)r   i^  a  A standard library API compatible decompressor.

This type implements a compressor that conforms to the API by other
decompressors in Python's standard library. e.g. ``zlib.decompressobj``
or ``bz2.BZ2Decompressor``. This allows callers to use zstd compression
while conforming to a similar API.

Compressed data chunks are fed into ``decompress(data)`` and
uncompressed output (or an empty bytes) is returned. Output from
subsequent calls needs to be concatenated to reassemble the full
decompressed byte sequence.

If ``read_across_frames=False``, each instance is single use: once an
input frame is decoded, ``decompress()`` will raise an exception. If
``read_across_frames=True``, instances can decode multiple frames.

>>> dctx = zstandard.ZstdDecompressor()
>>> dobj = dctx.decompressobj()
>>> data = dobj.decompress(compressed_chunk_0)
>>> data = dobj.decompress(compressed_chunk_1)

By default, calls to ``decompress()`` write output data in chunks of size
``DECOMPRESSION_RECOMMENDED_OUTPUT_SIZE``. These chunks are concatenated
before being returned to the caller. It is possible to define the size of
these temporary chunks by passing ``write_size`` to ``decompressobj()``:

>>> dctx = zstandard.ZstdDecompressor()
>>> dobj = dctx.decompressobj(write_size=1048576)

.. note::

   Because calls to ``decompress()`` may need to perform multiple
   memory (re)allocations, this streaming decompression API isn't as
   efficient as other APIs.
c                 D    Xl         X l        SU l        X0l        SU l        g )NFrW   )_decompressorr+  r  _read_across_frames_unused_input)r`   decompressorr<  read_across_framess       rX   r   ZstdDecompressionObj.__init__  s#    )%#5  rW   c                    U R                   (       a  [        S5      e[        R                  " S5      n[        R                  " S5      n[        R                  " U5      n[        U5      S:X  a  gXBl        [        U5      Ul        SUl        [        R                  " SU R                  5      nXSl
        [        U5      Ul        SUl        / n [        R                  " U R                  R                  X25      n[        R                  " U5      (       a  [        S[!        U5      -  5      eUR                  (       a=  UR#                  [        R$                  " UR                  UR                  5      S	S	 5        US:X  a=  U R&                  (       d,  SU l         S	U l        XR                  UR                   U l        OyUS:X  a4  U R&                  (       a#  UR                  UR                  :X  a  OGSUl        O<UR                  UR                  :X  a  UR                  UR                  :  a  O
SUl        GMg  SR+                  U5      $ )
zSend compressed data to the decompressor and obtain decompressed data.

:param data:
   Data to feed into the decompressor.
:return:
   Decompressed bytes.
z)cannot use a decompressobj multiple timesr  r'  r   rW   r&  Tzstd decompressor error: %sN)r  r   rL   r!  r  r6  r  rw   r7  r+  r5  rM   ZSTD_decompressStreamr  _dctxr  r   r  r  r  r  r  )r`   r  r  r  r  r!  r  r   s           rX   
decompressZstdDecompressionObj.decompress  s    >>GHHGG-.	WW/0
ood+{q #[)		WWXt'7'78
#j/

//""((*G ((1K4HH 
 ~~cjjHKL
 !|D$<$< "&%)"%)--)..%I"A$":": ==INN2%&JN /NNZ__4!"
W Z xxrW   c                     g)zEffectively a no-op.

Implemented for compatibility with the standard library APIs.

Safe to call at any time.

:return:
   Empty bytes.
rW   rV   )r`   lengths     rX   ri  ZstdDecompressionObj.flush  s     rW   c                     U R                   $ )zBytes past the end of compressed data.

If ``decompress()`` is fed additional data beyond the end of a zstd
frame, this value will be non-empty once ``decompress()`` fully decodes
the input frame.
)r  r_   s    rX   unused_data ZstdDecompressionObj.unused_data  s     !!!rW   c                     g)z5Data that has not yet been fed into the decompressor.rW   rV   r_   s    rX   unconsumed_tail$ZstdDecompressionObj.unconsumed_tail  s     rW   c                     U R                   $ )z?Whether the end of the compressed data stream has been reached.)r  r_   s    rX   eofZstdDecompressionObj.eof  s     ~~rW   )r  r  r  r  r+  N)r   )rk   rl   rm   rn   ro   r   r  ri  rp   r  r  r  rq   rV   rW   rX   r   r   ^  sU    "H!K Z
 " "    rW   r   c                       \ rS rSrSr SS jrS rS rS rS r	S r
SS	 jrSS
 jrS rS rS rS rS r\S 5       rS rS rS rS r\rS rS rSS jrS rSS jrS r\R@                  4S jr!Sr"g) r   i  a`	  Read only decompressor that pull uncompressed data from another stream.

This type provides a read-only stream interface for performing transparent
decompression from another stream or data source. It conforms to the
``io.RawIOBase`` interface. Only methods relevant to reading are
implemented.

>>> with open(path, 'rb') as fh:
>>> dctx = zstandard.ZstdDecompressor()
>>> reader = dctx.stream_reader(fh)
>>> while True:
...     chunk = reader.read(16384)
...     if not chunk:
...         break
...     # Do something with decompressed chunk.

The stream can also be used as a context manager:

>>> with open(path, 'rb') as fh:
...     dctx = zstandard.ZstdDecompressor()
...     with dctx.stream_reader(fh) as reader:
...         ...

When used as a context manager, the stream is closed and the underlying
resources are released when the context manager exits. Future operations
against the stream will fail.

The ``source`` argument to ``stream_reader()`` can be any object with a
``read(size)`` method or any object implementing the *buffer protocol*.

If the ``source`` is a stream, you can specify how large ``read()`` requests
to that stream should be via the ``read_size`` argument. It defaults to
``zstandard.DECOMPRESSION_RECOMMENDED_INPUT_SIZE``.:

>>> with open(path, 'rb') as fh:
...     dctx = zstandard.ZstdDecompressor()
...     # Will perform fh.read(8192) when obtaining data for the decompressor.
...     with dctx.stream_reader(fh, read_size=8192) as reader:
...         ...

Instances are *partially* seekable. Absolute and relative positions
(``SEEK_SET`` and ``SEEK_CUR``) forward of the current position are
allowed. Offsets behind the current read position and offsets relative
to the end of stream are not allowed and will raise ``ValueError``
if attempted.

``tell()`` returns the number of decompressed bytes read so far.

Not all I/O methods are implemented. Notably missing is support for
``readline()``, ``readlines()``, and linewise iteration support. This is
because streams operate on binary data - not text data. If you want to
convert decompressed output to text, you can chain an ``io.TextIOWrapper``
to the stream:

>>> with open(path, 'rb') as fh:
...     dctx = zstandard.ZstdDecompressor()
...     stream_reader = dctx.stream_reader(fh)
...     text_stream = io.TextIOWrapper(stream_reader, encoding='utf-8')
...     for line in text_stream:
...         ...
c                     Xl         X l        X0l        [        U5      U l        [        U5      U l        SU l        SU l        SU l        SU l	        SU l
        [        R                  " S5      U l        S U l        g r  )r  r  r  r,  r  r.  r/  r1  _bytes_decompressedr  r  rL   r!  r  r  )r`   r  r  r  r  r>  s         rX   r    ZstdDecompressionReader.__init__7  sl     *##'(:#; W#$ $ %''"34"rW   c                     U R                   (       a  [        S5      eU R                  (       a  [        S5      eSU l         U $ r  r  r_   s    rX   rE  !ZstdDecompressionReader.__enter__M  r  rW   c                 N    SU l         S U l        U R                  5         S U l        grH  )r/  r  rI  r  rJ  s       rX   rN   ZstdDecompressionReader.__exit__W  s$    !

rW   c                     gr  rV   r_   s    rX   ru   ZstdDecompressionReader.readable_  r  rW   c                     grH  rV   r_   s    rX   r   ZstdDecompressionReader.writableb  rs  rW   c                     grH  rV   r_   s    rX   r   ZstdDecompressionReader.seekablee  rs  rW   c                 ,    [         R                  " 5       erz   rQ  rx  s     rX   ry   ZstdDecompressionReader.readlineh  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r|  s     rX   r~  !ZstdDecompressionReader.readlinesk  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r  s     rX   r  ZstdDecompressionReader.writen  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r  s     rX   r  "ZstdDecompressionReader.writelinesq  rV  rW   c                     grH  rV   r_   s    rX   rq  ZstdDecompressionReader.isattyt  rs  rW   c                     g rz   rV   r_   s    rX   ri  ZstdDecompressionReader.flushw  r  rW   c                     U R                   (       a  g SU l         [        U R                  SS 5      nU R                  (       a  U(       a  U" 5         g g g r  r  rc  s     rX   rI  ZstdDecompressionReader.closez  s:    <<DLL'40==QC =rW   c                     U R                   $ rz   rl  r_   s    rX   rm  ZstdDecompressionReader.closed  ro  rW   c                     U R                   $ rz   )r  r_   s    rX   r  ZstdDecompressionReader.tell  s    '''rW   c                     / n U R                  S5      nU(       d  OUR                  U5        M,  SR                  U5      $ r
  r  r  s      rX   r  ZstdDecompressionReader.readall  r  rW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   rT   ZstdDecompressionReader.__iter__  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   rX   ZstdDecompressionReader.__next__  rV  rW   c                    U R                   R                  U R                   R                  :  a  g U R                  (       a  g [	        U R
                  S5      (       a  U R
                  R                  U R                  5      nU(       d  SU l        g [        R                  " U5      U l
        U R                  U R                   l        [        U R                  5      U R                   l        SU R                   l        g [        R                  " U R
                  5      U l
        U R                  U R                   l        [        U R                  5      U R                   l        SU R                   l        g r  )r  r7  rw   r  r  r  r  r  rL   r  r  r  r6  r  s     rX   r  #ZstdDecompressionReader._read_input  s    ??!5!55  4<<((<<$$T__5D'+$"%//$"7D"&"5"5DOO#&t':':#;DOO "#DOO"%//$,,"?D"&"5"5DOO#&t':':#;DOO "#DOOrW   c                    [         R                  " U R                  R                  XR                  5      nU R                  R
                  U R                  R                  :X  aj  [        R                  U R                  l	        SU R                  l        SU R                  l        SU l
        [        U R                  S5      (       d  SU l        [         R                  " U5      (       a  [        S[!        U5      -  5      eUR
                  =(       a=    UR
                  UR                  :H  =(       d    US:H  =(       a    U R"                  (       + $ )zlDecompress available input into an output buffer.

Returns True if data in output buffer should be emitted.
r   Nr  Tzstd decompress error: %s)rM   r  r  r  r  r7  rw   rL   r   r  r  r  r  r  r  r   r   r  )r`   r  r   s      rX   _decompress_into_buffer/ZstdDecompressionReader._decompress_into_buffer  s    
 ++$$j//
 ??$//"6"66"%((DOO"#DOO#$DOO "&D4<<00'+$G$$7+g:NNOO
 ~~ 
NNjoo- -!| -,,,	
rW   c                    U R                   (       a  [        S5      eUS:  a  [        S5      eUS:X  a  U R                  5       $ U R                  (       d  US:X  a  g[        R
                  " SU5      n[        R
                  " S5      nX#l        Xl        SUl        U R                  5         U R                  U5      (       aM  U =R                  UR                  -  sl        [        R                  " UR                  UR                  5      S S  $ U R                  (       d  U R                  5         U R                  U5      (       aM  U =R                  UR                  -  sl        [        R                  " UR                  UR                  5      S S  $ U R                  (       d  M  U =R                  UR                  -  sl        [        R                  " UR                  UR                  5      S S  $ NrB  r   r  r   rW   r&  r'  )r1  rU   r  r  rL   r!  r5  rw   r7  r  r  r  r  r  r`   rw   r!  r  s       rX   r  ZstdDecompressionReader.read  sk   <</00"9HII2:<<>!  DAI WWXt,
WW/0
#
''
33$$
6$::jnnjnn=a@@&&++J77((JNN:(zz*..*..A!DD	 &&& 	  JNN2 zz*..*..9!<<rW   c                    U R                   (       a  [        S5      eU R                  (       a  g[        R                  " U5      n[        R
                  " USS5        [        R                  " S5      nX#l        [        U5      Ul	        SUl
        U R                  5         U R                  U5      (       a+  U =R                  UR                  -  sl        UR                  $ U R                  (       dd  U R                  5         U R                  U5      (       a+  U =R                  UR                  -  sl        UR                  $ U R                  (       d  Md  U =R                  UR                  -  sl        UR                  $ NrB  r   rW   r'  )r1  rU   r  rL   r  r)  r!  r5  r6  rw   r7  r  r  r  r  r`   r  r+  r  s       rX   r   ZstdDecompressionReader.readinto  s   <</00   ooa(AsAWW/0
$k*

''
33$$
6$>>!&&++J77((JNN:(!~~%	 &&& 	  JNN2 ~~rW   c                 l   U R                   (       a  [        S5      eUS:  a  [        S5      eU R                  (       d  US:X  a  gUS:X  a  [        n[        R
                  " SU5      n[        R
                  " S5      nX#l        Xl        SUl        U R                  (       dF  U R                  5         U R                  U5        UR                  (       a  OU R                  (       d  MF  U =R                  UR                  -  sl        [        R                  " UR                  UR                  5      S S  $ r  )r1  rU   r  r(   rL   r!  r5  rw   r7  r  r  r  r  r  r  s       rX   r$  ZstdDecompressionReader.read1  s    <</00"9HII  DAI 2:8DWWXt,
WW/0
#
 &&((4~~ &&& 	  JNN2 zz*..*..9!<<rW   c                 h   U R                   (       a  [        S5      eU R                  (       a  g[        R                  " U5      n[        R
                  " USS5        [        R                  " S5      nX#l        [        U5      Ul	        SUl
        U R                  (       dh  U R                  (       dW  U R                  5         U R                  U5        UR                  (       a  O$U R                  (       d  U R                  (       d  MW  U =R                  UR                  -  sl        UR                  $ r!  )r1  rU   r  rL   r  r)  r!  r5  r6  rw   r7  r  r  r  r  r"  s       rX   r.  !ZstdDecompressionReader.readinto1;  s    <</00   ooa(AsAWW/0
$k*

&&t/D/D((4~~ &&t/D/D/D 	  JNN2 ~~rW   c                 "   U R                   (       a  [        S5      eSnU[        R                  :X  a:  US:  a  [	        S5      eXR
                  :  a  [	        S5      eXR
                  -
  nOGU[        R                  :X  a  US:  a  [	        S5      eUnOU[        R                  :X  a  [	        S5      eU(       aJ  U R                  [        U[        5      5      nU(       d   U R
                  $ U[        U5      -  nU(       a  MJ  U R
                  $ )NrB  r   z.cannot seek to negative position with SEEK_SETz/cannot seek zstd decompression stream backwardsz9zstd decompression streams cannot be seeked with SEEK_END)r1  rU   rQ   SEEK_SETrb  r  SEEK_CURSEEK_ENDr  rl  r(   r6  )r`   r7  r  read_amountr#  s        rX   r  ZstdDecompressionReader.seekU  s   <</00R[[ QwNOO---H   8 88Kr{{"QwH  Kr{{"N  YYK!FGF  ''' 3v;&K k '''rW   )r  r1  r.  r  r/  r  r  r  r  r  r  r  Nr  r  )#rk   rl   rm   rn   ro   r   rE  rN  ru  r  r  ry  r~  r  r  rq  ri  rI  rp   rm  r  r  rT  rX  r0  r  r  r  r  r$  r.  rQ   r)  r  rq   rV   rW   rX   r   r     s    <H #,((((  (
 (( D$6
<"=H: =D4  "{{ '(rW   r   c                       \ rS rSrSr SS jrS rS rS rS r	S r
S	 r\S
 5       rS rS rS rS rSS jrSS jrSS jrS rS rSS jrS rS rSS jrS rS rS rSrg) r   i  a<  
Write-only stream wrapper that performs decompression.

This type provides a writable stream that performs decompression and writes
decompressed data to another stream.

This type implements the ``io.RawIOBase`` interface. Only methods that
involve writing will do useful things.

Behavior is similar to :py:meth:`ZstdCompressor.stream_writer`: compressed
data is sent to the decompressor by calling ``write(data)`` and decompressed
output is written to the inner stream by calling its ``write(data)``
method:

>>> dctx = zstandard.ZstdDecompressor()
>>> decompressor = dctx.stream_writer(fh)
>>> # Will call fh.write() with uncompressed data.
>>> decompressor.write(compressed_data)

Instances can be used as context managers. However, context managers add no
extra special behavior other than automatically calling ``close()`` when
they exit.

Calling ``close()`` will mark the stream as closed and subsequent I/O
operations will raise ``ValueError`` (per the documented behavior of
``io.RawIOBase``). ``close()`` will also call ``close()`` on the
underlying stream if such a method exists and the instance was created with
``closefd=True``.

The size of chunks to ``write()`` to the destination can be specified:

>>> dctx = zstandard.ZstdDecompressor()
>>> with dctx.stream_writer(fh, write_size=16384) as decompressor:
>>>    pass

You can see how much memory is being used by the decompressor:

>>> dctx = zstandard.ZstdDecompressor()
>>> with dctx.stream_writer(fh) as decompressor:
>>>    byte_size = decompressor.memory_size()

``stream_writer()`` accepts a ``write_return_read`` boolean argument to control
the return value of ``write()``. When ``True`` (the default)``, ``write()``
returns the number of bytes that were read from the input. When ``False``,
``write()`` returns the number of bytes that were ``write()`` to the inner
stream.
c                     UR                  5         Xl        X l        X0l        [	        U5      U l        [	        U5      U l        SU l        SU l        SU l	        g rH  )
_ensure_dctxr  r*  r+  r,  r-  r.  r/  r0  r1  )r`   r  r;  r<  r=  r>  s         rX   r    ZstdDecompressionWriter.__init__  sO     	!!#)%"&'8"9WrW   c                     U R                   (       a  [        S5      eU R                  (       a  [        S5      eSU l        U $ rA  rD  r_   s    rX   rE  !ZstdDecompressionWriter.__enter__  s4    <</00===>>rW   c                 2    SU l         U R                  5         grH  )r/  rI  rJ  s       rX   rN   ZstdDecompressionWriter.__exit__  s    

rW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   rT   ZstdDecompressionWriter.__iter__  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   rX   ZstdDecompressionWriter.__next__  rV  rW   c                 V    [         R                  " U R                  R                  5      $ rz   )rM   ZSTD_sizeof_DCtxr  r  r_   s    rX   r\  #ZstdDecompressionWriter.memory_size  s    ##D$6$6$<$<==rW   c                 
   U R                   (       a  g  SU l        U R                  5         SU l        SU l         [        U R                  SS 5      nU R
                  (       a  U(       a  U" 5         g g g ! SU l        SU l         f = frh  )r1  r0  ri  r   r*  r.  rc  s     rX   rI  ZstdDecompressionWriter.close  sh    <<	  DMJJL!DMDLDLL'40==QC =	 "DMDLs   A2 2Bc                     U R                   $ rz   rl  r_   s    rX   rm  ZstdDecompressionWriter.closed  ro  rW   c                 b    [        U R                  SS 5      nU(       a  U" 5       $ [        S5      er_  ra  rc  s     rX   r`  ZstdDecompressionWriter.fileno  rf  rW   c                     U R                   (       a  [        S5      e[        U R                  SS 5      nU(       a  U R                  (       d  U" 5       $ g g )NrB  ri  )r1  rU   r   r*  r0  rc  s     rX   ri  ZstdDecompressionWriter.flush  s>    <</00DLL'40T]]3J #1rW   c                     grH  rV   r_   s    rX   rq  ZstdDecompressionWriter.isatty  rs  rW   c                     grH  rV   r_   s    rX   ru   ZstdDecompressionWriter.readable  rs  rW   c                 ,    [         R                  " 5       erz   rQ  rx  s     rX   ry   ZstdDecompressionWriter.readline  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r|  s     rX   r~  !ZstdDecompressionWriter.readlines  rV  rW   Nc                 ,    [         R                  " 5       erz   rQ  r  s      rX   r  ZstdDecompressionWriter.seek  rV  rW   c                     grH  rV   r_   s    rX   r   ZstdDecompressionWriter.seekable  rs  rW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   r  ZstdDecompressionWriter.tell  rV  rW   c                 ,    [         R                  " 5       erz   rQ  rx  s     rX   r   ZstdDecompressionWriter.truncate  rV  rW   c                     gr  rV   r_   s    rX   r   ZstdDecompressionWriter.writable  r  rW   c                 ,    [         R                  " 5       erz   rQ  r  s     rX   r  "ZstdDecompressionWriter.writelines  rV  rW   c                 ,    [         R                  " 5       erz   rQ  rx  s     rX   r  ZstdDecompressionWriter.read  rV  rW   c                 ,    [         R                  " 5       erz   rQ  r_   s    rX   r  ZstdDecompressionWriter.readall   rV  rW   c                 ,    [         R                  " 5       erz   rQ  r  s     rX   r   ZstdDecompressionWriter.readinto#  rV  rW   c                    U R                   (       a  [        S5      eSn[        R                  " S5      n[        R                  " S5      n[        R                  " U5      nXSl        [        U5      Ul        SUl        [        R                  " SU R                  5      nXdl
        [        U5      Ul        SUl        U R                  R                  nUR                  UR                  :  a  [        R                  " XtU5      n[        R                  " U5      (       a  [!        S[#        U5      -  5      eUR                  (       a\  U R$                  R'                  [        R(                  " UR                  UR                  5      S S  5        X$R                  -  nSUl        UR                  UR                  :  a  M  U R*                  (       a  UR                  $ U$ )NrB  r   r  r'  r&  r  )r1  rU   rL   r!  r  r  r6  rw   r7  r+  r5  r  r  rM   r  r  r   r   r*  r  r  r-  )	r`   r  r  r  r  r  r!  dctxr   s	            rX   r  ZstdDecompressionWriter.write&  sa   <</00GG-.	WW/0
ood+#[)		WWXt'7'78
#j/

!!''mminn,//)LG((/+g2FF  ~~""JJz~~z~~>qA ~~-!"
 mminn, ""== rW   )r1  r.  r0  r  r/  r-  r+  r*  r  r  rz   )rk   rl   rm   rn   ro   r   rE  rN  rT  rX  r\  rI  rp   rm  r`  ri  rq  ru  ry  r~  r  r  r  r  r  r  r  r  r  r  rq   rV   rW   rX   r   r     s    .l &	((>  G(((((((((&rW   r   c                       \ rS rSrSrSS\4S jrS r   SS	 jr\	SS4S
 jr
\S4S jr\	\S4S jr\SS4S jr\	\4S jrS r SS jrSS jrSrg)r   iO  aA  
Context for performing zstandard decompression.

Each instance is essentially a wrapper around a ``ZSTD_DCtx`` from zstd's
C API.

An instance can compress data various ways. Instances can be used multiple
times.

The interface of this class is very similar to
:py:class:`zstandard.ZstdCompressor` (by design).

Assume that each ``ZstdDecompressor`` instance can only handle a single
logical compression operation at the same time. i.e. if you call a method
like ``decompressobj()`` to obtain multiple objects derived from the same
``ZstdDecompressor`` instance and attempt to use them simultaneously, errors
will likely occur.

If you need to perform multiple logical decompression operations and you
can't guarantee those operations are temporally non-overlapping, you need
to obtain multiple ``ZstdDecompressor`` instances.

Unless specified otherwise, assume that no two methods of
``ZstdDecompressor`` instances can be called from multiple Python
threads simultaneously. In other words, assume instances are not thread safe
unless stated otherwise.

:param dict_data:
   Compression dictionary to use.
:param max_window_size:
   Sets an upper limit on the window size for decompression operations in
   kibibytes. This setting can be used to prevent large memory allocations
   for inputs using large compression windows.
:param format:
   Set the format of data for the decoder.

   By default this is ``zstandard.FORMAT_ZSTD1``. It can be set to
   ``zstandard.FORMAT_ZSTD1_MAGICLESS`` to allow decoding frames without
   the 4 byte magic header. Not all decompression APIs support this mode.
Nr   c           	         Xl         X l        X0l        [        R                  " 5       nU[
        R                  :X  a
  [        5       eX@l         U R                  5         [
        R                  " U[        R                  [        R                  " U5      S9U l        g ! [
        R                  " U[        R                  [        R                  " U5      S9U l        f = f)Nr3  )r6  _max_window_size_formatrM   ZSTD_createDCtxrL   r   r   r  r0  r   ZSTD_freeDCtxr;  )r`   r9  max_window_sizer   r`  s        rX   r   ZstdDecompressor.__init__y  s    # /""$388-
	c''c.B.B4.HDJc''c.B.B4.HDJs   B ?Cc                 B    [         R                  " U R                  5      $ )zpSize of decompression context, in bytes.

>>> dctx = zstandard.ZstdDecompressor()
>>> size = dctx.memory_size()
)rM   r;  r  r_   s    rX   r\  ZstdDecompressor.memory_size  rH  rW   FTc                 X   U(       a  [        S5      eU R                  5         [        R                  " U5      n[        R
                  " U[        U5      5      nU[        R                  :X  a  [        S5      eUS:X  a  gU[        R                  :X  a.  U(       d  [        S5      e[        R                  " SU5      nUnSnO[        R                  " SU5      nUn[        R                  " S5      n	Xyl
        Xl        SU	l        [        R                  " S5      n
XZl        [        U5      U
l        SU
l        [        R                  " U R                  X5      n[        R                   " U5      (       a  [        S	[#        U5      -  5      eU(       a  [        S
5      eU(       a  U	R                  U:w  a  [        SX4-  5      eU(       dA  U
R                  U
R                  :  a'  U
R                  U
R                  -
  n[        SU-  5      e[        R$                  " XyR                  5      SS $ )a  
Decompress data in a single operation.

This method will decompress the input data in a single operation and
return the decompressed data.

The input bytes are expected to contain at least 1 full Zstandard frame
(something compressed with :py:meth:`ZstdCompressor.compress` or
similar). If the input does not contain a full frame, an exception will
be raised.

``read_across_frames`` controls whether to read multiple zstandard
frames in the input. When False, decompression stops after reading the
first frame. This feature is not yet implemented but the argument is
provided for forward API compatibility when the default is changed to
True in a future release. For now, if you need to decompress multiple
frames, use an API like :py:meth:`ZstdCompressor.stream_reader` with
``read_across_frames=True``.

``allow_extra_data`` controls how to handle extra input data after a
fully decoded frame. If False, any extra data (which could be a valid
zstd frame) will result in ``ZstdError`` being raised. If True, extra
data is silently ignored. The default will likely change to False in a
future release when ``read_across_frames`` defaults to True.

If the input contains extra data after a full frame, that extra input
data is silently ignored. This behavior is undesirable in many scenarios
and will likely be changed or controllable in a future release (see
#181).

If the frame header of the compressed data does not contain the content
size, ``max_output_size`` must be specified or ``ZstdError`` will be
raised. An allocation of size ``max_output_size`` will be performed and an
attempt will be made to perform decompression into that buffer. If the
buffer is too small or cannot be allocated, ``ZstdError`` will be
raised. The buffer will be resized if it is too large.

Uncompressed data could be much larger than compressed data. As a result,
calling this function could result in a very large memory allocation
being performed to hold the uncompressed data. This could potentially
result in ``MemoryError`` or system memory swapping. If you don't need
the full output data in a single contiguous array in memory, consider
using streaming decompression for more resilient memory behavior.

Usage:

>>> dctx = zstandard.ZstdDecompressor()
>>> decompressed = dctx.decompress(data)

If the compressed data doesn't have its content size embedded within it,
decompression can be attempted by specifying the ``max_output_size``
argument:

>>> dctx = zstandard.ZstdDecompressor()
>>> uncompressed = dctx.decompress(data, max_output_size=1048576)

Ideally, ``max_output_size`` will be identical to the decompressed
output size.

.. important::

   If the exact size of decompressed data is unknown (not passed in
   explicitly and not stored in the zstd frame), for performance
   reasons it is encouraged to use a streaming API.

:param data:
   Compressed data to decompress.
:param max_output_size:
   Integer max size of response.

   If ``0``, there is no limit and we can attempt to allocate an output
   buffer of infinite size.
:return:
   ``bytes`` representing decompressed output.
z?ZstdDecompressor.read_across_frames=True is not yet implementedz0error determining content size from frame headerr   rW   z0could not determine content size in frame headerr&  r'  r  zdecompression error: %sz2decompression error: did not decompress full framez7decompression error: decompressed %d bytes; expected %dzFcompressed input contains %d bytes of unused data, which is disallowedN)r   r0  rL   r  rM   r  r6  r  rR  r!  r5  rw   r7  r  r  r  r  r   r  )r`   r  max_output_sizer  allow_extra_datar  output_sizeresult_bufferresult_sizer  r  r   counts                rX   r  ZstdDecompressor.decompress  s   f Q  	ood+22[)
 #444NOOAC888"F   GGHo>M)KKGGHk:M%KWW/0
&%
GG-.	#[)		++DJJ
NG$$5G8LLMMD  Z^^{:I()  "imminn&DNNY]]2EX 
 zz-8;;rW   c                 8    U R                  5         [        XX#US9$ )a  
Read-only stream wrapper that performs decompression.

This method obtains an object that conforms to the ``io.RawIOBase``
interface and performs transparent decompression via ``read()``
operations. Source data is obtained by calling ``read()`` on a
source stream or object implementing the buffer protocol.

See :py:class:`zstandard.ZstdDecompressionReader` for more documentation
and usage examples.

:param source:
   Source of compressed data to decompress. Can be any object
   with a ``read(size)`` method or that conforms to the buffer protocol.
:param read_size:
   Integer number of bytes to read from the source and feed into the
   compressor at a time.
:param read_across_frames:
   Whether to read data across multiple zstd frames. If False,
   decompression is stopped at frame boundaries.
:param closefd:
   Whether to close the source stream when this instance is closed.
:return:
   :py:class:`zstandard.ZstdDecompressionReader`.
rb  )r0  r   )r`   r  r  r  r>  s        rX   rd  ZstdDecompressor.stream_reader&  s%    @ 	&)
 	
rW   c                 X    US:  a  [        S5      eU R                  5         [        XUS9$ )a  Obtain a standard library compatible incremental decompressor.

See :py:class:`ZstdDecompressionObj` for more documentation
and usage examples.

:param write_size: size of internal output buffer to collect decompressed
  chunks in.
:param read_across_frames: whether to read across multiple zstd frames.
  If False, reading stops after 1 frame and subsequent decompress
  attempts will raise an exception.
:return:
   :py:class:`zstandard.ZstdDecompressionObj`
rK   zwrite_size must be positive)r<  r  )rU   r0  r   )r`   r<  r  s      rX   decompressobjZstdDecompressor.decompressobjK  s6    $ >:;;#<N
 	
rW   c              #     #    XB:  a  [        S5      e[        US5      (       a  SnO,[        US5      (       a  SnSn[        U5      nO[        S5      eU(       a,  U(       a  UR                  U5        OUW:  a  [        S5      eUnU R	                  5         [
        R                  " S	5      n[
        R                  " S
5      n	[
        R                  " SU5      n
Xl        [        U
5      U	l        SU	l	         U	R                  S:X  d   eU(       a  UR                  U5      nOWW-
  n[        X5      nXXm-    nXm-  nU(       d  g[
        R                  " U5      nXl        [        U5      Ul        SUl	        UR                  UR                  :  a  U	R                  S:X  d   e[        R                  " U R                  X5      n[        R                   " U5      (       a  [#        S[%        U5      -  5      eU	R                  (       a9  [
        R&                  " U	R                  U	R                  5      SS nSU	l	        Uv   US:X  a  gUR                  UR                  :  a  M  GMp  7f)a  Read compressed data to an iterator of uncompressed chunks.

This method will read data from ``reader``, feed it to a decompressor,
and emit ``bytes`` chunks representing the decompressed result.

>>> dctx = zstandard.ZstdDecompressor()
>>> for chunk in dctx.read_to_iter(fh):
...     # Do something with original data.

``read_to_iter()`` accepts an object with a ``read(size)`` method that
will return compressed bytes or an object conforming to the buffer
protocol.

``read_to_iter()`` returns an iterator whose elements are chunks of the
decompressed data.

The size of requested ``read()`` from the source can be specified:

>>> dctx = zstandard.ZstdDecompressor()
>>> for chunk in dctx.read_to_iter(fh, read_size=16384):
...    pass

It is also possible to skip leading bytes in the input data:

>>> dctx = zstandard.ZstdDecompressor()
>>> for chunk in dctx.read_to_iter(fh, skip_bytes=1):
...    pass

.. tip::

   Skipping leading bytes is useful if the source data contains extra
   *header* data. Traditionally, you would need to create a slice or
   ``memoryview`` of the data you want to decompress. This would create
   overhead. It is more efficient to pass the offset into this API.

Similarly to :py:meth:`ZstdCompressor.read_to_iter`, the consumer of the
iterator controls when data is decompressed. If the iterator isn't consumed,
decompression is put on hold.

When ``read_to_iter()`` is passed an object conforming to the buffer protocol,
the behavior may seem similar to what occurs when the simple decompression
API is used. However, this API works when the decompressed size is unknown.
Furthermore, if feeding large inputs, the decompressor will work in chunks
instead of performing a single operation.

:param reader:
   Source of compressed data. Can be any object with a
   ``read(size)`` method or any object conforming to the buffer
   protocol.
:param read_size:
   Integer size of data chunks to read from ``reader`` and feed into
   the decompressor.
:param write_size:
   Integer size of data chunks to emit from iterator.
:param skip_bytes:
   Integer number of bytes to skip over before sending data into
   the decompressor.
:return:
   Iterator of ``bytes`` representing uncompressed data.
z)skip_bytes must be smaller than read_sizer  Tr   Fr   rk  z(skip_bytes larger than first input chunkr  r'  r&  r  N)rU   r  r6  r  r0  rL   r!  r5  rw   r7  rl  r  r  rM   r  r  r  r   r   r  )r`   rm  r  r<  
skip_bytesrn  ro  rw   r  r  r!  rp  rq  rr  rs  r   r  s                    rX   rt  ZstdDecompressor.read_to_itere  s    H "HII66""IV]++IMv;D. 
 J'$$%OPP *GG-.	WW/0
WWXz2
#j/

>>Q&&&$kk)4 =0	 6
$]5OP+  //+6K'M -INIM--)..0!~~***33JJ
 ##G,,#3k'6JJ  >>::jnnjnnEaHD%&JNJa<# --)..0( s   II&#I&c                 T    [        US5      (       d  [        S5      e[        U UUUUS9$ )a1  
Push-based stream wrapper that performs decompression.

This method constructs a stream wrapper that conforms to the
``io.RawIOBase`` interface and performs transparent decompression
when writing to a wrapper stream.

See :py:class:`zstandard.ZstdDecompressionWriter` for more documentation
and usage examples.

:param writer:
   Destination for decompressed output. Can be any object with a
   ``write(data)``.
:param write_size:
   Integer size of chunks to ``write()`` to ``writer``.
:param write_return_read:
   Whether ``write()`` should return the number of bytes of input
   consumed. If False, ``write()`` returns the number of bytes sent
   to the inner stream.
:param closefd:
   Whether to ``close()`` the inner stream when this stream is closed.
:return:
   :py:class:`zstandard.ZstdDecompressionWriter`
r  rg  rb  )r  rU   r   )r`   r;  r<  r=  r>  s        rX   rh  ZstdDecompressor.stream_writer  s9    > vw''HII&
 	
rW   c                    [        US5      (       d  [        S5      e[        US5      (       d  [        S5      eU R                  5         [        R                  " S5      n[        R                  " S5      n[        R                  " SU5      nXvl        XFl        SUl        S	u  p UR                  U5      n
U
(       d   X4$ [        R                  " U
5      nU[        U5      -  nXl        [        U5      Ul        SUl        UR                  UR                  :  a  [        R                  " U R                  Xe5      n[        R                  " U5      (       a  [!        S
[#        U5      -  5      eUR                  (       aO  UR%                  [        R&                  " UR
                  UR                  5      5        XR                  -  n	SUl        UR                  UR                  :  a  M  GMI  )aL  
Copy data between streams, decompressing in the process.

Compressed data will be read from ``ifh``, decompressed, and written
to ``ofh``.

>>> dctx = zstandard.ZstdDecompressor()
>>> dctx.copy_stream(ifh, ofh)

e.g. to decompress a file to another file:

>>> dctx = zstandard.ZstdDecompressor()
>>> with open(input_path, 'rb') as ifh, open(output_path, 'wb') as ofh:
...     dctx.copy_stream(ifh, ofh)

The size of chunks being ``read()`` and ``write()`` from and to the
streams can be specified:

>>> dctx = zstandard.ZstdDecompressor()
>>> dctx.copy_stream(ifh, ofh, read_size=8192, write_size=16384)

:param ifh:
   Source stream to read compressed data from.

   Must have a ``read()`` method.
:param ofh:
   Destination stream to write uncompressed data to.

   Must have a ``write()`` method.
:param read_size:
   The number of bytes to ``read()`` from the source in a single
   operation.
:param write_size:
   The number of bytes to ``write()`` to the destination in a single
   operation.
:return:
   2-tuple of integers representing the number of bytes read and
   written, respectively.
r  rZ  r  r[  r  r'  r&  r   r  r  )r  rU   r0  rL   r!  r5  rw   r7  r  r  r6  r  rM   r  r  r  r   r   r  r  )r`   r\  r]  r  r<  r  r  r!  r^  r  r  r  r   s                rX   r_  ZstdDecompressor.copy_stream#  s   ^ sF##GHHsG$$IJJGG-.	WW/0
WWXz2
#$
"&
 88I&D2 &&/ //$/K#k**J'M -INIM --)..033JJ
 ##G,,#5G8LL  >>IIcjjHI>>1K%&JN --)..0 rW   c                    [        U[        5      (       d  [        S5      eU(       d  [        S5      eUS   n[        U[        5      (       d  [        S5      e[
        R                  " U5      n[
        R                  " S5      n[        R                  " XC[        U5      5      n[        R                  " U5      (       a  [        S5      eU(       a  [        S5      eUR                  [        R                  :X  a  [        S5      eU R                  S	S
9  [
        R                  " SUR                  5      n[
        R                  " S5      nXgl        [        U5      Ul        SUl        [
        R                  " S5      nX8l        [        U5      Ul        SUl        [        R&                  " U R(                  Xx5      n[        R                  " U5      (       a  [+        S[-        U5      -  5      eU(       a  [+        S5      e[        U5      S:X  a#  [
        R.                  " U[        U5      5      SS $ Sn	U	[        U5      :  Ga  X   n[        U[        5      (       d  [        SU	-  5      e[
        R                  " U5      n[        R                  " XC[        U5      5      n[        R                  " U5      (       a  [        SU	-  5      eU(       a  [        SU	-  5      eUR                  [        R                  :X  a  [        SU	-  5      e[
        R                  " SUR                  5      n
Xl        [        U
5      Ul        SUl        X8l        [        U5      Ul        SUl        [        R&                  " U R(                  Xx5      n[        R                  " U5      (       a  [+        S[-        U5      -  5      eU(       a  [+        SU	-  5      eU
nU	S-  n	U	[        U5      :  a  GM  [
        R.                  " U[        U5      5      SS $ )a  
Decompress a series of frames using the content dictionary chaining technique.

Such a list of frames is produced by compressing discrete inputs where
each non-initial input is compressed with a *prefix* dictionary consisting
of the content of the previous input.

For example, say you have the following inputs:

>>> inputs = [b"input 1", b"input 2", b"input 3"]

The zstd frame chain consists of:

1. ``b"input 1"`` compressed in standalone/discrete mode
2. ``b"input 2"`` compressed using ``b"input 1"`` as a *prefix* dictionary
3. ``b"input 3"`` compressed using ``b"input 2"`` as a *prefix* dictionary

Each zstd frame **must** have the content size written.

The following Python code can be used to produce a *prefix dictionary chain*:

>>> def make_chain(inputs):
...    frames = []
...
...    # First frame is compressed in standalone/discrete mode.
...    zctx = zstandard.ZstdCompressor()
...    frames.append(zctx.compress(inputs[0]))
...
...    # Subsequent frames use the previous fulltext as a prefix dictionary
...    for i, raw in enumerate(inputs[1:]):
...        dict_data = zstandard.ZstdCompressionDict(
...            inputs[i], dict_type=zstandard.DICT_TYPE_RAWCONTENT)
...        zctx = zstandard.ZstdCompressor(dict_data=dict_data)
...        frames.append(zctx.compress(raw))
...
...    return frames

``decompress_content_dict_chain()`` returns the uncompressed data of the last
element in the input chain.

.. note::

   It is possible to implement *prefix dictionary chain* decompression
   on top of other APIs. However, this function will likely be faster -
   especially for long input chains - as it avoids the overhead of
   instantiating and passing around intermediate objects between
   multiple functions.

:param frames:
   List of ``bytes`` holding compressed zstd frames.
:return:
zargument must be a listzempty input chainr   zchunk 0 must be bytesr  z!chunk 0 is not a valid zstd framez,chunk 0 is too small to contain a zstd framez%chunk 0 missing content size in frameF)	load_dictr&  r'  r  z could not decompress chunk 0: %sz%chunk 0 did not decompress full framerK   Nzchunk %d must be bytesz"chunk %d is not a valid zstd framez-chunk %d is too small to contain a zstd framez&chunk %d missing content size in framez!could not decompress chunk %d: %sz&chunk %d did not decompress full frame)r  r  r  rU   r  rL   r  r!  rM   r  r6  r  r  rR  r0  r5  rw   r7  r  r  r  r   r   r  )r`   framesr  chunk_bufferr   r   last_bufferr  r  r~   r+  s              rX   decompress_content_dict_chain.ZstdDecompressor.decompress_content_dict_chain  sd   j &$''566011 q	%''455 u--.))#l"3
 G$$@AAKLL""c&B&BBDEEE*ggh(?(?@WW/0
$k*

GG-.	$\*		++DJJ
NG$$2[5II  CDD v;!::k3{+;<Q??#f+oIEeU++ !9A!=>>??51L--c,&7G (( !E!IJJ CaG  &&#*F*FF !IA!MNN''(F,C,CDK(N!+.JOJN(M .INIM//

JG ((7+g:NN   H1 LMM%KFAQ #f+oT zz+s;'78;;rW   c                     [        5       e)a{  
Decompress multiple zstd frames to output buffers as a single operation.

(Experimental. Not available in CFFI backend.)

Compressed frames can be passed to the function as a
``BufferWithSegments``, a ``BufferWithSegmentsCollection``, or as a
list containing objects that conform to the buffer protocol. For best
performance, pass a ``BufferWithSegmentsCollection`` or a
``BufferWithSegments``, as minimal input validation will be done for
that type. If calling from Python (as opposed to C), constructing one
of these instances may add overhead cancelling out the performance
overhead of validation for list inputs.

Returns a ``BufferWithSegmentsCollection`` containing the decompressed
data. All decompressed data is allocated in a single memory buffer. The
``BufferWithSegments`` instance tracks which objects are at which offsets
and their respective lengths.

>>> dctx = zstandard.ZstdDecompressor()
>>> results = dctx.multi_decompress_to_buffer([b'...', b'...'])

The decompressed size of each frame MUST be discoverable. It can either be
embedded within the zstd frame or passed in via the ``decompressed_sizes``
argument.

The ``decompressed_sizes`` argument is an object conforming to the buffer
protocol which holds an array of 64-bit unsigned integers in the machine's
native format defining the decompressed sizes of each frame. If this argument
is passed, it avoids having to scan each frame for its decompressed size.
This frame scanning can add noticeable overhead in some scenarios.

>>> frames = [...]
>>> sizes = struct.pack('=QQQQ', len0, len1, len2, len3)
>>>
>>> dctx = zstandard.ZstdDecompressor()
>>> results = dctx.multi_decompress_to_buffer(frames, decompressed_sizes=sizes)

.. note::

   It is possible to pass a ``mmap.mmap()`` instance into this function by
   wrapping it with a ``BufferWithSegments`` instance (which will define the
   offsets of frames within the memory mapped region).

This function is logically equivalent to performing
:py:meth:`ZstdCompressor.decompress` on each input frame and returning the
result.

This function exists to perform decompression on multiple frames as fast
as possible by having as little overhead as possible. Since decompression is
performed as a single operation and since the decompressed output is stored in
a single buffer, extra memory allocations, Python objects, and Python function
calls are avoided. This is ideal for scenarios where callers know up front that
they need to access data for multiple frames, such as when  *delta chains* are
being used.

Currently, the implementation always spawns multiple threads when requested,
even if the amount of work to do is small. In the future, it will be smarter
about avoiding threads and their associated overhead when the amount of
work to do is small.

:param frames:
   Source defining zstd frames to decompress.
:param decompressed_sizes:
   Array of integers representing sizes of decompressed zstd frames.
:param threads:
   How many threads to use for decompression operations.

   Negative values will use the same number of threads as logical CPUs
   on the machine. Values ``0`` or ``1`` use a single thread.
:return:
   ``BufferWithSegmentsCollection``
r]   )r`   r  decompressed_sizesr   s       rX   multi_decompress_to_buffer+ZstdDecompressor.multi_decompress_to_buffer  s    X "##rW   c                    [         R                  " U R                  [         R                  5        U R                  (       a]  [         R
                  " U R                  U R                  5      n[         R                  " U5      (       a  [        S[        U5      -  5      e[         R                  " U R                  [         R                  U R                  5      n[         R                  " U5      (       a  [        S[        U5      -  5      eU R                  (       ap  U(       ah  [         R                  " U R                  U R                  R                  5      n[         R                  " U5      (       a  [        S[        U5      -  5      eg g g )Nz!unable to set max window size: %sz!unable to set decoding format: %sz+unable to reference prepared dictionary: %s)rM   ZSTD_DCtx_resetr  rK  rd  ZSTD_DCtx_setMaxWindowSizer  r   r   ZSTD_DCtx_setParameterZSTD_d_formatre  r6  ZSTD_DCtx_refDDictr  )r`   r  r   s      rX   r0  ZstdDecompressor._ensure_dctxe  s*   DJJ(C(CD  44

D11G ((7+g:NN  ,,JJ))4<<
 G$$3k'6JJ  ??y,,TZZ9O9OPG((A!'*+  )  )?rW   )r  r6  re  rd  )r   FT)Nr   r  )rk   rl   rm   rn   ro   rI   r   r\  r  r'   rd  r(   rw  rt  rh  r_  r  r  r0  rq   rV   rW   rX   r   r   O  s    'R "&q (0  O<h 7 #
N 9 
: 78Pn 9(
\ 78]'~S<l 89L$\rW   r   )
r   r   r   g        r   r   r   r   r   r   )ro   
__future__r   r   __all__rR  rQ   _cffirL   rM   setr   ZSTD_CStreamInSizer%   ZSTD_CStreamOutSizer&   ZSTD_DStreamInSizer'   ZSTD_DStreamOutSizer(   new_allocatorrM  r4  r$   ZSTD_MAGICNUMBERr)   r!   rR  r"   r  r#   ZSTD_VERSION_MAJORZSTD_VERSION_MINORZSTD_VERSION_RELEASEr    ZSTD_BLOCKSIZELOG_MAXr*   ZSTD_BLOCKSIZE_MAXr+   ZSTD_WINDOWLOG_MINr,   ZSTD_WINDOWLOG_MAXr-   ZSTD_CHAINLOG_MINr.   ZSTD_CHAINLOG_MAXr/   ZSTD_HASHLOG_MINr0   ZSTD_HASHLOG_MAXr1   ZSTD_MINMATCH_MINr2   ZSTD_MINMATCH_MAXr3   ZSTD_SEARCHLOG_MINr4   ZSTD_SEARCHLOG_MAXr5   r6   r7   ZSTD_TARGETLENGTH_MINr8   ZSTD_TARGETLENGTH_MAXr9   ZSTD_LDM_MINMATCH_MINr:   ZSTD_LDM_MINMATCH_MAXr;   ZSTD_LDM_BUCKETSIZELOG_MAXr<   	ZSTD_fastr=   
ZSTD_dfastr>   ZSTD_greedyr?   	ZSTD_lazyr@   
ZSTD_lazy2rA   ZSTD_btlazy2rB   
ZSTD_btoptrC   ZSTD_btultrarD   ZSTD_btultra2rE   ZSTD_dct_autorF   ZSTD_dct_rawContentrG   ZSTD_dct_fullDictrH   ZSTD_f_zstd1rI   ZSTD_f_zstd1_magiclessrJ   r   r   r   r   rY   r   r   r   r   rc  r   r   r   objectr   r   r   r   r   r   r	   r   r   r   r   r   r   r
   r   r   r   r   r   rV   rW   rX   <module>r     s   D 8HT 
 	 5 %(%;%;%= "&)&=&=&? #'*'='='? $(+(?(?(A %? **, ##"22 ..  ,, &&&&&&$$$$""""$$$$&&&&(( (( ,, ,, ,, ,, 66 //## ## %% "".. ** 33   "$ $. 3$ 3$l$ $,		 	F"J@G @GF'

P&F P&f
p  p fqV qh_F _DoPV oPd7f 7<*"&>j& j` 


L^W6 WtD(f D(NMf M`pv prW   