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