koffi 2.6.10 → 2.6.12

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/CHANGELOG.md CHANGED
@@ -4,6 +4,15 @@
4
4
 
5
5
  ### Koffi 2.6
6
6
 
7
+ #### Koffi 2.6.12 (2023-12-11)
8
+
9
+ - Fix possible crash introduced in Koffi 2.6.11
10
+
11
+ #### Koffi 2.6.11 (2023-12-05)
12
+
13
+ - Speed up resolving simple and often used type names
14
+ - Fix use of optional length argument with [koffi.encode()](variables.md#encode-to-c-memory)
15
+
7
16
  #### Koffi 2.6.10 (2023-11-29)
8
17
 
9
18
  - Protect GetLastError() value from Node.js and V8 on Windows
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/doc/variables.md CHANGED
@@ -54,7 +54,7 @@ There is also an optional ending `length` argument that you can use in two cases
54
54
  - Use it to give the number of bytes to decode in non-NUL terminated strings: `koffi.decode(value, 'char *', 5)`
55
55
  - Decode consecutive values into an array. For example, here is how you can decode an array with 3 float values: `koffi.decode(value, 'float', 3)`. This is equivalent to `koffi.decode(value, koffi.array('float', 3))`.
56
56
 
57
- Thge example below will decode the symbol `my_string` defined above but only the first three bytes.
57
+ The example below will decode the symbol `my_string` defined above but only the first three bytes.
58
58
 
59
59
  ```js
60
60
  // Only decode 3 bytes from the C string my_string
@@ -69,7 +69,7 @@ In Koffi 2.2 and earlier versions, the length argument is only used to decode st
69
69
 
70
70
  *New in Koffi 2.6*
71
71
 
72
- Use `koffi.encode()` to encode C pointers, wrapped as external objects or as simple numbers.
72
+ Use `koffi.encode()` to encode JS values into C symbols or pointers, wrapped as external objects or as simple numbers.
73
73
 
74
74
  Some arguments are optional and this function can be called in several ways:
75
75
 
@@ -98,3 +98,9 @@ console.log(koffi.decode(my_string, 'const char *')) // Prints "Hello World!"
98
98
  ```
99
99
 
100
100
  When encoding strings (either directly or embedded in arrays or structs), the memory will be bound to the raw pointer value and managed by Koffi. You can assign to the same string again and again without any leak or risk of use-after-free.
101
+
102
+ There is also an optional ending `length` argument that you can use to encode an array. For example, here is how you can encode an array with 3 float values: `koffi.encode(symbol, 'float', [1, 2, 3], 3)`. This is equivalent to `koffi.encode(symbol, koffi.array('float', 3), [1, 2, 3])`.
103
+
104
+ ```{note}
105
+ The length argument did not work correctly before Koffi 2.6.11.
106
+ ```
package/index.js CHANGED
@@ -378,8 +378,8 @@ var require_package = __commonJS({
378
378
  "build/dist/src/koffi/package.json"(exports2, module2) {
379
379
  module2.exports = {
380
380
  name: "koffi",
381
- version: "2.6.10",
382
- stable: "2.6.10",
381
+ version: "2.6.12",
382
+ stable: "2.6.12",
383
383
  description: "Fast and simple C FFI (foreign function interface) for Node.js",
384
384
  keywords: [
385
385
  "foreign",
package/indirect.js CHANGED
@@ -378,8 +378,8 @@ var require_package = __commonJS({
378
378
  "build/dist/src/koffi/package.json"(exports2, module2) {
379
379
  module2.exports = {
380
380
  name: "koffi",
381
- version: "2.6.10",
382
- stable: "2.6.10",
381
+ version: "2.6.12",
382
+ stable: "2.6.12",
383
383
  description: "Fast and simple C FFI (foreign function interface) for Node.js",
384
384
  keywords: [
385
385
  "foreign",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "2.6.10",
4
- "stable": "2.6.10",
3
+ "version": "2.6.12",
4
+ "stable": "2.6.12",
5
5
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
6
6
  "keywords": [
7
7
  "foreign",
@@ -931,6 +931,35 @@ static Span<char> FormatUnsignedToDecimal(uint64_t value, char out_buf[32])
931
931
  return MakeSpan(out_buf + offset, 32 - offset);
932
932
  }
933
933
 
934
+ static Span<char> FormatUnsignedToBinary(uint64_t value, char out_buf[64])
935
+ {
936
+ Size msb = 64 - (Size)CountLeadingZeros(value);
937
+ if (!msb) {
938
+ msb = 1;
939
+ }
940
+
941
+ for (Size i = 0; i < msb; i++) {
942
+ bool bit = (value >> (msb - i - 1)) & 0x1;
943
+ out_buf[i] = bit ? '1' : '0';
944
+ }
945
+
946
+ return MakeSpan(out_buf, msb);
947
+ }
948
+
949
+ static Span<char> FormatUnsignedToOctal(uint64_t value, char out_buf[64])
950
+ {
951
+ static const char literals[] = "012345678";
952
+
953
+ Size offset = 64;
954
+ do {
955
+ uint64_t digit = value & 0x7;
956
+ value >>= 3;
957
+ out_buf[--offset] = literals[digit];
958
+ } while (value);
959
+
960
+ return MakeSpan(out_buf + offset, 64 - offset);
961
+ }
962
+
934
963
  static Span<char> FormatUnsignedToBigHex(uint64_t value, char out_buf[32])
935
964
  {
936
965
  static const char literals[] = "0123456789ABCDEF";
@@ -959,21 +988,6 @@ static Span<char> FormatUnsignedToSmallHex(uint64_t value, char out_buf[32])
959
988
  return MakeSpan(out_buf + offset, 32 - offset);
960
989
  }
961
990
 
962
- static Span<char> FormatUnsignedToBinary(uint64_t value, char out_buf[64])
963
- {
964
- Size msb = 64 - (Size)CountLeadingZeros(value);
965
- if (!msb) {
966
- msb = 1;
967
- }
968
-
969
- for (Size i = 0; i < msb; i++) {
970
- bool bit = (value >> (msb - i - 1)) & 0x1;
971
- out_buf[i] = bit ? '1' : '0';
972
- }
973
-
974
- return MakeSpan(out_buf, msb);
975
- }
976
-
977
991
  #ifdef JKJ_HEADER_DRAGONBOX
978
992
  static Size FakeFloatPrecision(Span<char> buf, int K, int min_prec, int max_prec, int *out_K)
979
993
  {
@@ -1250,6 +1264,9 @@ static inline void ProcessArg(const FmtArg &arg, AppendFunc append)
1250
1264
  case FmtType::Binary: {
1251
1265
  out = FormatUnsignedToBinary(arg.u.u, num_buf);
1252
1266
  } break;
1267
+ case FmtType::Octal: {
1268
+ out = FormatUnsignedToOctal(arg.u.u, num_buf);
1269
+ } break;
1253
1270
  case FmtType::BigHex: {
1254
1271
  out = FormatUnsignedToBigHex(arg.u.u, num_buf);
1255
1272
  } break;
@@ -1480,6 +1497,7 @@ static inline void ProcessArg(const FmtArg &arg, AppendFunc append)
1480
1497
  case FmtType::Integer:
1481
1498
  case FmtType::Unsigned:
1482
1499
  case FmtType::Binary:
1500
+ case FmtType::Octal:
1483
1501
  case FmtType::BigHex:
1484
1502
  case FmtType::SmallHex: {
1485
1503
  switch (arg.u.span.type_len) {
@@ -2155,7 +2173,7 @@ fail:
2155
2173
  return str_buf;
2156
2174
  }
2157
2175
 
2158
- static FileType FileAttributesToType(uint32_t attr)
2176
+ static inline FileType FileAttributesToType(uint32_t attr)
2159
2177
  {
2160
2178
  if (attr & FILE_ATTRIBUTE_DIRECTORY) {
2161
2179
  return FileType::Directory;
@@ -8354,4 +8372,72 @@ bool PromptYN(const char *prompt, bool *out_value)
8354
8372
  return prompter.ReadYN(out_value);
8355
8373
  }
8356
8374
 
8375
+ // ------------------------------------------------------------------------
8376
+ // Mime types
8377
+ // ------------------------------------------------------------------------
8378
+
8379
+ const char *GetMimeType(Span<const char> extension, const char *default_type)
8380
+ {
8381
+ static const HashMap<Span<const char>, const char *> mime_types = {
8382
+ #define MIMETYPE(Extension, MimeType) { (Extension), (MimeType) },
8383
+ #include "mimetypes.inc"
8384
+
8385
+ {"", "application/octet-stream"}
8386
+ };
8387
+
8388
+ const char *mime_type = mime_types.FindValue(extension, nullptr);
8389
+
8390
+ if (!mime_type) {
8391
+ LogError("Unknown MIME type for extension '%1'", extension);
8392
+ mime_type = default_type;
8393
+ }
8394
+
8395
+ return mime_type;
8396
+ }
8397
+
8398
+ bool CanCompressFile(const char *filename)
8399
+ {
8400
+ char extension[8];
8401
+ {
8402
+ const char *ptr = GetPathExtension(filename).ptr;
8403
+
8404
+ Size i = 0;
8405
+ while (i < RG_SIZE(extension) - 1 && ptr[i]) {
8406
+ extension[i] = LowerAscii(ptr[i]);
8407
+ i++;
8408
+ }
8409
+ extension[i] = 0;
8410
+ }
8411
+
8412
+ if (TestStrI(extension, ".zip"))
8413
+ return false;
8414
+ if (TestStrI(extension, ".rar"))
8415
+ return false;
8416
+ if (TestStrI(extension, ".7z"))
8417
+ return false;
8418
+ if (TestStrI(extension, ".gz") || TestStrI(extension, ".tgz"))
8419
+ return false;
8420
+ if (TestStrI(extension, ".bz2") || TestStrI(extension, ".tbz2"))
8421
+ return false;
8422
+ if (TestStrI(extension, ".xz") || TestStrI(extension, ".txz"))
8423
+ return false;
8424
+ if (TestStrI(extension, ".zst") || TestStrI(extension, ".tzst"))
8425
+ return false;
8426
+ if (TestStrI(extension, ".woff") || TestStrI(extension, ".woff2"))
8427
+ return false;
8428
+ if (TestStrI(extension, ".db") || TestStrI(extension, ".sqlite3"))
8429
+ return false;
8430
+
8431
+ const char *mime_type = GetMimeType(extension);
8432
+
8433
+ if (StartsWith(mime_type, "video/"))
8434
+ return false;
8435
+ if (StartsWith(mime_type, "audio/"))
8436
+ return false;
8437
+ if (StartsWith(mime_type, "image/") && !TestStr(mime_type, "image/svg+xml"))
8438
+ return false;
8439
+
8440
+ return true;
8441
+ }
8442
+
8357
8443
  }
@@ -2862,6 +2862,7 @@ enum class FmtType {
2862
2862
  Float,
2863
2863
  Double,
2864
2864
  Binary,
2865
+ Octal,
2865
2866
  BigHex,
2866
2867
  SmallHex,
2867
2868
  MemorySize,
@@ -2960,6 +2961,13 @@ static inline FmtArg FmtBin(uint64_t u)
2960
2961
  arg.u.u = u;
2961
2962
  return arg;
2962
2963
  }
2964
+ static inline FmtArg FmtOctal(uint64_t u)
2965
+ {
2966
+ FmtArg arg;
2967
+ arg.type = FmtType::Octal;
2968
+ arg.u.u = u;
2969
+ return arg;
2970
+ }
2963
2971
  static inline FmtArg FmtHex(uint64_t u, FmtType type = FmtType::BigHex)
2964
2972
  {
2965
2973
  RG_ASSERT(type == FmtType::BigHex || type == FmtType::SmallHex);
@@ -5008,4 +5016,12 @@ static inline const char *Prompt(const char *prompt, Allocator *alloc)
5008
5016
  { return Prompt(prompt, nullptr, nullptr, alloc); }
5009
5017
  bool PromptYN(const char *prompt, bool *out_value);
5010
5018
 
5019
+ // ------------------------------------------------------------------------
5020
+ // Mime types
5021
+ // ------------------------------------------------------------------------
5022
+
5023
+ const char *GetMimeType(Span<const char> extension, const char *default_typ = "application/octet-stream");
5024
+
5025
+ bool CanCompressFile(const char *filename);
5026
+
5011
5027
  }