2007-12-10 00:01:47 -05:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
transcode.c -
|
|
|
|
|
2007-12-10 03:25:01 -05:00
|
|
|
$Author$
|
2007-12-10 00:01:47 -05:00
|
|
|
created at: Tue Oct 30 16:10:22 JST 2007
|
|
|
|
|
|
|
|
Copyright (C) 2007 Martin Duerst
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "ruby/encoding.h"
|
2007-12-25 00:57:04 -05:00
|
|
|
#define PType (int)
|
2007-12-10 00:01:47 -05:00
|
|
|
#include "transcode_data.h"
|
2007-12-25 00:57:04 -05:00
|
|
|
#include <ctype.h>
|
2007-12-10 00:01:47 -05:00
|
|
|
|
2008-08-12 03:20:10 -04:00
|
|
|
VALUE rb_eConversionUndefined;
|
|
|
|
VALUE rb_eInvalidByteSequence;
|
|
|
|
|
2008-08-12 10:46:18 -04:00
|
|
|
VALUE rb_cEncodingConverter;
|
|
|
|
|
2008-07-14 02:27:26 -04:00
|
|
|
static VALUE sym_invalid, sym_undef, sym_ignore, sym_replace;
|
2008-08-14 07:31:27 -04:00
|
|
|
#define INVALID_IGNORE 0x1
|
|
|
|
#define INVALID_REPLACE 0x2
|
|
|
|
#define UNDEF_IGNORE 0x10
|
|
|
|
#define UNDEF_REPLACE 0x20
|
2008-02-21 03:42:10 -05:00
|
|
|
|
2007-12-10 00:01:47 -05:00
|
|
|
/*
|
|
|
|
* Dispatch data and logic
|
|
|
|
*/
|
|
|
|
|
2008-08-07 10:53:30 -04:00
|
|
|
typedef struct {
|
|
|
|
const char *from;
|
|
|
|
const char *to;
|
|
|
|
const char *lib; /* maybe null. it means that don't load the library. */
|
|
|
|
const rb_transcoder *transcoder;
|
|
|
|
} transcoder_entry_t;
|
2007-12-25 00:57:04 -05:00
|
|
|
|
2008-08-07 10:53:30 -04:00
|
|
|
static st_table *transcoder_table;
|
2007-12-25 00:57:04 -05:00
|
|
|
|
2008-08-07 10:53:30 -04:00
|
|
|
static transcoder_entry_t *
|
|
|
|
make_transcoder_entry(const char *from, const char *to)
|
2007-12-10 00:01:47 -05:00
|
|
|
{
|
2008-08-07 10:53:30 -04:00
|
|
|
st_data_t val;
|
|
|
|
st_table *table2;
|
|
|
|
|
|
|
|
if (!st_lookup(transcoder_table, (st_data_t)from, &val)) {
|
|
|
|
val = (st_data_t)st_init_strcasetable();
|
|
|
|
st_add_direct(transcoder_table, (st_data_t)from, val);
|
|
|
|
}
|
|
|
|
table2 = (st_table *)val;
|
|
|
|
if (!st_lookup(table2, (st_data_t)to, &val)) {
|
|
|
|
transcoder_entry_t *entry = ALLOC(transcoder_entry_t);
|
|
|
|
entry->from = from;
|
|
|
|
entry->to = to;
|
|
|
|
entry->lib = NULL;
|
|
|
|
entry->transcoder = NULL;
|
|
|
|
val = (st_data_t)entry;
|
|
|
|
st_add_direct(table2, (st_data_t)to, val);
|
|
|
|
}
|
|
|
|
return (transcoder_entry_t *)val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static transcoder_entry_t *
|
|
|
|
get_transcoder_entry(const char *from, const char *to)
|
|
|
|
{
|
|
|
|
st_data_t val;
|
|
|
|
st_table *table2;
|
|
|
|
|
|
|
|
if (!st_lookup(transcoder_table, (st_data_t)from, &val)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
table2 = (st_table *)val;
|
|
|
|
if (!st_lookup(table2, (st_data_t)to, &val)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (transcoder_entry_t *)val;
|
2007-12-25 00:57:04 -05:00
|
|
|
}
|
2007-12-10 00:01:47 -05:00
|
|
|
|
2007-12-25 00:57:04 -05:00
|
|
|
void
|
|
|
|
rb_register_transcoder(const rb_transcoder *tr)
|
|
|
|
{
|
|
|
|
const char *const from_e = tr->from_encoding;
|
|
|
|
const char *const to_e = tr->to_encoding;
|
|
|
|
|
2008-08-07 10:53:30 -04:00
|
|
|
transcoder_entry_t *entry;
|
|
|
|
|
|
|
|
entry = make_transcoder_entry(from_e, to_e);
|
|
|
|
if (entry->transcoder) {
|
2007-12-25 00:57:04 -05:00
|
|
|
rb_raise(rb_eArgError, "transcoder from %s to %s has been already registered",
|
|
|
|
from_e, to_e);
|
|
|
|
}
|
2008-08-07 10:53:30 -04:00
|
|
|
|
|
|
|
entry->transcoder = tr;
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
|
|
|
|
2007-12-24 08:51:19 -05:00
|
|
|
static void
|
2007-12-25 00:57:04 -05:00
|
|
|
declare_transcoder(const char *to, const char *from, const char *lib)
|
2007-12-24 08:51:19 -05:00
|
|
|
{
|
2008-08-07 10:53:30 -04:00
|
|
|
transcoder_entry_t *entry;
|
2007-12-25 00:57:04 -05:00
|
|
|
|
2008-08-07 10:53:30 -04:00
|
|
|
entry = make_transcoder_entry(from, to);
|
|
|
|
entry->lib = lib;
|
2007-12-24 08:51:19 -05:00
|
|
|
}
|
|
|
|
|
2007-12-25 00:57:04 -05:00
|
|
|
#define MAX_TRANSCODER_LIBNAME_LEN 64
|
|
|
|
static const char transcoder_lib_prefix[] = "enc/trans/";
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_declare_transcoder(const char *enc1, const char *enc2, const char *lib)
|
2007-12-10 00:01:47 -05:00
|
|
|
{
|
2007-12-25 00:57:04 -05:00
|
|
|
if (!lib || strlen(lib) > MAX_TRANSCODER_LIBNAME_LEN) {
|
|
|
|
rb_raise(rb_eArgError, "invalid library name - %s",
|
|
|
|
lib ? lib : "(null)");
|
|
|
|
}
|
|
|
|
declare_transcoder(enc1, enc2, lib);
|
|
|
|
declare_transcoder(enc2, enc1, lib);
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
|
|
|
|
2008-01-01 07:24:04 -05:00
|
|
|
#define encoding_equal(enc1, enc2) (STRCASECMP(enc1, enc2) == 0)
|
2007-12-25 00:57:04 -05:00
|
|
|
|
2008-08-07 10: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)
|
|
|
|
{
|
|
|
|
const char *to = (const char *)key;
|
|
|
|
search_path_bfs_t *bfs = (search_path_bfs_t *)arg;
|
|
|
|
search_path_queue_t *q;
|
|
|
|
|
|
|
|
if (st_lookup(bfs->visited, (st_data_t)to, &val)) {
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
q = ALLOC(search_path_queue_t);
|
|
|
|
q->enc = to;
|
|
|
|
q->next = NULL;
|
|
|
|
*bfs->queue_last_ptr = q;
|
|
|
|
bfs->queue_last_ptr = &q->next;
|
|
|
|
|
|
|
|
st_add_direct(bfs->visited, (st_data_t)to, (st_data_t)bfs->base_enc);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
transcode_search_path(const char *from, const char *to,
|
|
|
|
void (*callback)(const char *from, const char *to, int depth, void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
search_path_bfs_t bfs;
|
|
|
|
search_path_queue_t *q;
|
|
|
|
st_data_t val;
|
|
|
|
st_table *table2;
|
|
|
|
int found;
|
2008-08-12 22:40:24 -04:00
|
|
|
int pathlen;
|
2008-08-07 10:53:30 -04:00
|
|
|
|
2008-08-18 00:22:31 -04:00
|
|
|
if (encoding_equal(from, to))
|
|
|
|
return -1;
|
|
|
|
|
2008-08-07 10:53:30 -04:00
|
|
|
q = ALLOC(search_path_queue_t);
|
|
|
|
q->enc = from;
|
|
|
|
q->next = NULL;
|
|
|
|
bfs.queue_last_ptr = &q->next;
|
|
|
|
bfs.queue = q;
|
|
|
|
|
|
|
|
bfs.visited = st_init_strcasetable();
|
|
|
|
st_add_direct(bfs.visited, (st_data_t)from, (st_data_t)NULL);
|
|
|
|
|
|
|
|
while (bfs.queue) {
|
|
|
|
q = bfs.queue;
|
|
|
|
bfs.queue = q->next;
|
|
|
|
if (!bfs.queue)
|
|
|
|
bfs.queue_last_ptr = &bfs.queue;
|
|
|
|
|
|
|
|
if (!st_lookup(transcoder_table, (st_data_t)q->enc, &val)) {
|
|
|
|
xfree(q);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
table2 = (st_table *)val;
|
|
|
|
|
|
|
|
if (st_lookup(table2, (st_data_t)to, &val)) {
|
|
|
|
st_add_direct(bfs.visited, (st_data_t)to, (st_data_t)q->enc);
|
|
|
|
xfree(q);
|
|
|
|
found = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
bfs.base_enc = q->enc;
|
|
|
|
st_foreach(table2, transcode_search_path_i, (st_data_t)&bfs);
|
|
|
|
bfs.base_enc = NULL;
|
|
|
|
|
|
|
|
xfree(q);
|
|
|
|
}
|
|
|
|
found = 0;
|
|
|
|
|
2008-08-14 01:56:51 -04:00
|
|
|
cleanup:
|
2008-08-07 10:53:30 -04:00
|
|
|
while (bfs.queue) {
|
|
|
|
q = bfs.queue;
|
|
|
|
bfs.queue = q->next;
|
|
|
|
xfree(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
const char *enc = to;
|
2008-08-12 22:40:24 -04:00
|
|
|
int depth;
|
2008-08-14 01:56:51 -04:00
|
|
|
pathlen = 0;
|
2008-08-07 10:53:30 -04:00
|
|
|
while (1) {
|
|
|
|
st_lookup(bfs.visited, (st_data_t)enc, &val);
|
|
|
|
if (!val)
|
|
|
|
break;
|
2008-08-12 22:40:24 -04:00
|
|
|
pathlen++;
|
2008-08-07 10:53:30 -04:00
|
|
|
enc = (const char *)val;
|
|
|
|
}
|
2008-08-12 22:40:24 -04:00
|
|
|
depth = pathlen;
|
2008-08-07 10:53:30 -04:00
|
|
|
enc = to;
|
|
|
|
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-08-12 22:40:24 -04:00
|
|
|
if (found)
|
|
|
|
return pathlen;
|
|
|
|
else
|
|
|
|
return -1;
|
2008-08-07 10:53:30 -04:00
|
|
|
}
|
|
|
|
|
2008-08-10 08:26:01 -04:00
|
|
|
static const rb_transcoder *
|
2008-08-12 22:40:24 -04:00
|
|
|
load_transcoder_entry(transcoder_entry_t *entry)
|
2008-08-10 08:26:01 -04:00
|
|
|
{
|
|
|
|
if (entry->transcoder)
|
|
|
|
return entry->transcoder;
|
|
|
|
|
|
|
|
if (entry->lib) {
|
|
|
|
const char *lib = entry->lib;
|
|
|
|
int len = strlen(lib);
|
|
|
|
char path[sizeof(transcoder_lib_prefix) + MAX_TRANSCODER_LIBNAME_LEN];
|
|
|
|
|
|
|
|
entry->lib = NULL;
|
|
|
|
|
|
|
|
if (len > MAX_TRANSCODER_LIBNAME_LEN)
|
|
|
|
return NULL;
|
|
|
|
memcpy(path, transcoder_lib_prefix, sizeof(transcoder_lib_prefix) - 1);
|
|
|
|
memcpy(path + sizeof(transcoder_lib_prefix) - 1, lib, len + 1);
|
|
|
|
if (!rb_require(path))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->transcoder)
|
|
|
|
return entry->transcoder;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-08-11 18:21:15 -04:00
|
|
|
static const char*
|
2008-08-15 07:02:07 -04:00
|
|
|
get_replacement_character(rb_encoding *enc, int *len_ret, const char **repl_enc_ptr)
|
2008-07-14 02:27:26 -04:00
|
|
|
{
|
|
|
|
static rb_encoding *utf16be_encoding, *utf16le_encoding;
|
|
|
|
static rb_encoding *utf32be_encoding, *utf32le_encoding;
|
|
|
|
if (!utf16be_encoding) {
|
|
|
|
utf16be_encoding = rb_enc_find("UTF-16BE");
|
|
|
|
utf16le_encoding = rb_enc_find("UTF-16LE");
|
|
|
|
utf32be_encoding = rb_enc_find("UTF-32BE");
|
|
|
|
utf32le_encoding = rb_enc_find("UTF-32LE");
|
|
|
|
}
|
2008-07-31 16:35:35 -04:00
|
|
|
if (rb_utf8_encoding() == enc) {
|
2008-08-11 18:21:15 -04:00
|
|
|
*len_ret = 3;
|
2008-08-15 07:02:07 -04:00
|
|
|
*repl_enc_ptr = "UTF-8";
|
2008-08-11 18:21:15 -04:00
|
|
|
return "\xEF\xBF\xBD";
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-07-31 04:12:08 -04:00
|
|
|
else if (utf16be_encoding == enc) {
|
2008-08-11 18:21:15 -04:00
|
|
|
*len_ret = 2;
|
2008-08-15 07:02:07 -04:00
|
|
|
*repl_enc_ptr = "UTF-16BE";
|
2008-08-11 18:21:15 -04:00
|
|
|
return "\xFF\xFD";
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-07-31 04:12:08 -04:00
|
|
|
else if (utf16le_encoding == enc) {
|
2008-08-11 18:21:15 -04:00
|
|
|
*len_ret = 2;
|
2008-08-15 07:02:07 -04:00
|
|
|
*repl_enc_ptr = "UTF-16LE";
|
2008-08-11 18:21:15 -04:00
|
|
|
return "\xFD\xFF";
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-07-31 04:12:08 -04:00
|
|
|
else if (utf32be_encoding == enc) {
|
2008-08-11 18:21:15 -04:00
|
|
|
*len_ret = 4;
|
2008-08-15 07:02:07 -04:00
|
|
|
*repl_enc_ptr = "UTF-32BE";
|
2008-08-11 18:21:15 -04:00
|
|
|
return "\x00\x00\xFF\xFD";
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-07-31 04:12:08 -04:00
|
|
|
else if (utf32le_encoding == enc) {
|
2008-08-11 18:21:15 -04:00
|
|
|
*len_ret = 4;
|
2008-08-15 07:02:07 -04:00
|
|
|
*repl_enc_ptr = "UTF-32LE";
|
2008-08-11 18:21:15 -04:00
|
|
|
return "\xFD\xFF\x00\x00";
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
|
|
|
else {
|
2008-08-11 18:21:15 -04:00
|
|
|
*len_ret = 1;
|
2008-08-15 07:02:07 -04:00
|
|
|
*repl_enc_ptr = "US-ASCII";
|
2008-08-11 18:21:15 -04:00
|
|
|
return "?";
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-08-11 18:21:15 -04:00
|
|
|
}
|
|
|
|
|
2007-12-10 00:01:47 -05:00
|
|
|
/*
|
|
|
|
* Transcoding engine logic
|
|
|
|
*/
|
2008-08-09 02:02:01 -04:00
|
|
|
|
|
|
|
static const unsigned char *
|
2008-08-09 22:33:05 -04:00
|
|
|
transcode_char_start(rb_transcoding *tc,
|
2008-08-09 20:23:36 -04:00
|
|
|
const unsigned char *in_start,
|
|
|
|
const unsigned char *inchar_start,
|
2008-08-09 19:33:37 -04:00
|
|
|
const unsigned char *in_p,
|
2008-08-09 20:23:36 -04:00
|
|
|
size_t *char_len_ptr)
|
2008-08-09 02:02:01 -04:00
|
|
|
{
|
|
|
|
const unsigned char *ptr;
|
2008-08-10 21:06:21 -04:00
|
|
|
if (inchar_start - in_start < tc->recognized_len) {
|
|
|
|
MEMCPY(TRANSCODING_READBUF(tc) + tc->recognized_len,
|
2008-08-09 20:23:36 -04:00
|
|
|
inchar_start, unsigned char, in_p - inchar_start);
|
2008-08-09 22:33:05 -04:00
|
|
|
ptr = TRANSCODING_READBUF(tc);
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
|
|
|
else {
|
2008-08-10 21:06:21 -04:00
|
|
|
ptr = inchar_start - tc->recognized_len;
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
2008-08-10 21:06:21 -04:00
|
|
|
*char_len_ptr = tc->recognized_len + (in_p - inchar_start);
|
2008-08-09 02:02:01 -04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2008-08-14 07:31:27 -04:00
|
|
|
static rb_econv_result_t
|
2008-08-09 22:17:56 -04:00
|
|
|
transcode_restartable0(const unsigned char **in_pos, unsigned char **out_pos,
|
2008-08-09 19:33:37 -04:00
|
|
|
const unsigned char *in_stop, unsigned char *out_stop,
|
2008-08-09 22:33:05 -04:00
|
|
|
rb_transcoding *tc,
|
2008-08-09 02:02:01 -04:00
|
|
|
const int opt)
|
|
|
|
|
2007-12-10 00:01:47 -05:00
|
|
|
{
|
2008-08-09 22:33:05 -04:00
|
|
|
const rb_transcoder *tr = tc->transcoder;
|
|
|
|
int unitlen = tr->input_unit_length;
|
2008-08-10 21:06:21 -04:00
|
|
|
int readagain_len = 0;
|
2008-08-09 02:02:01 -04:00
|
|
|
|
2008-08-09 20:23:36 -04:00
|
|
|
const unsigned char *inchar_start;
|
2008-08-09 19:33:37 -04:00
|
|
|
const unsigned char *in_p;
|
2008-08-09 20:23:36 -04:00
|
|
|
|
2008-08-09 02:02:01 -04:00
|
|
|
unsigned char *out_p;
|
2008-08-09 06:32:02 -04:00
|
|
|
|
2008-08-09 19:33:37 -04:00
|
|
|
unsigned char empty_buf;
|
|
|
|
unsigned char *empty_ptr = &empty_buf;
|
|
|
|
|
|
|
|
if (!in_pos) {
|
|
|
|
in_pos = (const unsigned char **)&empty_ptr;
|
|
|
|
in_stop = empty_ptr;
|
|
|
|
}
|
2008-08-09 02:02:01 -04:00
|
|
|
|
|
|
|
if (!out_pos) {
|
|
|
|
out_pos = &empty_ptr;
|
|
|
|
out_stop = empty_ptr;
|
|
|
|
}
|
2008-08-09 06:32:02 -04:00
|
|
|
|
2008-08-09 20:23:36 -04:00
|
|
|
in_p = inchar_start = *in_pos;
|
|
|
|
|
2008-08-09 19:33:37 -04:00
|
|
|
out_p = *out_pos;
|
2008-08-09 02:02:01 -04:00
|
|
|
|
|
|
|
#define SUSPEND(ret, num) \
|
|
|
|
do { \
|
2008-08-09 22:33:05 -04:00
|
|
|
tc->resume_position = (num); \
|
2008-08-09 20:23:36 -04:00
|
|
|
if (0 < in_p - inchar_start) \
|
2008-08-10 21:06:21 -04:00
|
|
|
MEMMOVE(TRANSCODING_READBUF(tc)+tc->recognized_len, \
|
2008-08-09 20:23:36 -04:00
|
|
|
inchar_start, unsigned char, in_p - inchar_start); \
|
2008-08-09 19:33:37 -04:00
|
|
|
*in_pos = in_p; \
|
2008-08-09 02:02:01 -04:00
|
|
|
*out_pos = out_p; \
|
2008-08-10 21: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-09 22:17:56 -04:00
|
|
|
} \
|
2008-08-09 02:02:01 -04:00
|
|
|
return ret; \
|
|
|
|
resume_label ## num:; \
|
|
|
|
} while (0)
|
2008-08-13 04:28:39 -04:00
|
|
|
#define SUSPEND_OBUF(num) \
|
|
|
|
do { \
|
2008-08-14 09:45:54 -04:00
|
|
|
while (out_stop - out_p < 1) { SUSPEND(econv_destination_buffer_full, num); } \
|
2008-08-13 04:28:39 -04:00
|
|
|
} while (0)
|
|
|
|
|
2008-08-14 02:12:27 -04:00
|
|
|
#define SUSPEND_OUTPUT_FOLLOWED_BY_INPUT(num) \
|
2008-08-14 10:48:41 -04:00
|
|
|
if ((opt & ECONV_OUTPUT_FOLLOWED_BY_INPUT) && *out_pos != out_p) { \
|
2008-08-14 09:45:54 -04:00
|
|
|
SUSPEND(econv_output_followed_by_input, num); \
|
2008-08-14 02:12:27 -04:00
|
|
|
}
|
|
|
|
|
2008-08-13 04: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 04:28:39 -04:00
|
|
|
#define writebuf_len (tc->writebuf_len)
|
|
|
|
#define writebuf_off (tc->writebuf_off)
|
2008-08-09 02:02:01 -04:00
|
|
|
|
2008-08-09 22:33:05 -04:00
|
|
|
switch (tc->resume_position) {
|
2008-08-09 02: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 04: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 02:12:27 -04:00
|
|
|
case 24: goto resume_label24;
|
|
|
|
case 25: goto resume_label25;
|
|
|
|
case 26: goto resume_label26;
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2008-08-16 01:32:42 -04:00
|
|
|
inchar_start = in_p;
|
|
|
|
tc->recognized_len = 0;
|
|
|
|
next_table = tr->conv_tree_start;
|
|
|
|
|
2008-08-14 02:12:27 -04:00
|
|
|
SUSPEND_OUTPUT_FOLLOWED_BY_INPUT(24);
|
2008-08-16 01:32:42 -04:00
|
|
|
|
2008-08-09 19:33:37 -04:00
|
|
|
if (in_stop <= in_p) {
|
2008-08-14 10:48:41 -04:00
|
|
|
if (!(opt & ECONV_PARTIAL_INPUT))
|
2008-08-09 02:02:01 -04:00
|
|
|
break;
|
2008-08-14 09:45:54 -04:00
|
|
|
SUSPEND(econv_source_buffer_empty, 7);
|
2008-08-09 02:02:01 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-08-09 19:33:37 -04:00
|
|
|
next_byte = (unsigned char)*in_p++;
|
2007-12-10 00:01:47 -05:00
|
|
|
follow_byte:
|
2008-08-08 11:48:17 -04:00
|
|
|
if (next_byte < next_table->base[0] || next_table->base[1] < next_byte)
|
|
|
|
next_info = INVALID;
|
|
|
|
else {
|
|
|
|
unsigned int next_offset = next_table->base[2+next_byte-next_table->base[0]];
|
|
|
|
next_info = (VALUE)next_table->info[next_offset];
|
|
|
|
}
|
2007-12-28 04:26:55 -05:00
|
|
|
follow_info:
|
2007-12-10 23:57:51 -05:00
|
|
|
switch (next_info & 0x1F) {
|
2008-08-09 02:02:01 -04:00
|
|
|
case NOMAP: /* xxx: copy last byte only? */
|
2008-08-13 04:28:39 -04:00
|
|
|
SUSPEND_OBUF(3); *out_p++ = next_byte;
|
2007-12-10 03:46:06 -05:00
|
|
|
continue;
|
|
|
|
case 0x00: case 0x04: case 0x08: case 0x0C:
|
|
|
|
case 0x10: case 0x14: case 0x18: case 0x1C:
|
2008-08-14 02:12:27 -04:00
|
|
|
SUSPEND_OUTPUT_FOLLOWED_BY_INPUT(25);
|
2008-08-09 19:33:37 -04:00
|
|
|
while (in_p >= in_stop) {
|
2008-08-14 10:48:41 -04:00
|
|
|
if (!(opt & ECONV_PARTIAL_INPUT))
|
2008-08-09 02:02:01 -04:00
|
|
|
goto invalid;
|
2008-08-14 09:45:54 -04:00
|
|
|
SUSPEND(econv_source_buffer_empty, 5);
|
2007-12-10 03:46:06 -05:00
|
|
|
}
|
2008-08-09 19:33:37 -04:00
|
|
|
next_byte = (unsigned char)*in_p++;
|
2007-12-28 04:26:55 -05:00
|
|
|
next_table = (const BYTE_LOOKUP *)next_info;
|
2007-12-10 03:46:06 -05:00
|
|
|
goto follow_byte;
|
|
|
|
case ZERObt: /* drop input */
|
|
|
|
continue;
|
|
|
|
case ONEbt:
|
2008-08-13 04:28:39 -04:00
|
|
|
SUSPEND_OBUF(9); *out_p++ = getBT1(next_info);
|
2007-12-10 03:46:06 -05:00
|
|
|
continue;
|
|
|
|
case TWObt:
|
2008-08-13 04:28:39 -04:00
|
|
|
SUSPEND_OBUF(10); *out_p++ = getBT1(next_info);
|
|
|
|
SUSPEND_OBUF(21); *out_p++ = getBT2(next_info);
|
2007-12-10 03:46:06 -05:00
|
|
|
continue;
|
2008-08-09 02:02:01 -04:00
|
|
|
case THREEbt:
|
2008-08-13 04: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 02:02:01 -04:00
|
|
|
continue;
|
2007-12-10 03:46:06 -05:00
|
|
|
case FOURbt:
|
2008-08-13 04: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 03:46:06 -05:00
|
|
|
continue;
|
2007-12-28 04:26:55 -05:00
|
|
|
case FUNii:
|
2008-08-09 22:33:05 -04:00
|
|
|
next_info = (VALUE)(*tr->func_ii)(tc, next_info);
|
2007-12-28 04:26:55 -05:00
|
|
|
goto follow_info;
|
2008-01-20 01:12:48 -05:00
|
|
|
case FUNsi:
|
2008-08-09 02:02:01 -04:00
|
|
|
{
|
2008-08-09 19:33:37 -04:00
|
|
|
const unsigned char *char_start;
|
2008-08-09 20:23:36 -04:00
|
|
|
size_t char_len;
|
2008-08-09 22:33:05 -04:00
|
|
|
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
|
|
|
|
next_info = (VALUE)(*tr->func_si)(tc, char_start, (size_t)char_len);
|
2008-08-11 11:50:42 -04:00
|
|
|
goto follow_info;
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
2008-01-20 01:12:48 -05:00
|
|
|
case FUNio:
|
2008-08-13 04:28:39 -04:00
|
|
|
SUSPEND_OBUF(13);
|
|
|
|
if (tr->max_output <= out_stop - out_p)
|
|
|
|
out_p += (VALUE)(*tr->func_io)(tc, next_info, out_p);
|
|
|
|
else {
|
|
|
|
writebuf_len = (VALUE)(*tr->func_io)(tc, next_info, TRANSCODING_WRITEBUF(tc));
|
|
|
|
writebuf_off = 0;
|
|
|
|
while (writebuf_off < writebuf_len) {
|
|
|
|
SUSPEND_OBUF(20);
|
|
|
|
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
|
|
|
|
}
|
|
|
|
}
|
2008-01-20 01:12:48 -05:00
|
|
|
break;
|
|
|
|
case FUNso:
|
2008-08-09 02:02:01 -04:00
|
|
|
{
|
2008-08-09 19:33:37 -04:00
|
|
|
const unsigned char *char_start;
|
2008-08-09 20:23:36 -04:00
|
|
|
size_t char_len;
|
2008-08-13 04: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);
|
|
|
|
out_p += (VALUE)(*tr->func_so)(tc, char_start, (size_t)char_len, out_p);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
|
|
|
|
writebuf_len = (VALUE)(*tr->func_so)(tc, char_start, (size_t)char_len, TRANSCODING_WRITEBUF(tc));
|
|
|
|
writebuf_off = 0;
|
|
|
|
while (writebuf_off < writebuf_len) {
|
|
|
|
SUSPEND_OBUF(22);
|
|
|
|
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
|
|
|
|
}
|
|
|
|
}
|
2008-08-09 02:02:01 -04:00
|
|
|
break;
|
|
|
|
}
|
2007-12-27 03:27:19 -05:00
|
|
|
case INVALID:
|
2008-08-10 21:06:21 -04:00
|
|
|
if (tc->recognized_len + (in_p - inchar_start) <= unitlen) {
|
2008-08-14 02:12:27 -04:00
|
|
|
if (tc->recognized_len + (in_p - inchar_start) < unitlen)
|
|
|
|
SUSPEND_OUTPUT_FOLLOWED_BY_INPUT(26);
|
2008-08-14 10:48:41 -04:00
|
|
|
while ((opt & ECONV_PARTIAL_INPUT) && tc->recognized_len + (in_stop - inchar_start) < unitlen) {
|
2008-08-10 09:37:44 -04:00
|
|
|
in_p = in_stop;
|
2008-08-14 09:45:54 -04:00
|
|
|
SUSPEND(econv_source_buffer_empty, 8);
|
2008-08-10 09:37:44 -04:00
|
|
|
}
|
2008-08-10 21:06:21 -04:00
|
|
|
if (tc->recognized_len + (in_stop - inchar_start) <= unitlen) {
|
2008-08-10 09:37:44 -04:00
|
|
|
in_p = in_stop;
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
2008-08-09 19:33:37 -04:00
|
|
|
else {
|
2008-08-10 21:06:21 -04:00
|
|
|
in_p = inchar_start + (unitlen - tc->recognized_len);
|
2008-08-09 19:33:37 -04:00
|
|
|
}
|
2008-08-08 11:48:17 -04:00
|
|
|
}
|
2008-08-10 09:37:44 -04:00
|
|
|
else {
|
|
|
|
int invalid_len; /* including the last byte which causes invalid */
|
|
|
|
int discard_len;
|
2008-08-10 21:06:21 -04:00
|
|
|
invalid_len = tc->recognized_len + (in_p - inchar_start);
|
2008-08-10 09:37:44 -04:00
|
|
|
discard_len = ((invalid_len - 1) / unitlen) * unitlen;
|
2008-08-10 21:06:21 -04:00
|
|
|
readagain_len = invalid_len - discard_len;
|
2008-08-10 09:37:44 -04:00
|
|
|
}
|
|
|
|
goto invalid;
|
2007-12-10 03:46:06 -05:00
|
|
|
case UNDEF:
|
2008-07-14 02:27:26 -04:00
|
|
|
goto undef;
|
2007-12-10 23:57:51 -05:00
|
|
|
}
|
|
|
|
continue;
|
2008-08-09 02:02:01 -04:00
|
|
|
|
2007-12-27 03:27:19 -05:00
|
|
|
invalid:
|
2008-08-14 09:45:54 -04:00
|
|
|
SUSPEND(econv_invalid_byte_sequence, 1);
|
2008-08-09 02:02:01 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
undef:
|
2008-08-14 09:45:54 -04:00
|
|
|
SUSPEND(econv_undefined_conversion, 2);
|
2008-08-09 02:02:01 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2008-08-09 22:33:05 -04:00
|
|
|
if (tr->finish_func) {
|
2008-08-13 04:28:39 -04:00
|
|
|
SUSPEND_OBUF(4);
|
|
|
|
if (tr->max_output <= out_stop - out_p) {
|
|
|
|
out_p += tr->finish_func(tc, out_p);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
writebuf_len = tr->finish_func(tc, TRANSCODING_WRITEBUF(tc));
|
|
|
|
writebuf_off = 0;
|
|
|
|
while (writebuf_off < writebuf_len) {
|
|
|
|
SUSPEND_OBUF(23);
|
|
|
|
*out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
|
|
|
|
}
|
|
|
|
}
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
|
|
|
while (1)
|
2008-08-14 09:45:54 -04:00
|
|
|
SUSPEND(econv_finished, 6);
|
2008-08-09 02:02:01 -04:00
|
|
|
#undef SUSPEND
|
2008-08-13 04:36:47 -04:00
|
|
|
#undef next_table
|
|
|
|
#undef next_info
|
|
|
|
#undef next_byte
|
2008-08-13 04:28:39 -04:00
|
|
|
#undef writebuf_len
|
|
|
|
#undef writebuf_off
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
|
|
|
|
2008-08-14 07:31:27 -04:00
|
|
|
static rb_econv_result_t
|
2008-08-09 22: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-09 22:33:05 -04:00
|
|
|
rb_transcoding *tc,
|
2008-08-09 22:17:56 -04:00
|
|
|
const int opt)
|
|
|
|
{
|
2008-08-10 21: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 07:31:27 -04:00
|
|
|
rb_econv_result_t res;
|
2008-08-09 22:17:56 -04:00
|
|
|
|
2008-08-10 21: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 10:48:41 -04:00
|
|
|
res = transcode_restartable0(&readagain_pos, out_pos, readagain_stop, out_stop, tc, opt|ECONV_PARTIAL_INPUT);
|
2008-08-14 09:45:54 -04:00
|
|
|
if (res != econv_source_buffer_empty) {
|
2008-08-10 21: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-09 22:17:56 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2008-08-09 22:33:05 -04:00
|
|
|
return transcode_restartable0(in_pos, out_pos, in_stop, out_stop, tc, opt);
|
2008-08-09 22:17:56 -04:00
|
|
|
}
|
|
|
|
|
2008-08-10 08:26:01 -04:00
|
|
|
static rb_transcoding *
|
2008-08-12 22:40:24 -04:00
|
|
|
rb_transcoding_open_by_transcoder(const rb_transcoder *tr, int flags)
|
2008-08-10 08:26:01 -04:00
|
|
|
{
|
|
|
|
rb_transcoding *tc;
|
|
|
|
|
|
|
|
tc = ALLOC(rb_transcoding);
|
|
|
|
tc->transcoder = tr;
|
|
|
|
tc->flags = flags;
|
|
|
|
memset(tc->stateful, 0, sizeof(tc->stateful));
|
|
|
|
tc->resume_position = 0;
|
2008-08-10 21:06:21 -04:00
|
|
|
tc->recognized_len = 0;
|
|
|
|
tc->readagain_len = 0;
|
2008-08-13 04:28:39 -04:00
|
|
|
tc->writebuf_len = 0;
|
|
|
|
tc->writebuf_off = 0;
|
2008-08-10 08:26:01 -04:00
|
|
|
if (sizeof(tc->readbuf.ary) < tr->max_input) {
|
|
|
|
tc->readbuf.ptr = xmalloc(tr->max_input);
|
|
|
|
}
|
2008-08-13 04:28:39 -04:00
|
|
|
if (sizeof(tc->writebuf.ary) < tr->max_output) {
|
|
|
|
tc->writebuf.ptr = xmalloc(tr->max_output);
|
|
|
|
}
|
2008-08-10 08:26:01 -04:00
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
2008-08-14 07:31:27 -04:00
|
|
|
static rb_econv_result_t
|
2008-08-10 08: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;
|
|
|
|
if (sizeof(tc->readbuf.ary) < tr->max_input)
|
|
|
|
xfree(tc->readbuf.ptr);
|
2008-08-13 04:28:39 -04:00
|
|
|
if (sizeof(tc->writebuf.ary) < tr->max_output)
|
|
|
|
xfree(tc->writebuf.ptr);
|
2008-08-10 08:26:01 -04:00
|
|
|
xfree(tc);
|
|
|
|
}
|
|
|
|
|
2008-08-14 07:31:27 -04:00
|
|
|
static rb_econv_t *
|
2008-08-14 07:59:47 -04:00
|
|
|
rb_econv_open_by_transcoder_entries(int n, transcoder_entry_t **entries)
|
2008-08-11 11:50:42 -04:00
|
|
|
{
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_t *ec;
|
2008-08-11 11:50:42 -04:00
|
|
|
int i;
|
|
|
|
|
2008-08-12 22:40:24 -04:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
const rb_transcoder *tr;
|
|
|
|
tr = load_transcoder_entry(entries[i]);
|
|
|
|
if (!tr)
|
|
|
|
return NULL;
|
2008-08-11 11:50:42 -04:00
|
|
|
}
|
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
ec = ALLOC(rb_econv_t);
|
2008-08-16 02:25:38 -04:00
|
|
|
ec->source_encoding_name = NULL;
|
|
|
|
ec->destination_encoding_name = NULL;
|
2008-08-16 01: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-08-14 10:35:55 -04:00
|
|
|
ec->num_trans = n;
|
|
|
|
ec->elems = ALLOC_N(rb_econv_elem_t, ec->num_trans);
|
|
|
|
ec->num_finished = 0;
|
|
|
|
ec->last_tc = NULL;
|
2008-08-16 01:32:42 -04:00
|
|
|
ec->last_trans_index = -1;
|
2008-08-14 10:35:55 -04:00
|
|
|
ec->source_encoding = NULL;
|
|
|
|
ec->destination_encoding = NULL;
|
|
|
|
for (i = 0; i < ec->num_trans; i++) {
|
2008-08-12 22:40:24 -04:00
|
|
|
const rb_transcoder *tr = load_transcoder_entry(entries[i]);
|
2008-08-14 10:35:55 -04:00
|
|
|
ec->elems[i].tc = rb_transcoding_open_by_transcoder(tr, 0);
|
|
|
|
ec->elems[i].out_buf_start = NULL;
|
|
|
|
ec->elems[i].out_data_start = NULL;
|
|
|
|
ec->elems[i].out_data_end = NULL;
|
|
|
|
ec->elems[i].out_buf_end = NULL;
|
|
|
|
ec->elems[i].last_result = econv_source_buffer_empty;
|
|
|
|
}
|
|
|
|
ec->last_tc = ec->elems[ec->num_trans-1].tc;
|
2008-08-16 01:32:42 -04:00
|
|
|
ec->last_trans_index = ec->num_trans-1;
|
2008-08-14 10:35:55 -04:00
|
|
|
|
|
|
|
for (i = 0; i < ec->num_trans-1; i++) {
|
2008-08-12 05:42:49 -04:00
|
|
|
int bufsize = 4096;
|
|
|
|
unsigned char *p;
|
|
|
|
p = xmalloc(bufsize);
|
2008-08-14 10:35:55 -04:00
|
|
|
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;
|
2008-08-12 05:42:49 -04:00
|
|
|
}
|
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
return ec;
|
2008-08-11 11:50:42 -04:00
|
|
|
}
|
|
|
|
|
2008-08-12 22:40:24 -04:00
|
|
|
static void
|
|
|
|
trans_open_i(const char *from, const char *to, int depth, void *arg)
|
|
|
|
{
|
|
|
|
transcoder_entry_t ***entries_ptr = arg;
|
|
|
|
transcoder_entry_t **entries;
|
|
|
|
|
|
|
|
if (!*entries_ptr) {
|
2008-08-13 01:48:57 -04:00
|
|
|
entries = ALLOC_N(transcoder_entry_t *, depth+1+2);
|
2008-08-12 22:40:24 -04:00
|
|
|
*entries_ptr = entries;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
entries = *entries_ptr;
|
|
|
|
}
|
|
|
|
entries[depth] = get_transcoder_entry(from, to);
|
|
|
|
}
|
|
|
|
|
2008-08-14 10:28:10 -04:00
|
|
|
rb_econv_t *
|
2008-08-14 07:31:27 -04:00
|
|
|
rb_econv_open(const char *from, const char *to, int flags)
|
2008-08-12 22:40:24 -04:00
|
|
|
{
|
|
|
|
transcoder_entry_t **entries = NULL;
|
|
|
|
int num_trans;
|
2008-08-14 10:35:55 -04:00
|
|
|
static rb_econv_t *ec;
|
2008-08-12 22:40:24 -04:00
|
|
|
|
|
|
|
num_trans = transcode_search_path(from, to, trans_open_i, (void *)&entries);
|
|
|
|
|
2008-08-19 13:32:31 -04:00
|
|
|
if (num_trans < 0 || !entries) {
|
|
|
|
xfree(entries);
|
2008-08-12 22:40:24 -04:00
|
|
|
return NULL;
|
2008-08-19 13:32:31 -04:00
|
|
|
}
|
2008-08-12 22:40:24 -04:00
|
|
|
|
2008-08-14 10:48:41 -04:00
|
|
|
if (flags & (ECONV_CRLF_NEWLINE_ENCODER|ECONV_CR_NEWLINE_ENCODER)) {
|
|
|
|
const char *name = (flags & ECONV_CRLF_NEWLINE_ENCODER) ? "crlf_newline" : "cr_newline";
|
2008-08-13 01:48:57 -04:00
|
|
|
transcoder_entry_t *e = get_transcoder_entry("", name);
|
2008-08-19 13:32:31 -04:00
|
|
|
if (!e) {
|
|
|
|
xfree(entries);
|
2008-08-13 01:48:57 -04:00
|
|
|
return NULL;
|
2008-08-19 13:32:31 -04:00
|
|
|
}
|
2008-08-13 01:48:57 -04:00
|
|
|
MEMMOVE(entries+1, entries, transcoder_entry_t *, num_trans);
|
|
|
|
entries[0] = e;
|
|
|
|
num_trans++;
|
|
|
|
}
|
|
|
|
|
2008-08-14 10:48:41 -04:00
|
|
|
if (flags & ECONV_UNIVERSAL_NEWLINE_DECODER) {
|
2008-08-13 01:30:42 -04:00
|
|
|
transcoder_entry_t *e = get_transcoder_entry("universal_newline", "");
|
2008-08-19 13:32:31 -04:00
|
|
|
if (!e) {
|
|
|
|
xfree(entries);
|
2008-08-13 01:30:42 -04:00
|
|
|
return NULL;
|
2008-08-19 13:32:31 -04:00
|
|
|
}
|
2008-08-13 01:30:42 -04:00
|
|
|
entries[num_trans++] = e;
|
|
|
|
}
|
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
ec = rb_econv_open_by_transcoder_entries(num_trans, entries);
|
2008-08-19 13:32:31 -04:00
|
|
|
xfree(entries);
|
2008-08-16 02:25:38 -04:00
|
|
|
if (!ec)
|
|
|
|
rb_raise(rb_eArgError, "encoding conversion not supported (from %s to %s)", from, to);
|
|
|
|
|
|
|
|
ec->source_encoding_name = from;
|
|
|
|
ec->destination_encoding_name = to;
|
2008-08-13 01:30:42 -04:00
|
|
|
|
2008-08-14 10:48:41 -04:00
|
|
|
if (flags & ECONV_UNIVERSAL_NEWLINE_DECODER) {
|
2008-08-14 10:35:55 -04:00
|
|
|
ec->last_tc = ec->elems[ec->num_trans-2].tc;
|
2008-08-16 01:32:42 -04:00
|
|
|
ec->last_trans_index = ec->num_trans-2;
|
2008-08-13 01:30:42 -04:00
|
|
|
}
|
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
return ec;
|
2008-08-12 22:40:24 -04:00
|
|
|
}
|
|
|
|
|
2008-08-11 11:50:42 -04:00
|
|
|
static int
|
2008-08-14 10:35:55 -04:00
|
|
|
trans_sweep(rb_econv_t *ec,
|
2008-08-11 11: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 07:31:27 -04:00
|
|
|
rb_econv_result_t res;
|
2008-08-11 11:50:42 -04:00
|
|
|
|
|
|
|
try = 1;
|
|
|
|
while (try) {
|
|
|
|
try = 0;
|
2008-08-14 10:35:55 -04:00
|
|
|
for (i = start; i < ec->num_trans; i++) {
|
|
|
|
rb_econv_elem_t *te = &ec->elems[i];
|
2008-08-11 11:50:42 -04:00
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
ipp = input_ptr;
|
|
|
|
is = input_stop;
|
|
|
|
}
|
|
|
|
else {
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_elem_t *prev_te = &ec->elems[i-1];
|
2008-08-11 11:50:42 -04:00
|
|
|
ipp = (const unsigned char **)&prev_te->out_data_start;
|
|
|
|
is = prev_te->out_data_end;
|
|
|
|
}
|
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
if (i == ec->num_trans-1) {
|
2008-08-11 11:50:42 -04:00
|
|
|
opp = output_ptr;
|
|
|
|
os = output_stop;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (te->out_buf_start != te->out_data_start) {
|
|
|
|
int len = te->out_data_end - te->out_data_start;
|
|
|
|
int off = te->out_data_start - te->out_buf_start;
|
|
|
|
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 10:35:55 -04:00
|
|
|
if (ec->num_finished != i)
|
2008-08-14 10:48:41 -04:00
|
|
|
f |= ECONV_PARTIAL_INPUT;
|
|
|
|
if (i == 0 && (flags & ECONV_OUTPUT_FOLLOWED_BY_INPUT)) {
|
2008-08-14 02:12:27 -04:00
|
|
|
start = 1;
|
2008-08-14 10:48:41 -04:00
|
|
|
flags &= ~ECONV_OUTPUT_FOLLOWED_BY_INPUT;
|
2008-08-14 02:12:27 -04:00
|
|
|
}
|
|
|
|
if (i != 0)
|
2008-08-14 10:48:41 -04:00
|
|
|
f &= ~ECONV_OUTPUT_FOLLOWED_BY_INPUT;
|
2008-08-11 11: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 09:45:54 -04:00
|
|
|
case econv_invalid_byte_sequence:
|
|
|
|
case econv_undefined_conversion:
|
|
|
|
case econv_output_followed_by_input:
|
2008-08-11 11:50:42 -04:00
|
|
|
return i;
|
|
|
|
|
2008-08-14 09:45:54 -04:00
|
|
|
case econv_destination_buffer_full:
|
|
|
|
case econv_source_buffer_empty:
|
2008-08-11 11:50:42 -04:00
|
|
|
break;
|
|
|
|
|
2008-08-14 09:45:54 -04:00
|
|
|
case econv_finished:
|
2008-08-14 10:35:55 -04:00
|
|
|
ec->num_finished = i+1;
|
2008-08-11 11:50:42 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-14 07:31:27 -04:00
|
|
|
static rb_econv_result_t
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_trans_conv(rb_econv_t *ec,
|
2008-08-11 11: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 05:12:56 -04:00
|
|
|
int flags,
|
|
|
|
int *result_position_ptr)
|
2008-08-11 11:50:42 -04:00
|
|
|
{
|
|
|
|
int i;
|
2008-08-14 02:12:27 -04:00
|
|
|
int needreport_index;
|
|
|
|
int sweep_start;
|
2008-08-11 11: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-08-14 10:35:55 -04:00
|
|
|
if (ec->elems[0].last_result == econv_output_followed_by_input)
|
|
|
|
ec->elems[0].last_result = econv_source_buffer_empty;
|
2008-08-14 02:12:27 -04:00
|
|
|
|
|
|
|
needreport_index = -1;
|
2008-08-14 10:35:55 -04:00
|
|
|
for (i = ec->num_trans-1; 0 <= i; i--) {
|
|
|
|
switch (ec->elems[i].last_result) {
|
2008-08-14 09:45:54 -04:00
|
|
|
case econv_invalid_byte_sequence:
|
|
|
|
case econv_undefined_conversion:
|
|
|
|
case econv_output_followed_by_input:
|
|
|
|
case econv_finished:
|
2008-08-14 02:12:27 -04:00
|
|
|
sweep_start = i+1;
|
|
|
|
needreport_index = i;
|
|
|
|
goto found_needreport;
|
|
|
|
|
2008-08-14 09:45:54 -04:00
|
|
|
case econv_destination_buffer_full:
|
|
|
|
case econv_source_buffer_empty:
|
2008-08-12 18:43:17 -04:00
|
|
|
break;
|
2008-08-14 02:12:27 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
rb_bug("unexpected transcode last result");
|
2008-08-11 11:50:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
/* /^[sd]+$/ is confirmed. but actually /^s*d*$/. */
|
2008-08-14 02:12:27 -04:00
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
if (ec->elems[ec->num_trans-1].last_result == econv_destination_buffer_full &&
|
2008-08-14 10:48:41 -04:00
|
|
|
(flags & ECONV_OUTPUT_FOLLOWED_BY_INPUT)) {
|
2008-08-14 07:31:27 -04:00
|
|
|
rb_econv_result_t res;
|
2008-08-14 02:12:27 -04:00
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
res = rb_trans_conv(ec, NULL, NULL, output_ptr, output_stop,
|
2008-08-15 05:12:56 -04:00
|
|
|
(flags & ~ECONV_OUTPUT_FOLLOWED_BY_INPUT)|ECONV_PARTIAL_INPUT,
|
|
|
|
result_position_ptr);
|
2008-08-14 02:12:27 -04:00
|
|
|
|
2008-08-14 09:45:54 -04:00
|
|
|
if (res == econv_source_buffer_empty)
|
|
|
|
return econv_output_followed_by_input;
|
2008-08-14 02:12:27 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
sweep_start = 0;
|
|
|
|
|
|
|
|
found_needreport:
|
|
|
|
|
2008-08-11 11:50:42 -04:00
|
|
|
do {
|
2008-08-14 10:35:55 -04:00
|
|
|
needreport_index = trans_sweep(ec, input_ptr, input_stop, output_ptr, output_stop, flags, sweep_start);
|
2008-08-14 02:12:27 -04:00
|
|
|
sweep_start = needreport_index + 1;
|
2008-08-14 10:35:55 -04:00
|
|
|
} while (needreport_index != -1 && needreport_index != ec->num_trans-1);
|
2008-08-11 11:50:42 -04:00
|
|
|
|
2008-08-14 10: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 09:45:54 -04:00
|
|
|
if (res == econv_invalid_byte_sequence ||
|
|
|
|
res == econv_undefined_conversion ||
|
|
|
|
res == econv_output_followed_by_input) {
|
2008-08-14 10:35:55 -04:00
|
|
|
ec->elems[i].last_result = econv_source_buffer_empty;
|
2008-08-14 02:12:27 -04:00
|
|
|
}
|
2008-08-15 05:12:56 -04:00
|
|
|
if (result_position_ptr)
|
|
|
|
*result_position_ptr = i;
|
2008-08-12 18:43:17 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2008-08-15 05:12:56 -04:00
|
|
|
if (result_position_ptr)
|
|
|
|
*result_position_ptr = -1;
|
2008-08-14 09:45:54 -04:00
|
|
|
return econv_source_buffer_empty;
|
2008-08-11 11:50:42 -04:00
|
|
|
}
|
|
|
|
|
2008-08-14 10:28:10 -04:00
|
|
|
rb_econv_result_t
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_convert(rb_econv_t *ec,
|
2008-08-14 02: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 07:31:27 -04:00
|
|
|
rb_econv_result_t res;
|
2008-08-15 05:12:56 -04:00
|
|
|
int result_position;
|
2008-08-16 01:32:42 -04:00
|
|
|
int has_output = 0;
|
2008-08-15 05:12:56 -04:00
|
|
|
|
|
|
|
memset(&ec->last_error, 0, sizeof(ec->last_error));
|
2008-08-14 02:35:33 -04:00
|
|
|
|
2008-08-16 01: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 =
|
|
|
|
ec->elems[ec->num_trans-1].out_data_end =
|
|
|
|
ec->elems[ec->num_trans-1].out_buf_start;
|
|
|
|
has_output = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ec->in_buf_start &&
|
|
|
|
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,
|
|
|
|
(flags&~ECONV_OUTPUT_FOLLOWED_BY_INPUT)|ECONV_PARTIAL_INPUT, &result_position);
|
|
|
|
if (res != econv_source_buffer_empty)
|
|
|
|
goto gotresult;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_output &&
|
|
|
|
(flags & ECONV_OUTPUT_FOLLOWED_BY_INPUT) &&
|
|
|
|
*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)
|
|
|
|
res = econv_output_followed_by_input;
|
|
|
|
}
|
|
|
|
else if ((flags & ECONV_OUTPUT_FOLLOWED_BY_INPUT) ||
|
2008-08-15 05: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 {
|
|
|
|
flags |= ECONV_OUTPUT_FOLLOWED_BY_INPUT;
|
|
|
|
do {
|
|
|
|
res = rb_trans_conv(ec, input_ptr, input_stop, output_ptr, output_stop, flags, &result_position);
|
|
|
|
} while (res == econv_output_followed_by_input);
|
|
|
|
}
|
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
gotresult:
|
2008-08-15 05:12:56 -04:00
|
|
|
ec->last_error.result = res;
|
|
|
|
ec->last_error.partial_input = flags & ECONV_PARTIAL_INPUT;
|
|
|
|
if (res == econv_invalid_byte_sequence ||
|
|
|
|
res == econv_undefined_conversion) {
|
2008-08-15 05:33:59 -04:00
|
|
|
rb_transcoding *error_tc = ec->elems[result_position].tc;
|
|
|
|
ec->last_error.error_tc = error_tc;
|
|
|
|
ec->last_error.source_encoding = error_tc->transcoder->from_encoding;
|
|
|
|
ec->last_error.destination_encoding = error_tc->transcoder->to_encoding;
|
|
|
|
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 05:12:56 -04:00
|
|
|
}
|
2008-08-14 02:35:33 -04:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
const char *
|
|
|
|
rb_econv_encoding_to_insert_output(rb_econv_t *ec)
|
2008-08-14 11:56:39 -04:00
|
|
|
{
|
|
|
|
rb_transcoding *tc = ec->last_tc;
|
|
|
|
const rb_transcoder *tr = tc->transcoder;
|
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
if (tr->stateful_type == stateful_encoder)
|
|
|
|
return tr->from_encoding;
|
|
|
|
return tr->to_encoding;
|
2008-08-14 11:56:39 -04:00
|
|
|
}
|
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
static unsigned char *
|
|
|
|
allocate_converted_string(const char *str_encoding, const char *insert_encoding,
|
|
|
|
const unsigned char *str, size_t len,
|
|
|
|
size_t *dst_len_ptr)
|
2008-08-15 07:02:07 -04:00
|
|
|
{
|
2008-08-16 01:32:42 -04:00
|
|
|
unsigned char *dst_str;
|
2008-08-15 10:17:11 -04:00
|
|
|
size_t dst_len;
|
2008-08-16 01:32:42 -04:00
|
|
|
size_t dst_bufsize = len;
|
2008-08-15 07:02:07 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
rb_econv_t *ec;
|
|
|
|
rb_econv_result_t res;
|
2008-08-15 07:02:07 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
const unsigned char *sp;
|
|
|
|
unsigned char *dp;
|
2008-08-15 07:02:07 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
if (dst_bufsize == 0)
|
|
|
|
dst_bufsize += 1;
|
2008-08-15 10:17:11 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
ec = rb_econv_open(str_encoding, insert_encoding, 0);
|
|
|
|
if (ec == NULL)
|
|
|
|
return NULL;
|
|
|
|
dst_str = xmalloc(dst_bufsize);
|
|
|
|
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) {
|
|
|
|
if (dst_bufsize * 2 < dst_bufsize) {
|
|
|
|
xfree(dst_str);
|
|
|
|
rb_econv_close(ec);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dst_bufsize *= 2;
|
|
|
|
dst_str = xrealloc(dst_str, dst_bufsize);
|
|
|
|
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) {
|
|
|
|
xfree(dst_str);
|
2008-08-15 10:17:11 -04:00
|
|
|
rb_econv_close(ec);
|
2008-08-16 01:32:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
rb_econv_close(ec);
|
|
|
|
*dst_len_ptr = dst_len;
|
|
|
|
return dst_str;
|
2008-08-15 10:17:11 -04:00
|
|
|
}
|
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
/* result: 0:success -1:failure */
|
2008-08-15 10:17:11 -04:00
|
|
|
int
|
2008-08-16 01:32:42 -04:00
|
|
|
rb_econv_insert_output(rb_econv_t *ec,
|
|
|
|
const unsigned char *str, size_t len, const char *str_encoding)
|
2008-08-15 10:17:11 -04:00
|
|
|
{
|
2008-08-16 01:32:42 -04:00
|
|
|
const char *insert_encoding = rb_econv_encoding_to_insert_output(ec);
|
|
|
|
const unsigned char *insert_str;
|
|
|
|
size_t insert_len;
|
2008-08-15 10:17:11 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
rb_transcoding *tc;
|
|
|
|
const rb_transcoder *tr;
|
2008-08-15 10:17:11 -04:00
|
|
|
|
2008-08-16 01: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 10:17:11 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
size_t need;
|
2008-08-15 10:17:11 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
2008-08-15 07:02:07 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
if (encoding_equal(insert_encoding, str_encoding)) {
|
|
|
|
insert_str = str;
|
|
|
|
insert_len = len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
insert_str = allocate_converted_string(str_encoding, insert_encoding, str, len, &insert_len);
|
|
|
|
if (insert_str == NULL)
|
|
|
|
return -1;
|
2008-08-15 07:02:07 -04:00
|
|
|
}
|
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
tc = ec->last_tc;
|
|
|
|
tr = tc->transcoder;
|
2008-08-15 07:02:07 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
need = insert_len;
|
|
|
|
if (tr->stateful_type == stateful_encoder) {
|
|
|
|
need += tc->readagain_len;
|
|
|
|
if (need < insert_len)
|
|
|
|
goto fail;
|
|
|
|
if (ec->last_trans_index == 0) {
|
|
|
|
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 {
|
|
|
|
rb_econv_elem_t *ee = &ec->elems[ec->last_trans_index-1];
|
|
|
|
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 {
|
|
|
|
rb_econv_elem_t *ee = &ec->elems[ec->last_trans_index];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else if (*buf_end_p - *data_end_p < need) {
|
|
|
|
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;
|
|
|
|
if (*buf_end_p - *data_end_p < need) {
|
|
|
|
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 07:02:07 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
if (tr->stateful_type == stateful_encoder) {
|
|
|
|
memcpy(*data_end_p, TRANSCODING_READBUF(tc)+tc->recognized_len, tc->readagain_len);
|
|
|
|
*data_end_p += tc->readagain_len;
|
|
|
|
tc->readagain_len = 0;
|
|
|
|
}
|
|
|
|
memcpy(*data_end_p, insert_str, insert_len);
|
|
|
|
*data_end_p += insert_len;
|
2008-08-15 07:02:07 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
if (insert_str != str)
|
|
|
|
xfree((void*)insert_str);
|
|
|
|
return 0;
|
2008-08-15 07:02:07 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
fail:
|
|
|
|
if (insert_str != str)
|
|
|
|
xfree((void*)insert_str);
|
|
|
|
return -1;
|
2008-08-15 07:02:07 -04:00
|
|
|
}
|
|
|
|
|
2008-08-14 10:28:10 -04:00
|
|
|
void
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_close(rb_econv_t *ec)
|
2008-08-11 11:50:42 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-08-14 10: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 11:50:42 -04:00
|
|
|
}
|
2008-08-19 13:32:31 -04:00
|
|
|
xfree(ec->in_buf_start);
|
2008-08-14 10:35:55 -04:00
|
|
|
xfree(ec->elems);
|
|
|
|
xfree(ec);
|
2008-08-11 11:50:42 -04:00
|
|
|
}
|
|
|
|
|
2008-08-17 00:25:56 -04:00
|
|
|
int
|
|
|
|
rb_econv_putbackable(rb_econv_t *ec)
|
|
|
|
{
|
|
|
|
return ec->elems[0].tc->readagain_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_econv_putback(rb_econv_t *ec, unsigned char *p, int n)
|
|
|
|
{
|
|
|
|
rb_transcoding *tc = ec->elems[0].tc;
|
|
|
|
memcpy(p, TRANSCODING_READBUF(tc) + tc->recognized_len, n);
|
|
|
|
tc->readagain_len -= n;
|
|
|
|
}
|
|
|
|
|
2008-08-18 08:06:42 -04:00
|
|
|
struct stateless_encoding_t {
|
|
|
|
const char *stateless_enc;
|
|
|
|
const char *stateful_enc;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
stateless_encoding_i(st_data_t key, st_data_t val, st_data_t arg)
|
|
|
|
{
|
|
|
|
struct stateless_encoding_t *data = (struct stateless_encoding_t *)arg;
|
|
|
|
st_table *table2 = (st_table *)val;
|
|
|
|
st_data_t v;
|
|
|
|
|
|
|
|
if (st_lookup(table2, (st_data_t)data->stateful_enc, &v)) {
|
|
|
|
transcoder_entry_t *entry = (transcoder_entry_t *)v;
|
|
|
|
const rb_transcoder *tr = load_transcoder_entry(entry);
|
|
|
|
if (tr && tr->stateful_type == stateful_encoder) {
|
|
|
|
data->stateless_enc = tr->from_encoding;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
rb_econv_stateless_encoding(const char *stateful_enc)
|
|
|
|
{
|
|
|
|
struct stateless_encoding_t data;
|
|
|
|
data.stateful_enc = stateful_enc;
|
|
|
|
data.stateless_enc = NULL;
|
|
|
|
st_foreach(transcoder_table, stateless_encoding_i, (st_data_t)&data);
|
|
|
|
if (data.stateless_enc)
|
|
|
|
return data.stateless_enc;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_econv_string(rb_econv_t *ec, VALUE src, long off, long len, VALUE dst, int flags)
|
|
|
|
{
|
|
|
|
unsigned const char *ss, *sp, *se;
|
|
|
|
unsigned char *ds, *dp, *de;
|
|
|
|
rb_econv_result_t res;
|
|
|
|
|
|
|
|
if (NIL_P(dst)) {
|
|
|
|
dst = rb_str_buf_new(len);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = econv_destination_buffer_full;
|
|
|
|
while (res == econv_destination_buffer_full) {
|
|
|
|
long dlen = RSTRING_LEN(dst);
|
|
|
|
int max_output = ec->last_tc->transcoder->max_output;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
ss = sp = (const unsigned char *)RSTRING_PTR(src) + off;
|
|
|
|
se = ss + len;
|
|
|
|
ds = dp = (unsigned char *)RSTRING_PTR(dst) + dlen;
|
|
|
|
de = ds + rb_str_capacity(dst);
|
|
|
|
res = rb_econv_convert(ec, &sp, se, &dp, de, flags);
|
|
|
|
off += sp - ss;
|
|
|
|
len -= sp - ss;
|
|
|
|
rb_str_set_len(dst, dlen + (dp - ds));
|
|
|
|
rb_econv_check_error(ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-16 11:04:39 -04:00
|
|
|
static VALUE
|
|
|
|
make_econv_exception(rb_econv_t *ec)
|
|
|
|
{
|
2008-08-16 23:05:40 -04:00
|
|
|
VALUE mesg, exc;
|
2008-08-16 11:04:39 -04:00
|
|
|
if (ec->last_error.result == econv_invalid_byte_sequence) {
|
|
|
|
VALUE bytes = rb_str_new((const char *)ec->last_error.error_bytes_start,
|
|
|
|
ec->last_error.error_bytes_len);
|
2008-08-16 23:05:40 -04:00
|
|
|
VALUE dumped;
|
|
|
|
dumped = rb_str_dump(bytes);
|
2008-08-16 11:04:39 -04:00
|
|
|
mesg = rb_sprintf("invalid byte sequence: %s on %s",
|
2008-08-16 23:05:40 -04:00
|
|
|
StringValueCStr(dumped),
|
2008-08-16 11:04:39 -04:00
|
|
|
ec->last_error.source_encoding);
|
2008-08-16 23:05:40 -04:00
|
|
|
exc = rb_exc_new3(rb_eInvalidByteSequence, mesg);
|
|
|
|
rb_ivar_set(exc, rb_intern("source_encoding"), rb_str_new2(ec->last_error.source_encoding));
|
|
|
|
rb_ivar_set(exc, rb_intern("destination_encoding"), rb_str_new2(ec->last_error.destination_encoding));
|
|
|
|
rb_ivar_set(exc, rb_intern("error_bytes"), bytes);
|
|
|
|
return exc;
|
2008-08-16 11: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);
|
2008-08-16 23:05:40 -04:00
|
|
|
VALUE dumped;
|
|
|
|
int idx;
|
|
|
|
dumped = rb_str_dump(bytes);
|
2008-08-16 11:04:39 -04:00
|
|
|
mesg = rb_sprintf("conversion undefined: %s from %s to %s",
|
2008-08-16 23:05:40 -04:00
|
|
|
StringValueCStr(dumped),
|
2008-08-16 11:04:39 -04:00
|
|
|
ec->last_error.source_encoding,
|
|
|
|
ec->last_error.destination_encoding);
|
2008-08-16 23:05:40 -04:00
|
|
|
exc = rb_exc_new3(rb_eConversionUndefined, mesg);
|
|
|
|
idx = rb_enc_find_index(ec->last_error.source_encoding);
|
|
|
|
rb_ivar_set(exc, rb_intern("source_encoding"), rb_str_new2(ec->last_error.source_encoding));
|
|
|
|
rb_ivar_set(exc, rb_intern("destination_encoding"), rb_str_new2(ec->last_error.destination_encoding));
|
|
|
|
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);
|
|
|
|
return exc;
|
2008-08-16 11:04:39 -04:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2008-08-09 02:02:01 -04:00
|
|
|
static void
|
|
|
|
more_output_buffer(
|
2008-08-09 09:34:21 -04:00
|
|
|
VALUE destination,
|
|
|
|
unsigned char *(*resize_destination)(VALUE, int, int),
|
2008-08-13 01:30:42 -04:00
|
|
|
int max_output,
|
2008-08-09 02: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 01:30:42 -04:00
|
|
|
size_t new_len = (len + max_output) * 2;
|
2008-08-09 09:34:21 -04:00
|
|
|
*out_start_ptr = resize_destination(destination, len, new_len);
|
2008-08-09 02:02:01 -04:00
|
|
|
*out_pos = *out_start_ptr + len;
|
|
|
|
*out_stop_ptr = *out_start_ptr + new_len;
|
|
|
|
}
|
|
|
|
|
2008-08-14 11:56:39 -04:00
|
|
|
static int
|
2008-08-16 01:32:42 -04:00
|
|
|
output_replacement_character(rb_econv_t *ec)
|
2008-08-11 18:44:23 -04:00
|
|
|
{
|
2008-08-14 11:56:39 -04:00
|
|
|
rb_transcoding *tc = ec->last_tc;
|
2008-08-11 18:44:23 -04:00
|
|
|
const rb_transcoder *tr;
|
|
|
|
rb_encoding *enc;
|
2008-08-14 11:56:39 -04:00
|
|
|
const unsigned char *replacement;
|
2008-08-15 07:02:07 -04:00
|
|
|
const char *repl_enc;
|
2008-08-11 18:44:23 -04:00
|
|
|
int len;
|
2008-08-15 07:02:07 -04:00
|
|
|
int ret;
|
2008-08-11 18:44:23 -04:00
|
|
|
|
|
|
|
tr = tc->transcoder;
|
|
|
|
enc = rb_enc_find(tr->to_encoding);
|
|
|
|
|
2008-08-15 07:02:07 -04:00
|
|
|
replacement = (const unsigned char *)get_replacement_character(enc, &len, &repl_enc);
|
2008-08-11 18:44:23 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
ret = rb_econv_insert_output(ec, replacement, len, repl_enc);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
2008-08-11 18:44:23 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
return 0;
|
2008-08-11 18:44:23 -04:00
|
|
|
}
|
|
|
|
|
2008-08-09 02:02:01 -04:00
|
|
|
#if 1
|
|
|
|
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 09:34:21 -04:00
|
|
|
VALUE destination,
|
|
|
|
unsigned char *(*resize_destination)(VALUE, int, int),
|
2008-08-11 11:50:42 -04:00
|
|
|
const char *from_encoding,
|
|
|
|
const char *to_encoding,
|
2008-08-09 02:02:01 -04:00
|
|
|
const int opt)
|
|
|
|
{
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_t *ec;
|
2008-08-13 01:30:42 -04:00
|
|
|
rb_transcoding *last_tc;
|
2008-08-14 07:31:27 -04:00
|
|
|
rb_econv_result_t ret;
|
2008-08-09 02:02:01 -04:00
|
|
|
unsigned char *out_start = *out_pos;
|
2008-08-11 11:50:42 -04:00
|
|
|
int max_output;
|
2008-08-16 11:04:39 -04:00
|
|
|
VALUE exc;
|
2008-08-09 02:02:01 -04:00
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
ec = rb_econv_open(from_encoding, to_encoding, 0);
|
|
|
|
if (!ec)
|
2008-08-11 11:50:42 -04:00
|
|
|
rb_raise(rb_eArgError, "transcoding not supported (from %s to %s)", from_encoding, to_encoding);
|
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
last_tc = ec->last_tc;
|
2008-08-13 01:30:42 -04:00
|
|
|
max_output = last_tc->transcoder->max_output;
|
2008-08-09 02:02:01 -04:00
|
|
|
|
|
|
|
resume:
|
2008-08-14 10:35:55 -04:00
|
|
|
ret = rb_econv_convert(ec, in_pos, in_stop, out_pos, out_stop, opt);
|
2008-08-14 09:45:54 -04:00
|
|
|
if (ret == econv_invalid_byte_sequence) {
|
2007-12-27 03:27:19 -05:00
|
|
|
/* deal with invalid byte sequence */
|
2008-02-21 03:42:10 -05:00
|
|
|
/* todo: add more alternative behaviors */
|
|
|
|
if (opt&INVALID_IGNORE) {
|
2008-08-09 02:02:01 -04:00
|
|
|
goto resume;
|
2008-02-21 03:42:10 -05:00
|
|
|
}
|
2008-07-14 02:27:26 -04:00
|
|
|
else if (opt&INVALID_REPLACE) {
|
2008-08-16 01:32:42 -04:00
|
|
|
if (output_replacement_character(ec) == 0)
|
2008-08-14 11:56:39 -04:00
|
|
|
goto resume;
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-08-16 11:04:39 -04:00
|
|
|
exc = make_econv_exception(ec);
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_close(ec);
|
2008-08-16 11:04:39 -04:00
|
|
|
rb_exc_raise(exc);
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
2008-08-14 09:45:54 -04:00
|
|
|
if (ret == econv_undefined_conversion) {
|
2008-07-14 02:27:26 -04:00
|
|
|
/* valid character in from encoding
|
|
|
|
* but no related character(s) in to encoding */
|
|
|
|
/* todo: add more alternative behaviors */
|
|
|
|
if (opt&UNDEF_IGNORE) {
|
2008-08-09 02:02:01 -04:00
|
|
|
goto resume;
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
|
|
|
else if (opt&UNDEF_REPLACE) {
|
2008-08-16 01:32:42 -04:00
|
|
|
if (output_replacement_character(ec) == 0)
|
2008-08-14 11:56:39 -04:00
|
|
|
goto resume;
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-08-16 11:04:39 -04:00
|
|
|
exc = make_econv_exception(ec);
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_close(ec);
|
2008-08-16 11:04:39 -04:00
|
|
|
rb_exc_raise(exc);
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
2008-08-14 09:45:54 -04:00
|
|
|
if (ret == econv_destination_buffer_full) {
|
2008-08-13 01:30:42 -04:00
|
|
|
more_output_buffer(destination, resize_destination, max_output, &out_start, out_pos, &out_stop);
|
2008-08-09 02:02:01 -04:00
|
|
|
goto resume;
|
|
|
|
}
|
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_close(ec);
|
2008-08-09 02: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 09:34:21 -04:00
|
|
|
VALUE destination,
|
2008-08-09 22:17:56 -04:00
|
|
|
unsigned char *(*resize_destination)(VALUE, int, int),
|
2008-08-11 11:50:42 -04:00
|
|
|
const char *from_encoding,
|
|
|
|
const char *to_encoding,
|
2008-08-09 02:02:01 -04:00
|
|
|
const int opt)
|
|
|
|
{
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_t *ec;
|
2008-08-13 01:30:42 -04:00
|
|
|
rb_transcoding *last_tc;
|
2008-08-14 07:31:27 -04:00
|
|
|
rb_econv_result_t ret;
|
2008-08-09 02:02:01 -04:00
|
|
|
unsigned char *out_start = *out_pos;
|
|
|
|
const unsigned char *ptr;
|
2008-08-11 11:50:42 -04:00
|
|
|
int max_output;
|
2008-08-16 11:04:39 -04:00
|
|
|
VALUE exc;
|
2008-08-09 02:02:01 -04:00
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
ec = rb_econv_open(from_encoding, to_encoding, 0);
|
|
|
|
if (!ec)
|
2008-08-11 11:50:42 -04:00
|
|
|
rb_raise(rb_eArgError, "transcoding not supported (from %s to %s)", from_encoding, to_encoding);
|
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
last_tc = ec->last_tc;
|
|
|
|
max_output = ec->elems[ec->num_trans-1].tc->transcoder->max_output;
|
2008-08-09 02:02:01 -04:00
|
|
|
|
2008-08-14 09:45:54 -04:00
|
|
|
ret = econv_source_buffer_empty;
|
2008-08-09 02:02:01 -04:00
|
|
|
ptr = *in_pos;
|
2008-08-14 09:45:54 -04:00
|
|
|
while (ret != econv_finished) {
|
2008-08-09 02:02:01 -04:00
|
|
|
unsigned char input_byte;
|
|
|
|
const unsigned char *p = &input_byte;
|
|
|
|
|
2008-08-14 09:45:54 -04:00
|
|
|
if (ret == econv_source_buffer_empty) {
|
2008-08-09 02:02:01 -04:00
|
|
|
if (ptr < in_stop) {
|
|
|
|
input_byte = *ptr;
|
2008-08-14 10:48:41 -04:00
|
|
|
ret = rb_econv_convert(ec, &p, p+1, out_pos, out_stop, ECONV_PARTIAL_INPUT);
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
|
|
|
else {
|
2008-08-14 10:35:55 -04:00
|
|
|
ret = rb_econv_convert(ec, NULL, NULL, out_pos, out_stop, 0);
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-08-14 10:48:41 -04:00
|
|
|
ret = rb_econv_convert(ec, NULL, NULL, out_pos, out_stop, ECONV_PARTIAL_INPUT);
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
|
|
|
if (&input_byte != p)
|
|
|
|
ptr += p - &input_byte;
|
|
|
|
switch (ret) {
|
2008-08-14 09:45:54 -04:00
|
|
|
case econv_invalid_byte_sequence:
|
2008-08-09 02:02:01 -04:00
|
|
|
/* deal with invalid byte sequence */
|
|
|
|
/* todo: add more alternative behaviors */
|
|
|
|
if (opt&INVALID_IGNORE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (opt&INVALID_REPLACE) {
|
2008-08-16 01:32:42 -04:00
|
|
|
if (output_replacement_character(ec) == 0)
|
2008-08-14 11:56:39 -04:00
|
|
|
break;
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
2008-08-16 11:04:39 -04:00
|
|
|
exc = make_econv_exception(ec);
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_close(ec);
|
2008-08-16 11:04:39 -04:00
|
|
|
rb_exc_raise(exc);
|
2008-08-09 02:02:01 -04:00
|
|
|
break;
|
|
|
|
|
2008-08-14 09:45:54 -04:00
|
|
|
case econv_undefined_conversion:
|
2008-08-09 02:02:01 -04:00
|
|
|
/* valid character in from encoding
|
|
|
|
* but no related character(s) in to encoding */
|
|
|
|
/* todo: add more alternative behaviors */
|
|
|
|
if (opt&UNDEF_IGNORE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (opt&UNDEF_REPLACE) {
|
2008-08-16 01:32:42 -04:00
|
|
|
if (output_replacement_character(ec) == 0)
|
2008-08-14 11:56:39 -04:00
|
|
|
break;
|
2008-08-09 02:02:01 -04:00
|
|
|
}
|
2008-08-16 11:04:39 -04:00
|
|
|
exc = make_econv_exception(ec);
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_close(ec);
|
2008-08-16 11:04:39 -04:00
|
|
|
rb_exc_raise(exc);
|
2008-08-09 02:02:01 -04:00
|
|
|
break;
|
|
|
|
|
2008-08-14 09:45:54 -04:00
|
|
|
case econv_destination_buffer_full:
|
2008-08-13 01:30:42 -04:00
|
|
|
more_output_buffer(destination, resize_destination, max_output, &out_start, out_pos, &out_stop);
|
2008-08-09 02:02:01 -04:00
|
|
|
break;
|
|
|
|
|
2008-08-14 09:45:54 -04:00
|
|
|
case econv_source_buffer_empty:
|
2008-08-09 02:02:01 -04:00
|
|
|
break;
|
|
|
|
|
2008-08-14 09:45:54 -04:00
|
|
|
case econv_finished:
|
2008-08-09 02:02:01 -04:00
|
|
|
break;
|
|
|
|
}
|
2008-08-07 10:53:30 -04:00
|
|
|
}
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_close(ec);
|
2008-08-09 02:02:01 -04:00
|
|
|
*in_pos = in_stop;
|
|
|
|
return;
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
2008-08-09 02:02:01 -04:00
|
|
|
#endif
|
2007-12-10 00:01:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* String-specific code
|
|
|
|
*/
|
|
|
|
|
2008-01-20 22:35:05 -05:00
|
|
|
static unsigned char *
|
2008-08-09 09:34:21 -04:00
|
|
|
str_transcoding_resize(VALUE destination, int len, int new_len)
|
2007-12-10 03:46:06 -05:00
|
|
|
{
|
2008-08-09 09:34:21 -04:00
|
|
|
rb_str_resize(destination, new_len);
|
|
|
|
return (unsigned char *)RSTRING_PTR(destination);
|
2007-12-10 03:46:06 -05:00
|
|
|
}
|
|
|
|
|
2007-12-18 03:04:26 -05:00
|
|
|
static int
|
|
|
|
str_transcode(int argc, VALUE *argv, VALUE *self)
|
2007-12-10 00:01:47 -05:00
|
|
|
{
|
|
|
|
VALUE dest;
|
2007-12-18 03:04:26 -05:00
|
|
|
VALUE str = *self;
|
2007-12-10 03:46:06 -05:00
|
|
|
long blen, slen;
|
2008-07-14 05:47:33 -04:00
|
|
|
unsigned char *buf, *bp, *sp;
|
|
|
|
const unsigned char *fromp;
|
2007-12-10 00:01:47 -05:00
|
|
|
rb_encoding *from_enc, *to_enc;
|
2007-12-10 03:46:06 -05:00
|
|
|
const char *from_e, *to_e;
|
2007-12-10 07:47:55 -05:00
|
|
|
int from_encidx, to_encidx;
|
|
|
|
VALUE from_encval, to_encval;
|
2008-02-21 03:42:10 -05:00
|
|
|
VALUE opt;
|
|
|
|
int options = 0;
|
|
|
|
|
|
|
|
opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash");
|
|
|
|
if (!NIL_P(opt)) {
|
|
|
|
VALUE v;
|
2007-12-10 00:01:47 -05:00
|
|
|
|
2008-02-21 03:42:10 -05:00
|
|
|
argc--;
|
|
|
|
v = rb_hash_aref(opt, sym_invalid);
|
|
|
|
if (NIL_P(v)) {
|
|
|
|
}
|
|
|
|
else if (v==sym_ignore) {
|
|
|
|
options |= INVALID_IGNORE;
|
|
|
|
}
|
2008-07-14 02:27:26 -04:00
|
|
|
else if (v==sym_replace) {
|
|
|
|
options |= INVALID_REPLACE;
|
|
|
|
v = rb_hash_aref(opt, sym_replace);
|
|
|
|
}
|
|
|
|
else {
|
2008-08-12 13:37:47 -04:00
|
|
|
rb_raise(rb_eArgError, "unknown value for invalid character option");
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
|
|
|
v = rb_hash_aref(opt, sym_undef);
|
|
|
|
if (NIL_P(v)) {
|
|
|
|
}
|
|
|
|
else if (v==sym_ignore) {
|
|
|
|
options |= UNDEF_IGNORE;
|
|
|
|
}
|
|
|
|
else if (v==sym_replace) {
|
|
|
|
options |= UNDEF_REPLACE;
|
|
|
|
}
|
|
|
|
else {
|
2008-08-12 13:37:47 -04:00
|
|
|
rb_raise(rb_eArgError, "unknown value for undefined character option");
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-02-21 03:42:10 -05:00
|
|
|
}
|
2007-12-27 11:55:06 -05:00
|
|
|
if (argc < 1 || argc > 2) {
|
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
2007-12-10 07:47:55 -05:00
|
|
|
if ((to_encidx = rb_to_encoding_index(to_encval = argv[0])) < 0) {
|
|
|
|
to_enc = 0;
|
2007-12-25 00:57:04 -05:00
|
|
|
to_encidx = 0;
|
2007-12-10 07:47:55 -05:00
|
|
|
to_e = StringValueCStr(to_encval);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
to_enc = rb_enc_from_index(to_encidx);
|
|
|
|
to_e = rb_enc_name(to_enc);
|
|
|
|
}
|
2007-12-10 00:01:47 -05:00
|
|
|
if (argc==1) {
|
2007-12-15 00:42:25 -05:00
|
|
|
from_encidx = rb_enc_get_index(str);
|
2007-12-10 07:47:55 -05:00
|
|
|
from_enc = rb_enc_from_index(from_encidx);
|
|
|
|
from_e = rb_enc_name(from_enc);
|
|
|
|
}
|
|
|
|
else if ((from_encidx = rb_to_encoding_index(from_encval = argv[1])) < 0) {
|
|
|
|
from_enc = 0;
|
|
|
|
from_e = StringValueCStr(from_encval);
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
|
|
|
else {
|
2007-12-10 07:47:55 -05:00
|
|
|
from_enc = rb_enc_from_index(from_encidx);
|
|
|
|
from_e = rb_enc_name(from_enc);
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
|
|
|
|
2007-12-10 07:47:55 -05:00
|
|
|
if (from_enc && from_enc == to_enc) {
|
2007-12-18 03:04:26 -05:00
|
|
|
return -1;
|
2007-12-10 03:46:06 -05:00
|
|
|
}
|
2007-12-10 07:47:55 -05:00
|
|
|
if (from_enc && to_enc && rb_enc_asciicompat(from_enc) && rb_enc_asciicompat(to_enc)) {
|
2007-12-28 04:26:55 -05:00
|
|
|
if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT) {
|
2007-12-18 03:04:26 -05:00
|
|
|
return to_encidx;
|
|
|
|
}
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
2008-01-20 16:40:08 -05:00
|
|
|
if (encoding_equal(from_e, to_e)) {
|
2007-12-18 03:04:26 -05:00
|
|
|
return -1;
|
2007-12-10 07:47:55 -05:00
|
|
|
}
|
2007-12-10 00:01:47 -05:00
|
|
|
|
2008-08-11 11: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 00:01:47 -05:00
|
|
|
|
2008-08-11 11:50:42 -04:00
|
|
|
transcode_loop(&fromp, &bp, (sp+slen), (bp+blen), dest, str_transcoding_resize, from_e, to_e, options);
|
|
|
|
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 00:42:25 -05:00
|
|
|
|
2007-12-10 03:46:06 -05:00
|
|
|
/* set encoding */
|
2007-12-10 07:47:55 -05:00
|
|
|
if (!to_enc) {
|
2007-12-25 00:57:04 -05:00
|
|
|
to_encidx = rb_define_dummy_encoding(to_e);
|
2007-12-10 07:47:55 -05:00
|
|
|
}
|
2007-12-18 03:04:26 -05:00
|
|
|
*self = dest;
|
2007-12-10 00:01:47 -05:00
|
|
|
|
2007-12-18 03:04:26 -05:00
|
|
|
return to_encidx;
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
|
|
|
|
2008-08-05 16: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 00:01:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-03-05 03:45:51 -05:00
|
|
|
* str.encode!(encoding [, options] ) => str
|
|
|
|
* str.encode!(to_encoding, from_encoding [, options] ) => str
|
2007-12-10 00:01:47 -05:00
|
|
|
*
|
2008-03-05 03:45:51 -05:00
|
|
|
* The first form transcodes the contents of <i>str</i> from
|
2007-12-10 00:01:47 -05:00
|
|
|
* str.encoding to +encoding+.
|
2008-03-05 03:45:51 -05:00
|
|
|
* The second form transcodes the contents of <i>str</i> from
|
2007-12-10 00:01:47 -05:00
|
|
|
* from_encoding to to_encoding.
|
2008-03-05 03:45:51 -05:00
|
|
|
* The options Hash gives details for conversion. See String#encode
|
|
|
|
* for details.
|
2007-12-10 00:01:47 -05:00
|
|
|
* Returns the string even if no changes were made.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2008-05-20 12:37:13 -04:00
|
|
|
str_encode_bang(int argc, VALUE *argv, VALUE str)
|
2007-12-10 00:01:47 -05:00
|
|
|
{
|
2007-12-18 03:04:26 -05:00
|
|
|
VALUE newstr = str;
|
|
|
|
int encidx = str_transcode(argc, argv, &newstr);
|
|
|
|
|
2007-12-21 03:49:08 -05:00
|
|
|
if (encidx < 0) return str;
|
2007-12-10 03:46:06 -05:00
|
|
|
rb_str_shared_replace(str, newstr);
|
2008-08-05 16:26:45 -04:00
|
|
|
return str_encode_associate(str, encidx);
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-03-05 03:45:51 -05:00
|
|
|
* str.encode(encoding [, options] ) => str
|
|
|
|
* str.encode(to_encoding, from_encoding [, options] ) => str
|
2007-12-10 00:01:47 -05:00
|
|
|
*
|
2008-03-05 03:45:51 -05:00
|
|
|
* The first form returns a copy of <i>str</i> transcoded
|
2007-12-10 00:01:47 -05:00
|
|
|
* to encoding +encoding+.
|
2008-03-05 03:45:51 -05:00
|
|
|
* The second form returns a copy of <i>str</i> transcoded
|
2007-12-10 00:01:47 -05:00
|
|
|
* from from_encoding to to_encoding.
|
2008-03-05 03:45:51 -05:00
|
|
|
* The options Hash gives details for conversion. Details
|
|
|
|
* to be added.
|
2007-12-10 00:01:47 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2008-05-20 12:37:13 -04:00
|
|
|
str_encode(int argc, VALUE *argv, VALUE str)
|
2007-12-10 00:01:47 -05:00
|
|
|
{
|
2008-08-05 16:26:45 -04:00
|
|
|
VALUE newstr = str;
|
|
|
|
int encidx = str_transcode(argc, argv, &newstr);
|
|
|
|
|
|
|
|
if (encidx < 0) return rb_str_dup(str);
|
|
|
|
RBASIC(newstr)->klass = rb_obj_class(str);
|
|
|
|
return str_encode_associate(newstr, encidx);
|
2008-05-20 12:37:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_transcode(VALUE str, VALUE to)
|
|
|
|
{
|
2008-05-23 19:15:14 -04:00
|
|
|
return str_encode(1, &to, str);
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
|
|
|
|
2008-08-12 10:46:18 -04:00
|
|
|
static void
|
2008-08-14 10:35:55 -04:00
|
|
|
econv_free(rb_econv_t *ec)
|
2008-08-12 10:46:18 -04:00
|
|
|
{
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_close(ec);
|
2008-08-12 10:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
econv_s_allocate(VALUE klass)
|
|
|
|
{
|
|
|
|
return Data_Wrap_Struct(klass, NULL, econv_free, NULL);
|
|
|
|
}
|
|
|
|
|
2008-08-14 10:28:10 -04:00
|
|
|
static rb_encoding *
|
2008-08-14 20:05:06 -04:00
|
|
|
make_dummy_encoding(const char *name)
|
2008-08-14 10:28:10 -04:00
|
|
|
{
|
|
|
|
rb_encoding *enc;
|
2008-08-14 20:05:06 -04:00
|
|
|
int idx;
|
|
|
|
idx = rb_define_dummy_encoding(name);
|
|
|
|
enc = rb_enc_from_index(idx);
|
2008-08-14 10:28:10 -04:00
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
2008-08-13 03:32:34 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-08-14 07:31:27 -04:00
|
|
|
* Encoding::Converter.new(source_encoding, destination_encoding)
|
|
|
|
* Encoding::Converter.new(source_encoding, destination_encoding, flags)
|
2008-08-13 03:32:34 -04:00
|
|
|
*
|
|
|
|
* possible flags:
|
2008-08-14 07:31:27 -04:00
|
|
|
* Encoding::Converter::UNIVERSAL_NEWLINE_DECODER # convert CRLF and CR to LF at last
|
|
|
|
* Encoding::Converter::CRLF_NEWLINE_ENCODER # convert LF to CRLF at first
|
|
|
|
* Encoding::Converter::CR_NEWLINE_ENCODER # convert LF to CR at first
|
2008-08-13 03:32:34 -04:00
|
|
|
*
|
|
|
|
* Encoding::Converter.new creates an instance of Encoding::Converter.
|
|
|
|
*
|
2008-08-14 07:31:27 -04:00
|
|
|
* source_encoding and destination_encoding should be a string.
|
2008-08-13 03:32:34 -04:00
|
|
|
* flags should be an integer.
|
|
|
|
*
|
|
|
|
* example:
|
|
|
|
* # UTF-16BE to UTF-8
|
|
|
|
* ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
|
|
|
|
*
|
|
|
|
* # (1) convert UTF-16BE to UTF-8
|
|
|
|
* # (2) convert CRLF and CR to LF
|
2008-08-14 07:31:27 -04:00
|
|
|
* ec = Encoding::Converter.new("UTF-16BE", "UTF-8", Encoding::Converter::UNIVERSAL_NEWLINE_DECODER)
|
2008-08-13 03:32:34 -04:00
|
|
|
*
|
|
|
|
* # (1) convert LF to CRLF
|
|
|
|
* # (2) convert UTF-8 to UTF-16BE
|
2008-08-14 07:31:27 -04:00
|
|
|
* ec = Encoding::Converter.new("UTF-8", "UTF-16BE", Encoding::Converter::CRLF_NEWLINE_ENCODER)
|
2008-08-13 03:32:34 -04:00
|
|
|
*
|
|
|
|
*/
|
2008-08-12 10:46:18 -04:00
|
|
|
static VALUE
|
2008-08-13 02:08:56 -04:00
|
|
|
econv_init(int argc, VALUE *argv, VALUE self)
|
2008-08-12 10:46:18 -04:00
|
|
|
{
|
2008-08-14 08:35:19 -04:00
|
|
|
VALUE source_encoding, destination_encoding, flags_v;
|
2008-08-14 20:05:06 -04:00
|
|
|
int sidx, didx;
|
|
|
|
const char *sname, *dname;
|
2008-08-14 08:35:19 -04:00
|
|
|
rb_encoding *senc, *denc;
|
|
|
|
rb_econv_t *ec;
|
2008-08-13 01:30:42 -04:00
|
|
|
int flags;
|
|
|
|
|
2008-08-14 08:35:19 -04:00
|
|
|
rb_scan_args(argc, argv, "21", &source_encoding, &destination_encoding, &flags_v);
|
2008-08-13 02:08:56 -04:00
|
|
|
|
|
|
|
if (flags_v == Qnil)
|
|
|
|
flags = 0;
|
|
|
|
else
|
|
|
|
flags = NUM2INT(flags_v);
|
2008-08-12 10:46:18 -04:00
|
|
|
|
2008-08-14 20:05:06 -04:00
|
|
|
senc = NULL;
|
|
|
|
sidx = rb_to_encoding_index(source_encoding);
|
|
|
|
if (0 <= sidx) {
|
|
|
|
senc = rb_enc_from_index(sidx);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
StringValue(source_encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
denc = NULL;
|
|
|
|
didx = rb_to_encoding_index(destination_encoding);
|
|
|
|
if (0 <= didx) {
|
|
|
|
denc = rb_enc_from_index(didx);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
StringValue(destination_encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
sname = senc ? senc->name : StringValueCStr(source_encoding);
|
|
|
|
dname = denc ? denc->name : StringValueCStr(destination_encoding);
|
2008-08-12 10:46:18 -04:00
|
|
|
|
|
|
|
if (DATA_PTR(self)) {
|
|
|
|
rb_raise(rb_eTypeError, "already initialized");
|
|
|
|
}
|
|
|
|
|
2008-08-14 20:05:06 -04:00
|
|
|
ec = rb_econv_open(sname, dname, flags);
|
2008-08-14 08:35:19 -04:00
|
|
|
if (!ec) {
|
2008-08-14 20:05:06 -04:00
|
|
|
rb_raise(rb_eArgError, "encoding convewrter not supported (from %s to %s)", sname, dname);
|
2008-08-12 10:46:18 -04:00
|
|
|
}
|
|
|
|
|
2008-08-14 20:34:16 -04:00
|
|
|
if (*sname && *dname) { /* check "" to "universal_newline" */
|
|
|
|
if (!senc)
|
|
|
|
senc = make_dummy_encoding(sname);
|
|
|
|
if (!denc)
|
|
|
|
denc = make_dummy_encoding(dname);
|
|
|
|
}
|
2008-08-14 20:05:06 -04:00
|
|
|
|
2008-08-14 10:28:10 -04:00
|
|
|
ec->source_encoding = senc;
|
|
|
|
ec->destination_encoding = denc;
|
|
|
|
|
2008-08-16 02:25:38 -04:00
|
|
|
ec->source_encoding_name = ec->elems[0].tc->transcoder->from_encoding;
|
|
|
|
ec->destination_encoding_name = ec->last_tc->transcoder->to_encoding;
|
|
|
|
|
2008-08-14 08:35:19 -04:00
|
|
|
DATA_PTR(self) = ec;
|
2008-08-12 10:46:18 -04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2008-08-13 04:47:57 -04:00
|
|
|
static VALUE
|
|
|
|
econv_inspect(VALUE self)
|
|
|
|
{
|
|
|
|
const char *cname = rb_obj_classname(self);
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_t *ec = DATA_PTR(self);
|
2008-08-13 04:47:57 -04:00
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
if (!ec)
|
2008-08-13 04:47:57 -04:00
|
|
|
return rb_sprintf("#<%s: uninitialized>", cname);
|
|
|
|
else
|
|
|
|
return rb_sprintf("#<%s: %s to %s>", cname,
|
2008-08-16 02:25:38 -04:00
|
|
|
ec->source_encoding_name,
|
|
|
|
ec->destination_encoding_name);
|
2008-08-13 04:47:57 -04:00
|
|
|
}
|
|
|
|
|
2008-08-12 10:46:18 -04:00
|
|
|
#define IS_ECONV(obj) (RDATA(obj)->dfree == (RUBY_DATA_FUNC)econv_free)
|
|
|
|
|
2008-08-14 07:31:27 -04:00
|
|
|
static rb_econv_t *
|
2008-08-12 10:46:18 -04:00
|
|
|
check_econv(VALUE self)
|
|
|
|
{
|
|
|
|
Check_Type(self, T_DATA);
|
|
|
|
if (!IS_ECONV(self)) {
|
|
|
|
rb_raise(rb_eTypeError, "wrong argument type %s (expected Encoding::Converter)",
|
|
|
|
rb_class2name(CLASS_OF(self)));
|
|
|
|
}
|
2008-08-13 04:47:57 -04:00
|
|
|
if (!DATA_PTR(self)) {
|
|
|
|
rb_raise(rb_eTypeError, "uninitialized encoding converter");
|
|
|
|
}
|
2008-08-12 10:46:18 -04:00
|
|
|
return DATA_PTR(self);
|
|
|
|
}
|
|
|
|
|
2008-08-14 10:28:10 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* source_encoding -> encoding
|
|
|
|
*
|
|
|
|
* returns source encoding as Encoding object.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_source_encoding(VALUE self)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
if (!ec->source_encoding)
|
|
|
|
return Qnil;
|
|
|
|
return rb_enc_from_encoding(ec->source_encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* destination_encoding -> encoding
|
|
|
|
*
|
|
|
|
* returns destination encoding as Encoding object.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_destination_encoding(VALUE self)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
if (!ec->destination_encoding)
|
|
|
|
return Qnil;
|
|
|
|
return rb_enc_from_encoding(ec->destination_encoding);
|
|
|
|
}
|
|
|
|
|
2008-08-15 05:12:56 -04:00
|
|
|
static VALUE
|
|
|
|
econv_result_to_symbol(rb_econv_result_t res)
|
|
|
|
{
|
|
|
|
switch (res) {
|
|
|
|
case econv_invalid_byte_sequence: return ID2SYM(rb_intern("invalid_byte_sequence"));
|
|
|
|
case econv_undefined_conversion: return ID2SYM(rb_intern("undefined_conversion"));
|
|
|
|
case econv_destination_buffer_full: return ID2SYM(rb_intern("destination_buffer_full"));
|
|
|
|
case econv_source_buffer_empty: return ID2SYM(rb_intern("source_buffer_empty"));
|
|
|
|
case econv_finished: return ID2SYM(rb_intern("finished"));
|
|
|
|
case econv_output_followed_by_input: return ID2SYM(rb_intern("output_followed_by_input"));
|
|
|
|
default: return INT2NUM(res); /* should not be reached */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-13 03:32:34 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-08-14 07:31:27 -04:00
|
|
|
* primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize) -> symbol
|
|
|
|
* primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize, flags) -> symbol
|
2008-08-13 03:32:34 -04:00
|
|
|
*
|
|
|
|
* possible flags:
|
2008-08-14 07:31:27 -04:00
|
|
|
* Encoding::Converter::PARTIAL_INPUT # source buffer may be part of larger source
|
2008-08-14 02:35:33 -04:00
|
|
|
* Encoding::Converter::OUTPUT_FOLLOWED_BY_INPUT # stop conversion after output before input
|
2008-08-13 03:32:34 -04:00
|
|
|
*
|
|
|
|
* possible results:
|
2008-08-14 07:31:27 -04:00
|
|
|
* :invalid_byte_sequence
|
2008-08-13 03:32:34 -04:00
|
|
|
* :undefined_conversion
|
2008-08-14 02:35:33 -04:00
|
|
|
* :output_followed_by_input
|
2008-08-14 07:31:27 -04:00
|
|
|
* :destination_buffer_full
|
|
|
|
* :source_buffer_empty
|
2008-08-13 03:32:34 -04:00
|
|
|
* :finished
|
|
|
|
*
|
2008-08-14 07:31:27 -04:00
|
|
|
* primitive_convert converts source_buffer into destination_buffer.
|
2008-08-13 03:32:34 -04:00
|
|
|
*
|
2008-08-14 07:31:27 -04:00
|
|
|
* source_buffer and destination_buffer should be a string.
|
|
|
|
* destination_byteoffset should be an integer or nil.
|
|
|
|
* destination_bytesize and flags should be an integer.
|
2008-08-13 03:32:34 -04:00
|
|
|
*
|
2008-08-14 07:31:27 -04:00
|
|
|
* primitive_convert convert the content of source_buffer from beginning
|
|
|
|
* and store the result into destination_buffer.
|
2008-08-13 03:32:34 -04:00
|
|
|
*
|
2008-08-14 07:31:27 -04:00
|
|
|
* destination_byteoffset and destination_bytesize specify the region which
|
2008-08-13 19:45:31 -04:00
|
|
|
* the converted result is stored.
|
2008-08-14 07: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.
|
|
|
|
* After conversion, destination_buffer is resized to
|
|
|
|
* destination_byteoffset + actually converted number of bytes.
|
2008-08-14 12:10:57 -04:00
|
|
|
* Also destination_buffer's encoding is set to destination_encoding.
|
2008-08-13 19:45:31 -04:00
|
|
|
*
|
2008-08-14 07:31:27 -04:00
|
|
|
* primitive_convert drops the first part of source_buffer.
|
|
|
|
* the dropped part is converted in destination_buffer or
|
2008-08-13 03:32:34 -04:00
|
|
|
* buffered in Encoding::Converter object.
|
|
|
|
*
|
|
|
|
* primitive_convert stops conversion when one of following condition met.
|
2008-08-14 07:31:27 -04:00
|
|
|
* - invalid byte sequence found in source buffer (:invalid_byte_sequence)
|
2008-08-13 03:32:34 -04:00
|
|
|
* - character not representable in output encoding (:undefined_conversion)
|
2008-08-14 07:31:27 -04:00
|
|
|
* - after some output is generated, before input is done (:output_followed_by_input)
|
2008-08-14 02:35:33 -04:00
|
|
|
* this occur only when OUTPUT_FOLLOWED_BY_INPUT is specified.
|
2008-08-14 07:31:27 -04:00
|
|
|
* - destination buffer is full (:destination_buffer_full)
|
|
|
|
* - source buffer is empty (:source_buffer_empty)
|
2008-08-13 03:32:34 -04:00
|
|
|
* this occur only when PARTIAL_INPUT is specified.
|
|
|
|
* - conversion is finished (:finished)
|
|
|
|
*
|
|
|
|
* example:
|
|
|
|
* ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
|
|
|
|
* ret = ec.primitive_convert(src="pi", dst="", 100)
|
|
|
|
* p [ret, src, dst] #=> [:finished, "", "\x00p\x00i"]
|
|
|
|
*
|
|
|
|
* ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
|
2008-08-13 04:28:39 -04:00
|
|
|
* ret = ec.primitive_convert(src="pi", dst="", 1)
|
2008-08-14 07:31:27 -04:00
|
|
|
* p [ret, src, dst] #=> [:destination_buffer_full, "i", "\x00"]
|
2008-08-13 04:28:39 -04:00
|
|
|
* ret = ec.primitive_convert(src, dst="", 1)
|
2008-08-14 07:31:27 -04:00
|
|
|
* p [ret, src, dst] #=> [:destination_buffer_full, "", "p"]
|
2008-08-13 04:28:39 -04:00
|
|
|
* ret = ec.primitive_convert(src, dst="", 1)
|
2008-08-14 07:31:27 -04:00
|
|
|
* p [ret, src, dst] #=> [:destination_buffer_full, "", "\x00"]
|
2008-08-13 04:28:39 -04:00
|
|
|
* ret = ec.primitive_convert(src, dst="", 1)
|
|
|
|
* p [ret, src, dst] #=> [:finished, "", "i"]
|
2008-08-13 03:32:34 -04:00
|
|
|
*
|
|
|
|
*/
|
2008-08-12 10:46:18 -04:00
|
|
|
static VALUE
|
2008-08-13 02:08:56 -04:00
|
|
|
econv_primitive_convert(int argc, VALUE *argv, VALUE self)
|
2008-08-12 10:46:18 -04:00
|
|
|
{
|
2008-08-13 13:24:42 -04:00
|
|
|
VALUE input, output, output_byteoffset_v, output_bytesize_v, flags_v;
|
2008-08-14 10:35:55 -04:00
|
|
|
rb_econv_t *ec = check_econv(self);
|
2008-08-14 07:31:27 -04:00
|
|
|
rb_econv_result_t res;
|
2008-08-12 10:46:18 -04:00
|
|
|
const unsigned char *ip, *is;
|
|
|
|
unsigned char *op, *os;
|
2008-08-13 13:24:42 -04:00
|
|
|
long output_byteoffset, output_bytesize;
|
|
|
|
unsigned long output_byteend;
|
2008-08-12 10:46:18 -04:00
|
|
|
int flags;
|
|
|
|
|
2008-08-13 13:24:42 -04:00
|
|
|
rb_scan_args(argc, argv, "41", &input, &output, &output_byteoffset_v, &output_bytesize_v, &flags_v);
|
|
|
|
|
|
|
|
if (output_byteoffset_v == Qnil)
|
|
|
|
output_byteoffset = 0;
|
|
|
|
else
|
|
|
|
output_byteoffset = NUM2LONG(output_byteoffset_v);
|
|
|
|
|
|
|
|
output_bytesize = NUM2LONG(output_bytesize_v);
|
2008-08-13 02:08:56 -04:00
|
|
|
|
|
|
|
if (flags_v == Qnil)
|
|
|
|
flags = 0;
|
|
|
|
else
|
|
|
|
flags = NUM2INT(flags_v);
|
2008-08-13 13:24:42 -04:00
|
|
|
|
2008-08-12 10:46:18 -04:00
|
|
|
StringValue(output);
|
2008-08-12 19:03:46 -04:00
|
|
|
StringValue(input);
|
2008-08-12 10:46:18 -04:00
|
|
|
rb_str_modify(output);
|
2008-08-12 19:03:46 -04:00
|
|
|
|
2008-08-13 13:24:42 -04:00
|
|
|
if (output_byteoffset_v == Qnil)
|
|
|
|
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 10:46:18 -04:00
|
|
|
|
|
|
|
ip = (const unsigned char *)RSTRING_PTR(input);
|
|
|
|
is = ip + RSTRING_LEN(input);
|
|
|
|
|
2008-08-13 13:24:42 -04:00
|
|
|
op = (unsigned char *)RSTRING_PTR(output) + output_byteoffset;
|
|
|
|
os = op + output_bytesize;
|
2008-08-12 10:46:18 -04:00
|
|
|
|
2008-08-14 10:35:55 -04:00
|
|
|
res = rb_econv_convert(ec, &ip, is, &op, os, flags);
|
2008-08-12 10:46:18 -04:00
|
|
|
rb_str_set_len(output, op-(unsigned char *)RSTRING_PTR(output));
|
|
|
|
rb_str_drop_bytes(input, ip - (unsigned char *)RSTRING_PTR(input));
|
|
|
|
|
2008-08-14 12:06:33 -04:00
|
|
|
if (ec->destination_encoding) {
|
|
|
|
rb_enc_associate(output, ec->destination_encoding);
|
|
|
|
}
|
|
|
|
|
2008-08-15 05:12:56 -04:00
|
|
|
return econv_result_to_symbol(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* primitive_errinfo -> array
|
|
|
|
*
|
|
|
|
* primitive_errinfo returns a precious information of last error result
|
|
|
|
* as a 6-elements array:
|
|
|
|
*
|
|
|
|
* [result, enc1, enc2, error_bytes, readagain_bytes, partial_input]
|
|
|
|
*
|
|
|
|
* result is the last result of primitive_convert.
|
|
|
|
*
|
|
|
|
* partial_input is :partial_input or nil.
|
|
|
|
* :partial_input means that Encoding::Converter::PARTIAL_INPUT is specified
|
|
|
|
* for primitive_convert.
|
|
|
|
*
|
|
|
|
* Other elements are only meaningful when result is
|
|
|
|
* :invalid_byte_sequence or :undefined_conversion.
|
|
|
|
*
|
|
|
|
* enc1 and enc2 indicats a conversion step as pair of strings.
|
|
|
|
* For example, EUC-JP to ISO-8859-1 is
|
|
|
|
* converted as EUC-JP -> UTF-8 -> ISO-8859-1.
|
|
|
|
* So [enc1, enc2] is ["EUC-JP", "UTF-8"] or ["UTF-8", "ISO-8859-1"].
|
|
|
|
*
|
|
|
|
* error_bytes and readagain_bytes indicats the byte sequences which causes the error.
|
|
|
|
* 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")
|
|
|
|
* ec.primitive_convert(src="\xff", dst="", nil, 10)
|
|
|
|
* p ec.primitive_errinfo
|
|
|
|
* #=> [:invalid_byte_sequence, "EUC-JP", "UTF-8", "\xFF", "", nil]
|
|
|
|
*
|
|
|
|
* # 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
|
|
|
|
* #=> [:undefined_conversion, "UTF-8", "ISO-8859-1", "\xE3\x81\x82", "", nil]
|
|
|
|
*
|
|
|
|
* # 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
|
|
|
|
* #=> [:invalid_byte_sequence, "EUC-JP", "UTF-8", "\xA4", "", nil]
|
|
|
|
*
|
|
|
|
* # Encoding::Converter::PARTIAL_INPUT prevents invalid errors by
|
|
|
|
* # partial characters.
|
|
|
|
* ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
|
|
|
|
* ec.primitive_convert(src="\xa4", dst="", nil, 10, Encoding::Converter::PARTIAL_INPUT)
|
|
|
|
* p ec.primitive_errinfo
|
|
|
|
* #=> [:source_buffer_empty, nil, nil, nil, nil, :partial_input]
|
|
|
|
*
|
|
|
|
* # \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
|
|
|
|
* #=> [:invalid_byte_sequence, "UTF-16BE", "UTF-8", "\xD8\x00", "\x00", nil]
|
|
|
|
* 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
|
|
|
|
* #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "@\x00", nil]
|
|
|
|
* p src
|
|
|
|
* #=> ""
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
econv_primitive_errinfo(VALUE self)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
|
|
|
|
VALUE ary;
|
|
|
|
|
|
|
|
ary = rb_ary_new2(6);
|
|
|
|
|
|
|
|
rb_ary_store(ary, 0, econv_result_to_symbol(ec->last_error.result));
|
|
|
|
|
|
|
|
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 10:46:18 -04:00
|
|
|
}
|
2008-08-15 05:12:56 -04:00
|
|
|
|
|
|
|
rb_ary_store(ary, 5, ec->last_error.partial_input ? ID2SYM(rb_intern("partial_input")) : Qnil);
|
|
|
|
|
|
|
|
return ary;
|
2008-08-12 10:46:18 -04:00
|
|
|
}
|
|
|
|
|
2008-08-15 10:17:11 -04:00
|
|
|
static VALUE
|
2008-08-16 01:32:42 -04:00
|
|
|
econv_primitive_insert_output(VALUE self, VALUE string)
|
2008-08-15 10:17:11 -04:00
|
|
|
{
|
2008-08-16 01:32:42 -04:00
|
|
|
const char *insert_enc;
|
2008-08-15 10:17:11 -04:00
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
|
|
|
|
StringValue(string);
|
2008-08-16 01:32:42 -04:00
|
|
|
insert_enc = rb_econv_encoding_to_insert_output(ec);
|
|
|
|
string = rb_str_transcode(string, rb_enc_from_encoding(rb_enc_find(insert_enc)));
|
2008-08-15 10:17:11 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
ret = rb_econv_insert_output(ec, (const unsigned char *)RSTRING_PTR(string), RSTRING_LEN(string), insert_enc);
|
2008-08-15 10:17:11 -04:00
|
|
|
|
2008-08-16 01:32:42 -04:00
|
|
|
if (ret == -1)
|
|
|
|
return Qfalse;
|
|
|
|
return Qtrue;
|
2008-08-15 10:17:11 -04:00
|
|
|
}
|
|
|
|
|
2008-08-17 00:40:59 -04:00
|
|
|
static VALUE
|
|
|
|
econv_primitive_putback(VALUE self, VALUE max)
|
|
|
|
{
|
|
|
|
rb_econv_t *ec = check_econv(self);
|
|
|
|
int n;
|
|
|
|
int putbackable;
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
if (NIL_P(max))
|
|
|
|
n = rb_econv_putbackable(ec);
|
|
|
|
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);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-08-16 11: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-16 23:05:40 -04:00
|
|
|
static VALUE
|
|
|
|
ecerr_source_encoding(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("source_encoding"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ecerr_destination_encoding(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("destination_encoding"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ecerr_error_char(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("error_char"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ecerr_error_bytes(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("error_bytes"));
|
|
|
|
}
|
|
|
|
|
2007-12-10 00:01:47 -05:00
|
|
|
void
|
|
|
|
Init_transcode(void)
|
|
|
|
{
|
2008-08-12 03:20:10 -04:00
|
|
|
rb_eConversionUndefined = rb_define_class_under(rb_cEncoding, "ConversionUndefined", rb_eStandardError);
|
|
|
|
rb_eInvalidByteSequence = rb_define_class_under(rb_cEncoding, "InvalidByteSequence", rb_eStandardError);
|
|
|
|
|
2007-12-25 00:57:04 -05:00
|
|
|
transcoder_table = st_init_strcasetable();
|
|
|
|
|
2008-02-21 03:42:10 -05:00
|
|
|
sym_invalid = ID2SYM(rb_intern("invalid"));
|
2008-07-14 02:27:26 -04:00
|
|
|
sym_undef = ID2SYM(rb_intern("undef"));
|
2008-02-21 03:42:10 -05:00
|
|
|
sym_ignore = ID2SYM(rb_intern("ignore"));
|
2008-07-14 02:27:26 -04:00
|
|
|
sym_replace = ID2SYM(rb_intern("replace"));
|
2008-02-21 03:42:10 -05:00
|
|
|
|
2008-05-20 12: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 10: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-08-13 02:08:56 -04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "initialize", econv_init, -1);
|
2008-08-13 04:47:57 -04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "inspect", econv_inspect, 0);
|
2008-08-14 10: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 02:08:56 -04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "primitive_convert", econv_primitive_convert, -1);
|
2008-08-15 05:12:56 -04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "primitive_errinfo", econv_primitive_errinfo, 0);
|
2008-08-16 01:32:42 -04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "primitive_insert_output", econv_primitive_insert_output, 1);
|
2008-08-17 00:40:59 -04:00
|
|
|
rb_define_method(rb_cEncodingConverter, "primitive_putback", econv_primitive_putback, 1);
|
2008-08-14 10:48:41 -04:00
|
|
|
rb_define_const(rb_cEncodingConverter, "PARTIAL_INPUT", INT2FIX(ECONV_PARTIAL_INPUT));
|
|
|
|
rb_define_const(rb_cEncodingConverter, "OUTPUT_FOLLOWED_BY_INPUT", INT2FIX(ECONV_OUTPUT_FOLLOWED_BY_INPUT));
|
|
|
|
rb_define_const(rb_cEncodingConverter, "UNIVERSAL_NEWLINE_DECODER", INT2FIX(ECONV_UNIVERSAL_NEWLINE_DECODER));
|
|
|
|
rb_define_const(rb_cEncodingConverter, "CRLF_NEWLINE_ENCODER", INT2FIX(ECONV_CRLF_NEWLINE_ENCODER));
|
|
|
|
rb_define_const(rb_cEncodingConverter, "CR_NEWLINE_ENCODER", INT2FIX(ECONV_CR_NEWLINE_ENCODER));
|
2008-08-16 23:05:40 -04:00
|
|
|
|
|
|
|
rb_define_method(rb_eConversionUndefined, "source_encoding", ecerr_source_encoding, 0);
|
|
|
|
rb_define_method(rb_eConversionUndefined, "destination_encoding", ecerr_destination_encoding, 0);
|
|
|
|
rb_define_method(rb_eConversionUndefined, "error_char", ecerr_error_char, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_eInvalidByteSequence, "source_encoding", ecerr_source_encoding, 0);
|
|
|
|
rb_define_method(rb_eInvalidByteSequence, "destination_encoding", ecerr_destination_encoding, 0);
|
|
|
|
rb_define_method(rb_eInvalidByteSequence, "error_bytes", ecerr_error_bytes, 0);
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|