-
Notifications
You must be signed in to change notification settings - Fork 2
/
codec.c
169 lines (131 loc) · 4.67 KB
/
codec.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "codec.h"
#include "Python.h"
#include "percentcoding.h"
/* Codec methods */
static PyObject *
Codec_new(PyTypeObject *type, PyObject *arg, PyObject *kwds)
{
Codec *self;
if (!(self = (Codec*)type->tp_alloc(type, 0)))
return NULL;
return (PyObject *)self;
}
static int
Codec_init(Codec *self, PyObject *args, PyObject *kwds)
{
char a = 'a';
const char* safeset = NULL;
int len = 0;
if (!PyArg_ParseTuple(args, "s#|c:init", &safeset, &len, &a))
return -1;
/* Initialize the byte -> 2 hex char lookup table.
By default, everything is "unsafe" and gets percent encoded.
Anything in safeset is okay.
The percent character itself is never safe. */
unsigned int i;
for (i=0; i<256; i++)
btox((uint8_t)i, &self->chrtohex[(uint8_t)i*2], a);
const uint8_t* p;
for (i=0, p=(uint8_t*)safeset; i<len; i++, p++)
if (*p != '%')
self->chrtohex[*p*2] = self->chrtohex[*p*2+1] = 0;
return 0;
}
static PyObject *
Codec_encode(Codec *self, PyObject *args)
{
char* in = NULL;
int inlen = 0;
PyObject *result = NULL;
/* Make sure we can handle unicode string inputs.
The encoded result will always be a plain ascii string.
FIXME not true -- eg what if safe set has 0xff */
if (!PyArg_ParseTuple(args, "et#:encode", "utf8", &in, &inlen))
goto done;
char* out = NULL;
Py_ssize_t size;
/* First pass: calculate size of encoded string.
Create a new string object of exactly that size. */
size = percent_encode(in, inlen, NULL, self->chrtohex);
if (!(result = PyString_FromStringAndSize(NULL,size)))
goto done;
/* Second pass: actually encode this time. */
out = PyString_AsString(result);
size = percent_encode(in, inlen, out, self->chrtohex);
done:
if (in) PyMem_Free(in);
return result;
}
static PyObject *
Codec_decode(Codec *self, PyObject *args)
{
const char* in = NULL;
int inlen = 0;
if (!PyArg_ParseTuple(args, "s#:decode", &in, &inlen))
return NULL;
char* out = NULL;
PyObject *result = NULL;
Py_ssize_t size;
/* First pass: calculate size of decoded string.
Create a new string object of exactly that size. */
size = percent_decode(in, inlen, NULL);
if (!(result = PyString_FromStringAndSize(NULL,size)))
return NULL;
/* Second pass: actually decode this time. */
out = PyString_AsString(result);
size = percent_decode(in, inlen, out);
return result;
}
static PyMethodDef Codec_methods[] = {
{"encode", (PyCFunction)Codec_encode, METH_VARARGS,
PyDoc_STR("encode(str) -> str")},
{"decode", (PyCFunction)Codec_decode, METH_VARARGS,
PyDoc_STR("decode(str) -> str")},
{NULL, NULL} /* sentinel */
};
PyTypeObject CodecType = {
/* The ob_type field must be initialized in the module init function
* to be portable to Windows without using C++. */
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"percentcoding.cext.Codec", /*tp_name*/
sizeof(Codec), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro */
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
Codec_methods, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
(initproc)Codec_init, /*tp_init*/
0, /*tp_alloc*/
Codec_new, /*tp_new*/
0, /*tp_free*/
0, /*tp_is_gc*/
};