chiark / gitweb /
bytestring.c (dowrap): Factor out allocating the bytestring object.
[catacomb-python] / bytestring.c
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 *allocate(PyTypeObject *ty, size_t n)
36 {
37   PyStringObject *x;
38   x = (PyStringObject *)ty->tp_alloc(ty, 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 static 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
56 PyObject *bytestring_pywrap(const void *p, size_t n)
57   { return (dowrap(bytestring_pytype, p, n)); }
58
59 PyObject *bytestring_pywrapbuf(buf *b)
60   { return (dowrap(bytestring_pytype, BCUR(b), BLEFT(b))); }
61
62 static PyObject *bytestring_pynew(PyTypeObject *ty,
63                                   PyObject *arg, PyObject *kw)
64 {
65   const char *p;
66   Py_ssize_t n;
67   static const char *const kwlist[] = { "data", 0 };
68   if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &p, &n))
69     return (0);
70   return (dowrap(ty, p, n));
71 }
72
73 static PyObject *meth_ctstreq(PyObject *me, PyObject *arg)
74 {
75   char *p, *q;
76   Py_ssize_t psz, qsz;
77   if (!PyArg_ParseTuple(arg, "s#s#:ctstreq", &p, &psz, &q, &qsz))
78     goto end;
79   if (psz == qsz && ct_memeq(p, q, psz)) RETURN_TRUE;
80   else RETURN_FALSE;
81 end:
82   return (0);
83 }
84
85 static 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);
92 end:
93   return (rc);
94 }
95
96 static 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
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;                                                  \
135     Py_ssize_t xsz, ysz;                                                \
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   }
148 BINOP(and, &)
149 BINOP(or, |)
150 BINOP(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;                                                  \
157     Py_ssize_t xsz;                                                     \
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   }
167 UNOP(not, ~)
168
169 static 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
195 static PyBufferProcs bytestring_pybuffer;
196
197 static PyTypeObject bytestring_pytype_skel = {
198   PyObject_HEAD_INIT(0) 0,              /* Header */
199   "ByteString",                         /* @tp_name@ */
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@ */
223 "ByteString(STR): byte string class.",
224
225   0,                                    /* @tp_traverse@ */
226   0,                                    /* @tp_clear@ */
227   bytestring_pyrichcompare,             /* @tp_richcompare@ */
228   0,                                    /* @tp_weaklistoffset@ */
229   0,                                    /* @tp_iter@ */
230   0,                                    /* @tp_iternext@ */
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@ */
241   bytestring_pynew,                     /* @tp_new@ */
242   0,                                    /* @tp_free@ */
243   0                                     /* @tp_is_gc@ */
244 };
245
246 /*----- Initialization ----------------------------------------------------*/
247
248 static PyMethodDef methods[] = {
249 #define METHNAME(func) meth_##func
250   METH  (ctstreq,               "ctstreq(S, T) -> BOOL")
251   METH  (_ByteString_zero,      "zero(N) -> 0000...00")
252 #undef METHNAME
253   { 0 }
254 };
255
256 #define string_pytype &PyString_Type
257 void bytestring_pyinit(void)
258 {
259   INITTYPE(bytestring, string);
260   addmethods(methods);
261 }
262
263 void bytestring_pyinsert(PyObject *mod)
264 {
265   INSERT("ByteString", bytestring_pytype);
266 }
267
268 /*----- That's all, folks -------------------------------------------------*/