-
Notifications
You must be signed in to change notification settings - Fork 0
/
pynlzss.c
124 lines (102 loc) · 3.92 KB
/
pynlzss.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*----------------------------------------------------------------------------*/
/*-- LZSS coding for Nintendo GBA/DS --*/
/*-- Copyright (C) 2011 CUE --*/
/*-- Copyright (C) 2017 Dorkmaster Flek --*/
/*-- --*/
/*-- This program is free software: you can redistribute it and/or modify --*/
/*-- it under the terms of the GNU General Public License as published by --*/
/*-- the Free Software Foundation, either version 3 of the License, or --*/
/*-- (at your option) any later version. --*/
/*-- --*/
/*-- This program is distributed in the hope that it will be useful, --*/
/*-- but WITHOUT ANY WARRANTY; without even the implied warranty of --*/
/*-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --*/
/*-- GNU General Public License for more details. --*/
/*-- --*/
/*-- You should have received a copy of the GNU General Public License --*/
/*-- along with this program. If not, see <http://www.gnu.org/licenses/>. --*/
/*----------------------------------------------------------------------------*/
#include <Python.h>
#include "nlzss.h"
#ifndef uint8_t
typedef unsigned char uint8_t;
#endif
static PyObject *pynlzss_error;
/* -- private functions -- */
static PyObject *pynlzss_encode_decode_file(PyObject *m, PyObject *args,
PyObject *kw, int encode);
/* ----------------------------------------------------------------------------
* module methods
*/
static PyObject *pynlzss_encode_file(PyObject *m, PyObject *args, PyObject *kw)
{
return pynlzss_encode_decode_file(m, args, kw, 1);
}
static PyObject *pynlzss_decode_file(PyObject *m, PyObject *args, PyObject *kw)
{
return pynlzss_encode_decode_file(m, args, kw, 0);
}
static PyMethodDef pynlzss_methods[] = {
{ "encode_file", (PyCFunction)pynlzss_encode_file,
METH_VARARGS | METH_KEYWORDS,
"Encode an original file using LZSS into another one." },
{ "decode_file", (PyCFunction)pynlzss_decode_file,
METH_VARARGS | METH_KEYWORDS,
"Decode a LZSS compressed file into another original one." },
{ NULL, NULL, 0, NULL }
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef pynlzss = {
PyModuleDef_HEAD_INIT,
"nlzss", /* name of module */
"", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
pynlzss_methods
};
#endif
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_nlzss(void)
#else
PyMODINIT_FUNC initnlzss(void)
#endif
{
PyObject *m;
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&pynlzss);
#else
m = Py_InitModule("nlzss", pynlzss_methods);
#endif
if (m == NULL)
#if PY_MAJOR_VERSION >= 3
return m;
#else
return;
#endif
pynlzss_error = PyErr_NewException("nlzss.error", NULL, NULL);
Py_INCREF(pynlzss_error);
PyModule_AddObject(m, "error", pynlzss_error);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}
/* -----------------------------------------------------------------------------
* private functions
*/
static PyObject *pynlzss_encode_decode_file(PyObject *m, PyObject *args, PyObject *kw, int encode)
{
static char *pynlzss_kwlist[] = {"in_path", "out_path", NULL };
char *in_path;
char *out_path;
int stat;
if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", pynlzss_kwlist, &in_path, &out_path))
return NULL;
if (encode)
stat = LZS_Encode(in_path, out_path);
else
stat = LZS_Decode(in_path, out_path);
if (stat) {
PyErr_SetString(pynlzss_error, "Failed to process LZSS file");
return NULL;
}
Py_RETURN_NONE;
}