Skip to content

Commit ba63675

Browse files
committed
reverse SQLite check-in 5a877221
1 parent b8e6e27 commit ba63675

File tree

21 files changed

+601
-58
lines changed

21 files changed

+601
-58
lines changed

src/attach.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,42 @@ static void attachFunc(
187187
if( rc==SQLITE_OK && pNew->zDbSName==0 ){
188188
rc = SQLITE_NOMEM_BKPT;
189189
}
190+
191+
192+
#ifdef SQLITE_HAS_CODEC
193+
if( rc==SQLITE_OK ){
194+
extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
195+
extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
196+
int nKey;
197+
char *zKey;
198+
int t = sqlite3_value_type(argv[2]);
199+
switch( t ){
200+
case SQLITE_INTEGER:
201+
case SQLITE_FLOAT:
202+
zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
203+
rc = SQLITE_ERROR;
204+
break;
205+
206+
case SQLITE_TEXT:
207+
case SQLITE_BLOB:
208+
nKey = sqlite3_value_bytes(argv[2]);
209+
zKey = (char *)sqlite3_value_blob(argv[2]);
210+
rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
211+
break;
212+
213+
case SQLITE_NULL:
214+
/* No key specified. Use the key from URI filename, or if none,
215+
** use the key from the main database. */
216+
if( sqlite3CodecQueryParameters(db, zName, zPath)==0 ){
217+
sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
218+
if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
219+
rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
220+
}
221+
}
222+
break;
223+
}
224+
}
225+
#endif
190226
sqlite3_free( zPath );
191227

192228
/* If the file was opened successfully, read the schema for the new database.

src/backup.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ static int backupOnePage(
256256
int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
257257
const int nCopy = MIN(nSrcPgsz, nDestPgsz);
258258
const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
259+
#ifdef SQLITE_HAS_CODEC
260+
/* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
261+
** guaranteed that the shared-mutex is held by this thread, handle
262+
** p->pSrc may not actually be the owner. */
263+
int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
264+
int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest);
265+
#endif
259266
int rc = SQLITE_OK;
260267
i64 iOff;
261268

@@ -272,6 +279,26 @@ static int backupOnePage(
272279
rc = SQLITE_READONLY;
273280
}
274281

282+
#ifdef SQLITE_HAS_CODEC
283+
/* Backup is not possible if the page size of the destination is changing
284+
** and a codec is in use.
285+
*/
286+
if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
287+
rc = SQLITE_READONLY;
288+
}
289+
290+
/* Backup is not possible if the number of bytes of reserve space differ
291+
** between source and destination. If there is a difference, try to
292+
** fix the destination to agree with the source. If that is not possible,
293+
** then the backup cannot proceed.
294+
*/
295+
if( nSrcReserve!=nDestReserve ){
296+
u32 newPgsz = nSrcPgsz;
297+
rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
298+
if( rc==SQLITE_OK && newPgsz!=(u32)nSrcPgsz ) rc = SQLITE_READONLY;
299+
}
300+
#endif
301+
275302
/* This loop runs once for each destination page spanned by the source
276303
** page. For each iteration, variable iOff is set to the byte offset
277304
** of the destination page.
@@ -767,6 +794,10 @@ int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
767794
b.pDest = pTo;
768795
b.iNext = 1;
769796

797+
#ifdef SQLITE_HAS_CODEC
798+
sqlite3PagerAlignReserve(sqlite3BtreePager(pTo), sqlite3BtreePager(pFrom));
799+
#endif
800+
770801
/* 0x7FFFFFFF is the hard limit for the number of pages in a database
771802
** file. By passing this as the number of pages to copy to
772803
** sqlite3_backup_step(), we can guarantee that the copy finishes

src/btree.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,6 +2859,9 @@ int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
28592859
BtShared *pBt = p->pBt;
28602860
assert( nReserve>=-1 && nReserve<=255 );
28612861
sqlite3BtreeEnter(p);
2862+
#if SQLITE_HAS_CODEC
2863+
if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve;
2864+
#endif
28622865
if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
28632866
sqlite3BtreeLeave(p);
28642867
return SQLITE_READONLY;
@@ -2919,6 +2922,9 @@ int sqlite3BtreeGetOptimalReserve(Btree *p){
29192922
int n;
29202923
sqlite3BtreeEnter(p);
29212924
n = sqlite3BtreeGetReserveNoMutex(p);
2925+
#ifdef SQLITE_HAS_CODEC
2926+
if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve;
2927+
#endif
29222928
sqlite3BtreeLeave(p);
29232929
return n;
29242930
}

src/btreeInt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ struct BtShared {
417417
#endif
418418
u8 inTransaction; /* Transaction state */
419419
u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
420+
#ifdef SQLITE_HAS_CODEC
421+
u8 optimalReserve; /* Desired amount of reserved space per page */
422+
#endif
420423
u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
421424
u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
422425
u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */

