@ohos-ports/lmdb 2.8.6-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.
Files changed (125) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +580 -0
  3. package/SECURITY.md +12 -0
  4. package/bin/download-prebuilds.js +15 -0
  5. package/binding.gyp +113 -0
  6. package/build/Release/lmdb.node +0 -0
  7. package/caching.js +212 -0
  8. package/dependencies/lmdb/libraries/liblmdb/COPYRIGHT +20 -0
  9. package/dependencies/lmdb/libraries/liblmdb/Doxyfile +1631 -0
  10. package/dependencies/lmdb/libraries/liblmdb/LICENSE +47 -0
  11. package/dependencies/lmdb/libraries/liblmdb/Makefile +136 -0
  12. package/dependencies/lmdb/libraries/liblmdb/chacha8.c +183 -0
  13. package/dependencies/lmdb/libraries/liblmdb/chacha8.h +14 -0
  14. package/dependencies/lmdb/libraries/liblmdb/crypto.c +121 -0
  15. package/dependencies/lmdb/libraries/liblmdb/intro.doc +192 -0
  16. package/dependencies/lmdb/libraries/liblmdb/lmdb.h +1817 -0
  17. package/dependencies/lmdb/libraries/liblmdb/mdb.c +12555 -0
  18. package/dependencies/lmdb/libraries/liblmdb/mdb_copy.1 +74 -0
  19. package/dependencies/lmdb/libraries/liblmdb/mdb_copy.c +106 -0
  20. package/dependencies/lmdb/libraries/liblmdb/mdb_drop.1 +53 -0
  21. package/dependencies/lmdb/libraries/liblmdb/mdb_drop.c +154 -0
  22. package/dependencies/lmdb/libraries/liblmdb/mdb_dump.1 +94 -0
  23. package/dependencies/lmdb/libraries/liblmdb/mdb_dump.c +333 -0
  24. package/dependencies/lmdb/libraries/liblmdb/mdb_load.1 +97 -0
  25. package/dependencies/lmdb/libraries/liblmdb/mdb_load.c +530 -0
  26. package/dependencies/lmdb/libraries/liblmdb/mdb_stat.1 +83 -0
  27. package/dependencies/lmdb/libraries/liblmdb/mdb_stat.c +276 -0
  28. package/dependencies/lmdb/libraries/liblmdb/midl-sparse.c +452 -0
  29. package/dependencies/lmdb/libraries/liblmdb/midl.c +452 -0
  30. package/dependencies/lmdb/libraries/liblmdb/midl.h +208 -0
  31. package/dependencies/lmdb/libraries/liblmdb/module.c +101 -0
  32. package/dependencies/lmdb/libraries/liblmdb/module.h +16 -0
  33. package/dependencies/lmdb/libraries/liblmdb/mtest.c +178 -0
  34. package/dependencies/lmdb/libraries/liblmdb/mtest2.c +124 -0
  35. package/dependencies/lmdb/libraries/liblmdb/mtest3.c +133 -0
  36. package/dependencies/lmdb/libraries/liblmdb/mtest4.c +168 -0
  37. package/dependencies/lmdb/libraries/liblmdb/mtest5.c +135 -0
  38. package/dependencies/lmdb/libraries/liblmdb/mtest6.c +141 -0
  39. package/dependencies/lmdb/libraries/liblmdb/mtest_enc.c +190 -0
  40. package/dependencies/lmdb/libraries/liblmdb/mtest_enc2.c +189 -0
  41. package/dependencies/lmdb/libraries/liblmdb/mtest_remap.c +177 -0
  42. package/dependencies/lmdb/libraries/liblmdb/sample-bdb.txt +73 -0
  43. package/dependencies/lmdb/libraries/liblmdb/sample-mdb.txt +62 -0
  44. package/dependencies/lmdb/libraries/liblmdb/tooltag +27 -0
  45. package/dependencies/lmdb-data-v1/libraries/liblmdb/CHANGES +266 -0
  46. package/dependencies/lmdb-data-v1/libraries/liblmdb/COPYRIGHT +20 -0
  47. package/dependencies/lmdb-data-v1/libraries/liblmdb/Doxyfile +1631 -0
  48. package/dependencies/lmdb-data-v1/libraries/liblmdb/LICENSE +47 -0
  49. package/dependencies/lmdb-data-v1/libraries/liblmdb/intro.doc +192 -0
  50. package/dependencies/lmdb-data-v1/libraries/liblmdb/lmdb.h +1608 -0
  51. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb.c +10320 -0
  52. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_copy.1 +55 -0
  53. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_copy.c +82 -0
  54. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_dump.1 +75 -0
  55. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_dump.c +319 -0
  56. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_load.1 +84 -0
  57. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_load.c +496 -0
  58. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_stat.1 +64 -0
  59. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_stat.c +263 -0
  60. package/dependencies/lmdb-data-v1/libraries/liblmdb/midl.c +359 -0
  61. package/dependencies/lmdb-data-v1/libraries/liblmdb/midl.h +186 -0
  62. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest.c +177 -0
  63. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest2.c +124 -0
  64. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest3.c +133 -0
  65. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest4.c +168 -0
  66. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest5.c +135 -0
  67. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest6.c +141 -0
  68. package/dependencies/lmdb-data-v1/libraries/liblmdb/sample-bdb.txt +73 -0
  69. package/dependencies/lmdb-data-v1/libraries/liblmdb/sample-mdb.txt +62 -0
  70. package/dependencies/lmdb-data-v1/libraries/liblmdb/tooltag +22 -0
  71. package/dependencies/lz4/LICENSE +11 -0
  72. package/dependencies/lz4/lib/LICENSE +24 -0
  73. package/dependencies/lz4/lib/Makefile +225 -0
  74. package/dependencies/lz4/lib/README.md +169 -0
  75. package/dependencies/lz4/lib/dll/example/Makefile +63 -0
  76. package/dependencies/lz4/lib/dll/example/README.md +69 -0
  77. package/dependencies/lz4/lib/dll/example/fullbench-dll.sln +25 -0
  78. package/dependencies/lz4/lib/dll/example/fullbench-dll.vcxproj +182 -0
  79. package/dependencies/lz4/lib/dll/liblz4.def +62 -0
  80. package/dependencies/lz4/lib/liblz4-dll.rc.in +35 -0
  81. package/dependencies/lz4/lib/liblz4.pc.in +14 -0
  82. package/dependencies/lz4/lib/lz4.c +2722 -0
  83. package/dependencies/lz4/lib/lz4.h +842 -0
  84. package/dependencies/lz4/lib/lz4file.c +311 -0
  85. package/dependencies/lz4/lib/lz4file.h +93 -0
  86. package/dependencies/lz4/lib/lz4frame.c +2078 -0
  87. package/dependencies/lz4/lib/lz4frame.h +692 -0
  88. package/dependencies/lz4/lib/lz4frame_static.h +47 -0
  89. package/dependencies/lz4/lib/lz4hc.c +1631 -0
  90. package/dependencies/lz4/lib/lz4hc.h +413 -0
  91. package/dependencies/lz4/lib/xxhash.c +1030 -0
  92. package/dependencies/lz4/lib/xxhash.h +328 -0
  93. package/dependencies/v8/v8-fast-api-calls-v16.h +791 -0
  94. package/dependencies/v8/v8-fast-api-calls.h +888 -0
  95. package/dict/dict.txt +1 -0
  96. package/dict/dict2.txt +1 -0
  97. package/dist/index.cjs +2950 -0
  98. package/dist/index.cjs.map +1 -0
  99. package/index.d.cts +420 -0
  100. package/index.d.ts +450 -0
  101. package/index.js +38 -0
  102. package/keys.js +116 -0
  103. package/level.js +27 -0
  104. package/native.js +85 -0
  105. package/node-index.js +32 -0
  106. package/open.js +448 -0
  107. package/package.json +124 -0
  108. package/read.js +833 -0
  109. package/rollup.config.js +12 -0
  110. package/src/compression.cpp +225 -0
  111. package/src/cursor.cpp +395 -0
  112. package/src/dbi.cpp +382 -0
  113. package/src/env.cpp +1030 -0
  114. package/src/lmdb-js.cpp +64 -0
  115. package/src/lmdb-js.h +633 -0
  116. package/src/misc.cpp +593 -0
  117. package/src/ordered-binary.cpp +46 -0
  118. package/src/txn.cpp +210 -0
  119. package/src/v8-functions.cpp +170 -0
  120. package/src/windows.c +35 -0
  121. package/src/writer.cpp +537 -0
  122. package/util/RangeIterable.js +236 -0
  123. package/util/set-optional-deps.cjs +14 -0
  124. package/util/when.js +8 -0
  125. package/write.js +912 -0
