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.

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