
    tni                     Z    d dl mZ d dlmZ d dlmZmZmZmZm	Z	  G d d          Z
d ZdS )    )suppress)Lock)	ZstdError
_check_int_DictionaryType_ffi_libc                       e Zd ZdZdddZd Zd Zed             Zed             Z	ed	             Z
ed
             Zed             Zd Zd Zd ZdS )ZstdDicta  
    Represents a Zstandard dictionary.

      dict_content
        The content of a Zstandard dictionary as a bytes-like object.
      is_raw
        If true, perform no checks on *dict_content*, useful for some
        advanced cases. Otherwise, check that the content represents
        a Zstandard dictionary created by the zstd library or CLI.

    The dictionary can be used for compression or decompression, and can be shared
    by multiple ZstdCompressor or ZstdDecompressor objects.
    F)is_rawc                   t          |          dk     rt          d          t          j        | _        d| _        t                      | _        i | _        t          |          | _
        t          j        t          j        | j
                  t          | j
                            | _        |s| j        dk    rt          d          d S d S )N   zGZstandard dictionary content too short (must have at least eight bytes)r   zinvalid Zstandard dictionary)len
ValueErrorr   NULL_d_dict_dict_idr   _lock_c_dictsbytes_dict_contentr	   ZSTD_getDictID_fromDictfrom_bufferdict_id)selfdict_contentr   s      Z/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/backports/zstd/_cffi/zstddict.py__init__zZstdDict.__init__   s    |q  3  
 yVV
 "<00 4T/00#d6H2I2I
 

  	=$,!++;<<<	= 	=++    c                 >    d| j         t          | j                  fz  S )Nz"<ZstdDict dict_id=%u dict_size=%d>)r   r   r   r   s    r   __repr__zZstdDict.__repr__6   s'    3M"##7
 
 	
r   c                 *    t          | j                  S N)r   r   r!   s    r   __len__zZstdDict.__len__<   s    4%&&&r   c                     | j         S )z9The content of a Zstandard dictionary, as a bytes object.)r   r!   s    r   r   zZstdDict.dict_content?   s     !!r   c                     | j         S )a3  
        The Zstandard dictionary, an int between 0 and 2**32.

        A non-zero value represents an ordinary Zstandard dictionary,
        conforming to the standardised format.

        A value of zero indicates a 'raw content' dictionary,
        without any restrictions on format or content.
        )r   r!   s    r   r   zZstdDict.dict_idD   s     }r   c                     | t           j        fS )a  
        Load as a digested dictionary to compressor.

        Pass this attribute as zstd_dict argument:
        compress(dat, zstd_dict=zd.as_digested_dict)

        1. Some advanced compression parameters of compressor may be overridden
           by parameters of digested dictionary.
        2. ZstdDict has a digested dictionaries cache for each compression level.
           It's faster when loading again a digested dictionary with the same
           compression level.
        3. No need to use this for decompression.
        )r   DICT_TYPE_DIGESTEDr!   s    r   as_digested_dictzZstdDict.as_digested_dictQ   s     _777r   c                     | t           j        fS )a  
        Load as an undigested dictionary to compressor.

        Pass this attribute as zstd_dict argument:
        compress(dat, zstd_dict=zd.as_undigested_dict)

        1. The advanced compression parameters of compressor will not be overridden.
        2. Loading an undigested dictionary is costly. If load an undigested dictionary
           multiple times, consider reusing a compressor object.
        3. No need to use this for decompression.
        )r   DICT_TYPE_UNDIGESTEDr!   s    r   as_undigested_dictzZstdDict.as_undigested_dictb   s     _999r   c                     | t           j        fS )a  
        Load as a prefix to compressor/decompressor.

        Pass this attribute as zstd_dict argument:
        compress(dat, zstd_dict=zd.as_prefix)

        1. Prefix is compatible with long distance matching, while dictionary is not.
        2. It only works for the first frame, then the compressor/decompressor will
           return to no prefix state.
        3. When decompressing, must use the same prefix as when compressing.
        )r   DICT_TYPE_PREFIXr!   s    r   	as_prefixzZstdDict.as_prefixq   s     _555r   c                 (   | j         5  | j                            |          }|Vt          j        | j        t          | j                  |          }|t          j        k    rt          d          || j        |<   d d d            n# 1 swxY w Y   |S )NzIFailed to create a ZSTD_CDict instance from Zstandard dictionary content.)
r   r   getr	   ZSTD_createCDictr   r   r   r   r   )r   compression_levelcdicts      r   
_get_cdictzZstdDict._get_cdict   s    Z 	9 	9M%%&788E}-&D,>(?(?AR  DI%%#8   49/0	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 s   A3BBBc                 &   | j         5  | j        t          j        k    rUt	          j        | j        t          | j                            | _        | j        t          j        k    rt          d          | j        cd d d            S # 1 swxY w Y   d S )NzIFailed to create a ZSTD_DDict instance from Zstandard dictionary content.)	r   r   r   r   r	   ZSTD_createDDictr   r   r   r!   s    r   
_get_ddictzZstdDict._get_ddict   s    Z 	  	 |ty((#4&D,>(?(?    <49,,#8  
 <	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   A1BB
B
c                    t          t                    5  | j                                        D ]-\  }}t	          j        |           t          j        | j        |<   .	 d d d            n# 1 swxY w Y   t          t                    5  | j        t          j        k    r*t	          j	        | j                   t          j        | _        d d d            d S # 1 swxY w Y   d S r$   )
r   AttributeErrorr   itemsr	   ZSTD_freeCDictr   r   r   ZSTD_freeDDict)r   levelr5   s      r   __del__zZstdDict.__del__   sL   n%% 	1 	1 $ 3 3 5 5 1 1u#E***'+ye$$1	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1
 n%% 	) 	)|ty((#DL111#y	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s%   AA**A.1A.	A CCCN)__name__
__module____qualname____doc__r   r"   r%   propertyr   r   r*   r-   r0   r6   r9   r@    r   r   r   r      s         05 = = = = =4
 
 
' ' ' " " X" 
 
 X
 8 8 X8  : : X: 6 6 X6        	) 	) 	) 	) 	)r   r   c                 p   t          | t                    r| |fS t          | t                    rzt          |           dk    rg| \  } }t          | t                    rMt          |t                    r8t          |           |t          j        t          j        t          j	        fv r| |fS t          d          )N   z/zstd_dict argument should be a ZstdDict object.)
isinstancer   tupler   intr   r   r)   r,   r/   	TypeError)zddefault_typetype_s      r   _Py_parse_zstd_dictrP      s    "h  < "e 	!RA	Eb(## 	!
5#(>(> 	!u240  
 5y  E
F
FFr   N)
contextlibr   	threadingr   backports.zstd._cffi._commonr   r   r   r   r	   r   rP   rF   r   r   <module>rT      s                            [) [) [) [) [) [) [) [)|G G G G Gr   