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