aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/zstandard/py3/c-ext/decompressobj.c
blob: a22257bbb9e7eefff161afc78729535a410b0659 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
 * Copyright (c) 2016-present, Gregory Szorc
 * All rights reserved.
 *
 * This software may be modified and distributed under the terms
 * of the BSD license. See the LICENSE file for details.
 */

#include "python-zstandard.h"

extern PyObject *ZstdError;

static void DecompressionObj_dealloc(ZstdDecompressionObj *self) {
    Py_XDECREF(self->decompressor);
    Py_CLEAR(self->unused_data);

    PyObject_Del(self);
}

static PyObject *DecompressionObj_decompress(ZstdDecompressionObj *self,
                                             PyObject *args, PyObject *kwargs) {
    static char *kwlist[] = {"data", NULL};

    Py_buffer source;
    size_t zresult;
    ZSTD_inBuffer input;
    ZSTD_outBuffer output;
    PyObject *result = NULL;
    Py_ssize_t resultSize = 0;

    output.dst = NULL;

    if (self->finished) {
        PyErr_SetString(ZstdError, "cannot use a decompressobj multiple times");
        return NULL;
    }

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:decompress", kwlist,
                                     &source)) {
        return NULL;
    }

    /* Special case of empty input. Output will always be empty. */
    if (source.len == 0) {
        result = PyBytes_FromString("");
        goto finally;
    }

    input.src = source.buf;
    input.size = source.len;
    input.pos = 0;

    output.dst = PyMem_Malloc(self->outSize);
    if (!output.dst) {
        PyErr_NoMemory();
        goto except;
    }
    output.size = self->outSize;
    output.pos = 0;

    while (1) {
        Py_BEGIN_ALLOW_THREADS zresult =
            ZSTD_decompressStream(self->decompressor->dctx, &output, &input);
        Py_END_ALLOW_THREADS

        if (ZSTD_isError(zresult)) {
            PyErr_Format(ZstdError, "zstd decompressor error: %s",
                         ZSTD_getErrorName(zresult));
            goto except;
        }

        if (output.pos) {
            if (result) {
                resultSize = PyBytes_GET_SIZE(result);
                if (-1 ==
                    safe_pybytes_resize(&result, resultSize + output.pos)) {
                    Py_XDECREF(result);
                    goto except;
                }

                memcpy(PyBytes_AS_STRING(result) + resultSize, output.dst,
                       output.pos);
            }
            else {
                result = PyBytes_FromStringAndSize(output.dst, output.pos);
                if (!result) {
                    goto except;
                }
            }
        }

        if (0 == zresult && !self->readAcrossFrames) {
            self->finished = 1;

            /* We should only get here at most once. */
            assert(!self->unused_data);
            self->unused_data = PyBytes_FromStringAndSize((char *)(input.src) + input.pos, input.size - input.pos);

            break;
        }
        else if (0 == zresult && self->readAcrossFrames) {
            if (input.pos == input.size) {
                break;
            } else {
                output.pos = 0;
            }
        }
        else if (input.pos == input.size && output.pos == 0) {
            break;
        }
        else {
            output.pos = 0;
        }
    }

    if (!result) {
        result = PyBytes_FromString("");
    }

    goto finally;

except:
    Py_CLEAR(result);

finally:
    PyMem_Free(output.dst);
    PyBuffer_Release(&source);

    return result;
}

static PyObject *DecompressionObj_flush(ZstdDecompressionObj *self,
                                        PyObject *args, PyObject *kwargs) {
    static char *kwlist[] = {"length", NULL};

    PyObject *length = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:flush", kwlist,
                                     &length)) {
        return NULL;
    }

    return PyBytes_FromString("");
}

static PyObject *DecompressionObj_unused_data(PyObject *self, void *unused) {
    ZstdDecompressionObj *slf = (ZstdDecompressionObj *)(self);

    if (slf->unused_data) {
        Py_INCREF(slf->unused_data);
        return slf->unused_data;
    } else {
        return PyBytes_FromString("");
    }
}

static PyObject *DecompressionObj_unconsumed_tail(PyObject *self, void *unused) {
    return PyBytes_FromString("");
}

static PyObject *DecompressionObj_eof(PyObject *self, void *unused) {
    ZstdDecompressionObj *slf = (ZstdDecompressionObj*)(self);

    if (slf->finished) {
        Py_RETURN_TRUE;
    } else {
        Py_RETURN_FALSE;
    }
}

static PyMethodDef DecompressionObj_methods[] = {
    {"decompress", (PyCFunction)DecompressionObj_decompress,
     METH_VARARGS | METH_KEYWORDS, PyDoc_STR("decompress data")},
    {"flush", (PyCFunction)DecompressionObj_flush, METH_VARARGS | METH_KEYWORDS,
     PyDoc_STR("no-op")},
    {NULL, NULL}};

static PyGetSetDef DecompressionObj_getset[] = {
    {"unused_data", DecompressionObj_unused_data, NULL, NULL, NULL },
    {"unconsumed_tail", DecompressionObj_unconsumed_tail, NULL, NULL, NULL },
    {"eof", DecompressionObj_eof, NULL, NULL, NULL },
    {NULL}};

PyType_Slot ZstdDecompressionObjSlots[] = {
    {Py_tp_dealloc, DecompressionObj_dealloc},
    {Py_tp_methods, DecompressionObj_methods},
    {Py_tp_getset, DecompressionObj_getset},
    {Py_tp_new, PyType_GenericNew},
    {0, NULL},
};

PyType_Spec ZstdDecompressionObjSpec = {
    "zstd.ZstdDecompressionObj",
    sizeof(ZstdDecompressionObj),
    0,
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    ZstdDecompressionObjSlots,
};

PyTypeObject *ZstdDecompressionObjType;

void decompressobj_module_init(PyObject *module) {
    ZstdDecompressionObjType =
        (PyTypeObject *)PyType_FromSpec(&ZstdDecompressionObjSpec);
    if (PyType_Ready(ZstdDecompressionObjType) < 0) {
        return;
    }
}