@finogeeks2026/finshield-opencode 0.0.0-stage → 0.11.50

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 (143) hide show
  1. package/DESIGN.md +593 -0
  2. package/LICENSE.rtf +28 -0
  3. package/README.md +132 -2
  4. package/THIRD-PARTY-NOTICES.md +3131 -0
  5. package/bin/node.exe +4 -0
  6. package/dist/gui-experimental.cjs +96 -0
  7. package/dist/updater/server.js +1046 -0
  8. package/install/download-source.json +3 -0
  9. package/install/runtime.js +60 -0
  10. package/node_modules/@finogeeks2026/finsafe-sdk/LICENSE.rtf +28 -0
  11. package/node_modules/@finogeeks2026/finsafe-sdk/README.md +116 -0
  12. package/node_modules/@finogeeks2026/finsafe-sdk/bin/finsafe_native.dll +0 -0
  13. package/node_modules/@finogeeks2026/finsafe-sdk/native-build.json +5 -0
  14. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/README.md +6 -0
  15. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/index.js +12 -0
  16. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/package.json +24 -0
  17. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/win32_x64/koffi.lib +0 -0
  18. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/win32_x64/koffi.node +0 -0
  19. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/CHANGELOG.md +1183 -0
  20. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/LICENSE.txt +22 -0
  21. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/README.md +42 -0
  22. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/cnoke.cjs +816 -0
  23. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/benchmarks.md +80 -0
  24. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/callbacks.md +182 -0
  25. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/composites.md +549 -0
  26. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/contribute.md +163 -0
  27. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/index.md +46 -0
  28. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/load.md +191 -0
  29. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/migration.md +256 -0
  30. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/misc.md +76 -0
  31. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/output.md +299 -0
  32. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/pointers.md +385 -0
  33. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/primitives.md +151 -0
  34. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/start.md +110 -0
  35. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/types.md +88 -0
  36. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/unions.md +0 -0
  37. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/values.md +134 -0
  38. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.cjs +1 -0
  39. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.d.ts +389 -0
  40. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.js +2 -0
  41. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/indirect.cjs +1 -0
  42. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/indirect.js +2 -0
  43. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/base.cc +11064 -0
  44. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/base.hh +5951 -0
  45. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/unicode.inc +408 -0
  46. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/package.json +68 -0
  47. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/CMakeLists.txt +249 -0
  48. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/index.cjs +296 -0
  49. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/index.js +281 -0
  50. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/indirect.cjs +295 -0
  51. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/indirect.js +280 -0
  52. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64.cc +1408 -0
  53. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64_asm.S +216 -0
  54. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64_asm.asm +181 -0
  55. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/loong64.cc +5 -0
  56. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/loong64_asm.S +200 -0
  57. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/riscv64.cc +1345 -0
  58. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/riscv64_asm.S +200 -0
  59. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64sysv.cc +1434 -0
  60. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64sysv_asm.S +224 -0
  61. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win.cc +1086 -0
  62. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win_asm.S +162 -0
  63. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win_asm.asm +189 -0
  64. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86.cc +1185 -0
  65. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86_asm.S +217 -0
  66. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86_asm.asm +174 -0
  67. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/call.cc +2094 -0
  68. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/call.hh +179 -0
  69. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/errno.inc +462 -0
  70. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/ffi.cc +3009 -0
  71. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/ffi.hh +461 -0
  72. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/parser.cc +218 -0
  73. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/parser.hh +54 -0
  74. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/primitives.inc +36 -0
  75. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/static.cjs +130 -0
  76. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/static.js +133 -0
  77. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/trampolines.cjs +77 -0
  78. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/type.cc +725 -0
  79. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/type.hh +71 -0
  80. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/util.cc +1040 -0
  81. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/util.hh +493 -0
  82. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.cc +214 -0
  83. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.def +11 -0
  84. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.hh +112 -0
  85. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/win32.cc +198 -0
  86. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/win32.hh +148 -0
  87. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/LICENSE.md +9 -0
  88. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/README.md +95 -0
  89. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi-inl.deprecated.h +186 -0
  90. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi-inl.h +7211 -0
  91. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi.h +3421 -0
  92. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/LICENSE +21 -0
  93. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/README.md +95 -0
  94. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/def/js_native_api.def +125 -0
  95. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/def/node_api.def +157 -0
  96. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/js_native_api.h +591 -0
  97. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/js_native_api_types.h +210 -0
  98. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/node_api.h +265 -0
  99. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/node_api_types.h +58 -0
  100. package/node_modules/@finogeeks2026/finsafe-sdk/package.json +36 -0
  101. package/node_modules/@finogeeks2026/finsafe-sdk/release.json +4 -0
  102. package/node_modules/@finogeeks2026/finsafe-sdk/src/native.js +85 -0
  103. package/node_modules/@finogeeks2026/finsafe-sdk/src/shell.js +20 -0
  104. package/node_modules/@finogeeks2026/finsafe-sdk/src/status.js +57 -0
  105. package/package.json +47 -4
  106. package/policy/candidate.yaml +347 -0
  107. package/release.json +4 -0
  108. package/src/activation.js +2 -0
  109. package/src/attachment.js +34 -0
  110. package/src/connector.js +91 -0
  111. package/src/contract.js +8 -0
  112. package/src/desktop-contract.js +4 -0
  113. package/src/desktop.js +25 -0
  114. package/src/gui.tsx +58 -0
  115. package/src/host.js +38 -0
  116. package/src/index.js +12 -0
  117. package/src/instance-settings.js +27 -0
  118. package/src/known-folders.js +22 -0
  119. package/src/management.js +17 -0
  120. package/src/preparation.js +18 -0
  121. package/src/presentation.js +153 -0
  122. package/src/settings.js +18 -0
  123. package/ui/feedback.cjs +45 -0
  124. package/ui/page.cjs +225 -0
  125. package/ui/state.cjs +44 -0
  126. package/updater/bootstrap.js +41 -0
  127. package/updater/common/activation.js +81 -0
  128. package/updater/common/coordinator.js +329 -0
  129. package/updater/common/download.js +57 -0
  130. package/updater/common/index.js +12 -0
  131. package/updater/common/package.js +53 -0
  132. package/updater/common/protocol.js +38 -0
  133. package/updater/common/transport.js +65 -0
  134. package/updater/coordinator.js +2 -0
  135. package/updater/launch.js +40 -0
  136. package/updater/management.js +9 -0
  137. package/updater/native.js +18 -0
  138. package/updater/operations.js +45 -0
  139. package/updater/protocol.js +1 -0
  140. package/updater/server.js +100 -0
  141. package/updater/transport.js +10 -0
  142. package/updater/windows-launch-reader.js +40 -0
  143. package/updater/windows-launch.js +96 -0
