constants.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 char frame_header[] = {
  11. '\x28',
  12. '\xb5',
  13. '\x2f',
  14. '\xfd',
  15. };
  16. void constants_module_init(PyObject* mod) {
  17. PyObject* version;
  18. PyObject* zstdVersion;
  19. PyObject* frameHeader;
  20. #if PY_MAJOR_VERSION >= 3
  21. version = PyUnicode_FromString(PYTHON_ZSTANDARD_VERSION);
  22. #else
  23. version = PyString_FromString(PYTHON_ZSTANDARD_VERSION);
  24. #endif
  25. PyModule_AddObject(mod, "__version__", version);
  26. ZstdError = PyErr_NewException("zstd.ZstdError", NULL, NULL);
  27. PyModule_AddObject(mod, "ZstdError", ZstdError);
  28. PyModule_AddIntConstant(mod, "FLUSH_BLOCK", 0);
  29. PyModule_AddIntConstant(mod, "FLUSH_FRAME", 1);
  30. PyModule_AddIntConstant(mod, "COMPRESSOBJ_FLUSH_FINISH", compressorobj_flush_finish);
  31. PyModule_AddIntConstant(mod, "COMPRESSOBJ_FLUSH_BLOCK", compressorobj_flush_block);
  32. /* For now, the version is a simple tuple instead of a dedicated type. */
  33. zstdVersion = PyTuple_New(3);
  34. PyTuple_SetItem(zstdVersion, 0, PyLong_FromLong(ZSTD_VERSION_MAJOR));
  35. PyTuple_SetItem(zstdVersion, 1, PyLong_FromLong(ZSTD_VERSION_MINOR));
  36. PyTuple_SetItem(zstdVersion, 2, PyLong_FromLong(ZSTD_VERSION_RELEASE));
  37. PyModule_AddObject(mod, "ZSTD_VERSION", zstdVersion);
  38. frameHeader = PyBytes_FromStringAndSize(frame_header, sizeof(frame_header));
  39. if (frameHeader) {
  40. PyModule_AddObject(mod, "FRAME_HEADER", frameHeader);
  41. }
  42. else {
  43. PyErr_Format(PyExc_ValueError, "could not create frame header object");
  44. }
  45. PyModule_AddObject(mod, "CONTENTSIZE_UNKNOWN",
  46. PyLong_FromUnsignedLongLong(ZSTD_CONTENTSIZE_UNKNOWN));
  47. PyModule_AddObject(mod, "CONTENTSIZE_ERROR",
  48. PyLong_FromUnsignedLongLong(ZSTD_CONTENTSIZE_ERROR));
  49. PyModule_AddIntConstant(mod, "MAX_COMPRESSION_LEVEL", ZSTD_maxCLevel());
  50. PyModule_AddIntConstant(mod, "COMPRESSION_RECOMMENDED_INPUT_SIZE",
  51. (long)ZSTD_CStreamInSize());
  52. PyModule_AddIntConstant(mod, "COMPRESSION_RECOMMENDED_OUTPUT_SIZE",
  53. (long)ZSTD_CStreamOutSize());
  54. PyModule_AddIntConstant(mod, "DECOMPRESSION_RECOMMENDED_INPUT_SIZE",
  55. (long)ZSTD_DStreamInSize());
  56. PyModule_AddIntConstant(mod, "DECOMPRESSION_RECOMMENDED_OUTPUT_SIZE",
  57. (long)ZSTD_DStreamOutSize());
  58. PyModule_AddIntConstant(mod, "MAGIC_NUMBER", ZSTD_MAGICNUMBER);
  59. PyModule_AddIntConstant(mod, "BLOCKSIZELOG_MAX", ZSTD_BLOCKSIZELOG_MAX);
  60. PyModule_AddIntConstant(mod, "BLOCKSIZE_MAX", ZSTD_BLOCKSIZE_MAX);
  61. PyModule_AddIntConstant(mod, "WINDOWLOG_MIN", ZSTD_WINDOWLOG_MIN);
  62. PyModule_AddIntConstant(mod, "WINDOWLOG_MAX", ZSTD_WINDOWLOG_MAX);
  63. PyModule_AddIntConstant(mod, "CHAINLOG_MIN", ZSTD_CHAINLOG_MIN);
  64. PyModule_AddIntConstant(mod, "CHAINLOG_MAX", ZSTD_CHAINLOG_MAX);
  65. PyModule_AddIntConstant(mod, "HASHLOG_MIN", ZSTD_HASHLOG_MIN);
  66. PyModule_AddIntConstant(mod, "HASHLOG_MAX", ZSTD_HASHLOG_MAX);
  67. PyModule_AddIntConstant(mod, "SEARCHLOG_MIN", ZSTD_SEARCHLOG_MIN);
  68. PyModule_AddIntConstant(mod, "SEARCHLOG_MAX", ZSTD_SEARCHLOG_MAX);
  69. PyModule_AddIntConstant(mod, "MINMATCH_MIN", ZSTD_MINMATCH_MIN);
  70. PyModule_AddIntConstant(mod, "MINMATCH_MAX", ZSTD_MINMATCH_MAX);
  71. /* TODO SEARCHLENGTH_* is deprecated. */
  72. PyModule_AddIntConstant(mod, "SEARCHLENGTH_MIN", ZSTD_MINMATCH_MIN);
  73. PyModule_AddIntConstant(mod, "SEARCHLENGTH_MAX", ZSTD_MINMATCH_MAX);
  74. PyModule_AddIntConstant(mod, "TARGETLENGTH_MIN", ZSTD_TARGETLENGTH_MIN);
  75. PyModule_AddIntConstant(mod, "TARGETLENGTH_MAX", ZSTD_TARGETLENGTH_MAX);
  76. PyModule_AddIntConstant(mod, "LDM_MINMATCH_MIN", ZSTD_LDM_MINMATCH_MIN);
  77. PyModule_AddIntConstant(mod, "LDM_MINMATCH_MAX", ZSTD_LDM_MINMATCH_MAX);
  78. PyModule_AddIntConstant(mod, "LDM_BUCKETSIZELOG_MAX", ZSTD_LDM_BUCKETSIZELOG_MAX);
  79. PyModule_AddIntConstant(mod, "STRATEGY_FAST", ZSTD_fast);
  80. PyModule_AddIntConstant(mod, "STRATEGY_DFAST", ZSTD_dfast);
  81. PyModule_AddIntConstant(mod, "STRATEGY_GREEDY", ZSTD_greedy);
  82. PyModule_AddIntConstant(mod, "STRATEGY_LAZY", ZSTD_lazy);
  83. PyModule_AddIntConstant(mod, "STRATEGY_LAZY2", ZSTD_lazy2);
  84. PyModule_AddIntConstant(mod, "STRATEGY_BTLAZY2", ZSTD_btlazy2);
  85. PyModule_AddIntConstant(mod, "STRATEGY_BTOPT", ZSTD_btopt);
  86. PyModule_AddIntConstant(mod, "STRATEGY_BTULTRA", ZSTD_btultra);
  87. PyModule_AddIntConstant(mod, "STRATEGY_BTULTRA2", ZSTD_btultra2);
  88. PyModule_AddIntConstant(mod, "DICT_TYPE_AUTO", ZSTD_dct_auto);
  89. PyModule_AddIntConstant(mod, "DICT_TYPE_RAWCONTENT", ZSTD_dct_rawContent);
  90. PyModule_AddIntConstant(mod, "DICT_TYPE_FULLDICT", ZSTD_dct_fullDict);
  91. PyModule_AddIntConstant(mod, "FORMAT_ZSTD1", ZSTD_f_zstd1);
  92. PyModule_AddIntConstant(mod, "FORMAT_ZSTD1_MAGICLESS", ZSTD_f_zstd1_magicless);
  93. }