int main(int argc, char **argv){ struct stat sbuf; unsigned char zPgSz[2]; if( argc<2 ){ usage(argv[0]); exit(1); } db = open(argv[1], O_RDONLY); if( db<0 ){ fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]); exit(1); } zPgSz[0] = 0; zPgSz[1] = 0; lseek(db, 16, SEEK_SET); read(db, zPgSz, 2); pagesize = zPgSz[0]*256 + zPgSz[1]*65536; if( pagesize==0 ) pagesize = 1024; printf("Pagesize: %d\n", pagesize); fstat(db, &sbuf); mxPage = sbuf.st_size/pagesize; printf("Available pages: 1..%d\n", mxPage); if( argc==2 ){ int i; for(i=1; i<=mxPage; i++) print_page(i); }else{ int i; for(i=2; i<argc; i++){ int iStart, iEnd; char *zLeft; if( strcmp(argv[i], "dbheader")==0 ){ print_db_header(); continue; } if( strcmp(argv[i], "pgidx")==0 ){ page_usage_report(argv[1]); continue; } if( !isdigit(argv[i][0]) ){ fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]); continue; } iStart = strtol(argv[i], &zLeft, 0); if( zLeft && strcmp(zLeft,"..end")==0 ){ iEnd = mxPage; }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ iEnd = strtol(&zLeft[2], 0, 0); }else if( zLeft && zLeft[0]=='b' ){ int ofst, nByte, hdrSize; unsigned char *a; if( iStart==1 ){ ofst = hdrSize = 100; nByte = pagesize-100; }else{ hdrSize = 0; ofst = (iStart-1)*pagesize; nByte = pagesize; } a = getContent(ofst, nByte); decode_btree_page(a, iStart, hdrSize, &zLeft[1]); free(a); continue; }else if( zLeft && zLeft[0]=='t' ){ unsigned char *a; int detail = 0; int recursive = 0; int i; for(i=1; zLeft[i]; i++){ if( zLeft[i]=='r' ) recursive = 1; if( zLeft[i]=='d' ) detail = 1; } decode_trunk_page(iStart, pagesize, detail, recursive); continue; }else{ iEnd = iStart; } if( iStart<1 || iEnd<iStart || iEnd>mxPage ){ fprintf(stderr, "Page argument should be LOWER?..UPPER?. Range 1 to %d\n", mxPage); exit(1); } while( iStart<=iEnd ){ print_page(iStart); iStart++; } } } close(db); }
int main(int argc, char **argv){ sqlite3_int64 szFile; unsigned char *zPgSz; const char *zPrg = argv[0]; /* Name of this executable */ char **azArg = argv; int nArg = argc; /* Check for the "--uri" or "-uri" switch. */ if( nArg>1 ){ if( sqlite3_stricmp("-raw", azArg[1])==0 || sqlite3_stricmp("--raw", azArg[1])==0 ){ g.bRaw = 1; azArg++; nArg--; } } if( nArg<2 ){ usage(zPrg); exit(1); } fileOpen(zPrg, azArg[1]); szFile = fileGetsize(); zPgSz = fileRead(16, 2); g.pagesize = zPgSz[0]*256 + zPgSz[1]*65536; if( g.pagesize==0 ) g.pagesize = 1024; sqlite3_free(zPgSz); printf("Pagesize: %d\n", g.pagesize); g.mxPage = (int)((szFile+g.pagesize-1)/g.pagesize); printf("Available pages: 1..%d\n", g.mxPage); if( nArg==2 ){ int i; for(i=1; i<=g.mxPage; i++) print_page(i); }else{ int i; for(i=2; i<nArg; i++){ int iStart, iEnd; char *zLeft; if( strcmp(azArg[i], "dbheader")==0 ){ print_db_header(); continue; } if( strcmp(azArg[i], "pgidx")==0 ){ page_usage_report(zPrg, azArg[1]); continue; } if( strcmp(azArg[i], "ptrmap")==0 ){ ptrmap_coverage_report(azArg[1]); continue; } if( strcmp(azArg[i], "help")==0 ){ usage(zPrg); continue; } if( !ISDIGIT(azArg[i][0]) ){ fprintf(stderr, "%s: unknown option: [%s]\n", zPrg, azArg[i]); continue; } iStart = strtol(azArg[i], &zLeft, 0); if( zLeft && strcmp(zLeft,"..end")==0 ){ iEnd = g.mxPage; }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ iEnd = strtol(&zLeft[2], 0, 0); }else if( zLeft && zLeft[0]=='b' ){ int ofst, nByte, hdrSize; unsigned char *a; if( iStart==1 ){ ofst = hdrSize = 100; nByte = g.pagesize-100; }else{ hdrSize = 0; ofst = (iStart-1)*g.pagesize; nByte = g.pagesize; } a = fileRead(ofst, nByte); decode_btree_page(a, iStart, hdrSize, &zLeft[1]); sqlite3_free(a); continue; }else if( zLeft && zLeft[0]=='t' ){ int detail = 0; int recursive = 0; int j; for(j=1; zLeft[j]; j++){ if( zLeft[j]=='r' ) recursive = 1; if( zLeft[j]=='d' ) detail = 1; } decode_trunk_page(iStart, detail, recursive); continue; }else{ iEnd = iStart; } if( iStart<1 || iEnd<iStart || iEnd>g.mxPage ){ fprintf(stderr, "Page argument should be LOWER?..UPPER?. Range 1 to %d\n", g.mxPage); exit(1); } while( iStart<=iEnd ){ print_page(iStart); iStart++; } } } fileClose(); return 0; }