| 1 |
from contextlib import suppress |
| 2 |
from threading import Lock |
| 3 |
|
| 4 |
from backports.zstd._cffi._common import ( |
| 5 |
ZstdError, |
| 6 |
_check_int, |
| 7 |
_DictionaryType, |
| 8 |
_ffi, |
| 9 |
_lib, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
class ZstdDict: |
| 14 |
"""Represents a Zstandard dictionary. |
| 15 |
|
| 16 |
dict_content |
| 17 |
The content of a Zstandard dictionary as a bytes-like object. |
| 18 |
is_raw |
| 19 |
If true, perform no checks on *dict_content*, useful for some |
| 20 |
advanced cases. Otherwise, check that the content represents |
| 21 |
a Zstandard dictionary created by the zstd library or CLI. |
| 22 |
|
| 23 |
The dictionary can be used for compression or decompression, and can be |
| 24 |
shared by multiple ZstdCompressor or ZstdDecompressor objects.""" |
| 25 |
|
| 26 |
def __init__(self, dict_content, *, is_raw=False): |
| 27 |
# All dictionaries must be at least 8 bytes |
| 28 |
if len(dict_content) < 8: |
| 29 |
raise ValueError( |
| 30 |
"Zstandard dictionary content too short " |
| 31 |
"(must have at least eight bytes)" |
| 32 |
) |
| 33 |
|
| 34 |
self._d_dict = _ffi.NULL |
| 35 |
self._dict_id = 0 |
| 36 |
self._lock = Lock() |
| 37 |
|
| 38 |
# ZSTD_CDict dict |
| 39 |
self._c_dicts = {} |
| 40 |
|
| 41 |
self._dict_content = bytes(dict_content) |
| 42 |
|
| 43 |
# Get dict_id, 0 means "raw content" dictionary. |
| 44 |
self._dict_id = _lib.ZSTD_getDictID_fromDict( |
| 45 |
_ffi.from_buffer(self._dict_content), len(self._dict_content) |
| 46 |
) |
| 47 |
|
| 48 |
# Check validity for ordinary dictionary |
| 49 |
if not is_raw and self.dict_id == 0: |
| 50 |
raise ValueError("invalid Zstandard dictionary") |
| 51 |
|
| 52 |
def __repr__(self): |
| 53 |
return "<ZstdDict dict_id=%u dict_size=%d>" % ( |
| 54 |
self._dict_id, |
| 55 |
len(self._dict_content), |
| 56 |
) |
| 57 |
|
| 58 |
def __len__(self): |
| 59 |
return len(self._dict_content) |
| 60 |
|
| 61 |
@property |
| 62 |
def dict_content(self): |
| 63 |
"The content of a Zstandard dictionary, as a bytes object." |
| 64 |
return self._dict_content |
| 65 |
|
| 66 |
@property |
| 67 |
def dict_id(self): |
| 68 |
"""The Zstandard dictionary, an int between 0 and 2**32. |
| 69 |
|
| 70 |
A non-zero value represents an ordinary Zstandard dictionary, |
| 71 |
conforming to the standardised format. |
| 72 |
|
| 73 |
A value of zero indicates a 'raw content' dictionary, |
| 74 |
without any restrictions on format or content.""" |
| 75 |
return self._dict_id |
| 76 |
|
| 77 |
@property |
| 78 |
def as_digested_dict(self): |
| 79 |
"""Load as a digested dictionary to compressor. |
| 80 |
|
| 81 |
Pass this attribute as zstd_dict argument: |
| 82 |
compress(dat, zstd_dict=zd.as_digested_dict) |
| 83 |
|
| 84 |
1. Some advanced compression parameters of compressor may be |
| 85 |
overridden by parameters of digested dictionary. |
| 86 |
2. ZstdDict has a digested dictionaries cache for each compression |
| 87 |
level. It's faster when loading again a digested dictionary with |
| 88 |
the same compression level. |
| 89 |
3. No need to use this for decompression.""" |
| 90 |
return self, _DictionaryType.DICT_TYPE_DIGESTED |
| 91 |
|
| 92 |
@property |
| 93 |
def as_undigested_dict(self): |
| 94 |
"""Load as an undigested dictionary to compressor. |
| 95 |
|
| 96 |
Pass this attribute as zstd_dict argument: |
| 97 |
compress(dat, zstd_dict=zd.as_undigested_dict) |
| 98 |
|
| 99 |
1. The advanced compression parameters of compressor will not be |
| 100 |
overridden. |
| 101 |
2. Loading an undigested dictionary is costly. If load an undigested |
| 102 |
dictionary multiple times, consider reusing a compressor object. |
| 103 |
3. No need to use this for decompression.""" |
| 104 |
return self, _DictionaryType.DICT_TYPE_UNDIGESTED |
| 105 |
|
| 106 |
@property |
| 107 |
def as_prefix(self): |
| 108 |
"""Load as a prefix to compressor/decompressor. |
| 109 |
|
| 110 |
Pass this attribute as zstd_dict argument: |
| 111 |
compress(dat, zstd_dict=zd.as_prefix) |
| 112 |
|
| 113 |
1. Prefix is compatible with long distance matching, while |
| 114 |
dictionary is not. |
| 115 |
2. It only works for the first frame, then the |
| 116 |
compressor/decompressor will return to no prefix state. |
| 117 |
3. When decompressing, must use the same prefix as when compressing.""" |
| 118 |
return self, _DictionaryType.DICT_TYPE_PREFIX |
| 119 |
|
| 120 |
def _get_cdict(self, compression_level): |
| 121 |
with self._lock: |
| 122 |
cdict = self._c_dicts.get(compression_level) |
| 123 |
if cdict is None: |
| 124 |
cdict = _lib.ZSTD_createCDict( |
| 125 |
self._dict_content, len(self._dict_content), compression_level |
| 126 |
) |
| 127 |
if cdict == _ffi.NULL: |
| 128 |
raise ZstdError( |
| 129 |
"Failed to create a ZSTD_CDict instance from " |
| 130 |
"Zstandard dictionary content." |
| 131 |
) |
| 132 |
self._c_dicts[compression_level] = cdict |
| 133 |
|
| 134 |
return cdict |
| 135 |
|
| 136 |
def _get_ddict(self): |
| 137 |
with self._lock: |
| 138 |
if self._d_dict == _ffi.NULL: |
| 139 |
# Create ZSTD_DDict instance from dictionary content |
| 140 |
self._d_dict = _lib.ZSTD_createDDict( |
| 141 |
self._dict_content, len(self._dict_content) |
| 142 |
) |
| 143 |
if self._d_dict == _ffi.NULL: |
| 144 |
raise ZstdError( |
| 145 |
"Failed to create a ZSTD_DDict instance from " |
| 146 |
"Zstandard dictionary content." |
| 147 |
) |
| 148 |
|
| 149 |
return self._d_dict |
| 150 |
|
| 151 |
def __del__(self): |
| 152 |
with suppress(AttributeError): |
| 153 |
for level, cdict in self._c_dicts.items(): |
| 154 |
_lib.ZSTD_freeCDict(cdict) |
| 155 |
self._c_dicts[level] = _ffi.NULL |
| 156 |
|
| 157 |
with suppress(AttributeError): |
| 158 |
if self._d_dict != _ffi.NULL: |
| 159 |
_lib.ZSTD_freeDDict(self._d_dict) |
| 160 |
self._d_dict = _ffi.NULL |
| 161 |
|
| 162 |
|
| 163 |
def _Py_parse_zstd_dict(zd, default_type): |
| 164 |
# Check ZstdDict |
| 165 |
if isinstance(zd, ZstdDict): |
| 166 |
return zd, default_type |
| 167 |
|
| 168 |
# Check (ZstdDict, type) |
| 169 |
if isinstance(zd, tuple) and len(zd) == 2: |
| 170 |
zd, type_ = zd |
| 171 |
if isinstance(zd, ZstdDict) and isinstance(type_, int): |
| 172 |
_check_int(type_) |
| 173 |
if type_ in ( |
| 174 |
_DictionaryType.DICT_TYPE_DIGESTED, |
| 175 |
_DictionaryType.DICT_TYPE_UNDIGESTED, |
| 176 |
_DictionaryType.DICT_TYPE_PREFIX, |
| 177 |
): |
| 178 |
return zd, type_ |
| 179 |
|
| 180 |
# Wrong type |
| 181 |
raise TypeError("zstd_dict argument should be a ZstdDict object.") |