2 ** A utility for printing an SQLite database journal.
12 static int pageSize
= 1024;
13 static int sectorSize
= 512;
15 static int fileSize
= 0;
16 static unsigned cksumNonce
= 0;
18 /* Report a memory allocation error */
19 static void out_of_memory(void){
20 fprintf(stderr
,"Out of memory...\n");
25 ** Read N bytes of memory starting at iOfst into space obtained
28 static unsigned char *read_content(int N
, int iOfst
){
30 unsigned char *pBuf
= malloc(N
);
31 if( pBuf
==0 ) out_of_memory();
32 fseek(db
, iOfst
, SEEK_SET
);
33 got
= (int)fread(pBuf
, 1, N
, db
);
35 fprintf(stderr
, "I/O error reading %d bytes from %d\n", N
, iOfst
);
38 fprintf(stderr
, "Short read: got only %d of %d bytes from %d\n",
40 memset(&pBuf
[got
], 0, N
-got
);
45 /* Print a line of decode output showing a 4-byte integer.
47 static unsigned print_decode_line(
48 const unsigned char *aData
, /* Content being decoded */
49 int ofst
, int nByte
, /* Start and size of decode */
50 const char *zMsg
/* Message to append */
53 unsigned val
= aData
[ofst
];
55 sprintf(zBuf
, " %05x: %02x", ofst
, aData
[ofst
]);
56 i
= (int)strlen(zBuf
);
59 sprintf(&zBuf
[i
], " ");
61 sprintf(&zBuf
[i
], " %02x", aData
[ofst
+j
]);
62 val
= val
*256 + aData
[ofst
+j
];
64 i
+= (int)strlen(&zBuf
[i
]);
66 sprintf(&zBuf
[i
], " %10u", val
);
67 printf("%s %s\n", zBuf
, zMsg
);
72 ** Read and print a journal header. Store key information (page size, etc)
73 ** in global variables.
75 static unsigned decode_journal_header(int iOfst
){
76 unsigned char *pHdr
= read_content(64, iOfst
);
78 printf("Header at offset %d:\n", iOfst
);
79 print_decode_line(pHdr
, 0, 4, "Header part 1 (3654616569)");
80 print_decode_line(pHdr
, 4, 4, "Header part 2 (547447767)");
82 print_decode_line(pHdr
, 8, 4, "page count");
84 print_decode_line(pHdr
, 12, 4, "chksum nonce");
85 print_decode_line(pHdr
, 16, 4, "initial database size in pages");
87 print_decode_line(pHdr
, 20, 4, "sector size");
89 print_decode_line(pHdr
, 24, 4, "page size");
90 print_decode_line(pHdr
, 28, 4, "zero");
91 print_decode_line(pHdr
, 32, 4, "zero");
92 print_decode_line(pHdr
, 36, 4, "zero");
93 print_decode_line(pHdr
, 40, 4, "zero");
98 static void print_page(int iOfst
){
101 aData
= read_content(pageSize
+8, iOfst
);
102 sprintf(zTitle
, "page number for page at offset %d", iOfst
);
103 print_decode_line(aData
-iOfst
, iOfst
, 4, zTitle
);
107 int main(int argc
, char **argv
){
111 fprintf(stderr
,"Usage: %s FILENAME\n", argv
[0]);
114 db
= fopen(argv
[1], "rb");
116 fprintf(stderr
,"%s: can't open %s\n", argv
[0], argv
[1]);
119 fseek(db
, 0, SEEK_END
);
120 fileSize
= ftell(db
);
121 printf("journal file size: %d bytes\n", fileSize
);
122 fseek(db
, 0, SEEK_SET
);
124 while( iOfst
<fileSize
){
125 cnt
= nPage
= (int)decode_journal_header(iOfst
);
127 cnt
= (fileSize
- sectorSize
)/(pageSize
+8);
130 while( cnt
&& iOfst
<fileSize
){
134 iOfst
= (iOfst
/sectorSize
+ 1)*sectorSize
;