You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

955 lines
27 KiB

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