interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

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 <stdio.h>
47#include <string.h>
48#include <stdlib.h>
49#include <ctype.h>
50
51/* Return true if the line is all zeros */
52static int allZero(unsigned char *aLine){
53 int i;
54 for(i=0; i<16 && aLine[i]==0; i++){}
55 return i==16;
56}
57
58int 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; i<argc; i++){
81 if( argv[i][0]=='-' ){
82 const char *z = argv[i];
83 z++;
84 if( z[0]=='-' ) z++;
85 if( strcmp(z,"pagesize")==0 ){
86 i++;
87 pgsz = atoi(argv[i]);
88 if( pgsz<512 || pgsz>65536 || (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; i<szFile && aData[i]!=0; i++){}
148 if( i==szFile ){
149 fprintf(stderr, "No zero terminator on SQL script\n");
150 exit(1);
151 }
152 nSQL = i+1;
153 if( szFile - nSQL<100 && !bRaw ){
154 fprintf(stderr, "Less than 100 bytes in the database\n");
155 exit(1);
156 }
157 }else{
158 nSQL = 0;
159 }
160 aHdr = aData + nSQL;
161 if( pgsz==0 ){
162 if( bRaw ){
163 pgsz = 1024;
164 }else{
165 pgsz = (aHdr[16]<<8) | aHdr[17];
166 if( pgsz==1 ) pgsz = 65536;
167 if( pgsz<512 || (pgsz&(pgsz-1))!=0 ){
168 fprintf(stderr, "Invalid page size in header: %d\n", pgsz);
169 exit(1);
170 }
171 }
172 }
173 zBaseName = zInputFile;
174 for(i=0; zInputFile[i]; i++){
175 if( zInputFile[i]=='/' && zInputFile[i+1]!=0 ) zBaseName = zInputFile+i+1;
176 }
177 if( forCli ){
178 printf(".open --hexdb\n");
179 }
180 printf("| size %d pagesize %d filename %s\n",(int)szFile,pgsz,zBaseName);
181 for(i=nSQL; i<szFile; i+=16){
182 aLine = aData+i;
183 if( allZero(aLine) ) continue;
184 iPage = i/pgsz + 1;
185 if( lastPage!=iPage ){
186 printf("| page %d offset %d\n", iPage, (iPage-1)*pgsz);
187 lastPage = iPage;
188 }
189 printf("| %5d:", i-(iPage-1)*pgsz);
190 for(j=0; j<16; j++) printf(" %02x", aLine[j]);
191 printf(" ");
192 for(j=0; j<16; j++){
193 unsigned char c = (unsigned char)aLine[j];
194 fputc( bShow[c], stdout);
195 }
196 fputc('\n', stdout);
197 }
198 printf("| end %s\n", zBaseName);
199 if( nSQL>0 ){
200 printf("%s\n", aData);
201 }
202 free( aData );
203 return 0;
204}