Ruby 4.1.0dev (2026-11-16 revision 9c7bc705b8bd4542beac71542a8ea79f9bb7622b)
transcode.c (9c7bc705b8bd4542beac71542a8ea79f9bb7622b)
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, rb_len_t 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 rb_len_t dlen = RSTRING_LEN(dst);
1907 if (rb_str_capacity(dst) - dlen < (size_t)len + max_output) {
1908 rb_ulen_t new_capa = (rb_ulen_t)dlen + len + max_output;
1909 if (RB_LEN_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
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, rb_len_t byteoff, rb_len_t 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 if (len > (size_t)(RB_LEN_MAX / 2 - max_output))
2248 rb_raise(rb_eArgError, "string size too big");
2249 size_t new_len = (len + max_output) * 2;
2250 *out_start_ptr = resize_destination(destination, len, new_len);
2251 *out_pos = *out_start_ptr + len;
2252 *out_stop_ptr = *out_start_ptr + new_len;
2253}
2254
2255static int
2256make_replacement(rb_econv_t *ec)
2257{
2258 rb_transcoding *tc;
2259 const rb_transcoder *tr;
2260 const unsigned char *replacement;
2261 const char *repl_enc;
2262 const char *ins_enc;
2263 size_t len;
2264
2265 if (ec->replacement_str)
2266 return 0;
2267
2269
2270 tc = ec->last_tc;
2271 if (*ins_enc) {
2272 tr = tc->transcoder;
2273 rb_enc_find(tr->dst_encoding);
2274 replacement = (const unsigned char *)get_replacement_character(ins_enc, &len, &repl_enc);
2275 }
2276 else {
2277 replacement = (unsigned char *)"?";
2278 len = 1;
2279 repl_enc = "";
2280 }
2281
2282 ec->replacement_str = replacement;
2283 ec->replacement_len = len;
2284 ec->replacement_bufsize = len;
2285 ec->replacement_enc = repl_enc;
2286 ec->replacement_allocated = 0;
2287 return 0;
2288}
2289
2290int
2292 const unsigned char *str, size_t len, const char *encname)
2293{
2294 unsigned char *str2;
2295 size_t len2, buf_size2;
2296 const char *encname2;
2297
2299
2300 if (!*encname2 || encoding_equal(encname, encname2)) {
2301 str2 = xmalloc(len);
2302 MEMCPY(str2, str, unsigned char, len); /* xxx: str may be invalid */
2303 buf_size2 = len2 = len;
2304 encname2 = encname;
2305 }
2306 else {
2307 str2 = allocate_converted_string(encname, encname2, str, len, NULL, 0, &len2, &buf_size2);
2308 if (!str2)
2309 return -1;
2310 }
2311
2312 if (ec->replacement_allocated) {
2313 SIZED_FREE_N((char *)ec->replacement_str, ec->replacement_bufsize);
2314 }
2315 ec->replacement_allocated = 1;
2316 ec->replacement_str = str2;
2317 ec->replacement_len = len2;
2318 ec->replacement_bufsize = buf_size2;
2319 ec->replacement_enc = encname2;
2320 return 0;
2321}
2322
2323static int
2324output_replacement_character(rb_econv_t *ec)
2325{
2326 int ret;
2327
2328 if (make_replacement(ec) == -1)
2329 return -1;
2330
2331 ret = rb_econv_insert_output(ec, ec->replacement_str, ec->replacement_len, ec->replacement_enc);
2332 if (ret == -1)
2333 return -1;
2334
2335 return 0;
2336}
2337
2338#if 1
2339#define hash_fallback rb_hash_aref
2340
2341static VALUE
2342proc_fallback(VALUE fallback, VALUE c)
2343{
2344 return rb_proc_call(fallback, rb_ary_new4(1, &c));
2345}
2346
2347static VALUE
2348method_fallback(VALUE fallback, VALUE c)
2349{
2350 return rb_method_call(1, &c, fallback);
2351}
2352
2353static VALUE
2354aref_fallback(VALUE fallback, VALUE c)
2355{
2356 return rb_funcallv_public(fallback, idAREF, 1, &c);
2357}
2358
2360 VALUE (*fallback_func)(VALUE, VALUE);
2361 VALUE fallback;
2362 VALUE rep;
2363};
2364
2365static VALUE
2366transcode_loop_fallback_try(VALUE a)
2367{
2369
2370 VALUE ret = args->fallback_func(args->fallback, args->rep);
2371
2372 if (!NIL_OR_UNDEF_P(ret)) {
2373 StringValue(ret);
2374 }
2375
2376 return ret;
2377}
2378
2379static void
2380transcode_loop(const unsigned char **in_pos, unsigned char **out_pos,
2381 const unsigned char *in_stop, unsigned char *out_stop,
2382 VALUE destination,
2383 unsigned char *(*resize_destination)(VALUE, size_t, size_t),
2384 const char *src_encoding,
2385 const char *dst_encoding,
2386 int ecflags,
2387 VALUE ecopts,
2388 VALUE source)
2389{
2390 rb_econv_t *ec;
2391 rb_transcoding *last_tc;
2393 unsigned char *out_start = *out_pos;
2394 int max_output;
2395 VALUE exc;
2396 VALUE fallback = Qnil;
2397 VALUE (*fallback_func)(VALUE, VALUE) = 0;
2398 const unsigned char *source_start = *in_pos;
2399 rb_len_t source_len = in_stop - *in_pos;
2400
2401 ec = rb_econv_open_opts(src_encoding, dst_encoding, ecflags, ecopts);
2402 if (!ec)
2403 rb_exc_raise(rb_econv_open_exc(src_encoding, dst_encoding, ecflags));
2404
2405 if (!NIL_P(ecopts) && RB_TYPE_P(ecopts, T_HASH)) {
2406 fallback = rb_hash_aref(ecopts, sym_fallback);
2407 if (RB_TYPE_P(fallback, T_HASH)) {
2408 fallback_func = hash_fallback;
2409 }
2410 else if (rb_obj_is_proc(fallback)) {
2411 fallback_func = proc_fallback;
2412 }
2413 else if (rb_obj_is_method(fallback)) {
2414 fallback_func = method_fallback;
2415 }
2416 else {
2417 fallback_func = aref_fallback;
2418 }
2419 }
2420 last_tc = ec->last_tc;
2421 max_output = last_tc ? last_tc->transcoder->max_output : 1;
2422
2423 resume:
2424 ret = rb_econv_convert(ec, in_pos, in_stop, out_pos, out_stop, 0);
2425
2426 if (!NIL_P(fallback) && ret == econv_undefined_conversion) {
2427 VALUE rep = rb_enc_str_new(
2428 (const char *)ec->last_error.error_bytes_start,
2429 ec->last_error.error_bytes_len,
2430 rb_enc_find(ec->last_error.source_encoding));
2431
2432
2433 struct transcode_loop_fallback_args args = {
2434 .fallback_func = fallback_func,
2435 .fallback = fallback,
2436 .rep = rep,
2437 };
2438
2439 int state;
2440 rep = rb_protect(transcode_loop_fallback_try, (VALUE)&args, &state);
2441 if (state) {
2442 rb_econv_close(ec);
2443 rb_jump_tag(state);
2444 }
2445
2446 /* Ruby code run during the conversion (e.g. the fallback) may have
2447 * modified the source string, invalidating the pointers into its
2448 * buffer. */
2449 if ((const unsigned char *)RSTRING_PTR(source) != source_start ||
2450 RSTRING_LEN(source) != source_len) {
2451 rb_econv_close(ec);
2452 rb_raise(rb_eRuntimeError, "string modified");
2453 }
2454
2455 if (!NIL_OR_UNDEF_P(rep)) {
2456 ret = rb_econv_insert_output(ec, (const unsigned char *)RSTRING_PTR(rep),
2457 RSTRING_LEN(rep), rb_enc_name(rb_enc_get(rep)));
2458 RB_GC_GUARD(rep); // insert_output may GC while reading rep's bytes
2459 if ((int)ret == -1) {
2460 rb_econv_close(ec);
2461 rb_raise(rb_eArgError, "too big fallback string");
2462 }
2463 goto resume;
2464 }
2465 }
2466
2467 if (ret == econv_invalid_byte_sequence ||
2468 ret == econv_incomplete_input ||
2470 exc = make_econv_exception(ec);
2471 rb_econv_close(ec);
2472 rb_exc_raise(exc);
2473 }
2474
2475 if (ret == econv_destination_buffer_full) {
2476 more_output_buffer(destination, resize_destination, max_output, &out_start, out_pos, &out_stop);
2477 goto resume;
2478 }
2479
2480 rb_econv_close(ec);
2481 return;
2482}
2483#else
2484/* sample transcode_loop implementation in byte-by-byte stream style */
2485static void
2486transcode_loop(const unsigned char **in_pos, unsigned char **out_pos,
2487 const unsigned char *in_stop, unsigned char *out_stop,
2488 VALUE destination,
2489 unsigned char *(*resize_destination)(VALUE, size_t, size_t),
2490 const char *src_encoding,
2491 const char *dst_encoding,
2492 int ecflags,
2493 VALUE ecopts,
2494 VALUE source)
2495{
2496 rb_econv_t *ec;
2497 rb_transcoding *last_tc;
2499 unsigned char *out_start = *out_pos;
2500 const unsigned char *ptr;
2501 int max_output;
2502 VALUE exc;
2503
2504 ec = rb_econv_open_opts(src_encoding, dst_encoding, ecflags, ecopts);
2505 if (!ec)
2506 rb_exc_raise(rb_econv_open_exc(src_encoding, dst_encoding, ecflags));
2507
2508 last_tc = ec->last_tc;
2509 max_output = last_tc ? last_tc->transcoder->max_output : 1;
2510
2512 ptr = *in_pos;
2513 while (ret != econv_finished) {
2514 unsigned char input_byte;
2515 const unsigned char *p = &input_byte;
2516
2517 if (ret == econv_source_buffer_empty) {
2518 if (ptr < in_stop) {
2519 input_byte = *ptr;
2520 ret = rb_econv_convert(ec, &p, p+1, out_pos, out_stop, ECONV_PARTIAL_INPUT);
2521 }
2522 else {
2523 ret = rb_econv_convert(ec, NULL, NULL, out_pos, out_stop, 0);
2524 }
2525 }
2526 else {
2527 ret = rb_econv_convert(ec, NULL, NULL, out_pos, out_stop, ECONV_PARTIAL_INPUT);
2528 }
2529 if (&input_byte != p)
2530 ptr += p - &input_byte;
2531 switch (ret) {
2535 exc = make_econv_exception(ec);
2536 rb_econv_close(ec);
2537 rb_exc_raise(exc);
2538 break;
2539
2541 more_output_buffer(destination, resize_destination, max_output, &out_start, out_pos, &out_stop);
2542 break;
2543
2545 break;
2546
2547 case econv_finished:
2548 break;
2549 }
2550 }
2551 rb_econv_close(ec);
2552 *in_pos = in_stop;
2553 return;
2554}
2555#endif
2556
2557
2558/*
2559 * String-specific code
2560 */
2561
2562static unsigned char *
2563str_transcoding_resize(VALUE destination, size_t len, size_t new_len)
2564{
2565 rb_str_resize(destination, new_len);
2566 return (unsigned char *)RSTRING_PTR(destination);
2567}
2568
2569static int
2570econv_opts(VALUE opt, int ecflags)
2571{
2572 VALUE v;
2573 int newlineflag = 0;
2574
2575 v = rb_hash_aref(opt, sym_invalid);
2576 if (NIL_P(v)) {
2577 }
2578 else if (v==sym_replace) {
2579 ecflags |= ECONV_INVALID_REPLACE;
2580 }
2581 else {
2582 rb_raise(rb_eArgError, "unknown value for invalid character option");
2583 }
2584
2585 v = rb_hash_aref(opt, sym_undef);
2586 if (NIL_P(v)) {
2587 }
2588 else if (v==sym_replace) {
2589 ecflags |= ECONV_UNDEF_REPLACE;
2590 }
2591 else {
2592 rb_raise(rb_eArgError, "unknown value for undefined character option");
2593 }
2594
2595 v = rb_hash_aref(opt, sym_replace);
2596 if (!NIL_P(v) && !(ecflags & ECONV_INVALID_REPLACE)) {
2597 ecflags |= ECONV_UNDEF_REPLACE;
2598 }
2599
2600 v = rb_hash_aref(opt, sym_xml);
2601 if (!NIL_P(v)) {
2602 if (v==sym_text) {
2604 }
2605 else if (v==sym_attr) {
2607 }
2608 else if (SYMBOL_P(v)) {
2609 rb_raise(rb_eArgError, "unexpected value for xml option: %"PRIsVALUE, rb_sym2str(v));
2610 }
2611 else {
2612 rb_raise(rb_eArgError, "unexpected value for xml option");
2613 }
2614 }
2615
2616#ifdef ENABLE_ECONV_NEWLINE_OPTION
2617 v = rb_hash_aref(opt, sym_newline);
2618 if (!NIL_P(v)) {
2619 newlineflag = 2;
2620 ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
2621 if (v == sym_universal) {
2623 }
2624 else if (v == sym_crlf) {
2626 }
2627 else if (v == sym_cr) {
2628 ecflags |= ECONV_CR_NEWLINE_DECORATOR;
2629 }
2630 else if (v == sym_lf) {
2631 ecflags |= ECONV_LF_NEWLINE_DECORATOR;
2632 }
2633 else if (SYMBOL_P(v)) {
2634 rb_raise(rb_eArgError, "unexpected value for newline option: %"PRIsVALUE,
2635 rb_sym2str(v));
2636 }
2637 else {
2638 rb_raise(rb_eArgError, "unexpected value for newline option");
2639 }
2640 }
2641#endif
2642 {
2643 int setflags = 0;
2644
2645 v = rb_hash_aref(opt, sym_universal_newline);
2646 if (RTEST(v))
2648 newlineflag |= !NIL_P(v);
2649
2650 v = rb_hash_aref(opt, sym_crlf_newline);
2651 if (RTEST(v))
2652 setflags |= ECONV_CRLF_NEWLINE_DECORATOR;
2653 newlineflag |= !NIL_P(v);
2654
2655 v = rb_hash_aref(opt, sym_cr_newline);
2656 if (RTEST(v))
2657 setflags |= ECONV_CR_NEWLINE_DECORATOR;
2658 newlineflag |= !NIL_P(v);
2659
2660 v = rb_hash_aref(opt, sym_lf_newline);
2661 if (RTEST(v))
2662 setflags |= ECONV_LF_NEWLINE_DECORATOR;
2663 newlineflag |= !NIL_P(v);
2664
2665 switch (newlineflag) {
2666 case 1:
2667 ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
2668 ecflags |= setflags;
2669 break;
2670
2671 case 3:
2672 rb_warning(":newline option precedes other newline options");
2673 break;
2674 }
2675 }
2676
2677 return ecflags;
2678}
2679
2680int
2681rb_econv_prepare_options(VALUE opthash, VALUE *opts, int ecflags)
2682{
2683 VALUE newhash = Qnil;
2684 VALUE v;
2685
2686 if (NIL_P(opthash)) {
2687 *opts = Qnil;
2688 return ecflags;
2689 }
2690 ecflags = econv_opts(opthash, ecflags);
2691
2692 v = rb_hash_aref(opthash, sym_replace);
2693 if (!NIL_P(v)) {
2694 StringValue(v);
2695 if (is_broken_string(v)) {
2696 VALUE dumped = rb_str_dump(v);
2697 rb_raise(rb_eArgError, "replacement string is broken: %s as %s",
2698 StringValueCStr(dumped),
2699 rb_enc_name(rb_enc_get(v)));
2700 }
2701 v = rb_str_new_frozen(v);
2702 newhash = rb_hash_new();
2703 rb_hash_aset(newhash, sym_replace, v);
2704 }
2705
2706 v = rb_hash_aref(opthash, sym_fallback);
2707 if (!NIL_P(v)) {
2708 VALUE h = rb_check_hash_type(v);
2709 if (NIL_P(h)
2710 ? (rb_obj_is_proc(v) || rb_obj_is_method(v) || rb_respond_to(v, idAREF))
2711 : (v = h, 1)) {
2712 if (NIL_P(newhash))
2713 newhash = rb_hash_new();
2714 rb_hash_aset(newhash, sym_fallback, v);
2715 }
2716 }
2717
2718 if (!NIL_P(newhash))
2719 rb_hash_freeze(newhash);
2720 *opts = newhash;
2721
2722 return ecflags;
2723}
2724
2725int
2727{
2728 return rb_econv_prepare_options(opthash, opts, 0);
2729}
2730
2731rb_econv_t *
2732rb_econv_open_opts(const char *source_encoding, const char *destination_encoding, int ecflags, VALUE opthash)
2733{
2734 rb_econv_t *ec;
2735 VALUE replacement;
2736
2737 if (NIL_P(opthash)) {
2738 replacement = Qnil;
2739 }
2740 else {
2741 if (!RB_TYPE_P(opthash, T_HASH) || !OBJ_FROZEN(opthash))
2742 rb_bug("rb_econv_open_opts called with invalid opthash");
2743 replacement = rb_hash_aref(opthash, sym_replace);
2744 }
2745
2746 ec = rb_econv_open(source_encoding, destination_encoding, ecflags);
2747 if (ec) {
2748 if (!NIL_P(replacement)) {
2749 int ret;
2750 rb_encoding *enc = rb_enc_get(replacement);
2751
2752 ret = rb_econv_set_replacement(ec,
2753 (const unsigned char *)RSTRING_PTR(replacement),
2754 RSTRING_LEN(replacement),
2755 rb_enc_name(enc));
2756 if (ret == -1) {
2757 rb_econv_close(ec);
2758 ec = NULL;
2759 }
2760 }
2761 }
2762 return ec; // can be NULL
2763}
2764
2765static int
2766enc_arg(VALUE *arg, const char **name_p, rb_encoding **enc_p)
2767{
2768 rb_encoding *enc;
2769 const char *n;
2770 int encidx;
2771 VALUE encval;
2772
2773 if (((encidx = rb_to_encoding_index(encval = *arg)) < 0) ||
2774 !(enc = rb_enc_from_index(encidx))) {
2775 enc = NULL;
2776 encidx = 0;
2777 n = StringValueCStr(*arg);
2778 }
2779 else {
2780 n = rb_enc_name(enc);
2781 }
2782
2783 *name_p = n;
2784 *enc_p = enc;
2785
2786 return encidx;
2787}
2788
2789static int
2790str_transcode_enc_args(VALUE str, VALUE *arg1, VALUE *arg2,
2791 const char **sname_p, rb_encoding **senc_p,
2792 const char **dname_p, rb_encoding **denc_p)
2793{
2794 rb_encoding *senc, *denc;
2795 const char *sname, *dname;
2796 int sencidx, dencidx;
2797
2798 dencidx = enc_arg(arg1, &dname, &denc);
2799
2800 if (NIL_P(*arg2)) {
2801 sencidx = rb_enc_get_index(str);
2802 senc = rb_enc_from_index(sencidx);
2803 sname = rb_enc_name(senc);
2804 }
2805 else {
2806 sencidx = enc_arg(arg2, &sname, &senc);
2807 }
2808
2809 *sname_p = sname;
2810 *senc_p = senc;
2811 *dname_p = dname;
2812 *denc_p = denc;
2813 return dencidx;
2814}
2815
2816static int
2817str_transcode0(int argc, VALUE *argv, VALUE *self, int ecflags, VALUE ecopts)
2818{
2819 VALUE dest;
2820 VALUE str = *self;
2821 VALUE arg1, arg2;
2822 rb_len_t blen, slen;
2823 unsigned char *buf, *bp, *sp;
2824 const unsigned char *fromp;
2825 rb_encoding *senc, *denc;
2826 const char *sname, *dname;
2827 int dencidx;
2828 int explicitly_invalid_replace = TRUE;
2829
2830 rb_check_arity(argc, 0, 2);
2831
2832 if (argc == 0) {
2833 arg1 = rb_enc_default_internal();
2834 if (NIL_P(arg1)) {
2835 if (!ecflags) return -1;
2836 arg1 = rb_obj_encoding(str);
2837 }
2838 if (!(ecflags & ECONV_INVALID_MASK)) {
2839 explicitly_invalid_replace = FALSE;
2840 }
2842 }
2843 else {
2844 arg1 = argv[0];
2845 }
2846 arg2 = argc<=1 ? Qnil : argv[1];
2847 dencidx = str_transcode_enc_args(str, &arg1, &arg2, &sname, &senc, &dname, &denc);
2848
2849 if ((ecflags & (ECONV_NEWLINE_DECORATOR_MASK|
2853 if (senc && senc == denc) {
2854 if ((ecflags & ECONV_INVALID_MASK) && explicitly_invalid_replace) {
2855 VALUE rep = Qnil;
2856 if (!NIL_P(ecopts)) {
2857 rep = rb_hash_aref(ecopts, sym_replace);
2858 }
2859 dest = rb_enc_str_scrub(senc, str, rep);
2860 if (NIL_P(dest)) dest = str;
2861 *self = dest;
2862 return dencidx;
2863 }
2864 return NIL_P(arg2) ? -1 : dencidx;
2865 }
2866 if (senc && denc && rb_enc_asciicompat(senc) && rb_enc_asciicompat(denc)) {
2867 if (is_ascii_string(str)) {
2868 return dencidx;
2869 }
2870 }
2871 if (encoding_equal(sname, dname)) {
2872 return NIL_P(arg2) ? -1 : dencidx;
2873 }
2874 }
2875 else {
2876 if (senc && denc && !rb_enc_asciicompat(senc) && !rb_enc_asciicompat(denc)) {
2877 rb_encoding *utf8 = rb_utf8_encoding();
2878 str = rb_str_conv_enc(str, senc, utf8);
2879 senc = utf8;
2880 sname = "UTF-8";
2881 }
2882 if (encoding_equal(sname, dname)) {
2883 sname = "";
2884 dname = "";
2885 }
2886 }
2887
2888 fromp = sp = (unsigned char *)RSTRING_PTR(str);
2889 slen = RSTRING_LEN(str);
2890 blen = slen + 30; /* len + margin */
2891 dest = rb_str_tmp_new(blen);
2892 bp = (unsigned char *)RSTRING_PTR(dest);
2893
2894 transcode_loop(&fromp, &bp, (sp+slen), (bp+blen), dest, str_transcoding_resize, sname, dname, ecflags, ecopts, str);
2895 if (fromp != sp+slen) {
2896 rb_raise(rb_eArgError, "not fully converted, %"PRIdPTRDIFF" bytes left", sp+slen-fromp);
2897 }
2898 buf = (unsigned char *)RSTRING_PTR(dest);
2899 *bp = '\0';
2900 rb_str_set_len(dest, bp - buf);
2901
2902 /* set encoding */
2903 if (!denc) {
2904 dencidx = rb_define_dummy_encoding(dname);
2905 RB_GC_GUARD(arg1);
2906 RB_GC_GUARD(arg2);
2907 }
2908 *self = dest;
2909
2910 return dencidx;
2911}
2912
2913static int
2914str_transcode(int argc, VALUE *argv, VALUE *self)
2915{
2916 VALUE opt;
2917 int ecflags = 0;
2918 VALUE ecopts = Qnil;
2919
2920 argc = rb_scan_args(argc, argv, "02:", NULL, NULL, &opt);
2921 if (!NIL_P(opt)) {
2922 ecflags = rb_econv_prepare_opts(opt, &ecopts);
2923 }
2924 return str_transcode0(argc, argv, self, ecflags, ecopts);
2925}
2926
2927static inline VALUE
2928str_encode_associate(VALUE str, int encidx)
2929{
2930 int cr = 0;
2931
2932 rb_enc_associate_index(str, encidx);
2933
2934 /* transcoded string never be broken. */
2935 if (rb_enc_asciicompat(rb_enc_from_index(encidx))) {
2936 rb_str_coderange_scan_restartable(RSTRING_PTR(str), RSTRING_END(str), 0, &cr);
2937 }
2938 else {
2940 }
2941 ENC_CODERANGE_SET(str, cr);
2942 return str;
2943}
2944
2945/*
2946 * call-seq:
2947 * encode!(dst_encoding = Encoding.default_internal, **enc_opts) -> self
2948 * encode!(dst_encoding, src_encoding, **enc_opts) -> self
2949 *
2950 * Like #encode, but applies encoding changes to +self+; returns +self+.
2951 *
2952 * Related: see {Modifying}[rdoc-ref:String@Modifying].
2953 */
2954
2955static VALUE
2956str_encode_bang(int argc, VALUE *argv, VALUE str)
2957{
2958 VALUE newstr;
2959 int encidx;
2960
2961 rb_check_frozen(str);
2962
2963 newstr = str;
2964 encidx = str_transcode(argc, argv, &newstr);
2965
2966 if (encidx < 0) return str;
2967 if (newstr == str) {
2968 rb_enc_associate_index(str, encidx);
2969 return str;
2970 }
2971 rb_str_shared_replace(str, newstr);
2972 return str_encode_associate(str, encidx);
2973}
2974
2975static VALUE encoded_dup(VALUE newstr, VALUE str, int encidx);
2976
2977/*
2978 * call-seq:
2979 * encode(dst_encoding = Encoding.default_internal, **enc_opts) -> string
2980 * encode(dst_encoding, src_encoding, **enc_opts) -> string
2981 *
2982 * Returns a copy of +self+ transcoded as determined by +dst_encoding+;
2983 * see {Encodings}[rdoc-ref:encodings.rdoc].
2984 *
2985 * By default, raises an exception if +self+
2986 * contains an invalid byte or a character not defined in +dst_encoding+;
2987 * that behavior may be modified by encoding options; see below.
2988 *
2989 * With no arguments:
2990 *
2991 * - Uses the same encoding if <tt>Encoding.default_internal</tt> is +nil+
2992 * (the default):
2993 *
2994 * Encoding.default_internal # => nil
2995 * s = "Ruby\x99".force_encoding('Windows-1252')
2996 * s.encoding # => #<Encoding:Windows-1252>
2997 * s.bytes # => [82, 117, 98, 121, 153]
2998 * t = s.encode # => "Ruby\x99"
2999 * t.encoding # => #<Encoding:Windows-1252>
3000 * t.bytes # => [82, 117, 98, 121, 153]
3001 *
3002 * - Otherwise, uses the encoding <tt>Encoding.default_internal</tt>:
3003 *
3004 * Encoding.default_internal = 'UTF-8'
3005 * t = s.encode # => "Rubyâ„¢"
3006 * t.encoding # => #<Encoding:UTF-8>
3007 *
3008 * With only argument +dst_encoding+ given, uses that encoding:
3009 *
3010 * s = "Ruby\x99".force_encoding('Windows-1252')
3011 * s.encoding # => #<Encoding:Windows-1252>
3012 * t = s.encode('UTF-8') # => "Rubyâ„¢"
3013 * t.encoding # => #<Encoding:UTF-8>
3014 *
3015 * With arguments +dst_encoding+ and +src_encoding+ given,
3016 * interprets +self+ using +src_encoding+, encodes the new string using +dst_encoding+:
3017 *
3018 * s = "Ruby\x99"
3019 * t = s.encode('UTF-8', 'Windows-1252') # => "Rubyâ„¢"
3020 * t.encoding # => #<Encoding:UTF-8>
3021 *
3022 * Optional keyword arguments +enc_opts+ specify encoding options;
3023 * see {Encoding Options}[rdoc-ref:encodings.rdoc@Encoding+Options].
3024 *
3025 * Please note that, unless <code>invalid: :replace</code> option is
3026 * given, conversion from an encoding +enc+ to the same encoding +enc+
3027 * (independent of whether +enc+ is given explicitly or implicitly) is a
3028 * no-op, i.e. the string is simply copied without any changes, and no
3029 * exceptions are raised, even if there are invalid bytes.
3030 *
3031 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
3032 *
3033 */
3034
3035static VALUE
3036str_encode(int argc, VALUE *argv, VALUE str)
3037{
3038 VALUE newstr = str;
3039 int encidx = str_transcode(argc, argv, &newstr);
3040 return encoded_dup(newstr, str, encidx);
3041}
3042
3043VALUE
3044rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts)
3045{
3046 int argc = 1;
3047 VALUE *argv = &to;
3048 VALUE newstr = str;
3049 int encidx = str_transcode0(argc, argv, &newstr, ecflags, ecopts);
3050 return encoded_dup(newstr, str, encidx);
3051}
3052
3053static VALUE
3054encoded_dup(VALUE newstr, VALUE str, int encidx)
3055{
3056 if (encidx < 0) return rb_str_dup(str);
3057 if (newstr == str) {
3058 newstr = rb_str_dup(str);
3059 rb_enc_associate_index(newstr, encidx);
3060 return newstr;
3061 }
3062 else {
3063 RBASIC_SET_CLASS(newstr, rb_obj_class(str));
3064 }
3065 return str_encode_associate(newstr, encidx);
3066}
3067
3068/*
3069 * Document-class: Encoding::Converter
3070 *
3071 * Encoding conversion class.
3072 */
3073static void
3074econv_free(void *ptr)
3075{
3076 rb_econv_t *ec = ptr;
3077 rb_econv_close(ec);
3078}
3079
3080static size_t
3081econv_memsize(const void *ptr)
3082{
3083 return ptr ? rb_econv_memsize((rb_econv_t *)ptr) : 0;
3084}
3085
3086static const rb_data_type_t econv_data_type = {
3087 "econv",
3088 {0, econv_free, econv_memsize,},
3089 0, 0, RUBY_TYPED_THREAD_SAFE_FREE
3090};
3091
3092static VALUE
3093econv_s_allocate(VALUE klass)
3094{
3095 return TypedData_Wrap_Struct(klass, &econv_data_type, NULL);
3096}
3097
3098static rb_encoding *
3099make_dummy_encoding(const char *name)
3100{
3101 rb_encoding *enc;
3102 int idx;
3103 idx = rb_define_dummy_encoding(name);
3104 enc = rb_enc_from_index(idx);
3105 return enc;
3106}
3107
3108static rb_encoding *
3109make_encoding(const char *name)
3110{
3111 rb_encoding *enc;
3112 enc = rb_enc_find(name);
3113 if (!enc) {
3114 RB_VM_LOCKING() {
3115 if (rb_enc_registered(name)) {
3116 enc = NULL;
3117 }
3118 else {
3119 enc = make_dummy_encoding(name);
3120 }
3121 }
3122 }
3123 return enc;
3124}
3125
3126static VALUE
3127make_encobj(const char *name)
3128{
3129 return rb_enc_from_encoding(make_encoding(name));
3130}
3131
3132/*
3133 * call-seq:
3134 * Encoding::Converter.asciicompat_encoding(string) -> encoding or nil
3135 * Encoding::Converter.asciicompat_encoding(encoding) -> encoding or nil
3136 *
3137 * Returns the corresponding ASCII compatible encoding.
3138 *
3139 * Returns nil if the argument is an ASCII compatible encoding.
3140 *
3141 * "corresponding ASCII compatible encoding" is an ASCII compatible encoding which
3142 * can represents exactly the same characters as the given ASCII incompatible encoding.
3143 * So, no conversion undefined error occurs when converting between the two encodings.
3144 *
3145 * Encoding::Converter.asciicompat_encoding("ISO-2022-JP") #=> #<Encoding:stateless-ISO-2022-JP>
3146 * Encoding::Converter.asciicompat_encoding("UTF-16BE") #=> #<Encoding:UTF-8>
3147 * Encoding::Converter.asciicompat_encoding("UTF-8") #=> nil
3148 *
3149 */
3150static VALUE
3151econv_s_asciicompat_encoding(VALUE klass, VALUE arg)
3152{
3153 const char *arg_name, *result_name;
3154 rb_encoding *arg_enc, *result_enc;
3155 VALUE enc = Qnil;
3156
3157 enc_arg(&arg, &arg_name, &arg_enc);
3158 result_name = rb_econv_asciicompat_encoding(arg_name);
3159 if (result_name) {
3160 result_enc = make_encoding(result_name);
3161 enc = rb_enc_from_encoding(result_enc);
3162 }
3163 return enc;
3164}
3165
3166static void
3167econv_args(int argc, VALUE *argv,
3168 VALUE *snamev_p, VALUE *dnamev_p,
3169 const char **sname_p, const char **dname_p,
3170 rb_encoding **senc_p, rb_encoding **denc_p,
3171 int *ecflags_p,
3172 VALUE *ecopts_p)
3173{
3174 VALUE opt, flags_v, ecopts;
3175 int sidx, didx;
3176 const char *sname, *dname;
3177 rb_encoding *senc, *denc;
3178 int ecflags;
3179
3180 argc = rb_scan_args(argc, argv, "21:", snamev_p, dnamev_p, &flags_v, &opt);
3181
3182 if (!NIL_P(flags_v)) {
3183 if (!NIL_P(opt)) {
3184 rb_error_arity(argc + 1, 2, 3);
3185 }
3186 ecflags = NUM2INT(rb_to_int(flags_v));
3187 ecopts = Qnil;
3188 }
3189 else if (!NIL_P(opt)) {
3190 ecflags = rb_econv_prepare_opts(opt, &ecopts);
3191 }
3192 else {
3193 ecflags = 0;
3194 ecopts = Qnil;
3195 }
3196
3197 senc = NULL;
3198 sidx = rb_to_encoding_index(*snamev_p);
3199 if (0 <= sidx) {
3200 senc = rb_enc_from_index(sidx);
3201 }
3202 else {
3203 StringValue(*snamev_p);
3204 }
3205
3206 denc = NULL;
3207 didx = rb_to_encoding_index(*dnamev_p);
3208 if (0 <= didx) {
3209 denc = rb_enc_from_index(didx);
3210 }
3211 else {
3212 StringValue(*dnamev_p);
3213 }
3214
3215 sname = senc ? rb_enc_name(senc) : StringValueCStr(*snamev_p);
3216 dname = denc ? rb_enc_name(denc) : StringValueCStr(*dnamev_p);
3217
3218 *sname_p = sname;
3219 *dname_p = dname;
3220 *senc_p = senc;
3221 *denc_p = denc;
3222 *ecflags_p = ecflags;
3223 *ecopts_p = ecopts;
3224}
3225
3226static int
3227decorate_convpath(VALUE convpath, int ecflags)
3228{
3229 int num_decorators;
3230 const char *decorators[MAX_ECFLAGS_DECORATORS];
3231 int i;
3232 int n, len;
3233
3234 num_decorators = decorator_names(ecflags, decorators);
3235 if (num_decorators == -1)
3236 return -1;
3237
3238 len = n = RARRAY_LENINT(convpath);
3239 if (n != 0) {
3240 VALUE pair = RARRAY_AREF(convpath, n-1);
3241 if (RB_TYPE_P(pair, T_ARRAY)) {
3242 const char *sname = rb_enc_name(rb_to_encoding(RARRAY_AREF(pair, 0)));
3243 const char *dname = rb_enc_name(rb_to_encoding(RARRAY_AREF(pair, 1)));
3244 transcoder_entry_t *entry;
3245 const rb_transcoder *tr;
3246 entry = get_transcoder_entry(sname, dname);
3247 tr = load_transcoder_entry(entry);
3248 if (!tr)
3249 return -1;
3250 if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding) &&
3251 tr->asciicompat_type == asciicompat_encoder) {
3252 n--;
3253 rb_ary_store(convpath, len + num_decorators - 1, pair);
3254 }
3255 }
3256 else {
3257 rb_ary_store(convpath, len + num_decorators - 1, pair);
3258 }
3259 }
3260
3261 for (i = 0; i < num_decorators; i++)
3262 rb_ary_store(convpath, n + i, rb_str_new_cstr(decorators[i]));
3263
3264 return 0;
3265}
3266
3267static void
3268search_convpath_i(const char *sname, const char *dname, int depth, void *arg)
3269{
3270 VALUE *ary_p = arg;
3271 VALUE v;
3272
3273 if (NIL_P(*ary_p)) {
3274 *ary_p = rb_ary_new();
3275 }
3276
3277 if (DECORATOR_P(sname, dname)) {
3278 v = rb_str_new_cstr(dname);
3279 }
3280 else {
3281 v = rb_assoc_new(make_encobj(sname), make_encobj(dname));
3282 }
3283 rb_ary_store(*ary_p, depth, v);
3284}
3285
3286/*
3287 * call-seq:
3288 * Encoding::Converter.search_convpath(source_encoding, destination_encoding) -> ary
3289 * Encoding::Converter.search_convpath(source_encoding, destination_encoding, opt) -> ary
3290 *
3291 * Returns a conversion path.
3292 *
3293 * p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP")
3294 * #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
3295 * # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>]]
3296 *
3297 * p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP", universal_newline: true)
3298 * or
3299 * p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP", newline: :universal)
3300 * #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
3301 * # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>],
3302 * # "universal_newline"]
3303 *
3304 * p Encoding::Converter.search_convpath("ISO-8859-1", "UTF-32BE", universal_newline: true)
3305 * or
3306 * p Encoding::Converter.search_convpath("ISO-8859-1", "UTF-32BE", newline: :universal)
3307 * #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
3308 * # "universal_newline",
3309 * # [#<Encoding:UTF-8>, #<Encoding:UTF-32BE>]]
3310 */
3311static VALUE
3312econv_s_search_convpath(int argc, VALUE *argv, VALUE klass)
3313{
3314 VALUE snamev, dnamev;
3315 const char *sname, *dname;
3316 rb_encoding *senc, *denc;
3317 int ecflags;
3318 VALUE ecopts;
3319 VALUE convpath;
3320
3321 econv_args(argc, argv, &snamev, &dnamev, &sname, &dname, &senc, &denc, &ecflags, &ecopts);
3322
3323 convpath = Qnil;
3324 transcode_search_path(sname, dname, search_convpath_i, &convpath);
3325
3326 if (NIL_P(convpath)) {
3327 VALUE exc = rb_econv_open_exc(sname, dname, ecflags);
3328 RB_GC_GUARD(snamev);
3329 RB_GC_GUARD(dnamev);
3330 rb_exc_raise(exc);
3331 }
3332
3333 if (decorate_convpath(convpath, ecflags) == -1) {
3334 VALUE exc = rb_econv_open_exc(sname, dname, ecflags);
3335 RB_GC_GUARD(snamev);
3336 RB_GC_GUARD(dnamev);
3337 rb_exc_raise(exc);
3338 }
3339
3340 return convpath;
3341}
3342
3343/*
3344 * Check the existence of a conversion path.
3345 * Returns the number of converters in the conversion path.
3346 * result: >=0:success -1:failure
3347 */
3348int
3349rb_econv_has_convpath_p(const char* from_encoding, const char* to_encoding)
3350{
3351 VALUE convpath = Qnil;
3352 transcode_search_path(from_encoding, to_encoding, search_convpath_i,
3353 &convpath);
3354 return RTEST(convpath);
3355}
3356
3358 rb_econv_t *ec;
3359 int index;
3360 int ret;
3361};
3362
3363static void
3364rb_econv_init_by_convpath_i(const char *sname, const char *dname, int depth, void *arg)
3365{
3367 int ret;
3368
3369 if (a->ret == -1)
3370 return;
3371
3372 ret = rb_econv_add_converter(a->ec, sname, dname, a->index);
3373
3374 a->ret = ret;
3375 return;
3376}
3377
3378static rb_econv_t *
3379rb_econv_init_by_convpath(VALUE self, VALUE convpath,
3380 const char **sname_p, const char **dname_p,
3381 rb_encoding **senc_p, rb_encoding**denc_p)
3382{
3383 rb_econv_t *ec;
3384 rb_len_t i;
3385 int ret, first=1;
3386 VALUE elt;
3387 rb_encoding *senc = 0, *denc = 0;
3388 const char *sname, *dname;
3389
3390 ec = rb_econv_alloc(RARRAY_LENINT(convpath));
3391 DATA_PTR(self) = ec;
3392
3393 for (i = 0; i < RARRAY_LEN(convpath); i++) {
3394 VALUE snamev, dnamev;
3395 VALUE pair;
3396 elt = rb_ary_entry(convpath, i);
3397 if (!NIL_P(pair = rb_check_array_type(elt))) {
3398 if (RARRAY_LEN(pair) != 2)
3399 rb_raise(rb_eArgError, "not a 2-element array in convpath");
3400 snamev = rb_ary_entry(pair, 0);
3401 enc_arg(&snamev, &sname, &senc);
3402 dnamev = rb_ary_entry(pair, 1);
3403 enc_arg(&dnamev, &dname, &denc);
3404 }
3405 else {
3406 sname = "";
3407 dname = StringValueCStr(elt);
3408 }
3409 if (DECORATOR_P(sname, dname)) {
3410 ret = rb_econv_add_converter(ec, sname, dname, ec->num_trans);
3411 if (ret == -1) {
3412 VALUE msg = rb_sprintf("decoration failed: %s", dname);
3413 RB_GC_GUARD(snamev);
3414 RB_GC_GUARD(dnamev);
3415 rb_exc_raise(rb_exc_new_str(rb_eArgError, msg));
3416 }
3417 }
3418 else {
3419 int j = ec->num_trans;
3420 struct rb_econv_init_by_convpath_t arg;
3421 arg.ec = ec;
3422 arg.index = ec->num_trans;
3423 arg.ret = 0;
3424 ret = transcode_search_path(sname, dname, rb_econv_init_by_convpath_i, &arg);
3425 if (ret == -1 || arg.ret == -1) {
3426 VALUE msg = rb_sprintf("adding conversion failed: %s to %s", sname, dname);
3427 RB_GC_GUARD(snamev);
3428 RB_GC_GUARD(dnamev);
3429 rb_exc_raise(rb_exc_new_str(rb_eArgError, msg));
3430 }
3431 if (first) {
3432 first = 0;
3433 *senc_p = senc;
3434 *sname_p = ec->elems[j].tc->transcoder->src_encoding;
3435 }
3436 *denc_p = denc;
3437 *dname_p = ec->elems[ec->num_trans-1].tc->transcoder->dst_encoding;
3438 }
3439 }
3440
3441 if (first) {
3442 *senc_p = NULL;
3443 *denc_p = NULL;
3444 *sname_p = "";
3445 *dname_p = "";
3446 }
3447
3448 ec->source_encoding_name = *sname_p;
3449 ec->destination_encoding_name = *dname_p;
3450
3451 return ec;
3452}
3453
3454/*
3455 * call-seq:
3456 * Encoding::Converter.new(source_encoding, destination_encoding)
3457 * Encoding::Converter.new(source_encoding, destination_encoding, opt)
3458 * Encoding::Converter.new(convpath)
3459 *
3460 * possible options elements:
3461 * hash form:
3462 * :invalid => nil # raise error on invalid byte sequence (default)
3463 * :invalid => :replace # replace invalid byte sequence
3464 * :undef => nil # raise error on undefined conversion (default)
3465 * :undef => :replace # replace undefined conversion
3466 * :replace => string # replacement string ("?" or "\uFFFD" if not specified)
3467 * :newline => :universal # decorator for converting CRLF and CR to LF
3468 * :newline => :lf # decorator for converting CRLF and CR to LF when writing
3469 * :newline => :crlf # decorator for converting LF to CRLF
3470 * :newline => :cr # decorator for converting LF to CR
3471 * :universal_newline => true # decorator for converting CRLF and CR to LF
3472 * :crlf_newline => true # decorator for converting LF to CRLF
3473 * :cr_newline => true # decorator for converting LF to CR
3474 * :lf_newline => true # decorator for converting CRLF and CR to LF when writing
3475 * :xml => :text # escape as XML CharData.
3476 * :xml => :attr # escape as XML AttValue
3477 * integer form:
3478 * Encoding::Converter::INVALID_REPLACE
3479 * Encoding::Converter::UNDEF_REPLACE
3480 * Encoding::Converter::UNDEF_HEX_CHARREF
3481 * Encoding::Converter::UNIVERSAL_NEWLINE_DECORATOR
3482 * Encoding::Converter::LF_NEWLINE_DECORATOR
3483 * Encoding::Converter::CRLF_NEWLINE_DECORATOR
3484 * Encoding::Converter::CR_NEWLINE_DECORATOR
3485 * Encoding::Converter::XML_TEXT_DECORATOR
3486 * Encoding::Converter::XML_ATTR_CONTENT_DECORATOR
3487 * Encoding::Converter::XML_ATTR_QUOTE_DECORATOR
3488 *
3489 * Encoding::Converter.new creates an instance of Encoding::Converter.
3490 *
3491 * Source_encoding and destination_encoding should be a string or
3492 * Encoding object.
3493 *
3494 * opt should be nil, a hash or an integer.
3495 *
3496 * convpath should be an array.
3497 * convpath may contain
3498 * - two-element arrays which contain encodings or encoding names, or
3499 * - strings representing decorator names.
3500 *
3501 * Encoding::Converter.new optionally takes an option.
3502 * The option should be a hash or an integer.
3503 * The option hash can contain :invalid => nil, etc.
3504 * The option integer should be logical-or of constants such as
3505 * Encoding::Converter::INVALID_REPLACE, etc.
3506 *
3507 * [:invalid => nil]
3508 * Raise error on invalid byte sequence. This is a default behavior.
3509 * [:invalid => :replace]
3510 * Replace invalid byte sequence by replacement string.
3511 * [:undef => nil]
3512 * Raise an error if a character in source_encoding is not defined in destination_encoding.
3513 * This is a default behavior.
3514 * [:undef => :replace]
3515 * Replace undefined character in destination_encoding with replacement string.
3516 * [:replace => string]
3517 * Specify the replacement string.
3518 * If not specified, "\uFFFD" is used for Unicode encodings and "?" for others.
3519 * [:universal_newline => true]
3520 * Convert CRLF and CR to LF.
3521 * [:crlf_newline => true]
3522 * Convert LF to CRLF.
3523 * [:cr_newline => true]
3524 * Convert LF to CR.
3525 * [:lf_newline => true]
3526 * Convert CRLF and CR to LF (when writing).
3527 * [:xml => :text]
3528 * Escape as XML CharData.
3529 * This form can be used as an HTML 4.0 #PCDATA.
3530 * - '&' -> '&amp;'
3531 * - '<' -> '&lt;'
3532 * - '>' -> '&gt;'
3533 * - undefined characters in destination_encoding -> hexadecimal CharRef such as &#xHH;
3534 * [:xml => :attr]
3535 * Escape as XML AttValue.
3536 * The converted result is quoted as "...".
3537 * This form can be used as an HTML 4.0 attribute value.
3538 * - '&' -> '&amp;'
3539 * - '<' -> '&lt;'
3540 * - '>' -> '&gt;'
3541 * - '"' -> '&quot;'
3542 * - undefined characters in destination_encoding -> hexadecimal CharRef such as &#xHH;
3543 *
3544 * Examples:
3545 * # UTF-16BE to UTF-8
3546 * ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
3547 *
3548 * # Usually, decorators such as newline conversion are inserted last.
3549 * ec = Encoding::Converter.new("UTF-16BE", "UTF-8", :universal_newline => true)
3550 * p ec.convpath #=> [[#<Encoding:UTF-16BE>, #<Encoding:UTF-8>],
3551 * # "universal_newline"]
3552 *
3553 * # But, if the last encoding is ASCII incompatible,
3554 * # decorators are inserted before the last conversion.
3555 * ec = Encoding::Converter.new("UTF-8", "UTF-16BE", :crlf_newline => true)
3556 * p ec.convpath #=> ["crlf_newline",
3557 * # [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]
3558 *
3559 * # Conversion path can be specified directly.
3560 * ec = Encoding::Converter.new(["universal_newline", ["EUC-JP", "UTF-8"], ["UTF-8", "UTF-16BE"]])
3561 * p ec.convpath #=> ["universal_newline",
3562 * # [#<Encoding:EUC-JP>, #<Encoding:UTF-8>],
3563 * # [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]
3564 */
3565static VALUE
3566econv_init(int argc, VALUE *argv, VALUE self)
3567{
3568 VALUE ecopts;
3569 VALUE snamev, dnamev;
3570 const char *sname, *dname;
3571 rb_encoding *senc, *denc;
3572 rb_econv_t *ec;
3573 int ecflags;
3574 VALUE convpath;
3575
3576 if (rb_check_typeddata(self, &econv_data_type)) {
3577 rb_raise(rb_eTypeError, "already initialized");
3578 }
3579
3580 if (argc == 1 && !NIL_P(convpath = rb_check_array_type(argv[0]))) {
3581 ec = rb_econv_init_by_convpath(self, convpath, &sname, &dname, &senc, &denc);
3582 ecflags = 0;
3583 ecopts = Qnil;
3584 }
3585 else {
3586 econv_args(argc, argv, &snamev, &dnamev, &sname, &dname, &senc, &denc, &ecflags, &ecopts);
3587 ec = rb_econv_open_opts(sname, dname, ecflags, ecopts);
3588 }
3589
3590 if (!ec) {
3591 VALUE exc = rb_econv_open_exc(sname, dname, ecflags);
3592 RB_GC_GUARD(snamev);
3593 RB_GC_GUARD(dnamev);
3594 rb_exc_raise(exc);
3595 }
3596
3597 if (!DECORATOR_P(sname, dname)) {
3598 if (!senc)
3599 senc = make_dummy_encoding(sname);
3600 if (!denc)
3601 denc = make_dummy_encoding(dname);
3602 RB_GC_GUARD(snamev);
3603 RB_GC_GUARD(dnamev);
3604 }
3605
3606 ec->source_encoding = senc;
3607 ec->destination_encoding = denc;
3608
3609 DATA_PTR(self) = ec;
3610
3611 return self;
3612}
3613
3614/*
3615 * call-seq:
3616 * ec.inspect -> string
3617 *
3618 * Returns a printable version of <i>ec</i>
3619 *
3620 * ec = Encoding::Converter.new("iso-8859-1", "utf-8")
3621 * puts ec.inspect #=> #<Encoding::Converter: ISO-8859-1 to UTF-8>
3622 *
3623 */
3624static VALUE
3625econv_inspect(VALUE self)
3626{
3627 const char *cname = rb_obj_classname(self);
3628 rb_econv_t *ec;
3629
3630 TypedData_Get_Struct(self, rb_econv_t, &econv_data_type, ec);
3631 if (!ec)
3632 return rb_sprintf("#<%s: uninitialized>", cname);
3633 else {
3634 const char *sname = ec->source_encoding_name;
3635 const char *dname = ec->destination_encoding_name;
3636 VALUE str;
3637 str = rb_sprintf("#<%s: ", cname);
3638 econv_description(sname, dname, ec->flags, str);
3639 rb_str_cat2(str, ">");
3640 return str;
3641 }
3642}
3643
3644static rb_econv_t *
3645check_econv(VALUE self)
3646{
3647 rb_econv_t *ec;
3648
3649 TypedData_Get_Struct(self, rb_econv_t, &econv_data_type, ec);
3650 if (!ec) {
3651 rb_raise(rb_eTypeError, "uninitialized encoding converter");
3652 }
3653 return ec;
3654}
3655
3656static VALUE
3657econv_get_encoding(rb_encoding *encoding)
3658{
3659 if (!encoding)
3660 return Qnil;
3661 return rb_enc_from_encoding(encoding);
3662}
3663
3664/*
3665 * call-seq:
3666 * ec.source_encoding -> encoding
3667 *
3668 * Returns the source encoding as an Encoding object.
3669 */
3670static VALUE
3671econv_source_encoding(VALUE self)
3672{
3673 rb_econv_t *ec = check_econv(self);
3674 return econv_get_encoding(ec->source_encoding);
3675}
3676
3677/*
3678 * call-seq:
3679 * ec.destination_encoding -> encoding
3680 *
3681 * Returns the destination encoding as an Encoding object.
3682 */
3683static VALUE
3684econv_destination_encoding(VALUE self)
3685{
3686 rb_econv_t *ec = check_econv(self);
3687 return econv_get_encoding(ec->destination_encoding);
3688}
3689
3690/*
3691 * call-seq:
3692 * ec.convpath -> ary
3693 *
3694 * Returns the conversion path of ec.
3695 *
3696 * The result is an array of conversions.
3697 *
3698 * ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP", crlf_newline: true)
3699 * p ec.convpath
3700 * #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
3701 * # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>],
3702 * # "crlf_newline"]
3703 *
3704 * Each element of the array is a pair of encodings or a string.
3705 * A pair means an encoding conversion.
3706 * A string means a decorator.
3707 *
3708 * In the above example, [#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>] means
3709 * a converter from ISO-8859-1 to UTF-8.
3710 * "crlf_newline" means newline converter from LF to CRLF.
3711 */
3712static VALUE
3713econv_convpath(VALUE self)
3714{
3715 rb_econv_t *ec = check_econv(self);
3716 VALUE result;
3717 int i;
3718
3719 result = rb_ary_new();
3720 for (i = 0; i < ec->num_trans; i++) {
3721 const rb_transcoder *tr = ec->elems[i].tc->transcoder;
3722 VALUE v;
3723 if (DECORATOR_P(tr->src_encoding, tr->dst_encoding))
3724 v = rb_str_new_cstr(tr->dst_encoding);
3725 else
3726 v = rb_assoc_new(make_encobj(tr->src_encoding), make_encobj(tr->dst_encoding));
3727 rb_ary_push(result, v);
3728 }
3729 return result;
3730}
3731
3732/*
3733 * call-seq:
3734 * ec == other -> true or false
3735 */
3736static VALUE
3737econv_equal(VALUE self, VALUE other)
3738{
3739 rb_econv_t *ec1 = check_econv(self);
3740 rb_econv_t *ec2;
3741 int i;
3742
3743 if (!rb_typeddata_is_kind_of(other, &econv_data_type)) {
3744 return Qnil;
3745 }
3746 ec2 = DATA_PTR(other);
3747 if (!ec2) return Qfalse;
3748 if (ec1->source_encoding_name != ec2->source_encoding_name &&
3749 strcmp(ec1->source_encoding_name, ec2->source_encoding_name))
3750 return Qfalse;
3751 if (ec1->destination_encoding_name != ec2->destination_encoding_name &&
3752 strcmp(ec1->destination_encoding_name, ec2->destination_encoding_name))
3753 return Qfalse;
3754 if (ec1->flags != ec2->flags) return Qfalse;
3755 if (ec1->replacement_enc != ec2->replacement_enc &&
3756 strcmp(ec1->replacement_enc, ec2->replacement_enc))
3757 return Qfalse;
3758 if (ec1->replacement_len != ec2->replacement_len) return Qfalse;
3759 if (ec1->replacement_str != ec2->replacement_str &&
3760 memcmp(ec1->replacement_str, ec2->replacement_str, ec2->replacement_len))
3761 return Qfalse;
3762
3763 if (ec1->num_trans != ec2->num_trans) return Qfalse;
3764 for (i = 0; i < ec1->num_trans; i++) {
3765 if (ec1->elems[i].tc->transcoder != ec2->elems[i].tc->transcoder)
3766 return Qfalse;
3767 }
3768 return Qtrue;
3769}
3770
3771static VALUE
3772econv_result_to_symbol(rb_econv_result_t res)
3773{
3774 switch (res) {
3775 case econv_invalid_byte_sequence: return sym_invalid_byte_sequence;
3776 case econv_incomplete_input: return sym_incomplete_input;
3777 case econv_undefined_conversion: return sym_undefined_conversion;
3778 case econv_destination_buffer_full: return sym_destination_buffer_full;
3779 case econv_source_buffer_empty: return sym_source_buffer_empty;
3780 case econv_finished: return sym_finished;
3781 case econv_after_output: return sym_after_output;
3782 default: return INT2NUM(res); /* should not be reached */
3783 }
3784}
3785
3786/*
3787 * call-seq:
3788 * ec.primitive_convert(source_buffer, destination_buffer) -> symbol
3789 * ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset) -> symbol
3790 * ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize) -> symbol
3791 * ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize, opt) -> symbol
3792 *
3793 * possible opt elements:
3794 * hash form:
3795 * :partial_input => true # source buffer may be part of larger source
3796 * :after_output => true # stop conversion after output before input
3797 * integer form:
3798 * Encoding::Converter::PARTIAL_INPUT
3799 * Encoding::Converter::AFTER_OUTPUT
3800 *
3801 * possible results:
3802 * :invalid_byte_sequence
3803 * :incomplete_input
3804 * :undefined_conversion
3805 * :after_output
3806 * :destination_buffer_full
3807 * :source_buffer_empty
3808 * :finished
3809 *
3810 * primitive_convert converts source_buffer into destination_buffer.
3811 *
3812 * source_buffer should be a string or nil.
3813 * nil means an empty string.
3814 *
3815 * destination_buffer should be a string.
3816 *
3817 * destination_byteoffset should be an integer or nil.
3818 * nil means the end of destination_buffer.
3819 * If it is omitted, nil is assumed.
3820 *
3821 * destination_bytesize should be an integer or nil.
3822 * nil means unlimited.
3823 * If it is omitted, nil is assumed.
3824 *
3825 * opt should be nil, a hash or an integer.
3826 * nil means no flags.
3827 * If it is omitted, nil is assumed.
3828 *
3829 * primitive_convert converts the content of source_buffer from beginning
3830 * and store the result into destination_buffer.
3831 *
3832 * destination_byteoffset and destination_bytesize specify the region which
3833 * the converted result is stored.
3834 * destination_byteoffset specifies the start position in destination_buffer in bytes.
3835 * If destination_byteoffset is nil,
3836 * destination_buffer.bytesize is used for appending the result.
3837 * destination_bytesize specifies maximum number of bytes.
3838 * If destination_bytesize is nil,
3839 * destination size is unlimited.
3840 * After conversion, destination_buffer is resized to
3841 * destination_byteoffset + actually produced number of bytes.
3842 * Also destination_buffer's encoding is set to destination_encoding.
3843 *
3844 * primitive_convert drops the converted part of source_buffer.
3845 * the dropped part is converted in destination_buffer or
3846 * buffered in Encoding::Converter object.
3847 *
3848 * primitive_convert stops conversion when one of following condition met.
3849 * - invalid byte sequence found in source buffer (:invalid_byte_sequence)
3850 * +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
3851 * - unexpected end of source buffer (:incomplete_input)
3852 * this occur only when :partial_input is not specified.
3853 * +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
3854 * - character not representable in output encoding (:undefined_conversion)
3855 * +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
3856 * - after some output is generated, before input is done (:after_output)
3857 * this occur only when :after_output is specified.
3858 * - destination buffer is full (:destination_buffer_full)
3859 * this occur only when destination_bytesize is non-nil.
3860 * - source buffer is empty (:source_buffer_empty)
3861 * this occur only when :partial_input is specified.
3862 * - conversion is finished (:finished)
3863 *
3864 * example:
3865 * ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
3866 * ret = ec.primitive_convert(src="pi", dst="", nil, 100)
3867 * p [ret, src, dst] #=> [:finished, "", "\x00p\x00i"]
3868 *
3869 * ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
3870 * ret = ec.primitive_convert(src="pi", dst="", nil, 1)
3871 * p [ret, src, dst] #=> [:destination_buffer_full, "i", "\x00"]
3872 * ret = ec.primitive_convert(src, dst="", nil, 1)
3873 * p [ret, src, dst] #=> [:destination_buffer_full, "", "p"]
3874 * ret = ec.primitive_convert(src, dst="", nil, 1)
3875 * p [ret, src, dst] #=> [:destination_buffer_full, "", "\x00"]
3876 * ret = ec.primitive_convert(src, dst="", nil, 1)
3877 * p [ret, src, dst] #=> [:finished, "", "i"]
3878 *
3879 */
3880static VALUE
3881econv_primitive_convert(int argc, VALUE *argv, VALUE self)
3882{
3883 VALUE input, output, output_byteoffset_v, output_bytesize_v, opt, flags_v;
3884 rb_econv_t *ec = check_econv(self);
3886 const unsigned char *ip, *is;
3887 unsigned char *op, *os;
3888 rb_len_t output_byteoffset, output_bytesize;
3889 rb_ulen_t output_byteend;
3890 int flags;
3891
3892 argc = rb_scan_args(argc, argv, "23:", &input, &output, &output_byteoffset_v, &output_bytesize_v, &flags_v, &opt);
3893
3894 if (NIL_P(output_byteoffset_v))
3895 output_byteoffset = 0; /* dummy */
3896 else
3897 output_byteoffset = NUM2LEN(output_byteoffset_v);
3898
3899 if (NIL_P(output_bytesize_v))
3900 output_bytesize = 0; /* dummy */
3901 else
3902 output_bytesize = NUM2LEN(output_bytesize_v);
3903
3904 if (!NIL_P(flags_v)) {
3905 if (!NIL_P(opt)) {
3906 rb_error_arity(argc + 1, 2, 5);
3907 }
3908 flags = NUM2INT(rb_to_int(flags_v));
3909 }
3910 else if (!NIL_P(opt)) {
3911 VALUE v;
3912 flags = 0;
3913 v = rb_hash_aref(opt, sym_partial_input);
3914 if (RTEST(v))
3915 flags |= ECONV_PARTIAL_INPUT;
3916 v = rb_hash_aref(opt, sym_after_output);
3917 if (RTEST(v))
3918 flags |= ECONV_AFTER_OUTPUT;
3919 }
3920 else {
3921 flags = 0;
3922 }
3923
3924 StringValue(output);
3925 if (!NIL_P(input))
3926 StringValue(input);
3927 rb_str_modify(output);
3928
3929 if (NIL_P(output_bytesize_v)) {
3930 output_bytesize = rb_str_capacity(output);
3931
3932 if (!NIL_P(input) && output_bytesize < RSTRING_LEN(input))
3933 output_bytesize = RSTRING_LEN(input);
3934 }
3935
3936 retry:
3937
3938 if (NIL_P(output_byteoffset_v))
3939 output_byteoffset = RSTRING_LEN(output);
3940
3941 if (output_byteoffset < 0)
3942 rb_raise(rb_eArgError, "negative output_byteoffset");
3943
3944 if (RSTRING_LEN(output) < output_byteoffset)
3945 rb_raise(rb_eArgError, "output_byteoffset too big");
3946
3947 if (output_bytesize < 0)
3948 rb_raise(rb_eArgError, "negative output_bytesize");
3949
3950 output_byteend = (rb_ulen_t)output_byteoffset +
3951 (rb_ulen_t)output_bytesize;
3952
3953 if (output_byteend < (rb_ulen_t)output_byteoffset ||
3954 RB_LEN_MAX < output_byteend)
3955 rb_raise(rb_eArgError, "output_byteoffset+output_bytesize too big");
3956
3957 if (rb_str_capacity(output) < output_byteend)
3958 rb_str_resize(output, output_byteend);
3959
3960 if (NIL_P(input)) {
3961 ip = is = NULL;
3962 }
3963 else {
3964 ip = (const unsigned char *)RSTRING_PTR(input);
3965 is = ip + RSTRING_LEN(input);
3966 }
3967
3968 op = (unsigned char *)RSTRING_PTR(output) + output_byteoffset;
3969 os = op + output_bytesize;
3970
3971 res = rb_econv_convert(ec, &ip, is, &op, os, flags);
3972 rb_str_set_len(output, op-(unsigned char *)RSTRING_PTR(output));
3973 if (!NIL_P(input)) {
3974 rb_str_drop_bytes(input, ip - (unsigned char *)RSTRING_PTR(input));
3975 }
3976
3977 if (NIL_P(output_bytesize_v) && res == econv_destination_buffer_full) {
3978 if (RB_LEN_MAX / 2 < output_bytesize)
3979 rb_raise(rb_eArgError, "too long conversion result");
3980 output_bytesize *= 2;
3981 output_byteoffset_v = Qnil;
3982 goto retry;
3983 }
3984
3985 if (ec->destination_encoding) {
3986 rb_enc_associate(output, ec->destination_encoding);
3987 }
3988
3989 return econv_result_to_symbol(res);
3990}
3991
3992/*
3993 * call-seq:
3994 * ec.convert(source_string) -> destination_string
3995 *
3996 * Convert source_string and return destination_string.
3997 *
3998 * source_string is assumed as a part of source.
3999 * i.e. :partial_input=>true is specified internally.
4000 * finish method should be used last.
4001 *
4002 * ec = Encoding::Converter.new("utf-8", "euc-jp")
4003 * puts ec.convert("\u3042").dump #=> "\xA4\xA2"
4004 * puts ec.finish.dump #=> ""
4005 *
4006 * ec = Encoding::Converter.new("euc-jp", "utf-8")
4007 * puts ec.convert("\xA4").dump #=> ""
4008 * puts ec.convert("\xA2").dump #=> "\xE3\x81\x82"
4009 * puts ec.finish.dump #=> ""
4010 *
4011 * ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
4012 * puts ec.convert("\xE3").dump #=> "".force_encoding("ISO-2022-JP")
4013 * puts ec.convert("\x81").dump #=> "".force_encoding("ISO-2022-JP")
4014 * puts ec.convert("\x82").dump #=> "\e$B$\"".force_encoding("ISO-2022-JP")
4015 * puts ec.finish.dump #=> "\e(B".force_encoding("ISO-2022-JP")
4016 *
4017 * If a conversion error occur,
4018 * Encoding::UndefinedConversionError or
4019 * Encoding::InvalidByteSequenceError is raised.
4020 * Encoding::Converter#convert doesn't supply methods to recover or restart
4021 * from these exceptions.
4022 * When you want to handle these conversion errors,
4023 * use Encoding::Converter#primitive_convert.
4024 *
4025 */
4026static VALUE
4027econv_convert(VALUE self, VALUE source_string)
4028{
4029 VALUE ret, dst;
4030 VALUE av[5];
4031 int ac;
4032 rb_econv_t *ec = check_econv(self);
4033
4034 StringValue(source_string);
4035
4036 dst = rb_str_new(NULL, 0);
4037
4038 av[0] = rb_str_dup(source_string);
4039 av[1] = dst;
4040 av[2] = Qnil;
4041 av[3] = Qnil;
4043 ac = 5;
4044
4045 ret = econv_primitive_convert(ac, av, self);
4046
4047 if (ret == sym_invalid_byte_sequence ||
4048 ret == sym_undefined_conversion ||
4049 ret == sym_incomplete_input) {
4050 VALUE exc = make_econv_exception(ec);
4051 rb_exc_raise(exc);
4052 }
4053
4054 if (ret == sym_finished) {
4055 rb_raise(rb_eArgError, "converter already finished");
4056 }
4057
4058 if (ret != sym_source_buffer_empty) {
4059 rb_bug("unexpected result of econv_primitive_convert");
4060 }
4061
4062 return dst;
4063}
4064
4065/*
4066 * call-seq:
4067 * ec.finish -> string
4068 *
4069 * Finishes the converter.
4070 * It returns the last part of the converted string.
4071 *
4072 * ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
4073 * p ec.convert("\u3042") #=> "\e$B$\""
4074 * p ec.finish #=> "\e(B"
4075 */
4076static VALUE
4077econv_finish(VALUE self)
4078{
4079 VALUE ret, dst;
4080 VALUE av[5];
4081 int ac;
4082 rb_econv_t *ec = check_econv(self);
4083
4084 dst = rb_str_new(NULL, 0);
4085
4086 av[0] = Qnil;
4087 av[1] = dst;
4088 av[2] = Qnil;
4089 av[3] = Qnil;
4090 av[4] = INT2FIX(0);
4091 ac = 5;
4092
4093 ret = econv_primitive_convert(ac, av, self);
4094
4095 if (ret == sym_invalid_byte_sequence ||
4096 ret == sym_undefined_conversion ||
4097 ret == sym_incomplete_input) {
4098 VALUE exc = make_econv_exception(ec);
4099 rb_exc_raise(exc);
4100 }
4101
4102 if (ret != sym_finished) {
4103 rb_bug("unexpected result of econv_primitive_convert");
4104 }
4105
4106 return dst;
4107}
4108
4109/*
4110 * call-seq:
4111 * ec.primitive_errinfo -> array
4112 *
4113 * primitive_errinfo returns important information regarding the last error
4114 * as a 5-element array:
4115 *
4116 * [result, enc1, enc2, error_bytes, readagain_bytes]
4117 *
4118 * result is the last result of primitive_convert.
4119 *
4120 * Other elements are only meaningful when result is
4121 * :invalid_byte_sequence, :incomplete_input or :undefined_conversion.
4122 *
4123 * enc1 and enc2 indicate a conversion step as a pair of strings.
4124 * For example, a converter from EUC-JP to ISO-8859-1 converts
4125 * a string as follows: EUC-JP -> UTF-8 -> ISO-8859-1.
4126 * So [enc1, enc2] is either ["EUC-JP", "UTF-8"] or ["UTF-8", "ISO-8859-1"].
4127 *
4128 * error_bytes and readagain_bytes indicate the byte sequences which caused the error.
4129 * error_bytes is discarded portion.
4130 * readagain_bytes is buffered portion which is read again on next conversion.
4131 *
4132 * Example:
4133 *
4134 * # \xff is invalid as EUC-JP.
4135 * ec = Encoding::Converter.new("EUC-JP", "Shift_JIS")
4136 * ec.primitive_convert(src="\xff", dst="", nil, 10)
4137 * p ec.primitive_errinfo
4138 * #=> [:invalid_byte_sequence, "EUC-JP", "Shift_JIS", "\xFF", ""]
4139 *
4140 * # HIRAGANA LETTER A (\xa4\xa2 in EUC-JP) is not representable in ISO-8859-1.
4141 * # Since this error is occur in UTF-8 to ISO-8859-1 conversion,
4142 * # error_bytes is HIRAGANA LETTER A in UTF-8 (\xE3\x81\x82).
4143 * ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
4144 * ec.primitive_convert(src="\xa4\xa2", dst="", nil, 10)
4145 * p ec.primitive_errinfo
4146 * #=> [:undefined_conversion, "UTF-8", "ISO-8859-1", "\xE3\x81\x82", ""]
4147 *
4148 * # partial character is invalid
4149 * ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
4150 * ec.primitive_convert(src="\xa4", dst="", nil, 10)
4151 * p ec.primitive_errinfo
4152 * #=> [:incomplete_input, "EUC-JP", "UTF-8", "\xA4", ""]
4153 *
4154 * # Encoding::Converter::PARTIAL_INPUT prevents invalid errors by
4155 * # partial characters.
4156 * ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
4157 * ec.primitive_convert(src="\xa4", dst="", nil, 10, Encoding::Converter::PARTIAL_INPUT)
4158 * p ec.primitive_errinfo
4159 * #=> [:source_buffer_empty, nil, nil, nil, nil]
4160 *
4161 * # \xd8\x00\x00@ is invalid as UTF-16BE because
4162 * # no low surrogate after high surrogate (\xd8\x00).
4163 * # It is detected by 3rd byte (\00) which is part of next character.
4164 * # So the high surrogate (\xd8\x00) is discarded and
4165 * # the 3rd byte is read again later.
4166 * # Since the byte is buffered in ec, it is dropped from src.
4167 * ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
4168 * ec.primitive_convert(src="\xd8\x00\x00@", dst="", nil, 10)
4169 * p ec.primitive_errinfo
4170 * #=> [:invalid_byte_sequence, "UTF-16BE", "UTF-8", "\xD8\x00", "\x00"]
4171 * p src
4172 * #=> "@"
4173 *
4174 * # Similar to UTF-16BE, \x00\xd8@\x00 is invalid as UTF-16LE.
4175 * # The problem is detected by 4th byte.
4176 * ec = Encoding::Converter.new("UTF-16LE", "UTF-8")
4177 * ec.primitive_convert(src="\x00\xd8@\x00", dst="", nil, 10)
4178 * p ec.primitive_errinfo
4179 * #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "@\x00"]
4180 * p src
4181 * #=> ""
4182 *
4183 */
4184static VALUE
4185econv_primitive_errinfo(VALUE self)
4186{
4187 rb_econv_t *ec = check_econv(self);
4188
4189 VALUE ary;
4190
4191 ary = rb_ary_new2(5);
4192
4193 rb_ary_store(ary, 0, econv_result_to_symbol(ec->last_error.result));
4194 rb_ary_store(ary, 4, Qnil);
4195
4196 if (ec->last_error.source_encoding)
4197 rb_ary_store(ary, 1, rb_str_new2(ec->last_error.source_encoding));
4198
4199 if (ec->last_error.destination_encoding)
4200 rb_ary_store(ary, 2, rb_str_new2(ec->last_error.destination_encoding));
4201
4202 if (ec->last_error.error_bytes_start) {
4203 rb_ary_store(ary, 3, rb_str_new((const char *)ec->last_error.error_bytes_start, ec->last_error.error_bytes_len));
4204 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));
4205 }
4206
4207 return ary;
4208}
4209
4210/*
4211 * call-seq:
4212 * ec.insert_output(string) -> nil
4213 *
4214 * Inserts string into the encoding converter.
4215 * The string will be converted to the destination encoding and
4216 * output on later conversions.
4217 *
4218 * If the destination encoding is stateful,
4219 * string is converted according to the state and the state is updated.
4220 *
4221 * This method should be used only when a conversion error occurs.
4222 *
4223 * ec = Encoding::Converter.new("utf-8", "iso-8859-1")
4224 * src = "HIRAGANA LETTER A is \u{3042}."
4225 * dst = ""
4226 * p ec.primitive_convert(src, dst) #=> :undefined_conversion
4227 * puts "[#{dst.dump}, #{src.dump}]" #=> ["HIRAGANA LETTER A is ", "."]
4228 * ec.insert_output("<err>")
4229 * p ec.primitive_convert(src, dst) #=> :finished
4230 * puts "[#{dst.dump}, #{src.dump}]" #=> ["HIRAGANA LETTER A is <err>.", ""]
4231 *
4232 * ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
4233 * src = "\u{306F 3041 3068 2661 3002}" # U+2661 is not representable in iso-2022-jp
4234 * dst = ""
4235 * p ec.primitive_convert(src, dst) #=> :undefined_conversion
4236 * puts "[#{dst.dump}, #{src.dump}]" #=> ["\e$B$O$!$H".force_encoding("ISO-2022-JP"), "\xE3\x80\x82"]
4237 * ec.insert_output "?" # state change required to output "?".
4238 * p ec.primitive_convert(src, dst) #=> :finished
4239 * puts "[#{dst.dump}, #{src.dump}]" #=> ["\e$B$O$!$H\e(B?\e$B!#\e(B".force_encoding("ISO-2022-JP"), ""]
4240 *
4241 */
4242static VALUE
4243econv_insert_output(VALUE self, VALUE string)
4244{
4245 const char *insert_enc;
4246
4247 int ret;
4248
4249 rb_econv_t *ec = check_econv(self);
4250
4251 StringValue(string);
4252 insert_enc = rb_econv_encoding_to_insert_output(ec);
4253 string = rb_str_encode(string, rb_enc_from_encoding(rb_enc_find(insert_enc)), 0, Qnil);
4254
4255 ret = rb_econv_insert_output(ec, (const unsigned char *)RSTRING_PTR(string), RSTRING_LEN(string), insert_enc);
4256 if (ret == -1) {
4257 rb_raise(rb_eArgError, "too big string");
4258 }
4259
4260 return Qnil;
4261}
4262
4263/*
4264 * call-seq:
4265 * ec.putback -> string
4266 * ec.putback(max_numbytes) -> string
4267 *
4268 * Put back the bytes which will be converted.
4269 *
4270 * The bytes are caused by invalid_byte_sequence error.
4271 * When invalid_byte_sequence error, some bytes are discarded and
4272 * some bytes are buffered to be converted later.
4273 * The latter bytes can be put back.
4274 * It can be observed by
4275 * Encoding::InvalidByteSequenceError#readagain_bytes and
4276 * Encoding::Converter#primitive_errinfo.
4277 *
4278 * ec = Encoding::Converter.new("utf-16le", "iso-8859-1")
4279 * src = "\x00\xd8\x61\x00"
4280 * dst = ""
4281 * p ec.primitive_convert(src, dst) #=> :invalid_byte_sequence
4282 * p ec.primitive_errinfo #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "a\x00"]
4283 * p ec.putback #=> "a\x00"
4284 * p ec.putback #=> "" # no more bytes to put back
4285 *
4286 */
4287static VALUE
4288econv_putback(int argc, VALUE *argv, VALUE self)
4289{
4290 rb_econv_t *ec = check_econv(self);
4291 int n;
4292 int putbackable;
4293 VALUE str, max;
4294
4295 if (!rb_check_arity(argc, 0, 1) || NIL_P(max = argv[0])) {
4296 n = rb_econv_putbackable(ec);
4297 }
4298 else {
4299 n = NUM2INT(max);
4300 putbackable = rb_econv_putbackable(ec);
4301 if (putbackable < n)
4302 n = putbackable;
4303 }
4304
4305 str = rb_str_new(NULL, n);
4306 rb_econv_putback(ec, (unsigned char *)RSTRING_PTR(str), n);
4307
4308 if (ec->source_encoding) {
4309 rb_enc_associate(str, ec->source_encoding);
4310 }
4311
4312 return str;
4313}
4314
4315/*
4316 * call-seq:
4317 * ec.last_error -> exception or nil
4318 *
4319 * Returns an exception object for the last conversion.
4320 * Returns nil if the last conversion did not produce an error.
4321 *
4322 * "error" means that
4323 * Encoding::InvalidByteSequenceError and Encoding::UndefinedConversionError for
4324 * Encoding::Converter#convert and
4325 * :invalid_byte_sequence, :incomplete_input and :undefined_conversion for
4326 * Encoding::Converter#primitive_convert.
4327 *
4328 * ec = Encoding::Converter.new("utf-8", "iso-8859-1")
4329 * p ec.primitive_convert(src="\xf1abcd", dst="") #=> :invalid_byte_sequence
4330 * p ec.last_error #=> #<Encoding::InvalidByteSequenceError: "\xF1" followed by "a" on UTF-8>
4331 * p ec.primitive_convert(src, dst, nil, 1) #=> :destination_buffer_full
4332 * p ec.last_error #=> nil
4333 *
4334 */
4335static VALUE
4336econv_last_error(VALUE self)
4337{
4338 rb_econv_t *ec = check_econv(self);
4339 VALUE exc;
4340
4341 exc = make_econv_exception(ec);
4342 if (NIL_P(exc))
4343 return Qnil;
4344 return exc;
4345}
4346
4347/*
4348 * call-seq:
4349 * ec.replacement -> string
4350 *
4351 * Returns the replacement string.
4352 *
4353 * ec = Encoding::Converter.new("euc-jp", "us-ascii")
4354 * p ec.replacement #=> "?"
4355 *
4356 * ec = Encoding::Converter.new("euc-jp", "utf-8")
4357 * p ec.replacement #=> "\uFFFD"
4358 */
4359static VALUE
4360econv_get_replacement(VALUE self)
4361{
4362 rb_econv_t *ec = check_econv(self);
4363 int ret;
4364 rb_encoding *enc;
4365
4366 ret = make_replacement(ec);
4367 if (ret == -1) {
4368 rb_raise(rb_eUndefinedConversionError, "replacement character setup failed");
4369 }
4370
4371 enc = rb_enc_find(ec->replacement_enc);
4372 return rb_enc_str_new((const char *)ec->replacement_str, (rb_len_t)ec->replacement_len, enc);
4373}
4374
4375/*
4376 * call-seq:
4377 * ec.replacement = string
4378 *
4379 * Sets the replacement string.
4380 *
4381 * ec = Encoding::Converter.new("utf-8", "us-ascii", :undef => :replace)
4382 * ec.replacement = "<undef>"
4383 * p ec.convert("a \u3042 b") #=> "a <undef> b"
4384 */
4385static VALUE
4386econv_set_replacement(VALUE self, VALUE arg)
4387{
4388 rb_econv_t *ec = check_econv(self);
4389 VALUE string = arg;
4390 int ret;
4391 rb_encoding *enc;
4392
4393 StringValue(string);
4394 enc = rb_enc_get(string);
4395
4396 ret = rb_econv_set_replacement(ec,
4397 (const unsigned char *)RSTRING_PTR(string),
4398 RSTRING_LEN(string),
4399 rb_enc_name(enc));
4400
4401 if (ret == -1) {
4402 /* xxx: rb_eInvalidByteSequenceError? */
4403 rb_raise(rb_eUndefinedConversionError, "replacement character setup failed");
4404 }
4405
4406 return arg;
4407}
4408
4409VALUE
4411{
4412 return make_econv_exception(ec);
4413}
4414
4415void
4417{
4418 VALUE exc;
4419
4420 exc = make_econv_exception(ec);
4421 if (NIL_P(exc))
4422 return;
4423 rb_exc_raise(exc);
4424}
4425
4426/*
4427 * call-seq:
4428 * ecerr.source_encoding_name -> string
4429 *
4430 * Returns the source encoding name as a string.
4431 */
4432static VALUE
4433ecerr_source_encoding_name(VALUE self)
4434{
4435 return rb_attr_get(self, id_source_encoding_name);
4436}
4437
4438/*
4439 * call-seq:
4440 * ecerr.source_encoding -> encoding
4441 *
4442 * Returns the source encoding as an encoding object.
4443 *
4444 * Note that the result may not be equal to the source encoding of
4445 * the encoding converter if the conversion has multiple steps.
4446 *
4447 * ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP") # ISO-8859-1 -> UTF-8 -> EUC-JP
4448 * begin
4449 * ec.convert("\xa0") # NO-BREAK SPACE, which is available in UTF-8 but not in EUC-JP.
4450 * rescue Encoding::UndefinedConversionError
4451 * p $!.source_encoding #=> #<Encoding:UTF-8>
4452 * p $!.destination_encoding #=> #<Encoding:EUC-JP>
4453 * p $!.source_encoding_name #=> "UTF-8"
4454 * p $!.destination_encoding_name #=> "EUC-JP"
4455 * end
4456 *
4457 */
4458static VALUE
4459ecerr_source_encoding(VALUE self)
4460{
4461 return rb_attr_get(self, id_source_encoding);
4462}
4463
4464/*
4465 * call-seq:
4466 * ecerr.destination_encoding_name -> string
4467 *
4468 * Returns the destination encoding name as a string.
4469 */
4470static VALUE
4471ecerr_destination_encoding_name(VALUE self)
4472{
4473 return rb_attr_get(self, id_destination_encoding_name);
4474}
4475
4476/*
4477 * call-seq:
4478 * ecerr.destination_encoding -> string
4479 *
4480 * Returns the destination encoding as an encoding object.
4481 */
4482static VALUE
4483ecerr_destination_encoding(VALUE self)
4484{
4485 return rb_attr_get(self, id_destination_encoding);
4486}
4487
4488/*
4489 * call-seq:
4490 * ecerr.error_char -> string
4491 *
4492 * Returns the one-character string which cause Encoding::UndefinedConversionError.
4493 *
4494 * ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP")
4495 * begin
4496 * ec.convert("\xa0")
4497 * rescue Encoding::UndefinedConversionError
4498 * puts $!.error_char.dump #=> "\xC2\xA0"
4499 * p $!.error_char.encoding #=> #<Encoding:UTF-8>
4500 * end
4501 *
4502 */
4503static VALUE
4504ecerr_error_char(VALUE self)
4505{
4506 return rb_attr_get(self, id_error_char);
4507}
4508
4509/*
4510 * call-seq:
4511 * ecerr.error_bytes -> string
4512 *
4513 * Returns the discarded bytes when Encoding::InvalidByteSequenceError occurs.
4514 *
4515 * ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
4516 * begin
4517 * ec.convert("abc\xA1\xFFdef")
4518 * rescue Encoding::InvalidByteSequenceError
4519 * p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "\xFF" on EUC-JP>
4520 * puts $!.error_bytes.dump #=> "\xA1"
4521 * puts $!.readagain_bytes.dump #=> "\xFF"
4522 * end
4523 */
4524static VALUE
4525ecerr_error_bytes(VALUE self)
4526{
4527 return rb_attr_get(self, id_error_bytes);
4528}
4529
4530/*
4531 * call-seq:
4532 * ecerr.readagain_bytes -> string
4533 *
4534 * Returns the bytes to be read again when Encoding::InvalidByteSequenceError occurs.
4535 */
4536static VALUE
4537ecerr_readagain_bytes(VALUE self)
4538{
4539 return rb_attr_get(self, id_readagain_bytes);
4540}
4541
4542/*
4543 * call-seq:
4544 * ecerr.incomplete_input? -> true or false
4545 *
4546 * Returns true if the invalid byte sequence error is caused by
4547 * premature end of string.
4548 *
4549 * ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
4550 *
4551 * begin
4552 * ec.convert("abc\xA1z")
4553 * rescue Encoding::InvalidByteSequenceError
4554 * p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "z" on EUC-JP>
4555 * p $!.incomplete_input? #=> false
4556 * end
4557 *
4558 * begin
4559 * ec.convert("abc\xA1")
4560 * ec.finish
4561 * rescue Encoding::InvalidByteSequenceError
4562 * p $! #=> #<Encoding::InvalidByteSequenceError: incomplete "\xA1" on EUC-JP>
4563 * p $!.incomplete_input? #=> true
4564 * end
4565 */
4566static VALUE
4567ecerr_incomplete_input(VALUE self)
4568{
4569 return rb_attr_get(self, id_incomplete_input);
4570}
4571
4572/*
4573 * Document-class: Encoding::UndefinedConversionError
4574 *
4575 * Raised by Encoding and String methods when a transcoding operation
4576 * fails.
4577 */
4578
4579/*
4580 * Document-class: Encoding::InvalidByteSequenceError
4581 *
4582 * Raised by Encoding and String methods when the string being
4583 * transcoded contains a byte invalid for the either the source or
4584 * target encoding.
4585 */
4586
4587/*
4588 * Document-class: Encoding::ConverterNotFoundError
4589 *
4590 * Raised by transcoding methods when a named encoding does not
4591 * correspond with a known converter.
4592 */
4593
4594void
4595Init_transcode(void)
4596{
4597 transcoder_table = st_init_strcasetable();
4598
4599 id_destination_encoding = rb_intern_const("destination_encoding");
4600 id_destination_encoding_name = rb_intern_const("destination_encoding_name");
4601 id_error_bytes = rb_intern_const("error_bytes");
4602 id_error_char = rb_intern_const("error_char");
4603 id_incomplete_input = rb_intern_const("incomplete_input");
4604 id_readagain_bytes = rb_intern_const("readagain_bytes");
4605 id_source_encoding = rb_intern_const("source_encoding");
4606 id_source_encoding_name = rb_intern_const("source_encoding_name");
4607
4608 sym_invalid = ID2SYM(rb_intern_const("invalid"));
4609 sym_undef = ID2SYM(rb_intern_const("undef"));
4610 sym_replace = ID2SYM(rb_intern_const("replace"));
4611 sym_fallback = ID2SYM(rb_intern_const("fallback"));
4612 sym_xml = ID2SYM(rb_intern_const("xml"));
4613 sym_text = ID2SYM(rb_intern_const("text"));
4614 sym_attr = ID2SYM(rb_intern_const("attr"));
4615
4616 sym_invalid_byte_sequence = ID2SYM(rb_intern_const("invalid_byte_sequence"));
4617 sym_undefined_conversion = ID2SYM(rb_intern_const("undefined_conversion"));
4618 sym_destination_buffer_full = ID2SYM(rb_intern_const("destination_buffer_full"));
4619 sym_source_buffer_empty = ID2SYM(rb_intern_const("source_buffer_empty"));
4620 sym_finished = ID2SYM(rb_intern_const("finished"));
4621 sym_after_output = ID2SYM(rb_intern_const("after_output"));
4622 sym_incomplete_input = ID2SYM(rb_intern_const("incomplete_input"));
4623 sym_universal_newline = ID2SYM(rb_intern_const("universal_newline"));
4624 sym_crlf_newline = ID2SYM(rb_intern_const("crlf_newline"));
4625 sym_cr_newline = ID2SYM(rb_intern_const("cr_newline"));
4626 sym_lf_newline = ID2SYM(rb_intern("lf_newline"));
4627 sym_partial_input = ID2SYM(rb_intern_const("partial_input"));
4628
4629#ifdef ENABLE_ECONV_NEWLINE_OPTION
4630 sym_newline = ID2SYM(rb_intern_const("newline"));
4631 sym_universal = ID2SYM(rb_intern_const("universal"));
4632 sym_crlf = ID2SYM(rb_intern_const("crlf"));
4633 sym_cr = ID2SYM(rb_intern_const("cr"));
4634 sym_lf = ID2SYM(rb_intern_const("lf"));
4635#endif
4636
4637 InitVM(transcode);
4638}
4639
4640void
4641InitVM_transcode(void)
4642{
4643 rb_eUndefinedConversionError = rb_define_class_under(rb_cEncoding, "UndefinedConversionError", rb_eEncodingError);
4644 rb_eInvalidByteSequenceError = rb_define_class_under(rb_cEncoding, "InvalidByteSequenceError", rb_eEncodingError);
4645 rb_eConverterNotFoundError = rb_define_class_under(rb_cEncoding, "ConverterNotFoundError", rb_eEncodingError);
4646
4647 rb_define_method(rb_cString, "encode", str_encode, -1);
4648 rb_define_method(rb_cString, "encode!", str_encode_bang, -1);
4649
4650 rb_cEncodingConverter = rb_define_class_under(rb_cEncoding, "Converter", rb_cObject);
4651 rb_define_alloc_func(rb_cEncodingConverter, econv_s_allocate);
4652 rb_define_singleton_method(rb_cEncodingConverter, "asciicompat_encoding", econv_s_asciicompat_encoding, 1);
4653 rb_define_singleton_method(rb_cEncodingConverter, "search_convpath", econv_s_search_convpath, -1);
4654 rb_define_method(rb_cEncodingConverter, "initialize", econv_init, -1);
4655 rb_define_method(rb_cEncodingConverter, "inspect", econv_inspect, 0);
4656 rb_define_method(rb_cEncodingConverter, "convpath", econv_convpath, 0);
4657 rb_define_method(rb_cEncodingConverter, "source_encoding", econv_source_encoding, 0);
4658 rb_define_method(rb_cEncodingConverter, "destination_encoding", econv_destination_encoding, 0);
4659 rb_define_method(rb_cEncodingConverter, "primitive_convert", econv_primitive_convert, -1);
4660 rb_define_method(rb_cEncodingConverter, "convert", econv_convert, 1);
4661 rb_define_method(rb_cEncodingConverter, "finish", econv_finish, 0);
4662 rb_define_method(rb_cEncodingConverter, "primitive_errinfo", econv_primitive_errinfo, 0);
4663 rb_define_method(rb_cEncodingConverter, "insert_output", econv_insert_output, 1);
4664 rb_define_method(rb_cEncodingConverter, "putback", econv_putback, -1);
4665 rb_define_method(rb_cEncodingConverter, "last_error", econv_last_error, 0);
4666 rb_define_method(rb_cEncodingConverter, "replacement", econv_get_replacement, 0);
4667 rb_define_method(rb_cEncodingConverter, "replacement=", econv_set_replacement, 1);
4668 rb_define_method(rb_cEncodingConverter, "==", econv_equal, 1);
4669
4670 /*
4671 *Mask for invalid byte sequences
4672 */
4673 rb_define_const(rb_cEncodingConverter, "INVALID_MASK", INT2FIX(ECONV_INVALID_MASK));
4674
4675 /*
4676 * Replace invalid byte sequences
4677 */
4678 rb_define_const(rb_cEncodingConverter, "INVALID_REPLACE", INT2FIX(ECONV_INVALID_REPLACE));
4679
4680 /*
4681 * Mask for a valid character in the source encoding but no related
4682 * character(s) in destination encoding.
4683 */
4684 rb_define_const(rb_cEncodingConverter, "UNDEF_MASK", INT2FIX(ECONV_UNDEF_MASK));
4685
4686 /*
4687 * Replace byte sequences that are undefined in the destination encoding.
4688 */
4689 rb_define_const(rb_cEncodingConverter, "UNDEF_REPLACE", INT2FIX(ECONV_UNDEF_REPLACE));
4690
4691 /*
4692 * Replace byte sequences that are undefined in the destination encoding
4693 * with an XML hexadecimal character reference. This is valid for XML
4694 * conversion.
4695 */
4696 rb_define_const(rb_cEncodingConverter, "UNDEF_HEX_CHARREF", INT2FIX(ECONV_UNDEF_HEX_CHARREF));
4697
4698 /*
4699 * Indicates the source may be part of a larger string. See
4700 * primitive_convert for an example.
4701 */
4702 rb_define_const(rb_cEncodingConverter, "PARTIAL_INPUT", INT2FIX(ECONV_PARTIAL_INPUT));
4703
4704 /*
4705 * Stop converting after some output is complete but before all of the
4706 * input was consumed. See primitive_convert for an example.
4707 */
4708 rb_define_const(rb_cEncodingConverter, "AFTER_OUTPUT", INT2FIX(ECONV_AFTER_OUTPUT));
4709
4710 /*
4711 * Decorator for converting CRLF and CR to LF
4712 */
4713 rb_define_const(rb_cEncodingConverter, "UNIVERSAL_NEWLINE_DECORATOR", INT2FIX(ECONV_UNIVERSAL_NEWLINE_DECORATOR));
4714
4715 /*
4716 * Decorator for converting CRLF and CR to LF when writing
4717 */
4718 rb_define_const(rb_cEncodingConverter, "LF_NEWLINE_DECORATOR", INT2FIX(ECONV_LF_NEWLINE_DECORATOR));
4719
4720 /*
4721 * Decorator for converting LF to CRLF
4722 */
4723 rb_define_const(rb_cEncodingConverter, "CRLF_NEWLINE_DECORATOR", INT2FIX(ECONV_CRLF_NEWLINE_DECORATOR));
4724
4725 /*
4726 * Decorator for converting LF to CR
4727 */
4728 rb_define_const(rb_cEncodingConverter, "CR_NEWLINE_DECORATOR", INT2FIX(ECONV_CR_NEWLINE_DECORATOR));
4729
4730 /*
4731 * Escape as XML CharData
4732 */
4733 rb_define_const(rb_cEncodingConverter, "XML_TEXT_DECORATOR", INT2FIX(ECONV_XML_TEXT_DECORATOR));
4734
4735 /*
4736 * Escape as XML AttValue
4737 */
4738 rb_define_const(rb_cEncodingConverter, "XML_ATTR_CONTENT_DECORATOR", INT2FIX(ECONV_XML_ATTR_CONTENT_DECORATOR));
4739
4740 /*
4741 * Escape as XML AttValue
4742 */
4743 rb_define_const(rb_cEncodingConverter, "XML_ATTR_QUOTE_DECORATOR", INT2FIX(ECONV_XML_ATTR_QUOTE_DECORATOR));
4744
4745 rb_define_method(rb_eUndefinedConversionError, "source_encoding_name", ecerr_source_encoding_name, 0);
4746 rb_define_method(rb_eUndefinedConversionError, "destination_encoding_name", ecerr_destination_encoding_name, 0);
4747 rb_define_method(rb_eUndefinedConversionError, "source_encoding", ecerr_source_encoding, 0);
4748 rb_define_method(rb_eUndefinedConversionError, "destination_encoding", ecerr_destination_encoding, 0);
4749 rb_define_method(rb_eUndefinedConversionError, "error_char", ecerr_error_char, 0);
4750
4751 rb_define_method(rb_eInvalidByteSequenceError, "source_encoding_name", ecerr_source_encoding_name, 0);
4752 rb_define_method(rb_eInvalidByteSequenceError, "destination_encoding_name", ecerr_destination_encoding_name, 0);
4753 rb_define_method(rb_eInvalidByteSequenceError, "source_encoding", ecerr_source_encoding, 0);
4754 rb_define_method(rb_eInvalidByteSequenceError, "destination_encoding", ecerr_destination_encoding, 0);
4755 rb_define_method(rb_eInvalidByteSequenceError, "error_bytes", ecerr_error_bytes, 0);
4756 rb_define_method(rb_eInvalidByteSequenceError, "readagain_bytes", ecerr_readagain_bytes, 0);
4757 rb_define_method(rb_eInvalidByteSequenceError, "incomplete_input?", ecerr_incomplete_input, 0);
4758
4759 Init_newline();
4760}
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:3358
#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:1700
#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:403
#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:1708
#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:402
#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 NUM2LEN
Old name of RB_NUM2LEN.
Definition len_t.h:34
#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:1457
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1455
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:1443
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:1508
VALUE rb_eEncodingError
EncodingError exception.
Definition error.c:1463
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:495
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:230
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:3337
Encoding relates APIs.
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
Definition string.c:1393
rb_len_t 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:855
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:2681
VALUE rb_econv_substr_convert(rb_econv_t *ec, VALUE src, rb_len_t byteoff, rb_len_t bytesize, int flags)
Identical to rb_econv_str_convert(), except it converts only a part of the passed string.
Definition transcode.c:1956
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:2726
VALUE rb_econv_substr_append(rb_econv_t *ec, VALUE src, rb_len_t byteoff, rb_len_t 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
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:3349
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
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:2732
VALUE rb_econv_append(rb_econv_t *ec, const char *bytesrc, rb_len_t 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
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:3044
VALUE rb_econv_make_exception(rb_econv_t *ec)
This function makes sense right after rb_econv_convert() returns.
Definition transcode.c:4410
void rb_econv_check_error(rb_econv_t *ec)
This is a rb_econv_make_exception() + rb_exc_raise() combo.
Definition transcode.c:4416
void rb_econv_close(rb_econv_t *ec)
Destructs a converter.
Definition transcode.c:1744
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:2291
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:1200
VALUE rb_ary_entry(VALUE ary, rb_len_t off)
Queries an element of an array.
void rb_ary_store(VALUE ary, rb_len_t key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
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_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
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:1743
VALUE rb_obj_is_method(VALUE recv)
Queries if the given object is a method.
Definition proc.c:2465
VALUE rb_method_call(int argc, const VALUE *argv, VALUE recv)
Evaluates the passed method with the passed arguments.
Definition proc.c:3420
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:386
void rb_str_modify_expand(VALUE str, rb_len_t capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition string.c:2838
VALUE rb_str_tmp_new(rb_len_t len)
Allocates a "temporary" string.
Definition string.c:1811
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1523
void rb_str_shared_replace(VALUE dst, VALUE src)
Replaces the contents of the former with the latter.
Definition string.c:1853
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
Definition string.c:1037
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition string.c:1569
void rb_str_set_len(VALUE str, rb_len_t len)
Overwrites the length of the string.
Definition string.c:3522
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:2052
VALUE rb_str_drop_bytes(VALUE str, rb_len_t len)
Shrinks the given string for the given number of bytes.
Definition string.c:6198
VALUE rb_str_buf_new(rb_len_t capa)
Allocates a "string buffer".
Definition string.c:1783
VALUE rb_str_dump(VALUE str)
"Inverse" of rb_eval_string().
Definition string.c:9398
#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:1539
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:2168
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3743
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:1155
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:375
#define ALLOCA_N(type, n)
Definition memory.h:295
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
Definition memory.h:387
#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)
References an object in an array.
Definition rarray.h:421
#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:532
#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:10420
Definition transcode.c:179
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
#define RB_LEN_MAX
Maximum possible value that a rb_len_t can take.
Definition value.h:156
unsigned long rb_ulen_t
An unsigned integer type that has the same width with rb_len_t.
Definition value.h:145
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