frameparams.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Copyright (c) 2017-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. FrameParametersObject *get_frame_parameters(PyObject *self, PyObject *args,
  11. PyObject *kwargs) {
  12. static char *kwlist[] = {"data", NULL};
  13. Py_buffer source;
  14. ZSTD_frameHeader header;
  15. FrameParametersObject *result = NULL;
  16. size_t zresult;
  17. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*:get_frame_parameters",
  18. kwlist, &source)) {
  19. return NULL;
  20. }
  21. zresult = ZSTD_getFrameHeader(&header, source.buf, source.len);
  22. if (ZSTD_isError(zresult)) {
  23. PyErr_Format(ZstdError, "cannot get frame parameters: %s",
  24. ZSTD_getErrorName(zresult));
  25. goto finally;
  26. }
  27. if (zresult) {
  28. PyErr_Format(ZstdError,
  29. "not enough data for frame parameters; need %zu bytes",
  30. zresult);
  31. goto finally;
  32. }
  33. result = PyObject_New(FrameParametersObject, FrameParametersType);
  34. if (!result) {
  35. goto finally;
  36. }
  37. result->frameContentSize = header.frameContentSize;
  38. result->windowSize = header.windowSize;
  39. result->dictID = header.dictID;
  40. result->checksumFlag = header.checksumFlag ? 1 : 0;
  41. finally:
  42. PyBuffer_Release(&source);
  43. return result;
  44. }
  45. static void FrameParameters_dealloc(PyObject *self) {
  46. PyObject_Del(self);
  47. }
  48. static PyMemberDef FrameParameters_members[] = {
  49. {"content_size", T_ULONGLONG,
  50. offsetof(FrameParametersObject, frameContentSize), READONLY,
  51. "frame content size"},
  52. {"window_size", T_ULONGLONG, offsetof(FrameParametersObject, windowSize),
  53. READONLY, "window size"},
  54. {"dict_id", T_UINT, offsetof(FrameParametersObject, dictID), READONLY,
  55. "dictionary ID"},
  56. {"has_checksum", T_BOOL, offsetof(FrameParametersObject, checksumFlag),
  57. READONLY, "checksum flag"},
  58. {NULL}};
  59. PyType_Slot FrameParametersSlots[] = {
  60. {Py_tp_dealloc, FrameParameters_dealloc},
  61. {Py_tp_members, FrameParameters_members},
  62. {0, NULL},
  63. };
  64. PyType_Spec FrameParametersSpec = {
  65. "zstd.FrameParameters",
  66. sizeof(FrameParametersObject),
  67. 0,
  68. Py_TPFLAGS_DEFAULT,
  69. FrameParametersSlots,
  70. };
  71. PyTypeObject *FrameParametersType;
  72. void frameparams_module_init(PyObject *mod) {
  73. FrameParametersType = (PyTypeObject *)PyType_FromSpec(&FrameParametersSpec);
  74. if (PyType_Ready(FrameParametersType) < 0) {
  75. return;
  76. }
  77. Py_INCREF(FrameParametersType);
  78. PyModule_AddObject(mod, "FrameParameters", (PyObject *)FrameParametersType);
  79. }