compressoriterator.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include "python-zstandard.h"
  9. #define min(a, b) (((a) < (b)) ? (a) : (b))
  10. extern PyObject* ZstdError;
  11. PyDoc_STRVAR(ZstdCompressorIterator__doc__,
  12. "Represents an iterator of compressed data.\n"
  13. );
  14. static void ZstdCompressorIterator_dealloc(ZstdCompressorIterator* self) {
  15. Py_XDECREF(self->readResult);
  16. Py_XDECREF(self->compressor);
  17. Py_XDECREF(self->reader);
  18. if (self->buffer.buf) {
  19. PyBuffer_Release(&self->buffer);
  20. memset(&self->buffer, 0, sizeof(self->buffer));
  21. }
  22. if (self->output.dst) {
  23. PyMem_Free(self->output.dst);
  24. self->output.dst = NULL;
  25. }
  26. PyObject_Del(self);
  27. }
  28. static PyObject* ZstdCompressorIterator_iter(PyObject* self) {
  29. Py_INCREF(self);
  30. return self;
  31. }
  32. static PyObject* ZstdCompressorIterator_iternext(ZstdCompressorIterator* self) {
  33. size_t zresult;
  34. PyObject* readResult = NULL;
  35. PyObject* chunk;
  36. char* readBuffer;
  37. Py_ssize_t readSize = 0;
  38. Py_ssize_t bufferRemaining;
  39. if (self->finishedOutput) {
  40. PyErr_SetString(PyExc_StopIteration, "output flushed");
  41. return NULL;
  42. }
  43. feedcompressor:
  44. /* If we have data left in the input, consume it. */
  45. if (self->input.pos < self->input.size) {
  46. Py_BEGIN_ALLOW_THREADS
  47. zresult = ZSTD_compressStream2(self->compressor->cctx, &self->output,
  48. &self->input, ZSTD_e_continue);
  49. Py_END_ALLOW_THREADS
  50. /* Release the Python object holding the input buffer. */
  51. if (self->input.pos == self->input.size) {
  52. self->input.src = NULL;
  53. self->input.pos = 0;
  54. self->input.size = 0;
  55. Py_DECREF(self->readResult);
  56. self->readResult = NULL;
  57. }
  58. if (ZSTD_isError(zresult)) {
  59. PyErr_Format(ZstdError, "zstd compress error: %s", ZSTD_getErrorName(zresult));
  60. return NULL;
  61. }
  62. /* If it produced output data, emit it. */
  63. if (self->output.pos) {
  64. chunk = PyBytes_FromStringAndSize(self->output.dst, self->output.pos);
  65. self->output.pos = 0;
  66. return chunk;
  67. }
  68. }
  69. /* We should never have output data sitting around after a previous call. */
  70. assert(self->output.pos == 0);
  71. /* The code above should have either emitted a chunk and returned or consumed
  72. the entire input buffer. So the state of the input buffer is not
  73. relevant. */
  74. if (!self->finishedInput) {
  75. if (self->reader) {
  76. readResult = PyObject_CallMethod(self->reader, "read", "I", self->inSize);
  77. if (!readResult) {
  78. PyErr_SetString(ZstdError, "could not read() from source");
  79. return NULL;
  80. }
  81. PyBytes_AsStringAndSize(readResult, &readBuffer, &readSize);
  82. }
  83. else {
  84. assert(self->buffer.buf);
  85. /* Only support contiguous C arrays. */
  86. assert(self->buffer.strides == NULL && self->buffer.suboffsets == NULL);
  87. assert(self->buffer.itemsize == 1);
  88. readBuffer = (char*)self->buffer.buf + self->bufferOffset;
  89. bufferRemaining = self->buffer.len - self->bufferOffset;
  90. readSize = min(bufferRemaining, (Py_ssize_t)self->inSize);
  91. self->bufferOffset += readSize;
  92. }
  93. if (0 == readSize) {
  94. Py_XDECREF(readResult);
  95. self->finishedInput = 1;
  96. }
  97. else {
  98. self->readResult = readResult;
  99. }
  100. }
  101. /* EOF */
  102. if (0 == readSize) {
  103. self->input.src = NULL;
  104. self->input.size = 0;
  105. self->input.pos = 0;
  106. zresult = ZSTD_compressStream2(self->compressor->cctx, &self->output,
  107. &self->input, ZSTD_e_end);
  108. if (ZSTD_isError(zresult)) {
  109. PyErr_Format(ZstdError, "error ending compression stream: %s",
  110. ZSTD_getErrorName(zresult));
  111. return NULL;
  112. }
  113. assert(self->output.pos);
  114. if (0 == zresult) {
  115. self->finishedOutput = 1;
  116. }
  117. chunk = PyBytes_FromStringAndSize(self->output.dst, self->output.pos);
  118. self->output.pos = 0;
  119. return chunk;
  120. }
  121. /* New data from reader. Feed into compressor. */
  122. self->input.src = readBuffer;
  123. self->input.size = readSize;
  124. self->input.pos = 0;
  125. Py_BEGIN_ALLOW_THREADS
  126. zresult = ZSTD_compressStream2(self->compressor->cctx, &self->output,
  127. &self->input, ZSTD_e_continue);
  128. Py_END_ALLOW_THREADS
  129. /* The input buffer currently points to memory managed by Python
  130. (readBuffer). This object was allocated by this function. If it wasn't
  131. fully consumed, we need to release it in a subsequent function call.
  132. If it is fully consumed, do that now.
  133. */
  134. if (self->input.pos == self->input.size) {
  135. self->input.src = NULL;
  136. self->input.pos = 0;
  137. self->input.size = 0;
  138. Py_XDECREF(self->readResult);
  139. self->readResult = NULL;
  140. }
  141. if (ZSTD_isError(zresult)) {
  142. PyErr_Format(ZstdError, "zstd compress error: %s", ZSTD_getErrorName(zresult));
  143. return NULL;
  144. }
  145. assert(self->input.pos <= self->input.size);
  146. /* If we didn't write anything, start the process over. */
  147. if (0 == self->output.pos) {
  148. goto feedcompressor;
  149. }
  150. chunk = PyBytes_FromStringAndSize(self->output.dst, self->output.pos);
  151. self->output.pos = 0;
  152. return chunk;
  153. }
  154. PyTypeObject ZstdCompressorIteratorType = {
  155. PyVarObject_HEAD_INIT(NULL, 0)
  156. "zstd.ZstdCompressorIterator", /* tp_name */
  157. sizeof(ZstdCompressorIterator), /* tp_basicsize */
  158. 0, /* tp_itemsize */
  159. (destructor)ZstdCompressorIterator_dealloc, /* tp_dealloc */
  160. 0, /* tp_print */
  161. 0, /* tp_getattr */
  162. 0, /* tp_setattr */
  163. 0, /* tp_compare */
  164. 0, /* tp_repr */
  165. 0, /* tp_as_number */
  166. 0, /* tp_as_sequence */
  167. 0, /* tp_as_mapping */
  168. 0, /* tp_hash */
  169. 0, /* tp_call */
  170. 0, /* tp_str */
  171. 0, /* tp_getattro */
  172. 0, /* tp_setattro */
  173. 0, /* tp_as_buffer */
  174. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
  175. ZstdCompressorIterator__doc__, /* tp_doc */
  176. 0, /* tp_traverse */
  177. 0, /* tp_clear */
  178. 0, /* tp_richcompare */
  179. 0, /* tp_weaklistoffset */
  180. ZstdCompressorIterator_iter, /* tp_iter */
  181. (iternextfunc)ZstdCompressorIterator_iternext, /* tp_iternext */
  182. 0, /* tp_methods */
  183. 0, /* tp_members */
  184. 0, /* tp_getset */
  185. 0, /* tp_base */
  186. 0, /* tp_dict */
  187. 0, /* tp_descr_get */
  188. 0, /* tp_descr_set */
  189. 0, /* tp_dictoffset */
  190. 0, /* tp_init */
  191. 0, /* tp_alloc */
  192. PyType_GenericNew, /* tp_new */
  193. };
  194. void compressoriterator_module_init(PyObject* mod) {
  195. Py_TYPE(&ZstdCompressorIteratorType) = &PyType_Type;
  196. if (PyType_Ready(&ZstdCompressorIteratorType) < 0) {
  197. return;
  198. }
  199. }