Commit | Line | Data |
---|---|---|
46e6ad89 | 1 | /* -*-c-*- |
46e6ad89 | 2 | * |
3 | * Reading and writing buffers of stuff | |
4 | * | |
5 | * (c) 2005 Straylight/Edgeware | |
6 | */ | |
7 | ||
b2687a0a | 8 | /*----- Licensing notice --------------------------------------------------* |
46e6ad89 | 9 | * |
10 | * This file is part of the Python interface to Catacomb. | |
11 | * | |
12 | * Catacomb/Python is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
b2687a0a | 16 | * |
46e6ad89 | 17 | * Catacomb/Python is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
b2687a0a | 21 | * |
46e6ad89 | 22 | * You should have received a copy of the GNU General Public License |
23 | * along with Catacomb/Python; if not, write to the Free Software Foundation, | |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | */ | |
26 | ||
27 | /*----- Header files ------------------------------------------------------*/ | |
28 | ||
29 | #include "catacomb-python.h" | |
30 | ||
31 | /*----- Data structures ---------------------------------------------------*/ | |
32 | ||
33 | typedef struct buf_pyobj { | |
34 | PyObject_HEAD | |
35 | buf b; | |
0463996f | 36 | PyObject *sub; |
46e6ad89 | 37 | } buf_pyobj; |
38 | ||
39 | static PyTypeObject *rbuf_pytype, *wbuf_pytype; | |
40 | #define RBUF_PYCHECK(o) PyObject_TypeCheck((o), rbuf_pytype) | |
41 | #define WBUF_PYCHECK(o) PyObject_TypeCheck((o), wbuf_pytype) | |
42 | #define BUF_B(o) (&((buf_pyobj *)(o))->b) | |
0463996f | 43 | #define BUF_SUB(o) (((buf_pyobj *)(o))->sub) |
46e6ad89 | 44 | |
45 | /*----- Exceptions --------------------------------------------------------*/ | |
46 | ||
47 | static PyObject *buferr; | |
48 | ||
49 | #define BUFERR() do { PyErr_SetNone(buferr); goto end; } while (0) | |
50 | ||
51 | /*----- Read buffers ------------------------------------------------------*/ | |
52 | ||
53 | static PyObject *rbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
54 | { | |
55 | char *p, *q; | |
6b54260d | 56 | Py_ssize_t n; |
46e6ad89 | 57 | buf_pyobj *me = 0; |
58 | static char *kwlist[] = { "data", 0 }; | |
59 | ||
60 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &p, &n)) | |
61 | goto end; | |
46e6ad89 | 62 | q = xmalloc(n); |
63 | memcpy(q, p, n); | |
0463996f | 64 | me = (buf_pyobj *)ty->tp_alloc(ty, 0); |
65 | me->sub = 0; | |
46e6ad89 | 66 | buf_init(&me->b, q, n); |
67 | end: | |
68 | return ((PyObject *)me); | |
69 | } | |
70 | ||
71 | static void buf_pydealloc(PyObject *me) | |
0463996f | 72 | { |
73 | if (BUF_SUB(me)) | |
74 | Py_DECREF(BUF_SUB(me)); | |
75 | else | |
76 | xfree(BBASE(BUF_B(me))); | |
77 | FREEOBJ(me); | |
78 | } | |
46e6ad89 | 79 | |
3d8f5f7c | 80 | static Py_ssize_t rbuf_pysegcount(PyObject *me, Py_ssize_t *nn) |
46e6ad89 | 81 | { if (nn) *nn = BSZ(BUF_B(me)); return (1); } |
82 | ||
3d8f5f7c | 83 | static Py_ssize_t rbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q) |
f368b46e | 84 | { assert(seg == 0); *q = BCUR(BUF_B(me)); return (BLEFT(BUF_B(me))); } |
46e6ad89 | 85 | |
86 | static PyObject *rbmeth_skip(PyObject *me, PyObject *arg) | |
87 | { | |
88 | size_t n; | |
89 | ||
90 | if (!PyArg_ParseTuple(arg, "O&:skip", convszt, &n)) goto end; | |
91 | if (!buf_get(BUF_B(me), n)) BUFERR(); | |
92 | RETURN_ME; | |
93 | end: | |
94 | return (0); | |
95 | } | |
96 | ||
97 | static PyObject *rbmeth_get(PyObject *me, PyObject *arg) | |
98 | { | |
99 | void *p; | |
100 | size_t n; | |
101 | ||
102 | if (!PyArg_ParseTuple(arg, "O&:get", convszt, &n)) goto end; | |
103 | if ((p = buf_get(BUF_B(me), n)) == 0) BUFERR(); | |
104 | return (bytestring_pywrap(p, n)); | |
105 | end: | |
106 | return (0); | |
107 | } | |
108 | ||
109 | #define RBMETH_GETU_(n, W, w) \ | |
110 | static PyObject *rbmeth_getu##w(PyObject *me, PyObject *arg) \ | |
111 | { \ | |
112 | uint##n x; \ | |
113 | if (!PyArg_ParseTuple(arg, ":getu" #w)) goto end; \ | |
114 | if (buf_getu##w(BUF_B(me), &x)) BUFERR(); \ | |
fbca05a1 | 115 | return (getulong(x)); \ |
46e6ad89 | 116 | end: \ |
117 | return (0); \ | |
118 | } | |
119 | DOUINTCONV(RBMETH_GETU_) | |
120 | ||
121 | #define RBMETH_GETBLK_(n, W, w) \ | |
122 | static PyObject *rbmeth_getblk##w(PyObject *me, PyObject *arg) \ | |
123 | { \ | |
124 | size_t sz; \ | |
125 | char *q; \ | |
126 | if (!PyArg_ParseTuple(arg, ":getblk" #w)) goto end; \ | |
127 | if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) BUFERR(); \ | |
128 | return (bytestring_pywrap(q, sz)); \ | |
129 | end: \ | |
130 | return (0); \ | |
131 | } | |
132 | BUF_DOSUFFIXES(RBMETH_GETBLK_) | |
133 | ||
0463996f | 134 | #define RBMETH_GETBUF_(n, W, w) \ |
135 | static PyObject *rbmeth_getbuf##w(PyObject *me, PyObject *arg) \ | |
136 | { \ | |
137 | buf_pyobj *b; \ | |
138 | buf bb; \ | |
139 | if (!PyArg_ParseTuple(arg, ":getbuf" #w)) goto end; \ | |
140 | if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR(); \ | |
141 | b = PyObject_NEW(buf_pyobj, rbuf_pytype); \ | |
142 | b->b = bb; \ | |
143 | b->sub = me; \ | |
144 | Py_INCREF(me); \ | |
145 | return ((PyObject *)b); \ | |
146 | end: \ | |
147 | return (0); \ | |
148 | } | |
149 | BUF_DOSUFFIXES(RBMETH_GETBUF_) | |
150 | ||
46e6ad89 | 151 | static PyObject *rbmeth_getmp(PyObject *me, PyObject *arg) |
152 | { | |
153 | mp *x; | |
154 | if (!PyArg_ParseTuple(arg, ":getmp")) goto end; | |
155 | if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR(); | |
156 | return (mp_pywrap(x)); | |
157 | end: | |
158 | return (0); | |
159 | } | |
160 | ||
161 | static PyObject *rbmeth_getgf(PyObject *me, PyObject *arg) | |
162 | { | |
163 | mp *x; | |
164 | if (!PyArg_ParseTuple(arg, ":getgf")) goto end; | |
165 | if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR(); | |
166 | return (gf_pywrap(x)); | |
167 | end: | |
168 | return (0); | |
169 | } | |
170 | ||
171 | static PyObject *rbmeth_getecpt(PyObject *me, PyObject *arg, PyObject *kw) | |
172 | { | |
173 | PyObject *cobj = Py_None; | |
174 | static char *kwlist[] = { "curve", 0 }; | |
175 | ec pt = EC_INIT; | |
176 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:getecpt", kwlist, &cobj)) | |
177 | goto end; | |
178 | if (cobj == Py_None) cobj = (PyObject *)ecpt_pytype; | |
179 | if (!PyType_Check(cobj) || | |
180 | !PyType_IsSubtype((PyTypeObject *)cobj, ecpt_pytype)) | |
181 | TYERR("expected elliptic curve type"); | |
182 | if (buf_getec(BUF_B(me), &pt)) BUFERR(); | |
183 | return (ecpt_pywrapout(cobj, &pt)); | |
184 | end: | |
185 | return (0); | |
186 | } | |
b2687a0a | 187 | |
46e6ad89 | 188 | static PyObject *rbmeth_getecptraw(PyObject *me, PyObject *arg) |
189 | { | |
190 | PyTypeObject *cobj = ecpt_pytype; | |
191 | ec pt = EC_INIT; | |
192 | if (!PyArg_ParseTuple(arg, "O!:getecptraw", eccurve_pytype, &cobj)) | |
193 | goto end; | |
194 | if (ec_getraw(ECCURVE_C(cobj), BUF_B(me), &pt)) BUFERR(); | |
195 | return (ecpt_pywrapout(cobj, &pt)); | |
196 | end: | |
197 | return (0); | |
198 | } | |
199 | ||
200 | static PyObject *rbmeth_getge(PyObject *me, PyObject *arg) | |
201 | { | |
202 | PyObject *gobj; | |
203 | ge *x = 0; | |
204 | if (!PyArg_ParseTuple(arg, "O!:getge", group_pytype, &gobj)) goto end; | |
205 | x = G_CREATE(GROUP_G(gobj)); | |
206 | if (G_FROMBUF(GROUP_G(gobj), BUF_B(me), x)) BUFERR(); | |
207 | return (ge_pywrap(gobj, x)); | |
208 | end: | |
209 | if (x) G_DESTROY(GROUP_G(gobj), x); | |
210 | return (0); | |
211 | } | |
212 | ||
213 | static PyObject *rbmeth_getgeraw(PyObject *me, PyObject *arg) | |
214 | { | |
215 | PyObject *gobj; | |
216 | ge *x = 0; | |
217 | if (!PyArg_ParseTuple(arg, "O!:getgeraw", group_pytype, &gobj)) goto end; | |
218 | x = G_CREATE(GROUP_G(gobj)); | |
219 | if (G_FROMRAW(GROUP_G(gobj), BUF_B(me), x)) BUFERR(); | |
220 | return (ge_pywrap(gobj, x)); | |
221 | end: | |
222 | if (x) G_DESTROY(GROUP_G(gobj), x); | |
223 | return (0); | |
224 | } | |
225 | ||
226 | static PyObject *rbget_size(PyObject *me, void *hunoz) | |
227 | { return (PyInt_FromLong(BSZ(BUF_B(me)))); } | |
228 | static PyObject *rbget_left(PyObject *me, void *hunoz) | |
229 | { return (PyInt_FromLong(BLEFT(BUF_B(me)))); } | |
230 | static PyObject *rbget_endp(PyObject *me, void *hunoz) | |
231 | { return (getbool(!BLEFT(BUF_B(me)))); } | |
0463996f | 232 | static PyObject *rbget_offset(PyObject *me, void *hunoz) |
233 | { return (PyInt_FromLong(BLEN(BUF_B(me)))); } | |
234 | static int rbset_offset(PyObject *me, PyObject *x, void *hunoz) | |
235 | { | |
236 | size_t n; | |
f368b46e | 237 | if (!x) NIERR("__del__"); |
0463996f | 238 | if (!convszt(x, &n)) goto end; |
239 | if (n > BSZ(BUF_B(me))) VALERR("out of range"); | |
240 | BCUR(BUF_B(me)) = BBASE(BUF_B(me)) + n; | |
241 | return (0); | |
242 | end: | |
243 | return (-1); | |
244 | } | |
46e6ad89 | 245 | |
246 | static PyGetSetDef rbuf_pygetset[] = { | |
247 | #define GETSETNAME(op, name) rb##op##_##name | |
248 | GET (size, "RBUF.size -> SIZE") | |
249 | GET (left, "RBUF.left -> REMAINDER") | |
250 | GET (endp, "RBUF.endp -> BOOL") | |
0463996f | 251 | GETSET(offset, "RBUF.offset -> OFFSET") |
46e6ad89 | 252 | #undef GETSETNAME |
253 | { 0 } | |
254 | }; | |
255 | ||
256 | static PyMethodDef rbuf_pymethods[] = { | |
257 | #define METHNAME(func) rbmeth_##func | |
258 | METH (skip, "RBUF.skip(N)") | |
259 | METH (get, "RBUF.get(N) -> BYTES") | |
260 | #define RBMETH_DECL_GETU_(n, W, w) \ | |
261 | METH(getu##w, "RBUF.getu" #w "() -> INT") | |
262 | DOUINTCONV(RBMETH_DECL_GETU_) | |
263 | #define RBMETH_DECL_GETBLK_(n, W, w) \ | |
264 | METH(getblk##w, "RBUF.getblk" #w "() -> INT") | |
265 | BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_) | |
0463996f | 266 | #define RBMETH_DECL_GETBUF_(n, W, w) \ |
267 | METH(getbuf##w, "RBUF.getbuf" #w "() -> INT") | |
268 | BUF_DOSUFFIXES(RBMETH_DECL_GETBUF_) | |
46e6ad89 | 269 | METH (getmp, "RBUF.getmp() -> X") |
270 | METH (getgf, "RBUF.getgf() -> X") | |
271 | KWMETH(getecpt, "RBUF.getecpt(curve = None) -> P") | |
272 | METH (getecptraw, "RBUF.getecptraw(CURVE) -> P") | |
273 | METH (getge, "RBUF.getge(GROUP) -> X") | |
274 | METH (getgeraw, "RBUF.getgeraw(GROUP) -> X") | |
275 | #undef METHNAME | |
276 | { 0 } | |
277 | }; | |
278 | ||
279 | static PyBufferProcs rbuf_pybuffer = { | |
280 | rbuf_pyreadbuf, /* @bf_getreadbuffer@ */ | |
281 | 0, /* @bf_getwritebuffer@ */ | |
282 | rbuf_pysegcount, /* @bf_getsegcount@ */ | |
283 | 0 /* @bf_getcharbuffer@ */ | |
284 | }; | |
285 | ||
286 | static PyTypeObject rbuf_pytype_skel = { | |
287 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
c461c9b3 | 288 | "ReadBuffer", /* @tp_name@ */ |
46e6ad89 | 289 | sizeof(buf_pyobj), /* @tp_basicsize@ */ |
290 | 0, /* @tp_itemsize@ */ | |
291 | ||
292 | buf_pydealloc, /* @tp_dealloc@ */ | |
293 | 0, /* @tp_print@ */ | |
294 | 0, /* @tp_getattr@ */ | |
295 | 0, /* @tp_setattr@ */ | |
296 | 0, /* @tp_compare@ */ | |
297 | 0, /* @tp_repr@ */ | |
298 | 0, /* @tp_as_number@ */ | |
299 | 0, /* @tp_as_sequence@ */ | |
300 | 0, /* @tp_as_mapping@ */ | |
301 | 0, /* @tp_hash@ */ | |
302 | 0, /* @tp_call@ */ | |
303 | 0, /* @tp_str@ */ | |
304 | 0, /* @tp_getattro@ */ | |
305 | 0, /* @tp_setattro@ */ | |
306 | &rbuf_pybuffer, /* @tp_as_buffer@ */ | |
307 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
308 | Py_TPFLAGS_BASETYPE, | |
309 | ||
310 | /* @tp_doc@ */ | |
311 | "A read buffer.", | |
312 | ||
313 | 0, /* @tp_traverse@ */ | |
314 | 0, /* @tp_clear@ */ | |
315 | 0, /* @tp_richcompare@ */ | |
316 | 0, /* @tp_weaklistoffset@ */ | |
317 | 0, /* @tp_iter@ */ | |
963a6148 | 318 | 0, /* @tp_iternext@ */ |
46e6ad89 | 319 | rbuf_pymethods, /* @tp_methods@ */ |
320 | 0, /* @tp_members@ */ | |
321 | rbuf_pygetset, /* @tp_getset@ */ | |
322 | 0, /* @tp_base@ */ | |
323 | 0, /* @tp_dict@ */ | |
324 | 0, /* @tp_descr_get@ */ | |
325 | 0, /* @tp_descr_set@ */ | |
326 | 0, /* @tp_dictoffset@ */ | |
327 | 0, /* @tp_init@ */ | |
328 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
329 | rbuf_pynew, /* @tp_new@ */ | |
330 | 0, /* @tp_free@ */ | |
331 | 0 /* @tp_is_gc@ */ | |
332 | }; | |
333 | ||
334 | /*----- Write buffers -----------------------------------------------------*/ | |
335 | ||
336 | static void ensure(PyObject *me, size_t n) | |
337 | { | |
338 | buf *b = BUF_B(me); | |
339 | ||
340 | if (BLEFT(b) < n) { | |
341 | size_t nn = BSZ(b); | |
342 | octet *p; | |
c4ca600f | 343 | size_t want = BLEN(b) + n; |
46e6ad89 | 344 | while (nn < want) nn <<= 1; |
345 | p = xrealloc(BBASE(b), nn, BSZ(b)); | |
346 | BCUR(b) = p + BLEN(b); | |
347 | BLIM(b) = p + nn; | |
348 | BBASE(b) = p; | |
349 | } | |
350 | } | |
351 | ||
352 | static PyObject *wbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
353 | { | |
354 | char *p; | |
355 | size_t n = 64; | |
356 | buf_pyobj *me = 0; | |
357 | static char *kwlist[] = { "size", 0 }; | |
358 | ||
359 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", kwlist, | |
360 | convszt, &n)) | |
361 | goto end; | |
362 | me = (buf_pyobj *)ty->tp_alloc(ty, 0); | |
363 | p = xmalloc(n); | |
0463996f | 364 | me->sub = 0; |
46e6ad89 | 365 | buf_init(&me->b, p, n); |
366 | end: | |
367 | return ((PyObject *)me); | |
368 | } | |
369 | ||
3d8f5f7c | 370 | static Py_ssize_t wbuf_pysegcount(PyObject *me, Py_ssize_t *nn) |
46e6ad89 | 371 | { if (nn) *nn = BLEN(BUF_B(me)); return (1); } |
372 | ||
3d8f5f7c | 373 | static Py_ssize_t wbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q) |
46e6ad89 | 374 | { assert(seg == 0); *q = BBASE(BUF_B(me)); return (BLEN(BUF_B(me))); } |
375 | ||
376 | static PyObject *wbmeth_zero(PyObject *me, PyObject *arg) | |
377 | { | |
378 | void *p; | |
379 | size_t n; | |
380 | if (!PyArg_ParseTuple(arg, "O&:zero", convszt, &n)) return (0); | |
381 | ensure(me, n); | |
382 | p = buf_get(BUF_B(me), n); assert(p && BOK(BUF_B(me))); | |
383 | memset(p, 0, n); | |
384 | RETURN_ME; | |
385 | } | |
386 | ||
387 | static PyObject *wbmeth_put(PyObject *me, PyObject *arg) | |
388 | { | |
389 | void *p; | |
6b54260d | 390 | Py_ssize_t n; |
46e6ad89 | 391 | if (!PyArg_ParseTuple(arg, "s#:put", &p, &n)) return (0); |
392 | ensure(me, n); | |
a1184906 | 393 | buf_put(BUF_B(me), p, n); assert(BOK(BUF_B(me))); |
46e6ad89 | 394 | RETURN_ME; |
395 | } | |
396 | ||
397 | #define WBMETH_PUTU_(n, W, w) \ | |
398 | static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg) \ | |
399 | { \ | |
400 | uint##n i; \ | |
401 | if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \ | |
402 | ensure(me, SZ_##n); \ | |
403 | buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \ | |
404 | RETURN_ME; \ | |
405 | } | |
406 | DOUINTCONV(WBMETH_PUTU_) | |
407 | ||
408 | #define SZ_z 1 | |
409 | #define WBMETH_PUTBLK_(n, W, w) \ | |
410 | static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg) \ | |
411 | { \ | |
412 | char *p; \ | |
6b54260d | 413 | Py_ssize_t sz; \ |
46e6ad89 | 414 | if (!PyArg_ParseTuple(arg, "s#:putblk" #w, &p, &sz)) return (0); \ |
415 | ensure(me, sz + SZ_##n); \ | |
416 | buf_putmem##w(BUF_B(me), p, sz); assert(BOK(BUF_B(me))); \ | |
417 | RETURN_ME; \ | |
418 | } | |
419 | BUF_DOSUFFIXES(WBMETH_PUTBLK_) | |
420 | ||
421 | static PyObject *wbmeth_putmp(PyObject *me, PyObject *arg) | |
422 | { | |
423 | mp *x = 0; | |
424 | if (!PyArg_ParseTuple(arg, "O&:putmp", convmp, &x)) return (0); | |
425 | ensure(me, mp_octets(x) + 2); | |
426 | buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me))); | |
427 | RETURN_ME; | |
428 | } | |
429 | ||
430 | static PyObject *wbmeth_putgf(PyObject *me, PyObject *arg) | |
431 | { | |
432 | mp *x = 0; | |
433 | if (!PyArg_ParseTuple(arg, "O&:putgf", convgf, &x)) return (0); | |
434 | ensure(me, mp_octets(x) + 2); | |
435 | buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me))); | |
436 | MP_DROP(x); | |
437 | RETURN_ME; | |
438 | } | |
439 | ||
440 | static PyObject *wbmeth_putecpt(PyObject *me, PyObject *arg) | |
441 | { | |
442 | ec pt = EC_INIT; | |
443 | if (!PyArg_ParseTuple(arg, "O&:putecpt", convecpt, &pt)) return (0); | |
444 | if (EC_ATINF(&pt)) ensure(me, 2); | |
445 | else ensure(me, 4 + mp_octets(pt.x) + mp_octets(pt.y)); | |
446 | buf_putec(BUF_B(me), &pt); assert(BOK(BUF_B(me))); | |
447 | EC_DESTROY(&pt); | |
448 | RETURN_ME; | |
449 | } | |
450 | ||
451 | static PyObject *wbmeth_putecptraw(PyObject *me, PyObject *arg) | |
452 | { | |
453 | PyObject *ptobj; | |
454 | ec pt = EC_INIT; | |
455 | if (!PyArg_ParseTuple(arg, "O!:putecptraw", ecptcurve_pytype, &ptobj)) | |
456 | return (0); | |
457 | EC_OUT(ECPT_C(ptobj), &pt, ECPT_P(ptobj)); | |
458 | ensure(me, ECPT_C(ptobj)->f->noctets * 2 + 1); | |
459 | ec_putraw(ECPT_C(ptobj), BUF_B(me), &pt); assert(BOK(BUF_B(me))); | |
460 | EC_DESTROY(&pt); | |
461 | RETURN_ME; | |
462 | } | |
463 | ||
464 | static PyObject *wbmeth_putge(PyObject *me, PyObject *arg) | |
465 | { | |
466 | PyObject *geobj; | |
467 | if (!PyArg_ParseTuple(arg, "O!:putge", ge_pytype, &geobj)) return (0); | |
468 | ensure(me, GE_G(geobj)->noctets); | |
469 | G_TOBUF(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me))); | |
470 | RETURN_ME; | |
471 | } | |
472 | ||
473 | static PyObject *wbmeth_putgeraw(PyObject *me, PyObject *arg) | |
474 | { | |
475 | PyObject *geobj; | |
476 | if (!PyArg_ParseTuple(arg, "O!:putgeraw", ge_pytype, &geobj)) return (0); | |
477 | ensure(me, GE_G(geobj)->noctets); | |
478 | G_TORAW(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me))); | |
479 | RETURN_ME; | |
480 | } | |
481 | ||
482 | static PyObject *wbget_size(PyObject *me, void *hunoz) | |
483 | { return (PyInt_FromLong(BLEN(BUF_B(me)))); } | |
484 | ||
0051d10f MW |
485 | static PyObject *wbget_contents(PyObject *me, void *hunoz) |
486 | { return (bytestring_pywrap(BBASE(BUF_B(me)), BLEN(BUF_B(me)))); } | |
487 | ||
46e6ad89 | 488 | static PyGetSetDef wbuf_pygetset[] = { |
489 | #define GETSETNAME(op, name) wb##op##_##name | |
490 | GET (size, "WBUF.size -> SIZE") | |
0051d10f | 491 | GET (contents, "WBUF.contents -> STR") |
46e6ad89 | 492 | #undef GETSETNAME |
493 | { 0 } | |
494 | }; | |
495 | ||
496 | static PyMethodDef wbuf_pymethods[] = { | |
497 | #define METHNAME(func) wbmeth_##func | |
60ddd740 | 498 | METH (zero, "WBUF.zero(N)") |
46e6ad89 | 499 | METH (put, "WBUF.put(BYTES)") |
500 | #define WBMETH_DECL_PUTU_(n, W, w) \ | |
501 | METH(putu##w, "WBUF.putu" #w "(INT)") | |
502 | DOUINTCONV(WBMETH_DECL_PUTU_) | |
503 | #define WBMETH_DECL_PUTBLK_(n, W, w) \ | |
504 | METH(putblk##w, "WBUF.putblk" #w "(BYTES)") | |
505 | BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_) | |
506 | METH (putmp, "WBUF.putmp(X)") | |
507 | METH (putgf, "WBUF.putgf(X)") | |
508 | KWMETH(putecpt, "WBUF.putecpt(P)") | |
509 | METH (putecptraw, "WBUF.putecptraw(P)") | |
510 | METH (putge, "WBUF.putge(X)") | |
511 | METH (putgeraw, "WBUF.putgeraw(X)") | |
512 | #undef METHNAME | |
513 | { 0 } | |
514 | }; | |
515 | ||
516 | static PyBufferProcs wbuf_pybuffer = { | |
517 | wbuf_pyreadbuf, /* @bf_getreadbuffer@ */ | |
518 | 0, /* @bf_getwritebuffer@ */ | |
519 | wbuf_pysegcount, /* @bf_getsegcount@ */ | |
520 | 0 /* @bf_getcharbuffer@ */ | |
521 | }; | |
522 | ||
523 | static PyTypeObject wbuf_pytype_skel = { | |
524 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
c461c9b3 | 525 | "WriteBuffer", /* @tp_name@ */ |
46e6ad89 | 526 | sizeof(buf_pyobj), /* @tp_basicsize@ */ |
527 | 0, /* @tp_itemsize@ */ | |
528 | ||
529 | buf_pydealloc, /* @tp_dealloc@ */ | |
530 | 0, /* @tp_print@ */ | |
531 | 0, /* @tp_getattr@ */ | |
532 | 0, /* @tp_setattr@ */ | |
533 | 0, /* @tp_compare@ */ | |
534 | 0, /* @tp_repr@ */ | |
535 | 0, /* @tp_as_number@ */ | |
536 | 0, /* @tp_as_sequence@ */ | |
537 | 0, /* @tp_as_mapping@ */ | |
538 | 0, /* @tp_hash@ */ | |
539 | 0, /* @tp_call@ */ | |
540 | 0, /* @tp_str@ */ | |
541 | 0, /* @tp_getattro@ */ | |
542 | 0, /* @tp_setattro@ */ | |
543 | &wbuf_pybuffer, /* @tp_as_buffer@ */ | |
544 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
545 | Py_TPFLAGS_BASETYPE, | |
546 | ||
547 | /* @tp_doc@ */ | |
548 | "A write buffer.", | |
549 | ||
550 | 0, /* @tp_traverse@ */ | |
551 | 0, /* @tp_clear@ */ | |
552 | 0, /* @tp_richcompare@ */ | |
553 | 0, /* @tp_weaklistoffset@ */ | |
554 | 0, /* @tp_iter@ */ | |
963a6148 | 555 | 0, /* @tp_iternext@ */ |
46e6ad89 | 556 | wbuf_pymethods, /* @tp_methods@ */ |
557 | 0, /* @tp_members@ */ | |
558 | wbuf_pygetset, /* @tp_getset@ */ | |
559 | 0, /* @tp_base@ */ | |
560 | 0, /* @tp_dict@ */ | |
561 | 0, /* @tp_descr_get@ */ | |
562 | 0, /* @tp_descr_set@ */ | |
563 | 0, /* @tp_dictoffset@ */ | |
564 | 0, /* @tp_init@ */ | |
565 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
566 | wbuf_pynew, /* @tp_new@ */ | |
567 | 0, /* @tp_free@ */ | |
568 | 0 /* @tp_is_gc@ */ | |
569 | }; | |
570 | ||
571 | /*----- Initialization ----------------------------------------------------*/ | |
572 | ||
573 | void buffer_pyinit(void) | |
574 | { | |
575 | INITTYPE(rbuf, root); | |
576 | INITTYPE(wbuf, root); | |
577 | } | |
578 | ||
579 | void buffer_pyinsert(PyObject *mod) | |
580 | { | |
581 | INSEXC("BufferError", buferr, PyExc_Exception, 0); | |
582 | INSERT("ReadBuffer", rbuf_pytype); | |
583 | INSERT("WriteBuffer", wbuf_pytype); | |
584 | } | |
585 | ||
586 | /*----- That's all, folks -------------------------------------------------*/ |