| 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 |
#ifndef PYTHON_ZSTANDARD_H |
| 10 |
#define PYTHON_ZSTANDARD_H |
| 11 |
|
| 12 |
#define PY_SSIZE_T_CLEAN |
| 13 |
#include <Python.h> |
| 14 |
#include <pythoncapi_compat.h> |
| 15 |
#include "structmember.h" |
| 16 |
|
| 17 |
#define ZSTD_STATIC_LINKING_ONLY |
| 18 |
#define ZDICT_STATIC_LINKING_ONLY |
| 19 |
|
| 20 |
#ifdef ZSTD_SINGLE_FILE |
| 21 |
#include <zstd.c> |
| 22 |
|
| 23 |
/* We use private APIs from pool.h. We can't rely on availability |
| 24 |
of this header or symbols when linking against the system libzstd. |
| 25 |
But we know it works when using the bundled single file library. */ |
| 26 |
#define HAVE_ZSTD_POOL_APIS |
| 27 |
#else |
| 28 |
#include <zdict.h> |
| 29 |
#include <zstd.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
/* Remember to change the string in zstandard/__init__.py, rust-ext/src/lib.rs, |
| 33 |
and debian/changelog as well */ |
| 34 |
#define PYTHON_ZSTANDARD_VERSION "0.25.0" |
| 35 |
|
| 36 |
typedef enum { |
| 37 |
compressorobj_flush_finish, |
| 38 |
compressorobj_flush_block, |
| 39 |
} CompressorObj_Flush; |
| 40 |
|
| 41 |
/* |
| 42 |
Represents a ZstdCompressionParameters type. |
| 43 |
|
| 44 |
This type holds all the low-level compression parameters that can be set. |
| 45 |
*/ |
| 46 |
typedef struct { |
| 47 |
PyObject_HEAD ZSTD_CCtx_params *params; |
| 48 |
} ZstdCompressionParametersObject; |
| 49 |
|
| 50 |
extern PyTypeObject *ZstdCompressionParametersType; |
| 51 |
|
| 52 |
/* |
| 53 |
Represents a FrameParameters type. |
| 54 |
|
| 55 |
This type is basically a wrapper around ZSTD_frameParams. |
| 56 |
*/ |
| 57 |
typedef struct { |
| 58 |
PyObject_HEAD unsigned long long frameContentSize; |
| 59 |
unsigned long long windowSize; |
| 60 |
unsigned dictID; |
| 61 |
char checksumFlag; |
| 62 |
} FrameParametersObject; |
| 63 |
|
| 64 |
extern PyTypeObject *FrameParametersType; |
| 65 |
|
| 66 |
/* |
| 67 |
Represents a ZstdCompressionDict type. |
| 68 |
|
| 69 |
Instances hold data used for a zstd compression dictionary. |
| 70 |
*/ |
| 71 |
typedef struct { |
| 72 |
PyObject_HEAD |
| 73 |
|
| 74 |
/* Pointer to dictionary data. Owned by self. */ |
| 75 |
void *dictData; |
| 76 |
/* Size of dictionary data. */ |
| 77 |
size_t dictSize; |
| 78 |
ZSTD_dictContentType_e dictType; |
| 79 |
/* k parameter for cover dictionaries. Only populated by train_cover_dict(). |
| 80 |
*/ |
| 81 |
unsigned k; |
| 82 |
/* d parameter for cover dictionaries. Only populated by train_cover_dict(). |
| 83 |
*/ |
| 84 |
unsigned d; |
| 85 |
/* Digested dictionary, suitable for reuse. */ |
| 86 |
ZSTD_CDict *cdict; |
| 87 |
ZSTD_DDict *ddict; |
| 88 |
} ZstdCompressionDict; |
| 89 |
|
| 90 |
extern PyTypeObject *ZstdCompressionDictType; |
| 91 |
|
| 92 |
/* |
| 93 |
Represents a ZstdCompressor type. |
| 94 |
*/ |
| 95 |
typedef struct { |
| 96 |
PyObject_HEAD |
| 97 |
|
| 98 |
/* Number of threads to use for operations. */ |
| 99 |
unsigned int threads; |
| 100 |
/* Pointer to compression dictionary to use. NULL if not using dictionary |
| 101 |
compression. */ |
| 102 |
ZstdCompressionDict *dict; |
| 103 |
/* Compression context to use. Populated during object construction. */ |
| 104 |
ZSTD_CCtx *cctx; |
| 105 |
/* Compression parameters in use. */ |
| 106 |
ZSTD_CCtx_params *params; |
| 107 |
} ZstdCompressor; |
| 108 |
|
| 109 |
extern PyTypeObject *ZstdCompressorType; |
| 110 |
|
| 111 |
typedef struct { |
| 112 |
PyObject_HEAD |
| 113 |
|
| 114 |
ZstdCompressor *compressor; |
| 115 |
ZSTD_outBuffer output; |
| 116 |
int finished; |
| 117 |
} ZstdCompressionObj; |
| 118 |
|
| 119 |
extern PyTypeObject *ZstdCompressionObjType; |
| 120 |
|
| 121 |
typedef struct { |
| 122 |
PyObject_HEAD |
| 123 |
|
| 124 |
ZstdCompressor *compressor; |
| 125 |
PyObject *writer; |
| 126 |
ZSTD_outBuffer output; |
| 127 |
size_t outSize; |
| 128 |
int entered; |
| 129 |
int closing; |
| 130 |
char closed; |
| 131 |
int writeReturnRead; |
| 132 |
int closefd; |
| 133 |
unsigned long long bytesCompressed; |
| 134 |
} ZstdCompressionWriter; |
| 135 |
|
| 136 |
extern PyTypeObject *ZstdCompressionWriterType; |
| 137 |
|
| 138 |
typedef struct { |
| 139 |
PyObject_HEAD |
| 140 |
|
| 141 |
ZstdCompressor *compressor; |
| 142 |
PyObject *reader; |
| 143 |
Py_buffer buffer; |
| 144 |
Py_ssize_t bufferOffset; |
| 145 |
size_t inSize; |
| 146 |
size_t outSize; |
| 147 |
|
| 148 |
ZSTD_inBuffer input; |
| 149 |
ZSTD_outBuffer output; |
| 150 |
int finishedOutput; |
| 151 |
int finishedInput; |
| 152 |
PyObject *readResult; |
| 153 |
} ZstdCompressorIterator; |
| 154 |
|
| 155 |
extern PyTypeObject *ZstdCompressorIteratorType; |
| 156 |
|
| 157 |
typedef struct { |
| 158 |
PyObject_HEAD |
| 159 |
|
| 160 |
ZstdCompressor *compressor; |
| 161 |
PyObject *reader; |
| 162 |
Py_buffer buffer; |
| 163 |
size_t readSize; |
| 164 |
int closefd; |
| 165 |
|
| 166 |
int entered; |
| 167 |
char closed; |
| 168 |
unsigned long long bytesCompressed; |
| 169 |
|
| 170 |
ZSTD_inBuffer input; |
| 171 |
ZSTD_outBuffer output; |
| 172 |
int finishedInput; |
| 173 |
int finishedOutput; |
| 174 |
PyObject *readResult; |
| 175 |
} ZstdCompressionReader; |
| 176 |
|
| 177 |
extern PyTypeObject *ZstdCompressionReaderType; |
| 178 |
|
| 179 |
typedef struct { |
| 180 |
PyObject_HEAD |
| 181 |
|
| 182 |
ZstdCompressor *compressor; |
| 183 |
ZSTD_inBuffer input; |
| 184 |
ZSTD_outBuffer output; |
| 185 |
Py_buffer inBuffer; |
| 186 |
int finished; |
| 187 |
size_t chunkSize; |
| 188 |
} ZstdCompressionChunker; |
| 189 |
|
| 190 |
extern PyTypeObject *ZstdCompressionChunkerType; |
| 191 |
|
| 192 |
typedef enum { |
| 193 |
compressionchunker_mode_normal, |
| 194 |
compressionchunker_mode_flush, |
| 195 |
compressionchunker_mode_finish, |
| 196 |
} CompressionChunkerMode; |
| 197 |
|
| 198 |
typedef struct { |
| 199 |
PyObject_HEAD |
| 200 |
|
| 201 |
ZstdCompressionChunker *chunker; |
| 202 |
CompressionChunkerMode mode; |
| 203 |
} ZstdCompressionChunkerIterator; |
| 204 |
|
| 205 |
extern PyTypeObject *ZstdCompressionChunkerIteratorType; |
| 206 |
|
| 207 |
typedef struct { |
| 208 |
PyObject_HEAD |
| 209 |
|
| 210 |
ZSTD_DCtx *dctx; |
| 211 |
ZstdCompressionDict *dict; |
| 212 |
size_t maxWindowSize; |
| 213 |
ZSTD_format_e format; |
| 214 |
} ZstdDecompressor; |
| 215 |
|
| 216 |
extern PyTypeObject *ZstdDecompressorType; |
| 217 |
|
| 218 |
typedef struct { |
| 219 |
PyObject_HEAD |
| 220 |
|
| 221 |
ZstdDecompressor *decompressor; |
| 222 |
size_t outSize; |
| 223 |
int readAcrossFrames; |
| 224 |
int finished; |
| 225 |
PyObject *unused_data; |
| 226 |
} ZstdDecompressionObj; |
| 227 |
|
| 228 |
extern PyTypeObject *ZstdDecompressionObjType; |
| 229 |
|
| 230 |
typedef struct { |
| 231 |
PyObject_HEAD |
| 232 |
|
| 233 |
/* Parent decompressor to which this object is associated. */ |
| 234 |
ZstdDecompressor *decompressor; |
| 235 |
/* Object to read() from (if reading from a stream). */ |
| 236 |
PyObject *reader; |
| 237 |
/* Size for read() operations on reader. */ |
| 238 |
size_t readSize; |
| 239 |
/* Whether a read() can return data spanning multiple zstd frames. */ |
| 240 |
int readAcrossFrames; |
| 241 |
/* Buffer to read from (if reading from a buffer). */ |
| 242 |
Py_buffer buffer; |
| 243 |
/* Whether to close the inner object on close() */ |
| 244 |
int closefd; |
| 245 |
|
| 246 |
/* Whether the context manager is active. */ |
| 247 |
int entered; |
| 248 |
/* Whether we've closed the stream. */ |
| 249 |
char closed; |
| 250 |
|
| 251 |
/* Number of bytes decompressed and returned to user. */ |
| 252 |
unsigned long long bytesDecompressed; |
| 253 |
|
| 254 |
/* Tracks data going into decompressor. */ |
| 255 |
ZSTD_inBuffer input; |
| 256 |
|
| 257 |
/* Holds output from read() operation on reader. */ |
| 258 |
PyObject *readResult; |
| 259 |
|
| 260 |
/* Whether all input has been sent to the decompressor. */ |
| 261 |
int finishedInput; |
| 262 |
/* Whether all output has been flushed from the decompressor. */ |
| 263 |
int finishedOutput; |
| 264 |
} ZstdDecompressionReader; |
| 265 |
|
| 266 |
extern PyTypeObject *ZstdDecompressionReaderType; |
| 267 |
|
| 268 |
typedef struct { |
| 269 |
PyObject_HEAD |
| 270 |
|
| 271 |
ZstdDecompressor *decompressor; |
| 272 |
PyObject *writer; |
| 273 |
size_t outSize; |
| 274 |
int entered; |
| 275 |
int closing; |
| 276 |
char closed; |
| 277 |
int writeReturnRead; |
| 278 |
int closefd; |
| 279 |
} ZstdDecompressionWriter; |
| 280 |
|
| 281 |
extern PyTypeObject *ZstdDecompressionWriterType; |
| 282 |
|
| 283 |
typedef struct { |
| 284 |
PyObject_HEAD |
| 285 |
|
| 286 |
ZstdDecompressor *decompressor; |
| 287 |
PyObject *reader; |
| 288 |
Py_buffer buffer; |
| 289 |
Py_ssize_t bufferOffset; |
| 290 |
size_t inSize; |
| 291 |
size_t outSize; |
| 292 |
size_t skipBytes; |
| 293 |
ZSTD_inBuffer input; |
| 294 |
ZSTD_outBuffer output; |
| 295 |
Py_ssize_t readCount; |
| 296 |
int finishedInput; |
| 297 |
int finishedOutput; |
| 298 |
} ZstdDecompressorIterator; |
| 299 |
|
| 300 |
extern PyTypeObject *ZstdDecompressorIteratorType; |
| 301 |
|
| 302 |
typedef struct { |
| 303 |
int errored; |
| 304 |
PyObject *chunk; |
| 305 |
} DecompressorIteratorResult; |
| 306 |
|
| 307 |
typedef struct { |
| 308 |
/* The public API is that these are 64-bit unsigned integers. So these can't |
| 309 |
* be size_t, even though values larger than SIZE_MAX or PY_SSIZE_T_MAX may |
| 310 |
* be nonsensical for this platform. */ |
| 311 |
unsigned long long offset; |
| 312 |
unsigned long long length; |
| 313 |
} BufferSegment; |
| 314 |
|
| 315 |
typedef struct { |
| 316 |
PyObject_HEAD |
| 317 |
|
| 318 |
PyObject *parent; |
| 319 |
BufferSegment *segments; |
| 320 |
Py_ssize_t segmentCount; |
| 321 |
} ZstdBufferSegments; |
| 322 |
|
| 323 |
extern PyTypeObject *ZstdBufferSegmentsType; |
| 324 |
|
| 325 |
typedef struct { |
| 326 |
PyObject_HEAD |
| 327 |
|
| 328 |
PyObject *parent; |
| 329 |
void *data; |
| 330 |
Py_ssize_t dataSize; |
| 331 |
unsigned long long offset; |
| 332 |
} ZstdBufferSegment; |
| 333 |
|
| 334 |
extern PyTypeObject *ZstdBufferSegmentType; |
| 335 |
|
| 336 |
typedef struct { |
| 337 |
PyObject_HEAD |
| 338 |
|
| 339 |
Py_buffer parent; |
| 340 |
void *data; |
| 341 |
unsigned long long dataSize; |
| 342 |
BufferSegment *segments; |
| 343 |
Py_ssize_t segmentCount; |
| 344 |
int useFree; |
| 345 |
} ZstdBufferWithSegments; |
| 346 |
|
| 347 |
extern PyTypeObject *ZstdBufferWithSegmentsType; |
| 348 |
|
| 349 |
/** |
| 350 |
* An ordered collection of BufferWithSegments exposed as a squashed collection. |
| 351 |
* |
| 352 |
* This type provides a virtual view spanning multiple BufferWithSegments |
| 353 |
* instances. It allows multiple instances to be "chained" together and |
| 354 |
* exposed as a single collection. e.g. if there are 2 buffers holding |
| 355 |
* 10 segments each, then o[14] will access the 5th segment in the 2nd buffer. |
| 356 |
*/ |
| 357 |
typedef struct { |
| 358 |
PyObject_HEAD |
| 359 |
|
| 360 |
/* An array of buffers that should be exposed through this instance. */ |
| 361 |
ZstdBufferWithSegments **buffers; |
| 362 |
/* Number of elements in buffers array. */ |
| 363 |
Py_ssize_t bufferCount; |
| 364 |
/* Array of first offset in each buffer instance. 0th entry corresponds |
| 365 |
to number of elements in the 0th buffer. 1st entry corresponds to the |
| 366 |
sum of elements in 0th and 1st buffers. */ |
| 367 |
Py_ssize_t *firstElements; |
| 368 |
} ZstdBufferWithSegmentsCollection; |
| 369 |
|
| 370 |
extern PyTypeObject *ZstdBufferWithSegmentsCollectionType; |
| 371 |
|
| 372 |
int set_parameter(ZSTD_CCtx_params *params, ZSTD_cParameter param, int value); |
| 373 |
int set_parameters(ZSTD_CCtx_params *params, |
| 374 |
ZstdCompressionParametersObject *obj); |
| 375 |
int to_cparams(ZstdCompressionParametersObject *params, |
| 376 |
ZSTD_compressionParameters *cparams); |
| 377 |
FrameParametersObject *get_frame_parameters(PyObject *self, PyObject *args, |
| 378 |
PyObject *kwargs); |
| 379 |
int ensure_ddict(ZstdCompressionDict *dict); |
| 380 |
int ensure_dctx(ZstdDecompressor *decompressor, int loadDict); |
| 381 |
ZstdCompressionDict *train_dictionary(PyObject *self, PyObject *args, |
| 382 |
PyObject *kwargs); |
| 383 |
ZstdBufferWithSegments * |
| 384 |
BufferWithSegments_FromMemory(void *data, unsigned long long dataSize, |
| 385 |
BufferSegment *segments, Py_ssize_t segmentsSize); |
| 386 |
Py_ssize_t |
| 387 |
BufferWithSegmentsCollection_length(ZstdBufferWithSegmentsCollection *); |
| 388 |
int cpu_count(void); |
| 389 |
size_t roundpow2(size_t); |
| 390 |
int safe_pybytes_resize(PyObject **obj, Py_ssize_t size); |
| 391 |
void set_io_unsupported_operation(void); |
| 392 |
|
| 393 |
#endif |