decompressionwriter.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 ZstdDecompressionWriter_dealloc(ZstdDecompressionWriter *self) {
  11. Py_XDECREF(self->decompressor);
  12. Py_XDECREF(self->writer);
  13. PyObject_Del(self);
  14. }
  15. static PyObject *ZstdDecompressionWriter_enter(ZstdDecompressionWriter *self) {
  16. if (self->closed) {
  17. PyErr_SetString(PyExc_ValueError, "stream is closed");
  18. return NULL;
  19. }
  20. if (self->entered) {
  21. PyErr_SetString(ZstdError, "cannot __enter__ multiple times");
  22. return NULL;
  23. }
  24. self->entered = 1;
  25. Py_INCREF(self);
  26. return (PyObject *)self;
  27. }
  28. static PyObject *ZstdDecompressionWriter_exit(ZstdDecompressionWriter *self,
  29. PyObject *args) {
  30. self->entered = 0;
  31. if (NULL == PyObject_CallMethod((PyObject *)self, "close", NULL)) {
  32. return NULL;
  33. }
  34. Py_RETURN_FALSE;
  35. }
  36. static PyObject *
  37. ZstdDecompressionWriter_memory_size(ZstdDecompressionWriter *self) {
  38. return PyLong_FromSize_t(ZSTD_sizeof_DCtx(self->decompressor->dctx));
  39. }
  40. static PyObject *ZstdDecompressionWriter_write(ZstdDecompressionWriter *self,
  41. PyObject *args,
  42. PyObject *kwargs) {
  43. static char *kwlist[] = {"data", NULL};
  44. PyObject *result = NULL;
  45. Py_buffer source;
  46. size_t zresult = 0;
  47. ZSTD_inBuffer input;
  48. ZSTD_outBuffer output;
  49. PyObject *res;
  50. Py_ssize_t totalWrite = 0;
  51. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:write", kwlist,
  52. &source)) {
  53. return NULL;
  54. }
  55. if (self->closed) {
  56. PyErr_SetString(PyExc_ValueError, "stream is closed");
  57. return NULL;
  58. }
  59. output.dst = PyMem_Malloc(self->outSize);
  60. if (!output.dst) {
  61. PyErr_NoMemory();
  62. goto finally;
  63. }
  64. output.size = self->outSize;
  65. output.pos = 0;
  66. input.src = source.buf;
  67. input.size = source.len;
  68. input.pos = 0;
  69. while (input.pos < (size_t)source.len) {
  70. Py_BEGIN_ALLOW_THREADS zresult =
  71. ZSTD_decompressStream(self->decompressor->dctx, &output, &input);
  72. Py_END_ALLOW_THREADS
  73. if (ZSTD_isError(zresult)) {
  74. PyMem_Free(output.dst);
  75. PyErr_Format(ZstdError, "zstd decompress error: %s",
  76. ZSTD_getErrorName(zresult));
  77. goto finally;
  78. }
  79. if (output.pos) {
  80. res = PyObject_CallMethod(self->writer, "write", "y#", output.dst,
  81. output.pos);
  82. if (NULL == res) {
  83. goto finally;
  84. }
  85. Py_XDECREF(res);
  86. totalWrite += output.pos;
  87. output.pos = 0;
  88. }
  89. }
  90. PyMem_Free(output.dst);
  91. if (self->writeReturnRead) {
  92. result = PyLong_FromSize_t(input.pos);
  93. }
  94. else {
  95. result = PyLong_FromSsize_t(totalWrite);
  96. }
  97. finally:
  98. PyBuffer_Release(&source);
  99. return result;
  100. }
  101. static PyObject *ZstdDecompressionWriter_close(ZstdDecompressionWriter *self) {
  102. PyObject *result;
  103. if (self->closed) {
  104. Py_RETURN_NONE;
  105. }
  106. self->closing = 1;
  107. result = PyObject_CallMethod((PyObject *)self, "flush", NULL);
  108. self->closing = 0;
  109. self->closed = 1;
  110. if (NULL == result) {
  111. return NULL;
  112. }
  113. /* Call close on underlying stream as well. */
  114. if (self->closefd && PyObject_HasAttrString(self->writer, "close")) {
  115. return PyObject_CallMethod(self->writer, "close", NULL);
  116. }
  117. Py_RETURN_NONE;
  118. }
  119. static PyObject *ZstdDecompressionWriter_fileno(ZstdDecompressionWriter *self) {
  120. if (PyObject_HasAttrString(self->writer, "fileno")) {
  121. return PyObject_CallMethod(self->writer, "fileno", NULL);
  122. }
  123. else {
  124. PyErr_SetString(PyExc_OSError,
  125. "fileno not available on underlying writer");
  126. return NULL;
  127. }
  128. }
  129. static PyObject *ZstdDecompressionWriter_flush(ZstdDecompressionWriter *self) {
  130. if (self->closed) {
  131. PyErr_SetString(PyExc_ValueError, "stream is closed");
  132. return NULL;
  133. }
  134. if (!self->closing && PyObject_HasAttrString(self->writer, "flush")) {
  135. return PyObject_CallMethod(self->writer, "flush", NULL);
  136. }
  137. else {
  138. Py_RETURN_NONE;
  139. }
  140. }
  141. static PyObject *ZstdDecompressionWriter_iter(PyObject *self) {
  142. set_io_unsupported_operation();
  143. return NULL;
  144. }
  145. static PyObject *ZstdDecompressionWriter_iternext(PyObject *self) {
  146. set_io_unsupported_operation();
  147. return NULL;
  148. }
  149. static PyObject *ZstdDecompressionWriter_false(PyObject *self, PyObject *args) {
  150. Py_RETURN_FALSE;
  151. }
  152. static PyObject *ZstdDecompressionWriter_true(PyObject *self, PyObject *args) {
  153. Py_RETURN_TRUE;
  154. }
  155. static PyObject *ZstdDecompressionWriter_unsupported(PyObject *self,
  156. PyObject *args,
  157. PyObject *kwargs) {
  158. set_io_unsupported_operation();
  159. return NULL;
  160. }
  161. static PyMethodDef ZstdDecompressionWriter_methods[] = {
  162. {"__enter__", (PyCFunction)ZstdDecompressionWriter_enter, METH_NOARGS,
  163. PyDoc_STR("Enter a decompression context.")},
  164. {"__exit__", (PyCFunction)ZstdDecompressionWriter_exit, METH_VARARGS,
  165. PyDoc_STR("Exit a decompression context.")},
  166. {"memory_size", (PyCFunction)ZstdDecompressionWriter_memory_size,
  167. METH_NOARGS,
  168. PyDoc_STR(
  169. "Obtain the memory size in bytes of the underlying decompressor.")},
  170. {"close", (PyCFunction)ZstdDecompressionWriter_close, METH_NOARGS, NULL},
  171. {"fileno", (PyCFunction)ZstdDecompressionWriter_fileno, METH_NOARGS, NULL},
  172. {"flush", (PyCFunction)ZstdDecompressionWriter_flush, METH_NOARGS, NULL},
  173. {"isatty", ZstdDecompressionWriter_false, METH_NOARGS, NULL},
  174. {"readable", ZstdDecompressionWriter_false, METH_NOARGS, NULL},
  175. {"readline", (PyCFunction)ZstdDecompressionWriter_unsupported,
  176. METH_VARARGS | METH_KEYWORDS, NULL},
  177. {"readlines", (PyCFunction)ZstdDecompressionWriter_unsupported,
  178. METH_VARARGS | METH_KEYWORDS, NULL},
  179. {"seek", (PyCFunction)ZstdDecompressionWriter_unsupported,
  180. METH_VARARGS | METH_KEYWORDS, NULL},
  181. {"seekable", ZstdDecompressionWriter_false, METH_NOARGS, NULL},
  182. {"tell", (PyCFunction)ZstdDecompressionWriter_unsupported,
  183. METH_VARARGS | METH_KEYWORDS, NULL},
  184. {"truncate", (PyCFunction)ZstdDecompressionWriter_unsupported,
  185. METH_VARARGS | METH_KEYWORDS, NULL},
  186. {"writable", ZstdDecompressionWriter_true, METH_NOARGS, NULL},
  187. {"writelines", (PyCFunction)ZstdDecompressionWriter_unsupported,
  188. METH_VARARGS | METH_KEYWORDS, NULL},
  189. {"read", (PyCFunction)ZstdDecompressionWriter_unsupported,
  190. METH_VARARGS | METH_KEYWORDS, NULL},
  191. {"readall", (PyCFunction)ZstdDecompressionWriter_unsupported,
  192. METH_VARARGS | METH_KEYWORDS, NULL},
  193. {"readinto", (PyCFunction)ZstdDecompressionWriter_unsupported,
  194. METH_VARARGS | METH_KEYWORDS, NULL},
  195. {"write", (PyCFunction)ZstdDecompressionWriter_write,
  196. METH_VARARGS | METH_KEYWORDS, PyDoc_STR("Compress data")},
  197. {NULL, NULL}};
  198. static PyMemberDef ZstdDecompressionWriter_members[] = {
  199. {"closed", T_BOOL, offsetof(ZstdDecompressionWriter, closed), READONLY,
  200. NULL},
  201. {NULL}};
  202. PyType_Slot ZstdDecompressionWriterSlots[] = {
  203. {Py_tp_dealloc, ZstdDecompressionWriter_dealloc},
  204. {Py_tp_iter, ZstdDecompressionWriter_iter},
  205. {Py_tp_iternext, ZstdDecompressionWriter_iternext},
  206. {Py_tp_methods, ZstdDecompressionWriter_methods},
  207. {Py_tp_members, ZstdDecompressionWriter_members},
  208. {Py_tp_new, PyType_GenericNew},
  209. {0, NULL},
  210. };
  211. PyType_Spec ZstdDecompressionWriterSpec = {
  212. "zstd.ZstdDecompressionWriter",
  213. sizeof(ZstdDecompressionWriter),
  214. 0,
  215. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
  216. ZstdDecompressionWriterSlots,
  217. };
  218. PyTypeObject *ZstdDecompressionWriterType;
  219. void decompressionwriter_module_init(PyObject *mod) {
  220. ZstdDecompressionWriterType =
  221. (PyTypeObject *)PyType_FromSpec(&ZstdDecompressionWriterSpec);
  222. if (PyType_Ready(ZstdDecompressionWriterType) < 0) {
  223. return;
  224. }
  225. Py_INCREF((PyObject *)ZstdDecompressionWriterType);
  226. PyModule_AddObject(mod, "ZstdDecompressionWriter",
  227. (PyObject *)ZstdDecompressionWriterType);
  228. }