Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 1 | |
| 2 | /* Buffer object implementation */ |
| 3 | |
| 4 | #include "Python.h" |
| 5 | |
| 6 | |
| 7 | typedef struct { |
| 8 | PyObject_HEAD |
| 9 | PyObject *b_base; |
| 10 | void *b_ptr; |
| 11 | int b_size; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 12 | int b_offset; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 13 | int b_readonly; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 14 | long b_hash; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 15 | } PyBufferObject; |
| 16 | |
| 17 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 18 | static int |
| 19 | get_buf(PyBufferObject *self, void **ptr, int *size) |
| 20 | { |
| 21 | if (self->b_base == NULL) { |
| 22 | assert (ptr != NULL); |
| 23 | *ptr = self->b_ptr; |
| 24 | *size = self->b_size; |
| 25 | } |
| 26 | else { |
| 27 | int count, offset; |
| 28 | getreadbufferproc proc; |
| 29 | PyBufferProcs *bp = self->b_base->ob_type->tp_as_buffer; |
| 30 | if ((*bp->bf_getsegcount)(self->b_base, NULL) != 1) { |
| 31 | PyErr_SetString(PyExc_TypeError, |
| 32 | "single-segment buffer object expected"); |
| 33 | return 0; |
| 34 | } |
| 35 | if (self->b_readonly) |
| 36 | proc = bp->bf_getreadbuffer; |
| 37 | else |
| 38 | proc = (getreadbufferproc)bp->bf_getwritebuffer; |
| 39 | if ((count = (*proc)(self->b_base, 0, ptr)) < 0) |
| 40 | return 0; |
| 41 | /* apply constraints to the start/end */ |
| 42 | if (self->b_offset > count) |
| 43 | offset = count; |
| 44 | else |
| 45 | offset = self->b_offset; |
| 46 | (char *)*ptr = (char *)*ptr + offset; |
| 47 | if (self->b_size == Py_END_OF_BUFFER) |
| 48 | *size = count; |
| 49 | else |
| 50 | *size = self->b_size; |
| 51 | if (offset + *size > count) |
| 52 | *size = count - offset; |
| 53 | } |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 58 | static PyObject * |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 59 | buffer_from_memory(PyObject *base, int size, int offset, void *ptr, |
| 60 | int readonly) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 61 | { |
| 62 | PyBufferObject * b; |
| 63 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 64 | if (size < 0 && size != Py_END_OF_BUFFER) { |
Guido van Rossum | 49ded3e | 1999-03-19 19:04:25 +0000 | [diff] [blame] | 65 | PyErr_SetString(PyExc_ValueError, |
| 66 | "size must be zero or positive"); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 70 | b = PyObject_NEW(PyBufferObject, &PyBuffer_Type); |
| 71 | if ( b == NULL ) |
| 72 | return NULL; |
| 73 | |
| 74 | Py_XINCREF(base); |
| 75 | b->b_base = base; |
| 76 | b->b_ptr = ptr; |
| 77 | b->b_size = size; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 78 | b->b_offset = offset; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 79 | b->b_readonly = readonly; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 80 | b->b_hash = -1; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 81 | |
| 82 | return (PyObject *) b; |
| 83 | } |
| 84 | |
| 85 | static PyObject * |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 86 | buffer_from_object(PyObject *base, int size, int offset, int readonly) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 87 | { |
Guido van Rossum | 49ded3e | 1999-03-19 19:04:25 +0000 | [diff] [blame] | 88 | if ( offset < 0 ) { |
| 89 | PyErr_SetString(PyExc_ValueError, |
| 90 | "offset must be zero or positive"); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 94 | /* if the base object is another buffer, then try to refer to the |
| 95 | * base object. |
Thomas Heller | fdc1bd3 | 2001-10-19 13:49:35 +0000 | [diff] [blame] | 96 | */ |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 97 | if ( PyBuffer_Check(base) && (((PyBufferObject *)base)->b_base) ) { |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 98 | base = ((PyBufferObject *)base)->b_base; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 99 | offset = ((PyBufferObject *)base)->b_offset + offset; |
| 100 | } |
Thomas Heller | fdc1bd3 | 2001-10-19 13:49:35 +0000 | [diff] [blame] | 101 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 102 | return buffer_from_memory(base, size, offset, NULL, readonly); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
| 106 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 107 | PyBuffer_FromObject(PyObject *base, int offset, int size) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 108 | { |
| 109 | PyBufferProcs *pb = base->ob_type->tp_as_buffer; |
| 110 | |
| 111 | if ( pb == NULL || |
| 112 | pb->bf_getreadbuffer == NULL || |
| 113 | pb->bf_getsegcount == NULL ) |
| 114 | { |
| 115 | PyErr_SetString(PyExc_TypeError, "buffer object expected"); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 119 | return buffer_from_object(base, size, offset, 1); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 123 | PyBuffer_FromReadWriteObject(PyObject *base, int offset, int size) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 124 | { |
| 125 | PyBufferProcs *pb = base->ob_type->tp_as_buffer; |
| 126 | |
| 127 | if ( pb == NULL || |
| 128 | pb->bf_getwritebuffer == NULL || |
| 129 | pb->bf_getsegcount == NULL ) |
| 130 | { |
| 131 | PyErr_SetString(PyExc_TypeError, "buffer object expected"); |
| 132 | return NULL; |
| 133 | } |
| 134 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 135 | return buffer_from_object(base, size, offset, 0); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 139 | PyBuffer_FromMemory(void *ptr, int size) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 140 | { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 141 | return buffer_from_memory(NULL, size, 0, ptr, 1); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 145 | PyBuffer_FromReadWriteMemory(void *ptr, int size) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 146 | { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 147 | return buffer_from_memory(NULL, size, 0, ptr, 0); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 151 | PyBuffer_New(int size) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 152 | { |
Moshe Zadka | cf703f0 | 2000-08-04 15:36:13 +0000 | [diff] [blame] | 153 | PyObject *o; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 154 | PyBufferObject * b; |
| 155 | |
Fred Drake | 4574f23 | 1999-08-04 13:08:19 +0000 | [diff] [blame] | 156 | if (size < 0) { |
| 157 | PyErr_SetString(PyExc_ValueError, |
| 158 | "size must be zero or positive"); |
| 159 | return NULL; |
| 160 | } |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 161 | /* Inline PyObject_New */ |
Moshe Zadka | cf703f0 | 2000-08-04 15:36:13 +0000 | [diff] [blame] | 162 | o = PyObject_MALLOC(sizeof(*b) + size); |
| 163 | if ( o == NULL ) |
Fred Drake | 4574f23 | 1999-08-04 13:08:19 +0000 | [diff] [blame] | 164 | return PyErr_NoMemory(); |
Moshe Zadka | cf703f0 | 2000-08-04 15:36:13 +0000 | [diff] [blame] | 165 | b = (PyBufferObject *) PyObject_INIT(o, &PyBuffer_Type); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 166 | |
| 167 | b->b_base = NULL; |
| 168 | b->b_ptr = (void *)(b + 1); |
| 169 | b->b_size = size; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 170 | b->b_offset = 0; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 171 | b->b_readonly = 0; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 172 | b->b_hash = -1; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 173 | |
Moshe Zadka | cf703f0 | 2000-08-04 15:36:13 +0000 | [diff] [blame] | 174 | return o; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* Methods */ |
| 178 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 179 | static PyObject * |
| 180 | buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 181 | { |
| 182 | PyObject *ob; |
| 183 | int offset = 0; |
| 184 | int size = Py_END_OF_BUFFER; |
| 185 | |
| 186 | if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) ) |
| 187 | return NULL; |
| 188 | return PyBuffer_FromObject(ob, offset, size); |
| 189 | } |
| 190 | |
| 191 | PyDoc_STRVAR(buffer_doc, |
| 192 | "buffer(object [, offset[, size]])\n\ |
| 193 | \n\ |
| 194 | Create a new buffer object which references the given object.\n\ |
| 195 | The buffer will reference a slice of the target object from the\n\ |
| 196 | start of the object (or at the specified offset). The slice will\n\ |
| 197 | extend to the end of the target object (or with the specified size)."); |
| 198 | |
| 199 | |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 200 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 201 | buffer_dealloc(PyBufferObject *self) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 202 | { |
| 203 | Py_XDECREF(self->b_base); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 204 | PyObject_DEL(self); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 208 | buffer_compare(PyBufferObject *self, PyBufferObject *other) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 209 | { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 210 | void *p1, *p2; |
| 211 | int len_self, len_other, min_len, cmp; |
| 212 | |
| 213 | if (!get_buf(self, &p1, &len_self)) |
| 214 | return -1; |
| 215 | if (!get_buf(other, &p2, &len_other)) |
| 216 | return -1; |
| 217 | min_len = (len_self < len_other) ? len_self : len_other; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 218 | if (min_len > 0) { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 219 | cmp = memcmp(p1, p2, min_len); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 220 | if (cmp != 0) |
| 221 | return cmp; |
| 222 | } |
| 223 | return (len_self < len_other) ? -1 : (len_self > len_other) ? 1 : 0; |
| 224 | } |
| 225 | |
| 226 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 227 | buffer_repr(PyBufferObject *self) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 228 | { |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 229 | char *status = self->b_readonly ? "read-only" : "read-write"; |
| 230 | |
| 231 | if ( self->b_base == NULL ) |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 232 | return PyString_FromFormat("<%s buffer ptr %p, size %d at %p>", |
| 233 | status, |
| 234 | self->b_ptr, |
| 235 | self->b_size, |
| 236 | self); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 237 | else |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 238 | return PyString_FromFormat( |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 239 | "<%s buffer for %p, size %d, offset %d at %p>", |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 240 | status, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 241 | self->b_base, |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 242 | self->b_size, |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 243 | self->b_offset, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 244 | self); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static long |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 248 | buffer_hash(PyBufferObject *self) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 249 | { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 250 | void *ptr; |
| 251 | int size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 252 | register int len; |
| 253 | register unsigned char *p; |
| 254 | register long x; |
| 255 | |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 256 | if ( self->b_hash != -1 ) |
| 257 | return self->b_hash; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 258 | |
Neil Schemenauer | 0eadcd9 | 2004-03-11 01:00:44 +0000 | [diff] [blame] | 259 | /* XXX potential bugs here, a readonly buffer does not imply that the |
| 260 | * underlying memory is immutable. b_readonly is a necessary but not |
| 261 | * sufficient condition for a buffer to be hashable. Perhaps it would |
| 262 | * be better to only allow hashing if the underlying object is known to |
| 263 | * be immutable (e.g. PyString_Check() is true). Another idea would |
| 264 | * be to call tp_hash on the underlying object and see if it raises |
| 265 | * an error. */ |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 266 | if ( !self->b_readonly ) |
| 267 | { |
Neil Schemenauer | 0eadcd9 | 2004-03-11 01:00:44 +0000 | [diff] [blame] | 268 | PyErr_SetString(PyExc_TypeError, |
| 269 | "writable buffers are not hashable"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 270 | return -1; |
| 271 | } |
| 272 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 273 | if (!get_buf(self, &ptr, &size)) |
| 274 | return -1; |
| 275 | p = (unsigned char *) ptr; |
| 276 | len = size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 277 | x = *p << 7; |
| 278 | while (--len >= 0) |
| 279 | x = (1000003*x) ^ *p++; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 280 | x ^= size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 281 | if (x == -1) |
| 282 | x = -2; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 283 | self->b_hash = x; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 284 | return x; |
| 285 | } |
| 286 | |
| 287 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 288 | buffer_str(PyBufferObject *self) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 289 | { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 290 | void *ptr; |
| 291 | int size; |
| 292 | if (!get_buf(self, &ptr, &size)) |
| 293 | return NULL; |
| 294 | return PyString_FromStringAndSize(ptr, size); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* Sequence methods */ |
| 298 | |
| 299 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 300 | buffer_length(PyBufferObject *self) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 301 | { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 302 | void *ptr; |
| 303 | int size; |
| 304 | if (!get_buf(self, &ptr, &size)) |
| 305 | return -1; |
| 306 | return size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 310 | buffer_concat(PyBufferObject *self, PyObject *other) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 311 | { |
| 312 | PyBufferProcs *pb = other->ob_type->tp_as_buffer; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 313 | void *ptr1, *ptr2; |
| 314 | char *p; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 315 | PyObject *ob; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 316 | int size, count; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 317 | |
| 318 | if ( pb == NULL || |
| 319 | pb->bf_getreadbuffer == NULL || |
| 320 | pb->bf_getsegcount == NULL ) |
| 321 | { |
| 322 | PyErr_BadArgument(); |
| 323 | return NULL; |
| 324 | } |
| 325 | if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) |
| 326 | { |
| 327 | /* ### use a different exception type/message? */ |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 328 | PyErr_SetString(PyExc_TypeError, |
| 329 | "single-segment buffer object expected"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 330 | return NULL; |
| 331 | } |
| 332 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 333 | if (!get_buf(self, &ptr1, &size)) |
| 334 | return NULL; |
| 335 | |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 336 | /* optimize special case */ |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 337 | if ( size == 0 ) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 338 | { |
| 339 | Py_INCREF(other); |
| 340 | return other; |
| 341 | } |
| 342 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 343 | if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 ) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 344 | return NULL; |
| 345 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 346 | ob = PyString_FromStringAndSize(NULL, size + count); |
| 347 | p = PyString_AS_STRING(ob); |
| 348 | memcpy(p, ptr1, size); |
| 349 | memcpy(p + size, ptr2, count); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 350 | |
| 351 | /* there is an extra byte in the string object, so this is safe */ |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 352 | p[size + count] = '\0'; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 353 | |
| 354 | return ob; |
| 355 | } |
| 356 | |
| 357 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 358 | buffer_repeat(PyBufferObject *self, int count) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 359 | { |
| 360 | PyObject *ob; |
| 361 | register char *p; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 362 | void *ptr; |
| 363 | int size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 364 | |
| 365 | if ( count < 0 ) |
| 366 | count = 0; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 367 | if (!get_buf(self, &ptr, &size)) |
| 368 | return NULL; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 369 | ob = PyString_FromStringAndSize(NULL, size * count); |
| 370 | if ( ob == NULL ) |
| 371 | return NULL; |
| 372 | |
| 373 | p = PyString_AS_STRING(ob); |
| 374 | while ( count-- ) |
| 375 | { |
| 376 | memcpy(p, ptr, size); |
| 377 | p += size; |
| 378 | } |
| 379 | |
| 380 | /* there is an extra byte in the string object, so this is safe */ |
| 381 | *p = '\0'; |
| 382 | |
| 383 | return ob; |
| 384 | } |
| 385 | |
| 386 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 387 | buffer_item(PyBufferObject *self, int idx) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 388 | { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 389 | void *ptr; |
| 390 | int size; |
| 391 | if (!get_buf(self, &ptr, &size)) |
| 392 | return NULL; |
| 393 | if ( idx < 0 || idx >= size ) { |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 394 | PyErr_SetString(PyExc_IndexError, "buffer index out of range"); |
| 395 | return NULL; |
| 396 | } |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 397 | return PyString_FromStringAndSize((char *)ptr + idx, 1); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 401 | buffer_slice(PyBufferObject *self, int left, int right) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 402 | { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 403 | void *ptr; |
| 404 | int size; |
| 405 | if (!get_buf(self, &ptr, &size)) |
| 406 | return NULL; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 407 | if ( left < 0 ) |
| 408 | left = 0; |
| 409 | if ( right < 0 ) |
| 410 | right = 0; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 411 | if ( right > size ) |
| 412 | right = size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 413 | if ( right < left ) |
| 414 | right = left; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 415 | return PyString_FromStringAndSize((char *)ptr + left, |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 416 | right - left); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 420 | buffer_ass_item(PyBufferObject *self, int idx, PyObject *other) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 421 | { |
| 422 | PyBufferProcs *pb; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 423 | void *ptr1, *ptr2; |
| 424 | int size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 425 | int count; |
| 426 | |
| 427 | if ( self->b_readonly ) { |
| 428 | PyErr_SetString(PyExc_TypeError, |
| 429 | "buffer is read-only"); |
| 430 | return -1; |
| 431 | } |
| 432 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 433 | if (!get_buf(self, &ptr1, &size)) |
| 434 | return -1; |
| 435 | |
| 436 | if (idx < 0 || idx >= size) { |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 437 | PyErr_SetString(PyExc_IndexError, |
| 438 | "buffer assignment index out of range"); |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | pb = other ? other->ob_type->tp_as_buffer : NULL; |
| 443 | if ( pb == NULL || |
| 444 | pb->bf_getreadbuffer == NULL || |
| 445 | pb->bf_getsegcount == NULL ) |
| 446 | { |
| 447 | PyErr_BadArgument(); |
| 448 | return -1; |
| 449 | } |
| 450 | if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) |
| 451 | { |
| 452 | /* ### use a different exception type/message? */ |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 453 | PyErr_SetString(PyExc_TypeError, |
| 454 | "single-segment buffer object expected"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 455 | return -1; |
| 456 | } |
| 457 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 458 | if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 ) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 459 | return -1; |
| 460 | if ( count != 1 ) { |
| 461 | PyErr_SetString(PyExc_TypeError, |
| 462 | "right operand must be a single byte"); |
| 463 | return -1; |
| 464 | } |
| 465 | |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 466 | ((char *)ptr1)[idx] = *(char *)ptr2; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 471 | buffer_ass_slice(PyBufferObject *self, int left, int right, PyObject *other) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 472 | { |
| 473 | PyBufferProcs *pb; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 474 | void *ptr1, *ptr2; |
| 475 | int size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 476 | int slice_len; |
| 477 | int count; |
| 478 | |
| 479 | if ( self->b_readonly ) { |
| 480 | PyErr_SetString(PyExc_TypeError, |
| 481 | "buffer is read-only"); |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | pb = other ? other->ob_type->tp_as_buffer : NULL; |
| 486 | if ( pb == NULL || |
| 487 | pb->bf_getreadbuffer == NULL || |
| 488 | pb->bf_getsegcount == NULL ) |
| 489 | { |
| 490 | PyErr_BadArgument(); |
| 491 | return -1; |
| 492 | } |
| 493 | if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) |
| 494 | { |
| 495 | /* ### use a different exception type/message? */ |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 496 | PyErr_SetString(PyExc_TypeError, |
| 497 | "single-segment buffer object expected"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 498 | return -1; |
| 499 | } |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 500 | if (!get_buf(self, &ptr1, &size)) |
| 501 | return -1; |
| 502 | if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 ) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 503 | return -1; |
| 504 | |
| 505 | if ( left < 0 ) |
| 506 | left = 0; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 507 | else if ( left > size ) |
| 508 | left = size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 509 | if ( right < left ) |
| 510 | right = left; |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 511 | else if ( right > size ) |
| 512 | right = size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 513 | slice_len = right - left; |
| 514 | |
| 515 | if ( count != slice_len ) { |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 516 | PyErr_SetString( |
| 517 | PyExc_TypeError, |
| 518 | "right operand length must match slice length"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | if ( slice_len ) |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 523 | memcpy((char *)ptr1 + left, ptr2, slice_len); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /* Buffer methods */ |
| 529 | |
| 530 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 531 | buffer_getreadbuf(PyBufferObject *self, int idx, void **pp) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 532 | { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 533 | int size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 534 | if ( idx != 0 ) { |
| 535 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 536 | "accessing non-existent buffer segment"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 537 | return -1; |
| 538 | } |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 539 | if (!get_buf(self, pp, &size)) |
| 540 | return -1; |
| 541 | return size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 545 | buffer_getwritebuf(PyBufferObject *self, int idx, void **pp) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 546 | { |
| 547 | if ( self->b_readonly ) |
| 548 | { |
| 549 | PyErr_SetString(PyExc_TypeError, "buffer is read-only"); |
| 550 | return -1; |
| 551 | } |
| 552 | return buffer_getreadbuf(self, idx, pp); |
| 553 | } |
| 554 | |
| 555 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 556 | buffer_getsegcount(PyBufferObject *self, int *lenp) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 557 | { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 558 | void *ptr; |
| 559 | int size; |
| 560 | if (!get_buf(self, &ptr, &size)) |
| 561 | return -1; |
| 562 | if (lenp) |
| 563 | *lenp = size; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 564 | return 1; |
| 565 | } |
| 566 | |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 567 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 568 | buffer_getcharbuf(PyBufferObject *self, int idx, const char **pp) |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 569 | { |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 570 | void *ptr; |
| 571 | int size; |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 572 | if ( idx != 0 ) { |
| 573 | PyErr_SetString(PyExc_SystemError, |
| 574 | "accessing non-existent buffer segment"); |
| 575 | return -1; |
| 576 | } |
Neil Schemenauer | 4252a7a | 2004-03-11 02:42:45 +0000 | [diff] [blame] | 577 | if (!get_buf(self, &ptr, &size)) |
| 578 | return -1; |
| 579 | *pp = (const char *)ptr; |
| 580 | return size; |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 583 | |
| 584 | static PySequenceMethods buffer_as_sequence = { |
| 585 | (inquiry)buffer_length, /*sq_length*/ |
| 586 | (binaryfunc)buffer_concat, /*sq_concat*/ |
| 587 | (intargfunc)buffer_repeat, /*sq_repeat*/ |
| 588 | (intargfunc)buffer_item, /*sq_item*/ |
| 589 | (intintargfunc)buffer_slice, /*sq_slice*/ |
| 590 | (intobjargproc)buffer_ass_item, /*sq_ass_item*/ |
| 591 | (intintobjargproc)buffer_ass_slice, /*sq_ass_slice*/ |
| 592 | }; |
| 593 | |
| 594 | static PyBufferProcs buffer_as_buffer = { |
| 595 | (getreadbufferproc)buffer_getreadbuf, |
| 596 | (getwritebufferproc)buffer_getwritebuf, |
| 597 | (getsegcountproc)buffer_getsegcount, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 598 | (getcharbufferproc)buffer_getcharbuf, |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 599 | }; |
| 600 | |
| 601 | PyTypeObject PyBuffer_Type = { |
| 602 | PyObject_HEAD_INIT(&PyType_Type) |
| 603 | 0, |
| 604 | "buffer", |
| 605 | sizeof(PyBufferObject), |
| 606 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 607 | (destructor)buffer_dealloc, /* tp_dealloc */ |
| 608 | 0, /* tp_print */ |
| 609 | 0, /* tp_getattr */ |
| 610 | 0, /* tp_setattr */ |
| 611 | (cmpfunc)buffer_compare, /* tp_compare */ |
| 612 | (reprfunc)buffer_repr, /* tp_repr */ |
| 613 | 0, /* tp_as_number */ |
| 614 | &buffer_as_sequence, /* tp_as_sequence */ |
| 615 | 0, /* tp_as_mapping */ |
| 616 | (hashfunc)buffer_hash, /* tp_hash */ |
| 617 | 0, /* tp_call */ |
| 618 | (reprfunc)buffer_str, /* tp_str */ |
| 619 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 620 | 0, /* tp_setattro */ |
| 621 | &buffer_as_buffer, /* tp_as_buffer */ |
| 622 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 623 | buffer_doc, /* tp_doc */ |
| 624 | 0, /* tp_traverse */ |
| 625 | 0, /* tp_clear */ |
| 626 | 0, /* tp_richcompare */ |
| 627 | 0, /* tp_weaklistoffset */ |
| 628 | 0, /* tp_iter */ |
| 629 | 0, /* tp_iternext */ |
| 630 | 0, /* tp_methods */ |
| 631 | 0, /* tp_members */ |
| 632 | 0, /* tp_getset */ |
| 633 | 0, /* tp_base */ |
| 634 | 0, /* tp_dict */ |
| 635 | 0, /* tp_descr_get */ |
| 636 | 0, /* tp_descr_set */ |
| 637 | 0, /* tp_dictoffset */ |
| 638 | 0, /* tp_init */ |
| 639 | 0, /* tp_alloc */ |
| 640 | buffer_new, /* tp_new */ |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 641 | }; |