master
c 1,023 lines 29 KB
Raw
1 /*
2 ** 2019-04-17
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
10 **
11 ******************************************************************************
12 **
13 ** This file contains an implementation of two eponymous virtual tables,
14 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
15 ** "sqlite_dbpage" eponymous virtual table be available.
16 **
17 ** SQLITE_DBDATA:
18 ** sqlite_dbdata is used to extract data directly from a database b-tree
19 ** page and its associated overflow pages, bypassing the b-tree layer.
20 ** The table schema is equivalent to:
21 **
22 ** CREATE TABLE sqlite_dbdata(
23 ** pgno INTEGER,
24 ** cell INTEGER,
25 ** field INTEGER,
26 ** value ANY,
27 ** schema TEXT HIDDEN
28 ** );
29 **
30 ** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
31 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
32 ** "schema".
33 **
34 ** Each page of the database is inspected. If it cannot be interpreted as
35 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
36 ** sqlite_dbdata table contains no rows for that page. Otherwise, the
37 ** table contains one row for each field in the record associated with
38 ** each cell on the page. For intkey b-trees, the key value is stored in
39 ** field -1.
40 **
41 ** For example, for the database:
42 **
43 ** CREATE TABLE t1(a, b); -- root page is page 2
44 ** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
45 ** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
46 **
47 ** the sqlite_dbdata table contains, as well as from entries related to
48 ** page 1, content equivalent to:
49 **
50 ** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
51 ** (2, 0, -1, 5 ),
52 ** (2, 0, 0, 'v' ),
53 ** (2, 0, 1, 'five'),
54 ** (2, 1, -1, 10 ),
55 ** (2, 1, 0, 'x' ),
56 ** (2, 1, 1, 'ten' );
57 **
58 ** If database corruption is encountered, this module does not report an
59 ** error. Instead, it attempts to extract as much data as possible and
60 ** ignores the corruption.
61 **
62 ** SQLITE_DBPTR:
63 ** The sqlite_dbptr table has the following schema:
64 **
65 ** CREATE TABLE sqlite_dbptr(
66 ** pgno INTEGER,
67 ** child INTEGER,
68 ** schema TEXT HIDDEN
69 ** );
70 **
71 ** It contains one entry for each b-tree pointer between a parent and
72 ** child page in the database.
73 */
74
75 #pragma GCC diagnostic push
76 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
77 #pragma GCC diagnostic ignored "-Wunused-parameter"
78 #if !defined(SQLITEINT_H)
79 #include "sqlite3.h"
80
81 typedef unsigned char u8;
82 typedef unsigned int u32;
83
84 #endif
85 #include <string.h>
86 #include <assert.h>
87
88 #ifndef SQLITE_OMIT_VIRTUALTABLE
89
90 #define DBDATA_PADDING_BYTES 100
91
92 typedef struct DbdataTable DbdataTable;
93 typedef struct DbdataCursor DbdataCursor;
94 typedef struct DbdataBuffer DbdataBuffer;
95
96 /*
97 ** Buffer type.
98 */
99 struct DbdataBuffer {
100 u8 *aBuf;
101 sqlite3_int64 nBuf;
102 };
103
104 /* Cursor object */
105 struct DbdataCursor {
106 sqlite3_vtab_cursor base; /* Base class. Must be first */
107 sqlite3_stmt *pStmt; /* For fetching database pages */
108
109 int iPgno; /* Current page number */
110 u8 *aPage; /* Buffer containing page */
111 int nPage; /* Size of aPage[] in bytes */
112 int nCell; /* Number of cells on aPage[] */
113 int iCell; /* Current cell number */
114 int bOnePage; /* True to stop after one page */
115 int szDb;
116 sqlite3_int64 iRowid;
117
118 /* Only for the sqlite_dbdata table */
119 DbdataBuffer rec;
120 sqlite3_int64 nRec; /* Size of pRec[] in bytes */
121 sqlite3_int64 nHdr; /* Size of header in bytes */
122 int iField; /* Current field number */
123 u8 *pHdrPtr;
124 u8 *pPtr;
125 u32 enc; /* Text encoding */
126
127 sqlite3_int64 iIntkey; /* Integer key value */
128 };
129
130 /* Table object */
131 struct DbdataTable {
132 sqlite3_vtab base; /* Base class. Must be first */
133 sqlite3 *db; /* The database connection */
134 sqlite3_stmt *pStmt; /* For fetching database pages */
135 int bPtr; /* True for sqlite3_dbptr table */
136 };
137
138 /* Column and schema definitions for sqlite_dbdata */
139 #define DBDATA_COLUMN_PGNO 0
140 #define DBDATA_COLUMN_CELL 1
141 #define DBDATA_COLUMN_FIELD 2
142 #define DBDATA_COLUMN_VALUE 3
143 #define DBDATA_COLUMN_SCHEMA 4
144 #define DBDATA_SCHEMA \
145 "CREATE TABLE x(" \
146 " pgno INTEGER," \
147 " cell INTEGER," \
148 " field INTEGER," \
149 " value ANY," \
150 " schema TEXT HIDDEN" \
151 ")"
152
153 /* Column and schema definitions for sqlite_dbptr */
154 #define DBPTR_COLUMN_PGNO 0
155 #define DBPTR_COLUMN_CHILD 1
156 #define DBPTR_COLUMN_SCHEMA 2
157 #define DBPTR_SCHEMA \
158 "CREATE TABLE x(" \
159 " pgno INTEGER," \
160 " child INTEGER," \
161 " schema TEXT HIDDEN" \
162 ")"
163
164 /*
165 ** Ensure the buffer passed as the first argument is at least nMin bytes
166 ** in size. If an error occurs while attempting to resize the buffer,
167 ** SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
168 */
169 static int dbdataBufferSize(DbdataBuffer *pBuf, sqlite3_int64 nMin){
170 if( nMin>pBuf->nBuf ){
171 sqlite3_int64 nNew = nMin+16384;
172 u8 *aNew = (u8*)sqlite3_realloc64(pBuf->aBuf, nNew);
173
174 if( aNew==0 ) return SQLITE_NOMEM;
175 pBuf->aBuf = aNew;
176 pBuf->nBuf = nNew;
177 }
178 return SQLITE_OK;
179 }
180
181 /*
182 ** Release the allocation managed by buffer pBuf.
183 */
184 static void dbdataBufferFree(DbdataBuffer *pBuf){
185 sqlite3_free(pBuf->aBuf);
186 memset(pBuf, 0, sizeof(*pBuf));
187 }
188
189 /*
190 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
191 ** table.
192 */
193 static int dbdataConnect(
194 sqlite3 *db,
195 void *pAux,
196 int argc, const char *const*argv,
197 sqlite3_vtab **ppVtab,
198 char **pzErr
199 ){
200 DbdataTable *pTab = 0;
201 int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
202
203 (void)argc;
204 (void)argv;
205 (void)pzErr;
206 sqlite3_vtab_config(db, SQLITE_VTAB_USES_ALL_SCHEMAS);
207 if( rc==SQLITE_OK ){
208 pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
209 if( pTab==0 ){
210 rc = SQLITE_NOMEM;
211 }else{
212 memset(pTab, 0, sizeof(DbdataTable));
213 pTab->db = db;
214 pTab->bPtr = (pAux!=0);
215 }
216 }
217
218 *ppVtab = (sqlite3_vtab*)pTab;
219 return rc;
220 }
221
222 /*
223 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
224 */
225 static int dbdataDisconnect(sqlite3_vtab *pVtab){
226 DbdataTable *pTab = (DbdataTable*)pVtab;
227 if( pTab ){
228 sqlite3_finalize(pTab->pStmt);
229 sqlite3_free(pVtab);
230 }
231 return SQLITE_OK;
232 }
233
234 /*
235 ** This function interprets two types of constraints:
236 **
237 ** schema=?
238 ** pgno=?
239 **
240 ** If neither are present, idxNum is set to 0. If schema=? is present,
241 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
242 ** in idxNum is set.
243 **
244 ** If both parameters are present, schema is in position 0 and pgno in
245 ** position 1.
246 */
247 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
248 DbdataTable *pTab = (DbdataTable*)tab;
249 int i;
250 int iSchema = -1;
251 int iPgno = -1;
252 int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
253
254 for(i=0; i<pIdx->nConstraint; i++){
255 struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
256 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
257 if( p->iColumn==colSchema ){
258 if( p->usable==0 ) return SQLITE_CONSTRAINT;
259 iSchema = i;
260 }
261 if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
262 iPgno = i;
263 }
264 }
265 }
266
267 if( iSchema>=0 ){
268 pIdx->aConstraintUsage[iSchema].argvIndex = 1;
269 pIdx->aConstraintUsage[iSchema].omit = 1;
270 }
271 if( iPgno>=0 ){
272 pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
273 pIdx->aConstraintUsage[iPgno].omit = 1;
274 pIdx->estimatedCost = 100;
275 pIdx->estimatedRows = 50;
276
277 if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
278 int iCol = pIdx->aOrderBy[0].iColumn;
279 if( pIdx->nOrderBy==1 ){
280 pIdx->orderByConsumed = (iCol==0 || iCol==1);
281 }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
282 pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
283 }
284 }
285
286 }else{
287 pIdx->estimatedCost = 100000000;
288 pIdx->estimatedRows = 1000000000;
289 }
290 pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
291 return SQLITE_OK;
292 }
293
294 /*
295 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
296 */
297 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
298 DbdataCursor *pCsr;
299
300 pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
301 if( pCsr==0 ){
302 return SQLITE_NOMEM;
303 }else{
304 memset(pCsr, 0, sizeof(DbdataCursor));
305 pCsr->base.pVtab = pVTab;
306 }
307
308 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
309 return SQLITE_OK;
310 }
311
312 /*
313 ** Restore a cursor object to the state it was in when first allocated
314 ** by dbdataOpen().
315 */
316 static void dbdataResetCursor(DbdataCursor *pCsr){
317 DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
318 if( pTab->pStmt==0 ){
319 pTab->pStmt = pCsr->pStmt;
320 }else{
321 sqlite3_finalize(pCsr->pStmt);
322 }
323 pCsr->pStmt = 0;
324 pCsr->iPgno = 1;
325 pCsr->iCell = 0;
326 pCsr->iField = 0;
327 pCsr->bOnePage = 0;
328 sqlite3_free(pCsr->aPage);
329 dbdataBufferFree(&pCsr->rec);
330 pCsr->aPage = 0;
331 pCsr->nRec = 0;
332 }
333
334 /*
335 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
336 */
337 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
338 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
339 dbdataResetCursor(pCsr);
340 sqlite3_free(pCsr);
341 return SQLITE_OK;
342 }
343
344 /*
345 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
346 */
347 static u32 get_uint16(unsigned char *a){
348 return (a[0]<<8)|a[1];
349 }
350 static u32 get_uint32(unsigned char *a){
351 return ((u32)a[0]<<24)
352 | ((u32)a[1]<<16)
353 | ((u32)a[2]<<8)
354 | ((u32)a[3]);
355 }
356
357 /*
358 ** Load page pgno from the database via the sqlite_dbpage virtual table.
359 ** If successful, set (*ppPage) to point to a buffer containing the page
360 ** data, (*pnPage) to the size of that buffer in bytes and return
361 ** SQLITE_OK. In this case it is the responsibility of the caller to
362 ** eventually free the buffer using sqlite3_free().
363 **
364 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
365 ** return an SQLite error code.
366 */
367 static int dbdataLoadPage(
368 DbdataCursor *pCsr, /* Cursor object */
369 u32 pgno, /* Page number of page to load */
370 u8 **ppPage, /* OUT: pointer to page buffer */
371 int *pnPage /* OUT: Size of (*ppPage) in bytes */
372 ){
373 int rc2;
374 int rc = SQLITE_OK;
375 sqlite3_stmt *pStmt = pCsr->pStmt;
376
377 *ppPage = 0;
378 *pnPage = 0;
379 if( pgno>0 ){
380 sqlite3_bind_int64(pStmt, 2, pgno);
381 if( SQLITE_ROW==sqlite3_step(pStmt) ){
382 int nCopy = sqlite3_column_bytes(pStmt, 0);
383 if( nCopy>0 ){
384 u8 *pPage;
385 pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
386 if( pPage==0 ){
387 rc = SQLITE_NOMEM;
388 }else{
389 const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
390 memcpy(pPage, pCopy, nCopy);
391 memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
392 }
393 *ppPage = pPage;
394 *pnPage = nCopy;
395 }
396 }
397 rc2 = sqlite3_reset(pStmt);
398 if( rc==SQLITE_OK ) rc = rc2;
399 }
400
401 return rc;
402 }
403
404 /*
405 ** Read a varint. Put the value in *pVal and return the number of bytes.
406 */
407 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
408 sqlite3_uint64 u = 0;
409 int i;
410 for(i=0; i<8; i++){
411 u = (u<<7) + (z[i]&0x7f);
412 if( (z[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
413 }
414 u = (u<<8) + (z[i]&0xff);
415 *pVal = (sqlite3_int64)u;
416 return 9;
417 }
418
419 /*
420 ** Like dbdataGetVarint(), but set the output to 0 if it is less than 0
421 ** or greater than 0xFFFFFFFF. This can be used for all varints in an
422 ** SQLite database except for key values in intkey tables.
423 */
424 static int dbdataGetVarintU32(const u8 *z, sqlite3_int64 *pVal){
425 sqlite3_int64 val;
426 int nRet = dbdataGetVarint(z, &val);
427 if( val<0 || val>0xFFFFFFFF ) val = 0;
428 *pVal = val;
429 return nRet;
430 }
431
432 /*
433 ** Return the number of bytes of space used by an SQLite value of type
434 ** eType.
435 */
436 static int dbdataValueBytes(int eType){
437 switch( eType ){
438 case 0: case 8: case 9:
439 case 10: case 11:
440 return 0;
441 case 1:
442 return 1;
443 case 2:
444 return 2;
445 case 3:
446 return 3;
447 case 4:
448 return 4;
449 case 5:
450 return 6;
451 case 6:
452 case 7:
453 return 8;
454 default:
455 if( eType>0 ){
456 return ((eType-12) / 2);
457 }
458 return 0;
459 }
460 }
461
462 /*
463 ** Load a value of type eType from buffer pData and use it to set the
464 ** result of context object pCtx.
465 */
466 static void dbdataValue(
467 sqlite3_context *pCtx,
468 u32 enc,
469 int eType,
470 u8 *pData,
471 sqlite3_int64 nData
472 ){
473 if( eType>=0 ){
474 if( dbdataValueBytes(eType)<=nData ){
475 switch( eType ){
476 case 0:
477 case 10:
478 case 11:
479 sqlite3_result_null(pCtx);
480 break;
481
482 case 8:
483 sqlite3_result_int(pCtx, 0);
484 break;
485 case 9:
486 sqlite3_result_int(pCtx, 1);
487 break;
488
489 case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
490 sqlite3_uint64 v = (signed char)pData[0];
491 pData++;
492 switch( eType ){
493 case 7:
494 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
495 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
496 case 4: v = (v<<8) + pData[0]; pData++;
497 case 3: v = (v<<8) + pData[0]; pData++;
498 case 2: v = (v<<8) + pData[0]; pData++;
499 }
500
501 if( eType==7 ){
502 double r;
503 memcpy(&r, &v, sizeof(r));
504 sqlite3_result_double(pCtx, r);
505 }else{
506 sqlite3_result_int64(pCtx, (sqlite3_int64)v);
507 }
508 break;
509 }
510
511 default: {
512 int n = ((eType-12) / 2);
513 if( eType % 2 ){
514 switch( enc ){
515 #ifndef SQLITE_OMIT_UTF16
516 case SQLITE_UTF16BE:
517 sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
518 break;
519 case SQLITE_UTF16LE:
520 sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
521 break;
522 #endif
523 default:
524 sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
525 break;
526 }
527 }else{
528 sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
529 }
530 }
531 }
532 }else{
533 if( eType==7 ){
534 sqlite3_result_double(pCtx, 0.0);
535 }else if( eType<7 ){
536 sqlite3_result_int(pCtx, 0);
537 }else if( eType%2 ){
538 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
539 }else{
540 sqlite3_result_blob(pCtx, "", 0, SQLITE_STATIC);
541 }
542 }
543 }
544 }
545
546 /* This macro is a copy of the MX_CELL() macro in the SQLite core. Given
547 ** a page-size, it returns the maximum number of cells that may be present
548 ** on the page. */
549 #define DBDATA_MX_CELL(pgsz) ((pgsz-8)/6)
550
551 /* Maximum number of fields that may appear in a single record. This is
552 ** the "hard-limit", according to comments in sqliteLimit.h. */
553 #define DBDATA_MX_FIELD 32676
554
555 /*
556 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
557 */
558 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
559 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
560 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
561
562 pCsr->iRowid++;
563 while( 1 ){
564 int rc;
565 int iOff = (pCsr->iPgno==1 ? 100 : 0);
566 int bNextPage = 0;
567
568 if( pCsr->aPage==0 ){
569 while( 1 ){
570 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
571 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
572 if( rc!=SQLITE_OK ) return rc;
573 if( pCsr->aPage && pCsr->nPage>=256 ) break;
574 sqlite3_free(pCsr->aPage);
575 pCsr->aPage = 0;
576 if( pCsr->bOnePage ) return SQLITE_OK;
577 pCsr->iPgno++;
578 }
579
580 assert( iOff+3+2<=pCsr->nPage );
581 pCsr->iCell = pTab->bPtr ? -2 : 0;
582 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
583 if( pCsr->nCell>DBDATA_MX_CELL(pCsr->nPage) ){
584 pCsr->nCell = DBDATA_MX_CELL(pCsr->nPage);
585 }
586 }
587
588 if( pTab->bPtr ){
589 if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
590 pCsr->iCell = pCsr->nCell;
591 }
592 pCsr->iCell++;
593 if( pCsr->iCell>=pCsr->nCell ){
594 sqlite3_free(pCsr->aPage);
595 pCsr->aPage = 0;
596 if( pCsr->bOnePage ) return SQLITE_OK;
597 pCsr->iPgno++;
598 }else{
599 return SQLITE_OK;
600 }
601 }else{
602 /* If there is no record loaded, load it now. */
603 assert( pCsr->rec.aBuf!=0 || pCsr->nRec==0 );
604 if( pCsr->nRec==0 ){
605 int bHasRowid = 0;
606 int nPointer = 0;
607 sqlite3_int64 nPayload = 0;
608 sqlite3_int64 nHdr = 0;
609 int iHdr;
610 int U, X;
611 int nLocal;
612
613 switch( pCsr->aPage[iOff] ){
614 case 0x02:
615 nPointer = 4;
616 break;
617 case 0x0a:
618 break;
619 case 0x0d:
620 bHasRowid = 1;
621 break;
622 default:
623 /* This is not a b-tree page with records on it. Continue. */
624 pCsr->iCell = pCsr->nCell;
625 break;
626 }
627
628 if( pCsr->iCell>=pCsr->nCell ){
629 bNextPage = 1;
630 }else{
631 int iCellPtr = iOff + 8 + nPointer + pCsr->iCell*2;
632
633 if( iCellPtr>pCsr->nPage ){
634 bNextPage = 1;
635 }else{
636 iOff = get_uint16(&pCsr->aPage[iCellPtr]);
637 }
638
639 /* For an interior node cell, skip past the child-page number */
640 iOff += nPointer;
641
642 /* Load the "byte of payload including overflow" field */
643 if( bNextPage || iOff>pCsr->nPage || iOff<=iCellPtr ){
644 bNextPage = 1;
645 }else{
646 iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
647 if( nPayload>0x7fffff00 ) nPayload &= 0x3fff;
648 if( nPayload==0 ) nPayload = 1;
649 }
650
651 /* If this is a leaf intkey cell, load the rowid */
652 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
653 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
654 }
655
656 /* Figure out how much data to read from the local page */
657 U = pCsr->nPage;
658 if( bHasRowid ){
659 X = U-35;
660 }else{
661 X = ((U-12)*64/255)-23;
662 }
663 if( nPayload<=X ){
664 nLocal = nPayload;
665 }else{
666 int M, K;
667 M = ((U-12)*32/255)-23;
668 K = M+((nPayload-M)%(U-4));
669 if( K<=X ){
670 nLocal = K;
671 }else{
672 nLocal = M;
673 }
674 }
675
676 if( bNextPage || nLocal+iOff>pCsr->nPage ){
677 bNextPage = 1;
678 }else{
679
680 /* Allocate space for payload. And a bit more to catch small buffer
681 ** overruns caused by attempting to read a varint or similar from
682 ** near the end of a corrupt record. */
683 rc = dbdataBufferSize(&pCsr->rec, nPayload+DBDATA_PADDING_BYTES);
684 if( rc!=SQLITE_OK ) return rc;
685 assert( nPayload!=0 );
686
687 /* Load the nLocal bytes of payload */
688 memcpy(pCsr->rec.aBuf, &pCsr->aPage[iOff], nLocal);
689 iOff += nLocal;
690
691 /* Load content from overflow pages */
692 if( nPayload>nLocal ){
693 sqlite3_int64 nRem = nPayload - nLocal;
694 u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
695 while( nRem>0 ){
696 u8 *aOvfl = 0;
697 int nOvfl = 0;
698 int nCopy;
699 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
700 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
701 if( rc!=SQLITE_OK ) return rc;
702 if( aOvfl==0 ) break;
703
704 nCopy = U-4;
705 if( nCopy>nRem ) nCopy = nRem;
706 memcpy(&pCsr->rec.aBuf[nPayload-nRem], &aOvfl[4], nCopy);
707 nRem -= nCopy;
708
709 pgnoOvfl = get_uint32(aOvfl);
710 sqlite3_free(aOvfl);
711 }
712 nPayload -= nRem;
713 }
714 memset(&pCsr->rec.aBuf[nPayload], 0, DBDATA_PADDING_BYTES);
715 pCsr->nRec = nPayload;
716
717 iHdr = dbdataGetVarintU32(pCsr->rec.aBuf, &nHdr);
718 if( nHdr>nPayload ) nHdr = 0;
719 pCsr->nHdr = nHdr;
720 pCsr->pHdrPtr = &pCsr->rec.aBuf[iHdr];
721 pCsr->pPtr = &pCsr->rec.aBuf[pCsr->nHdr];
722 pCsr->iField = (bHasRowid ? -1 : 0);
723 }
724 }
725 }else{
726 pCsr->iField++;
727 if( pCsr->iField>0 ){
728 sqlite3_int64 iType;
729 if( pCsr->pHdrPtr>=&pCsr->rec.aBuf[pCsr->nRec]
730 || pCsr->iField>=DBDATA_MX_FIELD
731 ){
732 bNextPage = 1;
733 }else{
734 int szField = 0;
735 pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
736 szField = dbdataValueBytes(iType);
737 if( (pCsr->nRec - (pCsr->pPtr - pCsr->rec.aBuf))<szField ){
738 pCsr->pPtr = &pCsr->rec.aBuf[pCsr->nRec];
739 }else{
740 pCsr->pPtr += szField;
741 }
742 }
743 }
744 }
745
746 if( bNextPage ){
747 sqlite3_free(pCsr->aPage);
748 pCsr->aPage = 0;
749 pCsr->nRec = 0;
750 if( pCsr->bOnePage ) return SQLITE_OK;
751 pCsr->iPgno++;
752 }else{
753 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->rec.aBuf[pCsr->nHdr] ){
754 return SQLITE_OK;
755 }
756
757 /* Advance to the next cell. The next iteration of the loop will load
758 ** the record and so on. */
759 pCsr->nRec = 0;
760 pCsr->iCell++;
761 }
762 }
763 }
764
765 assert( !"can't get here" );
766 return SQLITE_OK;
767 }
768
769 /*
770 ** Return true if the cursor is at EOF.
771 */
772 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
773 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
774 return pCsr->aPage==0;
775 }
776
777 /*
778 ** Return true if nul-terminated string zSchema ends in "()". Or false
779 ** otherwise.
780 */
781 static int dbdataIsFunction(const char *zSchema){
782 size_t n = strlen(zSchema);
783 if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){
784 return (int)n-2;
785 }
786 return 0;
787 }
788
789 /*
790 ** Determine the size in pages of database zSchema (where zSchema is
791 ** "main", "temp" or the name of an attached database) and set
792 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
793 ** an SQLite error code.
794 */
795 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
796 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
797 char *zSql = 0;
798 int rc, rc2;
799 int nFunc = 0;
800 sqlite3_stmt *pStmt = 0;
801
802 if( (nFunc = dbdataIsFunction(zSchema))>0 ){
803 zSql = sqlite3_mprintf("SELECT %.*s(0)", nFunc, zSchema);
804 }else{
805 zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
806 }
807 if( zSql==0 ) return SQLITE_NOMEM;
808
809 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
810 sqlite3_free(zSql);
811 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
812 pCsr->szDb = sqlite3_column_int(pStmt, 0);
813 }
814 rc2 = sqlite3_finalize(pStmt);
815 if( rc==SQLITE_OK ) rc = rc2;
816 return rc;
817 }
818
819 /*
820 ** Attempt to figure out the encoding of the database by retrieving page 1
821 ** and inspecting the header field. If successful, set the pCsr->enc variable
822 ** and return SQLITE_OK. Otherwise, return an SQLite error code.
823 */
824 static int dbdataGetEncoding(DbdataCursor *pCsr){
825 int rc = SQLITE_OK;
826 int nPg1 = 0;
827 u8 *aPg1 = 0;
828 rc = dbdataLoadPage(pCsr, 1, &aPg1, &nPg1);
829 if( rc==SQLITE_OK && nPg1>=(56+4) ){
830 pCsr->enc = get_uint32(&aPg1[56]);
831 }
832 sqlite3_free(aPg1);
833 return rc;
834 }
835
836
837 /*
838 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
839 */
840 static int dbdataFilter(
841 sqlite3_vtab_cursor *pCursor,
842 int idxNum, const char *idxStr,
843 int argc, sqlite3_value **argv
844 ){
845 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
846 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
847 int rc = SQLITE_OK;
848 const char *zSchema = "main";
849 (void)idxStr;
850 (void)argc;
851
852 dbdataResetCursor(pCsr);
853 assert( pCsr->iPgno==1 );
854 if( idxNum & 0x01 ){
855 zSchema = (const char*)sqlite3_value_text(argv[0]);
856 if( zSchema==0 ) zSchema = "";
857 }
858 if( idxNum & 0x02 ){
859 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
860 pCsr->bOnePage = 1;
861 }else{
862 rc = dbdataDbsize(pCsr, zSchema);
863 }
864
865 if( rc==SQLITE_OK ){
866 int nFunc = 0;
867 if( pTab->pStmt ){
868 pCsr->pStmt = pTab->pStmt;
869 pTab->pStmt = 0;
870 }else if( (nFunc = dbdataIsFunction(zSchema))>0 ){
871 char *zSql = sqlite3_mprintf("SELECT %.*s(?2)", nFunc, zSchema);
872 if( zSql==0 ){
873 rc = SQLITE_NOMEM;
874 }else{
875 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
876 sqlite3_free(zSql);
877 }
878 }else{
879 rc = sqlite3_prepare_v2(pTab->db,
880 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
881 &pCsr->pStmt, 0
882 );
883 }
884 }
885 if( rc==SQLITE_OK ){
886 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
887 }
888
889 /* Try to determine the encoding of the db by inspecting the header
890 ** field on page 1. */
891 if( rc==SQLITE_OK ){
892 rc = dbdataGetEncoding(pCsr);
893 }
894
895 if( rc!=SQLITE_OK ){
896 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
897 }
898
899 if( rc==SQLITE_OK ){
900 rc = dbdataNext(pCursor);
901 }
902 return rc;
903 }
904
905 /*
906 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
907 */
908 static int dbdataColumn(
909 sqlite3_vtab_cursor *pCursor,
910 sqlite3_context *ctx,
911 int i
912 ){
913 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
914 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
915 if( pTab->bPtr ){
916 switch( i ){
917 case DBPTR_COLUMN_PGNO:
918 sqlite3_result_int64(ctx, pCsr->iPgno);
919 break;
920 case DBPTR_COLUMN_CHILD: {
921 int iOff = pCsr->iPgno==1 ? 100 : 0;
922 if( pCsr->iCell<0 ){
923 iOff += 8;
924 }else{
925 iOff += 12 + pCsr->iCell*2;
926 if( iOff>pCsr->nPage ) return SQLITE_OK;
927 iOff = get_uint16(&pCsr->aPage[iOff]);
928 }
929 if( iOff<=pCsr->nPage ){
930 sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
931 }
932 break;
933 }
934 }
935 }else{
936 switch( i ){
937 case DBDATA_COLUMN_PGNO:
938 sqlite3_result_int64(ctx, pCsr->iPgno);
939 break;
940 case DBDATA_COLUMN_CELL:
941 sqlite3_result_int(ctx, pCsr->iCell);
942 break;
943 case DBDATA_COLUMN_FIELD:
944 sqlite3_result_int(ctx, pCsr->iField);
945 break;
946 case DBDATA_COLUMN_VALUE: {
947 if( pCsr->iField<0 ){
948 sqlite3_result_int64(ctx, pCsr->iIntkey);
949 }else if( &pCsr->rec.aBuf[pCsr->nRec] >= pCsr->pPtr ){
950 sqlite3_int64 iType;
951 dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
952 dbdataValue(
953 ctx, pCsr->enc, iType, pCsr->pPtr,
954 &pCsr->rec.aBuf[pCsr->nRec] - pCsr->pPtr
955 );
956 }
957 break;
958 }
959 }
960 }
961 return SQLITE_OK;
962 }
963
964 /*
965 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
966 */
967 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
968 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
969 *pRowid = pCsr->iRowid;
970 return SQLITE_OK;
971 }
972
973
974 /*
975 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
976 */
977 static int sqlite3DbdataRegister(sqlite3 *db){
978 static sqlite3_module dbdata_module = {
979 0, /* iVersion */
980 0, /* xCreate */
981 dbdataConnect, /* xConnect */
982 dbdataBestIndex, /* xBestIndex */
983 dbdataDisconnect, /* xDisconnect */
984 0, /* xDestroy */
985 dbdataOpen, /* xOpen - open a cursor */
986 dbdataClose, /* xClose - close a cursor */
987 dbdataFilter, /* xFilter - configure scan constraints */
988 dbdataNext, /* xNext - advance a cursor */
989 dbdataEof, /* xEof - check for end of scan */
990 dbdataColumn, /* xColumn - read data */
991 dbdataRowid, /* xRowid - read data */
992 0, /* xUpdate */
993 0, /* xBegin */
994 0, /* xSync */
995 0, /* xCommit */
996 0, /* xRollback */
997 0, /* xFindMethod */
998 0, /* xRename */
999 0, /* xSavepoint */
1000 0, /* xRelease */
1001 0, /* xRollbackTo */
1002 0, /* xShadowName */
1003 0 /* xIntegrity */
1004 };
1005
1006 int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
1007 if( rc==SQLITE_OK ){
1008 rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
1009 }
1010 return rc;
1011 }
1012
1013 int sqlite3_dbdata_init(
1014 sqlite3 *db,
1015 char **pzErrMsg,
1016 const sqlite3_api_routines *pApi
1017 ){
1018 (void)pzErrMsg;
1019 return sqlite3DbdataRegister(db);
1020 }
1021
1022 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
1023 #pragma GCC diagnostic pop