better-sqlite3-multiple-ciphers 8.7.0 → 8.7.1-beta.0

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.
package/README.md CHANGED
@@ -23,10 +23,10 @@ The fastest and simplest library for SQLite3 in Node.js. This particular fork su
23
23
  - **SQLite3 Multiple Ciphers** - [`1.6.5`](https://github.com/utelle/SQLite3MultipleCiphers/releases/tag/v1.6.5)
24
24
 
25
25
  - ### Beta
26
- - **better-sqlite3-multiple-ciphers** - [`v8.6.1-beta.0`](https://github.com/m4heshd/better-sqlite3-multiple-ciphers/releases/tag/v8.6.1-beta.0)
27
- - **better-sqlite3** - [`8.6.0`](https://github.com/JoshuaWise/better-sqlite3/releases/tag/v8.6.0)
26
+ - **better-sqlite3-multiple-ciphers** - [`v8.7.1-beta.0`](https://github.com/m4heshd/better-sqlite3-multiple-ciphers/releases/tag/v8.7.1-beta.0)
27
+ - **better-sqlite3** - [`8.7.0`](https://github.com/JoshuaWise/better-sqlite3/releases/tag/v8.7.0)
28
28
  - **SQLite** - [`3.43.1`](https://www.sqlite.org/releaselog/3_43_1.html)
29
- - **SQLite3 Multiple Ciphers** - [`1.6.5`](https://github.com/utelle/SQLite3MultipleCiphers/releases/tag/v1.6.5)
29
+ - **SQLite3 Multiple Ciphers** - [`1.7.0`](https://github.com/utelle/SQLite3MultipleCiphers/releases/tag/v1.7.0)
30
30
 
31
31
  ## Help this project stay strong! 💪
32
32
 
package/binding.gyp CHANGED
@@ -12,6 +12,7 @@
12
12
  'cflags_cc': ['-std=c++17'],
13
13
  'xcode_settings': {
14
14
  'OTHER_CPLUSPLUSFLAGS': ['-std=c++17', '-stdlib=libc++'],
15
+ 'OTHER_LDFLAGS': ['-framework Security', '-framework Foundation'],
15
16
  },
16
17
  'msvs_settings': {
17
18
  'VCCLCompilerTool': {
package/deps/setup.ps1 CHANGED
@@ -2,7 +2,7 @@
2
2
  $ErrorActionPreference = "Stop"
3
3
 
4
4
  # SQLite Info
5
- $SQLITEMC_VER = "v1.6.5"
5
+ $SQLITEMC_VER = "v1.7.0"
6
6
  $API_URL = "https://api.github.com/repos/utelle/SQLite3MultipleCiphers/releases/tags/" + $SQLITEMC_VER
7
7
 
8
8
  # Paths
@@ -11084,6 +11084,9 @@ SQLITE_PRIVATE int sqlite3mcHandleAttachKey(sqlite3*, const char*, const char*,
11084
11084
  SQLITE_PRIVATE int sqlite3mcHandleMainKey(sqlite3*, const char*);
11085
11085
  typedef struct PgHdr PgHdrMC;
11086
11086
  SQLITE_PRIVATE void* sqlite3mcPagerCodec(PgHdrMC* pPg);
11087
+ typedef struct Pager PagerMC;
11088
+ SQLITE_PRIVATE int sqlite3mcPagerHasCodec(PagerMC* pPager);
11089
+ SQLITE_PRIVATE void sqlite3mcInitMemoryMethods();
11087
11090
 
11088
11091
  /******** Begin file sqlite3rtree.h *********/
11089
11092
  /*
@@ -29807,6 +29810,12 @@ SQLITE_PRIVATE int sqlite3MallocInit(void){
29807
29810
  }
29808
29811
  rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
29809
29812
  if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
29813
+
29814
+ /* Initialize wrapper for memory management.*/
29815
+ if( rc==SQLITE_OK ) {
29816
+ sqlite3mcInitMemoryMethods();
29817
+ }
29818
+
29810
29819
  return rc;
29811
29820
  }
29812
29821
 
@@ -57156,6 +57165,7 @@ static const unsigned char aJournalMagic[] = {
57156
57165
  SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
57157
57166
  if( pPager->fd->pMethods==0 ) return 0;
57158
57167
  if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
57168
+ if( sqlite3mcPagerHasCodec(pPager) != 0 ) return 0;
57159
57169
  #ifndef SQLITE_OMIT_WAL
57160
57170
  if( pPager->pWal ){
57161
57171
  u32 iRead = 0;
@@ -57389,7 +57399,7 @@ static void setGetterMethod(Pager *pPager){
57389
57399
  if( pPager->errCode ){
57390
57400
  pPager->xGet = getPageError;
57391
57401
  #if SQLITE_MAX_MMAP_SIZE>0
57392
- }else if( USEFETCH(pPager) ){
57402
+ }else if( USEFETCH(pPager) && sqlite3mcPagerHasCodec(pPager) == 0 ){
57393
57403
  pPager->xGet = getPageMMap;
57394
57404
  #endif /* SQLITE_MAX_MMAP_SIZE>0 */
57395
57405
  }else{
@@ -251128,10 +251138,10 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
251128
251138
  #define SQLITE3MC_VERSION_H_
251129
251139
 
251130
251140
  #define SQLITE3MC_VERSION_MAJOR 1
251131
- #define SQLITE3MC_VERSION_MINOR 6
251132
- #define SQLITE3MC_VERSION_RELEASE 5
251141
+ #define SQLITE3MC_VERSION_MINOR 7
251142
+ #define SQLITE3MC_VERSION_RELEASE 0
251133
251143
  #define SQLITE3MC_VERSION_SUBRELEASE 0
251134
- #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.6.5"
251144
+ #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.7.0"
251135
251145
 
251136
251146
  #endif /* SQLITE3MC_VERSION_H_ */
251137
251147
  /*** End of #include "sqlite3mc_version.h" ***/
@@ -264601,6 +264611,31 @@ sqlite3mcVersion(sqlite3_context* context, int argc, sqlite3_value** argv)
264601
264611
  sqlite3_result_text(context, sqlite3mc_version(), -1, 0);
264602
264612
  }
264603
264613
 
264614
+ #ifndef SQLITE3MC_SECURE_MEMORY
264615
+ #define SQLITE3MC_SECURE_MEMORY 0
264616
+ #endif
264617
+
264618
+ #if SQLITE3MC_SECURE_MEMORY
264619
+
264620
+ #define SECURE_MEMORY_NONE 0
264621
+ #define SECURE_MEMORY_FILL 1
264622
+ #define SECURE_MEMORY_LOCK 2
264623
+
264624
+ SQLITE_PRIVATE void sqlite3mcSetMemorySecurity(int value);
264625
+ SQLITE_PRIVATE int sqlite3mcGetMemorySecurity();
264626
+
264627
+ #ifndef SQLITE3MC_USE_RANDOM_FILL_MEMORY
264628
+ #define SQLITE3MC_USE_RANDOM_FILL_MEMORY 0
264629
+ #endif
264630
+
264631
+ /* Memory locking is currently not supported */
264632
+ #ifdef SQLITE3MC_ENABLE_MEMLOCK
264633
+ #undef SQLITE3MC_ENABLE_MEMLOCK
264634
+ #endif
264635
+ #define SQLITE3MC_ENABLE_MEMLOCK 0
264636
+
264637
+ #endif
264638
+
264604
264639
  /*
264605
264640
  ** Crypto algorithms
264606
264641
  */
@@ -267348,14 +267383,14 @@ fail:
267348
267383
  return 0;
267349
267384
  }
267350
267385
 
267351
- #if defined(__APPLE__) && defined(__MAC_10_12) && !defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
267352
- #include <sys/random.h>
267386
+ #if defined(__APPLE__)
267387
+ #include <Security/SecRandom.h>
267353
267388
  #endif
267354
267389
 
267355
267390
  static size_t entropy(void* buf, size_t n)
267356
267391
  {
267357
- #if defined(__APPLE__) && defined(__MAC_10_12) && __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_12
267358
- if (getentropy(buf, n) == 0)
267392
+ #if defined(__APPLE__)
267393
+ if (SecRandomCopyBytes(kSecRandomDefault, n, (uint8_t*) buf) == 0)
267359
267394
  return n;
267360
267395
  #elif defined(__linux__) && defined(SYS_getrandom)
267361
267396
  if (syscall(SYS_getrandom, buf, n, 0) == n)
@@ -274343,6 +274378,54 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
274343
274378
  ((char**)pArg)[0] = sqlite3_mprintf("Malformed hex string");
274344
274379
  }
274345
274380
  }
274381
+ #if SQLITE3MC_SECURE_MEMORY
274382
+ else if (sqlite3StrICmp(pragmaName, "memory_security") == 0)
274383
+ {
274384
+ if (pragmaValue)
274385
+ {
274386
+ int intValue = -1;
274387
+ if (0 == sqlite3StrICmp(pragmaValue, "none"))
274388
+ {
274389
+ intValue = SECURE_MEMORY_NONE;
274390
+ }
274391
+ else if (0 == sqlite3StrICmp(pragmaValue, "fill") )
274392
+ {
274393
+ intValue = SECURE_MEMORY_FILL;
274394
+ }
274395
+ #if SQLITE3MC_ENABLE_MEMLOCK
274396
+ else if (0 == sqlite3StrICmp(pragmaValue, "lock") )
274397
+ {
274398
+ intValue = SECURE_MEMORY_LOCK;
274399
+ }
274400
+ #endif
274401
+ else
274402
+ {
274403
+ intValue = sqlite3Atoi(pragmaValue);
274404
+ #if SQLITE3MC_ENABLE_MEMLOCK
274405
+ intValue = (intValue >=0 && intValue <= 2) ? intValue : -1;
274406
+ #else
274407
+ intValue = (intValue >=0 && intValue <= 1) ? intValue : -1;
274408
+ #endif
274409
+ }
274410
+ if (intValue >= 0)
274411
+ {
274412
+ sqlite3mcSetMemorySecurity(intValue);
274413
+ rc = SQLITE_OK;
274414
+ ((char**)pArg)[0] = sqlite3_mprintf("%d", intValue);
274415
+ }
274416
+ else
274417
+ {
274418
+ rc = SQLITE_ERROR;
274419
+ ((char**) pArg)[0] = sqlite3_mprintf("Secure memory option '%s' invalid.", pragmaValue);
274420
+ }
274421
+ }
274422
+ else
274423
+ {
274424
+ rc = SQLITE_OK;
274425
+ ((char**)pArg)[0] = sqlite3_mprintf("%d", sqlite3mcGetMemorySecurity());
274426
+ }
274427
+ }
274428
+ #endif /* SQLITE3MC_SECURE_MEMORY */
274346
274429
  else
274347
274430
  {
274348
274431
  int j;
@@ -275418,6 +275501,212 @@ sqlite3_rekey(sqlite3 *db, const void *zKey, int nKey)
275418
275501
  /*** End of #include "codecext.c" ***/
275419
275502
 
275420
275503
 
275504
+ /*
275505
+ ** Functions for securing allocated memory
275506
+ */
275507
+ /* #include "memory_secure.c" */
275508
+ /*** Begin of #include "memory_secure.c" ***/
275509
+ /*
275510
+ ** Name: mem_secure.c
275511
+ ** Purpose: Memory manager for SQLite3 Multiple Ciphers
275512
+ ** Author: Ulrich Telle
275513
+ ** Created: 2023-09-17
275514
+ ** Copyright: (c) 2023 Ulrich Telle
275515
+ ** License: MIT
275516
+ */
275517
+
275518
+ #if SQLITE3MC_SECURE_MEMORY
275519
+
275520
+ /* Flag indicating whether securing memory allocations is initialized */
275521
+ static volatile int mcSecureMemoryInitialized = 0;
275522
+ /* Flag indicating whether memory allocations will be secured */
275523
+ static volatile int mcSecureMemoryFlag = 0;
275524
+
275525
+ /* Map of default memory allocation methods */
275526
+ static volatile sqlite3_mem_methods mcDefaultMemoryMethods;
275527
+
275528
+ #if SQLITE3MC_ENABLE_RANDOM_FILL_MEMORY
275529
+
275530
+ /*
275531
+ ** Fill a buffer with pseudo-random bytes. This is used to preset
275532
+ ** the content of a new memory allocation to unpredictable values and
275533
+ ** to clear the content of a freed allocation to unpredictable values.
275534
+ */
275535
+ static void mcRandomFill(char* pBuf, int nByte)
275536
+ {
275537
+ unsigned int x, y, r;
275538
+ x = SQLITE_PTR_TO_INT(pBuf);
275539
+ y = nByte | 1;
275540
+ while( nByte >= 4 )
275541
+ {
275542
+ x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
275543
+ y = y*1103515245 + 12345;
275544
+ r = x ^ y;
275545
+ *(int*)pBuf = r;
275546
+ pBuf += 4;
275547
+ nByte -= 4;
275548
+ }
275549
+ while( nByte-- > 0 )
275550
+ {
275551
+ x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
275552
+ y = y*1103515245 + 12345;
275553
+ r = x ^ y;
275554
+ *(pBuf++) = r & 0xff;
275555
+ }
275556
+ }
275557
+
275558
+ #endif
275559
+
275560
+ /*
275561
+ ** Return the size of an allocation
275562
+ */
275563
+ static int mcMemorySize(void* pBuf)
275564
+ {
275565
+ return mcDefaultMemoryMethods.xSize(pBuf);
275566
+ }
275567
+
275568
+ /*
275569
+ ** Memory allocation function
275570
+ */
275571
+ static void* mcMemoryAlloc(int nByte)
275572
+ {
275573
+ return mcDefaultMemoryMethods.xMalloc(nByte);
275574
+ }
275575
+
275576
+ /*
275577
+ ** Free a prior allocation
275578
+ */
275579
+ static void mcMemoryFree(void* pPrior)
275580
+ {
275581
+ if (mcSecureMemoryFlag)
275582
+ {
275583
+ #if SQLITE3MC_USE_RANDOM_FILL_MEMORY
275584
+ int nSize = mcMemorySize(pPrior);
275585
+ mcRandomFill((char*) pPrior, nSize)
275586
+ #else
275587
+ int nSize = mcMemorySize(pPrior);
275588
+ memset(pPrior, 0, nSize);
275589
+ #endif
275590
+ }
275591
+ mcDefaultMemoryMethods.xFree(pPrior);
275592
+ }
275593
+
275594
+ /*
275595
+ ** Resize an allocation
275596
+ */
275597
+ static void* mcMemoryRealloc(void* pPrior, int nByte)
275598
+ {
275599
+ void* pNew = NULL;
275600
+ if (mcSecureMemoryFlag)
275601
+ {
275602
+ int nPriorSize = mcMemorySize(pPrior);
275603
+ if (nByte == 0)
275604
+ {
275605
+ /* New size = 0, just free prior memory */
275606
+ mcMemoryFree(pPrior);
275607
+ return NULL;
275608
+ }
275609
+ else if (!pPrior)
275610
+ {
275611
+ /* Prior size = 0, just allocate new memory */
275612
+ return mcMemoryAlloc(nByte);
275613
+ }
275614
+ else if(nByte <= nPriorSize)
275615
+ {
275616
+ /* New size less or equal prior size, do nothing - we do not shrink allocations */
275617
+ return pPrior;
275618
+ }
275619
+ else
275620
+ {
275621
+ /* New size greater than prior size, reallocate memory */
275622
+ pNew = mcMemoryAlloc(nByte);
275623
+ if (pNew)
275624
+ {
275625
+ memcpy(pNew, pPrior, nPriorSize);
275626
+ mcMemoryFree(pPrior);
275627
+ }
275628
+ return pNew;
275629
+ }
275630
+ }
275631
+ else
275632
+ {
275633
+ return mcDefaultMemoryMethods.xRealloc(pPrior, nByte);
275634
+ }
275635
+ }
275636
+
275637
+ /*
275638
+ ** Round up request size to allocation size
275639
+ */
275640
+ static int mcMemoryRoundup(int nByte)
275641
+ {
275642
+ return mcDefaultMemoryMethods.xRoundup(nByte);
275643
+ }
275644
+
275645
+ /*
275646
+ ** Initialize the memory allocator
275647
+ */
275648
+ static int mcMemoryInit(void* pAppData)
275649
+ {
275650
+ return mcDefaultMemoryMethods.xInit(pAppData);
275651
+ }
275652
+
275653
+ /*
275654
+ ** Deinitialize the memory allocator
275655
+ */
275656
+ static void mcMemoryShutdown(void* pAppData)
275657
+ {
275658
+ mcDefaultMemoryMethods.xShutdown(pAppData);
275659
+ }
275660
+
275661
+ static sqlite3_mem_methods mcSecureMemoryMethods =
275662
+ {
275663
+ mcMemoryAlloc,
275664
+ mcMemoryFree,
275665
+ mcMemoryRealloc,
275666
+ mcMemorySize,
275667
+ mcMemoryRoundup,
275668
+ mcMemoryInit,
275669
+ mcMemoryShutdown,
275670
+ 0
275671
+ };
275672
+
275673
+ SQLITE_PRIVATE void sqlite3mcSetMemorySecurity(int value)
275674
+ {
275675
+ /* memory security can be changed only, if locking is not enabled */
275676
+ if (mcSecureMemoryFlag < 2)
275677
+ {
275678
+ mcSecureMemoryFlag = (value >= 0 && value <= 2) ? value : 0;
275679
+ }
275680
+ }
275681
+
275682
+ SQLITE_PRIVATE int sqlite3mcGetMemorySecurity()
275683
+ {
275684
+ return mcSecureMemoryFlag;
275685
+ }
275686
+
275687
+ #endif /* SQLITE3MC_SECURE_MEMORY */
275688
+
275689
+ SQLITE_PRIVATE void sqlite3mcInitMemoryMethods()
275690
+ {
275691
+ #if SQLITE3MC_SECURE_MEMORY
275692
+ if (!mcSecureMemoryInitialized)
275693
+ {
275694
+ if (sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mcDefaultMemoryMethods) != SQLITE_OK ||
275695
+ sqlite3_config(SQLITE_CONFIG_MALLOC, &mcSecureMemoryMethods) != SQLITE_OK)
275696
+ {
275697
+ mcSecureMemoryFlag = mcSecureMemoryInitialized = 0;
275698
+ }
275699
+ else
275700
+ {
275701
+ mcSecureMemoryInitialized = 1;
275702
+ }
275703
+ }
275704
+ #endif /* SQLITE3MC_SECURE_MEMORY */
275705
+ }
275706
+
275707
+ /*** End of #include "memory_secure.c" ***/
275708
+
275709
+
275421
275710
  /*
275422
275711
  ** Extension functions
275423
275712
  */
@@ -298610,6 +298899,37 @@ sqlite3mcCheckVfs(const char* zVfs)
298610
298899
  return rc;
298611
298900
  }
298612
298901
 
298902
+ SQLITE_PRIVATE int
298903
+ sqlite3mcPagerHasCodec(PagerMC* pPager)
298904
+ {
298905
+ int hasCodec = 0;
298906
+ sqlite3mc_vfs* pVfsMC = NULL;
298907
+ sqlite3_vfs* pVfs = pPager->pVfs;
298908
+
298909
+ /* Check whether the VFS stack of the pager contains a Multiple Ciphers VFS */
298910
+ for (; pVfs; pVfs = pVfs->pNext)
298911
+ {
298912
+ if (pVfs && pVfs->xOpen == mcVfsOpen)
298913
+ {
298914
+ /* Multiple Ciphers VFS found */
298915
+ pVfsMC = (sqlite3mc_vfs*)(pVfs);
298916
+ break;
298917
+ }
298918
+ }
298919
+
298920
+ /* Check whether codec is enabled for associated database file */
298921
+ if (pVfsMC)
298922
+ {
298923
+ sqlite3mc_file* mcFile = mcFindDbMainFileName(pVfsMC, pPager->zFilename);
298924
+ if (mcFile)
298925
+ {
298926
+ Codec* codec = mcFile->codec;
298927
+ hasCodec = (codec != 0 && sqlite3mcIsEncrypted(codec));
298928
+ }
298929
+ }
298930
+ return hasCodec;
298931
+ }
298932
+
298613
298933
  /*
298614
298934
  ** SQLite3 Multiple Ciphers external API functions
298615
298935
  */
@@ -30,10 +30,10 @@
30
30
  #define SQLITE3MC_VERSION_H_
31
31
 
32
32
  #define SQLITE3MC_VERSION_MAJOR 1
33
- #define SQLITE3MC_VERSION_MINOR 6
34
- #define SQLITE3MC_VERSION_RELEASE 5
33
+ #define SQLITE3MC_VERSION_MINOR 7
34
+ #define SQLITE3MC_VERSION_RELEASE 0
35
35
  #define SQLITE3MC_VERSION_SUBRELEASE 0
36
- #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.6.5"
36
+ #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.7.0"
37
37
 
38
38
  #endif /* SQLITE3MC_VERSION_H_ */
39
39
  /*** End of #include "sqlite3mc_version.h" ***/
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-sqlite3-multiple-ciphers",
3
- "version": "8.7.0",
3
+ "version": "8.7.1-beta.0",
4
4
  "description": "better-sqlite3 with multiple-cipher encryption support",
5
5
  "homepage": "https://github.com/m4heshd/better-sqlite3-multiple-ciphers",
6
6
  "author": "Mahesh Bandara Wijerathna (m4heshd) <m4heshd@gmail.com>",