Commit | Line | Data |
---|---|---|
d7ab1bab | 1 | /* -*-c-*- |
2 | * | |
d7ab1bab | 3 | * Byte strings |
4 | * | |
5 | * (c) 2004 Straylight/Edgeware | |
6 | */ | |
7 | ||
b2687a0a | 8 | /*----- Licensing notice --------------------------------------------------* |
d7ab1bab | 9 | * |
10 | * This file is part of the Python interface to Catacomb. | |
11 | * | |
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. | |
b2687a0a | 16 | * |
d7ab1bab | 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. | |
b2687a0a | 21 | * |
d7ab1bab | 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. | |
25 | */ | |
26 | ||
27 | /*----- Header files ------------------------------------------------------*/ | |
28 | ||
29 | #include "catacomb-python.h" | |
30 | ||
31 | /*----- Main code ---------------------------------------------------------*/ | |
32 | ||
c6e89d48 | 33 | static PyTypeObject *bytestring_pytype; |
d7ab1bab | 34 | |
4d09d07e MW |
35 | static PyObject *empty, *bytev[256]; |
36 | ||
438b1639 | 37 | static PyObject *allocate(PyTypeObject *ty, size_t n) |
d7ab1bab | 38 | { |
438b1639 MW |
39 | PyStringObject *x; |
40 | x = (PyStringObject *)ty->tp_alloc(ty, n); | |
d7ab1bab | 41 | x->ob_sval[n] = 0; |
69e13768 | 42 | #if defined(CACHE_HASH) || PY_VERSION_HEX >= 0x02030000 |
d7ab1bab | 43 | x->ob_shash = -1; |
44 | #endif | |
3aa33042 | 45 | x->ob_sstate = SSTATE_NOT_INTERNED; |
d7ab1bab | 46 | return ((PyObject *)x); |
47 | } | |
48 | ||
438b1639 MW |
49 | static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n) |
50 | { | |
51 | PyObject *x; | |
4d09d07e MW |
52 | int ch; |
53 | ||
54 | if (p && ty == bytestring_pytype) { | |
55 | if (!n) { | |
56 | if (!empty) empty = allocate(ty, 0); | |
57 | Py_INCREF(empty); return (empty); | |
58 | } else if (n == 1 && (ch = *(unsigned char *)p) < sizeof(bytev)) { | |
59 | if (!bytev[ch]) | |
60 | { bytev[ch] = allocate(ty, 1); *PyString_AS_STRING(bytev[ch]) = ch; } | |
61 | Py_INCREF(bytev[ch]); return (bytev[ch]); | |
62 | } | |
63 | } | |
438b1639 MW |
64 | |
65 | x = allocate(ty, n); | |
66 | if (p) memcpy(PyString_AS_STRING(x), p, n); | |
67 | return (x); | |
68 | } | |
69 | ||
46e6ad89 | 70 | PyObject *bytestring_pywrap(const void *p, size_t n) |
71 | { return (dowrap(bytestring_pytype, p, n)); } | |
72 | ||
d7ab1bab | 73 | PyObject *bytestring_pywrapbuf(buf *b) |
46e6ad89 | 74 | { return (dowrap(bytestring_pytype, BCUR(b), BLEFT(b))); } |
75 | ||
76 | static PyObject *bytestring_pynew(PyTypeObject *ty, | |
77 | PyObject *arg, PyObject *kw) | |
78 | { | |
79 | const char *p; | |
6b54260d | 80 | Py_ssize_t n; |
827f89d7 MW |
81 | static const char *const kwlist[] = { "data", 0 }; |
82 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &p, &n)) | |
46e6ad89 | 83 | return (0); |
84 | return (dowrap(ty, p, n)); | |
85 | } | |
d7ab1bab | 86 | |
2a21aec7 | 87 | static PyObject *meth_ctstreq(PyObject *me, PyObject *arg) |
bfb450cc MW |
88 | { |
89 | char *p, *q; | |
90 | Py_ssize_t psz, qsz; | |
2a21aec7 | 91 | if (!PyArg_ParseTuple(arg, "s#s#:ctstreq", &p, &psz, &q, &qsz)) |
bfb450cc MW |
92 | goto end; |
93 | if (psz == qsz && ct_memeq(p, q, psz)) RETURN_TRUE; | |
94 | else RETURN_FALSE; | |
95 | end: | |
96 | return (0); | |
97 | } | |
98 | ||
a157093f | 99 | static PyObject *bymeth_zero(PyObject *me, PyObject *arg) |
5a8b43b3 MW |
100 | { |
101 | size_t sz; | |
102 | PyObject *rc = 0; | |
a157093f | 103 | if (!PyArg_ParseTuple(arg, "O&:zero", convszt, &sz)) goto end; |
5a8b43b3 MW |
104 | rc = bytestring_pywrap(0, sz); |
105 | memset(PyString_AS_STRING(rc), 0, sz); | |
106 | end: | |
107 | return (rc); | |
108 | } | |
109 | ||
bfb450cc MW |
110 | static PyObject *bytestring_pyrichcompare(PyObject *me, |
111 | PyObject *you, int op) | |
112 | { | |
113 | int b; | |
114 | void *mystr, *yourstr; | |
115 | Py_ssize_t mylen, yourlen, minlen; | |
116 | ||
117 | if (!PyString_Check(me) || !PyString_Check(you)) RETURN_NOTIMPL; | |
118 | mystr = PyString_AS_STRING(me); mylen = PyString_GET_SIZE(me); | |
119 | yourstr = PyString_AS_STRING(you); yourlen = PyString_GET_SIZE(you); | |
120 | ||
121 | switch (op) { | |
122 | case Py_EQ: | |
123 | b = mylen == yourlen && ct_memeq(mystr, yourstr, mylen); | |
124 | break; | |
125 | case Py_NE: | |
126 | b = mylen != yourlen || !ct_memeq(mystr, yourstr, mylen); | |
127 | break; | |
128 | default: | |
129 | minlen = mylen < yourlen ? mylen : yourlen; | |
130 | b = memcmp(mystr, yourstr, minlen); | |
131 | if (!b) b = mylen < yourlen ? -1 : mylen > yourlen ? +1 : 0; | |
132 | switch (op) { | |
133 | case Py_LT: b = b < 0; break; | |
134 | case Py_LE: b = b <= 0; break; | |
135 | case Py_GE: b = b >= 0; break; | |
136 | case Py_GT: b = b > 0; break; | |
137 | default: abort(); | |
138 | } | |
139 | } | |
140 | if (b) RETURN_TRUE; | |
141 | else RETURN_FALSE; | |
142 | } | |
143 | ||
bb5c23a4 MW |
144 | static PyObject *bytestring_pyconcat(PyObject *x, PyObject *y) |
145 | { | |
146 | const void *xv; Py_ssize_t xsz; | |
147 | const void *yv; Py_ssize_t ysz; | |
148 | PyObject *z = 0; char *zp; size_t zsz; | |
149 | ||
150 | if (PyObject_AsReadBuffer(x, &xv, &xsz) || | |
151 | PyObject_AsReadBuffer(y, &yv, &ysz)) | |
152 | goto end; | |
153 | zsz = (size_t)xsz + (size_t)ysz; | |
154 | if (xsz < 0 || ysz < 0 || zsz < xsz) VALERR("too long"); | |
155 | z = bytestring_pywrap(0, zsz); zp = PyString_AS_STRING(z); | |
156 | memcpy(zp, xv, xsz); memcpy(zp + xsz, yv, ysz); | |
157 | end: | |
158 | return (z); | |
159 | } | |
160 | ||
161 | static PyObject *bytestring_pyrepeat(PyObject *me, Py_ssize_t n) | |
162 | { | |
163 | const unsigned char *xp; size_t xsz; | |
164 | PyObject *z = 0; char *zp; size_t zsz; | |
165 | ||
166 | xp = (const unsigned char *)PyString_AS_STRING(me); | |
167 | xsz = PyString_GET_SIZE(me); | |
e8a6a67b | 168 | if (n < 0 || (n && xsz >= (size_t)-1/n)) VALERR("too long"); |
bb5c23a4 MW |
169 | zsz = n*xsz; z = bytestring_pywrap(0, zsz); zp = PyString_AS_STRING(z); |
170 | if (xsz == 1) memset(zp, *xp, zsz); | |
171 | else while (zsz) { memcpy(zp, xp, xsz); zp += xsz; zsz -= xsz; } | |
172 | end: | |
173 | return (z); | |
174 | } | |
175 | ||
176 | static PyObject *bytestring_pyitem(PyObject *me, Py_ssize_t i) | |
177 | { | |
178 | PyObject *rc = 0; | |
179 | ||
180 | if (i < 0 || i >= PyString_GET_SIZE(me)) IXERR("out of range"); | |
181 | rc = bytestring_pywrap(PyString_AS_STRING(me) + i, 1); | |
182 | end: | |
183 | return (rc); | |
184 | } | |
185 | ||
186 | static PyObject *bytestring_pyslice(PyObject *me, Py_ssize_t i, Py_ssize_t j) | |
187 | { | |
188 | PyObject *rc = 0; | |
189 | size_t n = PyString_GET_SIZE(me); | |
190 | ||
191 | if (i < 0) i = 0; | |
192 | if (j < 0) j = 0; | |
193 | else if (j > n) j = n; | |
194 | if (j < i) i = j = 0; | |
195 | if (i == 0 && j == n && me->ob_type == bytestring_pytype) | |
196 | { Py_INCREF(me); rc = me; goto end; } | |
197 | rc = bytestring_pywrap(PyString_AS_STRING(me) + i, j - i); | |
198 | end: | |
199 | return (rc); | |
200 | } | |
201 | ||
202 | static PyObject *bytestring_pysubscript(PyObject *me, PyObject *ix) | |
203 | { | |
204 | Py_ssize_t i, j, k, n; | |
205 | const unsigned char *p; | |
206 | unsigned char *q; | |
207 | PyObject *rc = 0; | |
208 | ||
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 += PyString_GET_SIZE(me); | |
213 | rc = bytestring_pyitem(me, i); | |
214 | } else if (PySlice_Check(ix)) { | |
215 | if (PySlice_GetIndicesEx((PySliceObject *)ix, PyString_GET_SIZE(me), | |
216 | &i, &j, &k, &n)) | |
217 | return (0); | |
218 | if (k == 1) return bytestring_pyslice(me, i, j); | |
219 | rc = bytestring_pywrap(0, n); | |
220 | p = (unsigned char *)PyString_AS_STRING(me) + i; | |
221 | q = (unsigned char *)PyString_AS_STRING(rc); | |
222 | while (n--) { *q++ = *p; p += k; } | |
223 | } else | |
224 | TYERR("wanted integer or slice"); | |
225 | end: | |
226 | return (rc); | |
227 | } | |
228 | ||
d7ab1bab | 229 | #define BINOP(name, op) \ |
230 | static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \ | |
231 | const void *xv, *yv; \ | |
232 | const unsigned char *xp, *yp; \ | |
233 | unsigned char *zp; \ | |
87d705a8 | 234 | Py_ssize_t xsz, ysz; \ |
d7ab1bab | 235 | int i; \ |
236 | PyObject *rc = 0; \ | |
237 | if (PyObject_AsReadBuffer(x, &xv, &xsz) || \ | |
238 | PyObject_AsReadBuffer(y, &yv, &ysz)) \ | |
239 | goto end; \ | |
240 | if (xsz != ysz) VALERR("length mismatch"); \ | |
241 | rc = bytestring_pywrap(0, xsz); \ | |
242 | xp = xv; yp = yv; zp = (unsigned char *)PyString_AS_STRING(rc); \ | |
243 | for (i = xsz; i > 0; i--) *zp++ = *xp++ op *yp++; \ | |
244 | end: \ | |
245 | return (rc); \ | |
246 | } | |
247 | BINOP(and, &) | |
248 | BINOP(or, |) | |
249 | BINOP(xor, ^) | |
250 | ||
251 | #define UNOP(name, op) \ | |
252 | static PyObject *bytestring_py##name(PyObject *x) { \ | |
253 | const void *xv; \ | |
254 | const unsigned char *xp; \ | |
255 | unsigned char *zp; \ | |
87d705a8 | 256 | Py_ssize_t xsz; \ |
d7ab1bab | 257 | int i; \ |
258 | PyObject *rc = 0; \ | |
259 | if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end; \ | |
260 | rc = bytestring_pywrap(0, xsz); \ | |
261 | xp = xv; zp = (unsigned char *)PyString_AS_STRING(rc); \ | |
262 | for (i = xsz; i > 0; i--) *zp++ = op *xp++; \ | |
263 | end: \ | |
264 | return (rc); \ | |
265 | } | |
266 | UNOP(not, ~) | |
267 | ||
a157093f MW |
268 | static const PyMethodDef bytestring_pymethods[] = { |
269 | #define METHNAME(name) bymeth_##name | |
270 | SMTH (zero, "zero(N) -> 0000...00") | |
271 | #undef METHNAME | |
272 | { 0 } | |
273 | }; | |
274 | ||
c90f712e | 275 | static const PyNumberMethods bytestring_pynumber = { |
d7ab1bab | 276 | 0, /* @nb_add@ */ |
277 | 0, /* @nb_subtract@ */ | |
278 | 0, /* @nb_multiply@ */ | |
279 | 0, /* @nb_divide@ */ | |
280 | 0, /* @nb_remainder@ */ | |
281 | 0, /* @nb_divmod@ */ | |
282 | 0, /* @nb_power@ */ | |
283 | 0, /* @nb_negative@ */ | |
284 | 0, /* @nb_positive@ */ | |
285 | 0, /* @nb_absolute@ */ | |
286 | 0, /* @nb_nonzero@ */ | |
287 | bytestring_pynot, /* @nb_invert@ */ | |
288 | 0, /* @nb_lshift@ */ | |
289 | 0, /* @nb_rshift@ */ | |
290 | bytestring_pyand, /* @nb_and@ */ | |
291 | bytestring_pyxor, /* @nb_xor@ */ | |
292 | bytestring_pyor, /* @nb_or@ */ | |
293 | 0, /* @nb_coerce@ */ | |
294 | 0, /* @nb_int@ */ | |
295 | 0, /* @nb_long@ */ | |
296 | 0, /* @nb_float@ */ | |
297 | 0, /* @nb_oct@ */ | |
298 | 0, /* @nb_hex@ */ | |
299 | }; | |
300 | ||
c90f712e | 301 | static const PySequenceMethods bytestring_pysequence = { |
bb5c23a4 MW |
302 | 0, /* @sq_length@ */ |
303 | bytestring_pyconcat, /* @sq_concat@ */ | |
304 | bytestring_pyrepeat, /* @sq_repeat@ */ | |
305 | bytestring_pyitem, /* @sq_item@ */ | |
306 | bytestring_pyslice, /* @sq_slice@ */ | |
307 | 0, /* @sq_ass_item@ */ | |
308 | 0, /* @sq_ass_slice@ */ | |
309 | 0, /* @sq_contains@ */ | |
310 | 0, /* @sq_inplace_concat@ */ | |
311 | 0, /* @sq_inplace_repeat@ */ | |
312 | }; | |
313 | ||
c90f712e | 314 | static const PyMappingMethods bytestring_pymapping = { |
bb5c23a4 MW |
315 | 0, /* @mp_length@ */ |
316 | bytestring_pysubscript, /* @mp_subscript@ */ | |
317 | 0, /* @mp_ass_subscript@ */ | |
318 | }; | |
319 | ||
c263b05c | 320 | static const PyTypeObject bytestring_pytype_skel = { |
6d4db0bf | 321 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 322 | "ByteString", /* @tp_name@ */ |
d7ab1bab | 323 | 0, /* @tp_basicsize@ */ |
324 | 0, /* @tp_itemsize@ */ | |
325 | ||
326 | 0, /* @tp_dealloc@ */ | |
327 | 0, /* @tp_print@ */ | |
328 | 0, /* @tp_getattr@ */ | |
329 | 0, /* @tp_setattr@ */ | |
330 | 0, /* @tp_compare@ */ | |
331 | 0, /* @tp_repr@ */ | |
c90f712e MW |
332 | PYNUMBER(bytestring), /* @tp_as_number@ */ |
333 | PYSEQUENCE(bytestring), /* @tp_as_sequence@ */ | |
334 | PYMAPPING(bytestring), /* @tp_as_mapping@ */ | |
d7ab1bab | 335 | 0, /* @tp_hash@ */ |
336 | 0, /* @tp_call@ */ | |
337 | 0, /* @tp_str@ */ | |
338 | 0, /* @tp_getattro@ */ | |
339 | 0, /* @tp_setattro@ */ | |
8d2f9896 | 340 | 0, /* @tp_as_buffer@ */ |
d7ab1bab | 341 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
342 | Py_TPFLAGS_CHECKTYPES | | |
343 | Py_TPFLAGS_BASETYPE, | |
344 | ||
345 | /* @tp_doc@ */ | |
d96c882e | 346 | "ByteString(STR): byte string class.", |
d7ab1bab | 347 | |
348 | 0, /* @tp_traverse@ */ | |
349 | 0, /* @tp_clear@ */ | |
bfb450cc | 350 | bytestring_pyrichcompare, /* @tp_richcompare@ */ |
d7ab1bab | 351 | 0, /* @tp_weaklistoffset@ */ |
352 | 0, /* @tp_iter@ */ | |
963a6148 | 353 | 0, /* @tp_iternext@ */ |
a157093f | 354 | PYMETHODS(bytestring), /* @tp_methods@ */ |
d7ab1bab | 355 | 0, /* @tp_members@ */ |
356 | 0, /* @tp_getset@ */ | |
357 | 0, /* @tp_base@ */ | |
358 | 0, /* @tp_dict@ */ | |
359 | 0, /* @tp_descr_get@ */ | |
360 | 0, /* @tp_descr_set@ */ | |
361 | 0, /* @tp_dictoffset@ */ | |
362 | 0, /* @tp_init@ */ | |
363 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
46e6ad89 | 364 | bytestring_pynew, /* @tp_new@ */ |
3aa33042 | 365 | 0, /* @tp_free@ */ |
d7ab1bab | 366 | 0 /* @tp_is_gc@ */ |
367 | }; | |
368 | ||
369 | /*----- Initialization ----------------------------------------------------*/ | |
370 | ||
c90f712e | 371 | static const PyMethodDef methods[] = { |
bfb450cc | 372 | #define METHNAME(func) meth_##func |
d96c882e | 373 | METH (ctstreq, "ctstreq(S, T) -> BOOL") |
bfb450cc MW |
374 | #undef METHNAME |
375 | { 0 } | |
376 | }; | |
377 | ||
d7ab1bab | 378 | #define string_pytype &PyString_Type |
379 | void bytestring_pyinit(void) | |
380 | { | |
381 | INITTYPE(bytestring, string); | |
bfb450cc | 382 | addmethods(methods); |
d7ab1bab | 383 | } |
384 | ||
385 | void bytestring_pyinsert(PyObject *mod) | |
386 | { | |
387 | INSERT("ByteString", bytestring_pytype); | |
388 | } | |
389 | ||
390 | /*----- That's all, folks -------------------------------------------------*/ |