showjournal.c 3.6 KB

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