asherah 1.3.13 → 1.3.14
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/package.json +1 -1
- package/src/asherah.cc +66 -36
- package/src/cobhan_napi_interop.h +69 -24
- package/src/logging.h +1 -1
package/package.json
CHANGED
package/src/asherah.cc
CHANGED
|
@@ -45,7 +45,8 @@ void setup(const Napi::CallbackInfo &info) {
|
|
|
45
45
|
Napi::String product_id = config_json.Get("ProductID").As<Napi::String>();
|
|
46
46
|
Napi::String service_name = config_json.Get("ServiceName").As<Napi::String>();
|
|
47
47
|
|
|
48
|
-
est_intermediate_key_overhead =
|
|
48
|
+
est_intermediate_key_overhead =
|
|
49
|
+
product_id.Utf8Value().length() + service_name.Utf8Value().length();
|
|
49
50
|
|
|
50
51
|
Napi::Value verbose = config_json.Get("Verbose");
|
|
51
52
|
if (likely(verbose.IsBoolean())) {
|
|
@@ -82,7 +83,8 @@ void setup(const Napi::CallbackInfo &info) {
|
|
|
82
83
|
config_cobhan_buffer = config_cobhan_buffer_unique_ptr.get();
|
|
83
84
|
}
|
|
84
85
|
if (unlikely(config_cobhan_buffer == nullptr)) {
|
|
85
|
-
log_error_and_throw(env, "setup",
|
|
86
|
+
log_error_and_throw(env, "setup",
|
|
87
|
+
"Failed to allocate config cobhan buffer");
|
|
86
88
|
return;
|
|
87
89
|
}
|
|
88
90
|
|
|
@@ -145,7 +147,7 @@ Napi::Value encrypt_to_json(Napi::Env &env, size_t partition_bytes,
|
|
|
145
147
|
}
|
|
146
148
|
if (unlikely(output_cobhan_buffer == nullptr)) {
|
|
147
149
|
return log_error_and_throw(env, "encrypt_to_json",
|
|
148
|
-
|
|
150
|
+
"Failed to allocate cobhan output buffer");
|
|
149
151
|
}
|
|
150
152
|
|
|
151
153
|
if (unlikely(verbose_flag)) {
|
|
@@ -195,7 +197,7 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
|
|
|
195
197
|
partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
|
|
196
198
|
if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
|
|
197
199
|
return log_error_and_throw(env, "encrypt",
|
|
198
|
-
|
|
200
|
+
"Failed to get partition_id utf8 length");
|
|
199
201
|
}
|
|
200
202
|
if (unlikely(partition_utf8_byte_length == 0)) {
|
|
201
203
|
return log_error_and_throw(env, "encrypt", "partition_id is empty");
|
|
@@ -221,7 +223,7 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
|
|
|
221
223
|
}
|
|
222
224
|
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
|
|
223
225
|
return log_error_and_throw(env, "encrypt",
|
|
224
|
-
|
|
226
|
+
"Failed to allocate partitionId cobhan buffer");
|
|
225
227
|
}
|
|
226
228
|
|
|
227
229
|
// Copy
|
|
@@ -231,7 +233,7 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
|
|
|
231
233
|
&partition_copied_bytes);
|
|
232
234
|
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
|
|
233
235
|
return log_error_and_throw(env, "encrypt",
|
|
234
|
-
|
|
236
|
+
"Failed to copy partitionId to cobhan buffer");
|
|
235
237
|
}
|
|
236
238
|
|
|
237
239
|
// Determine size
|
|
@@ -269,11 +271,18 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
|
|
|
269
271
|
copy_nbuffer_to_cbuffer(env, input_napi_buffer, input_cobhan_buffer);
|
|
270
272
|
if (unlikely(input_cobhan_buffer == nullptr)) {
|
|
271
273
|
return log_error_and_throw(env, "encrypt",
|
|
272
|
-
|
|
274
|
+
"Failed to copy input buffer to cobhan buffer");
|
|
273
275
|
}
|
|
274
276
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
+
Napi::Value output =
|
|
278
|
+
encrypt_to_json(env, partition_copied_bytes, input_byte_length,
|
|
279
|
+
partition_id_cobhan_buffer, input_cobhan_buffer);
|
|
280
|
+
|
|
281
|
+
if (unlikely(verbose_flag)) {
|
|
282
|
+
debug_log("encrypt", "finished");
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return output;
|
|
277
286
|
}
|
|
278
287
|
|
|
279
288
|
Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
|
|
@@ -288,7 +297,8 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
|
|
|
288
297
|
}
|
|
289
298
|
|
|
290
299
|
if (unlikely(info.Length() < 2)) {
|
|
291
|
-
return log_error_and_throw(env, "encrypt_string",
|
|
300
|
+
return log_error_and_throw(env, "encrypt_string",
|
|
301
|
+
"Wrong number of arguments");
|
|
292
302
|
}
|
|
293
303
|
|
|
294
304
|
if (unlikely(!info[0].IsString() || !info[1].IsString())) {
|
|
@@ -301,7 +311,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
|
|
|
301
311
|
partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
|
|
302
312
|
if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
|
|
303
313
|
return log_error_and_throw(env, "encrypt_string",
|
|
304
|
-
|
|
314
|
+
"Failed to get partition_id utf8 length");
|
|
305
315
|
}
|
|
306
316
|
if (unlikely(partition_utf8_byte_length == 0)) {
|
|
307
317
|
return log_error_and_throw(env, "encrypt_string", "partition_id is empty");
|
|
@@ -327,7 +337,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
|
|
|
327
337
|
}
|
|
328
338
|
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
|
|
329
339
|
return log_error_and_throw(env, "encrypt_string",
|
|
330
|
-
|
|
340
|
+
"Failed to allocate partitionId cobhan buffer");
|
|
331
341
|
}
|
|
332
342
|
|
|
333
343
|
// Copy
|
|
@@ -337,7 +347,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
|
|
|
337
347
|
&partition_copied_bytes);
|
|
338
348
|
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
|
|
339
349
|
return log_error_and_throw(env, "encrypt_string",
|
|
340
|
-
|
|
350
|
+
"Failed to copy partitionId to cobhan buffer");
|
|
341
351
|
}
|
|
342
352
|
|
|
343
353
|
// Determine size
|
|
@@ -346,7 +356,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
|
|
|
346
356
|
input_utf8_byte_length = nstring_utf8_byte_length(env, input);
|
|
347
357
|
if (unlikely(input_utf8_byte_length == (size_t)(-1))) {
|
|
348
358
|
return log_error_and_throw(env, "encrypt_string",
|
|
349
|
-
|
|
359
|
+
"Failed to get input utf8 length");
|
|
350
360
|
}
|
|
351
361
|
if (unlikely(input_utf8_byte_length == 0)) {
|
|
352
362
|
return log_error_and_throw(env, "encrypt_string", "input is empty");
|
|
@@ -371,7 +381,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
|
|
|
371
381
|
}
|
|
372
382
|
if (unlikely(input_cobhan_buffer == nullptr)) {
|
|
373
383
|
return log_error_and_throw(env, "encrypt_string",
|
|
374
|
-
|
|
384
|
+
"Failed to allocate input cobhan buffer");
|
|
375
385
|
}
|
|
376
386
|
|
|
377
387
|
// Copy
|
|
@@ -381,11 +391,18 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
|
|
|
381
391
|
input_cobhan_buffer, &input_copied_bytes);
|
|
382
392
|
if (unlikely(input_cobhan_buffer == nullptr)) {
|
|
383
393
|
return log_error_and_throw(env, "encrypt_string",
|
|
384
|
-
|
|
394
|
+
"Failed to copy input to cobhan buffer");
|
|
385
395
|
}
|
|
386
396
|
|
|
387
|
-
|
|
388
|
-
|
|
397
|
+
Napi::Value output =
|
|
398
|
+
encrypt_to_json(env, partition_copied_bytes, input_utf8_byte_length,
|
|
399
|
+
partition_id_cobhan_buffer, input_cobhan_buffer);
|
|
400
|
+
|
|
401
|
+
if (unlikely(verbose_flag)) {
|
|
402
|
+
debug_log("encrypt_string", "finished");
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return output;
|
|
389
406
|
}
|
|
390
407
|
|
|
391
408
|
Napi::Value decrypt(const Napi::CallbackInfo &info) {
|
|
@@ -413,7 +430,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
|
|
|
413
430
|
partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
|
|
414
431
|
if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
|
|
415
432
|
return log_error_and_throw(env, "decrypt",
|
|
416
|
-
|
|
433
|
+
"Failed to get partition_id utf8 length");
|
|
417
434
|
}
|
|
418
435
|
if (unlikely(partition_utf8_byte_length == 0)) {
|
|
419
436
|
return log_error_and_throw(env, "decrypt", "partition_id is empty");
|
|
@@ -439,7 +456,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
|
|
|
439
456
|
}
|
|
440
457
|
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
|
|
441
458
|
return log_error_and_throw(env, "decrypt",
|
|
442
|
-
|
|
459
|
+
"Failed to allocate partition_id cobhan buffer");
|
|
443
460
|
}
|
|
444
461
|
|
|
445
462
|
// Copy
|
|
@@ -448,7 +465,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
|
|
|
448
465
|
&partition_copied_bytes);
|
|
449
466
|
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
|
|
450
467
|
return log_error_and_throw(env, "decrypt",
|
|
451
|
-
|
|
468
|
+
"Failed to copy partition_id to cobhan buffer");
|
|
452
469
|
}
|
|
453
470
|
|
|
454
471
|
// Determine size
|
|
@@ -456,7 +473,8 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
|
|
|
456
473
|
Napi::String input = info[1].As<Napi::String>();
|
|
457
474
|
input_utf8_byte_length = nstring_utf8_byte_length(env, input);
|
|
458
475
|
if (unlikely(input_utf8_byte_length == (size_t)(-1))) {
|
|
459
|
-
return log_error_and_throw(env, "decrypt",
|
|
476
|
+
return log_error_and_throw(env, "decrypt",
|
|
477
|
+
"Failed to get input utf8 length");
|
|
460
478
|
}
|
|
461
479
|
if (unlikely(input_utf8_byte_length == 0)) {
|
|
462
480
|
return log_error_and_throw(env, "decrypt", "input is empty");
|
|
@@ -486,7 +504,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
|
|
|
486
504
|
}
|
|
487
505
|
if (unlikely(input_cobhan_buffer == nullptr)) {
|
|
488
506
|
return log_error_and_throw(env, "decrypt",
|
|
489
|
-
|
|
507
|
+
"Failed to allocate input cobhan buffer");
|
|
490
508
|
}
|
|
491
509
|
|
|
492
510
|
// Copy
|
|
@@ -496,7 +514,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
|
|
|
496
514
|
input_cobhan_buffer, &input_copied_bytes);
|
|
497
515
|
if (unlikely(input_cobhan_buffer == nullptr)) {
|
|
498
516
|
return log_error_and_throw(env, "decrypt",
|
|
499
|
-
|
|
517
|
+
"Failed to copy input to cobhan buffer");
|
|
500
518
|
}
|
|
501
519
|
|
|
502
520
|
char *output_cobhan_buffer;
|
|
@@ -517,7 +535,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
|
|
|
517
535
|
}
|
|
518
536
|
if (unlikely(output_cobhan_buffer == nullptr)) {
|
|
519
537
|
return log_error_and_throw(env, "decrypt",
|
|
520
|
-
|
|
538
|
+
"Failed to allocate cobhan output buffer");
|
|
521
539
|
}
|
|
522
540
|
|
|
523
541
|
if (unlikely(verbose_flag)) {
|
|
@@ -538,7 +556,13 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
|
|
|
538
556
|
return log_error_and_throw(env, "decrypt", std::to_string(result));
|
|
539
557
|
}
|
|
540
558
|
|
|
541
|
-
|
|
559
|
+
Napi::Value output = cbuffer_to_nbuffer(env, output_cobhan_buffer);
|
|
560
|
+
|
|
561
|
+
if (unlikely(verbose_flag)) {
|
|
562
|
+
debug_log("decrypt", "finished");
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
return output;
|
|
542
566
|
}
|
|
543
567
|
|
|
544
568
|
Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
|
|
@@ -553,7 +577,8 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
|
|
|
553
577
|
}
|
|
554
578
|
|
|
555
579
|
if (unlikely(info.Length() < 2)) {
|
|
556
|
-
return log_error_and_throw(env, "decrypt_string",
|
|
580
|
+
return log_error_and_throw(env, "decrypt_string",
|
|
581
|
+
"Wrong number of arguments");
|
|
557
582
|
}
|
|
558
583
|
|
|
559
584
|
if (unlikely(!info[0].IsString() || !info[1].IsString())) {
|
|
@@ -566,7 +591,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
|
|
|
566
591
|
partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
|
|
567
592
|
if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
|
|
568
593
|
return log_error_and_throw(env, "decrypt_string",
|
|
569
|
-
|
|
594
|
+
"Failed to get partition_id utf8 length");
|
|
570
595
|
}
|
|
571
596
|
if (unlikely(partition_utf8_byte_length == 0)) {
|
|
572
597
|
return log_error_and_throw(env, "decrypt_string", "partition_id is empty");
|
|
@@ -592,7 +617,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
|
|
|
592
617
|
}
|
|
593
618
|
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
|
|
594
619
|
return log_error_and_throw(env, "decrypt_string",
|
|
595
|
-
|
|
620
|
+
"Failed to allocate partitionId cobhan buffer");
|
|
596
621
|
}
|
|
597
622
|
|
|
598
623
|
// Copy
|
|
@@ -602,7 +627,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
|
|
|
602
627
|
&partition_copied_bytes);
|
|
603
628
|
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
|
|
604
629
|
return log_error_and_throw(env, "decrypt_string",
|
|
605
|
-
|
|
630
|
+
"Failed to copy partitionId to cobhan buffer");
|
|
606
631
|
}
|
|
607
632
|
|
|
608
633
|
// Determine size
|
|
@@ -611,7 +636,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
|
|
|
611
636
|
input_utf8_byte_length = nstring_utf8_byte_length(env, input);
|
|
612
637
|
if (unlikely(input_utf8_byte_length == (size_t)(-1))) {
|
|
613
638
|
return log_error_and_throw(env, "decrypt_string",
|
|
614
|
-
|
|
639
|
+
"Failed to get input utf8 length");
|
|
615
640
|
}
|
|
616
641
|
if (unlikely(input_utf8_byte_length == 0)) {
|
|
617
642
|
return log_error_and_throw(env, "decrypt_string", "input is empty");
|
|
@@ -636,7 +661,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
|
|
|
636
661
|
}
|
|
637
662
|
if (unlikely(input_cobhan_buffer == nullptr)) {
|
|
638
663
|
return log_error_and_throw(env, "decrypt_string",
|
|
639
|
-
|
|
664
|
+
"Failed to allocate input cobhan buffer");
|
|
640
665
|
}
|
|
641
666
|
|
|
642
667
|
// Copy
|
|
@@ -646,7 +671,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
|
|
|
646
671
|
input_cobhan_buffer, &input_copied_bytes);
|
|
647
672
|
if (unlikely(input_cobhan_buffer == nullptr)) {
|
|
648
673
|
return log_error_and_throw(env, "decrypt_string",
|
|
649
|
-
|
|
674
|
+
"Failed to copy input to cobhan buffer");
|
|
650
675
|
}
|
|
651
676
|
|
|
652
677
|
char *output_cobhan_buffer;
|
|
@@ -667,7 +692,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
|
|
|
667
692
|
}
|
|
668
693
|
if (unlikely(output_cobhan_buffer == nullptr)) {
|
|
669
694
|
return log_error_and_throw(env, "decrypt_string",
|
|
670
|
-
|
|
695
|
+
"Failed to allocate cobhan output buffer");
|
|
671
696
|
}
|
|
672
697
|
|
|
673
698
|
if (unlikely(verbose_flag)) {
|
|
@@ -689,6 +714,11 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
|
|
|
689
714
|
}
|
|
690
715
|
|
|
691
716
|
Napi::Value output = cbuffer_to_nstring(env, output_cobhan_buffer);
|
|
717
|
+
|
|
718
|
+
if (unlikely(verbose_flag)) {
|
|
719
|
+
debug_log("decrypt_string", "finished");
|
|
720
|
+
}
|
|
721
|
+
|
|
692
722
|
return output;
|
|
693
723
|
}
|
|
694
724
|
|
|
@@ -720,7 +750,7 @@ void set_max_stack_alloc_item_size(const Napi::CallbackInfo &info) {
|
|
|
720
750
|
|
|
721
751
|
if (unlikely(info.Length() < 1)) {
|
|
722
752
|
log_error_and_throw(env, "set_max_stack_alloc_item_size",
|
|
723
|
-
|
|
753
|
+
"Wrong number of arguments");
|
|
724
754
|
return;
|
|
725
755
|
}
|
|
726
756
|
|
|
@@ -738,7 +768,7 @@ void set_safety_padding_overhead(const Napi::CallbackInfo &info) {
|
|
|
738
768
|
|
|
739
769
|
if (unlikely(info.Length() < 1)) {
|
|
740
770
|
log_error_and_throw(env, "set_safety_padding_overhead",
|
|
741
|
-
|
|
771
|
+
"Wrong number of arguments");
|
|
742
772
|
return;
|
|
743
773
|
}
|
|
744
774
|
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
#define COBHAN_NAPI_INTEROP_H
|
|
3
3
|
#include <string>
|
|
4
4
|
#define NODE_ADDON_API_DISABLE_DEPRECATED
|
|
5
|
-
#include <napi.h>
|
|
6
|
-
#include "logging.h"
|
|
7
5
|
#include "hints.h"
|
|
6
|
+
#include "logging.h"
|
|
7
|
+
#include <napi.h>
|
|
8
8
|
|
|
9
9
|
extern size_t est_intermediate_key_overhead;
|
|
10
10
|
extern size_t safety_padding_bytes;
|
|
@@ -66,14 +66,14 @@ std::string napi_status_to_string(napi_status status) {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
__attribute__((always_inline)) inline
|
|
69
|
+
__attribute__((always_inline)) inline int32_t
|
|
70
70
|
cbuffer_byte_length(char *cobhan_buffer) {
|
|
71
|
-
return *((
|
|
71
|
+
return *((int32_t *)cobhan_buffer);
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
__attribute__((always_inline)) inline Napi::Value
|
|
75
75
|
log_error_and_throw(Napi::Env &env, const char *function_name,
|
|
76
|
-
|
|
76
|
+
std::string error_msg) {
|
|
77
77
|
error_log(function_name, error_msg);
|
|
78
78
|
Napi::Error::New(env, function_name + (": " + error_msg))
|
|
79
79
|
.ThrowAsJavaScriptException();
|
|
@@ -143,15 +143,21 @@ __attribute__((always_inline)) inline Napi::Value
|
|
|
143
143
|
cbuffer_to_nstring(Napi::Env &env, char *cobhan_buffer) {
|
|
144
144
|
napi_value output;
|
|
145
145
|
|
|
146
|
+
int32_t cobhan_buffer_size_bytes = cbuffer_byte_length(cobhan_buffer);
|
|
147
|
+
if (cobhan_buffer_size_bytes <= 0) {
|
|
148
|
+
return log_error_and_throw(env, "cbuffer_to_nstring",
|
|
149
|
+
"Invalid cobhan buffer byte length");
|
|
150
|
+
}
|
|
151
|
+
|
|
146
152
|
// Using C function because it allows length delimited input
|
|
147
153
|
napi_status status = napi_create_string_utf8(
|
|
148
154
|
env, ((const char *)cobhan_buffer) + cobhan_header_size_bytes,
|
|
149
|
-
|
|
155
|
+
cobhan_buffer_size_bytes, &output);
|
|
150
156
|
|
|
151
157
|
if (unlikely(status != napi_ok)) {
|
|
152
158
|
return log_error_and_throw(env, "cbuffer_to_nstring",
|
|
153
|
-
|
|
154
|
-
|
|
159
|
+
"napi_create_string_utf8 failed: " +
|
|
160
|
+
napi_status_to_string(status));
|
|
155
161
|
}
|
|
156
162
|
|
|
157
163
|
return Napi::String(env, output);
|
|
@@ -165,8 +171,8 @@ nstring_utf8_byte_length(Napi::Env &env, Napi::String &str) {
|
|
|
165
171
|
status = napi_get_value_string_utf8(env, str, nullptr, 0, &utf8_length);
|
|
166
172
|
if (unlikely(status != napi_ok)) {
|
|
167
173
|
log_error_and_throw(env, "nstring_utf8_length",
|
|
168
|
-
|
|
169
|
-
|
|
174
|
+
"napi_get_value_string_utf8 length check failed: " +
|
|
175
|
+
napi_status_to_string(status));
|
|
170
176
|
return (size_t)(-1);
|
|
171
177
|
}
|
|
172
178
|
|
|
@@ -179,9 +185,15 @@ copy_nstring_to_cbuffer(Napi::Env &env, Napi::String &str,
|
|
|
179
185
|
size_t *byte_length = nullptr) {
|
|
180
186
|
|
|
181
187
|
size_t cobhan_buffer_size_bytes = cbuffer_byte_length(cobhan_buffer);
|
|
188
|
+
if (unlikely(cobhan_buffer_size_bytes <= 0)) {
|
|
189
|
+
log_error_and_throw(env, "copy_nstring_to_cbuffer",
|
|
190
|
+
"Invalid cobhan buffer byte length");
|
|
191
|
+
return nullptr;
|
|
192
|
+
}
|
|
193
|
+
|
|
182
194
|
if (cobhan_buffer_size_bytes < str_utf8_byte_length) {
|
|
183
195
|
log_error_and_throw(env, "copy_nstring_to_cbuffer",
|
|
184
|
-
|
|
196
|
+
"String too large for cobhan buffer");
|
|
185
197
|
return nullptr;
|
|
186
198
|
}
|
|
187
199
|
|
|
@@ -190,21 +202,21 @@ copy_nstring_to_cbuffer(Napi::Env &env, Napi::String &str,
|
|
|
190
202
|
// NOTE: This implementation relies on the additional byte that is reserved
|
|
191
203
|
// upon allocation for a NULL delimiter as methods like
|
|
192
204
|
// napi_get_value_string_utf8 append a NULL delimiter
|
|
193
|
-
status = napi_get_value_string_utf8(
|
|
194
|
-
|
|
195
|
-
|
|
205
|
+
status = napi_get_value_string_utf8(env, str,
|
|
206
|
+
cobhan_buffer + cobhan_header_size_bytes,
|
|
207
|
+
str_utf8_byte_length + 1, &copied_bytes);
|
|
196
208
|
if (unlikely(status != napi_ok)) {
|
|
197
209
|
log_error_and_throw(env, "copy_nstring_to_cbuffer",
|
|
198
|
-
|
|
199
|
-
|
|
210
|
+
"Napi utf8 string conversion failure: " +
|
|
211
|
+
napi_status_to_string(status));
|
|
200
212
|
return nullptr;
|
|
201
213
|
}
|
|
202
214
|
|
|
203
215
|
if (unlikely(copied_bytes != str_utf8_byte_length)) {
|
|
204
216
|
log_error_and_throw(env, "copy_nstring_to_cbuffer",
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
217
|
+
"Did not copy expected number of bytes " +
|
|
218
|
+
std::to_string(str_utf8_byte_length) + " copied " +
|
|
219
|
+
std::to_string(copied_bytes));
|
|
208
220
|
return nullptr;
|
|
209
221
|
}
|
|
210
222
|
|
|
@@ -218,10 +230,19 @@ copy_nstring_to_cbuffer(Napi::Env &env, Napi::String &str,
|
|
|
218
230
|
__attribute__((always_inline)) inline char *
|
|
219
231
|
copy_nbuffer_to_cbuffer(Napi::Env &env, Napi::Buffer<unsigned char> &nbuffer,
|
|
220
232
|
char *cobhan_buffer) {
|
|
233
|
+
|
|
234
|
+
int32_t cobhan_buffer_size_bytes = cbuffer_byte_length(cobhan_buffer);
|
|
235
|
+
if (unlikely(cobhan_buffer_size_bytes <= 0)) {
|
|
236
|
+
log_error_and_throw(env, "copy_nbuffer_to_cbuffer",
|
|
237
|
+
"Invalid cobhan buffer byte length");
|
|
238
|
+
return nullptr;
|
|
239
|
+
}
|
|
240
|
+
|
|
221
241
|
size_t nbuffer_byte_length = nbuffer.ByteLength();
|
|
222
|
-
if (
|
|
242
|
+
if (nbuffer_byte_length > INT32_MAX ||
|
|
243
|
+
cobhan_buffer_size_bytes < (int32_t)nbuffer_byte_length) {
|
|
223
244
|
log_error_and_throw(env, "copy_nbuffer_to_cbuffer",
|
|
224
|
-
|
|
245
|
+
"Buffer too large for cobhan buffer");
|
|
225
246
|
return nullptr;
|
|
226
247
|
}
|
|
227
248
|
memcpy(cobhan_buffer + cobhan_header_size_bytes, nbuffer.Data(),
|
|
@@ -230,11 +251,35 @@ copy_nbuffer_to_cbuffer(Napi::Env &env, Napi::Buffer<unsigned char> &nbuffer,
|
|
|
230
251
|
return cobhan_buffer;
|
|
231
252
|
}
|
|
232
253
|
|
|
233
|
-
__attribute__((always_inline)) inline Napi::
|
|
254
|
+
__attribute__((always_inline)) inline Napi::Value
|
|
234
255
|
cbuffer_to_nbuffer(Napi::Env &env, char *cobhan_buffer) {
|
|
235
|
-
|
|
256
|
+
int32_t cobhan_buffer_byte_length = cbuffer_byte_length(cobhan_buffer);
|
|
257
|
+
if (unlikely(cobhan_buffer_byte_length <= 0)) {
|
|
258
|
+
return log_error_and_throw(env, "cbuffer_to_nbuffer",
|
|
259
|
+
"Invalid cobhan buffer byte length");
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (unlikely(verbose_flag)) {
|
|
263
|
+
debug_log("cbuffer_to_nbuffer",
|
|
264
|
+
"cbuffer_byte_length: " +
|
|
265
|
+
std::to_string(cobhan_buffer_byte_length));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (unlikely(cobhan_buffer_byte_length <= 0)) {
|
|
269
|
+
log_error_and_throw(env, "cbuffer_to_nbuffer",
|
|
270
|
+
"Invalid cobhan buffer byte length");
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
Napi::Buffer nbuffer = Napi::Buffer<unsigned char>::Copy(
|
|
236
274
|
env, ((unsigned char *)cobhan_buffer) + cobhan_header_size_bytes,
|
|
237
|
-
|
|
275
|
+
cobhan_buffer_byte_length);
|
|
276
|
+
|
|
277
|
+
if (unlikely(verbose_flag)) {
|
|
278
|
+
debug_log("cbuffer_to_nbuffer",
|
|
279
|
+
"nbuffer.ByteLength(): " + std::to_string(nbuffer.ByteLength()));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return nbuffer;
|
|
238
283
|
}
|
|
239
284
|
|
|
240
285
|
#endif
|