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