xref: /sqlite-3.40.0/ext/recover/dbdata.c (revision 4df2ab57)
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   sqlite3_int64 nRec;             /* Size of pRec[] in bytes */
107   sqlite3_int64 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   if( pgno>0 ){
337     sqlite3_bind_int64(pStmt, 2, pgno);
338     if( SQLITE_ROW==sqlite3_step(pStmt) ){
339       int nCopy = sqlite3_column_bytes(pStmt, 0);
340       if( nCopy>0 ){
341         u8 *pPage;
342         pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
343         if( pPage==0 ){
344           rc = SQLITE_NOMEM;
345         }else{
346           const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
347           memcpy(pPage, pCopy, nCopy);
348           memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
349         }
350         *ppPage = pPage;
351         *pnPage = nCopy;
352       }
353     }
354     rc2 = sqlite3_reset(pStmt);
355     if( rc==SQLITE_OK ) rc = rc2;
356   }
357 
358   return rc;
359 }
360 
361 /*
362 ** Read a varint.  Put the value in *pVal and return the number of bytes.
363 */
364 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
365   sqlite3_uint64 u = 0;
366   int i;
367   for(i=0; i<8; i++){
368     u = (u<<7) + (z[i]&0x7f);
369     if( (z[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
370   }
371   u = (u<<8) + (z[i]&0xff);
372   *pVal = (sqlite3_int64)u;
373   return 9;
374 }
375 
376 /*
377 ** Like dbdataGetVarint(), but set the output to 0 if it is less than 0
378 ** or greater than 0xFFFFFFFF. This can be used for all varints in an
379 ** SQLite database except for key values in intkey tables.
380 */
381 static int dbdataGetVarintU32(const u8 *z, sqlite3_int64 *pVal){
382   sqlite3_int64 val;
383   int nRet = dbdataGetVarint(z, &val);
384   if( val<0 || val>0xFFFFFFFF ) val = 0;
385   *pVal = val;
386   return nRet;
387 }
388 
389 /*
390 ** Return the number of bytes of space used by an SQLite value of type
391 ** eType.
392 */
393 static int dbdataValueBytes(int eType){
394   switch( eType ){
395     case 0: case 8: case 9:
396     case 10: case 11:
397       return 0;
398     case 1:
399       return 1;
400     case 2:
401       return 2;
402     case 3:
403       return 3;
404     case 4:
405       return 4;
406     case 5:
407       return 6;
408     case 6:
409     case 7:
410       return 8;
411     default:
412       if( eType>0 ){
413         return ((eType-12) / 2);
414       }
415       return 0;
416   }
417 }
418 
419 /*
420 ** Load a value of type eType from buffer pData and use it to set the
421 ** result of context object pCtx.
422 */
423 static void dbdataValue(
424   sqlite3_context *pCtx,
425   u32 enc,
426   int eType,
427   u8 *pData,
428   int nData
429 ){
430   if( eType>=0 && dbdataValueBytes(eType)<=nData ){
431     switch( eType ){
432       case 0:
433       case 10:
434       case 11:
435         sqlite3_result_null(pCtx);
436         break;
437 
438       case 8:
439         sqlite3_result_int(pCtx, 0);
440         break;
441       case 9:
442         sqlite3_result_int(pCtx, 1);
443         break;
444 
445       case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
446         sqlite3_uint64 v = (signed char)pData[0];
447         pData++;
448         switch( eType ){
449           case 7:
450           case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
451           case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
452           case 4:  v = (v<<8) + pData[0];  pData++;
453           case 3:  v = (v<<8) + pData[0];  pData++;
454           case 2:  v = (v<<8) + pData[0];  pData++;
455         }
456 
457         if( eType==7 ){
458           double r;
459           memcpy(&r, &v, sizeof(r));
460           sqlite3_result_double(pCtx, r);
461         }else{
462           sqlite3_result_int64(pCtx, (sqlite3_int64)v);
463         }
464         break;
465       }
466 
467       default: {
468         int n = ((eType-12) / 2);
469         if( eType % 2 ){
470           switch( enc ){
471 #ifndef SQLITE_OMIT_UTF16
472             case SQLITE_UTF16BE:
473               sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
474               break;
475             case SQLITE_UTF16LE:
476               sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
477               break;
478 #endif
479             default:
480               sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
481               break;
482           }
483         }else{
484           sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
485         }
486       }
487     }
488   }
489 }
490 
491 /*
492 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
493 */
494 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
495   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
496   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
497 
498   pCsr->iRowid++;
499   while( 1 ){
500     int rc;
501     int iOff = (pCsr->iPgno==1 ? 100 : 0);
502     int bNextPage = 0;
503 
504     if( pCsr->aPage==0 ){
505       while( 1 ){
506         if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
507         rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
508         if( rc!=SQLITE_OK ) return rc;
509         if( pCsr->aPage ) break;
510         if( pCsr->bOnePage ) return SQLITE_OK;
511         pCsr->iPgno++;
512       }
513       pCsr->iCell = pTab->bPtr ? -2 : 0;
514       pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
515     }
516 
517     if( pTab->bPtr ){
518       if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
519         pCsr->iCell = pCsr->nCell;
520       }
521       pCsr->iCell++;
522       if( pCsr->iCell>=pCsr->nCell ){
523         sqlite3_free(pCsr->aPage);
524         pCsr->aPage = 0;
525         if( pCsr->bOnePage ) return SQLITE_OK;
526         pCsr->iPgno++;
527       }else{
528         return SQLITE_OK;
529       }
530     }else{
531       /* If there is no record loaded, load it now. */
532       if( pCsr->pRec==0 ){
533         int bHasRowid = 0;
534         int nPointer = 0;
535         sqlite3_int64 nPayload = 0;
536         sqlite3_int64 nHdr = 0;
537         int iHdr;
538         int U, X;
539         int nLocal;
540 
541         switch( pCsr->aPage[iOff] ){
542           case 0x02:
543             nPointer = 4;
544             break;
545           case 0x0a:
546             break;
547           case 0x0d:
548             bHasRowid = 1;
549             break;
550           default:
551             /* This is not a b-tree page with records on it. Continue. */
552             pCsr->iCell = pCsr->nCell;
553             break;
554         }
555 
556         if( pCsr->iCell>=pCsr->nCell ){
557           bNextPage = 1;
558         }else{
559 
560           iOff += 8 + nPointer + pCsr->iCell*2;
561           if( iOff>pCsr->nPage ){
562             bNextPage = 1;
563           }else{
564             iOff = get_uint16(&pCsr->aPage[iOff]);
565           }
566 
567           /* For an interior node cell, skip past the child-page number */
568           iOff += nPointer;
569 
570           /* Load the "byte of payload including overflow" field */
571           if( bNextPage || iOff>pCsr->nPage ){
572             bNextPage = 1;
573           }else{
574             iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
575           }
576 
577           /* If this is a leaf intkey cell, load the rowid */
578           if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
579             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
580           }
581 
582           /* Figure out how much data to read from the local page */
583           U = pCsr->nPage;
584           if( bHasRowid ){
585             X = U-35;
586           }else{
587             X = ((U-12)*64/255)-23;
588           }
589           if( nPayload<=X ){
590             nLocal = nPayload;
591           }else{
592             int M, K;
593             M = ((U-12)*32/255)-23;
594             K = M+((nPayload-M)%(U-4));
595             if( K<=X ){
596               nLocal = K;
597             }else{
598               nLocal = M;
599             }
600           }
601 
602           if( bNextPage || nLocal+iOff>pCsr->nPage ){
603             bNextPage = 1;
604           }else{
605 
606             /* Allocate space for payload. And a bit more to catch small buffer
607             ** overruns caused by attempting to read a varint or similar from
608             ** near the end of a corrupt record.  */
609             pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
610             if( pCsr->pRec==0 ) return SQLITE_NOMEM;
611             memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
612             pCsr->nRec = nPayload;
613 
614             /* Load the nLocal bytes of payload */
615             memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
616             iOff += nLocal;
617 
618             /* Load content from overflow pages */
619             if( nPayload>nLocal ){
620               sqlite3_int64 nRem = nPayload - nLocal;
621               u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
622               while( nRem>0 ){
623                 u8 *aOvfl = 0;
624                 int nOvfl = 0;
625                 int nCopy;
626                 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
627                 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
628                 if( rc!=SQLITE_OK ) return rc;
629                 if( aOvfl==0 ) break;
630 
631                 nCopy = U-4;
632                 if( nCopy>nRem ) nCopy = nRem;
633                 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
634                 nRem -= nCopy;
635 
636                 pgnoOvfl = get_uint32(aOvfl);
637                 sqlite3_free(aOvfl);
638               }
639             }
640 
641             iHdr = dbdataGetVarintU32(pCsr->pRec, &nHdr);
642             if( nHdr>nPayload ) nHdr = 0;
643             pCsr->nHdr = nHdr;
644             pCsr->pHdrPtr = &pCsr->pRec[iHdr];
645             pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
646             pCsr->iField = (bHasRowid ? -1 : 0);
647           }
648         }
649       }else{
650         pCsr->iField++;
651         if( pCsr->iField>0 ){
652           sqlite3_int64 iType;
653           if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
654             bNextPage = 1;
655           }else{
656             pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
657             pCsr->pPtr += dbdataValueBytes(iType);
658           }
659         }
660       }
661 
662       if( bNextPage ){
663         sqlite3_free(pCsr->aPage);
664         sqlite3_free(pCsr->pRec);
665         pCsr->aPage = 0;
666         pCsr->pRec = 0;
667         if( pCsr->bOnePage ) return SQLITE_OK;
668         pCsr->iPgno++;
669       }else{
670         if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
671           return SQLITE_OK;
672         }
673 
674         /* Advance to the next cell. The next iteration of the loop will load
675         ** the record and so on. */
676         sqlite3_free(pCsr->pRec);
677         pCsr->pRec = 0;
678         pCsr->iCell++;
679       }
680     }
681   }
682 
683   assert( !"can't get here" );
684   return SQLITE_OK;
685 }
686 
687 /*
688 ** Return true if the cursor is at EOF.
689 */
690 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
691   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
692   return pCsr->aPage==0;
693 }
694 
695 /*
696 ** Return true if nul-terminated string zSchema ends in "()". Or false
697 ** otherwise.
698 */
699 static int dbdataIsFunction(const char *zSchema){
700   size_t n = strlen(zSchema);
701   if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){
702     return (int)n-2;
703   }
704   return 0;
705 }
706 
707 /*
708 ** Determine the size in pages of database zSchema (where zSchema is
709 ** "main", "temp" or the name of an attached database) and set
710 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
711 ** an SQLite error code.
712 */
713 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
714   DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
715   char *zSql = 0;
716   int rc, rc2;
717   int nFunc = 0;
718   sqlite3_stmt *pStmt = 0;
719 
720   if( (nFunc = dbdataIsFunction(zSchema))>0 ){
721     zSql = sqlite3_mprintf("SELECT %.*s(0)", nFunc, zSchema);
722   }else{
723     zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
724   }
725   if( zSql==0 ) return SQLITE_NOMEM;
726 
727   rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
728   sqlite3_free(zSql);
729   if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
730     pCsr->szDb = sqlite3_column_int(pStmt, 0);
731   }
732   rc2 = sqlite3_finalize(pStmt);
733   if( rc==SQLITE_OK ) rc = rc2;
734   return rc;
735 }
736 
737 /*
738 ** Attempt to figure out the encoding of the database by retrieving page 1
739 ** and inspecting the header field. If successful, set the pCsr->enc variable
740 ** and return SQLITE_OK. Otherwise, return an SQLite error code.
741 */
742 static int dbdataGetEncoding(DbdataCursor *pCsr){
743   int rc = SQLITE_OK;
744   int nPg1 = 0;
745   u8 *aPg1 = 0;
746   rc = dbdataLoadPage(pCsr, 1, &aPg1, &nPg1);
747   assert( rc!=SQLITE_OK || nPg1==0 || nPg1>=512 );
748   if( rc==SQLITE_OK && nPg1>0 ){
749     pCsr->enc = get_uint32(&aPg1[56]);
750   }
751   sqlite3_free(aPg1);
752   return rc;
753 }
754 
755 
756 /*
757 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
758 */
759 static int dbdataFilter(
760   sqlite3_vtab_cursor *pCursor,
761   int idxNum, const char *idxStr,
762   int argc, sqlite3_value **argv
763 ){
764   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
765   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
766   int rc = SQLITE_OK;
767   const char *zSchema = "main";
768 
769   dbdataResetCursor(pCsr);
770   assert( pCsr->iPgno==1 );
771   if( idxNum & 0x01 ){
772     zSchema = (const char*)sqlite3_value_text(argv[0]);
773     if( zSchema==0 ) zSchema = "";
774   }
775   if( idxNum & 0x02 ){
776     pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
777     pCsr->bOnePage = 1;
778   }else{
779     rc = dbdataDbsize(pCsr, zSchema);
780   }
781 
782   if( rc==SQLITE_OK ){
783     int nFunc = 0;
784     if( pTab->pStmt ){
785       pCsr->pStmt = pTab->pStmt;
786       pTab->pStmt = 0;
787     }else if( (nFunc = dbdataIsFunction(zSchema))>0 ){
788       char *zSql = sqlite3_mprintf("SELECT %.*s(?2)", nFunc, zSchema);
789       if( zSql==0 ){
790         rc = SQLITE_NOMEM;
791       }else{
792         rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
793         sqlite3_free(zSql);
794       }
795     }else{
796       rc = sqlite3_prepare_v2(pTab->db,
797           "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
798           &pCsr->pStmt, 0
799       );
800     }
801   }
802   if( rc==SQLITE_OK ){
803     rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
804   }else{
805     pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
806   }
807 
808   /* Try to determine the encoding of the db by inspecting the header
809   ** field on page 1. */
810   if( rc==SQLITE_OK ){
811     rc = dbdataGetEncoding(pCsr);
812   }
813 
814   if( rc==SQLITE_OK ){
815     rc = dbdataNext(pCursor);
816   }
817   return rc;
818 }
819 
820 /*
821 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
822 */
823 static int dbdataColumn(
824   sqlite3_vtab_cursor *pCursor,
825   sqlite3_context *ctx,
826   int i
827 ){
828   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
829   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
830   if( pTab->bPtr ){
831     switch( i ){
832       case DBPTR_COLUMN_PGNO:
833         sqlite3_result_int64(ctx, pCsr->iPgno);
834         break;
835       case DBPTR_COLUMN_CHILD: {
836         int iOff = pCsr->iPgno==1 ? 100 : 0;
837         if( pCsr->iCell<0 ){
838           iOff += 8;
839         }else{
840           iOff += 12 + pCsr->iCell*2;
841           if( iOff>pCsr->nPage ) return SQLITE_OK;
842           iOff = get_uint16(&pCsr->aPage[iOff]);
843         }
844         if( iOff<=pCsr->nPage ){
845           sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
846         }
847         break;
848       }
849     }
850   }else{
851     switch( i ){
852       case DBDATA_COLUMN_PGNO:
853         sqlite3_result_int64(ctx, pCsr->iPgno);
854         break;
855       case DBDATA_COLUMN_CELL:
856         sqlite3_result_int(ctx, pCsr->iCell);
857         break;
858       case DBDATA_COLUMN_FIELD:
859         sqlite3_result_int(ctx, pCsr->iField);
860         break;
861       case DBDATA_COLUMN_VALUE: {
862         if( pCsr->iField<0 ){
863           sqlite3_result_int64(ctx, pCsr->iIntkey);
864         }else{
865           sqlite3_int64 iType;
866           dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
867           dbdataValue(
868               ctx, pCsr->enc, iType, pCsr->pPtr,
869               &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
870           );
871         }
872         break;
873       }
874     }
875   }
876   return SQLITE_OK;
877 }
878 
879 /*
880 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
881 */
882 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
883   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
884   *pRowid = pCsr->iRowid;
885   return SQLITE_OK;
886 }
887 
888 
889 /*
890 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
891 */
892 static int sqlite3DbdataRegister(sqlite3 *db){
893   static sqlite3_module dbdata_module = {
894     0,                            /* iVersion */
895     0,                            /* xCreate */
896     dbdataConnect,                /* xConnect */
897     dbdataBestIndex,              /* xBestIndex */
898     dbdataDisconnect,             /* xDisconnect */
899     0,                            /* xDestroy */
900     dbdataOpen,                   /* xOpen - open a cursor */
901     dbdataClose,                  /* xClose - close a cursor */
902     dbdataFilter,                 /* xFilter - configure scan constraints */
903     dbdataNext,                   /* xNext - advance a cursor */
904     dbdataEof,                    /* xEof - check for end of scan */
905     dbdataColumn,                 /* xColumn - read data */
906     dbdataRowid,                  /* xRowid - read data */
907     0,                            /* xUpdate */
908     0,                            /* xBegin */
909     0,                            /* xSync */
910     0,                            /* xCommit */
911     0,                            /* xRollback */
912     0,                            /* xFindMethod */
913     0,                            /* xRename */
914     0,                            /* xSavepoint */
915     0,                            /* xRelease */
916     0,                            /* xRollbackTo */
917     0                             /* xShadowName */
918   };
919 
920   int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
921   if( rc==SQLITE_OK ){
922     rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
923   }
924   return rc;
925 }
926 
927 #ifdef _WIN32
928 __declspec(dllexport)
929 #endif
930 int sqlite3_dbdata_init(
931   sqlite3 *db,
932   char **pzErrMsg,
933   const sqlite3_api_routines *pApi
934 ){
935   SQLITE_EXTENSION_INIT2(pApi);
936   return sqlite3DbdataRegister(db);
937 }
938