| 1 |
/** |
| 2 |
* Copyright (c) 2016-present, Gregory Szorc |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* This software may be modified and distributed under the terms |
| 6 |
* of the BSD license. See the LICENSE file for details. |
| 7 |
*/ |
| 8 |
|
| 9 |
/* A Python C extension for Zstandard. */ |
| 10 |
|
| 11 |
#if defined(_WIN32) |
| 12 |
#define WIN32_LEAN_AND_MEAN |
| 13 |
#include <Windows.h> |
| 14 |
#elif defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || \ |
| 15 |
defined(__NetBSD__) || defined(__DragonFly__) |
| 16 |
#include <sys/types.h> |
| 17 |
|
| 18 |
#include <sys/sysctl.h> |
| 19 |
|
| 20 |
#endif |
| 21 |
|
| 22 |
#include "python-zstandard.h" |
| 23 |
|
| 24 |
#include "bufferutil.c" |
| 25 |
#include "compressionchunker.c" |
| 26 |
#include "compressiondict.c" |
| 27 |
#include "compressionparams.c" |
| 28 |
#include "compressionreader.c" |
| 29 |
#include "compressionwriter.c" |
| 30 |
#include "compressobj.c" |
| 31 |
#include "compressor.c" |
| 32 |
#include "compressoriterator.c" |
| 33 |
#include "constants.c" |
| 34 |
#include "decompressionreader.c" |
| 35 |
#include "decompressionwriter.c" |
| 36 |
#include "decompressobj.c" |
| 37 |
#include "decompressor.c" |
| 38 |
#include "decompressoriterator.c" |
| 39 |
#include "frameparams.c" |
| 40 |
|
| 41 |
PyObject *ZstdError; |
| 42 |
|
| 43 |
static PyObject *estimate_decompression_context_size(PyObject *self) { |
| 44 |
return PyLong_FromSize_t(ZSTD_estimateDCtxSize()); |
| 45 |
} |
| 46 |
|
| 47 |
static PyObject *frame_content_size(PyObject *self, PyObject *args, |
| 48 |
PyObject *kwargs) { |
| 49 |
static char *kwlist[] = {"source", NULL}; |
| 50 |
|
| 51 |
Py_buffer source; |
| 52 |
PyObject *result = NULL; |
| 53 |
unsigned long long size; |
| 54 |
|
| 55 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:frame_content_size", |
| 56 |
kwlist, &source)) { |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
|
| 60 |
size = ZSTD_getFrameContentSize(source.buf, source.len); |
| 61 |
|
| 62 |
if (size == ZSTD_CONTENTSIZE_ERROR) { |
| 63 |
PyErr_SetString(ZstdError, "error when determining content size"); |
| 64 |
} |
| 65 |
else if (size == ZSTD_CONTENTSIZE_UNKNOWN) { |
| 66 |
result = PyLong_FromLong(-1); |
| 67 |
} |
| 68 |
else { |
| 69 |
result = PyLong_FromUnsignedLongLong(size); |
| 70 |
} |
| 71 |
|
| 72 |
PyBuffer_Release(&source); |
| 73 |
|
| 74 |
return result; |
| 75 |
} |
| 76 |
|
| 77 |
static PyObject *frame_header_size(PyObject *self, PyObject *args, |
| 78 |
PyObject *kwargs) { |
| 79 |
static char *kwlist[] = {"source", NULL}; |
| 80 |
|
| 81 |
Py_buffer source; |
| 82 |
PyObject *result = NULL; |
| 83 |
size_t zresult; |
| 84 |
|
| 85 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:frame_header_size", |
| 86 |
kwlist, &source)) { |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
|
| 90 |
zresult = ZSTD_frameHeaderSize(source.buf, source.len); |
| 91 |
if (ZSTD_isError(zresult)) { |
| 92 |
PyErr_Format(ZstdError, "could not determine frame header size: %s", |
| 93 |
ZSTD_getErrorName(zresult)); |
| 94 |
} |
| 95 |
else { |
| 96 |
result = PyLong_FromSize_t(zresult); |
| 97 |
} |
| 98 |
|
| 99 |
PyBuffer_Release(&source); |
| 100 |
|
| 101 |
return result; |
| 102 |
} |
| 103 |
|
| 104 |
static char zstd_doc[] = "Interface to zstandard"; |
| 105 |
|
| 106 |
static PyMethodDef zstd_methods[] = { |
| 107 |
{"estimate_decompression_context_size", |
| 108 |
(PyCFunction)estimate_decompression_context_size, METH_NOARGS, NULL}, |
| 109 |
{"frame_content_size", (PyCFunction)frame_content_size, |
| 110 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 111 |
{"frame_header_size", (PyCFunction)frame_header_size, |
| 112 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 113 |
{"get_frame_parameters", (PyCFunction)get_frame_parameters, |
| 114 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 115 |
{"train_dictionary", (PyCFunction)train_dictionary, |
| 116 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 117 |
{NULL, NULL}}; |
| 118 |
|
| 119 |
void bufferutil_module_init(PyObject *mod); |
| 120 |
void compressobj_module_init(PyObject *mod); |
| 121 |
void compressor_module_init(PyObject *mod); |
| 122 |
void compressionparams_module_init(PyObject *mod); |
| 123 |
void constants_module_init(PyObject *mod); |
| 124 |
void compressionchunker_module_init(PyObject *mod); |
| 125 |
void compressiondict_module_init(PyObject *mod); |
| 126 |
void compressionreader_module_init(PyObject *mod); |
| 127 |
void compressionwriter_module_init(PyObject *mod); |
| 128 |
void compressoriterator_module_init(PyObject *mod); |
| 129 |
void decompressor_module_init(PyObject *mod); |
| 130 |
void decompressobj_module_init(PyObject *mod); |
| 131 |
void decompressionreader_module_init(PyObject *mod); |
| 132 |
void decompressionwriter_module_init(PyObject *mod); |
| 133 |
void decompressoriterator_module_init(PyObject *mod); |
| 134 |
void frameparams_module_init(PyObject *mod); |
| 135 |
|
| 136 |
void zstd_module_init(PyObject *m) { |
| 137 |
#ifdef Py_GIL_DISABLED |
| 138 |
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED); |
| 139 |
#endif |
| 140 |
|
| 141 |
/* |
| 142 |
python-zstandard relies on unstable zstd C API features. This means |
| 143 |
that changes in zstd may break expectations in python-zstandard. |
| 144 |
|
| 145 |
python-zstandard is distributed with a copy of the zstd sources. |
| 146 |
python-zstandard is only guaranteed to work with the bundled version |
| 147 |
of zstd. |
| 148 |
|
| 149 |
However, downstream redistributors or packagers may unbundle zstd |
| 150 |
from python-zstandard. This can result in a mismatch between zstd |
| 151 |
versions and API semantics. This essentially "voids the warranty" |
| 152 |
of python-zstandard and may cause undefined behavior. |
| 153 |
|
| 154 |
We detect this mismatch here and refuse to load the module if this |
| 155 |
scenario is detected. |
| 156 |
|
| 157 |
Historically we required exact matches. But over the years the churn |
| 158 |
in libzstd became reasonable and we relaxed this constraint to a minimum |
| 159 |
version check. Our assumption going forward is that we will only rely on |
| 160 |
unstable C API features that are in reality stable for years. |
| 161 |
*/ |
| 162 |
PyObject *features = NULL; |
| 163 |
PyObject *feature = NULL; |
| 164 |
unsigned zstd_version_no = ZSTD_versionNumber(); |
| 165 |
unsigned zstd_version_min = 10506; |
| 166 |
// if either compile-time or runtime version of libzstd is lower than expected, abort initialization |
| 167 |
if (ZSTD_VERSION_NUMBER < zstd_version_min || |
| 168 |
zstd_version_no < zstd_version_min) { |
| 169 |
PyErr_Format( |
| 170 |
PyExc_ImportError, |
| 171 |
"zstd C API versions mismatch; Python bindings were not " |
| 172 |
"compiled/linked against expected zstd version (%u returned by the " |
| 173 |
"lib, %u hardcoded in zstd headers, %u hardcoded in the cext)", |
| 174 |
zstd_version_no, ZSTD_VERSION_NUMBER, zstd_version_min); |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
features = PySet_New(NULL); |
| 179 |
if (NULL == features) { |
| 180 |
PyErr_SetString(PyExc_ImportError, "could not create empty set"); |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
feature = PyUnicode_FromString("buffer_types"); |
| 185 |
if (NULL == feature) { |
| 186 |
PyErr_SetString(PyExc_ImportError, "could not create feature string"); |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
if (PySet_Add(features, feature) == -1) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
Py_DECREF(feature); |
| 195 |
|
| 196 |
#ifdef HAVE_ZSTD_POOL_APIS |
| 197 |
feature = PyUnicode_FromString("multi_compress_to_buffer"); |
| 198 |
if (NULL == feature) { |
| 199 |
PyErr_SetString(PyExc_ImportError, "could not create feature string"); |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
if (PySet_Add(features, feature) == -1) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
Py_DECREF(feature); |
| 208 |
#endif |
| 209 |
|
| 210 |
#ifdef HAVE_ZSTD_POOL_APIS |
| 211 |
feature = PyUnicode_FromString("multi_decompress_to_buffer"); |
| 212 |
if (NULL == feature) { |
| 213 |
PyErr_SetString(PyExc_ImportError, "could not create feature string"); |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
if (PySet_Add(features, feature) == -1) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
Py_DECREF(feature); |
| 222 |
#endif |
| 223 |
|
| 224 |
if (PyObject_SetAttrString(m, "backend_features", features) == -1) { |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
Py_DECREF(features); |
| 229 |
|
| 230 |
bufferutil_module_init(m); |
| 231 |
compressionparams_module_init(m); |
| 232 |
compressiondict_module_init(m); |
| 233 |
compressobj_module_init(m); |
| 234 |
compressor_module_init(m); |
| 235 |
compressionchunker_module_init(m); |
| 236 |
compressionreader_module_init(m); |
| 237 |
compressionwriter_module_init(m); |
| 238 |
compressoriterator_module_init(m); |
| 239 |
constants_module_init(m); |
| 240 |
decompressor_module_init(m); |
| 241 |
decompressobj_module_init(m); |
| 242 |
decompressionreader_module_init(m); |
| 243 |
decompressionwriter_module_init(m); |
| 244 |
decompressoriterator_module_init(m); |
| 245 |
frameparams_module_init(m); |
| 246 |
} |
| 247 |
|
| 248 |
#if defined(__GNUC__) && (__GNUC__ >= 4) |
| 249 |
#define PYTHON_ZSTD_VISIBILITY __attribute__((visibility("default"))) |
| 250 |
#else |
| 251 |
#define PYTHON_ZSTD_VISIBILITY |
| 252 |
#endif |
| 253 |
|
| 254 |
static struct PyModuleDef zstd_module = {PyModuleDef_HEAD_INIT, "backend_c", |
| 255 |
zstd_doc, -1, zstd_methods}; |
| 256 |
|
| 257 |
PYTHON_ZSTD_VISIBILITY PyMODINIT_FUNC PyInit_backend_c(void) { |
| 258 |
PyObject *m = PyModule_Create(&zstd_module); |
| 259 |
if (m) { |
| 260 |
zstd_module_init(m); |
| 261 |
if (PyErr_Occurred()) { |
| 262 |
Py_DECREF(m); |
| 263 |
m = NULL; |
| 264 |
} |
| 265 |
} |
| 266 |
return m; |
| 267 |
} |
| 268 |
|
| 269 |
/* Attempt to resolve the number of CPUs in the system. */ |
| 270 |
int cpu_count() { |
| 271 |
int count = 0; |
| 272 |
|
| 273 |
#if defined(_WIN32) |
| 274 |
SYSTEM_INFO si; |
| 275 |
si.dwNumberOfProcessors = 0; |
| 276 |
GetSystemInfo(&si); |
| 277 |
count = si.dwNumberOfProcessors; |
| 278 |
#elif defined(__APPLE__) |
| 279 |
int num; |
| 280 |
size_t size = sizeof(int); |
| 281 |
|
| 282 |
if (0 == sysctlbyname("hw.logicalcpu", &num, &size, NULL, 0)) { |
| 283 |
count = num; |
| 284 |
} |
| 285 |
#elif defined(__linux__) |
| 286 |
count = sysconf(_SC_NPROCESSORS_ONLN); |
| 287 |
#elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || \ |
| 288 |
defined(__DragonFly__) |
| 289 |
int mib[2]; |
| 290 |
size_t len = sizeof(count); |
| 291 |
mib[0] = CTL_HW; |
| 292 |
mib[1] = HW_NCPU; |
| 293 |
if (0 != sysctl(mib, 2, &count, &len, NULL, 0)) { |
| 294 |
count = 0; |
| 295 |
} |
| 296 |
#elif defined(__hpux) |
| 297 |
count = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 298 |
#endif |
| 299 |
|
| 300 |
return count; |
| 301 |
} |
| 302 |
|
| 303 |
size_t roundpow2(size_t i) { |
| 304 |
i--; |
| 305 |
i |= i >> 1; |
| 306 |
i |= i >> 2; |
| 307 |
i |= i >> 4; |
| 308 |
i |= i >> 8; |
| 309 |
i |= i >> 16; |
| 310 |
i++; |
| 311 |
|
| 312 |
return i; |
| 313 |
} |
| 314 |
|
| 315 |
/* Safer version of _PyBytes_Resize(). |
| 316 |
* |
| 317 |
* _PyBytes_Resize() only works if the refcount is 1. In some scenarios, |
| 318 |
* we can get an object with a refcount > 1, even if it was just created |
| 319 |
* with PyBytes_FromStringAndSize()! That's because (at least) CPython |
| 320 |
* pre-allocates PyBytes instances of size 1 for every possible byte value. |
| 321 |
* |
| 322 |
* If non-0 is returned, obj may or may not be NULL. |
| 323 |
*/ |
| 324 |
int safe_pybytes_resize(PyObject **obj, Py_ssize_t size) { |
| 325 |
PyObject *tmp; |
| 326 |
|
| 327 |
if (PyUnstable_Object_IsUniquelyReferenced(*obj)) { |
| 328 |
return _PyBytes_Resize(obj, size); |
| 329 |
} |
| 330 |
|
| 331 |
tmp = PyBytes_FromStringAndSize(NULL, size); |
| 332 |
if (!tmp) { |
| 333 |
return -1; |
| 334 |
} |
| 335 |
|
| 336 |
memcpy(PyBytes_AS_STRING(tmp), PyBytes_AS_STRING(*obj), |
| 337 |
PyBytes_GET_SIZE(*obj)); |
| 338 |
|
| 339 |
Py_DECREF(*obj); |
| 340 |
*obj = tmp; |
| 341 |
|
| 342 |
return 0; |
| 343 |
} |
| 344 |
|
| 345 |
// Set/raise an `io.UnsupportedOperation` exception. |
| 346 |
void set_io_unsupported_operation(void) { |
| 347 |
PyObject *iomod; |
| 348 |
PyObject *exc; |
| 349 |
|
| 350 |
iomod = PyImport_ImportModule("io"); |
| 351 |
if (NULL == iomod) { |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
exc = PyObject_GetAttrString(iomod, "UnsupportedOperation"); |
| 356 |
if (NULL == exc) { |
| 357 |
Py_DECREF(iomod); |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
PyErr_SetNone(exc); |
| 362 |
Py_DECREF(exc); |
| 363 |
Py_DECREF(iomod); |
| 364 |
} |