| 1 |
/* Low level interface to the Zstandard algorithm & the zstd library. */ |
| 2 |
|
| 3 |
#ifndef Py_BUILD_CORE_BUILTIN |
| 4 |
# define Py_BUILD_CORE_MODULE 1 |
| 5 |
#endif |
| 6 |
|
| 7 |
#include "Python.h" |
| 8 |
|
| 9 |
#include "_zstdmodule.h" |
| 10 |
|
| 11 |
#include <zstd.h> // ZSTD_*() |
| 12 |
#include <zdict.h> // ZDICT_*() |
| 13 |
|
| 14 |
/*[clinic input] |
| 15 |
module _zstd |
| 16 |
|
| 17 |
[clinic start generated code]*/ |
| 18 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b5f5587aac15c14]*/ |
| 19 |
#include "clinic/_zstdmodule.c.h" |
| 20 |
|
| 21 |
|
| 22 |
ZstdDict * |
| 23 |
_Py_parse_zstd_dict(const _zstd_state *state, PyObject *dict, int *ptype) |
| 24 |
{ |
| 25 |
if (state == NULL) { |
| 26 |
return NULL; |
| 27 |
} |
| 28 |
|
| 29 |
/* Check ZstdDict */ |
| 30 |
if (PyObject_TypeCheck(dict, state->ZstdDict_type)) { |
| 31 |
return (ZstdDict*)dict; |
| 32 |
} |
| 33 |
|
| 34 |
/* Check (ZstdDict, type) */ |
| 35 |
if (PyTuple_CheckExact(dict) && PyTuple_GET_SIZE(dict) == 2 |
| 36 |
&& PyObject_TypeCheck(PyTuple_GET_ITEM(dict, 0), state->ZstdDict_type) |
| 37 |
&& PyLong_Check(PyTuple_GET_ITEM(dict, 1))) |
| 38 |
{ |
| 39 |
int type = PyLong_AsInt(PyTuple_GET_ITEM(dict, 1)); |
| 40 |
if (type == -1 && PyErr_Occurred()) { |
| 41 |
return NULL; |
| 42 |
} |
| 43 |
if (type == DICT_TYPE_DIGESTED |
| 44 |
|| type == DICT_TYPE_UNDIGESTED |
| 45 |
|| type == DICT_TYPE_PREFIX) |
| 46 |
{ |
| 47 |
*ptype = type; |
| 48 |
return (ZstdDict*)PyTuple_GET_ITEM(dict, 0); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/* Wrong type */ |
| 53 |
PyErr_SetString(PyExc_TypeError, |
| 54 |
"zstd_dict argument should be a ZstdDict object."); |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
|
| 58 |
/* Format error message and set ZstdError. */ |
| 59 |
void |
| 60 |
set_zstd_error(const _zstd_state *state, error_type type, size_t zstd_ret) |
| 61 |
{ |
| 62 |
const char *msg; |
| 63 |
assert(ZSTD_isError(zstd_ret)); |
| 64 |
|
| 65 |
if (state == NULL) { |
| 66 |
return; |
| 67 |
} |
| 68 |
switch (type) { |
| 69 |
case ERR_DECOMPRESS: |
| 70 |
msg = "Unable to decompress Zstandard data: %s"; |
| 71 |
break; |
| 72 |
case ERR_COMPRESS: |
| 73 |
msg = "Unable to compress Zstandard data: %s"; |
| 74 |
break; |
| 75 |
case ERR_SET_PLEDGED_INPUT_SIZE: |
| 76 |
msg = "Unable to set pledged uncompressed content size: %s"; |
| 77 |
break; |
| 78 |
|
| 79 |
case ERR_LOAD_D_DICT: |
| 80 |
msg = "Unable to load Zstandard dictionary or prefix for " |
| 81 |
"decompression: %s"; |
| 82 |
break; |
| 83 |
case ERR_LOAD_C_DICT: |
| 84 |
msg = "Unable to load Zstandard dictionary or prefix for " |
| 85 |
"compression: %s"; |
| 86 |
break; |
| 87 |
|
| 88 |
case ERR_GET_C_BOUNDS: |
| 89 |
msg = "Unable to get zstd compression parameter bounds: %s"; |
| 90 |
break; |
| 91 |
case ERR_GET_D_BOUNDS: |
| 92 |
msg = "Unable to get zstd decompression parameter bounds: %s"; |
| 93 |
break; |
| 94 |
case ERR_SET_C_LEVEL: |
| 95 |
msg = "Unable to set zstd compression level: %s"; |
| 96 |
break; |
| 97 |
|
| 98 |
case ERR_TRAIN_DICT: |
| 99 |
msg = "Unable to train the Zstandard dictionary: %s"; |
| 100 |
break; |
| 101 |
case ERR_FINALIZE_DICT: |
| 102 |
msg = "Unable to finalize the Zstandard dictionary: %s"; |
| 103 |
break; |
| 104 |
|
| 105 |
default: |
| 106 |
Py_UNREACHABLE(); |
| 107 |
} |
| 108 |
PyErr_Format(state->ZstdError, msg, ZSTD_getErrorName(zstd_ret)); |
| 109 |
} |
| 110 |
|
| 111 |
typedef struct { |
| 112 |
int parameter; |
| 113 |
char parameter_name[32]; |
| 114 |
} ParameterInfo; |
| 115 |
|
| 116 |
static const ParameterInfo cp_list[] = { |
| 117 |
{ZSTD_c_compressionLevel, "compression_level"}, |
| 118 |
{ZSTD_c_windowLog, "window_log"}, |
| 119 |
{ZSTD_c_hashLog, "hash_log"}, |
| 120 |
{ZSTD_c_chainLog, "chain_log"}, |
| 121 |
{ZSTD_c_searchLog, "search_log"}, |
| 122 |
{ZSTD_c_minMatch, "min_match"}, |
| 123 |
{ZSTD_c_targetLength, "target_length"}, |
| 124 |
{ZSTD_c_strategy, "strategy"}, |
| 125 |
|
| 126 |
{ZSTD_c_enableLongDistanceMatching, "enable_long_distance_matching"}, |
| 127 |
{ZSTD_c_ldmHashLog, "ldm_hash_log"}, |
| 128 |
{ZSTD_c_ldmMinMatch, "ldm_min_match"}, |
| 129 |
{ZSTD_c_ldmBucketSizeLog, "ldm_bucket_size_log"}, |
| 130 |
{ZSTD_c_ldmHashRateLog, "ldm_hash_rate_log"}, |
| 131 |
|
| 132 |
{ZSTD_c_contentSizeFlag, "content_size_flag"}, |
| 133 |
{ZSTD_c_checksumFlag, "checksum_flag"}, |
| 134 |
{ZSTD_c_dictIDFlag, "dict_id_flag"}, |
| 135 |
|
| 136 |
{ZSTD_c_nbWorkers, "nb_workers"}, |
| 137 |
{ZSTD_c_jobSize, "job_size"}, |
| 138 |
{ZSTD_c_overlapLog, "overlap_log"} |
| 139 |
}; |
| 140 |
|
| 141 |
static const ParameterInfo dp_list[] = { |
| 142 |
{ZSTD_d_windowLogMax, "window_log_max"} |
| 143 |
}; |
| 144 |
|
| 145 |
void |
| 146 |
set_parameter_error(int is_compress, int key_v, int value_v) |
| 147 |
{ |
| 148 |
ParameterInfo const *list; |
| 149 |
int list_size; |
| 150 |
char *type; |
| 151 |
ZSTD_bounds bounds; |
| 152 |
char pos_msg[64]; |
| 153 |
|
| 154 |
if (is_compress) { |
| 155 |
list = cp_list; |
| 156 |
list_size = Py_ARRAY_LENGTH(cp_list); |
| 157 |
type = "compression"; |
| 158 |
} |
| 159 |
else { |
| 160 |
list = dp_list; |
| 161 |
list_size = Py_ARRAY_LENGTH(dp_list); |
| 162 |
type = "decompression"; |
| 163 |
} |
| 164 |
|
| 165 |
/* Find parameter's name */ |
| 166 |
char const *name = NULL; |
| 167 |
for (int i = 0; i < list_size; i++) { |
| 168 |
if (key_v == (list+i)->parameter) { |
| 169 |
name = (list+i)->parameter_name; |
| 170 |
break; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/* Unknown parameter */ |
| 175 |
if (name == NULL) { |
| 176 |
PyOS_snprintf(pos_msg, sizeof(pos_msg), |
| 177 |
"unknown parameter (key %d)", key_v); |
| 178 |
name = pos_msg; |
| 179 |
} |
| 180 |
|
| 181 |
/* Get parameter bounds */ |
| 182 |
if (is_compress) { |
| 183 |
bounds = ZSTD_cParam_getBounds(key_v); |
| 184 |
} |
| 185 |
else { |
| 186 |
bounds = ZSTD_dParam_getBounds(key_v); |
| 187 |
} |
| 188 |
if (ZSTD_isError(bounds.error)) { |
| 189 |
PyErr_Format(PyExc_ValueError, "invalid %s parameter '%s'", |
| 190 |
type, name); |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
/* Error message */ |
| 195 |
PyErr_Format(PyExc_ValueError, |
| 196 |
"%s parameter '%s' received an illegal value %d; " |
| 197 |
"the valid range is [%d, %d]", |
| 198 |
type, name, value_v, bounds.lowerBound, bounds.upperBound); |
| 199 |
} |
| 200 |
|
| 201 |
static inline _zstd_state* |
| 202 |
get_zstd_state(PyObject *module) |
| 203 |
{ |
| 204 |
void *state = PyModule_GetState(module); |
| 205 |
assert(state != NULL); |
| 206 |
return (_zstd_state *)state; |
| 207 |
} |
| 208 |
|
| 209 |
static Py_ssize_t |
| 210 |
calculate_samples_stats(PyBytesObject *samples_bytes, PyObject *samples_sizes, |
| 211 |
size_t **chunk_sizes) |
| 212 |
{ |
| 213 |
Py_ssize_t chunks_number; |
| 214 |
Py_ssize_t sizes_sum; |
| 215 |
Py_ssize_t i; |
| 216 |
|
| 217 |
chunks_number = PyTuple_GET_SIZE(samples_sizes); |
| 218 |
if ((size_t) chunks_number > UINT32_MAX) { |
| 219 |
PyErr_Format(PyExc_ValueError, |
| 220 |
"The number of samples should be <= %u.", UINT32_MAX); |
| 221 |
return -1; |
| 222 |
} |
| 223 |
|
| 224 |
/* Prepare chunk_sizes */ |
| 225 |
*chunk_sizes = PyMem_New(size_t, chunks_number); |
| 226 |
if (*chunk_sizes == NULL) { |
| 227 |
PyErr_NoMemory(); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
sizes_sum = PyBytes_GET_SIZE(samples_bytes); |
| 232 |
for (i = 0; i < chunks_number; i++) { |
| 233 |
size_t size = PyLong_AsSize_t(PyTuple_GET_ITEM(samples_sizes, i)); |
| 234 |
(*chunk_sizes)[i] = size; |
| 235 |
if (size == (size_t)-1 && PyErr_Occurred()) { |
| 236 |
if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 237 |
goto sum_error; |
| 238 |
} |
| 239 |
return -1; |
| 240 |
} |
| 241 |
if ((size_t)sizes_sum < size) { |
| 242 |
goto sum_error; |
| 243 |
} |
| 244 |
sizes_sum -= size; |
| 245 |
} |
| 246 |
|
| 247 |
if (sizes_sum != 0) { |
| 248 |
sum_error: |
| 249 |
PyErr_SetString(PyExc_ValueError, |
| 250 |
"The samples size tuple doesn't match the " |
| 251 |
"concatenation's size."); |
| 252 |
return -1; |
| 253 |
} |
| 254 |
return chunks_number; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
/*[clinic input] |
| 259 |
_zstd.train_dict |
| 260 |
|
| 261 |
samples_bytes: PyBytesObject |
| 262 |
Concatenation of samples. |
| 263 |
samples_sizes: object(subclass_of='&PyTuple_Type') |
| 264 |
Tuple of samples' sizes. |
| 265 |
dict_size: Py_ssize_t |
| 266 |
The size of the dictionary. |
| 267 |
/ |
| 268 |
|
| 269 |
Train a Zstandard dictionary on sample data. |
| 270 |
[clinic start generated code]*/ |
| 271 |
|
| 272 |
static PyObject * |
| 273 |
_zstd_train_dict_impl(PyObject *module, PyBytesObject *samples_bytes, |
| 274 |
PyObject *samples_sizes, Py_ssize_t dict_size) |
| 275 |
/*[clinic end generated code: output=8e87fe43935e8f77 input=d20dedb21c72cb62]*/ |
| 276 |
{ |
| 277 |
PyObject *dst_dict_bytes = NULL; |
| 278 |
size_t *chunk_sizes = NULL; |
| 279 |
Py_ssize_t chunks_number; |
| 280 |
size_t zstd_ret; |
| 281 |
|
| 282 |
/* Check arguments */ |
| 283 |
if (dict_size <= 0) { |
| 284 |
PyErr_SetString(PyExc_ValueError, |
| 285 |
"dict_size argument should be positive number."); |
| 286 |
return NULL; |
| 287 |
} |
| 288 |
|
| 289 |
/* Check that the samples are valid and get their sizes */ |
| 290 |
chunks_number = calculate_samples_stats(samples_bytes, samples_sizes, |
| 291 |
&chunk_sizes); |
| 292 |
if (chunks_number < 0) { |
| 293 |
goto error; |
| 294 |
} |
| 295 |
|
| 296 |
/* Allocate dict buffer */ |
| 297 |
dst_dict_bytes = PyBytes_FromStringAndSize(NULL, dict_size); |
| 298 |
if (dst_dict_bytes == NULL) { |
| 299 |
goto error; |
| 300 |
} |
| 301 |
|
| 302 |
/* Train the dictionary */ |
| 303 |
char *dst_dict_buffer = PyBytes_AS_STRING(dst_dict_bytes); |
| 304 |
const char *samples_buffer = PyBytes_AS_STRING(samples_bytes); |
| 305 |
Py_BEGIN_ALLOW_THREADS |
| 306 |
zstd_ret = ZDICT_trainFromBuffer(dst_dict_buffer, dict_size, |
| 307 |
samples_buffer, |
| 308 |
chunk_sizes, (uint32_t)chunks_number); |
| 309 |
Py_END_ALLOW_THREADS |
| 310 |
|
| 311 |
/* Check Zstandard dict error */ |
| 312 |
if (ZDICT_isError(zstd_ret)) { |
| 313 |
_zstd_state* mod_state = get_zstd_state(module); |
| 314 |
set_zstd_error(mod_state, ERR_TRAIN_DICT, zstd_ret); |
| 315 |
goto error; |
| 316 |
} |
| 317 |
|
| 318 |
/* Resize dict_buffer */ |
| 319 |
if (_PyBytes_Resize(&dst_dict_bytes, zstd_ret) < 0) { |
| 320 |
goto error; |
| 321 |
} |
| 322 |
|
| 323 |
goto success; |
| 324 |
|
| 325 |
error: |
| 326 |
Py_CLEAR(dst_dict_bytes); |
| 327 |
|
| 328 |
success: |
| 329 |
PyMem_Free(chunk_sizes); |
| 330 |
return dst_dict_bytes; |
| 331 |
} |
| 332 |
|
| 333 |
/*[clinic input] |
| 334 |
_zstd.finalize_dict |
| 335 |
|
| 336 |
custom_dict_bytes: PyBytesObject |
| 337 |
Custom dictionary content. |
| 338 |
samples_bytes: PyBytesObject |
| 339 |
Concatenation of samples. |
| 340 |
samples_sizes: object(subclass_of='&PyTuple_Type') |
| 341 |
Tuple of samples' sizes. |
| 342 |
dict_size: Py_ssize_t |
| 343 |
The size of the dictionary. |
| 344 |
compression_level: int |
| 345 |
Optimize for a specific Zstandard compression level, 0 means default. |
| 346 |
/ |
| 347 |
|
| 348 |
Finalize a Zstandard dictionary. |
| 349 |
[clinic start generated code]*/ |
| 350 |
|
| 351 |
static PyObject * |
| 352 |
_zstd_finalize_dict_impl(PyObject *module, PyBytesObject *custom_dict_bytes, |
| 353 |
PyBytesObject *samples_bytes, |
| 354 |
PyObject *samples_sizes, Py_ssize_t dict_size, |
| 355 |
int compression_level) |
| 356 |
/*[clinic end generated code: output=f91821ba5ae85bda input=3c7e2480aa08fb56]*/ |
| 357 |
{ |
| 358 |
Py_ssize_t chunks_number; |
| 359 |
size_t *chunk_sizes = NULL; |
| 360 |
PyObject *dst_dict_bytes = NULL; |
| 361 |
size_t zstd_ret; |
| 362 |
ZDICT_params_t params; |
| 363 |
|
| 364 |
/* Check arguments */ |
| 365 |
if (dict_size <= 0) { |
| 366 |
PyErr_SetString(PyExc_ValueError, |
| 367 |
"dict_size argument should be positive number."); |
| 368 |
return NULL; |
| 369 |
} |
| 370 |
|
| 371 |
/* Check that the samples are valid and get their sizes */ |
| 372 |
chunks_number = calculate_samples_stats(samples_bytes, samples_sizes, |
| 373 |
&chunk_sizes); |
| 374 |
if (chunks_number < 0) { |
| 375 |
goto error; |
| 376 |
} |
| 377 |
|
| 378 |
/* Allocate dict buffer */ |
| 379 |
dst_dict_bytes = PyBytes_FromStringAndSize(NULL, dict_size); |
| 380 |
if (dst_dict_bytes == NULL) { |
| 381 |
goto error; |
| 382 |
} |
| 383 |
|
| 384 |
/* Parameters */ |
| 385 |
|
| 386 |
/* Optimize for a specific Zstandard compression level, 0 means default. */ |
| 387 |
params.compressionLevel = compression_level; |
| 388 |
/* Write log to stderr, 0 = none. */ |
| 389 |
params.notificationLevel = 0; |
| 390 |
/* Force dictID value, 0 means auto mode (32-bits random value). */ |
| 391 |
params.dictID = 0; |
| 392 |
|
| 393 |
/* Finalize the dictionary */ |
| 394 |
Py_BEGIN_ALLOW_THREADS |
| 395 |
zstd_ret = ZDICT_finalizeDictionary( |
| 396 |
PyBytes_AS_STRING(dst_dict_bytes), dict_size, |
| 397 |
PyBytes_AS_STRING(custom_dict_bytes), |
| 398 |
Py_SIZE(custom_dict_bytes), |
| 399 |
PyBytes_AS_STRING(samples_bytes), chunk_sizes, |
| 400 |
(uint32_t)chunks_number, params); |
| 401 |
Py_END_ALLOW_THREADS |
| 402 |
|
| 403 |
/* Check Zstandard dict error */ |
| 404 |
if (ZDICT_isError(zstd_ret)) { |
| 405 |
_zstd_state* mod_state = get_zstd_state(module); |
| 406 |
set_zstd_error(mod_state, ERR_FINALIZE_DICT, zstd_ret); |
| 407 |
goto error; |
| 408 |
} |
| 409 |
|
| 410 |
/* Resize dict_buffer */ |
| 411 |
if (_PyBytes_Resize(&dst_dict_bytes, zstd_ret) < 0) { |
| 412 |
goto error; |
| 413 |
} |
| 414 |
|
| 415 |
goto success; |
| 416 |
|
| 417 |
error: |
| 418 |
Py_CLEAR(dst_dict_bytes); |
| 419 |
|
| 420 |
success: |
| 421 |
PyMem_Free(chunk_sizes); |
| 422 |
return dst_dict_bytes; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
/*[clinic input] |
| 427 |
_zstd.get_param_bounds |
| 428 |
|
| 429 |
parameter: int |
| 430 |
The parameter to get bounds. |
| 431 |
is_compress: bool |
| 432 |
True for CompressionParameter, False for DecompressionParameter. |
| 433 |
|
| 434 |
Get CompressionParameter/DecompressionParameter bounds. |
| 435 |
[clinic start generated code]*/ |
| 436 |
|
| 437 |
static PyObject * |
| 438 |
_zstd_get_param_bounds_impl(PyObject *module, int parameter, int is_compress) |
| 439 |
/*[clinic end generated code: output=4acf5a876f0620ca input=45742ef0a3531b65]*/ |
| 440 |
{ |
| 441 |
ZSTD_bounds bound; |
| 442 |
if (is_compress) { |
| 443 |
bound = ZSTD_cParam_getBounds(parameter); |
| 444 |
if (ZSTD_isError(bound.error)) { |
| 445 |
_zstd_state* mod_state = get_zstd_state(module); |
| 446 |
set_zstd_error(mod_state, ERR_GET_C_BOUNDS, bound.error); |
| 447 |
return NULL; |
| 448 |
} |
| 449 |
} |
| 450 |
else { |
| 451 |
bound = ZSTD_dParam_getBounds(parameter); |
| 452 |
if (ZSTD_isError(bound.error)) { |
| 453 |
_zstd_state* mod_state = get_zstd_state(module); |
| 454 |
set_zstd_error(mod_state, ERR_GET_D_BOUNDS, bound.error); |
| 455 |
return NULL; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
return Py_BuildValue("ii", bound.lowerBound, bound.upperBound); |
| 460 |
} |
| 461 |
|
| 462 |
/*[clinic input] |
| 463 |
_zstd.get_frame_size |
| 464 |
|
| 465 |
frame_buffer: Py_buffer |
| 466 |
A bytes-like object, it should start from the beginning of a frame, |
| 467 |
and contains at least one complete frame. |
| 468 |
|
| 469 |
Get the size of a Zstandard frame, including the header and optional checksum. |
| 470 |
[clinic start generated code]*/ |
| 471 |
|
| 472 |
static PyObject * |
| 473 |
_zstd_get_frame_size_impl(PyObject *module, Py_buffer *frame_buffer) |
| 474 |
/*[clinic end generated code: output=a7384c2f8780f442 input=3b9f73f8c8129d38]*/ |
| 475 |
{ |
| 476 |
size_t frame_size; |
| 477 |
|
| 478 |
frame_size = ZSTD_findFrameCompressedSize(frame_buffer->buf, |
| 479 |
frame_buffer->len); |
| 480 |
if (ZSTD_isError(frame_size)) { |
| 481 |
_zstd_state* mod_state = get_zstd_state(module); |
| 482 |
PyErr_Format(mod_state->ZstdError, |
| 483 |
"Error when finding the compressed size of a Zstandard frame. " |
| 484 |
"Ensure the frame_buffer argument starts from the " |
| 485 |
"beginning of a frame, and its length is not less than this " |
| 486 |
"complete frame. Zstd error message: %s.", |
| 487 |
ZSTD_getErrorName(frame_size)); |
| 488 |
return NULL; |
| 489 |
} |
| 490 |
|
| 491 |
return PyLong_FromSize_t(frame_size); |
| 492 |
} |
| 493 |
|
| 494 |
/*[clinic input] |
| 495 |
_zstd.get_frame_info |
| 496 |
|
| 497 |
frame_buffer: Py_buffer |
| 498 |
A bytes-like object, containing the header of a Zstandard frame. |
| 499 |
|
| 500 |
Get Zstandard frame infomation from a frame header. |
| 501 |
[clinic start generated code]*/ |
| 502 |
|
| 503 |
static PyObject * |
| 504 |
_zstd_get_frame_info_impl(PyObject *module, Py_buffer *frame_buffer) |
| 505 |
/*[clinic end generated code: output=56e033cf48001929 input=94b240583ae22ca5]*/ |
| 506 |
{ |
| 507 |
uint64_t decompressed_size; |
| 508 |
uint32_t dict_id; |
| 509 |
|
| 510 |
/* ZSTD_getFrameContentSize */ |
| 511 |
decompressed_size = ZSTD_getFrameContentSize(frame_buffer->buf, |
| 512 |
frame_buffer->len); |
| 513 |
|
| 514 |
/* #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1) |
| 515 |
#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) */ |
| 516 |
if (decompressed_size == ZSTD_CONTENTSIZE_ERROR) { |
| 517 |
_zstd_state* mod_state = get_zstd_state(module); |
| 518 |
PyErr_SetString(mod_state->ZstdError, |
| 519 |
"Error when getting information from the header of " |
| 520 |
"a Zstandard frame. Ensure the frame_buffer argument " |
| 521 |
"starts from the beginning of a frame, and its length " |
| 522 |
"is not less than the frame header (6~18 bytes)."); |
| 523 |
return NULL; |
| 524 |
} |
| 525 |
|
| 526 |
/* ZSTD_getDictID_fromFrame */ |
| 527 |
dict_id = ZSTD_getDictID_fromFrame(frame_buffer->buf, frame_buffer->len); |
| 528 |
|
| 529 |
/* Build tuple */ |
| 530 |
if (decompressed_size == ZSTD_CONTENTSIZE_UNKNOWN) { |
| 531 |
return Py_BuildValue("OI", Py_None, dict_id); |
| 532 |
} |
| 533 |
return Py_BuildValue("KI", decompressed_size, dict_id); |
| 534 |
} |
| 535 |
|
| 536 |
/*[clinic input] |
| 537 |
_zstd.set_parameter_types |
| 538 |
|
| 539 |
c_parameter_type: object(subclass_of='&PyType_Type') |
| 540 |
CompressionParameter IntEnum type object |
| 541 |
d_parameter_type: object(subclass_of='&PyType_Type') |
| 542 |
DecompressionParameter IntEnum type object |
| 543 |
|
| 544 |
Set CompressionParameter and DecompressionParameter types for validity check. |
| 545 |
[clinic start generated code]*/ |
| 546 |
|
| 547 |
static PyObject * |
| 548 |
_zstd_set_parameter_types_impl(PyObject *module, PyObject *c_parameter_type, |
| 549 |
PyObject *d_parameter_type) |
| 550 |
/*[clinic end generated code: output=f3313b1294f19502 input=75d7a953580fae5f]*/ |
| 551 |
{ |
| 552 |
_zstd_state* mod_state = get_zstd_state(module); |
| 553 |
|
| 554 |
Py_INCREF(c_parameter_type); |
| 555 |
Py_XSETREF(mod_state->CParameter_type, (PyTypeObject*)c_parameter_type); |
| 556 |
Py_INCREF(d_parameter_type); |
| 557 |
Py_XSETREF(mod_state->DParameter_type, (PyTypeObject*)d_parameter_type); |
| 558 |
|
| 559 |
Py_RETURN_NONE; |
| 560 |
} |
| 561 |
|
| 562 |
static PyMethodDef _zstd_methods[] = { |
| 563 |
_ZSTD_TRAIN_DICT_METHODDEF |
| 564 |
_ZSTD_FINALIZE_DICT_METHODDEF |
| 565 |
_ZSTD_GET_PARAM_BOUNDS_METHODDEF |
| 566 |
_ZSTD_GET_FRAME_SIZE_METHODDEF |
| 567 |
_ZSTD_GET_FRAME_INFO_METHODDEF |
| 568 |
_ZSTD_SET_PARAMETER_TYPES_METHODDEF |
| 569 |
{NULL, NULL} |
| 570 |
}; |
| 571 |
|
| 572 |
static int |
| 573 |
_zstd_exec(PyObject *m) |
| 574 |
{ |
| 575 |
#define ADD_TYPE(TYPE, SPEC) \ |
| 576 |
do { \ |
| 577 |
TYPE = (PyTypeObject *)PyType_FromModuleAndSpec(m, &(SPEC), NULL); \ |
| 578 |
if (TYPE == NULL) { \ |
| 579 |
return -1; \ |
| 580 |
} \ |
| 581 |
if (PyModule_AddType(m, TYPE) < 0) { \ |
| 582 |
return -1; \ |
| 583 |
} \ |
| 584 |
} while (0) |
| 585 |
|
| 586 |
#define ADD_INT_MACRO(MACRO) \ |
| 587 |
if (PyModule_AddIntConstant((m), #MACRO, (MACRO)) < 0) { \ |
| 588 |
return -1; \ |
| 589 |
} |
| 590 |
|
| 591 |
#define ADD_INT_CONST_TO_TYPE(TYPE, NAME, VALUE) \ |
| 592 |
do { \ |
| 593 |
PyObject *v = PyLong_FromLong((VALUE)); \ |
| 594 |
if (v == NULL || PyObject_SetAttrString((PyObject *)(TYPE), \ |
| 595 |
(NAME), v) < 0) { \ |
| 596 |
Py_XDECREF(v); \ |
| 597 |
return -1; \ |
| 598 |
} \ |
| 599 |
Py_DECREF(v); \ |
| 600 |
} while (0) |
| 601 |
|
| 602 |
_zstd_state* mod_state = get_zstd_state(m); |
| 603 |
|
| 604 |
/* Reusable objects & variables */ |
| 605 |
mod_state->CParameter_type = NULL; |
| 606 |
mod_state->DParameter_type = NULL; |
| 607 |
|
| 608 |
/* Create and add heap types */ |
| 609 |
ADD_TYPE(mod_state->ZstdDict_type, zstd_dict_type_spec); |
| 610 |
ADD_TYPE(mod_state->ZstdCompressor_type, zstd_compressor_type_spec); |
| 611 |
ADD_TYPE(mod_state->ZstdDecompressor_type, zstd_decompressor_type_spec); |
| 612 |
mod_state->ZstdError = PyErr_NewExceptionWithDoc( |
| 613 |
"compression.zstd.ZstdError", |
| 614 |
"An error occurred in the zstd library.", |
| 615 |
NULL, NULL); |
| 616 |
if (mod_state->ZstdError == NULL) { |
| 617 |
return -1; |
| 618 |
} |
| 619 |
if (PyModule_AddType(m, (PyTypeObject *)mod_state->ZstdError) < 0) { |
| 620 |
return -1; |
| 621 |
} |
| 622 |
|
| 623 |
/* Add constants */ |
| 624 |
if (PyModule_AddIntConstant(m, "zstd_version_number", |
| 625 |
ZSTD_versionNumber()) < 0) { |
| 626 |
return -1; |
| 627 |
} |
| 628 |
|
| 629 |
if (PyModule_AddStringConstant(m, "zstd_version", |
| 630 |
ZSTD_versionString()) < 0) { |
| 631 |
return -1; |
| 632 |
} |
| 633 |
|
| 634 |
#if ZSTD_VERSION_NUMBER >= 10500 |
| 635 |
if (PyModule_AddIntConstant(m, "ZSTD_CLEVEL_DEFAULT", |
| 636 |
ZSTD_defaultCLevel()) < 0) { |
| 637 |
return -1; |
| 638 |
} |
| 639 |
#else |
| 640 |
ADD_INT_MACRO(ZSTD_CLEVEL_DEFAULT); |
| 641 |
#endif |
| 642 |
|
| 643 |
if (PyModule_Add(m, "ZSTD_DStreamOutSize", |
| 644 |
PyLong_FromSize_t(ZSTD_DStreamOutSize())) < 0) { |
| 645 |
return -1; |
| 646 |
} |
| 647 |
|
| 648 |
/* Add zstd compression parameters. All should also be in cp_list. */ |
| 649 |
ADD_INT_MACRO(ZSTD_c_compressionLevel); |
| 650 |
ADD_INT_MACRO(ZSTD_c_windowLog); |
| 651 |
ADD_INT_MACRO(ZSTD_c_hashLog); |
| 652 |
ADD_INT_MACRO(ZSTD_c_chainLog); |
| 653 |
ADD_INT_MACRO(ZSTD_c_searchLog); |
| 654 |
ADD_INT_MACRO(ZSTD_c_minMatch); |
| 655 |
ADD_INT_MACRO(ZSTD_c_targetLength); |
| 656 |
ADD_INT_MACRO(ZSTD_c_strategy); |
| 657 |
|
| 658 |
ADD_INT_MACRO(ZSTD_c_enableLongDistanceMatching); |
| 659 |
ADD_INT_MACRO(ZSTD_c_ldmHashLog); |
| 660 |
ADD_INT_MACRO(ZSTD_c_ldmMinMatch); |
| 661 |
ADD_INT_MACRO(ZSTD_c_ldmBucketSizeLog); |
| 662 |
ADD_INT_MACRO(ZSTD_c_ldmHashRateLog); |
| 663 |
|
| 664 |
ADD_INT_MACRO(ZSTD_c_contentSizeFlag); |
| 665 |
ADD_INT_MACRO(ZSTD_c_checksumFlag); |
| 666 |
ADD_INT_MACRO(ZSTD_c_dictIDFlag); |
| 667 |
|
| 668 |
ADD_INT_MACRO(ZSTD_c_nbWorkers); |
| 669 |
ADD_INT_MACRO(ZSTD_c_jobSize); |
| 670 |
ADD_INT_MACRO(ZSTD_c_overlapLog); |
| 671 |
|
| 672 |
/* Add zstd decompression parameters. All should also be in dp_list. */ |
| 673 |
ADD_INT_MACRO(ZSTD_d_windowLogMax); |
| 674 |
|
| 675 |
/* Add ZSTD_strategy enum members */ |
| 676 |
ADD_INT_MACRO(ZSTD_fast); |
| 677 |
ADD_INT_MACRO(ZSTD_dfast); |
| 678 |
ADD_INT_MACRO(ZSTD_greedy); |
| 679 |
ADD_INT_MACRO(ZSTD_lazy); |
| 680 |
ADD_INT_MACRO(ZSTD_lazy2); |
| 681 |
ADD_INT_MACRO(ZSTD_btlazy2); |
| 682 |
ADD_INT_MACRO(ZSTD_btopt); |
| 683 |
ADD_INT_MACRO(ZSTD_btultra); |
| 684 |
ADD_INT_MACRO(ZSTD_btultra2); |
| 685 |
|
| 686 |
/* Add ZSTD_EndDirective enum members to ZstdCompressor */ |
| 687 |
ADD_INT_CONST_TO_TYPE(mod_state->ZstdCompressor_type, |
| 688 |
"CONTINUE", ZSTD_e_continue); |
| 689 |
ADD_INT_CONST_TO_TYPE(mod_state->ZstdCompressor_type, |
| 690 |
"FLUSH_BLOCK", ZSTD_e_flush); |
| 691 |
ADD_INT_CONST_TO_TYPE(mod_state->ZstdCompressor_type, |
| 692 |
"FLUSH_FRAME", ZSTD_e_end); |
| 693 |
|
| 694 |
/* Make ZstdCompressor immutable (set Py_TPFLAGS_IMMUTABLETYPE) */ |
| 695 |
PyType_Freeze(mod_state->ZstdCompressor_type); |
| 696 |
|
| 697 |
#undef ADD_TYPE |
| 698 |
#undef ADD_INT_MACRO |
| 699 |
#undef ADD_ZSTD_COMPRESSOR_INT_CONST |
| 700 |
|
| 701 |
return 0; |
| 702 |
} |
| 703 |
|
| 704 |
static int |
| 705 |
_zstd_traverse(PyObject *module, visitproc visit, void *arg) |
| 706 |
{ |
| 707 |
_zstd_state* mod_state = get_zstd_state(module); |
| 708 |
|
| 709 |
Py_VISIT(mod_state->ZstdDict_type); |
| 710 |
Py_VISIT(mod_state->ZstdCompressor_type); |
| 711 |
|
| 712 |
Py_VISIT(mod_state->ZstdDecompressor_type); |
| 713 |
|
| 714 |
Py_VISIT(mod_state->ZstdError); |
| 715 |
|
| 716 |
Py_VISIT(mod_state->CParameter_type); |
| 717 |
Py_VISIT(mod_state->DParameter_type); |
| 718 |
return 0; |
| 719 |
} |
| 720 |
|
| 721 |
static int |
| 722 |
_zstd_clear(PyObject *module) |
| 723 |
{ |
| 724 |
_zstd_state* mod_state = get_zstd_state(module); |
| 725 |
|
| 726 |
Py_CLEAR(mod_state->ZstdDict_type); |
| 727 |
Py_CLEAR(mod_state->ZstdCompressor_type); |
| 728 |
|
| 729 |
Py_CLEAR(mod_state->ZstdDecompressor_type); |
| 730 |
|
| 731 |
Py_CLEAR(mod_state->ZstdError); |
| 732 |
|
| 733 |
Py_CLEAR(mod_state->CParameter_type); |
| 734 |
Py_CLEAR(mod_state->DParameter_type); |
| 735 |
return 0; |
| 736 |
} |
| 737 |
|
| 738 |
static void |
| 739 |
_zstd_free(void *module) |
| 740 |
{ |
| 741 |
(void)_zstd_clear((PyObject *)module); |
| 742 |
} |
| 743 |
|
| 744 |
static struct PyModuleDef_Slot _zstd_slots[] = { |
| 745 |
{Py_mod_exec, _zstd_exec}, |
| 746 |
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, |
| 747 |
{Py_mod_gil, Py_MOD_GIL_NOT_USED}, |
| 748 |
{0, NULL}, |
| 749 |
}; |
| 750 |
|
| 751 |
static struct PyModuleDef _zstdmodule = { |
| 752 |
.m_base = PyModuleDef_HEAD_INIT, |
| 753 |
.m_name = "_zstd", |
| 754 |
.m_doc = "Implementation module for Zstandard compression.", |
| 755 |
.m_size = sizeof(_zstd_state), |
| 756 |
.m_slots = _zstd_slots, |
| 757 |
.m_methods = _zstd_methods, |
| 758 |
.m_traverse = _zstd_traverse, |
| 759 |
.m_clear = _zstd_clear, |
| 760 |
.m_free = _zstd_free, |
| 761 |
}; |
| 762 |
|
| 763 |
PyMODINIT_FUNC |
| 764 |
PyInit__zstd(void) |
| 765 |
{ |
| 766 |
return PyModuleDef_Init(&_zstdmodule); |
| 767 |
} |