dbdata.c 27 KB

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