5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- Main code ---------------------------------------------------------*/
33 PyTypeObject
*bytestring_pytype
;
35 static PyObject
*allocate(PyTypeObject
*ty
, size_t n
)
38 x
= (PyStringObject
*)ty
->tp_alloc(ty
, n
);
40 #if defined(CACHE_HASH) || PY_VERSION_HEX >= 0x02030000
43 x
->ob_sstate
= SSTATE_NOT_INTERNED
;
44 return ((PyObject
*)x
);
47 static PyObject
*dowrap(PyTypeObject
*ty
, const void *p
, size_t n
)
52 if (p
) memcpy(PyString_AS_STRING(x
), p
, n
);
56 PyObject
*bytestring_pywrap(const void *p
, size_t n
)
57 { return (dowrap(bytestring_pytype
, p
, n
)); }
59 PyObject
*bytestring_pywrapbuf(buf
*b
)
60 { return (dowrap(bytestring_pytype
, BCUR(b
), BLEFT(b
))); }
62 static PyObject
*bytestring_pynew(PyTypeObject
*ty
,
63 PyObject
*arg
, PyObject
*kw
)
67 static const char *const kwlist
[] = { "data", 0 };
68 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &p
, &n
))
70 return (dowrap(ty
, p
, n
));
73 static PyObject
*meth_ctstreq(PyObject
*me
, PyObject
*arg
)
77 if (!PyArg_ParseTuple(arg
, "s#s#:ctstreq", &p
, &psz
, &q
, &qsz
))
79 if (psz
== qsz
&& ct_memeq(p
, q
, psz
)) RETURN_TRUE
;
85 static PyObject
*meth__ByteString_zero(PyObject
*me
, PyObject
*arg
)
89 if (!PyArg_ParseTuple(arg
, "OO&:zero", &me
, convszt
, &sz
)) goto end
;
90 rc
= bytestring_pywrap(0, sz
);
91 memset(PyString_AS_STRING(rc
), 0, sz
);
96 static PyObject
*bytestring_pyrichcompare(PyObject
*me
,
97 PyObject
*you
, int op
)
100 void *mystr
, *yourstr
;
101 Py_ssize_t mylen
, yourlen
, minlen
;
103 if (!PyString_Check(me
) || !PyString_Check(you
)) RETURN_NOTIMPL
;
104 mystr
= PyString_AS_STRING(me
); mylen
= PyString_GET_SIZE(me
);
105 yourstr
= PyString_AS_STRING(you
); yourlen
= PyString_GET_SIZE(you
);
109 b
= mylen
== yourlen
&& ct_memeq(mystr
, yourstr
, mylen
);
112 b
= mylen
!= yourlen
|| !ct_memeq(mystr
, yourstr
, mylen
);
115 minlen
= mylen
< yourlen ? mylen
: yourlen
;
116 b
= memcmp(mystr
, yourstr
, minlen
);
117 if (!b
) b
= mylen
< yourlen ?
-1 : mylen
> yourlen ?
+1 : 0;
119 case Py_LT
: b
= b
< 0; break;
120 case Py_LE
: b
= b
<= 0; break;
121 case Py_GE
: b
= b
>= 0; break;
122 case Py_GT
: b
= b
> 0; break;
130 #define BINOP(name, op) \
131 static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \
132 const void *xv, *yv; \
133 const unsigned char *xp, *yp; \
135 Py_ssize_t xsz, ysz; \
138 if (PyObject_AsReadBuffer(x, &xv, &xsz) || \
139 PyObject_AsReadBuffer(y, &yv, &ysz)) \
141 if (xsz != ysz) VALERR("length mismatch"); \
142 rc = bytestring_pywrap(0, xsz); \
143 xp = xv; yp = yv; zp = (unsigned char *)PyString_AS_STRING(rc); \
144 for (i = xsz; i > 0; i--) *zp++ = *xp++ op *yp++; \
152 #define UNOP(name, op) \
153 static PyObject *bytestring_py##name(PyObject *x) { \
155 const unsigned char *xp; \
160 if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end; \
161 rc = bytestring_pywrap(0, xsz); \
162 xp = xv; zp = (unsigned char *)PyString_AS_STRING(rc); \
163 for (i = xsz; i > 0; i--) *zp++ = op *xp++; \
169 static PyNumberMethods bytestring_pynumber
= {
171 0, /* @nb_subtract@ */
172 0, /* @nb_multiply@ */
174 0, /* @nb_remainder@ */
177 0, /* @nb_negative@ */
178 0, /* @nb_positive@ */
179 0, /* @nb_absolute@ */
180 0, /* @nb_nonzero@ */
181 bytestring_pynot
, /* @nb_invert@ */
184 bytestring_pyand
, /* @nb_and@ */
185 bytestring_pyxor
, /* @nb_xor@ */
186 bytestring_pyor
, /* @nb_or@ */
195 static PyBufferProcs bytestring_pybuffer
;
197 static PyTypeObject bytestring_pytype_skel
= {
198 PyObject_HEAD_INIT(0) 0, /* Header */
199 "ByteString", /* @tp_name@ */
200 0, /* @tp_basicsize@ */
201 0, /* @tp_itemsize@ */
203 0, /* @tp_dealloc@ */
205 0, /* @tp_getattr@ */
206 0, /* @tp_setattr@ */
207 0, /* @tp_compare@ */
209 &bytestring_pynumber
, /* @tp_as_number@ */
210 0, /* @tp_as_sequence@ */
211 0, /* @tp_as_mapping@ */
215 0, /* @tp_getattro@ */
216 0, /* @tp_setattro@ */
217 &bytestring_pybuffer
, /* @tp_as_buffer@ */
218 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
219 Py_TPFLAGS_CHECKTYPES
|
223 "ByteString(STR): byte string class.",
225 0, /* @tp_traverse@ */
227 bytestring_pyrichcompare
, /* @tp_richcompare@ */
228 0, /* @tp_weaklistoffset@ */
230 0, /* @tp_iternext@ */
231 0, /* @tp_methods@ */
232 0, /* @tp_members@ */
236 0, /* @tp_descr_get@ */
237 0, /* @tp_descr_set@ */
238 0, /* @tp_dictoffset@ */
240 PyType_GenericAlloc
, /* @tp_alloc@ */
241 bytestring_pynew
, /* @tp_new@ */
246 /*----- Initialization ----------------------------------------------------*/
248 static PyMethodDef methods
[] = {
249 #define METHNAME(func) meth_##func
250 METH (ctstreq
, "ctstreq(S, T) -> BOOL")
251 METH (_ByteString_zero
, "zero(N) -> 0000...00")
256 #define string_pytype &PyString_Type
257 void bytestring_pyinit(void)
259 INITTYPE(bytestring
, string
);
263 void bytestring_pyinsert(PyObject
*mod
)
265 INSERT("ByteString", bytestring_pytype
);
268 /*----- That's all, folks -------------------------------------------------*/