koffi 2.3.10-beta.2 → 2.3.10

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 (47) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/build/2.3.10/koffi_darwin_arm64/koffi.node +0 -0
  3. package/build/2.3.10/koffi_darwin_x64/koffi.node +0 -0
  4. package/build/2.3.10/koffi_freebsd_arm64/koffi.node +0 -0
  5. package/build/2.3.10/koffi_freebsd_ia32/koffi.node +0 -0
  6. package/build/2.3.10/koffi_freebsd_x64/koffi.node +0 -0
  7. package/build/2.3.10/koffi_linux_arm32hf/koffi.node +0 -0
  8. package/build/2.3.10/koffi_linux_arm64/koffi.node +0 -0
  9. package/build/2.3.10/koffi_linux_ia32/koffi.node +0 -0
  10. package/build/2.3.10/koffi_linux_riscv64hf64/koffi.node +0 -0
  11. package/build/2.3.10/koffi_linux_x64/koffi.node +0 -0
  12. package/build/2.3.10/koffi_openbsd_ia32/koffi.node +0 -0
  13. package/build/2.3.10/koffi_openbsd_x64/koffi.node +0 -0
  14. package/build/2.3.10/koffi_win32_arm64/koffi.node +0 -0
  15. package/build/2.3.10/koffi_win32_ia32/koffi.node +0 -0
  16. package/build/2.3.10/koffi_win32_x64/koffi.node +0 -0
  17. package/package.json +2 -2
  18. package/src/cnoke/package.json +2 -8
  19. package/src/cnoke/src/builder.js +5 -7
  20. package/src/core/libcc/brotli.cc +194 -0
  21. package/src/core/libcc/libcc.cc +111 -615
  22. package/src/core/libcc/libcc.hh +102 -28
  23. package/src/core/libcc/lz4.cc +204 -0
  24. package/src/core/libcc/miniz.cc +361 -0
  25. package/src/koffi/CMakeLists.txt +1 -1
  26. package/src/koffi/src/call.cc +18 -0
  27. package/build/2.3.10-beta.2/koffi_darwin_arm64/koffi.node +0 -0
  28. package/build/2.3.10-beta.2/koffi_darwin_x64/koffi.node +0 -0
  29. package/build/2.3.10-beta.2/koffi_freebsd_arm64/koffi.node +0 -0
  30. package/build/2.3.10-beta.2/koffi_freebsd_ia32/koffi.node +0 -0
  31. package/build/2.3.10-beta.2/koffi_freebsd_x64/koffi.node +0 -0
  32. package/build/2.3.10-beta.2/koffi_linux_arm32hf/koffi.node +0 -0
  33. package/build/2.3.10-beta.2/koffi_linux_arm64/koffi.node +0 -0
  34. package/build/2.3.10-beta.2/koffi_linux_ia32/koffi.node +0 -0
  35. package/build/2.3.10-beta.2/koffi_linux_riscv64hf64/koffi.node +0 -0
  36. package/build/2.3.10-beta.2/koffi_linux_x64/koffi.node +0 -0
  37. package/build/2.3.10-beta.2/koffi_openbsd_ia32/koffi.node +0 -0
  38. package/build/2.3.10-beta.2/koffi_openbsd_x64/koffi.node +0 -0
  39. package/build/2.3.10-beta.2/koffi_win32_arm64/koffi.node +0 -0
  40. package/build/2.3.10-beta.2/koffi_win32_ia32/koffi.node +0 -0
  41. package/build/2.3.10-beta.2/koffi_win32_x64/koffi.node +0 -0
  42. /package/build/{2.3.10-beta.2 → 2.3.10}/koffi_win32_arm64/koffi.exp +0 -0
  43. /package/build/{2.3.10-beta.2 → 2.3.10}/koffi_win32_arm64/koffi.lib +0 -0
  44. /package/build/{2.3.10-beta.2 → 2.3.10}/koffi_win32_ia32/koffi.exp +0 -0
  45. /package/build/{2.3.10-beta.2 → 2.3.10}/koffi_win32_ia32/koffi.lib +0 -0
  46. /package/build/{2.3.10-beta.2 → 2.3.10}/koffi_win32_x64/koffi.exp +0 -0
  47. /package/build/{2.3.10-beta.2 → 2.3.10}/koffi_win32_x64/koffi.lib +0 -0
