python-zstandard.h 8.6 KB

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