forked from hqwrong/py-sproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpysproto.c
361 lines (312 loc) · 9.21 KB
/
pysproto.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <stdarg.h>
#include <stdio.h>
#include <Python.h>
#include "clib/sproto.h"
#define ENCODE_BUFFERSIZE 2050
static PyObject *SprotoError;
/* ------------------------------ buf api ------------------ */
struct spbuf {
void *buf;
size_t sz;
};
struct spbuf*
newbuf() {
struct spbuf *spbuf = PyMem_Malloc(sizeof(spbuf));
spbuf->buf = NULL;
spbuf->sz = 0;
return spbuf;
}
void
expandbuf(struct spbuf *spbuf, size_t n) {
if (n > spbuf->sz) {
spbuf->buf = PyMem_Malloc(n);
spbuf->sz = n;
}
}
void
delbuf(struct spbuf *spbuf) {
if (spbuf->buf)
PyMem_Free(spbuf->buf);
PyMem_Free(spbuf);
}
/* ----------------------------------------- end buf api ----------------- */
int
_error(const char* tagname, int index, const char* msg) {
PyErr_SetObject(SprotoError, PyString_FromFormat("%s @ tag[%s] index[%d]", msg, tagname, index));
return -1;
}
int
_strerr(const char* msg) {
PyErr_SetString(SprotoError, msg);
return -1;
}
void
_free_sp(PyObject *spcap) {
struct spbuf *spbuf = PyCapsule_GetContext(spcap);
struct sproto *sp = PyCapsule_GetPointer(spcap, "pysproto");
delbuf(spbuf);
sproto_release(sp);
}
static int
_encode(const struct sproto_arg *args) {
const char *tagname = args->tagname;
PyObject *dict = args->ud;
int type = args->type;
int index = args->index;
int length = args->length;
struct sproto_type *st = args->subtype;
void *value = args->value;
PyObject *pyval = NULL;
if (!PyDict_Check(dict))
return _error(tagname, index, "need dict");
pyval = PyDict_GetItemString(dict, tagname);
if (pyval && index) { /* is array? */
if (!PyList_Check(pyval))
return _error(tagname, index, "need list");
if (PyList_Size(pyval) < index)
pyval = NULL;
else
pyval = PyList_GetItem(pyval, index-1);
}
if (!pyval)
return 0;
switch (type) {
case SPROTO_TINTEGER: {
if (!PyInt_Check(pyval))
return _error(tagname, index, "need integer");
long v = PyInt_AsLong(pyval);
long vh = v >> 31;
if (vh == 0 || vh == -1) {
*(uint32_t *)value = (uint32_t)v;
return 4;
}
else {
*(uint64_t *)value = (uint64_t)v;
return 8;
}
}
case SPROTO_TBOOLEAN: {
if (!PyBool_Check(pyval))
return _error(tagname, index, "need boolean");
*(int *)value = (pyval == Py_True);
return 4;
}
case SPROTO_TSTRING: {
if (!PyString_Check(pyval))
return _error(tagname, index, "need string");
char* str;
size_t sz = 0;
PyString_AsStringAndSize(pyval, &str, &sz);
if (sz > length)
return -1;
memcpy(value, str, sz);
return sz + 1; // The length of empty string is 1.
}
case SPROTO_TSTRUCT: {
return sproto_encode(st, value, length, _encode, pyval);
}
default:
return _error(tagname, index, "unexpected type");
}
}
static int
_decode(const struct sproto_arg *args) {
const char *tagname = args->tagname;
PyObject *table = args->ud;
int type = args->type;
int index = args->index;
int length = args->length;
struct sproto_type *st = args->subtype;
void *value = args->value;
PyObject *list = PyDict_GetItemString(table, tagname);
PyObject *pyval;
if (index > 0 && !list) {
list = PyList_New(0);
PyDict_SetItemString(table, tagname, list);
}
switch (type) {
case SPROTO_TINTEGER:
pyval = PyInt_FromLong(*(long*)value); break;
case SPROTO_TBOOLEAN:
pyval = (*(long*)value) == 1 ? Py_True : Py_False; break;
case SPROTO_TSTRING:
pyval = PyString_FromStringAndSize(value, length); break;
case SPROTO_TSTRUCT: {
pyval = PyDict_New();
int r = sproto_decode(st, value, length, _decode, pyval);
if (r < 0)
return _error(tagname, index, "decode failed");
}
}
if (index)
PyList_Append(list, pyval);
else
PyDict_SetItemString(table, tagname, pyval);
Py_XDECREF(list);
Py_XDECREF(pyval);
return 0;
}
static PyObject*
py_new(PyObject *self, PyObject *args) {
struct sproto * sp;
const char *buf;
int sz = 0;
PyArg_ParseTuple(args, "s#", &buf, &sz);
sp = sproto_create(buf, sz);
if (!sp)
return NULL;
PyObject *spcap = PyCapsule_New(sp, "pysproto", _free_sp);
PyCapsule_SetContext(spcap, newbuf());
return spcap;
}
static PyObject*
py_encode(PyObject *self, PyObject *args) {
PyObject *stcap, *spcap, *dict;
struct sproto_type *st;
struct spbuf *spbuf;
PyArg_ParseTuple(args, "OOO", &spcap, &stcap, &dict);
st = PyCapsule_GetPointer(stcap, "sproto_type");
spbuf = PyCapsule_GetContext(spcap);
for (;;) {
int r = sproto_encode(st, spbuf->buf, spbuf->sz, _encode, dict);
if (PyErr_Occurred()) /* error occurred? */
return NULL;
if (r<0)
expandbuf(spbuf, spbuf->sz ? spbuf->sz*2 : ENCODE_BUFFERSIZE);
else
return Py_BuildValue("s#", spbuf->buf, r);
}
}
static PyObject*
py_decode(PyObject *self, PyObject *args) {
PyObject *stcap, *spcap, *dict;
struct sproto_type *st;
const char* buffer;
int sz;
int r;
PyArg_ParseTuple(args, "OOs#", &spcap, &stcap, &buffer, &sz);
st = PyCapsule_GetPointer(stcap, "sproto_type");
dict = PyDict_New();
r = sproto_decode(st, buffer, (int)sz, _decode, dict);
if (r < 0) {
_error(".toplevel", 0, "decode failed");
return NULL;
}
if (PyErr_Occurred())
return NULL;
res = Py_BuildValue("(Oi)", dict, r);
Py_XDECREF(dict);
return res;
}
static PyObject*
py_query(PyObject *self, PyObject *args) {
char * typename;
PyObject *spcap;
struct sproto *sp;
struct sproto_type *st;
PyArg_ParseTuple(args, "Os", &spcap, &typename);
sp = PyCapsule_GetPointer(spcap, "pysproto");
st = sproto_type(sp, typename);
if (!st) {
_strerr(typename);
return NULL;
}
return PyCapsule_New(st, "sproto_type", NULL);
}
static PyObject*
py_protocal(PyObject *self, PyObject *args) {
struct sproto *sp;
const char *protoname;
PyObject *spcap, *handle;
int tag;
struct sproto_type *request, *response;
PyObject *req = Py_None;
PyObject *resp = Py_None;
PyArg_ParseTuple(args, "OO", &spcap, &handle);
sp = PyCapsule_GetPointer(spcap, "pysproto");
if (PyString_Check(handle)) {
protoname = PyString_AsString(handle);
tag = sproto_prototag(sp, protoname);
if (tag < 0) {
_strerr(protoname);
return NULL;
}
} else if (PyInt_Check(handle)) {
tag = PyInt_AsLong(handle);
protoname = sproto_protoname(sp, tag);
if (protoname == NULL) {
_strerr("invalid proto tag");
return NULL;
}
}
else {
_strerr("unexpected protoname");
return NULL;
}
request = sproto_protoquery(sp, tag, SPROTO_REQUEST);
response = sproto_protoquery(sp, tag, SPROTO_RESPONSE);
if (request)
req = PyCapsule_New(request, "sproto_type", NULL);
if (response)
resp = PyCapsule_New(response, "sproto_type", NULL);
if (PyString_Check(handle))
return Py_BuildValue("(OOi)", req, resp, tag);
else
return Py_BuildValue("(OOs)", req, resp, protoname);
}
static PyObject*
py_pack(PyObject *self, PyObject *args) {
PyObject * spcap;
const void* buffer;
struct spbuf *spbuf;
int sz = 0;
PyArg_ParseTuple(args, "Os#", &spcap, &buffer, &sz);
spbuf = PyCapsule_GetContext(spcap);
// the worst-case space overhead of packing is 2 bytes per 2 KiB of input (256 words = 2KiB).
size_t maxsz = (sz + 2047) / 2048 * 2 + sz;
if (spbuf->sz < maxsz)
expandbuf(spbuf, maxsz);
int bytes = sproto_pack(buffer, sz, spbuf->buf, maxsz);
if (bytes > maxsz)
return NULL;
return Py_BuildValue("s#", spbuf->buf, bytes);
}
static PyObject*
py_unpack(PyObject *self, PyObject *args) {
const void* buffer;
PyObject *spcap;
struct spbuf *spbuf;
int sz = 0;
PyArg_ParseTuple(args, "Os#", &spcap, &buffer, &sz);
spbuf = PyCapsule_GetContext(spcap);
int r = sproto_unpack(buffer, sz, spbuf->buf, spbuf->sz);
if (r <0) {
_strerr("invalid unpack stream");
return NULL;
}
if (r > spbuf->sz)
expandbuf(spbuf, r);
r = sproto_unpack(buffer, sz, spbuf->buf, spbuf->sz);
if (r<0) {
_strerr("invalid unpack stream");
return NULL;
}
return Py_BuildValue("s#", spbuf->buf, r);
}
static PyMethodDef SprotoMethods[] = {
{"new", py_new, METH_VARARGS, NULL},
{"querytype", py_query, METH_VARARGS, NULL},
{"protocal", py_protocal, METH_VARARGS, NULL},
{"encode", py_encode, METH_VARARGS, NULL},
{"decode", py_decode, METH_VARARGS, NULL},
{"pack", py_pack, METH_VARARGS, NULL},
{"unpack", py_unpack, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL},
};
PyMODINIT_FUNC
initpysproto(void) {
PyObject *m = Py_InitModule("pysproto", SprotoMethods);
SprotoError = PyErr_NewException("sproto.error", NULL, NULL);
Py_INCREF(SprotoError);
PyModule_AddObject(m, "error", SprotoError);
}