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/dbtotxt.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 / dbtotxt.c` 204 lines ยท 6363 bytes Raw Blame History 1 /* 2 ** 2018-12-13 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ****************************************************************************** 12 ** 13 ** This file implements a stand-alone utility program that converts 14 ** a binary file (usually an SQLite database) into a text format that 15 ** is compact and friendly to human-readers. 16 ** 17 ** Usage: 18 ** 19 ** dbtotxt [OPTIONS] FILENAME 20 ** 21 ** where OPTIONS are zero or more of: 22 ** 23 ** --for-cli prepending '.open --hexdb' to the output 24 ** 25 ** --script The input file is expected to start with a 26 ** zero-terminated SQL string. Output the 27 ** ".open --hexdb" header, then the database 28 ** then the SQL. 29 ** 30 ** --pagesize N set the database page size for later reading 31 ** 32 ** --raw not actually a database, just binary data 33 ** 34 ** The translation of the database appears on standard output. If the 35 ** --pagesize command-line option is omitted, then the page size is taken 36 ** from the database header. 37 ** 38 ** Compactness is achieved by suppressing lines of all zero bytes. This 39 ** works well at compressing test databases that are mostly empty. But 40 ** the output will probably be lengthy for a real database containing lots 41 ** of real content. For maximum compactness, it is suggested that test 42 ** databases be constructed with "zeroblob()" rather than "randomblob()" 43 ** used for filler content and with "PRAGMA secure_delete=ON" selected to 44 ** zero-out deleted content. 45 */ 46 #include 47 #include 48 #include 49 #include 50 51 /* Return true if the line is all zeros */ 52 static int allZero(unsigned char *aLine){ 53 int i; 54 for(i=0; i<16 && aLine[i]==0; i++){} 55 return i==16; 56 } 57 58 int main(int argc, char **argv){ 59 int pgsz = 0; /* page size */ 60 int forCli = 0; /* whether to prepend with .open */ 61 int bSQL = 0; /* Expect and SQL prefix */ 62 long szFile; /* Size of the input file in bytes */ 63 FILE *in; /* Input file */ 64 int nSQL; /* Number of bytes of script */ 65 int i, j; /* Loop counters */ 66 int nErr = 0; /* Number of errors */ 67 const char *zInputFile = 0; /* Name of the input file */ 68 const char *zBaseName = 0; /* Base name of the file */ 69 int lastPage = 0; /* Last page number shown */ 70 int iPage; /* Current page number */ 71 int bRaw = 0; /* Not actually a database */ 72 unsigned char *aData = 0; /* All data */ 73 unsigned char *aLine; /* A single line of the file */ 74 unsigned char *aHdr; /* File header */ 75 unsigned char bShow[256]; /* Characters ok to display */ 76 memset(bShow, '.', sizeof(bShow)); 77 for(i=' '; i<='~'; i++){ 78 if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = (unsigned char)i; 79 } 80 for(i=1; i65536 || (pgsz&(pgsz-1))!=0 ){ 89 fprintf(stderr, "Page size must be a power of two between" 90 " 512 and 65536.\n"); 91 nErr++; 92 } 93 continue; 94 }else if( strcmp(z,"for-cli")==0 ){ 95 forCli = 1; 96 continue; 97 }else if( strcmp(z,"script")==0 ){ 98 forCli = 1; 99 bSQL = 1; 100 continue; 101 }else if( strcmp(z,"raw")==0 ){ 102 bRaw = 1; 103 continue; 104 } 105 fprintf(stderr, "Unknown option: %s\n", argv[i]); 106 nErr++; 107 }else if( zInputFile ){ 108 fprintf(stderr, "Already using a different input file: [%s]\n", argv[i]); 109 nErr++; 110 }else{ 111 zInputFile = argv[i]; 112 } 113 } 114 if( zInputFile==0 ){ 115 fprintf(stderr, "No input file specified.\n"); 116 nErr++; 117 } 118 if( nErr ){ 119 fprintf(stderr, 120 "Usage: %s [--pagesize N] [--script] [--for-cli] FILENAME\n", argv[0]); 121 exit(1); 122 } 123 in = fopen(zInputFile, "rb"); 124 if( in==0 ){ 125 fprintf(stderr, "Cannot open input file [%s]\n", zInputFile); 126 exit(1); 127 } 128 fseek(in, 0, SEEK_END); 129 szFile = ftell(in); 130 rewind(in); 131 if( szFile<100 && !bRaw ){ 132 fprintf(stderr, "File too short. Minimum size is 100 bytes.\n"); 133 exit(1); 134 } 135 aData = malloc( szFile+16 ); 136 if( aData==0 ){ 137 fprintf(stderr, "Failed to allocate %ld bytes\n", szFile); 138 exit(1); 139 } 140 if( fread(aData, szFile, 1, in)!=1 ){ 141 fprintf(stderr, "Cannot read file info memory\n"); 142 exit(1); 143 } 144 memset(aData+szFile, 0, 16); 145 fclose(in); 146 if( bSQL ){ 147 for(i=0; i0 ){ 200 printf("%s\n", aData); 201 } 202 free( aData ); 203 return 0; 204 }