11 extern PyObject* ZstdError; |
11 extern PyObject* ZstdError; |
12 |
12 |
13 PyDoc_STRVAR(FrameParameters__doc__, |
13 PyDoc_STRVAR(FrameParameters__doc__, |
14 "FrameParameters: information about a zstd frame"); |
14 "FrameParameters: information about a zstd frame"); |
15 |
15 |
16 FrameParametersObject* get_frame_parameters(PyObject* self, PyObject* args) { |
16 FrameParametersObject* get_frame_parameters(PyObject* self, PyObject* args, PyObject* kwargs) { |
17 const char* source; |
17 static char* kwlist[] = { |
18 Py_ssize_t sourceSize; |
18 "data", |
19 ZSTD_frameParams params; |
19 NULL |
|
20 }; |
|
21 |
|
22 Py_buffer source; |
|
23 ZSTD_frameHeader header; |
20 FrameParametersObject* result = NULL; |
24 FrameParametersObject* result = NULL; |
21 size_t zresult; |
25 size_t zresult; |
22 |
26 |
23 #if PY_MAJOR_VERSION >= 3 |
27 #if PY_MAJOR_VERSION >= 3 |
24 if (!PyArg_ParseTuple(args, "y#:get_frame_parameters", |
28 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:get_frame_parameters", |
25 #else |
29 #else |
26 if (!PyArg_ParseTuple(args, "s#:get_frame_parameters", |
30 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s*:get_frame_parameters", |
27 #endif |
31 #endif |
28 &source, &sourceSize)) { |
32 kwlist, &source)) { |
29 return NULL; |
33 return NULL; |
30 } |
34 } |
31 |
35 |
32 /* Needed for Python 2 to reject unicode */ |
36 if (!PyBuffer_IsContiguous(&source, 'C') || source.ndim > 1) { |
33 if (!PyBytes_Check(PyTuple_GET_ITEM(args, 0))) { |
37 PyErr_SetString(PyExc_ValueError, |
34 PyErr_SetString(PyExc_TypeError, "argument must be bytes"); |
38 "data buffer should be contiguous and have at most one dimension"); |
35 return NULL; |
39 goto finally; |
36 } |
40 } |
37 |
41 |
38 zresult = ZSTD_getFrameParams(¶ms, (void*)source, sourceSize); |
42 zresult = ZSTD_getFrameHeader(&header, source.buf, source.len); |
39 |
43 |
40 if (ZSTD_isError(zresult)) { |
44 if (ZSTD_isError(zresult)) { |
41 PyErr_Format(ZstdError, "cannot get frame parameters: %s", ZSTD_getErrorName(zresult)); |
45 PyErr_Format(ZstdError, "cannot get frame parameters: %s", ZSTD_getErrorName(zresult)); |
42 return NULL; |
46 goto finally; |
43 } |
47 } |
44 |
48 |
45 if (zresult) { |
49 if (zresult) { |
46 PyErr_Format(ZstdError, "not enough data for frame parameters; need %zu bytes", zresult); |
50 PyErr_Format(ZstdError, "not enough data for frame parameters; need %zu bytes", zresult); |
47 return NULL; |
51 goto finally; |
48 } |
52 } |
49 |
53 |
50 result = PyObject_New(FrameParametersObject, &FrameParametersType); |
54 result = PyObject_New(FrameParametersObject, &FrameParametersType); |
51 if (!result) { |
55 if (!result) { |
52 return NULL; |
56 goto finally; |
53 } |
57 } |
54 |
58 |
55 result->frameContentSize = params.frameContentSize; |
59 result->frameContentSize = header.frameContentSize; |
56 result->windowSize = params.windowSize; |
60 result->windowSize = header.windowSize; |
57 result->dictID = params.dictID; |
61 result->dictID = header.dictID; |
58 result->checksumFlag = params.checksumFlag ? 1 : 0; |
62 result->checksumFlag = header.checksumFlag ? 1 : 0; |
59 |
63 |
|
64 finally: |
|
65 PyBuffer_Release(&source); |
60 return result; |
66 return result; |
61 } |
67 } |
62 |
68 |
63 static void FrameParameters_dealloc(PyObject* self) { |
69 static void FrameParameters_dealloc(PyObject* self) { |
64 PyObject_Del(self); |
70 PyObject_Del(self); |
66 |
72 |
67 static PyMemberDef FrameParameters_members[] = { |
73 static PyMemberDef FrameParameters_members[] = { |
68 { "content_size", T_ULONGLONG, |
74 { "content_size", T_ULONGLONG, |
69 offsetof(FrameParametersObject, frameContentSize), READONLY, |
75 offsetof(FrameParametersObject, frameContentSize), READONLY, |
70 "frame content size" }, |
76 "frame content size" }, |
71 { "window_size", T_UINT, |
77 { "window_size", T_ULONGLONG, |
72 offsetof(FrameParametersObject, windowSize), READONLY, |
78 offsetof(FrameParametersObject, windowSize), READONLY, |
73 "window size" }, |
79 "window size" }, |
74 { "dict_id", T_UINT, |
80 { "dict_id", T_UINT, |
75 offsetof(FrameParametersObject, dictID), READONLY, |
81 offsetof(FrameParametersObject, dictID), READONLY, |
76 "dictionary ID" }, |
82 "dictionary ID" }, |