OLD | NEW |
1 /* | 1 /* |
2 ** A utility for printing an SQLite database journal. | 2 ** A utility for printing an SQLite database journal. |
3 */ | 3 */ |
4 #include <stdio.h> | 4 #include <stdio.h> |
5 #include <ctype.h> | 5 #include <ctype.h> |
6 #include <sys/types.h> | |
7 #include <sys/stat.h> | |
8 #include <fcntl.h> | |
9 #include <unistd.h> | |
10 #include <stdlib.h> | 6 #include <stdlib.h> |
| 7 #include <string.h> |
11 | 8 |
| 9 /* |
| 10 ** state information |
| 11 */ |
| 12 static int pageSize = 1024; |
| 13 static int sectorSize = 512; |
| 14 static FILE *db = 0; |
| 15 static int showPageContent = 0; |
| 16 static int fileSize = 0; |
| 17 static unsigned cksumNonce = 0; |
12 | 18 |
13 static int pagesize = 1024; | 19 /* Report a memory allocation error */ |
14 static int db = -1; | |
15 static int mxPage = 0; | |
16 | |
17 static void out_of_memory(void){ | 20 static void out_of_memory(void){ |
18 fprintf(stderr,"Out of memory...\n"); | 21 fprintf(stderr,"Out of memory...\n"); |
19 exit(1); | 22 exit(1); |
20 } | 23 } |
21 | 24 |
22 static print_page(int iPg){ | 25 /* |
| 26 ** Read N bytes of memory starting at iOfst into space obtained |
| 27 ** from malloc(). |
| 28 */ |
| 29 static char *read_content(int N, int iOfst){ |
| 30 int got; |
| 31 char *pBuf = malloc(N); |
| 32 if( pBuf==0 ) out_of_memory(); |
| 33 fseek(db, iOfst, SEEK_SET); |
| 34 got = fread(pBuf, 1, N, db); |
| 35 if( got<0 ){ |
| 36 fprintf(stderr, "I/O error reading %d bytes from %d\n", N, iOfst); |
| 37 memset(pBuf, 0, N); |
| 38 }else if( got<N ){ |
| 39 fprintf(stderr, "Short read: got only %d of %d bytes from %d\n", |
| 40 got, N, iOfst); |
| 41 memset(&pBuf[got], 0, N-got); |
| 42 } |
| 43 return pBuf; |
| 44 } |
| 45 |
| 46 /* Print a line of decode output showing a 4-byte integer. |
| 47 */ |
| 48 static unsigned print_decode_line( |
| 49 unsigned char *aData, /* Content being decoded */ |
| 50 int ofst, int nByte, /* Start and size of decode */ |
| 51 const char *zMsg /* Message to append */ |
| 52 ){ |
| 53 int i, j; |
| 54 unsigned val = aData[ofst]; |
| 55 char zBuf[100]; |
| 56 sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]); |
| 57 i = strlen(zBuf); |
| 58 for(j=1; j<4; j++){ |
| 59 if( j>=nByte ){ |
| 60 sprintf(&zBuf[i], " "); |
| 61 }else{ |
| 62 sprintf(&zBuf[i], " %02x", aData[ofst+j]); |
| 63 val = val*256 + aData[ofst+j]; |
| 64 } |
| 65 i += strlen(&zBuf[i]); |
| 66 } |
| 67 sprintf(&zBuf[i], " %10u", val); |
| 68 printf("%s %s\n", zBuf, zMsg); |
| 69 return val; |
| 70 } |
| 71 |
| 72 /* |
| 73 ** Read and print a journal header. Store key information (page size, etc) |
| 74 ** in global variables. |
| 75 */ |
| 76 static unsigned decode_journal_header(int iOfst){ |
| 77 char *pHdr = read_content(64, iOfst); |
| 78 unsigned nPage; |
| 79 printf("Header at offset %d:\n", iOfst); |
| 80 print_decode_line(pHdr, 0, 4, "Header part 1 (3654616569)"); |
| 81 print_decode_line(pHdr, 4, 4, "Header part 2 (547447767)"); |
| 82 nPage = |
| 83 print_decode_line(pHdr, 8, 4, "page count"); |
| 84 cksumNonce = |
| 85 print_decode_line(pHdr, 12, 4, "chksum nonce"); |
| 86 print_decode_line(pHdr, 16, 4, "initial database size in pages"); |
| 87 sectorSize = |
| 88 print_decode_line(pHdr, 20, 4, "sector size"); |
| 89 pageSize = |
| 90 print_decode_line(pHdr, 24, 4, "page size"); |
| 91 print_decode_line(pHdr, 28, 4, "zero"); |
| 92 print_decode_line(pHdr, 32, 4, "zero"); |
| 93 print_decode_line(pHdr, 36, 4, "zero"); |
| 94 print_decode_line(pHdr, 40, 4, "zero"); |
| 95 free(pHdr); |
| 96 return nPage; |
| 97 } |
| 98 |
| 99 static void print_page(int iOfst){ |
23 unsigned char *aData; | 100 unsigned char *aData; |
24 int i, j; | 101 char zTitle[50]; |
25 aData = malloc(pagesize); | 102 aData = read_content(pageSize+8, iOfst); |
26 if( aData==0 ) out_of_memory(); | 103 sprintf(zTitle, "page number for page at offset %d", iOfst); |
27 read(db, aData, pagesize); | 104 print_decode_line(aData, 0, 4, zTitle); |
28 fprintf(stdout, "Page %d:\n", iPg); | |
29 for(i=0; i<pagesize; i += 16){ | |
30 fprintf(stdout, " %03x: ",i); | |
31 for(j=0; j<16; j++){ | |
32 fprintf(stdout,"%02x ", aData[i+j]); | |
33 } | |
34 for(j=0; j<16; j++){ | |
35 fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.'); | |
36 } | |
37 fprintf(stdout,"\n"); | |
38 } | |
39 free(aData); | 105 free(aData); |
40 } | 106 } |
41 | 107 |
42 int main(int argc, char **argv){ | 108 int main(int argc, char **argv){ |
43 struct stat sbuf; | |
44 unsigned int u; | |
45 int rc; | 109 int rc; |
46 unsigned char zBuf[10]; | 110 int nPage, cnt; |
47 unsigned char zBuf2[sizeof(u)]; | 111 int iOfst; |
48 if( argc!=2 ){ | 112 if( argc!=2 ){ |
49 fprintf(stderr,"Usage: %s FILENAME\n", argv[0]); | 113 fprintf(stderr,"Usage: %s FILENAME\n", argv[0]); |
50 exit(1); | 114 exit(1); |
51 } | 115 } |
52 db = open(argv[1], O_RDONLY); | 116 db = fopen(argv[1], "rb"); |
53 if( db<0 ){ | 117 if( db==0 ){ |
54 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]); | 118 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]); |
55 exit(1); | 119 exit(1); |
56 } | 120 } |
57 read(db, zBuf, 8); | 121 fseek(db, 0, SEEK_END); |
58 if( zBuf[7]==0xd6 ){ | 122 fileSize = ftell(db); |
59 read(db, &u, sizeof(u)); | 123 printf("journal file size: %d bytes\n", fileSize); |
60 printf("Records in Journal: %u\n", u); | 124 fseek(db, 0, SEEK_SET); |
61 read(db, &u, sizeof(u)); | 125 iOfst = 0; |
62 printf("Magic Number: 0x%08x\n", u); | 126 while( iOfst<fileSize ){ |
| 127 cnt = nPage = (int)decode_journal_header(iOfst); |
| 128 if( cnt==0 ){ |
| 129 cnt = (fileSize - sectorSize)/(pageSize+8); |
| 130 } |
| 131 iOfst += sectorSize; |
| 132 while( cnt && iOfst<fileSize ){ |
| 133 print_page(iOfst); |
| 134 iOfst += pageSize+8; |
| 135 } |
| 136 iOfst = (iOfst/sectorSize + 1)*sectorSize; |
63 } | 137 } |
64 read(db, zBuf2, sizeof(zBuf2)); | 138 fclose(db); |
65 u = zBuf2[0]<<24 | zBuf2[1]<<16 | zBuf2[2]<<8 | zBuf2[3]; | |
66 printf("Database Size: %u\n", u); | |
67 while( read(db, zBuf2, sizeof(zBuf2))==sizeof(zBuf2) ){ | |
68 u = zBuf2[0]<<24 | zBuf2[1]<<16 | zBuf2[2]<<8 | zBuf2[3]; | |
69 print_page(u); | |
70 if( zBuf[7]==0xd6 ){ | |
71 read(db, &u, sizeof(u)); | |
72 printf("Checksum: 0x%08x\n", u); | |
73 } | |
74 } | |
75 close(db); | |
76 } | 139 } |
OLD | NEW |