asherah 1.3.23 → 1.3.24

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/binding.gyp CHANGED
@@ -19,6 +19,7 @@
19
19
  'defines': [ 'NAPI_CPP_EXCEPTIONS', 'NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS', 'NODE_ADDON_API_DISABLE_DEPRECATED', 'NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED' ],
20
20
  'sources': [
21
21
  'lib/libasherah.h',
22
+ 'src/asherah.h',
22
23
  'src/asherah.cc',
23
24
  'src/logging.cc',
24
25
  'src/logging.h',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asherah",
3
- "version": "1.3.23",
3
+ "version": "1.3.24",
4
4
  "description": "Asherah envelope encryption and key rotation library",
5
5
  "exports": {
6
6
  "node-addons": "./dist/asherah.node"
@@ -22,6 +22,7 @@
22
22
  "license": "MIT",
23
23
  "files": [
24
24
  "binding.gyp",
25
+ "src/asherah.h",
25
26
  "src/asherah.cc",
26
27
  "src/hints.h",
27
28
  "src/logging.h",
package/src/asherah.cc CHANGED
@@ -1,3 +1,4 @@
1
+ #include "asherah.h"
1
2
  #include "../lib/libasherah.h"
2
3
  #include "cobhan_napi_interop.h"
3
4
  #include "hints.h"
@@ -12,71 +13,6 @@ size_t maximum_stack_alloc_size = 2048;
12
13
  int32_t setup_state = 0;
13
14
  std::mutex asherah_lock;
14
15
 
15
- __attribute__((always_inline)) inline size_t
16
- estimate_asherah_output_size_bytes(size_t data_byte_len,
17
- size_t partition_byte_len) {
18
- const size_t est_encryption_overhead = 48;
19
- const size_t est_envelope_overhead = 185;
20
- const double base64_overhead = 1.34;
21
-
22
- // Add one rather than using std::ceil to round up
23
- double est_data_byte_len =
24
- (double(data_byte_len + est_encryption_overhead) * base64_overhead) + 1;
25
-
26
- size_t asherah_output_size_bytes =
27
- size_t(est_envelope_overhead + est_intermediate_key_overhead +
28
- partition_byte_len + est_data_byte_len);
29
- if (unlikely(verbose_flag)) {
30
- std::string log_msg =
31
- "estimate_asherah_output_size(" + std::to_string(data_byte_len) + ", " +
32
- std::to_string(partition_byte_len) +
33
- ") est_data_byte_len: " + std::to_string(est_data_byte_len) +
34
- " asherah_output_size_bytes: " +
35
- std::to_string(asherah_output_size_bytes);
36
- debug_log(__func__, log_msg);
37
- }
38
- return asherah_output_size_bytes;
39
- }
40
-
41
- __attribute__((always_inline)) inline const char* asherah_cobhan_error_to_string(int32_t error) {
42
- switch(error) {
43
- case 0:
44
- return "Success";
45
- case -1:
46
- return "Cobhan error: NULL pointer";
47
- case -2:
48
- return "Cobhan error: Buffer too large";
49
- case -3:
50
- return "Cobhan error: Buffer too small";
51
- case -4:
52
- return "Cobhan error: Copy failed";
53
- case -5:
54
- return "Cobhan error: JSON decode failed";
55
- case -6:
56
- return "Cobhan error: JSON encode failed";
57
- case -7:
58
- return "Cobhan error: Invalid UTF-8";
59
- case -8:
60
- return "Cobhan error: Read temp file failed";
61
- case -9:
62
- return "Cobhan error: Write temp file failed";
63
- case -100:
64
- return "Asherah error: Not initialized";
65
- case -101:
66
- return "Asherah error: Already initialized";
67
- case -102:
68
- return "Asherah error: Failed to get session";
69
- case -103:
70
- return "Asherah error: Encrypt operation failed";
71
- case -104:
72
- return "Asherah error: Decrypt operation failed";
73
- case -105:
74
- return "Asherah error: Invalid configuration";
75
- default:
76
- return "Unknown error";
77
- }
78
- }
79
-
80
16
  void setup(const Napi::CallbackInfo &info) {
81
17
  std::lock_guard<std::mutex> lock(asherah_lock);
82
18
 
@@ -143,8 +79,7 @@ void setup(const Napi::CallbackInfo &info) {
143
79
 
144
80
  if (unlikely(!check_canary_ptr(config_canary_ptr))) {
145
81
  log_error_and_throw(
146
- __func__,
147
- "Failed post-call canary check for config_cobhan_buffer");
82
+ __func__, "Failed post-call canary check for config_cobhan_buffer");
148
83
  }
149
84
 
150
85
  if (unlikely(result < 0)) {
@@ -153,73 +88,6 @@ void setup(const Napi::CallbackInfo &info) {
153
88
  setup_state = 1;
154
89
  }
155
90
 
156
- Napi::String encrypt_to_json(Napi::Env &env, size_t partition_bytes,
157
- size_t data_bytes,
158
- char *partition_id_cobhan_buffer,
159
- char *input_cobhan_buffer) {
160
-
161
- size_t asherah_output_size_bytes =
162
- estimate_asherah_output_size_bytes(data_bytes, partition_bytes);
163
-
164
- if (unlikely(verbose_flag)) {
165
- debug_log(__func__, " asherah_output_size_bytes " +
166
- std::to_string(asherah_output_size_bytes));
167
- }
168
-
169
- char *output_cobhan_buffer;
170
- ALLOCATE_CBUFFER(output_cobhan_buffer, asherah_output_size_bytes,
171
- maximum_stack_alloc_size, __func__);
172
-
173
- char *partition_id_canary_ptr = get_canary_ptr(partition_id_cobhan_buffer);
174
- if (unlikely(!check_canary_ptr(partition_id_canary_ptr))) {
175
- log_error_and_throw(__func__,
176
- "Failed initial canary check for partition_id_cobhan_buffer");
177
- }
178
- char *input_canary_ptr = get_canary_ptr(input_cobhan_buffer);
179
- if (unlikely(!check_canary_ptr(input_canary_ptr))) {
180
- log_error_and_throw(__func__,
181
- "Failed initial canary check for input_cobhan_buffer");
182
- }
183
- char *output_canary_ptr = get_canary_ptr(output_cobhan_buffer);
184
- if (unlikely(!check_canary_ptr(output_canary_ptr))) {
185
- log_error_and_throw(__func__,
186
- "Failed initial canary check for output_cobhan_buffer");
187
- }
188
-
189
- if (unlikely(verbose_flag)) {
190
- debug_log(__func__, "Calling asherah-cobhan EncryptToJson");
191
- }
192
-
193
- // extern GoInt32 EncryptToJson(void* partitionIdPtr, void* dataPtr, void*
194
- // jsonPtr);
195
- GoInt32 result = EncryptToJson(partition_id_cobhan_buffer,
196
- input_cobhan_buffer, output_cobhan_buffer);
197
-
198
- if (unlikely(verbose_flag)) {
199
- debug_log(__func__, "Returning from asherah-cobhan EncryptToJson");
200
- }
201
-
202
- if (unlikely(!check_canary_ptr(partition_id_canary_ptr))) {
203
- log_error_and_throw(__func__,
204
- "Failed post-call canary check for partition_id_cobhan_buffer");
205
- }
206
- if (unlikely(!check_canary_ptr(input_canary_ptr))) {
207
- log_error_and_throw(__func__,
208
- "Failed post-call canary check for input_cobhan_buffer");
209
- }
210
- if (unlikely(!check_canary_ptr(output_canary_ptr))) {
211
- log_error_and_throw(__func__,
212
- "Failed post-call canary check for output_cobhan_buffer");
213
- }
214
-
215
- if (unlikely(result < 0)) {
216
- log_error_and_throw(__func__, asherah_cobhan_error_to_string(result));
217
- }
218
-
219
- Napi::String output = cbuffer_to_nstring(env, output_cobhan_buffer);
220
- return output;
221
- }
222
-
223
91
  Napi::String encrypt(const Napi::CallbackInfo &info) {
224
92
  std::lock_guard<std::mutex> lock(asherah_lock);
225
93
 
@@ -312,6 +180,74 @@ Napi::String encrypt_string(const Napi::CallbackInfo &info) {
312
180
  return output;
313
181
  }
314
182
 
183
+ Napi::String encrypt_to_json(Napi::Env &env, size_t partition_bytes,
184
+ size_t data_bytes,
185
+ char *partition_id_cobhan_buffer,
186
+ char *input_cobhan_buffer) {
187
+
188
+ size_t asherah_output_size_bytes =
189
+ estimate_asherah_output_size_bytes(data_bytes, partition_bytes);
190
+
191
+ if (unlikely(verbose_flag)) {
192
+ debug_log(__func__, " asherah_output_size_bytes " +
193
+ std::to_string(asherah_output_size_bytes));
194
+ }
195
+
196
+ char *output_cobhan_buffer;
197
+ ALLOCATE_CBUFFER(output_cobhan_buffer, asherah_output_size_bytes,
198
+ maximum_stack_alloc_size, __func__);
199
+
200
+ char *partition_id_canary_ptr = get_canary_ptr(partition_id_cobhan_buffer);
201
+ if (unlikely(!check_canary_ptr(partition_id_canary_ptr))) {
202
+ log_error_and_throw(
203
+ __func__, "Failed initial canary check for partition_id_cobhan_buffer");
204
+ }
205
+ char *input_canary_ptr = get_canary_ptr(input_cobhan_buffer);
206
+ if (unlikely(!check_canary_ptr(input_canary_ptr))) {
207
+ log_error_and_throw(__func__,
208
+ "Failed initial canary check for input_cobhan_buffer");
209
+ }
210
+ char *output_canary_ptr = get_canary_ptr(output_cobhan_buffer);
211
+ if (unlikely(!check_canary_ptr(output_canary_ptr))) {
212
+ log_error_and_throw(__func__,
213
+ "Failed initial canary check for output_cobhan_buffer");
214
+ }
215
+
216
+ if (unlikely(verbose_flag)) {
217
+ debug_log(__func__, "Calling asherah-cobhan EncryptToJson");
218
+ }
219
+
220
+ // extern GoInt32 EncryptToJson(void* partitionIdPtr, void* dataPtr, void*
221
+ // jsonPtr);
222
+ GoInt32 result = EncryptToJson(partition_id_cobhan_buffer,
223
+ input_cobhan_buffer, output_cobhan_buffer);
224
+
225
+ if (unlikely(verbose_flag)) {
226
+ debug_log(__func__, "Returning from asherah-cobhan EncryptToJson");
227
+ }
228
+
229
+ if (unlikely(!check_canary_ptr(partition_id_canary_ptr))) {
230
+ log_error_and_throw(
231
+ __func__,
232
+ "Failed post-call canary check for partition_id_cobhan_buffer");
233
+ }
234
+ if (unlikely(!check_canary_ptr(input_canary_ptr))) {
235
+ log_error_and_throw(
236
+ __func__, "Failed post-call canary check for input_cobhan_buffer");
237
+ }
238
+ if (unlikely(!check_canary_ptr(output_canary_ptr))) {
239
+ log_error_and_throw(
240
+ __func__, "Failed post-call canary check for output_cobhan_buffer");
241
+ }
242
+
243
+ if (unlikely(result < 0)) {
244
+ log_error_and_throw(__func__, asherah_cobhan_error_to_string(result));
245
+ }
246
+
247
+ Napi::String output = cbuffer_to_nstring(env, output_cobhan_buffer);
248
+ return output;
249
+ }
250
+
315
251
  Napi::Buffer<unsigned char> decrypt(const Napi::CallbackInfo &info) {
316
252
  std::lock_guard<std::mutex> lock(asherah_lock);
317
253
 
@@ -353,8 +289,7 @@ Napi::Buffer<unsigned char> decrypt(const Napi::CallbackInfo &info) {
353
289
  char *partition_id_canary_ptr = get_canary_ptr(partition_id_cobhan_buffer);
354
290
  if (unlikely(!check_canary_ptr(partition_id_canary_ptr))) {
355
291
  log_error_and_throw(
356
- __func__,
357
- "Failed initial canary check for partition_id_cobhan_buffer");
292
+ __func__, "Failed initial canary check for partition_id_cobhan_buffer");
358
293
  }
359
294
  char *input_canary_ptr = get_canary_ptr(input_cobhan_buffer);
360
295
  if (unlikely(!check_canary_ptr(input_canary_ptr))) {
@@ -381,16 +316,17 @@ Napi::Buffer<unsigned char> decrypt(const Napi::CallbackInfo &info) {
381
316
  }
382
317
 
383
318
  if (unlikely(!check_canary_ptr(partition_id_canary_ptr))) {
384
- log_error_and_throw(__func__,
319
+ log_error_and_throw(
320
+ __func__,
385
321
  "Failed post-call canary check for partition_id_cobhan_buffer");
386
322
  }
387
323
  if (unlikely(!check_canary_ptr(input_canary_ptr))) {
388
- log_error_and_throw(__func__,
389
- "Failed post-call canary check for input_cobhan_buffer");
324
+ log_error_and_throw(
325
+ __func__, "Failed post-call canary check for input_cobhan_buffer");
390
326
  }
391
327
  if (unlikely(!check_canary_ptr(output_canary_ptr))) {
392
- log_error_and_throw(__func__,
393
- "Failed post-call canary check for output_cobhan_buffer");
328
+ log_error_and_throw(
329
+ __func__, "Failed post-call canary check for output_cobhan_buffer");
394
330
  }
395
331
 
396
332
  if (unlikely(result < 0)) {
@@ -448,8 +384,7 @@ Napi::String decrypt_string(const Napi::CallbackInfo &info) {
448
384
  char *partition_id_canary_ptr = get_canary_ptr(partition_id_cobhan_buffer);
449
385
  if (unlikely(!check_canary_ptr(partition_id_canary_ptr))) {
450
386
  log_error_and_throw(
451
- __func__,
452
- "Failed initial canary check for partition_id_cobhan_buffer");
387
+ __func__, "Failed initial canary check for partition_id_cobhan_buffer");
453
388
  }
454
389
  char *input_canary_ptr = get_canary_ptr(input_cobhan_buffer);
455
390
  if (unlikely(!check_canary_ptr(input_canary_ptr))) {
@@ -476,16 +411,17 @@ Napi::String decrypt_string(const Napi::CallbackInfo &info) {
476
411
  }
477
412
 
478
413
  if (unlikely(!check_canary_ptr(partition_id_canary_ptr))) {
479
- log_error_and_throw(__func__,
414
+ log_error_and_throw(
415
+ __func__,
480
416
  "Failed post-call canary check for partition_id_cobhan_buffer");
481
417
  }
482
418
  if (unlikely(!check_canary_ptr(input_canary_ptr))) {
483
- log_error_and_throw(__func__,
484
- "Failed post-call canary check for input_cobhan_buffer");
419
+ log_error_and_throw(
420
+ __func__, "Failed post-call canary check for input_cobhan_buffer");
485
421
  }
486
422
  if (unlikely(!check_canary_ptr(output_canary_ptr))) {
487
- log_error_and_throw(__func__,
488
- "Failed post-call canary check for output_cobhan_buffer");
423
+ log_error_and_throw(
424
+ __func__, "Failed post-call canary check for output_cobhan_buffer");
489
425
  }
490
426
 
491
427
  if (unlikely(result < 0)) {
@@ -530,8 +466,7 @@ void set_max_stack_alloc_item_size(const Napi::CallbackInfo &info) {
530
466
  }
531
467
 
532
468
  if (unlikely(info.Length() < 1)) {
533
- log_error_and_throw(__func__,
534
- "Wrong number of arguments");
469
+ log_error_and_throw(__func__, "Wrong number of arguments");
535
470
  }
536
471
 
537
472
  Napi::Number item_size = info[0].ToNumber();
@@ -547,8 +482,7 @@ void set_safety_padding_overhead(const Napi::CallbackInfo &info) {
547
482
  }
548
483
 
549
484
  if (unlikely(info.Length() < 1)) {
550
- log_error_and_throw(__func__,
551
- "Wrong number of arguments");
485
+ log_error_and_throw(__func__, "Wrong number of arguments");
552
486
  }
553
487
 
554
488
  Napi::Number safety_padding_number = info[0].ToNumber();
@@ -556,6 +490,72 @@ void set_safety_padding_overhead(const Napi::CallbackInfo &info) {
556
490
  set_safety_padding_bytes((size_t)safety_padding_number.Int32Value());
557
491
  }
558
492
 
493
+ __attribute__((always_inline)) inline size_t
494
+ estimate_asherah_output_size_bytes(size_t data_byte_len,
495
+ size_t partition_byte_len) {
496
+ const size_t est_encryption_overhead = 48;
497
+ const size_t est_envelope_overhead = 185;
498
+ const double base64_overhead = 1.34;
499
+
500
+ // Add one rather than using std::ceil to round up
501
+ double est_data_byte_len =
502
+ (double(data_byte_len + est_encryption_overhead) * base64_overhead) + 1;
503
+
504
+ size_t asherah_output_size_bytes =
505
+ size_t(est_envelope_overhead + est_intermediate_key_overhead +
506
+ partition_byte_len + est_data_byte_len);
507
+ if (unlikely(verbose_flag)) {
508
+ std::string log_msg =
509
+ "estimate_asherah_output_size(" + std::to_string(data_byte_len) + ", " +
510
+ std::to_string(partition_byte_len) +
511
+ ") est_data_byte_len: " + std::to_string(est_data_byte_len) +
512
+ " asherah_output_size_bytes: " +
513
+ std::to_string(asherah_output_size_bytes);
514
+ debug_log(__func__, log_msg);
515
+ }
516
+ return asherah_output_size_bytes;
517
+ }
518
+
519
+ __attribute__((always_inline)) inline const char *
520
+ asherah_cobhan_error_to_string(int32_t error) {
521
+ switch (error) {
522
+ case 0:
523
+ return "Success";
524
+ case -1:
525
+ return "Cobhan error: NULL pointer";
526
+ case -2:
527
+ return "Cobhan error: Buffer too large";
528
+ case -3:
529
+ return "Cobhan error: Buffer too small";
530
+ case -4:
531
+ return "Cobhan error: Copy failed";
532
+ case -5:
533
+ return "Cobhan error: JSON decode failed";
534
+ case -6:
535
+ return "Cobhan error: JSON encode failed";
536
+ case -7:
537
+ return "Cobhan error: Invalid UTF-8";
538
+ case -8:
539
+ return "Cobhan error: Read temp file failed";
540
+ case -9:
541
+ return "Cobhan error: Write temp file failed";
542
+ case -100:
543
+ return "Asherah error: Not initialized";
544
+ case -101:
545
+ return "Asherah error: Already initialized";
546
+ case -102:
547
+ return "Asherah error: Failed to get session";
548
+ case -103:
549
+ return "Asherah error: Encrypt operation failed";
550
+ case -104:
551
+ return "Asherah error: Decrypt operation failed";
552
+ case -105:
553
+ return "Asherah error: Invalid configuration";
554
+ default:
555
+ return "Unknown error";
556
+ }
557
+ }
558
+
559
559
  Napi::Object Init(Napi::Env env, Napi::Object exports) {
560
560
  exports.Set(Napi::String::New(env, "setup"), Napi::Function::New(env, setup));
561
561
  exports.Set(Napi::String::New(env, "encrypt"),
package/src/asherah.h ADDED
@@ -0,0 +1,14 @@
1
+ #ifndef ASHERAH_H
2
+ #define ASHERAH_H
3
+ #include <cstdint>
4
+ #include <napi.h>
5
+ #include <stddef.h>
6
+
7
+ size_t estimate_asherah_output_size_bytes(size_t data_byte_len,
8
+ size_t partition_byte_len);
9
+ const char *asherah_cobhan_error_to_string(int32_t error);
10
+ Napi::String encrypt_to_json(Napi::Env &env, size_t partition_bytes,
11
+ size_t data_bytes,
12
+ char *partition_id_cobhan_buffer,
13
+ char *input_cobhan_buffer);
14
+ #endif
package/src/cobhan.h CHANGED
@@ -37,9 +37,8 @@ calculate_cobhan_buffer_allocation_size(size_t data_len_bytes) {
37
37
  __attribute__((always_inline)) inline void
38
38
  configure_cbuffer(char *cobhan_buffer, size_t length) {
39
39
  if (unlikely(verbose_flag)) {
40
- debug_log(__func__, "configure_cbuffer(" +
41
- format_ptr(cobhan_buffer) + ", " +
42
- std::to_string(length) + ")");
40
+ debug_log(__func__, "configure_cbuffer(" + format_ptr(cobhan_buffer) +
41
+ ", " + std::to_string(length) + ")");
43
42
  }
44
43
 
45
44
  *((int32_t *)cobhan_buffer) = length;
@@ -87,8 +86,7 @@ heap_allocate_cbuffer(const char *variable_name, size_t size_bytes) {
87
86
  calculate_cobhan_buffer_allocation_size(size_bytes);
88
87
 
89
88
  if (unlikely(verbose_flag)) {
90
- debug_log_new(__func__, variable_name,
91
- cobhan_buffer_allocation_size);
89
+ debug_log_new(__func__, variable_name, cobhan_buffer_allocation_size);
92
90
  }
93
91
 
94
92
  char *cobhan_buffer = new (std::nothrow) char[cobhan_buffer_allocation_size];
@@ -6,56 +6,7 @@
6
6
  #include <napi.h>
7
7
  #include <string>
8
8
 
9
- std::string napi_status_to_string(napi_status status) {
10
- switch (status) {
11
- case napi_ok:
12
- return "napi_ok";
13
- case napi_invalid_arg:
14
- return "napi_invalid_arg";
15
- case napi_object_expected:
16
- return "napi_object_expected";
17
- case napi_string_expected:
18
- return "napi_string_expected";
19
- case napi_name_expected:
20
- return "napi_name_expected";
21
- case napi_function_expected:
22
- return "napi_function_expected";
23
- case napi_number_expected:
24
- return "napi_number_expected";
25
- case napi_boolean_expected:
26
- return "napi_boolean_expected";
27
- case napi_array_expected:
28
- return "napi_array_expected";
29
- case napi_generic_failure:
30
- return "napi_generic_failure";
31
- case napi_pending_exception:
32
- return "napi_pending_exception";
33
- case napi_cancelled:
34
- return "napi_cancelled";
35
- case napi_escape_called_twice:
36
- return "napi_escape_called_twice";
37
- case napi_handle_scope_mismatch:
38
- return "napi_handle_scope_mismatch";
39
- case napi_callback_scope_mismatch:
40
- return "napi_callback_scope_mismatch";
41
- case napi_queue_full:
42
- return "napi_queue_full";
43
- case napi_closing:
44
- return "napi_closing";
45
- case napi_bigint_expected:
46
- return "napi_bigint_expected";
47
- case napi_date_expected:
48
- return "napi_date_expected";
49
- case napi_arraybuffer_expected:
50
- return "napi_arraybuffer_expected";
51
- case napi_detachable_arraybuffer_expected:
52
- return "napi_detachable_arraybuffer_expected";
53
- case napi_would_deadlock:
54
- return "napi_would_deadlock";
55
- default:
56
- return "Unknown napi_status";
57
- }
58
- }
9
+ std::string napi_status_to_string(napi_status status);
59
10
 
60
11
  __attribute__((always_inline)) inline size_t
61
12
  nstring_utf8_byte_length(Napi::Env &env, Napi::String &str) {
@@ -64,9 +15,8 @@ nstring_utf8_byte_length(Napi::Env &env, Napi::String &str) {
64
15
 
65
16
  status = napi_get_value_string_utf8(env, str, nullptr, 0, &utf8_length);
66
17
  if (unlikely(status != napi_ok)) {
67
- error_log(__func__,
68
- "napi_get_value_string_utf8 length check failed: " +
69
- napi_status_to_string(status));
18
+ error_log(__func__, "napi_get_value_string_utf8 length check failed: " +
19
+ napi_status_to_string(status));
70
20
  return (size_t)(-1);
71
21
  }
72
22
 
@@ -105,17 +55,15 @@ copy_nstring_to_cbuffer(Napi::Env &env, Napi::String &str,
105
55
  status = napi_get_value_string_utf8(env, str, cbuffer_data_ptr(cobhan_buffer),
106
56
  str_utf8_byte_length + 1, &copied_bytes);
107
57
  if (unlikely(status != napi_ok)) {
108
- error_log(__func__,
109
- "Napi utf8 string conversion failure: " +
110
- napi_status_to_string(status));
58
+ error_log(__func__, "Napi utf8 string conversion failure: " +
59
+ napi_status_to_string(status));
111
60
  return nullptr;
112
61
  }
113
62
 
114
63
  if (unlikely(copied_bytes != str_utf8_byte_length)) {
115
- error_log(__func__,
116
- "Did not copy expected number of bytes " +
117
- std::to_string(str_utf8_byte_length) + " copied " +
118
- std::to_string(copied_bytes));
64
+ error_log(__func__, "Did not copy expected number of bytes " +
65
+ std::to_string(str_utf8_byte_length) + " copied " +
66
+ std::to_string(copied_bytes));
119
67
  return nullptr;
120
68
  }
121
69
 
@@ -131,8 +79,7 @@ cbuffer_to_nstring(Napi::Env &env, char *cobhan_buffer) {
131
79
 
132
80
  int32_t cobhan_buffer_size_bytes = cbuffer_byte_length(cobhan_buffer);
133
81
  if (cobhan_buffer_size_bytes <= 0) {
134
- log_error_and_throw(__func__,
135
- "Invalid cobhan buffer byte length");
82
+ log_error_and_throw(__func__, "Invalid cobhan buffer byte length");
136
83
  }
137
84
 
138
85
  // Using C function because it allows length delimited input
@@ -140,9 +87,8 @@ cbuffer_to_nstring(Napi::Env &env, char *cobhan_buffer) {
140
87
  env, cbuffer_data_ptr(cobhan_buffer), cobhan_buffer_size_bytes, &output);
141
88
 
142
89
  if (unlikely(status != napi_ok)) {
143
- log_error_and_throw(__func__,
144
- "napi_create_string_utf8 failed: " +
145
- napi_status_to_string(status));
90
+ log_error_and_throw(__func__, "napi_create_string_utf8 failed: " +
91
+ napi_status_to_string(status));
146
92
  }
147
93
 
148
94
  return Napi::String(env, output);
@@ -152,8 +98,7 @@ __attribute__((always_inline)) inline Napi::Buffer<unsigned char>
152
98
  cbuffer_to_nbuffer(Napi::Env &env, char *cobhan_buffer) {
153
99
  int32_t cobhan_buffer_byte_length = cbuffer_byte_length(cobhan_buffer);
154
100
  if (unlikely(cobhan_buffer_byte_length <= 0)) {
155
- log_error_and_throw(__func__,
156
- "Invalid cobhan buffer byte length");
101
+ log_error_and_throw(__func__, "Invalid cobhan buffer byte length");
157
102
  }
158
103
 
159
104
  Napi::Buffer<unsigned char> nbuffer = Napi::Buffer<unsigned char>::Copy(
@@ -189,8 +134,8 @@ copy_nbuffer_to_cbuffer(Napi::Env &env, Napi::Buffer<unsigned char> &nbuffer,
189
134
  debug_log(__func__,
190
135
  "Copying " + std::to_string(nbuffer_byte_length) + " bytes to " +
191
136
  format_ptr(cbuffer_data_ptr(cobhan_buffer)) + " - " +
192
- format_ptr((cbuffer_data_ptr(cobhan_buffer) +
193
- nbuffer_byte_length)));
137
+ format_ptr(
138
+ (cbuffer_data_ptr(cobhan_buffer) + nbuffer_byte_length)));
194
139
  }
195
140
 
196
141
  memcpy(cbuffer_data_ptr(cobhan_buffer), nbuffer.Data(), nbuffer_byte_length);
@@ -250,4 +195,55 @@ copy_nbuffer_to_cbuffer(Napi::Env &env, Napi::Buffer<unsigned char> &nbuffer,
250
195
  bytes_copied = napi_buffer##_byte_length; \
251
196
  } while (0);
252
197
 
198
+ std::string napi_status_to_string(napi_status status) {
199
+ switch (status) {
200
+ case napi_ok:
201
+ return "napi_ok";
202
+ case napi_invalid_arg:
203
+ return "napi_invalid_arg";
204
+ case napi_object_expected:
205
+ return "napi_object_expected";
206
+ case napi_string_expected:
207
+ return "napi_string_expected";
208
+ case napi_name_expected:
209
+ return "napi_name_expected";
210
+ case napi_function_expected:
211
+ return "napi_function_expected";
212
+ case napi_number_expected:
213
+ return "napi_number_expected";
214
+ case napi_boolean_expected:
215
+ return "napi_boolean_expected";
216
+ case napi_array_expected:
217
+ return "napi_array_expected";
218
+ case napi_generic_failure:
219
+ return "napi_generic_failure";
220
+ case napi_pending_exception:
221
+ return "napi_pending_exception";
222
+ case napi_cancelled:
223
+ return "napi_cancelled";
224
+ case napi_escape_called_twice:
225
+ return "napi_escape_called_twice";
226
+ case napi_handle_scope_mismatch:
227
+ return "napi_handle_scope_mismatch";
228
+ case napi_callback_scope_mismatch:
229
+ return "napi_callback_scope_mismatch";
230
+ case napi_queue_full:
231
+ return "napi_queue_full";
232
+ case napi_closing:
233
+ return "napi_closing";
234
+ case napi_bigint_expected:
235
+ return "napi_bigint_expected";
236
+ case napi_date_expected:
237
+ return "napi_date_expected";
238
+ case napi_arraybuffer_expected:
239
+ return "napi_arraybuffer_expected";
240
+ case napi_detachable_arraybuffer_expected:
241
+ return "napi_detachable_arraybuffer_expected";
242
+ case napi_would_deadlock:
243
+ return "napi_would_deadlock";
244
+ default:
245
+ return "Unknown napi_status";
246
+ }
247
+ }
248
+
253
249
  #endif