| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Byte strings |
| 6 | * |
| 7 | * (c) 2004 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of the Python interface to Catacomb. |
| 13 | * |
| 14 | * Catacomb/Python is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb/Python is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with Catacomb/Python; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Header files ------------------------------------------------------*/ |
| 30 | |
| 31 | #include "catacomb-python.h" |
| 32 | |
| 33 | /*----- Main code ---------------------------------------------------------*/ |
| 34 | |
| 35 | PyTypeObject *bytestring_pytype; |
| 36 | |
| 37 | PyObject *bytestring_pywrap(const void *p, size_t n) |
| 38 | { |
| 39 | PyStringObject *x = PyObject_NewVar(PyStringObject, bytestring_pytype, n); |
| 40 | if (p) memcpy(x->ob_sval, p, n); |
| 41 | x->ob_sval[n] = 0; |
| 42 | #ifdef CACHE_HASH |
| 43 | x->ob_shash = -1; |
| 44 | #endif |
| 45 | x->ob_sstate = SSTATE_NOT_INTERNED; |
| 46 | return ((PyObject *)x); |
| 47 | } |
| 48 | |
| 49 | PyObject *bytestring_pywrapbuf(buf *b) |
| 50 | { return bytestring_pywrap(BCUR(b), BLEFT(b)); } |
| 51 | |
| 52 | #define BINOP(name, op) \ |
| 53 | static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \ |
| 54 | const void *xv, *yv; \ |
| 55 | const unsigned char *xp, *yp; \ |
| 56 | unsigned char *zp; \ |
| 57 | int xsz, ysz; \ |
| 58 | int i; \ |
| 59 | PyObject *rc = 0; \ |
| 60 | if (PyObject_AsReadBuffer(x, &xv, &xsz) || \ |
| 61 | PyObject_AsReadBuffer(y, &yv, &ysz)) \ |
| 62 | goto end; \ |
| 63 | if (xsz != ysz) VALERR("length mismatch"); \ |
| 64 | rc = bytestring_pywrap(0, xsz); \ |
| 65 | xp = xv; yp = yv; zp = (unsigned char *)PyString_AS_STRING(rc); \ |
| 66 | for (i = xsz; i > 0; i--) *zp++ = *xp++ op *yp++; \ |
| 67 | end: \ |
| 68 | return (rc); \ |
| 69 | } |
| 70 | BINOP(and, &) |
| 71 | BINOP(or, |) |
| 72 | BINOP(xor, ^) |
| 73 | |
| 74 | #define UNOP(name, op) \ |
| 75 | static PyObject *bytestring_py##name(PyObject *x) { \ |
| 76 | const void *xv; \ |
| 77 | const unsigned char *xp; \ |
| 78 | unsigned char *zp; \ |
| 79 | int xsz; \ |
| 80 | int i; \ |
| 81 | PyObject *rc = 0; \ |
| 82 | if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end; \ |
| 83 | rc = bytestring_pywrap(0, xsz); \ |
| 84 | xp = xv; zp = (unsigned char *)PyString_AS_STRING(rc); \ |
| 85 | for (i = xsz; i > 0; i--) *zp++ = op *xp++; \ |
| 86 | end: \ |
| 87 | return (rc); \ |
| 88 | } |
| 89 | UNOP(not, ~) |
| 90 | |
| 91 | static PyNumberMethods bytestring_pynumber = { |
| 92 | 0, /* @nb_add@ */ |
| 93 | 0, /* @nb_subtract@ */ |
| 94 | 0, /* @nb_multiply@ */ |
| 95 | 0, /* @nb_divide@ */ |
| 96 | 0, /* @nb_remainder@ */ |
| 97 | 0, /* @nb_divmod@ */ |
| 98 | 0, /* @nb_power@ */ |
| 99 | 0, /* @nb_negative@ */ |
| 100 | 0, /* @nb_positive@ */ |
| 101 | 0, /* @nb_absolute@ */ |
| 102 | 0, /* @nb_nonzero@ */ |
| 103 | bytestring_pynot, /* @nb_invert@ */ |
| 104 | 0, /* @nb_lshift@ */ |
| 105 | 0, /* @nb_rshift@ */ |
| 106 | bytestring_pyand, /* @nb_and@ */ |
| 107 | bytestring_pyxor, /* @nb_xor@ */ |
| 108 | bytestring_pyor, /* @nb_or@ */ |
| 109 | 0, /* @nb_coerce@ */ |
| 110 | 0, /* @nb_int@ */ |
| 111 | 0, /* @nb_long@ */ |
| 112 | 0, /* @nb_float@ */ |
| 113 | 0, /* @nb_oct@ */ |
| 114 | 0, /* @nb_hex@ */ |
| 115 | }; |
| 116 | |
| 117 | static PyBufferProcs bytestring_pybuffer; |
| 118 | |
| 119 | static PyTypeObject bytestring_pytype_skel = { |
| 120 | PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ |
| 121 | "catacomb.ByteString", /* @tp_name@ */ |
| 122 | 0, /* @tp_basicsize@ */ |
| 123 | 0, /* @tp_itemsize@ */ |
| 124 | |
| 125 | 0, /* @tp_dealloc@ */ |
| 126 | 0, /* @tp_print@ */ |
| 127 | 0, /* @tp_getattr@ */ |
| 128 | 0, /* @tp_setattr@ */ |
| 129 | 0, /* @tp_compare@ */ |
| 130 | 0, /* @tp_repr@ */ |
| 131 | &bytestring_pynumber, /* @tp_as_number@ */ |
| 132 | 0, /* @tp_as_sequence@ */ |
| 133 | 0, /* @tp_as_mapping@ */ |
| 134 | 0, /* @tp_hash@ */ |
| 135 | 0, /* @tp_call@ */ |
| 136 | 0, /* @tp_str@ */ |
| 137 | 0, /* @tp_getattro@ */ |
| 138 | 0, /* @tp_setattro@ */ |
| 139 | &bytestring_pybuffer, /* @tp_as_buffer@ */ |
| 140 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 141 | Py_TPFLAGS_CHECKTYPES | |
| 142 | Py_TPFLAGS_BASETYPE, |
| 143 | |
| 144 | /* @tp_doc@ */ |
| 145 | "Byte string class.", |
| 146 | |
| 147 | 0, /* @tp_traverse@ */ |
| 148 | 0, /* @tp_clear@ */ |
| 149 | 0, /* @tp_richcompare@ */ |
| 150 | 0, /* @tp_weaklistoffset@ */ |
| 151 | 0, /* @tp_iter@ */ |
| 152 | 0, /* @tp_iternexr@ */ |
| 153 | 0, /* @tp_methods@ */ |
| 154 | 0, /* @tp_members@ */ |
| 155 | 0, /* @tp_getset@ */ |
| 156 | 0, /* @tp_base@ */ |
| 157 | 0, /* @tp_dict@ */ |
| 158 | 0, /* @tp_descr_get@ */ |
| 159 | 0, /* @tp_descr_set@ */ |
| 160 | 0, /* @tp_dictoffset@ */ |
| 161 | 0, /* @tp_init@ */ |
| 162 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 163 | 0, /* @tp_new@ */ |
| 164 | 0, /* @tp_free@ */ |
| 165 | 0 /* @tp_is_gc@ */ |
| 166 | }; |
| 167 | |
| 168 | /*----- Initialization ----------------------------------------------------*/ |
| 169 | |
| 170 | #define string_pytype &PyString_Type |
| 171 | void bytestring_pyinit(void) |
| 172 | { |
| 173 | INITTYPE(bytestring, string); |
| 174 | } |
| 175 | |
| 176 | void bytestring_pyinsert(PyObject *mod) |
| 177 | { |
| 178 | INSERT("ByteString", bytestring_pytype); |
| 179 | } |
| 180 | |
| 181 | /*----- That's all, folks -------------------------------------------------*/ |