|
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 |
|
9 #include "python-zstandard.h" |
|
10 |
|
11 extern PyObject* ZstdError; |
|
12 |
|
13 static char frame_header[] = { |
|
14 '\x28', |
|
15 '\xb5', |
|
16 '\x2f', |
|
17 '\xfd', |
|
18 }; |
|
19 |
|
20 void constants_module_init(PyObject* mod) { |
|
21 PyObject* version; |
|
22 PyObject* zstdVersion; |
|
23 PyObject* frameHeader; |
|
24 |
|
25 #if PY_MAJOR_VERSION >= 3 |
|
26 version = PyUnicode_FromString(PYTHON_ZSTANDARD_VERSION); |
|
27 #else |
|
28 version = PyString_FromString(PYTHON_ZSTANDARD_VERSION); |
|
29 #endif |
|
30 Py_INCREF(version); |
|
31 PyModule_AddObject(mod, "__version__", version); |
|
32 |
|
33 ZstdError = PyErr_NewException("zstd.ZstdError", NULL, NULL); |
|
34 PyModule_AddObject(mod, "ZstdError", ZstdError); |
|
35 |
|
36 /* For now, the version is a simple tuple instead of a dedicated type. */ |
|
37 zstdVersion = PyTuple_New(3); |
|
38 PyTuple_SetItem(zstdVersion, 0, PyLong_FromLong(ZSTD_VERSION_MAJOR)); |
|
39 PyTuple_SetItem(zstdVersion, 1, PyLong_FromLong(ZSTD_VERSION_MINOR)); |
|
40 PyTuple_SetItem(zstdVersion, 2, PyLong_FromLong(ZSTD_VERSION_RELEASE)); |
|
41 Py_IncRef(zstdVersion); |
|
42 PyModule_AddObject(mod, "ZSTD_VERSION", zstdVersion); |
|
43 |
|
44 frameHeader = PyBytes_FromStringAndSize(frame_header, sizeof(frame_header)); |
|
45 if (frameHeader) { |
|
46 PyModule_AddObject(mod, "FRAME_HEADER", frameHeader); |
|
47 } |
|
48 else { |
|
49 PyErr_Format(PyExc_ValueError, "could not create frame header object"); |
|
50 } |
|
51 |
|
52 PyModule_AddIntConstant(mod, "MAX_COMPRESSION_LEVEL", ZSTD_maxCLevel()); |
|
53 PyModule_AddIntConstant(mod, "COMPRESSION_RECOMMENDED_INPUT_SIZE", |
|
54 (long)ZSTD_CStreamInSize()); |
|
55 PyModule_AddIntConstant(mod, "COMPRESSION_RECOMMENDED_OUTPUT_SIZE", |
|
56 (long)ZSTD_CStreamOutSize()); |
|
57 PyModule_AddIntConstant(mod, "DECOMPRESSION_RECOMMENDED_INPUT_SIZE", |
|
58 (long)ZSTD_DStreamInSize()); |
|
59 PyModule_AddIntConstant(mod, "DECOMPRESSION_RECOMMENDED_OUTPUT_SIZE", |
|
60 (long)ZSTD_DStreamOutSize()); |
|
61 |
|
62 PyModule_AddIntConstant(mod, "MAGIC_NUMBER", ZSTD_MAGICNUMBER); |
|
63 PyModule_AddIntConstant(mod, "WINDOWLOG_MIN", ZSTD_WINDOWLOG_MIN); |
|
64 PyModule_AddIntConstant(mod, "WINDOWLOG_MAX", ZSTD_WINDOWLOG_MAX); |
|
65 PyModule_AddIntConstant(mod, "CHAINLOG_MIN", ZSTD_CHAINLOG_MIN); |
|
66 PyModule_AddIntConstant(mod, "CHAINLOG_MAX", ZSTD_CHAINLOG_MAX); |
|
67 PyModule_AddIntConstant(mod, "HASHLOG_MIN", ZSTD_HASHLOG_MIN); |
|
68 PyModule_AddIntConstant(mod, "HASHLOG_MAX", ZSTD_HASHLOG_MAX); |
|
69 PyModule_AddIntConstant(mod, "HASHLOG3_MAX", ZSTD_HASHLOG3_MAX); |
|
70 PyModule_AddIntConstant(mod, "SEARCHLOG_MIN", ZSTD_SEARCHLOG_MIN); |
|
71 PyModule_AddIntConstant(mod, "SEARCHLOG_MAX", ZSTD_SEARCHLOG_MAX); |
|
72 PyModule_AddIntConstant(mod, "SEARCHLENGTH_MIN", ZSTD_SEARCHLENGTH_MIN); |
|
73 PyModule_AddIntConstant(mod, "SEARCHLENGTH_MAX", ZSTD_SEARCHLENGTH_MAX); |
|
74 PyModule_AddIntConstant(mod, "TARGETLENGTH_MIN", ZSTD_TARGETLENGTH_MIN); |
|
75 PyModule_AddIntConstant(mod, "TARGETLENGTH_MAX", ZSTD_TARGETLENGTH_MAX); |
|
76 |
|
77 PyModule_AddIntConstant(mod, "STRATEGY_FAST", ZSTD_fast); |
|
78 PyModule_AddIntConstant(mod, "STRATEGY_DFAST", ZSTD_dfast); |
|
79 PyModule_AddIntConstant(mod, "STRATEGY_GREEDY", ZSTD_greedy); |
|
80 PyModule_AddIntConstant(mod, "STRATEGY_LAZY", ZSTD_lazy); |
|
81 PyModule_AddIntConstant(mod, "STRATEGY_LAZY2", ZSTD_lazy2); |
|
82 PyModule_AddIntConstant(mod, "STRATEGY_BTLAZY2", ZSTD_btlazy2); |
|
83 PyModule_AddIntConstant(mod, "STRATEGY_BTOPT", ZSTD_btopt); |
|
84 } |