establish default sqlcipher log level and target upon first activation
[sqlcipher.git] / tool / showjournal.c
blob51c89da499bc9b53a6b75785677a58ecd3169d02
1 /*
2 ** A utility for printing an SQLite database journal.
3 */
4 #include <stdio.h>
5 #include <ctype.h>
6 #include <stdlib.h>
7 #include <string.h>
9 /*
10 ** state information
12 static int pageSize = 1024;
13 static int sectorSize = 512;
14 static FILE *db = 0;
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");
21 exit(1);
25 ** Read N bytes of memory starting at iOfst into space obtained
26 ** from malloc().
28 static unsigned char *read_content(int N, int iOfst){
29 int got;
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);
34 if( got<0 ){
35 fprintf(stderr, "I/O error reading %d bytes from %d\n", N, iOfst);
36 memset(pBuf, 0, N);
37 }else if( got<N ){
38 fprintf(stderr, "Short read: got only %d of %d bytes from %d\n",
39 got, N, iOfst);
40 memset(&pBuf[got], 0, N-got);
42 return pBuf;
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 */
52 int i, j;
53 unsigned val = aData[ofst];
54 char zBuf[100];
55 sprintf(zBuf, " %05x: %02x", ofst, aData[ofst]);
56 i = (int)strlen(zBuf);
57 for(j=1; j<4; j++){
58 if( j>=nByte ){
59 sprintf(&zBuf[i], " ");
60 }else{
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);
68 return val;
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);
77 unsigned nPage;
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)");
81 nPage =
82 print_decode_line(pHdr, 8, 4, "page count");
83 cksumNonce =
84 print_decode_line(pHdr, 12, 4, "chksum nonce");
85 print_decode_line(pHdr, 16, 4, "initial database size in pages");
86 sectorSize =
87 print_decode_line(pHdr, 20, 4, "sector size");
88 pageSize =
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");
94 free(pHdr);
95 return nPage;
98 static void print_page(int iOfst){
99 unsigned char *aData;
100 char zTitle[50];
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);
104 free(aData);
107 int main(int argc, char **argv){
108 int nPage, cnt;
109 int iOfst;
110 if( argc!=2 ){
111 fprintf(stderr,"Usage: %s FILENAME\n", argv[0]);
112 exit(1);
114 db = fopen(argv[1], "rb");
115 if( db==0 ){
116 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
117 exit(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);
123 iOfst = 0;
124 while( iOfst<fileSize ){
125 cnt = nPage = (int)decode_journal_header(iOfst);
126 if( cnt==0 ){
127 cnt = (fileSize - sectorSize)/(pageSize+8);
129 iOfst += sectorSize;
130 while( cnt && iOfst<fileSize ){
131 print_page(iOfst);
132 iOfst += pageSize+8;
134 iOfst = (iOfst/sectorSize + 1)*sectorSize;
136 fclose(db);
137 return 0;