@@ -0,0 +1,47 @@
1
+ The OpenLDAP Public License
2
+ Version 2.8, 17 August 2003
3
+
4
+ Redistribution and use of this software and associated documentation
5
+ ("Software"), with or without modification, are permitted provided
6
+ that the following conditions are met:
7
+
8
+ 1. Redistributions in source form must retain copyright statements
9
+ and notices,
10
+
11
+ 2. Redistributions in binary form must reproduce applicable copyright
12
+ statements and notices, this list of conditions, and the following
13
+ disclaimer in the documentation and/or other materials provided
14
+ with the distribution, and
15
+
16
+ 3. Redistributions must contain a verbatim copy of this document.
17
+
18
+ The OpenLDAP Foundation may revise this license from time to time.
19
+ Each revision is distinguished by a version number. You may use
20
+ this Software under terms of this license revision or under the
21
+ terms of any subsequent revision of the license.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE OPENLDAP FOUNDATION AND ITS
24
+ CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
25
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26
+ AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27
+ SHALL THE OPENLDAP FOUNDATION, ITS CONTRIBUTORS, OR THE AUTHOR(S)
28
+ OR OWNER(S) OF THE SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
29
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
+ POSSIBILITY OF SUCH DAMAGE.
36
+
37
+ The names of the authors and copyright holders must not be used in
38
+ advertising or otherwise to promote the sale, use or other dealing
39
+ in this Software without specific, written prior permission. Title
40
+ to copyright in this Software shall at all times remain with copyright
41
+ holders.
42
+
43
+ OpenLDAP is a registered trademark of the OpenLDAP Foundation.
44
+
45
+ Copyright 1999-2003 The OpenLDAP Foundation, Redwood City,
46
+ California, USA. All Rights Reserved. Permission to copy and
47
+ distribute verbatim copies of this document is granted.
@@ -0,0 +1,136 @@
1
+ # Makefile for liblmdb (Lightning memory-mapped database library).
2
+
3
+ ########################################################################
4
+ # Configuration. The compiler options must enable threaded compilation.
5
+ #
6
+ # Preprocessor macros (for CPPFLAGS) of interest...
7
+ # Note that the defaults should already be correct for most
8
+ # platforms; you should not need to change any of these.
9
+ # Read their descriptions in mdb.c if you do:
10
+ #
11
+ # - MDB_USE_POSIX_MUTEX, MDB_USE_POSIX_SEM, MDB_USE_SYSV_SEM
12
+ # - MDB_DSYNC
13
+ # - MDB_FDATASYNC
14
+ # - MDB_FDATASYNC_WORKS
15
+ # - MDB_USE_PWRITEV
16
+ # - MDB_USE_ROBUST
17
+ #
18
+ # There may be other macros in mdb.c of interest. You should
19
+ # read mdb.c before changing any of them.
20
+ #
21
+ CC = gcc
22
+ AR = ar
23
+ W = -W -Wall -Wno-unused-parameter -Wbad-function-cast -Wuninitialized
24
+ THREADS = -pthread
25
+ OPT = -O2 -g
26
+ CFLAGS = $(THREADS) $(OPT) $(W) $(XCFLAGS)
27
+ LDFLAGS = $(THREADS)
28
+ LDLIBS =
29
+ SOLIBS =
30
+ SOEXT = .so
31
+ LDL = -ldl
32
+ prefix = /usr/local
33
+ exec_prefix = $(prefix)
34
+ bindir = $(exec_prefix)/bin
35
+ libdir = $(exec_prefix)/lib
36
+ includedir = $(prefix)/include
37
+ datarootdir = $(prefix)/share
38
+ mandir = $(datarootdir)/man
39
+
40
+ ########################################################################
41
+
42
+ IHDRS = lmdb.h
43
+ ILIBS = liblmdb.a liblmdb$(SOEXT)
44
+ IPROGS = mdb_stat mdb_copy mdb_dump mdb_load mdb_drop
45
+ IDOCS = mdb_stat.1 mdb_copy.1 mdb_dump.1 mdb_load.1 mdb_drop.1
46
+ PROGS = $(IPROGS) mtest mtest2 mtest3 mtest4 mtest5
47
+ RPROGS = mtest_remap mtest_enc mtest_enc2
48
+
49
+ all: $(ILIBS) $(PROGS)
50
+ # Requires CPPFLAGS=-DMDB_VL32 and/or -DMDB_RPAGE_CACHE
51
+ rall: all $(RPROGS)
52
+
53
+ install: $(ILIBS) $(IPROGS) $(IHDRS)
54
+ mkdir -p $(DESTDIR)$(bindir)
55
+ mkdir -p $(DESTDIR)$(libdir)
56
+ mkdir -p $(DESTDIR)$(includedir)
57
+ mkdir -p $(DESTDIR)$(mandir)/man1
58
+ for f in $(IPROGS); do cp $$f $(DESTDIR)$(bindir); done
59
+ for f in $(ILIBS); do cp $$f $(DESTDIR)$(libdir); done
60
+ for f in $(IHDRS); do cp $$f $(DESTDIR)$(includedir); done
61
+ for f in $(IDOCS); do cp $$f $(DESTDIR)$(mandir)/man1; done
62
+
63
+ clean:
64
+ rm -rf $(PROGS) $(RPROGS) *.[ao] *.[ls]o *~ testdb
65
+
66
+ test: all
67
+ rm -rf testdb && mkdir testdb
68
+ ./mtest && ./mdb_stat testdb
69
+
70
+ liblmdb.a: mdb.o midl.o
71
+ $(AR) rs $@ mdb.o midl.o
72
+
73
+ liblmdb$(SOEXT): mdb.lo midl.lo
74
+ # $(CC) $(LDFLAGS) -pthread -shared -Wl,-Bsymbolic -o $@ mdb.o midl.o $(SOLIBS)
75
+ $(CC) $(LDFLAGS) -pthread -shared -o $@ mdb.lo midl.lo $(SOLIBS)
76
+
77
+ mdb_stat: mdb_stat.o module.o liblmdb.a
78
+ $(CC) $(LDFLAGS) -o $@ $^ $(LDL)
79
+ mdb_copy: mdb_copy.o module.o liblmdb.a
80
+ $(CC) $(LDFLAGS) -o $@ $^ $(LDL)
81
+ mdb_dump: mdb_dump.o module.o liblmdb.a
82
+ $(CC) $(LDFLAGS) -o $@ $^ $(LDL)
83
+ mdb_load: mdb_load.o module.o liblmdb.a
84
+ $(CC) $(LDFLAGS) -o $@ $^ $(LDL)
85
+ mdb_drop: mdb_drop.o module.o liblmdb.a
86
+ $(CC) $(LDFLAGS) -o $@ $^ $(LDL)
87
+ mtest: mtest.o liblmdb.a
88
+ mtest2: mtest2.o liblmdb.a
89
+ mtest3: mtest3.o liblmdb.a
90
+ mtest4: mtest4.o liblmdb.a
91
+ mtest5: mtest5.o liblmdb.a
92
+ mtest6: mtest6.o liblmdb.a
93
+ mtest_remap: mtest_remap.o liblmdb.a
94
+ mtest_enc: mtest_enc.o chacha8.o liblmdb.a
95
+ mtest_enc2: mtest_enc2.o module.o liblmdb.a crypto.lm
96
+ $(CC) $(LDFLAGS) -pthread -o $@ mtest_enc2.o module.o liblmdb.a $(LDL)
97
+
98
+ crypto.lm: crypto.c
99
+ $(CC) -shared -o $@ -lcrypto
100
+
101
+ mdb.o: mdb.c lmdb.h midl.h
102
+ $(CC) $(CFLAGS) $(CPPFLAGS) -c mdb.c
103
+
104
+ midl.o: midl.c midl.h
105
+ $(CC) $(CFLAGS) $(CPPFLAGS) -c midl.c
106
+
107
+ mdb.lo: mdb.c lmdb.h midl.h
108
+ $(CC) $(CFLAGS) -fPIC $(CPPFLAGS) -c mdb.c -o $@
109
+
110
+ midl.lo: midl.c midl.h
111
+ $(CC) $(CFLAGS) -fPIC $(CPPFLAGS) -c midl.c -o $@
112
+
113
+ %: %.o
114
+ $(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
115
+
116
+ %.o: %.c lmdb.h
117
+ $(CC) $(CFLAGS) $(CPPFLAGS) -c $<
118
+
119
+ COV_FLAGS=-fprofile-arcs -ftest-coverage
120
+ COV_OBJS=xmdb.o xmidl.o
121
+
122
+ coverage: xmtest
123
+ for i in mtest*.c [0-9]*.c; do j=`basename \$$i .c`; $(MAKE) $$j.o; \
124
+ gcc -o x$$j $$j.o $(COV_OBJS) -pthread $(COV_FLAGS); \
125
+ rm -rf testdb; mkdir testdb; ./x$$j; done
126
+ gcov xmdb.c
127
+ gcov xmidl.c
128
+
129
+ xmtest: mtest.o xmdb.o xmidl.o
130
+ gcc -o xmtest mtest.o xmdb.o xmidl.o -pthread $(COV_FLAGS)
131
+
132
+ xmdb.o: mdb.c lmdb.h midl.h
133
+ $(CC) $(CFLAGS) -fPIC $(CPPFLAGS) -O0 $(COV_FLAGS) -c mdb.c -o $@
134
+
135
+ xmidl.o: midl.c midl.h
136
+ $(CC) $(CFLAGS) -fPIC $(CPPFLAGS) -O0 $(COV_FLAGS) -c midl.c -o $@
@@ -0,0 +1,183 @@
1
+ /*
2
+ chacha-merged.c version 20080118
3
+ D. J. Bernstein
4
+ Public domain.
5
+ */
6
+
7
+ #include <memory.h>
8
+ #include <stdio.h>
9
+ //#include <sys/param.h>
10
+
11
+ #include "chacha8.h"
12
+ #if 0
13
+ #include "common/int-util.h"
14
+ #include "warnings.h"
15
+ #endif
16
+
17
+ #if BYTE_ORDER == LITTLE_ENDIAN
18
+ #define SWAP32LE(x) (x)
19
+ #else
20
+ #define SWAP32LE(x) ((((uint32_t) (x) & 0x000000ff) << 24) | \
21
+ (((uint32_t) (x) & 0x0000ff00) << 8) | \
22
+ (((uint32_t) (x) & 0x00ff0000) >> 8) | \
23
+ (((uint32_t) (x) & 0xff000000) >> 24))
24
+ #endif
25
+
26
+ /*
27
+ * The following macros are used to obtain exact-width results.
28
+ */
29
+ #define U8V(v) ((uint8_t)(v) & UINT8_C(0xFF))
30
+ #define U32V(v) ((uint32_t)(v) & UINT32_C(0xFFFFFFFF))
31
+
32
+ /*
33
+ * The following macros load words from an array of bytes with
34
+ * different types of endianness, and vice versa.
35
+ */
36
+ #define U8TO32_LITTLE(p) SWAP32LE(((uint32_t*)(p))[0])
37
+ #define U32TO8_LITTLE(p, v) (((uint32_t*)(p))[0] = SWAP32LE(v))
38
+
39
+ #define ROTATE(v,c) (rol32(v,c))
40
+ #define XOR(v,w) ((v) ^ (w))
41
+ #define PLUS(v,w) (U32V((v) + (w)))
42
+ #define PLUSONE(v) (PLUS((v),1))
43
+
44
+ #define QUARTERROUND(a,b,c,d) \
45
+ a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
46
+ c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
47
+ a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
48
+ c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
49
+
50
+ static const char sigma[] = "expand 32-byte k";
51
+
52
+ static uint32_t rol32(uint32_t x, int r) {
53
+ return (x << (r & 31)) | (x >> (-r & 31));
54
+ }
55
+
56
+ void chacha8(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher) {
57
+ uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
58
+ uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
59
+ char* ctarget = 0;
60
+ char tmp[64];
61
+ int i;
62
+
63
+ if (!length) return;
64
+
65
+ j0 = U8TO32_LITTLE(sigma + 0);
66
+ j1 = U8TO32_LITTLE(sigma + 4);
67
+ j2 = U8TO32_LITTLE(sigma + 8);
68
+ j3 = U8TO32_LITTLE(sigma + 12);
69
+ j4 = U8TO32_LITTLE(key + 0);
70
+ j5 = U8TO32_LITTLE(key + 4);
71
+ j6 = U8TO32_LITTLE(key + 8);
72
+ j7 = U8TO32_LITTLE(key + 12);
73
+ j8 = U8TO32_LITTLE(key + 16);
74
+ j9 = U8TO32_LITTLE(key + 20);
75
+ j10 = U8TO32_LITTLE(key + 24);
76
+ j11 = U8TO32_LITTLE(key + 28);
77
+ j12 = 0;
78
+ j13 = 0;
79
+ j14 = U8TO32_LITTLE(iv + 0);
80
+ j15 = U8TO32_LITTLE(iv + 4);
81
+
82
+ for (;;) {
83
+ if (length < 64) {
84
+ memcpy(tmp, data, length);
85
+ data = tmp;
86
+ ctarget = cipher;
87
+ cipher = tmp;
88
+ }
89
+ x0 = j0;
90
+ x1 = j1;
91
+ x2 = j2;
92
+ x3 = j3;
93
+ x4 = j4;
94
+ x5 = j5;
95
+ x6 = j6;
96
+ x7 = j7;
97
+ x8 = j8;
98
+ x9 = j9;
99
+ x10 = j10;
100
+ x11 = j11;
101
+ x12 = j12;
102
+ x13 = j13;
103
+ x14 = j14;
104
+ x15 = j15;
105
+ for (i = 8;i > 0;i -= 2) {
106
+ QUARTERROUND( x0, x4, x8,x12)
107
+ QUARTERROUND( x1, x5, x9,x13)
108
+ QUARTERROUND( x2, x6,x10,x14)
109
+ QUARTERROUND( x3, x7,x11,x15)
110
+ QUARTERROUND( x0, x5,x10,x15)
111
+ QUARTERROUND( x1, x6,x11,x12)
112
+ QUARTERROUND( x2, x7, x8,x13)
113
+ QUARTERROUND( x3, x4, x9,x14)
114
+ }
115
+ x0 = PLUS( x0, j0);
116
+ x1 = PLUS( x1, j1);
117
+ x2 = PLUS( x2, j2);
118
+ x3 = PLUS( x3, j3);
119
+ x4 = PLUS( x4, j4);
120
+ x5 = PLUS( x5, j5);
121
+ x6 = PLUS( x6, j6);
122
+ x7 = PLUS( x7, j7);
123
+ x8 = PLUS( x8, j8);
124
+ x9 = PLUS( x9, j9);
125
+ x10 = PLUS(x10,j10);
126
+ x11 = PLUS(x11,j11);
127
+ x12 = PLUS(x12,j12);
128
+ x13 = PLUS(x13,j13);
129
+ x14 = PLUS(x14,j14);
130
+ x15 = PLUS(x15,j15);
131
+
132
+ x0 = XOR( x0,U8TO32_LITTLE((uint8_t*)data + 0));
133
+ x1 = XOR( x1,U8TO32_LITTLE((uint8_t*)data + 4));
134
+ x2 = XOR( x2,U8TO32_LITTLE((uint8_t*)data + 8));
135
+ x3 = XOR( x3,U8TO32_LITTLE((uint8_t*)data + 12));
136
+ x4 = XOR( x4,U8TO32_LITTLE((uint8_t*)data + 16));
137
+ x5 = XOR( x5,U8TO32_LITTLE((uint8_t*)data + 20));
138
+ x6 = XOR( x6,U8TO32_LITTLE((uint8_t*)data + 24));
139
+ x7 = XOR( x7,U8TO32_LITTLE((uint8_t*)data + 28));
140
+ x8 = XOR( x8,U8TO32_LITTLE((uint8_t*)data + 32));
141
+ x9 = XOR( x9,U8TO32_LITTLE((uint8_t*)data + 36));
142
+ x10 = XOR(x10,U8TO32_LITTLE((uint8_t*)data + 40));
143
+ x11 = XOR(x11,U8TO32_LITTLE((uint8_t*)data + 44));
144
+ x12 = XOR(x12,U8TO32_LITTLE((uint8_t*)data + 48));
145
+ x13 = XOR(x13,U8TO32_LITTLE((uint8_t*)data + 52));
146
+ x14 = XOR(x14,U8TO32_LITTLE((uint8_t*)data + 56));
147
+ x15 = XOR(x15,U8TO32_LITTLE((uint8_t*)data + 60));
148
+
149
+ j12 = PLUSONE(j12);
150
+ if (!j12)
151
+ {
152
+ j13 = PLUSONE(j13);
153
+ /* stopping at 2^70 bytes per iv is user's responsibility */
154
+ }
155
+
156
+ U32TO8_LITTLE(cipher + 0,x0);
157
+ U32TO8_LITTLE(cipher + 4,x1);
158
+ U32TO8_LITTLE(cipher + 8,x2);
159
+ U32TO8_LITTLE(cipher + 12,x3);
160
+ U32TO8_LITTLE(cipher + 16,x4);
161
+ U32TO8_LITTLE(cipher + 20,x5);
162
+ U32TO8_LITTLE(cipher + 24,x6);
163
+ U32TO8_LITTLE(cipher + 28,x7);
164
+ U32TO8_LITTLE(cipher + 32,x8);
165
+ U32TO8_LITTLE(cipher + 36,x9);
166
+ U32TO8_LITTLE(cipher + 40,x10);
167
+ U32TO8_LITTLE(cipher + 44,x11);
168
+ U32TO8_LITTLE(cipher + 48,x12);
169
+ U32TO8_LITTLE(cipher + 52,x13);
170
+ U32TO8_LITTLE(cipher + 56,x14);
171
+ U32TO8_LITTLE(cipher + 60,x15);
172
+
173
+ if (length <= 64) {
174
+ if (length < 64) {
175
+ memcpy(ctarget, cipher, length);
176
+ }
177
+ return;
178
+ }
179
+ length -= 64;
180
+ cipher += 64;
181
+ data = (uint8_t*)data + 64;
182
+ }
183
+ }
@@ -0,0 +1,14 @@
1
+ #include <stdint.h>
2
+ #include <stddef.h>
3
+ #ifdef __cplusplus
4
+ extern "C" {
5
+ #endif
6
+ void chacha8(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher);
7
+
8
+ #define CHACHA8_KEY_SIZE 32
9
+ #define CHACHA8_IV_SIZE 8
10
+
11
+ #ifdef __cplusplus
12
+ }
13
+ #endif
14
+
@@ -0,0 +1,121 @@
1
+ /* crypto.c - LMDB encryption helper module */
2
+ /*
3
+ * Copyright 2020-2021 Howard Chu, Symas Corp.
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted only as authorized by the Symas
8
+ * Dual-Use License.
9
+ *
10
+ * A copy of this license is available in the file LICENSE in the
11
+ * source distribution.
12
+ */
13
+ #include <string.h>
14
+
15
+ #include <openssl/engine.h>
16
+
17
+ #include "lmdb.h"
18
+
19
+ MDB_crypto_hooks MDB_crypto;
20
+
21
+ static EVP_CIPHER *cipher;
22
+
23
+ static int mcf_str2key(const char *passwd, MDB_val *key)
24
+ {
25
+ unsigned int size;
26
+ EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
27
+ EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL);
28
+ EVP_DigestUpdate(mdctx, "Just a Constant", sizeof("Just a Constant"));
29
+ EVP_DigestUpdate(mdctx, passwd, strlen(passwd));
30
+ EVP_DigestFinal_ex(mdctx, key->mv_data, &size);
31
+ EVP_MD_CTX_free(mdctx);
32
+ return 0;
33
+ }
34
+
35
+ /* cheats - internal OpenSSL 1.1 structures */
36
+ typedef struct evp_cipher_ctx_st {
37
+ const EVP_CIPHER *cipher;
38
+ ENGINE *engine; /* functional reference if 'cipher' is
39
+ * ENGINE-provided */
40
+ int encrypt; /* encrypt or decrypt */
41
+ int buf_len; /* number we have left */
42
+ unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
43
+ unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
44
+ unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
45
+ int num; /* used by cfb/ofb/ctr mode */
46
+ /* FIXME: Should this even exist? It appears unused */
47
+ void *app_data; /* application stuff */
48
+ int key_len; /* May change for variable length cipher */
49
+ unsigned long flags; /* Various flags */
50
+ void *cipher_data; /* per EVP data */
51
+ int final_used;
52
+ int block_mask;
53
+ unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
54
+ } EVP_CIPHER_CTX;
55
+
56
+ #define CHACHA_KEY_SIZE 32
57
+ #define CHACHA_CTR_SIZE 16
58
+ #define CHACHA_BLK_SIZE 64
59
+ #define POLY1305_BLOCK_SIZE 16
60
+
61
+ typedef struct {
62
+ union {
63
+ double align; /* this ensures even sizeof(EVP_CHACHA_KEY)%8==0 */
64
+ unsigned int d[CHACHA_KEY_SIZE / 4];
65
+ } key;
66
+ unsigned int counter[CHACHA_CTR_SIZE / 4];
67
+ unsigned char buf[CHACHA_BLK_SIZE];
68
+ unsigned int partial_len;
69
+ } EVP_CHACHA_KEY;
70
+
71
+ typedef struct {
72
+ EVP_CHACHA_KEY key;
73
+ unsigned int nonce[12/4];
74
+ unsigned char tag[POLY1305_BLOCK_SIZE];
75
+ unsigned char tls_aad[POLY1305_BLOCK_SIZE];
76
+ struct { uint64_t aad, text; } len;
77
+ int aad, mac_inited, tag_len, nonce_len;
78
+ size_t tls_payload_length;
79
+ } EVP_CHACHA_AEAD_CTX;
80
+
81
+ static int mcf_encfunc(const MDB_val *src, MDB_val *dst, const MDB_val *key, int encdec)
82
+ {
83
+ unsigned char iv[12];
84
+ int ivl, outl, rc;
85
+ mdb_size_t *ptr;
86
+ EVP_CIPHER_CTX ctx = {0};
87
+ EVP_CHACHA_AEAD_CTX cactx;
88
+
89
+ ctx.cipher_data = &cactx;
90
+ ptr = key[1].mv_data;
91
+ ivl = ptr[0] & 0xffffffff;
92
+ memcpy(iv, &ivl, 4);
93
+ memcpy(iv+4, ptr+1, sizeof(mdb_size_t));
94
+ EVP_CipherInit_ex(&ctx, cipher, NULL, key[0].mv_data, iv, encdec);
95
+ EVP_CIPHER_CTX_set_padding(&ctx, 0);
96
+ if (!encdec) {
97
+ EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_AEAD_SET_TAG, key[2].mv_size, key[2].mv_data);
98
+ }
99
+ rc = EVP_CipherUpdate(&ctx, dst->mv_data, &outl, src->mv_data, src->mv_size);
100
+ if (rc)
101
+ rc = EVP_CipherFinal_ex(&ctx, key[2].mv_data, &outl);
102
+ if (rc && encdec) {
103
+ EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_AEAD_GET_TAG, key[2].mv_size, key[2].mv_data);
104
+ }
105
+ return rc == 0;
106
+ }
107
+
108
+ static const MDB_crypto_funcs mcf_table = {
109
+ mcf_str2key,
110
+ mcf_encfunc,
111
+ NULL,
112
+ CHACHA_KEY_SIZE,
113
+ POLY1305_BLOCK_SIZE,
114
+ 0
115
+ };
116
+
117
+ MDB_crypto_funcs *MDB_crypto()
118
+ {
119
+ cipher = (EVP_CIPHER *)EVP_chacha20_poly1305();
120
+ return (MDB_crypto_funcs *)&mcf_table;
121
+ }
@@ -0,0 +1,192 @@
1
+ /*
2
+ * Copyright 2015-2021 Howard Chu, Symas Corp.
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted only as authorized by the OpenLDAP
7
+ * Public License.
8
+ *
9
+ * A copy of this license is available in the file LICENSE in the
10
+ * top-level directory of the distribution or, alternatively, at
11
+ * <http://www.OpenLDAP.org/license.html>.
12
+ */
13
+ /** @page starting Getting Started
14
+
15
+ LMDB is compact, fast, powerful, and robust and implements a simplified
16
+ variant of the BerkeleyDB (BDB) API. (BDB is also very powerful, and verbosely
17
+ documented in its own right.) After reading this page, the main
18
+ \ref mdb documentation should make sense. Thanks to Bert Hubert
19
+ for creating the
20
+ <a href="https://github.com/ahupowerdns/ahutils/blob/master/lmdb-semantics.md">
21
+ initial version</a> of this writeup.
22
+
23
+ Everything starts with an environment, created by #mdb_env_create().
24
+ Once created, this environment must also be opened with #mdb_env_open().
25
+
26
+ #mdb_env_open() gets passed a name which is interpreted as a directory
27
+ path. Note that this directory must exist already, it is not created
28
+ for you. Within that directory, a lock file and a storage file will be
29
+ generated. If you don't want to use a directory, you can pass the
30
+ #MDB_NOSUBDIR option, in which case the path you provided is used
31
+ directly as the data file, and another file with a "-lock" suffix
32
+ added will be used for the lock file.
33
+
34
+ Once the environment is open, a transaction can be created within it
35
+ using #mdb_txn_begin(). Transactions may be read-write or read-only,
36
+ and read-write transactions may be nested. A transaction must only
37
+ be used by one thread at a time. Transactions are always required,
38
+ even for read-only access. The transaction provides a consistent
39
+ view of the data.
40
+
41
+ Once a transaction has been created, a database can be opened within it
42
+ using #mdb_dbi_open(). If only one database will ever be used in the
43
+ environment, a NULL can be passed as the database name. For named
44
+ databases, the #MDB_CREATE flag must be used to create the database
45
+ if it doesn't already exist. Also, #mdb_env_set_maxdbs() must be
46
+ called after #mdb_env_create() and before #mdb_env_open() to set the
47
+ maximum number of named databases you want to support.
48
+
49
+ Note: a single transaction can open multiple databases. Generally
50
+ databases should only be opened once, by the first transaction in
51
+ the process. After the first transaction completes, the database
52
+ handles can freely be used by all subsequent transactions.
53
+
54
+ Within a transaction, #mdb_get() and #mdb_put() can store single
55
+ key/value pairs if that is all you need to do (but see \ref Cursors
56
+ below if you want to do more).
57
+
58
+ A key/value pair is expressed as two #MDB_val structures. This struct
59
+ has two fields, \c mv_size and \c mv_data. The data is a \c void pointer to
60
+ an array of \c mv_size bytes.
61
+
62
+ Because LMDB is very efficient (and usually zero-copy), the data returned
63
+ in an #MDB_val structure may be memory-mapped straight from disk. In
64
+ other words <b>look but do not touch</b> (or free() for that matter).
65
+ Once a transaction is closed, the values can no longer be used, so
66
+ make a copy if you need to keep them after that.
67
+
68
+ @section Cursors Cursors
69
+
70
+ To do more powerful things, we must use a cursor.
71
+
72
+ Within the transaction, a cursor can be created with #mdb_cursor_open().
73
+ With this cursor we can store/retrieve/delete (multiple) values using
74
+ #mdb_cursor_get(), #mdb_cursor_put(), and #mdb_cursor_del().
75
+
76
+ #mdb_cursor_get() positions itself depending on the cursor operation
77
+ requested, and for some operations, on the supplied key. For example,
78
+ to list all key/value pairs in a database, use operation #MDB_FIRST for
79
+ the first call to #mdb_cursor_get(), and #MDB_NEXT on subsequent calls,
80
+ until the end is hit.
81
+
82
+ To retrieve all keys starting from a specified key value, use #MDB_SET.
83
+ For more cursor operations, see the \ref mdb docs.
84
+
85
+ When using #mdb_cursor_put(), either the function will position the
86
+ cursor for you based on the \b key, or you can use operation
87
+ #MDB_CURRENT to use the current position of the cursor. Note that
88
+ \b key must then match the current position's key.
89
+
90
+ @subsection summary Summarizing the Opening
91
+
92
+ So we have a cursor in a transaction which opened a database in an
93
+ environment which is opened from a filesystem after it was
94
+ separately created.
95
+
96
+ Or, we create an environment, open it from a filesystem, create a
97
+ transaction within it, open a database within that transaction,
98
+ and create a cursor within all of the above.
99
+
100
+ Got it?
101
+
102
+ @section thrproc Threads and Processes
103
+
104
+ LMDB uses POSIX locks on files, and these locks have issues if one
105
+ process opens a file multiple times. Because of this, do not
106
+ #mdb_env_open() a file multiple times from a single process. Instead,
107
+ share the LMDB environment that has opened the file across all threads.
108
+ Otherwise, if a single process opens the same environment multiple times,
109
+ closing it once will remove all the locks held on it, and the other
110
+ instances will be vulnerable to corruption from other processes.
111
+
112
+ Also note that a transaction is tied to one thread by default using
113
+ Thread Local Storage. If you want to pass read-only transactions across
114
+ threads, you can use the #MDB_NOTLS option on the environment.
115
+
116
+ @section txns Transactions, Rollbacks, etc.
117
+
118
+ To actually get anything done, a transaction must be committed using
119
+ #mdb_txn_commit(). Alternatively, all of a transaction's operations
120
+ can be discarded using #mdb_txn_abort(). In a read-only transaction,
121
+ any cursors will \b not automatically be freed. In a read-write
122
+ transaction, all cursors will be freed and must not be used again.
123
+
124
+ For read-only transactions, obviously there is nothing to commit to
125
+ storage. The transaction still must eventually be aborted to close
126
+ any database handle(s) opened in it, or committed to keep the
127
+ database handles around for reuse in new transactions.
128
+
129
+ In addition, as long as a transaction is open, a consistent view of
130
+ the database is kept alive, which requires storage. A read-only
131
+ transaction that no longer requires this consistent view should
132
+ be terminated (committed or aborted) when the view is no longer
133
+ needed (but see below for an optimization).
134
+
135
+ There can be multiple simultaneously active read-only transactions
136
+ but only one that can write. Once a single read-write transaction
137
+ is opened, all further attempts to begin one will block until the
138
+ first one is committed or aborted. This has no effect on read-only
139
+ transactions, however, and they may continue to be opened at any time.
140
+
141
+ @section dupkeys Duplicate Keys
142
+
143
+ #mdb_get() and #mdb_put() respectively have no and only some support
144
+ for multiple key/value pairs with identical keys. If there are multiple
145
+ values for a key, #mdb_get() will only return the first value.
146
+
147
+ When multiple values for one key are required, pass the #MDB_DUPSORT
148
+ flag to #mdb_dbi_open(). In an #MDB_DUPSORT database, by default
149
+ #mdb_put() will not replace the value for a key if the key existed
150
+ already. Instead it will add the new value to the key. In addition,
151
+ #mdb_del() will pay attention to the value field too, allowing for
152
+ specific values of a key to be deleted.
153
+
154
+ Finally, additional cursor operations become available for
155
+ traversing through and retrieving duplicate values.
156
+
157
+ @section optim Some Optimization
158
+
159
+ If you frequently begin and abort read-only transactions, as an
160
+ optimization, it is possible to only reset and renew a transaction.
161
+
162
+ #mdb_txn_reset() releases any old copies of data kept around for
163
+ a read-only transaction. To reuse this reset transaction, call
164
+ #mdb_txn_renew() on it. Any cursors in this transaction must also
165
+ be renewed using #mdb_cursor_renew().
166
+
167
+ Note that #mdb_txn_reset() is similar to #mdb_txn_abort() and will
168
+ close any databases you opened within the transaction.
169
+
170
+ To permanently free a transaction, reset or not, use #mdb_txn_abort().
171
+
172
+ @section cleanup Cleaning Up
173
+
174
+ For read-only transactions, any cursors created within it must
175
+ be closed using #mdb_cursor_close().
176
+
177
+ It is very rarely necessary to close a database handle, and in
178
+ general they should just be left open.
179
+
180
+ @section onward The Full API
181
+
182
+ The full \ref mdb documentation lists further details, like how to:
183
+
184
+ \li size a database (the default limits are intentionally small)
185
+ \li drop and clean a database
186
+ \li detect and report errors
187
+ \li optimize (bulk) loading speed
188
+ \li (temporarily) reduce robustness to gain even more speed
189
+ \li gather statistics about the database
190
+ \li define custom sort orders
191
+
192
+ */