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