Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/tool/showjournal.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / tool / showjournal.c` 138 lines ยท 3648 bytes Raw Blame History 1 /* 2 ** A utility for printing an SQLite database journal. 3 */ 4 #include 5 #include 6 #include 7 #include 8 9 /* 10 ** state information 11 */ 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; 17 18 /* Report a memory allocation error */ 19 static void out_of_memory(void){ 20 fprintf(stderr,"Out of memory...\n"); 21 exit(1); 22 } 23 24 /* 25 ** Read N bytes of memory starting at iOfst into space obtained 26 ** from malloc(). 27 */ 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=nByte ){ 59 sprintf(&zBuf[i], " "); 60 }else{ 61 sprintf(&zBuf[i], " %02x", aData[ofst+j]); 62 val = val*256 + aData[ofst+j]; 63 } 64 i += (int)strlen(&zBuf[i]); 65 } 66 sprintf(&zBuf[i], " %10u", val); 67 printf("%s %s\n", zBuf, zMsg); 68 return val; 69 } 70 71 /* 72 ** Read and print a journal header. Store key information (page size, etc) 73 ** in global variables. 74 */ 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; 96 } 97 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); 105 } 106 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); 113 } 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); 118 } 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