better-sqlite3-multiple-ciphers 7.6.2 → 7.6.3-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@
3
3
  ** Purpose: Amalgamation of the SQLite3 Multiple Ciphers encryption extension for SQLite
4
4
  ** Author: Ulrich Telle
5
5
  ** Created: 2020-02-28
6
- ** Copyright: (c) 2006-2021 Ulrich Telle
6
+ ** Copyright: (c) 2006-2022 Ulrich Telle
7
7
  ** License: MIT
8
8
  */
9
9
 
@@ -92,7 +92,7 @@ extern SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
92
92
  /*** Begin of #include "sqlite3patched.c" ***/
93
93
  /******************************************************************************
94
94
  ** This file is an amalgamation of many separate C source files from SQLite
95
- ** version 3.39.1. By combining all the individual C code files into this
95
+ ** version 3.39.4. By combining all the individual C code files into this
96
96
  ** single large file, the entire code can be compiled as a single translation
97
97
  ** unit. This allows many compilers to do optimizations that would not be
98
98
  ** possible if the files were compiled separately. Performance improvements
@@ -544,9 +544,9 @@ extern "C" {
544
544
  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
545
545
  ** [sqlite_version()] and [sqlite_source_id()].
546
546
  */
547
- #define SQLITE_VERSION "3.39.1"
548
- #define SQLITE_VERSION_NUMBER 3039001
549
- #define SQLITE_SOURCE_ID "2022-07-13 19:41:41 7c16541a0efb3985578181171c9f2bb3fdc4bad6a2ec85c6e31ab96f3eff201f"
547
+ #define SQLITE_VERSION "3.39.4"
548
+ #define SQLITE_VERSION_NUMBER 3039004
549
+ #define SQLITE_SOURCE_ID "2022-09-29 15:55:41 a29f9949895322123f7c38fbe94c649a9d6e6c9cd0c3b41c96d694552f26b309"
550
550
 
551
551
  /*
552
552
  ** CAPI3REF: Run-Time Library Version Numbers
@@ -13243,6 +13243,11 @@ struct fts5_api {
13243
13243
  /************** End of sqlite3.h *********************************************/
13244
13244
  /************** Continuing where we left off in sqliteInt.h ******************/
13245
13245
 
13246
+ /*
13247
+ ** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory.
13248
+ */
13249
+ #define SQLITE_MUTEX_STATIC_TEMPDIR SQLITE_MUTEX_STATIC_VFS1
13250
+
13246
13251
  /*
13247
13252
  ** Include the configuration header output by 'configure' if we're using the
13248
13253
  ** autoconf-based build
@@ -29662,8 +29667,13 @@ SQLITE_PRIVATE void *sqlite3OomFault(sqlite3 *db){
29662
29667
  }
29663
29668
  DisableLookaside;
29664
29669
  if( db->pParse ){
29670
+ Parse *pParse;
29665
29671
  sqlite3ErrorMsg(db->pParse, "out of memory");
29666
29672
  db->pParse->rc = SQLITE_NOMEM_BKPT;
29673
+ for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
29674
+ pParse->nErr++;
29675
+ pParse->rc = SQLITE_NOMEM;
29676
+ }
29667
29677
  }
29668
29678
  }
29669
29679
  return 0;
@@ -30529,8 +30539,8 @@ SQLITE_API void sqlite3_str_vappendf(
30529
30539
  case etSQLESCAPE: /* %q: Escape ' characters */
30530
30540
  case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
