Debian Codegraph @

Modules/_sqlite/blob.c

raw
1 #ifndef Py_BUILD_CORE_BUILTIN
2 # define Py_BUILD_CORE_MODULE 1
3 #endif
4
5 #include "blob.h"
6 #include "util.h"
7 #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
8
9 #define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
10 #include "clinic/blob.c.h"
11 #undef clinic_state
12
13 #define _pysqlite_Blob_CAST(op) ((pysqlite_Blob *)(op))
14
15 /*[clinic input]
16 module _sqlite3
17 class _sqlite3.Blob "pysqlite_Blob *" "clinic_state()->BlobType"
18 [clinic start generated code]*/
19 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=908d3e16a45f8da7]*/
20
21 static void
22 close_blob(pysqlite_Blob *self)
23 {
24 if (self->blob) {
25 sqlite3_blob *blob = self->blob;
26 self->blob = NULL;
27
28 Py_BEGIN_ALLOW_THREADS
29 sqlite3_blob_close(blob);
30 Py_END_ALLOW_THREADS
31 }
32 }
33
34 static int
35 blob_traverse(PyObject *op, visitproc visit, void *arg)
36 {
37 pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
38 Py_VISIT(Py_TYPE(self));
39 Py_VISIT(self->connection);
40 return 0;
41 }
42
43 static int
44 blob_clear(PyObject *op)
45 {
46 pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
47 Py_CLEAR(self->connection);
48 return 0;
49 }
50
51 static void
52 blob_dealloc(PyObject *op)
53 {
54 pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
55 PyTypeObject *tp = Py_TYPE(self);
56 PyObject_GC_UnTrack(self);
57
58 close_blob(self);
59
60 FT_CLEAR_WEAKREFS(op, self->in_weakreflist);
61 (void)tp->tp_clear(op);
62 tp->tp_free(self);
63 Py_DECREF(tp);
64 }
65
66 // Return 1 if the blob object is usable, 0 if not.
67 static int
68 check_blob(pysqlite_Blob *self)
69 {
70 if (!pysqlite_check_connection(self->connection) ||
71 !pysqlite_check_thread(self->connection)) {
72 return 0;
73 }
74 if (self->blob == NULL) {
75 pysqlite_state *state = self->connection->state;
76 PyErr_SetString(state->ProgrammingError,
77 "Cannot operate on a closed blob.");
78 return 0;
79 }
80 return 1;
81 }
82
83
84 /*[clinic input]
85 _sqlite3.Blob.close as blob_close
86
87 Close the blob.
88 [clinic start generated code]*/
89
90 static PyObject *
91 blob_close_impl(pysqlite_Blob *self)
92 /*[clinic end generated code: output=848accc20a138d1b input=7bc178a402a40bd8]*/
93 {
94 if (!pysqlite_check_connection(self->connection) ||
95 !pysqlite_check_thread(self->connection))
96 {
97 return NULL;
98 }
99 close_blob(self);
100 Py_RETURN_NONE;
101 };
102
103 void
104 pysqlite_close_all_blobs(pysqlite_Connection *self)
105 {
106 for (Py_ssize_t i = 0; i < PyList_GET_SIZE(self->blobs); i++) {
107 PyObject *weakref = PyList_GET_ITEM(self->blobs, i);
108 PyObject *blob;
109 if (!PyWeakref_GetRef(weakref, &blob)) {
110 continue;
111 }
112 close_blob((pysqlite_Blob *)blob);
113 Py_DECREF(blob);
114 }
115 }
116
117 static void
118 blob_seterror(pysqlite_Blob *self, int rc)
119 {
120 assert(self->connection != NULL);
121 set_error_from_db(self->connection->state, self->connection->db);
122 }
123
124 static PyObject *
125 read_single(pysqlite_Blob *self, Py_ssize_t offset)
126 {
127 unsigned char buf = 0;
128 int rc;
129 Py_BEGIN_ALLOW_THREADS
130 rc = sqlite3_blob_read(self->blob, (void *)&buf, 1, (int)offset);
131 Py_END_ALLOW_THREADS
132
133 if (rc != SQLITE_OK) {
134 blob_seterror(self, rc);
135 return NULL;
136 }
137 return PyLong_FromUnsignedLong((unsigned long)buf);
138 }
139
140 static PyObject *
141 read_multiple(pysqlite_Blob *self, Py_ssize_t length, Py_ssize_t offset)
142 {
143 assert(length <= sqlite3_blob_bytes(self->blob));
144 assert(offset < sqlite3_blob_bytes(self->blob));
145
146 PyObject *buffer = PyBytes_FromStringAndSize(NULL, length);
147 if (buffer == NULL) {
148 return NULL;
149 }
150
151 char *raw_buffer = PyBytes_AS_STRING(buffer);
152 int rc;
153 Py_BEGIN_ALLOW_THREADS
154 rc = sqlite3_blob_read(self->blob, raw_buffer, (int)length, (int)offset);
155 Py_END_ALLOW_THREADS
156
157 if (rc != SQLITE_OK) {
158 Py_DECREF(buffer);
159 blob_seterror(self, rc);
160 return NULL;
161 }
162 return buffer;
163 }
164
165
166 /*[clinic input]
167 _sqlite3.Blob.read as blob_read
168
169 length: int = -1
170 Read length in bytes.
171 /
172
173 Read data at the current offset position.
174
175 If the end of the blob is reached, the data up to end of file will be returned.
176 When length is not specified, or is negative, Blob.read() will read until the
177 end of the blob.
178 [clinic start generated code]*/
179
180 static PyObject *
181 blob_read_impl(pysqlite_Blob *self, int length)
182 /*[clinic end generated code: output=1fc99b2541360dde input=f2e4aa4378837250]*/
183 {
184 if (!check_blob(self)) {
185 return NULL;
186 }
187
188 /* Make sure we never read past "EOB". Also read the rest of the blob if a
189 * negative length is specified. */
190 int blob_len = sqlite3_blob_bytes(self->blob);
191 int max_read_len = blob_len - self->offset;
192 if (length < 0 || length > max_read_len) {
193 length = max_read_len;
194 }
195
196 assert(length >= 0);
197 if (length == 0) {
198 return PyBytes_FromStringAndSize(NULL, 0);
199 }
200
201 PyObject *buffer = read_multiple(self, length, self->offset);
202 if (buffer == NULL) {
203 return NULL;
204 }
205 self->offset += length;
206 return buffer;
207 };
208
209 static int
210 inner_write(pysqlite_Blob *self, const void *buf, Py_ssize_t len,
211 Py_ssize_t offset)
212 {
213 Py_ssize_t blob_len = sqlite3_blob_bytes(self->blob);
214 Py_ssize_t remaining_len = blob_len - offset;
215 if (len > remaining_len) {
216 PyErr_SetString(PyExc_ValueError, "data longer than blob length");
217 return -1;
218 }
219
220 assert(offset <= blob_len);
221 int rc;
222 Py_BEGIN_ALLOW_THREADS
223 rc = sqlite3_blob_write(self->blob, buf, (int)len, (int)offset);
224 Py_END_ALLOW_THREADS
225
226 if (rc != SQLITE_OK) {
227 blob_seterror(self, rc);
228 return -1;
229 }
230 return 0;
231 }
232
233
234 /*[clinic input]
235 _sqlite3.Blob.write as blob_write
236
237 data: Py_buffer
238 /
239
240 Write data at the current offset.
241
242 This function cannot change the blob length. Writing beyond the end of the
243 blob will result in an exception being raised.
244 [clinic start generated code]*/
245
246 static PyObject *
247 blob_write_impl(pysqlite_Blob *self, Py_buffer *data)
248 /*[clinic end generated code: output=b34cf22601b570b2 input=a84712f24a028e6d]*/
249 {
250 if (!check_blob(self)) {
251 return NULL;
252 }
253
254 int rc = inner_write(self, data->buf, data->len, self->offset);
255 if (rc < 0) {
256 return NULL;
257 }
258 self->offset += (int)data->len;
259 Py_RETURN_NONE;
260 }
261
262
263 /*[clinic input]
264 _sqlite3.Blob.seek as blob_seek
265
266 offset: int
267 origin: int = 0
268 /
269
270 Set the current access position to offset.
271
272 The origin argument defaults to os.SEEK_SET (absolute blob positioning).
273 Other values for origin are os.SEEK_CUR (seek relative to the current position)
274 and os.SEEK_END (seek relative to the blob's end).
275 [clinic start generated code]*/
276
277 static PyObject *
278 blob_seek_impl(pysqlite_Blob *self, int offset, int origin)
279 /*[clinic end generated code: output=854c5a0e208547a5 input=5da9a07e55fe6bb6]*/
280 {
281 if (!check_blob(self)) {
282 return NULL;
283 }
284
285 int blob_len = sqlite3_blob_bytes(self->blob);
286 switch (origin) {
287 case SEEK_SET:
288 break;
289 case SEEK_CUR:
290 if (offset > INT_MAX - self->offset) {
291 goto overflow;
292 }
293 offset += self->offset;
294 break;
295 case SEEK_END:
296 if (offset > INT_MAX - blob_len) {
297 goto overflow;
298 }
299 offset += blob_len;
300 break;
301 default:
302 PyErr_SetString(PyExc_ValueError,
303 "'origin' should be os.SEEK_SET, os.SEEK_CUR, or "
304 "os.SEEK_END");
305 return NULL;
306 }
307
308 if (offset < 0 || offset > blob_len) {
309 PyErr_SetString(PyExc_ValueError, "offset out of blob range");
310 return NULL;
311 }
312
313 self->offset = offset;
314 Py_RETURN_NONE;
315
316 overflow:
317 PyErr_SetString(PyExc_OverflowError, "seek offset results in overflow");
318 return NULL;
319 }
320
321
322 /*[clinic input]
323 _sqlite3.Blob.tell as blob_tell
324
325 Return the current access position for the blob.
326 [clinic start generated code]*/
327
328 static PyObject *
329 blob_tell_impl(pysqlite_Blob *self)
330 /*[clinic end generated code: output=3d3ba484a90b3a99 input=7e34057aa303612c]*/
331 {
332 if (!check_blob(self)) {
333 return NULL;
334 }
335 return PyLong_FromLong(self->offset);
336 }
337
338
339 /*[clinic input]
340 _sqlite3.Blob.__enter__ as blob_enter
341
342 Blob context manager enter.
343 [clinic start generated code]*/
344
345 static PyObject *
346 blob_enter_impl(pysqlite_Blob *self)
347 /*[clinic end generated code: output=4fd32484b071a6cd input=fe4842c3c582d5a7]*/
348 {
349 if (!check_blob(self)) {
350 return NULL;
351 }
352 return Py_NewRef(self);
353 }
354
355
356 /*[clinic input]
357 _sqlite3.Blob.__exit__ as blob_exit
358
359 type: object
360 val: object
361 tb: object
362 /
363
364 Blob context manager exit.
365 [clinic start generated code]*/
366
367 static PyObject *
368 blob_exit_impl(pysqlite_Blob *self, PyObject *type, PyObject *val,
369 PyObject *tb)
370 /*[clinic end generated code: output=fc86ceeb2b68c7b2 input=575d9ecea205f35f]*/
371 {
372 if (!check_blob(self)) {
373 return NULL;
374 }
375 close_blob(self);
376 Py_RETURN_FALSE;
377 }
378
379 static Py_ssize_t
380 blob_length(PyObject *op)
381 {
382 pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
383 if (!check_blob(self)) {
384 return -1;
385 }
386 return sqlite3_blob_bytes(self->blob);
387 };
388
389 static Py_ssize_t
390 get_subscript_index(pysqlite_Blob *self, PyObject *item)
391 {
392 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
393 if (i == -1 && PyErr_Occurred()) {
394 return -1;
395 }
396 int blob_len = sqlite3_blob_bytes(self->blob);
397 if (i < 0) {
398 i += blob_len;
399 }
400 if (i < 0 || i >= blob_len) {
401 PyErr_SetString(PyExc_IndexError, "Blob index out of range");
402 return -1;
403 }
404 return i;
405 }
406
407 static PyObject *
408 subscript_index(pysqlite_Blob *self, PyObject *item)
409 {
410 Py_ssize_t i = get_subscript_index(self, item);
411 if (i < 0) {
412 return NULL;
413 }
414 return read_single(self, i);
415 }
416
417 static int
418 get_slice_info(pysqlite_Blob *self, PyObject *item, Py_ssize_t *start,
419 Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelen)
420 {
421 if (PySlice_Unpack(item, start, stop, step) < 0) {
422 return -1;
423 }
424 int len = sqlite3_blob_bytes(self->blob);
425 *slicelen = PySlice_AdjustIndices(len, start, stop, *step);
426 return 0;
427 }
428
429 static PyObject *
430 subscript_slice(pysqlite_Blob *self, PyObject *item)
431 {
432 Py_ssize_t start, stop, step, len;
433 if (get_slice_info(self, item, &start, &stop, &step, &len) < 0) {
434 return NULL;
435 }
436
437 if (len == 0) {
438 return PyBytes_FromStringAndSize(NULL, 0);
439 }
440
441 if (step == 1) {
442 return read_multiple(self, len, start);
443 }
444 PyObject *blob = read_multiple(self, stop - start, start);
445 if (blob == NULL) {
446 return NULL;
447 }
448 PyObject *result = PyBytes_FromStringAndSize(NULL, len);
449 if (result != NULL) {
450 char *blob_buf = PyBytes_AS_STRING(blob);
451 char *res_buf = PyBytes_AS_STRING(result);
452 for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) {
453 res_buf[i] = blob_buf[j];
454 }
455 Py_DECREF(blob);
456 }
457 return result;
458 }
459
460 static PyObject *
461 blob_subscript(PyObject *op, PyObject *item)
462 {
463 pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
464 if (!check_blob(self)) {
465 return NULL;
466 }
467
468 if (PyIndex_Check(item)) {
469 return subscript_index(self, item);
470 }
471 if (PySlice_Check(item)) {
472 return subscript_slice(self, item);
473 }
474
475 PyErr_SetString(PyExc_TypeError, "Blob indices must be integers");
476 return NULL;
477 }
478
479 static int
480 ass_subscript_index(pysqlite_Blob *self, PyObject *item, PyObject *value)
481 {
482 if (value == NULL) {
483 PyErr_SetString(PyExc_TypeError,
484 "Blob doesn't support item deletion");
485 return -1;
486 }
487 if (!PyLong_Check(value)) {
488 PyErr_Format(PyExc_TypeError,
489 "'%s' object cannot be interpreted as an integer",
490 Py_TYPE(value)->tp_name);
491 return -1;
492 }
493 Py_ssize_t i = get_subscript_index(self, item);
494 if (i < 0) {
495 return -1;
496 }
497
498 long val = PyLong_AsLong(value);
499 if (val == -1 && PyErr_Occurred()) {
500 PyErr_Clear();
501 val = -1;
502 }
503 if (val < 0 || val > 255) {
504 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
505 return -1;
506 }
507 // Downcast to avoid endianness problems.
508 unsigned char byte = (unsigned char)val;
509 return inner_write(self, (const void *)&byte, 1, i);
510 }
511
512 static int
513 ass_subscript_slice(pysqlite_Blob *self, PyObject *item, PyObject *value)
514 {
515 if (value == NULL) {
516 PyErr_SetString(PyExc_TypeError,
517 "Blob doesn't support slice deletion");
518 return -1;
519 }
520
521 Py_ssize_t start, stop, step, len;
522 if (get_slice_info(self, item, &start, &stop, &step, &len) < 0) {
523 return -1;
524 }
525
526 if (len == 0) {
527 return 0;
528 }
529
530 Py_buffer vbuf;
531 if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0) {
532 return -1;
533 }
534
535 int rc = -1;
536 if (vbuf.len != len) {
537 PyErr_SetString(PyExc_IndexError,
538 "Blob slice assignment is wrong size");
539 }
540 else if (step == 1) {
541 rc = inner_write(self, vbuf.buf, len, start);
542 }
543 else {
544 PyObject *blob_bytes = read_multiple(self, stop - start, start);
545 if (blob_bytes != NULL) {
546 char *blob_buf = PyBytes_AS_STRING(blob_bytes);
547 for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) {
548 blob_buf[j] = ((char *)vbuf.buf)[i];
549 }
550 rc = inner_write(self, blob_buf, stop - start, start);
551 Py_DECREF(blob_bytes);
552 }
553 }
554 PyBuffer_Release(&vbuf);
555 return rc;
556 }
557
558 static int
559 blob_ass_subscript(PyObject *op, PyObject *item, PyObject *value)
560 {
561 pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
562 if (!check_blob(self)) {
563 return -1;
564 }
565
566 if (PyIndex_Check(item)) {
567 return ass_subscript_index(self, item, value);
568 }
569 if (PySlice_Check(item)) {
570 return ass_subscript_slice(self, item, value);
571 }
572
573 PyErr_SetString(PyExc_TypeError, "Blob indices must be integers");
574 return -1;
575 }
576
577
578 static PyMethodDef blob_methods[] = {
579 BLOB_CLOSE_METHODDEF
580 BLOB_ENTER_METHODDEF
581 BLOB_EXIT_METHODDEF
582 BLOB_READ_METHODDEF
583 BLOB_SEEK_METHODDEF
584 BLOB_TELL_METHODDEF
585 BLOB_WRITE_METHODDEF
586 {NULL, NULL}
587 };
588
589 static struct PyMemberDef blob_members[] = {
590 {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(pysqlite_Blob, in_weakreflist), Py_READONLY},
591 {NULL},
592 };
593
594 static PyType_Slot blob_slots[] = {
595 {Py_tp_dealloc, blob_dealloc},
596 {Py_tp_traverse, blob_traverse},
597 {Py_tp_clear, blob_clear},
598 {Py_tp_methods, blob_methods},
599 {Py_tp_members, blob_members},
600
601 // Mapping protocol
602 {Py_mp_length, blob_length},
603 {Py_mp_subscript, blob_subscript},
604 {Py_mp_ass_subscript, blob_ass_subscript},
605 {0, NULL},
606 };
607
608 static PyType_Spec blob_spec = {
609 .name = MODULE_NAME ".Blob",
610 .basicsize = sizeof(pysqlite_Blob),
611 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
612 Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
613 .slots = blob_slots,
614 };
615
616 int
617 pysqlite_blob_setup_types(PyObject *mod)
618 {
619 PyObject *type = PyType_FromModuleAndSpec(mod, &blob_spec, NULL);
620 if (type == NULL) {
621 return -1;
622 }
623 pysqlite_state *state = pysqlite_get_state(mod);
624 state->BlobType = (PyTypeObject *)type;
625 return 0;
626 }