-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
transcode.c
4583 lines (4057 loc) · 137 KB
/
transcode.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
/**********************************************************************
transcode.c -
$Author$
created at: Tue Oct 30 16:10:22 JST 2007
Copyright (C) 2007 Martin Duerst
**********************************************************************/
#include "ruby/internal/config.h"
#include <ctype.h>
#include "internal.h"
#include "internal/array.h"
#include "internal/inits.h"
#include "internal/object.h"
#include "internal/string.h"
#include "internal/transcode.h"
#include "ruby/encoding.h"
#include "transcode_data.h"
#include "id.h"
#define ENABLE_ECONV_NEWLINE_OPTION 1
/* VALUE rb_cEncoding = rb_define_class("Encoding", rb_cObject); */
static VALUE rb_eUndefinedConversionError;
static VALUE rb_eInvalidByteSequenceError;
static VALUE rb_eConverterNotFoundError;
VALUE rb_cEncodingConverter;
static VALUE sym_invalid, sym_undef, sym_replace, sym_fallback;
static VALUE sym_xml, sym_text, sym_attr;
static VALUE sym_universal_newline;
static VALUE sym_crlf_newline;
static VALUE sym_cr_newline;
#ifdef ENABLE_ECONV_NEWLINE_OPTION
static VALUE sym_newline, sym_universal, sym_crlf, sym_cr, sym_lf;
#endif
static VALUE sym_partial_input;
static VALUE sym_invalid_byte_sequence;
static VALUE sym_undefined_conversion;
static VALUE sym_destination_buffer_full;
static VALUE sym_source_buffer_empty;
static VALUE sym_finished;
static VALUE sym_after_output;
static VALUE sym_incomplete_input;
static unsigned char *
allocate_converted_string(const char *sname, const char *dname,
const unsigned char *str, size_t len,
unsigned char *caller_dst_buf, size_t caller_dst_bufsize,
size_t *dst_len_ptr);
/* dynamic structure, one per conversion (similar to iconv_t) */
/* may carry conversion state (e.g. for iso-2022-jp) */
typedef struct rb_transcoding {
const rb_transcoder *transcoder;
int flags;
int resume_position;
unsigned int next_table;
VALUE next_info;
unsigned char next_byte;
unsigned int output_index;
ssize_t recognized_len; /* already interpreted */
ssize_t readagain_len; /* not yet interpreted */
union {
unsigned char ary[8]; /* max_input <= sizeof(ary) */
unsigned char *ptr; /* length: max_input */
} readbuf; /* recognized_len + readagain_len used */
ssize_t writebuf_off;
ssize_t writebuf_len;
union {
unsigned char ary[8]; /* max_output <= sizeof(ary) */
unsigned char *ptr; /* length: max_output */
} writebuf;
union rb_transcoding_state_t { /* opaque data for stateful encoding */
void *ptr;
char ary[sizeof(double) > sizeof(void*) ? sizeof(double) : sizeof(void*)];
double dummy_for_alignment;
} state;
} rb_transcoding;
#define TRANSCODING_READBUF(tc) \
((tc)->transcoder->max_input <= (int)sizeof((tc)->readbuf.ary) ? \
(tc)->readbuf.ary : \
(tc)->readbuf.ptr)
#define TRANSCODING_WRITEBUF(tc) \
((tc)->transcoder->max_output <= (int)sizeof((tc)->writebuf.ary) ? \
(tc)->writebuf.ary : \
(tc)->writebuf.ptr)
#define TRANSCODING_WRITEBUF_SIZE(tc) \
((tc)->transcoder->max_output <= (int)sizeof((tc)->writebuf.ary) ? \
sizeof((tc)->writebuf.ary) : \
(size_t)(tc)->transcoder->max_output)
#define TRANSCODING_STATE_EMBED_MAX ((int)sizeof(union rb_transcoding_state_t))
#define TRANSCODING_STATE(tc) \
((tc)->transcoder->state_size <= (int)sizeof((tc)->state) ? \
(tc)->state.ary : \
(tc)->state.ptr)
typedef struct {
struct rb_transcoding *tc;
unsigned char *out_buf_start;
unsigned char *out_data_start;
unsigned char *out_data_end;
unsigned char *out_buf_end;
rb_econv_result_t last_result;
} rb_econv_elem_t;
struct rb_econv_t {
int flags;
int started; /* bool */
const char *source_encoding_name;
const char *destination_encoding_name;
const unsigned char *replacement_str;
size_t replacement_len;
const char *replacement_enc;
unsigned char *in_buf_start;
unsigned char *in_data_start;
unsigned char *in_data_end;
unsigned char *in_buf_end;
rb_econv_elem_t *elems;
int replacement_allocated; /* bool */
int num_allocated;
int num_trans;
int num_finished;
struct rb_transcoding *last_tc;
/* last error */
struct {
rb_econv_result_t result;
struct rb_transcoding *error_tc;
const char *source_encoding;
const char *destination_encoding;
const unsigned char *error_bytes_start;
size_t error_bytes_len;
size_t readagain_len;
} last_error;
/* The following fields are only for Encoding::Converter.
* rb_econv_open set them NULL. */
rb_encoding *source_encoding;
rb_encoding *destination_encoding;
};
/*
* Dispatch data and logic
*/
#define DECORATOR_P(sname, dname) (*(sname) == '\0')
typedef struct {
const char *sname;
const char *dname;
const char *lib; /* null means no need to load a library */
const rb_transcoder *transcoder;
} transcoder_entry_t;
static st_table *transcoder_table;
static transcoder_entry_t *
make_transcoder_entry(const char *sname, const char *dname)
{
st_data_t val;
st_table *table2;
if (!st_lookup(transcoder_table, (st_data_t)sname, &val)) {
val = (st_data_t)st_init_strcasetable();
st_add_direct(transcoder_table, (st_data_t)sname, val);
}
table2 = (st_table *)val;
if (!st_lookup(table2, (st_data_t)dname, &val)) {
transcoder_entry_t *entry = ALLOC(transcoder_entry_t);
entry->sname = sname;
entry->dname = dname;
entry->lib = NULL;
entry->transcoder = NULL;
val = (st_data_t)entry;
st_add_direct(table2, (st_data_t)dname, val);
}
return (transcoder_entry_t *)val;
}
static transcoder_entry_t *
get_transcoder_entry(const char *sname, const char *dname)
{
st_data_t val;
st_table *table2;
if (!st_lookup(transcoder_table, (st_data_t)sname, &val)) {
return NULL;
}
table2 = (st_table *)val;
if (!st_lookup(table2, (st_data_t)dname, &val)) {
return NULL;
}
return (transcoder_entry_t *)val;
}
void
rb_register_transcoder(const rb_transcoder *tr)
{
const char *const sname = tr->src_encoding;
const char *const dname = tr->dst_encoding;
transcoder_entry_t *entry;
entry = make_transcoder_entry(sname, dname);
if (entry->transcoder) {
rb_raise(rb_eArgError, "transcoder from %s to %s has been already registered",
sname, dname);
}
entry->transcoder = tr;
}
static void
declare_transcoder(const char *sname, const char *dname, const char *lib)
{
transcoder_entry_t *entry;
entry = make_transcoder_entry(sname, dname);
entry->lib = lib;
}
static const char transcoder_lib_prefix[] = "enc/trans/";
void
rb_declare_transcoder(const char *enc1, const char *enc2, const char *lib)
{
if (!lib) {
rb_raise(rb_eArgError, "invalid library name - (null)");
}
declare_transcoder(enc1, enc2, lib);
}
#define encoding_equal(enc1, enc2) (STRCASECMP((enc1), (enc2)) == 0)
typedef struct search_path_queue_tag {
struct search_path_queue_tag *next;
const char *enc;
} search_path_queue_t;
typedef struct {
st_table *visited;
search_path_queue_t *queue;
search_path_queue_t **queue_last_ptr;
const char *base_enc;
} search_path_bfs_t;
static int
transcode_search_path_i(st_data_t key, st_data_t val, st_data_t arg)
{
const char *dname = (const char *)key;
search_path_bfs_t *bfs = (search_path_bfs_t *)arg;
search_path_queue_t *q;
if (st_lookup(bfs->visited, (st_data_t)dname, &val)) {
return ST_CONTINUE;
}
q = ALLOC(search_path_queue_t);
q->enc = dname;
q->next = NULL;
*bfs->queue_last_ptr = q;
bfs->queue_last_ptr = &q->next;
st_add_direct(bfs->visited, (st_data_t)dname, (st_data_t)bfs->base_enc);
return ST_CONTINUE;
}
static int
transcode_search_path(const char *sname, const char *dname,
void (*callback)(const char *sname, const char *dname, int depth, void *arg),
void *arg)
{
search_path_bfs_t bfs;
search_path_queue_t *q;
st_data_t val;
st_table *table2;
int found;
int pathlen = -1;
if (encoding_equal(sname, dname))
return -1;
q = ALLOC(search_path_queue_t);
q->enc = sname;
q->next = NULL;
bfs.queue_last_ptr = &q->next;
bfs.queue = q;
bfs.visited = st_init_strcasetable();
st_add_direct(bfs.visited, (st_data_t)sname, (st_data_t)NULL);
while (bfs.queue) {
q = bfs.queue;
bfs.queue = q->next;
if (!bfs.queue)
bfs.queue_last_ptr = &bfs.queue;
if (!st_lookup(transcoder_table, (st_data_t)q->enc, &val)) {
xfree(q);
continue;
}
table2 = (st_table *)val;
if (st_lookup(table2, (st_data_t)dname, &val)) {
st_add_direct(bfs.visited, (st_data_t)dname, (st_data_t)q->enc);
xfree(q);
found = 1;
goto cleanup;
}
bfs.base_enc = q->enc;
st_foreach(table2, transcode_search_path_i, (st_data_t)&bfs);
bfs.base_enc = NULL;
xfree(q);
}
found = 0;
cleanup:
while (bfs.queue) {
q = bfs.queue;
bfs.queue = q->next;
xfree(q);
}
if (found) {
const char *enc = dname;
int depth;
pathlen = 0;
while (1) {
st_lookup(bfs.visited, (st_data_t)enc, &val);
if (!val)
break;
pathlen++;
enc = (const char *)val;
}
depth = pathlen;
enc = dname;
while (1) {
st_lookup(bfs.visited, (st_data_t)enc, &val);
if (!val)
break;
callback((const char *)val, enc, --depth, arg);
enc = (const char *)val;
}
}
st_free_table(bfs.visited);
return pathlen; /* is -1 if not found */
}
static const rb_transcoder *
load_transcoder_entry(transcoder_entry_t *entry)
{
if (entry->transcoder)
return entry->transcoder;
if (entry->lib) {
const char *const lib = entry->lib;
const size_t len = strlen(lib);
const size_t total_len = sizeof(transcoder_lib_prefix) - 1 + len;
const VALUE fn = rb_str_new(0, total_len);
char *const path = RSTRING_PTR(fn);
memcpy(path, transcoder_lib_prefix, sizeof(transcoder_lib_prefix) - 1);
memcpy(path + sizeof(transcoder_lib_prefix) - 1, lib, len);
rb_str_set_len(fn, total_len);
OBJ_FREEZE(fn);
rb_require_string(fn);
}
if (entry->transcoder)
return entry->transcoder;
return NULL;
}
static const char*
get_replacement_character(const char *encname, size_t *len_ret, const char **repl_encname_ptr)
{
if (encoding_equal(encname, "UTF-8")) {
*len_ret = 3;
*repl_encname_ptr = "UTF-8";
return "\xEF\xBF\xBD";
}
else {
*len_ret = 1;
*repl_encname_ptr = "US-ASCII";
return "?";
}
}
/*
* Transcoding engine logic
*/
static const unsigned char *
transcode_char_start(rb_transcoding *tc,
const unsigned char *in_start,
const unsigned char *inchar_start,
const unsigned char *in_p,
size_t *char_len_ptr)
{
const unsigned char *ptr;
if (inchar_start - in_start < tc->recognized_len) {
MEMCPY(TRANSCODING_READBUF(tc) + tc->recognized_len,
inchar_start, unsigned char, in_p - inchar_start);
ptr = TRANSCODING_READBUF(tc);
}
else {
ptr = inchar_start - tc->recognized_len;
}
*char_len_ptr = tc->recognized_len + (in_p - inchar_start);
return ptr;
}
static rb_econv_result_t
transcode_restartable0(const unsigned char **in_pos, unsigned char **out_pos,
const unsigned char *in_stop, unsigned char *out_stop,
rb_transcoding *tc,
const int opt)
{
const rb_transcoder *tr = tc->transcoder;
int unitlen = tr->input_unit_length;
ssize_t readagain_len = 0;
const unsigned char *inchar_start;
const unsigned char *in_p;
unsigned char *out_p;
in_p = inchar_start = *in_pos;
out_p = *out_pos;
#define SUSPEND(ret, num) \
do { \
tc->resume_position = (num); \
if (0 < in_p - inchar_start) \
MEMMOVE(TRANSCODING_READBUF(tc)+tc->recognized_len, \
inchar_start, unsigned char, in_p - inchar_start); \
*in_pos = in_p; \
*out_pos = out_p; \
tc->recognized_len += in_p - inchar_start; \
if (readagain_len) { \
tc->recognized_len -= readagain_len; \
tc->readagain_len = readagain_len; \
} \
return (ret); \
resume_label ## num:; \
} while (0)
#define SUSPEND_OBUF(num) \
do { \
while (out_stop - out_p < 1) { SUSPEND(econv_destination_buffer_full, num); } \
} while (0)
#define SUSPEND_AFTER_OUTPUT(num) \
if ((opt & ECONV_AFTER_OUTPUT) && *out_pos != out_p) { \
SUSPEND(econv_after_output, num); \
}
#define next_table (tc->next_table)
#define next_info (tc->next_info)
#define next_byte (tc->next_byte)
#define writebuf_len (tc->writebuf_len)
#define writebuf_off (tc->writebuf_off)
switch (tc->resume_position) {
case 0: break;
case 1: goto resume_label1;
case 2: goto resume_label2;
case 3: goto resume_label3;
case 4: goto resume_label4;
case 5: goto resume_label5;
case 6: goto resume_label6;
case 7: goto resume_label7;
case 8: goto resume_label8;
case 9: goto resume_label9;
case 10: goto resume_label10;
case 11: goto resume_label11;
case 12: goto resume_label12;
case 13: goto resume_label13;
case 14: goto resume_label14;
case 15: goto resume_label15;
case 16: goto resume_label16;
case 17: goto resume_label17;
case 18: goto resume_label18;
case 19: goto resume_label19;
case 20: goto resume_label20;
case 21: goto resume_label21;
case 22: goto resume_label22;
case 23: goto resume_label23;
case 24: goto resume_label24;
case 25: goto resume_label25;
case 26: goto resume_label26;
case 27: goto resume_label27;
case 28: goto resume_label28;
case 29: goto resume_label29;
case 30: goto resume_label30;
case 31: goto resume_label31;
case 32: goto resume_label32;
case 33: goto resume_label33;
case 34: goto resume_label34;
}
while (1) {
inchar_start = in_p;
tc->recognized_len = 0;
next_table = tr->conv_tree_start;
SUSPEND_AFTER_OUTPUT(24);
if (in_stop <= in_p) {
if (!(opt & ECONV_PARTIAL_INPUT))
break;
SUSPEND(econv_source_buffer_empty, 7);
continue;
}
#define BYTE_ADDR(index) (tr->byte_array + (index))
#define WORD_ADDR(index) (tr->word_array + INFO2WORDINDEX(index))
#define BL_BASE BYTE_ADDR(BYTE_LOOKUP_BASE(WORD_ADDR(next_table)))
#define BL_INFO WORD_ADDR(BYTE_LOOKUP_INFO(WORD_ADDR(next_table)))
#define BL_MIN_BYTE (BL_BASE[0])
#define BL_MAX_BYTE (BL_BASE[1])
#define BL_OFFSET(byte) (BL_BASE[2+(byte)-BL_MIN_BYTE])
#define BL_ACTION(byte) (BL_INFO[BL_OFFSET((byte))])
next_byte = (unsigned char)*in_p++;
follow_byte:
if (next_byte < BL_MIN_BYTE || BL_MAX_BYTE < next_byte)
next_info = INVALID;
else {
next_info = (VALUE)BL_ACTION(next_byte);
}
follow_info:
switch (next_info & 0x1F) {
case NOMAP:
{
const unsigned char *p = inchar_start;
writebuf_off = 0;
while (p < in_p) {
TRANSCODING_WRITEBUF(tc)[writebuf_off++] = (unsigned char)*p++;
}
writebuf_len = writebuf_off;
writebuf_off = 0;
while (writebuf_off < writebuf_len) {
SUSPEND_OBUF(3);
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
}
}
continue;
case 0x00: case 0x04: case 0x08: case 0x0C:
case 0x10: case 0x14: case 0x18: case 0x1C:
SUSPEND_AFTER_OUTPUT(25);
while (in_p >= in_stop) {
if (!(opt & ECONV_PARTIAL_INPUT))
goto incomplete;
SUSPEND(econv_source_buffer_empty, 5);
}
next_byte = (unsigned char)*in_p++;
next_table = (unsigned int)next_info;
goto follow_byte;
case ZERObt: /* drop input */
continue;
case ONEbt:
SUSPEND_OBUF(9); *out_p++ = getBT1(next_info);
continue;
case TWObt:
SUSPEND_OBUF(10); *out_p++ = getBT1(next_info);
SUSPEND_OBUF(21); *out_p++ = getBT2(next_info);
continue;
case THREEbt:
SUSPEND_OBUF(11); *out_p++ = getBT1(next_info);
SUSPEND_OBUF(15); *out_p++ = getBT2(next_info);
SUSPEND_OBUF(16); *out_p++ = getBT3(next_info);
continue;
case FOURbt:
SUSPEND_OBUF(12); *out_p++ = getBT0(next_info);
SUSPEND_OBUF(17); *out_p++ = getBT1(next_info);
SUSPEND_OBUF(18); *out_p++ = getBT2(next_info);
SUSPEND_OBUF(19); *out_p++ = getBT3(next_info);
continue;
case GB4bt:
SUSPEND_OBUF(29); *out_p++ = getGB4bt0(next_info);
SUSPEND_OBUF(30); *out_p++ = getGB4bt1(next_info);
SUSPEND_OBUF(31); *out_p++ = getGB4bt2(next_info);
SUSPEND_OBUF(32); *out_p++ = getGB4bt3(next_info);
continue;
case STR1:
tc->output_index = 0;
while (tc->output_index < STR1_LENGTH(BYTE_ADDR(STR1_BYTEINDEX(next_info)))) {
SUSPEND_OBUF(28); *out_p++ = BYTE_ADDR(STR1_BYTEINDEX(next_info))[1+tc->output_index];
tc->output_index++;
}
continue;
case FUNii:
next_info = (VALUE)(*tr->func_ii)(TRANSCODING_STATE(tc), next_info);
goto follow_info;
case FUNsi:
{
const unsigned char *char_start;
size_t char_len;
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
next_info = (VALUE)(*tr->func_si)(TRANSCODING_STATE(tc), char_start, (size_t)char_len);
goto follow_info;
}
case FUNio:
SUSPEND_OBUF(13);
if (tr->max_output <= out_stop - out_p)
out_p += tr->func_io(TRANSCODING_STATE(tc),
next_info, out_p, out_stop - out_p);
else {
writebuf_len = tr->func_io(TRANSCODING_STATE(tc),
next_info,
TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
writebuf_off = 0;
while (writebuf_off < writebuf_len) {
SUSPEND_OBUF(20);
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
}
}
break;
case FUNso:
{
const unsigned char *char_start;
size_t char_len;
SUSPEND_OBUF(14);
if (tr->max_output <= out_stop - out_p) {
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
out_p += tr->func_so(TRANSCODING_STATE(tc),
char_start, (size_t)char_len,
out_p, out_stop - out_p);
}
else {
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
writebuf_len = tr->func_so(TRANSCODING_STATE(tc),
char_start, (size_t)char_len,
TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
writebuf_off = 0;
while (writebuf_off < writebuf_len) {
SUSPEND_OBUF(22);
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
}
}
break;
}
case FUNsio:
{
const unsigned char *char_start;
size_t char_len;
SUSPEND_OBUF(33);
if (tr->max_output <= out_stop - out_p) {
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
out_p += tr->func_sio(TRANSCODING_STATE(tc),
char_start, (size_t)char_len, next_info,
out_p, out_stop - out_p);
}
else {
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
writebuf_len = tr->func_sio(TRANSCODING_STATE(tc),
char_start, (size_t)char_len, next_info,
TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
writebuf_off = 0;
while (writebuf_off < writebuf_len) {
SUSPEND_OBUF(34);
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
}
}
break;
}
case INVALID:
if (tc->recognized_len + (in_p - inchar_start) <= unitlen) {
if (tc->recognized_len + (in_p - inchar_start) < unitlen)
SUSPEND_AFTER_OUTPUT(26);
while ((opt & ECONV_PARTIAL_INPUT) && tc->recognized_len + (in_stop - inchar_start) < unitlen) {
in_p = in_stop;
SUSPEND(econv_source_buffer_empty, 8);
}
if (tc->recognized_len + (in_stop - inchar_start) <= unitlen) {
in_p = in_stop;
}
else {
in_p = inchar_start + (unitlen - tc->recognized_len);
}
}
else {
ssize_t invalid_len; /* including the last byte which causes invalid */
ssize_t discard_len;
invalid_len = tc->recognized_len + (in_p - inchar_start);
discard_len = ((invalid_len - 1) / unitlen) * unitlen;
readagain_len = invalid_len - discard_len;
}
goto invalid;
case UNDEF:
goto undef;
default:
rb_raise(rb_eRuntimeError, "unknown transcoding instruction");
}
continue;
invalid:
SUSPEND(econv_invalid_byte_sequence, 1);
continue;
incomplete:
SUSPEND(econv_incomplete_input, 27);
continue;
undef:
SUSPEND(econv_undefined_conversion, 2);
continue;
}
/* cleanup */
if (tr->finish_func) {
SUSPEND_OBUF(4);
if (tr->max_output <= out_stop - out_p) {
out_p += tr->finish_func(TRANSCODING_STATE(tc),
out_p, out_stop - out_p);
}
else {
writebuf_len = tr->finish_func(TRANSCODING_STATE(tc),
TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
writebuf_off = 0;
while (writebuf_off < writebuf_len) {
SUSPEND_OBUF(23);
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
}
}
}
while (1)
SUSPEND(econv_finished, 6);
#undef SUSPEND
#undef next_table
#undef next_info
#undef next_byte
#undef writebuf_len
#undef writebuf_off
}
static rb_econv_result_t
transcode_restartable(const unsigned char **in_pos, unsigned char **out_pos,
const unsigned char *in_stop, unsigned char *out_stop,
rb_transcoding *tc,
const int opt)
{
if (tc->readagain_len) {
unsigned char *readagain_buf = ALLOCA_N(unsigned char, tc->readagain_len);
const unsigned char *readagain_pos = readagain_buf;
const unsigned char *readagain_stop = readagain_buf + tc->readagain_len;
rb_econv_result_t res;
MEMCPY(readagain_buf, TRANSCODING_READBUF(tc) + tc->recognized_len,
unsigned char, tc->readagain_len);
tc->readagain_len = 0;
res = transcode_restartable0(&readagain_pos, out_pos, readagain_stop, out_stop, tc, opt|ECONV_PARTIAL_INPUT);
if (res != econv_source_buffer_empty) {
MEMCPY(TRANSCODING_READBUF(tc) + tc->recognized_len + tc->readagain_len,
readagain_pos, unsigned char, readagain_stop - readagain_pos);
tc->readagain_len += readagain_stop - readagain_pos;
return res;
}
}
return transcode_restartable0(in_pos, out_pos, in_stop, out_stop, tc, opt);
}
static rb_transcoding *
rb_transcoding_open_by_transcoder(const rb_transcoder *tr, int flags)
{
rb_transcoding *tc;
tc = ALLOC(rb_transcoding);
tc->transcoder = tr;
tc->flags = flags;
if (TRANSCODING_STATE_EMBED_MAX < tr->state_size)
tc->state.ptr = xmalloc(tr->state_size);
if (tr->state_init_func) {
(tr->state_init_func)(TRANSCODING_STATE(tc)); /* xxx: check return value */
}
tc->resume_position = 0;
tc->recognized_len = 0;
tc->readagain_len = 0;
tc->writebuf_len = 0;
tc->writebuf_off = 0;
if ((int)sizeof(tc->readbuf.ary) < tr->max_input) {
tc->readbuf.ptr = xmalloc(tr->max_input);
}
if ((int)sizeof(tc->writebuf.ary) < tr->max_output) {
tc->writebuf.ptr = xmalloc(tr->max_output);
}
return tc;
}
static rb_econv_result_t
rb_transcoding_convert(rb_transcoding *tc,
const unsigned char **input_ptr, const unsigned char *input_stop,
unsigned char **output_ptr, unsigned char *output_stop,
int flags)
{
return transcode_restartable(
input_ptr, output_ptr,
input_stop, output_stop,
tc, flags);
}
static void
rb_transcoding_close(rb_transcoding *tc)
{
const rb_transcoder *tr = tc->transcoder;
if (tr->state_fini_func) {
(tr->state_fini_func)(TRANSCODING_STATE(tc)); /* check return value? */
}
if (TRANSCODING_STATE_EMBED_MAX < tr->state_size)
xfree(tc->state.ptr);
if ((int)sizeof(tc->readbuf.ary) < tr->max_input)
xfree(tc->readbuf.ptr);
if ((int)sizeof(tc->writebuf.ary) < tr->max_output)
xfree(tc->writebuf.ptr);
xfree(tc);
}
static size_t
rb_transcoding_memsize(rb_transcoding *tc)
{
size_t size = sizeof(rb_transcoding);
const rb_transcoder *tr = tc->transcoder;
if (TRANSCODING_STATE_EMBED_MAX < tr->state_size) {
size += tr->state_size;
}
if ((int)sizeof(tc->readbuf.ary) < tr->max_input) {
size += tr->max_input;
}
if ((int)sizeof(tc->writebuf.ary) < tr->max_output) {
size += tr->max_output;
}
return size;
}
static rb_econv_t *
rb_econv_alloc(int n_hint)
{
rb_econv_t *ec;
if (n_hint <= 0)
n_hint = 1;
ec = ALLOC(rb_econv_t);
ec->flags = 0;
ec->source_encoding_name = NULL;
ec->destination_encoding_name = NULL;
ec->started = 0;
ec->replacement_str = NULL;
ec->replacement_len = 0;
ec->replacement_enc = NULL;
ec->replacement_allocated = 0;
ec->in_buf_start = NULL;
ec->in_data_start = NULL;
ec->in_data_end = NULL;
ec->in_buf_end = NULL;
ec->num_allocated = n_hint;
ec->num_trans = 0;
ec->elems = ALLOC_N(rb_econv_elem_t, ec->num_allocated);
ec->num_finished = 0;
ec->last_tc = NULL;
ec->last_error.result = econv_source_buffer_empty;
ec->last_error.error_tc = NULL;
ec->last_error.source_encoding = NULL;
ec->last_error.destination_encoding = NULL;
ec->last_error.error_bytes_start = NULL;
ec->last_error.error_bytes_len = 0;
ec->last_error.readagain_len = 0;
ec->source_encoding = NULL;
ec->destination_encoding = NULL;
return ec;
}
static int
rb_econv_add_transcoder_at(rb_econv_t *ec, const rb_transcoder *tr, int i)
{
int n, j;
int bufsize = 4096;
unsigned char *p;
if (ec->num_trans == ec->num_allocated) {
n = ec->num_allocated * 2;
REALLOC_N(ec->elems, rb_econv_elem_t, n);
ec->num_allocated = n;
}
p = xmalloc(bufsize);
MEMMOVE(ec->elems+i+1, ec->elems+i, rb_econv_elem_t, ec->num_trans-i);
ec->elems[i].tc = rb_transcoding_open_by_transcoder(tr, 0);
ec->elems[i].out_buf_start = p;
ec->elems[i].out_buf_end = p + bufsize;
ec->elems[i].out_data_start = p;
ec->elems[i].out_data_end = p;
ec->elems[i].last_result = econv_source_buffer_empty;
ec->num_trans++;
if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding))
for (j = ec->num_trans-1; i <= j; j--) {
rb_transcoding *tc = ec->elems[j].tc;
const rb_transcoder *tr2 = tc->transcoder;
if (!DECORATOR_P(tr2->src_encoding, tr2->dst_encoding)) {
ec->last_tc = tc;
break;
}
}
return 0;
}
static rb_econv_t *
rb_econv_open_by_transcoder_entries(int n, transcoder_entry_t **entries)
{
rb_econv_t *ec;
int i, ret;
for (i = 0; i < n; i++) {
const rb_transcoder *tr;
tr = load_transcoder_entry(entries[i]);
if (!tr)
return NULL;
}
ec = rb_econv_alloc(n);
for (i = 0; i < n; i++) {
const rb_transcoder *tr = load_transcoder_entry(entries[i]);
ret = rb_econv_add_transcoder_at(ec, tr, ec->num_trans);
if (ret == -1) {
rb_econv_close(ec);
return NULL;
}
}
return ec;
}
struct trans_open_t {
transcoder_entry_t **entries;
int num_additional;
};
static void
trans_open_i(const char *sname, const char *dname, int depth, void *arg)
{
struct trans_open_t *toarg = arg;
if (!toarg->entries) {
toarg->entries = ALLOC_N(transcoder_entry_t *, depth+1+toarg->num_additional);
}
toarg->entries[depth] = get_transcoder_entry(sname, dname);
}
static rb_econv_t *
rb_econv_open0(const char *sname, const char *dname, int ecflags)
{
transcoder_entry_t **entries = NULL;
int num_trans;
rb_econv_t *ec;
/* Just check if sname and dname are defined */
/* (This check is needed?) */
if (*sname) rb_enc_find_index(sname);
if (*dname) rb_enc_find_index(dname);
if (*sname == '\0' && *dname == '\0') {
num_trans = 0;
entries = NULL;
sname = dname = "";
}
else {
struct trans_open_t toarg;
toarg.entries = NULL;
toarg.num_additional = 0;
num_trans = transcode_search_path(sname, dname, trans_open_i, (void *)&toarg);