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
= (BINOBJ
*)ty
->tp_alloc(ty
, n
);
42 #if defined(CACHE_HASH) || PY_VERSION_HEX >= 0x02030000
46 x
->ob_sstate
= SSTATE_NOT_INTERNED
;
48 return ((PyObject
*)x
);
51 static PyObject
*dowrap(PyTypeObject
*ty
, const void *p
, size_t n
)
56 if (p
&& ty
== bytestring_pytype
) {
58 if (!empty
) empty
= allocate(ty
, 0);
59 Py_INCREF(empty
); return (empty
);
60 } else if (n
== 1 && (ch
= *(unsigned char *)p
) < sizeof(bytev
)) {
62 { bytev
[ch
] = allocate(ty
, 1); *BIN_PTR(bytev
[ch
]) = ch
; }
63 Py_INCREF(bytev
[ch
]); return (bytev
[ch
]);
68 if (p
) memcpy(BIN_PTR(x
), p
, n
);
72 PyObject
*bytestring_pywrap(const void *p
, size_t n
)
73 { return (dowrap(bytestring_pytype
, p
, n
)); }
75 PyObject
*bytestring_pywrapbuf(buf
*b
)
76 { return (dowrap(bytestring_pytype
, BCUR(b
), BLEFT(b
))); }
78 static PyObject
*bytestring_pynew(PyTypeObject
*ty
,
79 PyObject
*arg
, PyObject
*kw
)
82 static const char *const kwlist
[] = { "data", 0 };
83 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convbin
, &in
))
85 return (dowrap(ty
, in
.p
, in
.sz
));
88 static PyObject
*meth_ctstreq(PyObject
*me
, PyObject
*arg
)
91 if (!PyArg_ParseTuple(arg
, "O&O&:ctstreq", convbin
, &s0
, convbin
, &s1
))
93 if (s0
.sz
== s1
.sz
&& ct_memeq(s0
.p
, s1
.p
, s0
.sz
)) RETURN_TRUE
;
99 static PyObject
*bymeth_zero(PyObject
*me
, PyObject
*arg
)
103 if (!PyArg_ParseTuple(arg
, "O&:zero", convszt
, &sz
)) goto end
;
104 rc
= bytestring_pywrap(0, sz
);
105 memset(BIN_PTR(rc
), 0, sz
);
110 static PyObject
*bytestring_pyrichcompare(PyObject
*me
,
111 PyObject
*you
, int op
)
117 s0
.p
= BIN_PTR(me
); s0
.sz
= BIN_LEN(me
);
118 if (!convbin(you
, &s1
)) { PyErr_Clear(); RETURN_NOTIMPL
; }
122 b
= s0
.sz
== s1
.sz
&& ct_memeq(s0
.p
, s1
.p
, s1
.sz
);
125 b
= s0
.sz
!= s1
.sz
|| !ct_memeq(s0
.p
, s1
.p
, s1
.sz
);
128 minlen
= s0
.sz
< s1
.sz ? s0
.sz
: s1
.sz
;
129 b
= memcmp(s0
.p
, s1
.p
, minlen
);
130 if (!b
) b
= s0
.sz
< s1
.sz ?
-1 : s0
.sz
> s1
.sz ?
+1 : 0;
132 case Py_LT
: b
= b
< 0; break;
133 case Py_LE
: b
= b
<= 0; break;
134 case Py_GE
: b
= b
>= 0; break;
135 case Py_GT
: b
= b
> 0; break;
143 static PyObject
*bytestring_pyconcat(PyObject
*x
, PyObject
*y
)
146 PyObject
*z
= 0; char *zp
; size_t zsz
;
148 if (!convbin(x
, &xx
) || !convbin(y
, &yy
)) goto end
;
149 zsz
= (size_t)xx
.sz
+ (size_t)yy
.sz
;
150 if (xx
.sz
< 0 || yy
.sz
< 0 || zsz
< xx
.sz
) VALERR("too long");
151 z
= bytestring_pywrap(0, zsz
); zp
= BIN_PTR(z
);
152 memcpy(zp
, xx
.p
, xx
.sz
); memcpy(zp
+ xx
.sz
, yy
.p
, yy
.sz
);
157 static PyObject
*bytestring_pyrepeat(PyObject
*me
, Py_ssize_t n
)
159 const unsigned char *xp
; size_t xsz
;
160 PyObject
*z
= 0; char *zp
; size_t zsz
;
162 xp
= (const unsigned char *)BIN_PTR(me
);
164 if (n
< 0 || (n
&& xsz
>= (size_t)-1/n
)) VALERR("too long");
165 zsz
= n
*xsz
; z
= bytestring_pywrap(0, zsz
); zp
= BIN_PTR(z
);
166 if (xsz
== 1) memset(zp
, *xp
, zsz
);
167 else while (zsz
) { memcpy(zp
, xp
, xsz
); zp
+= xsz
; zsz
-= xsz
; }
172 static PyObject
*bytestring_pyitem(PyObject
*me
, Py_ssize_t i
)
176 if (i
< 0 || i
>= BIN_LEN(me
)) IXERR("out of range");
178 rc
= getulong(BIN_PTR(me
)[i
]&0xff);
180 rc
= bytestring_pywrap(BIN_PTR(me
) + i
, 1);
186 static PyObject
*bytestring_pyslice(PyObject
*me
, Py_ssize_t i
, Py_ssize_t j
)
189 size_t n
= BIN_LEN(me
);
193 else if (j
> n
) j
= n
;
194 if (j
< i
) i
= j
= 0;
195 if (i
== 0 && j
== n
&& Py_TYPE(me
) == bytestring_pytype
)
196 { Py_INCREF(me
); rc
= me
; goto end
; }
197 rc
= bytestring_pywrap(BIN_PTR(me
) + i
, j
- i
);
202 static PyObject
*bytestring_pysubscript(PyObject
*me
, PyObject
*ix
)
204 Py_ssize_t i
, j
, k
, n
;
205 const unsigned char *p
;
209 if (PyIndex_Check(ix
)) {
210 i
= PyNumber_AsSsize_t(ix
, PyExc_IndexError
);
211 if (i
== -1 && PyErr_Occurred()) return (0);
212 if (i
< 0) i
+= BIN_LEN(me
);
213 rc
= bytestring_pyitem(me
, i
);
214 } else if (PySlice_Check(ix
)) {
215 if (PySlice_GetIndicesEx(PY23((PySliceObject
*), NOTHING
)ix
,
216 BIN_LEN(me
), &i
, &j
, &k
, &n
))
218 if (k
== 1) return bytestring_pyslice(me
, i
, j
);
219 rc
= bytestring_pywrap(0, n
);
220 p
= (unsigned char *)BIN_PTR(me
) + i
;
221 q
= (unsigned char *)BIN_PTR(rc
);
222 while (n
--) { *q
++ = *p
; p
+= k
; }
224 TYERR("wanted integer or slice");
229 #define BINOP(name, op) \
230 static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \
232 const unsigned char *xp, *yp; \
236 if (!convbin(x, &xx) || !convbin(y, &yy)) goto end; \
237 if (xx.sz != yy.sz) VALERR("length mismatch"); \
238 rc = bytestring_pywrap(0, xx.sz); \
239 xp = xx.p; yp = yy.p; zp = (unsigned char *)BIN_PTR(rc); \
240 for (i = xx.sz; i > 0; i--) *zp++ = *xp++ op *yp++; \
248 #define UNOP(name, op) \
249 static PyObject *bytestring_py##name(PyObject *x) { \
251 const unsigned char *xp; \
255 if (!convbin(x, &xx)) goto end; \
256 rc = bytestring_pywrap(0, xx.sz); \
257 xp = xx.p; zp = (unsigned char *)BIN_PTR(rc); \
258 for (i = xx.sz; i > 0; i--) *zp++ = op *xp++; \
264 static const PyMethodDef bytestring_pymethods
[] = {
265 #define METHNAME(name) bymeth_##name
266 SMTH (zero
, "zero(N) -> 0000...00")
271 static const PyNumberMethods bytestring_pynumber
= {
273 0, /* @nb_subtract@ */
274 0, /* @nb_multiply@ */
278 0, /* @nb_remainder@ */
281 0, /* @nb_negative@ */
282 0, /* @nb_positive@ */
283 0, /* @nb_absolute@ */
284 0, /* @nb_nonzero@ */
285 bytestring_pynot
, /* @nb_invert@ */
288 bytestring_pyand
, /* @nb_and@ */
289 bytestring_pyxor
, /* @nb_xor@ */
290 bytestring_pyor
, /* @nb_or@ */
299 static const PySequenceMethods bytestring_pysequence
= {
301 bytestring_pyconcat
, /* @sq_concat@ */
302 bytestring_pyrepeat
, /* @sq_repeat@ */
303 bytestring_pyitem
, /* @sq_item@ */
304 bytestring_pyslice
, /* @sq_slice@ */
305 0, /* @sq_ass_item@ */
306 0, /* @sq_ass_slice@ */
307 0, /* @sq_contains@ */
308 0, /* @sq_inplace_concat@ */
309 0, /* @sq_inplace_repeat@ */
312 static const PyMappingMethods bytestring_pymapping
= {
314 bytestring_pysubscript
, /* @mp_subscript@ */
315 0, /* @mp_ass_subscript@ */
318 static const PyTypeObject bytestring_pytype_skel
= {
319 PyVarObject_HEAD_INIT(0, 0) /* Header */
320 "ByteString", /* @tp_name@ */
321 0, /* @tp_basicsize@ */
322 0, /* @tp_itemsize@ */
324 0, /* @tp_dealloc@ */
326 0, /* @tp_getattr@ */
327 0, /* @tp_setattr@ */
328 0, /* @tp_compare@ */
330 PYNUMBER(bytestring
), /* @tp_as_number@ */
331 PYSEQUENCE(bytestring
), /* @tp_as_sequence@ */
332 PYMAPPING(bytestring
), /* @tp_as_mapping@ */
336 0, /* @tp_getattro@ */
337 0, /* @tp_setattro@ */
338 0, /* @tp_as_buffer@ */
339 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
340 Py_TPFLAGS_CHECKTYPES
|
344 "ByteString(STR): byte string class.",
346 0, /* @tp_traverse@ */
348 bytestring_pyrichcompare
, /* @tp_richcompare@ */
349 0, /* @tp_weaklistoffset@ */
351 0, /* @tp_iternext@ */
352 PYMETHODS(bytestring
), /* @tp_methods@ */
353 0, /* @tp_members@ */
357 0, /* @tp_descr_get@ */
358 0, /* @tp_descr_set@ */
359 0, /* @tp_dictoffset@ */
361 PyType_GenericAlloc
, /* @tp_alloc@ */
362 bytestring_pynew
, /* @tp_new@ */
367 /*----- Initialization ----------------------------------------------------*/
369 static const PyMethodDef methods
[] = {
370 #define METHNAME(func) meth_##func
371 METH (ctstreq
, "ctstreq(S, T) -> BOOL")
376 #define string_pytype &BIN_TYPE
377 void bytestring_pyinit(void)
379 INITTYPE(bytestring
, string
);
383 void bytestring_pyinsert(PyObject
*mod
)
385 INSERT("ByteString", bytestring_pytype
);
388 /*----- That's all, folks -------------------------------------------------*/