| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef Py_BUILD_CORE_BUILTIN |
| 12 |
# define Py_BUILD_CORE_MODULE 1 |
| 13 |
#endif |
| 14 |
|
| 15 |
#include "Python.h" |
| 16 |
|
| 17 |
#include "backports_zstd_edits.h" |
| 18 |
|
| 19 |
#include "_zstdmodule.h" |
| 20 |
#include "clinic/zstddict.c.h" |
| 21 |
|
| 22 |
#include <zstd.h> |
| 23 |
|
| 24 |
#define ZstdDict_CAST(op) ((ZstdDict *)op) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static PyObject * |
| 45 |
_zstd_ZstdDict_new_impl(PyTypeObject *type, Py_buffer *dict_content, |
| 46 |
int is_raw) |
| 47 |
|
| 48 |
{ |
| 49 |
|
| 50 |
if (dict_content->len < 8) { |
| 51 |
PyErr_SetString(PyExc_ValueError, |
| 52 |
"Zstandard dictionary content too short " |
| 53 |
"(must have at least eight bytes)"); |
| 54 |
return NULL; |
| 55 |
} |
| 56 |
|
| 57 |
ZstdDict* self = PyObject_GC_New(ZstdDict, type); |
| 58 |
if (self == NULL) { |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
|
| 62 |
self->d_dict = NULL; |
| 63 |
self->dict_buffer = NULL; |
| 64 |
self->dict_id = 0; |
| 65 |
self->lock = BACKPORTSZSTD_LOCK_allocate(); |
| 66 |
if(BACKPORTSZSTD_LOCK_isError(self->lock)) { |
| 67 |
_zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self)); |
| 68 |
if (mod_state != NULL) { |
| 69 |
PyErr_SetString(mod_state->ZstdError, |
| 70 |
"Unable to allocate lock"); |
| 71 |
} |
| 72 |
goto error; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
self->c_dicts = PyDict_New(); |
| 77 |
if (self->c_dicts == NULL) { |
| 78 |
goto error; |
| 79 |
} |
| 80 |
|
| 81 |
self->dict_buffer = PyMem_Malloc(dict_content->len); |
| 82 |
if (!self->dict_buffer) { |
| 83 |
PyErr_NoMemory(); |
| 84 |
goto error; |
| 85 |
} |
| 86 |
memcpy(self->dict_buffer, dict_content->buf, dict_content->len); |
| 87 |
self->dict_len = dict_content->len; |
| 88 |
|
| 89 |
|
| 90 |
self->dict_id = ZSTD_getDictID_fromDict(self->dict_buffer, self->dict_len); |
| 91 |
|
| 92 |
|
| 93 |
if (!is_raw && self->dict_id == 0) { |
| 94 |
PyErr_SetString(PyExc_ValueError, "invalid Zstandard dictionary"); |
| 95 |
goto error; |
| 96 |
} |
| 97 |
|
| 98 |
PyObject_GC_Track(self); |
| 99 |
|
| 100 |
return (PyObject *)self; |
| 101 |
|
| 102 |
error: |
| 103 |
Py_XDECREF(self); |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
|
| 107 |
static void |
| 108 |
ZstdDict_dealloc(PyObject *ob) |
| 109 |
{ |
| 110 |
ZstdDict *self = ZstdDict_CAST(ob); |
| 111 |
|
| 112 |
PyObject_GC_UnTrack(self); |
| 113 |
|
| 114 |
|
| 115 |
if (self->d_dict) { |
| 116 |
ZSTD_freeDDict(self->d_dict); |
| 117 |
} |
| 118 |
|
| 119 |
assert(!BACKPORTSZSTD_LOCK_isLocked(&self->lock)); |
| 120 |
BACKPORTSZSTD_LOCK_free(self->lock); |
| 121 |
|
| 122 |
|
| 123 |
PyMem_Free(self->dict_buffer); |
| 124 |
Py_CLEAR(self->c_dicts); |
| 125 |
|
| 126 |
PyTypeObject *tp = Py_TYPE(self); |
| 127 |
tp->tp_free(self); |
| 128 |
Py_DECREF(tp); |
| 129 |
} |
| 130 |
|
| 131 |
PyDoc_STRVAR(ZstdDict_dictid_doc, |
| 132 |
"The Zstandard dictionary, an int between 0 and 2**32.\n\n" |
| 133 |
"A non-zero value represents an ordinary Zstandard dictionary,\n" |
| 134 |
"conforming to the standardised format.\n\n" |
| 135 |
"A value of zero indicates a 'raw content' dictionary,\n" |
| 136 |
"without any restrictions on format or content."); |
| 137 |
|
| 138 |
static PyObject * |
| 139 |
ZstdDict_repr(PyObject *ob) |
| 140 |
{ |
| 141 |
ZstdDict *dict = ZstdDict_CAST(ob); |
| 142 |
return PyUnicode_FromFormat("<ZstdDict dict_id=%u dict_size=%zd>", |
| 143 |
(unsigned int)dict->dict_id, dict->dict_len); |
| 144 |
} |
| 145 |
|
| 146 |
static PyMemberDef ZstdDict_members[] = { |
| 147 |
{"dict_id", Py_T_UINT, offsetof(ZstdDict, dict_id), Py_READONLY, ZstdDict_dictid_doc}, |
| 148 |
{NULL} |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
static PyObject * |
| 159 |
_zstd_ZstdDict_dict_content_get_impl(ZstdDict *self) |
| 160 |
|
| 161 |
{ |
| 162 |
return PyBytes_FromStringAndSize(self->dict_buffer, self->dict_len); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
static PyObject * |
| 183 |
_zstd_ZstdDict_as_digested_dict_get_impl(ZstdDict *self) |
| 184 |
|
| 185 |
{ |
| 186 |
return Py_BuildValue("Oi", self, DICT_TYPE_DIGESTED); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
static PyObject * |
| 206 |
_zstd_ZstdDict_as_undigested_dict_get_impl(ZstdDict *self) |
| 207 |
|
| 208 |
{ |
| 209 |
return Py_BuildValue("Oi", self, DICT_TYPE_UNDIGESTED); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
static PyObject * |
| 229 |
_zstd_ZstdDict_as_prefix_get_impl(ZstdDict *self) |
| 230 |
|
| 231 |
{ |
| 232 |
return Py_BuildValue("Oi", self, DICT_TYPE_PREFIX); |
| 233 |
} |
| 234 |
|
| 235 |
static PyGetSetDef ZstdDict_getset[] = { |
| 236 |
_ZSTD_ZSTDDICT_DICT_CONTENT_GETSETDEF |
| 237 |
_ZSTD_ZSTDDICT_AS_DIGESTED_DICT_GETSETDEF |
| 238 |
_ZSTD_ZSTDDICT_AS_UNDIGESTED_DICT_GETSETDEF |
| 239 |
_ZSTD_ZSTDDICT_AS_PREFIX_GETSETDEF |
| 240 |
{NULL} |
| 241 |
}; |
| 242 |
|
| 243 |
static Py_ssize_t |
| 244 |
ZstdDict_length(PyObject *ob) |
| 245 |
{ |
| 246 |
ZstdDict *self = ZstdDict_CAST(ob); |
| 247 |
return self->dict_len; |
| 248 |
} |
| 249 |
|
| 250 |
static int |
| 251 |
ZstdDict_traverse(PyObject *ob, visitproc visit, void *arg) |
| 252 |
{ |
| 253 |
ZstdDict *self = ZstdDict_CAST(ob); |
| 254 |
Py_VISIT(self->c_dicts); |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
static int |
| 259 |
ZstdDict_clear(PyObject *ob) |
| 260 |
{ |
| 261 |
ZstdDict *self = ZstdDict_CAST(ob); |
| 262 |
Py_CLEAR(self->c_dicts); |
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
static PyType_Slot zstddict_slots[] = { |
| 267 |
{Py_tp_members, ZstdDict_members}, |
| 268 |
{Py_tp_getset, ZstdDict_getset}, |
| 269 |
{Py_tp_new, _zstd_ZstdDict_new}, |
| 270 |
{Py_tp_dealloc, ZstdDict_dealloc}, |
| 271 |
{Py_tp_repr, ZstdDict_repr}, |
| 272 |
{Py_tp_doc, (void *)_zstd_ZstdDict_new__doc__}, |
| 273 |
{Py_sq_length, ZstdDict_length}, |
| 274 |
{Py_tp_traverse, ZstdDict_traverse}, |
| 275 |
{Py_tp_clear, ZstdDict_clear}, |
| 276 |
{0, 0} |
| 277 |
}; |
| 278 |
|
| 279 |
PyType_Spec zstd_dict_type_spec = { |
| 280 |
.name = "backports.zstd.ZstdDict", |
| 281 |
.basicsize = sizeof(ZstdDict), |
| 282 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
| 283 |
| Py_TPFLAGS_HAVE_GC, |
| 284 |
.slots = zstddict_slots, |
| 285 |
}; |