Debian Codegraph @

ext/recover/dbdata.c

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