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