Commit | Line | Data |
---|---|---|
d7ab1bab | 1 | /* -*-c-*- |
d7ab1bab | 2 | * |
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 | ||
33 | PyTypeObject *bytestring_pytype; | |
34 | ||
46e6ad89 | 35 | static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n) |
d7ab1bab | 36 | { |
46e6ad89 | 37 | PyStringObject *x = (PyStringObject *)ty->tp_alloc(ty, n); |
d7ab1bab | 38 | if (p) memcpy(x->ob_sval, p, n); |
39 | x->ob_sval[n] = 0; | |
69e13768 | 40 | #if defined(CACHE_HASH) || PY_VERSION_HEX >= 0x02030000 |
d7ab1bab | 41 | x->ob_shash = -1; |
42 | #endif | |
3aa33042 | 43 | x->ob_sstate = SSTATE_NOT_INTERNED; |
d7ab1bab | 44 | return ((PyObject *)x); |
45 | } | |
46 | ||
46e6ad89 | 47 | PyObject *bytestring_pywrap(const void *p, size_t n) |
48 | { return (dowrap(bytestring_pytype, p, n)); } | |
49 | ||
d7ab1bab | 50 | PyObject *bytestring_pywrapbuf(buf *b) |
46e6ad89 | 51 | { return (dowrap(bytestring_pytype, BCUR(b), BLEFT(b))); } |
52 | ||
53 | static PyObject *bytestring_pynew(PyTypeObject *ty, | |
54 | PyObject *arg, PyObject *kw) | |
55 | { | |
56 | const char *p; | |
6b54260d | 57 | Py_ssize_t n; |
46e6ad89 | 58 | static char *kwlist[] = { "data", 0 }; |
59 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &p, &n)) | |
60 | return (0); | |
61 | return (dowrap(ty, p, n)); | |
62 | } | |
d7ab1bab | 63 | |
bfb450cc MW |
64 | static PyObject *meth_ctstreq(PyObject *me, PyObject *args) |
65 | { | |
66 | char *p, *q; | |
67 | Py_ssize_t psz, qsz; | |
68 | if (!PyArg_ParseTuple(args, "s#s#:ctstreq", &p, &psz, &q, &qsz)) | |
69 | goto end; | |
70 | if (psz == qsz && ct_memeq(p, q, psz)) RETURN_TRUE; | |
71 | else RETURN_FALSE; | |
72 | end: | |
73 | return (0); | |
74 | } | |
75 | ||
76 | static PyObject *bytestring_pyrichcompare(PyObject *me, | |
77 | PyObject *you, int op) | |
78 | { | |
79 | int b; | |
80 | void *mystr, *yourstr; | |
81 | Py_ssize_t mylen, yourlen, minlen; | |
82 | ||
83 | if (!PyString_Check(me) || !PyString_Check(you)) RETURN_NOTIMPL; | |
84 | mystr = PyString_AS_STRING(me); mylen = PyString_GET_SIZE(me); | |
85 | yourstr = PyString_AS_STRING(you); yourlen = PyString_GET_SIZE(you); | |
86 | ||
87 | switch (op) { | |
88 | case Py_EQ: | |
89 | b = mylen == yourlen && ct_memeq(mystr, yourstr, mylen); | |
90 | break; | |
91 | case Py_NE: | |
92 | b = mylen != yourlen || !ct_memeq(mystr, yourstr, mylen); | |
93 | break; | |
94 | default: | |
95 | minlen = mylen < yourlen ? mylen : yourlen; | |
96 | b = memcmp(mystr, yourstr, minlen); | |
97 | if (!b) b = mylen < yourlen ? -1 : mylen > yourlen ? +1 : 0; | |
98 | switch (op) { | |
99 | case Py_LT: b = b < 0; break; | |
100 | case Py_LE: b = b <= 0; break; | |
101 | case Py_GE: b = b >= 0; break; | |
102 | case Py_GT: b = b > 0; break; | |
103 | default: abort(); | |
104 | } | |
105 | } | |
106 | if (b) RETURN_TRUE; | |
107 | else RETURN_FALSE; | |
108 | } | |
109 | ||
d7ab1bab | 110 | #define BINOP(name, op) \ |
111 | static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \ | |
112 | const void *xv, *yv; \ | |
113 | const unsigned char *xp, *yp; \ | |
114 | unsigned char *zp; \ | |
87d705a8 | 115 | Py_ssize_t xsz, ysz; \ |
d7ab1bab | 116 | int i; \ |
117 | PyObject *rc = 0; \ | |
118 | if (PyObject_AsReadBuffer(x, &xv, &xsz) || \ | |
119 | PyObject_AsReadBuffer(y, &yv, &ysz)) \ | |
120 | goto end; \ | |
121 | if (xsz != ysz) VALERR("length mismatch"); \ | |
122 | rc = bytestring_pywrap(0, xsz); \ | |
123 | xp = xv; yp = yv; zp = (unsigned char *)PyString_AS_STRING(rc); \ | |
124 | for (i = xsz; i > 0; i--) *zp++ = *xp++ op *yp++; \ | |
125 | end: \ | |
126 | return (rc); \ | |
127 | } | |
128 | BINOP(and, &) | |
129 | BINOP(or, |) | |
130 | BINOP(xor, ^) | |
131 | ||
132 | #define UNOP(name, op) \ | |
133 | static PyObject *bytestring_py##name(PyObject *x) { \ | |
134 | const void *xv; \ | |
135 | const unsigned char *xp; \ | |
136 | unsigned char *zp; \ | |
87d705a8 | 137 | Py_ssize_t xsz; \ |
d7ab1bab | 138 | int i; \ |
139 | PyObject *rc = 0; \ | |
140 | if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end; \ | |
141 | rc = bytestring_pywrap(0, xsz); \ | |
142 | xp = xv; zp = (unsigned char *)PyString_AS_STRING(rc); \ | |
143 | for (i = xsz; i > 0; i--) *zp++ = op *xp++; \ | |
144 | end: \ | |
145 | return (rc); \ | |
146 | } | |
147 | UNOP(not, ~) | |
148 | ||
149 | static PyNumberMethods bytestring_pynumber = { | |
150 | 0, /* @nb_add@ */ | |
151 | 0, /* @nb_subtract@ */ | |
152 | 0, /* @nb_multiply@ */ | |
153 | 0, /* @nb_divide@ */ | |
154 | 0, /* @nb_remainder@ */ | |
155 | 0, /* @nb_divmod@ */ | |
156 | 0, /* @nb_power@ */ | |
157 | 0, /* @nb_negative@ */ | |
158 | 0, /* @nb_positive@ */ | |
159 | 0, /* @nb_absolute@ */ | |
160 | 0, /* @nb_nonzero@ */ | |
161 | bytestring_pynot, /* @nb_invert@ */ | |
162 | 0, /* @nb_lshift@ */ | |
163 | 0, /* @nb_rshift@ */ | |
164 | bytestring_pyand, /* @nb_and@ */ | |
165 | bytestring_pyxor, /* @nb_xor@ */ | |
166 | bytestring_pyor, /* @nb_or@ */ | |
167 | 0, /* @nb_coerce@ */ | |
168 | 0, /* @nb_int@ */ | |
169 | 0, /* @nb_long@ */ | |
170 | 0, /* @nb_float@ */ | |
171 | 0, /* @nb_oct@ */ | |
172 | 0, /* @nb_hex@ */ | |
173 | }; | |
174 | ||
175 | static PyBufferProcs bytestring_pybuffer; | |
176 | ||
177 | static PyTypeObject bytestring_pytype_skel = { | |
6d4db0bf | 178 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 179 | "ByteString", /* @tp_name@ */ |
d7ab1bab | 180 | 0, /* @tp_basicsize@ */ |
181 | 0, /* @tp_itemsize@ */ | |
182 | ||
183 | 0, /* @tp_dealloc@ */ | |
184 | 0, /* @tp_print@ */ | |
185 | 0, /* @tp_getattr@ */ | |
186 | 0, /* @tp_setattr@ */ | |
187 | 0, /* @tp_compare@ */ | |
188 | 0, /* @tp_repr@ */ | |
189 | &bytestring_pynumber, /* @tp_as_number@ */ | |
190 | 0, /* @tp_as_sequence@ */ | |
191 | 0, /* @tp_as_mapping@ */ | |
192 | 0, /* @tp_hash@ */ | |
193 | 0, /* @tp_call@ */ | |
194 | 0, /* @tp_str@ */ | |
195 | 0, /* @tp_getattro@ */ | |
196 | 0, /* @tp_setattro@ */ | |
197 | &bytestring_pybuffer, /* @tp_as_buffer@ */ | |
198 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
199 | Py_TPFLAGS_CHECKTYPES | | |
200 | Py_TPFLAGS_BASETYPE, | |
201 | ||
202 | /* @tp_doc@ */ | |
203 | "Byte string class.", | |
204 | ||
205 | 0, /* @tp_traverse@ */ | |
206 | 0, /* @tp_clear@ */ | |
bfb450cc | 207 | bytestring_pyrichcompare, /* @tp_richcompare@ */ |
d7ab1bab | 208 | 0, /* @tp_weaklistoffset@ */ |
209 | 0, /* @tp_iter@ */ | |
963a6148 | 210 | 0, /* @tp_iternext@ */ |
d7ab1bab | 211 | 0, /* @tp_methods@ */ |
212 | 0, /* @tp_members@ */ | |
213 | 0, /* @tp_getset@ */ | |
214 | 0, /* @tp_base@ */ | |
215 | 0, /* @tp_dict@ */ | |
216 | 0, /* @tp_descr_get@ */ | |
217 | 0, /* @tp_descr_set@ */ | |
218 | 0, /* @tp_dictoffset@ */ | |
219 | 0, /* @tp_init@ */ | |
220 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
46e6ad89 | 221 | bytestring_pynew, /* @tp_new@ */ |
3aa33042 | 222 | 0, /* @tp_free@ */ |
d7ab1bab | 223 | 0 /* @tp_is_gc@ */ |
224 | }; | |
225 | ||
226 | /*----- Initialization ----------------------------------------------------*/ | |
227 | ||
bfb450cc MW |
228 | static PyMethodDef methods[] = { |
229 | #define METHNAME(func) meth_##func | |
230 | METH (ctstreq, "ctstreq(S, T) -> BOOL") | |
231 | #undef METHNAME | |
232 | { 0 } | |
233 | }; | |
234 | ||
d7ab1bab | 235 | #define string_pytype &PyString_Type |
236 | void bytestring_pyinit(void) | |
237 | { | |
238 | INITTYPE(bytestring, string); | |
bfb450cc | 239 | addmethods(methods); |
d7ab1bab | 240 | } |
241 | ||
242 | void bytestring_pyinsert(PyObject *mod) | |
243 | { | |
244 | INSERT("ByteString", bytestring_pytype); | |
245 | } | |
246 | ||
247 | /*----- That's all, folks -------------------------------------------------*/ |