2 ** A utility for printing an SQLite database journal.
12 static int pageSize
= 1024;
13 static int sectorSize
= 512;
15 static int showPageContent
= 0;
16 static int fileSize
= 0;
17 static unsigned cksumNonce
= 0;
19 /* Report a memory allocation error */
20 static void out_of_memory(void){
21 fprintf(stderr
,"Out of memory...\n");
26 ** Read N bytes of memory starting at iOfst into space obtained
29 static char *read_content(int N
, int iOfst
){
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
);
36 fprintf(stderr
, "I/O error reading %d bytes from %d\n", N
, iOfst
);
39 fprintf(stderr
, "Short read: got only %d of %d bytes from %d\n",
41 memset(&pBuf
[got
], 0, N
-got
);
46 /* Print a line of decode output showing a 4-byte integer.
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 */
54 unsigned val
= aData
[ofst
];
56 sprintf(zBuf
, " %03x: %02x", ofst
, aData
[ofst
]);
60 sprintf(&zBuf
[i
], " ");
62 sprintf(&zBuf
[i
], " %02x", aData
[ofst
+j
]);
63 val
= val
*256 + aData
[ofst
+j
];
65 i
+= strlen(&zBuf
[i
]);
67 sprintf(&zBuf
[i
], " %10u", val
);
68 printf("%s %s\n", zBuf
, zMsg
);
73 ** Read and print a journal header. Store key information (page size, etc)
74 ** in global variables.
76 static unsigned decode_journal_header(int iOfst
){
77 char *pHdr
= read_content(64, iOfst
);
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)");
83 print_decode_line(pHdr
, 8, 4, "page count");
85 print_decode_line(pHdr
, 12, 4, "chksum nonce");
86 print_decode_line(pHdr
, 16, 4, "initial database size in pages");
88 print_decode_line(pHdr
, 20, 4, "sector size");
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");
99 static void print_page(int iOfst
){
100 unsigned char *aData
;
102 aData
= read_content(pageSize
+8, iOfst
);
103 sprintf(zTitle
, "page number for page at offset %d", iOfst
);
104 print_decode_line(aData
, 0, 4, zTitle
);
108 int main(int argc
, char **argv
){
113 fprintf(stderr
,"Usage: %s FILENAME\n", argv
[0]);
116 db
= fopen(argv
[1], "rb");
118 fprintf(stderr
,"%s: can't open %s\n", argv
[0], argv
[1]);
121 fseek(db
, 0, SEEK_END
);
122 fileSize
= ftell(db
);
123 printf("journal file size: %d bytes\n", fileSize
);
124 fseek(db
, 0, SEEK_SET
);
126 while( iOfst
<fileSize
){
127 cnt
= nPage
= (int)decode_journal_header(iOfst
);
129 cnt
= (fileSize
- sectorSize
)/(pageSize
+8);
132 while( cnt
&& iOfst
<fileSize
){
136 iOfst
= (iOfst
/sectorSize
+ 1)*sectorSize
;