showdb.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /*
  2. ** A utility for printing all or part of an SQLite database file.
  3. */
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #define ISDIGIT(X) isdigit((unsigned char)(X))
  7. #define ISPRINT(X) isprint((unsigned char)(X))
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #if !defined(_MSC_VER)
  12. #include <unistd.h>
  13. #else
  14. #include <io.h>
  15. #endif
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <assert.h>
  19. #include "sqlite3.h"
  20. typedef unsigned char u8; /* unsigned 8-bit */
  21. typedef unsigned int u32; /* unsigned 32-bit */
  22. typedef sqlite3_int64 i64; /* signed 64-bit */
  23. typedef sqlite3_uint64 u64; /* unsigned 64-bit */
  24. static struct GlobalData {
  25. u32 pagesize; /* Size of a database page */
  26. int dbfd; /* File descriptor for reading the DB */
  27. u32 mxPage; /* Last page number */
  28. int perLine; /* HEX elements to print per line */
  29. int bRaw; /* True to access db file via OS APIs */
  30. sqlite3_file *pFd; /* File descriptor for non-raw mode */
  31. sqlite3 *pDb; /* Database handle that owns pFd */
  32. } g = {1024, -1, 0, 16, 0, 0, 0};
  33. /*
  34. ** Convert the var-int format into i64. Return the number of bytes
  35. ** in the var-int. Write the var-int value into *pVal.
  36. */
  37. static int decodeVarint(const unsigned char *z, i64 *pVal){
  38. i64 v = 0;
  39. int i;
  40. for(i=0; i<8; i++){
  41. v = (v<<7) + (z[i]&0x7f);
  42. if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
  43. }
  44. v = (v<<8) + (z[i]&0xff);
  45. *pVal = v;
  46. return 9;
  47. }
  48. /*
  49. ** Extract a big-endian 32-bit integer
  50. */
  51. static u32 decodeInt32(const u8 *z){
  52. return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3];
  53. }
  54. /* Report an out-of-memory error and die.
  55. */
  56. static void out_of_memory(void){
  57. fprintf(stderr,"Out of memory...\n");
  58. exit(1);
  59. }
  60. /*
  61. ** Open a database connection.
  62. */
  63. static sqlite3 *openDatabase(const char *zPrg, const char *zName){
  64. sqlite3 *db = 0;
  65. int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI;
  66. int rc = sqlite3_open_v2(zName, &db, flags, 0);
  67. if( rc!=SQLITE_OK ){
  68. const char *zErr = sqlite3_errmsg(db);
  69. fprintf(stderr, "%s: can't open %s (%s)\n", zPrg, zName, zErr);
  70. sqlite3_close(db);
  71. exit(1);
  72. }
  73. return db;
  74. }
  75. /**************************************************************************
  76. ** Beginning of low-level file access functions.
  77. **
  78. ** All low-level access to the database file read by this program is
  79. ** performed using the following four functions:
  80. **
  81. ** fileOpen() - open the db file
  82. ** fileClose() - close the db file
  83. ** fileRead() - read raw data from the db file
  84. ** fileGetsize() - return the size of the db file in bytes
  85. */
  86. /*
  87. ** Open the database file.
  88. */
  89. static void fileOpen(const char *zPrg, const char *zName){
  90. assert( g.dbfd<0 );
  91. if( g.bRaw==0 ){
  92. int rc;
  93. void *pArg = (void *)(&g.pFd);
  94. g.pDb = openDatabase(zPrg, zName);
  95. rc = sqlite3_file_control(g.pDb, "main", SQLITE_FCNTL_FILE_POINTER, pArg);
  96. if( rc!=SQLITE_OK ){
  97. fprintf(stderr,
  98. "%s: failed to obtain fd for %s (SQLite too old?)\n", zPrg, zName
  99. );
  100. exit(1);
  101. }
  102. }else{
  103. g.dbfd = open(zName, O_RDONLY);
  104. if( g.dbfd<0 ){
  105. fprintf(stderr,"%s: can't open %s\n", zPrg, zName);
  106. exit(1);
  107. }
  108. }
  109. }
  110. /*
  111. ** Close the database file opened by fileOpen()
  112. */
  113. static void fileClose(){
  114. if( g.bRaw==0 ){
  115. sqlite3_close(g.pDb);
  116. g.pDb = 0;
  117. g.pFd = 0;
  118. }else{
  119. close(g.dbfd);
  120. g.dbfd = -1;
  121. }
  122. }
  123. /*
  124. ** Read content from the file.
  125. **
  126. ** Space to hold the content is obtained from sqlite3_malloc() and needs
  127. ** to be freed by the caller.
  128. */
  129. static unsigned char *fileRead(sqlite3_int64 ofst, int nByte){
  130. unsigned char *aData;
  131. int got;
  132. aData = sqlite3_malloc64(32+(i64)nByte);
  133. if( aData==0 ) out_of_memory();
  134. memset(aData, 0, nByte+32);
  135. if( g.bRaw==0 ){
  136. int rc = g.pFd->pMethods->xRead(g.pFd, (void*)aData, nByte, ofst);
  137. if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
  138. fprintf(stderr, "error in xRead() - %d\n", rc);
  139. exit(1);
  140. }
  141. }else{
  142. lseek(g.dbfd, (long)ofst, SEEK_SET);
  143. got = read(g.dbfd, aData, nByte);
  144. if( got>0 && got<nByte ) memset(aData+got, 0, nByte-got);
  145. }
  146. return aData;
  147. }
  148. /*
  149. ** Return the size of the file in byte.
  150. */
  151. static i64 fileGetsize(void){
  152. i64 res = 0;
  153. if( g.bRaw==0 ){
  154. int rc = g.pFd->pMethods->xFileSize(g.pFd, &res);
  155. if( rc!=SQLITE_OK ){
  156. fprintf(stderr, "error in xFileSize() - %d\n", rc);
  157. exit(1);
  158. }
  159. }else{
  160. struct stat sbuf;
  161. fstat(g.dbfd, &sbuf);
  162. res = (sqlite3_int64)(sbuf.st_size);
  163. }
  164. return res;
  165. }
  166. /*
  167. ** End of low-level file access functions.
  168. **************************************************************************/
  169. /*
  170. ** Print a range of bytes as hex and as ascii.
  171. */
  172. static unsigned char *print_byte_range(
  173. sqlite3_int64 ofst, /* First byte in the range of bytes to print */
  174. int nByte, /* Number of bytes to print */
  175. int printOfst /* Add this amount to the index on the left column */
  176. ){
  177. unsigned char *aData;
  178. int i, j;
  179. const char *zOfstFmt;
  180. if( ((printOfst+nByte)&~0xfff)==0 ){
  181. zOfstFmt = " %03x: ";
  182. }else if( ((printOfst+nByte)&~0xffff)==0 ){
  183. zOfstFmt = " %04x: ";
  184. }else if( ((printOfst+nByte)&~0xfffff)==0 ){
  185. zOfstFmt = " %05x: ";
  186. }else if( ((printOfst+nByte)&~0xffffff)==0 ){
  187. zOfstFmt = " %06x: ";
  188. }else{
  189. zOfstFmt = " %08x: ";
  190. }
  191. aData = fileRead(ofst, nByte);
  192. for(i=0; i<nByte; i += g.perLine){
  193. int go = 0;
  194. for(j=0; j<g.perLine; j++){
  195. if( i+j>nByte ){ break; }
  196. if( aData[i+j] ){ go = 1; break; }
  197. }
  198. if( !go && i>0 && i+g.perLine<nByte ) continue;
  199. fprintf(stdout, zOfstFmt, i+printOfst);
  200. for(j=0; j<g.perLine; j++){
  201. if( i+j>nByte ){
  202. fprintf(stdout, " ");
  203. }else{
  204. fprintf(stdout,"%02x ", aData[i+j]);
  205. }
  206. }
  207. for(j=0; j<g.perLine; j++){
  208. if( i+j>nByte ){
  209. fprintf(stdout, " ");
  210. }else{
  211. fprintf(stdout,"%c", ISPRINT(aData[i+j]) ? aData[i+j] : '.');
  212. }
  213. }
  214. fprintf(stdout,"\n");
  215. }
  216. return aData;
  217. }
  218. /*
  219. ** Print an entire page of content as hex
  220. */
  221. static void print_page(u32 iPg){
  222. i64 iStart;
  223. unsigned char *aData;
  224. iStart = ((i64)(iPg-1))*g.pagesize;
  225. fprintf(stdout, "Page %u: (offsets 0x%llx..0x%llx)\n",
  226. iPg, iStart, iStart+g.pagesize-1);
  227. aData = print_byte_range(iStart, g.pagesize, 0);
  228. sqlite3_free(aData);
  229. }
  230. /* Print a line of decoded output showing a 4-byte unsigned integer.
  231. */
  232. static void print_decode_line(
  233. unsigned char *aData, /* Content being decoded */
  234. int ofst, int nByte, /* Start and size of decode */
  235. const char *zMsg /* Message to append */
  236. ){
  237. int i, j;
  238. u32 val = aData[ofst];
  239. char zBuf[100];
  240. sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
  241. i = (int)strlen(zBuf);
  242. for(j=1; j<4; j++){
  243. if( j>=nByte ){
  244. sprintf(&zBuf[i], " ");
  245. }else{
  246. sprintf(&zBuf[i], " %02x", aData[ofst+j]);
  247. val = val*256 + aData[ofst+j];
  248. }
  249. i += (int)strlen(&zBuf[i]);
  250. }
  251. sprintf(&zBuf[i], " %10u", val);
  252. printf("%s %s\n", zBuf, zMsg);
  253. }
  254. /*
  255. ** Decode the database header.
  256. */
  257. static void print_db_header(void){
  258. unsigned char *aData;
  259. aData = print_byte_range(0, 100, 0);
  260. printf("Decoded:\n");
  261. print_decode_line(aData, 16, 2, "Database page size");
  262. print_decode_line(aData, 18, 1, "File format write version");
  263. print_decode_line(aData, 19, 1, "File format read version");
  264. print_decode_line(aData, 20, 1, "Reserved space at end of page");
  265. print_decode_line(aData, 24, 4, "File change counter");
  266. print_decode_line(aData, 28, 4, "Size of database in pages");
  267. print_decode_line(aData, 32, 4, "Page number of first freelist page");
  268. print_decode_line(aData, 36, 4, "Number of freelist pages");
  269. print_decode_line(aData, 40, 4, "Schema cookie");
  270. print_decode_line(aData, 44, 4, "Schema format version");
  271. print_decode_line(aData, 48, 4, "Default page cache size");
  272. print_decode_line(aData, 52, 4, "Largest auto-vac root page");
  273. print_decode_line(aData, 56, 4, "Text encoding");
  274. print_decode_line(aData, 60, 4, "User version");
  275. print_decode_line(aData, 64, 4, "Incremental-vacuum mode");
  276. print_decode_line(aData, 68, 4, "Application ID");
  277. print_decode_line(aData, 72, 4, "meta[8]");
  278. print_decode_line(aData, 76, 4, "meta[9]");
  279. print_decode_line(aData, 80, 4, "meta[10]");
  280. print_decode_line(aData, 84, 4, "meta[11]");
  281. print_decode_line(aData, 88, 4, "meta[12]");
  282. print_decode_line(aData, 92, 4, "Change counter for version number");
  283. print_decode_line(aData, 96, 4, "SQLite version number");
  284. sqlite3_free(aData);
  285. }
  286. /*
  287. ** Describe cell content.
  288. */
  289. static i64 describeContent(
  290. unsigned char *a, /* Cell content */
  291. i64 nLocal, /* Bytes in a[] */
  292. char *zDesc /* Write description here */
  293. ){
  294. i64 nDesc = 0;
  295. int n, j;
  296. i64 i, x, v;
  297. const unsigned char *pData;
  298. const unsigned char *pLimit;
  299. char sep = ' ';
  300. pLimit = &a[nLocal];
  301. n = decodeVarint(a, &x);
  302. pData = &a[x];
  303. a += n;
  304. i = x - n;
  305. while( i>0 && pData<=pLimit ){
  306. n = decodeVarint(a, &x);
  307. a += n;
  308. i -= n;
  309. nLocal -= n;
  310. zDesc[0] = sep;
  311. sep = ',';
  312. nDesc++;
  313. zDesc++;
  314. if( x==0 ){
  315. sprintf(zDesc, "*"); /* NULL is a "*" */
  316. }else if( x>=1 && x<=6 ){
  317. v = (signed char)pData[0];
  318. pData++;
  319. switch( x ){
  320. case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
  321. case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
  322. case 4: v = (v<<8) + pData[0]; pData++;
  323. case 3: v = (v<<8) + pData[0]; pData++;
  324. case 2: v = (v<<8) + pData[0]; pData++;
  325. }
  326. sprintf(zDesc, "%lld", v);
  327. }else if( x==7 ){
  328. sprintf(zDesc, "real");
  329. pData += 8;
  330. }else if( x==8 ){
  331. sprintf(zDesc, "0");
  332. }else if( x==9 ){
  333. sprintf(zDesc, "1");
  334. }else if( x>=12 ){
  335. i64 size = (x-12)/2;
  336. if( (x&1)==0 ){
  337. sprintf(zDesc, "blob(%lld)", size);
  338. }else{
  339. sprintf(zDesc, "txt(%lld)", size);
  340. }
  341. pData += size;
  342. }
  343. j = (int)strlen(zDesc);
  344. zDesc += j;
  345. nDesc += j;
  346. }
  347. return nDesc;
  348. }
  349. /*
  350. ** Compute the local payload size given the total payload size and
  351. ** the page size.
  352. */
  353. static i64 localPayload(i64 nPayload, char cType){
  354. i64 maxLocal;
  355. i64 minLocal;
  356. i64 surplus;
  357. i64 nLocal;
  358. if( cType==13 ){
  359. /* Table leaf */
  360. maxLocal = g.pagesize-35;
  361. minLocal = (g.pagesize-12)*32/255-23;
  362. }else{
  363. maxLocal = (g.pagesize-12)*64/255-23;
  364. minLocal = (g.pagesize-12)*32/255-23;
  365. }
  366. if( nPayload>maxLocal ){
  367. surplus = minLocal + (nPayload-minLocal)%(g.pagesize-4);
  368. if( surplus<=maxLocal ){
  369. nLocal = surplus;
  370. }else{
  371. nLocal = minLocal;
  372. }
  373. }else{
  374. nLocal = nPayload;
  375. }
  376. return nLocal;
  377. }
  378. /*
  379. ** Create a description for a single cell.
  380. **
  381. ** The return value is the local cell size.
  382. */
  383. static i64 describeCell(
  384. unsigned char cType, /* Page type */
  385. unsigned char *a, /* Cell content */
  386. int showCellContent, /* Show cell content if true */
  387. char **pzDesc /* Store description here */
  388. ){
  389. int i;
  390. i64 nDesc = 0;
  391. int n = 0;
  392. u32 leftChild;
  393. i64 nPayload;
  394. i64 rowid;
  395. i64 nLocal;
  396. static char zDesc[1000];
  397. i = 0;
  398. if( cType<=5 ){
  399. leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
  400. a += 4;
  401. n += 4;
  402. sprintf(zDesc, "lx: %u ", leftChild);
  403. nDesc = strlen(zDesc);
  404. }
  405. if( cType!=5 ){
  406. i = decodeVarint(a, &nPayload);
  407. a += i;
  408. n += i;
  409. sprintf(&zDesc[nDesc], "n: %lld ", nPayload);
  410. nDesc += strlen(&zDesc[nDesc]);
  411. nLocal = localPayload(nPayload, cType);
  412. }else{
  413. nPayload = nLocal = 0;
  414. }
  415. if( cType==5 || cType==13 ){
  416. i = decodeVarint(a, &rowid);
  417. a += i;
  418. n += i;
  419. sprintf(&zDesc[nDesc], "r: %lld ", rowid);
  420. nDesc += strlen(&zDesc[nDesc]);
  421. }
  422. if( nLocal<nPayload ){
  423. u32 ovfl;
  424. unsigned char *b = &a[nLocal];
  425. ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3];
  426. sprintf(&zDesc[nDesc], "ov: %u ", ovfl);
  427. nDesc += strlen(&zDesc[nDesc]);
  428. n += 4;
  429. }
  430. if( showCellContent && cType!=5 ){
  431. nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]);
  432. }
  433. *pzDesc = zDesc;
  434. return nLocal+n;
  435. }
  436. /* Print an offset followed by nByte bytes. Add extra white-space
  437. ** at the end so that subsequent text is aligned.
  438. */
  439. static void printBytes(
  440. unsigned char *aData, /* Content being decoded */
  441. unsigned char *aStart, /* Start of content to be printed */
  442. int nByte /* Number of bytes to print */
  443. ){
  444. int j;
  445. printf(" %03x: ", (int)(aStart-aData));
  446. for(j=0; j<9; j++){
  447. if( j>=nByte ){
  448. printf(" ");
  449. }else{
  450. printf("%02x ", aStart[j]);
  451. }
  452. }
  453. }
  454. /*
  455. ** Write a full decode on stdout for the cell at a[ofst].
  456. ** Assume the page contains a header of size szPgHdr bytes.
  457. */
  458. static void decodeCell(
  459. unsigned char *a, /* Page content (without the page-1 header) */
  460. unsigned pgno, /* Page number */
  461. int iCell, /* Cell index */
  462. int szPgHdr, /* Size of the page header. 0 or 100 */
  463. int ofst /* Cell begins at a[ofst] */
  464. ){
  465. int i, j = 0;
  466. u32 leftChild;
  467. i64 k;
  468. i64 nPayload;
  469. i64 rowid;
  470. i64 nHdr;
  471. i64 iType;
  472. i64 nLocal;
  473. unsigned char *x = a + ofst;
  474. unsigned char *end;
  475. unsigned char cType = a[0];
  476. int nCol = 0;
  477. int szCol[2000];
  478. int ofstCol[2000];
  479. int typeCol[2000];
  480. printf("Cell[%d]:\n", iCell);
  481. if( cType<=5 ){
  482. leftChild = ((x[0]*256 + x[1])*256 + x[2])*256 + x[3];
  483. printBytes(a, x, 4);
  484. printf("left child page:: %u\n", leftChild);
  485. x += 4;
  486. }
  487. if( cType!=5 ){
  488. i = decodeVarint(x, &nPayload);
  489. printBytes(a, x, i);
  490. nLocal = localPayload(nPayload, cType);
  491. if( nLocal==nPayload ){
  492. printf("payload-size: %lld\n", nPayload);
  493. }else{
  494. printf("payload-size: %lld (%lld local, %lld overflow)\n",
  495. nPayload, nLocal, nPayload-nLocal);
  496. }
  497. x += i;
  498. }else{
  499. nPayload = nLocal = 0;
  500. }
  501. end = x + nLocal;
  502. if( cType==5 || cType==13 ){
  503. i = decodeVarint(x, &rowid);
  504. printBytes(a, x, i);
  505. printf("rowid: %lld\n", rowid);
  506. x += i;
  507. }
  508. if( nLocal>0 ){
  509. i = decodeVarint(x, &nHdr);
  510. printBytes(a, x, i);
  511. printf("record-header-size: %d\n", (int)nHdr);
  512. j = i;
  513. nCol = 0;
  514. k = nHdr;
  515. while( x+j<=end && j<nHdr ){
  516. const char *zTypeName;
  517. int sz = 0;
  518. char zNm[30];
  519. i = decodeVarint(x+j, &iType);
  520. printBytes(a, x+j, i);
  521. printf("typecode[%d]: %d - ", nCol, (int)iType);
  522. switch( iType ){
  523. case 0: zTypeName = "NULL"; sz = 0; break;
  524. case 1: zTypeName = "int8"; sz = 1; break;
  525. case 2: zTypeName = "int16"; sz = 2; break;
  526. case 3: zTypeName = "int24"; sz = 3; break;
  527. case 4: zTypeName = "int32"; sz = 4; break;
  528. case 5: zTypeName = "int48"; sz = 6; break;
  529. case 6: zTypeName = "int64"; sz = 8; break;
  530. case 7: zTypeName = "double"; sz = 8; break;
  531. case 8: zTypeName = "zero"; sz = 0; break;
  532. case 9: zTypeName = "one"; sz = 0; break;
  533. case 10:
  534. case 11: zTypeName = "error"; sz = 0; break;
  535. default: {
  536. sz = (int)(iType-12)/2;
  537. sprintf(zNm, (iType&1)==0 ? "blob(%d)" : "text(%d)", sz);
  538. zTypeName = zNm;
  539. break;
  540. }
  541. }
  542. printf("%s\n", zTypeName);
  543. szCol[nCol] = sz;
  544. ofstCol[nCol] = (int)k;
  545. typeCol[nCol] = (int)iType;
  546. k += sz;
  547. nCol++;
  548. j += i;
  549. }
  550. for(i=0; i<nCol && ofstCol[i]+szCol[i]<=nLocal; i++){
  551. int s = ofstCol[i];
  552. i64 v;
  553. const unsigned char *pData;
  554. if( szCol[i]==0 ) continue;
  555. printBytes(a, x+s, szCol[i]);
  556. printf("data[%d]: ", i);
  557. pData = x+s;
  558. if( typeCol[i]<=7 ){
  559. v = (signed char)pData[0];
  560. for(k=1; k<szCol[i]; k++){
  561. v = (v<<8) + pData[k];
  562. }
  563. if( typeCol[i]==7 ){
  564. double r;
  565. memcpy(&r, &v, sizeof(r));
  566. printf("%#g\n", r);
  567. }else{
  568. printf("%lld\n", v);
  569. }
  570. }else{
  571. int ii, jj;
  572. char zConst[32];
  573. if( (typeCol[i]&1)==0 ){
  574. zConst[0] = 'x';
  575. zConst[1] = '\'';
  576. for(ii=2, jj=0; jj<szCol[i] && ii<24; jj++, ii+=2){
  577. sprintf(zConst+ii, "%02x", pData[jj]);
  578. }
  579. }else{
  580. zConst[0] = '\'';
  581. for(ii=1, jj=0; jj<szCol[i] && ii<24; jj++, ii++){
  582. zConst[ii] = ISPRINT(pData[jj]) ? pData[jj] : '.';
  583. }
  584. zConst[ii] = 0;
  585. }
  586. if( jj<szCol[i] ){
  587. memcpy(zConst+ii, "...'", 5);
  588. }else{
  589. memcpy(zConst+ii, "'", 2);
  590. }
  591. printf("%s\n", zConst);
  592. }
  593. j = ofstCol[i] + szCol[i];
  594. }
  595. }
  596. if( j<nLocal ){
  597. printBytes(a, x+j, 0);
  598. printf("... %lld bytes of content ...\n", nLocal-j);
  599. }
  600. if( nLocal<nPayload ){
  601. printBytes(a, x+nLocal, 4);
  602. printf("overflow-page: %u\n", decodeInt32(x+nLocal));
  603. }
  604. }
  605. /*
  606. ** Decode a btree page
  607. */
  608. static void decode_btree_page(
  609. unsigned char *a, /* Page content */
  610. int pgno, /* Page number */
  611. int hdrSize, /* Size of the page header. 0 or 100 */
  612. char *zArgs /* Flags to control formatting */
  613. ){
  614. const char *zType = "unknown";
  615. int nCell;
  616. int i, j;
  617. int iCellPtr;
  618. int showCellContent = 0;
  619. int showMap = 0;
  620. int cellToDecode = -2;
  621. char *zMap = 0;
  622. switch( a[0] ){
  623. case 2: zType = "index interior node"; break;
  624. case 5: zType = "table interior node"; break;
  625. case 10: zType = "index leaf"; break;
  626. case 13: zType = "table leaf"; break;
  627. }
  628. while( zArgs[0] ){
  629. switch( zArgs[0] ){
  630. case 'c': showCellContent = 1; break;
  631. case 'm': showMap = 1; break;
  632. case 'd': {
  633. if( !ISDIGIT(zArgs[1]) ){
  634. cellToDecode = -1;
  635. }else{
  636. cellToDecode = 0;
  637. while( ISDIGIT(zArgs[1]) ){
  638. zArgs++;
  639. cellToDecode = cellToDecode*10 + zArgs[0] - '0';
  640. }
  641. }
  642. break;
  643. }
  644. }
  645. zArgs++;
  646. }
  647. nCell = a[3]*256 + a[4];
  648. iCellPtr = (a[0]==2 || a[0]==5) ? 12 : 8;
  649. if( cellToDecode>=nCell ){
  650. printf("Page %d has only %d cells\n", pgno, nCell);
  651. return;
  652. }
  653. printf("Header on btree page %d:\n", pgno);
  654. print_decode_line(a, 0, 1, zType);
  655. print_decode_line(a, 1, 2, "Offset to first freeblock");
  656. print_decode_line(a, 3, 2, "Number of cells on this page");
  657. print_decode_line(a, 5, 2, "Offset to cell content area");
  658. print_decode_line(a, 7, 1, "Fragmented byte count");
  659. if( a[0]==2 || a[0]==5 ){
  660. print_decode_line(a, 8, 4, "Right child");
  661. }
  662. if( cellToDecode==(-2) && nCell>0 ){
  663. printf(" key: lx=left-child n=payload-size r=rowid\n");
  664. }
  665. if( showMap ){
  666. zMap = sqlite3_malloc(g.pagesize);
  667. memset(zMap, '.', g.pagesize);
  668. memset(zMap, '1', hdrSize);
  669. memset(&zMap[hdrSize], 'H', iCellPtr);
  670. memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
  671. }
  672. for(i=0; i<nCell; i++){
  673. int cofst = iCellPtr + i*2;
  674. char *zDesc;
  675. i64 n;
  676. cofst = a[cofst]*256 + a[cofst+1];
  677. n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
  678. if( showMap ){
  679. char zBuf[30];
  680. memset(&zMap[cofst], '*', (size_t)n);
  681. zMap[cofst] = '[';
  682. zMap[cofst+n-1] = ']';
  683. sprintf(zBuf, "%d", i);
  684. j = (int)strlen(zBuf);
  685. if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
  686. }
  687. if( cellToDecode==(-2) ){
  688. printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
  689. }else if( cellToDecode==(-1) || cellToDecode==i ){
  690. decodeCell(a, pgno, i, hdrSize, cofst-hdrSize);
  691. }
  692. }
  693. if( showMap ){
  694. printf("Page map: (H=header P=cell-index 1=page-1-header .=free-space)\n");
  695. for(i=0; (u32)i<g.pagesize; i+=64){
  696. printf(" %03x: %.64s\n", i, &zMap[i]);
  697. }
  698. sqlite3_free(zMap);
  699. }
  700. }
  701. /*
  702. ** Decode a freelist trunk page.
  703. */
  704. static void decode_trunk_page(
  705. u32 pgno, /* The page number */
  706. int detail, /* Show leaf pages if true */
  707. int recursive /* Follow the trunk change if true */
  708. ){
  709. u32 i;
  710. u32 n;
  711. unsigned char *a;
  712. while( pgno>0 ){
  713. a = fileRead((pgno-1)*g.pagesize, g.pagesize);
  714. printf("Decode of freelist trunk page %d:\n", pgno);
  715. print_decode_line(a, 0, 4, "Next freelist trunk page");
  716. print_decode_line(a, 4, 4, "Number of entries on this page");
  717. if( detail ){
  718. n = decodeInt32(&a[4]);
  719. for(i=0; i<n && i<g.pagesize/4; i++){
  720. u32 x = decodeInt32(&a[8+4*i]);
  721. char zIdx[13];
  722. sprintf(zIdx, "[%d]", i);
  723. printf(" %5s %7u", zIdx, x);
  724. if( i%5==4 ) printf("\n");
  725. }
  726. if( i%5!=0 ) printf("\n");
  727. }
  728. if( !recursive ){
  729. pgno = 0;
  730. }else{
  731. pgno = decodeInt32(&a[0]);
  732. }
  733. sqlite3_free(a);
  734. }
  735. }
  736. /*
  737. ** A short text comment on the use of each page.
  738. */
  739. static char **zPageUse;
  740. /*
  741. ** Add a comment on the use of a page.
  742. */
  743. static void page_usage_msg(u32 pgno, const char *zFormat, ...){
  744. va_list ap;
  745. char *zMsg;
  746. va_start(ap, zFormat);
  747. zMsg = sqlite3_vmprintf(zFormat, ap);
  748. va_end(ap);
  749. if( pgno<=0 || pgno>g.mxPage ){
  750. printf("ERROR: page %d out of range 1..%u: %s\n",
  751. pgno, g.mxPage, zMsg);
  752. sqlite3_free(zMsg);
  753. return;
  754. }
  755. if( zPageUse[pgno]!=0 ){
  756. printf("ERROR: page %d used multiple times:\n", pgno);
  757. printf("ERROR: previous: %s\n", zPageUse[pgno]);
  758. printf("ERROR: current: %s\n", zMsg);
  759. sqlite3_free(zPageUse[pgno]);
  760. }
  761. zPageUse[pgno] = zMsg;
  762. }
  763. /*
  764. ** Find overflow pages of a cell and describe their usage.
  765. */
  766. static void page_usage_cell(
  767. unsigned char cType, /* Page type */
  768. unsigned char *a, /* Cell content */
  769. u32 pgno, /* page containing the cell */
  770. int cellno /* Index of the cell on the page */
  771. ){
  772. int i;
  773. int n = 0;
  774. i64 nPayload;
  775. i64 rowid;
  776. i64 nLocal;
  777. i = 0;
  778. if( cType<=5 ){
  779. a += 4;
  780. n += 4;
  781. }
  782. if( cType!=5 ){
  783. i = decodeVarint(a, &nPayload);
  784. a += i;
  785. n += i;
  786. nLocal = localPayload(nPayload, cType);
  787. }else{
  788. nPayload = nLocal = 0;
  789. }
  790. if( cType==5 || cType==13 ){
  791. i = decodeVarint(a, &rowid);
  792. a += i;
  793. n += i;
  794. }
  795. if( nLocal<nPayload ){
  796. u32 ovfl = decodeInt32(a+nLocal);
  797. u32 cnt = 0;
  798. while( ovfl && (cnt++)<g.mxPage ){
  799. page_usage_msg(ovfl, "overflow %d from cell %d of page %u",
  800. cnt, cellno, pgno);
  801. a = fileRead((ovfl-1)*(sqlite3_int64)g.pagesize, 4);
  802. ovfl = decodeInt32(a);
  803. sqlite3_free(a);
  804. }
  805. }
  806. }
  807. /*
  808. ** True if the memory is all zeros
  809. */
  810. static int allZero(unsigned char *a, int n){
  811. while( n && (a++)[0]==0 ){ n--; }
  812. return n==0;
  813. }
  814. /*
  815. ** Describe the usages of a b-tree page.
  816. **
  817. ** If parent==0, then this is the root of a btree. If parent<0 then
  818. ** this is an orphan page.
  819. */
  820. static void page_usage_btree(
  821. u32 pgno, /* Page to describe */
  822. int parent, /* Parent of this page. 0 for root pages */
  823. int idx, /* Which child of the parent */
  824. const char *zName /* Name of the table */
  825. ){
  826. unsigned char *a;
  827. const char *zType = "corrupt node";
  828. int nCell;
  829. int i;
  830. int hdr = pgno==1 ? 100 : 0;
  831. char zEntry[30];
  832. if( pgno<=0 || pgno>g.mxPage ) return;
  833. a = fileRead((pgno-1)*g.pagesize, g.pagesize);
  834. switch( a[hdr] ){
  835. case 0: {
  836. if( allZero(a, g.pagesize) ){
  837. zType = "zeroed page";
  838. }else if( parent<0 ){
  839. return;
  840. }else{
  841. zType = "corrupt node";
  842. }
  843. break;
  844. }
  845. case 2: zType = "interior node of index"; break;
  846. case 5: zType = "interior node of table"; break;
  847. case 10: zType = "leaf of index"; break;
  848. case 13: zType = "leaf of table"; break;
  849. default: {
  850. if( parent<0 ) return;
  851. zType = "corrupt node";
  852. }
  853. }
  854. nCell = a[hdr+3]*256 + a[hdr+4];
  855. if( nCell==1 ){
  856. sqlite3_snprintf(sizeof(zEntry),zEntry,"1 row");
  857. }else{
  858. sqlite3_snprintf(sizeof(zEntry),zEntry,"%d rows", nCell);
  859. }
  860. if( parent>0 ){
  861. page_usage_msg(pgno, "%s [%s], child %d of page %d, %s",
  862. zType, zName, idx, parent, zEntry);
  863. }else if( parent==0 ){
  864. page_usage_msg(pgno, "root %s [%s], %s", zType, zName, zEntry);
  865. }else{
  866. page_usage_msg(pgno, "orphaned %s, %s", zType, zEntry);
  867. }
  868. if( a[hdr]==2 || a[hdr]==5 ){
  869. int cellstart = hdr+12;
  870. u32 child;
  871. for(i=0; i<nCell; i++){
  872. u32 cellidx;
  873. u32 ofst;
  874. cellidx = cellstart + i*2;
  875. if( cellidx+1 >= g.pagesize ){
  876. printf("ERROR: page %d too many cells (%d)\n", pgno, nCell);
  877. break;
  878. }
  879. ofst = a[cellidx]*256 + a[cellidx+1];
  880. if( ofst<cellidx+2 || ofst+4>=g.pagesize ){
  881. printf("ERROR: page %d cell %d out of bounds\n", pgno, i);
  882. continue;
  883. }
  884. child = decodeInt32(a+ofst);
  885. page_usage_btree(child, pgno, i, zName);
  886. }
  887. child = decodeInt32(a+cellstart-4);
  888. page_usage_btree(child, pgno, i, zName);
  889. }
  890. if( a[hdr]==2 || a[hdr]==10 || a[hdr]==13 ){
  891. int cellstart = hdr + 8 + 4*(a[hdr]<=5);
  892. for(i=0; i<nCell; i++){
  893. int ofst;
  894. ofst = cellstart + i*2;
  895. ofst = a[ofst]*256 + a[ofst+1];
  896. page_usage_cell(a[hdr], a+ofst, pgno, i);
  897. }
  898. }
  899. sqlite3_free(a);
  900. }
  901. /*
  902. ** Determine page usage by the freelist
  903. */
  904. static void page_usage_freelist(u32 pgno){
  905. unsigned char *a;
  906. int cnt = 0;
  907. int i;
  908. int n;
  909. int iNext;
  910. int parent = 1;
  911. while( pgno>0 && pgno<=g.mxPage && (u32)(cnt++)<g.mxPage ){
  912. page_usage_msg(pgno, "freelist trunk #%d child of %d", cnt, parent);
  913. a = fileRead((pgno-1)*g.pagesize, g.pagesize);
  914. iNext = decodeInt32(a);
  915. n = decodeInt32(a+4);
  916. if( n>(g.pagesize - 8)/4 ){
  917. printf("ERROR: page %d too many freelist entries (%d)\n", pgno, n);
  918. n = (g.pagesize - 8)/4;
  919. }
  920. for(i=0; i<n; i++){
  921. int child = decodeInt32(a + (i*4+8));
  922. page_usage_msg(child, "freelist leaf, child %d of trunk page %d",
  923. i, pgno);
  924. }
  925. sqlite3_free(a);
  926. parent = pgno;
  927. pgno = iNext;
  928. }
  929. }
  930. /*
  931. ** Determine pages used as PTRMAP pages
  932. */
  933. static void page_usage_ptrmap(u8 *a){
  934. if( decodeInt32(a+52) ){
  935. int usable = g.pagesize - a[20];
  936. u64 pgno = 2;
  937. int perPage = usable/5;
  938. while( pgno<=g.mxPage ){
  939. page_usage_msg((u32)pgno, "PTRMAP page covering %llu..%llu",
  940. pgno+1, pgno+perPage);
  941. pgno += perPage + 1;
  942. }
  943. }
  944. }
  945. /*
  946. ** Try to figure out how every page in the database file is being used.
  947. */
  948. static void page_usage_report(const char *zPrg, const char *zDbName){
  949. u32 i, j;
  950. int rc;
  951. sqlite3 *db;
  952. sqlite3_stmt *pStmt;
  953. unsigned char *a;
  954. char zQuery[200];
  955. /* Avoid the pathological case */
  956. if( g.mxPage<1 ){
  957. printf("empty database\n");
  958. return;
  959. }
  960. /* Open the database file */
  961. db = openDatabase(zPrg, zDbName);
  962. /* Set up global variables zPageUse[] and g.mxPage to record page
  963. ** usages */
  964. zPageUse = sqlite3_malloc64( sizeof(zPageUse[0])*(g.mxPage+1) );
  965. if( zPageUse==0 ) out_of_memory();
  966. memset(zPageUse, 0, sizeof(zPageUse[0])*(g.mxPage+1));
  967. /* Discover the usage of each page */
  968. a = fileRead(0, 100);
  969. page_usage_freelist(decodeInt32(a+32));
  970. page_usage_ptrmap(a);
  971. sqlite3_free(a);
  972. page_usage_btree(1, 0, 0, "sqlite_schema");
  973. sqlite3_exec(db, "PRAGMA writable_schema=ON", 0, 0, 0);
  974. for(j=0; j<2; j++){
  975. sqlite3_snprintf(sizeof(zQuery), zQuery,
  976. "SELECT type, name, rootpage FROM SQLITE_MASTER WHERE rootpage"
  977. " ORDER BY rowid %s", j?"DESC":"");
  978. rc = sqlite3_prepare_v2(db, zQuery, -1, &pStmt, 0);
  979. if( rc==SQLITE_OK ){
  980. while( sqlite3_step(pStmt)==SQLITE_ROW ){
  981. u32 pgno = (u32)sqlite3_column_int64(pStmt, 2);
  982. page_usage_btree(pgno, 0, 0, (const char*)sqlite3_column_text(pStmt,1));
  983. }
  984. }else{
  985. printf("ERROR: cannot query database: %s\n", sqlite3_errmsg(db));
  986. }
  987. rc = sqlite3_finalize(pStmt);
  988. if( rc==SQLITE_OK ) break;
  989. }
  990. sqlite3_close(db);
  991. /* Print the report and free memory used */
  992. for(i=1; i<=g.mxPage; i++){
  993. if( zPageUse[i]==0 ) page_usage_btree(i, -1, 0, 0);
  994. printf("%5u: %s\n", i, zPageUse[i] ? zPageUse[i] : "???");
  995. }
  996. for(i=1; i<=g.mxPage; i++){
  997. sqlite3_free(zPageUse[i]);
  998. }
  999. sqlite3_free(zPageUse);
  1000. zPageUse = 0;
  1001. }
  1002. /*
  1003. ** Try to figure out how every page in the database file is being used.
  1004. */
  1005. static void ptrmap_coverage_report(const char *zDbName){
  1006. u64 pgno;
  1007. unsigned char *aHdr;
  1008. unsigned char *a;
  1009. int usable;
  1010. int perPage;
  1011. int i;
  1012. /* Avoid the pathological case */
  1013. if( g.mxPage<1 ){
  1014. printf("empty database\n");
  1015. return;
  1016. }
  1017. /* Make sure PTRMAPs are used in this database */
  1018. aHdr = fileRead(0, 100);
  1019. if( aHdr[55]==0 ){
  1020. printf("database does not use PTRMAP pages\n");
  1021. return;
  1022. }
  1023. usable = g.pagesize - aHdr[20];
  1024. perPage = usable/5;
  1025. sqlite3_free(aHdr);
  1026. printf("%5d: root of sqlite_schema\n", 1);
  1027. for(pgno=2; pgno<=g.mxPage; pgno += perPage+1){
  1028. printf("%5llu: PTRMAP page covering %llu..%llu\n", pgno,
  1029. pgno+1, pgno+perPage);
  1030. a = fileRead((pgno-1)*g.pagesize, usable);
  1031. for(i=0; i+5<=usable; i+=5){
  1032. const char *zType;
  1033. u32 iFrom = decodeInt32(&a[i+1]);
  1034. const char *zExtra = pgno+1+i/5>g.mxPage ? " (off end of DB)" : "";
  1035. switch( a[i] ){
  1036. case 1: zType = "b-tree root page"; break;
  1037. case 2: zType = "freelist page"; break;
  1038. case 3: zType = "first page of overflow"; break;
  1039. case 4: zType = "later page of overflow"; break;
  1040. case 5: zType = "b-tree non-root page"; break;
  1041. default: {
  1042. if( zExtra[0]==0 ){
  1043. printf("%5llu: invalid (0x%02x), parent=%u\n",
  1044. pgno+1+i/5, a[i], iFrom);
  1045. }
  1046. zType = 0;
  1047. break;
  1048. }
  1049. }
  1050. if( zType ){
  1051. printf("%5llu: %s, parent=%u%s\n", pgno+1+i/5, zType, iFrom, zExtra);
  1052. }
  1053. }
  1054. sqlite3_free(a);
  1055. }
  1056. }
  1057. /*
  1058. ** Check the range validity for a page number. Print an error and
  1059. ** exit if the page is out of range.
  1060. */
  1061. static void checkPageValidity(int iPage){
  1062. if( iPage<1 || iPage>g.mxPage ){
  1063. fprintf(stderr, "Invalid page number %d: valid range is 1..%d\n",
  1064. iPage, g.mxPage);
  1065. exit(1);
  1066. }
  1067. }
  1068. /*
  1069. ** Print a usage comment
  1070. */
  1071. static void usage(const char *argv0){
  1072. fprintf(stderr, "Usage %s ?--uri? FILENAME ?args...?\n\n", argv0);
  1073. fprintf(stderr,
  1074. "switches:\n"
  1075. " --raw Read db file directly, bypassing SQLite VFS\n"
  1076. "args:\n"
  1077. " dbheader Show database header\n"
  1078. " pgidx Index of how each page is used\n"
  1079. " ptrmap Show all PTRMAP page content\n"
  1080. " NNN..MMM Show hex of pages NNN through MMM\n"
  1081. " NNN..end Show hex of pages NNN through end of file\n"
  1082. " NNNb Decode btree page NNN\n"
  1083. " NNNbc Decode btree page NNN and show content\n"
  1084. " NNNbm Decode btree page NNN and show a layout map\n"
  1085. " NNNbdCCC Decode cell CCC on btree page NNN\n"
  1086. " NNNt Decode freelist trunk page NNN\n"
  1087. " NNNtd Show leaf freelist pages on the decode\n"
  1088. " NNNtr Recursively decode freelist starting at NNN\n"
  1089. );
  1090. }
  1091. int main(int argc, char **argv){
  1092. sqlite3_int64 szFile;
  1093. unsigned char *zPgSz;
  1094. const char *zPrg = argv[0]; /* Name of this executable */
  1095. char **azArg = argv;
  1096. int nArg = argc;
  1097. /* Check for the "--uri" or "-uri" switch. */
  1098. if( nArg>1 ){
  1099. if( sqlite3_stricmp("-raw", azArg[1])==0
  1100. || sqlite3_stricmp("--raw", azArg[1])==0
  1101. ){
  1102. g.bRaw = 1;
  1103. azArg++;
  1104. nArg--;
  1105. }
  1106. }
  1107. if( nArg<2 ){
  1108. usage(zPrg);
  1109. exit(1);
  1110. }
  1111. fileOpen(zPrg, azArg[1]);
  1112. szFile = fileGetsize();
  1113. zPgSz = fileRead(16, 2);
  1114. g.pagesize = zPgSz[0]*256 + zPgSz[1]*65536;
  1115. if( g.pagesize==0 ) g.pagesize = 1024;
  1116. sqlite3_free(zPgSz);
  1117. printf("Pagesize: %d\n", g.pagesize);
  1118. g.mxPage = (u32)((szFile+g.pagesize-1)/g.pagesize);
  1119. printf("Available pages: 1..%u\n", g.mxPage);
  1120. if( nArg==2 ){
  1121. u32 i;
  1122. for(i=1; i<=g.mxPage; i++) print_page(i);
  1123. }else{
  1124. int i;
  1125. for(i=2; i<nArg; i++){
  1126. u32 iStart, iEnd;
  1127. char *zLeft;
  1128. if( strcmp(azArg[i], "dbheader")==0 ){
  1129. print_db_header();
  1130. continue;
  1131. }
  1132. if( strcmp(azArg[i], "pgidx")==0 ){
  1133. page_usage_report(zPrg, azArg[1]);
  1134. continue;
  1135. }
  1136. if( strcmp(azArg[i], "ptrmap")==0 ){
  1137. ptrmap_coverage_report(azArg[1]);
  1138. continue;
  1139. }
  1140. if( strcmp(azArg[i], "help")==0 ){
  1141. usage(zPrg);
  1142. continue;
  1143. }
  1144. if( !ISDIGIT(azArg[i][0]) ){
  1145. fprintf(stderr, "%s: unknown option: [%s]\n", zPrg, azArg[i]);
  1146. continue;
  1147. }
  1148. iStart = strtoul(azArg[i], &zLeft, 0);
  1149. checkPageValidity(iStart);
  1150. if( zLeft && strcmp(zLeft,"..end")==0 ){
  1151. iEnd = g.mxPage;
  1152. }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
  1153. iEnd = strtol(&zLeft[2], 0, 0);
  1154. checkPageValidity(iEnd);
  1155. }else if( zLeft && zLeft[0]=='b' ){
  1156. int ofst, nByte, hdrSize;
  1157. unsigned char *a;
  1158. if( iStart==1 ){
  1159. ofst = hdrSize = 100;
  1160. nByte = g.pagesize-100;
  1161. }else{
  1162. hdrSize = 0;
  1163. ofst = (iStart-1)*g.pagesize;
  1164. nByte = g.pagesize;
  1165. }
  1166. a = fileRead(ofst, nByte);
  1167. decode_btree_page(a, iStart, hdrSize, &zLeft[1]);
  1168. sqlite3_free(a);
  1169. continue;
  1170. }else if( zLeft && zLeft[0]=='t' ){
  1171. int detail = 0;
  1172. int recursive = 0;
  1173. int j;
  1174. for(j=1; zLeft[j]; j++){
  1175. if( zLeft[j]=='r' ) recursive = 1;
  1176. if( zLeft[j]=='d' ) detail = 1;
  1177. }
  1178. decode_trunk_page(iStart, detail, recursive);
  1179. continue;
  1180. }else{
  1181. iEnd = iStart;
  1182. }
  1183. if( iStart<1 || iEnd<iStart || iEnd>g.mxPage ){
  1184. fprintf(stderr,
  1185. "Page argument should be LOWER?..UPPER?. Range 1 to %d\n",
  1186. g.mxPage);
  1187. exit(1);
  1188. }
  1189. while( iStart<=iEnd ){
  1190. print_page(iStart);
  1191. iStart++;
  1192. }
  1193. }
  1194. }
  1195. fileClose();
  1196. return 0;
  1197. }