python-zstandard.h 10 KB

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