1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
/*
* Copyright (C) Alexander Borisov
* Copyright (C) NGINX, Inc.
*/
#include <njs_main.h>
typedef enum {
NJS_ENCODING_UTF8,
} njs_encoding_t;
typedef struct {
njs_encoding_t encoding;
njs_bool_t fatal;
njs_bool_t ignore_bom;
njs_unicode_decode_t ctx;
} njs_encoding_decode_t;
typedef struct {
njs_str_t name;
njs_encoding_t encoding;
} njs_encoding_label_t;
static njs_encoding_label_t njs_encoding_labels[] =
{
{ njs_str("utf-8"), NJS_ENCODING_UTF8 },
{ njs_str("utf8") , NJS_ENCODING_UTF8 },
{ njs_null_str, 0 }
};
static njs_int_t njs_text_encoder_encode_utf8(njs_vm_t *vm,
njs_string_prop_t *prop, njs_value_t *retval);
static njs_int_t njs_text_decoder_arg_encoding(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_encoding_decode_t *data);
static njs_int_t njs_text_decoder_arg_options(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_encoding_decode_t *data);
static njs_int_t
njs_text_encoder_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused, njs_value_t *retval)
{
njs_object_value_t *encoder;
if (!vm->top_frame->ctor) {
njs_type_error(vm, "Constructor of TextEncoder requires 'new'");
return NJS_ERROR;
}
encoder = njs_object_value_alloc(vm, NJS_OBJ_TYPE_TEXT_ENCODER, 0, NULL);
if (njs_slow_path(encoder == NULL)) {
return NJS_ERROR;
}
njs_set_data(&encoder->value, NULL, NJS_DATA_TAG_TEXT_ENCODER);
njs_set_object_value(retval, encoder);
return NJS_OK;
}
static njs_int_t
njs_text_encoder_encode(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused, njs_value_t *retval)
{
u_char *dst;
size_t size;
njs_int_t ret;
njs_value_t *this, *input, value;
const u_char *start, *end;
njs_string_prop_t prop;
njs_typed_array_t *array;
njs_unicode_decode_t ctx;
this = njs_argument(args, 0);
if (njs_slow_path(!njs_is_object_data(this, NJS_DATA_TAG_TEXT_ENCODER))) {
njs_type_error(vm, "\"this\" is not a TextEncoder");
return NJS_ERROR;
}
start = NULL;
end = NULL;
if (nargs > 1) {
input = njs_argument(args, 1);
if (njs_slow_path(!njs_is_string(input))) {
ret = njs_value_to_string(vm, input, input);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
}
(void) njs_string_prop(vm, &prop, input);
if (prop.length != 0) {
return njs_text_encoder_encode_utf8(vm, &prop, retval);
}
start = prop.start;
end = start + prop.size;
}
njs_utf8_decode_init(&ctx);
(void) njs_utf8_stream_length(&ctx, start, end - start, 1, 0, &size);
njs_set_number(&value, size);
array = njs_typed_array_alloc(vm, &value, 1, 0, NJS_OBJ_TYPE_UINT8_ARRAY);
if (njs_slow_path(array == NULL)) {
return NJS_ERROR;
}
dst = njs_typed_array_buffer(array)->u.u8;
njs_utf8_decode_init(&ctx);
(void) njs_utf8_stream_encode(&ctx, start, end, dst, 1, 0);
njs_set_typed_array(retval, array);
return NJS_OK;
}
static njs_int_t
njs_text_encoder_encode_utf8(njs_vm_t *vm, njs_string_prop_t *prop,
njs_value_t *retval)
{
njs_value_t value;
njs_typed_array_t *array;
njs_set_number(&value, prop->size);
array = njs_typed_array_alloc(vm, &value, 1, 0, NJS_OBJ_TYPE_UINT8_ARRAY);
if (njs_slow_path(array == NULL)) {
return NJS_ERROR;
}
memcpy(njs_typed_array_buffer(array)->u.u8, prop->start, prop->size);
njs_set_typed_array(retval, array);
return NJS_OK;
}
static njs_int_t
njs_text_encoder_encode_into(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused, njs_value_t *retval)
{
u_char *to, *to_end;
size_t size;
uint32_t cp;
njs_int_t ret;
njs_str_t str;
njs_value_t *this, *input, *dest, value, read, written;
njs_object_t *object;
const u_char *start, *end;
njs_typed_array_t *array;
njs_unicode_decode_t ctx;
this = njs_argument(args, 0);
input = njs_arg(args, nargs, 1);
dest = njs_arg(args, nargs, 2);
if (njs_slow_path(!njs_is_object_data(this, NJS_DATA_TAG_TEXT_ENCODER))) {
njs_type_error(vm, "\"this\" is not a TextEncoder");
return NJS_ERROR;
}
if (njs_slow_path(!njs_is_string(input))) {
ret = njs_value_to_string(vm, &value, input);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
input = &value;
}
if (njs_slow_path(!njs_is_typed_array_uint8(dest))) {
njs_type_error(vm, "The \"destination\" argument must be an instance "
"of Uint8Array");
return NJS_ERROR;
}
njs_string_get(vm, input, &str);
start = str.start;
end = start + str.length;
array = njs_typed_array(dest);
to = njs_typed_array_start(array);
to_end = to + array->byte_length;
njs_set_number(&read, 0);
njs_set_number(&written, 0);
njs_utf8_decode_init(&ctx);
while (start < end) {
cp = njs_utf8_decode(&ctx, &start, end);
if (cp > NJS_UNICODE_MAX_CODEPOINT) {
cp = NJS_UNICODE_REPLACEMENT;
}
size = njs_utf8_size(cp);
if (to + size > to_end) {
break;
}
njs_number(&read) += (cp > 0xFFFF) ? 2 : 1;
njs_number(&written) += size;
to = njs_utf8_encode(to, cp);
}
object = njs_object_alloc(vm);
if (njs_slow_path(object == NULL)) {
return NJS_ERROR;
}
njs_set_object(retval, object);
ret = njs_object_prop_define(vm, retval, NJS_ATOM_STRING_read, &read,
NJS_OBJECT_PROP_VALUE_CW);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
ret = njs_object_prop_define(vm, retval, NJS_ATOM_STRING_written, &written,
NJS_OBJECT_PROP_VALUE_CW);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
return NJS_OK;
}
static const njs_object_prop_init_t njs_text_encoder_properties[] =
{
NJS_DECLARE_PROP_HANDLER(STRING_constructor,
njs_object_prototype_create_constructor, 0,
NJS_OBJECT_PROP_VALUE_CW),
NJS_DECLARE_PROP_VALUE(STRING_encoding, njs_ascii_strval("utf-8"), 0),
NJS_DECLARE_PROP_NATIVE(STRING_encode, njs_text_encoder_encode, 0, 0),
NJS_DECLARE_PROP_NATIVE(STRING_encodeInto, njs_text_encoder_encode_into,
2, 0),
};
const njs_object_init_t njs_text_encoder_init = {
njs_text_encoder_properties,
njs_nitems(njs_text_encoder_properties),
};
static const njs_object_prop_init_t njs_text_encoder_constructor_properties[] =
{
NJS_DECLARE_PROP_LENGTH(0),
NJS_DECLARE_PROP_NAME("TextEncoder"),
NJS_DECLARE_PROP_HANDLER(STRING_prototype, njs_object_prototype_create,
0, 0),
};
const njs_object_init_t njs_text_encoder_constructor_init = {
njs_text_encoder_constructor_properties,
njs_nitems(njs_text_encoder_constructor_properties),
};
const njs_object_type_init_t njs_text_encoder_type_init = {
.constructor = njs_native_ctor(njs_text_encoder_constructor, 0, 0),
.prototype_props = &njs_text_encoder_init,
.constructor_props = &njs_text_encoder_constructor_init,
.prototype_value = { .object = { .type = NJS_OBJECT } },
};
static njs_int_t
njs_text_decoder_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused, njs_value_t *retval)
{
njs_int_t ret;
njs_object_value_t *decoder;
njs_encoding_decode_t *data;
if (!vm->top_frame->ctor) {
njs_type_error(vm, "Constructor of TextDecoder requires 'new'");
return NJS_ERROR;
}
decoder = njs_object_value_alloc(vm, NJS_OBJ_TYPE_TEXT_DECODER,
sizeof(njs_encoding_decode_t), NULL);
if (njs_slow_path(decoder == NULL)) {
return NJS_ERROR;
}
data = (njs_encoding_decode_t *) ((uint8_t *) decoder
+ sizeof(njs_object_value_t));
ret = njs_text_decoder_arg_encoding(vm, args, nargs, data);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
ret = njs_text_decoder_arg_options(vm, args, nargs, data);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
njs_utf8_decode_init(&data->ctx);
njs_set_data(&decoder->value, data, NJS_DATA_TAG_TEXT_DECODER);
njs_set_object_value(retval, decoder);
return NJS_OK;
}
static njs_int_t
njs_text_decoder_arg_encoding(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_encoding_decode_t *data)
{
njs_str_t str;
njs_int_t ret;
njs_value_t *value;
njs_encoding_label_t *label;
if (nargs < 2) {
data->encoding = NJS_ENCODING_UTF8;
return NJS_OK;
}
value = njs_argument(args, 1);
if (njs_slow_path(!njs_is_string(value))) {
ret = njs_value_to_string(vm, value, value);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
}
njs_string_get(vm, value, &str);
for (label = &njs_encoding_labels[0]; label->name.length != 0; label++) {
if (njs_strstr_eq(&str, &label->name)) {
data->encoding = label->encoding;
return NJS_OK;
}
}
njs_range_error(vm, "The \"%V\" encoding is not supported", &str);
return NJS_ERROR;
}
static njs_int_t
njs_text_decoder_arg_options(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_encoding_decode_t *data)
{
njs_int_t ret;
njs_value_t retval, *value;
if (nargs < 3) {
data->fatal = 0;
data->ignore_bom = 0;
return NJS_OK;
}
value = njs_argument(args, 2);
if (njs_slow_path(!njs_is_object(value))) {
njs_type_error(vm, "The \"options\" argument must be of type object");
return NJS_ERROR;
}
ret = njs_value_property(vm, value, NJS_ATOM_STRING_fatal, &retval);
if (njs_slow_path(ret == NJS_ERROR)) {
return ret;
}
data->fatal = njs_bool(&retval);
ret = njs_value_property(vm, value, NJS_ATOM_STRING_ignoreBOM, &retval);
if (njs_slow_path(ret == NJS_ERROR)) {
return ret;
}
data->ignore_bom = njs_bool(&retval);
return NJS_OK;
}
static njs_int_t
njs_text_decoder_encoding(njs_vm_t *vm, njs_object_prop_t *prop, uint32_t unused,
njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
{
njs_encoding_decode_t *data;
if (njs_slow_path(!njs_is_object_data(value, NJS_DATA_TAG_TEXT_DECODER))) {
njs_set_undefined(retval);
return NJS_DECLINED;
}
data = njs_object_data(value);
switch (data->encoding) {
case NJS_ENCODING_UTF8:
njs_atom_to_value(vm, retval, NJS_ATOM_STRING_utf_8);
break;
default:
njs_type_error(vm, "unknown encoding");
return NJS_ERROR;
}
return NJS_OK;
}
static njs_int_t
njs_text_decoder_fatal(njs_vm_t *vm, njs_object_prop_t *prop, uint32_t unused,
njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
{
njs_encoding_decode_t *data;
if (njs_slow_path(!njs_is_object_data(value, NJS_DATA_TAG_TEXT_DECODER))) {
njs_set_undefined(retval);
return NJS_DECLINED;
}
data = njs_object_data(value);
njs_set_boolean(retval, data->fatal);
return NJS_OK;
}
static njs_int_t
njs_text_decoder_ignore_bom(njs_vm_t *vm, njs_object_prop_t *prop,
uint32_t unused, njs_value_t *value, njs_value_t *setval,
njs_value_t *retval)
{
njs_encoding_decode_t *data;
if (njs_slow_path(!njs_is_object_data(value, NJS_DATA_TAG_TEXT_DECODER))) {
njs_set_undefined(retval);
return NJS_DECLINED;
}
data = njs_object_data(value);
njs_set_boolean(retval, data->ignore_bom);
return NJS_OK;
}
static njs_int_t
njs_text_decoder_decode(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused, njs_value_t *retval)
{
u_char *dst;
size_t size;
ssize_t length;
njs_int_t ret;
njs_bool_t stream;
njs_value_t *this, *value, *options;
const u_char *start, *end;
njs_unicode_decode_t ctx;
njs_encoding_decode_t *data;
const njs_typed_array_t *array;
const njs_array_buffer_t *buffer;
start = NULL;
end = NULL;
stream = 0;
this = njs_argument(args, 0);
if (njs_slow_path(!njs_is_object_data(this, NJS_DATA_TAG_TEXT_DECODER))) {
njs_type_error(vm, "\"this\" is not a TextDecoder");
return NJS_ERROR;
}
if (njs_fast_path(nargs > 1)) {
value = njs_argument(args, 1);
if (njs_is_typed_array(value)) {
array = njs_typed_array(value);
start = njs_typed_array_start(array);
end = start + array->byte_length;
} else if (njs_is_array_buffer(value)) {
buffer = njs_array_buffer(value);
start = buffer->u.u8;
end = start + buffer->size;
} else {
njs_type_error(vm, "The \"input\" argument must be an instance "
"of TypedArray");
return NJS_ERROR;
}
}
if (nargs > 2) {
options = njs_argument(args, 2);
if (njs_slow_path(!njs_is_object(options))) {
njs_type_error(vm, "The \"options\" argument must be "
"of type object");
return NJS_ERROR;
}
ret = njs_value_property(vm, options, NJS_ATOM_STRING_stream, retval);
if (njs_slow_path(ret == NJS_ERROR)) {
return ret;
}
stream = njs_bool(retval);
}
data = njs_object_data(this);
ctx = data->ctx;
/* Looking for BOM. */
if (start != NULL && !data->ignore_bom) {
start += njs_utf8_bom(start, end);
}
length = njs_utf8_stream_length(&ctx, start, end - start, !stream,
data->fatal, &size);
if (length == -1) {
njs_type_error(vm, "The encoded data was not valid");
return NJS_ERROR;
}
dst = njs_string_alloc(vm, retval, size, length);
if (njs_slow_path(dst == NULL)) {
return NJS_ERROR;
}
(void) njs_utf8_stream_encode(&data->ctx, start, end, dst, !stream, 0);
if (!stream) {
njs_utf8_decode_init(&data->ctx);
}
return NJS_OK;
}
static const njs_object_prop_init_t njs_text_decoder_properties[] =
{
NJS_DECLARE_PROP_HANDLER(STRING_constructor,
njs_object_prototype_create_constructor, 0,
NJS_OBJECT_PROP_VALUE_CW),
NJS_DECLARE_PROP_HANDLER(STRING_encoding, njs_text_decoder_encoding, 0,
0),
NJS_DECLARE_PROP_HANDLER(STRING_fatal, njs_text_decoder_fatal, 0, 0),
NJS_DECLARE_PROP_HANDLER(STRING_ignoreBOM, njs_text_decoder_ignore_bom,
0, 0),
NJS_DECLARE_PROP_NATIVE(STRING_decode, njs_text_decoder_decode, 0, 0),
};
const njs_object_init_t njs_text_decoder_init = {
njs_text_decoder_properties,
njs_nitems(njs_text_decoder_properties),
};
static const njs_object_prop_init_t njs_text_decoder_constructor_properties[] =
{
NJS_DECLARE_PROP_LENGTH(0),
NJS_DECLARE_PROP_NAME("TextDecoder"),
NJS_DECLARE_PROP_HANDLER(STRING_prototype, njs_object_prototype_create,
0, 0),
};
const njs_object_init_t njs_text_decoder_constructor_init = {
njs_text_decoder_constructor_properties,
njs_nitems(njs_text_decoder_constructor_properties),
};
const njs_object_type_init_t njs_text_decoder_type_init = {
.constructor = njs_native_ctor(njs_text_decoder_constructor, 0, 0),
.prototype_props = &njs_text_decoder_init,
.constructor_props = &njs_text_decoder_constructor_init,
.prototype_value = { .object = { .type = NJS_OBJECT } },
};
|