src/ctime.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ static const char * const sqlite3azCompileOpt[] = {
355355
#if SQLITE_FTS5_NO_WITHOUT_ROWID
356356
"FTS5_NO_WITHOUT_ROWID",
357357
#endif
358+
#if SQLITE_HAS_CODEC
359+
"HAS_CODEC",
360+
#endif
358361
#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
359362
"HAVE_ISNAN",
360363
#endif

src/global.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,16 @@ const unsigned char sqlite3CtypeMap[256] = {
135135
** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
136136
** disabled. The default value may be changed by compiling with the
137137
** SQLITE_USE_URI symbol defined.
138+
**
139+
** URI filenames are enabled by default if SQLITE_HAS_CODEC is
140+
** enabled.
138141
*/
139142
#ifndef SQLITE_USE_URI
140-
# define SQLITE_USE_URI 0
143+
# ifdef SQLITE_HAS_CODEC
144+
# define SQLITE_USE_URI 1
145+
# else
146+
# define SQLITE_USE_URI 0
147+
# endif
141148
#endif
142149

143150
/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the

src/main.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,6 +3007,41 @@ static const char *uriParameter(const char *zFilename, const char *zParam){
30073007
return 0;
30083008
}
30093009

3010+
#if defined(SQLITE_HAS_CODEC)
3011+
/*
3012+
** Process URI filename query parameters relevant to the SQLite Encryption
3013+
** Extension. Return true if any of the relevant query parameters are
3014+
** seen and return false if not.
3015+
*/
3016+
int sqlite3CodecQueryParameters(
3017+
sqlite3 *db, /* Database connection */
3018+
const char *zDb, /* Which schema is being created/attached */
3019+
const char *zUri /* URI filename */
3020+
){
3021+
const char *zKey;
3022+
if( zUri==0 ){
3023+
return 0;
3024+
}else if( (zKey = uriParameter(zUri, "hexkey"))!=0 && zKey[0] ){
3025+
u8 iByte;
3026+
int i;
3027+
char zDecoded[40];
3028+
for(i=0, iByte=0; i<sizeof(zDecoded)*2 && sqlite3Isxdigit(zKey[i]); i++){
3029+
iByte = (iByte<<4) + sqlite3HexToInt(zKey[i]);
3030+
if( (i&1)!=0 ) zDecoded[i/2] = iByte;
3031+
}
3032+
sqlite3_key_v2(db, zDb, zDecoded, i/2);
3033+
return 1;
3034+
}else if( (zKey = uriParameter(zUri, "key"))!=0 ){
3035+
sqlite3_key_v2(db, zDb, zKey, sqlite3Strlen30(zKey));
3036+
return 1;
3037+
}else if( (zKey = uriParameter(zUri, "textkey"))!=0 ){
3038+
sqlite3_key_v2(db, zDb, zKey, -1);
3039+
return 1;
3040+
}else{
3041+
return 0;
3042+
}
3043+
}
3044+
#endif
30103045

30113046

30123047
/*
@@ -3397,6 +3432,9 @@ static int openDatabase(
33973432
void *pArg = sqlite3GlobalConfig.pSqllogArg;
33983433
sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
33993434
}
3435+
#endif
3436+
#if defined(SQLITE_HAS_CODEC)
3437+
if( rc==SQLITE_OK ) sqlite3CodecQueryParameters(db, 0, zOpen);
34003438
#endif
34013439
sqlite3_free(zOpen);
34023440
return rc & 0xff;

0 commit comments

Comments
 (0)