-
Notifications
You must be signed in to change notification settings - Fork 0
/
plissken.c
141 lines (114 loc) · 3.42 KB
/
plissken.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <Python.h>
#include "lib/houdini.h"
typedef int (*houdini_cb)(gh_buf *, const uint8_t *, size_t);
/**
* html_secure instance variable
*/
static int g_html_secure = 1;
static PyObject *
plissken__generic_escape(houdini_cb do_escape,PyObject *args)
{
const char *str;
gh_buf buf = GH_BUF_INIT;
if (!PyArg_ParseTuple(args, "s", &str))
return NULL;
if (do_escape(&buf, (const uint8_t *)str, sizeof(str))) {
PyObject *result = Py_BuildValue("s",buf.ptr);
return result;
}
return Py_BuildValue("s",str);
}
static PyObject *
plissken_escape_html(PyObject *self, PyObject *args)
{
const char *str;
PyObject *py_secure;
gh_buf buf = GH_BUF_INIT;
int secure = g_html_secure;
if (PyArg_ParseTuple(args, "s|O", &str,&py_secure))
{
if(!PyObject_IsTrue(py_secure))
{
secure=0;
}
} else {
return NULL;
}
if (houdini_escape_html0(&buf, (const uint8_t *)str, sizeof(str),secure)) {
PyObject *result = Py_BuildValue("s",buf.ptr);
return result;
}
return Py_BuildValue("s",str);
}
static PyObject *
plissken_unescape_html(PyObject *self, PyObject *args)
{
return plissken__generic_escape(&houdini_unescape_html,args);
}
static PyObject *
plissken_escape_js(PyObject *self, PyObject *args)
{
return plissken__generic_escape(&houdini_escape_js,args);
}
static PyObject *
plissken_unescape_js(PyObject *self, PyObject *args)
{
return plissken__generic_escape(&houdini_unescape_js,args);
}
static PyObject *
plissken_escape_uri(PyObject *self, PyObject *args)
{
return plissken__generic_escape(&houdini_escape_uri,args);
}
static PyObject *
plissken_unescape_uri(PyObject *self, PyObject *args)
{
return plissken__generic_escape(&houdini_unescape_uri,args);
}
static PyObject *
plissken_escape_url(PyObject *self, PyObject *args)
{
return plissken__generic_escape(&houdini_escape_url,args);
}
static PyObject *
plissken_unescape_url(PyObject *self, PyObject *args)
{
return plissken__generic_escape(&houdini_unescape_url,args);
}
static PyObject *
plissken_escape_xml(PyObject *self, PyObject *args)
{
return plissken__generic_escape(&houdini_escape_xml,args);
}
static PyObject *
plissken_escape_href(PyObject *self, PyObject *args)
{
return plissken__generic_escape(&houdini_escape_href,args);
}
static PyMethodDef PlisskenMethods[] = {
{ "escape_html", plissken_escape_html, METH_VARARGS, "Escape an HTML string." },
{ "unescape_html", plissken_unescape_html, METH_VARARGS, "Unescape an HTML string." },
{ "escape_js", plissken_escape_js, METH_VARARGS, "Escape a JS string." },
{ "unescape_js", plissken_unescape_js, METH_VARARGS, "Unescape a JS string." },
{ "escape_uri", plissken_escape_uri, METH_VARARGS, "Escape a URI string." },
{ "unescape_uri", plissken_unescape_uri, METH_VARARGS, "Unescape a URI string." },
{ "escape_xml", plissken_escape_xml, METH_VARARGS, "Escape an XML string." },
{ "escape_href", plissken_escape_href, METH_VARARGS, "Escape an HREF string." },
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
initplissken(void)
{
(void) Py_InitModule("plissken", PlisskenMethods);
}
int
main(int argc, char *argv[])
{
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
/* Add a static module */
initplissken();
return 1;
}