@@ -0,0 +1,361 @@
1
+ // Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
2
+
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ // this software and associated documentation files (the “Software”), to deal in
5
+ // the Software without restriction, including without limitation the rights to use,
6
+ // copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ // Software, and to permit persons to whom the Software is furnished to do so,
8
+ // subject to the following conditions:
9
+
10
+ // The above copyright notice and this permission notice shall be included in all
11
+ // copies or substantial portions of the Software.
12
+
13
+ // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
14
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
+ // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
+ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
+ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
+ // OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ #include "libcc.hh"
23
+
24
+ #define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
25
+ #include "vendor/miniz/miniz.h"
26
+
27
+ namespace RG {
28
+
29
+ class MinizDecompressor: public StreamDecompressor {
30
+ tinfl_decompressor inflator;
31
+ bool done = false;
32
+
33
+ uint8_t in_buf[256 * 1024];
34
+ uint8_t *in_ptr = nullptr;
35
+ Size in_len = 0;
36
+
37
+ uint8_t out_buf[256 * 1024];
38
+ uint8_t *out_ptr = nullptr;
39
+ Size out_len = 0;
40
+
41
+ // Gzip support
42
+ bool is_gzip = false;
43
+ bool header_done = false;
44
+ uint32_t crc32 = MZ_CRC32_INIT;
45
+ Size uncompressed_size = 0;
46
+
47
+ public:
48
+ MinizDecompressor(StreamReader *reader) : StreamDecompressor(reader) {}
49
+ ~MinizDecompressor() {}
50
+
51
+ bool Init(CompressionType type) override;
52
+ Size Read(Size max_len, void *out_buf) override;
53
+ };
54
+
55
+ bool MinizDecompressor::Init(CompressionType type)
56
+ {
57
+ RG_STATIC_ASSERT(RG_SIZE(out_buf) >= TINFL_LZ_DICT_SIZE);
58
+
59
+ tinfl_init(&inflator);
60
+ is_gzip = (type == CompressionType::Gzip);
61
+
62
+ return true;
63
+ }
64
+
65
+ Size MinizDecompressor::Read(Size max_len, void *user_buf)
66
+ {
67
+ // Gzip header is not directly supported by miniz. Currently this
68
+ // will fail if the header is longer than 4096 bytes, which is
69
+ // probably quite rare.
70
+ if (is_gzip && !header_done) {
71
+ uint8_t header[4096];
72
+ Size header_len;
73
+
74
+ header_len = ReadRaw(RG_SIZE(header), header);
75
+ if (header_len < 0) {
76
+ return -1;
77
+ } else if (header_len < 10 || header[0] != 0x1F || header[1] != 0x8B) {
78
+ LogError("File '%1' does not look like a Gzip stream", GetFileName());
79
+ return -1;
80
+ }
81
+
82
+ Size header_offset = 10;
83
+ if (header[3] & 0x4) { // FEXTRA
84
+ if (header_len - header_offset < 2)
85
+ goto truncated_error;
86
+ uint16_t extra_len = (uint16_t)((header[11] << 8) | header[10]);
87
+ if (extra_len > header_len - header_offset)
88
+ goto truncated_error;
89
+ header_offset += extra_len;
90
+ }
91
+ if (header[3] & 0x8) { // FNAME
92
+ uint8_t *end_ptr = (uint8_t *)memchr(header + header_offset, '\0',
93
+ (size_t)(header_len - header_offset));
94
+ if (!end_ptr)
95
+ goto truncated_error;
96
+ header_offset = end_ptr - header + 1;
97
+ }
98
+ if (header[3] & 0x10) { // FCOMMENT
99
+ uint8_t *end_ptr = (uint8_t *)memchr(header + header_offset, '\0',
100
+ (size_t)(header_len - header_offset));
101
+ if (!end_ptr)
102
+ goto truncated_error;
103
+ header_offset = end_ptr - header + 1;
104
+ }
105
+ if (header[3] & 0x2) { // FHCRC
106
+ if (header_len - header_offset < 2)
107
+ goto truncated_error;
108
+ uint16_t crc16 = (uint16_t)(header[1] << 8 | header[0]);
109
+ if ((mz_crc32(MZ_CRC32_INIT, header, (size_t)header_offset) & 0xFFFF) == crc16) {
110
+ LogError("Failed header CRC16 check in '%s'", GetFileName());
111
+ return -1;
112
+ }
113
+ header_offset += 2;
114
+ }
115
+
116
+ // Put back remaining data in the buffer
117
+ memcpy_safe(in_buf, header + header_offset, (size_t)(header_len - header_offset));
118
+ in_ptr = in_buf;
119
+ in_len = header_len - header_offset;
120
+
121
+ header_done = true;
122
+ }
123
+
124
+ // Inflate (with miniz)
125
+ {
126
+ Size read_len = 0;
127
+ for (;;) {
128
+ if (max_len < out_len) {
129
+ memcpy_safe(user_buf, out_ptr, (size_t)max_len);
130
+ read_len += max_len;
131
+ out_ptr += max_len;
132
+ out_len -= max_len;
133
+
134
+ return read_len;
135
+ } else {
136
+ memcpy_safe(user_buf, out_ptr, (size_t)out_len);
137
+ read_len += out_len;
138
+ user_buf = (uint8_t *)user_buf + out_len;
139
+ max_len -= out_len;
140
+ out_ptr = out_buf;
141
+ out_len = 0;
142
+
143
+ if (done) {
144
+ SetEOF(true);
145
+ return read_len;
146
+ }
147
+ }
148
+
149
+ while (out_len < RG_SIZE(out_buf)) {
150
+ if (!in_len) {
151
+ in_ptr = in_buf;
152
+ in_len = ReadRaw(RG_SIZE(in_buf), in_buf);
153
+ if (in_len < 0)
154
+ return read_len ? read_len : in_len;
155
+ }
156
+
157
+ size_t in_arg = (size_t)in_len;
158
+ size_t out_arg = (size_t)(RG_SIZE(out_buf) - out_len);
159
+ uint32_t flags = (uint32_t)
160
+ ((is_gzip ? 0 : TINFL_FLAG_PARSE_ZLIB_HEADER) |
161
+ (IsSourceEOF() ? 0 : TINFL_FLAG_HAS_MORE_INPUT));
162
+
163
+ tinfl_status status = tinfl_decompress(&inflator, in_ptr, &in_arg,
164
+ out_buf, out_buf + out_len,
165
+ &out_arg, flags);
166
+
167
+ if (is_gzip) {
168
+ crc32 = (uint32_t)mz_crc32(crc32, out_buf + out_len, out_arg);
169
+ uncompressed_size += (Size)out_arg;
170
+ }
171
+
172
+ in_ptr += (Size)in_arg;
173
+ in_len -= (Size)in_arg;
174
+ out_len += (Size)out_arg;
175
+
176
+ if (status == TINFL_STATUS_DONE) {
177
+ // Gzip footer (CRC and size check)
178
+ if (is_gzip) {
179
+ uint32_t footer[2];
180
+ RG_STATIC_ASSERT(RG_SIZE(footer) == 8);
181
+
182
+ if (in_len < RG_SIZE(footer)) {
183
+ memcpy_safe(footer, in_ptr, (size_t)in_len);
184
+
185
+ Size missing_len = RG_SIZE(footer) - in_len;
186
+ if (ReadRaw(missing_len, footer + in_len) < missing_len) {
187
+ if (IsValid()) {
188
+ goto truncated_error;
189
+ } else {
190
+ return -1;
191
+ }
192
+ }
193
+ } else {
194
+ memcpy_safe(footer, in_ptr, RG_SIZE(footer));
195
+ }
196
+ footer[0] = LittleEndian(footer[0]);
197
+ footer[1] = LittleEndian(footer[1]);
198
+
199
+ if (crc32 != footer[0] || (uint32_t)uncompressed_size != footer[1]) {
200
+ LogError("Failed CRC32 or size check in GZip stream '%1'", GetFileName());
201
+ return -1;
202
+ }
203
+ }
204
+
205
+ done = true;
206
+ break;
207
+ } else if (status < TINFL_STATUS_DONE) {
208
+ LogError("Failed to decompress '%1' (Deflate)", GetFileName());
209
+ return -1;
210
+ }
211
+ }
212
+ }
213
+ }
214
+
215
+ truncated_error:
216
+ LogError("Truncated Gzip header in '%1'", GetFileName());
217
+ return -1;
218
+ }
219
+
220
+ class MinizCompressor: public StreamCompressor {
221
+ tdefl_compressor deflator;
222
+
223
+ // Gzip support
224
+ bool is_gzip = false;
225
+ uint32_t crc32 = MZ_CRC32_INIT;
226
+ Size uncompressed_size = 0;
227
+
228
+ // Used to buffer small writes
229
+ LocalArray<uint8_t, 1024> small_buf;
230
+
231
+ public:
232
+ MinizCompressor(StreamWriter *writer) : StreamCompressor(writer) {}
233
+ ~MinizCompressor() {}
234
+
235
+ bool Init(CompressionType type, CompressionSpeed speed) override;
236
+ bool Write(Span<const uint8_t> buf) override;
237
+ bool Finalize() override;
238
+
239
+ private:
240
+ bool WriteDeflate(Span<const uint8_t> buf);
241
+ };
242
+
243
+ bool MinizCompressor::Init(CompressionType type, CompressionSpeed speed)
244
+ {
245
+ is_gzip = (type == CompressionType::Gzip);
246
+
247
+ int flags = 0;
248
+ switch (speed) {
249
+ case CompressionSpeed::Default: { flags = 32 | TDEFL_GREEDY_PARSING_FLAG; } break;
250
+ case CompressionSpeed::Slow: { flags = 512; } break;
251
+ case CompressionSpeed::Fast: { flags = 1 | TDEFL_GREEDY_PARSING_FLAG; } break;
252
+ }
253
+ flags |= (is_gzip ? 0 : TDEFL_WRITE_ZLIB_HEADER);
254
+
255
+ tdefl_status status = tdefl_init(&deflator, [](const void *buf, int len, void *udata) {
256
+ MinizCompressor *compressor = (MinizCompressor *)udata;
257
+ return (int)compressor->WriteRaw(MakeSpan((uint8_t *)buf, len));
258
+ }, this, flags);
259
+ if (status != TDEFL_STATUS_OKAY) {
260
+ LogError("Failed to initialize Deflate compression for '%1'", GetFileName());
261
+ return false;
262
+ }
263
+
264
+ if (is_gzip) {
265
+ static uint8_t gzip_header[] = {
266
+ 0x1F, 0x8B, // Fixed bytes
267
+ 8, // Deflate
268
+ 0, // FLG
269
+ 0, 0, 0, 0, // MTIME
270
+ 0, // XFL
271
+ 0 // OS
272
+ };
273
+
274
+ if (!WriteRaw(gzip_header))
275
+ return false;
276
+ }
277
+
278
+ return true;
279
+ }
280
+
281
+ bool MinizCompressor::Write(Span<const uint8_t> buf)
282
+ {
283
+ if (small_buf.len) {
284
+ Size copy_len = std::min(buf.len, small_buf.Available());
285
+
286
+ memcpy_safe(small_buf.end(), buf.ptr, copy_len);
287
+ small_buf.len += copy_len;
288
+ buf.ptr += copy_len;
289
+ buf.len -= copy_len;
290
+ }
291
+
292
+ if (buf.len) {
293
+ if (small_buf.len && !WriteDeflate(small_buf))
294
+ return false;
295
+ small_buf.Clear();
296
+
297
+ if (buf.len >= RG_SIZE(small_buf.data) / 2) {
298
+ if (!WriteDeflate(buf))
299
+ return false;
300
+ } else {
301
+ memcpy_safe(small_buf.data, buf.ptr, buf.len);
302
+ small_buf.len = buf.len;
303
+ }
304
+ }
305
+
306
+ return true;
307
+ }
308
+
309
+ bool MinizCompressor::WriteDeflate(Span<const uint8_t> buf)
310
+ {
311
+ if (is_gzip) {
312
+ crc32 = (uint32_t)mz_crc32(crc32, buf.ptr, (size_t)buf.len);
313
+ uncompressed_size += buf.len;
314
+ }
315
+
316
+ tdefl_status status = tdefl_compress_buffer(&deflator, buf.ptr, (size_t)buf.len, TDEFL_NO_FLUSH);
317
+ if (status < TDEFL_STATUS_OKAY) {
318
+ if (status != TDEFL_STATUS_PUT_BUF_FAILED) {
319
+ LogError("Failed to deflate stream to '%1'", GetFileName());
320
+ }
321
+
322
+ return false;
323
+ }
324
+
325
+ return true;
326
+ }
327
+
328
+ bool MinizCompressor::Finalize()
329
+ {
330
+ if (small_buf.len && !WriteDeflate(small_buf))
331
+ return false;
332
+
333
+ uint8_t dummy; // Avoid UB in miniz
334
+ tdefl_status status = tdefl_compress_buffer(&deflator, &dummy, 0, TDEFL_FINISH);
335
+ if (status != TDEFL_STATUS_DONE) {
336
+ if (status != TDEFL_STATUS_PUT_BUF_FAILED) {
337
+ LogError("Failed to end Deflate stream for '%1", GetFileName());
338
+ }
339
+
340
+ return false;
341
+ }
342
+
343
+ if (is_gzip) {
344
+ uint32_t gzip_footer[] = {
345
+ LittleEndian(crc32),
346
+ LittleEndian((uint32_t)uncompressed_size)
347
+ };
348
+
349
+ if (!WriteRaw(MakeSpan((uint8_t *)gzip_footer, RG_SIZE(gzip_footer))))
350
+ return false;
351
+ }
352
+
353
+ return true;
354
+ }
355
+
356
+ RG_REGISTER_DECOMPRESSOR(CompressionType::Zlib, MinizDecompressor);
357
+ RG_REGISTER_DECOMPRESSOR(CompressionType::Gzip, MinizDecompressor);
358
+ RG_REGISTER_COMPRESSOR(CompressionType::Zlib, MinizCompressor);
359
+ RG_REGISTER_COMPRESSOR(CompressionType::Gzip, MinizCompressor);
360
+
361
+ }
@@ -108,7 +108,7 @@ endif()
108
108
  add_node_addon(NAME koffi SOURCES ${KOFFI_SRC})
