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