Ruby 4.1.0dev (2026-09-27 revision f6ff9e7d02e46360f8930b280a3dd921cccbda29)
transcode.c (f6ff9e7d02e46360f8930b280a3dd921cccbda29)
1/**********************************************************************
2
3 transcode.c -
4
5 $Author$
6 created at: Tue Oct 30 16:10:22 JST 2007
7
8 Copyright (C) 2007 Martin Duerst
9
10**********************************************************************/
11
12#include "ruby/internal/config.h"
13
14#include <ctype.h>
15
16#include "internal.h"
17#include "internal/array.h"
18#include "internal/inits.h"
19#include "internal/gc.h"
20#include "internal/object.h"
21#include "internal/string.h"
22#include "internal/transcode.h"
23#include "internal/encoding.h"
24#include "ruby/encoding.h"
25#include "vm_sync.h"
26
27#include "transcode_data.h"
28#include "id.h"
29
30#define ENABLE_ECONV_NEWLINE_OPTION 1
31
32/* VALUE rb_cEncoding = rb_define_class("Encoding", rb_cObject); */
33static VALUE rb_eUndefinedConversionError;
34static VALUE rb_eInvalidByteSequenceError;
35static VALUE rb_eConverterNotFoundError;
36
37VALUE rb_cEncodingConverter;
38
39static ID id_destination_encoding;
40static ID id_destination_encoding_name;
41static ID id_error_bytes;
42static ID id_error_char;
43static ID id_incomplete_input;
44static ID id_readagain_bytes;
45static ID id_source_encoding;
46static ID id_source_encoding_name;
47
48static VALUE sym_invalid, sym_undef, sym_replace, sym_fallback;
49static VALUE sym_xml, sym_text, sym_attr;
50static VALUE sym_universal_newline;
51static VALUE sym_crlf_newline;
52static VALUE sym_cr_newline;
53static VALUE sym_lf_newline;
54#ifdef ENABLE_ECONV_NEWLINE_OPTION
55static VALUE sym_newline, sym_universal, sym_crlf, sym_cr, sym_lf;
56#endif
57static VALUE sym_partial_input;
58
59static VALUE sym_invalid_byte_sequence;
60static VALUE sym_undefined_conversion;
61static VALUE sym_destination_buffer_full;
62static VALUE sym_source_buffer_empty;
63static VALUE sym_finished;
64static VALUE sym_after_output;
65static VALUE sym_incomplete_input;
66
67static unsigned char *
68allocate_converted_string(const char *sname, const char *dname,
69 const unsigned char *str, size_t len,
70 unsigned char *caller_dst_buf, size_t caller_dst_bufsize,
71 size_t *dst_len_ptr, size_t *dst_bufsize_ptr);
72
73/* dynamic structure, one per conversion (similar to iconv_t) */
74/* may carry conversion state (e.g. for iso-2022-jp) */
75typedef struct rb_transcoding {
76 const rb_transcoder *transcoder;
77
78 int flags;
79
80 int resume_position;
81 unsigned int next_table;
82 VALUE next_info;
83 unsigned char next_byte;
84 unsigned int output_index;
85
86 ssize_t recognized_len; /* already interpreted */
87 ssize_t readagain_len; /* not yet interpreted */
88 union {
89 unsigned char ary[8]; /* max_input <= sizeof(ary) */
90 unsigned char *ptr; /* length: max_input */
91 } readbuf; /* recognized_len + readagain_len used */
92
93 ssize_t writebuf_off;
94 ssize_t writebuf_len;
95 union {
96 unsigned char ary[8]; /* max_output <= sizeof(ary) */
97 unsigned char *ptr; /* length: max_output */
98 } writebuf;
99
100 union rb_transcoding_state_t { /* opaque data for stateful encoding */
101 void *ptr;
102 char ary[sizeof(double) > sizeof(void*) ? sizeof(double) : sizeof(void*)];
103 double dummy_for_alignment;
104 } state;
106#define TRANSCODING_READBUF(tc) \
107 ((tc)->transcoder->max_input <= (int)sizeof((tc)->readbuf.ary) ? \
108 (tc)->readbuf.ary : \
109 (tc)->readbuf.ptr)
110#define TRANSCODING_WRITEBUF(tc) \
111 ((tc)->transcoder->max_output <= (int)sizeof((tc)->writebuf.ary) ? \
112 (tc)->writebuf.ary : \
113 (tc)->writebuf.ptr)
114#define TRANSCODING_WRITEBUF_SIZE(tc) \
115 ((tc)->transcoder->max_output <= (int)sizeof((tc)->writebuf.ary) ? \
116 sizeof((tc)->writebuf.ary) : \
117 (size_t)(tc)->transcoder->max_output)
118#define TRANSCODING_STATE_EMBED_MAX ((int)sizeof(union rb_transcoding_state_t))
119#define TRANSCODING_STATE(tc) \
120 ((tc)->transcoder->state_size <= (int)sizeof((tc)->state) ? \
121 (tc)->state.ary : \
122 (tc)->state.ptr)
123
124typedef struct {
125 struct rb_transcoding *tc;
126 unsigned char *out_buf_start;
127 unsigned char *out_data_start;
128 unsigned char *out_data_end;
129 unsigned char *out_buf_end;
130 rb_econv_result_t last_result;
132
134 int flags;
135 int started; /* bool */
136
137 const char *source_encoding_name;
138 const char *destination_encoding_name;
139
140 const unsigned char *replacement_str;
141 size_t replacement_len;
142 size_t replacement_bufsize;
143 const char *replacement_enc;
144
145 unsigned char *in_buf_start;
146 unsigned char *in_data_start;
147 unsigned char *in_data_end;
148 unsigned char *in_buf_end;
149 rb_econv_elem_t *elems;
150 int replacement_allocated; /* bool */
151 int num_allocated;
152 int num_trans;
153 int num_finished;
154 struct rb_transcoding *last_tc;
155
156 /* last error */
157 struct {
158 rb_econv_result_t result;
159 struct rb_transcoding *error_tc;
160 const char *source_encoding;
161 const char *destination_encoding;
162 const unsigned char *error_bytes_start;
163 size_t error_bytes_len;
164 size_t readagain_len;
165 } last_error;
166
167 /* The following fields are only for Encoding::Converter.
168 * rb_econv_open set them NULL. */
169 rb_encoding *source_encoding;
170 rb_encoding *destination_encoding;
171};
172
173/*
174 * Dispatch data and logic
175 */
176
177#define DECORATOR_P(sname, dname) (*(sname) == '\0')
178
179typedef struct {
180 const char *sname;
181 const char *dname;
182 const char *lib; /* null means no need to load a library */
183 const rb_transcoder *transcoder;
185
186static st_table *transcoder_table;
187
188static int
189free_inner_transcode_i(st_data_t key, st_data_t val, st_data_t arg)
190{
191 SIZED_FREE((transcoder_entry_t *)val);
192 return ST_DELETE;
193}
194
195static int
196free_transcode_i(st_data_t key, st_data_t val, st_data_t arg)
197{
198 st_foreach((void *)val, free_inner_transcode_i, 0);
199 st_free_table((void *)val);
200 return ST_DELETE;
201}
202
203void
204rb_free_transcoder_table(void)
205{
206 st_foreach(transcoder_table, free_transcode_i, 0);
207 st_free_table(transcoder_table);
208}
209
210static transcoder_entry_t *
211make_transcoder_entry(const char *sname, const char *dname)
212{
213 st_data_t val;
214 st_table *table2;
215
216 RB_VM_LOCKING() {
217 if (!st_lookup(transcoder_table, (st_data_t)sname, &val)) {
218 val = (st_data_t)st_init_strcasetable();
219 st_add_direct(transcoder_table, (st_data_t)sname, val);
220 }
221 table2 = (st_table *)val;
222 if (!st_lookup(table2, (st_data_t)dname, &val)) {
224 entry->sname = sname;
225 entry->dname = dname;
226 entry->lib = NULL;
227 entry->transcoder = NULL;
228 val = (st_data_t)entry;
229 st_add_direct(table2, (st_data_t)dname, val);
230 }
231 }
232 return (transcoder_entry_t *)val;
233}
234
235static transcoder_entry_t *
236get_transcoder_entry(const char *sname, const char *dname)
237{
238 st_data_t val = 0;
239 st_table *table2;
240 RB_VM_LOCKING() {
241 if (st_lookup(transcoder_table, (st_data_t)sname, &val)) {
242 table2 = (st_table *)val;
243 if (!st_lookup(table2, (st_data_t)dname, &val)) {
244 val = 0;
245 }
246 }
247 }
248 return (transcoder_entry_t *)val;
249}
250
251void
252rb_register_transcoder(const rb_transcoder *tr)
253{
254 const char *const sname = tr->src_encoding;
255 const char *const dname = tr->dst_encoding;
256
257 transcoder_entry_t *entry;
258
259 RB_VM_LOCKING() {
260 entry = make_transcoder_entry(sname, dname);
261 if (entry->transcoder) {
262 rb_raise(rb_eArgError, "transcoder from %s to %s has been already registered",
263 sname, dname);
264 }
265 entry->transcoder = tr;
266 }
267}
268
269static void
270declare_transcoder(const char *sname, const char *dname, const char *lib)
271{
272 transcoder_entry_t *entry;
273
274 entry = make_transcoder_entry(sname, dname);
275 entry->lib = lib;
276}
277
278static const char transcoder_lib_prefix[] = "enc/trans/";
279
280void
281rb_declare_transcoder(const char *enc1, const char *enc2, const char *lib)
282{
283 if (!lib) {
284 rb_raise(rb_eArgError, "invalid library name - (null)");
285 }
286 declare_transcoder(enc1, enc2, lib);
287}
288
289#define encoding_equal(enc1, enc2) (STRCASECMP((enc1), (enc2)) == 0)
290
291typedef struct search_path_queue_tag {
292 struct search_path_queue_tag *next;
293 const char *enc;
295
296typedef struct {
297 st_table *visited;
298 search_path_queue_t *queue;
299 search_path_queue_t **queue_last_ptr;
300 const char *base_enc;
302
303static int
304transcode_search_path_i(st_data_t key, st_data_t val, st_data_t arg)
305{
306 const char *dname = (const char *)key;
309
310 if (st_lookup(bfs->visited, (st_data_t)dname, &val)) {
311 return ST_CONTINUE;
312 }
313
315 q->enc = dname;
316 q->next = NULL;
317 *bfs->queue_last_ptr = q;
318 bfs->queue_last_ptr = &q->next;
319
320 st_add_direct(bfs->visited, (st_data_t)dname, (st_data_t)bfs->base_enc);
321 return ST_CONTINUE;
322}
323
324static int
325transcode_search_path(const char *sname, const char *dname,
326 void (*callback)(const char *sname, const char *dname, int depth, void *arg),
327 void *arg)
328{
331 st_data_t val;
332 st_table *table2;
333 int pathlen = -1;
334 bool found = false;
335 bool lookup_res;
336
337 if (encoding_equal(sname, dname))
338 return -1;
339
341 q->enc = sname;
342 q->next = NULL;
343 bfs.queue_last_ptr = &q->next;
344 bfs.queue = q;
345
346 bfs.visited = st_init_strcasetable(); // due to base encodings, we need to do search in a loop
347 st_add_direct(bfs.visited, (st_data_t)sname, (st_data_t)NULL);
348
349 RB_VM_LOCKING() {
350 while (bfs.queue) {
351 q = bfs.queue;
352 bfs.queue = q->next;
353 if (!bfs.queue) {
354 bfs.queue_last_ptr = &bfs.queue;
355 }
356
357 lookup_res = st_lookup(transcoder_table, (st_data_t)q->enc, &val); // src => table2
358 if (!lookup_res) {
359 SIZED_FREE(q);
360 continue;
361 }
362 table2 = (st_table *)val;
363
364 if (st_lookup(table2, (st_data_t)dname, &val)) { // dest => econv
365 st_add_direct(bfs.visited, (st_data_t)dname, (st_data_t)q->enc);
366 SIZED_FREE(q);
367 found = true;
368 break;
369 }
370
371 bfs.base_enc = q->enc;
372 st_foreach(table2, transcode_search_path_i, (st_data_t)&bfs);
373
374 bfs.base_enc = NULL;
375 SIZED_FREE(q);
376 }
377 }
378
379 while (bfs.queue) {
380 q = bfs.queue;
381 bfs.queue = q->next;
382 SIZED_FREE(q);
383 }
384
385 if (found) {
386 const char *enc = dname;
387 int depth;
388 pathlen = 0;
389 while (1) {
390 st_lookup(bfs.visited, (st_data_t)enc, &val);
391 if (!val)
392 break;
393 pathlen++;
394 enc = (const char *)val;
395 }
396 depth = pathlen;
397 enc = dname;
398 while (1) {
399 st_lookup(bfs.visited, (st_data_t)enc, &val);
400 if (!val)
401 break;
402 callback((const char *)val, enc, --depth, arg);
403 enc = (const char *)val;
404 }
405 }
406
407 st_free_table(bfs.visited);
408
409 return pathlen; /* is -1 if not found */
410}
411
412int rb_require_internal_silent(VALUE fname);
413
414static const rb_transcoder *
415load_transcoder_entry(transcoder_entry_t *entry)
416{
417 ASSERT_vm_unlocking();
418 if (entry->transcoder)
419 return entry->transcoder;
420
421 if (entry->lib) {
422 const char *const lib = entry->lib;
423 const size_t len = strlen(lib);
424 const size_t total_len = sizeof(transcoder_lib_prefix) - 1 + len;
425 const VALUE fn = rb_str_new(0, total_len);
426 char *const path = RSTRING_PTR(fn);
427
428 memcpy(path, transcoder_lib_prefix, sizeof(transcoder_lib_prefix) - 1);
429 memcpy(path + sizeof(transcoder_lib_prefix) - 1, lib, len);
430 rb_str_set_len(fn, total_len);
431 OBJ_FREEZE(fn);
432 rb_require_internal_silent(fn); // Sets entry->transcoder
433 }
434
435 if (entry->transcoder)
436 return entry->transcoder;
437
438 return NULL;
439}
440
441static const char*
442get_replacement_character(const char *encname, size_t *len_ret, const char **repl_encname_ptr)
443{
444 if (encoding_equal(encname, "UTF-8")) {
445 *len_ret = 3;
446 *repl_encname_ptr = "UTF-8";
447 return "\xEF\xBF\xBD";
448 }
449 else {
450 *len_ret = 1;
451 *repl_encname_ptr = "US-ASCII";
452 return "?";
453 }
454}
455
456/*
457 * Transcoding engine logic
458 */
459
460static const unsigned char *
461transcode_char_start(rb_transcoding *tc,
462 const unsigned char *in_start,
463 const unsigned char *inchar_start,
464 const unsigned char *in_p,
465 size_t *char_len_ptr)
466{
467 const unsigned char *ptr;
468 if (inchar_start - in_start < tc->recognized_len) {
469 MEMCPY(TRANSCODING_READBUF(tc) + tc->recognized_len,
470 inchar_start, unsigned char, in_p - inchar_start);
471 ptr = TRANSCODING_READBUF(tc);
472 }
473 else {
474 ptr = inchar_start - tc->recognized_len;
475 }
476 *char_len_ptr = tc->recognized_len + (in_p - inchar_start);
477 return ptr;
478}
479
481transcode_restartable0(const unsigned char **in_pos, unsigned char **out_pos,
482 const unsigned char *in_stop, unsigned char *out_stop,
483 rb_transcoding *tc,
484 const int opt)
485{
486 const rb_transcoder *tr = tc->transcoder;
487 int unitlen = tr->input_unit_length;
488 ssize_t readagain_len = 0;
489
490 const unsigned char *inchar_start;
491 const unsigned char *in_p;
492
493 unsigned char *out_p;
494
495 in_p = inchar_start = *in_pos;
496
497 out_p = *out_pos;
498
499#define SUSPEND(ret, num) \
500 do { \
501 tc->resume_position = (num); \
502 if (0 < in_p - inchar_start) \
503 MEMMOVE(TRANSCODING_READBUF(tc)+tc->recognized_len, \
504 inchar_start, unsigned char, in_p - inchar_start); \
505 *in_pos = in_p; \
506 *out_pos = out_p; \
507 tc->recognized_len += in_p - inchar_start; \
508 if (readagain_len) { \
509 tc->recognized_len -= readagain_len; \
510 tc->readagain_len = readagain_len; \
511 } \
512 return (ret); \
513 resume_label ## num:; \
514 } while (0)
515#define SUSPEND_OBUF(num) \
516 do { \
517 while (out_stop - out_p < 1) { SUSPEND(econv_destination_buffer_full, num); } \
518 } while (0)
519
520#define SUSPEND_AFTER_OUTPUT(num) \
521 if ((opt & ECONV_AFTER_OUTPUT) && *out_pos != out_p) { \
522 SUSPEND(econv_after_output, num); \
523 }
524
525#define next_table (tc->next_table)
526#define next_info (tc->next_info)
527#define next_byte (tc->next_byte)
528#define writebuf_len (tc->writebuf_len)
529#define writebuf_off (tc->writebuf_off)
530
531 switch (tc->resume_position) {
532 case 0: break;
533 case 1: goto resume_label1;
534 case 2: goto resume_label2;
535 case 3: goto resume_label3;
536 case 4: goto resume_label4;
537 case 5: goto resume_label5;
538 case 6: goto resume_label6;
539 case 7: goto resume_label7;
540 case 8: goto resume_label8;
541 case 9: goto resume_label9;
542 case 10: goto resume_label10;
543 case 11: goto resume_label11;
544 case 12: goto resume_label12;
545 case 13: goto resume_label13;
546 case 14: goto resume_label14;
547 case 15: goto resume_label15;
548 case 16: goto resume_label16;
549 case 17: goto resume_label17;
550 case 18: goto resume_label18;
551 case 19: goto resume_label19;
552 case 20: goto resume_label20;
553 case 21: goto resume_label21;
554 case 22: goto resume_label22;
555 case 23: goto resume_label23;
556 case 24: goto resume_label24;
557 case 25: goto resume_label25;
558 case 26: goto resume_label26;
559 case 27: goto resume_label27;
560 case 28: goto resume_label28;
561 case 29: goto resume_label29;
562 case 30: goto resume_label30;
563 case 31: goto resume_label31;
564 case 32: goto resume_label32;
565 case 33: goto resume_label33;
566 case 34: goto resume_label34;
567 }
568
569 while (1) {
570 inchar_start = in_p;
571 tc->recognized_len = 0;
572 next_table = tr->conv_tree_start;
573
574 SUSPEND_AFTER_OUTPUT(24);
575
576 if (in_stop <= in_p) {
577 if (!(opt & ECONV_PARTIAL_INPUT))
578 break;
579 SUSPEND(econv_source_buffer_empty, 7);
580 continue;
581 }
582
583#define BYTE_ADDR(index) (tr->byte_array + (index))
584#define WORD_ADDR(index) (tr->word_array + INFO2WORDINDEX(index))
585#define BL_BASE BYTE_ADDR(BYTE_LOOKUP_BASE(WORD_ADDR(next_table)))
586#define BL_INFO WORD_ADDR(BYTE_LOOKUP_INFO(WORD_ADDR(next_table)))
587#define BL_MIN_BYTE (BL_BASE[0])
588#define BL_MAX_BYTE (BL_BASE[1])
589#define BL_OFFSET(byte) (BL_BASE[2+(byte)-BL_MIN_BYTE])
590#define BL_ACTION(byte) (BL_INFO[BL_OFFSET((byte))])
591
592 next_byte = (unsigned char)*in_p++;
593 follow_byte:
594 if (next_byte < BL_MIN_BYTE || BL_MAX_BYTE < next_byte)
595 next_info = INVALID;
596 else {
597 next_info = (VALUE)BL_ACTION(next_byte);
598 }
599 follow_info:
600 switch (next_info & 0x1F) {
601 case NOMAP:
602 {
603 const unsigned char *p = inchar_start;
604 writebuf_off = 0;
605 while (p < in_p) {
606 TRANSCODING_WRITEBUF(tc)[writebuf_off++] = (unsigned char)*p++;
607 }
608 writebuf_len = writebuf_off;
609 writebuf_off = 0;
610 while (writebuf_off < writebuf_len) {
611 SUSPEND_OBUF(3);
612 *out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
613 }
614 }
615 continue;
616 case 0x00: case 0x04: case 0x08: case 0x0C:
617 case 0x10: case 0x14: case 0x18: case 0x1C:
618 SUSPEND_AFTER_OUTPUT(25);
619 while (in_p >= in_stop) {
620 if (!(opt & ECONV_PARTIAL_INPUT))
621 goto incomplete;
622 SUSPEND(econv_source_buffer_empty, 5);
623 }
624 next_byte = (unsigned char)*in_p++;
625 next_table = (unsigned int)next_info;
626 goto follow_byte;
627 case ZERObt: /* drop input */
628 continue;
629 case ONEbt:
630 SUSPEND_OBUF(9); *out_p++ = getBT1(next_info);
631 continue;
632 case TWObt:
633 SUSPEND_OBUF(10); *out_p++ = getBT1(next_info);
634 SUSPEND_OBUF(21); *out_p++ = getBT2(next_info);
635 continue;
636 case THREEbt:
637 SUSPEND_OBUF(11); *out_p++ = getBT1(next_info);
638 SUSPEND_OBUF(15); *out_p++ = getBT2(next_info);
639 SUSPEND_OBUF(16); *out_p++ = getBT3(next_info);
640 continue;
641 case FOURbt:
642 SUSPEND_OBUF(12); *out_p++ = getBT0(next_info);
643 SUSPEND_OBUF(17); *out_p++ = getBT1(next_info);
644 SUSPEND_OBUF(18); *out_p++ = getBT2(next_info);
645 SUSPEND_OBUF(19); *out_p++ = getBT3(next_info);
646 continue;
647 case GB4bt:
648 SUSPEND_OBUF(29); *out_p++ = getGB4bt0(next_info);
649 SUSPEND_OBUF(30); *out_p++ = getGB4bt1(next_info);
650 SUSPEND_OBUF(31); *out_p++ = getGB4bt2(next_info);
651 SUSPEND_OBUF(32); *out_p++ = getGB4bt3(next_info);
652 continue;
653 case STR1:
654 tc->output_index = 0;
655 while (tc->output_index < STR1_LENGTH(BYTE_ADDR(STR1_BYTEINDEX(next_info)))) {
656 SUSPEND_OBUF(28); *out_p++ = BYTE_ADDR(STR1_BYTEINDEX(next_info))[1+tc->output_index];
657 tc->output_index++;
658 }
659 continue;
660 case FUNii:
661 next_info = (VALUE)(*tr->func_ii)(TRANSCODING_STATE(tc), next_info);
662 goto follow_info;
663 case FUNsi:
664 {
665 const unsigned char *char_start;
666 size_t char_len;
667 char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
668 next_info = (VALUE)(*tr->func_si)(TRANSCODING_STATE(tc), char_start, (size_t)char_len);
669 goto follow_info;
670 }
671 case FUNio:
672 SUSPEND_OBUF(13);
673 if (tr->max_output <= out_stop - out_p)
674 out_p += tr->func_io(TRANSCODING_STATE(tc),
675 next_info, out_p, out_stop - out_p);
676 else {
677 writebuf_len = tr->func_io(TRANSCODING_STATE(tc),
678 next_info,
679 TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
680 writebuf_off = 0;
681 while (writebuf_off < writebuf_len) {
682 SUSPEND_OBUF(20);
683 *out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
684 }
685 }
686 break;
687 case FUNso:
688 {
689 const unsigned char *char_start;
690 size_t char_len;
691 SUSPEND_OBUF(14);
692 if (tr->max_output <= out_stop - out_p) {
693 char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
694 out_p += tr->func_so(TRANSCODING_STATE(tc),
695 char_start, (size_t)char_len,
696 out_p, out_stop - out_p);
697 }
698 else {
699 char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
700 writebuf_len = tr->func_so(TRANSCODING_STATE(tc),
701 char_start, (size_t)char_len,
702 TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
703 writebuf_off = 0;
704 while (writebuf_off < writebuf_len) {
705 SUSPEND_OBUF(22);
706 *out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
707 }
708 }
709 break;
710 }
711 case FUNsio:
712 {
713 const unsigned char *char_start;
714 size_t char_len;
715 SUSPEND_OBUF(33);
716 if (tr->max_output <= out_stop - out_p) {
717 char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
718 out_p += tr->func_sio(TRANSCODING_STATE(tc),
719 char_start, (size_t)char_len, next_info,
720 out_p, out_stop - out_p);
721 }
722 else {
723 char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
724 writebuf_len = tr->func_sio(TRANSCODING_STATE(tc),
725 char_start, (size_t)char_len, next_info,
726 TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
727 writebuf_off = 0;
728 while (writebuf_off < writebuf_len) {
729 SUSPEND_OBUF(34);
730 *out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
731 }
732 }
733 break;
734 }
735 case INVALID:
736 if (tc->recognized_len + (in_p - inchar_start) <= unitlen) {
737 if (tc->recognized_len + (in_p - inchar_start) < unitlen)
738 SUSPEND_AFTER_OUTPUT(26);
739 while ((opt & ECONV_PARTIAL_INPUT) && tc->recognized_len + (in_stop - inchar_start) < unitlen) {
740 in_p = in_stop;
741 SUSPEND(econv_source_buffer_empty, 8);
742 }
743 if (tc->recognized_len + (in_stop - inchar_start) <= unitlen) {
744 in_p = in_stop;
745 }
746 else {
747 in_p = inchar_start + (unitlen - tc->recognized_len);
748 }
749 }
750 else {
751 ssize_t invalid_len; /* including the last byte which causes invalid */
752 ssize_t discard_len;
753 invalid_len = tc->recognized_len + (in_p - inchar_start);
754 discard_len = ((invalid_len - 1) / unitlen) * unitlen;
755 readagain_len = invalid_len - discard_len;
756 }
757 goto invalid;
758 case UNDEF:
759 goto undef;
760 default:
761 rb_raise(rb_eRuntimeError, "unknown transcoding instruction");
762 }
763 continue;
764
765 invalid:
766 SUSPEND(econv_invalid_byte_sequence, 1);
767 continue;
768
769 incomplete:
770 SUSPEND(econv_incomplete_input, 27);
771 continue;
772
773 undef:
774 SUSPEND(econv_undefined_conversion, 2);
775 continue;
776 }
777
778 /* cleanup */
779 if (tr->finish_func) {
780 SUSPEND_OBUF(4);
781 if (tr->max_output <= out_stop - out_p) {
782 out_p += tr->finish_func(TRANSCODING_STATE(tc),
783 out_p, out_stop - out_p);
784 }
785 else {
786 writebuf_len = tr->finish_func(TRANSCODING_STATE(tc),
787 TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
788 writebuf_off = 0;
789 while (writebuf_off < writebuf_len) {
790 SUSPEND_OBUF(23);
791 *out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
792 }
793 }
794 }
795 while (1)
796 SUSPEND(econv_finished, 6);
797#undef SUSPEND
798#undef next_table
799#undef next_info
800#undef next_byte
801#undef writebuf_len
802#undef writebuf_off
803}
804
806transcode_restartable(const unsigned char **in_pos, unsigned char **out_pos,
807 const unsigned char *in_stop, unsigned char *out_stop,
808 rb_transcoding *tc,
809 const int opt)
810{
811 if (tc->readagain_len) {
812 unsigned char *readagain_buf = ALLOCA_N(unsigned char, tc->readagain_len);
813 const unsigned char *readagain_pos = readagain_buf;
814 const unsigned char *readagain_stop = readagain_buf + tc->readagain_len;
816
817 MEMCPY(readagain_buf, TRANSCODING_READBUF(tc) + tc->recognized_len,
818 unsigned char, tc->readagain_len);
819 tc->readagain_len = 0;
820 res = transcode_restartable0(&readagain_pos, out_pos, readagain_stop, out_stop, tc, opt|ECONV_PARTIAL_INPUT);
821 if (res != econv_source_buffer_empty) {
822 MEMCPY(TRANSCODING_READBUF(tc) + tc->recognized_len + tc->readagain_len,
823 readagain_pos, unsigned char, readagain_stop - readagain_pos);
824 tc->readagain_len += readagain_stop - readagain_pos;
825 return res;
826 }
827 }
828 return transcode_restartable0(in_pos, out_pos, in_stop, out_stop, tc, opt);
829}
830
831static rb_transcoding *
832rb_transcoding_open_by_transcoder(const rb_transcoder *tr, int flags)
833{
834 rb_transcoding *tc;
835
836 tc = ALLOC(rb_transcoding);
837 tc->transcoder = tr;
838 tc->flags = flags;
839 if (TRANSCODING_STATE_EMBED_MAX < tr->state_size)
840 tc->state.ptr = xmalloc(tr->state_size);
841 if (tr->state_init_func) {
842 (tr->state_init_func)(TRANSCODING_STATE(tc)); /* xxx: check return value */
843 }
844 tc->resume_position = 0;
845 tc->recognized_len = 0;
846 tc->readagain_len = 0;
847 tc->writebuf_len = 0;
848 tc->writebuf_off = 0;
849 if ((int)sizeof(tc->readbuf.ary) < tr->max_input) {
850 tc->readbuf.ptr = xmalloc(tr->max_input);
851 }
852 if ((int)sizeof(tc->writebuf.ary) < tr->max_output) {
853 tc->writebuf.ptr = xmalloc(tr->max_output);
854 }
855 return tc;
856}
857
859rb_transcoding_convert(rb_transcoding *tc,
860 const unsigned char **input_ptr, const unsigned char *input_stop,
861 unsigned char **output_ptr, unsigned char *output_stop,
862 int flags)
863{
864 return transcode_restartable(
865 input_ptr, output_ptr,
866 input_stop, output_stop,
867 tc, flags);
868}
869
870static void
871rb_transcoding_close(rb_transcoding *tc)
872{
873 const rb_transcoder *tr = tc->transcoder;
874 if (tr->state_fini_func) {
875 (tr->state_fini_func)(TRANSCODING_STATE(tc)); /* check return value? */
876 }
877 if (TRANSCODING_STATE_EMBED_MAX < tr->state_size)
878 ruby_xfree_sized(tc->state.ptr, tr->state_size);
879 if ((int)sizeof(tc->readbuf.ary) < tr->max_input)
880 ruby_xfree_sized(tc->readbuf.ptr, tr->max_input);
881 if ((int)sizeof(tc->writebuf.ary) < tr->max_output)
882 ruby_xfree_sized(tc->writebuf.ptr, tr->max_output);
883 SIZED_FREE(tc);
884}
885
886static size_t
887rb_transcoding_memsize(rb_transcoding *tc)
888{
889 size_t size = sizeof(rb_transcoding);
890 const rb_transcoder *tr = tc->transcoder;
891
892 if (TRANSCODING_STATE_EMBED_MAX < tr->state_size) {
893 size += tr->state_size;
894 }
895 if ((int)sizeof(tc->readbuf.ary) < tr->max_input) {
896 size += tr->max_input;
897 }
898 if ((int)sizeof(tc->writebuf.ary) < tr->max_output) {
899 size += tr->max_output;
900 }
901 return size;
902}
903
904static rb_econv_t *
905rb_econv_alloc(int n_hint)
906{
907 rb_econv_t *ec;
908
909 if (n_hint <= 0)
910 n_hint = 1;
911
912 ec = ALLOC(rb_econv_t);
913 ec->flags = 0;
914 ec->source_encoding_name = NULL;
915 ec->destination_encoding_name = NULL;
916 ec->started = 0;
917 ec->replacement_str = NULL;
918 ec->replacement_len = 0;
919 ec->replacement_bufsize = 0;
920 ec->replacement_enc = NULL;
921 ec->replacement_allocated = 0;
922 ec->in_buf_start = NULL;
923 ec->in_data_start = NULL;
924 ec->in_data_end = NULL;
925 ec->in_buf_end = NULL;
926 ec->num_allocated = n_hint;
927 ec->num_trans = 0;
928 ec->elems = ALLOC_N(rb_econv_elem_t, ec->num_allocated);
929 ec->num_finished = 0;
930 ec->last_tc = NULL;
931 ec->last_error.result = econv_source_buffer_empty;
932 ec->last_error.error_tc = NULL;
933 ec->last_error.source_encoding = NULL;
934 ec->last_error.destination_encoding = NULL;
935 ec->last_error.error_bytes_start = NULL;
936 ec->last_error.error_bytes_len = 0;
937 ec->last_error.readagain_len = 0;
938 ec->source_encoding = NULL;
939 ec->destination_encoding = NULL;
940 return ec;
941}
942
943static int
944rb_econv_add_transcoder_at(rb_econv_t *ec, const rb_transcoder *tr, int i)
945{
946 int n, j;
947 int bufsize = 4096;
948 unsigned char *p;
949
950 if (ec->num_trans == ec->num_allocated) {
951 n = ec->num_allocated * 2;
952 SIZED_REALLOC_N(ec->elems, rb_econv_elem_t, n, ec->num_allocated);
953 ec->num_allocated = n;
954 }
955
956 p = xmalloc(bufsize);
957
958 MEMMOVE(ec->elems+i+1, ec->elems+i, rb_econv_elem_t, ec->num_trans-i);
959
960 ec->elems[i].tc = rb_transcoding_open_by_transcoder(tr, 0);
961 ec->elems[i].out_buf_start = p;
962 ec->elems[i].out_buf_end = p + bufsize;
963 ec->elems[i].out_data_start = p;
964 ec->elems[i].out_data_end = p;
965 ec->elems[i].last_result = econv_source_buffer_empty;
966
967 ec->num_trans++;
968
969 if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding))
970 for (j = ec->num_trans-1; i <= j; j--) {
971 rb_transcoding *tc = ec->elems[j].tc;
972 const rb_transcoder *tr2 = tc->transcoder;
973 if (!DECORATOR_P(tr2->src_encoding, tr2->dst_encoding)) {
974 ec->last_tc = tc;
975 break;
976 }
977 }
978
979 return 0;
980}
981
982static rb_econv_t *
983rb_econv_open_by_transcoder_entries(int n, transcoder_entry_t **entries)
984{
985 rb_econv_t *ec;
986 int i, ret;
987
988 for (i = 0; i < n; i++) {
989 const rb_transcoder *tr;
990 tr = load_transcoder_entry(entries[i]);
991 if (!tr)
992 return NULL;
993 }
994
995 ec = rb_econv_alloc(n);
996
997 for (i = 0; i < n; i++) {
998 const rb_transcoder *tr = load_transcoder_entry(entries[i]);
999 ret = rb_econv_add_transcoder_at(ec, tr, ec->num_trans);
1000 if (ret == -1) {
1001 rb_econv_close(ec);
1002 return NULL;
1003 }
1004 }
1005
1006 return ec;
1007}
1008
1010 transcoder_entry_t **entries;
1011};
1012
1013static void
1014trans_open_i(const char *sname, const char *dname, int depth, void *arg)
1015{
1016 struct trans_open_t *toarg = arg;
1017
1018 if (!toarg->entries) {
1019 toarg->entries = ALLOC_N(transcoder_entry_t *, depth + 1);
1020 }
1021 toarg->entries[depth] = get_transcoder_entry(sname, dname);
1022}
1023
1024static rb_econv_t *
1025rb_econv_open0(const char *sname, const char *dname, int ecflags)
1026{
1027 transcoder_entry_t **entries = NULL;
1028 int num_trans;
1029 rb_econv_t *ec;
1030
1031 // loads encodings if not loaded already
1032 if (*sname) rb_enc_find_index(sname);
1033 if (*dname) rb_enc_find_index(dname);
1034
1035 if (*sname == '\0' && *dname == '\0') {
1036 num_trans = 0;
1037 entries = NULL;
1038 sname = dname = "";
1039 }
1040 else {
1041 struct trans_open_t toarg = {0};
1042 num_trans = transcode_search_path(sname, dname, trans_open_i, (void *)&toarg);
1043 entries = toarg.entries;
1044 if (num_trans < 0) {
1045 SIZED_FREE_N(entries, num_trans);
1046 return NULL;
1047 }
1048 sname = entries[0]->sname;
1049 dname = entries[num_trans-1]->dname;
1050 }
1051
1052 ec = rb_econv_open_by_transcoder_entries(num_trans, entries);
1053 SIZED_FREE_N(entries, num_trans);
1054 if (!ec)
1055 return NULL;
1056
1057 ec->flags = ecflags;
1058 ec->source_encoding_name = sname;
1059 ec->destination_encoding_name = dname;
1060
1061 return ec;
1062}
1063
1064#define MAX_ECFLAGS_DECORATORS 32
1065
1066static int
1067decorator_names(int ecflags, const char **decorators_ret)
1068{
1069 int num_decorators;
1070
1071 switch (ecflags & ECONV_NEWLINE_DECORATOR_MASK) {
1076 case 0:
1077 break;
1078 default:
1079 return -1;
1080 }
1081
1082 if ((ecflags & ECONV_XML_TEXT_DECORATOR) &&
1084 return -1;
1085
1086 num_decorators = 0;
1087
1088 if (ecflags & ECONV_XML_TEXT_DECORATOR)
1089 decorators_ret[num_decorators++] = "xml_text_escape";
1091 decorators_ret[num_decorators++] = "xml_attr_content_escape";
1092 if (ecflags & ECONV_XML_ATTR_QUOTE_DECORATOR)
1093 decorators_ret[num_decorators++] = "xml_attr_quote";
1094
1095 if (ecflags & ECONV_CRLF_NEWLINE_DECORATOR)
1096 decorators_ret[num_decorators++] = "crlf_newline";
1097 if (ecflags & ECONV_CR_NEWLINE_DECORATOR)
1098 decorators_ret[num_decorators++] = "cr_newline";
1099 if (ecflags & ECONV_LF_NEWLINE_DECORATOR)
1100 decorators_ret[num_decorators++] = "lf_newline";
1102 decorators_ret[num_decorators++] = "universal_newline";
1103
1104 return num_decorators;
1105}
1106
1107rb_econv_t *
1108rb_econv_open(const char *sname, const char *dname, int ecflags)
1109{
1110 rb_econv_t *ec;
1111 int num_decorators;
1112 const char *decorators[MAX_ECFLAGS_DECORATORS];
1113 int i;
1114
1115 num_decorators = decorator_names(ecflags, decorators);
1116 if (num_decorators == -1)
1117 return NULL;
1118
1119 ec = rb_econv_open0(sname, dname, ecflags & ECONV_ERROR_HANDLER_MASK);
1120 if (ec) {
1121 for (i = 0; i < num_decorators; i++) {
1122 if (rb_econv_decorate_at_last(ec, decorators[i]) == -1) {
1123 rb_econv_close(ec);
1124 ec = NULL;
1125 break;
1126 }
1127 }
1128 }
1129
1130 if (ec) {
1131 ec->flags |= ecflags & ~ECONV_ERROR_HANDLER_MASK;
1132 }
1133 return ec; // can be NULL
1134}
1135
1136static int
1137trans_sweep(rb_econv_t *ec,
1138 const unsigned char **input_ptr, const unsigned char *input_stop,
1139 unsigned char **output_ptr, unsigned char *output_stop,
1140 int flags,
1141 int start)
1142{
1143 int try;
1144 int i, f;
1145
1146 const unsigned char **ipp, *is, *iold;
1147 unsigned char **opp, *os, *oold;
1149
1150 try = 1;
1151 while (try) {
1152 try = 0;
1153 for (i = start; i < ec->num_trans; i++) {
1154 rb_econv_elem_t *te = &ec->elems[i];
1155
1156 if (i == 0) {
1157 ipp = input_ptr;
1158 is = input_stop;
1159 }
1160 else {
1161 rb_econv_elem_t *prev_te = &ec->elems[i-1];
1162 ipp = (const unsigned char **)&prev_te->out_data_start;
1163 is = prev_te->out_data_end;
1164 }
1165
1166 if (i == ec->num_trans-1) {
1167 opp = output_ptr;
1168 os = output_stop;
1169 }
1170 else {
1171 if (te->out_buf_start != te->out_data_start) {
1172 ssize_t len = te->out_data_end - te->out_data_start;
1173 ssize_t off = te->out_data_start - te->out_buf_start;
1174 MEMMOVE(te->out_buf_start, te->out_data_start, unsigned char, len);
1175 te->out_data_start = te->out_buf_start;
1176 te->out_data_end -= off;
1177 }
1178 opp = &te->out_data_end;
1179 os = te->out_buf_end;
1180 }
1181
1182 f = flags;
1183 if (ec->num_finished != i)
1185 if (i == 0 && (flags & ECONV_AFTER_OUTPUT)) {
1186 start = 1;
1187 flags &= ~ECONV_AFTER_OUTPUT;
1188 }
1189 if (i != 0)
1190 f &= ~ECONV_AFTER_OUTPUT;
1191 iold = *ipp;
1192 oold = *opp;
1193 te->last_result = res = rb_transcoding_convert(te->tc, ipp, is, opp, os, f);
1194 if (iold != *ipp || oold != *opp)
1195 try = 1;
1196
1197 switch (res) {
1201 case econv_after_output:
1202 return i;
1203
1206 break;
1207
1208 case econv_finished:
1209 ec->num_finished = i+1;
1210 break;
1211 }
1212 }
1213 }
1214 return -1;
1215}
1216
1217static rb_econv_result_t
1218rb_trans_conv(rb_econv_t *ec,
1219 const unsigned char **input_ptr, const unsigned char *input_stop,
1220 unsigned char **output_ptr, unsigned char *output_stop,
1221 int flags,
1222 int *result_position_ptr)
1223{
1224 int i;
1225 int needreport_index;
1226 int sweep_start;
1227
1228 unsigned char empty_buf;
1229 unsigned char *empty_ptr = &empty_buf;
1230
1231 if (!input_ptr) {
1232 input_ptr = (const unsigned char **)&empty_ptr;
1233 input_stop = empty_ptr;
1234 }
1235
1236 if (!output_ptr) {
1237 output_ptr = &empty_ptr;
1238 output_stop = empty_ptr;
1239 }
1240
1241 if (ec->elems[0].last_result == econv_after_output)
1242 ec->elems[0].last_result = econv_source_buffer_empty;
1243
1244 for (i = ec->num_trans-1; 0 <= i; i--) {
1245 switch (ec->elems[i].last_result) {
1249 case econv_after_output:
1250 case econv_finished:
1251 sweep_start = i+1;
1252 goto found_needreport;
1253
1256 break;
1257
1258 default:
1259 rb_bug("unexpected transcode last result");
1260 }
1261 }
1262
1263 /* /^[sd]+$/ is confirmed. but actually /^s*d*$/. */
1264
1265 if (ec->elems[ec->num_trans-1].last_result == econv_destination_buffer_full &&
1266 (flags & ECONV_AFTER_OUTPUT)) {
1268
1269 res = rb_trans_conv(ec, NULL, NULL, output_ptr, output_stop,
1271 result_position_ptr);
1272
1273 if (res == econv_source_buffer_empty)
1274 return econv_after_output;
1275 return res;
1276 }
1277
1278 sweep_start = 0;
1279
1280 found_needreport:
1281
1282 do {
1283 needreport_index = trans_sweep(ec, input_ptr, input_stop, output_ptr, output_stop, flags, sweep_start);
1284 sweep_start = needreport_index + 1;
1285 } while (needreport_index != -1 && needreport_index != ec->num_trans-1);
1286
1287 for (i = ec->num_trans-1; 0 <= i; i--) {
1288 if (ec->elems[i].last_result != econv_source_buffer_empty) {
1289 rb_econv_result_t res = ec->elems[i].last_result;
1290 if (res == econv_invalid_byte_sequence ||
1291 res == econv_incomplete_input ||
1293 res == econv_after_output) {
1294 ec->elems[i].last_result = econv_source_buffer_empty;
1295 }
1296 if (result_position_ptr)
1297 *result_position_ptr = i;
1298 return res;
1299 }
1300 }
1301 if (result_position_ptr)
1302 *result_position_ptr = -1;
1304}
1305
1306static rb_econv_result_t
1307rb_econv_convert0(rb_econv_t *ec,
1308 const unsigned char **input_ptr, const unsigned char *input_stop,
1309 unsigned char **output_ptr, unsigned char *output_stop,
1310 int flags)
1311{
1313 int result_position;
1314 int has_output = 0;
1315
1316 memset(&ec->last_error, 0, sizeof(ec->last_error));
1317
1318 if (ec->num_trans == 0) {
1319 size_t len;
1320 if (ec->in_buf_start && ec->in_data_start != ec->in_data_end) {
1321 if (output_stop - *output_ptr < ec->in_data_end - ec->in_data_start) {
1322 len = output_stop - *output_ptr;
1323 memcpy(*output_ptr, ec->in_data_start, len);
1324 *output_ptr = output_stop;
1325 ec->in_data_start += len;
1327 goto gotresult;
1328 }
1329 len = ec->in_data_end - ec->in_data_start;
1330 memcpy(*output_ptr, ec->in_data_start, len);
1331 *output_ptr += len;
1332 ec->in_data_start = ec->in_data_end = ec->in_buf_start;
1333 if (flags & ECONV_AFTER_OUTPUT) {
1334 res = econv_after_output;
1335 goto gotresult;
1336 }
1337 }
1338 if (output_stop - *output_ptr < input_stop - *input_ptr) {
1339 len = output_stop - *output_ptr;
1340 }
1341 else {
1342 len = input_stop - *input_ptr;
1343 }
1344 if (0 < len && (flags & ECONV_AFTER_OUTPUT)) {
1345 *(*output_ptr)++ = *(*input_ptr)++;
1346 res = econv_after_output;
1347 goto gotresult;
1348 }
1349 memcpy(*output_ptr, *input_ptr, len);
1350 *output_ptr += len;
1351 *input_ptr += len;
1352 if (*input_ptr != input_stop)
1354 else if (flags & ECONV_PARTIAL_INPUT)
1356 else
1357 res = econv_finished;
1358 goto gotresult;
1359 }
1360
1361 if (ec->elems[ec->num_trans-1].out_data_start) {
1362 unsigned char *data_start = ec->elems[ec->num_trans-1].out_data_start;
1363 unsigned char *data_end = ec->elems[ec->num_trans-1].out_data_end;
1364 if (data_start != data_end) {
1365 size_t len;
1366 if (output_stop - *output_ptr < data_end - data_start) {
1367 len = output_stop - *output_ptr;
1368 memcpy(*output_ptr, data_start, len);
1369 *output_ptr = output_stop;
1370 ec->elems[ec->num_trans-1].out_data_start += len;
1372 goto gotresult;
1373 }
1374 len = data_end - data_start;
1375 memcpy(*output_ptr, data_start, len);
1376 *output_ptr += len;
1377 ec->elems[ec->num_trans-1].out_data_start =
1378 ec->elems[ec->num_trans-1].out_data_end =
1379 ec->elems[ec->num_trans-1].out_buf_start;
1380 has_output = 1;
1381 }
1382 }
1383
1384 if (ec->in_buf_start &&
1385 ec->in_data_start != ec->in_data_end) {
1386 res = rb_trans_conv(ec, (const unsigned char **)&ec->in_data_start, ec->in_data_end, output_ptr, output_stop,
1387 (flags&~ECONV_AFTER_OUTPUT)|ECONV_PARTIAL_INPUT, &result_position);
1388 if (res != econv_source_buffer_empty)
1389 goto gotresult;
1390 }
1391
1392 if (has_output &&
1393 (flags & ECONV_AFTER_OUTPUT) &&
1394 *input_ptr != input_stop) {
1395 input_stop = *input_ptr;
1396 res = rb_trans_conv(ec, input_ptr, input_stop, output_ptr, output_stop, flags, &result_position);
1397 if (res == econv_source_buffer_empty)
1398 res = econv_after_output;
1399 }
1400 else if ((flags & ECONV_AFTER_OUTPUT) ||
1401 ec->num_trans == 1) {
1402 res = rb_trans_conv(ec, input_ptr, input_stop, output_ptr, output_stop, flags, &result_position);
1403 }
1404 else {
1405 flags |= ECONV_AFTER_OUTPUT;
1406 do {
1407 res = rb_trans_conv(ec, input_ptr, input_stop, output_ptr, output_stop, flags, &result_position);
1408 } while (res == econv_after_output);
1409 }
1410
1411 gotresult:
1412 ec->last_error.result = res;
1413 if (res == econv_invalid_byte_sequence ||
1414 res == econv_incomplete_input ||
1416 rb_transcoding *error_tc = ec->elems[result_position].tc;
1417 ec->last_error.error_tc = error_tc;
1418 ec->last_error.source_encoding = error_tc->transcoder->src_encoding;
1419 ec->last_error.destination_encoding = error_tc->transcoder->dst_encoding;
1420 ec->last_error.error_bytes_start = TRANSCODING_READBUF(error_tc);
1421 ec->last_error.error_bytes_len = error_tc->recognized_len;
1422 ec->last_error.readagain_len = error_tc->readagain_len;
1423 }
1424
1425 return res;
1426}
1427
1428static int output_replacement_character(rb_econv_t *ec);
1429
1430static int
1431output_hex_charref(rb_econv_t *ec)
1432{
1433 int ret;
1434 unsigned char utfbuf[1024];
1435 const unsigned char *utf;
1436 size_t utf_len, utf_bufsize;
1437 int utf_allocated = 0;
1438 char charef_buf[16];
1439 const unsigned char *p;
1440
1441 if (encoding_equal(ec->last_error.source_encoding, "UTF-32BE")) {
1442 utf = ec->last_error.error_bytes_start;
1443 utf_len = ec->last_error.error_bytes_len;
1444 }
1445 else {
1446 utf = allocate_converted_string(ec->last_error.source_encoding, "UTF-32BE",
1447 ec->last_error.error_bytes_start, ec->last_error.error_bytes_len,
1448 utfbuf, sizeof(utfbuf),
1449 &utf_len, &utf_bufsize);
1450 if (!utf)
1451 return -1;
1452 if (utf != utfbuf && utf != ec->last_error.error_bytes_start)
1453 utf_allocated = 1;
1454 }
1455
1456 if (utf_len % 4 != 0)
1457 goto fail;
1458
1459 p = utf;
1460 while (4 <= utf_len) {
1461 unsigned int u = 0;
1462 u += p[0] << 24;
1463 u += p[1] << 16;
1464 u += p[2] << 8;
1465 u += p[3];
1466 snprintf(charef_buf, sizeof(charef_buf), "&#x%X;", u);
1467
1468 ret = rb_econv_insert_output(ec, (unsigned char *)charef_buf, strlen(charef_buf), "US-ASCII");
1469 if (ret == -1)
1470 goto fail;
1471
1472 p += 4;
1473 utf_len -= 4;
1474 }
1475
1476 if (utf_allocated)
1477 ruby_xfree_sized((void *)utf, utf_bufsize);
1478 return 0;
1479
1480 fail:
1481 if (utf_allocated)
1482 ruby_xfree_sized((void *)utf, utf_bufsize);
1483 return -1;
1484}
1485
1488 const unsigned char **input_ptr, const unsigned char *input_stop,
1489 unsigned char **output_ptr, unsigned char *output_stop,
1490 int flags)
1491{
1493
1494 unsigned char empty_buf;
1495 unsigned char *empty_ptr = &empty_buf;
1496
1497 ec->started = 1;
1498
1499 if (!input_ptr) {
1500 input_ptr = (const unsigned char **)&empty_ptr;
1501 input_stop = empty_ptr;
1502 }
1503
1504 if (!output_ptr) {
1505 output_ptr = &empty_ptr;
1506 output_stop = empty_ptr;
1507 }
1508
1509 resume:
1510 ret = rb_econv_convert0(ec, input_ptr, input_stop, output_ptr, output_stop, flags);
1511
1512 if (ret == econv_invalid_byte_sequence ||
1513 ret == econv_incomplete_input) {
1514 /* deal with invalid byte sequence */
1515 /* todo: add more alternative behaviors */
1516 switch (ec->flags & ECONV_INVALID_MASK) {
1518 if (output_replacement_character(ec) == 0)
1519 goto resume;
1520 }
1521 }
1522
1523 if (ret == econv_undefined_conversion) {
1524 /* valid character in source encoding
1525 * but no related character(s) in destination encoding */
1526 /* todo: add more alternative behaviors */
1527 switch (ec->flags & ECONV_UNDEF_MASK) {
1529 if (output_replacement_character(ec) == 0)
1530 goto resume;
1531 break;
1532
1534 if (output_hex_charref(ec) == 0)
1535 goto resume;
1536 break;
1537 }
1538 }
1539
1540 return ret;
1541}
1542
1543const char *
1545{
1546 rb_transcoding *tc = ec->last_tc;
1547 const rb_transcoder *tr;
1548
1549 if (tc == NULL)
1550 return "";
1551
1552 tr = tc->transcoder;
1553
1554 if (tr->asciicompat_type == asciicompat_encoder)
1555 return tr->src_encoding;
1556 return tr->dst_encoding;
1557}
1558
1559static unsigned char *
1560allocate_converted_string(const char *sname, const char *dname,
1561 const unsigned char *str, size_t len,
1562 unsigned char *caller_dst_buf, size_t caller_dst_bufsize,
1563 size_t *dst_len_ptr, size_t *dst_bufsize_ptr)
1564{
1565 unsigned char *dst_str;
1566 size_t dst_len;
1567 size_t dst_bufsize;
1568
1569 rb_econv_t *ec;
1571
1572 const unsigned char *sp;
1573 unsigned char *dp;
1574
1575 if (caller_dst_buf)
1576 dst_bufsize = caller_dst_bufsize;
1577 else if (len == 0)
1578 dst_bufsize = 1;
1579 else
1580 dst_bufsize = len;
1581
1582 ec = rb_econv_open(sname, dname, 0);
1583 if (ec == NULL)
1584 return NULL;
1585 if (caller_dst_buf)
1586 dst_str = caller_dst_buf;
1587 else
1588 dst_str = xmalloc(dst_bufsize);
1589 dst_len = 0;
1590 sp = str;
1591 dp = dst_str+dst_len;
1592 res = rb_econv_convert(ec, &sp, str+len, &dp, dst_str+dst_bufsize, 0);
1593 dst_len = dp - dst_str;
1594 while (res == econv_destination_buffer_full) {
1595 if (SIZE_MAX/2 < dst_bufsize) {
1596 goto fail;
1597 }
1598 dst_bufsize *= 2;
1599 if (dst_str == caller_dst_buf) {
1600 unsigned char *tmp;
1601 tmp = xmalloc(dst_bufsize);
1602 memcpy(tmp, dst_str, dst_bufsize/2);
1603 dst_str = tmp;
1604 }
1605 else {
1606 dst_str = ruby_xrealloc_sized(dst_str, dst_bufsize, dst_bufsize / 2);
1607 }
1608 dp = dst_str+dst_len;
1609 res = rb_econv_convert(ec, &sp, str+len, &dp, dst_str+dst_bufsize, 0);
1610 dst_len = dp - dst_str;
1611 }
1612 if (res != econv_finished) {
1613 goto fail;
1614 }
1615 rb_econv_close(ec);
1616 *dst_len_ptr = dst_len;
1617 *dst_bufsize_ptr = dst_bufsize;
1618 return dst_str;
1619
1620 fail:
1621 if (dst_str != caller_dst_buf)
1622 ruby_xfree_sized(dst_str, dst_bufsize);
1623 rb_econv_close(ec);
1624 return NULL;
1625}
1626
1627/* result: 0:success -1:failure */
1628int
1630 const unsigned char *str, size_t len, const char *str_encoding)
1631{
1632 const char *insert_encoding = rb_econv_encoding_to_insert_output(ec);
1633 unsigned char insert_buf[4096];
1634 const unsigned char *insert_str = NULL;
1635 size_t insert_len, insert_bufsize;
1636
1637 int last_trans_index;
1638 rb_transcoding *tc;
1639
1640 unsigned char **buf_start_p;
1641 unsigned char **data_start_p;
1642 unsigned char **data_end_p;
1643 unsigned char **buf_end_p;
1644
1645 size_t need;
1646
1647 ec->started = 1;
1648
1649 if (len == 0)
1650 return 0;
1651
1652 if (encoding_equal(insert_encoding, str_encoding)) {
1653 insert_str = str;
1654 insert_len = len;
1655 }
1656 else {
1657 insert_str = allocate_converted_string(str_encoding, insert_encoding,
1658 str, len, insert_buf, sizeof(insert_buf), &insert_len, &insert_bufsize);
1659 if (insert_str == NULL)
1660 return -1;
1661 }
1662
1663 need = insert_len;
1664
1665 last_trans_index = ec->num_trans-1;
1666 if (ec->num_trans == 0) {
1667 tc = NULL;
1668 buf_start_p = &ec->in_buf_start;
1669 data_start_p = &ec->in_data_start;
1670 data_end_p = &ec->in_data_end;
1671 buf_end_p = &ec->in_buf_end;
1672 }
1673 else if (ec->elems[last_trans_index].tc->transcoder->asciicompat_type == asciicompat_encoder) {
1674 tc = ec->elems[last_trans_index].tc;
1675 need += tc->readagain_len;
1676 if (need < insert_len)
1677 goto fail;
1678 if (last_trans_index == 0) {
1679 buf_start_p = &ec->in_buf_start;
1680 data_start_p = &ec->in_data_start;
1681 data_end_p = &ec->in_data_end;
1682 buf_end_p = &ec->in_buf_end;
1683 }
1684 else {
1685 rb_econv_elem_t *ee = &ec->elems[last_trans_index-1];
1686 buf_start_p = &ee->out_buf_start;
1687 data_start_p = &ee->out_data_start;
1688 data_end_p = &ee->out_data_end;
1689 buf_end_p = &ee->out_buf_end;
1690 }
1691 }
1692 else {
1693 rb_econv_elem_t *ee = &ec->elems[last_trans_index];
1694 buf_start_p = &ee->out_buf_start;
1695 data_start_p = &ee->out_data_start;
1696 data_end_p = &ee->out_data_end;
1697 buf_end_p = &ee->out_buf_end;
1698 tc = ec->elems[last_trans_index].tc;
1699 }
1700
1701 if (*buf_start_p == NULL) {
1702 unsigned char *buf = xmalloc(need);
1703 *buf_start_p = buf;
1704 *data_start_p = buf;
1705 *data_end_p = buf;
1706 *buf_end_p = buf+need;
1707 }
1708 else if ((size_t)(*buf_end_p - *data_end_p) < need) {
1709 MEMMOVE(*buf_start_p, *data_start_p, unsigned char, *data_end_p - *data_start_p);
1710 *data_end_p = *buf_start_p + (*data_end_p - *data_start_p);
1711 *data_start_p = *buf_start_p;
1712 if ((size_t)(*buf_end_p - *data_end_p) < need) {
1713 unsigned char *buf;
1714 size_t s = (*data_end_p - *buf_start_p) + need;
1715 if (s < need)
1716 goto fail;
1717 buf = ruby_xrealloc_sized(*buf_start_p, s, *buf_end_p - *buf_start_p);
1718 *data_start_p = buf;
1719 *data_end_p = buf + (*data_end_p - *buf_start_p);
1720 *buf_start_p = buf;
1721 *buf_end_p = buf + s;
1722 }
1723 }
1724
1725 memcpy(*data_end_p, insert_str, insert_len);
1726 *data_end_p += insert_len;
1727 if (tc && tc->transcoder->asciicompat_type == asciicompat_encoder) {
1728 memcpy(*data_end_p, TRANSCODING_READBUF(tc)+tc->recognized_len, tc->readagain_len);
1729 *data_end_p += tc->readagain_len;
1730 tc->readagain_len = 0;
1731 }
1732
1733 if (insert_str != str && insert_str != insert_buf)
1734 ruby_xfree_sized((void *)insert_str, insert_bufsize);
1735 return 0;
1736
1737 fail:
1738 if (insert_str != str && insert_str != insert_buf)
1739 ruby_xfree_sized((void *)insert_str, insert_bufsize);
1740 return -1;
1741}
1742
1743void
1745{
1746 int i;
1747
1748 if (ec->replacement_allocated) {
1749 SIZED_FREE_N((char *)ec->replacement_str, ec->replacement_bufsize);
1750 }
1751 for (i = 0; i < ec->num_trans; i++) {
1752 rb_transcoding_close(ec->elems[i].tc);
1753 ruby_xfree_sized(ec->elems[i].out_buf_start, ec->elems[i].out_buf_end - ec->elems[i].out_buf_start);
1754 }
1755 SIZED_FREE_N(ec->in_buf_start, ec->in_buf_end - ec->in_buf_start);
1756 SIZED_FREE_N(ec->elems, ec->num_allocated);
1757 SIZED_FREE(ec);
1758}
1759
1760size_t
1761rb_econv_memsize(rb_econv_t *ec)
1762{
1763 size_t size = sizeof(rb_econv_t);
1764 int i;
1765
1766 if (ec->replacement_allocated) {
1767 size += ec->replacement_len;
1768 }
1769 for (i = 0; i < ec->num_trans; i++) {
1770 size += rb_transcoding_memsize(ec->elems[i].tc);
1771
1772 if (ec->elems[i].out_buf_start) {
1773 size += ec->elems[i].out_buf_end - ec->elems[i].out_buf_start;
1774 }
1775 }
1776 size += ec->in_buf_end - ec->in_buf_start;
1777 size += sizeof(rb_econv_elem_t) * ec->num_allocated;
1778
1779 return size;
1780}
1781
1782int
1784{
1785 if (ec->num_trans == 0)
1786 return 0;
1787#if SIZEOF_SIZE_T > SIZEOF_INT
1788 if (ec->elems[0].tc->readagain_len > INT_MAX) return INT_MAX;
1789#endif
1790 return (int)ec->elems[0].tc->readagain_len;
1791}
1792
1793void
1794rb_econv_putback(rb_econv_t *ec, unsigned char *p, int n)
1795{
1796 rb_transcoding *tc;
1797 if (ec->num_trans == 0 || n == 0)
1798 return;
1799 tc = ec->elems[0].tc;
1800 memcpy(p, TRANSCODING_READBUF(tc) + tc->recognized_len + tc->readagain_len - n, n);
1801 tc->readagain_len -= n;
1802}
1803
1805 const char *ascii_compat_name;
1806 const char *ascii_incompat_name;
1807};
1808
1809static int
1810asciicompat_encoding_i(st_data_t key, st_data_t val, st_data_t arg)
1811{
1812 struct asciicompat_encoding_t *data = (struct asciicompat_encoding_t *)arg;
1813 transcoder_entry_t *entry = (transcoder_entry_t *)val;
1814 const rb_transcoder *tr;
1815
1816 if (DECORATOR_P(entry->sname, entry->dname))
1817 return ST_CONTINUE;
1818 tr = load_transcoder_entry(entry);
1819 if (tr && tr->asciicompat_type == asciicompat_decoder) {
1820 data->ascii_compat_name = tr->dst_encoding;
1821 return ST_STOP;
1822 }
1823 return ST_CONTINUE;
1824}
1825
1826const char *
1827rb_econv_asciicompat_encoding(const char *ascii_incompat_name)
1828{
1829 st_data_t v;
1830 st_table *table2;
1831 struct asciicompat_encoding_t data = {0};
1832
1833 unsigned int lev;
1834 RB_VM_LOCK_ENTER_LEV(&lev);
1835 {
1836 if (st_lookup(transcoder_table, (st_data_t)ascii_incompat_name, &v)) {
1837 table2 = (st_table *)v;
1838 /*
1839 * Assumption:
1840 * There is at most one transcoder for
1841 * converting from ASCII incompatible encoding.
1842 *
1843 * For ISO-2022-JP, there is ISO-2022-JP -> stateless-ISO-2022-JP and no others.
1844 */
1845 if (table2->num_entries == 1) {
1846 data.ascii_incompat_name = ascii_incompat_name;
1847 data.ascii_compat_name = NULL;
1848 if (rb_multi_ractor_p()) {
1849 /*
1850 * We need to unlock in case `load_transcoder_entry` actually loads the encoding
1851 * and table2 could be inserted into when we unlock.
1852 */
1853 st_table *dup_table2 = st_copy(table2);
1854 RB_VM_LOCK_LEAVE_LEV(&lev);
1855 st_foreach(dup_table2, asciicompat_encoding_i, (st_data_t)&data);
1856 st_free_table(dup_table2);
1857 RB_VM_LOCK_ENTER_LEV(&lev);
1858 }
1859 else {
1860 st_foreach(table2, asciicompat_encoding_i, (st_data_t)&data);
1861 }
1862 }
1863
1864 }
1865 }
1866 RB_VM_LOCK_LEAVE_LEV(&lev);
1867
1868 return data.ascii_compat_name; // can be NULL
1869}
1870
1871/*
1872 * Append `len` bytes pointed by `ss` to `dst` with converting with `ec`.
1873 *
1874 * If the result of the conversion is not compatible with the encoding of
1875 * `dst`, `dst` may not be valid encoding.
1876 */
1877VALUE
1878rb_econv_append(rb_econv_t *ec, const char *ss, long len, VALUE dst, int flags)
1879{
1880 unsigned const char *sp, *se;
1881 unsigned char *ds, *dp, *de;
1883 int max_output;
1884 enum ruby_coderange_type coderange;
1885 rb_encoding *dst_enc = ec->destination_encoding;
1886
1887 if (NIL_P(dst)) {
1888 dst = rb_str_buf_new(len);
1889 if (dst_enc) {
1890 rb_enc_associate(dst, dst_enc);
1891 }
1892 coderange = ENC_CODERANGE_7BIT; // scan from the start
1893 }
1894 else {
1895 dst_enc = rb_enc_get(dst);
1896 coderange = rb_enc_str_coderange(dst);
1897 }
1898
1899 if (ec->last_tc)
1900 max_output = ec->last_tc->transcoder->max_output;
1901 else
1902 max_output = 1;
1903
1904 do {
1905 int cr;
1906 long dlen = RSTRING_LEN(dst);
1907 if (rb_str_capacity(dst) - dlen < (size_t)len + max_output) {
1908 unsigned long new_capa = (unsigned long)dlen + len + max_output;
1909 if (LONG_MAX < new_capa)
1910 rb_raise(rb_eArgError, "too long string");
1911 rb_str_modify_expand(dst, new_capa - dlen);
1912 }
1913 sp = (const unsigned char *)ss;
1914 se = sp + len;
1915 ds = (unsigned char *)RSTRING_PTR(dst);
1916 de = ds + rb_str_capacity(dst);
1917 dp = ds += dlen;
1918 res = rb_econv_convert(ec, &sp, se, &dp, de, flags);
1919 switch (coderange) {
1920 case ENC_CODERANGE_7BIT:
1922 cr = (int)coderange;
1923 rb_str_coderange_scan_restartable((char *)ds, (char *)dp, dst_enc, &cr);
1924 coderange = cr;
1925 ENC_CODERANGE_SET(dst, coderange);
1926 break;
1929 break;
1930 }
1931 len -= (const char *)sp - ss;
1932 ss = (const char *)sp;
1933 rb_str_set_len(dst, dlen + (dp - ds));
1935 } while (res == econv_destination_buffer_full);
1936
1937 return dst;
1938}
1939
1940VALUE
1941rb_econv_substr_append(rb_econv_t *ec, VALUE src, long off, long len, VALUE dst, int flags)
1942{
1943 src = rb_str_new_frozen(src);
1944 dst = rb_econv_append(ec, RSTRING_PTR(src) + off, len, dst, flags);
1945 RB_GC_GUARD(src);
1946 return dst;
1947}
1948
1949VALUE
1951{
1952 return rb_econv_substr_append(ec, src, 0, RSTRING_LEN(src), dst, flags);
1953}
1954
1955VALUE
1956rb_econv_substr_convert(rb_econv_t *ec, VALUE src, long byteoff, long bytesize, int flags)
1957{
1958 return rb_econv_substr_append(ec, src, byteoff, bytesize, Qnil, flags);
1959}
1960
1961VALUE
1963{
1964 return rb_econv_substr_append(ec, src, 0, RSTRING_LEN(src), Qnil, flags);
1965}
1966
1967static int
1968rb_econv_add_converter(rb_econv_t *ec, const char *sname, const char *dname, int n)
1969{
1970 transcoder_entry_t *entry;
1971 const rb_transcoder *tr = NULL;
1972
1973 if (ec->started != 0)
1974 return -1;
1975
1976 entry = get_transcoder_entry(sname, dname);
1977 if (entry) {
1978 tr = load_transcoder_entry(entry);
1979 }
1980
1981 return tr ? rb_econv_add_transcoder_at(ec, tr, n) : -1;
1982}
1983
1984static int
1985rb_econv_decorate_at(rb_econv_t *ec, const char *decorator_name, int n)
1986{
1987 return rb_econv_add_converter(ec, "", decorator_name, n);
1988}
1989
1990int
1991rb_econv_decorate_at_first(rb_econv_t *ec, const char *decorator_name)
1992{
1993 const rb_transcoder *tr;
1994
1995 if (ec->num_trans == 0)
1996 return rb_econv_decorate_at(ec, decorator_name, 0);
1997
1998 tr = ec->elems[0].tc->transcoder;
1999
2000 if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding) &&
2001 tr->asciicompat_type == asciicompat_decoder)
2002 return rb_econv_decorate_at(ec, decorator_name, 1);
2003
2004 return rb_econv_decorate_at(ec, decorator_name, 0);
2005}
2006
2007int
2008rb_econv_decorate_at_last(rb_econv_t *ec, const char *decorator_name)
2009{
2010 const rb_transcoder *tr;
2011
2012 if (ec->num_trans == 0)
2013 return rb_econv_decorate_at(ec, decorator_name, 0);
2014
2015 tr = ec->elems[ec->num_trans-1].tc->transcoder;
2016
2017 if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding) &&
2018 tr->asciicompat_type == asciicompat_encoder)
2019 return rb_econv_decorate_at(ec, decorator_name, ec->num_trans-1);
2020
2021 return rb_econv_decorate_at(ec, decorator_name, ec->num_trans);
2022}
2023
2024void
2026{
2027 const char *dname = 0;
2028
2029 switch (ec->flags & ECONV_NEWLINE_DECORATOR_MASK) {
2031 dname = "universal_newline";
2032 break;
2034 dname = "crlf_newline";
2035 break;
2037 dname = "cr_newline";
2038 break;
2040 dname = "lf_newline";
2041 break;
2042 }
2043
2044 if (dname) {
2045 const rb_transcoder *transcoder = get_transcoder_entry("", dname)->transcoder;
2046 int num_trans = ec->num_trans;
2047 int i, j = 0;
2048
2049 for (i=0; i < num_trans; i++) {
2050 if (transcoder == ec->elems[i].tc->transcoder) {
2051 rb_transcoding_close(ec->elems[i].tc);
2052 ruby_xfree_sized(ec->elems[i].out_buf_start, ec->elems[i].out_buf_end - ec->elems[i].out_buf_start);
2053 ec->num_trans--;
2054 }
2055 else
2056 ec->elems[j++] = ec->elems[i];
2057 }
2058 }
2059
2060 ec->flags &= ~ECONV_NEWLINE_DECORATOR_MASK;
2061}
2062
2063static VALUE
2064econv_description(const char *sname, const char *dname, int ecflags, VALUE mesg)
2065{
2066 int has_description = 0;
2067
2068 if (NIL_P(mesg))
2069 mesg = rb_str_new(NULL, 0);
2070
2071 if (*sname != '\0' || *dname != '\0') {
2072 if (*sname == '\0')
2073 rb_str_cat2(mesg, dname);
2074 else if (*dname == '\0')
2075 rb_str_cat2(mesg, sname);
2076 else
2077 rb_str_catf(mesg, "%s to %s", sname, dname);
2078 has_description = 1;
2079 }
2080
2081 if (ecflags & (ECONV_NEWLINE_DECORATOR_MASK|
2085 const char *pre = "";
2086 if (has_description)
2087 rb_str_cat2(mesg, " with ");
2088 if (ecflags & ECONV_UNIVERSAL_NEWLINE_DECORATOR) {
2089 rb_str_cat2(mesg, pre); pre = ",";
2090 rb_str_cat2(mesg, "universal_newline");
2091 }
2092 if (ecflags & ECONV_CRLF_NEWLINE_DECORATOR) {
2093 rb_str_cat2(mesg, pre); pre = ",";
2094 rb_str_cat2(mesg, "crlf_newline");
2095 }
2096 if (ecflags & ECONV_CR_NEWLINE_DECORATOR) {
2097 rb_str_cat2(mesg, pre); pre = ",";
2098 rb_str_cat2(mesg, "cr_newline");
2099 }
2100 if (ecflags & ECONV_LF_NEWLINE_DECORATOR) {
2101 rb_str_cat2(mesg, pre); pre = ",";
2102 rb_str_cat2(mesg, "lf_newline");
2103 }
2104 if (ecflags & ECONV_XML_TEXT_DECORATOR) {
2105 rb_str_cat2(mesg, pre); pre = ",";
2106 rb_str_cat2(mesg, "xml_text");
2107 }
2108 if (ecflags & ECONV_XML_ATTR_CONTENT_DECORATOR) {
2109 rb_str_cat2(mesg, pre); pre = ",";
2110 rb_str_cat2(mesg, "xml_attr_content");
2111 }
2112 if (ecflags & ECONV_XML_ATTR_QUOTE_DECORATOR) {
2113 rb_str_cat2(mesg, pre); pre = ",";
2114 rb_str_cat2(mesg, "xml_attr_quote");
2115 }
2116 has_description = 1;
2117 }
2118 if (!has_description) {
2119 rb_str_cat2(mesg, "no-conversion");
2120 }
2121
2122 return mesg;
2123}
2124
2125VALUE
2126rb_econv_open_exc(const char *sname, const char *dname, int ecflags)
2127{
2128 VALUE mesg, exc;
2129 mesg = rb_str_new_cstr("code converter not found (");
2130 econv_description(sname, dname, ecflags, mesg);
2131 rb_str_cat2(mesg, ")");
2132 exc = rb_exc_new3(rb_eConverterNotFoundError, mesg);
2133 return exc;
2134}
2135
2136static VALUE
2137make_econv_exception(rb_econv_t *ec)
2138{
2139 VALUE mesg, exc;
2140 if (ec->last_error.result == econv_invalid_byte_sequence ||
2141 ec->last_error.result == econv_incomplete_input) {
2142 const char *err = (const char *)ec->last_error.error_bytes_start;
2143 size_t error_len = ec->last_error.error_bytes_len;
2144 VALUE bytes = rb_str_new(err, error_len);
2145 VALUE dumped = rb_str_dump(bytes);
2146 size_t readagain_len = ec->last_error.readagain_len;
2147 VALUE bytes2 = Qnil;
2148 VALUE dumped2;
2149 if (ec->last_error.result == econv_incomplete_input) {
2150 mesg = rb_sprintf("incomplete %s on %s",
2151 StringValueCStr(dumped),
2152 ec->last_error.source_encoding);
2153 }
2154 else if (readagain_len) {
2155 bytes2 = rb_str_new(err+error_len, readagain_len);
2156 dumped2 = rb_str_dump(bytes2);
2157 mesg = rb_sprintf("%s followed by %s on %s",
2158 StringValueCStr(dumped),
2159 StringValueCStr(dumped2),
2160 ec->last_error.source_encoding);
2161 }
2162 else {
2163 mesg = rb_sprintf("%s on %s",
2164 StringValueCStr(dumped),
2165 ec->last_error.source_encoding);
2166 }
2167
2168 exc = rb_exc_new3(rb_eInvalidByteSequenceError, mesg);
2169 rb_ivar_set(exc, id_error_bytes, bytes);
2170 rb_ivar_set(exc, id_readagain_bytes, bytes2);
2171 rb_ivar_set(exc, id_incomplete_input, RBOOL(ec->last_error.result == econv_incomplete_input));
2172 goto set_encs;
2173 }
2174 if (ec->last_error.result == econv_undefined_conversion) {
2175 VALUE bytes = rb_str_new((const char *)ec->last_error.error_bytes_start,
2176 ec->last_error.error_bytes_len);
2177 VALUE dumped = Qnil;
2178 int idx;
2179 if (strcmp(ec->last_error.source_encoding, "UTF-8") == 0) {
2180 rb_encoding *utf8 = rb_utf8_encoding();
2181 const char *start, *end;
2182 int n;
2183 start = (const char *)ec->last_error.error_bytes_start;
2184 end = start + ec->last_error.error_bytes_len;
2185 n = rb_enc_precise_mbclen(start, end, utf8);
2186 if (MBCLEN_CHARFOUND_P(n) &&
2187 (size_t)MBCLEN_CHARFOUND_LEN(n) == ec->last_error.error_bytes_len) {
2188 unsigned int cc = rb_enc_mbc_to_codepoint(start, end, utf8);
2189 dumped = rb_sprintf("U+%04X", cc);
2190 }
2191 }
2192 if (NIL_P(dumped))
2193 dumped = rb_str_dump(bytes);
2194 if (strcmp(ec->last_error.source_encoding,
2195 ec->source_encoding_name) == 0 &&
2196 strcmp(ec->last_error.destination_encoding,
2197 ec->destination_encoding_name) == 0) {
2198 mesg = rb_sprintf("%s from %s to %s",
2199 StringValueCStr(dumped),
2200 ec->last_error.source_encoding,
2201 ec->last_error.destination_encoding);
2202 }
2203 else {
2204 int i;
2205 mesg = rb_sprintf("%s to %s in conversion from %s",
2206 StringValueCStr(dumped),
2207 ec->last_error.destination_encoding,
2208 ec->source_encoding_name);
2209 for (i = 0; i < ec->num_trans; i++) {
2210 const rb_transcoder *tr = ec->elems[i].tc->transcoder;
2211 if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding))
2212 rb_str_catf(mesg, " to %s",
2213 ec->elems[i].tc->transcoder->dst_encoding);
2214 }
2215 }
2216 exc = rb_exc_new3(rb_eUndefinedConversionError, mesg);
2217 idx = rb_enc_find_index(ec->last_error.source_encoding);
2218 if (0 <= idx)
2219 rb_enc_associate_index(bytes, idx);
2220 rb_ivar_set(exc, id_error_char, bytes);
2221 goto set_encs;
2222 }
2223 return Qnil;
2224
2225 set_encs:
2226 rb_ivar_set(exc, id_source_encoding_name, rb_str_new2(ec->last_error.source_encoding));
2227 rb_ivar_set(exc, id_destination_encoding_name, rb_str_new2(ec->last_error.destination_encoding));
2228 int idx = rb_enc_find_index(ec->last_error.source_encoding);
2229 if (0 <= idx)
2230 rb_ivar_set(exc, id_source_encoding, rb_enc_from_encoding(rb_enc_from_index(idx)));
2231 idx = rb_enc_find_index(ec->last_error.destination_encoding);
2232 if (0 <= idx)
2233 rb_ivar_set(exc, id_destination_encoding, rb_enc_from_encoding(rb_enc_from_index(idx)));
2234 return exc;
2235}
2236
2237static void
2238more_output_buffer(
2239 VALUE destination,
2240 unsigned char *(*resize_destination)(VALUE, size_t, size_t),
2241 int max_output,
2242 unsigned char **out_start_ptr,
2243 unsigned char **out_pos,
2244 unsigned char **out_stop_ptr)
2245{
2246 size_t len = (*out_pos - *out_start_ptr);
2247 size_t new_len = (len + max_output) * 2;
2248 *out_start_ptr = resize_destination(destination, len, new_len);
2249 *out_pos = *out_start_ptr + len;
2250 *out_stop_ptr = *out_start_ptr + new_len;
2251}
2252
2253static int
2254make_replacement(rb_econv_t *ec)
2255{
2256 rb_transcoding *tc;
2257 const rb_transcoder *tr;
2258 const unsigned char *replacement;
2259 const char *repl_enc;
2260 const char *ins_enc;
2261 size_t len;
2262
2263 if (ec->replacement_str)
2264 return 0;
2265
2267
2268 tc = ec->last_tc;
2269 if (*ins_enc) {
2270 tr = tc->transcoder;
2271 rb_enc_find(tr->dst_encoding);
2272 replacement = (const unsigned char *)get_replacement_character(ins_enc, &len, &repl_enc);
2273 }
2274 else {
2275 replacement = (unsigned char *)"?";
2276 len = 1;
2277 repl_enc = "";
2278 }
2279
2280 ec->replacement_str = replacement;
2281 ec->replacement_len = len;
2282 ec->replacement_bufsize = len;
2283 ec->replacement_enc = repl_enc;
2284 ec->replacement_allocated = 0;
2285 return 0;
2286}
2287
2288int
2290 const unsigned char *str, size_t len, const char *encname)
2291{
2292 unsigned char *str2;
2293 size_t len2, buf_size2;
2294 const char *encname2;
2295
2297
2298 if (!*encname2 || encoding_equal(encname, encname2)) {
2299 str2 = xmalloc(len);
2300 MEMCPY(str2, str, unsigned char, len); /* xxx: str may be invalid */
2301 buf_size2 = len2 = len;
2302 encname2 = encname;
2303 }
2304 else {
2305 str2 = allocate_converted_string(encname, encname2, str, len, NULL, 0, &len2, &buf_size2);
2306 if (!str2)
2307 return -1;
2308 }
2309
2310 if (ec->replacement_allocated) {
2311 SIZED_FREE_N((char *)ec->replacement_str, ec->replacement_bufsize);
2312 }
2313 ec->replacement_allocated = 1;
2314 ec->replacement_str = str2;
2315 ec->replacement_len = len2;
2316 ec->replacement_bufsize = buf_size2;
2317 ec->replacement_enc = encname2;
2318 return 0;
2319}
2320
2321static int
2322output_replacement_character(rb_econv_t *ec)
2323{
2324 int ret;
2325
2326 if (make_replacement(ec) == -1)
2327 return -1;
2328
2329 ret = rb_econv_insert_output(ec, ec->replacement_str, ec->replacement_len, ec->replacement_enc);
2330 if (ret == -1)
2331 return -1;
2332
2333 return 0;
2334}
2335
2336#if 1
2337#define hash_fallback rb_hash_aref
2338
2339static VALUE
2340proc_fallback(VALUE fallback, VALUE c)
2341{
2342 return rb_proc_call(fallback, rb_ary_new4(1, &c));
2343}
2344
2345static VALUE
2346method_fallback(VALUE fallback, VALUE c)
2347{
2348 return rb_method_call(1, &c, fallback);
2349}
2350
2351static VALUE
2352aref_fallback(VALUE fallback, VALUE c)
2353{
2354 return rb_funcallv_public(fallback, idAREF, 1, &c);
2355}
2356
2358 VALUE (*fallback_func)(VALUE, VALUE);
2359 VALUE fallback;
2360 VALUE rep;
2361};
2362
2363static VALUE
2364transcode_loop_fallback_try(VALUE a)
2365{
2367
2368 VALUE ret = args->fallback_func(args->fallback, args->rep);
2369
2370 if (!UNDEF_P(ret) && !NIL_P(ret)) {
2371 StringValue(ret);
2372 }
2373
2374 return ret;
2375}
2376
2377static void
2378transcode_loop(const unsigned char **in_pos, unsigned char **out_pos,
2379 const unsigned char *in_stop, unsigned char *out_stop,
2380 VALUE destination,
2381 unsigned char *(*resize_destination)(VALUE, size_t, size_t),
2382 const char *src_encoding,
2383 const char *dst_encoding,
2384 int ecflags,
2385 VALUE ecopts,
2386 VALUE source)
2387{
2388 rb_econv_t *ec;
2389 rb_transcoding *last_tc;
2391 unsigned char *out_start = *out_pos;
2392 int max_output;
2393 VALUE exc;
2394 VALUE fallback = Qnil;
2395 VALUE (*fallback_func)(VALUE, VALUE) = 0;
2396 const unsigned char *source_start = *in_pos;
2397 long source_len = in_stop - *in_pos;
2398
2399 ec = rb_econv_open_opts(src_encoding, dst_encoding, ecflags, ecopts);
2400 if (!ec)
2401 rb_exc_raise(rb_econv_open_exc(src_encoding, dst_encoding, ecflags));
2402
2403 if (!NIL_P(ecopts) && RB_TYPE_P(ecopts, T_HASH)) {
2404 fallback = rb_hash_aref(ecopts, sym_fallback);
2405 if (RB_TYPE_P(fallback, T_HASH)) {
2406 fallback_func = hash_fallback;
2407 }
2408 else if (rb_obj_is_proc(fallback)) {
2409 fallback_func = proc_fallback;
2410 }
2411 else if (rb_obj_is_method(fallback)) {
2412 fallback_func = method_fallback;
2413 }
2414 else {
2415 fallback_func = aref_fallback;
2416 }
2417 }
2418 last_tc = ec->last_tc;
2419 max_output = last_tc ? last_tc->transcoder->max_output : 1;
2420
2421 resume:
2422 ret = rb_econv_convert(ec, in_pos, in_stop, out_pos, out_stop, 0);
2423
2424 if (!NIL_P(fallback) && ret == econv_undefined_conversion) {
2425 VALUE rep = rb_enc_str_new(
2426 (const char *)ec->last_error.error_bytes_start,
2427 ec->last_error.error_bytes_len,
2428 rb_enc_find(ec->last_error.source_encoding));
2429
2430
2431 struct transcode_loop_fallback_args args = {
2432 .fallback_func = fallback_func,
2433 .fallback = fallback,
2434 .rep = rep,
2435 };
2436
2437 int state;
2438 rep = rb_protect(transcode_loop_fallback_try, (VALUE)&args, &state);
2439 if (state) {
2440 rb_econv_close(ec);
2441 rb_jump_tag(state);
2442 }
2443
2444 /* Ruby code run during the conversion (e.g. the fallback) may have
2445 * modified the source string, invalidating the pointers into its
2446 * buffer. */
2447 if ((const unsigned char *)RSTRING_PTR(source) != source_start ||
2448 RSTRING_LEN(source) != source_len) {
2449 rb_econv_close(ec);
2450 rb_raise(rb_eRuntimeError, "string modified");
2451 }
2452
2453 if (!UNDEF_P(rep) && !NIL_P(rep)) {
2454 ret = rb_econv_insert_output(ec, (const unsigned char *)RSTRING_PTR(rep),
2455 RSTRING_LEN(rep), rb_enc_name(rb_enc_get(rep)));
2456 RB_GC_GUARD(rep); // insert_output may GC while reading rep's bytes
2457 if ((int)ret == -1) {
2458 rb_econv_close(ec);
2459 rb_raise(rb_eArgError, "too big fallback string");
2460 }
2461 goto resume;
2462 }
2463 }
2464
2465 if (ret == econv_invalid_byte_sequence ||
2466 ret == econv_incomplete_input ||
2468 exc = make_econv_exception(ec);
2469 rb_econv_close(ec);
2470 rb_exc_raise(exc);
2471 }
2472
2473 if (ret == econv_destination_buffer_full) {
2474 more_output_buffer(destination, resize_destination, max_output, &out_start, out_pos, &out_stop);
2475 goto resume;
2476 }
2477
2478 rb_econv_close(ec);
2479 return;
2480}
2481#else
2482/* sample transcode_loop implementation in byte-by-byte stream style */
2483static void
2484transcode_loop(const unsigned char **in_pos, unsigned char **out_pos,
2485 const unsigned char *in_stop, unsigned char *out_stop,
2486 VALUE destination,
2487 unsigned char *(*resize_destination)(VALUE, size_t, size_t),
2488 const char *src_encoding,
2489 const char *dst_encoding,
2490 int ecflags,
2491 VALUE ecopts,
2492 VALUE source)
2493{
2494 rb_econv_t *ec;
2495 rb_transcoding *last_tc;
2497 unsigned char *out_start = *out_pos;
2498 const unsigned char *ptr;
2499 int max_output;
2500 VALUE exc;
2501
2502 ec = rb_econv_open_opts(src_encoding, dst_encoding, ecflags, ecopts);
2503 if (!ec)
2504 rb_exc_raise(rb_econv_open_exc(src_encoding, dst_encoding, ecflags));
2505
2506 last_tc = ec->last_tc;
2507 max_output = last_tc ? last_tc->transcoder->max_output : 1;
2508
2510 ptr = *in_pos;
2511 while (ret != econv_finished) {
2512 unsigned char input_byte;
2513 const unsigned char *p = &input_byte;
2514
2515 if (ret == econv_source_buffer_empty) {
2516 if (ptr < in_stop) {
2517 input_byte = *ptr;
2518 ret = rb_econv_convert(ec, &p, p+1, out_pos, out_stop, ECONV_PARTIAL_INPUT);
2519 }
2520 else {
2521 ret = rb_econv_convert(ec, NULL, NULL, out_pos, out_stop, 0);
2522 }
2523 }
2524 else {
2525 ret = rb_econv_convert(ec, NULL, NULL, out_pos, out_stop, ECONV_PARTIAL_INPUT);
2526 }
2527 if (&input_byte != p)
2528 ptr += p - &input_byte;
2529 switch (ret) {
2533 exc = make_econv_exception(ec);
2534 rb_econv_close(ec);
2535 rb_exc_raise(exc);
2536 break;
2537
2539 more_output_buffer(destination, resize_destination, max_output, &out_start, out_pos, &out_stop);
2540 break;
2541
2543 break;
2544
2545 case econv_finished:
2546 break;
2547 }
2548 }
2549 rb_econv_close(ec);
2550 *in_pos = in_stop;
2551 return;
2552}
2553#endif
2554
2555
2556/*
2557 * String-specific code
2558 */
2559
2560static unsigned char *
2561str_transcoding_resize(VALUE destination, size_t len, size_t new_len)
2562{
2563 rb_str_resize(destination, new_len);
2564 return (unsigned char *)RSTRING_PTR(destination);
2565}
2566
2567static int
2568econv_opts(VALUE opt, int ecflags)
2569{
2570 VALUE v;
2571 int newlineflag = 0;
2572
2573 v = rb_hash_aref(opt, sym_invalid);
2574 if (NIL_P(v)) {
2575 }
2576 else if (v==sym_replace) {
2577 ecflags |= ECONV_INVALID_REPLACE;
2578 }
2579 else {
2580 rb_raise(rb_eArgError, "unknown value for invalid character option");
2581 }
2582
2583 v = rb_hash_aref(opt, sym_undef);
2584 if (NIL_P(v)) {
2585 }
2586 else if (v==sym_replace) {
2587 ecflags |= ECONV_UNDEF_REPLACE;
2588 }
2589 else {
2590 rb_raise(rb_eArgError, "unknown value for undefined character option");
2591 }
2592
2593 v = rb_hash_aref(opt, sym_replace);
2594 if (!NIL_P(v) && !(ecflags & ECONV_INVALID_REPLACE)) {
2595 ecflags |= ECONV_UNDEF_REPLACE;
2596 }
2597
2598 v = rb_hash_aref(opt, sym_xml);
2599 if (!NIL_P(v)) {
2600 if (v==sym_text) {
2602 }
2603 else if (v==sym_attr) {
2605 }
2606 else if (SYMBOL_P(v)) {
2607 rb_raise(rb_eArgError, "unexpected value for xml option: %"PRIsVALUE, rb_sym2str(v));
2608 }
2609 else {
2610 rb_raise(rb_eArgError, "unexpected value for xml option");
2611 }
2612 }
2613
2614#ifdef ENABLE_ECONV_NEWLINE_OPTION
2615 v = rb_hash_aref(opt, sym_newline);
2616 if (!NIL_P(v)) {
2617 newlineflag = 2;
2618 ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
2619 if (v == sym_universal) {
2621 }
2622 else if (v == sym_crlf) {
2624 }
2625 else if (v == sym_cr) {
2626 ecflags |= ECONV_CR_NEWLINE_DECORATOR;
2627 }
2628 else if (v == sym_lf) {
2629 ecflags |= ECONV_LF_NEWLINE_DECORATOR;
2630 }
2631 else if (SYMBOL_P(v)) {
2632 rb_raise(rb_eArgError, "unexpected value for newline option: %"PRIsVALUE,
2633 rb_sym2str(v));
2634 }
2635 else {
2636 rb_raise(rb_eArgError, "unexpected value for newline option");
2637 }
2638 }
2639#endif
2640 {
2641 int setflags = 0;
2642
2643 v = rb_hash_aref(opt, sym_universal_newline);
2644 if (RTEST(v))
2646 newlineflag |= !NIL_P(v);
2647
2648 v = rb_hash_aref(opt, sym_crlf_newline);
2649 if (RTEST(v))
2650 setflags |= ECONV_CRLF_NEWLINE_DECORATOR;
2651 newlineflag |= !NIL_P(v);
2652
2653 v = rb_hash_aref(opt, sym_cr_newline);
2654 if (RTEST(v))
2655 setflags |= ECONV_CR_NEWLINE_DECORATOR;
2656 newlineflag |= !NIL_P(v);
2657
2658 v = rb_hash_aref(opt, sym_lf_newline);
2659 if (RTEST(v))
2660 setflags |= ECONV_LF_NEWLINE_DECORATOR;
2661 newlineflag |= !NIL_P(v);
2662
2663 switch (newlineflag) {
2664 case 1:
2665 ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
2666 ecflags |= setflags;
2667 break;
2668
2669 case 3:
2670 rb_warning(":newline option precedes other newline options");
2671 break;
2672 }
2673 }
2674
2675 return ecflags;
2676}
2677
2678int
2679rb_econv_prepare_options(VALUE opthash, VALUE *opts, int ecflags)
2680{
2681 VALUE newhash = Qnil;
2682 VALUE v;
2683
2684 if (NIL_P(opthash)) {
2685 *opts = Qnil;
2686 return ecflags;
2687 }
2688 ecflags = econv_opts(opthash, ecflags);
2689
2690 v = rb_hash_aref(opthash, sym_replace);
2691 if (!NIL_P(v)) {
2692 StringValue(v);
2693 if (is_broken_string(v)) {
2694 VALUE dumped = rb_str_dump(v);
2695 rb_raise(rb_eArgError, "replacement string is broken: %s as %s",
2696 StringValueCStr(dumped),
2697 rb_enc_name(rb_enc_get(v)));
2698 }
2699 v = rb_str_new_frozen(v);
2700 newhash = rb_hash_new();
2701 rb_hash_aset(newhash, sym_replace, v);
2702 }
2703
2704 v = rb_hash_aref(opthash, sym_fallback);
2705 if (!NIL_P(v)) {
2706 VALUE h = rb_check_hash_type(v);
2707 if (NIL_P(h)
2708 ? (rb_obj_is_proc(v) || rb_obj_is_method(v) || rb_respond_to(v, idAREF))
2709 : (v = h, 1)) {
2710 if (NIL_P(newhash))
2711 newhash = rb_hash_new();
2712 rb_hash_aset(newhash, sym_fallback, v);
2713 }
2714 }
2715
2716 if (!NIL_P(newhash))
2717 rb_hash_freeze(newhash);
2718 *opts = newhash;
2719
2720 return ecflags;
2721}
2722
2723int
2725{
2726 return rb_econv_prepare_options(opthash, opts, 0);
2727}
2728
2729rb_econv_t *
2730rb_econv_open_opts(const char *source_encoding, const char *destination_encoding, int ecflags, VALUE opthash)
2731{
2732 rb_econv_t *ec;
2733 VALUE replacement;
2734
2735 if (NIL_P(opthash)) {
2736 replacement = Qnil;
2737 }
2738 else {
2739 if (!RB_TYPE_P(opthash, T_HASH) || !OBJ_FROZEN(opthash))
2740 rb_bug("rb_econv_open_opts called with invalid opthash");
2741 replacement = rb_hash_aref(opthash, sym_replace);
2742 }
2743
2744 ec = rb_econv_open(source_encoding, destination_encoding, ecflags);
2745 if (ec) {
2746 if (!NIL_P(replacement)) {
2747 int ret;
2748 rb_encoding *enc = rb_enc_get(replacement);
2749
2750 ret = rb_econv_set_replacement(ec,
2751 (const unsigned char *)RSTRING_PTR(replacement),
2752 RSTRING_LEN(replacement),
2753 rb_enc_name(enc));
2754 if (ret == -1) {
2755 rb_econv_close(ec);
2756 ec = NULL;
2757 }
2758 }
2759 }
2760 return ec; // can be NULL
2761}
2762
2763static int
2764enc_arg(VALUE *arg, const char **name_p, rb_encoding **enc_p)
2765{
2766 rb_encoding *enc;
2767 const char *n;
2768 int encidx;
2769 VALUE encval;
2770
2771 if (((encidx = rb_to_encoding_index(encval = *arg)) < 0) ||
2772 !(enc = rb_enc_from_index(encidx))) {
2773 enc = NULL;
2774 encidx = 0;
2775 n = StringValueCStr(*arg);
2776 }
2777 else {
2778 n = rb_enc_name(enc);
2779 }
2780
2781 *name_p = n;
2782 *enc_p = enc;
2783
2784 return encidx;
2785}
2786
2787static int
2788str_transcode_enc_args(VALUE str, VALUE *arg1, VALUE *arg2,
2789 const char **sname_p, rb_encoding **senc_p,
2790 const char **dname_p, rb_encoding **denc_p)
2791{
2792 rb_encoding *senc, *denc;
2793 const char *sname, *dname;
2794 int sencidx, dencidx;
2795
2796 dencidx = enc_arg(arg1, &dname, &denc);
2797
2798 if (NIL_P(*arg2)) {
2799 sencidx = rb_enc_get_index(str);
2800 senc = rb_enc_from_index(sencidx);
2801 sname = rb_enc_name(senc);
2802 }
2803 else {
2804 sencidx = enc_arg(arg2, &sname, &senc);
2805 }
2806
2807 *sname_p = sname;
2808 *senc_p = senc;
2809 *dname_p = dname;
2810 *denc_p = denc;
2811 return dencidx;
2812}
2813
2814static int
2815str_transcode0(int argc, VALUE *argv, VALUE *self, int ecflags, VALUE ecopts)
2816{
2817 VALUE dest;
2818 VALUE str = *self;
2819 VALUE arg1, arg2;
2820 long blen, slen;
2821 unsigned char *buf, *bp, *sp;
2822 const unsigned char *fromp;
2823 rb_encoding *senc, *denc;
2824 const char *sname, *dname;
2825 int dencidx;
2826 int explicitly_invalid_replace = TRUE;
2827
2828 rb_check_arity(argc, 0, 2);
2829
2830 if (argc == 0) {
2831 arg1 = rb_enc_default_internal();
2832 if (NIL_P(arg1)) {
2833 if (!ecflags) return -1;
2834 arg1 = rb_obj_encoding(str);
2835 }
2836 if (!(ecflags & ECONV_INVALID_MASK)) {
2837 explicitly_invalid_replace = FALSE;
2838 }
2840 }
2841 else {
2842 arg1 = argv[0];
2843 }
2844 arg2 = argc<=1 ? Qnil : argv[1];
2845 dencidx = str_transcode_enc_args(str, &arg1, &arg2, &sname, &senc, &dname, &denc);
2846
2847 if ((ecflags & (ECONV_NEWLINE_DECORATOR_MASK|
2851 if (senc && senc == denc) {
2852 if ((ecflags & ECONV_INVALID_MASK) && explicitly_invalid_replace) {
2853 VALUE rep = Qnil;
2854 if (!NIL_P(ecopts)) {
2855 rep = rb_hash_aref(ecopts, sym_replace);
2856 }
2857 dest = rb_enc_str_scrub(senc, str, rep);
2858 if (NIL_P(dest)) dest = str;
2859 *self = dest;
2860 return dencidx;
2861 }
2862 return NIL_P(arg2) ? -1 : dencidx;
2863 }
2864 if (senc && denc && rb_enc_asciicompat(senc) && rb_enc_asciicompat(denc)) {
2865 if (is_ascii_string(str)) {
2866 return dencidx;
2867 }
2868 }
2869 if (encoding_equal(sname, dname)) {
2870 return NIL_P(arg2) ? -1 : dencidx;
2871 }
2872 }
2873 else {
2874 if (senc && denc && !rb_enc_asciicompat(senc) && !rb_enc_asciicompat(denc)) {
2875 rb_encoding *utf8 = rb_utf8_encoding();
2876 str = rb_str_conv_enc(str, senc, utf8);
2877 senc = utf8;
2878 sname = "UTF-8";
2879 }
2880 if (encoding_equal(sname, dname)) {
2881 sname = "";
2882 dname = "";
2883 }
2884 }
2885
2886 fromp = sp = (unsigned char *)RSTRING_PTR(str);
2887 slen = RSTRING_LEN(str);
2888 blen = slen + 30; /* len + margin */
2889 dest = rb_str_tmp_new(blen);
2890 bp = (unsigned char *)RSTRING_PTR(dest);
2891
2892 transcode_loop(&fromp, &bp, (sp+slen), (bp+blen), dest, str_transcoding_resize, sname, dname, ecflags, ecopts, str);
2893 if (fromp != sp+slen) {
2894 rb_raise(rb_eArgError, "not fully converted, %"PRIdPTRDIFF" bytes left", sp+slen-fromp);
2895 }
2896 buf = (unsigned char *)RSTRING_PTR(dest);
2897 *bp = '\0';
2898 rb_str_set_len(dest, bp - buf);
2899
2900 /* set encoding */
2901 if (!denc) {
2902 dencidx = rb_define_dummy_encoding(dname);
2903 RB_GC_GUARD(arg1);
2904 RB_GC_GUARD(arg2);
2905 }
2906 *self = dest;
2907
2908 return dencidx;
2909}
2910
2911static int
2912str_transcode(int argc, VALUE *argv, VALUE *self)
2913{
2914 VALUE opt;
2915 int ecflags = 0;
2916 VALUE ecopts = Qnil;
2917
2918 argc = rb_scan_args(argc, argv, "02:", NULL, NULL, &opt);
2919 if (!NIL_P(opt)) {
2920 ecflags = rb_econv_prepare_opts(opt, &ecopts);
2921 }
2922 return str_transcode0(argc, argv, self, ecflags, ecopts);
2923}
2924
2925static inline VALUE
2926str_encode_associate(VALUE str, int encidx)
2927{
2928 int cr = 0;
2929
2930 rb_enc_associate_index(str, encidx);
2931
2932 /* transcoded string never be broken. */
2933 if (rb_enc_asciicompat(rb_enc_from_index(encidx))) {
2934 rb_str_coderange_scan_restartable(RSTRING_PTR(str), RSTRING_END(str), 0, &cr);
2935 }
2936 else {
2938 }
2939 ENC_CODERANGE_SET(str, cr);
2940 return str;
2941}
2942
2943/*
2944 * call-seq:
2945 * encode!(dst_encoding = Encoding.default_internal, **enc_opts) -> self
2946 * encode!(dst_encoding, src_encoding, **enc_opts) -> self
2947 *
2948 * Like #encode, but applies encoding changes to +self+; returns +self+.
2949 *
2950 * Related: see {Modifying}[rdoc-ref:String@Modifying].
2951 */
2952
2953static VALUE
2954str_encode_bang(int argc, VALUE *argv, VALUE str)
2955{
2956 VALUE newstr;
2957 int encidx;
2958
2959 rb_check_frozen(str);
2960
2961 newstr = str;
2962 encidx = str_transcode(argc, argv, &newstr);
2963
2964 if (encidx < 0) return str;
2965 if (newstr == str) {
2966 rb_enc_associate_index(str, encidx);
2967 return str;
2968 }
2969 rb_str_shared_replace(str, newstr);
2970 return str_encode_associate(str, encidx);
2971}
2972
2973static VALUE encoded_dup(VALUE newstr, VALUE str, int encidx);
2974
2975/*
2976 * call-seq:
2977 * encode(dst_encoding = Encoding.default_internal, **enc_opts) -> string
2978 * encode(dst_encoding, src_encoding, **enc_opts) -> string
2979 *
2980 * :include: doc/string/encode.rdoc
2981 *
2982 */
2983
2984static VALUE
2985str_encode(int argc, VALUE *argv, VALUE str)
2986{
2987 VALUE newstr = str;
2988 int encidx = str_transcode(argc, argv, &newstr);
2989 return encoded_dup(newstr, str, encidx);
2990}
2991
2992VALUE
2993rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts)
2994{
2995 int argc = 1;
2996 VALUE *argv = &to;
2997 VALUE newstr = str;
2998 int encidx = str_transcode0(argc, argv, &newstr, ecflags, ecopts);
2999 return encoded_dup(newstr, str, encidx);
3000}
3001
3002static VALUE
3003encoded_dup(VALUE newstr, VALUE str, int encidx)
3004{
3005 if (encidx < 0) return rb_str_dup(str);
3006 if (newstr == str) {
3007 newstr = rb_str_dup(str);
3008 rb_enc_associate_index(newstr, encidx);
3009 return newstr;
3010 }
3011 else {
3012 RBASIC_SET_CLASS(newstr, rb_obj_class(str));
3013 }
3014 return str_encode_associate(newstr, encidx);
3015}
3016
3017/*
3018 * Document-class: Encoding::Converter
3019 *
3020 * Encoding conversion class.
3021 */
3022static void
3023econv_free(void *ptr)
3024{
3025 rb_econv_t *ec = ptr;
3026 rb_econv_close(ec);
3027}
3028
3029static size_t
3030econv_memsize(const void *ptr)
3031{
3032 return ptr ? rb_econv_memsize((rb_econv_t *)ptr) : 0;
3033}
3034
3035static const rb_data_type_t econv_data_type = {
3036 "econv",
3037 {0, econv_free, econv_memsize,},
3038 0, 0, RUBY_TYPED_THREAD_SAFE_FREE
3039};
3040
3041static VALUE
3042econv_s_allocate(VALUE klass)
3043{
3044 return TypedData_Wrap_Struct(klass, &econv_data_type, NULL);
3045}
3046
3047static rb_encoding *
3048make_dummy_encoding(const char *name)
3049{
3050 rb_encoding *enc;
3051 int idx;
3052 idx = rb_define_dummy_encoding(name);
3053 enc = rb_enc_from_index(idx);
3054 return enc;
3055}
3056
3057static rb_encoding *
3058make_encoding(const char *name)
3059{
3060 rb_encoding *enc;
3061 enc = rb_enc_find(name);
3062 if (!enc) {
3063 RB_VM_LOCKING() {
3064 if (rb_enc_registered(name)) {
3065 enc = NULL;
3066 }
3067 else {
3068 enc = make_dummy_encoding(name);
3069 }
3070 }
3071 }
3072 return enc;
3073}
3074
3075static VALUE
3076make_encobj(const char *name)
3077{
3078 return rb_enc_from_encoding(make_encoding(name));
3079}
3080
3081/*
3082 * call-seq:
3083 * Encoding::Converter.asciicompat_encoding(string) -> encoding or nil
3084 * Encoding::Converter.asciicompat_encoding(encoding) -> encoding or nil
3085 *
3086 * Returns the corresponding ASCII compatible encoding.
3087 *
3088 * Returns nil if the argument is an ASCII compatible encoding.
3089 *
3090 * "corresponding ASCII compatible encoding" is an ASCII compatible encoding which
3091 * can represents exactly the same characters as the given ASCII incompatible encoding.
3092 * So, no conversion undefined error occurs when converting between the two encodings.
3093 *
3094 * Encoding::Converter.asciicompat_encoding("ISO-2022-JP") #=> #<Encoding:stateless-ISO-2022-JP>
3095 * Encoding::Converter.asciicompat_encoding("UTF-16BE") #=> #<Encoding:UTF-8>
3096 * Encoding::Converter.asciicompat_encoding("UTF-8") #=> nil
3097 *
3098 */
3099static VALUE
3100econv_s_asciicompat_encoding(VALUE klass, VALUE arg)
3101{
3102 const char *arg_name, *result_name;
3103 rb_encoding *arg_enc, *result_enc;
3104 VALUE enc = Qnil;
3105
3106 enc_arg(&arg, &arg_name, &arg_enc);
3107 result_name = rb_econv_asciicompat_encoding(arg_name);
3108 if (result_name) {
3109 result_enc = make_encoding(result_name);
3110 enc = rb_enc_from_encoding(result_enc);
3111 }
3112 return enc;
3113}
3114
3115static void
3116econv_args(int argc, VALUE *argv,
3117 VALUE *snamev_p, VALUE *dnamev_p,
3118 const char **sname_p, const char **dname_p,
3119 rb_encoding **senc_p, rb_encoding **denc_p,
3120 int *ecflags_p,
3121 VALUE *ecopts_p)
3122{
3123 VALUE opt, flags_v, ecopts;
3124 int sidx, didx;
3125 const char *sname, *dname;
3126 rb_encoding *senc, *denc;
3127 int ecflags;
3128
3129 argc = rb_scan_args(argc, argv, "21:", snamev_p, dnamev_p, &flags_v, &opt);
3130
3131 if (!NIL_P(flags_v)) {
3132 if (!NIL_P(opt)) {
3133 rb_error_arity(argc + 1, 2, 3);
3134 }
3135 ecflags = NUM2INT(rb_to_int(flags_v));
3136 ecopts = Qnil;
3137 }
3138 else if (!NIL_P(opt)) {
3139 ecflags = rb_econv_prepare_opts(opt, &ecopts);
3140 }
3141 else {
3142 ecflags = 0;
3143 ecopts = Qnil;
3144 }
3145
3146 senc = NULL;
3147 sidx = rb_to_encoding_index(*snamev_p);
3148 if (0 <= sidx) {
3149 senc = rb_enc_from_index(sidx);
3150 }
3151 else {
3152 StringValue(*snamev_p);
3153 }
3154
3155 denc = NULL;
3156 didx = rb_to_encoding_index(*dnamev_p);
3157 if (0 <= didx) {
3158 denc = rb_enc_from_index(didx);
3159 }
3160 else {
3161 StringValue(*dnamev_p);
3162 }
3163
3164 sname = senc ? rb_enc_name(senc) : StringValueCStr(*snamev_p);
3165 dname = denc ? rb_enc_name(denc) : StringValueCStr(*dnamev_p);
3166
3167 *sname_p = sname;
3168 *dname_p = dname;
3169 *senc_p = senc;
3170 *denc_p = denc;
3171 *ecflags_p = ecflags;
3172 *ecopts_p = ecopts;
3173}
3174
3175static int
3176decorate_convpath(VALUE convpath, int ecflags)
3177{
3178 int num_decorators;
3179 const char *decorators[MAX_ECFLAGS_DECORATORS];
3180 int i;
3181 int n, len;
3182
3183 num_decorators = decorator_names(ecflags, decorators);
3184 if (num_decorators == -1)
3185 return -1;
3186
3187 len = n = RARRAY_LENINT(convpath);
3188 if (n != 0) {
3189 VALUE pair = RARRAY_AREF(convpath, n-1);
3190 if (RB_TYPE_P(pair, T_ARRAY)) {
3191 const char *sname = rb_enc_name(rb_to_encoding(RARRAY_AREF(pair, 0)));
3192 const char *dname = rb_enc_name(rb_to_encoding(RARRAY_AREF(pair, 1)));
3193 transcoder_entry_t *entry;
3194 const rb_transcoder *tr;
3195 entry = get_transcoder_entry(sname, dname);
3196 tr = load_transcoder_entry(entry);
3197 if (!tr)
3198 return -1;
3199 if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding) &&
3200 tr->asciicompat_type == asciicompat_encoder) {
3201 n--;
3202 rb_ary_store(convpath, len + num_decorators - 1, pair);
3203 }
3204 }
3205 else {
3206 rb_ary_store(convpath, len + num_decorators - 1, pair);
3207 }
3208 }
3209
3210 for (i = 0; i < num_decorators; i++)
3211 rb_ary_store(convpath, n + i, rb_str_new_cstr(decorators[i]));
3212
3213 return 0;
3214}
3215
3216static void
3217search_convpath_i(const char *sname, const char *dname, int depth, void *arg)
3218{
3219 VALUE *ary_p = arg;
3220 VALUE v;
3221
3222 if (NIL_P(*ary_p)) {
3223 *ary_p = rb_ary_new();
3224 }
3225
3226 if (DECORATOR_P(sname, dname)) {
3227 v = rb_str_new_cstr(dname);
3228 }
3229 else {
3230 v = rb_assoc_new(make_encobj(sname), make_encobj(dname));
3231 }
3232 rb_ary_store(*ary_p, depth, v);
3233}
3234
3235/*
3236 * call-seq:
3237 * Encoding::Converter.search_convpath(source_encoding, destination_encoding) -> ary
3238 * Encoding::Converter.search_convpath(source_encoding, destination_encoding, opt) -> ary
3239 *
3240 * Returns a conversion path.
3241 *
3242 * p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP")
3243 * #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
3244 * # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>]]
3245 *
3246 * p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP", universal_newline: true)
3247 * or
3248 * p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP", newline: :universal)
3249 * #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
3250 * # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>],
3251 * # "universal_newline"]
3252 *
3253 * p Encoding::Converter.search_convpath("ISO-8859-1", "UTF-32BE", universal_newline: true)
3254 * or
3255 * p Encoding::Converter.search_convpath("ISO-8859-1", "UTF-32BE", newline: :universal)
3256 * #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
3257 * # "universal_newline",
3258 * # [#<Encoding:UTF-8>, #<Encoding:UTF-32BE>]]
3259 */
3260static VALUE
3261econv_s_search_convpath(int argc, VALUE *argv, VALUE klass)
3262{
3263 VALUE snamev, dnamev;
3264 const char *sname, *dname;
3265 rb_encoding *senc, *denc;
3266 int ecflags;
3267 VALUE ecopts;
3268 VALUE convpath;
3269
3270 econv_args(argc, argv, &snamev, &dnamev, &sname, &dname, &senc, &denc, &ecflags, &ecopts);
3271
3272 convpath = Qnil;
3273 transcode_search_path(sname, dname, search_convpath_i, &convpath);
3274
3275 if (NIL_P(convpath)) {
3276 VALUE exc = rb_econv_open_exc(sname, dname, ecflags);
3277 RB_GC_GUARD(snamev);
3278 RB_GC_GUARD(dnamev);
3279 rb_exc_raise(exc);
3280 }
3281
3282 if (decorate_convpath(convpath, ecflags) == -1) {
3283 VALUE exc = rb_econv_open_exc(sname, dname, ecflags);
3284 RB_GC_GUARD(snamev);
3285 RB_GC_GUARD(dnamev);
3286 rb_exc_raise(exc);
3287 }
3288
3289 return convpath;
3290}
3291
3292/*
3293 * Check the existence of a conversion path.
3294 * Returns the number of converters in the conversion path.
3295 * result: >=0:success -1:failure
3296 */
3297int
3298rb_econv_has_convpath_p(const char* from_encoding, const char* to_encoding)
3299{
3300 VALUE convpath = Qnil;
3301 transcode_search_path(from_encoding, to_encoding, search_convpath_i,
3302 &convpath);
3303 return RTEST(convpath);
3304}
3305
3307 rb_econv_t *ec;
3308 int index;
3309 int ret;
3310};
3311
3312static void
3313rb_econv_init_by_convpath_i(const char *sname, const char *dname, int depth, void *arg)
3314{
3316 int ret;
3317
3318 if (a->ret == -1)
3319 return;
3320
3321 ret = rb_econv_add_converter(a->ec, sname, dname, a->index);
3322
3323 a->ret = ret;
3324 return;
3325}
3326
3327static rb_econv_t *
3328rb_econv_init_by_convpath(VALUE self, VALUE convpath,
3329 const char **sname_p, const char **dname_p,
3330 rb_encoding **senc_p, rb_encoding**denc_p)
3331{
3332 rb_econv_t *ec;
3333 long i;
3334 int ret, first=1;
3335 VALUE elt;
3336 rb_encoding *senc = 0, *denc = 0;
3337 const char *sname, *dname;
3338
3339 ec = rb_econv_alloc(RARRAY_LENINT(convpath));
3340 DATA_PTR(self) = ec;
3341
3342 for (i = 0; i < RARRAY_LEN(convpath); i++) {
3343 VALUE snamev, dnamev;
3344 VALUE pair;
3345 elt = rb_ary_entry(convpath, i);
3346 if (!NIL_P(pair = rb_check_array_type(elt))) {
3347 if (RARRAY_LEN(pair) != 2)
3348 rb_raise(rb_eArgError, "not a 2-element array in convpath");
3349 snamev = rb_ary_entry(pair, 0);
3350 enc_arg(&snamev, &sname, &senc);
3351 dnamev = rb_ary_entry(pair, 1);
3352 enc_arg(&dnamev, &dname, &denc);
3353 }
3354 else {
3355 sname = "";
3356 dname = StringValueCStr(elt);
3357 }
3358 if (DECORATOR_P(sname, dname)) {
3359 ret = rb_econv_add_converter(ec, sname, dname, ec->num_trans);
3360 if (ret == -1) {
3361 VALUE msg = rb_sprintf("decoration failed: %s", dname);
3362 RB_GC_GUARD(snamev);
3363 RB_GC_GUARD(dnamev);
3364 rb_exc_raise(rb_exc_new_str(rb_eArgError, msg));
3365 }
3366 }
3367 else {
3368 int j = ec->num_trans;
3369 struct rb_econv_init_by_convpath_t arg;
3370 arg.ec = ec;
3371 arg.index = ec->num_trans;
3372 arg.ret = 0;
3373 ret = transcode_search_path(sname, dname, rb_econv_init_by_convpath_i, &arg);
3374 if (ret == -1 || arg.ret == -1) {
3375 VALUE msg = rb_sprintf("adding conversion failed: %s to %s", sname, dname);
3376 RB_GC_GUARD(snamev);
3377 RB_GC_GUARD(dnamev);
3378 rb_exc_raise(rb_exc_new_str(rb_eArgError, msg));
3379 }
3380 if (first) {
3381 first = 0;
3382 *senc_p = senc;
3383 *sname_p = ec->elems[j].tc->transcoder->src_encoding;
3384 }
3385 *denc_p = denc;
3386 *dname_p = ec->elems[ec->num_trans-1].tc->transcoder->dst_encoding;
3387 }
3388 }
3389
3390 if (first) {
3391 *senc_p = NULL;
3392 *denc_p = NULL;
3393 *sname_p = "";
3394 *dname_p = "";
3395 }
3396
3397 ec->source_encoding_name = *sname_p;
3398 ec->destination_encoding_name = *dname_p;
3399
3400 return ec;
3401}
3402
3403/*
3404 * call-seq:
3405 * Encoding::Converter.new(source_encoding, destination_encoding)
3406 * Encoding::Converter.new(source_encoding, destination_encoding, opt)
3407 * Encoding::Converter.new(convpath)
3408 *
3409 * possible options elements:
3410 * hash form:
3411 * :invalid => nil # raise error on invalid byte sequence (default)
3412 * :invalid => :replace # replace invalid byte sequence
3413 * :undef => nil # raise error on undefined conversion (default)
3414 * :undef => :replace # replace undefined conversion
3415 * :replace => string # replacement string ("?" or "\uFFFD" if not specified)
3416 * :newline => :universal # decorator for converting CRLF and CR to LF
3417 * :newline => :lf # decorator for converting CRLF and CR to LF when writing
3418 * :newline => :crlf # decorator for converting LF to CRLF
3419 * :newline => :cr # decorator for converting LF to CR
3420 * :universal_newline => true # decorator for converting CRLF and CR to LF
3421 * :crlf_newline => true # decorator for converting LF to CRLF
3422 * :cr_newline => true # decorator for converting LF to CR
3423 * :lf_newline => true # decorator for converting CRLF and CR to LF when writing
3424 * :xml => :text # escape as XML CharData.
3425 * :xml => :attr # escape as XML AttValue
3426 * integer form:
3427 * Encoding::Converter::INVALID_REPLACE
3428 * Encoding::Converter::UNDEF_REPLACE
3429 * Encoding::Converter::UNDEF_HEX_CHARREF
3430 * Encoding::Converter::UNIVERSAL_NEWLINE_DECORATOR
3431 * Encoding::Converter::LF_NEWLINE_DECORATOR
3432 * Encoding::Converter::CRLF_NEWLINE_DECORATOR
3433 * Encoding::Converter::CR_NEWLINE_DECORATOR
3434 * Encoding::Converter::XML_TEXT_DECORATOR
3435 * Encoding::Converter::XML_ATTR_CONTENT_DECORATOR
3436 * Encoding::Converter::XML_ATTR_QUOTE_DECORATOR
3437 *
3438 * Encoding::Converter.new creates an instance of Encoding::Converter.
3439 *
3440 * Source_encoding and destination_encoding should be a string or
3441 * Encoding object.
3442 *
3443 * opt should be nil, a hash or an integer.
3444 *
3445 * convpath should be an array.
3446 * convpath may contain
3447 * - two-element arrays which contain encodings or encoding names, or
3448 * - strings representing decorator names.
3449 *
3450 * Encoding::Converter.new optionally takes an option.
3451 * The option should be a hash or an integer.
3452 * The option hash can contain :invalid => nil, etc.
3453 * The option integer should be logical-or of constants such as
3454 * Encoding::Converter::INVALID_REPLACE, etc.
3455 *
3456 * [:invalid => nil]
3457 * Raise error on invalid byte sequence. This is a default behavior.
3458 * [:invalid => :replace]
3459 * Replace invalid byte sequence by replacement string.
3460 * [:undef => nil]
3461 * Raise an error if a character in source_encoding is not defined in destination_encoding.
3462 * This is a default behavior.
3463 * [:undef => :replace]
3464 * Replace undefined character in destination_encoding with replacement string.
3465 * [:replace => string]
3466 * Specify the replacement string.
3467 * If not specified, "\uFFFD" is used for Unicode encodings and "?" for others.
3468 * [:universal_newline => true]
3469 * Convert CRLF and CR to LF.
3470 * [:crlf_newline => true]
3471 * Convert LF to CRLF.
3472 * [:cr_newline => true]
3473 * Convert LF to CR.
3474 * [:lf_newline => true]
3475 * Convert CRLF and CR to LF (when writing).
3476 * [:xml => :text]
3477 * Escape as XML CharData.
3478 * This form can be used as an HTML 4.0 #PCDATA.
3479 * - '&' -> '&amp;'
3480 * - '<' -> '&lt;'
3481 * - '>' -> '&gt;'
3482 * - undefined characters in destination_encoding -> hexadecimal CharRef such as &#xHH;
3483 * [:xml => :attr]
3484 * Escape as XML AttValue.
3485 * The converted result is quoted as "...".
3486 * This form can be used as an HTML 4.0 attribute value.
3487 * - '&' -> '&amp;'
3488 * - '<' -> '&lt;'
3489 * - '>' -> '&gt;'
3490 * - '"' -> '&quot;'
3491 * - undefined characters in destination_encoding -> hexadecimal CharRef such as &#xHH;
3492 *
3493 * Examples:
3494 * # UTF-16BE to UTF-8
3495 * ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
3496 *
3497 * # Usually, decorators such as newline conversion are inserted last.
3498 * ec = Encoding::Converter.new("UTF-16BE", "UTF-8", :universal_newline => true)
3499 * p ec.convpath #=> [[#<Encoding:UTF-16BE>, #<Encoding:UTF-8>],
3500 * # "universal_newline"]
3501 *
3502 * # But, if the last encoding is ASCII incompatible,
3503 * # decorators are inserted before the last conversion.
3504 * ec = Encoding::Converter.new("UTF-8", "UTF-16BE", :crlf_newline => true)
3505 * p ec.convpath #=> ["crlf_newline",
3506 * # [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]
3507 *
3508 * # Conversion path can be specified directly.
3509 * ec = Encoding::Converter.new(["universal_newline", ["EUC-JP", "UTF-8"], ["UTF-8", "UTF-16BE"]])
3510 * p ec.convpath #=> ["universal_newline",
3511 * # [#<Encoding:EUC-JP>, #<Encoding:UTF-8>],
3512 * # [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]
3513 */
3514static VALUE
3515econv_init(int argc, VALUE *argv, VALUE self)
3516{
3517 VALUE ecopts;
3518 VALUE snamev, dnamev;
3519 const char *sname, *dname;
3520 rb_encoding *senc, *denc;
3521 rb_econv_t *ec;
3522 int ecflags;
3523 VALUE convpath;
3524
3525 if (rb_check_typeddata(self, &econv_data_type)) {
3526 rb_raise(rb_eTypeError, "already initialized");
3527 }
3528
3529 if (argc == 1 && !NIL_P(convpath = rb_check_array_type(argv[0]))) {
3530 ec = rb_econv_init_by_convpath(self, convpath, &sname, &dname, &senc, &denc);
3531 ecflags = 0;
3532 ecopts = Qnil;
3533 }
3534 else {
3535 econv_args(argc, argv, &snamev, &dnamev, &sname, &dname, &senc, &denc, &ecflags, &ecopts);
3536 ec = rb_econv_open_opts(sname, dname, ecflags, ecopts);
3537 }
3538
3539 if (!ec) {
3540 VALUE exc = rb_econv_open_exc(sname, dname, ecflags);
3541 RB_GC_GUARD(snamev);
3542 RB_GC_GUARD(dnamev);
3543 rb_exc_raise(exc);
3544 }
3545
3546 if (!DECORATOR_P(sname, dname)) {
3547 if (!senc)
3548 senc = make_dummy_encoding(sname);
3549 if (!denc)
3550 denc = make_dummy_encoding(dname);
3551 RB_GC_GUARD(snamev);
3552 RB_GC_GUARD(dnamev);
3553 }
3554
3555 ec->source_encoding = senc;
3556 ec->destination_encoding = denc;
3557
3558 DATA_PTR(self) = ec;
3559
3560 return self;
3561}
3562
3563/*
3564 * call-seq:
3565 * ec.inspect -> string
3566 *
3567 * Returns a printable version of <i>ec</i>
3568 *
3569 * ec = Encoding::Converter.new("iso-8859-1", "utf-8")
3570 * puts ec.inspect #=> #<Encoding::Converter: ISO-8859-1 to UTF-8>
3571 *
3572 */
3573static VALUE
3574econv_inspect(VALUE self)
3575{
3576 const char *cname = rb_obj_classname(self);
3577 rb_econv_t *ec;
3578
3579 TypedData_Get_Struct(self, rb_econv_t, &econv_data_type, ec);
3580 if (!ec)
3581 return rb_sprintf("#<%s: uninitialized>", cname);
3582 else {
3583 const char *sname = ec->source_encoding_name;
3584 const char *dname = ec->destination_encoding_name;
3585 VALUE str;
3586 str = rb_sprintf("#<%s: ", cname);
3587 econv_description(sname, dname, ec->flags, str);
3588 rb_str_cat2(str, ">");
3589 return str;
3590 }
3591}
3592
3593static rb_econv_t *
3594check_econv(VALUE self)
3595{
3596 rb_econv_t *ec;
3597
3598 TypedData_Get_Struct(self, rb_econv_t, &econv_data_type, ec);
3599 if (!ec) {
3600 rb_raise(rb_eTypeError, "uninitialized encoding converter");
3601 }
3602 return ec;
3603}
3604
3605static VALUE
3606econv_get_encoding(rb_encoding *encoding)
3607{
3608 if (!encoding)
3609 return Qnil;
3610 return rb_enc_from_encoding(encoding);
3611}
3612
3613/*
3614 * call-seq:
3615 * ec.source_encoding -> encoding
3616 *
3617 * Returns the source encoding as an Encoding object.
3618 */
3619static VALUE
3620econv_source_encoding(VALUE self)
3621{
3622 rb_econv_t *ec = check_econv(self);
3623 return econv_get_encoding(ec->source_encoding);
3624}
3625
3626/*
3627 * call-seq:
3628 * ec.destination_encoding -> encoding
3629 *
3630 * Returns the destination encoding as an Encoding object.
3631 */
3632static VALUE
3633econv_destination_encoding(VALUE self)
3634{
3635 rb_econv_t *ec = check_econv(self);
3636 return econv_get_encoding(ec->destination_encoding);
3637}
3638
3639/*
3640 * call-seq:
3641 * ec.convpath -> ary
3642 *
3643 * Returns the conversion path of ec.
3644 *
3645 * The result is an array of conversions.
3646 *
3647 * ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP", crlf_newline: true)
3648 * p ec.convpath
3649 * #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
3650 * # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>],
3651 * # "crlf_newline"]
3652 *
3653 * Each element of the array is a pair of encodings or a string.
3654 * A pair means an encoding conversion.
3655 * A string means a decorator.
3656 *
3657 * In the above example, [#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>] means
3658 * a converter from ISO-8859-1 to UTF-8.
3659 * "crlf_newline" means newline converter from LF to CRLF.
3660 */
3661static VALUE
3662econv_convpath(VALUE self)
3663{
3664 rb_econv_t *ec = check_econv(self);
3665 VALUE result;
3666 int i;
3667
3668 result = rb_ary_new();
3669 for (i = 0; i < ec->num_trans; i++) {
3670 const rb_transcoder *tr = ec->elems[i].tc->transcoder;
3671 VALUE v;
3672 if (DECORATOR_P(tr->src_encoding, tr->dst_encoding))
3673 v = rb_str_new_cstr(tr->dst_encoding);
3674 else
3675 v = rb_assoc_new(make_encobj(tr->src_encoding), make_encobj(tr->dst_encoding));
3676 rb_ary_push(result, v);
3677 }
3678 return result;
3679}
3680
3681/*
3682 * call-seq:
3683 * ec == other -> true or false
3684 */
3685static VALUE
3686econv_equal(VALUE self, VALUE other)
3687{
3688 rb_econv_t *ec1 = check_econv(self);
3689 rb_econv_t *ec2;
3690 int i;
3691
3692 if (!rb_typeddata_is_kind_of(other, &econv_data_type)) {
3693 return Qnil;
3694 }
3695 ec2 = DATA_PTR(other);
3696 if (!ec2) return Qfalse;
3697 if (ec1->source_encoding_name != ec2->source_encoding_name &&
3698 strcmp(ec1->source_encoding_name, ec2->source_encoding_name))
3699 return Qfalse;
3700 if (ec1->destination_encoding_name != ec2->destination_encoding_name &&
3701 strcmp(ec1->destination_encoding_name, ec2->destination_encoding_name))
3702 return Qfalse;
3703 if (ec1->flags != ec2->flags) return Qfalse;
3704 if (ec1->replacement_enc != ec2->replacement_enc &&
3705 strcmp(ec1->replacement_enc, ec2->replacement_enc))
3706 return Qfalse;
3707 if (ec1->replacement_len != ec2->replacement_len) return Qfalse;
3708 if (ec1->replacement_str != ec2->replacement_str &&
3709 memcmp(ec1->replacement_str, ec2->replacement_str, ec2->replacement_len))
3710 return Qfalse;
3711
3712 if (ec1->num_trans != ec2->num_trans) return Qfalse;
3713 for (i = 0; i < ec1->num_trans; i++) {
3714 if (ec1->elems[i].tc->transcoder != ec2->elems[i].tc->transcoder)
3715 return Qfalse;
3716 }
3717 return Qtrue;
3718}
3719
3720static VALUE
3721econv_result_to_symbol(rb_econv_result_t res)
3722{
3723 switch (res) {
3724 case econv_invalid_byte_sequence: return sym_invalid_byte_sequence;
3725 case econv_incomplete_input: return sym_incomplete_input;
3726 case econv_undefined_conversion: return sym_undefined_conversion;
3727 case econv_destination_buffer_full: return sym_destination_buffer_full;
3728 case econv_source_buffer_empty: return sym_source_buffer_empty;
3729 case econv_finished: return sym_finished;
3730 case econv_after_output: return sym_after_output;
3731 default: return INT2NUM(res); /* should not be reached */
3732 }
3733}
3734
3735/*
3736 * call-seq:
3737 * ec.primitive_convert(source_buffer, destination_buffer) -> symbol
3738 * ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset) -> symbol
3739 * ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize) -> symbol
3740 * ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize, opt) -> symbol
3741 *
3742 * possible opt elements:
3743 * hash form:
3744 * :partial_input => true # source buffer may be part of larger source
3745 * :after_output => true # stop conversion after output before input
3746 * integer form:
3747 * Encoding::Converter::PARTIAL_INPUT
3748 * Encoding::Converter::AFTER_OUTPUT
3749 *
3750 * possible results:
3751 * :invalid_byte_sequence
3752 * :incomplete_input
3753 * :undefined_conversion
3754 * :after_output
3755 * :destination_buffer_full
3756 * :source_buffer_empty
3757 * :finished
3758 *
3759 * primitive_convert converts source_buffer into destination_buffer.
3760 *
3761 * source_buffer should be a string or nil.
3762 * nil means an empty string.
3763 *
3764 * destination_buffer should be a string.
3765 *
3766 * destination_byteoffset should be an integer or nil.
3767 * nil means the end of destination_buffer.
3768 * If it is omitted, nil is assumed.
3769 *
3770 * destination_bytesize should be an integer or nil.
3771 * nil means unlimited.
3772 * If it is omitted, nil is assumed.
3773 *
3774 * opt should be nil, a hash or an integer.
3775 * nil means no flags.
3776 * If it is omitted, nil is assumed.
3777 *
3778 * primitive_convert converts the content of source_buffer from beginning
3779 * and store the result into destination_buffer.
3780 *
3781 * destination_byteoffset and destination_bytesize specify the region which
3782 * the converted result is stored.
3783 * destination_byteoffset specifies the start position in destination_buffer in bytes.
3784 * If destination_byteoffset is nil,
3785 * destination_buffer.bytesize is used for appending the result.
3786 * destination_bytesize specifies maximum number of bytes.
3787 * If destination_bytesize is nil,
3788 * destination size is unlimited.
3789 * After conversion, destination_buffer is resized to
3790 * destination_byteoffset + actually produced number of bytes.
3791 * Also destination_buffer's encoding is set to destination_encoding.
3792 *
3793 * primitive_convert drops the converted part of source_buffer.
3794 * the dropped part is converted in destination_buffer or
3795 * buffered in Encoding::Converter object.
3796 *
3797 * primitive_convert stops conversion when one of following condition met.
3798 * - invalid byte sequence found in source buffer (:invalid_byte_sequence)
3799 * +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
3800 * - unexpected end of source buffer (:incomplete_input)
3801 * this occur only when :partial_input is not specified.
3802 * +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
3803 * - character not representable in output encoding (:undefined_conversion)
3804 * +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
3805 * - after some output is generated, before input is done (:after_output)
3806 * this occur only when :after_output is specified.
3807 * - destination buffer is full (:destination_buffer_full)
3808 * this occur only when destination_bytesize is non-nil.
3809 * - source buffer is empty (:source_buffer_empty)
3810 * this occur only when :partial_input is specified.
3811 * - conversion is finished (:finished)
3812 *
3813 * example:
3814 * ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
3815 * ret = ec.primitive_convert(src="pi", dst="", nil, 100)
3816 * p [ret, src, dst] #=> [:finished, "", "\x00p\x00i"]
3817 *
3818 * ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
3819 * ret = ec.primitive_convert(src="pi", dst="", nil, 1)
3820 * p [ret, src, dst] #=> [:destination_buffer_full, "i", "\x00"]
3821 * ret = ec.primitive_convert(src, dst="", nil, 1)
3822 * p [ret, src, dst] #=> [:destination_buffer_full, "", "p"]
3823 * ret = ec.primitive_convert(src, dst="", nil, 1)
3824 * p [ret, src, dst] #=> [:destination_buffer_full, "", "\x00"]
3825 * ret = ec.primitive_convert(src, dst="", nil, 1)
3826 * p [ret, src, dst] #=> [:finished, "", "i"]
3827 *
3828 */
3829static VALUE
3830econv_primitive_convert(int argc, VALUE *argv, VALUE self)
3831{
3832 VALUE input, output, output_byteoffset_v, output_bytesize_v, opt, flags_v;
3833 rb_econv_t *ec = check_econv(self);
3835 const unsigned char *ip, *is;
3836 unsigned char *op, *os;
3837 long output_byteoffset, output_bytesize;
3838 unsigned long output_byteend;
3839 int flags;
3840
3841 argc = rb_scan_args(argc, argv, "23:", &input, &output, &output_byteoffset_v, &output_bytesize_v, &flags_v, &opt);
3842
3843 if (NIL_P(output_byteoffset_v))
3844 output_byteoffset = 0; /* dummy */
3845 else
3846 output_byteoffset = NUM2LONG(output_byteoffset_v);
3847
3848 if (NIL_P(output_bytesize_v))
3849 output_bytesize = 0; /* dummy */
3850 else
3851 output_bytesize = NUM2LONG(output_bytesize_v);
3852
3853 if (!NIL_P(flags_v)) {
3854 if (!NIL_P(opt)) {
3855 rb_error_arity(argc + 1, 2, 5);
3856 }
3857 flags = NUM2INT(rb_to_int(flags_v));
3858 }
3859 else if (!NIL_P(opt)) {
3860 VALUE v;
3861 flags = 0;
3862 v = rb_hash_aref(opt, sym_partial_input);
3863 if (RTEST(v))
3864 flags |= ECONV_PARTIAL_INPUT;
3865 v = rb_hash_aref(opt, sym_after_output);
3866 if (RTEST(v))
3867 flags |= ECONV_AFTER_OUTPUT;
3868 }
3869 else {
3870 flags = 0;
3871 }
3872
3873 StringValue(output);
3874 if (!NIL_P(input))
3875 StringValue(input);
3876 rb_str_modify(output);
3877
3878 if (NIL_P(output_bytesize_v)) {
3879 output_bytesize = rb_str_capacity(output);
3880
3881 if (!NIL_P(input) && output_bytesize < RSTRING_LEN(input))
3882 output_bytesize = RSTRING_LEN(input);
3883 }
3884
3885 retry:
3886
3887 if (NIL_P(output_byteoffset_v))
3888 output_byteoffset = RSTRING_LEN(output);
3889
3890 if (output_byteoffset < 0)
3891 rb_raise(rb_eArgError, "negative output_byteoffset");
3892
3893 if (RSTRING_LEN(output) < output_byteoffset)
3894 rb_raise(rb_eArgError, "output_byteoffset too big");
3895
3896 if (output_bytesize < 0)
3897 rb_raise(rb_eArgError, "negative output_bytesize");
3898
3899 output_byteend = (unsigned long)output_byteoffset +
3900 (unsigned long)output_bytesize;
3901
3902 if (output_byteend < (unsigned long)output_byteoffset ||
3903 LONG_MAX < output_byteend)
3904 rb_raise(rb_eArgError, "output_byteoffset+output_bytesize too big");
3905
3906 if (rb_str_capacity(output) < output_byteend)
3907 rb_str_resize(output, output_byteend);
3908
3909 if (NIL_P(input)) {
3910 ip = is = NULL;
3911 }
3912 else {
3913 ip = (const unsigned char *)RSTRING_PTR(input);
3914 is = ip + RSTRING_LEN(input);
3915 }
3916
3917 op = (unsigned char *)RSTRING_PTR(output) + output_byteoffset;
3918 os = op + output_bytesize;
3919
3920 res = rb_econv_convert(ec, &ip, is, &op, os, flags);
3921 rb_str_set_len(output, op-(unsigned char *)RSTRING_PTR(output));
3922 if (!NIL_P(input)) {
3923 rb_str_drop_bytes(input, ip - (unsigned char *)RSTRING_PTR(input));
3924 }
3925
3926 if (NIL_P(output_bytesize_v) && res == econv_destination_buffer_full) {
3927 if (LONG_MAX / 2 < output_bytesize)
3928 rb_raise(rb_eArgError, "too long conversion result");
3929 output_bytesize *= 2;
3930 output_byteoffset_v = Qnil;
3931 goto retry;
3932 }
3933
3934 if (ec->destination_encoding) {
3935 rb_enc_associate(output, ec->destination_encoding);
3936 }
3937
3938 return econv_result_to_symbol(res);
3939}
3940
3941/*
3942 * call-seq:
3943 * ec.convert(source_string) -> destination_string
3944 *
3945 * Convert source_string and return destination_string.
3946 *
3947 * source_string is assumed as a part of source.
3948 * i.e. :partial_input=>true is specified internally.
3949 * finish method should be used last.
3950 *
3951 * ec = Encoding::Converter.new("utf-8", "euc-jp")
3952 * puts ec.convert("\u3042").dump #=> "\xA4\xA2"
3953 * puts ec.finish.dump #=> ""
3954 *
3955 * ec = Encoding::Converter.new("euc-jp", "utf-8")
3956 * puts ec.convert("\xA4").dump #=> ""
3957 * puts ec.convert("\xA2").dump #=> "\xE3\x81\x82"
3958 * puts ec.finish.dump #=> ""
3959 *
3960 * ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
3961 * puts ec.convert("\xE3").dump #=> "".force_encoding("ISO-2022-JP")
3962 * puts ec.convert("\x81").dump #=> "".force_encoding("ISO-2022-JP")
3963 * puts ec.convert("\x82").dump #=> "\e$B$\"".force_encoding("ISO-2022-JP")
3964 * puts ec.finish.dump #=> "\e(B".force_encoding("ISO-2022-JP")
3965 *
3966 * If a conversion error occur,
3967 * Encoding::UndefinedConversionError or
3968 * Encoding::InvalidByteSequenceError is raised.
3969 * Encoding::Converter#convert doesn't supply methods to recover or restart
3970 * from these exceptions.
3971 * When you want to handle these conversion errors,
3972 * use Encoding::Converter#primitive_convert.
3973 *
3974 */
3975static VALUE
3976econv_convert(VALUE self, VALUE source_string)
3977{
3978 VALUE ret, dst;
3979 VALUE av[5];
3980 int ac;
3981 rb_econv_t *ec = check_econv(self);
3982
3983 StringValue(source_string);
3984
3985 dst = rb_str_new(NULL, 0);
3986
3987 av[0] = rb_str_dup(source_string);
3988 av[1] = dst;
3989 av[2] = Qnil;
3990 av[3] = Qnil;
3992 ac = 5;
3993
3994 ret = econv_primitive_convert(ac, av, self);
3995
3996 if (ret == sym_invalid_byte_sequence ||
3997 ret == sym_undefined_conversion ||
3998 ret == sym_incomplete_input) {
3999 VALUE exc = make_econv_exception(ec);
4000 rb_exc_raise(exc);
4001 }
4002
4003 if (ret == sym_finished) {
4004 rb_raise(rb_eArgError, "converter already finished");
4005 }
4006
4007 if (ret != sym_source_buffer_empty) {
4008 rb_bug("unexpected result of econv_primitive_convert");
4009 }
4010
4011 return dst;
4012}
4013
4014/*
4015 * call-seq:
4016 * ec.finish -> string
4017 *
4018 * Finishes the converter.
4019 * It returns the last part of the converted string.
4020 *
4021 * ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
4022 * p ec.convert("\u3042") #=> "\e$B$\""
4023 * p ec.finish #=> "\e(B"
4024 */
4025static VALUE
4026econv_finish(VALUE self)
4027{
4028 VALUE ret, dst;
4029 VALUE av[5];
4030 int ac;
4031 rb_econv_t *ec = check_econv(self);
4032
4033 dst = rb_str_new(NULL, 0);
4034
4035 av[0] = Qnil;
4036 av[1] = dst;
4037 av[2] = Qnil;
4038 av[3] = Qnil;
4039 av[4] = INT2FIX(0);
4040 ac = 5;
4041
4042 ret = econv_primitive_convert(ac, av, self);
4043
4044 if (ret == sym_invalid_byte_sequence ||
4045 ret == sym_undefined_conversion ||
4046 ret == sym_incomplete_input) {
4047 VALUE exc = make_econv_exception(ec);
4048 rb_exc_raise(exc);
4049 }
4050
4051 if (ret != sym_finished) {
4052 rb_bug("unexpected result of econv_primitive_convert");
4053 }
4054
4055 return dst;
4056}
4057
4058/*
4059 * call-seq:
4060 * ec.primitive_errinfo -> array
4061 *
4062 * primitive_errinfo returns important information regarding the last error
4063 * as a 5-element array:
4064 *
4065 * [result, enc1, enc2, error_bytes, readagain_bytes]
4066 *
4067 * result is the last result of primitive_convert.
4068 *
4069 * Other elements are only meaningful when result is
4070 * :invalid_byte_sequence, :incomplete_input or :undefined_conversion.
4071 *
4072 * enc1 and enc2 indicate a conversion step as a pair of strings.
4073 * For example, a converter from EUC-JP to ISO-8859-1 converts
4074 * a string as follows: EUC-JP -> UTF-8 -> ISO-8859-1.
4075 * So [enc1, enc2] is either ["EUC-JP", "UTF-8"] or ["UTF-8", "ISO-8859-1"].
4076 *
4077 * error_bytes and readagain_bytes indicate the byte sequences which caused the error.
4078 * error_bytes is discarded portion.
4079 * readagain_bytes is buffered portion which is read again on next conversion.
4080 *
4081 * Example:
4082 *
4083 * # \xff is invalid as EUC-JP.
4084 * ec = Encoding::Converter.new("EUC-JP", "Shift_JIS")
4085 * ec.primitive_convert(src="\xff", dst="", nil, 10)
4086 * p ec.primitive_errinfo
4087 * #=> [:invalid_byte_sequence, "EUC-JP", "Shift_JIS", "\xFF", ""]
4088 *
4089 * # HIRAGANA LETTER A (\xa4\xa2 in EUC-JP) is not representable in ISO-8859-1.
4090 * # Since this error is occur in UTF-8 to ISO-8859-1 conversion,
4091 * # error_bytes is HIRAGANA LETTER A in UTF-8 (\xE3\x81\x82).
4092 * ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
4093 * ec.primitive_convert(src="\xa4\xa2", dst="", nil, 10)
4094 * p ec.primitive_errinfo
4095 * #=> [:undefined_conversion, "UTF-8", "ISO-8859-1", "\xE3\x81\x82", ""]
4096 *
4097 * # partial character is invalid
4098 * ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
4099 * ec.primitive_convert(src="\xa4", dst="", nil, 10)
4100 * p ec.primitive_errinfo
4101 * #=> [:incomplete_input, "EUC-JP", "UTF-8", "\xA4", ""]
4102 *
4103 * # Encoding::Converter::PARTIAL_INPUT prevents invalid errors by
4104 * # partial characters.
4105 * ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
4106 * ec.primitive_convert(src="\xa4", dst="", nil, 10, Encoding::Converter::PARTIAL_INPUT)
4107 * p ec.primitive_errinfo
4108 * #=> [:source_buffer_empty, nil, nil, nil, nil]
4109 *
4110 * # \xd8\x00\x00@ is invalid as UTF-16BE because
4111 * # no low surrogate after high surrogate (\xd8\x00).
4112 * # It is detected by 3rd byte (\00) which is part of next character.
4113 * # So the high surrogate (\xd8\x00) is discarded and
4114 * # the 3rd byte is read again later.
4115 * # Since the byte is buffered in ec, it is dropped from src.
4116 * ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
4117 * ec.primitive_convert(src="\xd8\x00\x00@", dst="", nil, 10)
4118 * p ec.primitive_errinfo
4119 * #=> [:invalid_byte_sequence, "UTF-16BE", "UTF-8", "\xD8\x00", "\x00"]
4120 * p src
4121 * #=> "@"
4122 *
4123 * # Similar to UTF-16BE, \x00\xd8@\x00 is invalid as UTF-16LE.
4124 * # The problem is detected by 4th byte.
4125 * ec = Encoding::Converter.new("UTF-16LE", "UTF-8")
4126 * ec.primitive_convert(src="\x00\xd8@\x00", dst="", nil, 10)
4127 * p ec.primitive_errinfo
4128 * #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "@\x00"]
4129 * p src
4130 * #=> ""
4131 *
4132 */
4133static VALUE
4134econv_primitive_errinfo(VALUE self)
4135{
4136 rb_econv_t *ec = check_econv(self);
4137
4138 VALUE ary;
4139
4140 ary = rb_ary_new2(5);
4141
4142 rb_ary_store(ary, 0, econv_result_to_symbol(ec->last_error.result));
4143 rb_ary_store(ary, 4, Qnil);
4144
4145 if (ec->last_error.source_encoding)
4146 rb_ary_store(ary, 1, rb_str_new2(ec->last_error.source_encoding));
4147
4148 if (ec->last_error.destination_encoding)
4149 rb_ary_store(ary, 2, rb_str_new2(ec->last_error.destination_encoding));
4150
4151 if (ec->last_error.error_bytes_start) {
4152 rb_ary_store(ary, 3, rb_str_new((const char *)ec->last_error.error_bytes_start, ec->last_error.error_bytes_len));
4153 rb_ary_store(ary, 4, rb_str_new((const char *)ec->last_error.error_bytes_start + ec->last_error.error_bytes_len, ec->last_error.readagain_len));
4154 }
4155
4156 return ary;
4157}
4158
4159/*
4160 * call-seq:
4161 * ec.insert_output(string) -> nil
4162 *
4163 * Inserts string into the encoding converter.
4164 * The string will be converted to the destination encoding and
4165 * output on later conversions.
4166 *
4167 * If the destination encoding is stateful,
4168 * string is converted according to the state and the state is updated.
4169 *
4170 * This method should be used only when a conversion error occurs.
4171 *
4172 * ec = Encoding::Converter.new("utf-8", "iso-8859-1")
4173 * src = "HIRAGANA LETTER A is \u{3042}."
4174 * dst = ""
4175 * p ec.primitive_convert(src, dst) #=> :undefined_conversion
4176 * puts "[#{dst.dump}, #{src.dump}]" #=> ["HIRAGANA LETTER A is ", "."]
4177 * ec.insert_output("<err>")
4178 * p ec.primitive_convert(src, dst) #=> :finished
4179 * puts "[#{dst.dump}, #{src.dump}]" #=> ["HIRAGANA LETTER A is <err>.", ""]
4180 *
4181 * ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
4182 * src = "\u{306F 3041 3068 2661 3002}" # U+2661 is not representable in iso-2022-jp
4183 * dst = ""
4184 * p ec.primitive_convert(src, dst) #=> :undefined_conversion
4185 * puts "[#{dst.dump}, #{src.dump}]" #=> ["\e$B$O$!$H".force_encoding("ISO-2022-JP"), "\xE3\x80\x82"]
4186 * ec.insert_output "?" # state change required to output "?".
4187 * p ec.primitive_convert(src, dst) #=> :finished
4188 * puts "[#{dst.dump}, #{src.dump}]" #=> ["\e$B$O$!$H\e(B?\e$B!#\e(B".force_encoding("ISO-2022-JP"), ""]
4189 *
4190 */
4191static VALUE
4192econv_insert_output(VALUE self, VALUE string)
4193{
4194 const char *insert_enc;
4195
4196 int ret;
4197
4198 rb_econv_t *ec = check_econv(self);
4199
4200 StringValue(string);
4201 insert_enc = rb_econv_encoding_to_insert_output(ec);
4202 string = rb_str_encode(string, rb_enc_from_encoding(rb_enc_find(insert_enc)), 0, Qnil);
4203
4204 ret = rb_econv_insert_output(ec, (const unsigned char *)RSTRING_PTR(string), RSTRING_LEN(string), insert_enc);
4205 if (ret == -1) {
4206 rb_raise(rb_eArgError, "too big string");
4207 }
4208
4209 return Qnil;
4210}
4211
4212/*
4213 * call-seq:
4214 * ec.putback -> string
4215 * ec.putback(max_numbytes) -> string
4216 *
4217 * Put back the bytes which will be converted.
4218 *
4219 * The bytes are caused by invalid_byte_sequence error.
4220 * When invalid_byte_sequence error, some bytes are discarded and
4221 * some bytes are buffered to be converted later.
4222 * The latter bytes can be put back.
4223 * It can be observed by
4224 * Encoding::InvalidByteSequenceError#readagain_bytes and
4225 * Encoding::Converter#primitive_errinfo.
4226 *
4227 * ec = Encoding::Converter.new("utf-16le", "iso-8859-1")
4228 * src = "\x00\xd8\x61\x00"
4229 * dst = ""
4230 * p ec.primitive_convert(src, dst) #=> :invalid_byte_sequence
4231 * p ec.primitive_errinfo #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "a\x00"]
4232 * p ec.putback #=> "a\x00"
4233 * p ec.putback #=> "" # no more bytes to put back
4234 *
4235 */
4236static VALUE
4237econv_putback(int argc, VALUE *argv, VALUE self)
4238{
4239 rb_econv_t *ec = check_econv(self);
4240 int n;
4241 int putbackable;
4242 VALUE str, max;
4243
4244 if (!rb_check_arity(argc, 0, 1) || NIL_P(max = argv[0])) {
4245 n = rb_econv_putbackable(ec);
4246 }
4247 else {
4248 n = NUM2INT(max);
4249 putbackable = rb_econv_putbackable(ec);
4250 if (putbackable < n)
4251 n = putbackable;
4252 }
4253
4254 str = rb_str_new(NULL, n);
4255 rb_econv_putback(ec, (unsigned char *)RSTRING_PTR(str), n);
4256
4257 if (ec->source_encoding) {
4258 rb_enc_associate(str, ec->source_encoding);
4259 }
4260
4261 return str;
4262}
4263
4264/*
4265 * call-seq:
4266 * ec.last_error -> exception or nil
4267 *
4268 * Returns an exception object for the last conversion.
4269 * Returns nil if the last conversion did not produce an error.
4270 *
4271 * "error" means that
4272 * Encoding::InvalidByteSequenceError and Encoding::UndefinedConversionError for
4273 * Encoding::Converter#convert and
4274 * :invalid_byte_sequence, :incomplete_input and :undefined_conversion for
4275 * Encoding::Converter#primitive_convert.
4276 *
4277 * ec = Encoding::Converter.new("utf-8", "iso-8859-1")
4278 * p ec.primitive_convert(src="\xf1abcd", dst="") #=> :invalid_byte_sequence
4279 * p ec.last_error #=> #<Encoding::InvalidByteSequenceError: "\xF1" followed by "a" on UTF-8>
4280 * p ec.primitive_convert(src, dst, nil, 1) #=> :destination_buffer_full
4281 * p ec.last_error #=> nil
4282 *
4283 */
4284static VALUE
4285econv_last_error(VALUE self)
4286{
4287 rb_econv_t *ec = check_econv(self);
4288 VALUE exc;
4289
4290 exc = make_econv_exception(ec);
4291 if (NIL_P(exc))
4292 return Qnil;
4293 return exc;
4294}
4295
4296/*
4297 * call-seq:
4298 * ec.replacement -> string
4299 *
4300 * Returns the replacement string.
4301 *
4302 * ec = Encoding::Converter.new("euc-jp", "us-ascii")
4303 * p ec.replacement #=> "?"
4304 *
4305 * ec = Encoding::Converter.new("euc-jp", "utf-8")
4306 * p ec.replacement #=> "\uFFFD"
4307 */
4308static VALUE
4309econv_get_replacement(VALUE self)
4310{
4311 rb_econv_t *ec = check_econv(self);
4312 int ret;
4313 rb_encoding *enc;
4314
4315 ret = make_replacement(ec);
4316 if (ret == -1) {
4317 rb_raise(rb_eUndefinedConversionError, "replacement character setup failed");
4318 }
4319
4320 enc = rb_enc_find(ec->replacement_enc);
4321 return rb_enc_str_new((const char *)ec->replacement_str, (long)ec->replacement_len, enc);
4322}
4323
4324/*
4325 * call-seq:
4326 * ec.replacement = string
4327 *
4328 * Sets the replacement string.
4329 *
4330 * ec = Encoding::Converter.new("utf-8", "us-ascii", :undef => :replace)
4331 * ec.replacement = "<undef>"
4332 * p ec.convert("a \u3042 b") #=> "a <undef> b"
4333 */
4334static VALUE
4335econv_set_replacement(VALUE self, VALUE arg)
4336{
4337 rb_econv_t *ec = check_econv(self);
4338 VALUE string = arg;
4339 int ret;
4340 rb_encoding *enc;
4341
4342 StringValue(string);
4343 enc = rb_enc_get(string);
4344
4345 ret = rb_econv_set_replacement(ec,
4346 (const unsigned char *)RSTRING_PTR(string),
4347 RSTRING_LEN(string),
4348 rb_enc_name(enc));
4349
4350 if (ret == -1) {
4351 /* xxx: rb_eInvalidByteSequenceError? */
4352 rb_raise(rb_eUndefinedConversionError, "replacement character setup failed");
4353 }
4354
4355 return arg;
4356}
4357
4358VALUE
4360{
4361 return make_econv_exception(ec);
4362}
4363
4364void
4366{
4367 VALUE exc;
4368
4369 exc = make_econv_exception(ec);
4370 if (NIL_P(exc))
4371 return;
4372 rb_exc_raise(exc);
4373}
4374
4375/*
4376 * call-seq:
4377 * ecerr.source_encoding_name -> string
4378 *
4379 * Returns the source encoding name as a string.
4380 */
4381static VALUE
4382ecerr_source_encoding_name(VALUE self)
4383{
4384 return rb_attr_get(self, id_source_encoding_name);
4385}
4386
4387/*
4388 * call-seq:
4389 * ecerr.source_encoding -> encoding
4390 *
4391 * Returns the source encoding as an encoding object.
4392 *
4393 * Note that the result may not be equal to the source encoding of
4394 * the encoding converter if the conversion has multiple steps.
4395 *
4396 * ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP") # ISO-8859-1 -> UTF-8 -> EUC-JP
4397 * begin
4398 * ec.convert("\xa0") # NO-BREAK SPACE, which is available in UTF-8 but not in EUC-JP.
4399 * rescue Encoding::UndefinedConversionError
4400 * p $!.source_encoding #=> #<Encoding:UTF-8>
4401 * p $!.destination_encoding #=> #<Encoding:EUC-JP>
4402 * p $!.source_encoding_name #=> "UTF-8"
4403 * p $!.destination_encoding_name #=> "EUC-JP"
4404 * end
4405 *
4406 */
4407static VALUE
4408ecerr_source_encoding(VALUE self)
4409{
4410 return rb_attr_get(self, id_source_encoding);
4411}
4412
4413/*
4414 * call-seq:
4415 * ecerr.destination_encoding_name -> string
4416 *
4417 * Returns the destination encoding name as a string.
4418 */
4419static VALUE
4420ecerr_destination_encoding_name(VALUE self)
4421{
4422 return rb_attr_get(self, id_destination_encoding_name);
4423}
4424
4425/*
4426 * call-seq:
4427 * ecerr.destination_encoding -> string
4428 *
4429 * Returns the destination encoding as an encoding object.
4430 */
4431static VALUE
4432ecerr_destination_encoding(VALUE self)
4433{
4434 return rb_attr_get(self, id_destination_encoding);
4435}
4436
4437/*
4438 * call-seq:
4439 * ecerr.error_char -> string
4440 *
4441 * Returns the one-character string which cause Encoding::UndefinedConversionError.
4442 *
4443 * ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP")
4444 * begin
4445 * ec.convert("\xa0")
4446 * rescue Encoding::UndefinedConversionError
4447 * puts $!.error_char.dump #=> "\xC2\xA0"
4448 * p $!.error_char.encoding #=> #<Encoding:UTF-8>
4449 * end
4450 *
4451 */
4452static VALUE
4453ecerr_error_char(VALUE self)
4454{
4455 return rb_attr_get(self, id_error_char);
4456}
4457
4458/*
4459 * call-seq:
4460 * ecerr.error_bytes -> string
4461 *
4462 * Returns the discarded bytes when Encoding::InvalidByteSequenceError occurs.
4463 *
4464 * ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
4465 * begin
4466 * ec.convert("abc\xA1\xFFdef")
4467 * rescue Encoding::InvalidByteSequenceError
4468 * p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "\xFF" on EUC-JP>
4469 * puts $!.error_bytes.dump #=> "\xA1"
4470 * puts $!.readagain_bytes.dump #=> "\xFF"
4471 * end
4472 */
4473static VALUE
4474ecerr_error_bytes(VALUE self)
4475{
4476 return rb_attr_get(self, id_error_bytes);
4477}
4478
4479/*
4480 * call-seq:
4481 * ecerr.readagain_bytes -> string
4482 *
4483 * Returns the bytes to be read again when Encoding::InvalidByteSequenceError occurs.
4484 */
4485static VALUE
4486ecerr_readagain_bytes(VALUE self)
4487{
4488 return rb_attr_get(self, id_readagain_bytes);
4489}
4490
4491/*
4492 * call-seq:
4493 * ecerr.incomplete_input? -> true or false
4494 *
4495 * Returns true if the invalid byte sequence error is caused by
4496 * premature end of string.
4497 *
4498 * ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
4499 *
4500 * begin
4501 * ec.convert("abc\xA1z")
4502 * rescue Encoding::InvalidByteSequenceError
4503 * p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "z" on EUC-JP>
4504 * p $!.incomplete_input? #=> false
4505 * end
4506 *
4507 * begin
4508 * ec.convert("abc\xA1")
4509 * ec.finish
4510 * rescue Encoding::InvalidByteSequenceError
4511 * p $! #=> #<Encoding::InvalidByteSequenceError: incomplete "\xA1" on EUC-JP>
4512 * p $!.incomplete_input? #=> true
4513 * end
4514 */
4515static VALUE
4516ecerr_incomplete_input(VALUE self)
4517{
4518 return rb_attr_get(self, id_incomplete_input);
4519}
4520
4521/*
4522 * Document-class: Encoding::UndefinedConversionError
4523 *
4524 * Raised by Encoding and String methods when a transcoding operation
4525 * fails.
4526 */
4527
4528/*
4529 * Document-class: Encoding::InvalidByteSequenceError
4530 *
4531 * Raised by Encoding and String methods when the string being
4532 * transcoded contains a byte invalid for the either the source or
4533 * target encoding.
4534 */
4535
4536/*
4537 * Document-class: Encoding::ConverterNotFoundError
4538 *
4539 * Raised by transcoding methods when a named encoding does not
4540 * correspond with a known converter.
4541 */
4542
4543void
4544Init_transcode(void)
4545{
4546 transcoder_table = st_init_strcasetable();
4547
4548 id_destination_encoding = rb_intern_const("destination_encoding");
4549 id_destination_encoding_name = rb_intern_const("destination_encoding_name");
4550 id_error_bytes = rb_intern_const("error_bytes");
4551 id_error_char = rb_intern_const("error_char");
4552 id_incomplete_input = rb_intern_const("incomplete_input");
4553 id_readagain_bytes = rb_intern_const("readagain_bytes");
4554 id_source_encoding = rb_intern_const("source_encoding");
4555 id_source_encoding_name = rb_intern_const("source_encoding_name");
4556
4557 sym_invalid = ID2SYM(rb_intern_const("invalid"));
4558 sym_undef = ID2SYM(rb_intern_const("undef"));
4559 sym_replace = ID2SYM(rb_intern_const("replace"));
4560 sym_fallback = ID2SYM(rb_intern_const("fallback"));
4561 sym_xml = ID2SYM(rb_intern_const("xml"));
4562 sym_text = ID2SYM(rb_intern_const("text"));
4563 sym_attr = ID2SYM(rb_intern_const("attr"));
4564
4565 sym_invalid_byte_sequence = ID2SYM(rb_intern_const("invalid_byte_sequence"));
4566 sym_undefined_conversion = ID2SYM(rb_intern_const("undefined_conversion"));
4567 sym_destination_buffer_full = ID2SYM(rb_intern_const("destination_buffer_full"));
4568 sym_source_buffer_empty = ID2SYM(rb_intern_const("source_buffer_empty"));
4569 sym_finished = ID2SYM(rb_intern_const("finished"));
4570 sym_after_output = ID2SYM(rb_intern_const("after_output"));
4571 sym_incomplete_input = ID2SYM(rb_intern_const("incomplete_input"));
4572 sym_universal_newline = ID2SYM(rb_intern_const("universal_newline"));
4573 sym_crlf_newline = ID2SYM(rb_intern_const("crlf_newline"));
4574 sym_cr_newline = ID2SYM(rb_intern_const("cr_newline"));
4575 sym_lf_newline = ID2SYM(rb_intern("lf_newline"));
4576 sym_partial_input = ID2SYM(rb_intern_const("partial_input"));
4577
4578#ifdef ENABLE_ECONV_NEWLINE_OPTION
4579 sym_newline = ID2SYM(rb_intern_const("newline"));
4580 sym_universal = ID2SYM(rb_intern_const("universal"));
4581 sym_crlf = ID2SYM(rb_intern_const("crlf"));
4582 sym_cr = ID2SYM(rb_intern_const("cr"));
4583 sym_lf = ID2SYM(rb_intern_const("lf"));
4584#endif
4585
4586 InitVM(transcode);
4587}
4588
4589void
4590InitVM_transcode(void)
4591{
4592 rb_eUndefinedConversionError = rb_define_class_under(rb_cEncoding, "UndefinedConversionError", rb_eEncodingError);
4593 rb_eInvalidByteSequenceError = rb_define_class_under(rb_cEncoding, "InvalidByteSequenceError", rb_eEncodingError);
4594 rb_eConverterNotFoundError = rb_define_class_under(rb_cEncoding, "ConverterNotFoundError", rb_eEncodingError);
4595
4596 rb_define_method(rb_cString, "encode", str_encode, -1);
4597 rb_define_method(rb_cString, "encode!", str_encode_bang, -1);
4598
4599 rb_cEncodingConverter = rb_define_class_under(rb_cEncoding, "Converter", rb_cObject);
4600 rb_define_alloc_func(rb_cEncodingConverter, econv_s_allocate);
4601 rb_define_singleton_method(rb_cEncodingConverter, "asciicompat_encoding", econv_s_asciicompat_encoding, 1);
4602 rb_define_singleton_method(rb_cEncodingConverter, "search_convpath", econv_s_search_convpath, -1);
4603 rb_define_method(rb_cEncodingConverter, "initialize", econv_init, -1);
4604 rb_define_method(rb_cEncodingConverter, "inspect", econv_inspect, 0);
4605 rb_define_method(rb_cEncodingConverter, "convpath", econv_convpath, 0);
4606 rb_define_method(rb_cEncodingConverter, "source_encoding", econv_source_encoding, 0);
4607 rb_define_method(rb_cEncodingConverter, "destination_encoding", econv_destination_encoding, 0);
4608 rb_define_method(rb_cEncodingConverter, "primitive_convert", econv_primitive_convert, -1);
4609 rb_define_method(rb_cEncodingConverter, "convert", econv_convert, 1);
4610 rb_define_method(rb_cEncodingConverter, "finish", econv_finish, 0);
4611 rb_define_method(rb_cEncodingConverter, "primitive_errinfo", econv_primitive_errinfo, 0);
4612 rb_define_method(rb_cEncodingConverter, "insert_output", econv_insert_output, 1);
4613 rb_define_method(rb_cEncodingConverter, "putback", econv_putback, -1);
4614 rb_define_method(rb_cEncodingConverter, "last_error", econv_last_error, 0);
4615 rb_define_method(rb_cEncodingConverter, "replacement", econv_get_replacement, 0);
4616 rb_define_method(rb_cEncodingConverter, "replacement=", econv_set_replacement, 1);
4617 rb_define_method(rb_cEncodingConverter, "==", econv_equal, 1);
4618
4619 /*
4620 *Mask for invalid byte sequences
4621 */
4622 rb_define_const(rb_cEncodingConverter, "INVALID_MASK", INT2FIX(ECONV_INVALID_MASK));
4623
4624 /*
4625 * Replace invalid byte sequences
4626 */
4627 rb_define_const(rb_cEncodingConverter, "INVALID_REPLACE", INT2FIX(ECONV_INVALID_REPLACE));
4628
4629 /*
4630 * Mask for a valid character in the source encoding but no related
4631 * character(s) in destination encoding.
4632 */
4633 rb_define_const(rb_cEncodingConverter, "UNDEF_MASK", INT2FIX(ECONV_UNDEF_MASK));
4634
4635 /*
4636 * Replace byte sequences that are undefined in the destination encoding.
4637 */
4638 rb_define_const(rb_cEncodingConverter, "UNDEF_REPLACE", INT2FIX(ECONV_UNDEF_REPLACE));
4639
4640 /*
4641 * Replace byte sequences that are undefined in the destination encoding
4642 * with an XML hexadecimal character reference. This is valid for XML
4643 * conversion.
4644 */
4645 rb_define_const(rb_cEncodingConverter, "UNDEF_HEX_CHARREF", INT2FIX(ECONV_UNDEF_HEX_CHARREF));
4646
4647 /*
4648 * Indicates the source may be part of a larger string. See
4649 * primitive_convert for an example.
4650 */
4651 rb_define_const(rb_cEncodingConverter, "PARTIAL_INPUT", INT2FIX(ECONV_PARTIAL_INPUT));
4652
4653 /*
4654 * Stop converting after some output is complete but before all of the
4655 * input was consumed. See primitive_convert for an example.
4656 */
4657 rb_define_const(rb_cEncodingConverter, "AFTER_OUTPUT", INT2FIX(ECONV_AFTER_OUTPUT));
4658
4659 /*
4660 * Decorator for converting CRLF and CR to LF
4661 */
4662 rb_define_const(rb_cEncodingConverter, "UNIVERSAL_NEWLINE_DECORATOR", INT2FIX(ECONV_UNIVERSAL_NEWLINE_DECORATOR));
4663
4664 /*
4665 * Decorator for converting CRLF and CR to LF when writing
4666 */
4667 rb_define_const(rb_cEncodingConverter, "LF_NEWLINE_DECORATOR", INT2FIX(ECONV_LF_NEWLINE_DECORATOR));
4668
4669 /*
4670 * Decorator for converting LF to CRLF
4671 */
4672 rb_define_const(rb_cEncodingConverter, "CRLF_NEWLINE_DECORATOR", INT2FIX(ECONV_CRLF_NEWLINE_DECORATOR));
4673
4674 /*
4675 * Decorator for converting LF to CR
4676 */
4677 rb_define_const(rb_cEncodingConverter, "CR_NEWLINE_DECORATOR", INT2FIX(ECONV_CR_NEWLINE_DECORATOR));
4678
4679 /*
4680 * Escape as XML CharData
4681 */
4682 rb_define_const(rb_cEncodingConverter, "XML_TEXT_DECORATOR", INT2FIX(ECONV_XML_TEXT_DECORATOR));
4683
4684 /*
4685 * Escape as XML AttValue
4686 */
4687 rb_define_const(rb_cEncodingConverter, "XML_ATTR_CONTENT_DECORATOR", INT2FIX(ECONV_XML_ATTR_CONTENT_DECORATOR));
4688
4689 /*
4690 * Escape as XML AttValue
4691 */
4692 rb_define_const(rb_cEncodingConverter, "XML_ATTR_QUOTE_DECORATOR", INT2FIX(ECONV_XML_ATTR_QUOTE_DECORATOR));
4693
4694 rb_define_method(rb_eUndefinedConversionError, "source_encoding_name", ecerr_source_encoding_name, 0);
4695 rb_define_method(rb_eUndefinedConversionError, "destination_encoding_name", ecerr_destination_encoding_name, 0);
4696 rb_define_method(rb_eUndefinedConversionError, "source_encoding", ecerr_source_encoding, 0);
4697 rb_define_method(rb_eUndefinedConversionError, "destination_encoding", ecerr_destination_encoding, 0);
4698 rb_define_method(rb_eUndefinedConversionError, "error_char", ecerr_error_char, 0);
4699
4700 rb_define_method(rb_eInvalidByteSequenceError, "source_encoding_name", ecerr_source_encoding_name, 0);
4701 rb_define_method(rb_eInvalidByteSequenceError, "destination_encoding_name", ecerr_destination_encoding_name, 0);
4702 rb_define_method(rb_eInvalidByteSequenceError, "source_encoding", ecerr_source_encoding, 0);
4703 rb_define_method(rb_eInvalidByteSequenceError, "destination_encoding", ecerr_destination_encoding, 0);
4704 rb_define_method(rb_eInvalidByteSequenceError, "error_bytes", ecerr_error_bytes, 0);
4705 rb_define_method(rb_eInvalidByteSequenceError, "readagain_bytes", ecerr_readagain_bytes, 0);
4706 rb_define_method(rb_eInvalidByteSequenceError, "incomplete_input?", ecerr_incomplete_input, 0);
4707
4708 Init_newline();
4709}
ruby_coderange_type
What rb_enc_str_coderange() returns.
Definition coderange.h:33
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:3384
#define ECONV_XML_ATTR_QUOTE_DECORATOR
Old name of RUBY_ECONV_XML_ATTR_QUOTE_DECORATOR.
Definition transcode.h:539
#define ECONV_AFTER_OUTPUT
Old name of RUBY_ECONV_AFTER_OUTPUT.
Definition transcode.h:555
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1676
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
#define ENC_CODERANGE_VALID
Old name of RUBY_ENC_CODERANGE_VALID.
Definition coderange.h:181
#define ECONV_UNIVERSAL_NEWLINE_DECORATOR
Old name of RUBY_ECONV_UNIVERSAL_NEWLINE_DECORATOR.
Definition transcode.h:532
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
Definition fl_type.h:133
#define ECONV_XML_ATTR_CONTENT_DECORATOR
Old name of RUBY_ECONV_XML_ATTR_CONTENT_DECORATOR.
Definition transcode.h:537
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1684
#define ECONV_INVALID_MASK
Old name of RUBY_ECONV_INVALID_MASK.
Definition transcode.h:523
#define ECONV_CRLF_NEWLINE_DECORATOR
Old name of RUBY_ECONV_CRLF_NEWLINE_DECORATOR.
Definition transcode.h:533
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:131
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
Definition transcode.h:526
#define ECONV_XML_TEXT_DECORATOR
Old name of RUBY_ECONV_XML_TEXT_DECORATOR.
Definition transcode.h:536
#define rb_ary_new4
Old name of rb_ary_new_from_values.
Definition array.h:659
#define ENC_CODERANGE_UNKNOWN
Old name of RUBY_ENC_CODERANGE_UNKNOWN.
Definition coderange.h:179
#define ECONV_CR_NEWLINE_DECORATOR
Old name of RUBY_ECONV_CR_NEWLINE_DECORATOR.
Definition transcode.h:534
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define ECONV_INVALID_REPLACE
Old name of RUBY_ECONV_INVALID_REPLACE.
Definition transcode.h:524
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:399
#define MBCLEN_CHARFOUND_LEN(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_LEN.
Definition encoding.h:517
#define rb_exc_new3
Old name of rb_exc_new_str.
Definition error.h:38
#define ECONV_UNDEF_MASK
Old name of RUBY_ECONV_UNDEF_MASK.
Definition transcode.h:525
#define Qtrue
Old name of RUBY_Qtrue.
#define ECONV_PARTIAL_INPUT
Old name of RUBY_ECONV_PARTIAL_INPUT.
Definition transcode.h:554
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define ECONV_ERROR_HANDLER_MASK
Old name of RUBY_ECONV_ERROR_HANDLER_MASK.
Definition transcode.h:522
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define ENC_CODERANGE_BROKEN
Old name of RUBY_ENC_CODERANGE_BROKEN.
Definition coderange.h:182
#define ECONV_LF_NEWLINE_DECORATOR
Old name of RUBY_ECONV_LF_NEWLINE_DECORATOR.
Definition transcode.h:535
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
Definition encoding.h:516
#define ECONV_UNDEF_HEX_CHARREF
Old name of RUBY_ECONV_UNDEF_HEX_CHARREF.
Definition transcode.h:527
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define ECONV_NEWLINE_DECORATOR_MASK
Old name of RUBY_ECONV_NEWLINE_DECORATOR_MASK.
Definition transcode.h:529
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
#define ENC_CODERANGE_SET(obj, cr)
Old name of RB_ENC_CODERANGE_SET.
Definition coderange.h:186
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:678
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1473
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1471
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Identical to rb_typeddata_is_kind_of(), except it raises exceptions instead of returning false.
Definition error.c:1459
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1524
VALUE rb_eEncodingError
EncodingError exception.
Definition error.c:1479
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:499
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:234
VALUE rb_cEncoding
Encoding class.
Definition encoding.c:60
VALUE rb_cString
String class.
Definition string.c:85
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3328
Encoding relates APIs.
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
Definition string.c:1379
long rb_str_coderange_scan_restartable(const char *str, const char *end, rb_encoding *enc, int *cr)
Scans the passed string until it finds something odd.
Definition string.c:844
int rb_econv_prepare_options(VALUE opthash, VALUE *ecopts, int ecflags)
Identical to rb_econv_prepare_opts(), except it additionally takes the initial value of flags.
Definition transcode.c:2679
VALUE rb_econv_open_exc(const char *senc, const char *denc, int ecflags)
Creates a rb_eConverterNotFoundError exception object (but does not raise).
Definition transcode.c:2126
const char * rb_econv_encoding_to_insert_output(rb_econv_t *ec)
Queries an encoding name which best suits for rb_econv_insert_output()'s last parameter.
Definition transcode.c:1544
int rb_econv_prepare_opts(VALUE opthash, VALUE *ecopts)
Splits a keyword arguments hash (that for instance String#encode took) into a set of enum ruby_econv_...
Definition transcode.c:2724
rb_econv_result_t rb_econv_convert(rb_econv_t *ec, const unsigned char **source_buffer_ptr, const unsigned char *source_buffer_end, unsigned char **destination_buffer_ptr, unsigned char *destination_buffer_end, int flags)
Converts a string from an encoding to another.
Definition transcode.c:1487
rb_econv_result_t
return value of rb_econv_convert()
Definition transcode.h:30
@ econv_incomplete_input
The conversion stopped in middle of reading a character, possibly due to a partial read of a socket e...
Definition transcode.h:69
@ econv_finished
The conversion stopped after converting everything.
Definition transcode.h:57
@ econv_undefined_conversion
The conversion stopped when it found a character in the input which cannot be representable in the ou...
Definition transcode.h:41
@ econv_after_output
The conversion stopped after writing something to somewhere, before reading everything.
Definition transcode.h:63
@ econv_source_buffer_empty
The conversion stopped because there is no input.
Definition transcode.h:51
@ econv_destination_buffer_full
The conversion stopped because there is no destination.
Definition transcode.h:46
@ econv_invalid_byte_sequence
The conversion stopped when it found an invalid sequence.
Definition transcode.h:35
int rb_econv_putbackable(rb_econv_t *ec)
Queries if rb_econv_putback() makes sense, i.e.
Definition transcode.c:1783
int rb_econv_has_convpath_p(const char *from_encoding, const char *to_encoding)
Queries if there is more than one way to convert between the passed two encodings.
Definition transcode.c:3298
rb_econv_t * rb_econv_open(const char *source_encoding, const char *destination_encoding, int ecflags)
Creates a new instance of struct rb_econv_t.
Definition transcode.c:1108
VALUE rb_econv_str_append(rb_econv_t *ec, VALUE src, VALUE dst, int flags)
Identical to rb_econv_str_convert(), except it appends the conversion result to the additionally pass...
Definition transcode.c:1950
VALUE rb_econv_substr_append(rb_econv_t *ec, VALUE src, long byteoff, long bytesize, VALUE dst, int flags)
Identical to rb_econv_str_append(), except it appends only a part of the passed string with conversio...
Definition transcode.c:1941
const char * rb_econv_asciicompat_encoding(const char *encname)
Queries the passed encoding's corresponding ASCII compatible encoding.
Definition transcode.c:1827
int rb_econv_insert_output(rb_econv_t *ec, const unsigned char *str, size_t len, const char *str_encoding)
Appends the passed string to the passed converter's output buffer.
Definition transcode.c:1629
VALUE rb_econv_str_convert(rb_econv_t *ec, VALUE src, int flags)
Identical to rb_econv_convert(), except it takes Ruby's string instead of C's pointer.
Definition transcode.c:1962
rb_econv_t * rb_econv_open_opts(const char *source_encoding, const char *destination_encoding, int ecflags, VALUE ecopts)
Identical to rb_econv_open(), except it additionally takes a hash of optional strings.
Definition transcode.c:2730
int rb_econv_decorate_at_last(rb_econv_t *ec, const char *decorator_name)
Identical to rb_econv_decorate_at_first(), except it adds to the opposite direction.
Definition transcode.c:2008
void rb_econv_binmode(rb_econv_t *ec)
This badly named function does not set the destination encoding to binary, but instead just nullifies...
Definition transcode.c:2025
int rb_econv_decorate_at_first(rb_econv_t *ec, const char *decorator_name)
"Decorate"s a converter.
Definition transcode.c:1991
VALUE rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts)
Converts the contents of the passed string from its encoding to the passed one.
Definition transcode.c:2993
VALUE rb_econv_make_exception(rb_econv_t *ec)
This function makes sense right after rb_econv_convert() returns.
Definition transcode.c:4359
void rb_econv_check_error(rb_econv_t *ec)
This is a rb_econv_make_exception() + rb_exc_raise() combo.
Definition transcode.c:4365
VALUE rb_econv_substr_convert(rb_econv_t *ec, VALUE src, long byteoff, long bytesize, int flags)
Identical to rb_econv_str_convert(), except it converts only a part of the passed string.
Definition transcode.c:1956
void rb_econv_close(rb_econv_t *ec)
Destructs a converter.
Definition transcode.c:1744
VALUE rb_econv_append(rb_econv_t *ec, const char *bytesrc, long bytesize, VALUE dst, int flags)
Converts the passed C's pointer according to the passed converter, then append the conversion result ...
Definition transcode.c:1878
void rb_econv_putback(rb_econv_t *ec, unsigned char *p, int n)
Puts back the bytes.
Definition transcode.c:1794
int rb_econv_set_replacement(rb_econv_t *ec, const unsigned char *str, size_t len, const char *encname)
Assigns the replacement string.
Definition transcode.c:2289
VALUE rb_funcallv_public(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it only takes public methods into account.
Definition vm_eval.c:1174
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
VALUE rb_proc_call(VALUE recv, VALUE args)
Evaluates the passed proc with the passed arguments.
Definition proc.c:1738
VALUE rb_obj_is_method(VALUE recv)
Queries if the given object is a method.
Definition proc.c:2454
VALUE rb_method_call(int argc, const VALUE *argv, VALUE recv)
Evaluates the passed method with the passed arguments.
Definition proc.c:3345
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:386
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
Definition string.c:1791
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1499
void rb_str_shared_replace(VALUE dst, VALUE src)
Replaces the contents of the former with the latter.
Definition string.c:1833
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
Definition string.c:1023
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition string.c:1555
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:2031
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3493
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition string.c:2809
VALUE rb_str_dump(VALUE str)
"Inverse" of rb_eval_string().
Definition string.c:8262
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
Definition string.c:1763
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1515
VALUE rb_str_drop_bytes(VALUE str, long len)
Shrinks the given string for the given number of bytes.
Definition string.c:5852
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:2141
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3683
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:1148
int off
Offset inside of ptr.
Definition io.h:5
int len
Length of the buffer.
Definition io.h:8
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
#define ALLOCA_N(type, n)
Definition memory.h:292
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
Definition memory.h:384
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:50
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:280
#define RARRAY_AREF(a, i)
Definition rarray.h:402
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:409
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:89
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:773
#define DATA_PTR(obj)
Convenient casting macro for backward compatibility.
Definition rtypeddata.h:439
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:557
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:533
#define InitVM(ext)
This macro is for internal use.
Definition ruby.h:231
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:242
Definition st.h:79
Definition string.c:9186
Definition transcode.c:179
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376