offsets.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. ** This program searches an SQLite database file for the lengths and
  3. ** offsets for all TEXT or BLOB entries for a particular column of a
  4. ** particular table. The rowid, size and offset for the column are
  5. ** written to standard output. There are three arguments, which are the
  6. ** name of the database file, the table, and the column.
  7. */
  8. #include "sqlite3.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdarg.h>
  12. #include <string.h>
  13. typedef unsigned char u8;
  14. typedef struct GState GState;
  15. #define ArraySize(X) (sizeof(X)/sizeof(X[0]))
  16. /*
  17. ** Global state information for this program.
  18. */
  19. struct GState {
  20. char *zErr; /* Error message text */
  21. FILE *f; /* Open database file */
  22. int szPg; /* Page size for the database file */
  23. int iRoot; /* Root page of the table */
  24. int iCol; /* Column number for the column */
  25. int pgno; /* Current page number */
  26. u8 *aPage; /* Current page content */
  27. u8 *aStack[20]; /* Page stack */
  28. int aPgno[20]; /* Page number stack */
  29. int nStack; /* Depth of stack */
  30. int bTrace; /* True for tracing output */
  31. };
  32. /*
  33. ** Write an error.
  34. */
  35. static void ofstError(GState *p, const char *zFormat, ...){
  36. va_list ap;
  37. sqlite3_free(p->zErr);
  38. va_start(ap, zFormat);
  39. p->zErr = sqlite3_vmprintf(zFormat, ap);
  40. va_end(ap);
  41. }
  42. /*
  43. ** Write a trace message
  44. */
  45. static void ofstTrace(GState *p, const char *zFormat, ...){
  46. va_list ap;
  47. if( p->bTrace ){
  48. va_start(ap, zFormat);
  49. vprintf(zFormat, ap);
  50. va_end(ap);
  51. }
  52. }
  53. /*
  54. ** Find the root page of the table and the column number of the column.
  55. */
  56. static void ofstRootAndColumn(
  57. GState *p, /* Global state */
  58. const char *zFile, /* Name of the database file */
  59. const char *zTable, /* Name of the table */
  60. const char *zColumn /* Name of the column */
  61. ){
  62. sqlite3 *db = 0;
  63. sqlite3_stmt *pStmt = 0;
  64. char *zSql = 0;
  65. int rc;
  66. if( p->zErr ) return;
  67. rc = sqlite3_open(zFile, &db);
  68. if( rc ){
  69. ofstError(p, "cannot open database file \"%s\"", zFile);
  70. goto rootAndColumn_exit;
  71. }
  72. zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_schema WHERE name=%Q",
  73. zTable);
  74. rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  75. if( rc ) ofstError(p, "%s: [%s]", sqlite3_errmsg(db), zSql);
  76. sqlite3_free(zSql);
  77. if( p->zErr ) goto rootAndColumn_exit;
  78. if( sqlite3_step(pStmt)!=SQLITE_ROW ){
  79. ofstError(p, "cannot find table [%s]\n", zTable);
  80. sqlite3_finalize(pStmt);
  81. goto rootAndColumn_exit;
  82. }
  83. p->iRoot = sqlite3_column_int(pStmt , 0);
  84. sqlite3_finalize(pStmt);
  85. p->iCol = -1;
  86. zSql = sqlite3_mprintf("PRAGMA table_info(%Q)", zTable);
  87. rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  88. if( rc ) ofstError(p, "%s: [%s}", sqlite3_errmsg(db), zSql);
  89. sqlite3_free(zSql);
  90. if( p->zErr ) goto rootAndColumn_exit;
  91. while( sqlite3_step(pStmt)==SQLITE_ROW ){
  92. const char *zCol = sqlite3_column_text(pStmt, 1);
  93. if( strlen(zCol)==strlen(zColumn)
  94. && sqlite3_strnicmp(zCol, zColumn, strlen(zCol))==0
  95. ){
  96. p->iCol = sqlite3_column_int(pStmt, 0);
  97. break;
  98. }
  99. }
  100. sqlite3_finalize(pStmt);
  101. if( p->iCol<0 ){
  102. ofstError(p, "no such column: %s.%s", zTable, zColumn);
  103. goto rootAndColumn_exit;
  104. }
  105. zSql = sqlite3_mprintf("PRAGMA page_size");
  106. rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  107. if( rc ) ofstError(p, "%s: [%s]", sqlite3_errmsg(db), zSql);
  108. sqlite3_free(zSql);
  109. if( p->zErr ) goto rootAndColumn_exit;
  110. if( sqlite3_step(pStmt)!=SQLITE_ROW ){
  111. ofstError(p, "cannot find page size");
  112. }else{
  113. p->szPg = sqlite3_column_int(pStmt, 0);
  114. }
  115. sqlite3_finalize(pStmt);
  116. rootAndColumn_exit:
  117. sqlite3_close(db);
  118. return;
  119. }
  120. /*
  121. ** Pop a page from the stack
  122. */
  123. static void ofstPopPage(GState *p){
  124. if( p->nStack<=0 ) return;
  125. p->nStack--;
  126. sqlite3_free(p->aStack[p->nStack]);
  127. p->pgno = p->aPgno[p->nStack-1];
  128. p->aPage = p->aStack[p->nStack-1];
  129. }
  130. /*
  131. ** Push a new page onto the stack.
  132. */
  133. static void ofstPushPage(GState *p, int pgno){
  134. u8 *pPage;
  135. size_t got;
  136. if( p->zErr ) return;
  137. if( p->nStack >= ArraySize(p->aStack) ){
  138. ofstError(p, "page stack overflow");
  139. return;
  140. }
  141. p->aPgno[p->nStack] = pgno;
  142. p->aStack[p->nStack] = pPage = sqlite3_malloc( p->szPg );
  143. if( pPage==0 ){
  144. fprintf(stderr, "out of memory\n");
  145. exit(1);
  146. }
  147. p->nStack++;
  148. p->aPage = pPage;
  149. p->pgno = pgno;
  150. fseek(p->f, (pgno-1)*p->szPg, SEEK_SET);
  151. got = fread(pPage, 1, p->szPg, p->f);
  152. if( got!=p->szPg ){
  153. ofstError(p, "unable to read page %d", pgno);
  154. ofstPopPage(p);
  155. }
  156. }
  157. /* Read a two-byte integer at the given offset into the current page */
  158. static int ofst2byte(GState *p, int ofst){
  159. int x = p->aPage[ofst];
  160. return (x<<8) + p->aPage[ofst+1];
  161. }
  162. /* Read a four-byte integer at the given offset into the current page */
  163. static int ofst4byte(GState *p, int ofst){
  164. int x = p->aPage[ofst];
  165. x = (x<<8) + p->aPage[ofst+1];
  166. x = (x<<8) + p->aPage[ofst+2];
  167. x = (x<<8) + p->aPage[ofst+3];
  168. return x;
  169. }
  170. /* Read a variable-length integer. Update the offset */
  171. static sqlite3_int64 ofstVarint(GState *p, int *pOfst){
  172. sqlite3_int64 x = 0;
  173. u8 *a = &p->aPage[*pOfst];
  174. int n = 0;
  175. while( n<8 && (a[0] & 0x80)!=0 ){
  176. x = (x<<7) + (a[0] & 0x7f);
  177. n++;
  178. a++;
  179. }
  180. if( n==8 ){
  181. x = (x<<8) + a[0];
  182. }else{
  183. x = (x<<7) + a[0];
  184. }
  185. *pOfst += (n+1);
  186. return x;
  187. }
  188. /* Return the absolute offset into a file for the given offset
  189. ** into the current page */
  190. static int ofstInFile(GState *p, int ofst){
  191. return p->szPg*(p->pgno-1) + ofst;
  192. }
  193. /* Return the size (in bytes) of the data corresponding to the
  194. ** given serial code */
  195. static int ofstSerialSize(int scode){
  196. if( scode<5 ) return scode;
  197. if( scode==5 ) return 6;
  198. if( scode<8 ) return 8;
  199. if( scode<12 ) return 0;
  200. return (scode-12)/2;
  201. }
  202. /* Forward reference */
  203. static void ofstWalkPage(GState*, int);
  204. /* Walk an interior btree page */
  205. static void ofstWalkInteriorPage(GState *p){
  206. int nCell;
  207. int i;
  208. int ofst;
  209. int iChild;
  210. nCell = ofst2byte(p, 3);
  211. for(i=0; i<nCell; i++){
  212. ofst = ofst2byte(p, 12+i*2);
  213. iChild = ofst4byte(p, ofst);
  214. ofstWalkPage(p, iChild);
  215. if( p->zErr ) return;
  216. }
  217. ofstWalkPage(p, ofst4byte(p, 8));
  218. }
  219. /* Walk a leaf btree page */
  220. static void ofstWalkLeafPage(GState *p){
  221. int nCell;
  222. int i;
  223. int ofst;
  224. int nPayload;
  225. sqlite3_int64 rowid;
  226. int nHdr;
  227. int j;
  228. int scode;
  229. int sz;
  230. int dataOfst;
  231. char zMsg[200];
  232. nCell = ofst2byte(p, 3);
  233. for(i=0; i<nCell; i++){
  234. ofst = ofst2byte(p, 8+i*2);
  235. nPayload = ofstVarint(p, &ofst);
  236. rowid = ofstVarint(p, &ofst);
  237. if( nPayload > p->szPg-35 ){
  238. sqlite3_snprintf(sizeof(zMsg), zMsg,
  239. "# overflow rowid %lld", rowid);
  240. printf("%s\n", zMsg);
  241. continue;
  242. }
  243. dataOfst = ofst;
  244. nHdr = ofstVarint(p, &ofst);
  245. dataOfst += nHdr;
  246. for(j=0; j<p->iCol; j++){
  247. scode = ofstVarint(p, &ofst);
  248. dataOfst += ofstSerialSize(scode);
  249. }
  250. scode = ofstVarint(p, &ofst);
  251. sz = ofstSerialSize(scode);
  252. sqlite3_snprintf(sizeof(zMsg), zMsg,
  253. "rowid %12lld size %5d offset %8d",
  254. rowid, sz, ofstInFile(p, dataOfst));
  255. printf("%s\n", zMsg);
  256. }
  257. }
  258. /*
  259. ** Output results from a single page.
  260. */
  261. static void ofstWalkPage(GState *p, int pgno){
  262. if( p->zErr ) return;
  263. ofstPushPage(p, pgno);
  264. if( p->zErr ) return;
  265. if( p->aPage[0]==5 ){
  266. ofstWalkInteriorPage(p);
  267. }else if( p->aPage[0]==13 ){
  268. ofstWalkLeafPage(p);
  269. }else{
  270. ofstError(p, "page %d has a faulty type byte: %d", pgno, p->aPage[0]);
  271. }
  272. ofstPopPage(p);
  273. }
  274. int main(int argc, char **argv){
  275. GState g;
  276. memset(&g, 0, sizeof(g));
  277. if( argc>2 && strcmp(argv[1],"--trace")==0 ){
  278. g.bTrace = 1;
  279. argc--;
  280. argv++;
  281. }
  282. if( argc!=4 ){
  283. fprintf(stderr, "Usage: %s DATABASE TABLE COLUMN\n", *argv);
  284. exit(1);
  285. }
  286. ofstRootAndColumn(&g, argv[1], argv[2], argv[3]);
  287. if( g.zErr ){
  288. fprintf(stderr, "%s\n", g.zErr);
  289. exit(1);
  290. }
  291. ofstTrace(&g, "# szPg = %d\n", g.szPg);
  292. ofstTrace(&g, "# iRoot = %d\n", g.iRoot);
  293. ofstTrace(&g, "# iCol = %d\n", g.iCol);
  294. g.f = fopen(argv[1], "rb");
  295. if( g.f==0 ){
  296. fprintf(stderr, "cannot open \"%s\"\n", argv[1]);
  297. exit(1);
  298. }
  299. ofstWalkPage(&g, g.iRoot);
  300. if( g.zErr ){
  301. fprintf(stderr, "%s\n", g.zErr);
  302. exit(1);
  303. }
  304. return 0;
  305. }