2007-12-10 08:01:47 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
transcode.c -
|
|
|
|
|
2007-12-10 11:25:01 +03:00
|
|
|
$Author$
|
2007-12-10 08:01:47 +03:00
|
|
|
created at: Tue Oct 30 16:10:22 JST 2007
|
|
|
|
|
|
|
|
Copyright (C) 2007 Martin Duerst
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "ruby/config.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
* internal.h: declare internal functions here.
* node.h: declare NODE dependent internal functions here.
* iseq.h: declare rb_iseq_t dependent internal functions here.
* vm_core.h: declare rb_thread_t dependent internal functions here.
* bignum.c, class.c, compile.c, complex.c, cont.c, dir.c, encoding.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c, io.c,
iseq.c, load.c, marshal.c, math.c, numeric.c, object.c, parse.y,
proc.c, process.c, range.c, rational.c, re.c, ruby.c, string.c,
thread.c, time.c, transcode.c, variable.c, vm.c,
tool/compile_prelude.rb: don't declare internal functions declared
in above headers. include above headers if required.
Note that rb_thread_mark() was declared as
void rb_thread_mark(rb_thread_t *th) in cont.c but defined as
void rb_thread_mark(void *ptr) in vm.c. Now it is declared as
the later in internal.h.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32156 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-06-18 02:43:38 +04:00
|
|
|
#include "internal.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal/inits.h"
|
|
|
|
#include "internal/object.h"
|
|
|
|
#include "internal/string.h"
|
|
|
|
#include "internal/transcode.h"
|
|
|
|
#include "ruby/encoding.h"
|
|
|
|
|
2007-12-10 08:01:47 +03:00
|
|
|
#include "transcode_data.h"
|
|
|
|
|
2011-09-06 07:07:16 +04:00
|
|
|
#define ENABLE_ECONV_NEWLINE_OPTION 1
|
|
|
|
|
2009-01-13 11:18:16 +03:00
|
|
|
/* VALUE rb_cEncoding = rb_define_class("Encoding", rb_cObject); */
|
ruby/ruby.h: remove unnecessary exports from C-API
Needlessly exporting can reduce performance locally and increase
binary size.
Increasing the footprint of our C-API larger is also detrimental
to our development as it encourages tighter coupling with our
internals; making it harder for us to preserve compatibility.
If some parts of the core codebase needs access to globals,
internal.h should be used instead of anything in include/ruby/*.
"Urabe, Shyouhei" <shyouhei@ruby-lang.org> wrote:
> On Thu, Jan 18, 2018 at 7:33 PM, Eric Wong <normalperson@yhbt.net> wrote:
> > shyouhei@ruby-lang.org wrote:
> >> https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=61908
> >>
> >> export rb_mFConst
> >
> > Why are we exporting all these and making the public C-API bigger?
> > If anything, we should make these static. Thanks.
>
> No concrete reason, except they have already been externed in 2.5.
> These variables had lacked declarations so far, which resulted in their
> visibility to be that of extern. The commit is just confirming the status quo.
>
> I'm not against to turn them into static.
This reverts changes from r61910, r61909, r61908, r61907, and r61906.
* transcode.c (rb_eUndefinedConversionError): make static
(rb_eInvalidByteSequenceError): ditto
(rb_eConverterNotFoundError): ditto
* process.c (rb_mProcGID, rb_mProcUid, rb_mProcID_Syscall): ditto
* file.c (rb_mFConst): ditto
* error.c (rb_mWarning, rb_cWarningBuffer): ditto
* enumerator.c (rb_cLazy): ditto
[Misc #14381]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-25 00:07:14 +03:00
|
|
|
static VALUE rb_eUndefinedConversionError;
|
|
|
|
static VALUE rb_eInvalidByteSequenceError;
|
|
|
|
static VALUE rb_eConverterNotFoundError;
|
2008-08-12 11:20:10 +04:00
|
|
|
|
2008-08-12 18:46:18 +04:00
|
|
|
VALUE rb_cEncodingConverter;
|
|
|
|
|
2010-12-07 16:05:26 +03:00
|
|
|
static VALUE sym_invalid, sym_undef, sym_replace, sym_fallback, sym_aref;
|
2008-09-06 18:05:10 +04:00
|
|
|
static VALUE sym_xml, sym_text, sym_attr;
|
2008-09-09 16:22:43 +04:00
|
|
|
static VALUE sym_universal_newline;
|
|
|
|
static VALUE sym_crlf_newline;
|
|
|
|
static VALUE sym_cr_newline;
|
2011-04-26 19:55:58 +04:00
|
|
|
#ifdef ENABLE_ECONV_NEWLINE_OPTION
|
|
|
|
static VALUE sym_newline, sym_universal, sym_crlf, sym_cr, sym_lf;
|
|
|
|
#endif
|
2008-09-04 16:48:21 +04:00
|
|
|
static VALUE sym_partial_input;
|
2008-02-21 11:42:10 +03:00
|
|
|
|
2008-08-28 20:59:17 +04:00
|
|
|
static VALUE sym_invalid_byte_sequence;
|
|
|
|
static VALUE sym_undefined_conversion;
|
|
|
|
static VALUE sym_destination_buffer_full;
|
|
|
|
static VALUE sym_source_buffer_empty;
|
|
|
|
static VALUE sym_finished;
|
2008-09-09 20:27:02 +04:00
|
|
|
static VALUE sym_after_output;
|
2008-08-28 20:59:17 +04:00
|
|
|
static VALUE sym_incomplete_input;
|
|
|
|
|
2008-09-06 00:24:18 +04:00
|
|
|
static unsigned char *
|
|
|
|
allocate_converted_string(const char *sname, const char *dname,
|
|
|
|
const unsigned char *str, size_t len,
|
|
|
|
unsigned char *caller_dst_buf, size_t caller_dst_bufsize,
|
|
|
|
size_t *dst_len_ptr);
|
|
|
|
|
2008-09-03 18:34:09 +04:00
|
|
|
/* dynamic structure, one per conversion (similar to iconv_t) */
|
|
|
|
/* may carry conversion state (e.g. for iso-2022-jp) */
|
|
|
|
typedef struct rb_transcoding {
|
|
|
|
const rb_transcoder *transcoder;
|
|
|
|
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
int resume_position;
|
|
|
|
unsigned int next_table;
|
|
|
|
VALUE next_info;
|
|
|
|
unsigned char next_byte;
|
2008-09-15 19:42:30 +04:00
|
|
|
unsigned int output_index;
|
2008-09-03 18:34:09 +04:00
|
|
|
|
2009-10-10 18:51:25 +04:00
|
|
|
ssize_t recognized_len; /* already interpreted */
|
|
|
|
ssize_t readagain_len; /* not yet interpreted */
|
2008-09-03 18:34:09 +04:00
|
|
|
union {
|
|
|
|
unsigned char ary[8]; /* max_input <= sizeof(ary) */
|
|
|
|
unsigned char *ptr; /* length: max_input */
|
|
|
|
} readbuf; /* recognized_len + readagain_len used */
|
|
|
|
|
2009-10-10 18:51:25 +04:00
|
|
|
ssize_t writebuf_off;
|
|
|
|
ssize_t writebuf_len;
|
2008-09-03 18:34:09 +04:00
|
|
|
union {
|
|
|
|
unsigned char ary[8]; /* max_output <= sizeof(ary) */
|
|
|
|
unsigned char *ptr; /* length: max_output */
|
|
|
|
} writebuf;
|
|
|
|
|
2008-09-13 13:52:02 +04:00
|
|
|
union rb_transcoding_state_t { /* opaque data for stateful encoding */
|
|
|
|
void *ptr;
|
2009-01-27 04:15:37 +03:00
|
|
|
char ary[sizeof(double) > sizeof(void*) ? sizeof(double) : sizeof(void*)];
|
2008-09-13 13:52:02 +04:00
|
|
|
double dummy_for_alignment;
|
|
|
|
} state;
|
2008-09-03 18:34:09 +04:00
|
|
|
} rb_transcoding;
|
|
|
|
#define TRANSCODING_READBUF(tc) \
|
2009-10-10 18:51:25 +04:00
|
|
|
((tc)->transcoder->max_input <= (int)sizeof((tc)->readbuf.ary) ? \
|
2008-09-03 18:34:09 +04:00
|
|
|
(tc)->readbuf.ary : \
|
|
|
|
(tc)->readbuf.ptr)
|
|
|
|
#define TRANSCODING_WRITEBUF(tc) \
|
2009-10-10 18:51:25 +04:00
|
|
|
((tc)->transcoder->max_output <= (int)sizeof((tc)->writebuf.ary) ? \
|
2008-09-03 18:34:09 +04:00
|
|
|
(tc)->writebuf.ary : \
|
|
|
|
(tc)->writebuf.ptr)
|
2008-09-14 22:06:20 +04:00
|
|
|
#define TRANSCODING_WRITEBUF_SIZE(tc) \
|
2009-10-10 18:51:25 +04:00
|
|
|
((tc)->transcoder->max_output <= (int)sizeof((tc)->writebuf.ary) ? \
|
2008-09-14 22:06:20 +04:00
|
|
|
sizeof((tc)->writebuf.ary) : \
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
(size_t)(tc)->transcoder->max_output)
|
2009-10-10 18:51:25 +04:00
|
|
|
#define TRANSCODING_STATE_EMBED_MAX ((int)sizeof(union rb_transcoding_state_t))
|
2008-09-03 18:34:09 +04:00
|
|
|
#define TRANSCODING_STATE(tc) \
|
2009-10-10 18:51:25 +04:00
|
|
|
((tc)->transcoder->state_size <= (int)sizeof((tc)->state) ? \
|
2009-01-27 04:15:37 +03:00
|
|
|
(tc)->state.ary : \
|
2008-09-13 13:52:02 +04:00
|
|
|
(tc)->state.ptr)
|
2008-09-03 18:34:09 +04:00
|
|
|
|
2008-08-26 19:01:11 +04:00
|
|
|
typedef struct {
|
|
|
|
struct rb_transcoding *tc;
|
|
|
|
unsigned char *out_buf_start;
|
|
|
|
unsigned char *out_data_start;
|
|
|
|
unsigned char *out_data_end;
|
|
|
|
unsigned char *out_buf_end;
|
|
|
|
rb_econv_result_t last_result;
|
|
|
|
} rb_econv_elem_t;
|
|
|
|
|
|
|
|
struct rb_econv_t {
|
2008-09-03 19:11:46 +04:00
|
|
|
int flags;
|
2014-07-20 08:51:35 +04:00
|
|
|
int started; /* bool */
|
|
|
|
|
2008-08-26 19:01:11 +04:00
|
|
|
const char *source_encoding_name;
|
|
|
|
const char *destination_encoding_name;
|
|
|
|
|
2008-09-03 20:34:11 +04:00
|
|
|
const unsigned char *replacement_str;
|
|
|
|
size_t replacement_len;
|
|
|
|
const char *replacement_enc;
|
|
|
|
|
2008-08-26 19:01:11 +04:00
|
|
|
unsigned char *in_buf_start;
|
|
|
|
unsigned char *in_data_start;
|
|
|
|
unsigned char *in_data_end;
|
|
|
|
unsigned char *in_buf_end;
|
|
|
|
rb_econv_elem_t *elems;
|
2014-07-20 08:51:35 +04:00
|
|
|
int replacement_allocated; /* bool */
|
2008-09-12 16:58:23 +04:00
|
|
|
int num_allocated;
|
2008-08-26 19:01:11 +04:00
|
|
|
int num_trans;
|
|
|
|
int num_finished;
|
|
|
|
struct rb_transcoding *last_tc;
|
|
|
|
|
|
|
|
/* last error */
|
|
|
|
struct {
|
|
|
|
rb_econv_result_t result;
|
|
|
|
struct rb_transcoding *error_tc;
|
|
|
|
const char *source_encoding;
|
|
|
|
const char *destination_encoding;
|
|
|
|
const unsigned char *error_bytes_start;
|
|
|
|
size_t error_bytes_len;
|
|
|
|
size_t readagain_len;
|
|
|
|
} last_error;
|
|
|
|
|
|
|
|
/* The following fields are only for Encoding::Converter.
|
|
|
|
* rb_econv_open set them NULL. */
|
|
|
|
rb_encoding *source_encoding;
|
|
|
|
rb_encoding *destination_encoding;
|
|
|
|
};
|
|
|
|
|
2007-12-10 08:01:47 +03:00
|
|
|
/*
|
|
|
|
* Dispatch data and logic
|
|
|
|
*/
|
|
|
|
|
2008-09-13 13:21:34 +04:00
|
|
|
#define DECORATOR_P(sname, dname) (*(sname) == '\0')
|
2008-09-06 07:26:35 +04:00
|
|
|
|
2008-08-07 18:53:30 +04:00
|
|
|
typedef struct {
|
2008-09-04 19:35:30 +04:00
|
|
|
const char *sname;
|
|
|
|
const char *dname;
|
2015-11-11 08:14:36 +03:00
|
|
|
const char *lib; /* null means no need to load a library */
|
2008-08-07 18:53:30 +04:00
|
|
|
const rb_transcoder *transcoder;
|
|
|
|
} transcoder_entry_t;
|
2007-12-25 08:57:04 +03:00
|
|
|
|
2008-08-07 18:53:30 +04:00
|
|
|
static st_table *transcoder_table;
|
2007-12-25 08:57:04 +03:00
|
|
|
|
2008-08-07 18:53:30 +04:00
|
|
|
static transcoder_entry_t *
|
2008-09-04 19:35:30 +04:00
|
|
|
make_transcoder_entry(const char *sname, const char *dname)
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
2008-08-07 18:53:30 +04:00
|
|
|
st_data_t val;
|
|
|
|
st_table *table2;
|
|
|
|
|
2008-09-04 19:35:30 +04:00
|
|
|
if (!st_lookup(transcoder_table, (st_data_t)sname, &val)) {
|
2008-08-07 18:53:30 +04:00
|
|
|
val = (st_data_t)st_init_strcasetable();
|
2008-09-04 19:35:30 +04:00
|
|
|
st_add_direct(transcoder_table, (st_data_t)sname, val);
|
2008-08-07 18:53:30 +04:00
|
|
|
}
|
|
|
|
table2 = (st_table *)val;
|
2008-09-04 19:35:30 +04:00
|
|
|
if (!st_lookup(table2, (st_data_t)dname, &val)) {
|
2008-08-07 18:53:30 +04:00
|
|
|
transcoder_entry_t *entry = ALLOC(transcoder_entry_t);
|
2008-09-04 19:35:30 +04:00
|
|
|
entry->sname = sname;
|
|
|
|
entry->dname = dname;
|
2008-08-07 18:53:30 +04:00
|
|
|
entry->lib = NULL;
|
|
|
|
entry->transcoder = NULL;
|
|
|
|
val = (st_data_t)entry;
|
2008-09-04 19:35:30 +04:00
|
|
|
st_add_direct(table2, (st_data_t)dname, val);
|
2008-08-07 18:53:30 +04:00
|
|
|
}
|
|
|
|
return (transcoder_entry_t *)val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static transcoder_entry_t *
|
2008-09-04 19:35:30 +04:00
|
|
|
get_transcoder_entry(const char *sname, const char *dname)
|
2008-08-07 18:53:30 +04:00
|
|
|
{
|
|
|
|
st_data_t val;
|
|
|
|
st_table *table2;
|
|
|
|
|
2008-09-04 19:35:30 +04:00
|
|
|
if (!st_lookup(transcoder_table, (st_data_t)sname, &val)) {
|
2008-08-07 18:53:30 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
table2 = (st_table *)val;
|
2008-09-04 19:35:30 +04:00
|
|
|
if (!st_lookup(table2, (st_data_t)dname, &val)) {
|
2008-08-07 18:53:30 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (transcoder_entry_t *)val;
|
2007-12-25 08:57:04 +03:00
|
|
|
}
|
2007-12-10 08:01:47 +03:00
|
|
|
|
2007-12-25 08:57:04 +03:00
|
|
|
void
|
|
|
|
rb_register_transcoder(const rb_transcoder *tr)
|
|
|
|
{
|
2008-09-04 19:46:18 +04:00
|
|
|
const char *const sname = tr->src_encoding;
|
|
|
|
const char *const dname = tr->dst_encoding;
|
2007-12-25 08:57:04 +03:00
|
|
|
|
2008-08-07 18:53:30 +04:00
|
|
|
transcoder_entry_t *entry;
|
|
|
|
|
2008-09-04 19:35:30 +04:00
|
|
|
entry = make_transcoder_entry(sname, dname);
|
2008-08-07 18:53:30 +04:00
|
|
|
if (entry->transcoder) {
|
2007-12-25 08:57:04 +03:00
|
|
|
rb_raise(rb_eArgError, "transcoder from %s to %s has been already registered",
|
2008-09-04 19:35:30 +04:00
|
|
|
sname, dname);
|
2007-12-25 08:57:04 +03:00
|
|
|
}
|
2008-08-07 18:53:30 +04:00
|
|
|
|
|
|
|
entry->transcoder = tr;
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
2007-12-24 16:51:19 +03:00
|
|
|
static void
|
2008-09-04 19:35:30 +04:00
|
|
|
declare_transcoder(const char *sname, const char *dname, const char *lib)
|
2007-12-24 16:51:19 +03:00
|
|
|
{
|
2008-08-07 18:53:30 +04:00
|
|
|
transcoder_entry_t *entry;
|
2007-12-25 08:57:04 +03:00
|
|
|
|
2008-09-04 19:35:30 +04:00
|
|
|
entry = make_transcoder_entry(sname, dname);
|
2008-08-07 18:53:30 +04:00
|
|
|
entry->lib = lib;
|
2007-12-24 16:51:19 +03:00
|
|
|
}
|
|
|
|
|
2007-12-25 08:57:04 +03:00
|
|
|
static const char transcoder_lib_prefix[] = "enc/trans/";
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_declare_transcoder(const char *enc1, const char *enc2, const char *lib)
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
2011-09-08 02:44:27 +04:00
|
|
|
if (!lib) {
|
|
|
|
rb_raise(rb_eArgError, "invalid library name - (null)");
|
2007-12-25 08:57:04 +03:00
|
|
|
}
|
|
|
|
declare_transcoder(enc1, enc2, lib);
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
2011-01-09 14:38:55 +03:00
|
|
|
#define encoding_equal(enc1, enc2) (STRCASECMP((enc1), (enc2)) == 0)
|
2007-12-25 08:57:04 +03:00
|
|
|
|
2008-08-07 18:53:30 +04:00
|
|
|
typedef struct search_path_queue_tag {
|
|
|
|
struct search_path_queue_tag *next;
|
|
|
|
const char *enc;
|
|
|
|
} search_path_queue_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
st_table *visited;
|
|
|
|
search_path_queue_t *queue;
|
|
|
|
search_path_queue_t **queue_last_ptr;
|
|
|
|
const char *base_enc;
|
|
|
|
} search_path_bfs_t;
|
|
|
|
|
|
|
|
static int
|
|
|
|
transcode_search_path_i(st_data_t key, st_data_t val, st_data_t arg)
|
|
|
|
{
|
2008-09-04 19:35:30 +04:00
|
|
|
const char *dname = (const char *)key;
|
2008-08-07 18:53:30 +04:00
|
|
|
search_path_bfs_t *bfs = (search_path_bfs_t *)arg;
|
|
|
|
search_path_queue_t *q;
|
|
|
|
|
2008-09-04 19:35:30 +04:00
|
|
|
if (st_lookup(bfs->visited, (st_data_t)dname, &val)) {
|
2008-08-07 18:53:30 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
q = ALLOC(search_path_queue_t);
|
2008-09-04 19:35:30 +04:00
|
|
|
q->enc = dname;
|
2008-08-07 18:53:30 +04:00
|
|
|
q->next = NULL;
|
|
|
|
*bfs->queue_last_ptr = q;
|
|
|
|
bfs->queue_last_ptr = &q->next;
|
|
|
|
|
2008-09-04 19:35:30 +04:00
|
|
|
st_add_direct(bfs->visited, (st_data_t)dname, (st_data_t)bfs->base_enc);
|
2008-08-07 18:53:30 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-09-04 19:35:30 +04:00
|
|
|
transcode_search_path(const char *sname, const char *dname,
|
|
|
|
void (*callback)(const char *sname, const char *dname, int depth, void *arg),
|
2008-08-07 18:53:30 +04:00
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
search_path_bfs_t bfs;
|
|
|
|
search_path_queue_t *q;
|
|
|
|
st_data_t val;
|
|
|
|
st_table *table2;
|
|
|
|
int found;
|
2008-10-18 11:41:09 +04:00
|
|
|
int pathlen = -1;
|
2008-08-07 18:53:30 +04:00
|
|
|
|
2008-09-04 19:35:30 +04:00
|
|
|
if (encoding_equal(sname, dname))
|
2008-08-18 08:22:31 +04:00
|
|
|
return -1;
|
|
|
|
|
2008-08-07 18:53:30 +04:00
|
|
|
q = ALLOC(search_path_queue_t);
|
2008-09-04 19:35:30 +04:00
|
|
|
q->enc = sname;
|
2008-08-07 18:53:30 +04:00
|
|
|
q->next = NULL;
|
|
|
|
bfs.queue_last_ptr = &q->next;
|
|
|
|
bfs.queue = q;
|
|
|
|
|
|
|
|
bfs.visited = st_init_strcasetable();
|
2008-09-04 19:35:30 +04:00
|
|
|
st_add_direct(bfs.visited, (st_data_t)sname, (st_data_t)NULL);
|
2008-08-07 18:53:30 +04:00
|
|
|
|
|
|
|
while (bfs.queue) {
|
|
|
|
q = bfs.queue;
|
|
|
|
bfs.queue = q->next;
|
|
|
|
if (!bfs.queue)
|
|
|
|
bfs.queue_last_ptr = &bfs.queue;
|
|
|
|
|
|
|
|
if (!st_lookup(transcoder_table, (st_data_t)q->enc, &val)) {
|
|
|
|
xfree(q);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
table2 = (st_table *)val;
|
|
|
|
|
2008-09-04 19:35:30 +04:00
|
|
|
if (st_lookup(table2, (st_data_t)dname, &val)) {
|
|
|
|
st_add_direct(bfs.visited, (st_data_t)dname, (st_data_t)q->enc);
|
2008-08-07 18:53:30 +04:00
|
|
|
xfree(q);
|
|
|
|
found = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
bfs.base_enc = q->enc;
|
|
|
|
st_foreach(table2, transcode_search_path_i, (st_data_t)&bfs);
|
|
|
|
bfs.base_enc = NULL;
|
|
|
|
|
|
|
|
xfree(q);
|
|
|
|
}
|
|
|
|
found = 0;
|
|
|
|
|
2008-08-14 09:56:51 +04:00
|
|
|
cleanup:
|
2008-08-07 18:53:30 +04:00
|
|
|
while (bfs.queue) {
|
|
|
|
q = bfs.queue;
|
|
|
|
bfs.queue = q->next;
|
|
|
|
xfree(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
2008-09-04 19:35:30 +04:00
|
|
|
const char *enc = dname;
|
2008-08-13 06:40:24 +04:00
|
|
|
int depth;
|
2008-08-14 09:56:51 +04:00
|
|
|
pathlen = 0;
|
2008-08-07 18:53:30 +04:00
|
|
|
while (1) {
|
|
|
|
st_lookup(bfs.visited, (st_data_t)enc, &val);
|
|
|
|
if (!val)
|
|
|
|
break;
|
2008-08-13 06:40:24 +04:00
|
|
|
pathlen++;
|
2008-08-07 18:53:30 +04:00
|
|
|
enc = (const char *)val;
|
|
|
|
}
|
2008-08-13 06:40:24 +04:00
|
|
|
depth = pathlen;
|
2008-09-04 19:35:30 +04:00
|
|
|
enc = dname;
|
2008-08-07 18:53:30 +04:00
|
|
|
while (1) {
|
|
|
|
st_lookup(bfs.visited, (st_data_t)enc, &val);
|
|
|
|
if (!val)
|
|
|
|
break;
|
|
|
|
callback((const char *)val, enc, --depth, arg);
|
|
|
|
enc = (const char *)val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
st_free_table(bfs.visited);
|
|
|
|
|
2008-12-30 12:03:04 +03:00
|
|
|
return pathlen; /* is -1 if not found */
|
2008-08-07 18:53:30 +04:00
|
|
|
}
|
|
|
|
|
2008-08-10 16:26:01 +04:00
|
|
|
static const rb_transcoder *
|
2008-08-13 06:40:24 +04:00
|
|
|
load_transcoder_entry(transcoder_entry_t *entry)
|
2008-08-10 16:26:01 +04:00
|
|
|
{
|
|
|
|
if (entry->transcoder)
|
|
|
|
return entry->transcoder;
|
|
|
|
|
|
|
|
if (entry->lib) {
|
2011-09-08 02:44:27 +04:00
|
|
|
const char *const lib = entry->lib;
|
|
|
|
const size_t len = strlen(lib);
|
|
|
|
const size_t total_len = sizeof(transcoder_lib_prefix) - 1 + len;
|
|
|
|
const VALUE fn = rb_str_new(0, total_len);
|
|
|
|
char *const path = RSTRING_PTR(fn);
|
2008-08-10 16:26:01 +04:00
|
|
|
|
|
|
|
memcpy(path, transcoder_lib_prefix, sizeof(transcoder_lib_prefix) - 1);
|
2011-09-06 10:55:30 +04:00
|
|
|
memcpy(path + sizeof(transcoder_lib_prefix) - 1, lib, len);
|
|
|
|
rb_str_set_len(fn, total_len);
|
2011-09-06 08:15:49 +04:00
|
|
|
OBJ_FREEZE(fn);
|
2019-09-21 05:06:22 +03:00
|
|
|
rb_require_string(fn);
|
2008-08-10 16:26:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->transcoder)
|
|
|
|
return entry->transcoder;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-08-12 02:21:15 +04:00
|
|
|
static const char*
|
2008-09-09 20:18:29 +04:00
|
|
|
get_replacement_character(const char *encname, size_t *len_ret, const char **repl_encname_ptr)
|
2008-07-14 10:27:26 +04:00
|
|
|
{
|
2008-09-09 20:18:29 +04:00
|
|
|
if (encoding_equal(encname, "UTF-8")) {
|
2008-08-12 02:21:15 +04:00
|
|
|
*len_ret = 3;
|
2008-09-09 20:18:29 +04:00
|
|
|
*repl_encname_ptr = "UTF-8";
|
2008-08-12 02:21:15 +04:00
|
|
|
return "\xEF\xBF\xBD";
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-08-12 02:21:15 +04:00
|
|
|
*len_ret = 1;
|
2008-09-09 20:18:29 +04:00
|
|
|
*repl_encname_ptr = "US-ASCII";
|
2008-08-12 02:21:15 +04:00
|
|
|
return "?";
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
2008-08-12 02:21:15 +04:00
|
|
|
}
|
|
|
|
|
2007-12-10 08:01:47 +03:00
|
|
|
/*
|
|
|
|
* Transcoding engine logic
|
|
|
|
*/
|
2008-08-09 10:02:01 +04:00
|
|
|
|
|
|
|
static const unsigned char *
|
2008-08-10 06:33:05 +04:00
|
|
|
transcode_char_start(rb_transcoding *tc,
|
2008-08-10 04:23:36 +04:00
|
|
|
const unsigned char *in_start,
|
|
|
|
const unsigned char *inchar_start,
|
2008-08-10 03:33:37 +04:00
|
|
|
const unsigned char *in_p,
|
2008-08-10 04:23:36 +04:00
|
|
|
size_t *char_len_ptr)
|
2008-08-09 10:02:01 +04:00
|
|
|
{
|
|
|
|
const unsigned char *ptr;
|
2008-08-11 05:06:21 +04:00
|
|
|
if (inchar_start - in_start < tc->recognized_len) {
|
|
|
|
MEMCPY(TRANSCODING_READBUF(tc) + tc->recognized_len,
|
2008-08-10 04:23:36 +04:00
|
|
|
inchar_start, unsigned char, in_p - inchar_start);
|
2008-08-10 06:33:05 +04:00
|
|
|
ptr = TRANSCODING_READBUF(tc);
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-08-11 05:06:21 +04:00
|
|
|
ptr = inchar_start - tc->recognized_len;
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
2008-08-11 05:06:21 +04:00
|
|
|
*char_len_ptr = tc->recognized_len + (in_p - inchar_start);
|
2008-08-09 10:02:01 +04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2008-08-14 15:31:27 +04:00
|
|
|
static rb_econv_result_t
|
2008-08-10 06:17:56 +04:00
|
|
|
transcode_restartable0(const unsigned char **in_pos, unsigned char **out_pos,
|
2008-08-10 03:33:37 +04:00
|
|
|
const unsigned char *in_stop, unsigned char *out_stop,
|
2008-08-10 06:33:05 +04:00
|
|
|
rb_transcoding *tc,
|
2008-08-09 10:02:01 +04:00
|
|
|
const int opt)
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
2008-08-10 06:33:05 +04:00
|
|
|
const rb_transcoder *tr = tc->transcoder;
|
|
|
|
int unitlen = tr->input_unit_length;
|
2009-10-10 18:51:25 +04:00
|
|
|
ssize_t readagain_len = 0;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
2008-08-10 04:23:36 +04:00
|
|
|
const unsigned char *inchar_start;
|
2008-08-10 03:33:37 +04:00
|
|
|
const unsigned char *in_p;
|
2008-08-10 04:23:36 +04:00
|
|
|
|
2008-08-09 10:02:01 +04:00
|
|
|
unsigned char *out_p;
|
2008-08-09 14:32:02 +04:00
|
|
|
|
2008-08-10 04:23:36 +04:00
|
|
|
in_p = inchar_start = *in_pos;
|
|
|
|
|
2008-08-10 03:33:37 +04:00
|
|
|
out_p = *out_pos;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
|
|
|
#define SUSPEND(ret, num) \
|
|
|
|
do { \
|
2008-08-10 06:33:05 +04:00
|
|
|
tc->resume_position = (num); \
|
2008-08-10 04:23:36 +04:00
|
|
|
if (0 < in_p - inchar_start) \
|
2008-08-11 05:06:21 +04:00
|
|
|
MEMMOVE(TRANSCODING_READBUF(tc)+tc->recognized_len, \
|
2008-08-10 04:23:36 +04:00
|
|
|
inchar_start, unsigned char, in_p - inchar_start); \
|
2008-08-10 03:33:37 +04:00
|
|
|
*in_pos = in_p; \
|
2008-08-09 10:02:01 +04:00
|
|
|
*out_pos = out_p; \
|
2008-08-11 05:06:21 +04:00
|
|
|
tc->recognized_len += in_p - inchar_start; \
|
|
|
|
if (readagain_len) { \
|
|
|
|
tc->recognized_len -= readagain_len; \
|
|
|
|
tc->readagain_len = readagain_len; \
|
2008-08-10 06:17:56 +04:00
|
|
|
} \
|
2011-01-09 14:38:55 +03:00
|
|
|
return (ret); \
|
2008-08-09 10:02:01 +04:00
|
|
|
resume_label ## num:; \
|
|
|
|
} while (0)
|
2008-08-13 12:28:39 +04:00
|
|
|
#define SUSPEND_OBUF(num) \
|
|
|
|
do { \
|
2008-08-14 17:45:54 +04:00
|
|
|
while (out_stop - out_p < 1) { SUSPEND(econv_destination_buffer_full, num); } \
|
2008-08-13 12:28:39 +04:00
|
|
|
} while (0)
|
|
|
|
|
2008-09-09 20:27:02 +04:00
|
|
|
#define SUSPEND_AFTER_OUTPUT(num) \
|
|
|
|
if ((opt & ECONV_AFTER_OUTPUT) && *out_pos != out_p) { \
|
|
|
|
SUSPEND(econv_after_output, num); \
|
2008-08-14 10:12:27 +04:00
|
|
|
}
|
|
|
|
|
2008-08-13 12:36:47 +04:00
|
|
|
#define next_table (tc->next_table)
|
|
|
|
#define next_info (tc->next_info)
|
|
|
|
#define next_byte (tc->next_byte)
|
2008-08-13 12:28:39 +04:00
|
|
|
#define writebuf_len (tc->writebuf_len)
|
|
|
|
#define writebuf_off (tc->writebuf_off)
|
2008-08-09 10:02:01 +04:00
|
|
|
|
2008-08-10 06:33:05 +04:00
|
|
|
switch (tc->resume_position) {
|
2008-08-09 10:02:01 +04:00
|
|
|
case 0: break;
|
|
|
|
case 1: goto resume_label1;
|
|
|
|
case 2: goto resume_label2;
|
|
|
|
case 3: goto resume_label3;
|
|
|
|
case 4: goto resume_label4;
|
|
|
|
case 5: goto resume_label5;
|
|
|
|
case 6: goto resume_label6;
|
|
|
|
case 7: goto resume_label7;
|
|
|
|
case 8: goto resume_label8;
|
|
|
|
case 9: goto resume_label9;
|
|
|
|
case 10: goto resume_label10;
|
|
|
|
case 11: goto resume_label11;
|
|
|
|
case 12: goto resume_label12;
|
|
|
|
case 13: goto resume_label13;
|
|
|
|
case 14: goto resume_label14;
|
2008-08-13 12:28:39 +04:00
|
|
|
case 15: goto resume_label15;
|
|
|
|
case 16: goto resume_label16;
|
|
|
|
case 17: goto resume_label17;
|
|
|
|
case 18: goto resume_label18;
|
|
|
|
case 19: goto resume_label19;
|
|
|
|
case 20: goto resume_label20;
|
|
|
|
case 21: goto resume_label21;
|
|
|
|
case 22: goto resume_label22;
|
|
|
|
case 23: goto resume_label23;
|
2008-08-14 10:12:27 +04:00
|
|
|
case 24: goto resume_label24;
|
|
|
|
case 25: goto resume_label25;
|
|
|
|
case 26: goto resume_label26;
|
2008-08-26 20:09:29 +04:00
|
|
|
case 27: goto resume_label27;
|
2008-09-07 13:13:16 +04:00
|
|
|
case 28: goto resume_label28;
|
2009-01-14 14:12:30 +03:00
|
|
|
case 29: goto resume_label29;
|
|
|
|
case 30: goto resume_label30;
|
|
|
|
case 31: goto resume_label31;
|
|
|
|
case 32: goto resume_label32;
|
2009-12-10 14:59:12 +03:00
|
|
|
case 33: goto resume_label33;
|
|
|
|
case 34: goto resume_label34;
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2008-08-16 09:32:42 +04:00
|
|
|
inchar_start = in_p;
|
|
|
|
tc->recognized_len = 0;
|
2008-09-02 17:26:02 +04:00
|
|
|
next_table = tr->conv_tree_start;
|
2008-08-16 09:32:42 +04:00
|
|
|
|
2008-09-09 20:27:02 +04:00
|
|
|
SUSPEND_AFTER_OUTPUT(24);
|
2008-08-16 09:32:42 +04:00
|
|
|
|
2008-08-10 03:33:37 +04:00
|
|
|
if (in_stop <= in_p) {
|
2008-08-14 18:48:41 +04:00
|
|
|
if (!(opt & ECONV_PARTIAL_INPUT))
|
2008-08-09 10:02:01 +04:00
|
|
|
break;
|
2008-08-14 17:45:54 +04:00
|
|
|
SUSPEND(econv_source_buffer_empty, 7);
|
2008-08-09 10:02:01 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-09-03 15:59:50 +04:00
|
|
|
#define BYTE_ADDR(index) (tr->byte_array + (index))
|
2008-09-03 16:35:29 +04:00
|
|
|
#define WORD_ADDR(index) (tr->word_array + INFO2WORDINDEX(index))
|
2008-09-03 20:02:10 +04:00
|
|
|
#define BL_BASE BYTE_ADDR(BYTE_LOOKUP_BASE(WORD_ADDR(next_table)))
|
|
|
|
#define BL_INFO WORD_ADDR(BYTE_LOOKUP_INFO(WORD_ADDR(next_table)))
|
|
|
|
#define BL_MIN_BYTE (BL_BASE[0])
|
|
|
|
#define BL_MAX_BYTE (BL_BASE[1])
|
|
|
|
#define BL_OFFSET(byte) (BL_BASE[2+(byte)-BL_MIN_BYTE])
|
|
|
|
#define BL_ACTION(byte) (BL_INFO[BL_OFFSET((byte))])
|
2008-09-03 15:59:50 +04:00
|
|
|
|
2008-08-10 03:33:37 +04:00
|
|
|
next_byte = (unsigned char)*in_p++;
|
2007-12-10 08:01:47 +03:00
|
|
|
follow_byte:
|
2008-09-03 20:02:10 +04:00
|
|
|
if (next_byte < BL_MIN_BYTE || BL_MAX_BYTE < next_byte)
|
2008-08-08 19:48:17 +04:00
|
|
|
next_info = INVALID;
|
|
|
|
else {
|
2008-09-03 20:02:10 +04:00
|
|
|
next_info = (VALUE)BL_ACTION(next_byte);
|
2008-08-08 19:48:17 +04:00
|
|
|
}
|
2007-12-28 12:26:55 +03:00
|
|
|
follow_info:
|
2007-12-11 07:57:51 +03:00
|
|
|
switch (next_info & 0x1F) {
|
2009-04-26 17:03:50 +04:00
|
|
|
case NOMAP:
|
2009-06-16 15:10:10 +04:00
|
|
|
{
|
|
|
|
const unsigned char *p = inchar_start;
|
|
|
|
writebuf_off = 0;
|
|
|
|
while (p < in_p) {
|
|
|
|
TRANSCODING_WRITEBUF(tc)[writebuf_off++] = (unsigned char)*p++;
|
|
|
|
}
|
|
|
|
writebuf_len = writebuf_off;
|
|
|
|
writebuf_off = 0;
|
|
|
|
while (writebuf_off < writebuf_len) {
|
|
|
|
SUSPEND_OBUF(3);
|
|
|
|
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
2007-12-10 11:46:06 +03:00
|
|
|
case 0x00: case 0x04: case 0x08: case 0x0C:
|
|
|
|
case 0x10: case 0x14: case 0x18: case 0x1C:
|
2008-09-09 20:27:02 +04:00
|
|
|
SUSPEND_AFTER_OUTPUT(25);
|
2008-08-10 03:33:37 +04:00
|
|
|
while (in_p >= in_stop) {
|
2008-08-14 18:48:41 +04:00
|
|
|
if (!(opt & ECONV_PARTIAL_INPUT))
|
2008-08-26 20:09:29 +04:00
|
|
|
goto incomplete;
|
2008-08-14 17:45:54 +04:00
|
|
|
SUSPEND(econv_source_buffer_empty, 5);
|
2007-12-10 11:46:06 +03:00
|
|
|
}
|
2008-08-10 03:33:37 +04:00
|
|
|
next_byte = (unsigned char)*in_p++;
|
2009-10-10 18:51:25 +04:00
|
|
|
next_table = (unsigned int)next_info;
|
2007-12-10 11:46:06 +03:00
|
|
|
goto follow_byte;
|
|
|
|
case ZERObt: /* drop input */
|
|
|
|
continue;
|
|
|
|
case ONEbt:
|
2008-08-13 12:28:39 +04:00
|
|
|
SUSPEND_OBUF(9); *out_p++ = getBT1(next_info);
|
2007-12-10 11:46:06 +03:00
|
|
|
continue;
|
|
|
|
case TWObt:
|
2008-08-13 12:28:39 +04:00
|
|
|
SUSPEND_OBUF(10); *out_p++ = getBT1(next_info);
|
|
|
|
SUSPEND_OBUF(21); *out_p++ = getBT2(next_info);
|
2007-12-10 11:46:06 +03:00
|
|
|
continue;
|
2008-08-09 10:02:01 +04:00
|
|
|
case THREEbt:
|
2008-08-13 12:28:39 +04:00
|
|
|
SUSPEND_OBUF(11); *out_p++ = getBT1(next_info);
|
|
|
|
SUSPEND_OBUF(15); *out_p++ = getBT2(next_info);
|
|
|
|
SUSPEND_OBUF(16); *out_p++ = getBT3(next_info);
|
2008-08-09 10:02:01 +04:00
|
|
|
continue;
|
2007-12-10 11:46:06 +03:00
|
|
|
case FOURbt:
|
2008-08-13 12:28:39 +04:00
|
|
|
SUSPEND_OBUF(12); *out_p++ = getBT0(next_info);
|
|
|
|
SUSPEND_OBUF(17); *out_p++ = getBT1(next_info);
|
|
|
|
SUSPEND_OBUF(18); *out_p++ = getBT2(next_info);
|
|
|
|
SUSPEND_OBUF(19); *out_p++ = getBT3(next_info);
|
2007-12-10 11:46:06 +03:00
|
|
|
continue;
|
2009-01-14 14:12:30 +03:00
|
|
|
case GB4bt:
|
|
|
|
SUSPEND_OBUF(29); *out_p++ = getGB4bt0(next_info);
|
|
|
|
SUSPEND_OBUF(30); *out_p++ = getGB4bt1(next_info);
|
|
|
|
SUSPEND_OBUF(31); *out_p++ = getGB4bt2(next_info);
|
|
|
|
SUSPEND_OBUF(32); *out_p++ = getGB4bt3(next_info);
|
|
|
|
continue;
|
2008-09-07 13:13:16 +04:00
|
|
|
case STR1:
|
2008-09-15 19:42:30 +04:00
|
|
|
tc->output_index = 0;
|
|
|
|
while (tc->output_index < STR1_LENGTH(BYTE_ADDR(STR1_BYTEINDEX(next_info)))) {
|
|
|
|
SUSPEND_OBUF(28); *out_p++ = BYTE_ADDR(STR1_BYTEINDEX(next_info))[1+tc->output_index];
|
|
|
|
tc->output_index++;
|
2008-09-07 13:13:16 +04:00
|
|
|
}
|
|
|
|
continue;
|
2007-12-28 12:26:55 +03:00
|
|
|
case FUNii:
|
2008-09-03 18:12:06 +04:00
|
|
|
next_info = (VALUE)(*tr->func_ii)(TRANSCODING_STATE(tc), next_info);
|
2007-12-28 12:26:55 +03:00
|
|
|
goto follow_info;
|
2008-01-20 09:12:48 +03:00
|
|
|
case FUNsi:
|
2008-08-09 10:02:01 +04:00
|
|
|
{
|
2008-08-10 03:33:37 +04:00
|
|
|
const unsigned char *char_start;
|
2008-08-10 04:23:36 +04:00
|
|
|
size_t char_len;
|
2008-08-10 06:33:05 +04:00
|
|
|
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
|
2008-09-03 18:12:06 +04:00
|
|
|
next_info = (VALUE)(*tr->func_si)(TRANSCODING_STATE(tc), char_start, (size_t)char_len);
|
2008-08-11 19:50:42 +04:00
|
|
|
goto follow_info;
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
2008-01-20 09:12:48 +03:00
|
|
|
case FUNio:
|
2008-08-13 12:28:39 +04:00
|
|
|
SUSPEND_OBUF(13);
|
|
|
|
if (tr->max_output <= out_stop - out_p)
|
2008-09-14 22:35:17 +04:00
|
|
|
out_p += tr->func_io(TRANSCODING_STATE(tc),
|
|
|
|
next_info, out_p, out_stop - out_p);
|
2008-08-13 12:28:39 +04:00
|
|
|
else {
|
2008-09-14 22:35:17 +04:00
|
|
|
writebuf_len = tr->func_io(TRANSCODING_STATE(tc),
|
2008-09-14 22:06:20 +04:00
|
|
|
next_info,
|
|
|
|
TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
|
2008-08-13 12:28:39 +04:00
|
|
|
writebuf_off = 0;
|
|
|
|
while (writebuf_off < writebuf_len) {
|
|
|
|
SUSPEND_OBUF(20);
|
|
|
|
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
|
|
|
|
}
|
|
|
|
}
|
2008-01-20 09:12:48 +03:00
|
|
|
break;
|
|
|
|
case FUNso:
|
2008-08-09 10:02:01 +04:00
|
|
|
{
|
2008-08-10 03:33:37 +04:00
|
|
|
const unsigned char *char_start;
|
2008-08-10 04:23:36 +04:00
|
|
|
size_t char_len;
|
2008-08-13 12:28:39 +04:00
|
|
|
SUSPEND_OBUF(14);
|
|
|
|
if (tr->max_output <= out_stop - out_p) {
|
|
|
|
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
|
2008-09-14 22:35:17 +04:00
|
|
|
out_p += tr->func_so(TRANSCODING_STATE(tc),
|
2008-09-14 22:06:20 +04:00
|
|
|
char_start, (size_t)char_len,
|
|
|
|
out_p, out_stop - out_p);
|
2008-08-13 12:28:39 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
|
2008-09-14 22:35:17 +04:00
|
|
|
writebuf_len = tr->func_so(TRANSCODING_STATE(tc),
|
2008-09-14 22:06:20 +04:00
|
|
|
char_start, (size_t)char_len,
|
|
|
|
TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
|
2008-08-13 12:28:39 +04:00
|
|
|
writebuf_off = 0;
|
|
|
|
while (writebuf_off < writebuf_len) {
|
|
|
|
SUSPEND_OBUF(22);
|
|
|
|
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
|
|
|
|
}
|
2009-12-10 14:59:12 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FUNsio:
|
|
|
|
{
|
|
|
|
const unsigned char *char_start;
|
|
|
|
size_t char_len;
|
|
|
|
SUSPEND_OBUF(33);
|
|
|
|
if (tr->max_output <= out_stop - out_p) {
|
|
|
|
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
|
|
|
|
out_p += tr->func_sio(TRANSCODING_STATE(tc),
|
|
|
|
char_start, (size_t)char_len, next_info,
|
|
|
|
out_p, out_stop - out_p);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
|
|
|
|
writebuf_len = tr->func_sio(TRANSCODING_STATE(tc),
|
|
|
|
char_start, (size_t)char_len, next_info,
|
|
|
|
TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
|
|
|
|
writebuf_off = 0;
|
|
|
|
while (writebuf_off < writebuf_len) {
|
|
|
|
SUSPEND_OBUF(34);
|
|
|
|
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
|
|
|
|
}
|
2008-08-13 12:28:39 +04:00
|
|
|
}
|
2008-08-09 10:02:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2007-12-27 11:27:19 +03:00
|
|
|
case INVALID:
|
2008-08-11 05:06:21 +04:00
|
|
|
if (tc->recognized_len + (in_p - inchar_start) <= unitlen) {
|
2008-08-14 10:12:27 +04:00
|
|
|
if (tc->recognized_len + (in_p - inchar_start) < unitlen)
|
2008-09-09 20:27:02 +04:00
|
|
|
SUSPEND_AFTER_OUTPUT(26);
|
2008-08-14 18:48:41 +04:00
|
|
|
while ((opt & ECONV_PARTIAL_INPUT) && tc->recognized_len + (in_stop - inchar_start) < unitlen) {
|
2008-08-10 17:37:44 +04:00
|
|
|
in_p = in_stop;
|
2008-08-14 17:45:54 +04:00
|
|
|
SUSPEND(econv_source_buffer_empty, 8);
|
2008-08-10 17:37:44 +04:00
|
|
|
}
|
2008-08-11 05:06:21 +04:00
|
|
|
if (tc->recognized_len + (in_stop - inchar_start) <= unitlen) {
|
2008-08-10 17:37:44 +04:00
|
|
|
in_p = in_stop;
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
2008-08-10 03:33:37 +04:00
|
|
|
else {
|
2008-08-11 05:06:21 +04:00
|
|
|
in_p = inchar_start + (unitlen - tc->recognized_len);
|
2008-08-10 03:33:37 +04:00
|
|
|
}
|
2008-08-08 19:48:17 +04:00
|
|
|
}
|
2008-08-10 17:37:44 +04:00
|
|
|
else {
|
2009-10-10 18:51:25 +04:00
|
|
|
ssize_t invalid_len; /* including the last byte which causes invalid */
|
|
|
|
ssize_t discard_len;
|
2008-08-11 05:06:21 +04:00
|
|
|
invalid_len = tc->recognized_len + (in_p - inchar_start);
|
2008-08-10 17:37:44 +04:00
|
|
|
discard_len = ((invalid_len - 1) / unitlen) * unitlen;
|
2008-08-11 05:06:21 +04:00
|
|
|
readagain_len = invalid_len - discard_len;
|
2008-08-10 17:37:44 +04:00
|
|
|
}
|
|
|
|
goto invalid;
|
2007-12-10 11:46:06 +03:00
|
|
|
case UNDEF:
|
2008-07-14 10:27:26 +04:00
|
|
|
goto undef;
|
2009-11-25 13:28:45 +03:00
|
|
|
default:
|
2009-11-26 08:25:08 +03:00
|
|
|
rb_raise(rb_eRuntimeError, "unknown transcoding instruction");
|
2007-12-11 07:57:51 +03:00
|
|
|
}
|
|
|
|
continue;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
2007-12-27 11:27:19 +03:00
|
|
|
invalid:
|
2008-08-14 17:45:54 +04:00
|
|
|
SUSPEND(econv_invalid_byte_sequence, 1);
|
2008-08-09 10:02:01 +04:00
|
|
|
continue;
|
|
|
|
|
2008-08-26 20:09:29 +04:00
|
|
|
incomplete:
|
|
|
|
SUSPEND(econv_incomplete_input, 27);
|
|
|
|
continue;
|
|
|
|
|
2008-08-09 10:02:01 +04:00
|
|
|
undef:
|
2008-08-14 17:45:54 +04:00
|
|
|
SUSPEND(econv_undefined_conversion, 2);
|
2008-08-09 10:02:01 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2008-08-10 06:33:05 +04:00
|
|
|
if (tr->finish_func) {
|
2008-08-13 12:28:39 +04:00
|
|
|
SUSPEND_OBUF(4);
|
|
|
|
if (tr->max_output <= out_stop - out_p) {
|
2008-09-14 22:06:20 +04:00
|
|
|
out_p += tr->finish_func(TRANSCODING_STATE(tc),
|
|
|
|
out_p, out_stop - out_p);
|
2008-08-13 12:28:39 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-09-14 22:06:20 +04:00
|
|
|
writebuf_len = tr->finish_func(TRANSCODING_STATE(tc),
|
|
|
|
TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
|
2008-08-13 12:28:39 +04:00
|
|
|
writebuf_off = 0;
|
|
|
|
while (writebuf_off < writebuf_len) {
|
|
|
|
SUSPEND_OBUF(23);
|
|
|
|
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
|
|
|
|
}
|
|
|
|
}
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
while (1)
|
2008-08-14 17:45:54 +04:00
|
|
|
SUSPEND(econv_finished, 6);
|
2008-08-09 10:02:01 +04:00
|
|
|
#undef SUSPEND
|
2008-08-13 12:36:47 +04:00
|
|
|
#undef next_table
|
|
|
|
#undef next_info
|
|
|
|
#undef next_byte
|
2008-08-13 12:28:39 +04:00
|
|
|
#undef writebuf_len
|
|
|
|
#undef writebuf_off
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
|
2008-08-14 15:31:27 +04:00
|
|
|
static rb_econv_result_t
|
2008-08-10 06:17:56 +04:00
|
|
|
transcode_restartable(const unsigned char **in_pos, unsigned char **out_pos,
|
|
|
|
const unsigned char *in_stop, unsigned char *out_stop,
|
2008-08-10 06:33:05 +04:00
|
|
|
rb_transcoding *tc,
|
2008-08-10 06:17:56 +04:00
|
|
|
const int opt)
|
|
|
|
{
|
2008-08-11 05:06:21 +04:00
|
|
|
if (tc->readagain_len) {
|
|
|
|
unsigned char *readagain_buf = ALLOCA_N(unsigned char, tc->readagain_len);
|
|
|
|
const unsigned char *readagain_pos = readagain_buf;
|
|
|
|
const unsigned char *readagain_stop = readagain_buf + tc->readagain_len;
|
2008-08-14 15:31:27 +04:00
|
|
|
rb_econv_result_t res;
|
2008-08-10 06:17:56 +04:00
|
|
|
|
2008-08-11 05:06:21 +04:00
|
|
|
MEMCPY(readagain_buf, TRANSCODING_READBUF(tc) + tc->recognized_len,
|
|
|
|
unsigned char, tc->readagain_len);
|
|
|
|
tc->readagain_len = 0;
|
2008-08-14 18:48:41 +04:00
|
|
|
res = transcode_restartable0(&readagain_pos, out_pos, readagain_stop, out_stop, tc, opt|ECONV_PARTIAL_INPUT);
|
2008-08-14 17:45:54 +04:00
|
|
|
if (res != econv_source_buffer_empty) {
|
2008-08-11 05:06:21 +04:00
|
|
|
MEMCPY(TRANSCODING_READBUF(tc) + tc->recognized_len + tc->readagain_len,
|
|
|
|
readagain_pos, unsigned char, readagain_stop - readagain_pos);
|
|
|
|
tc->readagain_len += readagain_stop - readagain_pos;
|
2008-08-10 06:17:56 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2008-08-10 06:33:05 +04:00
|
|
|
return transcode_restartable0(in_pos, out_pos, in_stop, out_stop, tc, opt);
|
2008-08-10 06:17:56 +04:00
|
|
|
}
|
|
|
|
|
2008-08-10 16:26:01 +04:00
|
|
|
static rb_transcoding *
|
2008-08-13 06:40:24 +04:00
|
|
|
rb_transcoding_open_by_transcoder(const rb_transcoder *tr, int flags)
|
2008-08-10 16:26:01 +04:00
|
|
|
{
|
|
|
|
rb_transcoding *tc;
|
|
|
|
|
|
|
|
tc = ALLOC(rb_transcoding);
|
|
|
|
tc->transcoder = tr;
|
|
|
|
tc->flags = flags;
|
2008-09-03 18:12:06 +04:00
|
|
|
if (TRANSCODING_STATE_EMBED_MAX < tr->state_size)
|
2008-09-13 13:52:02 +04:00
|
|
|
tc->state.ptr = xmalloc(tr->state_size);
|
2008-09-03 18:12:06 +04:00
|
|
|
if (tr->state_init_func) {
|
|
|
|
(tr->state_init_func)(TRANSCODING_STATE(tc)); /* xxx: check return value */
|
|
|
|
}
|
2008-08-10 16:26:01 +04:00
|
|
|
tc->resume_position = 0;
|
2008-08-11 05:06:21 +04:00
|
|
|
tc->recognized_len = 0;
|
|
|
|
tc->readagain_len = 0;
|
2008-08-13 12:28:39 +04:00
|
|
|
tc->writebuf_len = 0;
|
|
|
|
tc->writebuf_off = 0;
|
2009-10-10 18:51:25 +04:00
|
|
|
if ((int)sizeof(tc->readbuf.ary) < tr->max_input) {
|
2008-08-10 16:26:01 +04:00
|
|
|
tc->readbuf.ptr = xmalloc(tr->max_input);
|
|
|
|
}
|
2009-10-10 18:51:25 +04:00
|
|
|
if ((int)sizeof(tc->writebuf.ary) < tr->max_output) {
|
2008-08-13 12:28:39 +04:00
|
|
|
tc->writebuf.ptr = xmalloc(tr->max_output);
|
|
|
|
}
|
2008-08-10 16:26:01 +04:00
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
2008-08-14 15:31:27 +04:00
|
|
|
static rb_econv_result_t
|
2008-08-10 16:26:01 +04:00
|
|
|
rb_transcoding_convert(rb_transcoding *tc,
|
|
|
|
const unsigned char **input_ptr, const unsigned char *input_stop,
|
|
|
|
unsigned char **output_ptr, unsigned char *output_stop,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
return transcode_restartable(
|
|
|
|
input_ptr, output_ptr,
|
|
|
|
input_stop, output_stop,
|
|
|
|
tc, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_transcoding_close(rb_transcoding *tc)
|
|
|
|
{
|
|
|
|
const rb_transcoder *tr = tc->transcoder;
|
2008-09-03 18:12:06 +04:00
|
|
|
if (tr->state_fini_func) {
|
|
|
|
(tr->state_fini_func)(TRANSCODING_STATE(tc)); /* check return value? */
|
|
|
|
}
|
|
|
|
if (TRANSCODING_STATE_EMBED_MAX < tr->state_size)
|
2008-09-13 13:52:02 +04:00
|
|
|
xfree(tc->state.ptr);
|
2009-10-10 18:51:25 +04:00
|
|
|
if ((int)sizeof(tc->readbuf.ary) < tr->max_input)
|
2008-08-10 16:26:01 +04:00
|
|
|
xfree(tc->readbuf.ptr);
|
2009-10-10 18:51:25 +04:00
|
|
|
if ((int)sizeof(tc->writebuf.ary) < tr->max_output)
|
2008-08-13 12:28:39 +04:00
|
|
|
xfree(tc->writebuf.ptr);
|
2008-08-10 16:26:01 +04:00
|
|
|
xfree(tc);
|
|
|
|
}
|
|
|
|
|
2009-06-17 02:36:27 +04:00
|
|
|
static size_t
|
|
|
|
rb_transcoding_memsize(rb_transcoding *tc)
|
|
|
|
{
|
|
|
|
size_t size = sizeof(rb_transcoding);
|
|
|
|
const rb_transcoder *tr = tc->transcoder;
|
|
|
|
|
|
|
|
if (TRANSCODING_STATE_EMBED_MAX < tr->state_size) {
|
|
|
|
size += tr->state_size;
|
|
|
|
}
|
2009-10-10 18:51:25 +04:00
|
|
|
if ((int)sizeof(tc->readbuf.ary) < tr->max_input) {
|
2009-06-17 02:36:27 +04:00
|
|
|
size += tr->max_input;
|
|
|
|
}
|
2009-10-10 18:51:25 +04:00
|
|
|
if ((int)sizeof(tc->writebuf.ary) < tr->max_output) {
|
2009-06-17 02:36:27 +04:00
|
|
|
size += tr->max_output;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2008-08-14 15:31:27 +04:00
|
|
|
static rb_econv_t *
|
2008-09-12 16:58:23 +04:00
|
|
|
rb_econv_alloc(int n_hint)
|
2008-08-11 19:50:42 +04:00
|
|
|
{
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_econv_t *ec;
|
2008-08-11 19:50:42 +04:00
|
|
|
|
2008-09-12 16:58:23 +04:00
|
|
|
if (n_hint <= 0)
|
|
|
|
n_hint = 1;
|
2008-08-11 19:50:42 +04:00
|
|
|
|
2008-08-14 18:35:55 +04:00
|
|
|
ec = ALLOC(rb_econv_t);
|
2008-09-03 19:11:46 +04:00
|
|
|
ec->flags = 0;
|
2008-08-16 10:25:38 +04:00
|
|
|
ec->source_encoding_name = NULL;
|
|
|
|
ec->destination_encoding_name = NULL;
|
2008-09-12 16:23:25 +04:00
|
|
|
ec->started = 0;
|
2008-09-03 20:34:11 +04:00
|
|
|
ec->replacement_str = NULL;
|
|
|
|
ec->replacement_len = 0;
|
2008-09-08 21:23:37 +04:00
|
|
|
ec->replacement_enc = NULL;
|
2008-09-03 20:34:11 +04:00
|
|
|
ec->replacement_allocated = 0;
|
2008-08-16 09:32:42 +04:00
|
|
|
ec->in_buf_start = NULL;
|
|
|
|
ec->in_data_start = NULL;
|
|
|
|
ec->in_data_end = NULL;
|
|
|
|
ec->in_buf_end = NULL;
|
2008-09-12 16:58:23 +04:00
|
|
|
ec->num_allocated = n_hint;
|
|
|
|
ec->num_trans = 0;
|
|
|
|
ec->elems = ALLOC_N(rb_econv_elem_t, ec->num_allocated);
|
2008-08-14 18:35:55 +04:00
|
|
|
ec->num_finished = 0;
|
|
|
|
ec->last_tc = NULL;
|
2008-08-25 17:04:16 +04:00
|
|
|
ec->last_error.result = econv_source_buffer_empty;
|
|
|
|
ec->last_error.error_tc = NULL;
|
|
|
|
ec->last_error.source_encoding = NULL;
|
|
|
|
ec->last_error.destination_encoding = NULL;
|
|
|
|
ec->last_error.error_bytes_start = NULL;
|
|
|
|
ec->last_error.error_bytes_len = 0;
|
|
|
|
ec->last_error.readagain_len = 0;
|
2008-08-14 18:35:55 +04:00
|
|
|
ec->source_encoding = NULL;
|
|
|
|
ec->destination_encoding = NULL;
|
2008-09-12 16:58:23 +04:00
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rb_econv_add_transcoder_at(rb_econv_t *ec, const rb_transcoder *tr, int i)
|
|
|
|
{
|
|
|
|
int n, j;
|
|
|
|
int bufsize = 4096;
|
|
|
|
unsigned char *p;
|
|
|
|
|
|
|
|
if (ec->num_trans == ec->num_allocated) {
|
|
|
|
n = ec->num_allocated * 2;
|
|
|
|
REALLOC_N(ec->elems, rb_econv_elem_t, n);
|
|
|
|
ec->num_allocated = n;
|
2008-08-14 18:35:55 +04:00
|
|
|
}
|
|
|
|
|
2008-09-12 16:58:23 +04:00
|
|
|
p = xmalloc(bufsize);
|
|
|
|
|
|
|
|
MEMMOVE(ec->elems+i+1, ec->elems+i, rb_econv_elem_t, ec->num_trans-i);
|
|
|
|
|
|
|
|
ec->elems[i].tc = rb_transcoding_open_by_transcoder(tr, 0);
|
|
|
|
ec->elems[i].out_buf_start = p;
|
|
|
|
ec->elems[i].out_buf_end = p + bufsize;
|
|
|
|
ec->elems[i].out_data_start = p;
|
|
|
|
ec->elems[i].out_data_end = p;
|
|
|
|
ec->elems[i].last_result = econv_source_buffer_empty;
|
|
|
|
|
|
|
|
ec->num_trans++;
|
|
|
|
|
2008-09-13 13:21:34 +04:00
|
|
|
if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding))
|
2008-09-12 18:20:27 +04:00
|
|
|
for (j = ec->num_trans-1; i <= j; j--) {
|
2008-09-12 16:58:23 +04:00
|
|
|
rb_transcoding *tc = ec->elems[j].tc;
|
|
|
|
const rb_transcoder *tr2 = tc->transcoder;
|
2008-09-13 13:21:34 +04:00
|
|
|
if (!DECORATOR_P(tr2->src_encoding, tr2->dst_encoding)) {
|
2008-09-12 16:58:23 +04:00
|
|
|
ec->last_tc = tc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rb_econv_t *
|
|
|
|
rb_econv_open_by_transcoder_entries(int n, transcoder_entry_t **entries)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec;
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
const rb_transcoder *tr;
|
|
|
|
tr = load_transcoder_entry(entries[i]);
|
|
|
|
if (!tr)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ec = rb_econv_alloc(n);
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
const rb_transcoder *tr = load_transcoder_entry(entries[i]);
|
|
|
|
ret = rb_econv_add_transcoder_at(ec, tr, ec->num_trans);
|
|
|
|
if (ret == -1) {
|
|
|
|
rb_econv_close(ec);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-12 13:42:49 +04:00
|
|
|
}
|
|
|
|
|
2008-08-14 18:35:55 +04:00
|
|
|
return ec;
|
2008-08-11 19:50:42 +04:00
|
|
|
}
|
|
|
|
|
2008-09-06 09:16:40 +04:00
|
|
|
struct trans_open_t {
|
|
|
|
transcoder_entry_t **entries;
|
|
|
|
int num_additional;
|
|
|
|
};
|
|
|
|
|
2008-08-13 06:40:24 +04:00
|
|
|
static void
|
2008-09-04 19:35:30 +04:00
|
|
|
trans_open_i(const char *sname, const char *dname, int depth, void *arg)
|
2008-08-13 06:40:24 +04:00
|
|
|
{
|
2008-09-06 09:16:40 +04:00
|
|
|
struct trans_open_t *toarg = arg;
|
2008-08-13 06:40:24 +04:00
|
|
|
|
2008-09-06 09:16:40 +04:00
|
|
|
if (!toarg->entries) {
|
|
|
|
toarg->entries = ALLOC_N(transcoder_entry_t *, depth+1+toarg->num_additional);
|
2008-08-13 06:40:24 +04:00
|
|
|
}
|
2008-09-06 09:16:40 +04:00
|
|
|
toarg->entries[depth] = get_transcoder_entry(sname, dname);
|
2008-08-13 06:40:24 +04:00
|
|
|
}
|
|
|
|
|
2008-09-09 15:57:31 +04:00
|
|
|
static rb_econv_t *
|
2008-09-08 21:23:37 +04:00
|
|
|
rb_econv_open0(const char *sname, const char *dname, int ecflags)
|
2008-08-13 06:40:24 +04:00
|
|
|
{
|
|
|
|
transcoder_entry_t **entries = NULL;
|
|
|
|
int num_trans;
|
2008-09-08 20:11:32 +04:00
|
|
|
rb_econv_t *ec;
|
2008-08-26 16:55:14 +04:00
|
|
|
|
2019-07-23 22:49:51 +03:00
|
|
|
/* Just check if sname and dname are defined */
|
|
|
|
/* (This check is needed?) */
|
|
|
|
if (*sname) rb_enc_find_index(sname);
|
|
|
|
if (*dname) rb_enc_find_index(dname);
|
2008-08-13 06:40:24 +04:00
|
|
|
|
2008-09-04 19:35:30 +04:00
|
|
|
if (*sname == '\0' && *dname == '\0') {
|
2008-08-22 20:44:00 +04:00
|
|
|
num_trans = 0;
|
2008-09-09 15:18:59 +04:00
|
|
|
entries = NULL;
|
2015-08-05 08:37:58 +03:00
|
|
|
sname = dname = "";
|
2008-08-22 20:44:00 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-09-06 09:16:40 +04:00
|
|
|
struct trans_open_t toarg;
|
|
|
|
toarg.entries = NULL;
|
2008-09-09 15:18:59 +04:00
|
|
|
toarg.num_additional = 0;
|
2008-09-06 09:16:40 +04:00
|
|
|
num_trans = transcode_search_path(sname, dname, trans_open_i, (void *)&toarg);
|
|
|
|
entries = toarg.entries;
|
2008-09-09 15:18:59 +04:00
|
|
|
if (num_trans < 0) {
|
|
|
|
xfree(entries);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-22 20:44:00 +04:00
|
|
|
}
|
2008-08-13 06:40:24 +04:00
|
|
|
|
2008-08-14 18:35:55 +04:00
|
|
|
ec = rb_econv_open_by_transcoder_entries(num_trans, entries);
|
2008-08-19 21:32:31 +04:00
|
|
|
xfree(entries);
|
2008-08-16 10:25:38 +04:00
|
|
|
if (!ec)
|
2008-08-24 07:03:18 +04:00
|
|
|
return NULL;
|
2008-08-16 10:25:38 +04:00
|
|
|
|
2008-09-03 19:11:46 +04:00
|
|
|
ec->flags = ecflags;
|
2008-09-04 19:35:30 +04:00
|
|
|
ec->source_encoding_name = sname;
|
|
|
|
ec->destination_encoding_name = dname;
|
2008-08-13 09:30:42 +04:00
|
|
|
|
2008-08-14 18:35:55 +04:00
|
|
|
return ec;
|
2008-08-13 06:40:24 +04:00
|
|
|
}
|
|
|
|
|
2008-09-12 17:53:42 +04:00
|
|
|
#define MAX_ECFLAGS_DECORATORS 32
|
|
|
|
|
|
|
|
static int
|
|
|
|
decorator_names(int ecflags, const char **decorators_ret)
|
2008-09-08 21:23:37 +04:00
|
|
|
{
|
2008-09-09 15:57:31 +04:00
|
|
|
int num_decorators;
|
2008-09-08 21:23:37 +04:00
|
|
|
|
2011-04-26 19:54:14 +04:00
|
|
|
switch (ecflags & ECONV_NEWLINE_DECORATOR_MASK) {
|
|
|
|
case ECONV_UNIVERSAL_NEWLINE_DECORATOR:
|
|
|
|
case ECONV_CRLF_NEWLINE_DECORATOR:
|
|
|
|
case ECONV_CR_NEWLINE_DECORATOR:
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
2008-09-12 17:53:42 +04:00
|
|
|
return -1;
|
2011-04-26 19:54:14 +04:00
|
|
|
}
|
2008-09-08 21:23:37 +04:00
|
|
|
|
2008-09-09 16:22:43 +04:00
|
|
|
if ((ecflags & ECONV_XML_TEXT_DECORATOR) &&
|
|
|
|
(ecflags & ECONV_XML_ATTR_CONTENT_DECORATOR))
|
2008-09-12 17:53:42 +04:00
|
|
|
return -1;
|
2008-09-08 21:23:37 +04:00
|
|
|
|
2008-09-09 15:57:31 +04:00
|
|
|
num_decorators = 0;
|
|
|
|
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_XML_TEXT_DECORATOR)
|
2008-09-12 21:30:07 +04:00
|
|
|
decorators_ret[num_decorators++] = "xml_text_escape";
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_XML_ATTR_CONTENT_DECORATOR)
|
2008-09-12 21:30:07 +04:00
|
|
|
decorators_ret[num_decorators++] = "xml_attr_content_escape";
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_XML_ATTR_QUOTE_DECORATOR)
|
2008-09-12 21:30:07 +04:00
|
|
|
decorators_ret[num_decorators++] = "xml_attr_quote";
|
2008-09-08 21:23:37 +04:00
|
|
|
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_CRLF_NEWLINE_DECORATOR)
|
2008-09-12 17:53:42 +04:00
|
|
|
decorators_ret[num_decorators++] = "crlf_newline";
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_CR_NEWLINE_DECORATOR)
|
2008-09-12 17:53:42 +04:00
|
|
|
decorators_ret[num_decorators++] = "cr_newline";
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_UNIVERSAL_NEWLINE_DECORATOR)
|
2008-09-12 17:53:42 +04:00
|
|
|
decorators_ret[num_decorators++] = "universal_newline";
|
|
|
|
|
|
|
|
return num_decorators;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_econv_t *
|
|
|
|
rb_econv_open(const char *sname, const char *dname, int ecflags)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec;
|
|
|
|
int num_decorators;
|
|
|
|
const char *decorators[MAX_ECFLAGS_DECORATORS];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
num_decorators = decorator_names(ecflags, decorators);
|
|
|
|
if (num_decorators == -1)
|
|
|
|
return NULL;
|
2008-09-08 21:23:37 +04:00
|
|
|
|
|
|
|
ec = rb_econv_open0(sname, dname, ecflags & ECONV_ERROR_HANDLER_MASK);
|
|
|
|
if (!ec)
|
|
|
|
return NULL;
|
|
|
|
|
2008-09-09 15:57:31 +04:00
|
|
|
for (i = 0; i < num_decorators; i++)
|
|
|
|
if (rb_econv_decorate_at_last(ec, decorators[i]) == -1) {
|
2008-09-08 21:23:37 +04:00
|
|
|
rb_econv_close(ec);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ec->flags |= ecflags & ~ECONV_ERROR_HANDLER_MASK;
|
|
|
|
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
2008-08-11 19:50:42 +04:00
|
|
|
static int
|
2008-08-14 18:35:55 +04:00
|
|
|
trans_sweep(rb_econv_t *ec,
|
2008-08-11 19:50:42 +04:00
|
|
|
const unsigned char **input_ptr, const unsigned char *input_stop,
|
|
|
|
unsigned char **output_ptr, unsigned char *output_stop,
|
|
|
|
int flags,
|
|
|
|
int start)
|
|
|
|
{
|
|
|
|
int try;
|
|
|
|
int i, f;
|
|
|
|
|
|
|
|
const unsigned char **ipp, *is, *iold;
|
|
|
|
unsigned char **opp, *os, *oold;
|
2008-08-14 15:31:27 +04:00
|
|
|
rb_econv_result_t res;
|
2008-08-11 19:50:42 +04:00
|
|
|
|
|
|
|
try = 1;
|
|
|
|
while (try) {
|
|
|
|
try = 0;
|
2008-08-14 18:35:55 +04:00
|
|
|
for (i = start; i < ec->num_trans; i++) {
|
|
|
|
rb_econv_elem_t *te = &ec->elems[i];
|
2008-08-11 19:50:42 +04:00
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
ipp = input_ptr;
|
|
|
|
is = input_stop;
|
|
|
|
}
|
|
|
|
else {
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_econv_elem_t *prev_te = &ec->elems[i-1];
|
2008-08-11 19:50:42 +04:00
|
|
|
ipp = (const unsigned char **)&prev_te->out_data_start;
|
|
|
|
is = prev_te->out_data_end;
|
|
|
|
}
|
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
if (i == ec->num_trans-1) {
|
2008-08-11 19:50:42 +04:00
|
|
|
opp = output_ptr;
|
|
|
|
os = output_stop;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (te->out_buf_start != te->out_data_start) {
|
2009-10-10 18:51:25 +04:00
|
|
|
ssize_t len = te->out_data_end - te->out_data_start;
|
|
|
|
ssize_t off = te->out_data_start - te->out_buf_start;
|
2008-08-11 19:50:42 +04:00
|
|
|
MEMMOVE(te->out_buf_start, te->out_data_start, unsigned char, len);
|
|
|
|
te->out_data_start = te->out_buf_start;
|
|
|
|
te->out_data_end -= off;
|
|
|
|
}
|
|
|
|
opp = &te->out_data_end;
|
|
|
|
os = te->out_buf_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
f = flags;
|
2008-08-14 18:35:55 +04:00
|
|
|
if (ec->num_finished != i)
|
2008-08-14 18:48:41 +04:00
|
|
|
f |= ECONV_PARTIAL_INPUT;
|
2008-09-09 20:27:02 +04:00
|
|
|
if (i == 0 && (flags & ECONV_AFTER_OUTPUT)) {
|
2008-08-14 10:12:27 +04:00
|
|
|
start = 1;
|
2008-09-09 20:27:02 +04:00
|
|
|
flags &= ~ECONV_AFTER_OUTPUT;
|
2008-08-14 10:12:27 +04:00
|
|
|
}
|
|
|
|
if (i != 0)
|
2008-09-09 20:27:02 +04:00
|
|
|
f &= ~ECONV_AFTER_OUTPUT;
|
2008-08-11 19:50:42 +04:00
|
|
|
iold = *ipp;
|
|
|
|
oold = *opp;
|
|
|
|
te->last_result = res = rb_transcoding_convert(te->tc, ipp, is, opp, os, f);
|
|
|
|
if (iold != *ipp || oold != *opp)
|
|
|
|
try = 1;
|
|
|
|
|
|
|
|
switch (res) {
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_invalid_byte_sequence:
|
2008-08-26 20:09:29 +04:00
|
|
|
case econv_incomplete_input:
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_undefined_conversion:
|
2008-09-09 20:27:02 +04:00
|
|
|
case econv_after_output:
|
2008-08-11 19:50:42 +04:00
|
|
|
return i;
|
|
|
|
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_destination_buffer_full:
|
|
|
|
case econv_source_buffer_empty:
|
2008-08-11 19:50:42 +04:00
|
|
|
break;
|
|
|
|
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_finished:
|
2008-08-14 18:35:55 +04:00
|
|
|
ec->num_finished = i+1;
|
2008-08-11 19:50:42 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-14 15:31:27 +04:00
|
|
|
static rb_econv_result_t
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_trans_conv(rb_econv_t *ec,
|
2008-08-11 19:50:42 +04:00
|
|
|
const unsigned char **input_ptr, const unsigned char *input_stop,
|
|
|
|
unsigned char **output_ptr, unsigned char *output_stop,
|
2008-08-15 13:12:56 +04:00
|
|
|
int flags,
|
|
|
|
int *result_position_ptr)
|
2008-08-11 19:50:42 +04:00
|
|
|
{
|
|
|
|
int i;
|
2008-08-14 10:12:27 +04:00
|
|
|
int needreport_index;
|
|
|
|
int sweep_start;
|
2008-08-11 19:50:42 +04:00
|
|
|
|
|
|
|
unsigned char empty_buf;
|
|
|
|
unsigned char *empty_ptr = &empty_buf;
|
|
|
|
|
|
|
|
if (!input_ptr) {
|
|
|
|
input_ptr = (const unsigned char **)&empty_ptr;
|
|
|
|
input_stop = empty_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!output_ptr) {
|
|
|
|
output_ptr = &empty_ptr;
|
|
|
|
output_stop = empty_ptr;
|
|
|
|
}
|
|
|
|
|
2008-09-09 20:27:02 +04:00
|
|
|
if (ec->elems[0].last_result == econv_after_output)
|
2008-08-14 18:35:55 +04:00
|
|
|
ec->elems[0].last_result = econv_source_buffer_empty;
|
2008-08-14 10:12:27 +04:00
|
|
|
|
2008-08-14 18:35:55 +04:00
|
|
|
for (i = ec->num_trans-1; 0 <= i; i--) {
|
|
|
|
switch (ec->elems[i].last_result) {
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_invalid_byte_sequence:
|
2008-08-26 20:09:29 +04:00
|
|
|
case econv_incomplete_input:
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_undefined_conversion:
|
2008-09-09 20:27:02 +04:00
|
|
|
case econv_after_output:
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_finished:
|
2008-08-14 10:12:27 +04:00
|
|
|
sweep_start = i+1;
|
|
|
|
goto found_needreport;
|
|
|
|
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_destination_buffer_full:
|
|
|
|
case econv_source_buffer_empty:
|
2008-08-13 02:43:17 +04:00
|
|
|
break;
|
2008-08-14 10:12:27 +04:00
|
|
|
|
|
|
|
default:
|
|
|
|
rb_bug("unexpected transcode last result");
|
2008-08-11 19:50:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
/* /^[sd]+$/ is confirmed. but actually /^s*d*$/. */
|
2008-08-14 10:12:27 +04:00
|
|
|
|
2008-08-14 18:35:55 +04:00
|
|
|
if (ec->elems[ec->num_trans-1].last_result == econv_destination_buffer_full &&
|
2008-09-09 20:27:02 +04:00
|
|
|
(flags & ECONV_AFTER_OUTPUT)) {
|
2008-08-14 15:31:27 +04:00
|
|
|
rb_econv_result_t res;
|
2008-08-14 10:12:27 +04:00
|
|
|
|
2008-08-14 18:35:55 +04:00
|
|
|
res = rb_trans_conv(ec, NULL, NULL, output_ptr, output_stop,
|
2008-09-09 20:27:02 +04:00
|
|
|
(flags & ~ECONV_AFTER_OUTPUT)|ECONV_PARTIAL_INPUT,
|
2008-08-15 13:12:56 +04:00
|
|
|
result_position_ptr);
|
2008-08-14 10:12:27 +04:00
|
|
|
|
2008-08-14 17:45:54 +04:00
|
|
|
if (res == econv_source_buffer_empty)
|
2008-09-09 20:27:02 +04:00
|
|
|
return econv_after_output;
|
2008-08-14 10:12:27 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
sweep_start = 0;
|
|
|
|
|
2008-08-26 20:09:29 +04:00
|
|
|
found_needreport:
|
2008-08-14 10:12:27 +04:00
|
|
|
|
2008-08-11 19:50:42 +04:00
|
|
|
do {
|
2008-08-14 18:35:55 +04:00
|
|
|
needreport_index = trans_sweep(ec, input_ptr, input_stop, output_ptr, output_stop, flags, sweep_start);
|
2008-08-14 10:12:27 +04:00
|
|
|
sweep_start = needreport_index + 1;
|
2008-08-14 18:35:55 +04:00
|
|
|
} while (needreport_index != -1 && needreport_index != ec->num_trans-1);
|
2008-08-11 19:50:42 +04:00
|
|
|
|
2008-08-14 18:35:55 +04:00
|
|
|
for (i = ec->num_trans-1; 0 <= i; i--) {
|
|
|
|
if (ec->elems[i].last_result != econv_source_buffer_empty) {
|
|
|
|
rb_econv_result_t res = ec->elems[i].last_result;
|
2008-08-14 17:45:54 +04:00
|
|
|
if (res == econv_invalid_byte_sequence ||
|
2008-08-26 20:09:29 +04:00
|
|
|
res == econv_incomplete_input ||
|
2008-08-14 17:45:54 +04:00
|
|
|
res == econv_undefined_conversion ||
|
2008-09-09 20:27:02 +04:00
|
|
|
res == econv_after_output) {
|
2008-08-14 18:35:55 +04:00
|
|
|
ec->elems[i].last_result = econv_source_buffer_empty;
|
2008-08-14 10:12:27 +04:00
|
|
|
}
|
2008-08-15 13:12:56 +04:00
|
|
|
if (result_position_ptr)
|
|
|
|
*result_position_ptr = i;
|
2008-08-13 02:43:17 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2008-08-15 13:12:56 +04:00
|
|
|
if (result_position_ptr)
|
|
|
|
*result_position_ptr = -1;
|
2008-08-14 17:45:54 +04:00
|
|
|
return econv_source_buffer_empty;
|
2008-08-11 19:50:42 +04:00
|
|
|
}
|
|
|
|
|
2008-08-23 10:02:58 +04:00
|
|
|
static rb_econv_result_t
|
|
|
|
rb_econv_convert0(rb_econv_t *ec,
|
2008-08-14 10:35:33 +04:00
|
|
|
const unsigned char **input_ptr, const unsigned char *input_stop,
|
|
|
|
unsigned char **output_ptr, unsigned char *output_stop,
|
|
|
|
int flags)
|
|
|
|
{
|
2008-08-14 15:31:27 +04:00
|
|
|
rb_econv_result_t res;
|
2008-08-15 13:12:56 +04:00
|
|
|
int result_position;
|
2008-08-16 09:32:42 +04:00
|
|
|
int has_output = 0;
|
2008-08-15 13:12:56 +04:00
|
|
|
|
|
|
|
memset(&ec->last_error, 0, sizeof(ec->last_error));
|
2008-08-14 10:35:33 +04:00
|
|
|
|
2008-08-25 17:04:16 +04:00
|
|
|
if (ec->num_trans == 0) {
|
|
|
|
size_t len;
|
|
|
|
if (ec->in_buf_start && ec->in_data_start != ec->in_data_end) {
|
|
|
|
if (output_stop - *output_ptr < ec->in_data_end - ec->in_data_start) {
|
|
|
|
len = output_stop - *output_ptr;
|
|
|
|
memcpy(*output_ptr, ec->in_data_start, len);
|
|
|
|
*output_ptr = output_stop;
|
|
|
|
ec->in_data_start += len;
|
|
|
|
res = econv_destination_buffer_full;
|
|
|
|
goto gotresult;
|
|
|
|
}
|
|
|
|
len = ec->in_data_end - ec->in_data_start;
|
|
|
|
memcpy(*output_ptr, ec->in_data_start, len);
|
|
|
|
*output_ptr += len;
|
|
|
|
ec->in_data_start = ec->in_data_end = ec->in_buf_start;
|
2008-09-09 20:27:02 +04:00
|
|
|
if (flags & ECONV_AFTER_OUTPUT) {
|
|
|
|
res = econv_after_output;
|
2008-08-25 17:04:16 +04:00
|
|
|
goto gotresult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (output_stop - *output_ptr < input_stop - *input_ptr) {
|
|
|
|
len = output_stop - *output_ptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = input_stop - *input_ptr;
|
|
|
|
}
|
2008-09-09 20:27:02 +04:00
|
|
|
if (0 < len && (flags & ECONV_AFTER_OUTPUT)) {
|
2008-08-25 17:04:16 +04:00
|
|
|
*(*output_ptr)++ = *(*input_ptr)++;
|
2008-09-09 20:27:02 +04:00
|
|
|
res = econv_after_output;
|
2008-08-25 17:04:16 +04:00
|
|
|
goto gotresult;
|
|
|
|
}
|
|
|
|
memcpy(*output_ptr, *input_ptr, len);
|
|
|
|
*output_ptr += len;
|
|
|
|
*input_ptr += len;
|
|
|
|
if (*input_ptr != input_stop)
|
|
|
|
res = econv_destination_buffer_full;
|
|
|
|
else if (flags & ECONV_PARTIAL_INPUT)
|
|
|
|
res = econv_source_buffer_empty;
|
|
|
|
else
|
|
|
|
res = econv_finished;
|
|
|
|
goto gotresult;
|
|
|
|
}
|
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
if (ec->elems[ec->num_trans-1].out_data_start) {
|
|
|
|
unsigned char *data_start = ec->elems[ec->num_trans-1].out_data_start;
|
|
|
|
unsigned char *data_end = ec->elems[ec->num_trans-1].out_data_end;
|
|
|
|
if (data_start != data_end) {
|
|
|
|
size_t len;
|
|
|
|
if (output_stop - *output_ptr < data_end - data_start) {
|
|
|
|
len = output_stop - *output_ptr;
|
|
|
|
memcpy(*output_ptr, data_start, len);
|
|
|
|
*output_ptr = output_stop;
|
|
|
|
ec->elems[ec->num_trans-1].out_data_start += len;
|
|
|
|
res = econv_destination_buffer_full;
|
|
|
|
goto gotresult;
|
|
|
|
}
|
|
|
|
len = data_end - data_start;
|
|
|
|
memcpy(*output_ptr, data_start, len);
|
|
|
|
*output_ptr += len;
|
|
|
|
ec->elems[ec->num_trans-1].out_data_start =
|
2009-02-22 17:23:33 +03:00
|
|
|
ec->elems[ec->num_trans-1].out_data_end =
|
2008-08-16 09:32:42 +04:00
|
|
|
ec->elems[ec->num_trans-1].out_buf_start;
|
|
|
|
has_output = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 17:23:33 +03:00
|
|
|
if (ec->in_buf_start &&
|
2008-08-16 09:32:42 +04:00
|
|
|
ec->in_data_start != ec->in_data_end) {
|
|
|
|
res = rb_trans_conv(ec, (const unsigned char **)&ec->in_data_start, ec->in_data_end, output_ptr, output_stop,
|
2008-09-09 20:27:02 +04:00
|
|
|
(flags&~ECONV_AFTER_OUTPUT)|ECONV_PARTIAL_INPUT, &result_position);
|
2008-08-16 09:32:42 +04:00
|
|
|
if (res != econv_source_buffer_empty)
|
|
|
|
goto gotresult;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_output &&
|
2008-09-09 20:27:02 +04:00
|
|
|
(flags & ECONV_AFTER_OUTPUT) &&
|
2008-08-16 09:32:42 +04:00
|
|
|
*input_ptr != input_stop) {
|
|
|
|
input_stop = *input_ptr;
|
|
|
|
res = rb_trans_conv(ec, input_ptr, input_stop, output_ptr, output_stop, flags, &result_position);
|
|
|
|
if (res == econv_source_buffer_empty)
|
2008-09-09 20:27:02 +04:00
|
|
|
res = econv_after_output;
|
2008-08-16 09:32:42 +04:00
|
|
|
}
|
2008-09-09 20:27:02 +04:00
|
|
|
else if ((flags & ECONV_AFTER_OUTPUT) ||
|
2008-08-15 13:12:56 +04:00
|
|
|
ec->num_trans == 1) {
|
|
|
|
res = rb_trans_conv(ec, input_ptr, input_stop, output_ptr, output_stop, flags, &result_position);
|
|
|
|
}
|
|
|
|
else {
|
2008-09-09 20:27:02 +04:00
|
|
|
flags |= ECONV_AFTER_OUTPUT;
|
2008-08-15 13:12:56 +04:00
|
|
|
do {
|
|
|
|
res = rb_trans_conv(ec, input_ptr, input_stop, output_ptr, output_stop, flags, &result_position);
|
2008-09-09 20:27:02 +04:00
|
|
|
} while (res == econv_after_output);
|
2008-08-15 13:12:56 +04:00
|
|
|
}
|
|
|
|
|
2008-08-26 20:09:29 +04:00
|
|
|
gotresult:
|
2008-08-15 13:12:56 +04:00
|
|
|
ec->last_error.result = res;
|
|
|
|
if (res == econv_invalid_byte_sequence ||
|
2008-08-26 20:09:29 +04:00
|
|
|
res == econv_incomplete_input ||
|
2008-08-15 13:12:56 +04:00
|
|
|
res == econv_undefined_conversion) {
|
2008-08-15 13:33:59 +04:00
|
|
|
rb_transcoding *error_tc = ec->elems[result_position].tc;
|
|
|
|
ec->last_error.error_tc = error_tc;
|
2008-09-04 19:46:18 +04:00
|
|
|
ec->last_error.source_encoding = error_tc->transcoder->src_encoding;
|
|
|
|
ec->last_error.destination_encoding = error_tc->transcoder->dst_encoding;
|
2008-08-15 13:33:59 +04:00
|
|
|
ec->last_error.error_bytes_start = TRANSCODING_READBUF(error_tc);
|
|
|
|
ec->last_error.error_bytes_len = error_tc->recognized_len;
|
|
|
|
ec->last_error.readagain_len = error_tc->readagain_len;
|
2008-08-15 13:12:56 +04:00
|
|
|
}
|
2008-08-14 10:35:33 +04:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-08-23 10:02:58 +04:00
|
|
|
static int output_replacement_character(rb_econv_t *ec);
|
|
|
|
|
2008-09-06 00:24:18 +04:00
|
|
|
static int
|
|
|
|
output_hex_charref(rb_econv_t *ec)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char utfbuf[1024];
|
|
|
|
const unsigned char *utf;
|
|
|
|
size_t utf_len;
|
|
|
|
int utf_allocated = 0;
|
|
|
|
char charef_buf[16];
|
|
|
|
const unsigned char *p;
|
|
|
|
|
|
|
|
if (encoding_equal(ec->last_error.source_encoding, "UTF-32BE")) {
|
|
|
|
utf = ec->last_error.error_bytes_start;
|
|
|
|
utf_len = ec->last_error.error_bytes_len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
utf = allocate_converted_string(ec->last_error.source_encoding, "UTF-32BE",
|
|
|
|
ec->last_error.error_bytes_start, ec->last_error.error_bytes_len,
|
|
|
|
utfbuf, sizeof(utfbuf),
|
|
|
|
&utf_len);
|
|
|
|
if (!utf)
|
|
|
|
return -1;
|
|
|
|
if (utf != utfbuf && utf != ec->last_error.error_bytes_start)
|
|
|
|
utf_allocated = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (utf_len % 4 != 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
p = utf;
|
|
|
|
while (4 <= utf_len) {
|
|
|
|
unsigned int u = 0;
|
|
|
|
u += p[0] << 24;
|
|
|
|
u += p[1] << 16;
|
|
|
|
u += p[2] << 8;
|
|
|
|
u += p[3];
|
2008-09-06 00:37:36 +04:00
|
|
|
snprintf(charef_buf, sizeof(charef_buf), "&#x%X;", u);
|
2008-09-06 00:24:18 +04:00
|
|
|
|
|
|
|
ret = rb_econv_insert_output(ec, (unsigned char *)charef_buf, strlen(charef_buf), "US-ASCII");
|
|
|
|
if (ret == -1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
p += 4;
|
|
|
|
utf_len -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (utf_allocated)
|
|
|
|
xfree((void *)utf);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (utf_allocated)
|
|
|
|
xfree((void *)utf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-23 10:02:58 +04:00
|
|
|
rb_econv_result_t
|
|
|
|
rb_econv_convert(rb_econv_t *ec,
|
|
|
|
const unsigned char **input_ptr, const unsigned char *input_stop,
|
|
|
|
unsigned char **output_ptr, unsigned char *output_stop,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
rb_econv_result_t ret;
|
|
|
|
|
2008-08-25 17:04:16 +04:00
|
|
|
unsigned char empty_buf;
|
|
|
|
unsigned char *empty_ptr = &empty_buf;
|
|
|
|
|
2008-09-12 16:23:25 +04:00
|
|
|
ec->started = 1;
|
|
|
|
|
2008-08-25 17:04:16 +04:00
|
|
|
if (!input_ptr) {
|
|
|
|
input_ptr = (const unsigned char **)&empty_ptr;
|
|
|
|
input_stop = empty_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!output_ptr) {
|
|
|
|
output_ptr = &empty_ptr;
|
|
|
|
output_stop = empty_ptr;
|
|
|
|
}
|
|
|
|
|
2008-08-26 20:09:29 +04:00
|
|
|
resume:
|
2008-08-23 10:02:58 +04:00
|
|
|
ret = rb_econv_convert0(ec, input_ptr, input_stop, output_ptr, output_stop, flags);
|
|
|
|
|
2008-08-26 20:09:29 +04:00
|
|
|
if (ret == econv_invalid_byte_sequence ||
|
|
|
|
ret == econv_incomplete_input) {
|
2008-08-23 10:02:58 +04:00
|
|
|
/* deal with invalid byte sequence */
|
|
|
|
/* todo: add more alternative behaviors */
|
2008-09-05 23:03:21 +04:00
|
|
|
switch (ec->flags & ECONV_INVALID_MASK) {
|
|
|
|
case ECONV_INVALID_REPLACE:
|
2008-08-23 10:02:58 +04:00
|
|
|
if (output_replacement_character(ec) == 0)
|
|
|
|
goto resume;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == econv_undefined_conversion) {
|
|
|
|
/* valid character in source encoding
|
|
|
|
* but no related character(s) in destination encoding */
|
|
|
|
/* todo: add more alternative behaviors */
|
2008-09-05 23:03:21 +04:00
|
|
|
switch (ec->flags & ECONV_UNDEF_MASK) {
|
|
|
|
case ECONV_UNDEF_REPLACE:
|
2008-08-23 10:02:58 +04:00
|
|
|
if (output_replacement_character(ec) == 0)
|
|
|
|
goto resume;
|
2008-09-05 23:03:21 +04:00
|
|
|
break;
|
2008-09-06 00:24:18 +04:00
|
|
|
|
|
|
|
case ECONV_UNDEF_HEX_CHARREF:
|
|
|
|
if (output_hex_charref(ec) == 0)
|
|
|
|
goto resume;
|
|
|
|
break;
|
2008-09-05 23:03:21 +04:00
|
|
|
}
|
2008-08-23 10:02:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
const char *
|
|
|
|
rb_econv_encoding_to_insert_output(rb_econv_t *ec)
|
2008-08-14 19:56:39 +04:00
|
|
|
{
|
|
|
|
rb_transcoding *tc = ec->last_tc;
|
2008-08-22 20:44:00 +04:00
|
|
|
const rb_transcoder *tr;
|
|
|
|
|
|
|
|
if (tc == NULL)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
tr = tc->transcoder;
|
2008-08-14 19:56:39 +04:00
|
|
|
|
2008-09-08 18:33:17 +04:00
|
|
|
if (tr->asciicompat_type == asciicompat_encoder)
|
2008-09-04 19:46:18 +04:00
|
|
|
return tr->src_encoding;
|
|
|
|
return tr->dst_encoding;
|
2008-08-14 19:56:39 +04:00
|
|
|
}
|
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
static unsigned char *
|
2008-09-04 19:35:30 +04:00
|
|
|
allocate_converted_string(const char *sname, const char *dname,
|
2008-08-16 09:32:42 +04:00
|
|
|
const unsigned char *str, size_t len,
|
2008-09-05 22:20:20 +04:00
|
|
|
unsigned char *caller_dst_buf, size_t caller_dst_bufsize,
|
2008-08-16 09:32:42 +04:00
|
|
|
size_t *dst_len_ptr)
|
2008-08-15 15:02:07 +04:00
|
|
|
{
|
2008-08-16 09:32:42 +04:00
|
|
|
unsigned char *dst_str;
|
2008-08-15 18:17:11 +04:00
|
|
|
size_t dst_len;
|
2008-09-05 22:20:20 +04:00
|
|
|
size_t dst_bufsize;
|
2008-08-15 15:02:07 +04:00
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
rb_econv_t *ec;
|
|
|
|
rb_econv_result_t res;
|
2008-08-15 15:02:07 +04:00
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
const unsigned char *sp;
|
|
|
|
unsigned char *dp;
|
2008-08-15 15:02:07 +04:00
|
|
|
|
2008-09-05 22:20:20 +04:00
|
|
|
if (caller_dst_buf)
|
|
|
|
dst_bufsize = caller_dst_bufsize;
|
|
|
|
else if (len == 0)
|
|
|
|
dst_bufsize = 1;
|
|
|
|
else
|
|
|
|
dst_bufsize = len;
|
2008-08-15 18:17:11 +04:00
|
|
|
|
2008-09-04 19:35:30 +04:00
|
|
|
ec = rb_econv_open(sname, dname, 0);
|
2008-08-16 09:32:42 +04:00
|
|
|
if (ec == NULL)
|
|
|
|
return NULL;
|
2008-09-05 22:20:20 +04:00
|
|
|
if (caller_dst_buf)
|
|
|
|
dst_str = caller_dst_buf;
|
|
|
|
else
|
|
|
|
dst_str = xmalloc(dst_bufsize);
|
2008-08-16 09:32:42 +04:00
|
|
|
dst_len = 0;
|
|
|
|
sp = str;
|
|
|
|
dp = dst_str+dst_len;
|
|
|
|
res = rb_econv_convert(ec, &sp, str+len, &dp, dst_str+dst_bufsize, 0);
|
|
|
|
dst_len = dp - dst_str;
|
|
|
|
while (res == econv_destination_buffer_full) {
|
2008-09-06 13:39:12 +04:00
|
|
|
if (SIZE_MAX/2 < dst_bufsize) {
|
2008-09-05 22:20:20 +04:00
|
|
|
goto fail;
|
2008-08-16 09:32:42 +04:00
|
|
|
}
|
|
|
|
dst_bufsize *= 2;
|
2008-09-05 22:20:20 +04:00
|
|
|
if (dst_str == caller_dst_buf) {
|
|
|
|
unsigned char *tmp;
|
|
|
|
tmp = xmalloc(dst_bufsize);
|
|
|
|
memcpy(tmp, dst_str, dst_bufsize/2);
|
|
|
|
dst_str = tmp;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dst_str = xrealloc(dst_str, dst_bufsize);
|
|
|
|
}
|
2008-08-16 09:32:42 +04:00
|
|
|
dp = dst_str+dst_len;
|
|
|
|
res = rb_econv_convert(ec, &sp, str+len, &dp, dst_str+dst_bufsize, 0);
|
|
|
|
dst_len = dp - dst_str;
|
|
|
|
}
|
|
|
|
if (res != econv_finished) {
|
2008-09-05 22:20:20 +04:00
|
|
|
goto fail;
|
2008-08-16 09:32:42 +04:00
|
|
|
}
|
|
|
|
rb_econv_close(ec);
|
|
|
|
*dst_len_ptr = dst_len;
|
|
|
|
return dst_str;
|
2008-09-05 22:20:20 +04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (dst_str != caller_dst_buf)
|
|
|
|
xfree(dst_str);
|
|
|
|
rb_econv_close(ec);
|
|
|
|
return NULL;
|
2008-08-15 18:17:11 +04:00
|
|
|
}
|
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
/* result: 0:success -1:failure */
|
2008-08-15 18:17:11 +04:00
|
|
|
int
|
2009-02-22 17:23:33 +03:00
|
|
|
rb_econv_insert_output(rb_econv_t *ec,
|
2008-08-16 09:32:42 +04:00
|
|
|
const unsigned char *str, size_t len, const char *str_encoding)
|
2008-08-15 18:17:11 +04:00
|
|
|
{
|
2008-08-16 09:32:42 +04:00
|
|
|
const char *insert_encoding = rb_econv_encoding_to_insert_output(ec);
|
2008-09-05 22:20:20 +04:00
|
|
|
unsigned char insert_buf[4096];
|
|
|
|
const unsigned char *insert_str = NULL;
|
2008-08-16 09:32:42 +04:00
|
|
|
size_t insert_len;
|
2008-08-15 18:17:11 +04:00
|
|
|
|
2008-09-09 15:37:23 +04:00
|
|
|
int last_trans_index;
|
2008-08-16 09:32:42 +04:00
|
|
|
rb_transcoding *tc;
|
2008-08-15 18:17:11 +04:00
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
unsigned char **buf_start_p;
|
|
|
|
unsigned char **data_start_p;
|
|
|
|
unsigned char **data_end_p;
|
|
|
|
unsigned char **buf_end_p;
|
2008-08-15 18:17:11 +04:00
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
size_t need;
|
2008-08-15 18:17:11 +04:00
|
|
|
|
2008-09-12 16:23:25 +04:00
|
|
|
ec->started = 1;
|
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
2008-08-15 15:02:07 +04:00
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
if (encoding_equal(insert_encoding, str_encoding)) {
|
|
|
|
insert_str = str;
|
|
|
|
insert_len = len;
|
|
|
|
}
|
|
|
|
else {
|
2008-09-05 22:20:20 +04:00
|
|
|
insert_str = allocate_converted_string(str_encoding, insert_encoding,
|
|
|
|
str, len, insert_buf, sizeof(insert_buf), &insert_len);
|
2008-08-16 09:32:42 +04:00
|
|
|
if (insert_str == NULL)
|
|
|
|
return -1;
|
2008-08-15 15:02:07 +04:00
|
|
|
}
|
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
need = insert_len;
|
2008-08-22 20:44:00 +04:00
|
|
|
|
2008-09-09 15:37:23 +04:00
|
|
|
last_trans_index = ec->num_trans-1;
|
|
|
|
if (ec->num_trans == 0) {
|
|
|
|
tc = NULL;
|
2008-08-22 20:44:00 +04:00
|
|
|
buf_start_p = &ec->in_buf_start;
|
|
|
|
data_start_p = &ec->in_data_start;
|
|
|
|
data_end_p = &ec->in_data_end;
|
|
|
|
buf_end_p = &ec->in_buf_end;
|
|
|
|
}
|
2008-09-09 15:37:23 +04:00
|
|
|
else if (ec->elems[last_trans_index].tc->transcoder->asciicompat_type == asciicompat_encoder) {
|
|
|
|
tc = ec->elems[last_trans_index].tc;
|
2008-08-16 09:32:42 +04:00
|
|
|
need += tc->readagain_len;
|
|
|
|
if (need < insert_len)
|
|
|
|
goto fail;
|
2008-09-09 15:37:23 +04:00
|
|
|
if (last_trans_index == 0) {
|
2008-08-16 09:32:42 +04:00
|
|
|
buf_start_p = &ec->in_buf_start;
|
|
|
|
data_start_p = &ec->in_data_start;
|
|
|
|
data_end_p = &ec->in_data_end;
|
|
|
|
buf_end_p = &ec->in_buf_end;
|
|
|
|
}
|
|
|
|
else {
|
2008-09-09 15:37:23 +04:00
|
|
|
rb_econv_elem_t *ee = &ec->elems[last_trans_index-1];
|
2008-08-16 09:32:42 +04:00
|
|
|
buf_start_p = &ee->out_buf_start;
|
|
|
|
data_start_p = &ee->out_data_start;
|
|
|
|
data_end_p = &ee->out_data_end;
|
|
|
|
buf_end_p = &ee->out_buf_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-09-09 15:37:23 +04:00
|
|
|
rb_econv_elem_t *ee = &ec->elems[last_trans_index];
|
2008-08-16 09:32:42 +04:00
|
|
|
buf_start_p = &ee->out_buf_start;
|
|
|
|
data_start_p = &ee->out_data_start;
|
|
|
|
data_end_p = &ee->out_data_end;
|
|
|
|
buf_end_p = &ee->out_buf_end;
|
2008-09-09 15:37:23 +04:00
|
|
|
tc = ec->elems[last_trans_index].tc;
|
2008-08-16 09:32:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*buf_start_p == NULL) {
|
|
|
|
unsigned char *buf = xmalloc(need);
|
|
|
|
*buf_start_p = buf;
|
|
|
|
*data_start_p = buf;
|
|
|
|
*data_end_p = buf;
|
|
|
|
*buf_end_p = buf+need;
|
|
|
|
}
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
else if ((size_t)(*buf_end_p - *data_end_p) < need) {
|
2008-08-16 09:32:42 +04:00
|
|
|
MEMMOVE(*buf_start_p, *data_start_p, unsigned char, *data_end_p - *data_start_p);
|
|
|
|
*data_end_p = *buf_start_p + (*data_end_p - *data_start_p);
|
|
|
|
*data_start_p = *buf_start_p;
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
if ((size_t)(*buf_end_p - *data_end_p) < need) {
|
2008-08-16 09:32:42 +04:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t s = (*data_end_p - *buf_start_p) + need;
|
|
|
|
if (s < need)
|
|
|
|
goto fail;
|
|
|
|
buf = xrealloc(*buf_start_p, s);
|
|
|
|
*data_start_p = buf;
|
|
|
|
*data_end_p = buf + (*data_end_p - *buf_start_p);
|
|
|
|
*buf_start_p = buf;
|
|
|
|
*buf_end_p = buf + s;
|
|
|
|
}
|
|
|
|
}
|
2008-08-15 15:02:07 +04:00
|
|
|
|
2008-09-08 18:08:44 +04:00
|
|
|
memcpy(*data_end_p, insert_str, insert_len);
|
|
|
|
*data_end_p += insert_len;
|
2008-09-08 18:33:17 +04:00
|
|
|
if (tc && tc->transcoder->asciicompat_type == asciicompat_encoder) {
|
2008-08-16 09:32:42 +04:00
|
|
|
memcpy(*data_end_p, TRANSCODING_READBUF(tc)+tc->recognized_len, tc->readagain_len);
|
|
|
|
*data_end_p += tc->readagain_len;
|
|
|
|
tc->readagain_len = 0;
|
|
|
|
}
|
2008-08-15 15:02:07 +04:00
|
|
|
|
2008-09-05 22:20:20 +04:00
|
|
|
if (insert_str != str && insert_str != insert_buf)
|
2008-08-16 09:32:42 +04:00
|
|
|
xfree((void*)insert_str);
|
|
|
|
return 0;
|
2008-08-15 15:02:07 +04:00
|
|
|
|
2008-08-26 20:09:29 +04:00
|
|
|
fail:
|
2008-09-05 22:20:20 +04:00
|
|
|
if (insert_str != str && insert_str != insert_buf)
|
2008-08-16 09:32:42 +04:00
|
|
|
xfree((void*)insert_str);
|
|
|
|
return -1;
|
2008-08-15 15:02:07 +04:00
|
|
|
}
|
|
|
|
|
2008-08-14 18:28:10 +04:00
|
|
|
void
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_econv_close(rb_econv_t *ec)
|
2008-08-11 19:50:42 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-09-03 20:34:11 +04:00
|
|
|
if (ec->replacement_allocated) {
|
|
|
|
xfree((void *)ec->replacement_str);
|
|
|
|
}
|
2008-08-14 18:35:55 +04:00
|
|
|
for (i = 0; i < ec->num_trans; i++) {
|
|
|
|
rb_transcoding_close(ec->elems[i].tc);
|
|
|
|
if (ec->elems[i].out_buf_start)
|
|
|
|
xfree(ec->elems[i].out_buf_start);
|
2008-08-11 19:50:42 +04:00
|
|
|
}
|
2008-08-19 21:32:31 +04:00
|
|
|
xfree(ec->in_buf_start);
|
2008-08-14 18:35:55 +04:00
|
|
|
xfree(ec->elems);
|
|
|
|
xfree(ec);
|
2008-08-11 19:50:42 +04:00
|
|
|
}
|
|
|
|
|
2009-06-17 02:36:27 +04:00
|
|
|
size_t
|
|
|
|
rb_econv_memsize(rb_econv_t *ec)
|
|
|
|
{
|
|
|
|
size_t size = sizeof(rb_econv_t);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (ec->replacement_allocated) {
|
|
|
|
size += ec->replacement_len;
|
|
|
|
}
|
|
|
|
for (i = 0; i < ec->num_trans; i++) {
|
|
|
|
size += rb_transcoding_memsize(ec->elems[i].tc);
|
|
|
|
|
|
|
|
if (ec->elems[i].out_buf_start) {
|
|
|
|
size += ec->elems[i].out_buf_end - ec->elems[i].out_buf_start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size += ec->in_buf_end - ec->in_buf_start;
|
|
|
|
size += sizeof(rb_econv_elem_t) * ec->num_allocated;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2008-08-17 08:25:56 +04:00
|
|
|
int
|
|
|
|
rb_econv_putbackable(rb_econv_t *ec)
|
|
|
|
{
|
2008-08-25 17:04:16 +04:00
|
|
|
if (ec->num_trans == 0)
|
|
|
|
return 0;
|
2009-10-10 18:51:25 +04:00
|
|
|
#if SIZEOF_SIZE_T > SIZEOF_INT
|
|
|
|
if (ec->elems[0].tc->readagain_len > INT_MAX) return INT_MAX;
|
|
|
|
#endif
|
|
|
|
return (int)ec->elems[0].tc->readagain_len;
|
2008-08-17 08:25:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_econv_putback(rb_econv_t *ec, unsigned char *p, int n)
|
|
|
|
{
|
2008-08-25 17:04:16 +04:00
|
|
|
rb_transcoding *tc;
|
|
|
|
if (ec->num_trans == 0 || n == 0)
|
|
|
|
return;
|
|
|
|
tc = ec->elems[0].tc;
|
2008-08-30 23:36:38 +04:00
|
|
|
memcpy(p, TRANSCODING_READBUF(tc) + tc->recognized_len + tc->readagain_len - n, n);
|
2008-08-17 08:25:56 +04:00
|
|
|
tc->readagain_len -= n;
|
|
|
|
}
|
|
|
|
|
2008-09-08 18:33:17 +04:00
|
|
|
struct asciicompat_encoding_t {
|
|
|
|
const char *ascii_compat_name;
|
|
|
|
const char *ascii_incompat_name;
|
2008-08-18 16:06:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2008-09-08 18:33:17 +04:00
|
|
|
asciicompat_encoding_i(st_data_t key, st_data_t val, st_data_t arg)
|
2008-08-18 16:06:42 +04:00
|
|
|
{
|
2008-09-08 18:33:17 +04:00
|
|
|
struct asciicompat_encoding_t *data = (struct asciicompat_encoding_t *)arg;
|
2008-09-08 19:21:05 +04:00
|
|
|
transcoder_entry_t *entry = (transcoder_entry_t *)val;
|
|
|
|
const rb_transcoder *tr;
|
2008-08-18 16:06:42 +04:00
|
|
|
|
2008-09-13 13:21:34 +04:00
|
|
|
if (DECORATOR_P(entry->sname, entry->dname))
|
2008-09-08 19:21:05 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
tr = load_transcoder_entry(entry);
|
|
|
|
if (tr && tr->asciicompat_type == asciicompat_decoder) {
|
|
|
|
data->ascii_compat_name = tr->dst_encoding;
|
|
|
|
return ST_STOP;
|
2008-08-18 16:06:42 +04:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2008-09-08 18:33:17 +04:00
|
|
|
rb_econv_asciicompat_encoding(const char *ascii_incompat_name)
|
|
|
|
{
|
2008-09-08 19:21:05 +04:00
|
|
|
st_data_t v;
|
|
|
|
st_table *table2;
|
2008-09-08 18:33:17 +04:00
|
|
|
struct asciicompat_encoding_t data;
|
2008-09-08 19:21:05 +04:00
|
|
|
|
|
|
|
if (!st_lookup(transcoder_table, (st_data_t)ascii_incompat_name, &v))
|
|
|
|
return NULL;
|
|
|
|
table2 = (st_table *)v;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assumption:
|
2008-12-30 12:03:04 +03:00
|
|
|
* There is at most one transcoder for
|
2008-09-08 19:21:05 +04:00
|
|
|
* converting from ASCII incompatible encoding.
|
|
|
|
*
|
|
|
|
* For ISO-2022-JP, there is ISO-2022-JP -> stateless-ISO-2022-JP and no others.
|
|
|
|
*/
|
|
|
|
if (table2->num_entries != 1)
|
|
|
|
return NULL;
|
|
|
|
|
2008-09-08 18:33:17 +04:00
|
|
|
data.ascii_incompat_name = ascii_incompat_name;
|
|
|
|
data.ascii_compat_name = NULL;
|
2008-09-08 19:21:05 +04:00
|
|
|
st_foreach(table2, asciicompat_encoding_i, (st_data_t)&data);
|
|
|
|
return data.ascii_compat_name;
|
2008-08-18 16:06:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2013-09-06 06:45:50 +04:00
|
|
|
rb_econv_append(rb_econv_t *ec, const char *ss, long len, VALUE dst, int flags)
|
2008-08-18 16:06:42 +04:00
|
|
|
{
|
2013-09-06 06:45:50 +04:00
|
|
|
unsigned const char *sp, *se;
|
2008-08-18 16:06:42 +04:00
|
|
|
unsigned char *ds, *dp, *de;
|
|
|
|
rb_econv_result_t res;
|
2008-08-22 20:44:00 +04:00
|
|
|
int max_output;
|
2008-08-18 16:06:42 +04:00
|
|
|
|
2008-08-24 10:44:21 +04:00
|
|
|
if (NIL_P(dst)) {
|
2008-08-18 16:06:42 +04:00
|
|
|
dst = rb_str_buf_new(len);
|
2008-08-24 10:44:21 +04:00
|
|
|
if (ec->destination_encoding)
|
|
|
|
rb_enc_associate(dst, ec->destination_encoding);
|
|
|
|
}
|
2008-08-18 16:06:42 +04:00
|
|
|
|
2008-08-22 20:44:00 +04:00
|
|
|
if (ec->last_tc)
|
|
|
|
max_output = ec->last_tc->transcoder->max_output;
|
|
|
|
else
|
|
|
|
max_output = 1;
|
|
|
|
|
2013-09-06 06:29:17 +04:00
|
|
|
do {
|
2008-08-18 16:06:42 +04:00
|
|
|
long dlen = RSTRING_LEN(dst);
|
|
|
|
if (rb_str_capacity(dst) - dlen < (size_t)len + max_output) {
|
|
|
|
unsigned long new_capa = (unsigned long)dlen + len + max_output;
|
|
|
|
if (LONG_MAX < new_capa)
|
|
|
|
rb_raise(rb_eArgError, "too long string");
|
|
|
|
rb_str_resize(dst, new_capa);
|
|
|
|
rb_str_set_len(dst, dlen);
|
|
|
|
}
|
2013-09-06 06:45:50 +04:00
|
|
|
sp = (const unsigned char *)ss;
|
|
|
|
se = sp + len;
|
2008-08-25 12:35:42 +04:00
|
|
|
ds = (unsigned char *)RSTRING_PTR(dst);
|
2008-08-18 16:06:42 +04:00
|
|
|
de = ds + rb_str_capacity(dst);
|
2008-08-25 12:35:42 +04:00
|
|
|
dp = ds += dlen;
|
2008-08-18 16:06:42 +04:00
|
|
|
res = rb_econv_convert(ec, &sp, se, &dp, de, flags);
|
2013-09-06 06:45:50 +04:00
|
|
|
len -= (const char *)sp - ss;
|
|
|
|
ss = (const char *)sp;
|
2008-08-18 16:06:42 +04:00
|
|
|
rb_str_set_len(dst, dlen + (dp - ds));
|
|
|
|
rb_econv_check_error(ec);
|
2013-09-06 06:29:17 +04:00
|
|
|
} while (res == econv_destination_buffer_full);
|
2008-08-18 16:06:42 +04:00
|
|
|
|
2013-09-06 06:45:50 +04:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_econv_substr_append(rb_econv_t *ec, VALUE src, long off, long len, VALUE dst, int flags)
|
|
|
|
{
|
|
|
|
src = rb_str_new_frozen(src);
|
|
|
|
dst = rb_econv_append(ec, RSTRING_PTR(src) + off, len, dst, flags);
|
2013-09-06 05:29:21 +04:00
|
|
|
RB_GC_GUARD(src);
|
2008-08-18 16:06:42 +04:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2008-08-24 07:22:43 +04:00
|
|
|
VALUE
|
|
|
|
rb_econv_str_append(rb_econv_t *ec, VALUE src, VALUE dst, int flags)
|
|
|
|
{
|
|
|
|
return rb_econv_substr_append(ec, src, 0, RSTRING_LEN(src), dst, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_econv_substr_convert(rb_econv_t *ec, VALUE src, long byteoff, long bytesize, int flags)
|
|
|
|
{
|
|
|
|
return rb_econv_substr_append(ec, src, byteoff, bytesize, Qnil, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_econv_str_convert(rb_econv_t *ec, VALUE src, int flags)
|
|
|
|
{
|
|
|
|
return rb_econv_substr_append(ec, src, 0, RSTRING_LEN(src), Qnil, flags);
|
|
|
|
}
|
|
|
|
|
2008-09-08 21:23:37 +04:00
|
|
|
static int
|
2008-09-12 16:58:23 +04:00
|
|
|
rb_econv_add_converter(rb_econv_t *ec, const char *sname, const char *dname, int n)
|
2008-09-08 21:23:37 +04:00
|
|
|
{
|
|
|
|
transcoder_entry_t *entry;
|
|
|
|
const rb_transcoder *tr;
|
|
|
|
|
2008-09-12 16:23:25 +04:00
|
|
|
if (ec->started != 0)
|
2008-09-08 21:23:37 +04:00
|
|
|
return -1;
|
|
|
|
|
2008-09-12 16:58:23 +04:00
|
|
|
entry = get_transcoder_entry(sname, dname);
|
2008-09-08 21:23:37 +04:00
|
|
|
if (!entry)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tr = load_transcoder_entry(entry);
|
2011-09-06 07:07:16 +04:00
|
|
|
if (!tr) return -1;
|
2008-09-08 21:23:37 +04:00
|
|
|
|
2008-09-12 16:58:23 +04:00
|
|
|
return rb_econv_add_transcoder_at(ec, tr, n);
|
|
|
|
}
|
2008-09-08 21:23:37 +04:00
|
|
|
|
2008-09-12 16:58:23 +04:00
|
|
|
static int
|
|
|
|
rb_econv_decorate_at(rb_econv_t *ec, const char *decorator_name, int n)
|
|
|
|
{
|
|
|
|
return rb_econv_add_converter(ec, "", decorator_name, n);
|
2008-09-08 21:23:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_econv_decorate_at_first(rb_econv_t *ec, const char *decorator_name)
|
|
|
|
{
|
|
|
|
const rb_transcoder *tr;
|
|
|
|
|
|
|
|
if (ec->num_trans == 0)
|
|
|
|
return rb_econv_decorate_at(ec, decorator_name, 0);
|
|
|
|
|
|
|
|
tr = ec->elems[0].tc->transcoder;
|
|
|
|
|
2008-09-13 13:21:34 +04:00
|
|
|
if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding) &&
|
2008-09-08 21:23:37 +04:00
|
|
|
tr->asciicompat_type == asciicompat_decoder)
|
|
|
|
return rb_econv_decorate_at(ec, decorator_name, 1);
|
|
|
|
|
|
|
|
return rb_econv_decorate_at(ec, decorator_name, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_econv_decorate_at_last(rb_econv_t *ec, const char *decorator_name)
|
|
|
|
{
|
|
|
|
const rb_transcoder *tr;
|
|
|
|
|
|
|
|
if (ec->num_trans == 0)
|
|
|
|
return rb_econv_decorate_at(ec, decorator_name, 0);
|
|
|
|
|
|
|
|
tr = ec->elems[ec->num_trans-1].tc->transcoder;
|
|
|
|
|
2008-09-13 13:21:34 +04:00
|
|
|
if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding) &&
|
2008-09-08 21:23:37 +04:00
|
|
|
tr->asciicompat_type == asciicompat_encoder)
|
|
|
|
return rb_econv_decorate_at(ec, decorator_name, ec->num_trans-1);
|
|
|
|
|
|
|
|
return rb_econv_decorate_at(ec, decorator_name, ec->num_trans);
|
|
|
|
}
|
|
|
|
|
2008-08-22 20:44:00 +04:00
|
|
|
void
|
|
|
|
rb_econv_binmode(rb_econv_t *ec)
|
|
|
|
{
|
2011-09-05 13:10:58 +04:00
|
|
|
const char *dname = 0;
|
2008-09-06 15:07:01 +04:00
|
|
|
|
2011-09-05 13:10:58 +04:00
|
|
|
switch (ec->flags & ECONV_NEWLINE_DECORATOR_MASK) {
|
|
|
|
case ECONV_UNIVERSAL_NEWLINE_DECORATOR:
|
|
|
|
dname = "universal_newline";
|
|
|
|
break;
|
|
|
|
case ECONV_CRLF_NEWLINE_DECORATOR:
|
|
|
|
dname = "crlf_newline";
|
|
|
|
break;
|
|
|
|
case ECONV_CR_NEWLINE_DECORATOR:
|
|
|
|
dname = "cr_newline";
|
|
|
|
break;
|
2008-09-06 15:07:01 +04:00
|
|
|
}
|
2009-02-22 17:23:33 +03:00
|
|
|
|
2011-11-30 15:03:22 +04:00
|
|
|
if (dname) {
|
|
|
|
const rb_transcoder *transcoder = get_transcoder_entry("", dname)->transcoder;
|
|
|
|
int num_trans = ec->num_trans;
|
|
|
|
int i, j = 0;
|
|
|
|
|
|
|
|
for (i=0; i < num_trans; i++) {
|
|
|
|
if (transcoder == ec->elems[i].tc->transcoder) {
|
|
|
|
rb_transcoding_close(ec->elems[i].tc);
|
|
|
|
xfree(ec->elems[i].out_buf_start);
|
|
|
|
ec->num_trans--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ec->elems[j++] = ec->elems[i];
|
|
|
|
}
|
2008-08-22 20:44:00 +04:00
|
|
|
}
|
2008-09-06 15:07:01 +04:00
|
|
|
|
2011-04-26 19:54:14 +04:00
|
|
|
ec->flags &= ~ECONV_NEWLINE_DECORATOR_MASK;
|
2008-08-22 20:44:00 +04:00
|
|
|
}
|
2008-08-18 16:06:42 +04:00
|
|
|
|
2008-08-25 19:01:36 +04:00
|
|
|
static VALUE
|
2008-09-04 19:35:30 +04:00
|
|
|
econv_description(const char *sname, const char *dname, int ecflags, VALUE mesg)
|
2008-08-24 06:42:37 +04:00
|
|
|
{
|
2008-08-25 19:01:36 +04:00
|
|
|
int has_description = 0;
|
|
|
|
|
|
|
|
if (NIL_P(mesg))
|
|
|
|
mesg = rb_str_new(NULL, 0);
|
|
|
|
|
2008-09-04 19:35:30 +04:00
|
|
|
if (*sname != '\0' || *dname != '\0') {
|
|
|
|
if (*sname == '\0')
|
|
|
|
rb_str_cat2(mesg, dname);
|
|
|
|
else if (*dname == '\0')
|
|
|
|
rb_str_cat2(mesg, sname);
|
2008-08-24 06:42:37 +04:00
|
|
|
else
|
2008-09-04 19:35:30 +04:00
|
|
|
rb_str_catf(mesg, "%s to %s", sname, dname);
|
2008-08-25 19:01:36 +04:00
|
|
|
has_description = 1;
|
2008-08-24 06:42:37 +04:00
|
|
|
}
|
2008-08-25 19:01:36 +04:00
|
|
|
|
2011-04-26 19:54:14 +04:00
|
|
|
if (ecflags & (ECONV_NEWLINE_DECORATOR_MASK|
|
2008-09-09 16:22:43 +04:00
|
|
|
ECONV_XML_TEXT_DECORATOR|
|
|
|
|
ECONV_XML_ATTR_CONTENT_DECORATOR|
|
|
|
|
ECONV_XML_ATTR_QUOTE_DECORATOR)) {
|
2008-08-25 05:42:36 +04:00
|
|
|
const char *pre = "";
|
2008-08-25 19:01:36 +04:00
|
|
|
if (has_description)
|
2008-08-24 06:42:37 +04:00
|
|
|
rb_str_cat2(mesg, " with ");
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_UNIVERSAL_NEWLINE_DECORATOR) {
|
2008-08-24 06:42:37 +04:00
|
|
|
rb_str_cat2(mesg, pre); pre = ",";
|
2008-09-12 21:30:07 +04:00
|
|
|
rb_str_cat2(mesg, "universal_newline");
|
2008-08-24 06:42:37 +04:00
|
|
|
}
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_CRLF_NEWLINE_DECORATOR) {
|
2008-08-24 06:42:37 +04:00
|
|
|
rb_str_cat2(mesg, pre); pre = ",";
|
2008-09-12 21:30:07 +04:00
|
|
|
rb_str_cat2(mesg, "crlf_newline");
|
2008-08-24 06:42:37 +04:00
|
|
|
}
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_CR_NEWLINE_DECORATOR) {
|
2008-08-24 06:42:37 +04:00
|
|
|
rb_str_cat2(mesg, pre); pre = ",";
|
2008-09-12 21:30:07 +04:00
|
|
|
rb_str_cat2(mesg, "cr_newline");
|
2008-08-24 06:42:37 +04:00
|
|
|
}
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_XML_TEXT_DECORATOR) {
|
2008-09-06 15:11:21 +04:00
|
|
|
rb_str_cat2(mesg, pre); pre = ",";
|
2008-09-12 21:30:07 +04:00
|
|
|
rb_str_cat2(mesg, "xml_text");
|
2008-09-06 15:11:21 +04:00
|
|
|
}
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_XML_ATTR_CONTENT_DECORATOR) {
|
2008-09-07 07:13:29 +04:00
|
|
|
rb_str_cat2(mesg, pre); pre = ",";
|
2008-09-12 21:30:07 +04:00
|
|
|
rb_str_cat2(mesg, "xml_attr_content");
|
2008-09-07 07:13:29 +04:00
|
|
|
}
|
2008-09-09 16:22:43 +04:00
|
|
|
if (ecflags & ECONV_XML_ATTR_QUOTE_DECORATOR) {
|
2008-09-06 15:11:21 +04:00
|
|
|
rb_str_cat2(mesg, pre); pre = ",";
|
2008-09-12 21:30:07 +04:00
|
|
|
rb_str_cat2(mesg, "xml_attr_quote");
|
2008-09-06 15:11:21 +04:00
|
|
|
}
|
2008-08-25 19:01:36 +04:00
|
|
|
has_description = 1;
|
2008-08-24 06:42:37 +04:00
|
|
|
}
|
2008-08-25 19:01:36 +04:00
|
|
|
if (!has_description) {
|
|
|
|
rb_str_cat2(mesg, "no-conversion");
|
|
|
|
}
|
|
|
|
|
|
|
|
return mesg;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2008-09-04 19:35:30 +04:00
|
|
|
rb_econv_open_exc(const char *sname, const char *dname, int ecflags)
|
2008-08-25 19:01:36 +04:00
|
|
|
{
|
|
|
|
VALUE mesg, exc;
|
2008-09-12 17:53:42 +04:00
|
|
|
mesg = rb_str_new_cstr("code converter not found (");
|
2008-09-04 19:35:30 +04:00
|
|
|
econv_description(sname, dname, ecflags, mesg);
|
2008-08-24 06:42:37 +04:00
|
|
|
rb_str_cat2(mesg, ")");
|
2008-09-25 16:47:30 +04:00
|
|
|
exc = rb_exc_new3(rb_eConverterNotFoundError, mesg);
|
2008-08-24 06:42:37 +04:00
|
|
|
return exc;
|
|
|
|
}
|
|
|
|
|
2008-08-16 19:04:39 +04:00
|
|
|
static VALUE
|
|
|
|
make_econv_exception(rb_econv_t *ec)
|
|
|
|
{
|
2008-08-17 07:05:40 +04:00
|
|
|
VALUE mesg, exc;
|
2008-08-26 20:09:29 +04:00
|
|
|
if (ec->last_error.result == econv_invalid_byte_sequence ||
|
|
|
|
ec->last_error.result == econv_incomplete_input) {
|
2008-08-25 19:26:54 +04:00
|
|
|
const char *err = (const char *)ec->last_error.error_bytes_start;
|
|
|
|
size_t error_len = ec->last_error.error_bytes_len;
|
|
|
|
VALUE bytes = rb_str_new(err, error_len);
|
|
|
|
VALUE dumped = rb_str_dump(bytes);
|
|
|
|
size_t readagain_len = ec->last_error.readagain_len;
|
|
|
|
VALUE bytes2 = Qnil;
|
|
|
|
VALUE dumped2;
|
2008-08-31 13:08:31 +04:00
|
|
|
int idx;
|
2008-08-26 20:09:29 +04:00
|
|
|
if (ec->last_error.result == econv_incomplete_input) {
|
2008-08-31 09:12:32 +04:00
|
|
|
mesg = rb_sprintf("incomplete %s on %s",
|
2008-08-26 20:09:29 +04:00
|
|
|
StringValueCStr(dumped),
|
|
|
|
ec->last_error.source_encoding);
|
|
|
|
}
|
|
|
|
else if (readagain_len) {
|
2008-08-25 19:26:54 +04:00
|
|
|
bytes2 = rb_str_new(err+error_len, readagain_len);
|
|
|
|
dumped2 = rb_str_dump(bytes2);
|
2008-08-31 09:12:32 +04:00
|
|
|
mesg = rb_sprintf("%s followed by %s on %s",
|
2008-08-25 19:26:54 +04:00
|
|
|
StringValueCStr(dumped),
|
|
|
|
StringValueCStr(dumped2),
|
|
|
|
ec->last_error.source_encoding);
|
|
|
|
}
|
|
|
|
else {
|
2008-08-31 09:12:32 +04:00
|
|
|
mesg = rb_sprintf("%s on %s",
|
2008-08-25 19:26:54 +04:00
|
|
|
StringValueCStr(dumped),
|
|
|
|
ec->last_error.source_encoding);
|
|
|
|
}
|
|
|
|
|
2008-09-18 12:57:33 +04:00
|
|
|
exc = rb_exc_new3(rb_eInvalidByteSequenceError, mesg);
|
2008-08-17 07:05:40 +04:00
|
|
|
rb_ivar_set(exc, rb_intern("error_bytes"), bytes);
|
2008-08-25 19:26:54 +04:00
|
|
|
rb_ivar_set(exc, rb_intern("readagain_bytes"), bytes2);
|
2008-08-26 20:09:29 +04:00
|
|
|
rb_ivar_set(exc, rb_intern("incomplete_input"), ec->last_error.result == econv_incomplete_input ? Qtrue : Qfalse);
|
2008-08-31 13:08:31 +04:00
|
|
|
|
|
|
|
set_encs:
|
|
|
|
rb_ivar_set(exc, rb_intern("source_encoding_name"), rb_str_new2(ec->last_error.source_encoding));
|
|
|
|
rb_ivar_set(exc, rb_intern("destination_encoding_name"), rb_str_new2(ec->last_error.destination_encoding));
|
|
|
|
idx = rb_enc_find_index(ec->last_error.source_encoding);
|
|
|
|
if (0 <= idx)
|
|
|
|
rb_ivar_set(exc, rb_intern("source_encoding"), rb_enc_from_encoding(rb_enc_from_index(idx)));
|
|
|
|
idx = rb_enc_find_index(ec->last_error.destination_encoding);
|
|
|
|
if (0 <= idx)
|
|
|
|
rb_ivar_set(exc, rb_intern("destination_encoding"), rb_enc_from_encoding(rb_enc_from_index(idx)));
|
2008-08-17 07:05:40 +04:00
|
|
|
return exc;
|
2008-08-16 19:04:39 +04:00
|
|
|
}
|
|
|
|
if (ec->last_error.result == econv_undefined_conversion) {
|
|
|
|
VALUE bytes = rb_str_new((const char *)ec->last_error.error_bytes_start,
|
|
|
|
ec->last_error.error_bytes_len);
|
2009-02-04 16:01:12 +03:00
|
|
|
VALUE dumped = Qnil;
|
2008-08-17 07:05:40 +04:00
|
|
|
int idx;
|
2009-02-04 16:01:12 +03:00
|
|
|
if (strcmp(ec->last_error.source_encoding, "UTF-8") == 0) {
|
|
|
|
rb_encoding *utf8 = rb_utf8_encoding();
|
|
|
|
const char *start, *end;
|
|
|
|
int n;
|
|
|
|
start = (const char *)ec->last_error.error_bytes_start;
|
|
|
|
end = start + ec->last_error.error_bytes_len;
|
|
|
|
n = rb_enc_precise_mbclen(start, end, utf8);
|
|
|
|
if (MBCLEN_CHARFOUND_P(n) &&
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
(size_t)MBCLEN_CHARFOUND_LEN(n) == ec->last_error.error_bytes_len) {
|
* encoding.c (rb_enc_codepoint_len): combine rb_enc_codepoint()
and rb_enc_codelen() in one function to reduce calls.
* encoding.c (rb_enc_codepoint): compatibility function.
* sprintf.c (rb_str_format): use rb_enc_codepoint_len().
* string.c (rb_str_inspect, rb_str_upcase_bang,
rb_str_downcase_bang, rb_str_capitalize_bang,
rb_str_swapcase_bang, trnext, tr_trans, rb_str_delete_bang,
rb_str_squeeze_bang, rb_str_count, rb_str_split_m,
rb_str_each_line, rb_str_each_codepoint, rb_str_lstrip_bang,
sym_printable): ditto.
* transcode.c (make_econv_exception): use rb_enc_mbc_to_codepoint()
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-05-19 20:59:22 +04:00
|
|
|
unsigned int cc = rb_enc_mbc_to_codepoint(start, end, utf8);
|
2009-02-04 16:01:12 +03:00
|
|
|
dumped = rb_sprintf("U+%04X", cc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dumped == Qnil)
|
|
|
|
dumped = rb_str_dump(bytes);
|
2008-12-27 09:28:34 +03:00
|
|
|
if (strcmp(ec->last_error.source_encoding,
|
2009-02-04 19:45:31 +03:00
|
|
|
ec->source_encoding_name) == 0 &&
|
2008-12-27 09:28:34 +03:00
|
|
|
strcmp(ec->last_error.destination_encoding,
|
2009-02-04 19:45:31 +03:00
|
|
|
ec->destination_encoding_name) == 0) {
|
|
|
|
mesg = rb_sprintf("%s from %s to %s",
|
|
|
|
StringValueCStr(dumped),
|
|
|
|
ec->last_error.source_encoding,
|
|
|
|
ec->last_error.destination_encoding);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
mesg = rb_sprintf("%s to %s in conversion from %s",
|
|
|
|
StringValueCStr(dumped),
|
|
|
|
ec->last_error.destination_encoding,
|
|
|
|
ec->source_encoding_name);
|
|
|
|
for (i = 0; i < ec->num_trans; i++) {
|
|
|
|
const rb_transcoder *tr = ec->elems[i].tc->transcoder;
|
|
|
|
if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding))
|
|
|
|
rb_str_catf(mesg, " to %s",
|
|
|
|
ec->elems[i].tc->transcoder->dst_encoding);
|
|
|
|
}
|
2008-12-27 09:28:34 +03:00
|
|
|
}
|
2008-09-25 16:47:30 +04:00
|
|
|
exc = rb_exc_new3(rb_eUndefinedConversionError, mesg);
|
2008-08-17 07:05:40 +04:00
|
|
|
idx = rb_enc_find_index(ec->last_error.source_encoding);
|
|
|
|
if (0 <= idx)
|
|
|
|
rb_enc_associate_index(bytes, idx);
|
|
|
|
rb_ivar_set(exc, rb_intern("error_char"), bytes);
|
2008-08-31 13:08:31 +04:00
|
|
|
goto set_encs;
|
2008-08-16 19:04:39 +04:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2008-08-09 10:02:01 +04:00
|
|
|
static void
|
|
|
|
more_output_buffer(
|
2008-08-09 17:34:21 +04:00
|
|
|
VALUE destination,
|
2009-10-10 18:51:25 +04:00
|
|
|
unsigned char *(*resize_destination)(VALUE, size_t, size_t),
|
2008-08-13 09:30:42 +04:00
|
|
|
int max_output,
|
2008-08-09 10:02:01 +04:00
|
|
|
unsigned char **out_start_ptr,
|
|
|
|
unsigned char **out_pos,
|
|
|
|
unsigned char **out_stop_ptr)
|
|
|
|
{
|
|
|
|
size_t len = (*out_pos - *out_start_ptr);
|
2008-08-13 09:30:42 +04:00
|
|
|
size_t new_len = (len + max_output) * 2;
|
2008-08-09 17:34:21 +04:00
|
|
|
*out_start_ptr = resize_destination(destination, len, new_len);
|
2008-08-09 10:02:01 +04:00
|
|
|
*out_pos = *out_start_ptr + len;
|
|
|
|
*out_stop_ptr = *out_start_ptr + new_len;
|
|
|
|
}
|
|
|
|
|
2008-08-14 19:56:39 +04:00
|
|
|
static int
|
2008-09-03 20:34:11 +04:00
|
|
|
make_replacement(rb_econv_t *ec)
|
2008-08-12 02:44:23 +04:00
|
|
|
{
|
2008-08-22 20:44:00 +04:00
|
|
|
rb_transcoding *tc;
|
2008-08-12 02:44:23 +04:00
|
|
|
const rb_transcoder *tr;
|
2008-08-14 19:56:39 +04:00
|
|
|
const unsigned char *replacement;
|
2008-08-15 15:02:07 +04:00
|
|
|
const char *repl_enc;
|
2008-09-03 20:34:11 +04:00
|
|
|
const char *ins_enc;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (ec->replacement_str)
|
|
|
|
return 0;
|
2008-08-12 02:44:23 +04:00
|
|
|
|
2008-09-09 20:18:29 +04:00
|
|
|
ins_enc = rb_econv_encoding_to_insert_output(ec);
|
|
|
|
|
2008-08-22 20:44:00 +04:00
|
|
|
tc = ec->last_tc;
|
2008-09-09 20:18:29 +04:00
|
|
|
if (*ins_enc) {
|
2008-08-22 20:44:00 +04:00
|
|
|
tr = tc->transcoder;
|
2011-12-05 13:50:12 +04:00
|
|
|
rb_enc_find(tr->dst_encoding);
|
2008-09-09 20:18:29 +04:00
|
|
|
replacement = (const unsigned char *)get_replacement_character(ins_enc, &len, &repl_enc);
|
2008-08-22 20:44:00 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
replacement = (unsigned char *)"?";
|
|
|
|
len = 1;
|
|
|
|
repl_enc = "";
|
|
|
|
}
|
2008-08-12 02:44:23 +04:00
|
|
|
|
2008-09-03 20:34:11 +04:00
|
|
|
ec->replacement_str = replacement;
|
|
|
|
ec->replacement_len = len;
|
|
|
|
ec->replacement_enc = repl_enc;
|
2008-09-09 20:18:29 +04:00
|
|
|
ec->replacement_allocated = 0;
|
2008-09-03 20:34:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-07 21:08:38 +04:00
|
|
|
rb_econv_set_replacement(rb_econv_t *ec,
|
2008-09-03 20:34:11 +04:00
|
|
|
const unsigned char *str, size_t len, const char *encname)
|
|
|
|
{
|
|
|
|
unsigned char *str2;
|
|
|
|
size_t len2;
|
|
|
|
const char *encname2;
|
|
|
|
|
|
|
|
encname2 = rb_econv_encoding_to_insert_output(ec);
|
|
|
|
|
2015-07-03 06:25:21 +03:00
|
|
|
if (!*encname2 || encoding_equal(encname, encname2)) {
|
2008-09-03 20:34:11 +04:00
|
|
|
str2 = xmalloc(len);
|
|
|
|
MEMCPY(str2, str, unsigned char, len); /* xxx: str may be invalid */
|
|
|
|
len2 = len;
|
|
|
|
encname2 = encname;
|
|
|
|
}
|
|
|
|
else {
|
2008-09-05 22:20:20 +04:00
|
|
|
str2 = allocate_converted_string(encname, encname2, str, len, NULL, 0, &len2);
|
2008-09-03 20:34:11 +04:00
|
|
|
if (!str2)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ec->replacement_allocated) {
|
|
|
|
xfree((void *)ec->replacement_str);
|
|
|
|
}
|
|
|
|
ec->replacement_allocated = 1;
|
|
|
|
ec->replacement_str = str2;
|
|
|
|
ec->replacement_len = len2;
|
|
|
|
ec->replacement_enc = encname2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
output_replacement_character(rb_econv_t *ec)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (make_replacement(ec) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = rb_econv_insert_output(ec, ec->replacement_str, ec->replacement_len, ec->replacement_enc);
|
2008-08-16 09:32:42 +04:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
2008-08-12 02:44:23 +04:00
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
return 0;
|
2008-08-12 02:44:23 +04:00
|
|
|
}
|
|
|
|
|
2008-08-09 10:02:01 +04:00
|
|
|
#if 1
|
2010-12-07 16:05:26 +03:00
|
|
|
#define hash_fallback rb_hash_aref
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
proc_fallback(VALUE fallback, VALUE c)
|
|
|
|
{
|
|
|
|
return rb_proc_call(fallback, rb_ary_new4(1, &c));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
method_fallback(VALUE fallback, VALUE c)
|
|
|
|
{
|
|
|
|
return rb_method_call(1, &c, fallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
aref_fallback(VALUE fallback, VALUE c)
|
|
|
|
{
|
|
|
|
return rb_funcall3(fallback, sym_aref, 1, &c);
|
|
|
|
}
|
|
|
|
|
2008-08-09 10:02:01 +04:00
|
|
|
static void
|
|
|
|
transcode_loop(const unsigned char **in_pos, unsigned char **out_pos,
|
|
|
|
const unsigned char *in_stop, unsigned char *out_stop,
|
2008-08-09 17:34:21 +04:00
|
|
|
VALUE destination,
|
2009-10-10 18:51:25 +04:00
|
|
|
unsigned char *(*resize_destination)(VALUE, size_t, size_t),
|
2008-09-04 19:46:18 +04:00
|
|
|
const char *src_encoding,
|
|
|
|
const char *dst_encoding,
|
2008-09-03 22:18:10 +04:00
|
|
|
int ecflags,
|
|
|
|
VALUE ecopts)
|
2008-08-09 10:02:01 +04:00
|
|
|
{
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_econv_t *ec;
|
2008-08-13 09:30:42 +04:00
|
|
|
rb_transcoding *last_tc;
|
2008-08-14 15:31:27 +04:00
|
|
|
rb_econv_result_t ret;
|
2008-08-09 10:02:01 +04:00
|
|
|
unsigned char *out_start = *out_pos;
|
2008-08-11 19:50:42 +04:00
|
|
|
int max_output;
|
2008-08-16 19:04:39 +04:00
|
|
|
VALUE exc;
|
2010-04-13 05:26:46 +04:00
|
|
|
VALUE fallback = Qnil;
|
2010-12-07 16:05:26 +03:00
|
|
|
VALUE (*fallback_func)(VALUE, VALUE) = 0;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
2008-09-04 19:46:18 +04:00
|
|
|
ec = rb_econv_open_opts(src_encoding, dst_encoding, ecflags, ecopts);
|
2008-08-14 18:35:55 +04:00
|
|
|
if (!ec)
|
2008-09-04 19:46:18 +04:00
|
|
|
rb_exc_raise(rb_econv_open_exc(src_encoding, dst_encoding, ecflags));
|
2008-08-11 19:50:42 +04:00
|
|
|
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!NIL_P(ecopts) && RB_TYPE_P(ecopts, T_HASH)) {
|
2010-04-13 05:26:46 +04:00
|
|
|
fallback = rb_hash_aref(ecopts, sym_fallback);
|
2010-12-07 16:05:26 +03:00
|
|
|
if (RB_TYPE_P(fallback, T_HASH)) {
|
|
|
|
fallback_func = hash_fallback;
|
|
|
|
}
|
|
|
|
else if (rb_obj_is_proc(fallback)) {
|
|
|
|
fallback_func = proc_fallback;
|
|
|
|
}
|
|
|
|
else if (rb_obj_is_method(fallback)) {
|
|
|
|
fallback_func = method_fallback;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fallback_func = aref_fallback;
|
|
|
|
}
|
|
|
|
}
|
2008-08-14 18:35:55 +04:00
|
|
|
last_tc = ec->last_tc;
|
2008-08-22 20:44:00 +04:00
|
|
|
max_output = last_tc ? last_tc->transcoder->max_output : 1;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
2008-08-26 20:09:29 +04:00
|
|
|
resume:
|
2008-08-24 12:39:09 +04:00
|
|
|
ret = rb_econv_convert(ec, in_pos, in_stop, out_pos, out_stop, 0);
|
2008-08-23 10:02:58 +04:00
|
|
|
|
2010-04-13 05:26:46 +04:00
|
|
|
if (!NIL_P(fallback) && ret == econv_undefined_conversion) {
|
|
|
|
VALUE rep = rb_enc_str_new(
|
|
|
|
(const char *)ec->last_error.error_bytes_start,
|
|
|
|
ec->last_error.error_bytes_len,
|
|
|
|
rb_enc_find(ec->last_error.source_encoding));
|
2010-12-07 16:05:26 +03:00
|
|
|
rep = (*fallback_func)(fallback, rep);
|
|
|
|
if (rep != Qundef && !NIL_P(rep)) {
|
2010-04-13 05:26:46 +04:00
|
|
|
StringValue(rep);
|
|
|
|
ret = rb_econv_insert_output(ec, (const unsigned char *)RSTRING_PTR(rep),
|
|
|
|
RSTRING_LEN(rep), rb_enc_name(rb_enc_get(rep)));
|
2010-04-18 07:28:12 +04:00
|
|
|
if ((int)ret == -1) {
|
2010-04-13 05:26:46 +04:00
|
|
|
rb_raise(rb_eArgError, "too big fallback string");
|
|
|
|
}
|
|
|
|
goto resume;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-26 20:09:29 +04:00
|
|
|
if (ret == econv_invalid_byte_sequence ||
|
2008-08-26 20:14:49 +04:00
|
|
|
ret == econv_incomplete_input ||
|
|
|
|
ret == econv_undefined_conversion) {
|
2008-08-16 19:04:39 +04:00
|
|
|
exc = make_econv_exception(ec);
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_econv_close(ec);
|
2008-08-16 19:04:39 +04:00
|
|
|
rb_exc_raise(exc);
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
2008-08-23 10:02:58 +04:00
|
|
|
|
2008-08-14 17:45:54 +04:00
|
|
|
if (ret == econv_destination_buffer_full) {
|
2008-08-13 09:30:42 +04:00
|
|
|
more_output_buffer(destination, resize_destination, max_output, &out_start, out_pos, &out_stop);
|
2008-08-09 10:02:01 +04:00
|
|
|
goto resume;
|
|
|
|
}
|
|
|
|
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_econv_close(ec);
|
2008-08-09 10:02:01 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* sample transcode_loop implementation in byte-by-byte stream style */
|
|
|
|
static void
|
|
|
|
transcode_loop(const unsigned char **in_pos, unsigned char **out_pos,
|
|
|
|
const unsigned char *in_stop, unsigned char *out_stop,
|
2008-08-09 17:34:21 +04:00
|
|
|
VALUE destination,
|
2009-10-10 18:51:25 +04:00
|
|
|
unsigned char *(*resize_destination)(VALUE, size_t, size_t),
|
2008-09-04 19:46:18 +04:00
|
|
|
const char *src_encoding,
|
|
|
|
const char *dst_encoding,
|
2008-09-03 22:18:10 +04:00
|
|
|
int ecflags,
|
|
|
|
VALUE ecopts)
|
2008-08-09 10:02:01 +04:00
|
|
|
{
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_econv_t *ec;
|
2008-08-13 09:30:42 +04:00
|
|
|
rb_transcoding *last_tc;
|
2008-08-14 15:31:27 +04:00
|
|
|
rb_econv_result_t ret;
|
2008-08-09 10:02:01 +04:00
|
|
|
unsigned char *out_start = *out_pos;
|
|
|
|
const unsigned char *ptr;
|
2008-08-11 19:50:42 +04:00
|
|
|
int max_output;
|
2008-08-16 19:04:39 +04:00
|
|
|
VALUE exc;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
2008-09-04 19:46:18 +04:00
|
|
|
ec = rb_econv_open_opts(src_encoding, dst_encoding, ecflags, ecopts);
|
2008-08-14 18:35:55 +04:00
|
|
|
if (!ec)
|
2008-09-04 19:46:18 +04:00
|
|
|
rb_exc_raise(rb_econv_open_exc(src_encoding, dst_encoding, ecflags));
|
2008-08-11 19:50:42 +04:00
|
|
|
|
2008-08-14 18:35:55 +04:00
|
|
|
last_tc = ec->last_tc;
|
2008-08-22 20:44:00 +04:00
|
|
|
max_output = last_tc ? last_tc->transcoder->max_output : 1;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
2008-08-14 17:45:54 +04:00
|
|
|
ret = econv_source_buffer_empty;
|
2008-08-09 10:02:01 +04:00
|
|
|
ptr = *in_pos;
|
2008-08-14 17:45:54 +04:00
|
|
|
while (ret != econv_finished) {
|
2008-08-09 10:02:01 +04:00
|
|
|
unsigned char input_byte;
|
|
|
|
const unsigned char *p = &input_byte;
|
|
|
|
|
2008-08-14 17:45:54 +04:00
|
|
|
if (ret == econv_source_buffer_empty) {
|
2008-08-09 10:02:01 +04:00
|
|
|
if (ptr < in_stop) {
|
|
|
|
input_byte = *ptr;
|
2008-08-14 18:48:41 +04:00
|
|
|
ret = rb_econv_convert(ec, &p, p+1, out_pos, out_stop, ECONV_PARTIAL_INPUT);
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-08-14 18:35:55 +04:00
|
|
|
ret = rb_econv_convert(ec, NULL, NULL, out_pos, out_stop, 0);
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-08-14 18:48:41 +04:00
|
|
|
ret = rb_econv_convert(ec, NULL, NULL, out_pos, out_stop, ECONV_PARTIAL_INPUT);
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
if (&input_byte != p)
|
|
|
|
ptr += p - &input_byte;
|
|
|
|
switch (ret) {
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_invalid_byte_sequence:
|
2008-08-26 20:09:29 +04:00
|
|
|
case econv_incomplete_input:
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_undefined_conversion:
|
2008-08-16 19:04:39 +04:00
|
|
|
exc = make_econv_exception(ec);
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_econv_close(ec);
|
2008-08-16 19:04:39 +04:00
|
|
|
rb_exc_raise(exc);
|
2008-08-09 10:02:01 +04:00
|
|
|
break;
|
|
|
|
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_destination_buffer_full:
|
2008-08-13 09:30:42 +04:00
|
|
|
more_output_buffer(destination, resize_destination, max_output, &out_start, out_pos, &out_stop);
|
2008-08-09 10:02:01 +04:00
|
|
|
break;
|
|
|
|
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_source_buffer_empty:
|
2008-08-09 10:02:01 +04:00
|
|
|
break;
|
|
|
|
|
2008-08-14 17:45:54 +04:00
|
|
|
case econv_finished:
|
2008-08-09 10:02:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-08-07 18:53:30 +04:00
|
|
|
}
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_econv_close(ec);
|
2008-08-09 10:02:01 +04:00
|
|
|
*in_pos = in_stop;
|
|
|
|
return;
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
2008-08-09 10:02:01 +04:00
|
|
|
#endif
|
2007-12-10 08:01:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* String-specific code
|
|
|
|
*/
|
|
|
|
|
2008-01-21 06:35:05 +03:00
|
|
|
static unsigned char *
|
2009-10-10 18:51:25 +04:00
|
|
|
str_transcoding_resize(VALUE destination, size_t len, size_t new_len)
|
2007-12-10 11:46:06 +03:00
|
|
|
{
|
2008-08-09 17:34:21 +04:00
|
|
|
rb_str_resize(destination, new_len);
|
|
|
|
return (unsigned char *)RSTRING_PTR(destination);
|
2007-12-10 11:46:06 +03:00
|
|
|
}
|
|
|
|
|
2007-12-18 11:04:26 +03:00
|
|
|
static int
|
2011-04-26 19:55:21 +04:00
|
|
|
econv_opts(VALUE opt, int ecflags)
|
2008-08-24 10:25:24 +04:00
|
|
|
{
|
|
|
|
VALUE v;
|
2020-01-11 04:19:29 +03:00
|
|
|
int newlineflag = 0;
|
2008-09-04 16:08:54 +04:00
|
|
|
|
2008-08-24 10:25:24 +04:00
|
|
|
v = rb_hash_aref(opt, sym_invalid);
|
|
|
|
if (NIL_P(v)) {
|
|
|
|
}
|
|
|
|
else if (v==sym_replace) {
|
2008-09-04 16:08:54 +04:00
|
|
|
ecflags |= ECONV_INVALID_REPLACE;
|
2008-08-24 10:25:24 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "unknown value for invalid character option");
|
|
|
|
}
|
2008-09-04 16:08:54 +04:00
|
|
|
|
2008-08-24 10:25:24 +04:00
|
|
|
v = rb_hash_aref(opt, sym_undef);
|
|
|
|
if (NIL_P(v)) {
|
|
|
|
}
|
|
|
|
else if (v==sym_replace) {
|
2008-09-04 16:08:54 +04:00
|
|
|
ecflags |= ECONV_UNDEF_REPLACE;
|
2008-08-24 10:25:24 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "unknown value for undefined character option");
|
|
|
|
}
|
2008-09-04 16:08:54 +04:00
|
|
|
|
2010-03-05 13:39:56 +03:00
|
|
|
v = rb_hash_aref(opt, sym_replace);
|
|
|
|
if (!NIL_P(v) && !(ecflags & ECONV_INVALID_REPLACE)) {
|
|
|
|
ecflags |= ECONV_UNDEF_REPLACE;
|
|
|
|
}
|
|
|
|
|
2008-09-06 18:05:10 +04:00
|
|
|
v = rb_hash_aref(opt, sym_xml);
|
2008-09-06 10:07:34 +04:00
|
|
|
if (!NIL_P(v)) {
|
|
|
|
if (v==sym_text) {
|
2008-09-09 16:22:43 +04:00
|
|
|
ecflags |= ECONV_XML_TEXT_DECORATOR|ECONV_UNDEF_HEX_CHARREF;
|
2008-09-06 10:07:34 +04:00
|
|
|
}
|
|
|
|
else if (v==sym_attr) {
|
2008-09-09 16:22:43 +04:00
|
|
|
ecflags |= ECONV_XML_ATTR_CONTENT_DECORATOR|ECONV_XML_ATTR_QUOTE_DECORATOR|ECONV_UNDEF_HEX_CHARREF;
|
2008-09-06 10:07:34 +04:00
|
|
|
}
|
2011-09-29 15:07:45 +04:00
|
|
|
else if (RB_TYPE_P(v, T_SYMBOL)) {
|
2014-07-30 17:17:45 +04:00
|
|
|
rb_raise(rb_eArgError, "unexpected value for xml option: %"PRIsVALUE, rb_sym2str(v));
|
2008-09-06 10:07:34 +04:00
|
|
|
}
|
2008-09-15 18:38:12 +04:00
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "unexpected value for xml option");
|
|
|
|
}
|
2008-09-06 10:07:34 +04:00
|
|
|
}
|
|
|
|
|
2011-04-26 19:55:58 +04:00
|
|
|
#ifdef ENABLE_ECONV_NEWLINE_OPTION
|
|
|
|
v = rb_hash_aref(opt, sym_newline);
|
|
|
|
if (!NIL_P(v)) {
|
2020-01-11 04:19:29 +03:00
|
|
|
newlineflag = 2;
|
2011-04-26 19:55:58 +04:00
|
|
|
ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
|
|
|
|
if (v == sym_universal) {
|
|
|
|
ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
|
|
|
|
}
|
|
|
|
else if (v == sym_crlf) {
|
|
|
|
ecflags |= ECONV_CRLF_NEWLINE_DECORATOR;
|
|
|
|
}
|
|
|
|
else if (v == sym_cr) {
|
|
|
|
ecflags |= ECONV_CR_NEWLINE_DECORATOR;
|
|
|
|
}
|
|
|
|
else if (v == sym_lf) {
|
|
|
|
/* ecflags |= ECONV_LF_NEWLINE_DECORATOR; */
|
|
|
|
}
|
|
|
|
else if (SYMBOL_P(v)) {
|
2014-08-03 05:55:32 +04:00
|
|
|
rb_raise(rb_eArgError, "unexpected value for newline option: %"PRIsVALUE,
|
|
|
|
rb_sym2str(v));
|
2011-04-26 19:55:58 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "unexpected value for newline option");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-04-26 19:55:21 +04:00
|
|
|
{
|
2020-01-11 04:19:29 +03:00
|
|
|
int setflags = 0;
|
2008-09-04 16:08:54 +04:00
|
|
|
|
2011-04-26 19:55:21 +04:00
|
|
|
v = rb_hash_aref(opt, sym_universal_newline);
|
|
|
|
if (RTEST(v))
|
|
|
|
setflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
|
|
|
|
newlineflag |= !NIL_P(v);
|
2008-09-04 16:08:54 +04:00
|
|
|
|
2011-04-26 19:55:21 +04:00
|
|
|
v = rb_hash_aref(opt, sym_crlf_newline);
|
|
|
|
if (RTEST(v))
|
|
|
|
setflags |= ECONV_CRLF_NEWLINE_DECORATOR;
|
|
|
|
newlineflag |= !NIL_P(v);
|
|
|
|
|
|
|
|
v = rb_hash_aref(opt, sym_cr_newline);
|
|
|
|
if (RTEST(v))
|
|
|
|
setflags |= ECONV_CR_NEWLINE_DECORATOR;
|
|
|
|
newlineflag |= !NIL_P(v);
|
|
|
|
|
2020-01-11 04:19:29 +03:00
|
|
|
switch (newlineflag) {
|
|
|
|
case 1:
|
2011-04-26 19:55:21 +04:00
|
|
|
ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
|
|
|
|
ecflags |= setflags;
|
2020-01-11 04:19:29 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
rb_warning(":newline option preceds other newline options");
|
|
|
|
break;
|
2011-04-26 19:55:21 +04:00
|
|
|
}
|
|
|
|
}
|
2008-09-04 16:08:54 +04:00
|
|
|
|
|
|
|
return ecflags;
|
2008-08-24 10:25:24 +04:00
|
|
|
}
|
|
|
|
|
2008-09-03 19:11:46 +04:00
|
|
|
int
|
2011-04-26 19:55:21 +04:00
|
|
|
rb_econv_prepare_options(VALUE opthash, VALUE *opts, int ecflags)
|
2008-08-24 11:20:21 +04:00
|
|
|
{
|
2008-09-03 22:18:10 +04:00
|
|
|
VALUE newhash = Qnil;
|
2008-10-17 13:13:04 +04:00
|
|
|
VALUE v;
|
|
|
|
|
2008-09-12 21:53:26 +04:00
|
|
|
if (NIL_P(opthash)) {
|
|
|
|
*opts = Qnil;
|
2011-09-02 16:11:17 +04:00
|
|
|
return ecflags;
|
2008-09-12 21:53:26 +04:00
|
|
|
}
|
2011-04-26 19:55:21 +04:00
|
|
|
ecflags = econv_opts(opthash, ecflags);
|
2010-04-13 05:26:46 +04:00
|
|
|
|
2008-10-17 13:13:04 +04:00
|
|
|
v = rb_hash_aref(opthash, sym_replace);
|
|
|
|
if (!NIL_P(v)) {
|
|
|
|
StringValue(v);
|
|
|
|
if (rb_enc_str_coderange(v) == ENC_CODERANGE_BROKEN) {
|
|
|
|
VALUE dumped = rb_str_dump(v);
|
|
|
|
rb_raise(rb_eArgError, "replacement string is broken: %s as %s",
|
|
|
|
StringValueCStr(dumped),
|
|
|
|
rb_enc_name(rb_enc_get(v)));
|
|
|
|
}
|
|
|
|
v = rb_str_new_frozen(v);
|
|
|
|
newhash = rb_hash_new();
|
|
|
|
rb_hash_aset(newhash, sym_replace, v);
|
2008-09-03 22:18:10 +04:00
|
|
|
}
|
2008-10-17 13:13:04 +04:00
|
|
|
|
2010-04-13 05:26:46 +04:00
|
|
|
v = rb_hash_aref(opthash, sym_fallback);
|
|
|
|
if (!NIL_P(v)) {
|
2010-12-07 16:05:26 +03:00
|
|
|
VALUE h = rb_check_hash_type(v);
|
|
|
|
if (NIL_P(h)
|
|
|
|
? (rb_obj_is_proc(v) || rb_obj_is_method(v) || rb_respond_to(v, sym_aref))
|
|
|
|
: (v = h, 1)) {
|
2010-04-13 05:26:46 +04:00
|
|
|
if (NIL_P(newhash))
|
|
|
|
newhash = rb_hash_new();
|
|
|
|
rb_hash_aset(newhash, sym_fallback, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-03 22:18:10 +04:00
|
|
|
if (!NIL_P(newhash))
|
|
|
|
rb_hash_freeze(newhash);
|
|
|
|
*opts = newhash;
|
|
|
|
|
|
|
|
return ecflags;
|
|
|
|
}
|
|
|
|
|
2011-04-26 19:55:21 +04:00
|
|
|
int
|
|
|
|
rb_econv_prepare_opts(VALUE opthash, VALUE *opts)
|
|
|
|
{
|
|
|
|
return rb_econv_prepare_options(opthash, opts, 0);
|
|
|
|
}
|
|
|
|
|
2008-09-03 22:18:10 +04:00
|
|
|
rb_econv_t *
|
|
|
|
rb_econv_open_opts(const char *source_encoding, const char *destination_encoding, int ecflags, VALUE opthash)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec;
|
|
|
|
VALUE replacement;
|
|
|
|
|
|
|
|
if (NIL_P(opthash)) {
|
|
|
|
replacement = Qnil;
|
|
|
|
}
|
|
|
|
else {
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(opthash, T_HASH) || !OBJ_FROZEN(opthash))
|
2008-09-03 22:18:10 +04:00
|
|
|
rb_bug("rb_econv_open_opts called with invalid opthash");
|
|
|
|
replacement = rb_hash_aref(opthash, sym_replace);
|
|
|
|
}
|
|
|
|
|
|
|
|
ec = rb_econv_open(source_encoding, destination_encoding, ecflags);
|
|
|
|
if (!ec)
|
|
|
|
return ec;
|
|
|
|
|
|
|
|
if (!NIL_P(replacement)) {
|
|
|
|
int ret;
|
|
|
|
rb_encoding *enc = rb_enc_get(replacement);
|
|
|
|
|
2008-09-07 21:08:38 +04:00
|
|
|
ret = rb_econv_set_replacement(ec,
|
2008-09-03 22:18:10 +04:00
|
|
|
(const unsigned char *)RSTRING_PTR(replacement),
|
|
|
|
RSTRING_LEN(replacement),
|
2008-09-13 13:06:51 +04:00
|
|
|
rb_enc_name(enc));
|
2008-09-03 22:18:10 +04:00
|
|
|
if (ret == -1) {
|
|
|
|
rb_econv_close(ec);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ec;
|
2008-08-24 11:20:21 +04:00
|
|
|
}
|
|
|
|
|
2008-09-05 21:02:33 +04:00
|
|
|
static int
|
2016-05-22 11:06:57 +03:00
|
|
|
enc_arg(VALUE *arg, const char **name_p, rb_encoding **enc_p)
|
2008-09-05 21:02:33 +04:00
|
|
|
{
|
2008-09-18 12:27:17 +04:00
|
|
|
rb_encoding *enc;
|
|
|
|
const char *n;
|
|
|
|
int encidx;
|
2008-09-18 13:29:01 +04:00
|
|
|
VALUE encval;
|
2008-09-18 12:27:17 +04:00
|
|
|
|
2009-06-21 05:46:37 +04:00
|
|
|
if (((encidx = rb_to_encoding_index(encval = *arg)) < 0) ||
|
|
|
|
!(enc = rb_enc_from_index(encidx))) {
|
2008-09-18 12:27:17 +04:00
|
|
|
enc = NULL;
|
|
|
|
encidx = 0;
|
2008-09-18 13:29:01 +04:00
|
|
|
n = StringValueCStr(*arg);
|
2008-09-18 12:27:17 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = rb_enc_name(enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
*name_p = n;
|
2008-09-05 21:02:33 +04:00
|
|
|
*enc_p = enc;
|
2008-09-18 12:27:17 +04:00
|
|
|
|
|
|
|
return encidx;
|
2008-09-05 21:02:33 +04:00
|
|
|
}
|
|
|
|
|
2008-08-24 10:25:24 +04:00
|
|
|
static int
|
2016-05-22 11:06:57 +03:00
|
|
|
str_transcode_enc_args(VALUE str, VALUE *arg1, VALUE *arg2,
|
2008-09-05 21:59:32 +04:00
|
|
|
const char **sname_p, rb_encoding **senc_p,
|
|
|
|
const char **dname_p, rb_encoding **denc_p)
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
2008-09-05 21:59:32 +04:00
|
|
|
rb_encoding *senc, *denc;
|
|
|
|
const char *sname, *dname;
|
|
|
|
int sencidx, dencidx;
|
2008-02-21 11:42:10 +03:00
|
|
|
|
2008-09-18 13:29:01 +04:00
|
|
|
dencidx = enc_arg(arg1, &dname, &denc);
|
2008-09-05 21:02:33 +04:00
|
|
|
|
2008-09-12 19:39:46 +04:00
|
|
|
if (NIL_P(*arg2)) {
|
2008-09-05 21:59:32 +04:00
|
|
|
sencidx = rb_enc_get_index(str);
|
|
|
|
senc = rb_enc_from_index(sencidx);
|
|
|
|
sname = rb_enc_name(senc);
|
2007-12-10 15:47:55 +03:00
|
|
|
}
|
2007-12-10 08:01:47 +03:00
|
|
|
else {
|
2008-09-18 13:29:01 +04:00
|
|
|
sencidx = enc_arg(arg2, &sname, &senc);
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
2008-09-05 21:59:32 +04:00
|
|
|
*sname_p = sname;
|
|
|
|
*senc_p = senc;
|
|
|
|
*dname_p = dname;
|
|
|
|
*denc_p = denc;
|
|
|
|
return dencidx;
|
2008-08-24 10:25:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-09-03 22:18:10 +04:00
|
|
|
str_transcode0(int argc, VALUE *argv, VALUE *self, int ecflags, VALUE ecopts)
|
2008-08-24 10:25:24 +04:00
|
|
|
{
|
|
|
|
VALUE dest;
|
|
|
|
VALUE str = *self;
|
2016-05-22 11:06:57 +03:00
|
|
|
VALUE arg1, arg2;
|
2008-08-24 10:25:24 +04:00
|
|
|
long blen, slen;
|
|
|
|
unsigned char *buf, *bp, *sp;
|
|
|
|
const unsigned char *fromp;
|
2008-09-05 21:59:32 +04:00
|
|
|
rb_encoding *senc, *denc;
|
|
|
|
const char *sname, *dname;
|
|
|
|
int dencidx;
|
2013-11-22 12:50:14 +04:00
|
|
|
int explicitly_invalid_replace = TRUE;
|
2008-08-24 10:25:24 +04:00
|
|
|
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, 0, 2);
|
2008-08-24 10:25:24 +04:00
|
|
|
|
2008-10-13 02:52:01 +04:00
|
|
|
if (argc == 0) {
|
|
|
|
arg1 = rb_enc_default_internal();
|
|
|
|
if (NIL_P(arg1)) {
|
2009-01-18 21:32:24 +03:00
|
|
|
if (!ecflags) return -1;
|
|
|
|
arg1 = rb_obj_encoding(str);
|
2008-10-13 02:52:01 +04:00
|
|
|
}
|
2013-11-22 12:50:14 +04:00
|
|
|
if (!(ecflags & ECONV_INVALID_MASK)) {
|
|
|
|
explicitly_invalid_replace = FALSE;
|
|
|
|
}
|
2008-10-17 13:13:04 +04:00
|
|
|
ecflags |= ECONV_INVALID_REPLACE | ECONV_UNDEF_REPLACE;
|
2008-10-13 02:52:01 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
arg1 = argv[0];
|
|
|
|
}
|
|
|
|
arg2 = argc<=1 ? Qnil : argv[1];
|
2008-09-12 19:39:46 +04:00
|
|
|
dencidx = str_transcode_enc_args(str, &arg1, &arg2, &sname, &senc, &dname, &denc);
|
2008-08-24 10:25:24 +04:00
|
|
|
|
2011-04-26 19:54:14 +04:00
|
|
|
if ((ecflags & (ECONV_NEWLINE_DECORATOR_MASK|
|
2008-09-09 16:22:43 +04:00
|
|
|
ECONV_XML_TEXT_DECORATOR|
|
|
|
|
ECONV_XML_ATTR_CONTENT_DECORATOR|
|
|
|
|
ECONV_XML_ATTR_QUOTE_DECORATOR)) == 0) {
|
2008-09-05 21:59:32 +04:00
|
|
|
if (senc && senc == denc) {
|
2013-11-22 12:50:14 +04:00
|
|
|
if ((ecflags & ECONV_INVALID_MASK) && explicitly_invalid_replace) {
|
2013-11-01 11:55:56 +04:00
|
|
|
VALUE rep = Qnil;
|
2013-04-19 21:50:38 +04:00
|
|
|
if (!NIL_P(ecopts)) {
|
2013-11-01 11:55:56 +04:00
|
|
|
rep = rb_hash_aref(ecopts, sym_replace);
|
2013-04-19 21:50:38 +04:00
|
|
|
}
|
2016-05-27 11:09:46 +03:00
|
|
|
dest = rb_enc_str_scrub(senc, str, rep);
|
2013-11-01 16:12:44 +04:00
|
|
|
if (NIL_P(dest)) dest = str;
|
|
|
|
*self = dest;
|
2013-04-19 21:50:38 +04:00
|
|
|
return dencidx;
|
|
|
|
}
|
2008-12-25 14:59:57 +03:00
|
|
|
return NIL_P(arg2) ? -1 : dencidx;
|
2008-08-25 19:58:35 +04:00
|
|
|
}
|
2008-09-05 21:59:32 +04:00
|
|
|
if (senc && denc && rb_enc_asciicompat(senc) && rb_enc_asciicompat(denc)) {
|
2010-03-11 11:32:03 +03:00
|
|
|
if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
|
2008-09-05 21:59:32 +04:00
|
|
|
return dencidx;
|
2008-08-25 19:58:35 +04:00
|
|
|
}
|
|
|
|
}
|
2008-09-05 21:59:32 +04:00
|
|
|
if (encoding_equal(sname, dname)) {
|
2008-12-25 14:59:57 +03:00
|
|
|
return NIL_P(arg2) ? -1 : dencidx;
|
2008-08-25 19:58:35 +04:00
|
|
|
}
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
2008-08-25 19:58:35 +04:00
|
|
|
else {
|
2008-09-05 21:59:32 +04:00
|
|
|
if (encoding_equal(sname, dname)) {
|
|
|
|
sname = "";
|
|
|
|
dname = "";
|
2008-08-25 19:58:35 +04:00
|
|
|
}
|
2007-12-10 15:47:55 +03:00
|
|
|
}
|
2007-12-10 08:01:47 +03:00
|
|
|
|
2008-08-11 19:50:42 +04:00
|
|
|
fromp = sp = (unsigned char *)RSTRING_PTR(str);
|
|
|
|
slen = RSTRING_LEN(str);
|
|
|
|
blen = slen + 30; /* len + margin */
|
|
|
|
dest = rb_str_tmp_new(blen);
|
|
|
|
bp = (unsigned char *)RSTRING_PTR(dest);
|
2007-12-10 08:01:47 +03:00
|
|
|
|
2008-09-05 21:59:32 +04:00
|
|
|
transcode_loop(&fromp, &bp, (sp+slen), (bp+blen), dest, str_transcoding_resize, sname, dname, ecflags, ecopts);
|
2008-08-11 19:50:42 +04:00
|
|
|
if (fromp != sp+slen) {
|
|
|
|
rb_raise(rb_eArgError, "not fully converted, %"PRIdPTRDIFF" bytes left", sp+slen-fromp);
|
|
|
|
}
|
|
|
|
buf = (unsigned char *)RSTRING_PTR(dest);
|
|
|
|
*bp = '\0';
|
|
|
|
rb_str_set_len(dest, bp - buf);
|
2007-12-15 08:42:25 +03:00
|
|
|
|
2007-12-10 11:46:06 +03:00
|
|
|
/* set encoding */
|
2008-09-05 21:59:32 +04:00
|
|
|
if (!denc) {
|
|
|
|
dencidx = rb_define_dummy_encoding(dname);
|
2016-05-22 11:06:57 +03:00
|
|
|
RB_GC_GUARD(arg1);
|
|
|
|
RB_GC_GUARD(arg2);
|
2007-12-10 15:47:55 +03:00
|
|
|
}
|
2007-12-18 11:04:26 +03:00
|
|
|
*self = dest;
|
2007-12-10 08:01:47 +03:00
|
|
|
|
2008-09-05 21:59:32 +04:00
|
|
|
return dencidx;
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
2008-08-24 10:25:24 +04:00
|
|
|
static int
|
|
|
|
str_transcode(int argc, VALUE *argv, VALUE *self)
|
|
|
|
{
|
|
|
|
VALUE opt;
|
2008-09-03 19:11:46 +04:00
|
|
|
int ecflags = 0;
|
2008-09-03 22:18:10 +04:00
|
|
|
VALUE ecopts = Qnil;
|
2008-08-24 10:25:24 +04:00
|
|
|
|
2010-09-10 11:51:58 +04:00
|
|
|
argc = rb_scan_args(argc, argv, "02:", NULL, NULL, &opt);
|
|
|
|
if (!NIL_P(opt)) {
|
|
|
|
ecflags = rb_econv_prepare_opts(opt, &ecopts);
|
2008-08-24 10:25:24 +04:00
|
|
|
}
|
2008-09-03 22:18:10 +04:00
|
|
|
return str_transcode0(argc, argv, self, ecflags, ecopts);
|
2008-08-24 10:25:24 +04:00
|
|
|
}
|
|
|
|
|
2008-08-06 00:26:45 +04:00
|
|
|
static inline VALUE
|
|
|
|
str_encode_associate(VALUE str, int encidx)
|
|
|
|
{
|
|
|
|
int cr = 0;
|
|
|
|
|
|
|
|
rb_enc_associate_index(str, encidx);
|
|
|
|
|
|
|
|
/* transcoded string never be broken. */
|
|
|
|
if (rb_enc_asciicompat(rb_enc_from_index(encidx))) {
|
|
|
|
rb_str_coderange_scan_restartable(RSTRING_PTR(str), RSTRING_END(str), 0, &cr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cr = ENC_CODERANGE_VALID;
|
|
|
|
}
|
|
|
|
ENC_CODERANGE_SET(str, cr);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-12-10 08:01:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.encode!(encoding [, options] ) -> str
|
|
|
|
* str.encode!(dst_encoding, src_encoding [, options] ) -> str
|
2007-12-10 08:01:47 +03:00
|
|
|
*
|
2008-03-05 11:45:51 +03:00
|
|
|
* The first form transcodes the contents of <i>str</i> from
|
2007-12-10 08:01:47 +03:00
|
|
|
* str.encoding to +encoding+.
|
2008-03-05 11:45:51 +03:00
|
|
|
* The second form transcodes the contents of <i>str</i> from
|
2008-09-04 19:46:18 +04:00
|
|
|
* src_encoding to dst_encoding.
|
2008-03-05 11:45:51 +03:00
|
|
|
* The options Hash gives details for conversion. See String#encode
|
|
|
|
* for details.
|
2007-12-10 08:01:47 +03:00
|
|
|
* Returns the string even if no changes were made.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2008-05-20 20:37:13 +04:00
|
|
|
str_encode_bang(int argc, VALUE *argv, VALUE str)
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
2009-07-31 08:16:01 +04:00
|
|
|
VALUE newstr;
|
|
|
|
int encidx;
|
|
|
|
|
* array.c, gc.c, hash.c, object.c, string.c, struct.c,
transcode.c, variable.c, vm.c, vm_insnhelper.c, vm_method.c:
replace calls to rb_error_frozen() with rb_check_frozen(). a
patch from Run Paint Run Run at [ruby-core:32014]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29583 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-24 12:14:05 +04:00
|
|
|
rb_check_frozen(str);
|
2009-07-30 13:40:06 +04:00
|
|
|
|
2009-07-31 08:16:01 +04:00
|
|
|
newstr = str;
|
|
|
|
encidx = str_transcode(argc, argv, &newstr);
|
2007-12-18 11:04:26 +03:00
|
|
|
|
2007-12-21 11:49:08 +03:00
|
|
|
if (encidx < 0) return str;
|
2012-03-22 18:14:39 +04:00
|
|
|
if (newstr == str) {
|
|
|
|
rb_enc_associate_index(str, encidx);
|
|
|
|
return str;
|
|
|
|
}
|
2007-12-10 11:46:06 +03:00
|
|
|
rb_str_shared_replace(str, newstr);
|
2008-08-06 00:26:45 +04:00
|
|
|
return str_encode_associate(str, encidx);
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
2011-02-05 03:38:15 +03:00
|
|
|
static VALUE encoded_dup(VALUE newstr, VALUE str, int encidx);
|
|
|
|
|
2007-12-10 08:01:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.encode(encoding [, options] ) -> str
|
|
|
|
* str.encode(dst_encoding, src_encoding [, options] ) -> str
|
|
|
|
* str.encode([options]) -> str
|
2007-12-10 08:01:47 +03:00
|
|
|
*
|
2010-12-16 22:20:51 +03:00
|
|
|
* The first form returns a copy of +str+ transcoded
|
2007-12-10 08:01:47 +03:00
|
|
|
* to encoding +encoding+.
|
2010-12-16 22:20:51 +03:00
|
|
|
* The second form returns a copy of +str+ transcoded
|
2008-09-04 19:46:18 +04:00
|
|
|
* from src_encoding to dst_encoding.
|
2010-12-16 22:20:51 +03:00
|
|
|
* The last form returns a copy of +str+ transcoded to
|
|
|
|
* <tt>Encoding.default_internal</tt>.
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* By default, the first and second form raise
|
|
|
|
* Encoding::UndefinedConversionError for characters that are
|
|
|
|
* undefined in the destination encoding, and
|
|
|
|
* Encoding::InvalidByteSequenceError for invalid byte sequences
|
|
|
|
* in the source encoding. The last form by default does not raise
|
|
|
|
* exceptions but uses replacement strings.
|
2008-12-23 14:30:44 +03:00
|
|
|
*
|
2010-12-16 22:20:51 +03:00
|
|
|
* The +options+ Hash gives details for conversion and can have the following
|
|
|
|
* keys:
|
|
|
|
*
|
2008-12-23 14:30:44 +03:00
|
|
|
* :invalid ::
|
2010-12-16 22:20:51 +03:00
|
|
|
* If the value is +:replace+, #encode replaces invalid byte sequences in
|
|
|
|
* +str+ with the replacement character. The default is to raise the
|
|
|
|
* Encoding::InvalidByteSequenceError exception
|
2008-12-23 14:30:44 +03:00
|
|
|
* :undef ::
|
2010-12-16 22:20:51 +03:00
|
|
|
* If the value is +:replace+, #encode replaces characters which are
|
|
|
|
* undefined in the destination encoding with the replacement character.
|
|
|
|
* The default is to raise the Encoding::UndefinedConversionError.
|
2008-12-23 14:30:44 +03:00
|
|
|
* :replace ::
|
2010-12-16 22:20:51 +03:00
|
|
|
* Sets the replacement string to the given value. The default replacement
|
2008-12-30 12:03:04 +03:00
|
|
|
* string is "\uFFFD" for Unicode encoding forms, and "?" otherwise.
|
2010-04-13 05:26:46 +04:00
|
|
|
* :fallback ::
|
2011-12-14 10:28:32 +04:00
|
|
|
* Sets the replacement string by the given object for undefined
|
|
|
|
* character. The object should be a Hash, a Proc, a Method, or an
|
|
|
|
* object which has [] method.
|
2010-12-16 22:20:51 +03:00
|
|
|
* Its key is an undefined character encoded in the source encoding
|
2010-04-13 05:26:46 +04:00
|
|
|
* of current transcoder. Its value can be any encoding until it
|
|
|
|
* can be converted into the destination encoding of the transcoder.
|
2008-12-23 14:30:44 +03:00
|
|
|
* :xml ::
|
2010-12-16 22:20:51 +03:00
|
|
|
* The value must be +:text+ or +:attr+.
|
|
|
|
* If the value is +:text+ #encode replaces undefined characters with their
|
|
|
|
* (upper-case hexadecimal) numeric character references. '&', '<', and '>'
|
|
|
|
* are converted to "&", "<", and ">", respectively.
|
|
|
|
* If the value is +:attr+, #encode also quotes the replacement result
|
|
|
|
* (using '"'), and replaces '"' with """.
|
2008-12-23 14:30:44 +03:00
|
|
|
* :cr_newline ::
|
2008-12-30 12:03:04 +03:00
|
|
|
* Replaces LF ("\n") with CR ("\r") if value is true.
|
2008-12-23 14:30:44 +03:00
|
|
|
* :crlf_newline ::
|
2008-12-30 12:03:04 +03:00
|
|
|
* Replaces LF ("\n") with CRLF ("\r\n") if value is true.
|
2008-12-23 14:30:44 +03:00
|
|
|
* :universal_newline ::
|
2008-12-30 12:03:04 +03:00
|
|
|
* Replaces CRLF ("\r\n") and CR ("\r") with LF ("\n") if value is true.
|
2007-12-10 08:01:47 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2008-05-20 20:37:13 +04:00
|
|
|
str_encode(int argc, VALUE *argv, VALUE str)
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
2008-08-06 00:26:45 +04:00
|
|
|
VALUE newstr = str;
|
|
|
|
int encidx = str_transcode(argc, argv, &newstr);
|
2011-02-05 03:38:15 +03:00
|
|
|
return encoded_dup(newstr, str, encidx);
|
2008-05-20 20:37:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2008-09-26 14:35:00 +04:00
|
|
|
rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts)
|
2008-05-20 20:37:13 +04:00
|
|
|
{
|
2008-08-24 10:25:24 +04:00
|
|
|
int argc = 1;
|
|
|
|
VALUE *argv = &to;
|
|
|
|
VALUE newstr = str;
|
2008-09-03 22:18:10 +04:00
|
|
|
int encidx = str_transcode0(argc, argv, &newstr, ecflags, ecopts);
|
2011-02-05 03:38:15 +03:00
|
|
|
return encoded_dup(newstr, str, encidx);
|
|
|
|
}
|
2008-08-24 10:25:24 +04:00
|
|
|
|
2011-02-05 03:38:15 +03:00
|
|
|
static VALUE
|
|
|
|
encoded_dup(VALUE newstr, VALUE str, int encidx)
|
|
|
|
{
|
2008-08-24 10:25:24 +04:00
|
|
|
if (encidx < 0) return rb_str_dup(str);
|
2010-06-17 13:46:30 +04:00
|
|
|
if (newstr == str) {
|
|
|
|
newstr = rb_str_dup(str);
|
2012-03-22 18:14:39 +04:00
|
|
|
rb_enc_associate_index(newstr, encidx);
|
|
|
|
return newstr;
|
2010-06-17 13:46:30 +04:00
|
|
|
}
|
|
|
|
else {
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 14:49:11 +04:00
|
|
|
RBASIC_SET_CLASS(newstr, rb_obj_class(str));
|
2010-06-17 13:46:30 +04:00
|
|
|
}
|
2008-08-24 10:25:24 +04:00
|
|
|
return str_encode_associate(newstr, encidx);
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 05:18:52 +03:00
|
|
|
/*
|
|
|
|
* Document-class: Encoding::Converter
|
|
|
|
*
|
|
|
|
* Encoding conversion class.
|
|
|
|
*/
|
2008-08-12 18:46:18 +04:00
|
|
|
static void
|
2009-09-09 07:56:58 +04:00
|
|
|
econv_free(void *ptr)
|
2008-08-12 18:46:18 +04:00
|
|
|
{
|
2009-09-09 07:56:58 +04:00
|
|
|
rb_econv_t *ec = ptr;
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_econv_close(ec);
|
2008-08-12 18:46:18 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 07:56:58 +04:00
|
|
|
static size_t
|
|
|
|
econv_memsize(const void *ptr)
|
|
|
|
{
|
2015-12-09 03:38:32 +03:00
|
|
|
return sizeof(rb_econv_t);
|
2009-09-09 07:56:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t econv_data_type = {
|
|
|
|
"econv",
|
2020-02-07 08:14:05 +03:00
|
|
|
{0, econv_free, econv_memsize,},
|
2014-12-01 09:38:04 +03:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
2009-09-09 07:56:58 +04:00
|
|
|
};
|
|
|
|
|
2008-08-12 18:46:18 +04:00
|
|
|
static VALUE
|
|
|
|
econv_s_allocate(VALUE klass)
|
|
|
|
{
|
2009-09-09 07:56:58 +04:00
|
|
|
return TypedData_Wrap_Struct(klass, &econv_data_type, NULL);
|
2008-08-12 18:46:18 +04:00
|
|
|
}
|
|
|
|
|
2008-08-14 18:28:10 +04:00
|
|
|
static rb_encoding *
|
2008-08-15 04:05:06 +04:00
|
|
|
make_dummy_encoding(const char *name)
|
2008-08-14 18:28:10 +04:00
|
|
|
{
|
|
|
|
rb_encoding *enc;
|
2008-08-15 04:05:06 +04:00
|
|
|
int idx;
|
|
|
|
idx = rb_define_dummy_encoding(name);
|
|
|
|
enc = rb_enc_from_index(idx);
|
2008-08-14 18:28:10 +04:00
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:26:49 +04:00
|
|
|
static rb_encoding *
|
|
|
|
make_encoding(const char *name)
|
|
|
|
{
|
|
|
|
rb_encoding *enc;
|
|
|
|
enc = rb_enc_find(name);
|
|
|
|
if (!enc)
|
|
|
|
enc = make_dummy_encoding(name);
|
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
make_encobj(const char *name)
|
|
|
|
{
|
|
|
|
return rb_enc_from_encoding(make_encoding(name));
|
|
|
|
}
|
|
|
|
|
2008-09-05 21:23:49 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Encoding::Converter.asciicompat_encoding(string) -> encoding or nil
|
|
|
|
* Encoding::Converter.asciicompat_encoding(encoding) -> encoding or nil
|
2008-09-05 21:23:49 +04:00
|
|
|
*
|
2009-01-14 14:12:30 +03:00
|
|
|
* Returns the corresponding ASCII compatible encoding.
|
2008-09-05 21:23:49 +04:00
|
|
|
*
|
2009-01-14 14:12:30 +03:00
|
|
|
* Returns nil if the argument is an ASCII compatible encoding.
|
2008-09-05 21:23:49 +04:00
|
|
|
*
|
2012-03-03 07:29:01 +04:00
|
|
|
* "corresponding ASCII compatible encoding" is an ASCII compatible encoding which
|
2008-12-30 12:03:04 +03:00
|
|
|
* can represents exactly the same characters as the given ASCII incompatible encoding.
|
|
|
|
* So, no conversion undefined error occurs when converting between the two encodings.
|
2008-09-05 21:23:49 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Encoding::Converter.asciicompat_encoding("ISO-2022-JP") #=> #<Encoding:stateless-ISO-2022-JP>
|
|
|
|
* Encoding::Converter.asciicompat_encoding("UTF-16BE") #=> #<Encoding:UTF-8>
|
|
|
|
* Encoding::Converter.asciicompat_encoding("UTF-8") #=> nil
|
2008-09-05 21:23:49 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
2008-09-08 18:33:17 +04:00
|
|
|
econv_s_asciicompat_encoding(VALUE klass, VALUE arg)
|
2008-09-05 21:23:49 +04:00
|
|
|
{
|
2008-09-08 18:33:17 +04:00
|
|
|
const char *arg_name, *result_name;
|
|
|
|
rb_encoding *arg_enc, *result_enc;
|
2008-09-05 21:23:49 +04:00
|
|
|
|
2008-09-18 13:29:01 +04:00
|
|
|
enc_arg(&arg, &arg_name, &arg_enc);
|
2008-09-05 21:23:49 +04:00
|
|
|
|
2008-09-08 18:33:17 +04:00
|
|
|
result_name = rb_econv_asciicompat_encoding(arg_name);
|
2008-09-05 21:23:49 +04:00
|
|
|
|
2008-09-08 18:33:17 +04:00
|
|
|
if (result_name == NULL)
|
2008-09-05 21:23:49 +04:00
|
|
|
return Qnil;
|
|
|
|
|
2008-09-10 21:26:49 +04:00
|
|
|
result_enc = make_encoding(result_name);
|
2008-09-05 21:23:49 +04:00
|
|
|
|
2008-09-08 18:33:17 +04:00
|
|
|
return rb_enc_from_encoding(result_enc);
|
2008-09-05 21:23:49 +04:00
|
|
|
}
|
|
|
|
|
2008-09-12 17:53:42 +04:00
|
|
|
static void
|
|
|
|
econv_args(int argc, VALUE *argv,
|
2016-05-22 11:06:57 +03:00
|
|
|
VALUE *snamev_p, VALUE *dnamev_p,
|
2008-09-12 17:53:42 +04:00
|
|
|
const char **sname_p, const char **dname_p,
|
|
|
|
rb_encoding **senc_p, rb_encoding **denc_p,
|
|
|
|
int *ecflags_p,
|
|
|
|
VALUE *ecopts_p)
|
|
|
|
{
|
2010-09-10 11:51:58 +04:00
|
|
|
VALUE opt, flags_v, ecopts;
|
2008-09-12 17:53:42 +04:00
|
|
|
int sidx, didx;
|
|
|
|
const char *sname, *dname;
|
|
|
|
rb_encoding *senc, *denc;
|
|
|
|
int ecflags;
|
|
|
|
|
2010-09-10 11:51:58 +04:00
|
|
|
argc = rb_scan_args(argc, argv, "21:", snamev_p, dnamev_p, &flags_v, &opt);
|
2008-09-12 17:53:42 +04:00
|
|
|
|
2010-09-10 11:51:58 +04:00
|
|
|
if (!NIL_P(flags_v)) {
|
|
|
|
if (!NIL_P(opt)) {
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_error_arity(argc + 1, 2, 3);
|
2010-09-10 11:51:58 +04:00
|
|
|
}
|
|
|
|
ecflags = NUM2INT(rb_to_int(flags_v));
|
2008-09-12 17:53:42 +04:00
|
|
|
ecopts = Qnil;
|
|
|
|
}
|
2010-09-10 11:51:58 +04:00
|
|
|
else if (!NIL_P(opt)) {
|
|
|
|
ecflags = rb_econv_prepare_opts(opt, &ecopts);
|
2008-09-12 17:53:42 +04:00
|
|
|
}
|
|
|
|
else {
|
2010-09-10 11:51:58 +04:00
|
|
|
ecflags = 0;
|
|
|
|
ecopts = Qnil;
|
2008-09-12 17:53:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
senc = NULL;
|
2008-09-12 20:13:02 +04:00
|
|
|
sidx = rb_to_encoding_index(*snamev_p);
|
2008-09-12 17:53:42 +04:00
|
|
|
if (0 <= sidx) {
|
|
|
|
senc = rb_enc_from_index(sidx);
|
|
|
|
}
|
|
|
|
else {
|
2008-09-12 20:13:02 +04:00
|
|
|
StringValue(*snamev_p);
|
2008-09-12 17:53:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
denc = NULL;
|
2008-09-12 20:13:02 +04:00
|
|
|
didx = rb_to_encoding_index(*dnamev_p);
|
2008-09-12 17:53:42 +04:00
|
|
|
if (0 <= didx) {
|
|
|
|
denc = rb_enc_from_index(didx);
|
|
|
|
}
|
|
|
|
else {
|
2008-09-12 20:13:02 +04:00
|
|
|
StringValue(*dnamev_p);
|
2008-09-12 17:53:42 +04:00
|
|
|
}
|
|
|
|
|
2008-09-13 13:06:51 +04:00
|
|
|
sname = senc ? rb_enc_name(senc) : StringValueCStr(*snamev_p);
|
|
|
|
dname = denc ? rb_enc_name(denc) : StringValueCStr(*dnamev_p);
|
2008-09-12 17:53:42 +04:00
|
|
|
|
|
|
|
*sname_p = sname;
|
|
|
|
*dname_p = dname;
|
|
|
|
*senc_p = senc;
|
|
|
|
*denc_p = denc;
|
|
|
|
*ecflags_p = ecflags;
|
|
|
|
*ecopts_p = ecopts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
decorate_convpath(VALUE convpath, int ecflags)
|
|
|
|
{
|
|
|
|
int num_decorators;
|
|
|
|
const char *decorators[MAX_ECFLAGS_DECORATORS];
|
|
|
|
int i;
|
|
|
|
int n, len;
|
|
|
|
|
|
|
|
num_decorators = decorator_names(ecflags, decorators);
|
|
|
|
if (num_decorators == -1)
|
|
|
|
return -1;
|
|
|
|
|
2009-10-10 18:51:25 +04:00
|
|
|
len = n = RARRAY_LENINT(convpath);
|
2008-09-12 17:53:42 +04:00
|
|
|
if (n != 0) {
|
2013-05-13 13:56:22 +04:00
|
|
|
VALUE pair = RARRAY_AREF(convpath, n-1);
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(pair, T_ARRAY)) {
|
2013-05-13 13:56:22 +04:00
|
|
|
const char *sname = rb_enc_name(rb_to_encoding(RARRAY_AREF(pair, 0)));
|
|
|
|
const char *dname = rb_enc_name(rb_to_encoding(RARRAY_AREF(pair, 1)));
|
2008-12-25 11:54:51 +03:00
|
|
|
transcoder_entry_t *entry = get_transcoder_entry(sname, dname);
|
|
|
|
const rb_transcoder *tr = load_transcoder_entry(entry);
|
|
|
|
if (!tr)
|
|
|
|
return -1;
|
|
|
|
if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding) &&
|
|
|
|
tr->asciicompat_type == asciicompat_encoder) {
|
|
|
|
n--;
|
|
|
|
rb_ary_store(convpath, len + num_decorators - 1, pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_ary_store(convpath, len + num_decorators - 1, pair);
|
|
|
|
}
|
2008-09-12 17:53:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_decorators; i++)
|
|
|
|
rb_ary_store(convpath, n + i, rb_str_new_cstr(decorators[i]));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
search_convpath_i(const char *sname, const char *dname, int depth, void *arg)
|
|
|
|
{
|
|
|
|
VALUE *ary_p = arg;
|
|
|
|
VALUE v;
|
|
|
|
|
|
|
|
if (*ary_p == Qnil) {
|
|
|
|
*ary_p = rb_ary_new();
|
|
|
|
}
|
|
|
|
|
2008-09-13 13:21:34 +04:00
|
|
|
if (DECORATOR_P(sname, dname)) {
|
2008-09-12 17:53:42 +04:00
|
|
|
v = rb_str_new_cstr(dname);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
v = rb_assoc_new(make_encobj(sname), make_encobj(dname));
|
|
|
|
}
|
|
|
|
rb_ary_store(*ary_p, depth, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Encoding::Converter.search_convpath(source_encoding, destination_encoding) -> ary
|
|
|
|
* Encoding::Converter.search_convpath(source_encoding, destination_encoding, opt) -> ary
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns a conversion path.
|
2008-09-12 17:53:42 +04:00
|
|
|
*
|
|
|
|
* p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP")
|
|
|
|
* #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
|
|
|
|
* # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>]]
|
|
|
|
*
|
|
|
|
* p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP", universal_newline: true)
|
2011-09-06 07:07:16 +04:00
|
|
|
* or
|
|
|
|
* p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP", newline: :universal)
|
2008-09-12 17:53:42 +04:00
|
|
|
* #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
|
|
|
|
* # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>],
|
|
|
|
* # "universal_newline"]
|
|
|
|
*
|
|
|
|
* p Encoding::Converter.search_convpath("ISO-8859-1", "UTF-32BE", universal_newline: true)
|
2011-09-06 07:07:16 +04:00
|
|
|
* or
|
|
|
|
* p Encoding::Converter.search_convpath("ISO-8859-1", "UTF-32BE", newline: :universal)
|
2008-09-12 17:53:42 +04:00
|
|
|
* #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
|
|
|
|
* # "universal_newline",
|
|
|
|
* # [#<Encoding:UTF-8>, #<Encoding:UTF-32BE>]]
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_s_search_convpath(int argc, VALUE *argv, VALUE klass)
|
|
|
|
{
|
2016-05-22 11:06:57 +03:00
|
|
|
VALUE snamev, dnamev;
|
2008-09-12 17:53:42 +04:00
|
|
|
const char *sname, *dname;
|
|
|
|
rb_encoding *senc, *denc;
|
|
|
|
int ecflags;
|
|
|
|
VALUE ecopts;
|
|
|
|
VALUE convpath;
|
|
|
|
|
2008-09-12 20:13:02 +04:00
|
|
|
econv_args(argc, argv, &snamev, &dnamev, &sname, &dname, &senc, &denc, &ecflags, &ecopts);
|
2008-09-12 17:53:42 +04:00
|
|
|
|
|
|
|
convpath = Qnil;
|
|
|
|
transcode_search_path(sname, dname, search_convpath_i, &convpath);
|
|
|
|
|
2018-09-29 09:47:30 +03:00
|
|
|
if (NIL_P(convpath)) {
|
2018-09-29 09:47:34 +03:00
|
|
|
VALUE exc = rb_econv_open_exc(sname, dname, ecflags);
|
|
|
|
RB_GC_GUARD(snamev);
|
|
|
|
RB_GC_GUARD(dnamev);
|
|
|
|
rb_exc_raise(exc);
|
2018-09-29 09:47:30 +03:00
|
|
|
}
|
2008-09-12 17:53:42 +04:00
|
|
|
|
2016-05-22 11:06:57 +03:00
|
|
|
if (decorate_convpath(convpath, ecflags) == -1) {
|
|
|
|
VALUE exc = rb_econv_open_exc(sname, dname, ecflags);
|
|
|
|
RB_GC_GUARD(snamev);
|
|
|
|
RB_GC_GUARD(dnamev);
|
|
|
|
rb_exc_raise(exc);
|
|
|
|
}
|
2008-09-12 17:53:42 +04:00
|
|
|
|
|
|
|
return convpath;
|
|
|
|
}
|
|
|
|
|
2008-09-26 06:07:19 +04:00
|
|
|
/*
|
2009-01-15 16:47:47 +03:00
|
|
|
* Check the existence of a conversion path.
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns the number of converters in the conversion path.
|
2008-09-26 06:07:19 +04:00
|
|
|
* result: >=0:success -1:failure
|
|
|
|
*/
|
|
|
|
int
|
2008-09-26 14:35:00 +04:00
|
|
|
rb_econv_has_convpath_p(const char* from_encoding, const char* to_encoding)
|
2008-09-26 06:07:19 +04:00
|
|
|
{
|
|
|
|
VALUE convpath = Qnil;
|
|
|
|
transcode_search_path(from_encoding, to_encoding, search_convpath_i,
|
|
|
|
&convpath);
|
|
|
|
return RTEST(convpath);
|
|
|
|
}
|
|
|
|
|
2008-09-12 21:13:04 +04:00
|
|
|
struct rb_econv_init_by_convpath_t {
|
|
|
|
rb_econv_t *ec;
|
|
|
|
int index;
|
|
|
|
int ret;
|
|
|
|
};
|
|
|
|
|
2009-01-29 20:57:13 +03:00
|
|
|
static void
|
|
|
|
rb_econv_init_by_convpath_i(const char *sname, const char *dname, int depth, void *arg)
|
2008-09-12 21:13:04 +04:00
|
|
|
{
|
|
|
|
struct rb_econv_init_by_convpath_t *a = (struct rb_econv_init_by_convpath_t *)arg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (a->ret == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ret = rb_econv_add_converter(a->ec, sname, dname, a->index);
|
|
|
|
|
|
|
|
a->ret = ret;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rb_econv_t *
|
|
|
|
rb_econv_init_by_convpath(VALUE self, VALUE convpath,
|
|
|
|
const char **sname_p, const char **dname_p,
|
|
|
|
rb_encoding **senc_p, rb_encoding**denc_p)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec;
|
|
|
|
long i;
|
|
|
|
int ret, first=1;
|
|
|
|
VALUE elt;
|
2008-10-13 04:56:40 +04:00
|
|
|
rb_encoding *senc = 0, *denc = 0;
|
2008-09-12 21:13:04 +04:00
|
|
|
const char *sname, *dname;
|
|
|
|
|
2009-10-10 18:51:25 +04:00
|
|
|
ec = rb_econv_alloc(RARRAY_LENINT(convpath));
|
2008-09-12 21:13:04 +04:00
|
|
|
DATA_PTR(self) = ec;
|
|
|
|
|
|
|
|
for (i = 0; i < RARRAY_LEN(convpath); i++) {
|
2016-05-22 11:06:57 +03:00
|
|
|
VALUE snamev, dnamev;
|
2008-09-12 21:13:04 +04:00
|
|
|
VALUE pair;
|
|
|
|
elt = rb_ary_entry(convpath, i);
|
|
|
|
if (!NIL_P(pair = rb_check_array_type(elt))) {
|
|
|
|
if (RARRAY_LEN(pair) != 2)
|
|
|
|
rb_raise(rb_eArgError, "not a 2-element array in convpath");
|
|
|
|
snamev = rb_ary_entry(pair, 0);
|
2008-09-18 13:29:01 +04:00
|
|
|
enc_arg(&snamev, &sname, &senc);
|
2008-09-12 21:13:04 +04:00
|
|
|
dnamev = rb_ary_entry(pair, 1);
|
2008-09-18 13:29:01 +04:00
|
|
|
enc_arg(&dnamev, &dname, &denc);
|
2008-09-12 21:13:04 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sname = "";
|
|
|
|
dname = StringValueCStr(elt);
|
|
|
|
}
|
2008-09-13 13:21:34 +04:00
|
|
|
if (DECORATOR_P(sname, dname)) {
|
2008-09-12 21:13:04 +04:00
|
|
|
ret = rb_econv_add_converter(ec, sname, dname, ec->num_trans);
|
2016-05-22 11:06:57 +03:00
|
|
|
if (ret == -1) {
|
|
|
|
VALUE msg = rb_sprintf("decoration failed: %s", dname);
|
|
|
|
RB_GC_GUARD(snamev);
|
|
|
|
RB_GC_GUARD(dnamev);
|
|
|
|
rb_exc_raise(rb_exc_new_str(rb_eArgError, msg));
|
|
|
|
}
|
2008-09-12 21:13:04 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int j = ec->num_trans;
|
|
|
|
struct rb_econv_init_by_convpath_t arg;
|
|
|
|
arg.ec = ec;
|
|
|
|
arg.index = ec->num_trans;
|
|
|
|
arg.ret = 0;
|
|
|
|
ret = transcode_search_path(sname, dname, rb_econv_init_by_convpath_i, &arg);
|
2016-05-22 11:06:57 +03:00
|
|
|
if (ret == -1 || arg.ret == -1) {
|
|
|
|
VALUE msg = rb_sprintf("adding conversion failed: %s to %s", sname, dname);
|
|
|
|
RB_GC_GUARD(snamev);
|
|
|
|
RB_GC_GUARD(dnamev);
|
|
|
|
rb_exc_raise(rb_exc_new_str(rb_eArgError, msg));
|
|
|
|
}
|
2008-09-12 21:13:04 +04:00
|
|
|
if (first) {
|
|
|
|
first = 0;
|
|
|
|
*senc_p = senc;
|
|
|
|
*sname_p = ec->elems[j].tc->transcoder->src_encoding;
|
|
|
|
}
|
|
|
|
*denc_p = denc;
|
|
|
|
*dname_p = ec->elems[ec->num_trans-1].tc->transcoder->dst_encoding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first) {
|
2015-08-05 08:37:58 +03:00
|
|
|
*senc_p = NULL;
|
|
|
|
*denc_p = NULL;
|
|
|
|
*sname_p = "";
|
|
|
|
*dname_p = "";
|
2008-09-12 21:13:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ec->source_encoding_name = *sname_p;
|
|
|
|
ec->destination_encoding_name = *dname_p;
|
|
|
|
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
2008-08-13 11:32:34 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-08-14 15:31:27 +04:00
|
|
|
* Encoding::Converter.new(source_encoding, destination_encoding)
|
2008-09-04 17:17:40 +04:00
|
|
|
* Encoding::Converter.new(source_encoding, destination_encoding, opt)
|
2008-09-12 21:13:04 +04:00
|
|
|
* Encoding::Converter.new(convpath)
|
2008-09-04 16:08:54 +04:00
|
|
|
*
|
2008-09-04 17:17:40 +04:00
|
|
|
* possible options elements:
|
|
|
|
* hash form:
|
2008-09-25 15:42:49 +04:00
|
|
|
* :invalid => nil # raise error on invalid byte sequence (default)
|
|
|
|
* :invalid => :replace # replace invalid byte sequence
|
|
|
|
* :undef => nil # raise error on undefined conversion (default)
|
|
|
|
* :undef => :replace # replace undefined conversion
|
|
|
|
* :replace => string # replacement string ("?" or "\uFFFD" if not specified)
|
2011-09-06 07:07:16 +04:00
|
|
|
* :newline => :universal # decorator for converting CRLF and CR to LF
|
|
|
|
* :newline => :crlf # decorator for converting LF to CRLF
|
|
|
|
* :newline => :cr # decorator for converting LF to CR
|
2008-09-25 15:42:49 +04:00
|
|
|
* :universal_newline => true # decorator for converting CRLF and CR to LF
|
2011-05-15 16:04:32 +04:00
|
|
|
* :crlf_newline => true # decorator for converting LF to CRLF
|
|
|
|
* :cr_newline => true # decorator for converting LF to CR
|
2008-09-25 15:42:49 +04:00
|
|
|
* :xml => :text # escape as XML CharData.
|
|
|
|
* :xml => :attr # escape as XML AttValue
|
2008-09-04 17:17:40 +04:00
|
|
|
* integer form:
|
2008-09-25 15:42:49 +04:00
|
|
|
* Encoding::Converter::INVALID_REPLACE
|
|
|
|
* Encoding::Converter::UNDEF_REPLACE
|
|
|
|
* Encoding::Converter::UNDEF_HEX_CHARREF
|
2008-09-09 16:22:43 +04:00
|
|
|
* Encoding::Converter::UNIVERSAL_NEWLINE_DECORATOR
|
|
|
|
* Encoding::Converter::CRLF_NEWLINE_DECORATOR
|
|
|
|
* Encoding::Converter::CR_NEWLINE_DECORATOR
|
2008-09-24 20:02:37 +04:00
|
|
|
* Encoding::Converter::XML_TEXT_DECORATOR
|
|
|
|
* Encoding::Converter::XML_ATTR_CONTENT_DECORATOR
|
|
|
|
* Encoding::Converter::XML_ATTR_QUOTE_DECORATOR
|
2008-08-13 11:32:34 +04:00
|
|
|
*
|
|
|
|
* Encoding::Converter.new creates an instance of Encoding::Converter.
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Source_encoding and destination_encoding should be a string or
|
2008-08-31 19:31:14 +04:00
|
|
|
* Encoding object.
|
|
|
|
*
|
2008-09-04 17:17:40 +04:00
|
|
|
* opt should be nil, a hash or an integer.
|
2008-08-13 11:32:34 +04:00
|
|
|
*
|
2008-09-12 21:13:04 +04:00
|
|
|
* convpath should be an array.
|
2008-12-30 12:03:04 +03:00
|
|
|
* convpath may contain
|
|
|
|
* - two-element arrays which contain encodings or encoding names, or
|
|
|
|
* - strings representing decorator names.
|
2008-09-12 21:13:04 +04:00
|
|
|
*
|
2008-09-25 15:42:49 +04:00
|
|
|
* Encoding::Converter.new optionally takes an option.
|
|
|
|
* The option should be a hash or an integer.
|
|
|
|
* The option hash can contain :invalid => nil, etc.
|
|
|
|
* The option integer should be logical-or of constants such as
|
|
|
|
* Encoding::Converter::INVALID_REPLACE, etc.
|
|
|
|
*
|
2008-09-25 21:42:52 +04:00
|
|
|
* [:invalid => nil]
|
2008-12-30 12:03:04 +03:00
|
|
|
* Raise error on invalid byte sequence. This is a default behavior.
|
2008-09-25 21:42:52 +04:00
|
|
|
* [:invalid => :replace]
|
2008-12-30 12:03:04 +03:00
|
|
|
* Replace invalid byte sequence by replacement string.
|
2008-09-25 21:42:52 +04:00
|
|
|
* [:undef => nil]
|
2008-12-30 12:03:04 +03:00
|
|
|
* Raise an error if a character in source_encoding is not defined in destination_encoding.
|
2008-09-25 15:42:49 +04:00
|
|
|
* This is a default behavior.
|
2008-09-25 21:42:52 +04:00
|
|
|
* [:undef => :replace]
|
2008-12-30 12:03:04 +03:00
|
|
|
* Replace undefined character in destination_encoding with replacement string.
|
2008-09-25 21:42:52 +04:00
|
|
|
* [:replace => string]
|
2008-12-30 12:03:04 +03:00
|
|
|
* Specify the replacement string.
|
2008-09-25 15:42:49 +04:00
|
|
|
* If not specified, "\uFFFD" is used for Unicode encodings and "?" for others.
|
2008-09-25 21:42:52 +04:00
|
|
|
* [:universal_newline => true]
|
2008-12-30 12:03:04 +03:00
|
|
|
* Convert CRLF and CR to LF.
|
2008-09-25 21:42:52 +04:00
|
|
|
* [:crlf_newline => true]
|
2008-12-30 12:03:04 +03:00
|
|
|
* Convert LF to CRLF.
|
2008-09-25 21:42:52 +04:00
|
|
|
* [:cr_newline => true]
|
2008-12-30 12:03:04 +03:00
|
|
|
* Convert LF to CR.
|
2008-09-25 21:42:52 +04:00
|
|
|
* [:xml => :text]
|
2008-12-30 12:03:04 +03:00
|
|
|
* Escape as XML CharData.
|
2016-08-16 05:18:51 +03:00
|
|
|
* This form can be used as an HTML 4.0 #PCDATA.
|
2008-09-25 15:42:49 +04:00
|
|
|
* - '&' -> '&'
|
|
|
|
* - '<' -> '<'
|
|
|
|
* - '>' -> '>'
|
|
|
|
* - undefined characters in destination_encoding -> hexadecimal CharRef such as &#xHH;
|
2008-09-25 21:42:52 +04:00
|
|
|
* [:xml => :attr]
|
2008-12-30 12:03:04 +03:00
|
|
|
* Escape as XML AttValue.
|
2008-09-25 15:42:49 +04:00
|
|
|
* The converted result is quoted as "...".
|
2016-08-16 05:18:51 +03:00
|
|
|
* This form can be used as an HTML 4.0 attribute value.
|
2008-09-25 15:42:49 +04:00
|
|
|
* - '&' -> '&'
|
|
|
|
* - '<' -> '<'
|
|
|
|
* - '>' -> '>'
|
|
|
|
* - '"' -> '"'
|
|
|
|
* - undefined characters in destination_encoding -> hexadecimal CharRef such as &#xHH;
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Examples:
|
2008-08-13 11:32:34 +04:00
|
|
|
* # UTF-16BE to UTF-8
|
|
|
|
* ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* # Usually, decorators such as newline conversion are inserted last.
|
2008-09-09 16:22:43 +04:00
|
|
|
* ec = Encoding::Converter.new("UTF-16BE", "UTF-8", :universal_newline => true)
|
2008-09-12 21:13:04 +04:00
|
|
|
* p ec.convpath #=> [[#<Encoding:UTF-16BE>, #<Encoding:UTF-8>],
|
|
|
|
* # "universal_newline"]
|
2008-08-13 11:32:34 +04:00
|
|
|
*
|
2009-02-22 17:23:33 +03:00
|
|
|
* # But, if the last encoding is ASCII incompatible,
|
2008-09-12 21:13:04 +04:00
|
|
|
* # decorators are inserted before the last conversion.
|
2008-09-09 16:22:43 +04:00
|
|
|
* ec = Encoding::Converter.new("UTF-8", "UTF-16BE", :crlf_newline => true)
|
2008-09-12 21:13:04 +04:00
|
|
|
* p ec.convpath #=> ["crlf_newline",
|
|
|
|
* # [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]
|
2008-08-13 11:32:34 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* # Conversion path can be specified directly.
|
2008-09-12 21:13:04 +04:00
|
|
|
* ec = Encoding::Converter.new(["universal_newline", ["EUC-JP", "UTF-8"], ["UTF-8", "UTF-16BE"]])
|
|
|
|
* p ec.convpath #=> ["universal_newline",
|
|
|
|
* # [#<Encoding:EUC-JP>, #<Encoding:UTF-8>],
|
|
|
|
* # [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]
|
2008-08-13 11:32:34 +04:00
|
|
|
*/
|
2008-08-12 18:46:18 +04:00
|
|
|
static VALUE
|
2008-08-13 10:08:56 +04:00
|
|
|
econv_init(int argc, VALUE *argv, VALUE self)
|
2008-08-12 18:46:18 +04:00
|
|
|
{
|
2008-09-12 17:53:42 +04:00
|
|
|
VALUE ecopts;
|
2016-05-22 11:06:57 +03:00
|
|
|
VALUE snamev, dnamev;
|
2008-08-15 04:05:06 +04:00
|
|
|
const char *sname, *dname;
|
2008-08-14 16:35:19 +04:00
|
|
|
rb_encoding *senc, *denc;
|
|
|
|
rb_econv_t *ec;
|
2008-09-03 19:11:46 +04:00
|
|
|
int ecflags;
|
2008-09-12 21:13:04 +04:00
|
|
|
VALUE convpath;
|
2008-08-13 09:30:42 +04:00
|
|
|
|
2009-09-09 07:56:58 +04:00
|
|
|
if (rb_check_typeddata(self, &econv_data_type)) {
|
2008-08-12 18:46:18 +04:00
|
|
|
rb_raise(rb_eTypeError, "already initialized");
|
|
|
|
}
|
|
|
|
|
2008-09-12 21:13:04 +04:00
|
|
|
if (argc == 1 && !NIL_P(convpath = rb_check_array_type(argv[0]))) {
|
|
|
|
ec = rb_econv_init_by_convpath(self, convpath, &sname, &dname, &senc, &denc);
|
|
|
|
ecflags = 0;
|
|
|
|
ecopts = Qnil;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
econv_args(argc, argv, &snamev, &dnamev, &sname, &dname, &senc, &denc, &ecflags, &ecopts);
|
|
|
|
ec = rb_econv_open_opts(sname, dname, ecflags, ecopts);
|
|
|
|
}
|
|
|
|
|
2008-08-14 16:35:19 +04:00
|
|
|
if (!ec) {
|
2015-08-05 08:40:17 +03:00
|
|
|
VALUE exc = rb_econv_open_exc(sname, dname, ecflags);
|
|
|
|
RB_GC_GUARD(snamev);
|
|
|
|
RB_GC_GUARD(dnamev);
|
|
|
|
rb_exc_raise(exc);
|
2008-08-12 18:46:18 +04:00
|
|
|
}
|
|
|
|
|
2008-09-13 13:21:34 +04:00
|
|
|
if (!DECORATOR_P(sname, dname)) {
|
2008-08-15 04:34:16 +04:00
|
|
|
if (!senc)
|
|
|
|
senc = make_dummy_encoding(sname);
|
|
|
|
if (!denc)
|
|
|
|
denc = make_dummy_encoding(dname);
|
2015-08-05 08:40:17 +03:00
|
|
|
RB_GC_GUARD(snamev);
|
|
|
|
RB_GC_GUARD(dnamev);
|
2008-08-15 04:34:16 +04:00
|
|
|
}
|
2008-08-15 04:05:06 +04:00
|
|
|
|
2008-08-14 18:28:10 +04:00
|
|
|
ec->source_encoding = senc;
|
|
|
|
ec->destination_encoding = denc;
|
|
|
|
|
2008-08-14 16:35:19 +04:00
|
|
|
DATA_PTR(self) = ec;
|
2008-08-12 18:46:18 +04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:38:12 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ec.inspect -> string
|
|
|
|
*
|
|
|
|
* Returns a printable version of <i>ec</i>
|
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("iso-8859-1", "utf-8")
|
|
|
|
* puts ec.inspect #=> #<Encoding::Converter: ISO-8859-1 to UTF-8>
|
|
|
|
*
|
|
|
|
*/
|
2008-08-13 12:47:57 +04:00
|
|
|
static VALUE
|
|
|
|
econv_inspect(VALUE self)
|
|
|
|
{
|
|
|
|
const char *cname = rb_obj_classname(self);
|
2009-09-09 07:56:58 +04:00
|
|
|
rb_econv_t *ec;
|
2008-08-13 12:47:57 +04:00
|
|
|
|
2009-09-09 07:56:58 +04:00
|
|
|
TypedData_Get_Struct(self, rb_econv_t, &econv_data_type, ec);
|
2008-08-14 18:35:55 +04:00
|
|
|
if (!ec)
|
2008-08-13 12:47:57 +04:00
|
|
|
return rb_sprintf("#<%s: uninitialized>", cname);
|
2008-08-22 20:44:00 +04:00
|
|
|
else {
|
|
|
|
const char *sname = ec->source_encoding_name;
|
|
|
|
const char *dname = ec->destination_encoding_name;
|
2008-08-25 19:01:36 +04:00
|
|
|
VALUE str;
|
|
|
|
str = rb_sprintf("#<%s: ", cname);
|
2008-09-03 19:11:46 +04:00
|
|
|
econv_description(sname, dname, ec->flags, str);
|
2008-08-25 19:01:36 +04:00
|
|
|
rb_str_cat2(str, ">");
|
|
|
|
return str;
|
2008-08-22 20:44:00 +04:00
|
|
|
}
|
2008-08-13 12:47:57 +04:00
|
|
|
}
|
|
|
|
|
2008-08-14 15:31:27 +04:00
|
|
|
static rb_econv_t *
|
2008-08-12 18:46:18 +04:00
|
|
|
check_econv(VALUE self)
|
|
|
|
{
|
2009-09-09 07:56:58 +04:00
|
|
|
rb_econv_t *ec;
|
|
|
|
|
|
|
|
TypedData_Get_Struct(self, rb_econv_t, &econv_data_type, ec);
|
|
|
|
if (!ec) {
|
2008-08-13 12:47:57 +04:00
|
|
|
rb_raise(rb_eTypeError, "uninitialized encoding converter");
|
|
|
|
}
|
2009-09-09 07:56:58 +04:00
|
|
|
return ec;
|
2008-08-12 18:46:18 +04:00
|
|
|
}
|
|
|
|
|
2008-08-14 18:28:10 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-08-31 13:38:12 +04:00
|
|
|
* ec.source_encoding -> encoding
|
2008-08-14 18:28:10 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns the source encoding as an Encoding object.
|
2008-08-14 18:28:10 +04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_source_encoding(VALUE self)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
2009-02-22 17:23:33 +03:00
|
|
|
if (!ec->source_encoding)
|
2008-08-14 18:28:10 +04:00
|
|
|
return Qnil;
|
|
|
|
return rb_enc_from_encoding(ec->source_encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-08-31 13:38:12 +04:00
|
|
|
* ec.destination_encoding -> encoding
|
2008-08-14 18:28:10 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns the destination encoding as an Encoding object.
|
2008-08-14 18:28:10 +04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_destination_encoding(VALUE self)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
2009-02-22 17:23:33 +03:00
|
|
|
if (!ec->destination_encoding)
|
2008-08-14 18:28:10 +04:00
|
|
|
return Qnil;
|
|
|
|
return rb_enc_from_encoding(ec->destination_encoding);
|
|
|
|
}
|
|
|
|
|
2008-09-10 21:15:08 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ec.convpath -> ary
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns the conversion path of ec.
|
2008-09-10 21:15:08 +04:00
|
|
|
*
|
|
|
|
* The result is an array of conversions.
|
|
|
|
*
|
2009-11-03 20:46:28 +03:00
|
|
|
* ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP", crlf_newline: true)
|
2008-09-10 21:15:08 +04:00
|
|
|
* p ec.convpath
|
2008-09-10 21:26:49 +04:00
|
|
|
* #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
|
|
|
|
* # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>],
|
|
|
|
* # "crlf_newline"]
|
2008-09-10 21:15:08 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Each element of the array is a pair of encodings or a string.
|
|
|
|
* A pair means an encoding conversion.
|
|
|
|
* A string means a decorator.
|
2008-09-10 21:15:08 +04:00
|
|
|
*
|
2008-09-10 21:26:49 +04:00
|
|
|
* In the above example, [#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>] means
|
|
|
|
* a converter from ISO-8859-1 to UTF-8.
|
2008-09-10 21:15:08 +04:00
|
|
|
* "crlf_newline" means newline converter from LF to CRLF.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_convpath(VALUE self)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
VALUE result;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
result = rb_ary_new();
|
|
|
|
for (i = 0; i < ec->num_trans; i++) {
|
|
|
|
const rb_transcoder *tr = ec->elems[i].tc->transcoder;
|
|
|
|
VALUE v;
|
2008-09-13 13:21:34 +04:00
|
|
|
if (DECORATOR_P(tr->src_encoding, tr->dst_encoding))
|
2008-09-10 21:15:08 +04:00
|
|
|
v = rb_str_new_cstr(tr->dst_encoding);
|
|
|
|
else
|
2008-09-10 21:26:49 +04:00
|
|
|
v = rb_assoc_new(make_encobj(tr->src_encoding), make_encobj(tr->dst_encoding));
|
2008-09-10 21:15:08 +04:00
|
|
|
rb_ary_push(result, v);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-06 07:07:16 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ec == other -> true or false
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_equal(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec1 = check_econv(self);
|
|
|
|
rb_econv_t *ec2;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!rb_typeddata_is_kind_of(other, &econv_data_type)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
ec2 = DATA_PTR(other);
|
|
|
|
if (!ec2) return Qfalse;
|
|
|
|
if (ec1->source_encoding_name != ec2->source_encoding_name &&
|
|
|
|
strcmp(ec1->source_encoding_name, ec2->source_encoding_name))
|
|
|
|
return Qfalse;
|
|
|
|
if (ec1->destination_encoding_name != ec2->destination_encoding_name &&
|
|
|
|
strcmp(ec1->destination_encoding_name, ec2->destination_encoding_name))
|
|
|
|
return Qfalse;
|
|
|
|
if (ec1->flags != ec2->flags) return Qfalse;
|
|
|
|
if (ec1->replacement_enc != ec2->replacement_enc &&
|
|
|
|
strcmp(ec1->replacement_enc, ec2->replacement_enc))
|
|
|
|
return Qfalse;
|
|
|
|
if (ec1->replacement_len != ec2->replacement_len) return Qfalse;
|
|
|
|
if (ec1->replacement_str != ec2->replacement_str &&
|
|
|
|
memcmp(ec1->replacement_str, ec2->replacement_str, ec2->replacement_len))
|
|
|
|
return Qfalse;
|
|
|
|
|
|
|
|
if (ec1->num_trans != ec2->num_trans) return Qfalse;
|
|
|
|
for (i = 0; i < ec1->num_trans; i++) {
|
|
|
|
if (ec1->elems[i].tc->transcoder != ec2->elems[i].tc->transcoder)
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2008-08-15 13:12:56 +04:00
|
|
|
static VALUE
|
|
|
|
econv_result_to_symbol(rb_econv_result_t res)
|
|
|
|
{
|
|
|
|
switch (res) {
|
2008-08-28 20:59:17 +04:00
|
|
|
case econv_invalid_byte_sequence: return sym_invalid_byte_sequence;
|
|
|
|
case econv_incomplete_input: return sym_incomplete_input;
|
|
|
|
case econv_undefined_conversion: return sym_undefined_conversion;
|
|
|
|
case econv_destination_buffer_full: return sym_destination_buffer_full;
|
|
|
|
case econv_source_buffer_empty: return sym_source_buffer_empty;
|
|
|
|
case econv_finished: return sym_finished;
|
2008-09-09 20:27:02 +04:00
|
|
|
case econv_after_output: return sym_after_output;
|
2008-08-15 13:12:56 +04:00
|
|
|
default: return INT2NUM(res); /* should not be reached */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-13 11:32:34 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-08-31 13:38:12 +04:00
|
|
|
* ec.primitive_convert(source_buffer, destination_buffer) -> symbol
|
|
|
|
* ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset) -> symbol
|
|
|
|
* ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize) -> symbol
|
2008-09-04 16:48:21 +04:00
|
|
|
* ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize, opt) -> symbol
|
2008-08-13 11:32:34 +04:00
|
|
|
*
|
2008-09-04 16:48:21 +04:00
|
|
|
* possible opt elements:
|
|
|
|
* hash form:
|
|
|
|
* :partial_input => true # source buffer may be part of larger source
|
2008-09-09 20:27:02 +04:00
|
|
|
* :after_output => true # stop conversion after output before input
|
2008-09-04 16:48:21 +04:00
|
|
|
* integer form:
|
|
|
|
* Encoding::Converter::PARTIAL_INPUT
|
2008-09-09 20:27:02 +04:00
|
|
|
* Encoding::Converter::AFTER_OUTPUT
|
2008-08-13 11:32:34 +04:00
|
|
|
*
|
|
|
|
* possible results:
|
2008-08-14 15:31:27 +04:00
|
|
|
* :invalid_byte_sequence
|
2008-08-26 20:09:29 +04:00
|
|
|
* :incomplete_input
|
2008-08-13 11:32:34 +04:00
|
|
|
* :undefined_conversion
|
2008-09-09 20:27:02 +04:00
|
|
|
* :after_output
|
2008-08-14 15:31:27 +04:00
|
|
|
* :destination_buffer_full
|
|
|
|
* :source_buffer_empty
|
2008-08-13 11:32:34 +04:00
|
|
|
* :finished
|
|
|
|
*
|
2008-08-14 15:31:27 +04:00
|
|
|
* primitive_convert converts source_buffer into destination_buffer.
|
2008-08-13 11:32:34 +04:00
|
|
|
*
|
2008-08-28 21:13:49 +04:00
|
|
|
* source_buffer should be a string or nil.
|
2012-03-03 07:29:01 +04:00
|
|
|
* nil means an empty string.
|
2008-08-28 21:13:49 +04:00
|
|
|
*
|
2008-08-28 21:39:02 +04:00
|
|
|
* destination_buffer should be a string.
|
2008-08-28 21:13:49 +04:00
|
|
|
*
|
2008-08-14 15:31:27 +04:00
|
|
|
* destination_byteoffset should be an integer or nil.
|
2008-08-31 09:18:29 +04:00
|
|
|
* nil means the end of destination_buffer.
|
|
|
|
* If it is omitted, nil is assumed.
|
2008-08-28 21:13:49 +04:00
|
|
|
*
|
2008-08-31 19:31:14 +04:00
|
|
|
* destination_bytesize should be an integer or nil.
|
|
|
|
* nil means unlimited.
|
|
|
|
* If it is omitted, nil is assumed.
|
|
|
|
*
|
2008-09-04 17:17:40 +04:00
|
|
|
* opt should be nil, a hash or an integer.
|
2008-08-31 19:31:14 +04:00
|
|
|
* nil means no flags.
|
2008-08-31 09:18:29 +04:00
|
|
|
* If it is omitted, nil is assumed.
|
2008-08-13 11:32:34 +04:00
|
|
|
*
|
2008-08-31 19:31:14 +04:00
|
|
|
* primitive_convert converts the content of source_buffer from beginning
|
2008-08-14 15:31:27 +04:00
|
|
|
* and store the result into destination_buffer.
|
2008-08-13 11:32:34 +04:00
|
|
|
*
|
2008-08-14 15:31:27 +04:00
|
|
|
* destination_byteoffset and destination_bytesize specify the region which
|
2008-08-14 03:45:31 +04:00
|
|
|
* the converted result is stored.
|
2008-08-14 15:31:27 +04:00
|
|
|
* destination_byteoffset specifies the start position in destination_buffer in bytes.
|
|
|
|
* If destination_byteoffset is nil,
|
|
|
|
* destination_buffer.bytesize is used for appending the result.
|
|
|
|
* destination_bytesize specifies maximum number of bytes.
|
2008-08-28 21:39:02 +04:00
|
|
|
* If destination_bytesize is nil,
|
|
|
|
* destination size is unlimited.
|
2008-08-14 15:31:27 +04:00
|
|
|
* After conversion, destination_buffer is resized to
|
2008-08-31 19:31:14 +04:00
|
|
|
* destination_byteoffset + actually produced number of bytes.
|
2008-08-14 20:10:57 +04:00
|
|
|
* Also destination_buffer's encoding is set to destination_encoding.
|
2008-08-14 03:45:31 +04:00
|
|
|
*
|
2008-08-31 19:31:14 +04:00
|
|
|
* primitive_convert drops the converted part of source_buffer.
|
2008-08-14 15:31:27 +04:00
|
|
|
* the dropped part is converted in destination_buffer or
|
2008-08-13 11:32:34 +04:00
|
|
|
* buffered in Encoding::Converter object.
|
|
|
|
*
|
|
|
|
* primitive_convert stops conversion when one of following condition met.
|
2008-08-14 15:31:27 +04:00
|
|
|
* - invalid byte sequence found in source buffer (:invalid_byte_sequence)
|
2012-04-24 17:52:13 +04:00
|
|
|
* +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
|
2008-08-26 20:09:29 +04:00
|
|
|
* - unexpected end of source buffer (:incomplete_input)
|
2008-09-04 16:48:21 +04:00
|
|
|
* this occur only when :partial_input is not specified.
|
2012-04-24 17:52:13 +04:00
|
|
|
* +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
|
2008-08-13 11:32:34 +04:00
|
|
|
* - character not representable in output encoding (:undefined_conversion)
|
2012-04-24 17:52:13 +04:00
|
|
|
* +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
|
2008-09-09 20:27:02 +04:00
|
|
|
* - after some output is generated, before input is done (:after_output)
|
|
|
|
* this occur only when :after_output is specified.
|
2008-08-14 15:31:27 +04:00
|
|
|
* - destination buffer is full (:destination_buffer_full)
|
2008-08-28 22:28:52 +04:00
|
|
|
* this occur only when destination_bytesize is non-nil.
|
2008-08-14 15:31:27 +04:00
|
|
|
* - source buffer is empty (:source_buffer_empty)
|
2008-09-04 16:48:21 +04:00
|
|
|
* this occur only when :partial_input is specified.
|
2008-08-13 11:32:34 +04:00
|
|
|
* - conversion is finished (:finished)
|
|
|
|
*
|
|
|
|
* example:
|
|
|
|
* ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
|
2009-06-13 13:59:46 +04:00
|
|
|
* ret = ec.primitive_convert(src="pi", dst="", nil, 100)
|
2008-08-13 11:32:34 +04:00
|
|
|
* p [ret, src, dst] #=> [:finished, "", "\x00p\x00i"]
|
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
|
2009-06-13 13:59:46 +04:00
|
|
|
* ret = ec.primitive_convert(src="pi", dst="", nil, 1)
|
2008-08-14 15:31:27 +04:00
|
|
|
* p [ret, src, dst] #=> [:destination_buffer_full, "i", "\x00"]
|
2009-06-13 13:59:46 +04:00
|
|
|
* ret = ec.primitive_convert(src, dst="", nil, 1)
|
2008-08-14 15:31:27 +04:00
|
|
|
* p [ret, src, dst] #=> [:destination_buffer_full, "", "p"]
|
2009-06-13 13:59:46 +04:00
|
|
|
* ret = ec.primitive_convert(src, dst="", nil, 1)
|
2008-08-14 15:31:27 +04:00
|
|
|
* p [ret, src, dst] #=> [:destination_buffer_full, "", "\x00"]
|
2009-06-13 13:59:46 +04:00
|
|
|
* ret = ec.primitive_convert(src, dst="", nil, 1)
|
2008-08-13 12:28:39 +04:00
|
|
|
* p [ret, src, dst] #=> [:finished, "", "i"]
|
2008-08-13 11:32:34 +04:00
|
|
|
*
|
|
|
|
*/
|
2008-08-12 18:46:18 +04:00
|
|
|
static VALUE
|
2008-08-13 10:08:56 +04:00
|
|
|
econv_primitive_convert(int argc, VALUE *argv, VALUE self)
|
2008-08-12 18:46:18 +04:00
|
|
|
{
|
2008-09-04 16:48:21 +04:00
|
|
|
VALUE input, output, output_byteoffset_v, output_bytesize_v, opt, flags_v;
|
2008-08-14 18:35:55 +04:00
|
|
|
rb_econv_t *ec = check_econv(self);
|
2008-08-14 15:31:27 +04:00
|
|
|
rb_econv_result_t res;
|
2008-08-12 18:46:18 +04:00
|
|
|
const unsigned char *ip, *is;
|
|
|
|
unsigned char *op, *os;
|
2008-08-13 21:24:42 +04:00
|
|
|
long output_byteoffset, output_bytesize;
|
|
|
|
unsigned long output_byteend;
|
2008-08-12 18:46:18 +04:00
|
|
|
int flags;
|
|
|
|
|
2010-09-10 11:51:58 +04:00
|
|
|
argc = rb_scan_args(argc, argv, "23:", &input, &output, &output_byteoffset_v, &output_bytesize_v, &flags_v, &opt);
|
2008-08-13 21:24:42 +04:00
|
|
|
|
2008-08-28 21:39:02 +04:00
|
|
|
if (NIL_P(output_byteoffset_v))
|
|
|
|
output_byteoffset = 0; /* dummy */
|
2008-08-13 21:24:42 +04:00
|
|
|
else
|
|
|
|
output_byteoffset = NUM2LONG(output_byteoffset_v);
|
|
|
|
|
2008-08-28 21:39:02 +04:00
|
|
|
if (NIL_P(output_bytesize_v))
|
|
|
|
output_bytesize = 0; /* dummy */
|
|
|
|
else
|
|
|
|
output_bytesize = NUM2LONG(output_bytesize_v);
|
2008-08-13 10:08:56 +04:00
|
|
|
|
2010-09-10 11:51:58 +04:00
|
|
|
if (!NIL_P(flags_v)) {
|
|
|
|
if (!NIL_P(opt)) {
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_error_arity(argc + 1, 2, 5);
|
2010-09-10 11:51:58 +04:00
|
|
|
}
|
|
|
|
flags = NUM2INT(rb_to_int(flags_v));
|
2008-09-04 16:48:21 +04:00
|
|
|
}
|
2010-09-10 11:51:58 +04:00
|
|
|
else if (!NIL_P(opt)) {
|
2008-09-04 16:48:21 +04:00
|
|
|
VALUE v;
|
|
|
|
flags = 0;
|
|
|
|
v = rb_hash_aref(opt, sym_partial_input);
|
|
|
|
if (RTEST(v))
|
|
|
|
flags |= ECONV_PARTIAL_INPUT;
|
2008-09-09 20:27:02 +04:00
|
|
|
v = rb_hash_aref(opt, sym_after_output);
|
2008-09-04 16:48:21 +04:00
|
|
|
if (RTEST(v))
|
2008-09-09 20:27:02 +04:00
|
|
|
flags |= ECONV_AFTER_OUTPUT;
|
2008-09-04 16:48:21 +04:00
|
|
|
}
|
2010-09-10 11:51:58 +04:00
|
|
|
else {
|
|
|
|
flags = 0;
|
|
|
|
}
|
2008-08-13 21:24:42 +04:00
|
|
|
|
2008-08-12 18:46:18 +04:00
|
|
|
StringValue(output);
|
2008-08-28 21:13:49 +04:00
|
|
|
if (!NIL_P(input))
|
|
|
|
StringValue(input);
|
2008-08-12 18:46:18 +04:00
|
|
|
rb_str_modify(output);
|
2008-08-13 03:03:46 +04:00
|
|
|
|
2008-08-28 21:39:02 +04:00
|
|
|
if (NIL_P(output_bytesize_v)) {
|
|
|
|
output_bytesize = RSTRING_EMBED_LEN_MAX;
|
|
|
|
if (!NIL_P(input) && output_bytesize < RSTRING_LEN(input))
|
|
|
|
output_bytesize = RSTRING_LEN(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
retry:
|
|
|
|
|
|
|
|
if (NIL_P(output_byteoffset_v))
|
2008-08-13 21:24:42 +04:00
|
|
|
output_byteoffset = RSTRING_LEN(output);
|
|
|
|
|
|
|
|
if (output_byteoffset < 0)
|
|
|
|
rb_raise(rb_eArgError, "negative output_byteoffset");
|
|
|
|
|
|
|
|
if (RSTRING_LEN(output) < output_byteoffset)
|
|
|
|
rb_raise(rb_eArgError, "output_byteoffset too big");
|
|
|
|
|
|
|
|
if (output_bytesize < 0)
|
|
|
|
rb_raise(rb_eArgError, "negative output_bytesize");
|
|
|
|
|
|
|
|
output_byteend = (unsigned long)output_byteoffset +
|
|
|
|
(unsigned long)output_bytesize;
|
|
|
|
|
|
|
|
if (output_byteend < (unsigned long)output_byteoffset ||
|
|
|
|
LONG_MAX < output_byteend)
|
|
|
|
rb_raise(rb_eArgError, "output_byteoffset+output_bytesize too big");
|
|
|
|
|
|
|
|
if (rb_str_capacity(output) < output_byteend)
|
|
|
|
rb_str_resize(output, output_byteend);
|
2008-08-12 18:46:18 +04:00
|
|
|
|
2008-08-28 21:13:49 +04:00
|
|
|
if (NIL_P(input)) {
|
|
|
|
ip = is = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ip = (const unsigned char *)RSTRING_PTR(input);
|
|
|
|
is = ip + RSTRING_LEN(input);
|
|
|
|
}
|
2008-08-12 18:46:18 +04:00
|
|
|
|
2008-08-13 21:24:42 +04:00
|
|
|
op = (unsigned char *)RSTRING_PTR(output) + output_byteoffset;
|
|
|
|
os = op + output_bytesize;
|
2008-08-12 18:46:18 +04:00
|
|
|
|
2008-08-14 18:35:55 +04:00
|
|
|
res = rb_econv_convert(ec, &ip, is, &op, os, flags);
|
2008-08-12 18:46:18 +04:00
|
|
|
rb_str_set_len(output, op-(unsigned char *)RSTRING_PTR(output));
|
2015-12-17 07:35:26 +03:00
|
|
|
if (!NIL_P(input)) {
|
2008-08-28 21:13:49 +04:00
|
|
|
rb_str_drop_bytes(input, ip - (unsigned char *)RSTRING_PTR(input));
|
2015-12-17 07:35:26 +03:00
|
|
|
}
|
2008-08-12 18:46:18 +04:00
|
|
|
|
2008-08-28 21:39:02 +04:00
|
|
|
if (NIL_P(output_bytesize_v) && res == econv_destination_buffer_full) {
|
|
|
|
if (LONG_MAX / 2 < output_bytesize)
|
|
|
|
rb_raise(rb_eArgError, "too long conversion result");
|
|
|
|
output_bytesize *= 2;
|
|
|
|
output_byteoffset_v = Qnil;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
2008-08-14 20:06:33 +04:00
|
|
|
if (ec->destination_encoding) {
|
|
|
|
rb_enc_associate(output, ec->destination_encoding);
|
|
|
|
}
|
|
|
|
|
2008-08-15 13:12:56 +04:00
|
|
|
return econv_result_to_symbol(res);
|
|
|
|
}
|
|
|
|
|
2008-08-28 21:46:18 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-08-31 13:38:12 +04:00
|
|
|
* ec.convert(source_string) -> destination_string
|
2008-08-28 21:46:18 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Convert source_string and return destination_string.
|
2008-08-28 21:46:18 +04:00
|
|
|
*
|
|
|
|
* source_string is assumed as a part of source.
|
2008-09-04 17:17:40 +04:00
|
|
|
* i.e. :partial_input=>true is specified internally.
|
2008-12-30 12:03:04 +03:00
|
|
|
* finish method should be used last.
|
2008-08-28 22:00:02 +04:00
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("utf-8", "euc-jp")
|
|
|
|
* puts ec.convert("\u3042").dump #=> "\xA4\xA2"
|
|
|
|
* puts ec.finish.dump #=> ""
|
2008-08-28 21:46:18 +04:00
|
|
|
*
|
2008-08-29 20:25:27 +04:00
|
|
|
* ec = Encoding::Converter.new("euc-jp", "utf-8")
|
|
|
|
* puts ec.convert("\xA4").dump #=> ""
|
|
|
|
* puts ec.convert("\xA2").dump #=> "\xE3\x81\x82"
|
|
|
|
* puts ec.finish.dump #=> ""
|
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
|
|
|
|
* puts ec.convert("\xE3").dump #=> "".force_encoding("ISO-2022-JP")
|
|
|
|
* puts ec.convert("\x81").dump #=> "".force_encoding("ISO-2022-JP")
|
|
|
|
* puts ec.convert("\x82").dump #=> "\e$B$\"".force_encoding("ISO-2022-JP")
|
|
|
|
* puts ec.finish.dump #=> "\e(B".force_encoding("ISO-2022-JP")
|
|
|
|
*
|
2008-08-28 21:46:18 +04:00
|
|
|
* If a conversion error occur,
|
2008-09-25 16:47:30 +04:00
|
|
|
* Encoding::UndefinedConversionError or
|
2008-09-18 12:57:33 +04:00
|
|
|
* Encoding::InvalidByteSequenceError is raised.
|
2009-01-15 16:47:47 +03:00
|
|
|
* Encoding::Converter#convert doesn't supply methods to recover or restart
|
|
|
|
* from these exceptions.
|
|
|
|
* When you want to handle these conversion errors,
|
|
|
|
* use Encoding::Converter#primitive_convert.
|
2008-08-28 21:46:18 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_convert(VALUE self, VALUE source_string)
|
|
|
|
{
|
|
|
|
VALUE ret, dst;
|
|
|
|
VALUE av[5];
|
|
|
|
int ac;
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
|
|
|
|
StringValue(source_string);
|
|
|
|
|
|
|
|
dst = rb_str_new(NULL, 0);
|
|
|
|
|
|
|
|
av[0] = rb_str_dup(source_string);
|
|
|
|
av[1] = dst;
|
|
|
|
av[2] = Qnil;
|
|
|
|
av[3] = Qnil;
|
|
|
|
av[4] = INT2NUM(ECONV_PARTIAL_INPUT);
|
|
|
|
ac = 5;
|
|
|
|
|
|
|
|
ret = econv_primitive_convert(ac, av, self);
|
|
|
|
|
|
|
|
if (ret == sym_invalid_byte_sequence ||
|
|
|
|
ret == sym_undefined_conversion ||
|
|
|
|
ret == sym_incomplete_input) {
|
|
|
|
VALUE exc = make_econv_exception(ec);
|
|
|
|
rb_exc_raise(exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == sym_finished) {
|
|
|
|
rb_raise(rb_eArgError, "converter already finished");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != sym_source_buffer_empty) {
|
|
|
|
rb_bug("unexpected result of econv_primitive_convert");
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2008-08-28 22:00:02 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-08-31 13:38:12 +04:00
|
|
|
* ec.finish -> string
|
2008-08-28 22:00:02 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Finishes the converter.
|
|
|
|
* It returns the last part of the converted string.
|
2008-08-28 22:00:02 +04:00
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
|
|
|
|
* p ec.convert("\u3042") #=> "\e$B$\""
|
|
|
|
* p ec.finish #=> "\e(B"
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_finish(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE ret, dst;
|
|
|
|
VALUE av[5];
|
|
|
|
int ac;
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
|
|
|
|
dst = rb_str_new(NULL, 0);
|
|
|
|
|
|
|
|
av[0] = Qnil;
|
|
|
|
av[1] = dst;
|
|
|
|
av[2] = Qnil;
|
|
|
|
av[3] = Qnil;
|
2013-03-05 20:31:19 +04:00
|
|
|
av[4] = INT2FIX(0);
|
2008-08-28 22:00:02 +04:00
|
|
|
ac = 5;
|
|
|
|
|
|
|
|
ret = econv_primitive_convert(ac, av, self);
|
|
|
|
|
|
|
|
if (ret == sym_invalid_byte_sequence ||
|
|
|
|
ret == sym_undefined_conversion ||
|
|
|
|
ret == sym_incomplete_input) {
|
|
|
|
VALUE exc = make_econv_exception(ec);
|
|
|
|
rb_exc_raise(exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != sym_finished) {
|
|
|
|
rb_bug("unexpected result of econv_primitive_convert");
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2008-08-15 13:12:56 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-08-31 13:38:12 +04:00
|
|
|
* ec.primitive_errinfo -> array
|
2008-08-15 13:12:56 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* primitive_errinfo returns important information regarding the last error
|
|
|
|
* as a 5-element array:
|
2008-08-15 13:12:56 +04:00
|
|
|
*
|
2008-08-30 22:45:48 +04:00
|
|
|
* [result, enc1, enc2, error_bytes, readagain_bytes]
|
2008-08-15 13:12:56 +04:00
|
|
|
*
|
|
|
|
* result is the last result of primitive_convert.
|
|
|
|
*
|
|
|
|
* Other elements are only meaningful when result is
|
2008-08-26 20:09:29 +04:00
|
|
|
* :invalid_byte_sequence, :incomplete_input or :undefined_conversion.
|
2008-08-15 13:12:56 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* enc1 and enc2 indicate a conversion step as a pair of strings.
|
|
|
|
* For example, a converter from EUC-JP to ISO-8859-1 converts
|
|
|
|
* a string as follows: EUC-JP -> UTF-8 -> ISO-8859-1.
|
|
|
|
* So [enc1, enc2] is either ["EUC-JP", "UTF-8"] or ["UTF-8", "ISO-8859-1"].
|
2008-08-15 13:12:56 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* error_bytes and readagain_bytes indicate the byte sequences which caused the error.
|
2008-08-15 13:12:56 +04:00
|
|
|
* error_bytes is discarded portion.
|
|
|
|
* readagain_bytes is buffered portion which is read again on next conversion.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* # \xff is invalid as EUC-JP.
|
|
|
|
* ec = Encoding::Converter.new("EUC-JP", "Shift_JIS")
|
2009-02-22 17:23:33 +03:00
|
|
|
* ec.primitive_convert(src="\xff", dst="", nil, 10)
|
2008-08-15 13:12:56 +04:00
|
|
|
* p ec.primitive_errinfo
|
2008-08-30 22:45:48 +04:00
|
|
|
* #=> [:invalid_byte_sequence, "EUC-JP", "UTF-8", "\xFF", ""]
|
2008-08-15 13:12:56 +04:00
|
|
|
*
|
|
|
|
* # HIRAGANA LETTER A (\xa4\xa2 in EUC-JP) is not representable in ISO-8859-1.
|
|
|
|
* # Since this error is occur in UTF-8 to ISO-8859-1 conversion,
|
|
|
|
* # error_bytes is HIRAGANA LETTER A in UTF-8 (\xE3\x81\x82).
|
|
|
|
* ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
|
|
|
|
* ec.primitive_convert(src="\xa4\xa2", dst="", nil, 10)
|
|
|
|
* p ec.primitive_errinfo
|
2008-08-30 22:45:48 +04:00
|
|
|
* #=> [:undefined_conversion, "UTF-8", "ISO-8859-1", "\xE3\x81\x82", ""]
|
2008-08-15 13:12:56 +04:00
|
|
|
*
|
|
|
|
* # partial character is invalid
|
|
|
|
* ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
|
|
|
|
* ec.primitive_convert(src="\xa4", dst="", nil, 10)
|
|
|
|
* p ec.primitive_errinfo
|
2008-08-30 22:45:48 +04:00
|
|
|
* #=> [:incomplete_input, "EUC-JP", "UTF-8", "\xA4", ""]
|
2008-08-15 13:12:56 +04:00
|
|
|
*
|
|
|
|
* # Encoding::Converter::PARTIAL_INPUT prevents invalid errors by
|
|
|
|
* # partial characters.
|
|
|
|
* ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
|
2009-02-22 17:23:33 +03:00
|
|
|
* ec.primitive_convert(src="\xa4", dst="", nil, 10, Encoding::Converter::PARTIAL_INPUT)
|
2008-08-15 13:12:56 +04:00
|
|
|
* p ec.primitive_errinfo
|
2008-08-30 22:45:48 +04:00
|
|
|
* #=> [:source_buffer_empty, nil, nil, nil, nil]
|
2008-08-15 13:12:56 +04:00
|
|
|
*
|
|
|
|
* # \xd8\x00\x00@ is invalid as UTF-16BE because
|
|
|
|
* # no low surrogate after high surrogate (\xd8\x00).
|
|
|
|
* # It is detected by 3rd byte (\00) which is part of next character.
|
|
|
|
* # So the high surrogate (\xd8\x00) is discarded and
|
|
|
|
* # the 3rd byte is read again later.
|
|
|
|
* # Since the byte is buffered in ec, it is dropped from src.
|
|
|
|
* ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
|
|
|
|
* ec.primitive_convert(src="\xd8\x00\x00@", dst="", nil, 10)
|
|
|
|
* p ec.primitive_errinfo
|
2008-08-30 22:45:48 +04:00
|
|
|
* #=> [:invalid_byte_sequence, "UTF-16BE", "UTF-8", "\xD8\x00", "\x00"]
|
2008-08-15 13:12:56 +04:00
|
|
|
* p src
|
|
|
|
* #=> "@"
|
|
|
|
*
|
|
|
|
* # Similar to UTF-16BE, \x00\xd8@\x00 is invalid as UTF-16LE.
|
|
|
|
* # The problem is detected by 4th byte.
|
|
|
|
* ec = Encoding::Converter.new("UTF-16LE", "UTF-8")
|
|
|
|
* ec.primitive_convert(src="\x00\xd8@\x00", dst="", nil, 10)
|
|
|
|
* p ec.primitive_errinfo
|
2008-08-30 22:45:48 +04:00
|
|
|
* #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "@\x00"]
|
2008-08-15 13:12:56 +04:00
|
|
|
* p src
|
|
|
|
* #=> ""
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_primitive_errinfo(VALUE self)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
|
|
|
|
VALUE ary;
|
|
|
|
|
2008-08-30 22:45:48 +04:00
|
|
|
ary = rb_ary_new2(5);
|
2008-08-15 13:12:56 +04:00
|
|
|
|
|
|
|
rb_ary_store(ary, 0, econv_result_to_symbol(ec->last_error.result));
|
2008-08-30 22:45:48 +04:00
|
|
|
rb_ary_store(ary, 4, Qnil);
|
2008-08-15 13:12:56 +04:00
|
|
|
|
|
|
|
if (ec->last_error.source_encoding)
|
|
|
|
rb_ary_store(ary, 1, rb_str_new2(ec->last_error.source_encoding));
|
|
|
|
|
|
|
|
if (ec->last_error.destination_encoding)
|
|
|
|
rb_ary_store(ary, 2, rb_str_new2(ec->last_error.destination_encoding));
|
|
|
|
|
|
|
|
if (ec->last_error.error_bytes_start) {
|
|
|
|
rb_ary_store(ary, 3, rb_str_new((const char *)ec->last_error.error_bytes_start, ec->last_error.error_bytes_len));
|
|
|
|
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));
|
2008-08-12 18:46:18 +04:00
|
|
|
}
|
2008-08-15 13:12:56 +04:00
|
|
|
|
|
|
|
return ary;
|
2008-08-12 18:46:18 +04:00
|
|
|
}
|
|
|
|
|
2008-08-31 11:43:19 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-08-31 13:38:12 +04:00
|
|
|
* ec.insert_output(string) -> nil
|
2008-08-31 11:43:19 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Inserts string into the encoding converter.
|
|
|
|
* The string will be converted to the destination encoding and
|
|
|
|
* output on later conversions.
|
2008-08-31 19:31:14 +04:00
|
|
|
*
|
|
|
|
* If the destination encoding is stateful,
|
2008-12-30 12:03:04 +03:00
|
|
|
* string is converted according to the state and the state is updated.
|
2008-08-31 11:43:19 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* This method should be used only when a conversion error occurs.
|
2008-08-31 11:43:19 +04:00
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("utf-8", "iso-8859-1")
|
|
|
|
* src = "HIRAGANA LETTER A is \u{3042}."
|
|
|
|
* dst = ""
|
|
|
|
* p ec.primitive_convert(src, dst) #=> :undefined_conversion
|
|
|
|
* puts "[#{dst.dump}, #{src.dump}]" #=> ["HIRAGANA LETTER A is ", "."]
|
|
|
|
* ec.insert_output("<err>")
|
|
|
|
* p ec.primitive_convert(src, dst) #=> :finished
|
|
|
|
* puts "[#{dst.dump}, #{src.dump}]" #=> ["HIRAGANA LETTER A is <err>.", ""]
|
|
|
|
*
|
2008-08-31 19:31:14 +04:00
|
|
|
* ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
|
|
|
|
* src = "\u{306F 3041 3068 2661 3002}" # U+2661 is not representable in iso-2022-jp
|
|
|
|
* dst = ""
|
|
|
|
* p ec.primitive_convert(src, dst) #=> :undefined_conversion
|
|
|
|
* puts "[#{dst.dump}, #{src.dump}]" #=> ["\e$B$O$!$H".force_encoding("ISO-2022-JP"), "\xE3\x80\x82"]
|
|
|
|
* ec.insert_output "?" # state change required to output "?".
|
|
|
|
* p ec.primitive_convert(src, dst) #=> :finished
|
|
|
|
* puts "[#{dst.dump}, #{src.dump}]" #=> ["\e$B$O$!$H\e(B?\e$B!#\e(B".force_encoding("ISO-2022-JP"), ""]
|
|
|
|
*
|
2008-08-31 11:43:19 +04:00
|
|
|
*/
|
2008-08-15 18:17:11 +04:00
|
|
|
static VALUE
|
2008-08-30 23:23:23 +04:00
|
|
|
econv_insert_output(VALUE self, VALUE string)
|
2008-08-15 18:17:11 +04:00
|
|
|
{
|
2008-08-16 09:32:42 +04:00
|
|
|
const char *insert_enc;
|
2008-08-15 18:17:11 +04:00
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
|
|
|
|
StringValue(string);
|
2008-08-16 09:32:42 +04:00
|
|
|
insert_enc = rb_econv_encoding_to_insert_output(ec);
|
2008-09-26 14:35:00 +04:00
|
|
|
string = rb_str_encode(string, rb_enc_from_encoding(rb_enc_find(insert_enc)), 0, Qnil);
|
2008-08-15 18:17:11 +04:00
|
|
|
|
2008-08-16 09:32:42 +04:00
|
|
|
ret = rb_econv_insert_output(ec, (const unsigned char *)RSTRING_PTR(string), RSTRING_LEN(string), insert_enc);
|
2008-08-31 11:43:19 +04:00
|
|
|
if (ret == -1) {
|
|
|
|
rb_raise(rb_eArgError, "too big string");
|
|
|
|
}
|
2008-08-24 06:42:37 +04:00
|
|
|
|
2008-08-31 11:43:19 +04:00
|
|
|
return Qnil;
|
2008-08-15 18:17:11 +04:00
|
|
|
}
|
|
|
|
|
2008-08-31 11:59:03 +04:00
|
|
|
/*
|
2018-11-01 09:36:04 +03:00
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* ec.putback -> string
|
|
|
|
* ec.putback(max_numbytes) -> string
|
2008-08-31 11:59:03 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Put back the bytes which will be converted.
|
2008-08-31 11:59:03 +04:00
|
|
|
*
|
|
|
|
* The bytes are caused by invalid_byte_sequence error.
|
|
|
|
* When invalid_byte_sequence error, some bytes are discarded and
|
2008-08-31 19:31:14 +04:00
|
|
|
* some bytes are buffered to be converted later.
|
2008-08-31 11:59:03 +04:00
|
|
|
* The latter bytes can be put back.
|
|
|
|
* It can be observed by
|
2008-09-18 12:57:33 +04:00
|
|
|
* Encoding::InvalidByteSequenceError#readagain_bytes and
|
2008-08-31 11:59:03 +04:00
|
|
|
* Encoding::Converter#primitive_errinfo.
|
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("utf-16le", "iso-8859-1")
|
|
|
|
* src = "\x00\xd8\x61\x00"
|
|
|
|
* dst = ""
|
|
|
|
* p ec.primitive_convert(src, dst) #=> :invalid_byte_sequence
|
|
|
|
* p ec.primitive_errinfo #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "a\x00"]
|
|
|
|
* p ec.putback #=> "a\x00"
|
|
|
|
* p ec.putback #=> "" # no more bytes to put back
|
|
|
|
*
|
|
|
|
*/
|
2008-08-17 08:40:59 +04:00
|
|
|
static VALUE
|
2008-08-30 23:39:16 +04:00
|
|
|
econv_putback(int argc, VALUE *argv, VALUE self)
|
2008-08-17 08:40:59 +04:00
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
int n;
|
|
|
|
int putbackable;
|
2008-08-30 23:39:16 +04:00
|
|
|
VALUE str, max;
|
|
|
|
|
2018-12-06 10:49:24 +03:00
|
|
|
if (!rb_check_arity(argc, 0, 1) || NIL_P(max = argv[0])) {
|
2008-08-17 08:40:59 +04:00
|
|
|
n = rb_econv_putbackable(ec);
|
2018-12-06 10:49:24 +03:00
|
|
|
}
|
2008-08-17 08:40:59 +04:00
|
|
|
else {
|
|
|
|
n = NUM2INT(max);
|
|
|
|
putbackable = rb_econv_putbackable(ec);
|
|
|
|
if (putbackable < n)
|
|
|
|
n = putbackable;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = rb_str_new(NULL, n);
|
|
|
|
rb_econv_putback(ec, (unsigned char *)RSTRING_PTR(str), n);
|
|
|
|
|
2008-08-31 11:59:03 +04:00
|
|
|
if (ec->source_encoding) {
|
|
|
|
rb_enc_associate(str, ec->source_encoding);
|
|
|
|
}
|
|
|
|
|
2008-08-17 08:40:59 +04:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-08-31 09:27:52 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-08-31 13:38:12 +04:00
|
|
|
* ec.last_error -> exception or nil
|
2008-08-31 09:27:52 +04:00
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns an exception object for the last conversion.
|
2009-02-22 17:23:33 +03:00
|
|
|
* Returns nil if the last conversion did not produce an error.
|
2008-08-31 09:27:52 +04:00
|
|
|
*
|
|
|
|
* "error" means that
|
2008-09-25 16:47:30 +04:00
|
|
|
* Encoding::InvalidByteSequenceError and Encoding::UndefinedConversionError for
|
2008-08-31 09:27:52 +04:00
|
|
|
* Encoding::Converter#convert and
|
|
|
|
* :invalid_byte_sequence, :incomplete_input and :undefined_conversion for
|
|
|
|
* Encoding::Converter#primitive_convert.
|
|
|
|
*
|
2008-09-03 20:34:11 +04:00
|
|
|
* ec = Encoding::Converter.new("utf-8", "iso-8859-1")
|
|
|
|
* p ec.primitive_convert(src="\xf1abcd", dst="") #=> :invalid_byte_sequence
|
2008-09-18 12:57:33 +04:00
|
|
|
* p ec.last_error #=> #<Encoding::InvalidByteSequenceError: "\xF1" followed by "a" on UTF-8>
|
2008-09-03 20:34:11 +04:00
|
|
|
* p ec.primitive_convert(src, dst, nil, 1) #=> :destination_buffer_full
|
|
|
|
* p ec.last_error #=> nil
|
2008-08-31 09:27:52 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_last_error(VALUE self)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
VALUE exc;
|
|
|
|
|
|
|
|
exc = make_econv_exception(ec);
|
|
|
|
if (NIL_P(exc))
|
|
|
|
return Qnil;
|
|
|
|
return exc;
|
|
|
|
}
|
|
|
|
|
2008-09-03 20:34:11 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ec.replacement -> string
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns the replacement string.
|
2008-09-03 20:34:11 +04:00
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("euc-jp", "us-ascii")
|
|
|
|
* p ec.replacement #=> "?"
|
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("euc-jp", "utf-8")
|
|
|
|
* p ec.replacement #=> "\uFFFD"
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_get_replacement(VALUE self)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
int ret;
|
|
|
|
rb_encoding *enc;
|
|
|
|
|
|
|
|
ret = make_replacement(ec);
|
|
|
|
if (ret == -1) {
|
2008-09-25 16:47:30 +04:00
|
|
|
rb_raise(rb_eUndefinedConversionError, "replacement character setup failed");
|
2008-09-03 20:34:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
enc = rb_enc_find(ec->replacement_enc);
|
|
|
|
return rb_enc_str_new((const char *)ec->replacement_str, (long)ec->replacement_len, enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ec.replacement = string
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Sets the replacement string.
|
2008-09-03 20:34:11 +04:00
|
|
|
*
|
2008-09-04 16:08:54 +04:00
|
|
|
* ec = Encoding::Converter.new("utf-8", "us-ascii", :undef => :replace)
|
2008-09-03 20:34:11 +04:00
|
|
|
* ec.replacement = "<undef>"
|
|
|
|
* p ec.convert("a \u3042 b") #=> "a <undef> b"
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_set_replacement(VALUE self, VALUE arg)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
VALUE string = arg;
|
|
|
|
int ret;
|
|
|
|
rb_encoding *enc;
|
|
|
|
|
|
|
|
StringValue(string);
|
|
|
|
enc = rb_enc_get(string);
|
|
|
|
|
2008-09-07 21:08:38 +04:00
|
|
|
ret = rb_econv_set_replacement(ec,
|
2008-09-03 20:34:11 +04:00
|
|
|
(const unsigned char *)RSTRING_PTR(string),
|
|
|
|
RSTRING_LEN(string),
|
2008-09-13 13:06:51 +04:00
|
|
|
rb_enc_name(enc));
|
2008-09-03 20:34:11 +04:00
|
|
|
|
|
|
|
if (ret == -1) {
|
2008-09-18 12:57:33 +04:00
|
|
|
/* xxx: rb_eInvalidByteSequenceError? */
|
2008-09-25 16:47:30 +04:00
|
|
|
rb_raise(rb_eUndefinedConversionError, "replacement character setup failed");
|
2008-09-03 20:34:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2008-12-26 08:04:39 +03:00
|
|
|
VALUE
|
|
|
|
rb_econv_make_exception(rb_econv_t *ec)
|
|
|
|
{
|
|
|
|
return make_econv_exception(ec);
|
|
|
|
}
|
|
|
|
|
2008-08-16 19:04:39 +04:00
|
|
|
void
|
|
|
|
rb_econv_check_error(rb_econv_t *ec)
|
|
|
|
{
|
|
|
|
VALUE exc;
|
|
|
|
|
|
|
|
exc = make_econv_exception(ec);
|
|
|
|
if (NIL_P(exc))
|
|
|
|
return;
|
|
|
|
rb_exc_raise(exc);
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:38:12 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ecerr.source_encoding_name -> string
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns the source encoding name as a string.
|
2008-08-31 13:38:12 +04:00
|
|
|
*/
|
2008-08-17 07:05:40 +04:00
|
|
|
static VALUE
|
2008-08-31 12:17:48 +04:00
|
|
|
ecerr_source_encoding_name(VALUE self)
|
2008-08-17 07:05:40 +04:00
|
|
|
{
|
2008-08-31 12:17:48 +04:00
|
|
|
return rb_attr_get(self, rb_intern("source_encoding_name"));
|
2008-08-17 07:05:40 +04:00
|
|
|
}
|
|
|
|
|
2008-08-31 13:38:12 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ecerr.source_encoding -> encoding
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns the source encoding as an encoding object.
|
2008-08-31 13:38:12 +04:00
|
|
|
*
|
|
|
|
* Note that the result may not be equal to the source encoding of
|
|
|
|
* the encoding converter if the conversion has multiple steps.
|
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP") # ISO-8859-1 -> UTF-8 -> EUC-JP
|
|
|
|
* begin
|
|
|
|
* ec.convert("\xa0") # NO-BREAK SPACE, which is available in UTF-8 but not in EUC-JP.
|
2008-09-25 16:47:30 +04:00
|
|
|
* rescue Encoding::UndefinedConversionError
|
2008-08-31 13:38:12 +04:00
|
|
|
* p $!.source_encoding #=> #<Encoding:UTF-8>
|
|
|
|
* p $!.destination_encoding #=> #<Encoding:EUC-JP>
|
|
|
|
* p $!.source_encoding_name #=> "UTF-8"
|
|
|
|
* p $!.destination_encoding_name #=> "EUC-JP"
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
*/
|
2008-08-31 13:08:31 +04:00
|
|
|
static VALUE
|
|
|
|
ecerr_source_encoding(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("source_encoding"));
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:38:12 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ecerr.destination_encoding_name -> string
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns the destination encoding name as a string.
|
2008-08-31 13:38:12 +04:00
|
|
|
*/
|
2008-08-17 07:05:40 +04:00
|
|
|
static VALUE
|
2008-08-31 12:17:48 +04:00
|
|
|
ecerr_destination_encoding_name(VALUE self)
|
2008-08-17 07:05:40 +04:00
|
|
|
{
|
2008-08-31 12:17:48 +04:00
|
|
|
return rb_attr_get(self, rb_intern("destination_encoding_name"));
|
2008-08-17 07:05:40 +04:00
|
|
|
}
|
|
|
|
|
2008-08-31 13:38:12 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ecerr.destination_encoding -> string
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns the destination encoding as an encoding object.
|
2008-08-31 13:38:12 +04:00
|
|
|
*/
|
2008-08-31 13:08:31 +04:00
|
|
|
static VALUE
|
|
|
|
ecerr_destination_encoding(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("destination_encoding"));
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:38:12 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ecerr.error_char -> string
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns the one-character string which cause Encoding::UndefinedConversionError.
|
2008-08-31 13:38:12 +04:00
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP")
|
|
|
|
* begin
|
|
|
|
* ec.convert("\xa0")
|
2008-09-25 16:47:30 +04:00
|
|
|
* rescue Encoding::UndefinedConversionError
|
2008-08-31 13:38:12 +04:00
|
|
|
* puts $!.error_char.dump #=> "\xC2\xA0"
|
|
|
|
* p $!.error_char.encoding #=> #<Encoding:UTF-8>
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
*/
|
2008-08-17 07:05:40 +04:00
|
|
|
static VALUE
|
|
|
|
ecerr_error_char(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("error_char"));
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:38:12 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ecerr.error_bytes -> string
|
|
|
|
*
|
2008-12-30 12:03:04 +03:00
|
|
|
* Returns the discarded bytes when Encoding::InvalidByteSequenceError occurs.
|
2008-08-31 13:38:12 +04:00
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
|
|
|
|
* begin
|
|
|
|
* ec.convert("abc\xA1\xFFdef")
|
2008-09-18 12:57:33 +04:00
|
|
|
* rescue Encoding::InvalidByteSequenceError
|
|
|
|
* p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "\xFF" on EUC-JP>
|
2008-08-31 13:38:12 +04:00
|
|
|
* puts $!.error_bytes.dump #=> "\xA1"
|
|
|
|
* puts $!.readagain_bytes.dump #=> "\xFF"
|
|
|
|
* end
|
|
|
|
*/
|
2008-08-17 07:05:40 +04:00
|
|
|
static VALUE
|
|
|
|
ecerr_error_bytes(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("error_bytes"));
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:38:12 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ecerr.readagain_bytes -> string
|
|
|
|
*
|
2009-01-14 14:12:30 +03:00
|
|
|
* Returns the bytes to be read again when Encoding::InvalidByteSequenceError occurs.
|
2008-08-31 13:38:12 +04:00
|
|
|
*/
|
2008-08-25 19:26:54 +04:00
|
|
|
static VALUE
|
|
|
|
ecerr_readagain_bytes(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("readagain_bytes"));
|
|
|
|
}
|
|
|
|
|
2008-08-31 13:38:12 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ecerr.incomplete_input? -> true or false
|
|
|
|
*
|
2009-01-14 14:12:30 +03:00
|
|
|
* Returns true if the invalid byte sequence error is caused by
|
2008-08-31 13:38:12 +04:00
|
|
|
* premature end of string.
|
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
|
|
|
|
*
|
|
|
|
* begin
|
|
|
|
* ec.convert("abc\xA1z")
|
2008-09-18 12:57:33 +04:00
|
|
|
* rescue Encoding::InvalidByteSequenceError
|
|
|
|
* p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "z" on EUC-JP>
|
2008-08-31 13:38:12 +04:00
|
|
|
* p $!.incomplete_input? #=> false
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* begin
|
|
|
|
* ec.convert("abc\xA1")
|
|
|
|
* ec.finish
|
2008-09-18 12:57:33 +04:00
|
|
|
* rescue Encoding::InvalidByteSequenceError
|
|
|
|
* p $! #=> #<Encoding::InvalidByteSequenceError: incomplete "\xA1" on EUC-JP>
|
2008-08-31 13:38:12 +04:00
|
|
|
* p $!.incomplete_input? #=> true
|
|
|
|
* end
|
|
|
|
*/
|
2008-08-26 20:09:29 +04:00
|
|
|
static VALUE
|
|
|
|
ecerr_incomplete_input(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("incomplete_input"));
|
|
|
|
}
|
|
|
|
|
2010-05-08 08:50:09 +04:00
|
|
|
/*
|
|
|
|
* Document-class: Encoding::UndefinedConversionError
|
|
|
|
*
|
|
|
|
* Raised by Encoding and String methods when a transcoding operation
|
|
|
|
* fails.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-class: Encoding::InvalidByteSequenceError
|
|
|
|
*
|
|
|
|
* Raised by Encoding and String methods when the string being
|
|
|
|
* transcoded contains a byte invalid for the either the source or
|
|
|
|
* target encoding.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-class: Encoding::ConverterNotFoundError
|
|
|
|
*
|
|
|
|
* Raised by transcoding methods when a named encoding does not
|
|
|
|
* correspond with a known converter.
|
|
|
|
*/
|
|
|
|
|
2014-07-03 07:38:10 +04:00
|
|
|
#undef rb_intern
|
2007-12-10 08:01:47 +03:00
|
|
|
void
|
|
|
|
Init_transcode(void)
|
|
|
|
{
|
2007-12-25 08:57:04 +03:00
|
|
|
transcoder_table = st_init_strcasetable();
|
|
|
|
|
2008-02-21 11:42:10 +03:00
|
|
|
sym_invalid = ID2SYM(rb_intern("invalid"));
|
2008-07-14 10:27:26 +04:00
|
|
|
sym_undef = ID2SYM(rb_intern("undef"));
|
|
|
|
sym_replace = ID2SYM(rb_intern("replace"));
|
2010-04-13 05:26:46 +04:00
|
|
|
sym_fallback = ID2SYM(rb_intern("fallback"));
|
2010-12-07 16:05:26 +03:00
|
|
|
sym_aref = ID2SYM(rb_intern("[]"));
|
2008-09-06 18:05:10 +04:00
|
|
|
sym_xml = ID2SYM(rb_intern("xml"));
|
2008-09-06 10:07:34 +04:00
|
|
|
sym_text = ID2SYM(rb_intern("text"));
|
|
|
|
sym_attr = ID2SYM(rb_intern("attr"));
|
2008-02-21 11:42:10 +03:00
|
|
|
|
2008-08-28 20:59:17 +04:00
|
|
|
sym_invalid_byte_sequence = ID2SYM(rb_intern("invalid_byte_sequence"));
|
|
|
|
sym_undefined_conversion = ID2SYM(rb_intern("undefined_conversion"));
|
|
|
|
sym_destination_buffer_full = ID2SYM(rb_intern("destination_buffer_full"));
|
|
|
|
sym_source_buffer_empty = ID2SYM(rb_intern("source_buffer_empty"));
|
|
|
|
sym_finished = ID2SYM(rb_intern("finished"));
|
2008-09-09 20:27:02 +04:00
|
|
|
sym_after_output = ID2SYM(rb_intern("after_output"));
|
2008-08-28 20:59:17 +04:00
|
|
|
sym_incomplete_input = ID2SYM(rb_intern("incomplete_input"));
|
2008-09-09 16:22:43 +04:00
|
|
|
sym_universal_newline = ID2SYM(rb_intern("universal_newline"));
|
|
|
|
sym_crlf_newline = ID2SYM(rb_intern("crlf_newline"));
|
|
|
|
sym_cr_newline = ID2SYM(rb_intern("cr_newline"));
|
2008-09-04 16:48:21 +04:00
|
|
|
sym_partial_input = ID2SYM(rb_intern("partial_input"));
|
2008-08-28 20:59:17 +04:00
|
|
|
|
2011-04-26 19:55:58 +04:00
|
|
|
#ifdef ENABLE_ECONV_NEWLINE_OPTION
|
|
|
|
sym_newline = ID2SYM(rb_intern("newline"));
|
|
|
|
sym_universal = ID2SYM(rb_intern("universal"));
|
|
|
|
sym_crlf = ID2SYM(rb_intern("crlf"));
|
|
|
|
sym_cr = ID2SYM(rb_intern("cr"));
|
|
|
|
sym_lf = ID2SYM(rb_intern("lf"));
|
|
|
|
#endif
|
|
|
|
|
2014-07-03 07:38:10 +04:00
|
|
|
InitVM(transcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InitVM_transcode(void)
|
|
|
|
{
|
|
|
|
rb_eUndefinedConversionError = rb_define_class_under(rb_cEncoding, "UndefinedConversionError", rb_eEncodingError);
|
|
|
|
rb_eInvalidByteSequenceError = rb_define_class_under(rb_cEncoding, "InvalidByteSequenceError", rb_eEncodingError);
|
|
|
|
rb_eConverterNotFoundError = rb_define_class_under(rb_cEncoding, "ConverterNotFoundError", rb_eEncodingError);
|
|
|
|
|
2008-05-20 20:37:13 +04:00
|
|
|
rb_define_method(rb_cString, "encode", str_encode, -1);
|
|
|
|
rb_define_method(rb_cString, "encode!", str_encode_bang, -1);
|
2008-08-12 18:46:18 +04:00
|
|
|
|
|
|
|
rb_cEncodingConverter = rb_define_class_under(rb_cEncoding, "Converter", rb_cData);
|
|
|
|
rb_define_alloc_func(rb_cEncodingConverter, econv_s_allocate);
|
2008-09-08 18:33:17 +04:00
|
|
|
rb_define_singleton_method(rb_cEncodingConverter, "asciicompat_encoding", econv_s_asciicompat_encoding, 1);
|
2008-09-12 17:53:42 +04:00
|
|
|
rb_define_singleton_method(rb_cEncodingConverter, "search_convpath", econv_s_search_convpath, -1);
|
2008-08-13 10:08:56 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "initialize", econv_init, -1);
|
2008-08-13 12:47:57 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "inspect", econv_inspect, 0);
|
2008-09-10 21:15:08 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "convpath", econv_convpath, 0);
|
2008-08-14 18:28:10 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "source_encoding", econv_source_encoding, 0);
|
|
|
|
rb_define_method(rb_cEncodingConverter, "destination_encoding", econv_destination_encoding, 0);
|
2008-08-13 10:08:56 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "primitive_convert", econv_primitive_convert, -1);
|
2008-08-28 21:46:18 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "convert", econv_convert, 1);
|
2008-08-28 22:00:02 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "finish", econv_finish, 0);
|
2008-08-15 13:12:56 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "primitive_errinfo", econv_primitive_errinfo, 0);
|
2008-08-30 23:23:23 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "insert_output", econv_insert_output, 1);
|
2008-08-30 23:39:16 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "putback", econv_putback, -1);
|
2008-08-31 09:27:52 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "last_error", econv_last_error, 0);
|
2008-09-03 20:34:11 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "replacement", econv_get_replacement, 0);
|
|
|
|
rb_define_method(rb_cEncodingConverter, "replacement=", econv_set_replacement, 1);
|
2011-09-06 07:07:16 +04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "==", econv_equal, 1);
|
2008-09-06 00:24:18 +04:00
|
|
|
|
2013-01-26 07:58:33 +04:00
|
|
|
/* Document-const: INVALID_MASK
|
|
|
|
*
|
|
|
|
* Mask for invalid byte sequences
|
|
|
|
*/
|
2008-09-04 16:55:02 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "INVALID_MASK", INT2FIX(ECONV_INVALID_MASK));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: INVALID_REPLACE
|
|
|
|
*
|
|
|
|
* Replace invalid byte sequences
|
|
|
|
*/
|
2008-09-04 16:55:02 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "INVALID_REPLACE", INT2FIX(ECONV_INVALID_REPLACE));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: UNDEF_MASK
|
|
|
|
*
|
|
|
|
* Mask for a valid character in the source encoding but no related
|
|
|
|
* character(s) in destination encoding.
|
|
|
|
*/
|
2008-09-04 16:55:02 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "UNDEF_MASK", INT2FIX(ECONV_UNDEF_MASK));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: UNDEF_REPLACE
|
|
|
|
*
|
|
|
|
* Replace byte sequences that are undefined in the destination encoding.
|
|
|
|
*/
|
2008-09-04 16:55:02 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "UNDEF_REPLACE", INT2FIX(ECONV_UNDEF_REPLACE));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: UNDEF_HEX_CHARREF
|
|
|
|
*
|
|
|
|
* Replace byte sequences that are undefined in the destination encoding
|
|
|
|
* with an XML hexadecimal character reference. This is valid for XML
|
|
|
|
* conversion.
|
|
|
|
*/
|
2008-09-06 00:24:18 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "UNDEF_HEX_CHARREF", INT2FIX(ECONV_UNDEF_HEX_CHARREF));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: PARTIAL_INPUT
|
|
|
|
*
|
|
|
|
* Indicates the source may be part of a larger string. See
|
|
|
|
* primitive_convert for an example.
|
|
|
|
*/
|
2008-08-14 18:48:41 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "PARTIAL_INPUT", INT2FIX(ECONV_PARTIAL_INPUT));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: AFTER_OUTPUT
|
|
|
|
*
|
|
|
|
* Stop converting after some output is complete but before all of the
|
|
|
|
* input was consumed. See primitive_convert for an example.
|
|
|
|
*/
|
2008-09-09 20:27:02 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "AFTER_OUTPUT", INT2FIX(ECONV_AFTER_OUTPUT));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: UNIVERSAL_NEWLINE_DECORATOR
|
|
|
|
*
|
|
|
|
* Decorator for converting CRLF and CR to LF
|
|
|
|
*/
|
2008-09-09 16:22:43 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "UNIVERSAL_NEWLINE_DECORATOR", INT2FIX(ECONV_UNIVERSAL_NEWLINE_DECORATOR));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: CRLF_NEWLINE_DECORATOR
|
|
|
|
*
|
|
|
|
* Decorator for converting LF to CRLF
|
|
|
|
*/
|
2008-09-09 16:22:43 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "CRLF_NEWLINE_DECORATOR", INT2FIX(ECONV_CRLF_NEWLINE_DECORATOR));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: CR_NEWLINE_DECORATOR
|
|
|
|
*
|
|
|
|
* Decorator for converting LF to CR
|
|
|
|
*/
|
2008-09-09 16:22:43 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "CR_NEWLINE_DECORATOR", INT2FIX(ECONV_CR_NEWLINE_DECORATOR));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: XML_TEXT_DECORATOR
|
|
|
|
*
|
|
|
|
* Escape as XML CharData
|
|
|
|
*/
|
2008-09-09 16:22:43 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "XML_TEXT_DECORATOR", INT2FIX(ECONV_XML_TEXT_DECORATOR));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: XML_ATTR_CONTENT_DECORATOR
|
|
|
|
*
|
|
|
|
* Escape as XML AttValue
|
|
|
|
*/
|
2008-09-09 16:22:43 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "XML_ATTR_CONTENT_DECORATOR", INT2FIX(ECONV_XML_ATTR_CONTENT_DECORATOR));
|
2013-01-26 07:58:33 +04:00
|
|
|
|
|
|
|
/* Document-const: XML_ATTR_QUOTE_DECORATOR
|
|
|
|
*
|
|
|
|
* Escape as XML AttValue
|
|
|
|
*/
|
2008-09-09 16:22:43 +04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "XML_ATTR_QUOTE_DECORATOR", INT2FIX(ECONV_XML_ATTR_QUOTE_DECORATOR));
|
2008-08-17 07:05:40 +04:00
|
|
|
|
2008-09-25 16:47:30 +04:00
|
|
|
rb_define_method(rb_eUndefinedConversionError, "source_encoding_name", ecerr_source_encoding_name, 0);
|
|
|
|
rb_define_method(rb_eUndefinedConversionError, "destination_encoding_name", ecerr_destination_encoding_name, 0);
|
|
|
|
rb_define_method(rb_eUndefinedConversionError, "source_encoding", ecerr_source_encoding, 0);
|
|
|
|
rb_define_method(rb_eUndefinedConversionError, "destination_encoding", ecerr_destination_encoding, 0);
|
|
|
|
rb_define_method(rb_eUndefinedConversionError, "error_char", ecerr_error_char, 0);
|
2008-09-18 12:57:33 +04:00
|
|
|
|
|
|
|
rb_define_method(rb_eInvalidByteSequenceError, "source_encoding_name", ecerr_source_encoding_name, 0);
|
|
|
|
rb_define_method(rb_eInvalidByteSequenceError, "destination_encoding_name", ecerr_destination_encoding_name, 0);
|
|
|
|
rb_define_method(rb_eInvalidByteSequenceError, "source_encoding", ecerr_source_encoding, 0);
|
|
|
|
rb_define_method(rb_eInvalidByteSequenceError, "destination_encoding", ecerr_destination_encoding, 0);
|
|
|
|
rb_define_method(rb_eInvalidByteSequenceError, "error_bytes", ecerr_error_bytes, 0);
|
|
|
|
rb_define_method(rb_eInvalidByteSequenceError, "readagain_bytes", ecerr_readagain_bytes, 0);
|
|
|
|
rb_define_method(rb_eInvalidByteSequenceError, "incomplete_input?", ecerr_incomplete_input, 0);
|
2008-08-24 19:58:43 +04:00
|
|
|
|
|
|
|
Init_newline();
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|