2 ** Try to enlarge an SQLite database by appending many unused pages.
3 ** The resulting database will fail PRAGMA integrity_check due to the
4 ** appended unused pages, but it should work otherwise.
8 ** enlargedb DATABASE N
10 ** Adds N blank pages onto the end of DATABASE. N can be decimal
11 ** or hex. The total number of pages after adding must be no greater
18 int main(int argc
, char **argv
){
20 long long int toAppend
;
21 long long int currentSz
;
27 unsigned char buf
[100];
29 if( argc
!=3 ) goto usage_error
;
30 toAppend
= strtoll(argv
[2], &zEnd
, 0);
31 if( zEnd
==argv
[2] || zEnd
[0] ) goto usage_error
;
33 fprintf(stderr
, "N must be at least 1\n");
36 f
= fopen(argv
[1], "r+b");
38 fprintf(stderr
, "cannot open \"%s\" for reading and writing\n", argv
[1]);
41 got
= fread(buf
, 1, sizeof(buf
), f
);
42 if( got
!=sizeof(buf
) ) goto not_valid_db
;
43 if( strcmp((char*)buf
,"SQLite format 3")!=0 ) goto not_valid_db
;
44 pgsz
= (buf
[16]<<8) + buf
[17];
45 if( pgsz
==1 ) pgsz
= 65536;
46 if( pgsz
<512 || pgsz
>65536 || (pgsz
&(pgsz
-1))!=0 ) goto not_valid_db
;
47 currentSz
= (buf
[28]<<24) + (buf
[29]<<16) + (buf
[30]<<8) + buf
[31];
48 newSz
= currentSz
+ toAppend
;
49 if( newSz
> 0xffffffff ) newSz
= 0xffffffff;
50 buf
[28] = (newSz
>>24) & 0xff;
51 buf
[29] = (newSz
>>16) & 0xff;
52 buf
[30] = (newSz
>>8) & 0xff;
53 buf
[31] = newSz
& 0xff;
54 fseek(f
, 28, SEEK_SET
);
55 fwrite(&buf
[28],4,1,f
);
56 fseek(f
, (long)(newSz
*pgsz
- 1), SEEK_SET
);
62 fprintf(stderr
,"not a valid database: %s\n", argv
[1]);
66 fprintf(stderr
,"Usage: %s DATABASE N\n", argv
[0]);