compressoriterator.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. static void ZstdCompressorIterator_dealloc(ZstdCompressorIterator *self) {
  12. Py_XDECREF(self->readResult);
  13. Py_XDECREF(self->compressor);
  14. Py_XDECREF(self->reader);
  15. if (self->buffer.buf) {
  16. PyBuffer_Release(&self->buffer);
  17. memset(&self->buffer, 0, sizeof(self->buffer));
  18. }
  19. if (self->output.dst) {
  20. PyMem_Free(self->output.dst);
  21. self->output.dst = NULL;
  22. }
  23. PyObject_Del(self);
  24. }
  25. static PyObject *ZstdCompressorIterator_iter(PyObject *self) {
  26. Py_INCREF(self);
  27. return self;
  28. }
  29. static PyObject *ZstdCompressorIterator_iternext(ZstdCompressorIterator *self) {
  30. size_t zresult;
  31. PyObject *readResult = NULL;
  32. PyObject *chunk;
  33. char *readBuffer;
  34. Py_ssize_t readSize = 0;
  35. Py_ssize_t bufferRemaining;
  36. if (self->finishedOutput) {
  37. PyErr_SetString(PyExc_StopIteration, "output flushed");
  38. return NULL;
  39. }
  40. feedcompressor:
  41. /* If we have data left in the input, consume it. */
  42. if (self->input.pos < self->input.size) {
  43. Py_BEGIN_ALLOW_THREADS zresult =
  44. ZSTD_compressStream2(self->compressor->cctx, &self->output,
  45. &self->input, ZSTD_e_continue);
  46. Py_END_ALLOW_THREADS
  47. /* Release the Python object holding the input buffer. */
  48. if (self->input.pos == self->input.size) {
  49. self->input.src = NULL;
  50. self->input.pos = 0;
  51. self->input.size = 0;
  52. Py_DECREF(self->readResult);
  53. self->readResult = NULL;
  54. }
  55. if (ZSTD_isError(zresult)) {
  56. PyErr_Format(ZstdError, "zstd compress error: %s",
  57. ZSTD_getErrorName(zresult));
  58. return NULL;
  59. }
  60. /* If it produced output data, emit it. */
  61. if (self->output.pos) {
  62. chunk =
  63. PyBytes_FromStringAndSize(self->output.dst, self->output.pos);
  64. self->output.pos = 0;
  65. return chunk;
  66. }
  67. }
  68. /* We should never have output data sitting around after a previous call. */
  69. assert(self->output.pos == 0);
  70. /* The code above should have either emitted a chunk and returned or
  71. consumed the entire input buffer. So the state of the input buffer is not
  72. relevant. */
  73. if (!self->finishedInput) {
  74. if (self->reader) {
  75. readResult =
  76. PyObject_CallMethod(self->reader, "read", "I", self->inSize);
  77. if (!readResult) {
  78. return NULL;
  79. }
  80. PyBytes_AsStringAndSize(readResult, &readBuffer, &readSize);
  81. }
  82. else {
  83. assert(self->buffer.buf);
  84. /* Only support contiguous C arrays. */
  85. assert(self->buffer.strides == NULL &&
  86. 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 zresult = ZSTD_compressStream2(
  126. self->compressor->cctx, &self->output, &self->input, ZSTD_e_continue);
  127. Py_END_ALLOW_THREADS
  128. /* The input buffer currently points to memory managed by Python
  129. (readBuffer). This object was allocated by this function. If it wasn't
  130. fully consumed, we need to release it in a subsequent function call.
  131. If it is fully consumed, do that now.
  132. */
  133. if (self->input.pos == self->input.size) {
  134. self->input.src = NULL;
  135. self->input.pos = 0;
  136. self->input.size = 0;
  137. Py_XDECREF(self->readResult);
  138. self->readResult = NULL;
  139. }
  140. if (ZSTD_isError(zresult)) {
  141. PyErr_Format(ZstdError, "zstd compress error: %s",
  142. 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. PyType_Slot ZstdCompressorIteratorSlots[] = {
  155. {Py_tp_dealloc, ZstdCompressorIterator_dealloc},
  156. {Py_tp_iter, ZstdCompressorIterator_iter},
  157. {Py_tp_iternext, ZstdCompressorIterator_iternext},
  158. {Py_tp_new, PyType_GenericNew},
  159. {0, NULL},
  160. };
  161. PyType_Spec ZstdCompressorIteratorSpec = {
  162. "zstd.ZstdCompressorIterator",
  163. sizeof(ZstdCompressorIterator),
  164. 0,
  165. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
  166. ZstdCompressorIteratorSlots,
  167. };
  168. PyTypeObject *ZstdCompressorIteratorType;
  169. void compressoriterator_module_init(PyObject *mod) {
  170. ZstdCompressorIteratorType =
  171. (PyTypeObject *)PyType_FromSpec(&ZstdCompressorIteratorSpec);
  172. if (PyType_Ready(ZstdCompressorIteratorType) < 0) {
  173. return;
  174. }
  175. }