109
109
  target_include_directories(koffi PRIVATE . ../.. ../../vendor/node-addon-api)
110
110
 
111
- target_compile_definitions(koffi PRIVATE FELIX_TARGET=koffi NAPI_DISABLE_CPP_EXCEPTIONS NAPI_VERSION=8 LIBCC_NO_BROTLI LIBCC_NO_MINIZ)
111
+ target_compile_definitions(koffi PRIVATE FELIX_TARGET=koffi NAPI_DISABLE_CPP_EXCEPTIONS NAPI_VERSION=8 LIBCC_NO_STATX)
112
112
  if(WIN32)
113
113
  target_compile_definitions(koffi PRIVATE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
114
114
  target_link_libraries(koffi PRIVATE ws2_32)
@@ -1049,6 +1049,24 @@ bool CallData::PushPointer(Napi::Value value, const TypeInfo *type, int directio
1049
1049
  return true;
1050
1050
  } break;
1051
1051
 
1052
+ case napi_number: {
1053
+ Napi::Number number = value.As<Napi::Number>();
1054
+ intptr_t ptr = (intptr_t)number.Int32Value();
1055
+
1056
+ *out_ptr = (void *)ptr;
1057
+ return true;
1058
+ } break;
1059
+
1060
+ case napi_bigint: {
1061
+ Napi::BigInt bigint = value.As<Napi::BigInt>();
1062
+
1063
+ bool lossless;
1064
+ intptr_t ptr = (intptr_t)bigint.Int64Value(&lossless);
1065
+
1066
+ *out_ptr = (void *)ptr;
1067
+ return true;
1068
+ } break;
1069
+
1052
1070
  default: {} break;
1053
1071
  }
1054
1072