Ruby  1.9.3p551(2014-11-13revision48407)
iconv.c
Go to the documentation of this file.
1 /* -*- mode:c; c-file-style:"ruby" -*- */
2 /**********************************************************************
3 
4  iconv.c -
5 
6  $Author: nobu $
7  created at: Wed Dec 1 20:28:09 JST 1999
8 
9  All the files in this distribution are covered under the Ruby's
10  license (see the file COPYING).
11 
12  Documentation by Yukihiro Matsumoto and Gavin Sinclair.
13 
14 **********************************************************************/
15 
16 #include "ruby/ruby.h"
17 #include <errno.h>
18 #include <iconv.h>
19 #include <assert.h>
20 #include "ruby/st.h"
21 #include "ruby/encoding.h"
22 
23 /*
24  * Document-class: Iconv
25  *
26  * == Summary
27  *
28  * Ruby extension for charset conversion.
29  *
30  * == Abstract
31  *
32  * Iconv is a wrapper class for the UNIX 95 <tt>iconv()</tt> function family,
33  * which translates string between various encoding systems.
34  *
35  * See Open Group's on-line documents for more details.
36  * * <tt>iconv.h</tt>: http://www.opengroup.org/onlinepubs/007908799/xsh/iconv.h.html
37  * * <tt>iconv_open()</tt>: http://www.opengroup.org/onlinepubs/007908799/xsh/iconv_open.html
38  * * <tt>iconv()</tt>: http://www.opengroup.org/onlinepubs/007908799/xsh/iconv.html
39  * * <tt>iconv_close()</tt>: http://www.opengroup.org/onlinepubs/007908799/xsh/iconv_close.html
40  *
41  * Which coding systems are available is platform-dependent.
42  *
43  * == Examples
44  *
45  * 1. Simple conversion between two charsets.
46  *
47  * converted_text = Iconv.conv('iso-8859-15', 'utf-8', text)
48  *
49  * 2. Instantiate a new Iconv and use method Iconv#iconv.
50  *
51  * cd = Iconv.new(to, from)
52  * begin
53  * input.each { |s| output << cd.iconv(s) }
54  * output << cd.iconv(nil) # Don't forget this!
55  * ensure
56  * cd.close
57  * end
58  *
59  * 3. Invoke Iconv.open with a block.
60  *
61  * Iconv.open(to, from) do |cd|
62  * input.each { |s| output << cd.iconv(s) }
63  * output << cd.iconv(nil)
64  * end
65  *
66  * 4. Shorthand for (3).
67  *
68  * Iconv.iconv(to, from, *input.to_a)
69  *
70  * == Attentions
71  *
72  * Even if some extentions of implementation dependent are useful,
73  * DON'T USE those extentions in libraries and scripts to widely distribute.
74  * If you want to use those feature, use String#encode.
75  */
76 
77 /* Invalid value for iconv_t is -1 but 0 for VALUE, I hope VALUE is
78  big enough to keep iconv_t */
79 #define VALUE2ICONV(v) ((iconv_t)((VALUE)(v) ^ -1))
80 #define ICONV2VALUE(c) ((VALUE)(c) ^ -1)
81 
83 {
84  iconv_t cd;
85  int argc;
88  int toidx;
89  VALUE (*append)_((VALUE, VALUE));
90 };
91 
93 {
96 };
97 
99 
106 
108 static VALUE iconv_fail _((VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg));
109 static VALUE iconv_fail_retry _((VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg));
110 static VALUE iconv_failure_initialize _((VALUE error, VALUE mesg, VALUE success, VALUE failed));
111 static VALUE iconv_failure_success _((VALUE self));
112 static VALUE iconv_failure_failed _((VALUE self));
113 
114 static iconv_t iconv_create _((VALUE to, VALUE from, struct rb_iconv_opt_t *opt, int *idx));
115 static void iconv_dfree _((void *cd));
116 static VALUE iconv_free _((VALUE cd));
117 static VALUE iconv_try _((iconv_t cd, const char **inptr, size_t *inlen, char **outptr, size_t *outlen));
118 static VALUE rb_str_derive _((VALUE str, const char* ptr, long len));
119 static VALUE iconv_convert _((iconv_t cd, VALUE str, long start, long length, int toidx,
120  struct iconv_env_t* env));
122 static VALUE iconv_initialize _((int argc, VALUE *argv, VALUE self));
123 static VALUE iconv_s_open _((int argc, VALUE *argv, VALUE self));
124 static VALUE iconv_s_convert _((struct iconv_env_t* env));
125 static VALUE iconv_s_iconv _((int argc, VALUE *argv, VALUE self));
126 static VALUE iconv_init_state _((VALUE cd));
127 static VALUE iconv_finish _((VALUE self));
128 static VALUE iconv_iconv _((int argc, VALUE *argv, VALUE self));
129 static VALUE iconv_conv _((int argc, VALUE *argv, VALUE self));
130 
132 
133 /*
134  * Document-method: charset_map
135  * call-seq: Iconv.charset_map
136  *
137  * Returns the map from canonical name to system dependent name.
138  */
139 static VALUE
141 {
142  return charset_map;
143 }
144 
145 static VALUE
147 {
148  VALUE val = StringValue(*code);
149  const char *ptr = RSTRING_PTR(val), *pend = RSTRING_END(val);
150  const char *slash = memchr(ptr, '/', pend - ptr);
151 
152  if (slash && slash < pend - 1 && slash[1] == '/') {
153  VALUE opt = rb_str_subseq(val, slash - ptr, pend - slash);
154  val = rb_str_subseq(val, 0, slash - ptr);
155  *code = val;
156  return opt;
157  }
158  return 0;
159 }
160 
161 static char *
163 {
164  VALUE val = StringValue(*code);
165 
166  if (RHASH_SIZE(charset_map)) {
167  st_data_t data;
168  VALUE key = rb_funcall2(val, rb_intern("downcase"), 0, 0);
169  StringValuePtr(key);
170  if (st_lookup(RHASH_TBL(charset_map), key, &data)) {
171  *code = (VALUE)data;
172  }
173  }
174  return StringValuePtr(*code);
175 }
176 
177 NORETURN(static void rb_iconv_sys_fail(const char *s));
178 static void
179 rb_iconv_sys_fail(const char *s)
180 {
181  if (errno == 0) {
183  }
184  rb_sys_fail(s);
185 }
186 
187 #define rb_sys_fail(s) rb_iconv_sys_fail(s)
188 
189 static iconv_t
190 iconv_create(VALUE to, VALUE from, struct rb_iconv_opt_t *opt, int *idx)
191 {
192  VALUE toopt = strip_glibc_option(&to);
193  VALUE fromopt = strip_glibc_option(&from);
194  VALUE toenc = 0, fromenc = 0;
195  const char* tocode = map_charset(&to);
196  const char* fromcode = map_charset(&from);
197  iconv_t cd;
198  int retry = 0;
199 
200  *idx = rb_enc_find_index(tocode);
201 
202  if (toopt) {
203  toenc = rb_str_plus(to, toopt);
204  tocode = RSTRING_PTR(toenc);
205  }
206  if (fromopt) {
207  fromenc = rb_str_plus(from, fromopt);
208  fromcode = RSTRING_PTR(fromenc);
209  }
210  while ((cd = iconv_open(tocode, fromcode)) == (iconv_t)-1) {
211  int inval = 0;
212  switch (errno) {
213  case EMFILE:
214  case ENFILE:
215  case ENOMEM:
216  if (!retry++) {
217  rb_gc();
218  continue;
219  }
220  break;
221  case EINVAL:
222  retry = 0;
223  inval = 1;
224  if (toenc) {
225  tocode = RSTRING_PTR(to);
226  rb_str_resize(toenc, 0);
227  toenc = 0;
228  continue;
229  }
230  if (fromenc) {
231  fromcode = RSTRING_PTR(from);
232  rb_str_resize(fromenc, 0);
233  fromenc = 0;
234  continue;
235  }
236  break;
237  }
238  {
239  const char *s = inval ? "invalid encoding " : "iconv";
240  volatile VALUE msg = rb_str_new(0, strlen(s) + RSTRING_LEN(to) +
241  RSTRING_LEN(from) + 8);
242 
243  sprintf(RSTRING_PTR(msg), "%s(\"%s\", \"%s\")",
244  s, RSTRING_PTR(to), RSTRING_PTR(from));
245  s = RSTRING_PTR(msg);
246  rb_str_set_len(msg, strlen(s));
247  if (!inval) rb_sys_fail(s);
249  rb_ary_new3(2, to, from), NULL, s));
250  }
251  }
252 
253  if (toopt || fromopt) {
254  if (toopt && fromopt && RTEST(rb_str_equal(toopt, fromopt))) {
255  fromopt = 0;
256  }
257  if (toopt && fromopt) {
258  rb_warning("encoding option isn't portable: %s, %s",
259  RSTRING_PTR(toopt) + 2, RSTRING_PTR(fromopt) + 2);
260  }
261  else {
262  rb_warning("encoding option isn't portable: %s",
263  (toopt ? RSTRING_PTR(toopt) : RSTRING_PTR(fromopt)) + 2);
264  }
265  }
266 
267  if (opt) {
268 #ifdef ICONV_SET_TRANSLITERATE
269  if (opt->transliterate != Qundef) {
270  int flag = RTEST(opt->transliterate);
271  rb_warning("encoding option isn't portable: transliterate");
272  if (iconvctl(cd, ICONV_SET_TRANSLITERATE, (void *)&flag))
273  rb_sys_fail("ICONV_SET_TRANSLITERATE");
274  }
275 #endif
276 #ifdef ICONV_SET_DISCARD_ILSEQ
277  if (opt->discard_ilseq != Qundef) {
278  int flag = RTEST(opt->discard_ilseq);
279  rb_warning("encoding option isn't portable: discard_ilseq");
280  if (iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, (void *)&flag))
281  rb_sys_fail("ICONV_SET_DISCARD_ILSEQ");
282  }
283 #endif
284  }
285 
286  return cd;
287 }
288 
289 static void
291 {
292  iconv_close(VALUE2ICONV(cd));
293 }
294 
295 #define ICONV_FREE iconv_dfree
296 
297 static VALUE
299 {
300  if (cd && iconv_close(VALUE2ICONV(cd)) == -1)
301  rb_sys_fail("iconv_close");
302  return Qnil;
303 }
304 
305 static VALUE
307 {
308  Check_Type(obj, T_DATA);
309  if (RDATA(obj)->dfree != ICONV_FREE) {
310  rb_raise(rb_eArgError, "Iconv expected (%s)", rb_class2name(CLASS_OF(obj)));
311  }
312  return (VALUE)DATA_PTR(obj);
313 }
314 
315 static VALUE
316 iconv_try(iconv_t cd, const char **inptr, size_t *inlen, char **outptr, size_t *outlen)
317 {
318 #ifdef ICONV_INPTR_CONST
319 #define ICONV_INPTR_CAST
320 #else
321 #define ICONV_INPTR_CAST (char **)
322 #endif
323  size_t ret;
324 
325  errno = 0;
326  ret = iconv(cd, ICONV_INPTR_CAST inptr, inlen, outptr, outlen);
327  if (ret == (size_t)-1) {
328  if (!*inlen)
329  return Qfalse;
330  switch (errno) {
331  case E2BIG:
332  /* try the left in next loop */
333  break;
334  case EILSEQ:
335  return rb_eIconvIllegalSeq;
336  case EINVAL:
337  return rb_eIconvInvalidChar;
338  case 0:
339  return rb_eIconvBrokenLibrary;
340  default:
341  rb_sys_fail("iconv");
342  }
343  }
344  else if (*inlen > 0) {
345  /* something goes wrong */
346  return rb_eIconvIllegalSeq;
347  }
348  else if (ret) {
349  return Qnil; /* conversion */
350  }
351  return Qfalse;
352 }
353 
354 #define FAILED_MAXLEN 16
355 
356 static VALUE
357 iconv_failure_initialize(VALUE error, VALUE mesg, VALUE success, VALUE failed)
358 {
359  rb_call_super(1, &mesg);
360  rb_ivar_set(error, rb_success, success);
361  rb_ivar_set(error, rb_failed, failed);
362  return error;
363 }
364 
365 static VALUE
366 iconv_fail(VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg)
367 {
368  VALUE args[3];
369 
370  if (mesg && *mesg) {
371  args[0] = rb_str_new2(mesg);
372  }
373  else if (TYPE(failed) != T_STRING || RSTRING_LEN(failed) < FAILED_MAXLEN) {
374  args[0] = rb_inspect(failed);
375  }
376  else {
377  args[0] = rb_inspect(rb_str_substr(failed, 0, FAILED_MAXLEN));
378  rb_str_cat2(args[0], "...");
379  }
380  args[1] = success;
381  args[2] = failed;
382  if (env) {
383  args[1] = env->append(rb_obj_dup(env->ret), success);
384  if (env->argc > 0) {
385  *(env->argv) = failed;
386  args[2] = rb_ary_new4(env->argc, env->argv);
387  }
388  }
389  return rb_class_new_instance(3, args, error);
390 }
391 
392 static VALUE
393 iconv_fail_retry(VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg)
394 {
395  error = iconv_fail(error, success, failed, env, mesg);
396  if (!rb_block_given_p()) rb_exc_raise(error);
397  rb_set_errinfo(error);
398  return rb_yield(failed);
399 }
400 
401 static VALUE
402 rb_str_derive(VALUE str, const char* ptr, long len)
403 {
404  VALUE ret;
405 
406  if (NIL_P(str))
407  return rb_str_new(ptr, len);
408  if (RSTRING_PTR(str) + RSTRING_LEN(str) == ptr + len)
409  ret = rb_str_subseq(str, ptr - RSTRING_PTR(str), len);
410  else
411  ret = rb_str_new(ptr, len);
412  OBJ_INFECT(ret, str);
413  return ret;
414 }
415 
416 static VALUE
417 iconv_convert(iconv_t cd, VALUE str, long start, long length, int toidx, struct iconv_env_t* env)
418 {
419  VALUE ret = Qfalse;
420  VALUE error = Qfalse;
421  VALUE rescue;
422  const char *inptr, *instart;
423  size_t inlen;
424  /* I believe ONE CHARACTER never exceed this. */
425  char buffer[BUFSIZ];
426  char *outptr;
427  size_t outlen;
428 
429  if (cd == (iconv_t)-1)
430  rb_raise(rb_eArgError, "closed iconv");
431 
432  if (NIL_P(str)) {
433  /* Reset output pointer or something. */
434  inptr = "";
435  inlen = 0;
436  outptr = buffer;
437  outlen = sizeof(buffer);
438  error = iconv_try(cd, &inptr, &inlen, &outptr, &outlen);
439  if (RTEST(error)) {
440  unsigned int i;
441  rescue = iconv_fail_retry(error, Qnil, Qnil, env, 0);
442  if (TYPE(rescue) == T_ARRAY) {
443  str = RARRAY_LEN(rescue) > 0 ? RARRAY_PTR(rescue)[0] : Qnil;
444  }
445  if (FIXNUM_P(str) && (i = FIX2INT(str)) <= 0xff) {
446  char c = i;
447  str = rb_str_new(&c, 1);
448  }
449  else if (!NIL_P(str)) {
450  StringValue(str);
451  }
452  }
453 
454  inptr = NULL;
455  length = 0;
456  }
457  else {
458  long slen;
459 
460  StringValue(str);
461  slen = RSTRING_LEN(str);
462  inptr = RSTRING_PTR(str);
463 
464  inptr += start;
465  if (length < 0 || length > start + slen)
466  length = slen - start;
467  }
468  instart = inptr;
469  inlen = length;
470 
471  do {
472  char errmsg[50];
473  const char *tmpstart = inptr;
474  outptr = buffer;
475  outlen = sizeof(buffer);
476 
477  errmsg[0] = 0;
478  error = iconv_try(cd, &inptr, &inlen, &outptr, &outlen);
479 
480  if (
481 #if SIGNEDNESS_OF_SIZE_T < 0
482  0 <= outlen &&
483 #endif
484  outlen <= sizeof(buffer)) {
485  outlen = sizeof(buffer) - outlen;
486  if (NIL_P(error) || /* something converted */
487  outlen > (size_t)(inptr - tmpstart) || /* input can't contain output */
488  (outlen < (size_t)(inptr - tmpstart) && inlen > 0) || /* something skipped */
489  memcmp(buffer, tmpstart, outlen)) /* something differs */
490  {
491  if (NIL_P(str)) {
492  ret = rb_str_new(buffer, outlen);
493  if (toidx >= 0) rb_enc_associate_index(ret, toidx);
494  }
495  else {
496  if (ret) {
497  ret = rb_str_buf_cat(ret, instart, tmpstart - instart);
498  }
499  else {
500  ret = rb_str_new(instart, tmpstart - instart);
501  if (toidx >= 0) rb_enc_associate_index(ret, toidx);
502  OBJ_INFECT(ret, str);
503  }
504  ret = rb_str_buf_cat(ret, buffer, outlen);
505  instart = inptr;
506  }
507  }
508  else if (!inlen) {
509  inptr = tmpstart + outlen;
510  }
511  }
512  else {
513  /* Some iconv() have a bug, return *outlen out of range */
514  sprintf(errmsg, "bug?(output length = %ld)", (long)(sizeof(buffer) - outlen));
515  error = rb_eIconvOutOfRange;
516  }
517 
518  if (RTEST(error)) {
519  long len = 0;
520 
521  if (!ret) {
522  ret = rb_str_derive(str, instart, inptr - instart);
523  if (toidx >= 0) rb_enc_associate_index(ret, toidx);
524  }
525  else if (inptr > instart) {
526  rb_str_cat(ret, instart, inptr - instart);
527  }
528  str = rb_str_derive(str, inptr, inlen);
529  rescue = iconv_fail_retry(error, ret, str, env, errmsg);
530  if (TYPE(rescue) == T_ARRAY) {
531  if ((len = RARRAY_LEN(rescue)) > 0)
532  rb_str_concat(ret, RARRAY_PTR(rescue)[0]);
533  if (len > 1 && !NIL_P(str = RARRAY_PTR(rescue)[1])) {
534  StringValue(str);
535  inlen = length = RSTRING_LEN(str);
536  instart = inptr = RSTRING_PTR(str);
537  continue;
538  }
539  }
540  else if (!NIL_P(rescue)) {
541  rb_str_concat(ret, rescue);
542  }
543  break;
544  }
545  } while (inlen > 0);
546 
547  if (!ret) {
548  ret = rb_str_derive(str, instart, inptr - instart);
549  if (toidx >= 0) rb_enc_associate_index(ret, toidx);
550  }
551  else if (inptr > instart) {
552  rb_str_cat(ret, instart, inptr - instart);
553  }
554  return ret;
555 }
556 
557 static VALUE
559 {
560  return Data_Wrap_Struct(klass, 0, ICONV_FREE, 0);
561 }
562 
563 static VALUE
565 {
566  struct rb_iconv_opt_t *opt = (struct rb_iconv_opt_t *)arg;
567  VALUE name, val;
568 
569  (void)opt;
570  i = rb_Array(i);
571  name = rb_ary_entry(i, 0);
572  val = rb_ary_entry(i, 1);
573  do {
574  if (SYMBOL_P(name)) {
575  ID id = SYM2ID(name);
576  if (id == id_transliterate) {
577 #ifdef ICONV_SET_TRANSLITERATE
578  opt->transliterate = val;
579 #else
580  rb_notimplement();
581 #endif
582  break;
583  }
584  if (id == id_discard_ilseq) {
585 #ifdef ICONV_SET_DISCARD_ILSEQ
586  opt->discard_ilseq = val;
587 #else
588  rb_notimplement();
589 #endif
590  break;
591  }
592  }
593  else {
594  const char *s = StringValueCStr(name);
595  if (strcmp(s, "transliterate") == 0) {
596 #ifdef ICONV_SET_TRANSLITERATE
597  opt->transliterate = val;
598 #else
599  rb_notimplement();
600 #endif
601  break;
602  }
603  if (strcmp(s, "discard_ilseq") == 0) {
604 #ifdef ICONV_SET_DISCARD_ILSEQ
605  opt->discard_ilseq = val;
606 #else
607  rb_notimplement();
608 #endif
609  break;
610  }
611  }
612  name = rb_inspect(name);
613  rb_raise(rb_eArgError, "unknown option - %s", StringValueCStr(name));
614  } while (0);
615  return Qnil;
616 }
617 
618 static void
620 {
621  opt->transliterate = Qundef;
622  opt->discard_ilseq = Qundef;
623  if (!NIL_P(options)) {
624  rb_block_call(options, rb_intern("each"), 0, 0, get_iconv_opt_i, (VALUE)opt);
625  }
626 }
627 
628 #define iconv_ctl(self, func, val) (\
629  iconvctl(VALUE2ICONV(check_iconv(self)), func, (void *)&(val)) ? \
630  rb_sys_fail(#func) : (void)0)
631 
632 /*
633  * Document-method: new
634  * call-seq: Iconv.new(to, from, [options])
635  *
636  * Creates new code converter from a coding-system designated with +from+
637  * to another one designated with +to+.
638  *
639  * === Parameters
640  *
641  * +to+:: encoding name for destination
642  * +from+:: encoding name for source
643  * +options+:: options for converter
644  *
645  * === Exceptions
646  *
647  * TypeError:: if +to+ or +from+ aren't String
648  * InvalidEncoding:: if designated converter couldn't find out
649  * SystemCallError:: if <tt>iconv_open(3)</tt> fails
650  */
651 static VALUE
653 {
654  VALUE to, from, options;
655  struct rb_iconv_opt_t opt;
656  int idx;
657 
658  rb_scan_args(argc, argv, "21", &to, &from, &options);
659  get_iconv_opt(&opt, options);
660  iconv_free(check_iconv(self));
661  DATA_PTR(self) = NULL;
662  DATA_PTR(self) = (void *)ICONV2VALUE(iconv_create(to, from, &opt, &idx));
663  if (idx >= 0) ENCODING_SET(self, idx);
664  return self;
665 }
666 
667 /*
668  * Document-method: open
669  * call-seq: Iconv.open(to, from) { |iconv| ... }
670  *
671  * Equivalent to Iconv.new except that when it is called with a block, it
672  * yields with the new instance and closes it, and returns the result which
673  * returned from the block.
674  */
675 static VALUE
677 {
678  VALUE to, from, options, cd;
679  struct rb_iconv_opt_t opt;
680  int idx;
681 
682  rb_scan_args(argc, argv, "21", &to, &from, &options);
683  get_iconv_opt(&opt, options);
684  cd = ICONV2VALUE(iconv_create(to, from, &opt, &idx));
685 
686  self = Data_Wrap_Struct(self, NULL, ICONV_FREE, (void *)cd);
687  if (idx >= 0) ENCODING_SET(self, idx);
688 
689  if (rb_block_given_p()) {
690  return rb_ensure(rb_yield, self, (VALUE(*)())iconv_finish, self);
691  }
692  else {
693  return self;
694  }
695 }
696 
697 static VALUE
699 {
700  VALUE last = 0;
701 
702  for (; env->argc > 0; --env->argc, ++env->argv) {
703  VALUE s = iconv_convert(env->cd, last = *(env->argv),
704  0, -1, env->toidx, env);
705  env->append(env->ret, s);
706  }
707 
708  if (!NIL_P(last)) {
709  VALUE s = iconv_convert(env->cd, Qnil, 0, 0, env->toidx, env);
710  if (RSTRING_LEN(s))
711  env->append(env->ret, s);
712  }
713 
714  return env->ret;
715 }
716 
717 /*
718  * Document-method: Iconv::iconv
719  * call-seq: Iconv.iconv(to, from, *strs)
720  *
721  * Shorthand for
722  * Iconv.open(to, from) { |cd|
723  * (strs + [nil]).collect { |s| cd.iconv(s) }
724  * }
725  *
726  * === Parameters
727  *
728  * <tt>to, from</tt>:: see Iconv.new
729  * <tt>strs</tt>:: strings to be converted
730  *
731  * === Exceptions
732  *
733  * Exceptions thrown by Iconv.new, Iconv.open and Iconv#iconv.
734  */
735 static VALUE
737 {
738  struct iconv_env_t arg;
739 
740  if (argc < 2) /* needs `to' and `from' arguments at least */
741  rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", argc, 2);
742 
743  arg.argc = argc -= 2;
744  arg.argv = argv + 2;
745  arg.append = rb_ary_push;
746  arg.ret = rb_ary_new2(argc);
747  arg.cd = iconv_create(argv[0], argv[1], NULL, &arg.toidx);
748  return rb_ensure(iconv_s_convert, (VALUE)&arg, iconv_free, ICONV2VALUE(arg.cd));
749 }
750 
751 /*
752  * Document-method: Iconv::conv
753  * call-seq: Iconv.conv(to, from, str)
754  *
755  * Shorthand for
756  * Iconv.iconv(to, from, str).join
757  * See Iconv.iconv.
758  */
759 static VALUE
761 {
762  struct iconv_env_t arg;
763 
764  arg.argc = 1;
765  arg.argv = &str;
766  arg.append = rb_str_append;
767  arg.ret = rb_str_new(0, 0);
768  arg.cd = iconv_create(to, from, NULL, &arg.toidx);
769  return rb_ensure(iconv_s_convert, (VALUE)&arg, iconv_free, ICONV2VALUE(arg.cd));
770 }
771 
772 /*
773  * Document-method: list
774  * call-seq: Iconv.list {|*aliases| ... }
775  *
776  * Iterates each alias sets.
777  */
778 
779 #ifdef HAVE_ICONVLIST
780 struct iconv_name_list
781 {
782  unsigned int namescount;
783  const char *const *names;
784  VALUE array;
785 };
786 
787 static VALUE
788 list_iconv_i(VALUE ptr)
789 {
790  struct iconv_name_list *p = (struct iconv_name_list *)ptr;
791  unsigned int i, namescount = p->namescount;
792  const char *const *names = p->names;
793  VALUE ary = rb_ary_new2(namescount);
794 
795  for (i = 0; i < namescount; i++) {
796  rb_ary_push(ary, rb_str_new2(names[i]));
797  }
798  if (p->array) {
799  return rb_ary_push(p->array, ary);
800  }
801  return rb_yield(ary);
802 }
803 
804 static int
805 list_iconv(unsigned int namescount, const char *const *names, void *data)
806 {
807  int *state = data;
808  struct iconv_name_list list;
809 
810  list.namescount = namescount;
811  list.names = names;
812  list.array = ((VALUE *)data)[1];
813  rb_protect(list_iconv_i, (VALUE)&list, state);
814  return *state;
815 }
816 #endif
817 
818 #if defined(HAVE_ICONVLIST) || defined(HAVE___ICONV_FREE_LIST)
819 static VALUE
820 iconv_s_list(void)
821 {
822 #ifdef HAVE_ICONVLIST
823  int state;
824  VALUE args[2];
825 
826  args[1] = rb_block_given_p() ? 0 : rb_ary_new();
827  iconvlist(list_iconv, args);
828  state = *(int *)args;
829  if (state) rb_jump_tag(state);
830  if (args[1]) return args[1];
831 #elif defined(HAVE___ICONV_FREE_LIST)
832  char **list;
833  size_t sz, i;
834  VALUE ary;
835 
836  if (__iconv_get_list(&list, &sz)) return Qnil;
837 
838  ary = rb_ary_new2(sz);
839  for (i = 0; i < sz; i++) {
840  rb_ary_push(ary, rb_str_new2(list[i]));
841  }
842  __iconv_free_list(list, sz);
843 
844  if (!rb_block_given_p())
845  return ary;
846  for (i = 0; i < RARRAY_LEN(ary); i++) {
847  rb_yield(RARRAY_PTR(ary)[i]);
848  }
849 #endif
850  return Qnil;
851 }
852 #else
853 #define iconv_s_list rb_f_notimplement
854 #endif
855 
856 /*
857  * Document-method: close
858  *
859  * Finishes conversion.
860  *
861  * After calling this, calling Iconv#iconv will cause an exception, but
862  * multiple calls of #close are guaranteed to end successfully.
863  *
864  * Returns a string containing the byte sequence to change the output buffer to
865  * its initial shift state.
866  */
867 static VALUE
869 {
870  iconv_t cd = VALUE2ICONV((VALUE)DATA_PTR(self));
871  DATA_PTR(self) = NULL;
872  return iconv_convert(cd, Qnil, 0, 0, ENCODING_GET(self), NULL);
873 }
874 
875 static VALUE
877 {
878  VALUE cd = check_iconv(self);
879 
880  if (!cd) return Qnil;
881  return rb_ensure(iconv_init_state, self, iconv_free, cd);
882 }
883 
884 /*
885  * Document-method: Iconv#iconv
886  * call-seq: iconv(str, start=0, length=-1)
887  *
888  * Converts string and returns the result.
889  * * If +str+ is a String, converts <tt>str[start, length]</tt> and returns the converted string.
890  * * If +str+ is +nil+, places converter itself into initial shift state and
891  * just returns a string containing the byte sequence to change the output
892  * buffer to its initial shift state.
893  * * Otherwise, raises an exception.
894  *
895  * === Parameters
896  *
897  * str:: string to be converted, or nil
898  * start:: starting offset
899  * length:: conversion length; nil or -1 means whole the string from start
900  *
901  * === Exceptions
902  *
903  * * IconvIllegalSequence
904  * * IconvInvalidCharacter
905  * * IconvOutOfRange
906  *
907  * === Examples
908  *
909  * See the Iconv documentation.
910  */
911 static VALUE
913 {
914  VALUE str, n1, n2;
915  VALUE cd = check_iconv(self);
916  long start = 0, length = 0, slen = 0;
917 
918  rb_scan_args(argc, argv, "12", &str, &n1, &n2);
919  if (!NIL_P(str)) {
921  slen = NUM2LONG(n);
922  }
923  if (argc != 2 || !RTEST(rb_range_beg_len(n1, &start, &length, slen, 0))) {
924  if (NIL_P(n1) || ((start = NUM2LONG(n1)) < 0 ? (start += slen) >= 0 : start < slen)) {
925  length = NIL_P(n2) ? -1 : NUM2LONG(n2);
926  }
927  }
928  if (start > 0 || length > 0) {
929  rb_encoding *enc = rb_enc_get(str);
930  const char *s = RSTRING_PTR(str), *e = s + RSTRING_LEN(str);
931  const char *ps = s;
932  if (start > 0) {
933  start = (ps = rb_enc_nth(s, e, start, enc)) - s;
934  }
935  if (length > 0) {
936  length = rb_enc_nth(ps, e, length, enc) - ps;
937  }
938  }
939 
940  return iconv_convert(VALUE2ICONV(cd), str, start, length, ENCODING_GET(self), NULL);
941 }
942 
943 /*
944  * Document-method: conv
945  * call-seq: conv(str...)
946  *
947  * Equivalent to
948  *
949  * iconv(nil, str..., nil).join
950  */
951 static VALUE
953 {
954  iconv_t cd = VALUE2ICONV(check_iconv(self));
955  VALUE str, s;
956  int toidx = ENCODING_GET(self);
957 
958  str = iconv_convert(cd, Qnil, 0, 0, toidx, NULL);
959  if (argc > 0) {
960  do {
961  s = iconv_convert(cd, *argv++, 0, -1, toidx, NULL);
962  if (RSTRING_LEN(s))
963  rb_str_buf_append(str, s);
964  } while (--argc);
965  s = iconv_convert(cd, Qnil, 0, 0, toidx, NULL);
966  if (RSTRING_LEN(s))
967  rb_str_buf_append(str, s);
968  }
969 
970  return str;
971 }
972 
973 #ifdef ICONV_TRIVIALP
974 /*
975  * Document-method: trivial?
976  * call-seq: trivial?
977  *
978  * Returns trivial flag.
979  */
980 static VALUE
981 iconv_trivialp(VALUE self)
982 {
983  int trivial = 0;
984  iconv_ctl(self, ICONV_TRIVIALP, trivial);
985  if (trivial) return Qtrue;
986  return Qfalse;
987 }
988 #else
989 #define iconv_trivialp rb_f_notimplement
990 #endif
991 
992 #ifdef ICONV_GET_TRANSLITERATE
993 /*
994  * Document-method: transliterate?
995  * call-seq: transliterate?
996  *
997  * Returns transliterate flag.
998  */
999 static VALUE
1001 {
1002  int trans = 0;
1003  iconv_ctl(self, ICONV_GET_TRANSLITERATE, trans);
1004  if (trans) return Qtrue;
1005  return Qfalse;
1006 }
1007 #else
1008 #define iconv_get_transliterate rb_f_notimplement
1009 #endif
1010 
1011 #ifdef ICONV_SET_TRANSLITERATE
1012 /*
1013  * Document-method: transliterate=
1014  * call-seq: cd.transliterate = flag
1015  *
1016  * Sets transliterate flag.
1017  */
1018 static VALUE
1019 iconv_set_transliterate(VALUE self, VALUE transliterate)
1020 {
1021  int trans = RTEST(transliterate);
1022  iconv_ctl(self, ICONV_SET_TRANSLITERATE, trans);
1023  return self;
1024 }
1025 #else
1026 #define iconv_set_transliterate rb_f_notimplement
1027 #endif
1028 
1029 #ifdef ICONV_GET_DISCARD_ILSEQ
1030 /*
1031  * Document-method: discard_ilseq?
1032  * call-seq: discard_ilseq?
1033  *
1034  * Returns discard_ilseq flag.
1035  */
1036 static VALUE
1038 {
1039  int dis = 0;
1040  iconv_ctl(self, ICONV_GET_DISCARD_ILSEQ, dis);
1041  if (dis) return Qtrue;
1042  return Qfalse;
1043 }
1044 #else
1045 #define iconv_get_discard_ilseq rb_f_notimplement
1046 #endif
1047 
1048 #ifdef ICONV_SET_DISCARD_ILSEQ
1049 /*
1050  * Document-method: discard_ilseq=
1051  * call-seq: cd.discard_ilseq = flag
1052  *
1053  * Sets discard_ilseq flag.
1054  */
1055 static VALUE
1056 iconv_set_discard_ilseq(VALUE self, VALUE discard_ilseq)
1057 {
1058  int dis = RTEST(discard_ilseq);
1059  iconv_ctl(self, ICONV_SET_DISCARD_ILSEQ, dis);
1060  return self;
1061 }
1062 #else
1063 #define iconv_set_discard_ilseq rb_f_notimplement
1064 #endif
1065 
1066 /*
1067  * Document-method: ctlmethods
1068  * call-seq: Iconv.ctlmethods => array
1069  *
1070  * Returns available iconvctl() method list.
1071  */
1072 static VALUE
1074 {
1075  VALUE ary = rb_ary_new();
1076 #ifdef ICONV_TRIVIALP
1077  rb_ary_push(ary, ID2SYM(rb_intern("trivial?")));
1078 #endif
1079 #ifdef ICONV_GET_TRANSLITERATE
1080  rb_ary_push(ary, ID2SYM(rb_intern("transliterate?")));
1081 #endif
1082 #ifdef ICONV_SET_TRANSLITERATE
1083  rb_ary_push(ary, ID2SYM(rb_intern("transliterate=")));
1084 #endif
1085 #ifdef ICONV_GET_DISCARD_ILSEQ
1086  rb_ary_push(ary, ID2SYM(rb_intern("discard_ilseq?")));
1087 #endif
1088 #ifdef ICONV_SET_DISCARD_ILSEQ
1089  rb_ary_push(ary, ID2SYM(rb_intern("discard_ilseq=")));
1090 #endif
1091  return ary;
1092 }
1093 
1094 /*
1095  * Document-class: Iconv::Failure
1096  *
1097  * Base attributes for Iconv exceptions.
1098  */
1099 
1100 /*
1101  * Document-method: success
1102  * call-seq: success
1103  *
1104  * Returns string(s) translated successfully until the exception occurred.
1105  * * In the case of failure occurred within Iconv.iconv, returned
1106  * value is an array of strings translated successfully preceding
1107  * failure and the last element is string on the way.
1108  */
1109 static VALUE
1111 {
1112  return rb_attr_get(self, rb_success);
1113 }
1114 
1115 /*
1116  * Document-method: failed
1117  * call-seq: failed
1118  *
1119  * Returns substring of the original string passed to Iconv that starts at the
1120  * character caused the exception.
1121  */
1122 static VALUE
1124 {
1125  return rb_attr_get(self, rb_failed);
1126 }
1127 
1128 /*
1129  * Document-method: inspect
1130  * call-seq: inspect
1131  *
1132  * Returns inspected string like as: #<_class_: _success_, _failed_>
1133  */
1134 static VALUE
1136 {
1137  const char *cname = rb_class2name(CLASS_OF(self));
1138  VALUE success = rb_attr_get(self, rb_success);
1139  VALUE failed = rb_attr_get(self, rb_failed);
1140  VALUE str = rb_str_buf_cat2(rb_str_new2("#<"), cname);
1141  str = rb_str_buf_cat(str, ": ", 2);
1142  str = rb_str_buf_append(str, rb_inspect(success));
1143  str = rb_str_buf_cat(str, ", ", 2);
1144  str = rb_str_buf_append(str, rb_inspect(failed));
1145  return rb_str_buf_cat(str, ">", 1);
1146 }
1147 
1148 /*
1149  * Document-class: Iconv::InvalidEncoding
1150  *
1151  * Requested coding-system is not available on this system.
1152  */
1153 
1154 /*
1155  * Document-class: Iconv::IllegalSequence
1156  *
1157  * Input conversion stopped due to an input byte that does not belong to
1158  * the input codeset, or the output codeset does not contain the
1159  * character.
1160  */
1161 
1162 /*
1163  * Document-class: Iconv::InvalidCharacter
1164  *
1165  * Input conversion stopped due to an incomplete character or shift
1166  * sequence at the end of the input buffer.
1167  */
1168 
1169 /*
1170  * Document-class: Iconv::OutOfRange
1171  *
1172  * Iconv library internal error. Must not occur.
1173  */
1174 
1175 /*
1176  * Document-class: Iconv::BrokenLibrary
1177  *
1178  * Detected a bug of underlying iconv(3) libray.
1179  * * returns an error without setting errno properly
1180  */
1181 
1182 static void
1184 {
1185  static const char message[] =
1186  ": iconv will be deprecated in the future, use String#encode instead.\n";
1187  VALUE msg = Qnil, caller = rb_make_backtrace();
1188  long i;
1189 
1190  for (i = 1; i < RARRAY_LEN(caller); ++i) {
1191  VALUE s = RARRAY_PTR(caller)[i];
1192  if (strncmp(RSTRING_PTR(s), "<internal:", 10) != 0) {
1193  msg = s;
1194  break;
1195  }
1196  }
1197  if (NIL_P(msg)) {
1198  msg = rb_str_new_cstr(message + 2);
1199  }
1200  else {
1201  rb_str_cat(msg, message, sizeof(message) - 1);
1202  }
1203  rb_io_puts(1, &msg, rb_stderr);
1204 }
1205 
1206 void
1208 {
1209  VALUE rb_cIconv = rb_define_class("Iconv", rb_cData);
1210 
1211  if (!NIL_P(ruby_verbose)) {
1212  warn_deprecated();
1213  }
1215  rb_define_singleton_method(rb_cIconv, "open", iconv_s_open, -1);
1216  rb_define_singleton_method(rb_cIconv, "iconv", iconv_s_iconv, -1);
1217  rb_define_singleton_method(rb_cIconv, "conv", iconv_s_conv, 3);
1218  rb_define_singleton_method(rb_cIconv, "list", iconv_s_list, 0);
1219  rb_define_singleton_method(rb_cIconv, "ctlmethods", iconv_s_ctlmethods, 0);
1220  rb_define_method(rb_cIconv, "initialize", iconv_initialize, -1);
1221  rb_define_method(rb_cIconv, "close", iconv_finish, 0);
1222  rb_define_method(rb_cIconv, "iconv", iconv_iconv, -1);
1223  rb_define_method(rb_cIconv, "conv", iconv_conv, -1);
1224  rb_define_method(rb_cIconv, "trivial?", iconv_trivialp, 0);
1225  rb_define_method(rb_cIconv, "transliterate?", iconv_get_transliterate, 0);
1226  rb_define_method(rb_cIconv, "transliterate=", iconv_set_transliterate, 1);
1227  rb_define_method(rb_cIconv, "discard_ilseq?", iconv_get_discard_ilseq, 0);
1228  rb_define_method(rb_cIconv, "discard_ilseq=", iconv_set_discard_ilseq, 1);
1229 
1230  rb_eIconvFailure = rb_define_module_under(rb_cIconv, "Failure");
1235 
1236  rb_eIconvInvalidEncoding = rb_define_class_under(rb_cIconv, "InvalidEncoding", rb_eArgError);
1237  rb_eIconvIllegalSeq = rb_define_class_under(rb_cIconv, "IllegalSequence", rb_eArgError);
1238  rb_eIconvInvalidChar = rb_define_class_under(rb_cIconv, "InvalidCharacter", rb_eArgError);
1239  rb_eIconvOutOfRange = rb_define_class_under(rb_cIconv, "OutOfRange", rb_eRuntimeError);
1240  rb_eIconvBrokenLibrary = rb_define_class_under(rb_cIconv, "BrokenLibrary", rb_eRuntimeError);
1246 
1247  rb_success = rb_intern("success");
1248  rb_failed = rb_intern("failed");
1249  id_transliterate = rb_intern("transliterate");
1250  id_discard_ilseq = rb_intern("discard_ilseq");
1251 
1254  rb_define_singleton_method(rb_cIconv, "charset_map", charset_map_get, 0);
1255 }
1256 
static VALUE iconv_conv(int argc, VALUE *argv, VALUE self)
Definition: iconv.c:952
VALUE transliterate
Definition: iconv.c:94
#define RSTRING_LEN(string)
Definition: generator.h:45
static long NUM2LONG(VALUE x)
Definition: ruby.h:510
void rb_gc(void)
Definition: gc.c:3160
RARRAY_PTR(q->result)[0]
#define iconv_ctl(self, func, val)
Definition: iconv.c:628
static ID rb_success
Definition: iconv.c:107
static VALUE rb_eIconvInvalidChar
Definition: iconv.c:103
#define ICONV2VALUE(c)
Definition: iconv.c:80
RUBY_EXTERN VALUE rb_cData
Definition: ruby.h:1253
ssize_t n
Definition: bigdecimal.c:5519
static VALUE iconv_s_ctlmethods(VALUE klass)
Definition: iconv.c:1073
volatile VALUE ary
Definition: tcltklib.c:9700
#define ICONV_FREE
Definition: iconv.c:295
VALUE rb_ary_new4(long n, const VALUE *elts)
Definition: array.c:366
VALUE rb_str_length(VALUE)
Definition: string.c:1137
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:956
VALUE rb_str_equal(VALUE str1, VALUE str2)
Definition: string.c:2284
int idx
Definition: tcltklib.c:9703
code
Definition: tcltklib.c:3375
size_t strlen(const char *)
static VALUE rb_eIconvOutOfRange
Definition: iconv.c:104
static VALUE iconv_fail_retry(VALUE error, VALUE success, VALUE failed, struct iconv_env_t *env, const char *mesg)
Definition: iconv.c:393
static VALUE iconv_free(VALUE cd)
Definition: iconv.c:298
parser parser_yylval val
Definition: ripper.c:14289
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1343
#define CLASS_OF(v)
Definition: ruby.h:376
VALUE rb_str_cat(VALUE, const char *, long)
Definition: string.c:1889
#define rb_block_call(arg1, arg2, arg3, arg4, arg5, arg6)
Definition: ruby_missing.h:38
static VALUE iconv_failure_inspect(VALUE self)
Definition: iconv.c:1135
SYMID SyckParser * p
Definition: yaml2byte.c:119
static nkf_char(* iconv)(nkf_char c2, nkf_char c1, nkf_char c0)
Definition: nkf.c:522
st_table * names
Definition: encoding.c:52
unsigned long VALUE
Definition: ruby.h:88
VALUE enc
Definition: tcltklib.c:10402
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:740
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:2088
VALUE rb_str_plus(VALUE, VALUE)
Definition: string.c:1187
#define iconv_trivialp
Definition: iconv.c:989
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:704
#define RSTRING_PTR(string)
Definition: generator.h:42
static VALUE iconv_s_conv(VALUE self, VALUE to, VALUE from, VALUE str)
Definition: iconv.c:760
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:515
#define Check_Type(v, t)
Definition: ruby.h:459
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1574
return Qtrue
Definition: tcltklib.c:9597
static iconv_t iconv_create(VALUE to, VALUE from, struct rb_iconv_opt_t *opt, int *idx)
Definition: iconv.c:190
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
static VALUE iconv_iconv(int argc, VALUE *argv, VALUE self)
Definition: iconv.c:912
#define RARRAY_LEN(ARRAY)
Definition: generator.h:39
VALUE rb_ary_new3(long n,...)
Definition: array.c:347
return str
Definition: ruby.c:1183
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:663
#define T_ARRAY
Definition: ruby.h:420
VALUE rb_range_beg_len(VALUE, long *, long *, long, int)
Definition: range.c:705
#define st_lookup
Definition: regint.h:149
void rb_gc_register_address(VALUE *addr)
Definition: gc.c:978
static VALUE strip_glibc_option(VALUE *code)
Definition: iconv.c:146
#define rb_sys_fail(s)
Definition: iconv.c:187
unsigned int last
Definition: nkf.c:3915
#define FIXNUM_P(f)
Definition: ruby.h:338
static VALUE iconv_finish(VALUE self)
Definition: iconv.c:876
VALUE VALUE args
Definition: tcltklib.c:2550
#define RDATA(obj)
Definition: ruby.h:913
VALUE rb_str_buf_append(VALUE, VALUE)
Definition: string.c:2031
#define ID2SYM(i)
Definition: cparse.c:63
static VALUE iconv_s_open(int argc, VALUE *argv, VALUE self)
Definition: iconv.c:676
static ID id_transliterate
Definition: iconv.c:98
flag
Definition: tcltklib.c:2039
static VALUE iconv_failure_failed(VALUE self)
Definition: iconv.c:1123
char * rb_enc_nth(const char *, const char *, long, rb_encoding *)
Definition: string.c:1523
VALUE rb_str_buf_cat(VALUE, const char *, long)
Definition: string.c:1873
static const signed char trans[][0x100]
Definition: big5.c:93
#define NORETURN(x)
Definition: ruby.h:31
void rb_exc_raise(VALUE mesg)
Definition: eval.c:460
#define Qnil
Definition: ruby.h:367
VALUE rb_obj_dup(VALUE)
Definition: object.c:315
#define iconv_s_list
Definition: iconv.c:853
VALUE * argv
Definition: iconv.c:86
return rb_str_append(rb_str_new2(cmd_id_head), id_num)
VALUE discard_ilseq
Definition: iconv.c:95
#define RHASH_TBL(h)
Definition: tkutil.c:27
n NULL
Definition: yaml2byte.c:134
return Data_Wrap_Struct(CLASS_OF(interp), 0, ip_free, slave)
void * data
Definition: yaml2byte.c:131
static VALUE rb_eIconvIllegalSeq
Definition: iconv.c:102
VALUE rb_Array(VALUE)
Definition: object.c:2469
VALUE rb_class_new_instance(int, VALUE *, VALUE)
Definition: object.c:1639
return Qfalse
Definition: tcltklib.c:6768
static VALUE iconv_convert(iconv_t cd, VALUE str, long start, long length, int toidx, struct iconv_env_t *env)
Definition: iconv.c:417
int rb_block_given_p(void)
Definition: eval.c:604
VALUE rb_str_substr(VALUE, long, long)
Definition: string.c:1629
VALUE rb_eRuntimeError
Definition: error.c:466
#define SYM2ID(v)
Definition: cparse.c:66
#define FAILED_MAXLEN
Definition: iconv.c:354
#define RSTRING_END(str)
Definition: ruby.h:680
VALUE rb_str_cat2(VALUE, const char *)
Definition: string.c:1908
VALUE rb_ary_new(void)
Definition: array.c:339
VALUE rb_str_buf_cat2(VALUE, const char *)
Definition: string.c:1883
#define NIL_P(v)
Definition: ruby.h:374
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:469
static VALUE VALUE obj
Definition: tcltklib.c:3147
#define VALUE2ICONV(v)
Definition: iconv.c:79
VALUE ret
Definition: iconv.c:87
#define TYPE(x)
Definition: ruby.h:441
static ID id_discard_ilseq
Definition: iconv.c:98
#define iconv_set_transliterate
Definition: iconv.c:1026
VALUE rb_enc_associate_index(VALUE obj, int idx)
Definition: encoding.c:709
static VALUE rb_eIconvFailure
Definition: iconv.c:101
static int VALUE key
Definition: tkutil.c:265
VALUE rb_make_backtrace(void)
Definition: vm_eval.c:1657
int toidx
Definition: iconv.c:88
VALUE rb_str_resize(VALUE, long)
Definition: string.c:1779
#define iconv_set_discard_ilseq
Definition: iconv.c:1063
int argc
Definition: iconv.c:85
static void rb_iconv_sys_fail(const char *s)
Definition: iconv.c:179
VALUE rb_str_subseq(VALUE, long, long)
Definition: string.c:1609
VALUE rb_yield(VALUE)
Definition: vm_eval.c:781
static VALUE get_iconv_opt_i(VALUE i, VALUE arg)
Definition: iconv.c:564
int errno
#define T_DATA
Definition: ruby.h:428
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
Definition: vm_eval.c:669
register char * s
Definition: os2.c:56
VALUE rb_hash_new(void)
Definition: hash.c:229
VP_EXPORT void
Definition: bigdecimal.c:4944
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1416
size_t length
Definition: tcltklib.c:4548
unsigned long ID
Definition: ruby.h:89
static ID rb_failed
Definition: iconv.c:107
static VALUE iconv_try(iconv_t cd, const char **inptr, size_t *inlen, char **outptr, size_t *outlen)
Definition: iconv.c:316
const char * rb_class2name(VALUE)
Definition: variable.c:311
#define RHASH_SIZE(hsh)
Definition: generator.h:28
static VALUE iconv_s_convert(struct iconv_env_t *env)
Definition: iconv.c:698
#define FIX2INT(x)
Definition: ruby.h:538
register unsigned int len
Definition: name2ctype.h:22210
VALUE rb_call_super(int, const VALUE *)
Definition: vm_eval.c:189
void Init_iconv(void)
Definition: iconv.c:1207
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:737
VALUE rb_str_new_cstr(const char *)
Definition: string.c:432
int memcmp(const void *s1, const void *s2, size_t len)
Definition: memcmp.c:7
ruby_verbose
Definition: tcltklib.c:5807
void rb_jump_tag(int tag)
Definition: eval.c:598
return ptr
Definition: tcltklib.c:780
VpDivd * c
Definition: bigdecimal.c:1163
long st_data_t
Definition: syck.h:69
state
Definition: gb18030.c:213
VALUE msg
Definition: tcltklib.c:842
static VALUE iconv_initialize(int argc, VALUE *argv, VALUE self)
Definition: iconv.c:652
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:607
#define StringValueCStr(v)
Definition: ruby.h:468
static VALUE charset_map_get(void)
Definition: iconv.c:140
#define SYMBOL_P(v)
Definition: cparse.c:69
arg
Definition: ripper.y:1287
#define ENCODING_GET(obj)
Definition: encoding.h:47
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:733
static VALUE iconv_s_iconv(int argc, VALUE *argv, VALUE self)
Definition: iconv.c:736
RUBY_EXTERN VALUE rb_stderr
Definition: ruby.h:1320
#define ICONV_INPTR_CAST
static VALUE iconv_fail(VALUE error, VALUE success, VALUE failed, struct iconv_env_t *env, const char *mesg)
Definition: iconv.c:366
void rb_set_errinfo(VALUE err)
Definition: eval.c:1065
DATA_PTR(self)
char * start
Definition: yaml2byte.c:126
iconv_t cd
Definition: iconv.c:84
static VALUE rb_eIconvBrokenLibrary
Definition: iconv.c:105
rb_ivar_set(einfo, ID_at_interp, interp)
#define iconv_get_transliterate
Definition: iconv.c:1008
#define iconv_get_discard_ilseq
Definition: iconv.c:1045
#define RTEST(v)
Definition: ruby.h:373
#define T_STRING
Definition: ruby.h:418
#define rb_str_set_len(str, length)
Definition: ruby_missing.h:30
klass
Definition: tcltklib.c:3493
#define OBJ_INFECT(x, s)
Definition: ruby.h:967
struct rb_encoding_entry * list
Definition: encoding.c:49
static VALUE iconv_failure_initialize(VALUE error, VALUE mesg, VALUE success, VALUE failed)
Definition: iconv.c:357
VALUE rb_io_puts(int, VALUE *, VALUE)
Definition: io.c:6543
static VALUE iconv_s_allocate(VALUE klass)
Definition: iconv.c:558
void rb_notimplement(void)
Definition: error.c:1598
VALUE rb_ary_new2(long capa)
Definition: array.c:332
static VALUE rb_eIconvInvalidEncoding
Definition: iconv.c:100
static VALUE rb_str_derive(VALUE str, const char *ptr, long len)
Definition: iconv.c:402
#define StringValuePtr(v)
Definition: ruby.h:467
static VALUE iconv_failure_success(VALUE self)
Definition: iconv.c:1110
BDIGIT e
Definition: bigdecimal.c:4946
VALUE rb_inspect(VALUE)
Definition: object.c:372
options
Definition: tcltklib.c:4470
static char * map_charset(VALUE *code)
Definition: iconv.c:162
void rb_warning(const char *fmt,...)
Definition: error.c:212
int rb_enc_find_index(const char *name)
Definition: encoding.c:596
static void warn_deprecated(void)
Definition: iconv.c:1183
static VALUE iconv_init_state(VALUE self)
Definition: iconv.c:868
ssize_t i
Definition: bigdecimal.c:5519
static VALUE check_iconv(VALUE obj)
Definition: iconv.c:306
#define rb_intern(str)
#define env
VALUE append _((VALUE, VALUE))
const char * name
Definition: nkf.c:208
#define Qundef
Definition: ruby.h:368
static void iconv_dfree(void *cd)
Definition: iconv.c:290
#define ENCODING_SET(obj, i)
Definition: encoding.h:37
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1210
VALUE rb_str_new2(const char *)
static void get_iconv_opt(struct rb_iconv_opt_t *opt, VALUE options)
Definition: iconv.c:619
VALUE rb_eArgError
Definition: error.c:468
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1032
#define StringValue(v)
Definition: ruby.h:466
static VALUE charset_map
Definition: iconv.c:131
VALUE rb_str_new(const char *, long)
Definition: string.c:410