blob: c90f410d95f1afc53491a91e63f2d3937c0dd03f [file] [log] [blame]
drh0de8c112002-07-06 16:32:14 +00001/*
2** A utility for printing all or part of an SQLite database file.
3*/
4#include <stdio.h>
5#include <ctype.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
mistachkin026262b2012-10-13 09:31:20 +00009
10#if !defined(_MSC_VER)
drh0de8c112002-07-06 16:32:14 +000011#include <unistd.h>
mistachkin0461cc42014-07-18 21:16:37 +000012#else
13#include <io.h>
mistachkin026262b2012-10-13 09:31:20 +000014#endif
15
drh0de8c112002-07-06 16:32:14 +000016#include <stdlib.h>
drh562cedb2010-04-26 15:44:07 +000017#include <string.h>
drh3aeea462012-04-03 14:59:50 +000018#include "sqlite3.h"
drh0de8c112002-07-06 16:32:14 +000019
20
drh562cedb2010-04-26 15:44:07 +000021static int pagesize = 1024; /* Size of a database page */
22static int db = -1; /* File descriptor for reading the DB */
23static int mxPage = 0; /* Last page number */
24static int perLine = 16; /* HEX elements to print per line */
drh0de8c112002-07-06 16:32:14 +000025
drh562cedb2010-04-26 15:44:07 +000026typedef long long int i64; /* Datatype for 64-bit integers */
27
28
29/*
30** Convert the var-int format into i64. Return the number of bytes
31** in the var-int. Write the var-int value into *pVal.
32*/
drh7ecc1472010-04-26 16:47:12 +000033static int decodeVarint(const unsigned char *z, i64 *pVal){
drh562cedb2010-04-26 15:44:07 +000034 i64 v = 0;
drh7ecc1472010-04-26 16:47:12 +000035 int i;
36 for(i=0; i<8; i++){
drh562cedb2010-04-26 15:44:07 +000037 v = (v<<7) + (z[i]&0x7f);
38 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
39 }
40 v = (v<<8) + (z[i]&0xff);
41 *pVal = v;
42 return 9;
43}
44
drh7d105f82010-08-23 15:26:49 +000045/*
46** Extract a big-endian 32-bit integer
47*/
48static unsigned int decodeInt32(const unsigned char *z){
49 return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3];
50}
51
drh562cedb2010-04-26 15:44:07 +000052/* Report an out-of-memory error and die.
53*/
drh0de8c112002-07-06 16:32:14 +000054static void out_of_memory(void){
55 fprintf(stderr,"Out of memory...\n");
56 exit(1);
57}
58
drh562cedb2010-04-26 15:44:07 +000059/*
60** Read content from the file.
61**
62** Space to hold the content is obtained from malloc() and needs to be
63** freed by the caller.
64*/
65static unsigned char *getContent(int ofst, int nByte){
66 unsigned char *aData;
drh748c7352015-04-15 15:29:05 +000067 int got;
drh5240aeb2011-01-06 01:26:38 +000068 aData = malloc(nByte+32);
drh562cedb2010-04-26 15:44:07 +000069 if( aData==0 ) out_of_memory();
drhb7787ee2011-01-06 15:51:18 +000070 memset(aData, 0, nByte+32);
drh562cedb2010-04-26 15:44:07 +000071 lseek(db, ofst, SEEK_SET);
drh748c7352015-04-15 15:29:05 +000072 got = read(db, aData, nByte);
73 if( got>0 && got<nByte ) memset(aData+got, 0, nByte-got);
drh562cedb2010-04-26 15:44:07 +000074 return aData;
75}
76
77/*
78** Print a range of bytes as hex and as ascii.
79*/
80static unsigned char *print_byte_range(
81 int ofst, /* First byte in the range of bytes to print */
82 int nByte, /* Number of bytes to print */
83 int printOfst /* Add this amount to the index on the left column */
84){
drh0de8c112002-07-06 16:32:14 +000085 unsigned char *aData;
86 int i, j;
drh562cedb2010-04-26 15:44:07 +000087 const char *zOfstFmt;
88
89 if( ((printOfst+nByte)&~0xfff)==0 ){
90 zOfstFmt = " %03x: ";
91 }else if( ((printOfst+nByte)&~0xffff)==0 ){
92 zOfstFmt = " %04x: ";
93 }else if( ((printOfst+nByte)&~0xfffff)==0 ){
94 zOfstFmt = " %05x: ";
95 }else if( ((printOfst+nByte)&~0xffffff)==0 ){
96 zOfstFmt = " %06x: ";
97 }else{
98 zOfstFmt = " %08x: ";
99 }
100
101 aData = getContent(ofst, nByte);
102 for(i=0; i<nByte; i += perLine){
103 fprintf(stdout, zOfstFmt, i+printOfst);
drhc9ac5ca2005-11-04 22:03:30 +0000104 for(j=0; j<perLine; j++){
drh562cedb2010-04-26 15:44:07 +0000105 if( i+j>nByte ){
106 fprintf(stdout, " ");
107 }else{
108 fprintf(stdout,"%02x ", aData[i+j]);
109 }
drh0de8c112002-07-06 16:32:14 +0000110 }
drhc9ac5ca2005-11-04 22:03:30 +0000111 for(j=0; j<perLine; j++){
drh562cedb2010-04-26 15:44:07 +0000112 if( i+j>nByte ){
113 fprintf(stdout, " ");
114 }else{
115 fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
116 }
drh0de8c112002-07-06 16:32:14 +0000117 }
118 fprintf(stdout,"\n");
119 }
drh562cedb2010-04-26 15:44:07 +0000120 return aData;
121}
122
123/*
124** Print an entire page of content as hex
125*/
drhdb718d82014-01-28 20:36:22 +0000126static void print_page(int iPg){
drh562cedb2010-04-26 15:44:07 +0000127 int iStart;
128 unsigned char *aData;
129 iStart = (iPg-1)*pagesize;
130 fprintf(stdout, "Page %d: (offsets 0x%x..0x%x)\n",
131 iPg, iStart, iStart+pagesize-1);
132 aData = print_byte_range(iStart, pagesize, 0);
drh0de8c112002-07-06 16:32:14 +0000133 free(aData);
134}
135
drh6a30cd52014-06-19 23:38:53 +0000136
drh562cedb2010-04-26 15:44:07 +0000137/* Print a line of decode output showing a 4-byte integer.
138*/
drhdb718d82014-01-28 20:36:22 +0000139static void print_decode_line(
drh562cedb2010-04-26 15:44:07 +0000140 unsigned char *aData, /* Content being decoded */
141 int ofst, int nByte, /* Start and size of decode */
142 const char *zMsg /* Message to append */
143){
144 int i, j;
145 int val = aData[ofst];
146 char zBuf[100];
147 sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
mistachkinef607032014-07-19 15:40:39 +0000148 i = (int)strlen(zBuf);
drh562cedb2010-04-26 15:44:07 +0000149 for(j=1; j<4; j++){
150 if( j>=nByte ){
151 sprintf(&zBuf[i], " ");
152 }else{
153 sprintf(&zBuf[i], " %02x", aData[ofst+j]);
154 val = val*256 + aData[ofst+j];
155 }
mistachkinef607032014-07-19 15:40:39 +0000156 i += (int)strlen(&zBuf[i]);
drh562cedb2010-04-26 15:44:07 +0000157 }
158 sprintf(&zBuf[i], " %9d", val);
drh7ecc1472010-04-26 16:47:12 +0000159 printf("%s %s\n", zBuf, zMsg);
drh562cedb2010-04-26 15:44:07 +0000160}
161
162/*
163** Decode the database header.
164*/
drh7ecc1472010-04-26 16:47:12 +0000165static void print_db_header(void){
drh562cedb2010-04-26 15:44:07 +0000166 unsigned char *aData;
167 aData = print_byte_range(0, 100, 0);
168 printf("Decoded:\n");
169 print_decode_line(aData, 16, 2, "Database page size");
170 print_decode_line(aData, 18, 1, "File format write version");
171 print_decode_line(aData, 19, 1, "File format read version");
172 print_decode_line(aData, 20, 1, "Reserved space at end of page");
173 print_decode_line(aData, 24, 4, "File change counter");
174 print_decode_line(aData, 28, 4, "Size of database in pages");
175 print_decode_line(aData, 32, 4, "Page number of first freelist page");
176 print_decode_line(aData, 36, 4, "Number of freelist pages");
177 print_decode_line(aData, 40, 4, "Schema cookie");
178 print_decode_line(aData, 44, 4, "Schema format version");
179 print_decode_line(aData, 48, 4, "Default page cache size");
180 print_decode_line(aData, 52, 4, "Largest auto-vac root page");
181 print_decode_line(aData, 56, 4, "Text encoding");
182 print_decode_line(aData, 60, 4, "User version");
183 print_decode_line(aData, 64, 4, "Incremental-vacuum mode");
drh4ee09b42013-05-01 19:49:27 +0000184 print_decode_line(aData, 68, 4, "Application ID");
drh562cedb2010-04-26 15:44:07 +0000185 print_decode_line(aData, 72, 4, "meta[8]");
186 print_decode_line(aData, 76, 4, "meta[9]");
187 print_decode_line(aData, 80, 4, "meta[10]");
188 print_decode_line(aData, 84, 4, "meta[11]");
189 print_decode_line(aData, 88, 4, "meta[12]");
drhb28e59b2010-06-17 02:13:39 +0000190 print_decode_line(aData, 92, 4, "Change counter for version number");
drhc6d2b4a2010-04-26 17:30:52 +0000191 print_decode_line(aData, 96, 4, "SQLite version number");
drh562cedb2010-04-26 15:44:07 +0000192}
193
drh7ecc1472010-04-26 16:47:12 +0000194/*
drh100335b2011-01-05 21:20:52 +0000195** Describe cell content.
196*/
mistachkin0461cc42014-07-18 21:16:37 +0000197static i64 describeContent(
drh5240aeb2011-01-06 01:26:38 +0000198 unsigned char *a, /* Cell content */
mistachkin0461cc42014-07-18 21:16:37 +0000199 i64 nLocal, /* Bytes in a[] */
drh5240aeb2011-01-06 01:26:38 +0000200 char *zDesc /* Write description here */
drh100335b2011-01-05 21:20:52 +0000201){
mistachkin0461cc42014-07-18 21:16:37 +0000202 i64 nDesc = 0;
203 int n, j;
204 i64 i, x, v;
drh100335b2011-01-05 21:20:52 +0000205 const unsigned char *pData;
drh5240aeb2011-01-06 01:26:38 +0000206 const unsigned char *pLimit;
drh100335b2011-01-05 21:20:52 +0000207 char sep = ' ';
208
drh5240aeb2011-01-06 01:26:38 +0000209 pLimit = &a[nLocal];
drh100335b2011-01-05 21:20:52 +0000210 n = decodeVarint(a, &x);
211 pData = &a[x];
212 a += n;
213 i = x - n;
drh5240aeb2011-01-06 01:26:38 +0000214 while( i>0 && pData<=pLimit ){
drh100335b2011-01-05 21:20:52 +0000215 n = decodeVarint(a, &x);
216 a += n;
217 i -= n;
drh5240aeb2011-01-06 01:26:38 +0000218 nLocal -= n;
drh100335b2011-01-05 21:20:52 +0000219 zDesc[0] = sep;
220 sep = ',';
221 nDesc++;
222 zDesc++;
223 if( x==0 ){
drh5240aeb2011-01-06 01:26:38 +0000224 sprintf(zDesc, "*"); /* NULL is a "*" */
drh100335b2011-01-05 21:20:52 +0000225 }else if( x>=1 && x<=6 ){
226 v = (signed char)pData[0];
227 pData++;
228 switch( x ){
229 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
230 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
231 case 4: v = (v<<8) + pData[0]; pData++;
232 case 3: v = (v<<8) + pData[0]; pData++;
233 case 2: v = (v<<8) + pData[0]; pData++;
234 }
235 sprintf(zDesc, "%lld", v);
236 }else if( x==7 ){
237 sprintf(zDesc, "real");
238 pData += 8;
239 }else if( x==8 ){
240 sprintf(zDesc, "0");
241 }else if( x==9 ){
242 sprintf(zDesc, "1");
243 }else if( x>=12 ){
mistachkin0461cc42014-07-18 21:16:37 +0000244 i64 size = (x-12)/2;
drhb2c062d2011-01-05 21:46:52 +0000245 if( (x&1)==0 ){
mistachkin35683972014-07-19 15:30:01 +0000246 sprintf(zDesc, "blob(%lld)", size);
drh100335b2011-01-05 21:20:52 +0000247 }else{
mistachkin35683972014-07-19 15:30:01 +0000248 sprintf(zDesc, "txt(%lld)", size);
drh100335b2011-01-05 21:20:52 +0000249 }
250 pData += size;
251 }
mistachkinef607032014-07-19 15:40:39 +0000252 j = (int)strlen(zDesc);
drh100335b2011-01-05 21:20:52 +0000253 zDesc += j;
254 nDesc += j;
255 }
256 return nDesc;
257}
drh5240aeb2011-01-06 01:26:38 +0000258
259/*
260** Compute the local payload size given the total payload size and
261** the page size.
262*/
mistachkin0461cc42014-07-18 21:16:37 +0000263static i64 localPayload(i64 nPayload, char cType){
264 i64 maxLocal;
265 i64 minLocal;
266 i64 surplus;
267 i64 nLocal;
drh5240aeb2011-01-06 01:26:38 +0000268 if( cType==13 ){
269 /* Table leaf */
270 maxLocal = pagesize-35;
271 minLocal = (pagesize-12)*32/255-23;
272 }else{
273 maxLocal = (pagesize-12)*64/255-23;
274 minLocal = (pagesize-12)*32/255-23;
275 }
276 if( nPayload>maxLocal ){
277 surplus = minLocal + (nPayload-minLocal)%(pagesize-4);
278 if( surplus<=maxLocal ){
279 nLocal = surplus;
280 }else{
281 nLocal = minLocal;
282 }
283 }else{
284 nLocal = nPayload;
285 }
286 return nLocal;
287}
drh100335b2011-01-05 21:20:52 +0000288
289
290/*
drh7ecc1472010-04-26 16:47:12 +0000291** Create a description for a single cell.
drh5240aeb2011-01-06 01:26:38 +0000292**
293** The return value is the local cell size.
drh7ecc1472010-04-26 16:47:12 +0000294*/
mistachkin0461cc42014-07-18 21:16:37 +0000295static i64 describeCell(
drh100335b2011-01-05 21:20:52 +0000296 unsigned char cType, /* Page type */
297 unsigned char *a, /* Cell content */
298 int showCellContent, /* Show cell content if true */
299 char **pzDesc /* Store description here */
300){
drh7ecc1472010-04-26 16:47:12 +0000301 int i;
mistachkin0461cc42014-07-18 21:16:37 +0000302 i64 nDesc = 0;
drh7ecc1472010-04-26 16:47:12 +0000303 int n = 0;
304 int leftChild;
305 i64 nPayload;
306 i64 rowid;
mistachkin0461cc42014-07-18 21:16:37 +0000307 i64 nLocal;
drh100335b2011-01-05 21:20:52 +0000308 static char zDesc[1000];
drh7ecc1472010-04-26 16:47:12 +0000309 i = 0;
310 if( cType<=5 ){
311 leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
312 a += 4;
313 n += 4;
drh100335b2011-01-05 21:20:52 +0000314 sprintf(zDesc, "lx: %d ", leftChild);
drh7ecc1472010-04-26 16:47:12 +0000315 nDesc = strlen(zDesc);
316 }
317 if( cType!=5 ){
318 i = decodeVarint(a, &nPayload);
319 a += i;
320 n += i;
drh100335b2011-01-05 21:20:52 +0000321 sprintf(&zDesc[nDesc], "n: %lld ", nPayload);
drh7ecc1472010-04-26 16:47:12 +0000322 nDesc += strlen(&zDesc[nDesc]);
drh5240aeb2011-01-06 01:26:38 +0000323 nLocal = localPayload(nPayload, cType);
324 }else{
325 nPayload = nLocal = 0;
drh7ecc1472010-04-26 16:47:12 +0000326 }
327 if( cType==5 || cType==13 ){
328 i = decodeVarint(a, &rowid);
329 a += i;
330 n += i;
drh100335b2011-01-05 21:20:52 +0000331 sprintf(&zDesc[nDesc], "r: %lld ", rowid);
drh7ecc1472010-04-26 16:47:12 +0000332 nDesc += strlen(&zDesc[nDesc]);
333 }
drhb7787ee2011-01-06 15:51:18 +0000334 if( nLocal<nPayload ){
335 int ovfl;
336 unsigned char *b = &a[nLocal];
337 ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3];
338 sprintf(&zDesc[nDesc], "ov: %d ", ovfl);
339 nDesc += strlen(&zDesc[nDesc]);
340 n += 4;
341 }
drh100335b2011-01-05 21:20:52 +0000342 if( showCellContent && cType!=5 ){
drh5240aeb2011-01-06 01:26:38 +0000343 nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]);
drh100335b2011-01-05 21:20:52 +0000344 }
drh7ecc1472010-04-26 16:47:12 +0000345 *pzDesc = zDesc;
drh5240aeb2011-01-06 01:26:38 +0000346 return nLocal+n;
drh7ecc1472010-04-26 16:47:12 +0000347}
348
drh6a30cd52014-06-19 23:38:53 +0000349/* Print an offset followed by nByte bytes. Add extra white-space
350** at the end so that subsequent text is aligned.
351*/
352static void printBytes(
353 unsigned char *aData, /* Content being decoded */
354 unsigned char *aStart, /* Start of content to be printed */
355 int nByte /* Number of bytes to print */
356){
357 int j;
358 printf(" %03x: ", (int)(aStart-aData));
359 for(j=0; j<9; j++){
360 if( j>=nByte ){
361 printf(" ");
362 }else{
363 printf("%02x ", aStart[j]);
364 }
365 }
366}
367
368
369/*
370** Write a full decode on stdout for the cell at a[ofst].
371** Assume the page contains a header of size szPgHdr bytes.
372*/
373static void decodeCell(
374 unsigned char *a, /* Page content (without the page-1 header) */
375 unsigned pgno, /* Page number */
376 int iCell, /* Cell index */
377 int szPgHdr, /* Size of the page header. 0 or 100 */
378 int ofst /* Cell begins at a[ofst] */
379){
mistachkin44723ce2015-03-21 02:22:37 +0000380 int i, j = 0;
drh6a30cd52014-06-19 23:38:53 +0000381 int leftChild;
mistachkin0461cc42014-07-18 21:16:37 +0000382 i64 k;
drh6a30cd52014-06-19 23:38:53 +0000383 i64 nPayload;
384 i64 rowid;
385 i64 nHdr;
386 i64 iType;
mistachkin0461cc42014-07-18 21:16:37 +0000387 i64 nLocal;
drh6a30cd52014-06-19 23:38:53 +0000388 unsigned char *x = a + ofst;
389 unsigned char *end;
390 unsigned char cType = a[0];
drh419e0402014-06-20 01:32:42 +0000391 int nCol = 0;
392 int szCol[2000];
393 int ofstCol[2000];
394 int typeCol[2000];
drh6a30cd52014-06-19 23:38:53 +0000395
drh419e0402014-06-20 01:32:42 +0000396 printf("Cell[%d]:\n", iCell);
drh6a30cd52014-06-19 23:38:53 +0000397 if( cType<=5 ){
398 leftChild = ((x[0]*256 + x[1])*256 + x[2])*256 + x[3];
399 printBytes(a, x, 4);
400 printf("left child page:: %d\n", leftChild);
401 x += 4;
402 }
403 if( cType!=5 ){
404 i = decodeVarint(x, &nPayload);
405 printBytes(a, x, i);
406 nLocal = localPayload(nPayload, cType);
drh419e0402014-06-20 01:32:42 +0000407 if( nLocal==nPayload ){
mistachkin35683972014-07-19 15:30:01 +0000408 printf("payload-size: %lld\n", nPayload);
drh419e0402014-06-20 01:32:42 +0000409 }else{
mistachkin35683972014-07-19 15:30:01 +0000410 printf("payload-size: %lld (%lld local, %lld overflow)\n",
411 nPayload, nLocal, nPayload-nLocal);
drh419e0402014-06-20 01:32:42 +0000412 }
drh6a30cd52014-06-19 23:38:53 +0000413 x += i;
414 }else{
415 nPayload = nLocal = 0;
416 }
417 end = x + nLocal;
418 if( cType==5 || cType==13 ){
419 i = decodeVarint(x, &rowid);
420 printBytes(a, x, i);
421 printf("rowid: %lld\n", rowid);
422 x += i;
423 }
424 if( nLocal>0 ){
425 i = decodeVarint(x, &nHdr);
426 printBytes(a, x, i);
drh419e0402014-06-20 01:32:42 +0000427 printf("record-header-size: %d\n", (int)nHdr);
drh6a30cd52014-06-19 23:38:53 +0000428 j = i;
drh419e0402014-06-20 01:32:42 +0000429 nCol = 0;
430 k = nHdr;
drh6a30cd52014-06-19 23:38:53 +0000431 while( x+j<end && j<nHdr ){
432 const char *zTypeName;
433 int sz = 0;
434 char zNm[30];
435 i = decodeVarint(x+j, &iType);
436 printBytes(a, x+j, i);
drh419e0402014-06-20 01:32:42 +0000437 printf("typecode[%d]: %d - ", nCol, (int)iType);
drh6a30cd52014-06-19 23:38:53 +0000438 switch( iType ){
439 case 0: zTypeName = "NULL"; sz = 0; break;
440 case 1: zTypeName = "int8"; sz = 1; break;
441 case 2: zTypeName = "int16"; sz = 2; break;
442 case 3: zTypeName = "int24"; sz = 3; break;
443 case 4: zTypeName = "int32"; sz = 4; break;
444 case 5: zTypeName = "int48"; sz = 6; break;
445 case 6: zTypeName = "int64"; sz = 8; break;
446 case 7: zTypeName = "double"; sz = 8; break;
447 case 8: zTypeName = "zero"; sz = 0; break;
448 case 9: zTypeName = "one"; sz = 0; break;
449 case 10:
450 case 11: zTypeName = "error"; sz = 0; break;
451 default: {
452 sz = (int)(iType-12)/2;
453 sprintf(zNm, (iType&1)==0 ? "blob(%d)" : "text(%d)", sz);
454 zTypeName = zNm;
455 break;
456 }
457 }
458 printf("%s\n", zTypeName);
drh419e0402014-06-20 01:32:42 +0000459 szCol[nCol] = sz;
mistachkin0461cc42014-07-18 21:16:37 +0000460 ofstCol[nCol] = (int)k;
drh419e0402014-06-20 01:32:42 +0000461 typeCol[nCol] = (int)iType;
462 k += sz;
463 nCol++;
drh6a30cd52014-06-19 23:38:53 +0000464 j += i;
465 }
drh419e0402014-06-20 01:32:42 +0000466 for(i=0; i<nCol && ofstCol[i]+szCol[i]<=nLocal; i++){
467 int s = ofstCol[i];
468 i64 v;
469 const unsigned char *pData;
470 if( szCol[i]==0 ) continue;
471 printBytes(a, x+s, szCol[i]);
472 printf("data[%d]: ", i);
473 pData = x+s;
474 if( typeCol[i]<=7 ){
475 v = (signed char)pData[0];
476 for(k=1; k<szCol[i]; k++){
477 v = (v<<8) + pData[k];
478 }
479 if( typeCol[i]==7 ){
480 double r;
481 memcpy(&r, &v, sizeof(r));
482 printf("%#g\n", r);
483 }else{
484 printf("%lld\n", v);
485 }
486 }else{
drh56e67dd2014-06-20 13:55:06 +0000487 int ii, jj;
488 char zConst[32];
489 if( (typeCol[i]&1)==0 ){
490 zConst[0] = 'x';
491 zConst[1] = '\'';
492 for(ii=2, jj=0; jj<szCol[i] && ii<24; jj++, ii+=2){
493 sprintf(zConst+ii, "%02x", pData[jj]);
494 }
495 }else{
496 zConst[0] = '\'';
497 for(ii=1, jj=0; jj<szCol[i] && ii<24; jj++, ii++){
498 zConst[ii] = isprint(pData[jj]) ? pData[jj] : '.';
499 }
500 zConst[ii] = 0;
501 }
502 if( jj<szCol[i] ){
503 memcpy(zConst+ii, "...'", 5);
504 }else{
505 memcpy(zConst+ii, "'", 2);
506 }
507 printf("%s\n", zConst);
drh419e0402014-06-20 01:32:42 +0000508 }
509 j = ofstCol[i] + szCol[i];
510 }
drh6a30cd52014-06-19 23:38:53 +0000511 }
512 if( j<nLocal ){
513 printBytes(a, x+j, 0);
mistachkin35683972014-07-19 15:30:01 +0000514 printf("... %lld bytes of content ...\n", nLocal-j);
drh6a30cd52014-06-19 23:38:53 +0000515 }
516 if( nLocal<nPayload ){
517 printBytes(a, x+nLocal, 4);
drh419e0402014-06-20 01:32:42 +0000518 printf("overflow-page: %d\n", decodeInt32(x+nLocal));
drh6a30cd52014-06-19 23:38:53 +0000519 }
520}
521
522
drh7ecc1472010-04-26 16:47:12 +0000523/*
524** Decode a btree page
525*/
drh100335b2011-01-05 21:20:52 +0000526static void decode_btree_page(
527 unsigned char *a, /* Page content */
528 int pgno, /* Page number */
529 int hdrSize, /* Size of the page header. 0 or 100 */
530 char *zArgs /* Flags to control formatting */
531){
drh7ecc1472010-04-26 16:47:12 +0000532 const char *zType = "unknown";
533 int nCell;
drh5240aeb2011-01-06 01:26:38 +0000534 int i, j;
drh7ecc1472010-04-26 16:47:12 +0000535 int iCellPtr;
drh100335b2011-01-05 21:20:52 +0000536 int showCellContent = 0;
drh5240aeb2011-01-06 01:26:38 +0000537 int showMap = 0;
drh6a30cd52014-06-19 23:38:53 +0000538 int cellToDecode = -2;
drh5240aeb2011-01-06 01:26:38 +0000539 char *zMap = 0;
drh7ecc1472010-04-26 16:47:12 +0000540 switch( a[0] ){
541 case 2: zType = "index interior node"; break;
542 case 5: zType = "table interior node"; break;
543 case 10: zType = "index leaf"; break;
544 case 13: zType = "table leaf"; break;
545 }
drh100335b2011-01-05 21:20:52 +0000546 while( zArgs[0] ){
547 switch( zArgs[0] ){
548 case 'c': showCellContent = 1; break;
drh5240aeb2011-01-06 01:26:38 +0000549 case 'm': showMap = 1; break;
drh6a30cd52014-06-19 23:38:53 +0000550 case 'd': {
551 if( !isdigit(zArgs[1]) ){
552 cellToDecode = -1;
553 }else{
554 cellToDecode = 0;
555 while( isdigit(zArgs[1]) ){
556 zArgs++;
557 cellToDecode = cellToDecode*10 + zArgs[0] - '0';
558 }
559 }
560 break;
561 }
drh100335b2011-01-05 21:20:52 +0000562 }
563 zArgs++;
564 }
drh7ecc1472010-04-26 16:47:12 +0000565 nCell = a[3]*256 + a[4];
drh6a30cd52014-06-19 23:38:53 +0000566 iCellPtr = (a[0]==2 || a[0]==5) ? 12 : 8;
drh419e0402014-06-20 01:32:42 +0000567 if( cellToDecode>=nCell ){
drh6a30cd52014-06-19 23:38:53 +0000568 printf("Page %d has only %d cells\n", pgno, nCell);
569 return;
drh5240aeb2011-01-06 01:26:38 +0000570 }
drh419e0402014-06-20 01:32:42 +0000571 printf("Header on btree page %d:\n", pgno);
572 print_decode_line(a, 0, 1, zType);
573 print_decode_line(a, 1, 2, "Offset to first freeblock");
574 print_decode_line(a, 3, 2, "Number of cells on this page");
575 print_decode_line(a, 5, 2, "Offset to cell content area");
576 print_decode_line(a, 7, 1, "Fragmented byte count");
577 if( a[0]==2 || a[0]==5 ){
578 print_decode_line(a, 8, 4, "Right child");
579 }
580 if( cellToDecode==(-2) && nCell>0 ){
581 printf(" key: lx=left-child n=payload-size r=rowid\n");
582 }
drh5240aeb2011-01-06 01:26:38 +0000583 if( showMap ){
584 zMap = malloc(pagesize);
585 memset(zMap, '.', pagesize);
586 memset(zMap, '1', hdrSize);
587 memset(&zMap[hdrSize], 'H', iCellPtr);
588 memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
589 }
drh7ecc1472010-04-26 16:47:12 +0000590 for(i=0; i<nCell; i++){
591 int cofst = iCellPtr + i*2;
592 char *zDesc;
mistachkin0461cc42014-07-18 21:16:37 +0000593 i64 n;
drh5240aeb2011-01-06 01:26:38 +0000594
drh7ecc1472010-04-26 16:47:12 +0000595 cofst = a[cofst]*256 + a[cofst+1];
drh5240aeb2011-01-06 01:26:38 +0000596 n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
597 if( showMap ){
598 char zBuf[30];
mistachkin0461cc42014-07-18 21:16:37 +0000599 memset(&zMap[cofst], '*', (size_t)n);
drh5240aeb2011-01-06 01:26:38 +0000600 zMap[cofst] = '[';
601 zMap[cofst+n-1] = ']';
602 sprintf(zBuf, "%d", i);
mistachkinef607032014-07-19 15:40:39 +0000603 j = (int)strlen(zBuf);
drh5240aeb2011-01-06 01:26:38 +0000604 if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
605 }
drh6a30cd52014-06-19 23:38:53 +0000606 if( cellToDecode==(-2) ){
607 printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
608 }else if( cellToDecode==(-1) || cellToDecode==i ){
609 decodeCell(a, pgno, i, hdrSize, cofst-hdrSize);
610 }
drh7ecc1472010-04-26 16:47:12 +0000611 }
drh5240aeb2011-01-06 01:26:38 +0000612 if( showMap ){
drh419e0402014-06-20 01:32:42 +0000613 printf("Page map: (H=header P=cell-index 1=page-1-header .=free-space)\n");
drh5240aeb2011-01-06 01:26:38 +0000614 for(i=0; i<pagesize; i+=64){
615 printf(" %03x: %.64s\n", i, &zMap[i]);
616 }
617 free(zMap);
drh6a30cd52014-06-19 23:38:53 +0000618 }
drh7ecc1472010-04-26 16:47:12 +0000619}
620
drh7d105f82010-08-23 15:26:49 +0000621/*
622** Decode a freelist trunk page.
623*/
624static void decode_trunk_page(
625 int pgno, /* The page number */
626 int pagesize, /* Size of each page */
627 int detail, /* Show leaf pages if true */
628 int recursive /* Follow the trunk change if true */
629){
drhdb718d82014-01-28 20:36:22 +0000630 int n, i;
drh7d105f82010-08-23 15:26:49 +0000631 unsigned char *a;
632 while( pgno>0 ){
633 a = getContent((pgno-1)*pagesize, pagesize);
634 printf("Decode of freelist trunk page %d:\n", pgno);
635 print_decode_line(a, 0, 4, "Next freelist trunk page");
636 print_decode_line(a, 4, 4, "Number of entries on this page");
637 if( detail ){
638 n = (int)decodeInt32(&a[4]);
639 for(i=0; i<n; i++){
640 unsigned int x = decodeInt32(&a[8+4*i]);
641 char zIdx[10];
642 sprintf(zIdx, "[%d]", i);
643 printf(" %5s %7u", zIdx, x);
644 if( i%5==4 ) printf("\n");
645 }
646 if( i%5!=0 ) printf("\n");
647 }
648 if( !recursive ){
649 pgno = 0;
650 }else{
651 pgno = (int)decodeInt32(&a[0]);
652 }
653 free(a);
654 }
655}
656
657/*
drh3aeea462012-04-03 14:59:50 +0000658** A short text comment on the use of each page.
659*/
660static char **zPageUse;
661
662/*
663** Add a comment on the use of a page.
664*/
665static void page_usage_msg(int pgno, const char *zFormat, ...){
666 va_list ap;
667 char *zMsg;
668
669 va_start(ap, zFormat);
670 zMsg = sqlite3_vmprintf(zFormat, ap);
671 va_end(ap);
672 if( pgno<=0 || pgno>mxPage ){
drhedf9a172013-03-05 01:46:26 +0000673 printf("ERROR: page %d out of range 1..%d: %s\n",
drh3aeea462012-04-03 14:59:50 +0000674 pgno, mxPage, zMsg);
675 sqlite3_free(zMsg);
676 return;
677 }
678 if( zPageUse[pgno]!=0 ){
679 printf("ERROR: page %d used multiple times:\n", pgno);
680 printf("ERROR: previous: %s\n", zPageUse[pgno]);
drh103a70f2013-02-19 20:25:16 +0000681 printf("ERROR: current: %s\n", zMsg);
drh3aeea462012-04-03 14:59:50 +0000682 sqlite3_free(zPageUse[pgno]);
683 }
684 zPageUse[pgno] = zMsg;
685}
686
687/*
688** Find overflow pages of a cell and describe their usage.
689*/
690static void page_usage_cell(
691 unsigned char cType, /* Page type */
692 unsigned char *a, /* Cell content */
693 int pgno, /* page containing the cell */
694 int cellno /* Index of the cell on the page */
695){
696 int i;
drh3aeea462012-04-03 14:59:50 +0000697 int n = 0;
698 i64 nPayload;
699 i64 rowid;
mistachkin0461cc42014-07-18 21:16:37 +0000700 i64 nLocal;
drh3aeea462012-04-03 14:59:50 +0000701 i = 0;
702 if( cType<=5 ){
703 a += 4;
704 n += 4;
705 }
706 if( cType!=5 ){
707 i = decodeVarint(a, &nPayload);
708 a += i;
709 n += i;
710 nLocal = localPayload(nPayload, cType);
711 }else{
712 nPayload = nLocal = 0;
713 }
714 if( cType==5 || cType==13 ){
715 i = decodeVarint(a, &rowid);
716 a += i;
717 n += i;
718 }
719 if( nLocal<nPayload ){
720 int ovfl = decodeInt32(a+nLocal);
721 int cnt = 0;
722 while( ovfl && (cnt++)<mxPage ){
723 page_usage_msg(ovfl, "overflow %d from cell %d of page %d",
724 cnt, cellno, pgno);
725 a = getContent((ovfl-1)*pagesize, 4);
726 ovfl = decodeInt32(a);
727 free(a);
728 }
729 }
730}
731
732
733/*
734** Describe the usages of a b-tree page
735*/
736static void page_usage_btree(
737 int pgno, /* Page to describe */
738 int parent, /* Parent of this page. 0 for root pages */
739 int idx, /* Which child of the parent */
740 const char *zName /* Name of the table */
741){
742 unsigned char *a;
743 const char *zType = "corrupt node";
744 int nCell;
745 int i;
746 int hdr = pgno==1 ? 100 : 0;
747
748 if( pgno<=0 || pgno>mxPage ) return;
749 a = getContent((pgno-1)*pagesize, pagesize);
750 switch( a[hdr] ){
751 case 2: zType = "interior node of index"; break;
752 case 5: zType = "interior node of table"; break;
753 case 10: zType = "leaf of index"; break;
754 case 13: zType = "leaf of table"; break;
755 }
756 if( parent ){
757 page_usage_msg(pgno, "%s [%s], child %d of page %d",
758 zType, zName, idx, parent);
759 }else{
760 page_usage_msg(pgno, "root %s [%s]", zType, zName);
761 }
762 nCell = a[hdr+3]*256 + a[hdr+4];
763 if( a[hdr]==2 || a[hdr]==5 ){
764 int cellstart = hdr+12;
765 unsigned int child;
766 for(i=0; i<nCell; i++){
767 int ofst;
768
769 ofst = cellstart + i*2;
770 ofst = a[ofst]*256 + a[ofst+1];
771 child = decodeInt32(a+ofst);
772 page_usage_btree(child, pgno, i, zName);
773 }
774 child = decodeInt32(a+cellstart-4);
775 page_usage_btree(child, pgno, i, zName);
776 }
777 if( a[hdr]==2 || a[hdr]==10 || a[hdr]==13 ){
778 int cellstart = hdr + 8 + 4*(a[hdr]<=5);
779 for(i=0; i<nCell; i++){
780 int ofst;
781 ofst = cellstart + i*2;
782 ofst = a[ofst]*256 + a[ofst+1];
783 page_usage_cell(a[hdr], a+ofst, pgno, i);
784 }
785 }
786 free(a);
787}
788
789/*
790** Determine page usage by the freelist
791*/
792static void page_usage_freelist(int pgno){
793 unsigned char *a;
794 int cnt = 0;
795 int i;
796 int n;
797 int iNext;
798 int parent = 1;
799
800 while( pgno>0 && pgno<=mxPage && (cnt++)<mxPage ){
801 page_usage_msg(pgno, "freelist trunk #%d child of %d", cnt, parent);
802 a = getContent((pgno-1)*pagesize, pagesize);
803 iNext = decodeInt32(a);
804 n = decodeInt32(a+4);
805 for(i=0; i<n; i++){
806 int child = decodeInt32(a + (i*4+8));
807 page_usage_msg(child, "freelist leaf, child %d of trunk page %d",
808 i, pgno);
809 }
810 free(a);
811 parent = pgno;
812 pgno = iNext;
813 }
814}
815
816/*
drh344a97b2013-02-19 22:26:51 +0000817** Determine pages used as PTRMAP pages
818*/
819static void page_usage_ptrmap(unsigned char *a){
820 if( a[55] ){
821 int usable = pagesize - a[20];
822 int pgno = 2;
823 int perPage = usable/5;
824 while( pgno<=mxPage ){
825 page_usage_msg(pgno, "PTRMAP page covering %d..%d",
826 pgno+1, pgno+perPage);
827 pgno += perPage + 1;
828 }
829 }
830}
831
832/*
drh3aeea462012-04-03 14:59:50 +0000833** Try to figure out how every page in the database file is being used.
834*/
835static void page_usage_report(const char *zDbName){
drh00e637f2013-02-19 18:45:11 +0000836 int i, j;
drh3aeea462012-04-03 14:59:50 +0000837 int rc;
838 sqlite3 *db;
839 sqlite3_stmt *pStmt;
840 unsigned char *a;
drh00e637f2013-02-19 18:45:11 +0000841 char zQuery[200];
drh3aeea462012-04-03 14:59:50 +0000842
843 /* Avoid the pathological case */
844 if( mxPage<1 ){
845 printf("empty database\n");
846 return;
847 }
848
849 /* Open the database file */
850 rc = sqlite3_open(zDbName, &db);
851 if( rc ){
852 printf("cannot open database: %s\n", sqlite3_errmsg(db));
853 sqlite3_close(db);
854 return;
855 }
856
857 /* Set up global variables zPageUse[] and mxPage to record page
858 ** usages */
859 zPageUse = sqlite3_malloc( sizeof(zPageUse[0])*(mxPage+1) );
860 if( zPageUse==0 ) out_of_memory();
861 memset(zPageUse, 0, sizeof(zPageUse[0])*(mxPage+1));
862
863 /* Discover the usage of each page */
864 a = getContent(0, 100);
865 page_usage_freelist(decodeInt32(a+32));
drh344a97b2013-02-19 22:26:51 +0000866 page_usage_ptrmap(a);
drh3aeea462012-04-03 14:59:50 +0000867 free(a);
868 page_usage_btree(1, 0, 0, "sqlite_master");
drh00e637f2013-02-19 18:45:11 +0000869 sqlite3_exec(db, "PRAGMA writable_schema=ON", 0, 0, 0);
870 for(j=0; j<2; j++){
871 sqlite3_snprintf(sizeof(zQuery), zQuery,
872 "SELECT type, name, rootpage FROM SQLITE_MASTER WHERE rootpage"
873 " ORDER BY rowid %s", j?"DESC":"");
874 rc = sqlite3_prepare_v2(db, zQuery, -1, &pStmt, 0);
875 if( rc==SQLITE_OK ){
876 while( sqlite3_step(pStmt)==SQLITE_ROW ){
877 int pgno = sqlite3_column_int(pStmt, 2);
drhdb718d82014-01-28 20:36:22 +0000878 page_usage_btree(pgno, 0, 0, (const char*)sqlite3_column_text(pStmt,1));
drh00e637f2013-02-19 18:45:11 +0000879 }
880 }else{
881 printf("ERROR: cannot query database: %s\n", sqlite3_errmsg(db));
drh3aeea462012-04-03 14:59:50 +0000882 }
drh00e637f2013-02-19 18:45:11 +0000883 rc = sqlite3_finalize(pStmt);
884 if( rc==SQLITE_OK ) break;
drh3aeea462012-04-03 14:59:50 +0000885 }
drh3aeea462012-04-03 14:59:50 +0000886 sqlite3_close(db);
887
888 /* Print the report and free memory used */
889 for(i=1; i<=mxPage; i++){
890 printf("%5d: %s\n", i, zPageUse[i] ? zPageUse[i] : "???");
891 sqlite3_free(zPageUse[i]);
892 }
893 sqlite3_free(zPageUse);
894 zPageUse = 0;
895}
896
897/*
drh344a97b2013-02-19 22:26:51 +0000898** Try to figure out how every page in the database file is being used.
899*/
900static void ptrmap_coverage_report(const char *zDbName){
mistachkin0461cc42014-07-18 21:16:37 +0000901 int pgno;
drh344a97b2013-02-19 22:26:51 +0000902 unsigned char *aHdr;
903 unsigned char *a;
904 int usable;
905 int perPage;
mistachkin0461cc42014-07-18 21:16:37 +0000906 int i;
drh344a97b2013-02-19 22:26:51 +0000907
908 /* Avoid the pathological case */
909 if( mxPage<1 ){
910 printf("empty database\n");
911 return;
912 }
913
914 /* Make sure PTRMAPs are used in this database */
915 aHdr = getContent(0, 100);
916 if( aHdr[55]==0 ){
917 printf("database does not use PTRMAP pages\n");
918 return;
919 }
920 usable = pagesize - aHdr[20];
921 perPage = usable/5;
922 free(aHdr);
923 printf("%5d: root of sqlite_master\n", 1);
924 for(pgno=2; pgno<=mxPage; pgno += perPage+1){
925 printf("%5d: PTRMAP page covering %d..%d\n", pgno,
926 pgno+1, pgno+perPage);
927 a = getContent((pgno-1)*pagesize, usable);
928 for(i=0; i+5<=usable && pgno+1+i/5<=mxPage; i+=5){
929 const char *zType = "???";
930 unsigned int iFrom = decodeInt32(&a[i+1]);
931 switch( a[i] ){
932 case 1: zType = "b-tree root page"; break;
933 case 2: zType = "freelist page"; break;
934 case 3: zType = "first page of overflow"; break;
935 case 4: zType = "later page of overflow"; break;
936 case 5: zType = "b-tree non-root page"; break;
937 }
938 printf("%5d: %s, parent=%u\n", pgno+1+i/5, zType, iFrom);
939 }
940 free(a);
941 }
942}
943
944/*
drh7d105f82010-08-23 15:26:49 +0000945** Print a usage comment
946*/
947static void usage(const char *argv0){
948 fprintf(stderr, "Usage %s FILENAME ?args...?\n\n", argv0);
949 fprintf(stderr,
950 "args:\n"
951 " dbheader Show database header\n"
drh3aeea462012-04-03 14:59:50 +0000952 " pgidx Index of how each page is used\n"
drh344a97b2013-02-19 22:26:51 +0000953 " ptrmap Show all PTRMAP page content\n"
drh7d105f82010-08-23 15:26:49 +0000954 " NNN..MMM Show hex of pages NNN through MMM\n"
955 " NNN..end Show hex of pages NNN through end of file\n"
956 " NNNb Decode btree page NNN\n"
drh5240aeb2011-01-06 01:26:38 +0000957 " NNNbc Decode btree page NNN and show content\n"
958 " NNNbm Decode btree page NNN and show a layout map\n"
drh6a30cd52014-06-19 23:38:53 +0000959 " NNNbdCCC Decode cell CCC on btree page NNN\n"
drh7d105f82010-08-23 15:26:49 +0000960 " NNNt Decode freelist trunk page NNN\n"
drh47fb0002011-04-13 16:52:41 +0000961 " NNNtd Show leaf freelist pages on the decode\n"
dan53d89cd2014-08-20 10:42:16 +0000962 " NNNtr Recursively decode freelist starting at NNN\n"
drh7d105f82010-08-23 15:26:49 +0000963 );
964}
965
drh0de8c112002-07-06 16:32:14 +0000966int main(int argc, char **argv){
967 struct stat sbuf;
drh562cedb2010-04-26 15:44:07 +0000968 unsigned char zPgSz[2];
drh0de8c112002-07-06 16:32:14 +0000969 if( argc<2 ){
drh7d105f82010-08-23 15:26:49 +0000970 usage(argv[0]);
drh0de8c112002-07-06 16:32:14 +0000971 exit(1);
972 }
973 db = open(argv[1], O_RDONLY);
974 if( db<0 ){
975 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
976 exit(1);
977 }
drh562cedb2010-04-26 15:44:07 +0000978 zPgSz[0] = 0;
979 zPgSz[1] = 0;
980 lseek(db, 16, SEEK_SET);
drh4bb77ec2014-06-30 11:14:26 +0000981 if( read(db, zPgSz, 2)<2 ) memset(zPgSz, 0, 2);
drh7d105f82010-08-23 15:26:49 +0000982 pagesize = zPgSz[0]*256 + zPgSz[1]*65536;
drh562cedb2010-04-26 15:44:07 +0000983 if( pagesize==0 ) pagesize = 1024;
984 printf("Pagesize: %d\n", pagesize);
drh0de8c112002-07-06 16:32:14 +0000985 fstat(db, &sbuf);
drh748c7352015-04-15 15:29:05 +0000986 mxPage = (sbuf.st_size+pagesize-1)/pagesize;
drh562cedb2010-04-26 15:44:07 +0000987 printf("Available pages: 1..%d\n", mxPage);
drh0de8c112002-07-06 16:32:14 +0000988 if( argc==2 ){
989 int i;
990 for(i=1; i<=mxPage; i++) print_page(i);
991 }else{
992 int i;
993 for(i=2; i<argc; i++){
994 int iStart, iEnd;
995 char *zLeft;
drh562cedb2010-04-26 15:44:07 +0000996 if( strcmp(argv[i], "dbheader")==0 ){
997 print_db_header();
998 continue;
999 }
drh3aeea462012-04-03 14:59:50 +00001000 if( strcmp(argv[i], "pgidx")==0 ){
1001 page_usage_report(argv[1]);
1002 continue;
1003 }
drh344a97b2013-02-19 22:26:51 +00001004 if( strcmp(argv[i], "ptrmap")==0 ){
1005 ptrmap_coverage_report(argv[1]);
1006 continue;
1007 }
1008 if( strcmp(argv[i], "help")==0 ){
1009 usage(argv[0]);
1010 continue;
1011 }
drh562cedb2010-04-26 15:44:07 +00001012 if( !isdigit(argv[i][0]) ){
1013 fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]);
1014 continue;
1015 }
drh0de8c112002-07-06 16:32:14 +00001016 iStart = strtol(argv[i], &zLeft, 0);
1017 if( zLeft && strcmp(zLeft,"..end")==0 ){
1018 iEnd = mxPage;
1019 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
1020 iEnd = strtol(&zLeft[2], 0, 0);
drh7ecc1472010-04-26 16:47:12 +00001021 }else if( zLeft && zLeft[0]=='b' ){
1022 int ofst, nByte, hdrSize;
1023 unsigned char *a;
1024 if( iStart==1 ){
1025 ofst = hdrSize = 100;
1026 nByte = pagesize-100;
1027 }else{
1028 hdrSize = 0;
1029 ofst = (iStart-1)*pagesize;
1030 nByte = pagesize;
1031 }
1032 a = getContent(ofst, nByte);
drh100335b2011-01-05 21:20:52 +00001033 decode_btree_page(a, iStart, hdrSize, &zLeft[1]);
drh7ecc1472010-04-26 16:47:12 +00001034 free(a);
1035 continue;
drh7d105f82010-08-23 15:26:49 +00001036 }else if( zLeft && zLeft[0]=='t' ){
drh7d105f82010-08-23 15:26:49 +00001037 int detail = 0;
1038 int recursive = 0;
1039 int i;
1040 for(i=1; zLeft[i]; i++){
1041 if( zLeft[i]=='r' ) recursive = 1;
1042 if( zLeft[i]=='d' ) detail = 1;
1043 }
1044 decode_trunk_page(iStart, pagesize, detail, recursive);
1045 continue;
drh0de8c112002-07-06 16:32:14 +00001046 }else{
1047 iEnd = iStart;
1048 }
1049 if( iStart<1 || iEnd<iStart || iEnd>mxPage ){
1050 fprintf(stderr,
1051 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n",
1052 mxPage);
1053 exit(1);
1054 }
1055 while( iStart<=iEnd ){
1056 print_page(iStart);
1057 iStart++;
1058 }
1059 }
1060 }
1061 close(db);
drhdb718d82014-01-28 20:36:22 +00001062 return 0;
drh0de8c112002-07-06 16:32:14 +00001063}