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