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 PyObject *bytestring_pywrap(const void *p, size_t n)
39 PyStringObject *x = PyObject_NewVar(PyStringObject, bytestring_pytype, n);
40 if (p) memcpy(x->ob_sval, p, n);
45 x->ob_sstate = SSTATE_NOT_INTERNED;
46 return ((PyObject *)x);
49 PyObject *bytestring_pywrapbuf(buf *b)
50 { return bytestring_pywrap(BCUR(b), BLEFT(b)); }
52 #define BINOP(name, op) \
53 static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \
54 const void *xv, *yv; \
55 const unsigned char *xp, *yp; \
60 if (PyObject_AsReadBuffer(x, &xv, &xsz) || \
61 PyObject_AsReadBuffer(y, &yv, &ysz)) \
63 if (xsz != ysz) VALERR("length mismatch"); \
64 rc = bytestring_pywrap(0, xsz); \
65 xp = xv; yp = yv; zp = (unsigned char *)PyString_AS_STRING(rc); \
66 for (i = xsz; i > 0; i--) *zp++ = *xp++ op *yp++; \
74 #define UNOP(name, op) \
75 static PyObject *bytestring_py##name(PyObject *x) { \
77 const unsigned char *xp; \
82 if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end; \
83 rc = bytestring_pywrap(0, xsz); \
84 xp = xv; zp = (unsigned char *)PyString_AS_STRING(rc); \
85 for (i = xsz; i > 0; i--) *zp++ = op *xp++; \
91 static PyNumberMethods bytestring_pynumber = {
93 0, /* @nb_subtract@ */
94 0, /* @nb_multiply@ */
96 0, /* @nb_remainder@ */
99 0, /* @nb_negative@ */
100 0, /* @nb_positive@ */
101 0, /* @nb_absolute@ */
102 0, /* @nb_nonzero@ */
103 bytestring_pynot, /* @nb_invert@ */
106 bytestring_pyand, /* @nb_and@ */
107 bytestring_pyxor, /* @nb_xor@ */
108 bytestring_pyor, /* @nb_or@ */
117 static PyBufferProcs bytestring_pybuffer;
119 static PyTypeObject bytestring_pytype_skel = {
120 PyObject_HEAD_INIT(0) 0, /* Header */
121 "catacomb.ByteString", /* @tp_name@ */
122 0, /* @tp_basicsize@ */
123 0, /* @tp_itemsize@ */
125 0, /* @tp_dealloc@ */
127 0, /* @tp_getattr@ */
128 0, /* @tp_setattr@ */
129 0, /* @tp_compare@ */
131 &bytestring_pynumber, /* @tp_as_number@ */
132 0, /* @tp_as_sequence@ */
133 0, /* @tp_as_mapping@ */
137 0, /* @tp_getattro@ */
138 0, /* @tp_setattro@ */
139 &bytestring_pybuffer, /* @tp_as_buffer@ */
140 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
141 Py_TPFLAGS_CHECKTYPES |
145 "Byte string class.",
147 0, /* @tp_traverse@ */
149 0, /* @tp_richcompare@ */
150 0, /* @tp_weaklistoffset@ */
152 0, /* @tp_iternexr@ */
153 0, /* @tp_methods@ */
154 0, /* @tp_members@ */
158 0, /* @tp_descr_get@ */
159 0, /* @tp_descr_set@ */
160 0, /* @tp_dictoffset@ */
162 PyType_GenericAlloc, /* @tp_alloc@ */
168 /*----- Initialization ----------------------------------------------------*/
170 #define string_pytype &PyString_Type
171 void bytestring_pyinit(void)
173 INITTYPE(bytestring, string);
176 void bytestring_pyinsert(PyObject *mod)
178 INSERT("ByteString", bytestring_pytype);
181 /*----- That's all, folks -------------------------------------------------*/