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 static PyTypeObject
*bytestring_pytype
;
35 static PyObject
*empty
, *bytev
[256];
37 static PyObject
*allocate(PyTypeObject
*ty
, size_t n
)
40 x
= (PyStringObject
*)ty
->tp_alloc(ty
, n
);
42 #if defined(CACHE_HASH) || PY_VERSION_HEX >= 0x02030000
45 x
->ob_sstate
= SSTATE_NOT_INTERNED
;
46 return ((PyObject
*)x
);
49 static PyObject
*dowrap(PyTypeObject
*ty
, const void *p
, size_t n
)
54 if (p
&& ty
== bytestring_pytype
) {
56 if (!empty
) empty
= allocate(ty
, 0);
57 Py_INCREF(empty
); return (empty
);
58 } else if (n
== 1 && (ch
= *(unsigned char *)p
) < sizeof(bytev
)) {
60 { bytev
[ch
] = allocate(ty
, 1); *PyString_AS_STRING(bytev
[ch
]) = ch
; }
61 Py_INCREF(bytev
[ch
]); return (bytev
[ch
]);
66 if (p
) memcpy(PyString_AS_STRING(x
), p
, n
);
70 PyObject
*bytestring_pywrap(const void *p
, size_t n
)
71 { return (dowrap(bytestring_pytype
, p
, n
)); }
73 PyObject
*bytestring_pywrapbuf(buf
*b
)
74 { return (dowrap(bytestring_pytype
, BCUR(b
), BLEFT(b
))); }
76 static PyObject
*bytestring_pynew(PyTypeObject
*ty
,
77 PyObject
*arg
, PyObject
*kw
)
80 static const char *const kwlist
[] = { "data", 0 };
81 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convbin
, &in
))
83 return (dowrap(ty
, in
.p
, in
.sz
));
86 static PyObject
*meth_ctstreq(PyObject
*me
, PyObject
*arg
)
89 if (!PyArg_ParseTuple(arg
, "O&O&:ctstreq", convbin
, &s0
, convbin
, &s1
))
91 if (s0
.sz
== s1
.sz
&& ct_memeq(s0
.p
, s1
.p
, s0
.sz
)) RETURN_TRUE
;
97 static PyObject
*bymeth_zero(PyObject
*me
, PyObject
*arg
)
101 if (!PyArg_ParseTuple(arg
, "O&:zero", convszt
, &sz
)) goto end
;
102 rc
= bytestring_pywrap(0, sz
);
103 memset(PyString_AS_STRING(rc
), 0, sz
);
108 static PyObject
*bytestring_pyrichcompare(PyObject
*me
,
109 PyObject
*you
, int op
)
115 s0
.p
= PyString_AS_STRING(me
); s0
.sz
= PyString_GET_SIZE(me
);
116 if (!convbin(you
, &s1
)) { PyErr_Clear(); RETURN_NOTIMPL
; }
120 b
= s0
.sz
== s1
.sz
&& ct_memeq(s0
.p
, s1
.p
, s1
.sz
);
123 b
= s0
.sz
!= s1
.sz
|| !ct_memeq(s0
.p
, s1
.p
, s1
.sz
);
126 minlen
= s0
.sz
< s1
.sz ? s0
.sz
: s1
.sz
;
127 b
= memcmp(s0
.p
, s1
.p
, minlen
);
128 if (!b
) b
= s0
.sz
< s1
.sz ?
-1 : s0
.sz
> s1
.sz ?
+1 : 0;
130 case Py_LT
: b
= b
< 0; break;
131 case Py_LE
: b
= b
<= 0; break;
132 case Py_GE
: b
= b
>= 0; break;
133 case Py_GT
: b
= b
> 0; break;
141 static PyObject
*bytestring_pyconcat(PyObject
*x
, PyObject
*y
)
144 PyObject
*z
= 0; char *zp
; size_t zsz
;
146 if (!convbin(x
, &xx
) || !convbin(y
, &yy
)) goto end
;
147 zsz
= (size_t)xx
.sz
+ (size_t)yy
.sz
;
148 if (xx
.sz
< 0 || yy
.sz
< 0 || zsz
< xx
.sz
) VALERR("too long");
149 z
= bytestring_pywrap(0, zsz
); zp
= PyString_AS_STRING(z
);
150 memcpy(zp
, xx
.p
, xx
.sz
); memcpy(zp
+ xx
.sz
, yy
.p
, yy
.sz
);
155 static PyObject
*bytestring_pyrepeat(PyObject
*me
, Py_ssize_t n
)
157 const unsigned char *xp
; size_t xsz
;
158 PyObject
*z
= 0; char *zp
; size_t zsz
;
160 xp
= (const unsigned char *)PyString_AS_STRING(me
);
161 xsz
= PyString_GET_SIZE(me
);
162 if (n
< 0 || (n
&& xsz
>= (size_t)-1/n
)) VALERR("too long");
163 zsz
= n
*xsz
; z
= bytestring_pywrap(0, zsz
); zp
= PyString_AS_STRING(z
);
164 if (xsz
== 1) memset(zp
, *xp
, zsz
);
165 else while (zsz
) { memcpy(zp
, xp
, xsz
); zp
+= xsz
; zsz
-= xsz
; }
170 static PyObject
*bytestring_pyitem(PyObject
*me
, Py_ssize_t i
)
174 if (i
< 0 || i
>= PyString_GET_SIZE(me
)) IXERR("out of range");
175 rc
= bytestring_pywrap(PyString_AS_STRING(me
) + i
, 1);
180 static PyObject
*bytestring_pyslice(PyObject
*me
, Py_ssize_t i
, Py_ssize_t j
)
183 size_t n
= PyString_GET_SIZE(me
);
187 else if (j
> n
) j
= n
;
188 if (j
< i
) i
= j
= 0;
189 if (i
== 0 && j
== n
&& Py_TYPE(me
) == bytestring_pytype
)
190 { Py_INCREF(me
); rc
= me
; goto end
; }
191 rc
= bytestring_pywrap(PyString_AS_STRING(me
) + i
, j
- i
);
196 static PyObject
*bytestring_pysubscript(PyObject
*me
, PyObject
*ix
)
198 Py_ssize_t i
, j
, k
, n
;
199 const unsigned char *p
;
203 if (PyIndex_Check(ix
)) {
204 i
= PyNumber_AsSsize_t(ix
, PyExc_IndexError
);
205 if (i
== -1 && PyErr_Occurred()) return (0);
206 if (i
< 0) i
+= PyString_GET_SIZE(me
);
207 rc
= bytestring_pyitem(me
, i
);
208 } else if (PySlice_Check(ix
)) {
209 if (PySlice_GetIndicesEx((PySliceObject
*)ix
, PyString_GET_SIZE(me
),
212 if (k
== 1) return bytestring_pyslice(me
, i
, j
);
213 rc
= bytestring_pywrap(0, n
);
214 p
= (unsigned char *)PyString_AS_STRING(me
) + i
;
215 q
= (unsigned char *)PyString_AS_STRING(rc
);
216 while (n
--) { *q
++ = *p
; p
+= k
; }
218 TYERR("wanted integer or slice");
223 #define BINOP(name, op) \
224 static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \
226 const unsigned char *xp, *yp; \
230 if (!convbin(x, &xx) || !convbin(y, &yy)) goto end; \
231 if (xx.sz != yy.sz) VALERR("length mismatch"); \
232 rc = bytestring_pywrap(0, xx.sz); \
233 xp = xx.p; yp = yy.p; zp = (unsigned char *)PyString_AS_STRING(rc); \
234 for (i = xx.sz; i > 0; i--) *zp++ = *xp++ op *yp++; \
242 #define UNOP(name, op) \
243 static PyObject *bytestring_py##name(PyObject *x) { \
245 const unsigned char *xp; \
249 if (!convbin(x, &xx)) goto end; \
250 rc = bytestring_pywrap(0, xx.sz); \
251 xp = xx.p; zp = (unsigned char *)PyString_AS_STRING(rc); \
252 for (i = xx.sz; i > 0; i--) *zp++ = op *xp++; \
258 static const PyMethodDef bytestring_pymethods
[] = {
259 #define METHNAME(name) bymeth_##name
260 SMTH (zero
, "zero(N) -> 0000...00")
265 static const PyNumberMethods bytestring_pynumber
= {
267 0, /* @nb_subtract@ */
268 0, /* @nb_multiply@ */
270 0, /* @nb_remainder@ */
273 0, /* @nb_negative@ */
274 0, /* @nb_positive@ */
275 0, /* @nb_absolute@ */
276 0, /* @nb_nonzero@ */
277 bytestring_pynot
, /* @nb_invert@ */
280 bytestring_pyand
, /* @nb_and@ */
281 bytestring_pyxor
, /* @nb_xor@ */
282 bytestring_pyor
, /* @nb_or@ */
291 static const PySequenceMethods bytestring_pysequence
= {
293 bytestring_pyconcat
, /* @sq_concat@ */
294 bytestring_pyrepeat
, /* @sq_repeat@ */
295 bytestring_pyitem
, /* @sq_item@ */
296 bytestring_pyslice
, /* @sq_slice@ */
297 0, /* @sq_ass_item@ */
298 0, /* @sq_ass_slice@ */
299 0, /* @sq_contains@ */
300 0, /* @sq_inplace_concat@ */
301 0, /* @sq_inplace_repeat@ */
304 static const PyMappingMethods bytestring_pymapping
= {
306 bytestring_pysubscript
, /* @mp_subscript@ */
307 0, /* @mp_ass_subscript@ */
310 static const PyTypeObject bytestring_pytype_skel
= {
311 PyVarObject_HEAD_INIT(0, 0) /* Header */
312 "ByteString", /* @tp_name@ */
313 0, /* @tp_basicsize@ */
314 0, /* @tp_itemsize@ */
316 0, /* @tp_dealloc@ */
318 0, /* @tp_getattr@ */
319 0, /* @tp_setattr@ */
320 0, /* @tp_compare@ */
322 PYNUMBER(bytestring
), /* @tp_as_number@ */
323 PYSEQUENCE(bytestring
), /* @tp_as_sequence@ */
324 PYMAPPING(bytestring
), /* @tp_as_mapping@ */
328 0, /* @tp_getattro@ */
329 0, /* @tp_setattro@ */
330 0, /* @tp_as_buffer@ */
331 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
332 Py_TPFLAGS_CHECKTYPES
|
336 "ByteString(STR): byte string class.",
338 0, /* @tp_traverse@ */
340 bytestring_pyrichcompare
, /* @tp_richcompare@ */
341 0, /* @tp_weaklistoffset@ */
343 0, /* @tp_iternext@ */
344 PYMETHODS(bytestring
), /* @tp_methods@ */
345 0, /* @tp_members@ */
349 0, /* @tp_descr_get@ */
350 0, /* @tp_descr_set@ */
351 0, /* @tp_dictoffset@ */
353 PyType_GenericAlloc
, /* @tp_alloc@ */
354 bytestring_pynew
, /* @tp_new@ */
359 /*----- Initialization ----------------------------------------------------*/
361 static const PyMethodDef methods
[] = {
362 #define METHNAME(func) meth_##func
363 METH (ctstreq
, "ctstreq(S, T) -> BOOL")
368 #define string_pytype &PyString_Type
369 void bytestring_pyinit(void)
371 INITTYPE(bytestring
, string
);
375 void bytestring_pyinsert(PyObject
*mod
)
377 INSERT("ByteString", bytestring_pytype
);
380 /*----- That's all, folks -------------------------------------------------*/