@@ -0,0 +1,549 @@
1
+ # Struct types
2
+
3
+ ## Struct definition
4
+
5
+ Koffi converts JS objects to C structs, and vice-versa.
6
+
7
+ Unlike function declarations, as of now there is only one way to create a struct type, with the `koffi.struct()` function. This function takes two arguments: the first one is the name of the type, and the second one is an object containing the struct member names and types. You can omit the first argument to declare an anonymous struct.
8
+
9
+ The following example illustrates how to declare the same struct in C and in JS with Koffi:
10
+
11
+ ```c
12
+ typedef struct A {
13
+ int a;
14
+ char b;
15
+ const char *c;
16
+ struct {
17
+ double d1;
18
+ double d2;
19
+ } d;
20
+ } A;
21
+ ```
22
+
23
+ ```js
24
+ const A = koffi.struct('A', {
25
+ a: 'int',
26
+ b: 'char',
27
+ c: 'const char *', // Koffi does not care about const, it is ignored
28
+ d: koffi.struct({
29
+ d1: 'double',
30
+ d2: 'double'
31
+ })
32
+ });
33
+ ```
34
+
35
+ Koffi automatically follows the platform C ABI regarding alignment and padding. However, you can override these rules if needed with:
36
+
37
+ - Pack all members without padding with `koffi.pack()` (instead of `koffi.struct()`)
38
+ - Change alignment of a specific member as shown below
39
+
40
+ ```js
41
+ // This struct is 3 bytes long
42
+ const PackedStruct = koffi.pack('PackedStruct', {
43
+ a: 'int8_t',
44
+ b: 'int16_t'
45
+ });
46
+
47
+ // This one is 10 bytes long, the second member has an alignment requirement of 8 bytes
48
+ const BigStruct = koffi.struct('BigStruct', {
49
+ a: 'int8_t',
50
+ b: [8, 'int16_t']
51
+ })
52
+ ```
53
+
54
+ Once a struct is declared, you can use it by name (with a string, like you can do for primitive types) or through the value returned by the call to `koffi.struct()`. Only the latter is possible when declaring an anonymous struct.
55
+
56
+ ```js
57
+ // The following two function declarations are equivalent, and declare a function taking an A value and returning A
58
+ const Function1 = lib.func('A Function(A value)');
59
+ const Function2 = lib.func('Function', A, [A]);
60
+ ```
61
+
62
+ ## Opaque types
63
+
64
+ Many C libraries use some kind of object-oriented API, with a pair of functions dedicated to create and delete objects. An obvious example of this can be found in stdio.h, with the opaque `FILE *` pointer. You can open and close files with `fopen()` and `fclose()`, and manipule the opaque pointer with other functions such as `fread()` or `ftell()`.
65
+
66
+ In Koffi, you can manage this with opaque types. Declare the opaque type with `koffi.opaque(name)`, and use a pointer to this type either as a return type or some kind of [output parameter](output) (with a double pointer).
67
+
68
+ The full example below implements an iterative string builder (concatenator) in C, and uses it from Javascript to output a mix of Hello World and FizzBuzz. The builder is hidden behind an opaque type, and is created and destroyed using a pair of C functions: `ConcatNew` (or `ConcatNewOut`) and `ConcatFree`.
69
+
70
+ ```c
71
+ // Build with: clang -fPIC -o handles.so -shared handles.c -Wall -O2
72
+
73
+ #include <stdlib.h>
74
+ #include <stdbool.h>
75
+ #include <stdio.h>
76
+ #include <errno.h>
77
+ #include <string.h>
78
+
79
+ typedef struct Fragment {
80
+ struct Fragment *next;
81
+
82
+ size_t len;
83
+ char str[];
84
+ } Fragment;
85
+
86
+ typedef struct Concat {
87
+ Fragment *first;
88
+ Fragment *last;
89
+
90
+ size_t total;
91
+ } Concat;
92
+
93
+ bool ConcatNewOut(Concat **out)
94
+ {
95
+ Concat *c = malloc(sizeof(*c));
96
+ if (!c) {
97
+ fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
98
+ return false;
99
+ }
100
+
101
+ c->first = NULL;
102
+ c->last = NULL;
103
+ c->total = 0;
104
+
105
+ *out = c;
106
+ return true;
107
+ }
108
+
109
+ Concat *ConcatNew()
110
+ {
111
+ Concat *c = NULL;
112
+ ConcatNewOut(&c);
113
+ return c;
114
+ }
115
+
116
+ void ConcatFree(Concat *c)
117
+ {
118
+ if (!c)
119
+ return;
120
+
121
+ Fragment *f = c->first;
122
+
123
+ while (f) {
124
+ Fragment *next = f->next;
125
+ free(f);
126
+ f = next;
127
+ }
128
+
129
+ free(c);
130
+ }
131
+
132
+ bool ConcatAppend(Concat *c, const char *frag)
133
+ {
134
+ size_t len = strlen(frag);
135
+
136
+ Fragment *f = malloc(sizeof(*f) + len + 1);
137
+ if (!f) {
138
+ fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
139
+ return false;
140
+ }
141
+
142
+ f->next = NULL;
143
+ if (c->last) {
144
+ c->last->next = f;
145
+ } else {
146
+ c->first = f;
147
+ }
148
+ c->last = f;
149
+ c->total += len;
150
+
151
+ f->len = len;
152
+ memcpy(f->str, frag, len);
153
+ f->str[len] = 0;
154
+
155
+ return true;
156
+ }
157
+
158
+ const char *ConcatBuild(Concat *c)
159
+ {
160
+ Fragment *r = malloc(sizeof(*r) + c->total + 1);
161
+ if (!r) {
162
+ fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
163
+ return NULL;
164
+ }
165
+
166
+ r->next = NULL;
167
+ r->len = 0;
168
+
169
+ Fragment *f = c->first;
170
+
171
+ while (f) {
172
+ Fragment *next = f->next;
173
+
174
+ memcpy(r->str + r->len, f->str, f->len);
175
+ r->len += f->len;
176
+
177
+ free(f);
178
+ f = next;
179
+ }
180
+ r->str[r->len] = 0;
181
+
182
+ c->first = r;
183
+ c->last = r;
184
+
185
+ return r->str;
186
+ }
187
+ ```
188
+
189
+ ```js
190
+ import koffi from 'koffi';
191
+ // CJS: const koffi = require('koffi');
192
+
193
+ const lib = koffi.load('./handles.so');
194
+
195
+ const Concat = koffi.opaque('Concat');
196
+ const ConcatNewOut = lib.func('bool ConcatNewOut(_Out_ Concat **out)');
197
+ const ConcatNew = lib.func('Concat *ConcatNew()');
198
+ const ConcatFree = lib.func('void ConcatFree(Concat *c)');
199
+ const ConcatAppend = lib.func('bool ConcatAppend(Concat *c, const char *frag)');
200
+ const ConcatBuild = lib.func('const char *ConcatBuild(Concat *c)');
201
+
202
+ let c = ConcatNew();
203
+ if (!c) {
204
+ // This is stupid, it does the same, but try both versions (return value, output parameter)
205
+ let ptr = [null];
206
+ if (!ConcatNewOut(ptr))
207
+ throw new Error('Allocation failure');
208
+ c = ptr[0];
209
+ }
210
+
211
+ try {
212
+ if (!ConcatAppend(c, 'Hello... '))
213
+ throw new Error('Allocation failure');
214
+ if (!ConcatAppend(c, 'World!\n'))
215
+ throw new Error('Allocation failure');
216
+
217
+ for (let i = 1; i <= 30; i++) {
218
+ let frag;
219
+ if (i % 15 == 0) {
220
+ frag = 'FizzBuzz';
221
+ } else if (i % 5 == 0) {
222
+ frag = 'Buzz';
223
+ } else if (i % 3 == 0) {
224
+ frag = 'Fizz';
225
+ } else {
226
+ frag = String(i);
227
+ }
228
+
229
+ if (!ConcatAppend(c, frag))
230
+ throw new Error('Allocation failure');
231
+ if (!ConcatAppend(c, ' '))
232
+ throw new Error('Allocation failure');
233
+ }
234
+
235
+ let str = ConcatBuild(c);
236
+ if (str == null)
237
+ throw new Error('Allocation failure');
238
+ console.log(str);
239
+ } finally {
240
+ ConcatFree(c);
241
+ }
242
+ ```
243
+
244
+ # Array types
245
+
246
+ ## Fixed-size C arrays
247
+
248
+ Fixed-size arrays are declared with `koffi.array(type, length)`. Just like in C, they cannot be passed as functions parameters (they degenerate to pointers), or returned by value. You can however embed them in struct types.
249
+
250
+ Koffi applies the following conversion rules when passing arrays to/from C:
251
+
252
+ - **JS to C**: Koffi can take a normal Array (e.g. `[1, 2]`) or a TypedArray of the correct type (e.g. `Uint8Array` for an array of `uint8_t` numbers)
253
+ - **C to JS** (return value, output parameters, callbacks): Koffi will use a TypedArray if possible. But you can change this behavior when you create the array type with the optional hint argument: `koffi.array('uint8_t', 64, 'Array')`. For non-number types, such as arrays of strings or structs, Koffi creates normal arrays.
254
+
255
+ See the example below:
256
+
257
+ ```js
258
+ import koffi from 'koffi';
259
+ // CJS: const koffi = require('koffi');
260
+
261
+ // Those two structs are exactly the same, only the array conversion hint is different
262
+ const Foo1 = koffi.struct('Foo1', {
263
+ i: 'int',
264
+ a16: koffi.array('int16_t', 2)
265
+ });
266
+ const Foo2 = koffi.struct('Foo2', {
267
+ i: 'int',
268
+ a16: koffi.array('int16_t', 2, 'Array')
269
+ });
270
+
271
+ // Uses an hypothetical C function that just returns the struct passed as a parameter
272
+ const ReturnFoo1 = lib.func('Foo1 ReturnFoo(Foo1 p)');
273
+ const ReturnFoo2 = lib.func('Foo2 ReturnFoo(Foo2 p)');
274
+
275
+ console.log(ReturnFoo1({ i: 5, a16: [6, 8] })) // Prints { i: 5, a16: Int16Array(2) [6, 8] }
276
+ console.log(ReturnFoo2({ i: 5, a16: [6, 8] })) // Prints { i: 5, a16: [6, 8] }
277
+ ```
278
+
279
+ You can also declare arrays with the C-like short syntax in type declarations, as shown below:
280
+
281
+ ```js
282
+ const StructType = koffi.struct('StructType', {
283
+ f8: 'float [8]',
284
+ self4: 'StructType *[4]'
285
+ });
286
+ ```
287
+
288
+ ## Fixed-size string buffers
289
+
290
+ Koffi can also convert JS strings to fixed-sized arrays in the following cases:
291
+
292
+ - **char arrays** are filled with the UTF-8 encoded string, truncated if needed. The buffer is always NUL-terminated.
293
+ - **char16 (or char16_t) arrays** are filled with the UTF-16 encoded string, truncated if needed. The buffer is always NUL-terminated.
294
+ - **char32 (or char32_t) arrays** are filled with the UTF-32 encoded string, truncated if needed. The buffer is always NUL-terminated.
295
+
296
+ The reverse case is also true, Koffi can convert a C fixed-size buffer to a JS string. This happens by default for char, char16_t and char32_t arrays, but you can also explicitly ask for this with the `String` array hint (e.g. `koffi.array('char', 8, 'String')`).
297
+
298
+ ## Flexible arrays
299
+
300
+ C structs ending with a flexible array member are often used for variable-sized structs, and are generally paired with dynamic memory allocation. In many cases, the number of elements is described by another struct member.
301
+
302
+ Use `koffi.array(type, countedBy, maxLen)` to make a flexible array type, for which the array length is determined by the struct member indicated by the `countedBy` parameter. Flexible array types can only be used as the last member of a struct, as shown below:
303
+
304
+ ```js
305
+ const FlexibleArray = koffi.struct('FlexibleArray', {
306
+ count: 'size_t',
307
+ numbers: koffi.array('int', 'count', 128)
308
+ });
309
+ ````
310
+
311
+ For various reasons, Koffi requires you to specify an upper bound (`maxLen`) for the number of elements in the flexible array member.
312
+
313
+ > [!WARNING]
314
+ > Also, unlike C flexible arrays, the struct size will expand to accomodate the maximum size of the flexible array.
315
+
316
+ The following example illustrates how to use a flexible array API in C from Koffi.
317
+
318
+ ```c
319
+ // Build with: clang -fPIC -o flexible.so -shared flexible.c -Wall -O2
320
+
321
+ #include <stddef.h>
322
+
323
+ struct FlexibleArray {
324
+ size_t count;
325
+ int numbers[];
326
+ };
327
+
328
+ void AppendValues(struct FlexibleArray *arr, size_t count, int start, int step)
329
+ {
330
+ for (size_t i = 0; i < count; i++) {
331
+ arr->numbers[arr->count + i] = start + i * step;
332
+ }
333
+ arr->count += count;
334
+ }
335
+ ```
336
+
337
+ ```js
338
+ import koffi from 'koffi';
339
+ // CJS: const koffi = require('koffi');
340
+
341
+ const lib = koffi.load('./flexible.so');
342
+
343
+ const FlexibleArray = koffi.struct('FlexibleArray', {
344
+ count: 'size_t',
345
+ numbers: koffi.array('int', 'count', 256, 'Array')
346
+ });
347
+
348
+ const AppendValues = lib.func('void AppendValues(_Inout_ FlexibleArray *arr, int count, int start, int step)');
349
+
350
+ let array = { count: 0, numbers: [] };
351
+
352
+ AppendValues(array, 5, 1, 1);
353
+ console.log(array); // Prints { count: 5, numbers: [1, 2, 3, 4, 5] }
354
+
355
+ AppendValues(array, 3, 10, 2);
356
+ console.log(array); // Prints { count: 8, numbers: [1, 2, 3, 4, 5, 10, 12, 14] }
357
+ ```
358
+
359
+ > [!NOTE]
360
+ > This is frequently used in the Win32 API, an exemple of this is [AllocateAndInitializeSid()](https://learn.microsoft.com/windows/win32/api/securitybaseapi/nf-securitybaseapi-allocateandinitializesid).
361
+
362
+ ## Dynamic arrays (pointers)
363
+
364
+ In C, dynamically-sized arrays are usually passed around as pointers. Read more about [array pointers](pointers#dynamic-arrays) in the relevant section.
365
+
366
+ # Unions
367
+
368
+ You can declare unions with a syntax similar to structs, but with the `koffi.union()` function. This function takes two arguments: the first one is the name of the type, and the second one is an object containing the union member names and types. You can omit the first argument to declare an anonymous union.
369
+
370
+ The following example illustrates how to declare the same union in C and in JS with Koffi:
371
+
372
+ ```c
373
+ typedef union IntOrDouble {
374
+ int64_t i;
375
+ double d;
376
+ } IntOrDouble;
377
+ ```
378
+
379
+ ```js
380
+ const IntOrDouble = koffi.union('IntOrDouble', {
381
+ i: 'int64_t',
382
+ d: 'double'
383
+ });
384
+ ```
385
+
386
+ ## Input unions
387
+
388
+ ### Passing union values to C
389
+
390
+ You can instantiate an union object with `koffi.Union(type)`. This will create a special object that contains at most one active member.
391
+
392
+ Once you have created an instance of your union, you can simply set the member with the dot operator as you would with a basic object. Then, simply pass your union value to the C function you wish.
393
+
394
+ ```js
395
+ const U = koffi.union('U', { i: 'int', str: 'char *' });
396
+
397
+ const DoSomething = lib.func('void DoSomething(const char *type, U u)');
398
+
399
+ const u1 = new koffi.Union('U'); u1.i = 42;
400
+ const u2 = new koffi.Union('U'); u2.str = 'Hello!';
401
+
402
+ DoSomething('int', u1);
403
+ DoSomething('string', u2);
404
+ ```
405
+
406
+ For simplicity, Koffi also accepts object literals with one property (no more, no less) setting the corresponding union member. The example belows uses this to simplify the code shown above:
407
+
408
+ ```js
409
+ const U = koffi.union('U', { i: 'int', str: 'char *' });
410
+
411
+ const DoSomething = lib.func('void DoSomething(const char *type, U u)');
412
+
413
+ DoSomething('int', { i: 42 });
414
+ DoSomething('string', { str: 'Hello!' });
415
+ ```
416
+
417
+ ### Win32 example
418
+
419
+ The following example uses the [SendInput](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput) Win32 API to emit the Win+D shortcut and hide windows (show the desktop).
420
+
421
+ ```js
422
+ import koffi from 'koffi';
423
+ // CJS: const koffi = require('koffi');
424
+
425
+ // Win32 type and functions
426
+
427
+ const user32 = koffi.load('user32.dll');
428
+
429
+ const INPUT_MOUSE = 0;
430
+ const INPUT_KEYBOARD = 1;
431
+ const INPUT_HARDWARE = 2;
432
+
433
+ const KEYEVENTF_KEYUP = 0x2;
434
+ const KEYEVENTF_SCANCODE = 0x8;
435
+
436
+ const VK_LWIN = 0x5B;
437
+ const VK_D = 0x44;
438
+
439
+ const MOUSEINPUT = koffi.struct('MOUSEINPUT', {
440
+ dx: 'long',
441
+ dy: 'long',
442
+ mouseData: 'uint32_t',
443
+ dwFlags: 'uint32_t',
444
+ time: 'uint32_t',
445
+ dwExtraInfo: 'uintptr_t'
446
+ });
447
+ const KEYBDINPUT = koffi.struct('KEYBDINPUT', {
448
+ wVk: 'uint16_t',
449
+ wScan: 'uint16_t',
450
+ dwFlags: 'uint32_t',
451
+ time: 'uint32_t',
452
+ dwExtraInfo: 'uintptr_t'
453
+ });
454
+ const HARDWAREINPUT = koffi.struct('HARDWAREINPUT', {
455
+ uMsg: 'uint32_t',
456
+ wParamL: 'uint16_t',
457
+ wParamH: 'uint16_t'
458
+ });
459
+
460
+ const INPUT = koffi.struct('INPUT', {
461
+ type: 'uint32_t',
462
+ u: koffi.union({
463
+ mi: MOUSEINPUT,
464
+ ki: KEYBDINPUT,
465
+ hi: HARDWAREINPUT
466
+ })
467
+ });
468
+
469
+ const SendInput = user32.func('unsigned int __stdcall SendInput(unsigned int cInputs, INPUT *pInputs, int cbSize)');
470
+
471
+ // Show/hide desktop with Win+D shortcut
472
+
473
+ let events = [
474
+ make_keyboard_event(VK_LWIN, true),
475
+ make_keyboard_event(VK_D, true),
476
+ make_keyboard_event(VK_D, false),
477
+ make_keyboard_event(VK_LWIN, false)
478
+ ];
479
+
480
+ SendInput(events.length, events, koffi.sizeof(INPUT));
481
+
482
+ // Utility
483
+
484
+ function make_keyboard_event(vk, down) {
485
+ let event = {
486
+ type: INPUT_KEYBOARD,
487
+ u: {
488
+ ki: {
489
+ wVk: vk,
490
+ wScan: 0,
491
+ dwFlags: down ? 0 : KEYEVENTF_KEYUP,
492
+ time: 0,
493
+ dwExtraInfo: 0
494
+ }
495
+ }
496
+ };
497
+
498
+ return event;
499
+ }
500
+ ```
501
+
502
+ ## Output unions
503
+
504
+ Unlike structs, Koffi does not know which union member is valid, and it cannot decode it automatically. You can however use special `koffi.Union` objects for output parameters, and decode the memory after the call.
505
+
506
+ To decode an output union pointer parameter, create a placeholder object with `new koffi.Union(type)` and pass the resulting object to the function.
507
+
508
+ After the call, you can dereference the member value you want on this object and Koffi will decode it at this moment.
509
+
510
+ The following example illustrates the use of `koffi.Union()` to decode output unions after the call.
511
+
512
+ ```c
513
+ #include <stdint.h>
514
+
515
+ typedef union IntOrDouble {
516
+ int64_t i;
517
+ double d;
518
+ } IntOrDouble;
519
+
520
+ void SetUnionInt(int64_t i, IntOrDouble *out)
521
+ {
522
+ out->i = i;
523
+ }
524
+
525
+ void SetUnionDouble(double d, IntOrDouble *out)
526
+ {
527
+ out->d = d;
528
+ }
529
+ ```
530
+
531
+ ```js
532
+ const IntOrDouble = koffi.union('IntOrDouble', {
533
+ i: 'int64_t',
534
+ d: 'double',
535
+ raw: koffi.array('uint8_t', 8)
536
+ });
537
+
538
+ const SetUnionInt = lib.func('void SetUnionInt(int64_t i, _Out_ IntOrDouble *out)');
539
+ const SetUnionDouble = lib.func('void SetUnionDouble(double d, _Out_ IntOrDouble *out)');
540
+
541
+ let u1 = new koffi.Union('IntOrDouble');
542
+ let u2 = new koffi.Union('IntOrDouble');
543
+
544
+ SetUnionInt(123, u1);
545
+ SetUnionDouble(123, u2);
546
+
547
+ console.log(u1.i, '---', u1.raw); // Prints 123 --- Uint8Array(8) [123, 0, 0, 0, 0, 0, 0, 0]
548
+ console.log(u2.d, '---', u2.raw); // Prints 123 --- Uint8Array(8) [0, 0, 0, 0, 0, 0, 69, 64]
549
+ ```
@@ -0,0 +1,163 @@
1
+ # Bugs and feature requests
2
+
3
+ Use the official repository for bugs, ideas and features requests: https://github.com/Koromix/koffi
4
+
5
+ Please note that the source code is not in this repository, instead it lives in a monorepo: https://codeberg.org/Koromix/rygel/ (in the *src/koffi* subdirectory).
6
+
7
+ # Build from source
8
+
9
+ We provide prebuilt binaries, packaged in NPM packages, so in most cases it should be as simple as `npm install koffi`. If you want to hack Koffi or use a specific platform, follow the instructions below.
10
+
11
+ Start by cloning the repository with [Git](https://git-scm.com/):
12
+
13
+ ```sh
14
+ git clone https://codeberg.org/Koromix/rygel
15
+ cd rygel
16
+ ```
17
+
18
+ As said before, this is a monorepository containg multiple projects, hence the name.
19
+
20
+ ## Windows
21
+
22
+ First, make sure the following dependencies are met:
23
+
24
+ - The "Desktop development with C++" workload from [Visual Studio 2022 or 2019](https://visualstudio.microsoft.com/downloads/) or the "C++ build tools" workload from the [Build Tools](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022), with the default optional components.
25
+ - [CMake meta build system](https://cmake.org/)
26
+ - [Node.js](https://nodejs.org/) 16 or later
27
+
28
+ Once this is done, run this command from the _src/koffi_ directory:
29
+
30
+ ```sh
31
+ cd src/koffi
32
+ node ../cnoke/cnoke.js
33
+ ```
34
+
35
+ > [!NOTE]
36
+ > Use `node ../cnoke/cnoke.js --debug` to make debug builds
37
+
38
+ ## Other platforms
39
+
40
+ Make sure the following dependencies are met:
41
+
42
+ - `gcc` and `g++` >= 8.3 or newer
43
+ - GNU Make 3.81 or newer
44
+ - [CMake meta build system](https://cmake.org/)
45
+ - [Node.js](https://nodejs.org/) 16 or later
46
+
47
+ Once this is done, run this command from the _src/koffi_ directory:
48
+
49
+ ```sh
50
+ cd src/koffi
51
+ node ../cnoke/cnoke.js
52
+ ```
53
+
54
+ > [!NOTE]
55
+ > Use `node ../cnoke/cnoke.js --debug` to make debug builds
56
+
57
+ # Run tests
58
+
59
+ ## On your machine
60
+
61
+ Once Koffi is built, you can build the tests and run them with the following commands:
62
+
63
+ ```sh
64
+ cd src/koffi/test
65
+ node ../../cnoke/cnoke.js # Build C test code
66
+ node test.js
67
+ ```
68
+
69
+ ## On virtual machines
70
+
71
+ Koffi is tested on multiple architectures using emulated (accelerated when possible) QEMU machines. First, you need to install qemu packages, such as `qemu-system` (or even `qemu-system-gui`) on Ubuntu.
72
+
73
+ The Linux machines are based on Debian, they are not included in the repository but they can be created at any time with the scripts in `tools/cross/build`. For example, to create the Debian ARM64 machine, use the following commands:
74
+
75
+ ```sh
76
+ cd tools/cross/build
77
+ ./debian_arm64/build.sh
78
+ ```
79
+
80
+ The other machines (Alpine, FreeBSD, OpenBSD, macOS and Windows) are created manually, at least for now. These machines are not included directly in this repository (for license and size reasons), but they are available here: https://download.koromix.dev/qemu/
81
+
82
+ For example, if you want to build and test for Windows, run the following commands:
83
+
84
+ ```sh
85
+ cd tools/cross/
86
+ wget -q -O- https://download.koromix.dev/qemu/qemu_windows_x64.tar.zst | zstd -d | tar xv
87
+ ```
88
+
89
+ Note that the machine disk content may change each time the machine runs, so the checksum test will fail once a machine has been used at least once.
90
+
91
+ And now you can run the tests with:
92
+
93
+ ```sh
94
+ cd src/koffi
95
+
96
+ node tools/brew.js test # Several options are available, use --help
97
+ ```
98
+
99
+ And be patient, this can be pretty slow for emulated machines. The Linux machines have and use ccache to build Koffi, so subsequent build steps will get much more tolerable.
100
+
101
+ Machines are started for each test if needed, and stay active until you stop them. You can start and stop them at any time with the following commands:
102
+
103
+ ```sh
104
+ node tools/brew.js start # Start all available machines
105
+ node tools/brew.js stop # Stop everything
106
+ ```
107
+
108
+ You can also restrict these commands to a subset of machines:
109
+
110
+ ```sh
111
+ # Full test cycle
112
+ node tools/brew.js test debian_x64 debian_i386
113
+
114
+ # Separate start, test, shutdown
115
+ node tools/brew.js start debian_x64 debian_i386
116
+ node tools/brew.js test debian_x64 debian_i386
117
+ node tools/brew.js stop
118
+ ```
119
+
120
+ Finally, you can join a running machine with SSH with the following shortcut, if you need to do some debugging or any other manual procedure:
121
+
122
+ ```sh
123
+ node tools/brew.js ssh debian_i386
124
+ ```
125
+
126
+ Each machine is configured to run a VNC server or SPICE, which you can use to access the display, using KRDC or any other compatible viewer. Use the `info` command to get the VNC or the SPICE port.
127
+
128
+ ```sh
129
+ node tools/brew.js info debian_x64
130
+ ```
131
+
132
+ # Making a release
133
+
134
+ First, you must update the code in four steps:
135
+
136
+ - Change the version number in `package.json`
137
+ - Add an entry to `CHANGELOG` to summarize the changes since last release
138
+ - Commit theses changes with the message *Bump Koffi to X.Y.Z*
139
+ - Create a signed tag on this commit with `git tag -s koffi/X.Y.Z -m 'Koffi X.Y.Z'`
140
+
141
+ Once this is done, you can publish a new release with the following commands:
142
+
143
+ ```sh
144
+ cd src/koffi
145
+
146
+ node tools/brew.js test # If not done before
147
+ node tools/brew.js build
148
+
149
+ cd ../../bin/Koffi/packages
150
+
151
+ ./prepare.sh
152
+ ./publish.sh
153
+ ```
154
+
155
+ Some platforms are emulated so this can take a few minutes until the pre-built binaries are ready. You might want to go grab a cup of coffee :)
156
+
157
+ # Code style
158
+
159
+ Koffi is programmed in a mix of C++ and assembly code (architecture-specific code). It uses [node-addon-api](https://github.com/nodejs/node-addon-api) (C++ N-API wrapper) to interact with Node.js.
160
+
161
+ My personal preference goes to a rather C-like C++ style, with careful use of templates (mainly for containers) and little object-oriented programming. I strongly prefer tagged unions and code locality over inheritance and virtual methods. Exceptions are disabled.
162
+
163
+ Find more information about code style in the [monorepository README](https://codeberg.org/Koromix/rygel/#c-flavor) file.