decompressobj.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. extern PyObject *ZstdError;
  10. static void DecompressionObj_dealloc(ZstdDecompressionObj *self) {
  11. Py_XDECREF(self->decompressor);
  12. Py_CLEAR(self->unused_data);
  13. PyObject_Del(self);
  14. }
  15. static PyObject *DecompressionObj_decompress(ZstdDecompressionObj *self,
  16. PyObject *args, PyObject *kwargs) {
  17. static char *kwlist[] = {"data", NULL};
  18. Py_buffer source;
  19. size_t zresult;
  20. ZSTD_inBuffer input;
  21. ZSTD_outBuffer output;
  22. PyObject *result = NULL;
  23. Py_ssize_t resultSize = 0;
  24. output.dst = NULL;
  25. if (self->finished) {
  26. PyErr_SetString(ZstdError, "cannot use a decompressobj multiple times");
  27. return NULL;
  28. }
  29. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:decompress", kwlist,
  30. &source)) {
  31. return NULL;
  32. }
  33. /* Special case of empty input. Output will always be empty. */
  34. if (source.len == 0) {
  35. result = PyBytes_FromString("");
  36. goto finally;
  37. }
  38. input.src = source.buf;
  39. input.size = source.len;
  40. input.pos = 0;
  41. output.dst = PyMem_Malloc(self->outSize);
  42. if (!output.dst) {
  43. PyErr_NoMemory();
  44. goto except;
  45. }
  46. output.size = self->outSize;
  47. output.pos = 0;
  48. while (1) {
  49. Py_BEGIN_ALLOW_THREADS zresult =
  50. ZSTD_decompressStream(self->decompressor->dctx, &output, &input);
  51. Py_END_ALLOW_THREADS
  52. if (ZSTD_isError(zresult)) {
  53. PyErr_Format(ZstdError, "zstd decompressor error: %s",
  54. ZSTD_getErrorName(zresult));
  55. goto except;
  56. }
  57. if (output.pos) {
  58. if (result) {
  59. resultSize = PyBytes_GET_SIZE(result);
  60. if (-1 ==
  61. safe_pybytes_resize(&result, resultSize + output.pos)) {
  62. Py_XDECREF(result);
  63. goto except;
  64. }
  65. memcpy(PyBytes_AS_STRING(result) + resultSize, output.dst,
  66. output.pos);
  67. }
  68. else {
  69. result = PyBytes_FromStringAndSize(output.dst, output.pos);
  70. if (!result) {
  71. goto except;
  72. }
  73. }
  74. }
  75. if (0 == zresult && !self->readAcrossFrames) {
  76. self->finished = 1;
  77. /* We should only get here at most once. */
  78. assert(!self->unused_data);
  79. self->unused_data = PyBytes_FromStringAndSize((char *)(input.src) + input.pos, input.size - input.pos);
  80. break;
  81. }
  82. else if (0 == zresult && self->readAcrossFrames) {
  83. if (input.pos == input.size) {
  84. break;
  85. } else {
  86. output.pos = 0;
  87. }
  88. }
  89. else if (input.pos == input.size && output.pos == 0) {
  90. break;
  91. }
  92. else {
  93. output.pos = 0;
  94. }
  95. }
  96. if (!result) {
  97. result = PyBytes_FromString("");
  98. }
  99. goto finally;
  100. except:
  101. Py_CLEAR(result);
  102. finally:
  103. PyMem_Free(output.dst);
  104. PyBuffer_Release(&source);
  105. return result;
  106. }
  107. static PyObject *DecompressionObj_flush(ZstdDecompressionObj *self,
  108. PyObject *args, PyObject *kwargs) {
  109. static char *kwlist[] = {"length", NULL};
  110. PyObject *length = NULL;
  111. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:flush", kwlist,
  112. &length)) {
  113. return NULL;
  114. }
  115. return PyBytes_FromString("");
  116. }
  117. static PyObject *DecompressionObj_unused_data(PyObject *self, void *unused) {
  118. ZstdDecompressionObj *slf = (ZstdDecompressionObj *)(self);
  119. if (slf->unused_data) {
  120. Py_INCREF(slf->unused_data);
  121. return slf->unused_data;
  122. } else {
  123. return PyBytes_FromString("");
  124. }
  125. }
  126. static PyObject *DecompressionObj_unconsumed_tail(PyObject *self, void *unused) {
  127. return PyBytes_FromString("");
  128. }
  129. static PyObject *DecompressionObj_eof(PyObject *self, void *unused) {
  130. ZstdDecompressionObj *slf = (ZstdDecompressionObj*)(self);
  131. if (slf->finished) {
  132. Py_RETURN_TRUE;
  133. } else {
  134. Py_RETURN_FALSE;
  135. }
  136. }
  137. static PyMethodDef DecompressionObj_methods[] = {
  138. {"decompress", (PyCFunction)DecompressionObj_decompress,
  139. METH_VARARGS | METH_KEYWORDS, PyDoc_STR("decompress data")},
  140. {"flush", (PyCFunction)DecompressionObj_flush, METH_VARARGS | METH_KEYWORDS,
  141. PyDoc_STR("no-op")},
  142. {NULL, NULL}};
  143. static PyGetSetDef DecompressionObj_getset[] = {
  144. {"unused_data", DecompressionObj_unused_data, NULL, NULL, NULL },
  145. {"unconsumed_tail", DecompressionObj_unconsumed_tail, NULL, NULL, NULL },
  146. {"eof", DecompressionObj_eof, NULL, NULL, NULL },
  147. {NULL}};
  148. PyType_Slot ZstdDecompressionObjSlots[] = {
  149. {Py_tp_dealloc, DecompressionObj_dealloc},
  150. {Py_tp_methods, DecompressionObj_methods},
  151. {Py_tp_getset, DecompressionObj_getset},
  152. {Py_tp_new, PyType_GenericNew},
  153. {0, NULL},
  154. };
  155. PyType_Spec ZstdDecompressionObjSpec = {
  156. "zstd.ZstdDecompressionObj",
  157. sizeof(ZstdDecompressionObj),
  158. 0,
  159. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
  160. ZstdDecompressionObjSlots,
  161. };
  162. PyTypeObject *ZstdDecompressionObjType;
  163. void decompressobj_module_init(PyObject *module) {
  164. ZstdDecompressionObjType =
  165. (PyTypeObject *)PyType_FromSpec(&ZstdDecompressionObjSpec);
  166. if (PyType_Ready(ZstdDecompressionObjType) < 0) {
  167. return;
  168. }
  169. }