better-sqlite3-multiple-ciphers 7.6.3-beta.0 → 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.2. 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.2"
548
- #define SQLITE_VERSION_NUMBER 3039002
549
- #define SQLITE_SOURCE_ID "2022-07-21 15:24:47 698edb77537b67c41adc68f9b892db56bcf9a55e00371a61420f3ddd668e6603"
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;
@@ -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
  ){
@@ -132808,6 +132873,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132808
132873
  **
132809
132874
  */
132810
132875
  case PragTyp_TEMP_STORE_DIRECTORY: {
132876
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132811
132877
  if( !zRight ){
132812
132878
  returnSingleText(v, sqlite3_temp_directory);
132813
132879
  }else{
@@ -132817,6 +132883,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132817
132883
  rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132818
132884
  if( rc!=SQLITE_OK || res==0 ){
132819
132885
  sqlite3ErrorMsg(pParse, "not a writable directory");
132886
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132820
132887
  goto pragma_out;
132821
132888
  }
132822
132889
  }
@@ -132834,6 +132901,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132834
132901
  }
132835
132902
  #endif /* SQLITE_OMIT_WSD */
132836
132903
  }
132904
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132837
132905
  break;
132838
132906
  }
132839
132907
 
@@ -132852,6 +132920,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132852
132920
  **
132853
132921
  */
132854
132922
  case PragTyp_DATA_STORE_DIRECTORY: {
132923
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132855
132924
  if( !zRight ){
132856
132925
  returnSingleText(v, sqlite3_data_directory);
132857
132926
  }else{
@@ -132861,6 +132930,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132861
132930
  rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132862
132931
  if( rc!=SQLITE_OK || res==0 ){
132863
132932
  sqlite3ErrorMsg(pParse, "not a writable directory");
132933
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132864
132934
  goto pragma_out;
132865
132935
  }
132866
132936
  }
@@ -132872,6 +132942,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
132872
132942
  }
132873
132943
  #endif /* SQLITE_OMIT_WSD */
132874
132944
  }
132945
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
132875
132946
  break;
132876
132947
  }
132877
132948
  #endif
@@ -137317,7 +137388,7 @@ static void generateSortTail(
137317
137388
  if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
137318
137389
  addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
137319
137390
  VdbeCoverage(v);
137320
- codeOffset(v, p->iOffset, addrContinue);
137391
+ assert( p->iLimit==0 && p->iOffset==0 );
137321
137392
  sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
137322
137393
  bSeq = 0;
137323
137394
  }else{
@@ -137325,6 +137396,9 @@ static void generateSortTail(
137325
137396
  codeOffset(v, p->iOffset, addrContinue);
137326
137397
  iSortTab = iTab;
137327
137398
  bSeq = 1;
137399
+ if( p->iOffset>0 ){
137400
+ sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
137401
+ }
137328
137402
  }
137329
137403
  for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
137330
137404
  #ifdef SQLITE_ENABLE_SORTER_REFERENCES
@@ -139317,10 +139391,11 @@ static int multiSelectOrderBy(
139317
139391
  */
139318
139392
  sqlite3VdbeResolveLabel(v, labelEnd);
139319
139393
 
139320
- /* Reassembly the compound query so that it will be freed correctly
139394
+ /* Reassemble the compound query so that it will be freed correctly
139321
139395
  ** by the calling function */
139322
139396
  if( pSplit->pPrior ){
139323
- sqlite3SelectDelete(db, pSplit->pPrior);
139397
+ sqlite3ParserAddCleanup(pParse,
139398
+ (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior);
139324
139399
  }
139325
139400
  pSplit->pPrior = pPrior;
139326
139401
  pPrior->pNext = pSplit;
@@ -140839,6 +140914,7 @@ static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
140839
140914
  || p->pSrc->nSrc!=1
140840
140915
  || p->pSrc->a[0].pSelect
140841
140916
  || pAggInfo->nFunc!=1
140917
+ || p->pHaving
140842
140918
  ){
140843
140919
  return 0;
140844
140920
  }
@@ -144076,6 +144152,23 @@ SQLITE_PRIVATE void sqlite3FinishTrigger(
144076
144152
  Vdbe *v;
144077
144153
  char *z;
144078
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
+
144079
144172
  /* Make an entry in the sqlite_schema table */
144080
144173
  v = sqlite3GetVdbe(pParse);
144081
144174
  if( v==0 ) goto triggerfinish_cleanup;
@@ -149887,7 +149980,8 @@ static int codeEqualityTerm(
149887
149980
  }
149888
149981
  sqlite3ExprDelete(db, pX);
149889
149982
  }else{
149890
- 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));
149891
149985
  eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap, &iTab);
149892
149986
  }
149893
149987
  pX = pExpr;