30531
30541
  case etSQLESCAPE3: { /* %w: Escape " characters */
30532
- int i, j, k, n, isnull;
30533
- int needQuote;
30542
+ i64 i, j, k, n;
30543
+ int needQuote, isnull;
30534
30544
  char ch;
30535
30545
  char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
30536
30546
  char *escarg;
@@ -31218,8 +31228,8 @@ SQLITE_PRIVATE void sqlite3TreeViewColumnList(
31218
31228
  sqlite3TreeViewLine(pView, "COLUMNS");
31219
31229
  for(i=0; i<nCol; i++){
31220
31230
  u16 flg = aCol[i].colFlags;
31221
- int moreToFollow = i<(nCol - 1);
31222
- sqlite3TreeViewPush(&pView, moreToFollow);
31231
+ int colMoreToFollow = i<(nCol - 1);
31232
+ sqlite3TreeViewPush(&pView, colMoreToFollow);
31223
31233
  sqlite3TreeViewLine(pView, 0);
31224
31234
  printf(" %s", aCol[i].zCnName);
31225
31235
  switch( aCol[i].eCType ){
@@ -31350,7 +31360,7 @@ SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc)
31350
31360
  Table *pTab = pItem->pTab;
31351
31361
  sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
31352
31362
  }
31353
- assert( pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
31363
+ assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
31354
31364
  sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
31355
31365
  }
31356
31366
  if( pItem->fg.isTabFunc ){
@@ -33558,7 +33568,7 @@ SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
33558
33568
  va_list ap;
33559
33569
  sqlite3 *db = pParse->db;
33560
33570
  assert( db!=0 );
33561
- assert( db->pParse==pParse );
33571
+ assert( db->pParse==pParse || db->pParse->pToplevel==pParse );
33562
33572
  db->errByteOffset = -2;
33563
33573
  va_start(ap, zFormat);
33564
33574
  zMsg = sqlite3VMPrintf(db, zFormat, ap);
@@ -41419,6 +41429,7 @@ static const char *unixTempFileDir(void){
41419
41429
  static int unixGetTempname(int nBuf, char *zBuf){
41420
41430
  const char *zDir;
41421
41431
  int iLimit = 0;
41432
+ int rc = SQLITE_OK;
41422
41433
 
41423
41434
  /* It's odd to simulate an io-error here, but really this is just
41424
41435
  ** using the io-error infrastructure to test that SQLite handles this
@@ -41427,18 +41438,26 @@ static int unixGetTempname(int nBuf, char *zBuf){
41427
41438
  zBuf[0] = 0;
41428
41439
  SimulateIOError( return SQLITE_IOERR );
41429
41440
 
41441
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
41430
41442
  zDir = unixTempFileDir();
41431
- if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
41432
- do{
41433
- u64 r;
41434
- sqlite3_randomness(sizeof(r), &r);
41435
- assert( nBuf>2 );
41436
- zBuf[nBuf-2] = 0;
41437
- sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
41438
- zDir, r, 0);
41439
- if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
41440
- }while( osAccess(zBuf,0)==0 );
41441
- return SQLITE_OK;
41443
+ if( zDir==0 ){
41444
+ rc = SQLITE_IOERR_GETTEMPPATH;
41445
+ }else{
41446
+ do{
41447
+ u64 r;
41448
+ sqlite3_randomness(sizeof(r), &r);
41449
+ assert( nBuf>2 );
41450
+ zBuf[nBuf-2] = 0;
41451
+ sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
41452
+ zDir, r, 0);
41453
+ if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
41454
+ rc = SQLITE_ERROR;
41455
+ break;
41456
+ }
41457
+ }while( osAccess(zBuf,0)==0 );
41458
+ }
41459
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
41460
+ return rc;
41442
41461
  }
41443
41462
 
41444
41463
  #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
@@ -45577,10 +45596,12 @@ SQLITE_API int sqlite3_win32_set_directory8(
45577
45596
  const char *zValue /* New value for directory being set or reset */
45578
45597
  ){
45579
45598
  char **ppDirectory = 0;
45599
+ int rc;
45580
45600
  #ifndef SQLITE_OMIT_AUTOINIT
45581
- int rc = sqlite3_initialize();
45601
+ rc = sqlite3_initialize();
45582
45602
  if( rc ) return rc;
45583
45603
  #endif
45604
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
45584
45605
  if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
45585
45606
  ppDirectory = &sqlite3_data_directory;
45586
45607
  }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
@@ -45595,14 +45616,19 @@ SQLITE_API int sqlite3_win32_set_directory8(
45595
45616
  if( zValue && zValue[0] ){
45596
45617
  zCopy = sqlite3_mprintf("%s", zValue);
45597
45618
  if ( zCopy==0 ){
45598
- return SQLITE_NOMEM_BKPT;
45619
+ rc = SQLITE_NOMEM_BKPT;
45620
+ goto set_directory8_done;
45599
45621
  }
45600
45622
  }
45601
45623
  sqlite3_free(*ppDirectory);
45602
45624
  *ppDirectory = zCopy;
45603
- return SQLITE_OK;
45625
+ rc = SQLITE_OK;
45626
+ }else{
45627
+ rc = SQLITE_ERROR;
45604
45628
  }
45605
- return SQLITE_ERROR;
45629
+ set_directory8_done:
45630
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
45631
+ return rc;
45606
45632
  }
45607
45633
 
45608
45634
  /*
@@ -48376,6 +48402,18 @@ static int winMakeEndInDirSep(int nBuf, char *zBuf){
48376
48402
  return 0;
48377
48403
  }
48378
48404
 
48405
+ /*
48406
+ ** If sqlite3_temp_directory is not, take the mutex and return true.
48407
+ **
48408
+ ** If sqlite3_temp_directory is NULL, omit the mutex and return false.
48409
+ */
48410
+ static int winTempDirDefined(void){
48411
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
48412
+ if( sqlite3_temp_directory!=0 ) return 1;
48413
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
48414
+ return 0;
48415
+ }
48416
+
48379
48417
  /*
48380
48418
  ** Create a temporary file name and store the resulting pointer into pzBuf.
48381
48419
  ** The pointer returned in pzBuf must be freed via sqlite3_free().
@@ -48412,20 +48450,23 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
48412
48450
  */
48413
48451
  nDir = nMax - (nPre + 15);
48414
48452
  assert( nDir>0 );
48415
- if( sqlite3_temp_directory ){
48453
+ if( winTempDirDefined() ){
48416
48454
  int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
48417
48455
  if( nDirLen>0 ){
48418
48456
  if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
48419
48457
  nDirLen++;
48420
48458
  }
48421
48459
  if( nDirLen>nDir ){
48460
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
48422
48461
  sqlite3_free(zBuf);
48423
48462
  OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48424
48463
  return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
48425
48464
  }
48426
48465
  sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
48427
48466
  }
48467
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
48428
48468
  }
48469
+
48429
48470
  #if defined(__CYGWIN__)
48430
48471
  else{
48431
48472
  static const char *azDirs[] = {
@@ -49214,7 +49255,7 @@ static BOOL winIsVerbatimPathname(
49214
49255
  ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
49215
49256
  ** bytes in size.
49216
49257
  */
49217
- static int winFullPathname(
49258
+ static int winFullPathnameNoMutex(
49218
49259
  sqlite3_vfs *pVfs, /* Pointer to vfs object */
49219
49260
  const char *zRelative, /* Possibly relative input path */
49220
49261
  int nFull, /* Size of output buffer in bytes */
@@ -49393,6 +49434,19 @@ static int winFullPathname(
49393
49434
  }
49394
49435
  #endif
49395
49436
  }
49437
+ static int winFullPathname(
49438
+ sqlite3_vfs *pVfs, /* Pointer to vfs object */
49439
+ const char *zRelative, /* Possibly relative input path */
49440
+ int nFull, /* Size of output buffer in bytes */
49441
+ char *zFull /* Output buffer */
49442
+ ){
49443
+ int rc;
49444
+ sqlite3_mutex *pMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR);
49445
+ sqlite3_mutex_enter(pMutex);
49446
+ rc = winFullPathnameNoMutex(pVfs, zRelative, nFull, zFull);
49447
+ sqlite3_mutex_leave(pMutex);
49448
+ return rc;
49449
+ }
49396
49450
 
49397
49451
  #ifndef SQLITE_OMIT_LOAD_EXTENSION
49398
49452
  /*
@@ -51737,14 +51791,24 @@ SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
51737
51791
  */
51738
51792
  SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
51739
51793
  PCache *pCache = p->pCache;
51794
+ sqlite3_pcache_page *pOther;
51740
51795
  assert( p->nRef>0 );
51741
51796
  assert( newPgno>0 );
51742
51797
  assert( sqlite3PcachePageSanity(p) );
51743
51798
  pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
51799
+ pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
51800
+ if( pOther ){
51801
+ PgHdr *pXPage = (PgHdr*)pOther->pExtra;
51802
+ assert( pXPage->nRef==0 );
51803
+ pXPage->nRef++;
51804
+ pCache->nRefSum++;
51805
+ sqlite3PcacheDrop(pXPage);
51806
+ }
51744
51807
  sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
51745
51808
  p->pgno = newPgno;
51746
51809
  if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
51747
51810
  pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
51811
+ assert( sqlite3PcachePageSanity(p) );
51748
51812
  }
51749
51813
  }
51750
51814
 
@@ -53126,23 +53190,26 @@ static void pcache1Rekey(
53126
53190
  PCache1 *pCache = (PCache1 *)p;
53127
53191
  PgHdr1 *pPage = (PgHdr1 *)pPg;
53128
53192
  PgHdr1 **pp;
53129
- unsigned int h;
53193
+ unsigned int hOld, hNew;
53130
53194
  assert( pPage->iKey==iOld );
53131
53195
  assert( pPage->pCache==pCache );
53196
+ assert( iOld!=iNew ); /* The page number really is changing */
53132
53197
 
53133
53198
  pcache1EnterMutex(pCache->pGroup);
53134
53199
 
53135
- h = iOld%pCache->nHash;
53136
- pp = &pCache->apHash[h];
53200
+ assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */
53201
+ hOld = iOld%pCache->nHash;
53202
+ pp = &pCache->apHash[hOld];
53137
53203
  while( (*pp)!=pPage ){
53138
53204
  pp = &(*pp)->pNext;
53139
53205
  }
53140
53206
  *pp = pPage->pNext;
53141
53207
 
53142
- h = iNew%pCache->nHash;
53208
+ assert( pcache1FetchNoMutex(p, iNew, 0)==0 ); /* iNew not in cache */
53209
+ hNew = iNew%pCache->nHash;
53143
53210
  pPage->iKey = iNew;
53144
- pPage->pNext = pCache->apHash[h];
53145
- pCache->apHash[h] = pPage;
53211
+ pPage->pNext = pCache->apHash[hNew];
53212
+ pCache->apHash[hNew] = pPage;
53146
53213
  if( iNew>pCache->iMaxKey ){
53147
53214
  pCache->iMaxKey = iNew;
53148
53215
  }
@@ -59775,6 +59842,7 @@ static int pager_open_journal(Pager *pPager){
59775
59842
  if( rc!=SQLITE_OK ){
59776
59843
  sqlite3BitvecDestroy(pPager->pInJournal);
59777
59844
  pPager->pInJournal = 0;
59845
+ pPager->journalOff = 0;
59778
59846
  }else{
59779
59847
  assert( pPager->eState==PAGER_WRITER_LOCKED );
59780
59848
  pPager->eState = PAGER_WRITER_CACHEMOD;
@@ -61330,7 +61398,7 @@ SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
61330
61398
  SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
61331
61399
  assert( assert_pager_state(pPager) );
61332
61400
  if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
61333
- if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
61401
+ if( isOpen(pPager->jfd) && pPager->journalOff>0 ) return 0;
61334
61402
  return 1;
61335
61403
  }
61336
61404
 
@@ -68445,7 +68513,7 @@ static int defragmentPage(MemPage *pPage, int nMaxFrag){
68445
68513
  if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
68446
68514
  memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
68447
68515
  sz += sz2;
68448
- }else if( NEVER(iFree+sz>usableSize) ){
68516
+ }else if( iFree+sz>usableSize ){
68449
68517
  return SQLITE_CORRUPT_PAGE(pPage);
68450
68518
  }
68451
68519
 
@@ -74801,8 +74869,6 @@ static int balance_nonroot(
74801
74869
  Pgno pgno; /* Temp var to store a page number in */
74802
74870
  u8 abDone[NB+2]; /* True after i'th new page is populated */
74803
74871
  Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
74804
- Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
74805
- u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
74806
74872
  CellArray b; /* Parsed information on cells being balanced */
74807
74873
 
74808
74874
  memset(abDone, 0, sizeof(abDone));
@@ -75226,42 +75292,39 @@ static int balance_nonroot(
75226
75292
  ** of the table is closer to a linear scan through the file. That in turn
75227
75293
  ** helps the operating system to deliver pages from the disk more rapidly.
75228
75294
  **
75229
- ** An O(n^2) insertion sort algorithm is used, but since n is never more
75230
- ** than (NB+2) (a small constant), that should not be a problem.
75295
+ ** An O(N*N) sort algorithm is used, but since N is never more than NB+2
75296
+ ** (5), that is not a performance concern.
75231
75297
  **
75232
75298
  ** When NB==3, this one optimization makes the database about 25% faster
75233
75299
  ** for large insertions and deletions.
75234
75300
  */
75235
75301
  for(i=0; i<nNew; i++){
75236
- aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
75237
- aPgFlags[i] = apNew[i]->pDbPage->flags;
75238
- for(j=0; j<i; j++){
75239
- if( NEVER(aPgno[j]==aPgno[i]) ){
75240
- /* This branch is taken if the set of sibling pages somehow contains
75241
- ** duplicate entries. This can happen if the database is corrupt.
75242
- ** It would be simpler to detect this as part of the loop below, but
75243
- ** we do the detection here in order to avoid populating the pager
75244
- ** cache with two separate objects associated with the same
75245
- ** page number. */
75246
- assert( CORRUPT_DB );
75247
- rc = SQLITE_CORRUPT_BKPT;
75248
- goto balance_cleanup;
75249
- }
75250
- }
75302
+ aPgno[i] = apNew[i]->pgno;
75303
+ assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
75304
+ assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
75251
75305
  }
75252
- for(i=0; i<nNew; i++){
75253
- int iBest = 0; /* aPgno[] index of page number to use */
75254
- for(j=1; j<nNew; j++){
75255
- if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
75306
+ for(i=0; i<nNew-1; i++){
75307
+ int iB = i;
75308
+ for(j=i+1; j<nNew; j++){
75309
+ if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
75256
75310
  }
75257
- pgno = aPgOrder[iBest];
75258
- aPgOrder[iBest] = 0xffffffff;
75259
- if( iBest!=i ){
75260
- if( iBest>i ){
75261
- sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
75262
- }
75263
- sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
75264
- apNew[i]->pgno = pgno;
75311
+
75312
+ /* If apNew[i] has a page number that is bigger than any of the
75313
+ ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent
75314
+ ** entry that has the smallest page number (which we know to be
75315
+ ** entry apNew[iB]).
75316
+ */
75317
+ if( iB!=i ){
75318
+ Pgno pgnoA = apNew[i]->pgno;
75319
+ Pgno pgnoB = apNew[iB]->pgno;
75320
+ Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
75321
+ u16 fgA = apNew[i]->pDbPage->flags;
75322
+ u16 fgB = apNew[iB]->pDbPage->flags;
75323
+ sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
75324
+ sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
75325
+ sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
75326
+ apNew[i]->pgno = pgnoB;
75327
+ apNew[iB]->pgno = pgnoA;
75265
75328
  }
75266
75329
  }
75267
75330
 
@@ -81134,6 +81197,7 @@ SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(
81134
81197
  addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc : OP_Function,
81135
81198
  p1, p2, p3, (char*)pCtx, P4_FUNCCTX);
81136
81199
  sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef);
81200
+ sqlite3MayAbort(pParse);
81137
81201
  return addr;
81138
81202
  }
81139
81203
 
@@ -81469,6 +81533,7 @@ SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
81469
81533
  || opcode==OP_VDestroy
81470
81534
  || opcode==OP_VCreate
81471
81535
  || opcode==OP_ParseSchema
81536
+ || opcode==OP_Function || opcode==OP_PureFunc
81472
81537
  || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
81473
81538
  && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
81474
81539
  ){
@@ -101639,33 +101704,23 @@ static void resolveAlias(
101639
101704
  sqlite3ExprDelete(db, pDup);
101640
101705
  pDup = 0;
101641
101706
  }else{
101707
+ Expr temp;
101642
101708
  incrAggFunctionDepth(pDup, nSubquery);
101643
101709
  if( pExpr->op==TK_COLLATE ){
101644
101710
  assert( !ExprHasProperty(pExpr, EP_IntValue) );
101645
101711
  pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
101646
101712
  }
101647
-
101648
- /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
101649
- ** prevents ExprDelete() from deleting the Expr structure itself,
101650
- ** allowing it to be repopulated by the memcpy() on the following line.
101651
- ** The pExpr->u.zToken might point into memory that will be freed by the
101652
- ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
101653
- ** make a copy of the token before doing the sqlite3DbFree().
101654
- */
101655
- ExprSetProperty(pExpr, EP_Static);
101656
- sqlite3ExprDelete(db, pExpr);
101657
- memcpy(pExpr, pDup, sizeof(*pExpr));
101658
- if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
101659
- assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
101660
- pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
101661
- pExpr->flags |= EP_MemToken;
101662
- }
101713
+ memcpy(&temp, pDup, sizeof(Expr));
101714
+ memcpy(pDup, pExpr, sizeof(Expr));
101715
+ memcpy(pExpr, &temp, sizeof(Expr));
101663
101716
  if( ExprHasProperty(pExpr, EP_WinFunc) ){
101664
101717
  if( ALWAYS(pExpr->y.pWin!=0) ){
101665
101718
  pExpr->y.pWin->pOwner = pExpr;
101666
101719
  }
101667
101720
  }
101668
- sqlite3DbFree(db, pDup);
101721
+ sqlite3ParserAddCleanup(pParse,
101722
+ (void(*)(sqlite3*,void*))sqlite3ExprDelete,
101723
+ pDup);
101669
101724
  }
101670
101725
  }
101671
101726
 
@@ -101868,7 +101923,7 @@ static int lookupName(
101868
101923
  pTab = pItem->pTab;
101869
101924
  assert( pTab!=0 && pTab->zName!=0 );
101870
101925
  assert( pTab->nCol>0 || pParse->nErr );
101871
- assert( pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
101926
+ assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
101872
101927
  if( pItem->fg.isNestedFrom ){
101873
101928
  /* In this case, pItem is a subquery that has been formed from a
101874
101929
  ** parenthesized subset of the FROM clause terms. Example:
@@ -132818,6 +132873,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132818
132873
  **
132819
132874
  */
132820
132875
  case PragTyp_TEMP_STORE_DIRECTORY: {
132876
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132821
132877
  if( !zRight ){
132822
132878
  returnSingleText(v, sqlite3_temp_directory);
132823
132879
  }else{
@@ -132827,6 +132883,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132827
132883
  rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132828
132884
  if( rc!=SQLITE_OK || res==0 ){
132829
132885
  sqlite3ErrorMsg(pParse, "not a writable directory");
132886
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132830
132887
  goto pragma_out;
132831
132888
  }
132832
132889
  }
@@ -132844,6 +132901,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132844
132901
  }
132845
132902
  #endif /* SQLITE_OMIT_WSD */
132846
132903
  }
132904
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132847
132905
  break;
132848
132906
  }
132849
132907
 
@@ -132862,6 +132920,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132862
132920
  **
132863
132921
  */
132864
132922
  case PragTyp_DATA_STORE_DIRECTORY: {
132923
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132865
132924
  if( !zRight ){
132866
132925
  returnSingleText(v, sqlite3_data_directory);
132867
132926
  }else{
@@ -132871,6 +132930,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132871
132930
  rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132872
132931
  if( rc!=SQLITE_OK || res==0 ){
132873
132932
  sqlite3ErrorMsg(pParse, "not a writable directory");
132933
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132874
132934
  goto pragma_out;
132875
132935
  }
132876
132936
  }
@@ -132882,6 +132942,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132882
132942
  }
132883
132943
  #endif /* SQLITE_OMIT_WSD */
132884
132944
  }
132945
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132885
132946
  break;
132886
132947
  }
132887
132948
  #endif
@@ -135963,7 +136024,7 @@ SQLITE_PRIVATE int sqlite3ColumnIndex(Table *pTab, const char *zCol){
135963
136024
  */
135964
136025
  SQLITE_PRIVATE void sqlite3SrcItemColumnUsed(SrcItem *pItem, int iCol){
135965
136026
  assert( pItem!=0 );
135966
- assert( pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
136027
+ assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
135967
136028
  if( pItem->fg.isNestedFrom ){
135968
136029
  ExprList *pResults;
135969
136030
  assert( pItem->pSelect!=0 );
@@ -137327,7 +137388,7 @@ static void generateSortTail(
137327
137388
  if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
137328
137389
  addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
137329
137390
  VdbeCoverage(v);
137330
- codeOffset(v, p->iOffset, addrContinue);
137391
+ assert( p->iLimit==0 && p->iOffset==0 );
137331
137392
  sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
137332
137393
  bSeq = 0;
137333
137394
  }else{
@@ -137335,6 +137396,9 @@ static void generateSortTail(
137335
137396
  codeOffset(v, p->iOffset, addrContinue);
137336
137397
  iSortTab = iTab;
137337
137398
  bSeq = 1;
137399
+ if( p->iOffset>0 ){
137400
+ sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
137401
+ }
137338
137402
  }
137339
137403
  for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
137340
137404
  #ifdef SQLITE_ENABLE_SORTER_REFERENCES
@@ -139327,10 +139391,11 @@ static int multiSelectOrderBy(
139327
139391
  */
139328
139392
  sqlite3VdbeResolveLabel(v, labelEnd);
139329
139393
 
139330
- /* Reassembly the compound query so that it will be freed correctly
139394
+ /* Reassemble the compound query so that it will be freed correctly
139331
139395
  ** by the calling function */
139332
139396
  if( pSplit->pPrior ){
139333
- sqlite3SelectDelete(db, pSplit->pPrior);
139397
+ sqlite3ParserAddCleanup(pParse,
139398
+ (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior);
139334
139399
  }
139335
139400
  pSplit->pPrior = pPrior;
139336
139401
  pPrior->pNext = pSplit;
@@ -140849,6 +140914,7 @@ static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
140849
140914
  || p->pSrc->nSrc!=1
140850
140915
  || p->pSrc->a[0].pSelect
140851
140916
  || pAggInfo->nFunc!=1
140917
+ || p->pHaving
140852
140918
  ){
140853
140919
  return 0;
140854
140920
  }
@@ -141550,7 +141616,7 @@ static int selectExpander(Walker *pWalker, Select *p){
141550
141616
  zTabName = pTab->zName;
141551
141617
  }
141552
141618
  if( db->mallocFailed ) break;
141553
- assert( pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) );
141619
+ assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) );
141554
141620
  if( pFrom->fg.isNestedFrom ){
141555
141621
  assert( pFrom->pSelect!=0 );
141556
141622
  pNestedFrom = pFrom->pSelect->pEList;
@@ -142479,7 +142545,9 @@ SQLITE_PRIVATE int sqlite3Select(
142479
142545
  ){
142480
142546
  SELECTTRACE(0x100,pParse,p,
142481
142547
  ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
142482
- sqlite3ExprListDelete(db, pSub->pOrderBy);
142548
+ sqlite3ParserAddCleanup(pParse,
142549
+ (void(*)(sqlite3*,void*))sqlite3ExprListDelete,
142550
+ pSub->pOrderBy);
142483
142551
  pSub->pOrderBy = 0;
142484
142552
  }
142485
142553
 
@@ -144084,6 +144152,23 @@ SQLITE_PRIVATE void sqlite3FinishTrigger(
144084
144152
  Vdbe *v;
144085
144153
  char *z;
144086
144154
 
144155
+ /* If this is a new CREATE TABLE statement, and if shadow tables
144156
+ ** are read-only, and the trigger makes a change to a shadow table,
144157
+ ** then raise an error - do not allow the trigger to be created. */
144158
+ if( sqlite3ReadOnlyShadowTables(db) ){
144159
+ TriggerStep *pStep;
144160
+ for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
144161
+ if( pStep->zTarget!=0
144162
+ && sqlite3ShadowTableName(db, pStep->zTarget)
144163
+ ){
144164
+ sqlite3ErrorMsg(pParse,
144165
+ "trigger \"%s\" may not write to shadow table \"%s\"",
144166
+ pTrig->zName, pStep->zTarget);
144167
+ goto triggerfinish_cleanup;
144168
+ }
144169
+ }
144170
+ }
144171
+
144087
144172
  /* Make an entry in the sqlite_schema table */
144088
144173
  v = sqlite3GetVdbe(pParse);
144089
144174
  if( v==0 ) goto triggerfinish_cleanup;
@@ -149895,7 +149980,8 @@ static int codeEqualityTerm(
149895
149980
  }
149896
149981
  sqlite3ExprDelete(db, pX);
149897
149982
  }else{
149898
- aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
149983
+ int n = sqlite3ExprVectorSize(pX->pLeft);
149984
+ aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*MAX(nEq,n));
149899
149985
  eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap, &iTab);
149900
149986
  }
149901
149987
  pX = pExpr;
@@ -155479,7 +155565,7 @@ static int whereKeyStats(
155479
155565
  #endif
155480
155566
  assert( pRec!=0 );
155481
155567
  assert( pIdx->nSample>0 );
155482
- assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
155568
+ assert( pRec->nField>0 );
155483
155569
 
155484
155570
  /* Do a binary search to find the first sample greater than or equal
155485
155571
  ** to pRec. If pRec contains a single field, the set of samples to search
@@ -155525,7 +155611,7 @@ static int whereKeyStats(
155525
155611
  ** it is extended to two fields. The duplicates that this creates do not
155526
155612
  ** cause any problems.
155527
155613
  */
155528
- nField = pRec->nField;
155614
+ nField = MIN(pRec->nField, pIdx->nSample);
155529
155615
  iCol = 0;
155530
155616
  iSample = pIdx->nSample * nField;
155531
155617
  do{
@@ -158214,6 +158300,7 @@ static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
158214
158300
  sqlite3 *db = pWInfo->pParse->db;
158215
158301
  int rc = SQLITE_OK;
158216
158302
  int bFirstPastRJ = 0;
158303
+ int hasRightJoin = 0;
158217
158304
  WhereLoop *pNew;
158218
158305
 
158219
158306
 
@@ -158234,15 +158321,16 @@ static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
158234
158321
  ** prevents the right operand of a RIGHT JOIN from being swapped with
158235
158322
  ** other elements even further to the right.
158236
158323
  **
158237
- ** The JT_LTORJ term prevents any FROM-clause term reordering for terms
158238
- ** to the left of a RIGHT JOIN. This is conservative. Relaxing this
158239
- ** constraint somewhat to prevent terms from crossing from the right
158240
- ** side of a LEFT JOIN over to the left side when they are on the
158241
- ** left side of a RIGHT JOIN would be sufficient for all known failure
158242
- ** cases. FIX ME: Implement this optimization.
158324
+ ** The JT_LTORJ case and the hasRightJoin flag work together to
158325
+ ** prevent FROM-clause terms from moving from the right side of
158326
+ ** a LEFT JOIN over to the left side of that join if the LEFT JOIN
158327
+ ** is itself on the left side of a RIGHT JOIN.
158243
158328
  */
158329
+ if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
158244
158330
  mPrereq |= mPrior;
158245
158331
  bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
158332
+ }else if( !hasRightJoin ){
158333
+ mPrereq = 0;
158246
158334
  }
158247
158335
  #ifndef SQLITE_OMIT_VIRTUALTABLE
158248
158336
  if( IsVirtual(pItem->pTab) ){
@@ -177051,7 +177139,7 @@ struct Fts3MultiSegReader {
177051
177139
  int nAdvance; /* How many seg-readers to advance */
177052
177140
  Fts3SegFilter *pFilter; /* Pointer to filter object */
177053
177141
  char *aBuffer; /* Buffer to merge doclists in */
177054
- int nBuffer; /* Allocated size of aBuffer[] in bytes */
177142
+ i64 nBuffer; /* Allocated size of aBuffer[] in bytes */
177055
177143
 
177056
177144
  int iColFilter; /* If >=0, filter for this column */
177057
177145
  int bRestart;
@@ -179747,7 +179835,7 @@ static int fts3TermSelectMerge(
179747
179835
  **
179748
179836
  ** Similar padding is added in the fts3DoclistOrMerge() function.
179749
179837
  */
179750
- pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
179838
+ pTS->aaOutput[0] = sqlite3_malloc64((i64)nDoclist + FTS3_VARINT_MAX + 1);
179751
179839
  pTS->anOutput[0] = nDoclist;
179752
179840
  if( pTS->aaOutput[0] ){
179753
179841
  memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
@@ -181167,8 +181255,7 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
181167
181255
  char *aPoslist = 0; /* Position list for deferred tokens */
181168
181256
  int nPoslist = 0; /* Number of bytes in aPoslist */
181169
181257
  int iPrev = -1; /* Token number of previous deferred token */
181170
-
181171
- assert( pPhrase->doclist.bFreeList==0 );
181258
+ char *aFree = (pPhrase->doclist.bFreeList ? pPhrase->doclist.pList : 0);
181172
181259
 
181173
181260
  for(iToken=0; iToken<pPhrase->nToken; iToken++){
181174
181261
  Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
@@ -181182,6 +181269,7 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
181182
181269
 
181183
181270
  if( pList==0 ){
181184
181271
  sqlite3_free(aPoslist);
181272
+ sqlite3_free(aFree);
181185
181273
  pPhrase->doclist.pList = 0;
181186
181274
  pPhrase->doclist.nList = 0;
181187
181275
  return SQLITE_OK;
@@ -181202,6 +181290,7 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
181202
181290
  nPoslist = (int)(aOut - aPoslist);
181203
181291
  if( nPoslist==0 ){
181204
181292
  sqlite3_free(aPoslist);
181293
+ sqlite3_free(aFree);
181205
181294
  pPhrase->doclist.pList = 0;
181206
181295
  pPhrase->doclist.nList = 0;
181207
181296
  return SQLITE_OK;
@@ -181234,7 +181323,7 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
181234
181323
  nDistance = iPrev - nMaxUndeferred;
181235
181324
  }
181236
181325
 
181237
- aOut = (char *)sqlite3_malloc(nPoslist+8);
181326
+ aOut = (char *)sqlite3Fts3MallocZero(nPoslist+FTS3_BUFFER_PADDING);
181238
181327
  if( !aOut ){
181239
181328
  sqlite3_free(aPoslist);
181240
181329
  return SQLITE_NOMEM;
@@ -181254,6 +181343,7 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
181254
181343
  }
181255
181344
  }
181256
181345
 
181346
+ if( pPhrase->doclist.pList!=aFree ) sqlite3_free(aFree);
181257
181347
  return SQLITE_OK;
181258
181348
  }
181259
181349
  #endif /* SQLITE_DISABLE_FTS4_DEFERRED */
@@ -181602,7 +181692,7 @@ static int fts3EvalIncrPhraseNext(
181602
181692
  if( bEof==0 ){
181603
181693
  int nList = 0;
181604
181694
  int nByte = a[p->nToken-1].nList;
181605
- char *aDoclist = sqlite3_malloc(nByte+FTS3_BUFFER_PADDING);
181695
+ char *aDoclist = sqlite3_malloc64((i64)nByte+FTS3_BUFFER_PADDING);
181606
181696
  if( !aDoclist ) return SQLITE_NOMEM;
181607
181697
  memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
181608
181698
  memset(&aDoclist[nByte], 0, FTS3_BUFFER_PADDING);
@@ -182432,7 +182522,6 @@ static int fts3EvalTestExpr(
182432
182522
  pExpr->iDocid==pCsr->iPrevId && pExpr->pPhrase->doclist.pList
182433
182523
  ))){
182434
182524
  Fts3Phrase *pPhrase = pExpr->pPhrase;
182435
- assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
182436
182525
  if( pExpr->bDeferred ){
182437
182526
  fts3EvalInvalidatePoslist(pPhrase);
182438
182527
  }
@@ -185839,7 +185928,7 @@ static int porterNext(
185839
185928
  if( n>c->nAllocated ){
185840
185929
  char *pNew;
185841
185930
  c->nAllocated = n+20;
185842
- pNew = sqlite3_realloc(c->zToken, c->nAllocated);
185931
+ pNew = sqlite3_realloc64(c->zToken, c->nAllocated);
185843
185932
  if( !pNew ) return SQLITE_NOMEM;
185844
185933
  c->zToken = pNew;
185845
185934
  }
@@ -186591,7 +186680,7 @@ static int simpleNext(
186591
186680
  if( n>c->nTokenAllocated ){
186592
186681
  char *pNew;
186593
186682
  c->nTokenAllocated = n+20;
186594
- pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
186683
+ pNew = sqlite3_realloc64(c->pToken, c->nTokenAllocated);
186595
186684
  if( !pNew ) return SQLITE_NOMEM;
186596
186685
  c->pToken = pNew;
186597
186686
  }
@@ -187753,7 +187842,7 @@ static int fts3PendingListAppendVarint(
187753
187842
 
187754
187843
  /* Allocate or grow the PendingList as required. */
187755
187844
  if( !p ){
187756
- p = sqlite3_malloc(sizeof(*p) + 100);
187845
+ p = sqlite3_malloc64(sizeof(*p) + 100);
187757
187846
  if( !p ){
187758
187847
  return SQLITE_NOMEM;
187759
187848
  }
@@ -187762,14 +187851,14 @@ static int fts3PendingListAppendVarint(
187762
187851
  p->nData = 0;
187763
187852
  }
187764
187853
  else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
187765
- int nNew = p->nSpace * 2;
187766
- p = sqlite3_realloc(p, sizeof(*p) + nNew);
187854
+ i64 nNew = p->nSpace * 2;
187855
+ p = sqlite3_realloc64(p, sizeof(*p) + nNew);
187767
187856
  if( !p ){
187768
187857
  sqlite3_free(*pp);
187769
187858
  *pp = 0;
187770
187859
  return SQLITE_NOMEM;
187771
187860
  }
187772
- p->nSpace = nNew;
187861
+ p->nSpace = (int)nNew;
187773
187862
  p->aData = (char *)&p[1];
187774
187863
  }
187775
187864
 
@@ -188326,7 +188415,7 @@ SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
188326
188415
  int nByte = sqlite3_blob_bytes(p->pSegments);
188327
188416
  *pnBlob = nByte;
188328
188417
  if( paBlob ){
188329
- char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
188418
+ char *aByte = sqlite3_malloc64((i64)nByte + FTS3_NODE_PADDING);
188330
188419
  if( !aByte ){
188331
188420
  rc = SQLITE_NOMEM;
188332
188421
  }else{
@@ -188443,7 +188532,7 @@ static int fts3SegReaderNext(
188443
188532
  int nTerm = fts3HashKeysize(pElem);
188444
188533
  if( (nTerm+1)>pReader->nTermAlloc ){
188445
188534
  sqlite3_free(pReader->zTerm);
188446
- pReader->zTerm = (char*)sqlite3_malloc((nTerm+1)*2);
188535
+ pReader->zTerm = (char*)sqlite3_malloc64(((i64)nTerm+1)*2);
188447
188536
  if( !pReader->zTerm ) return SQLITE_NOMEM;
188448
188537
  pReader->nTermAlloc = (nTerm+1)*2;
188449
188538
  }
@@ -188451,7 +188540,7 @@ static int fts3SegReaderNext(
188451
188540
  pReader->zTerm[nTerm] = '\0';
188452
188541
  pReader->nTerm = nTerm;
188453
188542
 
188454
- aCopy = (char*)sqlite3_malloc(nCopy);
188543
+ aCopy = (char*)sqlite3_malloc64(nCopy);
188455
188544
  if( !aCopy ) return SQLITE_NOMEM;
188456
188545
  memcpy(aCopy, pList->aData, nCopy);
188457
188546
  pReader->nNode = pReader->nDoclist = nCopy;
@@ -188738,7 +188827,7 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
188738
188827
  nExtra = nRoot + FTS3_NODE_PADDING;
188739
188828
  }
188740
188829
 
188741
- pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
188830
+ pReader = (Fts3SegReader *)sqlite3_malloc64(sizeof(Fts3SegReader) + nExtra);
188742
188831
  if( !pReader ){
188743
188832
  return SQLITE_NOMEM;
188744
188833
  }
@@ -188830,7 +188919,7 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
188830
188919
  if( nElem==nAlloc ){
188831
188920
  Fts3HashElem **aElem2;
188832
188921
  nAlloc += 16;
188833
- aElem2 = (Fts3HashElem **)sqlite3_realloc(
188922
+ aElem2 = (Fts3HashElem **)sqlite3_realloc64(
188834
188923
  aElem, nAlloc*sizeof(Fts3HashElem *)
188835
188924
  );
188836
188925
  if( !aElem2 ){
@@ -189164,7 +189253,7 @@ static int fts3NodeAddTerm(
189164
189253
  ** this is not expected to be a serious problem.
189165
189254
  */
189166
189255
  assert( pTree->aData==(char *)&pTree[1] );
189167
- pTree->aData = (char *)sqlite3_malloc(nReq);
189256
+ pTree->aData = (char *)sqlite3_malloc64(nReq);
189168
189257
  if( !pTree->aData ){
189169
189258
  return SQLITE_NOMEM;
189170
189259
  }
@@ -189182,7 +189271,7 @@ static int fts3NodeAddTerm(
189182
189271
 
189183
189272
  if( isCopyTerm ){
189184
189273
  if( pTree->nMalloc<nTerm ){
189185
- char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
189274
+ char *zNew = sqlite3_realloc64(pTree->zMalloc, (i64)nTerm*2);
189186
189275
  if( !zNew ){
189187
189276
  return SQLITE_NOMEM;
189188
189277
  }
@@ -189208,7 +189297,7 @@ static int fts3NodeAddTerm(
189208
189297
  ** now. Instead, the term is inserted into the parent of pTree. If pTree
189209
189298
  ** has no parent, one is created here.
189210
189299
  */
189211
- pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
189300
+ pNew = (SegmentNode *)sqlite3_malloc64(sizeof(SegmentNode) + p->nNodeSize);
189212
189301
  if( !pNew ){
189213
189302
  return SQLITE_NOMEM;
189214
189303
  }
@@ -189346,7 +189435,7 @@ static int fts3SegWriterAdd(
189346
189435
  ){
189347
189436
  int nPrefix; /* Size of term prefix in bytes */
189348
189437
  int nSuffix; /* Size of term suffix in bytes */
189349
- int nReq; /* Number of bytes required on leaf page */
189438
+ i64 nReq; /* Number of bytes required on leaf page */
189350
189439
  int nData;
189351
189440
  SegmentWriter *pWriter = *ppWriter;
189352
189441
 
@@ -189355,13 +189444,13 @@ static int fts3SegWriterAdd(
189355
189444
  sqlite3_stmt *pStmt;
189356
189445
 
189357
189446
  /* Allocate the SegmentWriter structure */
189358
- pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
189447
+ pWriter = (SegmentWriter *)sqlite3_malloc64(sizeof(SegmentWriter));
189359
189448
  if( !pWriter ) return SQLITE_NOMEM;
189360
189449
  memset(pWriter, 0, sizeof(SegmentWriter));
189361
189450
  *ppWriter = pWriter;
189362
189451
 
189363
189452
  /* Allocate a buffer in which to accumulate data */
189364
- pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
189453
+ pWriter->aData = (char *)sqlite3_malloc64(p->nNodeSize);
189365
189454
  if( !pWriter->aData ) return SQLITE_NOMEM;
189366
189455
  pWriter->nSize = p->nNodeSize;
189367
189456
 
@@ -189436,7 +189525,7 @@ static int fts3SegWriterAdd(
189436
189525
  ** the buffer to make it large enough.
189437
189526
  */
189438
189527
  if( nReq>pWriter->nSize ){
189439
- char *aNew = sqlite3_realloc(pWriter->aData, nReq);
189528
+ char *aNew = sqlite3_realloc64(pWriter->aData, nReq);
189440
189529
  if( !aNew ) return SQLITE_NOMEM;
189441
189530
  pWriter->aData = aNew;
189442
189531
  pWriter->nSize = nReq;
@@ -189461,7 +189550,7 @@ static int fts3SegWriterAdd(
189461
189550
  */
189462
189551
  if( isCopyTerm ){
189463
189552
  if( nTerm>pWriter->nMalloc ){
189464
- char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
189553
+ char *zNew = sqlite3_realloc64(pWriter->zMalloc, (i64)nTerm*2);
189465
189554
  if( !zNew ){
189466
189555
  return SQLITE_NOMEM;
189467
189556
  }
@@ -189769,12 +189858,12 @@ static void fts3ColumnFilter(
189769
189858
  static int fts3MsrBufferData(
189770
189859
  Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
189771
189860
  char *pList,
189772
- int nList
189861
+ i64 nList
189773
189862
  ){
189774
189863
  if( nList>pMsr->nBuffer ){
189775
189864
  char *pNew;
189776
189865
  pMsr->nBuffer = nList*2;
189777
- pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
189866
+ pNew = (char *)sqlite3_realloc64(pMsr->aBuffer, pMsr->nBuffer);
189778
189867
  if( !pNew ) return SQLITE_NOMEM;
189779
189868
  pMsr->aBuffer = pNew;
189780
189869
  }
@@ -189830,7 +189919,7 @@ SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
189830
189919
  fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
189831
189920
 
189832
189921
  if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
189833
- rc = fts3MsrBufferData(pMsr, pList, nList+1);
189922
+ rc = fts3MsrBufferData(pMsr, pList, (i64)nList+1);
189834
189923
  if( rc!=SQLITE_OK ) return rc;
189835
189924
  assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
189836
189925
  pList = pMsr->aBuffer;
@@ -189967,11 +190056,11 @@ SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
189967
190056
  return SQLITE_OK;
189968
190057
  }
189969
190058
 
189970
- static int fts3GrowSegReaderBuffer(Fts3MultiSegReader *pCsr, int nReq){
190059
+ static int fts3GrowSegReaderBuffer(Fts3MultiSegReader *pCsr, i64 nReq){
189971
190060
  if( nReq>pCsr->nBuffer ){
189972
190061
  char *aNew;
189973
190062
  pCsr->nBuffer = nReq*2;
189974
- aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
190063
+ aNew = sqlite3_realloc64(pCsr->aBuffer, pCsr->nBuffer);
189975
190064
  if( !aNew ){
189976
190065
  return SQLITE_NOMEM;
189977
190066
  }
@@ -190062,7 +190151,8 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
190062
190151
  ){
190063
190152
  pCsr->nDoclist = apSegment[0]->nDoclist;
190064
190153
  if( fts3SegReaderIsPending(apSegment[0]) ){
190065
- rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
190154
+ rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist,
190155
+ (i64)pCsr->nDoclist);
190066
190156
  pCsr->aDoclist = pCsr->aBuffer;
190067
190157
  }else{
190068
190158
  pCsr->aDoclist = apSegment[0]->aDoclist;
@@ -190115,7 +190205,8 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
190115
190205
 
190116
190206
  nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
190117
190207
 
190118
- rc = fts3GrowSegReaderBuffer(pCsr, nByte+nDoclist+FTS3_NODE_PADDING);
190208
+ rc = fts3GrowSegReaderBuffer(pCsr,
190209
+ (i64)nByte+nDoclist+FTS3_NODE_PADDING);
190119
190210
  if( rc ) return rc;
190120
190211
 
190121
190212
  if( isFirst ){
@@ -190141,7 +190232,7 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
190141
190232
  fts3SegReaderSort(apSegment, nMerge, j, xCmp);
190142
190233
  }
190143
190234
  if( nDoclist>0 ){
190144
- rc = fts3GrowSegReaderBuffer(pCsr, nDoclist+FTS3_NODE_PADDING);
190235
+ rc = fts3GrowSegReaderBuffer(pCsr, (i64)nDoclist+FTS3_NODE_PADDING);
190145
190236
  if( rc ) return rc;
190146
190237
  memset(&pCsr->aBuffer[nDoclist], 0, FTS3_NODE_PADDING);
190147
190238
  pCsr->aDoclist = pCsr->aBuffer;
@@ -190854,7 +190945,7 @@ struct NodeReader {
190854
190945
  static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
190855
190946
  if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
190856
190947
  int nAlloc = nMin;
190857
- char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
190948
+ char *a = (char *)sqlite3_realloc64(pBlob->a, nAlloc);
190858
190949
  if( a ){
190859
190950
  pBlob->nAlloc = nAlloc;
190860
190951
  pBlob->a = a;
@@ -191651,7 +191742,7 @@ static int fts3RepackSegdirLevel(
191651
191742
  if( nIdx>=nAlloc ){
191652
191743
  int *aNew;
191653
191744
  nAlloc += 16;
191654
- aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
191745
+ aNew = sqlite3_realloc64(aIdx, nAlloc*sizeof(int));
191655
191746
  if( !aNew ){
191656
191747
  rc = SQLITE_NOMEM;
191657
191748
  break;
@@ -192025,7 +192116,7 @@ SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
192025
192116
 
192026
192117
  /* Allocate space for the cursor, filter and writer objects */
192027
192118
  const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
192028
- pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
192119
+ pWriter = (IncrmergeWriter *)sqlite3_malloc64(nAlloc);
192029
192120
  if( !pWriter ) return SQLITE_NOMEM;
192030
192121
  pFilter = (Fts3SegFilter *)&pWriter[1];
192031
192122
  pCsr = (Fts3MultiSegReader *)&pFilter[1];
@@ -192661,7 +192752,7 @@ SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
192661
192752
  return SQLITE_OK;
192662
192753
  }
192663
192754
 
192664
- pRet = (char *)sqlite3_malloc(p->pList->nData);
192755
+ pRet = (char *)sqlite3_malloc64(p->pList->nData);
192665
192756
  if( !pRet ) return SQLITE_NOMEM;
192666
192757
 
192667
192758
  nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
@@ -192681,7 +192772,7 @@ SQLITE_PRIVATE int sqlite3Fts3DeferToken(
192681
192772
  int iCol /* Column that token must appear in (or -1) */
192682
192773
  ){
192683
192774
  Fts3DeferredToken *pDeferred;
192684
- pDeferred = sqlite3_malloc(sizeof(*pDeferred));
192775
+ pDeferred = sqlite3_malloc64(sizeof(*pDeferred));
192685
192776
  if( !pDeferred ){
192686
192777
  return SQLITE_NOMEM;
192687
192778
  }
@@ -204260,7 +204351,7 @@ static int geopolyUpdate(
204260
204351
  sqlite3_free(p);
204261
204352
  nChange = 1;
204262
204353
  }
204263
- for(jj=1; jj<pRtree->nAux; jj++){
204354
+ for(jj=1; jj<nData-2; jj++){
204264
204355
  nChange++;
204265
204356
  sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
204266
204357
  }
@@ -204863,8 +204954,9 @@ static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
204863
204954
 
204864
204955
  if( U_SUCCESS(status) ){
204865
204956
  sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
204866
- }else{
204867
- assert(!pExpr);
204957
+ pExpr = sqlite3_get_auxdata(p, 0);
204958
+ }
204959
+ if( !pExpr ){
204868
204960
  icuFunctionError(p, "uregex_open", status);
204869
204961
  return;
204870
204962
  }
@@ -236750,7 +236842,7 @@ static void fts5SourceIdFunc(
236750
236842
  ){
236751
236843
  assert( nArg==0 );
236752
236844
  UNUSED_PARAM2(nArg, apUnused);
236753
- sqlite3_result_text(pCtx, "fts5: 2022-07-13 19:41:41 7c16541a0efb3985578181171c9f2bb3fdc4bad6a2ec85c6e31ab96f3eff201f", -1, SQLITE_TRANSIENT);
236845
+ sqlite3_result_text(pCtx, "fts5: 2022-09-29 15:55:41 a29f9949895322123f7c38fbe94c649a9d6e6c9cd0c3b41c96d694552f26b309", -1, SQLITE_TRANSIENT);
236754
236846
  }
236755
236847
 
236756
236848
  /*
@@ -241744,7 +241836,7 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
241744
241836
  ** Purpose: Header file for SQLite3 Multiple Ciphers compile-time configuration
241745
241837
  ** Author: Ulrich Telle
241746
241838
  ** Created: 2021-09-27
241747
- ** Copyright: (c) 2019-2021 Ulrich Telle
241839
+ ** Copyright: (c) 2019-2022 Ulrich Telle
241748
241840
  ** License: MIT
241749
241841
  */
241750
241842
 
@@ -241801,6 +241893,27 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
241801
241893
  #define HAVE_CIPHER_RC4 1
241802
241894
  #endif
241803
241895
 
241896
+ /*
241897
+ ** Disable all built-in ciphers on request
241898
+ */
241899
+
241900
+ #if 0
241901
+ #define SQLITE3MC_OMIT_BUILTIN_CIPHERS
241902
+ #endif
241903
+
241904
+ #ifdef SQLITE3MC_OMIT_BUILTIN_CIPHERS
241905
+ #undef HAVE_CIPHER_AES_128_CBC
241906
+ #undef HAVE_CIPHER_AES_256_CBC
241907
+ #undef HAVE_CIPHER_CHACHA20
241908
+ #undef HAVE_CIPHER_SQLCIPHER
241909
+ #undef HAVE_CIPHER_RC4
241910
+ #define HAVE_CIPHER_AES_128_CBC 0
241911
+ #define HAVE_CIPHER_AES_256_CBC 0
241912
+ #define HAVE_CIPHER_CHACHA20 0
241913
+ #define HAVE_CIPHER_SQLCIPHER 0
241914
+ #define HAVE_CIPHER_RC4 0
241915
+ #endif
241916
+
241804
241917
  /*
241805
241918
  ** Check that at least one cipher is be supported
241806
241919
  */
@@ -241809,7 +241922,7 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
241809
241922
  HAVE_CIPHER_CHACHA20 == 0 && \
241810
241923
  HAVE_CIPHER_SQLCIPHER == 0 && \
241811
241924
  HAVE_CIPHER_RC4 == 0
241812
- #error Enable at least one cipher scheme!
241925
+ #pragma message ("sqlite3mc_config.h: WARNING - No built-in cipher scheme enabled!")
241813
241926
  #endif
241814
241927
 
241815
241928
  /*
@@ -241888,7 +242001,7 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
241888
242001
  ** Purpose: Header file for SQLite3 Multiple Ciphers support
241889
242002
  ** Author: Ulrich Telle
241890
242003
  ** Created: 2020-03-01
241891
- ** Copyright: (c) 2019-2021 Ulrich Telle
242004
+ ** Copyright: (c) 2019-2022 Ulrich Telle
241892
242005
  ** License: MIT
241893
242006
  */
241894
242007
 
@@ -241915,10 +242028,10 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
241915
242028
  #define SQLITE3MC_VERSION_H_
241916
242029
 
241917
242030
  #define SQLITE3MC_VERSION_MAJOR 1
241918
- #define SQLITE3MC_VERSION_MINOR 4
241919
- #define SQLITE3MC_VERSION_RELEASE 6
242031
+ #define SQLITE3MC_VERSION_MINOR 5
242032
+ #define SQLITE3MC_VERSION_RELEASE 3
241920
242033
  #define SQLITE3MC_VERSION_SUBRELEASE 0
241921
- #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.4.6"
242034
+ #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.5.3"
241922
242035
 
241923
242036
  #endif /* SQLITE3MC_VERSION_H_ */
241924
242037
  /*** End of #include "sqlite3mc_version.h" ***/
@@ -242077,9 +242190,9 @@ extern "C" {
242077
242190
  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
242078
242191
  ** [sqlite_version()] and [sqlite_source_id()].
242079
242192
  */
242080
- #define SQLITE_VERSION "3.39.1"
242081
- #define SQLITE_VERSION_NUMBER 3039001
242082
- #define SQLITE_SOURCE_ID "2022-07-13 19:41:41 7c16541a0efb3985578181171c9f2bb3fdc4bad6a2ec85c6e31ab96f3eff201f"
242193
+ #define SQLITE_VERSION "3.39.4"
242194
+ #define SQLITE_VERSION_NUMBER 3039004
242195
+ #define SQLITE_SOURCE_ID "2022-09-29 15:55:41 a29f9949895322123f7c38fbe94c649a9d6e6c9cd0c3b41c96d694552f26b309"
242083
242196
 
242084
242197
  /*
242085
242198
  ** CAPI3REF: Run-Time Library Version Numbers
@@ -254874,13 +254987,13 @@ int sqlite3_user_delete(
254874
254987
  /*
254875
254988
  ** Symbols for ciphers
254876
254989
  */
254877
- #define CODEC_TYPE_UNKNOWN 0
254878
- #define CODEC_TYPE_AES128 1
254879
- #define CODEC_TYPE_AES256 2
254880
- #define CODEC_TYPE_CHACHA20 3
254881
- #define CODEC_TYPE_SQLCIPHER 4
254882
- #define CODEC_TYPE_RC4 5
254883
- #define CODEC_TYPE_MAX 5
254990
+ #define CODEC_TYPE_UNKNOWN 0
254991
+ #define CODEC_TYPE_AES128 1
254992
+ #define CODEC_TYPE_AES256 2
254993
+ #define CODEC_TYPE_CHACHA20 3
254994
+ #define CODEC_TYPE_SQLCIPHER 4
254995
+ #define CODEC_TYPE_RC4 5
254996
+ #define CODEC_TYPE_MAX_BUILTIN 5
254884
254997
 
254885
254998
  /*
254886
254999
  ** Definition of API functions
@@ -254949,6 +255062,9 @@ SQLITE_API void sqlite3_activate_see(const char* zPassPhrase);
254949
255062
  /*
254950
255063
  ** Define functions for the configuration of the wxSQLite3 encryption extension
254951
255064
  */
255065
+ SQLITE_API int sqlite3mc_cipher_count();
255066
+ SQLITE_API int sqlite3mc_cipher_index(const char* cipherName);
255067
+ SQLITE_API const char* sqlite3mc_cipher_name(int cipherIndex);
254952
255068
  SQLITE_API int sqlite3mc_config(sqlite3* db, const char* paramName, int newValue);
254953
255069
  SQLITE_API int sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramName, int newValue);
254954
255070
  SQLITE_API unsigned char* sqlite3mc_codec_data(sqlite3* db, const char* zDbName, const char* paramName);
@@ -254960,6 +255076,88 @@ SQLITE_API int wxsqlite3_config_cipher(sqlite3* db, const char* cipherName, cons
254960
255076
  SQLITE_API unsigned char* wxsqlite3_codec_data(sqlite3* db, const char* zDbName, const char* paramName);
254961
255077
  #endif
254962
255078
 
255079
+ /*
255080
+ ** Structures and functions to dynamically register a cipher
255081
+ */
255082
+
255083
+ /*
255084
+ ** Structure for a single cipher configuration parameter
255085
+ **
255086
+ ** Components:
255087
+ ** m_name - name of parameter (1st char = alpha, rest = alphanumeric or underscore, max 63 characters)
255088
+ ** m_value - current/transient parameter value
255089
+ ** m_default - default parameter value
255090
+ ** m_minValue - minimum valid parameter value
255091
+ ** m_maxValue - maximum valid parameter value
255092
+ */
255093
+ typedef struct _CipherParams
255094
+ {
255095
+ char* m_name;
255096
+ int m_value;
255097
+ int m_default;
255098
+ int m_minValue;
255099
+ int m_maxValue;
255100
+ } CipherParams;
255101
+
255102
+ /*
255103
+ ** Structure for a cipher API
255104
+ **
255105
+ ** Components:
255106
+ ** m_name - name of cipher (1st char = alpha, rest = alphanumeric or underscore, max 63 characters)
255107
+ ** m_allocateCipher - Function pointer for function AllocateCipher
255108
+ ** m_freeCipher - Function pointer for function FreeCipher
255109
+ ** m_cloneCipher - Function pointer for function CloneCipher
255110
+ ** m_getLegacy - Function pointer for function GetLegacy
255111
+ ** m_getPageSize - Function pointer for function GetPageSize
255112
+ ** m_getReserved - Function pointer for function GetReserved
255113
+ ** m_getSalt - Function pointer for function GetSalt
255114
+ ** m_generateKey - Function pointer for function GenerateKey
255115
+ ** m_encryptPage - Function pointer for function EncryptPage
255116
+ ** m_decryptPage - Function pointer for function DecryptPage
255117
+ */
255118
+
255119
+ typedef struct BtShared BtSharedMC;
255120
+
255121
+ typedef void* (*AllocateCipher_t)(sqlite3* db);
255122
+ typedef void (*FreeCipher_t)(void* cipher);
255123
+ typedef void (*CloneCipher_t)(void* cipherTo, void* cipherFrom);
255124
+ typedef int (*GetLegacy_t)(void* cipher);
255125
+ typedef int (*GetPageSize_t)(void* cipher);
255126
+ typedef int (*GetReserved_t)(void* cipher);
255127
+ typedef unsigned char* (*GetSalt_t)(void* cipher);
255128
+ typedef void (*GenerateKey_t)(void* cipher, BtSharedMC* pBt, char* userPassword, int passwordLength, int rekey, unsigned char* cipherSalt);
255129
+ typedef int (*EncryptPage_t)(void* cipher, int page, unsigned char* data, int len, int reserved);
255130
+ typedef int (*DecryptPage_t)(void* cipher, int page, unsigned char* data, int len, int reserved, int hmacCheck);
255131
+
255132
+ typedef struct _CipherDescriptor
255133
+ {
255134
+ char* m_name;
255135
+ AllocateCipher_t m_allocateCipher;
255136
+ FreeCipher_t m_freeCipher;
255137
+ CloneCipher_t m_cloneCipher;
255138
+ GetLegacy_t m_getLegacy;
255139
+ GetPageSize_t m_getPageSize;
255140
+ GetReserved_t m_getReserved;
255141
+ GetSalt_t m_getSalt;
255142
+ GenerateKey_t m_generateKey;
255143
+ EncryptPage_t m_encryptPage;
255144
+ DecryptPage_t m_decryptPage;
255145
+ } CipherDescriptor;
255146
+
255147
+ /*
255148
+ ** Register a cipher
255149
+ **
255150
+ ** Arguments:
255151
+ ** desc - Cipher descriptor structure
255152
+ ** params - Cipher configuration parameter table
255153
+ ** makeDefault - flag whether to make the cipher the default cipher
255154
+ **
255155
+ ** Returns:
255156
+ ** SQLITE_OK - the cipher could be registered successfully
255157
+ ** SQLITE_ERROR - the cipher could not be registered
255158
+ */
255159
+ SQLITE_API int sqlite3mc_register_cipher(const CipherDescriptor* desc, const CipherParams* params, int makeDefault);
255160
+
254963
255161
  #ifdef __cplusplus
254964
255162
  }
254965
255163
  #endif
@@ -257774,6 +257972,16 @@ static size_t entropy(void* buf, size_t n)
257774
257972
  #endif
257775
257973
  return read_urandom(buf, n);
257776
257974
  }
257975
+
257976
+ #elif defined(__WASM__)
257977
+
257978
+ extern size_t getentropy(void* buf, size_t n);
257979
+
257980
+ static size_t entropy(void* buf, size_t n)
257981
+ {
257982
+ return (getentropy(buf, n) == 0) ? n : 0;
257983
+ }
257984
+
257777
257985
  #else
257778
257986
  # error "Secure pseudorandom number generator not implemented for this OS"
257779
257987
  #endif
@@ -260996,7 +261204,7 @@ void RijndaelInvalidate(Rijndael* rijndael)
260996
261204
  ** Purpose: Header for the ciphers of SQLite3 Multiple Ciphers
260997
261205
  ** Author: Ulrich Telle
260998
261206
  ** Created: 2020-02-02
260999
- ** Copyright: (c) 2006-2021 Ulrich Telle
261207
+ ** Copyright: (c) 2006-2022 Ulrich Telle
261000
261208
  ** License: MIT
261001
261209
  */
261002
261210
 
@@ -261018,10 +261226,31 @@ void RijndaelInvalidate(Rijndael* rijndael)
261018
261226
  #define CODEC_TYPE CODEC_TYPE_DEFAULT
261019
261227
  #endif
261020
261228
 
261021
- #if CODEC_TYPE < 1 || CODEC_TYPE > CODEC_TYPE_MAX
261229
+ #if CODEC_TYPE < 1 || CODEC_TYPE > CODEC_TYPE_MAX_BUILTIN
261022
261230
  #error "Invalid codec type selected"
261023
261231
  #endif
261024
261232
 
261233
+ /*
261234
+ ** Define the maximum number of ciphers that can be registered
261235
+ */
261236
+
261237
+ /* Use a reasonable upper limit for the maximum number of ciphers */
261238
+ #define CODEC_COUNT_LIMIT 16
261239
+
261240
+ #ifdef SQLITE3MC_MAX_CODEC_COUNT
261241
+ /* Allow at least to register all built-in ciphers, but use a reasonable upper limit */
261242
+ #if SQLITE3MC_MAX_CODEC_COUNT >= CODEC_TYPE_MAX_BUILTIN && SQLITE3MC_MAX_CODEC_COUNT <= CODEC_COUNT_LIMIT
261243
+ #define CODEC_COUNT_MAX SQLITE3MC_MAX_CODEC_COUNT
261244
+ #else
261245
+ #error "Maximum cipher count not in range [CODEC_TYPE_MAX_BUILTIN .. CODEC_COUNT_LIMIT]"
261246
+ #endif
261247
+ #else
261248
+ #define CODEC_COUNT_MAX CODEC_COUNT_LIMIT
261249
+ #endif
261250
+
261251
+ #define CIPHER_NAME_MAXLEN 32
261252
+ #define CIPHER_PARAMS_COUNT_MAX 64
261253
+
261025
261254
  #define MAXKEYLENGTH 32
261026
261255
  #define KEYLENGTH_AES128 16
261027
261256
  #define KEYLENGTH_AES256 32
@@ -261029,6 +261258,13 @@ void RijndaelInvalidate(Rijndael* rijndael)
261029
261258
 
261030
261259
  #define CODEC_SHA_ITER 4001
261031
261260
 
261261
+ typedef struct _CodecParameter
261262
+ {
261263
+ char* m_name;
261264
+ int m_id;
261265
+ CipherParams* m_params;
261266
+ } CodecParameter;
261267
+
261032
261268
  typedef struct _Codec
261033
261269
  {
261034
261270
  int m_isEncrypted;
@@ -261060,53 +261296,11 @@ typedef struct _Codec
261060
261296
  #define CIPHER_PARAMS_SENTINEL { "", 0, 0, 0, 0 }
261061
261297
  #define CIPHER_PAGE1_OFFSET 24
261062
261298
 
261063
- typedef struct _CipherParams
261064
- {
261065
- char* m_name;
261066
- int m_value;
261067
- int m_default;
261068
- int m_minValue;
261069
- int m_maxValue;
261070
- } CipherParams;
261071
-
261072
- typedef struct _CodecParameter
261073
- {
261074
- char* m_name;
261075
- int m_id;
261076
- CipherParams* m_params;
261077
- } CodecParameter;
261078
-
261079
- typedef void* (*AllocateCipher_t)(sqlite3* db);
261080
- typedef void (*FreeCipher_t)(void* cipher);
261081
- typedef void (*CloneCipher_t)(void* cipherTo, void* cipherFrom);
261082
- typedef int (*GetLegacy_t)(void* cipher);
261083
- typedef int (*GetPageSize_t)(void* cipher);
261084
- typedef int (*GetReserved_t)(void* cipher);
261085
- typedef unsigned char* (*GetSalt_t)(void* cipher);
261086
- typedef void (*GenerateKey_t)(void* cipher, BtShared* pBt, char* userPassword, int passwordLength, int rekey, unsigned char* cipherSalt);
261087
- typedef int (*EncryptPage_t)(void* cipher, int page, unsigned char* data, int len, int reserved);
261088
- typedef int (*DecryptPage_t)(void* cipher, int page, unsigned char* data, int len, int reserved, int hmacCheck);
261089
-
261090
- typedef struct _CodecDescriptor
261091
- {
261092
- char m_name[32];
261093
- AllocateCipher_t m_allocateCipher;
261094
- FreeCipher_t m_freeCipher;
261095
- CloneCipher_t m_cloneCipher;
261096
- GetLegacy_t m_getLegacy;
261097
- GetPageSize_t m_getPageSize;
261098
- GetReserved_t m_getReserved;
261099
- GetSalt_t m_getSalt;
261100
- GenerateKey_t m_generateKey;
261101
- EncryptPage_t m_encryptPage;
261102
- DecryptPage_t m_decryptPage;
261103
- } CipherDescriptor;
261104
-
261105
261299
  SQLITE_PRIVATE int sqlite3mcGetCipherParameter(CipherParams* cipherParams, const char* paramName);
261106
261300
 
261107
261301
  SQLITE_PRIVATE int sqlite3mcGetCipherType(sqlite3* db);
261108
261302
 
261109
- SQLITE_PRIVATE CipherParams* sqlite3mcGetCipherParams(sqlite3* db, int cypherType);
261303
+ SQLITE_PRIVATE CipherParams* sqlite3mcGetCipherParams(sqlite3* db, const char* cipherName);
261110
261304
 
261111
261305
  SQLITE_PRIVATE int sqlite3mcCodecInit(Codec* codec);
261112
261306
 
@@ -261509,6 +261703,8 @@ sqlite3mcConvertHex2Bin(const unsigned char* hex, int len, unsigned char* bin)
261509
261703
  /* --- AES 128-bit cipher (wxSQLite3) --- */
261510
261704
  #if HAVE_CIPHER_AES_128_CBC
261511
261705
 
261706
+ #define CIPHER_NAME_AES128 "aes128cbc"
261707
+
261512
261708
  /*
261513
261709
  ** Configuration parameters for "aes128cbc"
261514
261710
  **
@@ -261559,7 +261755,7 @@ AllocateAES128Cipher(sqlite3* db)
261559
261755
  }
261560
261756
  if (aesCipher != NULL)
261561
261757
  {
261562
- CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_AES128);
261758
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_AES128);
261563
261759
  aesCipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
261564
261760
  aesCipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
261565
261761
  }
@@ -261763,16 +261959,17 @@ DecryptPageAES128Cipher(void* cipher, int page, unsigned char* data, int len, in
261763
261959
 
261764
261960
  SQLITE_PRIVATE const CipherDescriptor mcAES128Descriptor =
261765
261961
  {
261766
- "aes128cbc", AllocateAES128Cipher,
261767
- FreeAES128Cipher,
261768
- CloneAES128Cipher,
261769
- GetLegacyAES128Cipher,
261770
- GetPageSizeAES128Cipher,
261771
- GetReservedAES128Cipher,
261772
- GetSaltAES128Cipher,
261773
- GenerateKeyAES128Cipher,
261774
- EncryptPageAES128Cipher,
261775
- DecryptPageAES128Cipher
261962
+ CIPHER_NAME_AES128,
261963
+ AllocateAES128Cipher,
261964
+ FreeAES128Cipher,
261965
+ CloneAES128Cipher,
261966
+ GetLegacyAES128Cipher,
261967
+ GetPageSizeAES128Cipher,
261968
+ GetReservedAES128Cipher,
261969
+ GetSaltAES128Cipher,
261970
+ GenerateKeyAES128Cipher,
261971
+ EncryptPageAES128Cipher,
261972
+ DecryptPageAES128Cipher
261776
261973
  };
261777
261974
  #endif
261778
261975
  /*** End of #include "cipher_wxaes128.c" ***/
@@ -261794,6 +261991,8 @@ SQLITE_PRIVATE const CipherDescriptor mcAES128Descriptor =
261794
261991
  /* --- AES 256-bit cipher (wxSQLite3) --- */
261795
261992
  #if HAVE_CIPHER_AES_256_CBC
261796
261993
 
261994
+ #define CIPHER_NAME_AES256 "aes256cbc"
261995
+
261797
261996
  /*
261798
261997
  ** Configuration parameters for "aes256cbc"
261799
261998
  **
@@ -261848,7 +262047,7 @@ AllocateAES256Cipher(sqlite3* db)
261848
262047
  }
261849
262048
  if (aesCipher != NULL)
261850
262049
  {
261851
- CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_AES256);
262050
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_AES256);
261852
262051
  aesCipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
261853
262052
  aesCipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
261854
262053
  aesCipher->m_kdfIter = sqlite3mcGetCipherParameter(cipherParams, "kdf_iter");
@@ -262016,16 +262215,17 @@ DecryptPageAES256Cipher(void* cipher, int page, unsigned char* data, int len, in
262016
262215
 
262017
262216
  SQLITE_PRIVATE const CipherDescriptor mcAES256Descriptor =
262018
262217
  {
262019
- "aes256cbc", AllocateAES256Cipher,
262020
- FreeAES256Cipher,
262021
- CloneAES256Cipher,
262022
- GetLegacyAES256Cipher,
262023
- GetPageSizeAES256Cipher,
262024
- GetReservedAES256Cipher,
262025
- GetSaltAES256Cipher,
262026
- GenerateKeyAES256Cipher,
262027
- EncryptPageAES256Cipher,
262028
- DecryptPageAES256Cipher
262218
+ CIPHER_NAME_AES256,
262219
+ AllocateAES256Cipher,
262220
+ FreeAES256Cipher,
262221
+ CloneAES256Cipher,
262222
+ GetLegacyAES256Cipher,
262223
+ GetPageSizeAES256Cipher,
262224
+ GetReservedAES256Cipher,
262225
+ GetSaltAES256Cipher,
262226
+ GenerateKeyAES256Cipher,
262227
+ EncryptPageAES256Cipher,
262228
+ DecryptPageAES256Cipher
262029
262229
  };
262030
262230
  #endif
262031
262231
  /*** End of #include "cipher_wxaes256.c" ***/
@@ -262047,6 +262247,8 @@ SQLITE_PRIVATE const CipherDescriptor mcAES256Descriptor =
262047
262247
  /* --- ChaCha20-Poly1305 cipher (plus sqleet variant) --- */
262048
262248
  #if HAVE_CIPHER_CHACHA20
262049
262249
 
262250
+ #define CIPHER_NAME_CHACHA20 "chacha20"
262251
+
262050
262252
  /*
262051
262253
  ** Configuration parameters for "chacha20"
262052
262254
  **
@@ -262103,7 +262305,7 @@ AllocateChaCha20Cipher(sqlite3* db)
262103
262305
  }
262104
262306
  if (chacha20Cipher != NULL)
262105
262307
  {
262106
- CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_CHACHA20);
262308
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_CHACHA20);
262107
262309
  chacha20Cipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
262108
262310
  chacha20Cipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
262109
262311
  chacha20Cipher->m_kdfIter = sqlite3mcGetCipherParameter(cipherParams, "kdf_iter");
@@ -262399,16 +262601,17 @@ DecryptPageChaCha20Cipher(void* cipher, int page, unsigned char* data, int len,
262399
262601
 
262400
262602
  SQLITE_PRIVATE const CipherDescriptor mcChaCha20Descriptor =
262401
262603
  {
262402
- "chacha20", AllocateChaCha20Cipher,
262403
- FreeChaCha20Cipher,
262404
- CloneChaCha20Cipher,
262405
- GetLegacyChaCha20Cipher,
262406
- GetPageSizeChaCha20Cipher,
262407
- GetReservedChaCha20Cipher,
262408
- GetSaltChaCha20Cipher,
262409
- GenerateKeyChaCha20Cipher,
262410
- EncryptPageChaCha20Cipher,
262411
- DecryptPageChaCha20Cipher
262604
+ CIPHER_NAME_CHACHA20,
262605
+ AllocateChaCha20Cipher,
262606
+ FreeChaCha20Cipher,
262607
+ CloneChaCha20Cipher,
262608
+ GetLegacyChaCha20Cipher,
262609
+ GetPageSizeChaCha20Cipher,
262610
+ GetReservedChaCha20Cipher,
262611
+ GetSaltChaCha20Cipher,
262612
+ GenerateKeyChaCha20Cipher,
262613
+ EncryptPageChaCha20Cipher,
262614
+ DecryptPageChaCha20Cipher
262412
262615
  };
262413
262616
  #endif
262414
262617
  /*** End of #include "cipher_chacha20.c" ***/
@@ -262430,6 +262633,8 @@ SQLITE_PRIVATE const CipherDescriptor mcChaCha20Descriptor =
262430
262633
  /* --- SQLCipher AES256CBC-HMAC cipher --- */
262431
262634
  #if HAVE_CIPHER_SQLCIPHER
262432
262635
 
262636
+ #define CIPHER_NAME_SQLCIPHER "sqlcipher"
262637
+
262433
262638
  /*
262434
262639
  ** Configuration parameters for "sqlcipher"
262435
262640
  **
@@ -262545,7 +262750,7 @@ AllocateSQLCipherCipher(sqlite3* db)
262545
262750
  }
262546
262751
  if (sqlCipherCipher != NULL)
262547
262752
  {
262548
- CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_SQLCIPHER);
262753
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_SQLCIPHER);
262549
262754
  sqlCipherCipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
262550
262755
  sqlCipherCipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
262551
262756
  sqlCipherCipher->m_kdfIter = sqlite3mcGetCipherParameter(cipherParams, "kdf_iter");
@@ -262918,16 +263123,17 @@ DecryptPageSQLCipherCipher(void* cipher, int page, unsigned char* data, int len,
262918
263123
  }
262919
263124
  SQLITE_PRIVATE const CipherDescriptor mcSQLCipherDescriptor =
262920
263125
  {
262921
- "sqlcipher", AllocateSQLCipherCipher,
262922
- FreeSQLCipherCipher,
262923
- CloneSQLCipherCipher,
262924
- GetLegacySQLCipherCipher,
262925
- GetPageSizeSQLCipherCipher,
262926
- GetReservedSQLCipherCipher,
262927
- GetSaltSQLCipherCipher,
262928
- GenerateKeySQLCipherCipher,
262929
- EncryptPageSQLCipherCipher,
262930
- DecryptPageSQLCipherCipher
263126
+ CIPHER_NAME_SQLCIPHER,
263127
+ AllocateSQLCipherCipher,
263128
+ FreeSQLCipherCipher,
263129
+ CloneSQLCipherCipher,
263130
+ GetLegacySQLCipherCipher,
263131
+ GetPageSizeSQLCipherCipher,
263132
+ GetReservedSQLCipherCipher,
263133
+ GetSaltSQLCipherCipher,
263134
+ GenerateKeySQLCipherCipher,
263135
+ EncryptPageSQLCipherCipher,
263136
+ DecryptPageSQLCipherCipher
262931
263137
  };
262932
263138
  #endif
262933
263139
  /*** End of #include "cipher_sqlcipher.c" ***/
@@ -262949,6 +263155,8 @@ SQLITE_PRIVATE const CipherDescriptor mcSQLCipherDescriptor =
262949
263155
  /* --- RC4 cipher (System.Data.SQLite) --- */
262950
263156
  #if HAVE_CIPHER_RC4
262951
263157
 
263158
+ #define CIPHER_NAME_RC4 "rc4"
263159
+
262952
263160
  /*
262953
263161
  ** Configuration parameters for "rc4"
262954
263162
  **
@@ -262989,7 +263197,7 @@ AllocateRC4Cipher(sqlite3* db)
262989
263197
  }
262990
263198
  if (rc4Cipher != NULL)
262991
263199
  {
262992
- CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_RC4);
263200
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_RC4);
262993
263201
  rc4Cipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
262994
263202
  rc4Cipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
262995
263203
  }
@@ -263092,16 +263300,17 @@ DecryptPageRC4Cipher(void* cipher, int page, unsigned char* data, int len, int r
263092
263300
 
263093
263301
  SQLITE_PRIVATE const CipherDescriptor mcRC4Descriptor =
263094
263302
  {
263095
- "rc4", AllocateRC4Cipher,
263096
- FreeRC4Cipher,
263097
- CloneRC4Cipher,
263098
- GetLegacyRC4Cipher,
263099
- GetPageSizeRC4Cipher,
263100
- GetReservedRC4Cipher,
263101
- GetSaltRC4Cipher,
263102
- GenerateKeyRC4Cipher,
263103
- EncryptPageRC4Cipher,
263104
- DecryptPageRC4Cipher
263303
+ CIPHER_NAME_RC4,
263304
+ AllocateRC4Cipher,
263305
+ FreeRC4Cipher,
263306
+ CloneRC4Cipher,
263307
+ GetLegacyRC4Cipher,
263308
+ GetPageSizeRC4Cipher,
263309
+ GetReservedRC4Cipher,
263310
+ GetSaltRC4Cipher,
263311
+ GenerateKeyRC4Cipher,
263312
+ EncryptPageRC4Cipher,
263313
+ DecryptPageRC4Cipher
263105
263314
  };
263106
263315
  #endif
263107
263316
  /*** End of #include "cipher_sds_rc4.c" ***/
@@ -263113,7 +263322,7 @@ SQLITE_PRIVATE const CipherDescriptor mcRC4Descriptor =
263113
263322
  ** Purpose: Implementation of SQLite codecs
263114
263323
  ** Author: Ulrich Telle
263115
263324
  ** Created: 2020-02-02
263116
- ** Copyright: (c) 2006-2021 Ulrich Telle
263325
+ ** Copyright: (c) 2006-2022 Ulrich Telle
263117
263326
  ** License: MIT
263118
263327
  */
263119
263328
 
@@ -263134,21 +263343,26 @@ static unsigned char padding[] =
263134
263343
 
263135
263344
  static CipherParams commonParams[] =
263136
263345
  {
263137
- { "cipher", CODEC_TYPE, CODEC_TYPE, 1, CODEC_TYPE_MAX },
263138
- { "hmac_check", 1, 1, 0, 1 },
263139
- { "mc_legacy_wal", SQLITE3MC_LEGACY_WAL, SQLITE3MC_LEGACY_WAL, 0, 1 },
263346
+ { "cipher", CODEC_TYPE_UNKNOWN, CODEC_TYPE_UNKNOWN, 1, CODEC_COUNT_MAX },
263347
+ { "hmac_check", 1, 1, 0, 1 },
263348
+ { "mc_legacy_wal", SQLITE3MC_LEGACY_WAL, SQLITE3MC_LEGACY_WAL, 0, 1 },
263140
263349
  CIPHER_PARAMS_SENTINEL
263141
263350
  };
263142
263351
 
263352
+ #define CIPHER_NAME_GLOBAL "global"
263353
+
263354
+ static CodecParameter globalCommonParams = { CIPHER_NAME_GLOBAL, CODEC_TYPE_UNKNOWN, commonParams };
263355
+ static CodecParameter globalSentinelParams = { "", CODEC_TYPE_UNKNOWN, NULL };
263356
+
263143
263357
  SQLITE_PRIVATE int
263144
263358
  sqlite3mcGetCipherParameter(CipherParams* cipherParams, const char* paramName)
263145
263359
  {
263146
263360
  int value = -1;
263147
- for (; strlen(cipherParams->m_name) > 0; ++cipherParams)
263361
+ for (; cipherParams->m_name[0] != 0; ++cipherParams)
263148
263362
  {
263149
263363
  if (sqlite3_stricmp(paramName, cipherParams->m_name) == 0) break;
263150
263364
  }
263151
- if (strlen(cipherParams->m_name) > 0)
263365
+ if (cipherParams->m_name[0] != 0)
263152
263366
  {
263153
263367
  value = cipherParams->m_value;
263154
263368
  cipherParams->m_value = cipherParams->m_default;
@@ -263156,26 +263370,15 @@ sqlite3mcGetCipherParameter(CipherParams* cipherParams, const char* paramName)
263156
263370
  return value;
263157
263371
  }
263158
263372
 
263159
- static CodecParameter globalCodecParameterTable[] =
263373
+ typedef struct _CipherName
263160
263374
  {
263161
- { "global", CODEC_TYPE_UNKNOWN, commonParams },
263162
- #if HAVE_CIPHER_AES_128_CBC
263163
- { "aes128cbc", CODEC_TYPE_AES128, mcAES128Params },
263164
- #endif
263165
- #if HAVE_CIPHER_AES_256_CBC
263166
- { "aes256cbc", CODEC_TYPE_AES256, mcAES256Params },
263167
- #endif
263168
- #if HAVE_CIPHER_CHACHA20
263169
- { "chacha20", CODEC_TYPE_CHACHA20, mcChaCha20Params },
263170
- #endif
263171
- #if HAVE_CIPHER_SQLCIPHER
263172
- { "sqlcipher", CODEC_TYPE_SQLCIPHER, mcSQLCipherParams },
263173
- #endif
263174
- #if HAVE_CIPHER_RC4
263175
- { "rc4", CODEC_TYPE_RC4, mcRC4Params },
263176
- #endif
263177
- { "", CODEC_TYPE_UNKNOWN, NULL }
263178
- };
263375
+ char m_name[CIPHER_NAME_MAXLEN];
263376
+ } CipherName;
263377
+
263378
+ static int globalCipherCount = 0;
263379
+ static char* globalSentinelName = "";
263380
+ static CipherName globalCipherNameTable[CODEC_COUNT_LIMIT + 2] = { 0 };
263381
+ static CodecParameter globalCodecParameterTable[CODEC_COUNT_LIMIT + 2];
263179
263382
 
263180
263383
  SQLITE_PRIVATE CodecParameter*
263181
263384
  sqlite3mcCloneCodecParameterTable()
@@ -263187,10 +263390,10 @@ sqlite3mcCloneCodecParameterTable()
263187
263390
  CipherParams* cloneCipherParams;
263188
263391
  CodecParameter* cloneCodecParams;
263189
263392
 
263190
- for (j = 0; strlen(globalCodecParameterTable[j].m_name) > 0; ++j)
263393
+ for (j = 0; globalCodecParameterTable[j].m_name[0] != 0; ++j)
263191
263394
  {
263192
263395
  CipherParams* params = globalCodecParameterTable[j].m_params;
263193
- for (k = 0; strlen(params[k].m_name) > 0; ++k);
263396
+ for (k = 0; params[k].m_name[0] != 0; ++k);
263194
263397
  nParams += k;
263195
263398
  }
263196
263399
  nTables = j;
@@ -263209,7 +263412,7 @@ sqlite3mcCloneCodecParameterTable()
263209
263412
  cloneCodecParams[j].m_name = globalCodecParameterTable[j].m_name;
263210
263413
  cloneCodecParams[j].m_id = globalCodecParameterTable[j].m_id;
263211
263414
  cloneCodecParams[j].m_params = &cloneCipherParams[offset];
263212
- for (n = 0; strlen(params[n].m_name) > 0; ++n);
263415
+ for (n = 0; params[n].m_name[0] != 0; ++n);
263213
263416
  /* Copy all parameters of the current table (including sentinel) */
263214
263417
  for (k = 0; k <= n; ++k)
263215
263418
  {
@@ -263249,35 +263452,7 @@ static const CipherDescriptor mcDummyDescriptor =
263249
263452
  "@dummy@", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
263250
263453
  };
263251
263454
 
263252
- static const CipherDescriptor* codecDescriptorTable[] =
263253
- {
263254
- #if HAVE_CIPHER_AES_128_CBC
263255
- &mcAES128Descriptor,
263256
- #else
263257
- &mcDummyDescriptor,
263258
- #endif
263259
- #if HAVE_CIPHER_AES_256_CBC
263260
- &mcAES256Descriptor,
263261
- #else
263262
- &mcDummyDescriptor,
263263
- #endif
263264
- #if HAVE_CIPHER_CHACHA20
263265
- &mcChaCha20Descriptor,
263266
- #else
263267
- &mcDummyDescriptor,
263268
- #endif
263269
- #if HAVE_CIPHER_SQLCIPHER
263270
- &mcSQLCipherDescriptor,
263271
- #else
263272
- &mcDummyDescriptor,
263273
- #endif
263274
- #if HAVE_CIPHER_RC4
263275
- &mcRC4Descriptor,
263276
- #else
263277
- &mcDummyDescriptor,
263278
- #endif
263279
- &mcSentinelDescriptor
263280
- };
263455
+ static CipherDescriptor globalCodecDescriptorTable[CODEC_COUNT_MAX + 1];
263281
263456
 
263282
263457
  /* --- Codec --- */
263283
263458
 
@@ -263291,11 +263466,11 @@ sqlite3mcGetCipherType(sqlite3* db)
263291
263466
  CipherParams* cipherParamTable = (codecParams != NULL) ? codecParams[0].m_params : commonParams;
263292
263467
  int cipherType = CODEC_TYPE;
263293
263468
  CipherParams* cipher = cipherParamTable;
263294
- for (; strlen(cipher->m_name) > 0; ++cipher)
263469
+ for (; cipher->m_name[0] != 0; ++cipher)
263295
263470
  {
263296
263471
  if (sqlite3_stricmp("cipher", cipher->m_name) == 0) break;
263297
263472
  }
263298
- if (strlen(cipher->m_name) > 0)
263473
+ if (cipher->m_name[0] != 0)
263299
263474
  {
263300
263475
  cipherType = cipher->m_value;
263301
263476
  cipher->m_value = cipher->m_default;
@@ -263304,19 +263479,20 @@ sqlite3mcGetCipherType(sqlite3* db)
263304
263479
  }
263305
263480
 
263306
263481
  SQLITE_PRIVATE CipherParams*
263307
- sqlite3mcGetCipherParams(sqlite3* db, int cypherType)
263482
+ sqlite3mcGetCipherParams(sqlite3* db, const char* cipherName)
263308
263483
  {
263309
263484
  int j = 0;
263485
+ int cipherType = sqlite3mc_cipher_index(cipherName);
263310
263486
  CodecParameter* codecParams = (db != NULL) ? sqlite3mcGetCodecParams(db) : globalCodecParameterTable;
263311
263487
  if (codecParams == NULL)
263312
263488
  {
263313
263489
  codecParams = globalCodecParameterTable;
263314
263490
  }
263315
- if (cypherType > 0)
263491
+ if (cipherType > 0)
263316
263492
  {
263317
263493
  for (j = 1; codecParams[j].m_id > 0; ++j)
263318
263494
  {
263319
- if (cypherType == codecParams[j].m_id) break;
263495
+ if (cipherType == codecParams[j].m_id) break;
263320
263496
  }
263321
263497
  }
263322
263498
  CipherParams* cipherParamTable = codecParams[j].m_params;
@@ -263366,12 +263542,12 @@ sqlite3mcCodecTerm(Codec* codec)
263366
263542
  {
263367
263543
  if (codec->m_readCipher != NULL)
263368
263544
  {
263369
- codecDescriptorTable[codec->m_readCipherType - 1]->m_freeCipher(codec->m_readCipher);
263545
+ globalCodecDescriptorTable[codec->m_readCipherType - 1].m_freeCipher(codec->m_readCipher);
263370
263546
  codec->m_readCipher = NULL;
263371
263547
  }
263372
263548
  if (codec->m_writeCipher != NULL)
263373
263549
  {
263374
- codecDescriptorTable[codec->m_writeCipherType - 1]->m_freeCipher(codec->m_writeCipher);
263550
+ globalCodecDescriptorTable[codec->m_writeCipherType - 1].m_freeCipher(codec->m_writeCipher);
263375
263551
  codec->m_writeCipher = NULL;
263376
263552
  }
263377
263553
  memset(codec, 0, sizeof(Codec));
@@ -263388,14 +263564,14 @@ SQLITE_PRIVATE int
263388
263564
  sqlite3mcCodecSetup(Codec* codec, int cipherType, char* userPassword, int passwordLength)
263389
263565
  {
263390
263566
  int rc = SQLITE_OK;
263391
- CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, 0);
263567
+ CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, CIPHER_NAME_GLOBAL);
263392
263568
  codec->m_isEncrypted = 1;
263393
263569
  codec->m_hmacCheck = sqlite3mcGetCipherParameter(globalParams, "hmac_check");
263394
263570
  codec->m_walLegacy = sqlite3mcGetCipherParameter(globalParams, "mc_legacy_wal");
263395
263571
  codec->m_hasReadCipher = 1;
263396
263572
  codec->m_hasWriteCipher = 1;
263397
263573
  codec->m_readCipherType = cipherType;
263398
- codec->m_readCipher = codecDescriptorTable[codec->m_readCipherType-1]->m_allocateCipher(codec->m_db);
263574
+ codec->m_readCipher = globalCodecDescriptorTable[codec->m_readCipherType-1].m_allocateCipher(codec->m_db);
263399
263575
  if (codec->m_readCipher != NULL)
263400
263576
  {
263401
263577
  unsigned char* keySalt = (codec->m_hasKeySalt != 0) ? codec->m_keySalt : NULL;
@@ -263413,17 +263589,17 @@ SQLITE_PRIVATE int
263413
263589
  sqlite3mcSetupWriteCipher(Codec* codec, int cipherType, char* userPassword, int passwordLength)
263414
263590
  {
263415
263591
  int rc = SQLITE_OK;
263416
- CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, 0);
263592
+ CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, CIPHER_NAME_GLOBAL);
263417
263593
  if (codec->m_writeCipher != NULL)
263418
263594
  {
263419
- codecDescriptorTable[codec->m_writeCipherType-1]->m_freeCipher(codec->m_writeCipher);
263595
+ globalCodecDescriptorTable[codec->m_writeCipherType-1].m_freeCipher(codec->m_writeCipher);
263420
263596
  }
263421
263597
  codec->m_isEncrypted = 1;
263422
263598
  codec->m_hmacCheck = sqlite3mcGetCipherParameter(globalParams, "hmac_check");
263423
263599
  codec->m_walLegacy = sqlite3mcGetCipherParameter(globalParams, "mc_legacy_wal");
263424
263600
  codec->m_hasWriteCipher = 1;
263425
263601
  codec->m_writeCipherType = cipherType;
263426
- codec->m_writeCipher = codecDescriptorTable[codec->m_writeCipherType-1]->m_allocateCipher(codec->m_db);
263602
+ codec->m_writeCipher = globalCodecDescriptorTable[codec->m_writeCipherType-1].m_allocateCipher(codec->m_db);
263427
263603
  if (codec->m_writeCipher != NULL)
263428
263604
  {
263429
263605
  unsigned char* keySalt = (codec->m_hasKeySalt != 0) ? codec->m_keySalt : NULL;
@@ -263544,57 +263720,57 @@ sqlite3mcGetPageBuffer(Codec* codec)
263544
263720
  SQLITE_PRIVATE int
263545
263721
  sqlite3mcGetLegacyReadCipher(Codec* codec)
263546
263722
  {
263547
- int legacy = (codec->m_hasReadCipher && codec->m_readCipher != NULL) ? codecDescriptorTable[codec->m_readCipherType - 1]->m_getLegacy(codec->m_readCipher) : 0;
263723
+ int legacy = (codec->m_hasReadCipher && codec->m_readCipher != NULL) ? globalCodecDescriptorTable[codec->m_readCipherType - 1].m_getLegacy(codec->m_readCipher) : 0;
263548
263724
  return legacy;
263549
263725
  }
263550
263726
 
263551
263727
  SQLITE_PRIVATE int
263552
263728
  sqlite3mcGetLegacyWriteCipher(Codec* codec)
263553
263729
  {
263554
- int legacy = (codec->m_hasWriteCipher && codec->m_writeCipher != NULL) ? codecDescriptorTable[codec->m_writeCipherType - 1]->m_getLegacy(codec->m_writeCipher) : -1;
263730
+ int legacy = (codec->m_hasWriteCipher && codec->m_writeCipher != NULL) ? globalCodecDescriptorTable[codec->m_writeCipherType - 1].m_getLegacy(codec->m_writeCipher) : -1;
263555
263731
  return legacy;
263556
263732
  }
263557
263733
 
263558
263734
  SQLITE_PRIVATE int
263559
263735
  sqlite3mcGetPageSizeReadCipher(Codec* codec)
263560
263736
  {
263561
- int pageSize = (codec->m_hasReadCipher && codec->m_readCipher != NULL) ? codecDescriptorTable[codec->m_readCipherType - 1]->m_getPageSize(codec->m_readCipher) : 0;
263737
+ int pageSize = (codec->m_hasReadCipher && codec->m_readCipher != NULL) ? globalCodecDescriptorTable[codec->m_readCipherType - 1].m_getPageSize(codec->m_readCipher) : 0;
263562
263738
  return pageSize;
263563
263739
  }
263564
263740
 
263565
263741
  SQLITE_PRIVATE int
263566
263742
  sqlite3mcGetPageSizeWriteCipher(Codec* codec)
263567
263743
  {
263568
- int pageSize = (codec->m_hasWriteCipher && codec->m_writeCipher != NULL) ? codecDescriptorTable[codec->m_writeCipherType - 1]->m_getPageSize(codec->m_writeCipher) : -1;
263744
+ int pageSize = (codec->m_hasWriteCipher && codec->m_writeCipher != NULL) ? globalCodecDescriptorTable[codec->m_writeCipherType - 1].m_getPageSize(codec->m_writeCipher) : -1;
263569
263745
  return pageSize;
263570
263746
  }
263571
263747
 
263572
263748
  SQLITE_PRIVATE int
263573
263749
  sqlite3mcGetReservedReadCipher(Codec* codec)
263574
263750
  {
263575
- int reserved = (codec->m_hasReadCipher && codec->m_readCipher != NULL) ? codecDescriptorTable[codec->m_readCipherType-1]->m_getReserved(codec->m_readCipher) : -1;
263751
+ int reserved = (codec->m_hasReadCipher && codec->m_readCipher != NULL) ? globalCodecDescriptorTable[codec->m_readCipherType-1].m_getReserved(codec->m_readCipher) : -1;
263576
263752
  return reserved;
263577
263753
  }
263578
263754
 
263579
263755
  SQLITE_PRIVATE int
263580
263756
  sqlite3mcGetReservedWriteCipher(Codec* codec)
263581
263757
  {
263582
- int reserved = (codec->m_hasWriteCipher && codec->m_writeCipher != NULL) ? codecDescriptorTable[codec->m_writeCipherType-1]->m_getReserved(codec->m_writeCipher) : -1;
263758
+ int reserved = (codec->m_hasWriteCipher && codec->m_writeCipher != NULL) ? globalCodecDescriptorTable[codec->m_writeCipherType-1].m_getReserved(codec->m_writeCipher) : -1;
263583
263759
  return reserved;
263584
263760
  }
263585
263761
 
263586
263762
  SQLITE_PRIVATE int
263587
263763
  sqlite3mcReservedEqual(Codec* codec)
263588
263764
  {
263589
- int readReserved = (codec->m_hasReadCipher && codec->m_readCipher != NULL) ? codecDescriptorTable[codec->m_readCipherType-1]->m_getReserved(codec->m_readCipher) : -1;
263590
- int writeReserved = (codec->m_hasWriteCipher && codec->m_writeCipher != NULL) ? codecDescriptorTable[codec->m_writeCipherType-1]->m_getReserved(codec->m_writeCipher) : -1;
263765
+ int readReserved = (codec->m_hasReadCipher && codec->m_readCipher != NULL) ? globalCodecDescriptorTable[codec->m_readCipherType-1].m_getReserved(codec->m_readCipher) : -1;
263766
+ int writeReserved = (codec->m_hasWriteCipher && codec->m_writeCipher != NULL) ? globalCodecDescriptorTable[codec->m_writeCipherType-1].m_getReserved(codec->m_writeCipher) : -1;
263591
263767
  return (readReserved == writeReserved);
263592
263768
  }
263593
263769
 
263594
263770
  SQLITE_PRIVATE unsigned char*
263595
263771
  sqlite3mcGetSaltWriteCipher(Codec* codec)
263596
263772
  {
263597
- unsigned char* salt = (codec->m_hasWriteCipher && codec->m_writeCipher != NULL) ? codecDescriptorTable[codec->m_writeCipherType - 1]->m_getSalt(codec->m_writeCipher) : NULL;
263773
+ unsigned char* salt = (codec->m_hasWriteCipher && codec->m_writeCipher != NULL) ? globalCodecDescriptorTable[codec->m_writeCipherType - 1].m_getSalt(codec->m_writeCipher) : NULL;
263598
263774
  return salt;
263599
263775
  }
263600
263776
 
@@ -263616,10 +263792,10 @@ sqlite3mcCodecCopy(Codec* codec, Codec* other)
263616
263792
 
263617
263793
  if (codec->m_hasReadCipher)
263618
263794
  {
263619
- codec->m_readCipher = codecDescriptorTable[codec->m_readCipherType - 1]->m_allocateCipher(codec->m_db);
263795
+ codec->m_readCipher = globalCodecDescriptorTable[codec->m_readCipherType - 1].m_allocateCipher(codec->m_db);
263620
263796
  if (codec->m_readCipher != NULL)
263621
263797
  {
263622
- codecDescriptorTable[codec->m_readCipherType - 1]->m_cloneCipher(codec->m_readCipher, other->m_readCipher);
263798
+ globalCodecDescriptorTable[codec->m_readCipherType - 1].m_cloneCipher(codec->m_readCipher, other->m_readCipher);
263623
263799
  }
263624
263800
  else
263625
263801
  {
@@ -263629,10 +263805,10 @@ sqlite3mcCodecCopy(Codec* codec, Codec* other)
263629
263805
 
263630
263806
  if (codec->m_hasWriteCipher)
263631
263807
  {
263632
- codec->m_writeCipher = codecDescriptorTable[codec->m_writeCipherType - 1]->m_allocateCipher(codec->m_db);
263808
+ codec->m_writeCipher = globalCodecDescriptorTable[codec->m_writeCipherType - 1].m_allocateCipher(codec->m_db);
263633
263809
  if (codec->m_writeCipher != NULL)
263634
263810
  {
263635
- codecDescriptorTable[codec->m_writeCipherType - 1]->m_cloneCipher(codec->m_writeCipher, other->m_writeCipher);
263811
+ globalCodecDescriptorTable[codec->m_writeCipherType - 1].m_cloneCipher(codec->m_writeCipher, other->m_writeCipher);
263636
263812
  }
263637
263813
  else
263638
263814
  {
@@ -263655,17 +263831,17 @@ sqlite3mcCopyCipher(Codec* codec, int read2write)
263655
263831
  {
263656
263832
  if (codec->m_writeCipher != NULL && codec->m_writeCipherType != codec->m_readCipherType)
263657
263833
  {
263658
- codecDescriptorTable[codec->m_writeCipherType-1]->m_freeCipher(codec->m_writeCipher);
263834
+ globalCodecDescriptorTable[codec->m_writeCipherType-1].m_freeCipher(codec->m_writeCipher);
263659
263835
  codec->m_writeCipher = NULL;
263660
263836
  }
263661
263837
  if (codec->m_writeCipher == NULL)
263662
263838
  {
263663
263839
  codec->m_writeCipherType = codec->m_readCipherType;
263664
- codec->m_writeCipher = codecDescriptorTable[codec->m_writeCipherType-1]->m_allocateCipher(codec->m_db);
263840
+ codec->m_writeCipher = globalCodecDescriptorTable[codec->m_writeCipherType-1].m_allocateCipher(codec->m_db);
263665
263841
  }
263666
263842
  if (codec->m_writeCipher != NULL)
263667
263843
  {
263668
- codecDescriptorTable[codec->m_writeCipherType-1]->m_cloneCipher(codec->m_writeCipher, codec->m_readCipher);
263844
+ globalCodecDescriptorTable[codec->m_writeCipherType-1].m_cloneCipher(codec->m_writeCipher, codec->m_readCipher);
263669
263845
  }
263670
263846
  else
263671
263847
  {
@@ -263676,17 +263852,17 @@ sqlite3mcCopyCipher(Codec* codec, int read2write)
263676
263852
  {
263677
263853
  if (codec->m_readCipher != NULL && codec->m_readCipherType != codec->m_writeCipherType)
263678
263854
  {
263679
- codecDescriptorTable[codec->m_readCipherType-1]->m_freeCipher(codec->m_readCipher);
263855
+ globalCodecDescriptorTable[codec->m_readCipherType-1].m_freeCipher(codec->m_readCipher);
263680
263856
  codec->m_readCipher = NULL;
263681
263857
  }
263682
263858
  if (codec->m_readCipher == NULL)
263683
263859
  {
263684
263860
  codec->m_readCipherType = codec->m_writeCipherType;
263685
- codec->m_readCipher = codecDescriptorTable[codec->m_readCipherType-1]->m_allocateCipher(codec->m_db);
263861
+ codec->m_readCipher = globalCodecDescriptorTable[codec->m_readCipherType-1].m_allocateCipher(codec->m_db);
263686
263862
  }
263687
263863
  if (codec->m_readCipher != NULL)
263688
263864
  {
263689
- codecDescriptorTable[codec->m_readCipherType-1]->m_cloneCipher(codec->m_readCipher, codec->m_writeCipher);
263865
+ globalCodecDescriptorTable[codec->m_readCipherType-1].m_cloneCipher(codec->m_readCipher, codec->m_writeCipher);
263690
263866
  }
263691
263867
  else
263692
263868
  {
@@ -263717,13 +263893,13 @@ sqlite3mcPadPassword(char* password, int pswdlen, unsigned char pswd[32])
263717
263893
  SQLITE_PRIVATE void
263718
263894
  sqlite3mcGenerateReadKey(Codec* codec, char* userPassword, int passwordLength, unsigned char* cipherSalt)
263719
263895
  {
263720
- codecDescriptorTable[codec->m_readCipherType-1]->m_generateKey(codec->m_readCipher, codec->m_btShared, userPassword, passwordLength, 0, cipherSalt);
263896
+ globalCodecDescriptorTable[codec->m_readCipherType-1].m_generateKey(codec->m_readCipher, codec->m_btShared, userPassword, passwordLength, 0, cipherSalt);
263721
263897
  }
263722
263898
 
263723
263899
  SQLITE_PRIVATE void
263724
263900
  sqlite3mcGenerateWriteKey(Codec* codec, char* userPassword, int passwordLength, unsigned char* cipherSalt)
263725
263901
  {
263726
- codecDescriptorTable[codec->m_writeCipherType-1]->m_generateKey(codec->m_writeCipher, codec->m_btShared, userPassword, passwordLength, 1, cipherSalt);
263902
+ globalCodecDescriptorTable[codec->m_writeCipherType-1].m_generateKey(codec->m_writeCipher, codec->m_btShared, userPassword, passwordLength, 1, cipherSalt);
263727
263903
  }
263728
263904
 
263729
263905
  SQLITE_PRIVATE int
@@ -263733,7 +263909,7 @@ sqlite3mcEncrypt(Codec* codec, int page, unsigned char* data, int len, int useWr
263733
263909
  void* cipher = (useWriteKey) ? codec->m_writeCipher : codec->m_readCipher;
263734
263910
  int reserved = (useWriteKey) ? (codec->m_writeReserved >= 0) ? codec->m_writeReserved : codec->m_reserved
263735
263911
  : (codec->m_readReserved >= 0) ? codec->m_readReserved : codec->m_reserved;
263736
- return codecDescriptorTable[cipherType-1]->m_encryptPage(cipher, page, data, len, reserved);
263912
+ return globalCodecDescriptorTable[cipherType-1].m_encryptPage(cipher, page, data, len, reserved);
263737
263913
  }
263738
263914
 
263739
263915
  SQLITE_PRIVATE int
@@ -263742,7 +263918,7 @@ sqlite3mcDecrypt(Codec* codec, int page, unsigned char* data, int len)
263742
263918
  int cipherType = codec->m_readCipherType;
263743
263919
  void* cipher = codec->m_readCipher;
263744
263920
  int reserved = (codec->m_readReserved >= 0) ? codec->m_readReserved : codec->m_reserved;
263745
- return codecDescriptorTable[cipherType-1]->m_decryptPage(cipher, page, data, len, reserved, codec->m_hmacCheck);
263921
+ return globalCodecDescriptorTable[cipherType-1].m_decryptPage(cipher, page, data, len, reserved, codec->m_hmacCheck);
263746
263922
  }
263747
263923
 
263748
263924
  #if HAVE_CIPHER_SQLCIPHER
@@ -263781,7 +263957,7 @@ sqlite3mcConfigureSQLCipherVersion(sqlite3* db, int configDefault, int legacyVer
263781
263957
  ** Purpose: Configuration of SQLite codecs
263782
263958
  ** Author: Ulrich Telle
263783
263959
  ** Created: 2020-03-02
263784
- ** Copyright: (c) 2006-2021 Ulrich Telle
263960
+ ** Copyright: (c) 2006-2022 Ulrich Telle
263785
263961
  ** License: MIT
263786
263962
  */
263787
263963
 
@@ -263825,6 +264001,8 @@ SQLITE_PRIVATE int sqlite3mcHandleMainKey(sqlite3* db, const char* zPath);
263825
264001
 
263826
264002
  /* --- Codec --- */
263827
264003
 
264004
+ SQLITE_PRIVATE int
264005
+ sqlite3mcGetGlobalCipherCount();
263828
264006
 
263829
264007
  SQLITE_PRIVATE Codec*
263830
264008
  sqlite3mcGetCodec(sqlite3* db, const char* zDbName);
@@ -263893,28 +264071,30 @@ sqlite3mc_config(sqlite3* db, const char* paramName, int newValue)
263893
264071
  }
263894
264072
 
263895
264073
  param = codecParams[0].m_params;
263896
- for (; strlen(param->m_name) > 0; ++param)
264074
+ for (; param->m_name[0] != 0; ++param)
263897
264075
  {
263898
264076
  if (sqlite3_stricmp(paramName, param->m_name) == 0) break;
263899
264077
  }
263900
- if (strlen(param->m_name) > 0)
264078
+ if (param->m_name[0] != 0)
263901
264079
  {
264080
+ int cipherCount = sqlite3mcGetGlobalCipherCount();
263902
264081
  if (db != NULL)
263903
264082
  {
263904
264083
  sqlite3_mutex_enter(db->mutex);
263905
264084
  }
263906
264085
  else
263907
264086
  {
263908
- sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
264087
+ sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN));
263909
264088
  }
263910
264089
  value = (hasDefaultPrefix) ? param->m_default : (hasMinPrefix) ? param->m_minValue : (hasMaxPrefix) ? param->m_maxValue : param->m_value;
263911
264090
  if (!hasMinPrefix && !hasMaxPrefix && newValue >= 0 && newValue >= param->m_minValue && newValue <= param->m_maxValue)
263912
264091
  {
263913
264092
  int allowChange = 1;
264093
+
263914
264094
  /* Allow cipher change only if new cipher is actually available */
263915
264095
  if (sqlite3_stricmp(paramName, "cipher") == 0)
263916
264096
  {
263917
- allowChange = (codecDescriptorTable[newValue - 1] != &mcDummyDescriptor);
264097
+ allowChange = newValue > 0 && newValue <= cipherCount;
263918
264098
  }
263919
264099
 
263920
264100
  if (allowChange)
@@ -263934,12 +264114,52 @@ sqlite3mc_config(sqlite3* db, const char* paramName, int newValue)
263934
264114
  }
263935
264115
  else
263936
264116
  {
263937
- sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
264117
+ sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN));
263938
264118
  }
263939
264119
  }
263940
264120
  return value;
263941
264121
  }
263942
264122
 
264123
+ SQLITE_API int
264124
+ sqlite3mc_cipher_count()
264125
+ {
264126
+ return sqlite3mcGetGlobalCipherCount();
264127
+ }
264128
+
264129
+ SQLITE_API int
264130
+ sqlite3mc_cipher_index(const char* cipherName)
264131
+ {
264132
+ int count = sqlite3mcGetGlobalCipherCount();
264133
+ int j = 0;
264134
+ for (j = 0; j < count && globalCodecDescriptorTable[j].m_name[0] != 0; ++j)
264135
+ {
264136
+ if (sqlite3_stricmp(cipherName, globalCodecDescriptorTable[j].m_name) == 0) break;
264137
+ }
264138
+ return (j < count && globalCodecDescriptorTable[j].m_name[0] != 0) ? j + 1 : -1;
264139
+ }
264140
+
264141
+ SQLITE_API const char*
264142
+ sqlite3mc_cipher_name(int cipherIndex)
264143
+ {
264144
+ static char cipherName[CIPHER_NAME_MAXLEN] = "";
264145
+ int count = sqlite3mcGetGlobalCipherCount();
264146
+ int j = 0;
264147
+ cipherName[0] = '\0';
264148
+ if (cipherIndex > 0 && cipherIndex <= count)
264149
+ {
264150
+ for (j = 0; j < count && globalCodecDescriptorTable[j].m_name[0] != 0; ++j)
264151
+ {
264152
+ if (cipherIndex == j + 1) break;
264153
+ }
264154
+ if (j < count && globalCodecDescriptorTable[j].m_name[0] != 0)
264155
+ {
264156
+ strncpy(cipherName, globalCodecDescriptorTable[j].m_name, CIPHER_NAME_MAXLEN - 1);
264157
+ cipherName[CIPHER_NAME_MAXLEN - 1] = '\0';
264158
+ }
264159
+ }
264160
+ return cipherName;
264161
+ }
264162
+
263943
264163
  SQLITE_API int
263944
264164
  sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramName, int newValue)
263945
264165
  {
@@ -263971,11 +264191,11 @@ sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramNa
263971
264191
  return value;
263972
264192
  }
263973
264193
 
263974
- for (j = 0; strlen(codecParams[j].m_name) > 0; ++j)
264194
+ for (j = 0; codecParams[j].m_name[0] != 0; ++j)
263975
264195
  {
263976
264196
  if (sqlite3_stricmp(cipherName, codecParams[j].m_name) == 0) break;
263977
264197
  }
263978
- if (strlen(codecParams[j].m_name) > 0)
264198
+ if (codecParams[j].m_name[0] != 0)
263979
264199
  {
263980
264200
  cipherParamTable = codecParams[j].m_params;
263981
264201
  }
@@ -264025,11 +264245,11 @@ sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramNa
264025
264245
  }
264026
264246
  #endif
264027
264247
 
264028
- for (; strlen(param->m_name) > 0; ++param)
264248
+ for (; param->m_name[0] != 0; ++param)
264029
264249
  {
264030
264250
  if (sqlite3_stricmp(paramName, param->m_name) == 0) break;
264031
264251
  }
264032
- if (strlen(param->m_name) > 0)
264252
+ if (param->m_name[0] != 0)
264033
264253
  {
264034
264254
  if (db != NULL)
264035
264255
  {
@@ -264205,11 +264425,11 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264205
264425
 
264206
264426
  param1 = codecParams[0].m_params;
264207
264427
  cipherParamTable = NULL;
264208
- for (; strlen(param1->m_name) > 0; ++param1)
264428
+ for (; param1->m_name[0] != 0; ++param1)
264209
264429
  {
264210
264430
  if (sqlite3_stricmp(nameParam1, param1->m_name) == 0) break;
264211
264431
  }
264212
- isCommonParam1 = strlen(param1->m_name) > 0;
264432
+ isCommonParam1 = param1->m_name[0] != 0;
264213
264433
 
264214
264434
  /* Check first argument whether it is a cipher name, if it wasn't a common parameter */
264215
264435
  /* If the first argument is a cipher name, cipherParamTable will point to the corresponding cipher parameter table */
@@ -264218,11 +264438,11 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264218
264438
  if (!hasDefaultPrefix && !hasMinPrefix && !hasMaxPrefix)
264219
264439
  {
264220
264440
  int j = 0;
264221
- for (j = 0; strlen(codecParams[j].m_name) > 0; ++j)
264441
+ for (j = 0; codecParams[j].m_name[0] != 0; ++j)
264222
264442
  {
264223
264443
  if (sqlite3_stricmp(nameParam1, codecParams[j].m_name) == 0) break;
264224
264444
  }
264225
- isCipherParam1 = strlen(codecParams[j].m_name) > 0;
264445
+ isCipherParam1 = codecParams[j].m_name[0] != 0;
264226
264446
  if (isCipherParam1)
264227
264447
  {
264228
264448
  cipherParamTable = codecParams[j].m_params;
@@ -264244,7 +264464,7 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264244
264464
  int value = (hasDefaultPrefix) ? param1->m_default : (hasMinPrefix) ? param1->m_minValue : (hasMaxPrefix) ? param1->m_maxValue : param1->m_value;
264245
264465
  if (sqlite3_stricmp(nameParam1, "cipher") == 0)
264246
264466
  {
264247
- sqlite3_result_text(context, codecDescriptorTable[value - 1]->m_name, -1, SQLITE_STATIC);
264467
+ sqlite3_result_text(context, globalCodecDescriptorTable[value - 1].m_name, -1, SQLITE_STATIC);
264248
264468
  }
264249
264469
  else
264250
264470
  {
@@ -264257,7 +264477,7 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264257
264477
  int nParams = 0;
264258
264478
  int lenTotal = 0;
264259
264479
  int j;
264260
- for (j = 0; strlen(cipherParamTable[j].m_name) > 0; ++j)
264480
+ for (j = 0; cipherParamTable[j].m_name[0] != 0; ++j)
264261
264481
  {
264262
264482
  ++nParams;
264263
264483
  lenTotal += (int) strlen(cipherParamTable[j].m_name);
@@ -264303,18 +264523,18 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264303
264523
  {
264304
264524
  const char* nameCipher = (const char*)sqlite3_value_text(argv[1]);
264305
264525
  int j = 0;
264306
- for (j = 0; strlen(codecDescriptorTable[j]->m_name) > 0; ++j)
264526
+ for (j = 0; globalCodecDescriptorTable[j].m_name[0] != 0; ++j)
264307
264527
  {
264308
- if (sqlite3_stricmp(nameCipher, codecDescriptorTable[j]->m_name) == 0) break;
264528
+ if (sqlite3_stricmp(nameCipher, globalCodecDescriptorTable[j].m_name) == 0) break;
264309
264529
  }
264310
- if (strlen(codecDescriptorTable[j]->m_name) > 0)
264530
+ if (globalCodecDescriptorTable[j].m_name[0] != 0)
264311
264531
  {
264312
264532
  if (hasDefaultPrefix)
264313
264533
  {
264314
264534
  param1->m_default = j + 1;
264315
264535
  }
264316
264536
  param1->m_value = j + 1;
264317
- sqlite3_result_text(context, codecDescriptorTable[j]->m_name, -1, SQLITE_STATIC);
264537
+ sqlite3_result_text(context, globalCodecDescriptorTable[j].m_name, -1, SQLITE_STATIC);
264318
264538
  }
264319
264539
  else
264320
264540
  {
@@ -264376,7 +264596,7 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264376
264596
  hasMaxPrefix = 1;
264377
264597
  nameParam2 += 4;
264378
264598
  }
264379
- for (; strlen(param2->m_name) > 0; ++param2)
264599
+ for (; param2->m_name[0] != 0; ++param2)
264380
264600
  {
264381
264601
  if (sqlite3_stricmp(nameParam2, param2->m_name) == 0) break;
264382
264602
  }
@@ -264399,7 +264619,7 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264399
264619
  }
264400
264620
  #endif
264401
264621
 
264402
- if (strlen(param2->m_name) > 0)
264622
+ if (param2->m_name[0] != 0)
264403
264623
  {
264404
264624
  if (argc == 2)
264405
264625
  {
@@ -264463,13 +264683,13 @@ sqlite3mcConfigureFromUri(sqlite3* db, const char *zDbName, int configDefault)
264463
264683
  CipherParams* cipherParams = NULL;
264464
264684
 
264465
264685
  /* Try to locate the cipher name */
264466
- for (j = 1; strlen(globalCodecParameterTable[j].m_name) > 0; ++j)
264686
+ for (j = 1; globalCodecParameterTable[j].m_name[0] != 0; ++j)
264467
264687
  {
264468
264688
  if (sqlite3_stricmp(cipherName, globalCodecParameterTable[j].m_name) == 0) break;
264469
264689
  }
264470
264690
 
264471
264691
  /* j is the index of the cipher name, if found */
264472
- cipherParams = (strlen(globalCodecParameterTable[j].m_name) > 0) ? globalCodecParameterTable[j].m_params : NULL;
264692
+ cipherParams = (globalCodecParameterTable[j].m_name[0] != 0) ? globalCodecParameterTable[j].m_params : NULL;
264473
264693
  if (cipherParams != NULL)
264474
264694
  {
264475
264695
  /*
@@ -264509,7 +264729,7 @@ sqlite3mcConfigureFromUri(sqlite3* db, const char *zDbName, int configDefault)
264509
264729
  #endif
264510
264730
 
264511
264731
  /* Check all cipher specific parameters */
264512
- for (j = 0; strlen(cipherParams[j].m_name) > 0; ++j)
264732
+ for (j = 0; cipherParams[j].m_name[0] != 0; ++j)
264513
264733
  {
264514
264734
  if (skipLegacy && sqlite3_stricmp(cipherParams[j].m_name, "legacy") == 0) continue;
264515
264735
 
@@ -264586,15 +264806,15 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
264586
264806
  {
264587
264807
  int j = 1;
264588
264808
  /* Try to locate the cipher name */
264589
- for (j = 1; strlen(globalCodecParameterTable[j].m_name) > 0; ++j)
264809
+ for (j = 1; globalCodecParameterTable[j].m_name[0] != 0; ++j)
264590
264810
  {
264591
264811
  if (sqlite3_stricmp(pragmaValue, globalCodecParameterTable[j].m_name) == 0) break;
264592
264812
  }
264593
- cipherId = (strlen(globalCodecParameterTable[j].m_name) > 0) ? globalCodecParameterTable[j].m_id : CODEC_TYPE_UNKNOWN;
264813
+ cipherId = (globalCodecParameterTable[j].m_name[0] != 0) ? globalCodecParameterTable[j].m_id : CODEC_TYPE_UNKNOWN;
264594
264814
  }
264595
264815
 
264596
264816
  /* cipherId is the numeric id of the cipher name, if found */
264597
- if ((cipherId == -1) || (cipherId > 0 && cipherId <= CODEC_TYPE_MAX))
264817
+ if ((cipherId == -1) || (cipherId > 0 && cipherId <= CODEC_COUNT_MAX))
264598
264818
  {
264599
264819
  int value;
264600
264820
  if (configDefault)
@@ -264606,7 +264826,7 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
264606
264826
  value = sqlite3mc_config(db, "cipher", cipherId);
264607
264827
  }
264608
264828
  rc = SQLITE_OK;
264609
- ((char**)pArg)[0] = sqlite3_mprintf("%s", codecDescriptorTable[value - 1]->m_name);
264829
+ ((char**)pArg)[0] = sqlite3_mprintf("%s", globalCodecDescriptorTable[value - 1].m_name);
264610
264830
  }
264611
264831
  else
264612
264832
  {
@@ -264635,20 +264855,42 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
264635
264855
  {
264636
264856
  ((char**)pArg)[0] = sqlite3_mprintf("ok");
264637
264857
  }
264858
+ else
264859
+ {
264860
+ if (db->pErr)
264861
+ {
264862
+ const char* z = (const char*)sqlite3_value_text(db->pErr);
264863
+ if (z && sqlite3Strlen30(z) > 0)
264864
+ {
264865
+ ((char**)pArg)[0] = sqlite3_mprintf(z);
264866
+ }
264867
+ }
264868
+ }
264638
264869
  }
264639
264870
  else if (sqlite3StrICmp(pragmaName, "hexkey") == 0)
264640
264871
  {
264641
264872
  int nValue = sqlite3Strlen30(pragmaValue);
264642
- if (((nValue & 1) == 0) && (sqlite3mcIsHexKey(pragmaValue, nValue) != 0))
264873
+ if (((nValue & 1) == 0) && (sqlite3mcIsHexKey((const unsigned char*) pragmaValue, nValue) != 0))
264643
264874
  {
264644
- char* zHexKey = sqlite3_malloc(nValue/2);
264645
- sqlite3mcConvertHex2Bin(pragmaValue, nValue, zHexKey);
264875
+ unsigned char* zHexKey = sqlite3_malloc(nValue/2);
264876
+ sqlite3mcConvertHex2Bin((const unsigned char*) pragmaValue, nValue, zHexKey);
264646
264877
  rc = sqlite3_key_v2(db, zDbName, zHexKey, nValue/2);
264647
264878
  sqlite3_free(zHexKey);
264648
264879
  if (rc == SQLITE_OK)
264649
264880
  {
264650
264881
  ((char**)pArg)[0] = sqlite3_mprintf("ok");
264651
264882
  }
264883
+ else
264884
+ {
264885
+ if (db->pErr)
264886
+ {
264887
+ const char* z = (const char*)sqlite3_value_text(db->pErr);
264888
+ if (z && sqlite3Strlen30(z) > 0)
264889
+ {
264890
+ ((char**)pArg)[0] = sqlite3_mprintf(z);
264891
+ }
264892
+ }
264893
+ }
264652
264894
  }
264653
264895
  else
264654
264896
  {
@@ -264678,10 +264920,10 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
264678
264920
  else if (sqlite3StrICmp(pragmaName, "hexrekey") == 0)
264679
264921
  {
264680
264922
  int nValue = sqlite3Strlen30(pragmaValue);
264681
- if (((nValue & 1) == 0) && (sqlite3mcIsHexKey(pragmaValue, nValue) != 0))
264923
+ if (((nValue & 1) == 0) && (sqlite3mcIsHexKey((const unsigned char*) pragmaValue, nValue) != 0))
264682
264924
  {
264683
- char* zHexKey = sqlite3_malloc(nValue/2);
264684
- sqlite3mcConvertHex2Bin(pragmaValue, nValue, zHexKey);
264925
+ unsigned char* zHexKey = sqlite3_malloc(nValue/2);
264926
+ sqlite3mcConvertHex2Bin((const unsigned char*) pragmaValue, nValue, zHexKey);
264685
264927
  rc = sqlite3_rekey_v2(db, zDbName, zHexKey, nValue/2);
264686
264928
  sqlite3_free(zHexKey);
264687
264929
  if (rc == SQLITE_OK)
@@ -264717,21 +264959,21 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
264717
264959
  CipherParams* cipherParams = NULL;
264718
264960
 
264719
264961
  /* Try to locate the cipher name */
264720
- for (j = 1; strlen(globalCodecParameterTable[j].m_name) > 0; ++j)
264962
+ for (j = 1; globalCodecParameterTable[j].m_name[0] != 0; ++j)
264721
264963
  {
264722
264964
  if (cipher == globalCodecParameterTable[j].m_id) break;
264723
264965
  }
264724
264966
 
264725
264967
  /* j is the index of the cipher name, if found */
264726
- cipherParams = (strlen(globalCodecParameterTable[j].m_name) > 0) ? globalCodecParameterTable[j].m_params : NULL;
264968
+ cipherParams = (globalCodecParameterTable[j].m_name[0] != 0) ? globalCodecParameterTable[j].m_params : NULL;
264727
264969
  if (cipherParams != NULL)
264728
264970
  {
264729
264971
  const char* cipherName = globalCodecParameterTable[j].m_name;
264730
- for (j = 0; strlen(cipherParams[j].m_name) > 0; ++j)
264972
+ for (j = 0; cipherParams[j].m_name[0] != 0; ++j)
264731
264973
  {
264732
264974
  if (sqlite3_stricmp(pragmaName, cipherParams[j].m_name) == 0) break;
264733
264975
  }
264734
- if (strlen(cipherParams[j].m_name) > 0)
264976
+ if (cipherParams[j].m_name[0] != 0)
264735
264977
  {
264736
264978
  char* param = (configDefault) ? sqlite3_mprintf("default:%s", pragmaName) : pragmaName;
264737
264979
  if (isIntValue)
@@ -264932,7 +265174,7 @@ sqlite3mcBtreeSetPageSize(Btree* p, int pageSize, int nReserve, int iFix)
264932
265174
  ** Change 4: Call sqlite3mcBtreeSetPageSize instead of sqlite3BtreeSetPageSize for main database
264933
265175
  ** (sqlite3mcBtreeSetPageSize allows to reduce the number of reserved bytes)
264934
265176
  **
264935
- ** This code is generated by the script rekeyvacuum.sh from SQLite version 3.39.1 amalgamation.
265177
+ ** This code is generated by the script rekeyvacuum.sh from SQLite version 3.39.4 amalgamation.
264936
265178
  */
264937
265179
  SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3mcRunVacuumForRekey(
264938
265180
  char **pzErrMsg, /* Write error message here */
@@ -265493,6 +265735,11 @@ sqlite3_key_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265493
265735
  {
265494
265736
  int dbIndex;
265495
265737
  const char* dbFileName = sqlite3_db_filename(db, zDbName);
265738
+ if (dbFileName == NULL || dbFileName[0] == 0)
265739
+ {
265740
+ sqlite3ErrorWithMsg(db, rc, "Setting key not supported for in-memory or temporary databases.");
265741
+ return rc;
265742
+ }
265496
265743
  /* Configure cipher from URI parameters if requested */
265497
265744
  if (sqlite3FindFunction(db, "sqlite3mc_config_table", 0, SQLITE_UTF8, 0) == NULL)
265498
265745
  {
@@ -265513,7 +265760,7 @@ sqlite3_key_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265513
265760
  else
265514
265761
  {
265515
265762
  rc = SQLITE_ERROR;
265516
- sqlite3ErrorWithMsg(db, rc, "Key failed. Database '%s' not found.", zDbName);
265763
+ sqlite3ErrorWithMsg(db, rc, "Setting key failed. Database '%s' not found.", zDbName);
265517
265764
  }
265518
265765
  }
265519
265766
  return rc;
@@ -265540,7 +265787,12 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265540
265787
  dbIndex = (zDbName) ? sqlite3FindDbName(db, zDbName) : 0;
265541
265788
  if (dbIndex < 0)
265542
265789
  {
265543
- sqlite3ErrorWithMsg(db, rc, "Rekey failed. Database '%s' not found.", zDbName);
265790
+ sqlite3ErrorWithMsg(db, rc, "Rekeying failed. Database '%s' not found.", zDbName);
265791
+ return rc;
265792
+ }
265793
+ if (dbFileName == NULL || dbFileName[0] == 0)
265794
+ {
265795
+ sqlite3ErrorWithMsg(db, rc, "Rekeying not supported for in-memory or temporary databases.");
265544
265796
  return rc;
265545
265797
  }
265546
265798
  pBt = db->aDb[dbIndex].pBt;
@@ -265555,7 +265807,7 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265555
265807
 
265556
265808
  if (pagerUseWal(pPager))
265557
265809
  {
265558
- sqlite3ErrorWithMsg(db, rc, "Rekey is not supported in WAL journal mode.");
265810
+ sqlite3ErrorWithMsg(db, rc, "Rekeying is not supported in WAL journal mode.");
265559
265811
  return rc;
265560
265812
  }
265561
265813
 
@@ -265599,6 +265851,10 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265599
265851
  sqlite3mcSetReadReserved(codec, nReserved);
265600
265852
  sqlite3mcSetWriteReserved(codec, nReservedWriteCipher);
265601
265853
  rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, NULL, nReservedWriteCipher);
265854
+ if (rc != SQLITE_OK && err != NULL)
265855
+ {
265856
+ sqlite3ErrorWithMsg(db, rc, err);
265857
+ }
265602
265858
  goto leave_rekey;
265603
265859
  }
265604
265860
  }
@@ -265606,7 +265862,7 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265606
265862
  {
265607
265863
  /* Pagesize cannot be changed for an encrypted database */
265608
265864
  rc = SQLITE_ERROR;
265609
- sqlite3ErrorWithMsg(db, rc, "Rekey failed. Pagesize cannot be changed for an encrypted database.");
265865
+ sqlite3ErrorWithMsg(db, rc, "Rekeying failed. Pagesize cannot be changed for an encrypted database.");
265610
265866
  goto leave_rekey;
265611
265867
  }
265612
265868
  }
@@ -265627,6 +265883,10 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265627
265883
  sqlite3mcSetReadReserved(codec, nReserved);
265628
265884
  sqlite3mcSetWriteReserved(codec, 0);
265629
265885
  rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, NULL, 0);
265886
+ if (rc != SQLITE_OK && err != NULL)
265887
+ {
265888
+ sqlite3ErrorWithMsg(db, rc, err);
265889
+ }
265630
265890
  goto leave_rekey;
265631
265891
  }
265632
265892
  }
@@ -265648,6 +265908,10 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265648
265908
  sqlite3mcSetReadReserved(codec, nReserved);
265649
265909
  sqlite3mcSetWriteReserved(codec, nReservedWriteCipher);
265650
265910
  rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, NULL, nReservedWriteCipher);
265911
+ if (rc != SQLITE_OK && err != NULL)
265912
+ {
265913
+ sqlite3ErrorWithMsg(db, rc, err);
265914
+ }
265651
265915
  goto leave_rekey;
265652
265916
  }
265653
265917
  }
@@ -265655,14 +265919,14 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265655
265919
  {
265656
265920
  /* Pagesize cannot be changed for an encrypted database */
265657
265921
  rc = SQLITE_ERROR;
265658
- sqlite3ErrorWithMsg(db, rc, "Rekey failed. Pagesize cannot be changed for an encrypted database.");
265922
+ sqlite3ErrorWithMsg(db, rc, "Rekeying failed. Pagesize cannot be changed for an encrypted database.");
265659
265923
  goto leave_rekey;
265660
265924
  }
265661
265925
  }
265662
265926
  else
265663
265927
  {
265664
265928
  /* Setup of write cipher failed */
265665
- sqlite3ErrorWithMsg(db, rc, "Rekey failed. Setup of write cipher failed.");
265929
+ sqlite3ErrorWithMsg(db, rc, "Rekeying failed. Setup of write cipher failed.");
265666
265930
  goto leave_rekey;
265667
265931
  }
265668
265932
  }
@@ -288731,16 +288995,262 @@ sqlite3_extfunc_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *p
288731
288995
  }
288732
288996
  #endif
288733
288997
 
288998
+ static int
288999
+ mcCheckValidName(char* name)
289000
+ {
289001
+ size_t nl;
289002
+ if (!name)
289003
+ return SQLITE_ERROR;
289004
+
289005
+ /* Check for valid cipher name length */
289006
+ nl = strlen(name);
289007
+ if (nl < 1 || nl >= CIPHER_NAME_MAXLEN)
289008
+ return SQLITE_ERROR;
289009
+
289010
+ /* Check for already registered names */
289011
+ CipherName* cipherNameTable = &globalCipherNameTable[0];
289012
+ for (; cipherNameTable->m_name[0] != 0; ++cipherNameTable)
289013
+ {
289014
+ if (sqlite3_stricmp(name, cipherNameTable->m_name) == 0) break;
289015
+ }
289016
+ if (cipherNameTable->m_name[0] != 0)
289017
+ return SQLITE_ERROR;
289018
+
289019
+ /* Check for valid character set (1st char = alpha, rest = alpha-numeric or underscore) */
289020
+ if (sqlite3Isalpha(name[0]))
289021
+ {
289022
+ size_t j;
289023
+ for (j = 1; j < nl && (name[j] == '_' || sqlite3Isalnum(name[j])); ++j) {}
289024
+ if (j == nl)
289025
+ return SQLITE_OK;
289026
+ }
289027
+ return SQLITE_ERROR;
289028
+ }
289029
+
289030
+ SQLITE_PRIVATE int
289031
+ sqlite3mcGetGlobalCipherCount()
289032
+ {
289033
+ int cipherCount = 0;
289034
+ sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN));
289035
+ cipherCount = globalCipherCount;
289036
+ sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN));
289037
+ return cipherCount;
289038
+ }
289039
+
289040
+ static int
289041
+ sqlite3mcRegisterCipher(const CipherDescriptor* desc, const CipherParams* params, int makeDefault)
289042
+ {
289043
+ int rc;
289044
+ int np;
289045
+ CipherParams* cipherParams;
289046
+
289047
+ /* Sanity checks */
289048
+
289049
+ /* Cipher description AND parameter are required */
289050
+ if (!desc || !params)
289051
+ return SQLITE_ERROR;
289052
+
289053
+ /* ALL methods of the cipher descriptor need to be defined */
289054
+ if (!desc->m_name ||
289055
+ !desc->m_allocateCipher ||
289056
+ !desc->m_freeCipher ||
289057
+ !desc->m_cloneCipher ||
289058
+ !desc->m_getLegacy ||
289059
+ !desc->m_getPageSize ||
289060
+ !desc->m_getReserved ||
289061
+ !desc->m_getSalt ||
289062
+ !desc->m_generateKey ||
289063
+ !desc->m_encryptPage ||
289064
+ !desc->m_decryptPage)
289065
+ return SQLITE_ERROR;
289066
+
289067
+ /* Check for valid cipher name */
289068
+ if (mcCheckValidName(desc->m_name) != SQLITE_OK)
289069
+ return SQLITE_ERROR;
289070
+
289071
+ /* Check cipher parameters */
289072
+ for (np = 0; np < CIPHER_PARAMS_COUNT_MAX; ++np)
289073
+ {
289074
+ CipherParams entry = params[np];
289075
+ /* Check for sentinel parameter */
289076
+ if (entry.m_name == 0 || entry.m_name[0] == 0)
289077
+ break;
289078
+ /* Check for valid parameter name */
289079
+ if (mcCheckValidName(entry.m_name) != SQLITE_OK)
289080
+ return SQLITE_ERROR;
289081
+ /* Check for valid parameter specification */
289082
+ if (!(entry.m_minValue >= 0 && entry.m_maxValue >= 0 && entry.m_minValue <= entry.m_maxValue &&
289083
+ entry.m_value >= entry.m_minValue && entry.m_value <= entry.m_maxValue &&
289084
+ entry.m_default >= entry.m_minValue && entry.m_default <= entry.m_maxValue))
289085
+ return SQLITE_ERROR;
289086
+ }
289087
+
289088
+ /* Check for parameter count in valid range and valid sentinel parameter */
289089
+ if (np >= CIPHER_PARAMS_COUNT_MAX || params[np].m_name == 0)
289090
+ return SQLITE_ERROR;
289091
+
289092
+ /* Sanity checks were successful, now register cipher */
289093
+
289094
+ cipherParams = (CipherParams*) sqlite3_malloc((np+1) * sizeof(CipherParams));
289095
+ if (!cipherParams)
289096
+ return SQLITE_NOMEM;
289097
+
289098
+ sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN));
289099
+
289100
+ /* Check for */
289101
+ if (globalCipherCount < CODEC_COUNT_MAX)
289102
+ {
289103
+ int n;
289104
+ char* cipherName;
289105
+ ++globalCipherCount;
289106
+ cipherName = globalCipherNameTable[globalCipherCount].m_name;
289107
+ strcpy(cipherName, desc->m_name);
289108
+
289109
+ globalCodecDescriptorTable[globalCipherCount - 1] = *desc;
289110
+ globalCodecDescriptorTable[globalCipherCount - 1].m_name = cipherName;
289111
+
289112
+ globalCodecParameterTable[globalCipherCount].m_name = cipherName;
289113
+ globalCodecParameterTable[globalCipherCount].m_id = globalCipherCount;
289114
+ globalCodecParameterTable[globalCipherCount].m_params = cipherParams;
289115
+
289116
+ /* Copy parameters */
289117
+ for (n = 0; n < np; ++n)
289118
+ {
289119
+ cipherParams[n] = params[n];
289120
+ cipherParams[n].m_name = (char*) sqlite3_malloc(strlen(params[n].m_name) + 1);
289121
+ strcpy(cipherParams[n].m_name, params[n].m_name);
289122
+ }
289123
+ /* Add sentinel */
289124
+ cipherParams[n] = params[n];
289125
+ cipherParams[n].m_name = globalSentinelName;
289126
+
289127
+ /* Make cipher default, if requested */
289128
+ if (makeDefault)
289129
+ {
289130
+ CipherParams* param = globalCodecParameterTable[0].m_params;
289131
+ for (; param->m_name[0] != 0; ++param)
289132
+ {
289133
+ if (sqlite3_stricmp("cipher", param->m_name) == 0) break;
289134
+ }
289135
+ if (param->m_name[0] != 0)
289136
+ {
289137
+ param->m_value = param->m_default = globalCipherCount;
289138
+ }
289139
+ }
289140
+
289141
+ rc = SQLITE_OK;
289142
+ }
289143
+ else
289144
+ {
289145
+ rc = SQLITE_NOMEM;
289146
+ }
289147
+
289148
+ sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN));
289149
+
289150
+ return rc;
289151
+ }
289152
+
289153
+ SQLITE_API int
289154
+ sqlite3mc_register_cipher(const CipherDescriptor* desc, const CipherParams* params, int makeDefault)
289155
+ {
289156
+ int rc;
289157
+ #ifndef SQLITE_OMIT_AUTOINIT
289158
+ rc = sqlite3_initialize();
289159
+ if (rc) return rc;
289160
+ #endif
289161
+ return sqlite3mcRegisterCipher(desc, params, makeDefault);
289162
+ }
289163
+
289164
+ SQLITE_PRIVATE int
289165
+ sqlite3mcInitCipherTables()
289166
+ {
289167
+ size_t n;
289168
+
289169
+ /* Initialize cipher name table */
289170
+ strcpy(globalCipherNameTable[0].m_name, "global");
289171
+ for (n = 1; n < CODEC_COUNT_MAX + 2; ++n)
289172
+ {
289173
+ strcpy(globalCipherNameTable[n].m_name, "");
289174
+ }
289175
+
289176
+ /* Initialize cipher descriptor table */
289177
+ for (n = 0; n < CODEC_COUNT_MAX + 1; ++n)
289178
+ {
289179
+ globalCodecDescriptorTable[n] = mcSentinelDescriptor;
289180
+ }
289181
+
289182
+ /* Initialize cipher parameter table */
289183
+ globalCodecParameterTable[0] = globalCommonParams;
289184
+ for (n = 1; n < CODEC_COUNT_MAX + 2; ++n)
289185
+ {
289186
+ globalCodecParameterTable[n] = globalSentinelParams;
289187
+ }
289188
+
289189
+ return SQLITE_OK;
289190
+ }
289191
+
289192
+ SQLITE_PRIVATE void
289193
+ sqlite3mcTermCipherTables()
289194
+ {
289195
+ size_t n;
289196
+ for (n = CODEC_COUNT_MAX+1; n > 0; --n)
289197
+ {
289198
+ if (globalCodecParameterTable[n].m_name[0] != 0)
289199
+ {
289200
+ int k;
289201
+ CipherParams* params = globalCodecParameterTable[n].m_params;
289202
+ for (k = 0; params[k].m_name[0] != 0; ++k)
289203
+ {
289204
+ sqlite3_free(params[k].m_name);
289205
+ }
289206
+ sqlite3_free(globalCodecParameterTable[n].m_params);
289207
+ }
289208
+ }
289209
+ }
289210
+
288734
289211
  int
288735
289212
  sqlite3mc_initialize(const char* arg)
288736
289213
  {
288737
- int rc = SQLITE_OK;
289214
+ int rc = sqlite3mcInitCipherTables();
289215
+ #if HAVE_CIPHER_AES_128_CBC
289216
+ if (rc == SQLITE_OK)
289217
+ {
289218
+ rc = sqlite3mcRegisterCipher(&mcAES128Descriptor, mcAES128Params, (CODEC_TYPE_AES128 == CODEC_TYPE));
289219
+ }
289220
+ #endif
289221
+ #if HAVE_CIPHER_AES_256_CBC
289222
+ if (rc == SQLITE_OK)
289223
+ {
289224
+ rc = sqlite3mcRegisterCipher(&mcAES256Descriptor, mcAES256Params, (CODEC_TYPE_AES256 == CODEC_TYPE));
289225
+ }
289226
+ #endif
289227
+ #if HAVE_CIPHER_CHACHA20
289228
+ if (rc == SQLITE_OK)
289229
+ {
289230
+ rc = sqlite3mcRegisterCipher(&mcChaCha20Descriptor, mcChaCha20Params, (CODEC_TYPE_CHACHA20 == CODEC_TYPE));
289231
+ }
289232
+ #endif
289233
+ #if HAVE_CIPHER_SQLCIPHER
289234
+ if (rc == SQLITE_OK)
289235
+ {
289236
+ rc = sqlite3mcRegisterCipher(&mcSQLCipherDescriptor, mcSQLCipherParams, (CODEC_TYPE_SQLCIPHER == CODEC_TYPE));
289237
+ }
289238
+ #endif
289239
+ #if HAVE_CIPHER_RC4
289240
+ if (rc == SQLITE_OK)
289241
+ {
289242
+ rc = sqlite3mcRegisterCipher(&mcRC4Descriptor, mcRC4Params, (CODEC_TYPE_RC4 == CODEC_TYPE));
289243
+ }
289244
+ #endif
288738
289245
 
288739
289246
  /*
288740
289247
  ** Initialize and register MultiCipher VFS as default VFS
288741
289248
  ** if it isn't already registered
288742
289249
  */
288743
- rc = sqlite3mc_vfs_create(NULL, 1);
289250
+ if (rc == SQLITE_OK)
289251
+ {
289252
+ rc = sqlite3mc_vfs_create(NULL, 1);
289253
+ }
288744
289254
 
288745
289255
  /*
288746
289256
  ** Register Multi Cipher extension
@@ -288828,6 +289338,7 @@ void
288828
289338
  sqlite3mc_shutdown(void)
288829
289339
  {
288830
289340
  sqlite3mc_vfs_shutdown();
289341
+ sqlite3mcTermCipherTables();
288831
289342
  }
288832
289343
 
288833
289344
  /*