Initial check-in of catacomb-python.
[catacomb-python] / bytestring.c
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 #ifdef INTERN_STRINGS
46 x->ob_sinterned = NULL;
47 #endif
48 return ((PyObject *)x);
49 }
50
51 PyObject *bytestring_pywrapbuf(buf *b)
52 { return bytestring_pywrap(BCUR(b), BLEFT(b)); }
53
54 #define BINOP(name, op) \
55 static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \
56 const void *xv, *yv; \
57 const unsigned char *xp, *yp; \
58 unsigned char *zp; \
59 int xsz, ysz; \
60 int i; \
61 PyObject *rc = 0; \
62 if (PyObject_AsReadBuffer(x, &xv, &xsz) || \
63 PyObject_AsReadBuffer(y, &yv, &ysz)) \
64 goto end; \
65 if (xsz != ysz) VALERR("length mismatch"); \
66 rc = bytestring_pywrap(0, xsz); \
67 xp = xv; yp = yv; zp = (unsigned char *)PyString_AS_STRING(rc); \
68 for (i = xsz; i > 0; i--) *zp++ = *xp++ op *yp++; \
69 end: \
70 return (rc); \
71 }
72 BINOP(and, &)
73 BINOP(or, |)
74 BINOP(xor, ^)
75
76 #define UNOP(name, op) \
77 static PyObject *bytestring_py##name(PyObject *x) { \
78 const void *xv; \
79 const unsigned char *xp; \
80 unsigned char *zp; \
81 int xsz; \
82 int i; \
83 PyObject *rc = 0; \
84 if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end; \
85 rc = bytestring_pywrap(0, xsz); \
86 xp = xv; zp = (unsigned char *)PyString_AS_STRING(rc); \
87 for (i = xsz; i > 0; i--) *zp++ = op *xp++; \
88 end: \
89 return (rc); \
90 }
91 UNOP(not, ~)
92
93 static PyNumberMethods bytestring_pynumber = {
94 0, /* @nb_add@ */
95 0, /* @nb_subtract@ */
96 0, /* @nb_multiply@ */
97 0, /* @nb_divide@ */
98 0, /* @nb_remainder@ */
99 0, /* @nb_divmod@ */
100 0, /* @nb_power@ */
101 0, /* @nb_negative@ */
102 0, /* @nb_positive@ */
103 0, /* @nb_absolute@ */
104 0, /* @nb_nonzero@ */
105 bytestring_pynot, /* @nb_invert@ */
106 0, /* @nb_lshift@ */
107 0, /* @nb_rshift@ */
108 bytestring_pyand, /* @nb_and@ */
109 bytestring_pyxor, /* @nb_xor@ */
110 bytestring_pyor, /* @nb_or@ */
111 0, /* @nb_coerce@ */
112 0, /* @nb_int@ */
113 0, /* @nb_long@ */
114 0, /* @nb_float@ */
115 0, /* @nb_oct@ */
116 0, /* @nb_hex@ */
117 };
118
119 static PyBufferProcs bytestring_pybuffer;
120
121 static PyTypeObject bytestring_pytype_skel = {
122 PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */
123 "catacomb.ByteString", /* @tp_name@ */
124 0, /* @tp_basicsize@ */
125 0, /* @tp_itemsize@ */
126
127 0, /* @tp_dealloc@ */
128 0, /* @tp_print@ */
129 0, /* @tp_getattr@ */
130 0, /* @tp_setattr@ */
131 0, /* @tp_compare@ */
132 0, /* @tp_repr@ */
133 &bytestring_pynumber, /* @tp_as_number@ */
134 0, /* @tp_as_sequence@ */
135 0, /* @tp_as_mapping@ */
136 0, /* @tp_hash@ */
137 0, /* @tp_call@ */
138 0, /* @tp_str@ */
139 0, /* @tp_getattro@ */
140 0, /* @tp_setattro@ */
141 &bytestring_pybuffer, /* @tp_as_buffer@ */
142 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
143 Py_TPFLAGS_CHECKTYPES |
144 Py_TPFLAGS_BASETYPE,
145
146 /* @tp_doc@ */
147 "Byte string class.",
148
149 0, /* @tp_traverse@ */
150 0, /* @tp_clear@ */
151 0, /* @tp_richcompare@ */
152 0, /* @tp_weaklistoffset@ */
153 0, /* @tp_iter@ */
154 0, /* @tp_iternexr@ */
155 0, /* @tp_methods@ */
156 0, /* @tp_members@ */
157 0, /* @tp_getset@ */
158 0, /* @tp_base@ */
159 0, /* @tp_dict@ */
160 0, /* @tp_descr_get@ */
161 0, /* @tp_descr_set@ */
162 0, /* @tp_dictoffset@ */
163 0, /* @tp_init@ */
164 PyType_GenericAlloc, /* @tp_alloc@ */
165 0, /* @tp_new@ */
166 _PyObject_Del, /* @tp_free@ */
167 0 /* @tp_is_gc@ */
168 };
169
170 /*----- Initialization ----------------------------------------------------*/
171
172 #define string_pytype &PyString_Type
173 void bytestring_pyinit(void)
174 {
175 INITTYPE(bytestring, string);
176 }
177
178 void bytestring_pyinsert(PyObject *mod)
179 {
180 INSERT("ByteString", bytestring_pytype);
181 }
182
183 /*----- That's all, folks -------------------------------------------------*/