@@ -177045,7 +177139,7 @@ struct Fts3MultiSegReader {
177045
177139
  int nAdvance; /* How many seg-readers to advance */
177046
177140
  Fts3SegFilter *pFilter; /* Pointer to filter object */
177047
177141
  char *aBuffer; /* Buffer to merge doclists in */
177048
- int nBuffer; /* Allocated size of aBuffer[] in bytes */
177142
+ i64 nBuffer; /* Allocated size of aBuffer[] in bytes */
177049
177143
 
177050
177144
  int iColFilter; /* If >=0, filter for this column */
177051
177145
  int bRestart;
@@ -179741,7 +179835,7 @@ static int fts3TermSelectMerge(
179741
179835
  **
179742
179836
  ** Similar padding is added in the fts3DoclistOrMerge() function.
179743
179837
  */
179744
- pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
179838
+ pTS->aaOutput[0] = sqlite3_malloc64((i64)nDoclist + FTS3_VARINT_MAX + 1);
179745
179839
  pTS->anOutput[0] = nDoclist;
179746
179840
  if( pTS->aaOutput[0] ){
179747
179841
  memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
@@ -181229,7 +181323,7 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
181229
181323
  nDistance = iPrev - nMaxUndeferred;
181230
181324
  }
181231
181325
 
181232
- aOut = (char *)sqlite3_malloc(nPoslist+8);
181326
+ aOut = (char *)sqlite3Fts3MallocZero(nPoslist+FTS3_BUFFER_PADDING);
181233
181327
  if( !aOut ){
181234
181328
  sqlite3_free(aPoslist);
181235
181329
  return SQLITE_NOMEM;
@@ -181598,7 +181692,7 @@ static int fts3EvalIncrPhraseNext(
181598
181692
  if( bEof==0 ){
181599
181693
  int nList = 0;
181600
181694
  int nByte = a[p->nToken-1].nList;
181601
- char *aDoclist = sqlite3_malloc(nByte+FTS3_BUFFER_PADDING);
181695
+ char *aDoclist = sqlite3_malloc64((i64)nByte+FTS3_BUFFER_PADDING);
181602
181696
  if( !aDoclist ) return SQLITE_NOMEM;
181603
181697
  memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
181604
181698
  memset(&aDoclist[nByte], 0, FTS3_BUFFER_PADDING);
@@ -185834,7 +185928,7 @@ static int porterNext(
185834
185928
  if( n>c->nAllocated ){
185835
185929
  char *pNew;
185836
185930
  c->nAllocated = n+20;
185837
- pNew = sqlite3_realloc(c->zToken, c->nAllocated);
185931
+ pNew = sqlite3_realloc64(c->zToken, c->nAllocated);
185838
185932
  if( !pNew ) return SQLITE_NOMEM;
185839
185933
  c->zToken = pNew;
185840
185934
  }
@@ -186586,7 +186680,7 @@ static int simpleNext(
186586
186680
  if( n>c->nTokenAllocated ){
186587
186681
  char *pNew;
186588
186682
  c->nTokenAllocated = n+20;
186589
- pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
186683
+ pNew = sqlite3_realloc64(c->pToken, c->nTokenAllocated);
186590
186684
  if( !pNew ) return SQLITE_NOMEM;
186591
186685
  c->pToken = pNew;
186592
186686
  }
@@ -187748,7 +187842,7 @@ static int fts3PendingListAppendVarint(
187748
187842
 
187749
187843
  /* Allocate or grow the PendingList as required. */
187750
187844
  if( !p ){
187751
- p = sqlite3_malloc(sizeof(*p) + 100);
187845
+ p = sqlite3_malloc64(sizeof(*p) + 100);
187752
187846
  if( !p ){
187753
187847
  return SQLITE_NOMEM;
187754
187848
  }
@@ -187757,14 +187851,14 @@ static int fts3PendingListAppendVarint(
187757
187851
  p->nData = 0;
187758
187852
  }
187759
187853
  else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
187760
- int nNew = p->nSpace * 2;
187761
- p = sqlite3_realloc(p, sizeof(*p) + nNew);
187854
+ i64 nNew = p->nSpace * 2;
187855
+ p = sqlite3_realloc64(p, sizeof(*p) + nNew);
187762
187856
  if( !p ){
187763
187857
  sqlite3_free(*pp);
187764
187858
  *pp = 0;
187765
187859
  return SQLITE_NOMEM;
187766
187860
  }
187767
- p->nSpace = nNew;
187861
+ p->nSpace = (int)nNew;
187768
187862
  p->aData = (char *)&p[1];
187769
187863
  }
187770
187864
 
@@ -188321,7 +188415,7 @@ SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
188321
188415
  int nByte = sqlite3_blob_bytes(p->pSegments);
188322
188416
  *pnBlob = nByte;
188323
188417
  if( paBlob ){
188324
- char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
188418
+ char *aByte = sqlite3_malloc64((i64)nByte + FTS3_NODE_PADDING);
188325
188419
  if( !aByte ){
188326
188420
  rc = SQLITE_NOMEM;
188327
188421
  }else{
@@ -188438,7 +188532,7 @@ static int fts3SegReaderNext(
188438
188532
  int nTerm = fts3HashKeysize(pElem);
188439
188533
  if( (nTerm+1)>pReader->nTermAlloc ){
188440
188534
  sqlite3_free(pReader->zTerm);
188441
- pReader->zTerm = (char*)sqlite3_malloc((nTerm+1)*2);
188535
+ pReader->zTerm = (char*)sqlite3_malloc64(((i64)nTerm+1)*2);
188442
188536
  if( !pReader->zTerm ) return SQLITE_NOMEM;
188443
188537
  pReader->nTermAlloc = (nTerm+1)*2;
188444
188538
  }
@@ -188446,7 +188540,7 @@ static int fts3SegReaderNext(
188446
188540
  pReader->zTerm[nTerm] = '\0';
188447
188541
  pReader->nTerm = nTerm;
188448
188542
 
188449
- aCopy = (char*)sqlite3_malloc(nCopy);
188543
+ aCopy = (char*)sqlite3_malloc64(nCopy);
188450
188544
  if( !aCopy ) return SQLITE_NOMEM;
188451
188545
  memcpy(aCopy, pList->aData, nCopy);
188452
188546
  pReader->nNode = pReader->nDoclist = nCopy;
@@ -188733,7 +188827,7 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
188733
188827
  nExtra = nRoot + FTS3_NODE_PADDING;
188734
188828
  }
188735
188829
 
188736
- pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
188830
+ pReader = (Fts3SegReader *)sqlite3_malloc64(sizeof(Fts3SegReader) + nExtra);
188737
188831
  if( !pReader ){
188738
188832
  return SQLITE_NOMEM;
188739
188833
  }
@@ -188825,7 +188919,7 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
188825
188919
  if( nElem==nAlloc ){
188826
188920
  Fts3HashElem **aElem2;
188827
188921
  nAlloc += 16;
188828
- aElem2 = (Fts3HashElem **)sqlite3_realloc(
188922
+ aElem2 = (Fts3HashElem **)sqlite3_realloc64(
188829
188923
  aElem, nAlloc*sizeof(Fts3HashElem *)
188830
188924
  );
188831
188925
  if( !aElem2 ){
@@ -189159,7 +189253,7 @@ static int fts3NodeAddTerm(
189159
189253
  ** this is not expected to be a serious problem.
189160
189254
  */
189161
189255
  assert( pTree->aData==(char *)&pTree[1] );
189162
- pTree->aData = (char *)sqlite3_malloc(nReq);
189256
+ pTree->aData = (char *)sqlite3_malloc64(nReq);
189163
189257
  if( !pTree->aData ){
189164
189258
  return SQLITE_NOMEM;
189165
189259
  }
@@ -189177,7 +189271,7 @@ static int fts3NodeAddTerm(
189177
189271
 
189178
189272
  if( isCopyTerm ){
189179
189273
  if( pTree->nMalloc<nTerm ){
189180
- char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
189274
+ char *zNew = sqlite3_realloc64(pTree->zMalloc, (i64)nTerm*2);
189181
189275
  if( !zNew ){
189182
189276
  return SQLITE_NOMEM;
189183
189277
  }
@@ -189203,7 +189297,7 @@ static int fts3NodeAddTerm(
189203
189297
  ** now. Instead, the term is inserted into the parent of pTree. If pTree
189204
189298
  ** has no parent, one is created here.
189205
189299
  */
189206
- pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
189300
+ pNew = (SegmentNode *)sqlite3_malloc64(sizeof(SegmentNode) + p->nNodeSize);
189207
189301
  if( !pNew ){
189208
189302
  return SQLITE_NOMEM;
189209
189303
  }
@@ -189341,7 +189435,7 @@ static int fts3SegWriterAdd(
189341
189435
  ){
189342
189436
  int nPrefix; /* Size of term prefix in bytes */
189343
189437
  int nSuffix; /* Size of term suffix in bytes */
189344
- int nReq; /* Number of bytes required on leaf page */
189438
+ i64 nReq; /* Number of bytes required on leaf page */
189345
189439
  int nData;
189346
189440
  SegmentWriter *pWriter = *ppWriter;
189347
189441
 
@@ -189350,13 +189444,13 @@ static int fts3SegWriterAdd(
189350
189444
  sqlite3_stmt *pStmt;
189351
189445
 
189352
189446
  /* Allocate the SegmentWriter structure */
189353
- pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
189447
+ pWriter = (SegmentWriter *)sqlite3_malloc64(sizeof(SegmentWriter));
189354
189448
  if( !pWriter ) return SQLITE_NOMEM;
189355
189449
  memset(pWriter, 0, sizeof(SegmentWriter));
189356
189450
  *ppWriter = pWriter;
189357
189451
 
189358
189452
  /* Allocate a buffer in which to accumulate data */
189359
- pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
189453
+ pWriter->aData = (char *)sqlite3_malloc64(p->nNodeSize);
189360
189454
  if( !pWriter->aData ) return SQLITE_NOMEM;
189361
189455
  pWriter->nSize = p->nNodeSize;
189362
189456
 
@@ -189431,7 +189525,7 @@ static int fts3SegWriterAdd(
189431
189525
  ** the buffer to make it large enough.
189432
189526
  */
189433
189527
  if( nReq>pWriter->nSize ){
189434
- char *aNew = sqlite3_realloc(pWriter->aData, nReq);
189528
+ char *aNew = sqlite3_realloc64(pWriter->aData, nReq);
189435
189529
  if( !aNew ) return SQLITE_NOMEM;
189436
189530
  pWriter->aData = aNew;
189437
189531
  pWriter->nSize = nReq;
@@ -189456,7 +189550,7 @@ static int fts3SegWriterAdd(
189456
189550
  */
189457
189551
  if( isCopyTerm ){
189458
189552
  if( nTerm>pWriter->nMalloc ){
189459
- char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
189553
+ char *zNew = sqlite3_realloc64(pWriter->zMalloc, (i64)nTerm*2);
189460
189554
  if( !zNew ){
189461
189555
  return SQLITE_NOMEM;
189462
189556
  }
@@ -189764,12 +189858,12 @@ static void fts3ColumnFilter(
189764
189858
  static int fts3MsrBufferData(
189765
189859
  Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
189766
189860
  char *pList,
189767
- int nList
189861
+ i64 nList
189768
189862
  ){
189769
189863
  if( nList>pMsr->nBuffer ){
189770
189864
  char *pNew;
189771
189865
  pMsr->nBuffer = nList*2;
189772
- pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
189866
+ pNew = (char *)sqlite3_realloc64(pMsr->aBuffer, pMsr->nBuffer);
189773
189867
  if( !pNew ) return SQLITE_NOMEM;
189774
189868
  pMsr->aBuffer = pNew;
189775
189869
  }
@@ -189825,7 +189919,7 @@ SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
189825
189919
  fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
189826
189920
 
189827
189921
  if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
189828
- rc = fts3MsrBufferData(pMsr, pList, nList+1);
189922
+ rc = fts3MsrBufferData(pMsr, pList, (i64)nList+1);
189829
189923
  if( rc!=SQLITE_OK ) return rc;
189830
189924
  assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
189831
189925
  pList = pMsr->aBuffer;
@@ -189962,11 +190056,11 @@ SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
189962
190056
  return SQLITE_OK;
189963
190057
  }
189964
190058
 
189965
- static int fts3GrowSegReaderBuffer(Fts3MultiSegReader *pCsr, int nReq){
190059
+ static int fts3GrowSegReaderBuffer(Fts3MultiSegReader *pCsr, i64 nReq){
189966
190060
  if( nReq>pCsr->nBuffer ){
189967
190061
  char *aNew;
189968
190062
  pCsr->nBuffer = nReq*2;
189969
- aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
190063
+ aNew = sqlite3_realloc64(pCsr->aBuffer, pCsr->nBuffer);
189970
190064
  if( !aNew ){
189971
190065
  return SQLITE_NOMEM;
189972
190066
  }
@@ -190057,7 +190151,8 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
190057
190151
  ){
190058
190152
  pCsr->nDoclist = apSegment[0]->nDoclist;
190059
190153
  if( fts3SegReaderIsPending(apSegment[0]) ){
190060
- rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
190154
+ rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist,
190155
+ (i64)pCsr->nDoclist);
190061
190156
  pCsr->aDoclist = pCsr->aBuffer;
190062
190157
  }else{
190063
190158
  pCsr->aDoclist = apSegment[0]->aDoclist;
@@ -190110,7 +190205,8 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
190110
190205
 
190111
190206
  nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
190112
190207
 
190113
- rc = fts3GrowSegReaderBuffer(pCsr, nByte+nDoclist+FTS3_NODE_PADDING);
190208
+ rc = fts3GrowSegReaderBuffer(pCsr,
190209
+ (i64)nByte+nDoclist+FTS3_NODE_PADDING);
190114
190210
  if( rc ) return rc;
190115
190211
 
190116
190212
  if( isFirst ){
@@ -190136,7 +190232,7 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
190136
190232
  fts3SegReaderSort(apSegment, nMerge, j, xCmp);
190137
190233
  }
190138
190234
  if( nDoclist>0 ){
190139
- rc = fts3GrowSegReaderBuffer(pCsr, nDoclist+FTS3_NODE_PADDING);
190235
+ rc = fts3GrowSegReaderBuffer(pCsr, (i64)nDoclist+FTS3_NODE_PADDING);
190140
190236
  if( rc ) return rc;
190141
190237
  memset(&pCsr->aBuffer[nDoclist], 0, FTS3_NODE_PADDING);
190142
190238
  pCsr->aDoclist = pCsr->aBuffer;
@@ -190849,7 +190945,7 @@ struct NodeReader {
190849
190945
  static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
190850
190946
  if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
190851
190947
  int nAlloc = nMin;
190852
- char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
190948
+ char *a = (char *)sqlite3_realloc64(pBlob->a, nAlloc);
190853
190949
  if( a ){
190854
190950
  pBlob->nAlloc = nAlloc;
190855
190951
  pBlob->a = a;
@@ -191646,7 +191742,7 @@ static int fts3RepackSegdirLevel(
191646
191742
  if( nIdx>=nAlloc ){
191647
191743
  int *aNew;
191648
191744
  nAlloc += 16;
191649
- aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
191745
+ aNew = sqlite3_realloc64(aIdx, nAlloc*sizeof(int));
191650
191746
  if( !aNew ){
191651
191747
  rc = SQLITE_NOMEM;
191652
191748
  break;
@@ -192020,7 +192116,7 @@ SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
192020
192116
 
192021
192117
  /* Allocate space for the cursor, filter and writer objects */
192022
192118
  const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
192023
- pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
192119
+ pWriter = (IncrmergeWriter *)sqlite3_malloc64(nAlloc);
192024
192120
  if( !pWriter ) return SQLITE_NOMEM;
192025
192121
  pFilter = (Fts3SegFilter *)&pWriter[1];
192026
192122
  pCsr = (Fts3MultiSegReader *)&pFilter[1];
@@ -192656,7 +192752,7 @@ SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
192656
192752
  return SQLITE_OK;
192657
192753
  }
192658
192754
 
192659
- pRet = (char *)sqlite3_malloc(p->pList->nData);
192755
+ pRet = (char *)sqlite3_malloc64(p->pList->nData);
192660
192756
  if( !pRet ) return SQLITE_NOMEM;
192661
192757
 
192662
192758
  nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
@@ -192676,7 +192772,7 @@ SQLITE_PRIVATE int sqlite3Fts3DeferToken(
192676
192772
  int iCol /* Column that token must appear in (or -1) */
192677
192773
  ){
192678
192774
  Fts3DeferredToken *pDeferred;
192679
- pDeferred = sqlite3_malloc(sizeof(*pDeferred));
192775
+ pDeferred = sqlite3_malloc64(sizeof(*pDeferred));
192680
192776
  if( !pDeferred ){
192681
192777
  return SQLITE_NOMEM;
192682
192778
  }
@@ -204255,7 +204351,7 @@ static int geopolyUpdate(
204255
204351
  sqlite3_free(p);
204256
204352
  nChange = 1;
204257
204353
  }
204258
- for(jj=1; jj<pRtree->nAux; jj++){
204354
+ for(jj=1; jj<nData-2; jj++){
204259
204355
  nChange++;
204260
204356
  sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
204261
204357
  }
@@ -204858,8 +204954,9 @@ static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
204858
204954
 
204859
204955
  if( U_SUCCESS(status) ){
204860
204956
  sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
204861
- }else{
204862
- assert(!pExpr);
204957
+ pExpr = sqlite3_get_auxdata(p, 0);
204958
+ }
204959
+ if( !pExpr ){
204863
204960
  icuFunctionError(p, "uregex_open", status);
204864
204961
  return;
204865
204962
  }
@@ -236745,7 +236842,7 @@ static void fts5SourceIdFunc(
236745
236842
  ){
236746
236843
  assert( nArg==0 );
236747
236844
  UNUSED_PARAM2(nArg, apUnused);
236748
- sqlite3_result_text(pCtx, "fts5: 2022-07-21 15:24:47 698edb77537b67c41adc68f9b892db56bcf9a55e00371a61420f3ddd668e6603", -1, SQLITE_TRANSIENT);
236845
+ sqlite3_result_text(pCtx, "fts5: 2022-09-29 15:55:41 a29f9949895322123f7c38fbe94c649a9d6e6c9cd0c3b41c96d694552f26b309", -1, SQLITE_TRANSIENT);
236749
236846
  }
236750
236847
 
236751
236848
  /*
@@ -241739,7 +241836,7 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
241739
241836
  ** Purpose: Header file for SQLite3 Multiple Ciphers compile-time configuration
241740
241837
  ** Author: Ulrich Telle
241741
241838
  ** Created: 2021-09-27
241742
- ** Copyright: (c) 2019-2021 Ulrich Telle
241839
+ ** Copyright: (c) 2019-2022 Ulrich Telle
241743
241840
  ** License: MIT
241744
241841
  */
241745
241842
 
@@ -241796,6 +241893,27 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
241796
241893
  #define HAVE_CIPHER_RC4 1
241797
241894
  #endif
241798
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
+
241799
241917
  /*
241800
241918
  ** Check that at least one cipher is be supported
241801
241919
  */
@@ -241804,7 +241922,7 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
241804
241922
  HAVE_CIPHER_CHACHA20 == 0 && \
241805
241923
  HAVE_CIPHER_SQLCIPHER == 0 && \
241806
241924
  HAVE_CIPHER_RC4 == 0
241807
- #error Enable at least one cipher scheme!
241925
+ #pragma message ("sqlite3mc_config.h: WARNING - No built-in cipher scheme enabled!")
241808
241926
  #endif
241809
241927
 
241810
241928
  /*
@@ -241883,7 +242001,7 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
241883
242001
  ** Purpose: Header file for SQLite3 Multiple Ciphers support
241884
242002
  ** Author: Ulrich Telle
241885
242003
  ** Created: 2020-03-01
241886
- ** Copyright: (c) 2019-2021 Ulrich Telle
242004
+ ** Copyright: (c) 2019-2022 Ulrich Telle
241887
242005
  ** License: MIT
241888
242006
  */
241889
242007
 
@@ -241910,10 +242028,10 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
241910
242028
  #define SQLITE3MC_VERSION_H_
241911
242029
 
241912
242030
  #define SQLITE3MC_VERSION_MAJOR 1
241913
- #define SQLITE3MC_VERSION_MINOR 4
241914
- #define SQLITE3MC_VERSION_RELEASE 8
242031
+ #define SQLITE3MC_VERSION_MINOR 5
242032
+ #define SQLITE3MC_VERSION_RELEASE 3
241915
242033
  #define SQLITE3MC_VERSION_SUBRELEASE 0
241916
- #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.4.8"
242034
+ #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.5.3"
241917
242035
 
241918
242036
  #endif /* SQLITE3MC_VERSION_H_ */
241919
242037
  /*** End of #include "sqlite3mc_version.h" ***/
@@ -242072,9 +242190,9 @@ extern "C" {
242072
242190
  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
242073
242191
  ** [sqlite_version()] and [sqlite_source_id()].
242074
242192
  */
242075
- #define SQLITE_VERSION "3.39.2"
242076
- #define SQLITE_VERSION_NUMBER 3039002
242077
- #define SQLITE_SOURCE_ID "2022-07-21 15:24:47 698edb77537b67c41adc68f9b892db56bcf9a55e00371a61420f3ddd668e6603"
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"
242078
242196
 
242079
242197
  /*
242080
242198
  ** CAPI3REF: Run-Time Library Version Numbers
@@ -254869,13 +254987,13 @@ int sqlite3_user_delete(
254869
254987
  /*
254870
254988
  ** Symbols for ciphers
254871
254989
  */
254872
- #define CODEC_TYPE_UNKNOWN 0
254873
- #define CODEC_TYPE_AES128 1
254874
- #define CODEC_TYPE_AES256 2
254875
- #define CODEC_TYPE_CHACHA20 3
254876
- #define CODEC_TYPE_SQLCIPHER 4
254877
- #define CODEC_TYPE_RC4 5
254878
- #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
254879
254997
 
254880
254998
  /*
254881
254999
  ** Definition of API functions
@@ -254944,6 +255062,9 @@ SQLITE_API void sqlite3_activate_see(const char* zPassPhrase);
254944
255062
  /*
254945
255063
  ** Define functions for the configuration of the wxSQLite3 encryption extension
254946
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);
254947
255068
  SQLITE_API int sqlite3mc_config(sqlite3* db, const char* paramName, int newValue);
254948
255069
  SQLITE_API int sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramName, int newValue);
254949
255070
  SQLITE_API unsigned char* sqlite3mc_codec_data(sqlite3* db, const char* zDbName, const char* paramName);
@@ -254955,6 +255076,88 @@ SQLITE_API int wxsqlite3_config_cipher(sqlite3* db, const char* cipherName, cons
254955
255076
  SQLITE_API unsigned char* wxsqlite3_codec_data(sqlite3* db, const char* zDbName, const char* paramName);
254956
255077
  #endif
254957
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
+
254958
255161
  #ifdef __cplusplus
254959
255162
  }
254960
255163
  #endif
@@ -257769,6 +257972,16 @@ static size_t entropy(void* buf, size_t n)
257769
257972
  #endif
257770
257973
  return read_urandom(buf, n);
257771
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
+
257772
257985
  #else
257773
257986
  # error "Secure pseudorandom number generator not implemented for this OS"
257774
257987
  #endif
@@ -260991,7 +261204,7 @@ void RijndaelInvalidate(Rijndael* rijndael)
260991
261204
  ** Purpose: Header for the ciphers of SQLite3 Multiple Ciphers
260992
261205
  ** Author: Ulrich Telle
260993
261206
  ** Created: 2020-02-02
260994
- ** Copyright: (c) 2006-2021 Ulrich Telle
261207
+ ** Copyright: (c) 2006-2022 Ulrich Telle
260995
261208
  ** License: MIT
260996
261209
  */
260997
261210
 
@@ -261013,10 +261226,31 @@ void RijndaelInvalidate(Rijndael* rijndael)
261013
261226
  #define CODEC_TYPE CODEC_TYPE_DEFAULT
261014
261227
  #endif
261015
261228
 
261016
- #if CODEC_TYPE < 1 || CODEC_TYPE > CODEC_TYPE_MAX
261229
+ #if CODEC_TYPE < 1 || CODEC_TYPE > CODEC_TYPE_MAX_BUILTIN
261017
261230
  #error "Invalid codec type selected"
261018
261231
  #endif
261019
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
+
261020
261254
  #define MAXKEYLENGTH 32
261021
261255
  #define KEYLENGTH_AES128 16
261022
261256
  #define KEYLENGTH_AES256 32
@@ -261024,6 +261258,13 @@ void RijndaelInvalidate(Rijndael* rijndael)
261024
261258
 
261025
261259
  #define CODEC_SHA_ITER 4001
261026
261260
 
261261
+ typedef struct _CodecParameter
261262
+ {
261263
+ char* m_name;
261264
+ int m_id;
261265
+ CipherParams* m_params;
261266
+ } CodecParameter;
261267
+
261027
261268
  typedef struct _Codec
261028
261269
  {
261029
261270
  int m_isEncrypted;
@@ -261055,53 +261296,11 @@ typedef struct _Codec
261055
261296
  #define CIPHER_PARAMS_SENTINEL { "", 0, 0, 0, 0 }
261056
261297
  #define CIPHER_PAGE1_OFFSET 24
261057
261298
 
261058
- typedef struct _CipherParams
261059
- {
261060
- char* m_name;
261061
- int m_value;
261062
- int m_default;
261063
- int m_minValue;
261064
- int m_maxValue;
261065
- } CipherParams;
261066
-
261067
- typedef struct _CodecParameter
261068
- {
261069
- char* m_name;
261070
- int m_id;
261071
- CipherParams* m_params;
261072
- } CodecParameter;
261073
-
261074
- typedef void* (*AllocateCipher_t)(sqlite3* db);
261075
- typedef void (*FreeCipher_t)(void* cipher);
261076
- typedef void (*CloneCipher_t)(void* cipherTo, void* cipherFrom);
261077
- typedef int (*GetLegacy_t)(void* cipher);
261078
- typedef int (*GetPageSize_t)(void* cipher);
261079
- typedef int (*GetReserved_t)(void* cipher);
261080
- typedef unsigned char* (*GetSalt_t)(void* cipher);
261081
- typedef void (*GenerateKey_t)(void* cipher, BtShared* pBt, char* userPassword, int passwordLength, int rekey, unsigned char* cipherSalt);
261082
- typedef int (*EncryptPage_t)(void* cipher, int page, unsigned char* data, int len, int reserved);
261083
- typedef int (*DecryptPage_t)(void* cipher, int page, unsigned char* data, int len, int reserved, int hmacCheck);
261084
-
261085
- typedef struct _CodecDescriptor
261086
- {
261087
- char m_name[32];
261088
- AllocateCipher_t m_allocateCipher;
261089
- FreeCipher_t m_freeCipher;
261090
- CloneCipher_t m_cloneCipher;
261091
- GetLegacy_t m_getLegacy;
261092
- GetPageSize_t m_getPageSize;
261093
- GetReserved_t m_getReserved;
261094
- GetSalt_t m_getSalt;
261095
- GenerateKey_t m_generateKey;
261096
- EncryptPage_t m_encryptPage;
261097
- DecryptPage_t m_decryptPage;
261098
- } CipherDescriptor;
261099
-
261100
261299
  SQLITE_PRIVATE int sqlite3mcGetCipherParameter(CipherParams* cipherParams, const char* paramName);
261101
261300
 
261102
261301
  SQLITE_PRIVATE int sqlite3mcGetCipherType(sqlite3* db);
261103
261302
 
261104
- SQLITE_PRIVATE CipherParams* sqlite3mcGetCipherParams(sqlite3* db, int cypherType);
261303
+ SQLITE_PRIVATE CipherParams* sqlite3mcGetCipherParams(sqlite3* db, const char* cipherName);
261105
261304
 
261106
261305
  SQLITE_PRIVATE int sqlite3mcCodecInit(Codec* codec);
261107
261306
 
@@ -261504,6 +261703,8 @@ sqlite3mcConvertHex2Bin(const unsigned char* hex, int len, unsigned char* bin)
261504
261703
  /* --- AES 128-bit cipher (wxSQLite3) --- */
261505
261704
  #if HAVE_CIPHER_AES_128_CBC
261506
261705
 
261706
+ #define CIPHER_NAME_AES128 "aes128cbc"
261707
+
261507
261708
  /*
261508
261709
  ** Configuration parameters for "aes128cbc"
261509
261710
  **
@@ -261554,7 +261755,7 @@ AllocateAES128Cipher(sqlite3* db)
261554
261755
  }
261555
261756
  if (aesCipher != NULL)
261556
261757
  {
261557
- CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_AES128);
261758
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_AES128);
261558
261759
  aesCipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
261559
261760
  aesCipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
261560
261761
  }
@@ -261758,16 +261959,17 @@ DecryptPageAES128Cipher(void* cipher, int page, unsigned char* data, int len, in
261758
261959
 
261759
261960
  SQLITE_PRIVATE const CipherDescriptor mcAES128Descriptor =
261760
261961
  {
261761
- "aes128cbc", AllocateAES128Cipher,
261762
- FreeAES128Cipher,
261763
- CloneAES128Cipher,
261764
- GetLegacyAES128Cipher,
261765
- GetPageSizeAES128Cipher,
261766
- GetReservedAES128Cipher,
261767
- GetSaltAES128Cipher,
261768
- GenerateKeyAES128Cipher,
261769
- EncryptPageAES128Cipher,
261770
- 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
261771
261973
  };
261772
261974
  #endif
261773
261975
  /*** End of #include "cipher_wxaes128.c" ***/
@@ -261789,6 +261991,8 @@ SQLITE_PRIVATE const CipherDescriptor mcAES128Descriptor =
261789
261991
  /* --- AES 256-bit cipher (wxSQLite3) --- */
261790
261992
  #if HAVE_CIPHER_AES_256_CBC
261791
261993
 
261994
+ #define CIPHER_NAME_AES256 "aes256cbc"
261995
+
261792
261996
  /*
261793
261997
  ** Configuration parameters for "aes256cbc"
261794
261998
  **
@@ -261843,7 +262047,7 @@ AllocateAES256Cipher(sqlite3* db)
261843
262047
  }
261844
262048
  if (aesCipher != NULL)
261845
262049
  {
261846
- CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_AES256);
262050
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_AES256);
261847
262051
  aesCipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
261848
262052
  aesCipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
261849
262053
  aesCipher->m_kdfIter = sqlite3mcGetCipherParameter(cipherParams, "kdf_iter");
@@ -262011,16 +262215,17 @@ DecryptPageAES256Cipher(void* cipher, int page, unsigned char* data, int len, in
262011
262215
 
262012
262216
  SQLITE_PRIVATE const CipherDescriptor mcAES256Descriptor =
262013
262217
  {
262014
- "aes256cbc", AllocateAES256Cipher,
262015
- FreeAES256Cipher,
262016
- CloneAES256Cipher,
262017
- GetLegacyAES256Cipher,
262018
- GetPageSizeAES256Cipher,
262019
- GetReservedAES256Cipher,
262020
- GetSaltAES256Cipher,
262021
- GenerateKeyAES256Cipher,
262022
- EncryptPageAES256Cipher,
262023
- 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
262024
262229
  };
262025
262230
  #endif
262026
262231
  /*** End of #include "cipher_wxaes256.c" ***/
@@ -262042,6 +262247,8 @@ SQLITE_PRIVATE const CipherDescriptor mcAES256Descriptor =
262042
262247
  /* --- ChaCha20-Poly1305 cipher (plus sqleet variant) --- */
262043
262248
  #if HAVE_CIPHER_CHACHA20
262044
262249
 
262250
+ #define CIPHER_NAME_CHACHA20 "chacha20"
262251
+
262045
262252
  /*
262046
262253
  ** Configuration parameters for "chacha20"
262047
262254
  **
@@ -262098,7 +262305,7 @@ AllocateChaCha20Cipher(sqlite3* db)
262098
262305
  }
262099
262306
  if (chacha20Cipher != NULL)
262100
262307
  {
262101
- CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_CHACHA20);
262308
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_CHACHA20);
262102
262309
  chacha20Cipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
262103
262310
  chacha20Cipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
262104
262311
  chacha20Cipher->m_kdfIter = sqlite3mcGetCipherParameter(cipherParams, "kdf_iter");
@@ -262394,16 +262601,17 @@ DecryptPageChaCha20Cipher(void* cipher, int page, unsigned char* data, int len,
262394
262601
 
262395
262602
  SQLITE_PRIVATE const CipherDescriptor mcChaCha20Descriptor =
262396
262603
  {
262397
- "chacha20", AllocateChaCha20Cipher,
262398
- FreeChaCha20Cipher,
262399
- CloneChaCha20Cipher,
262400
- GetLegacyChaCha20Cipher,
262401
- GetPageSizeChaCha20Cipher,
262402
- GetReservedChaCha20Cipher,
262403
- GetSaltChaCha20Cipher,
262404
- GenerateKeyChaCha20Cipher,
262405
- EncryptPageChaCha20Cipher,
262406
- 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
262407
262615
  };
262408
262616
  #endif
262409
262617
  /*** End of #include "cipher_chacha20.c" ***/
@@ -262425,6 +262633,8 @@ SQLITE_PRIVATE const CipherDescriptor mcChaCha20Descriptor =
262425
262633
  /* --- SQLCipher AES256CBC-HMAC cipher --- */
262426
262634
  #if HAVE_CIPHER_SQLCIPHER
262427
262635
 
262636
+ #define CIPHER_NAME_SQLCIPHER "sqlcipher"
262637
+
262428
262638
  /*
262429
262639
  ** Configuration parameters for "sqlcipher"
262430
262640
  **
@@ -262540,7 +262750,7 @@ AllocateSQLCipherCipher(sqlite3* db)
262540
262750
  }
262541
262751
  if (sqlCipherCipher != NULL)
262542
262752
  {
262543
- CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_SQLCIPHER);
262753
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_SQLCIPHER);
262544
262754
  sqlCipherCipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
262545
262755
  sqlCipherCipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
262546
262756
  sqlCipherCipher->m_kdfIter = sqlite3mcGetCipherParameter(cipherParams, "kdf_iter");
@@ -262913,16 +263123,17 @@ DecryptPageSQLCipherCipher(void* cipher, int page, unsigned char* data, int len,
262913
263123
  }
262914
263124
  SQLITE_PRIVATE const CipherDescriptor mcSQLCipherDescriptor =
262915
263125
  {
262916
- "sqlcipher", AllocateSQLCipherCipher,
262917
- FreeSQLCipherCipher,
262918
- CloneSQLCipherCipher,
262919
- GetLegacySQLCipherCipher,
262920
- GetPageSizeSQLCipherCipher,
262921
- GetReservedSQLCipherCipher,
262922
- GetSaltSQLCipherCipher,
262923
- GenerateKeySQLCipherCipher,
262924
- EncryptPageSQLCipherCipher,
262925
- 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
262926
263137
  };
262927
263138
  #endif
262928
263139
  /*** End of #include "cipher_sqlcipher.c" ***/
@@ -262944,6 +263155,8 @@ SQLITE_PRIVATE const CipherDescriptor mcSQLCipherDescriptor =
262944
263155
  /* --- RC4 cipher (System.Data.SQLite) --- */
262945
263156
  #if HAVE_CIPHER_RC4
262946
263157
 
263158
+ #define CIPHER_NAME_RC4 "rc4"
263159
+
262947
263160
  /*
262948
263161
  ** Configuration parameters for "rc4"
262949
263162
  **
@@ -262984,7 +263197,7 @@ AllocateRC4Cipher(sqlite3* db)
262984
263197
  }
262985
263198
  if (rc4Cipher != NULL)
262986
263199
  {
262987
- CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_RC4);
263200
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_RC4);
262988
263201
  rc4Cipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
262989
263202
  rc4Cipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
262990
263203
  }
@@ -263087,16 +263300,17 @@ DecryptPageRC4Cipher(void* cipher, int page, unsigned char* data, int len, int r
263087
263300
 
263088
263301
  SQLITE_PRIVATE const CipherDescriptor mcRC4Descriptor =
263089
263302
  {
263090
- "rc4", AllocateRC4Cipher,
263091
- FreeRC4Cipher,
263092
- CloneRC4Cipher,
263093
- GetLegacyRC4Cipher,
263094
- GetPageSizeRC4Cipher,
263095
- GetReservedRC4Cipher,
263096
- GetSaltRC4Cipher,
263097
- GenerateKeyRC4Cipher,
263098
- EncryptPageRC4Cipher,
263099
- 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
263100
263314
  };
263101
263315
  #endif
263102
263316
  /*** End of #include "cipher_sds_rc4.c" ***/
@@ -263108,7 +263322,7 @@ SQLITE_PRIVATE const CipherDescriptor mcRC4Descriptor =
263108
263322
  ** Purpose: Implementation of SQLite codecs
263109
263323
  ** Author: Ulrich Telle
263110
263324
  ** Created: 2020-02-02
263111
- ** Copyright: (c) 2006-2021 Ulrich Telle
263325
+ ** Copyright: (c) 2006-2022 Ulrich Telle
263112
263326
  ** License: MIT
263113
263327
  */
263114
263328
 
@@ -263129,21 +263343,26 @@ static unsigned char padding[] =
263129
263343
 
263130
263344
  static CipherParams commonParams[] =
263131
263345
  {
263132
- { "cipher", CODEC_TYPE, CODEC_TYPE, 1, CODEC_TYPE_MAX },
263133
- { "hmac_check", 1, 1, 0, 1 },
263134
- { "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 },
263135
263349
  CIPHER_PARAMS_SENTINEL
263136
263350
  };
263137
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
+
263138
263357
  SQLITE_PRIVATE int
263139
263358
  sqlite3mcGetCipherParameter(CipherParams* cipherParams, const char* paramName)
263140
263359
  {
263141
263360
  int value = -1;
263142
- for (; strlen(cipherParams->m_name) > 0; ++cipherParams)
263361
+ for (; cipherParams->m_name[0] != 0; ++cipherParams)
263143
263362
  {
263144
263363
  if (sqlite3_stricmp(paramName, cipherParams->m_name) == 0) break;
263145
263364
  }
263146
- if (strlen(cipherParams->m_name) > 0)
263365
+ if (cipherParams->m_name[0] != 0)
263147
263366
  {
263148
263367
  value = cipherParams->m_value;
263149
263368
  cipherParams->m_value = cipherParams->m_default;
@@ -263151,26 +263370,15 @@ sqlite3mcGetCipherParameter(CipherParams* cipherParams, const char* paramName)
263151
263370
  return value;
263152
263371
  }
263153
263372
 
263154
- static CodecParameter globalCodecParameterTable[] =
263373
+ typedef struct _CipherName
263155
263374
  {
263156
- { "global", CODEC_TYPE_UNKNOWN, commonParams },
263157
- #if HAVE_CIPHER_AES_128_CBC
263158
- { "aes128cbc", CODEC_TYPE_AES128, mcAES128Params },
263159
- #endif
263160
- #if HAVE_CIPHER_AES_256_CBC
263161
- { "aes256cbc", CODEC_TYPE_AES256, mcAES256Params },
263162
- #endif
263163
- #if HAVE_CIPHER_CHACHA20
263164
- { "chacha20", CODEC_TYPE_CHACHA20, mcChaCha20Params },
263165
- #endif
263166
- #if HAVE_CIPHER_SQLCIPHER
263167
- { "sqlcipher", CODEC_TYPE_SQLCIPHER, mcSQLCipherParams },
263168
- #endif
263169
- #if HAVE_CIPHER_RC4
263170
- { "rc4", CODEC_TYPE_RC4, mcRC4Params },
263171
- #endif
263172
- { "", CODEC_TYPE_UNKNOWN, NULL }
263173
- };
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];
263174
263382
 
263175
263383
  SQLITE_PRIVATE CodecParameter*
263176
263384
  sqlite3mcCloneCodecParameterTable()
@@ -263182,10 +263390,10 @@ sqlite3mcCloneCodecParameterTable()
263182
263390
  CipherParams* cloneCipherParams;
263183
263391
  CodecParameter* cloneCodecParams;
263184
263392
 
263185
- for (j = 0; strlen(globalCodecParameterTable[j].m_name) > 0; ++j)
263393
+ for (j = 0; globalCodecParameterTable[j].m_name[0] != 0; ++j)
263186
263394
  {
263187
263395
  CipherParams* params = globalCodecParameterTable[j].m_params;
263188
- for (k = 0; strlen(params[k].m_name) > 0; ++k);
263396
+ for (k = 0; params[k].m_name[0] != 0; ++k);
263189
263397
  nParams += k;
263190
263398
  }
263191
263399
  nTables = j;
@@ -263204,7 +263412,7 @@ sqlite3mcCloneCodecParameterTable()
263204
263412
  cloneCodecParams[j].m_name = globalCodecParameterTable[j].m_name;
263205
263413
  cloneCodecParams[j].m_id = globalCodecParameterTable[j].m_id;
263206
263414
  cloneCodecParams[j].m_params = &cloneCipherParams[offset];
263207
- for (n = 0; strlen(params[n].m_name) > 0; ++n);
263415
+ for (n = 0; params[n].m_name[0] != 0; ++n);
263208
263416
  /* Copy all parameters of the current table (including sentinel) */
263209
263417
  for (k = 0; k <= n; ++k)
263210
263418
  {
@@ -263244,35 +263452,7 @@ static const CipherDescriptor mcDummyDescriptor =
263244
263452
  "@dummy@", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
263245
263453
  };
263246
263454
 
263247
- static const CipherDescriptor* codecDescriptorTable[] =
263248
- {
263249
- #if HAVE_CIPHER_AES_128_CBC
263250
- &mcAES128Descriptor,
263251
- #else
263252
- &mcDummyDescriptor,
263253
- #endif
263254
- #if HAVE_CIPHER_AES_256_CBC
263255
- &mcAES256Descriptor,
263256
- #else
263257
- &mcDummyDescriptor,
263258
- #endif
263259
- #if HAVE_CIPHER_CHACHA20
263260
- &mcChaCha20Descriptor,
263261
- #else
263262
- &mcDummyDescriptor,
263263
- #endif
263264
- #if HAVE_CIPHER_SQLCIPHER
263265
- &mcSQLCipherDescriptor,
263266
- #else
263267
- &mcDummyDescriptor,
263268
- #endif
263269
- #if HAVE_CIPHER_RC4
263270
- &mcRC4Descriptor,
263271
- #else
263272
- &mcDummyDescriptor,
263273
- #endif
263274
- &mcSentinelDescriptor
263275
- };
263455
+ static CipherDescriptor globalCodecDescriptorTable[CODEC_COUNT_MAX + 1];
263276
263456
 
263277
263457
  /* --- Codec --- */
263278
263458
 
@@ -263286,11 +263466,11 @@ sqlite3mcGetCipherType(sqlite3* db)
263286
263466
  CipherParams* cipherParamTable = (codecParams != NULL) ? codecParams[0].m_params : commonParams;
263287
263467
  int cipherType = CODEC_TYPE;
263288
263468
  CipherParams* cipher = cipherParamTable;
263289
- for (; strlen(cipher->m_name) > 0; ++cipher)
263469
+ for (; cipher->m_name[0] != 0; ++cipher)
263290
263470
  {
263291
263471
  if (sqlite3_stricmp("cipher", cipher->m_name) == 0) break;
263292
263472
  }
263293
- if (strlen(cipher->m_name) > 0)
263473
+ if (cipher->m_name[0] != 0)
263294
263474
  {
263295
263475
  cipherType = cipher->m_value;
263296
263476
  cipher->m_value = cipher->m_default;
@@ -263299,19 +263479,20 @@ sqlite3mcGetCipherType(sqlite3* db)
263299
263479
  }
263300
263480
 
263301
263481
  SQLITE_PRIVATE CipherParams*
263302
- sqlite3mcGetCipherParams(sqlite3* db, int cypherType)
263482
+ sqlite3mcGetCipherParams(sqlite3* db, const char* cipherName)
263303
263483
  {
263304
263484
  int j = 0;
263485
+ int cipherType = sqlite3mc_cipher_index(cipherName);
263305
263486
  CodecParameter* codecParams = (db != NULL) ? sqlite3mcGetCodecParams(db) : globalCodecParameterTable;
263306
263487
  if (codecParams == NULL)
263307
263488
  {
263308
263489
  codecParams = globalCodecParameterTable;
263309
263490
  }
263310
- if (cypherType > 0)
263491
+ if (cipherType > 0)
263311
263492
  {
263312
263493
  for (j = 1; codecParams[j].m_id > 0; ++j)
263313
263494
  {
263314
- if (cypherType == codecParams[j].m_id) break;
263495
+ if (cipherType == codecParams[j].m_id) break;
263315
263496
  }
263316
263497
  }
263317
263498
  CipherParams* cipherParamTable = codecParams[j].m_params;
@@ -263361,12 +263542,12 @@ sqlite3mcCodecTerm(Codec* codec)
263361
263542
  {
263362
263543
  if (codec->m_readCipher != NULL)
263363
263544
  {
263364
- codecDescriptorTable[codec->m_readCipherType - 1]->m_freeCipher(codec->m_readCipher);
263545
+ globalCodecDescriptorTable[codec->m_readCipherType - 1].m_freeCipher(codec->m_readCipher);
263365
263546
  codec->m_readCipher = NULL;
263366
263547
  }
263367
263548
  if (codec->m_writeCipher != NULL)
263368
263549
  {
263369
- codecDescriptorTable[codec->m_writeCipherType - 1]->m_freeCipher(codec->m_writeCipher);
263550
+ globalCodecDescriptorTable[codec->m_writeCipherType - 1].m_freeCipher(codec->m_writeCipher);
263370
263551
  codec->m_writeCipher = NULL;
263371
263552
  }
263372
263553
  memset(codec, 0, sizeof(Codec));
@@ -263383,14 +263564,14 @@ SQLITE_PRIVATE int
263383
263564
  sqlite3mcCodecSetup(Codec* codec, int cipherType, char* userPassword, int passwordLength)
263384
263565
  {
263385
263566
  int rc = SQLITE_OK;
263386
- CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, 0);
263567
+ CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, CIPHER_NAME_GLOBAL);
263387
263568
  codec->m_isEncrypted = 1;
263388
263569
  codec->m_hmacCheck = sqlite3mcGetCipherParameter(globalParams, "hmac_check");
263389
263570
  codec->m_walLegacy = sqlite3mcGetCipherParameter(globalParams, "mc_legacy_wal");
263390
263571
  codec->m_hasReadCipher = 1;
263391
263572
  codec->m_hasWriteCipher = 1;
263392
263573
  codec->m_readCipherType = cipherType;
263393
- 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);
263394
263575
  if (codec->m_readCipher != NULL)
263395
263576
  {
263396
263577
  unsigned char* keySalt = (codec->m_hasKeySalt != 0) ? codec->m_keySalt : NULL;
@@ -263408,17 +263589,17 @@ SQLITE_PRIVATE int
263408
263589
  sqlite3mcSetupWriteCipher(Codec* codec, int cipherType, char* userPassword, int passwordLength)
263409
263590
  {
263410
263591
  int rc = SQLITE_OK;
263411
- CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, 0);
263592
+ CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, CIPHER_NAME_GLOBAL);
263412
263593
  if (codec->m_writeCipher != NULL)
263413
263594
  {
263414
- codecDescriptorTable[codec->m_writeCipherType-1]->m_freeCipher(codec->m_writeCipher);
263595
+ globalCodecDescriptorTable[codec->m_writeCipherType-1].m_freeCipher(codec->m_writeCipher);
263415
263596
  }
263416
263597
  codec->m_isEncrypted = 1;
263417
263598
  codec->m_hmacCheck = sqlite3mcGetCipherParameter(globalParams, "hmac_check");
263418
263599
  codec->m_walLegacy = sqlite3mcGetCipherParameter(globalParams, "mc_legacy_wal");
263419
263600
  codec->m_hasWriteCipher = 1;
263420
263601
  codec->m_writeCipherType = cipherType;
263421
- 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);
263422
263603
  if (codec->m_writeCipher != NULL)
263423
263604
  {
263424
263605
  unsigned char* keySalt = (codec->m_hasKeySalt != 0) ? codec->m_keySalt : NULL;
@@ -263539,57 +263720,57 @@ sqlite3mcGetPageBuffer(Codec* codec)
263539
263720
  SQLITE_PRIVATE int
263540
263721
  sqlite3mcGetLegacyReadCipher(Codec* codec)
263541
263722
  {
263542
- 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;
263543
263724
  return legacy;
263544
263725
  }
263545
263726
 
263546
263727
  SQLITE_PRIVATE int
263547
263728
  sqlite3mcGetLegacyWriteCipher(Codec* codec)
263548
263729
  {
263549
- 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;
263550
263731
  return legacy;
263551
263732
  }
263552
263733
 
263553
263734
  SQLITE_PRIVATE int
263554
263735
  sqlite3mcGetPageSizeReadCipher(Codec* codec)
263555
263736
  {
263556
- 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;
263557
263738
  return pageSize;
263558
263739
  }
263559
263740
 
263560
263741
  SQLITE_PRIVATE int
263561
263742
  sqlite3mcGetPageSizeWriteCipher(Codec* codec)
263562
263743
  {
263563
- 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;
263564
263745
  return pageSize;
263565
263746
  }
263566
263747
 
263567
263748
  SQLITE_PRIVATE int
263568
263749
  sqlite3mcGetReservedReadCipher(Codec* codec)
263569
263750
  {
263570
- 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;
263571
263752
  return reserved;
263572
263753
  }
263573
263754
 
263574
263755
  SQLITE_PRIVATE int
263575
263756
  sqlite3mcGetReservedWriteCipher(Codec* codec)
263576
263757
  {
263577
- 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;
263578
263759
  return reserved;
263579
263760
  }
263580
263761
 
263581
263762
  SQLITE_PRIVATE int
263582
263763
  sqlite3mcReservedEqual(Codec* codec)
263583
263764
  {
263584
- int readReserved = (codec->m_hasReadCipher && codec->m_readCipher != NULL) ? codecDescriptorTable[codec->m_readCipherType-1]->m_getReserved(codec->m_readCipher) : -1;
263585
- 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;
263586
263767
  return (readReserved == writeReserved);
263587
263768
  }
263588
263769
 
263589
263770
  SQLITE_PRIVATE unsigned char*
263590
263771
  sqlite3mcGetSaltWriteCipher(Codec* codec)
263591
263772
  {
263592
- 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;
263593
263774
  return salt;
263594
263775
  }
263595
263776
 
@@ -263611,10 +263792,10 @@ sqlite3mcCodecCopy(Codec* codec, Codec* other)
263611
263792
 
263612
263793
  if (codec->m_hasReadCipher)
263613
263794
  {
263614
- 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);
263615
263796
  if (codec->m_readCipher != NULL)
263616
263797
  {
263617
- 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);
263618
263799
  }
263619
263800
  else
263620
263801
  {
@@ -263624,10 +263805,10 @@ sqlite3mcCodecCopy(Codec* codec, Codec* other)
263624
263805
 
263625
263806
  if (codec->m_hasWriteCipher)
263626
263807
  {
263627
- 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);
263628
263809
  if (codec->m_writeCipher != NULL)
263629
263810
  {
263630
- 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);
263631
263812
  }
263632
263813
  else
263633
263814
  {
@@ -263650,17 +263831,17 @@ sqlite3mcCopyCipher(Codec* codec, int read2write)
263650
263831
  {
263651
263832
  if (codec->m_writeCipher != NULL && codec->m_writeCipherType != codec->m_readCipherType)
263652
263833
  {
263653
- codecDescriptorTable[codec->m_writeCipherType-1]->m_freeCipher(codec->m_writeCipher);
263834
+ globalCodecDescriptorTable[codec->m_writeCipherType-1].m_freeCipher(codec->m_writeCipher);
263654
263835
  codec->m_writeCipher = NULL;
263655
263836
  }
263656
263837
  if (codec->m_writeCipher == NULL)
263657
263838
  {
263658
263839
  codec->m_writeCipherType = codec->m_readCipherType;
263659
- 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);
263660
263841
  }
263661
263842
  if (codec->m_writeCipher != NULL)
263662
263843
  {
263663
- 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);
263664
263845
  }
263665
263846
  else
263666
263847
  {
@@ -263671,17 +263852,17 @@ sqlite3mcCopyCipher(Codec* codec, int read2write)
263671
263852
  {
263672
263853
  if (codec->m_readCipher != NULL && codec->m_readCipherType != codec->m_writeCipherType)
263673
263854
  {
263674
- codecDescriptorTable[codec->m_readCipherType-1]->m_freeCipher(codec->m_readCipher);
263855
+ globalCodecDescriptorTable[codec->m_readCipherType-1].m_freeCipher(codec->m_readCipher);
263675
263856
  codec->m_readCipher = NULL;
263676
263857
  }
263677
263858
  if (codec->m_readCipher == NULL)
263678
263859
  {
263679
263860
  codec->m_readCipherType = codec->m_writeCipherType;
263680
- 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);
263681
263862
  }
263682
263863
  if (codec->m_readCipher != NULL)
263683
263864
  {
263684
- 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);
263685
263866
  }
263686
263867
  else
263687
263868
  {
@@ -263712,13 +263893,13 @@ sqlite3mcPadPassword(char* password, int pswdlen, unsigned char pswd[32])
263712
263893
  SQLITE_PRIVATE void
263713
263894
  sqlite3mcGenerateReadKey(Codec* codec, char* userPassword, int passwordLength, unsigned char* cipherSalt)
263714
263895
  {
263715
- 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);
263716
263897
  }
263717
263898
 
263718
263899
  SQLITE_PRIVATE void
263719
263900
  sqlite3mcGenerateWriteKey(Codec* codec, char* userPassword, int passwordLength, unsigned char* cipherSalt)
263720
263901
  {
263721
- 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);
263722
263903
  }
263723
263904
 
263724
263905
  SQLITE_PRIVATE int
@@ -263728,7 +263909,7 @@ sqlite3mcEncrypt(Codec* codec, int page, unsigned char* data, int len, int useWr
263728
263909
  void* cipher = (useWriteKey) ? codec->m_writeCipher : codec->m_readCipher;
263729
263910
  int reserved = (useWriteKey) ? (codec->m_writeReserved >= 0) ? codec->m_writeReserved : codec->m_reserved
263730
263911
  : (codec->m_readReserved >= 0) ? codec->m_readReserved : codec->m_reserved;
263731
- return codecDescriptorTable[cipherType-1]->m_encryptPage(cipher, page, data, len, reserved);
263912
+ return globalCodecDescriptorTable[cipherType-1].m_encryptPage(cipher, page, data, len, reserved);
263732
263913
  }
263733
263914
 
263734
263915
  SQLITE_PRIVATE int
@@ -263737,7 +263918,7 @@ sqlite3mcDecrypt(Codec* codec, int page, unsigned char* data, int len)
263737
263918
  int cipherType = codec->m_readCipherType;
263738
263919
  void* cipher = codec->m_readCipher;
263739
263920
  int reserved = (codec->m_readReserved >= 0) ? codec->m_readReserved : codec->m_reserved;
263740
- 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);
263741
263922
  }
263742
263923
 
263743
263924
  #if HAVE_CIPHER_SQLCIPHER
@@ -263820,6 +264001,8 @@ SQLITE_PRIVATE int sqlite3mcHandleMainKey(sqlite3* db, const char* zPath);
263820
264001
 
263821
264002
  /* --- Codec --- */
263822
264003
 
264004
+ SQLITE_PRIVATE int
264005
+ sqlite3mcGetGlobalCipherCount();
263823
264006
 
263824
264007
  SQLITE_PRIVATE Codec*
263825
264008
  sqlite3mcGetCodec(sqlite3* db, const char* zDbName);
@@ -263888,28 +264071,30 @@ sqlite3mc_config(sqlite3* db, const char* paramName, int newValue)
263888
264071
  }
263889
264072
 
263890
264073
  param = codecParams[0].m_params;
263891
- for (; strlen(param->m_name) > 0; ++param)
264074
+ for (; param->m_name[0] != 0; ++param)
263892
264075
  {
263893
264076
  if (sqlite3_stricmp(paramName, param->m_name) == 0) break;
263894
264077
  }
263895
- if (strlen(param->m_name) > 0)
264078
+ if (param->m_name[0] != 0)
263896
264079
  {
264080
+ int cipherCount = sqlite3mcGetGlobalCipherCount();
263897
264081
  if (db != NULL)
263898
264082
  {
263899
264083
  sqlite3_mutex_enter(db->mutex);
263900
264084
  }
263901
264085
  else
263902
264086
  {
263903
- sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
264087
+ sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN));
263904
264088
  }
263905
264089
  value = (hasDefaultPrefix) ? param->m_default : (hasMinPrefix) ? param->m_minValue : (hasMaxPrefix) ? param->m_maxValue : param->m_value;
263906
264090
  if (!hasMinPrefix && !hasMaxPrefix && newValue >= 0 && newValue >= param->m_minValue && newValue <= param->m_maxValue)
263907
264091
  {
263908
264092
  int allowChange = 1;
264093
+
263909
264094
  /* Allow cipher change only if new cipher is actually available */
263910
264095
  if (sqlite3_stricmp(paramName, "cipher") == 0)
263911
264096
  {
263912
- allowChange = (codecDescriptorTable[newValue - 1] != &mcDummyDescriptor);
264097
+ allowChange = newValue > 0 && newValue <= cipherCount;
263913
264098
  }
263914
264099
 
263915
264100
  if (allowChange)
@@ -263929,12 +264114,52 @@ sqlite3mc_config(sqlite3* db, const char* paramName, int newValue)
263929
264114
  }
263930
264115
  else
263931
264116
  {
263932
- sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
264117
+ sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN));
263933
264118
  }
263934
264119
  }
263935
264120
  return value;
263936
264121
  }
263937
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
+
263938
264163
  SQLITE_API int
263939
264164
  sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramName, int newValue)
263940
264165
  {
@@ -263966,11 +264191,11 @@ sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramNa
263966
264191
  return value;
263967
264192
  }
263968
264193
 
263969
- for (j = 0; strlen(codecParams[j].m_name) > 0; ++j)
264194
+ for (j = 0; codecParams[j].m_name[0] != 0; ++j)
263970
264195
  {
263971
264196
  if (sqlite3_stricmp(cipherName, codecParams[j].m_name) == 0) break;
263972
264197
  }
263973
- if (strlen(codecParams[j].m_name) > 0)
264198
+ if (codecParams[j].m_name[0] != 0)
263974
264199
  {
263975
264200
  cipherParamTable = codecParams[j].m_params;
263976
264201
  }
@@ -264020,11 +264245,11 @@ sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramNa
264020
264245
  }
264021
264246
  #endif
264022
264247
 
264023
- for (; strlen(param->m_name) > 0; ++param)
264248
+ for (; param->m_name[0] != 0; ++param)
264024
264249
  {
264025
264250
  if (sqlite3_stricmp(paramName, param->m_name) == 0) break;
264026
264251
  }
264027
- if (strlen(param->m_name) > 0)
264252
+ if (param->m_name[0] != 0)
264028
264253
  {
264029
264254
  if (db != NULL)
264030
264255
  {
@@ -264200,11 +264425,11 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264200
264425
 
264201
264426
  param1 = codecParams[0].m_params;
264202
264427
  cipherParamTable = NULL;
264203
- for (; strlen(param1->m_name) > 0; ++param1)
264428
+ for (; param1->m_name[0] != 0; ++param1)
264204
264429
  {
264205
264430
  if (sqlite3_stricmp(nameParam1, param1->m_name) == 0) break;
264206
264431
  }
264207
- isCommonParam1 = strlen(param1->m_name) > 0;
264432
+ isCommonParam1 = param1->m_name[0] != 0;
264208
264433
 
264209
264434
  /* Check first argument whether it is a cipher name, if it wasn't a common parameter */
264210
264435
  /* If the first argument is a cipher name, cipherParamTable will point to the corresponding cipher parameter table */
@@ -264213,11 +264438,11 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264213
264438
  if (!hasDefaultPrefix && !hasMinPrefix && !hasMaxPrefix)
264214
264439
  {
264215
264440
  int j = 0;
264216
- for (j = 0; strlen(codecParams[j].m_name) > 0; ++j)
264441
+ for (j = 0; codecParams[j].m_name[0] != 0; ++j)
264217
264442
  {
264218
264443
  if (sqlite3_stricmp(nameParam1, codecParams[j].m_name) == 0) break;
264219
264444
  }
264220
- isCipherParam1 = strlen(codecParams[j].m_name) > 0;
264445
+ isCipherParam1 = codecParams[j].m_name[0] != 0;
264221
264446
  if (isCipherParam1)
264222
264447
  {
264223
264448
  cipherParamTable = codecParams[j].m_params;
@@ -264239,7 +264464,7 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264239
264464
  int value = (hasDefaultPrefix) ? param1->m_default : (hasMinPrefix) ? param1->m_minValue : (hasMaxPrefix) ? param1->m_maxValue : param1->m_value;
264240
264465
  if (sqlite3_stricmp(nameParam1, "cipher") == 0)
264241
264466
  {
264242
- 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);
264243
264468
  }
264244
264469
  else
264245
264470
  {
@@ -264252,7 +264477,7 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264252
264477
  int nParams = 0;
264253
264478
  int lenTotal = 0;
264254
264479
  int j;
264255
- for (j = 0; strlen(cipherParamTable[j].m_name) > 0; ++j)
264480
+ for (j = 0; cipherParamTable[j].m_name[0] != 0; ++j)
264256
264481
  {
264257
264482
  ++nParams;
264258
264483
  lenTotal += (int) strlen(cipherParamTable[j].m_name);
@@ -264298,18 +264523,18 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264298
264523
  {
264299
264524
  const char* nameCipher = (const char*)sqlite3_value_text(argv[1]);
264300
264525
  int j = 0;
264301
- for (j = 0; strlen(codecDescriptorTable[j]->m_name) > 0; ++j)
264526
+ for (j = 0; globalCodecDescriptorTable[j].m_name[0] != 0; ++j)
264302
264527
  {
264303
- if (sqlite3_stricmp(nameCipher, codecDescriptorTable[j]->m_name) == 0) break;
264528
+ if (sqlite3_stricmp(nameCipher, globalCodecDescriptorTable[j].m_name) == 0) break;
264304
264529
  }
264305
- if (strlen(codecDescriptorTable[j]->m_name) > 0)
264530
+ if (globalCodecDescriptorTable[j].m_name[0] != 0)
264306
264531
  {
264307
264532
  if (hasDefaultPrefix)
264308
264533
  {
264309
264534
  param1->m_default = j + 1;
264310
264535
  }
264311
264536
  param1->m_value = j + 1;
264312
- sqlite3_result_text(context, codecDescriptorTable[j]->m_name, -1, SQLITE_STATIC);
264537
+ sqlite3_result_text(context, globalCodecDescriptorTable[j].m_name, -1, SQLITE_STATIC);
264313
264538
  }
264314
264539
  else
264315
264540
  {
@@ -264371,7 +264596,7 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264371
264596
  hasMaxPrefix = 1;
264372
264597
  nameParam2 += 4;
264373
264598
  }
264374
- for (; strlen(param2->m_name) > 0; ++param2)
264599
+ for (; param2->m_name[0] != 0; ++param2)
264375
264600
  {
264376
264601
  if (sqlite3_stricmp(nameParam2, param2->m_name) == 0) break;
264377
264602
  }
@@ -264394,7 +264619,7 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
264394
264619
  }
264395
264620
  #endif
264396
264621
 
264397
- if (strlen(param2->m_name) > 0)
264622
+ if (param2->m_name[0] != 0)
264398
264623
  {
264399
264624
  if (argc == 2)
264400
264625
  {
@@ -264458,13 +264683,13 @@ sqlite3mcConfigureFromUri(sqlite3* db, const char *zDbName, int configDefault)
264458
264683
  CipherParams* cipherParams = NULL;
264459
264684
 
264460
264685
  /* Try to locate the cipher name */
264461
- for (j = 1; strlen(globalCodecParameterTable[j].m_name) > 0; ++j)
264686
+ for (j = 1; globalCodecParameterTable[j].m_name[0] != 0; ++j)
264462
264687
  {
264463
264688
  if (sqlite3_stricmp(cipherName, globalCodecParameterTable[j].m_name) == 0) break;
264464
264689
  }
264465
264690
 
264466
264691
  /* j is the index of the cipher name, if found */
264467
- 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;
264468
264693
  if (cipherParams != NULL)
264469
264694
  {
264470
264695
  /*
@@ -264504,7 +264729,7 @@ sqlite3mcConfigureFromUri(sqlite3* db, const char *zDbName, int configDefault)
264504
264729
  #endif
264505
264730
 
264506
264731
  /* Check all cipher specific parameters */
264507
- for (j = 0; strlen(cipherParams[j].m_name) > 0; ++j)
264732
+ for (j = 0; cipherParams[j].m_name[0] != 0; ++j)
264508
264733
  {
264509
264734
  if (skipLegacy && sqlite3_stricmp(cipherParams[j].m_name, "legacy") == 0) continue;
264510
264735
 
@@ -264581,15 +264806,15 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
264581
264806
  {
264582
264807
  int j = 1;
264583
264808
  /* Try to locate the cipher name */
264584
- for (j = 1; strlen(globalCodecParameterTable[j].m_name) > 0; ++j)
264809
+ for (j = 1; globalCodecParameterTable[j].m_name[0] != 0; ++j)
264585
264810
  {
264586
264811
  if (sqlite3_stricmp(pragmaValue, globalCodecParameterTable[j].m_name) == 0) break;
264587
264812
  }
264588
- 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;
264589
264814
  }
264590
264815
 
264591
264816
  /* cipherId is the numeric id of the cipher name, if found */
264592
- if ((cipherId == -1) || (cipherId > 0 && cipherId <= CODEC_TYPE_MAX))
264817
+ if ((cipherId == -1) || (cipherId > 0 && cipherId <= CODEC_COUNT_MAX))
264593
264818
  {
264594
264819
  int value;
264595
264820
  if (configDefault)
@@ -264601,7 +264826,7 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
264601
264826
  value = sqlite3mc_config(db, "cipher", cipherId);
264602
264827
  }
264603
264828
  rc = SQLITE_OK;
264604
- ((char**)pArg)[0] = sqlite3_mprintf("%s", codecDescriptorTable[value - 1]->m_name);
264829
+ ((char**)pArg)[0] = sqlite3_mprintf("%s", globalCodecDescriptorTable[value - 1].m_name);
264605
264830
  }
264606
264831
  else
264607
264832
  {
@@ -264645,10 +264870,10 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
264645
264870
  else if (sqlite3StrICmp(pragmaName, "hexkey") == 0)
264646
264871
  {
264647
264872
  int nValue = sqlite3Strlen30(pragmaValue);
264648
- if (((nValue & 1) == 0) && (sqlite3mcIsHexKey(pragmaValue, nValue) != 0))
264873
+ if (((nValue & 1) == 0) && (sqlite3mcIsHexKey((const unsigned char*) pragmaValue, nValue) != 0))
264649
264874
  {
264650
- char* zHexKey = sqlite3_malloc(nValue/2);
264651
- sqlite3mcConvertHex2Bin(pragmaValue, nValue, zHexKey);
264875
+ unsigned char* zHexKey = sqlite3_malloc(nValue/2);
264876
+ sqlite3mcConvertHex2Bin((const unsigned char*) pragmaValue, nValue, zHexKey);
264652
264877
  rc = sqlite3_key_v2(db, zDbName, zHexKey, nValue/2);
264653
264878
  sqlite3_free(zHexKey);
264654
264879
  if (rc == SQLITE_OK)
@@ -264695,10 +264920,10 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
264695
264920
  else if (sqlite3StrICmp(pragmaName, "hexrekey") == 0)
264696
264921
  {
264697
264922
  int nValue = sqlite3Strlen30(pragmaValue);
264698
- if (((nValue & 1) == 0) && (sqlite3mcIsHexKey(pragmaValue, nValue) != 0))
264923
+ if (((nValue & 1) == 0) && (sqlite3mcIsHexKey((const unsigned char*) pragmaValue, nValue) != 0))
264699
264924
  {
264700
- char* zHexKey = sqlite3_malloc(nValue/2);
264701
- sqlite3mcConvertHex2Bin(pragmaValue, nValue, zHexKey);
264925
+ unsigned char* zHexKey = sqlite3_malloc(nValue/2);
264926
+ sqlite3mcConvertHex2Bin((const unsigned char*) pragmaValue, nValue, zHexKey);
264702
264927
  rc = sqlite3_rekey_v2(db, zDbName, zHexKey, nValue/2);
264703
264928
  sqlite3_free(zHexKey);
264704
264929
  if (rc == SQLITE_OK)
@@ -264734,21 +264959,21 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
264734
264959
  CipherParams* cipherParams = NULL;
264735
264960
 
264736
264961
  /* Try to locate the cipher name */
264737
- for (j = 1; strlen(globalCodecParameterTable[j].m_name) > 0; ++j)
264962
+ for (j = 1; globalCodecParameterTable[j].m_name[0] != 0; ++j)
264738
264963
  {
264739
264964
  if (cipher == globalCodecParameterTable[j].m_id) break;
264740
264965
  }
264741
264966
 
264742
264967
  /* j is the index of the cipher name, if found */
264743
- 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;
264744
264969
  if (cipherParams != NULL)
264745
264970
  {
264746
264971
  const char* cipherName = globalCodecParameterTable[j].m_name;
264747
- for (j = 0; strlen(cipherParams[j].m_name) > 0; ++j)
264972
+ for (j = 0; cipherParams[j].m_name[0] != 0; ++j)
264748
264973
  {
264749
264974
  if (sqlite3_stricmp(pragmaName, cipherParams[j].m_name) == 0) break;
264750
264975
  }
264751
- if (strlen(cipherParams[j].m_name) > 0)
264976
+ if (cipherParams[j].m_name[0] != 0)
264752
264977
  {
264753
264978
  char* param = (configDefault) ? sqlite3_mprintf("default:%s", pragmaName) : pragmaName;
264754
264979
  if (isIntValue)
@@ -264949,7 +265174,7 @@ sqlite3mcBtreeSetPageSize(Btree* p, int pageSize, int nReserve, int iFix)
264949
265174
  ** Change 4: Call sqlite3mcBtreeSetPageSize instead of sqlite3BtreeSetPageSize for main database
264950
265175
  ** (sqlite3mcBtreeSetPageSize allows to reduce the number of reserved bytes)
264951
265176
  **
264952
- ** This code is generated by the script rekeyvacuum.sh from SQLite version 3.39.2 amalgamation.
265177
+ ** This code is generated by the script rekeyvacuum.sh from SQLite version 3.39.4 amalgamation.
264953
265178
  */
264954
265179
  SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3mcRunVacuumForRekey(
264955
265180
  char **pzErrMsg, /* Write error message here */
@@ -265626,6 +265851,10 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265626
265851
  sqlite3mcSetReadReserved(codec, nReserved);
265627
265852
  sqlite3mcSetWriteReserved(codec, nReservedWriteCipher);
265628
265853
  rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, NULL, nReservedWriteCipher);
265854
+ if (rc != SQLITE_OK && err != NULL)
265855
+ {
265856
+ sqlite3ErrorWithMsg(db, rc, err);
265857
+ }
265629
265858
  goto leave_rekey;
265630
265859
  }
265631
265860
  }
@@ -265654,6 +265883,10 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265654
265883
  sqlite3mcSetReadReserved(codec, nReserved);
265655
265884
  sqlite3mcSetWriteReserved(codec, 0);
265656
265885
  rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, NULL, 0);
265886
+ if (rc != SQLITE_OK && err != NULL)
265887
+ {
265888
+ sqlite3ErrorWithMsg(db, rc, err);
265889
+ }
265657
265890
  goto leave_rekey;
265658
265891
  }
265659
265892
  }
@@ -265675,6 +265908,10 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
265675
265908
  sqlite3mcSetReadReserved(codec, nReserved);
265676
265909
  sqlite3mcSetWriteReserved(codec, nReservedWriteCipher);
265677
265910
  rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, NULL, nReservedWriteCipher);
265911
+ if (rc != SQLITE_OK && err != NULL)
265912
+ {
265913
+ sqlite3ErrorWithMsg(db, rc, err);
265914
+ }
265678
265915
  goto leave_rekey;
265679
265916
  }
265680
265917
  }
@@ -288758,16 +288995,262 @@ sqlite3_extfunc_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *p
288758
288995
  }
288759
288996
  #endif
288760
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
+
288761
289211
  int
288762
289212
  sqlite3mc_initialize(const char* arg)
288763
289213
  {
288764
- 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
288765
289245
 
288766
289246
  /*
288767
289247
  ** Initialize and register MultiCipher VFS as default VFS
288768
289248
  ** if it isn't already registered
288769
289249
  */
288770
- rc = sqlite3mc_vfs_create(NULL, 1);
289250
+ if (rc == SQLITE_OK)
289251
+ {
289252
+ rc = sqlite3mc_vfs_create(NULL, 1);
289253
+ }
288771
289254
 
288772
289255
  /*
288773
289256
  ** Register Multi Cipher extension
@@ -288855,6 +289338,7 @@ void
288855
289338
  sqlite3mc_shutdown(void)
288856
289339
  {
288857
289340
  sqlite3mc_vfs_shutdown();
289341
+ sqlite3mcTermCipherTables();
288858
289342
  }
288859
289343
 
288860
289344
  /*