forked from erlang/otp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
erl_unicode.c
2805 lines (2590 loc) · 73.5 KB
/
erl_unicode.c
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2008-2016. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_process.h"
#include "error.h"
#include "bif.h"
#include "erl_binary.h"
#include "big.h"
#include "erl_unicode.h"
#include "erl_unicode_normalize.h"
typedef struct _restart_context {
byte *bytes;
Uint num_processed_bytes;
Uint num_bytes_to_process;
Uint num_resulting_chars;
int state;
} RestartContext;
#define LOOP_FACTOR 10
#define LOOP_FACTOR_SIMPLE 50 /* When just counting */
static Uint max_loop_limit;
static BIF_RETTYPE utf8_to_list(Process *p, Eterm arg1);
static BIF_RETTYPE finalize_list_to_list(Process *p,
byte *bytes,
Eterm rest,
Uint num_processed_bytes,
Uint num_bytes_to_process,
Uint num_resulting_chars,
int state, Sint left,
Eterm tail);
static BIF_RETTYPE characters_to_utf8_trap(BIF_ALIST_3);
static BIF_RETTYPE characters_to_list_trap_1(BIF_ALIST_3);
static BIF_RETTYPE characters_to_list_trap_2(BIF_ALIST_3);
static BIF_RETTYPE characters_to_list_trap_3(BIF_ALIST_3);
static BIF_RETTYPE characters_to_list_trap_4(BIF_ALIST_1);
static Export characters_to_utf8_trap_exp;
static Export characters_to_list_trap_1_exp;
static Export characters_to_list_trap_2_exp;
static Export characters_to_list_trap_3_exp;
static Export characters_to_list_trap_4_exp;
static Export *c_to_b_int_trap_exportp = NULL;
static Export *c_to_l_int_trap_exportp = NULL;
void erts_init_unicode(void)
{
max_loop_limit = CONTEXT_REDS * LOOP_FACTOR;
/* Non visual BIFs to trap to. */
erts_init_trap_export(&characters_to_utf8_trap_exp,
am_erlang, am_atom_put("characters_to_utf8_trap",23), 3,
&characters_to_utf8_trap);
erts_init_trap_export(&characters_to_list_trap_1_exp,
am_erlang, am_atom_put("characters_to_list_trap_1",25), 3,
&characters_to_list_trap_1);
erts_init_trap_export(&characters_to_list_trap_2_exp,
am_erlang, am_atom_put("characters_to_list_trap_2",25), 3,
&characters_to_list_trap_2);
erts_init_trap_export(&characters_to_list_trap_3_exp,
am_erlang, am_atom_put("characters_to_list_trap_3",25), 3,
&characters_to_list_trap_3);
erts_init_trap_export(&characters_to_list_trap_4_exp,
am_erlang, am_atom_put("characters_to_list_trap_4",25), 1,
&characters_to_list_trap_4);
c_to_b_int_trap_exportp = erts_export_put(am_unicode,am_characters_to_binary_int,2);
c_to_l_int_trap_exportp = erts_export_put(am_unicode,am_characters_to_list_int,2);
}
static ERTS_INLINE void *alloc_restart(size_t size)
{
return erts_alloc(ERTS_ALC_T_UNICODE_BUFFER,size);
}
static ERTS_INLINE void free_restart(void *ptr)
{
erts_free(ERTS_ALC_T_UNICODE_BUFFER, ptr);
}
static void cleanup_restart_context(RestartContext *rc)
{
if (rc->bytes != NULL) {
free_restart(rc->bytes);
rc->bytes = NULL;
}
}
static void cleanup_restart_context_bin(Binary *bp)
{
RestartContext *rc = ERTS_MAGIC_BIN_DATA(bp);
cleanup_restart_context(rc);
}
static RestartContext *get_rc_from_bin(Eterm bin)
{
Binary *mbp;
ASSERT(ERTS_TERM_IS_MAGIC_BINARY(bin));
mbp = ((ProcBin *) binary_val(bin))->val;
ASSERT(ERTS_MAGIC_BIN_DESTRUCTOR(mbp)
== cleanup_restart_context_bin);
return (RestartContext *) ERTS_MAGIC_BIN_DATA(mbp);
}
static Eterm make_magic_bin_for_restart(Process *p, RestartContext *rc)
{
Binary *mbp = erts_create_magic_binary(sizeof(RestartContext),
cleanup_restart_context_bin);
RestartContext *restartp = ERTS_MAGIC_BIN_DATA(mbp);
Eterm *hp;
memcpy(restartp,rc,sizeof(RestartContext));
hp = HAlloc(p, PROC_BIN_SIZE);
return erts_mk_magic_binary_term(&hp, &MSO(p), mbp);
}
Sint erts_unicode_set_loop_limit(Sint limit)
{
Sint save = (Sint) max_loop_limit;
if (limit <= 0) {
max_loop_limit = CONTEXT_REDS * LOOP_FACTOR;
} else {
max_loop_limit = (Uint) limit;
}
return save;
}
static ERTS_INLINE int allowed_iterations(Process *p)
{
int tmp = ERTS_BIF_REDS_LEFT(p) * LOOP_FACTOR;
int tmp2 = max_loop_limit;
if (tmp2 < tmp)
return tmp2;
else
return tmp;
}
static ERTS_INLINE void cost_to_proc(Process *p, Sint cost)
{
Sint x = (cost / LOOP_FACTOR);
BUMP_REDS(p,x);
}
static ERTS_INLINE int simple_loops_to_common(int cost)
{
int factor = (LOOP_FACTOR_SIMPLE / LOOP_FACTOR);
return (cost / factor);
}
static Sint aligned_binary_size(Eterm binary)
{
ERTS_DECLARE_DUMMY(unsigned char *bytes);
ERTS_DECLARE_DUMMY(Uint bitoffs);
Uint bitsize;
ERTS_GET_BINARY_BYTES(binary, bytes, bitoffs, bitsize);
if (bitsize != 0) {
return (Sint) -1;
}
return binary_size(binary);
}
static Sint latin1_binary_need(Eterm binary)
{
unsigned char *bytes;
byte *temp_alloc = NULL;
Uint bitoffs;
Uint bitsize;
Uint size;
Sint need = 0;
Sint i;
ERTS_GET_BINARY_BYTES(binary, bytes, bitoffs, bitsize);
if (bitsize != 0) {
return (Sint) -1;
}
if (bitoffs != 0) {
bytes = erts_get_aligned_binary_bytes(binary, &temp_alloc);
/* The call to erts_get_aligned_binary_bytes cannot fail as
we'we already checked bitsize and that this is a binary */
}
size = binary_size(binary);
for(i = 0; i < size; ++i) {
if (bytes[i] & ((byte) 0x80)) {
need += 2;
} else {
need += 1;
}
}
erts_free_aligned_binary_bytes(temp_alloc);
return need;
}
static int utf8_len(byte first)
{
if ((first & ((byte) 0x80)) == 0) {
return 1;
} else if ((first & ((byte) 0xE0)) == 0xC0) {
return 2;
} else if ((first & ((byte) 0xF0)) == 0xE0) {
return 3;
} else if ((first & ((byte) 0xF8)) == 0xF0) {
return 4;
}
return -1;
}
static Uint copy_utf8_bin(byte *target, byte *source, Uint size,
byte *leftover, int *num_leftovers,
byte **err_pos, Uint *characters)
{
Uint copied = 0;
if (leftover != NULL && *num_leftovers) {
int need = utf8_len(leftover[0]);
int from_source = need - (*num_leftovers);
Uint c;
byte *tmp_err_pos = NULL;
ASSERT(need > 0);
ASSERT(from_source > 0);
if (size < from_source) {
memcpy(leftover + (*num_leftovers), source, size);
*num_leftovers += size;
return 0;
}
/* leftover has room for four bytes (see bif) */
memcpy(leftover + (*num_leftovers),source,from_source);
c = copy_utf8_bin(target, leftover, need, NULL, NULL, &tmp_err_pos, characters);
if (tmp_err_pos != 0) {
*err_pos = source;
return 0;
}
copied += c;
*num_leftovers = 0;
size -= from_source;
target += c;
source += from_source;
}
while (size) {
if (((*source) & ((byte) 0x80)) == 0) {
*(target++) = *(source++);
--size; ++copied;
} else if (((*source) & ((byte) 0xE0)) == 0xC0) {
if (leftover && size < 2) {
*leftover = *source;
*num_leftovers = 1;
break;
}
if (size < 2 || ((source[1] & ((byte) 0xC0)) != 0x80) ||
((*source) < 0xC2) /* overlong */) {
*err_pos = source;
return copied;
}
*(target++) = *(source++);
*(target++) = *(source++);
size -= 2; copied += 2;
} else if (((*source) & ((byte) 0xF0)) == 0xE0) {
if (leftover && size < 3) {
memcpy(leftover, source, (int) size);
*num_leftovers = (int) size;
break;
}
if (size < 3 || ((source[1] & ((byte) 0xC0)) != 0x80) ||
((source[2] & ((byte) 0xC0)) != 0x80) ||
(((*source) == 0xE0) && (source[1] < 0xA0)) /* overlong */ ) {
*err_pos = source;
return copied;
}
if ((((*source) & ((byte) 0xF)) == 0xD) &&
((source[1] & 0x20) != 0)) {
*err_pos = source;
return copied;
}
*(target++) = *(source++);
*(target++) = *(source++);
*(target++) = *(source++);
size -= 3; copied += 3;
} else if (((*source) & ((byte) 0xF8)) == 0xF0) {
if (leftover && size < 4) {
memcpy(leftover, source, (int) size);
*num_leftovers = (int) size;
break;
}
if (size < 4 || ((source[1] & ((byte) 0xC0)) != 0x80) ||
((source[2] & ((byte) 0xC0)) != 0x80) ||
((source[3] & ((byte) 0xC0)) != 0x80) ||
(((*source) == 0xF0) && (source[1] < 0x90)) /* overlong */) {
*err_pos = source;
return copied;
}
if ((((*source) & ((byte)0x7)) > 0x4U) ||
((((*source) & ((byte)0x7)) == 0x4U) &&
((source[1] & ((byte)0x3F)) > 0xFU))) {
*err_pos = source;
return copied;
}
*(target++) = *(source++);
*(target++) = *(source++);
*(target++) = *(source++);
*(target++) = *(source++);
size -= 4; copied +=4;
} else {
*err_pos = source;
return copied;
}
++(*characters);
}
return copied;
}
static Sint utf8_need(Eterm ioterm, int latin1, Uint *costp)
{
Eterm *objp;
Eterm obj;
DECLARE_ESTACK(stack);
Sint need = 0;
Uint cost = 0;
if (is_nil(ioterm)) {
DESTROY_ESTACK(stack);
*costp = 0;
return need;
}
if(is_binary(ioterm)) {
DESTROY_ESTACK(stack);
if (latin1) {
Sint x = latin1_binary_need(ioterm);
*costp = x;
return x;
} else {
*costp = 1;
return aligned_binary_size(ioterm);
}
}
if (!is_list(ioterm)) {
DESTROY_ESTACK(stack);
*costp = 0;
return (Sint) -1;
}
/* OK a list, needs to be processed in order, handling each flat list-level
as they occur, just like io_list_to_binary would */
ESTACK_PUSH(stack,ioterm);
while (!ESTACK_ISEMPTY(stack)) {
ioterm = ESTACK_POP(stack);
if (is_nil(ioterm)) {
/* ignore empty lists */
continue;
}
if(is_list(ioterm)) {
L_Again: /* Restart with sublist, old listend was pushed on stack */
objp = list_val(ioterm);
obj = CAR(objp);
for(;;) { /* loop over one flat list of bytes and binaries
until sublist or list end is encountered */
if (is_small(obj)) { /* Always small */
for(;;) {
Uint x = unsigned_val(obj);
if (x < 0x80)
need +=1;
else if (x < 0x800)
need += 2;
else if (x < 0x10000)
need += 3;
else
need += 4;
/* everything else will give badarg later
in the process, so we dont check */
++cost;
ioterm = CDR(objp);
if (!is_list(ioterm)) {
break;
}
objp = list_val(ioterm);
obj = CAR(objp);
if (!is_small(obj))
break;
}
} else if (is_nil(obj)) {
ioterm = CDR(objp);
if (!is_list(ioterm)) {
break;
}
objp = list_val(ioterm);
obj = CAR(objp);
} else if (is_list(obj)) {
/* push rest of list for later processing, start
again with sublist */
ESTACK_PUSH(stack,CDR(objp));
ioterm = obj;
goto L_Again;
} else if (is_binary(obj)) {
Sint x;
if (latin1) {
x = latin1_binary_need(obj);
if (x < 0) {
DESTROY_ESTACK(stack);
*costp = cost;
return x;
}
cost += x;
} else {
x = aligned_binary_size(obj);
if (x < 0) {
DESTROY_ESTACK(stack);
*costp = cost;
return x;
}
++cost;
}
need += x;
ioterm = CDR(objp);
if (is_list(ioterm)) {
/* objp and obj need to be updated if
loop is to continue */
objp = list_val(ioterm);
obj = CAR(objp);
}
} else {
DESTROY_ESTACK(stack);
*costp = cost;
return ((Sint) -1);
}
if (is_nil(ioterm) || !is_list(ioterm)) {
break;
}
} /* for(;;) */
} /* is_list(ioterm) */
if (!is_list(ioterm) && !is_nil(ioterm)) {
/* inproper list end */
if (is_binary(ioterm)) {
Sint x;
if (latin1) {
x = latin1_binary_need(ioterm);
if (x < 0) {
DESTROY_ESTACK(stack);
*costp = cost;
return x;
}
cost += x;
} else {
x = aligned_binary_size(ioterm);
if (x < 0) {
DESTROY_ESTACK(stack);
*costp = cost;
return x;
}
++cost;
}
need += x;
} else {
DESTROY_ESTACK(stack);
*costp = cost;
return ((Sint) -1);
}
}
} /* while not estack empty */
DESTROY_ESTACK(stack);
*costp = cost;
return need;
}
static Eterm do_build_utf8(Process *p, Eterm ioterm, Sint *left, int latin1,
byte *target, Uint *pos, Uint *characters, int *err,
byte *leftover, int *num_leftovers)
{
int c;
Eterm *objp;
Eterm obj;
DECLARE_ESTACK(stack);
*err = 0;
if ((*left) <= 0 || is_nil(ioterm)) {
DESTROY_ESTACK(stack);
return ioterm;
}
if(is_binary(ioterm)) {
Uint bitoffs;
Uint bitsize;
Uint size;
Uint i;
Eterm res_term = NIL;
unsigned char *bytes;
byte *temp_alloc = NULL;
Uint orig_size;
ERTS_GET_BINARY_BYTES(ioterm, bytes, bitoffs, bitsize);
if (bitsize != 0) {
*err = 1;
DESTROY_ESTACK(stack);
return ioterm;
}
if (bitoffs != 0) {
bytes = erts_get_aligned_binary_bytes(ioterm, &temp_alloc);
/* The call to erts_get_aligned_binary_bytes cannot fail as
we'we already checked bitsize and that this is a binary */
}
orig_size = size = binary_size(ioterm);
/* This is done to avoid splitting binaries in two
and then create an unnecessary rest that eventually gives an error.
For cases where errors are not returned this is unnecessary */
if (!latin1) {
/* Find a valid character boundary */
while (size > (*left) &&
(((byte) bytes[(*left)]) & ((byte) 0xC0)) == ((byte) 0x80)) {
++(*left);
}
}
if (size > (*left)) {
Eterm *hp;
ErlSubBin *sb;
Eterm orig;
Uint offset;
/* Split the binary in two parts, of which we
only process the first */
hp = HAlloc(p, ERL_SUB_BIN_SIZE);
sb = (ErlSubBin *) hp;
ERTS_GET_REAL_BIN(ioterm, orig, offset, bitoffs, bitsize);
sb->thing_word = HEADER_SUB_BIN;
sb->size = size - (*left);
sb->offs = offset + (*left);
sb->orig = orig;
sb->bitoffs = bitoffs;
sb->bitsize = bitsize;
sb->is_writable = 0;
res_term = make_binary(sb);
size = (*left);
}
if (!latin1) {
Uint num;
byte *err_pos = NULL;
num = copy_utf8_bin(target + (*pos), bytes,
size, leftover, num_leftovers,&err_pos,characters);
*pos += num;
if (err_pos != NULL) {
int rest_bin_offset;
int rest_bin_size;
Eterm *hp;
ErlSubBin *sb;
Eterm orig;
Uint offset;
*err = 1;
/* we have no real stack, just build a list of the binaries
we have not decoded... */
DESTROY_ESTACK(stack);
rest_bin_offset = (err_pos - bytes);
rest_bin_size = orig_size - rest_bin_offset;
hp = HAlloc(p, ERL_SUB_BIN_SIZE);
sb = (ErlSubBin *) hp;
ERTS_GET_REAL_BIN(ioterm, orig, offset, bitoffs, bitsize);
sb->thing_word = HEADER_SUB_BIN;
sb->size = rest_bin_size;
sb->offs = offset + rest_bin_offset;
sb->orig = orig;
sb->bitoffs = bitoffs;
sb->bitsize = bitsize;
sb->is_writable = 0;
res_term = make_binary(sb);
erts_free_aligned_binary_bytes(temp_alloc);
return res_term;
}
} else {
i = 0;
while(i < size) {
if (bytes[i] < 0x80) {
target[(*pos)++] = bytes[i++];
} else {
target[(*pos)++] = ((bytes[i] >> 6) | ((byte) 0xC0));
target[(*pos)++] = ((bytes[i] & 0x3F) | ((byte) 0x80));
++i;
}
++(*characters);
}
}
*left -= size;
DESTROY_ESTACK(stack);
erts_free_aligned_binary_bytes(temp_alloc);
return res_term;
}
if (!is_list(ioterm)) {
*err = 1;
goto done;
}
/* OK a list, needs to be processed in order, handling each flat list-level
as they occur, just like io_list_to_binary would */
ESTACK_PUSH(stack,ioterm);
while (!ESTACK_ISEMPTY(stack) && (*left)) {
ioterm = ESTACK_POP(stack);
if (is_nil(ioterm)) {
/* ignore empty lists */
continue;
}
if(is_list(ioterm)) {
L_Again: /* Restart with sublist, old listend was pushed on stack */
objp = list_val(ioterm);
obj = CAR(objp);
for(;;) { /* loop over one flat list of bytes and binaries
until sublist or list end is encountered */
if (is_small(obj)) { /* Always small in unicode*/
if (*num_leftovers) {
/* Have rest from previous bin and this is an integer, not allowed */
*err = 1;
goto done;
}
for(;;) {
Uint x = unsigned_val(obj);
if (latin1 && x > 255) {
*err = 1;
goto done;
}
if (x < 0x80) {
target[(*pos)++] = (byte) x;
}
else if (x < 0x800) {
target[(*pos)++] = (((byte) (x >> 6)) |
((byte) 0xC0));
target[(*pos)++] = (((byte) (x & 0x3F)) |
((byte) 0x80));
} else if (x < 0x10000) {
if (x >= 0xD800 && x <= 0xDFFF) {
/* Invalid unicode range */
*err = 1;
goto done;
}
target[(*pos)++] = (((byte) (x >> 12)) |
((byte) 0xE0));
target[(*pos)++] = ((((byte) (x >> 6)) & 0x3F) |
((byte) 0x80));
target[(*pos)++] = (((byte) (x & 0x3F)) |
((byte) 0x80));
} else if (x < 0x110000) { /* Standard imposed max */
target[(*pos)++] = (((byte) (x >> 18)) |
((byte) 0xF0));
target[(*pos)++] = ((((byte) (x >> 12)) & 0x3F) |
((byte) 0x80));
target[(*pos)++] = ((((byte) (x >> 6)) & 0x3F) |
((byte) 0x80));
target[(*pos)++] = (((byte) (x & 0x3F)) |
((byte) 0x80));
} else {
*err = 1;
goto done;
}
++(*characters);
--(*left);
ioterm = CDR(objp);
if (!is_list(ioterm) || !(*left)) {
break;
}
objp = list_val(ioterm);
obj = CAR(objp);
if (!is_small(obj))
break;
}
} else if (is_nil(obj)) {
ioterm = CDR(objp);
if (!is_list(ioterm)) {
break;
}
objp = list_val(ioterm);
obj = CAR(objp);
} else if (is_list(obj)) {
/* push rest of list for later processing, start
again with sublist */
ESTACK_PUSH(stack,CDR(objp));
ioterm = obj;
goto L_Again;
} else if (is_binary(obj)) {
Eterm rest_term;
rest_term = do_build_utf8(p,obj,left,latin1,target,pos, characters, err,
leftover, num_leftovers);
if ((*err) != 0) {
Eterm *hp;
hp = HAlloc(p, 2);
obj = CDR(objp);
ioterm = CONS(hp, rest_term, obj);
/* (*left) = 0; */
goto done;
}
if (rest_term != NIL) {
Eterm *hp;
hp = HAlloc(p, 2);
obj = CDR(objp);
ioterm = CONS(hp, rest_term, obj);
(*left) = 0;
break;
}
ioterm = CDR(objp);
if (is_list(ioterm)) {
/* objp and obj need to be updated if
loop is to continue */
objp = list_val(ioterm);
obj = CAR(objp);
}
} else {
*err = 1;
goto done;
}
if (!(*left) || is_nil(ioterm) || !is_list(ioterm)) {
break;
}
} /* for(;;) */
} /* is_list(ioterm) */
if ((*left) && !is_list(ioterm) && !is_nil(ioterm)) {
/* inproper list end */
if (is_binary(ioterm)) {
ioterm = do_build_utf8(p,ioterm,left,latin1,target,pos,characters,err,leftover,num_leftovers);
if ((*err) != 0) {
goto done;
}
} else {
*err = 1;
goto done;
}
}
} /* while left and not estack empty */
done:
c = ESTACK_COUNT(stack);
if (c > 0) {
Eterm *hp = HAlloc(p,2*c);
while(!ESTACK_ISEMPTY(stack)) {
Eterm st = ESTACK_POP(stack);
ioterm = CONS(hp, ioterm, st);
hp += 2;
}
}
DESTROY_ESTACK(stack);
return ioterm;
}
static int check_leftovers(byte *source, int size)
{
if (((*source) & ((byte) 0xE0)) == 0xC0) {
return 0;
} else if (((*source) & ((byte) 0xF0)) == 0xE0) {
if (size < 2 ||
(size < 3 && ((source[1] & ((byte) 0xC0)) == 0x80))) {
return 0;
}
} else if (((*source) & ((byte) 0xF8)) == 0xF0) {
if (size < 2 ||
(size < 3 && ((source[1] & ((byte) 0xC0)) == 0x80)) ||
(size < 4 &&
((source[1] & ((byte) 0xC0)) == 0x80) &&
((source[2] & ((byte) 0xC0)) == 0x80))) {
return 0;
}
}
return -1;
}
static BIF_RETTYPE build_utf8_return(Process *p,Eterm bin,Uint pos,
Eterm rest_term,int err,
byte *leftover,int num_leftovers,Eterm latin1)
{
Eterm *hp;
Eterm ret;
binary_size(bin) = pos;
if (err) {
if (num_leftovers > 0) {
Eterm leftover_bin = new_binary(p, leftover, num_leftovers);
hp = HAlloc(p,8);
rest_term = CONS(hp,rest_term,NIL);
hp += 2;
rest_term = CONS(hp,leftover_bin,rest_term);
hp += 2;
} else {
hp = HAlloc(p,4);
}
ret = TUPLE3(hp,am_error,bin,rest_term);
} else if (rest_term == NIL && num_leftovers != 0) {
Eterm leftover_bin = new_binary(p, leftover, num_leftovers);
if (check_leftovers(leftover,num_leftovers) != 0) {
hp = HAlloc(p,4);
ret = TUPLE3(hp,am_error,bin,leftover_bin);
} else {
hp = HAlloc(p,4);
ret = TUPLE3(hp,am_incomplete,bin,leftover_bin);
}
} else { /* All OK */
if (rest_term != NIL) { /* Trap */
if (num_leftovers > 0) {
Eterm rest_bin = new_binary(p, leftover, num_leftovers);
hp = HAlloc(p,2);
rest_term = CONS(hp,rest_bin,rest_term);
}
BUMP_ALL_REDS(p);
BIF_TRAP3(&characters_to_utf8_trap_exp, p, bin, rest_term, latin1);
} else { /* Success */
/*hp = HAlloc(p,5);
ret = TUPLE4(hp,bin,rest_term,make_small(pos),make_small(err));*/
ret = bin;
}
}
BIF_RET(ret);
}
static BIF_RETTYPE characters_to_utf8_trap(BIF_ALIST_3)
{
#ifdef DEBUG
Eterm *real_bin;
#endif
byte* bytes;
Eterm rest_term;
Sint left, sleft;
Uint pos;
int err;
byte leftover[4]; /* used for temp buffer too,
otherwise 3 bytes would have been enough */
int num_leftovers = 0;
int latin1 = 0;
Uint characters = 0;
/*erts_printf("Trap %T!\r\n",BIF_ARG_2);*/
ASSERT(is_binary(BIF_ARG_1));
#ifdef DEBUG
real_bin = binary_val(BIF_ARG_1);
ASSERT(*real_bin == HEADER_PROC_BIN);
#endif
pos = binary_size(BIF_ARG_1);
bytes = binary_bytes(BIF_ARG_1);
sleft = left = allowed_iterations(BIF_P);
err = 0;
if (BIF_ARG_3 == am_latin1) {
latin1 = 1;
}
rest_term = do_build_utf8(BIF_P, BIF_ARG_2, &left, latin1,
bytes, &pos, &characters, &err, leftover, &num_leftovers);
cost_to_proc(BIF_P, sleft - left);
return build_utf8_return(BIF_P,BIF_ARG_1,pos,rest_term,err,
leftover,num_leftovers,BIF_ARG_3);
}
BIF_RETTYPE unicode_bin_is_7bit_1(BIF_ALIST_1)
{
Sint need;
if(!is_binary(BIF_ARG_1)) {
BIF_RET(am_false);
}
need = latin1_binary_need(BIF_ARG_1);
if(need >= 0 && aligned_binary_size(BIF_ARG_1) == need) {
BIF_RET(am_true);
}
BIF_RET(am_false);
}
static int is_valid_utf8(Eterm orig_bin)
{
Uint bitoffs;
Uint bitsize;
Uint size;
byte *temp_alloc = NULL;
byte *endpos;
Uint numchar;
byte *bytes;
int ret;
ERTS_GET_BINARY_BYTES(orig_bin, bytes, bitoffs, bitsize);
if (bitsize != 0) {
return 0;
}
if (bitoffs != 0) {
bytes = erts_get_aligned_binary_bytes(orig_bin, &temp_alloc);
}
size = binary_size(orig_bin);
ret = erts_analyze_utf8(bytes,
size,
&endpos,&numchar,NULL);
erts_free_aligned_binary_bytes(temp_alloc);
return (ret == ERTS_UTF8_OK);
}
BIF_RETTYPE unicode_characters_to_binary_2(BIF_ALIST_2)
{
Sint need;
Uint characters;
int latin1;
Eterm bin;
byte *bytes;
Uint pos;
int err;
Sint left, sleft;
Eterm rest_term, subject;
byte leftover[4]; /* used for temp buffer too, o
therwise 3 bytes would have been enough */
int num_leftovers = 0;
Uint cost_of_utf8_need;
if (BIF_ARG_2 == am_latin1) {
latin1 = 1;
} else if (BIF_ARG_2 == am_unicode || BIF_ARG_2 == am_utf8) {
latin1 = 0;
} else {
BIF_TRAP2(c_to_b_int_trap_exportp, BIF_P, BIF_ARG_1, BIF_ARG_2);
}
if (is_list(BIF_ARG_1) && is_binary(CAR(list_val(BIF_ARG_1))) &&
is_nil(CDR(list_val(BIF_ARG_1)))) {
subject = CAR(list_val(BIF_ARG_1));
} else {
subject = BIF_ARG_1;
}
need = utf8_need(subject,latin1,&cost_of_utf8_need);
if (need < 0) {
BIF_ERROR(BIF_P,BADARG);
}
if (is_binary(subject) && need >= 0 && aligned_binary_size(subject) == need
&& (latin1 || is_valid_utf8(subject))) {
cost_to_proc(BIF_P, simple_loops_to_common(cost_of_utf8_need));
BIF_RET(subject);
}
bin = erts_new_mso_binary(BIF_P, (byte *)NULL, need);
bytes = binary_bytes(bin);
cost_to_proc(BIF_P, simple_loops_to_common(cost_of_utf8_need));
left = allowed_iterations(BIF_P) -
simple_loops_to_common(cost_of_utf8_need);
if (left <= 0) {
/* simplified - let everything be setup by setting left to 1 */
left = 1;
}
sleft = left;
pos = 0;
err = 0;
rest_term = do_build_utf8(BIF_P, subject, &left, latin1,
bytes, &pos, &characters, &err, leftover, &num_leftovers);
#ifdef HARDDEBUG
if (left == 0) {
Eterm bin;
if (is_binary(subject)) {
bin = subject;
} else if(is_list(subject) && is_binary(CAR(list_val(subject)))) {
bin = CAR(list_val(subject));
} else {
bin = NIL;
}
if (is_binary(bin)) {