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