7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Python interface to Catacomb.
14 * Catacomb/Python is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Catacomb/Python is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Catacomb/Python; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
31 #include "catacomb-python.h"
33 /*----- Main code ---------------------------------------------------------*/
35 PyTypeObject
*bytestring_pytype
;
37 static PyObject
*dowrap(PyTypeObject
*ty
, const void *p
, size_t n
)
39 PyStringObject
*x
= (PyStringObject
*)ty
->tp_alloc(ty
, n
);
40 if (p
) memcpy(x
->ob_sval
, p
, n
);
45 x
->ob_sstate
= SSTATE_NOT_INTERNED
;
46 return ((PyObject
*)x
);
49 PyObject
*bytestring_pywrap(const void *p
, size_t n
)
50 { return (dowrap(bytestring_pytype
, p
, n
)); }
52 PyObject
*bytestring_pywrapbuf(buf
*b
)
53 { return (dowrap(bytestring_pytype
, BCUR(b
), BLEFT(b
))); }
55 static PyObject
*bytestring_pynew(PyTypeObject
*ty
,
56 PyObject
*arg
, PyObject
*kw
)
60 static char *kwlist
[] = { "data", 0 };
61 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &p
, &n
))
63 return (dowrap(ty
, p
, n
));
66 #define BINOP(name, op) \
67 static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \
68 const void *xv, *yv; \
69 const unsigned char *xp, *yp; \
74 if (PyObject_AsReadBuffer(x, &xv, &xsz) || \
75 PyObject_AsReadBuffer(y, &yv, &ysz)) \
77 if (xsz != ysz) VALERR("length mismatch"); \
78 rc = bytestring_pywrap(0, xsz); \
79 xp = xv; yp = yv; zp = (unsigned char *)PyString_AS_STRING(rc); \
80 for (i = xsz; i > 0; i--) *zp++ = *xp++ op *yp++; \
88 #define UNOP(name, op) \
89 static PyObject *bytestring_py##name(PyObject *x) { \
91 const unsigned char *xp; \
96 if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end; \
97 rc = bytestring_pywrap(0, xsz); \
98 xp = xv; zp = (unsigned char *)PyString_AS_STRING(rc); \
99 for (i = xsz; i > 0; i--) *zp++ = op *xp++; \
105 static PyNumberMethods bytestring_pynumber
= {
107 0, /* @nb_subtract@ */
108 0, /* @nb_multiply@ */
110 0, /* @nb_remainder@ */
113 0, /* @nb_negative@ */
114 0, /* @nb_positive@ */
115 0, /* @nb_absolute@ */
116 0, /* @nb_nonzero@ */
117 bytestring_pynot
, /* @nb_invert@ */
120 bytestring_pyand
, /* @nb_and@ */
121 bytestring_pyxor
, /* @nb_xor@ */
122 bytestring_pyor
, /* @nb_or@ */
131 static PyBufferProcs bytestring_pybuffer
;
133 static PyTypeObject bytestring_pytype_skel
= {
134 PyObject_HEAD_INIT(0) 0, /* Header */
135 "catacomb.ByteString", /* @tp_name@ */
136 0, /* @tp_basicsize@ */
137 0, /* @tp_itemsize@ */
139 0, /* @tp_dealloc@ */
141 0, /* @tp_getattr@ */
142 0, /* @tp_setattr@ */
143 0, /* @tp_compare@ */
145 &bytestring_pynumber
, /* @tp_as_number@ */
146 0, /* @tp_as_sequence@ */
147 0, /* @tp_as_mapping@ */
151 0, /* @tp_getattro@ */
152 0, /* @tp_setattro@ */
153 &bytestring_pybuffer
, /* @tp_as_buffer@ */
154 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
155 Py_TPFLAGS_CHECKTYPES
|
159 "Byte string class.",
161 0, /* @tp_traverse@ */
163 0, /* @tp_richcompare@ */
164 0, /* @tp_weaklistoffset@ */
166 0, /* @tp_iternext@ */
167 0, /* @tp_methods@ */
168 0, /* @tp_members@ */
172 0, /* @tp_descr_get@ */
173 0, /* @tp_descr_set@ */
174 0, /* @tp_dictoffset@ */
176 PyType_GenericAlloc
, /* @tp_alloc@ */
177 bytestring_pynew
, /* @tp_new@ */
182 /*----- Initialization ----------------------------------------------------*/
184 #define string_pytype &PyString_Type
185 void bytestring_pyinit(void)
187 INITTYPE(bytestring
, string
);
190 void bytestring_pyinsert(PyObject
*mod
)
192 INSERT("ByteString", bytestring_pytype
);
195 /*----- That's all, folks -------------------------------------------------*/