2007-12-10 08:01:47 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
transcode.c -
|
|
|
|
|
2007-12-10 11:25:01 +03:00
|
|
|
$Author$
|
2007-12-10 08:01:47 +03:00
|
|
|
created at: Tue Oct 30 16:10:22 JST 2007
|
|
|
|
|
|
|
|
Copyright (C) 2007 Martin Duerst
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "ruby/encoding.h"
|
2007-12-25 08:57:04 +03:00
|
|
|
#define PType (int)
|
2007-12-10 08:01:47 +03:00
|
|
|
#include "transcode_data.h"
|
2007-12-25 08:57:04 +03:00
|
|
|
#include <ctype.h>
|
2007-12-10 08:01:47 +03:00
|
|
|
|
2008-07-14 10:27:26 +04:00
|
|
|
static VALUE sym_invalid, sym_undef, sym_ignore, sym_replace;
|
2008-02-21 11:42:10 +03:00
|
|
|
#define INVALID_IGNORE 0x1
|
2008-07-14 10:27:26 +04:00
|
|
|
#define INVALID_REPLACE 0x2
|
|
|
|
#define UNDEF_IGNORE 0x10
|
|
|
|
#define UNDEF_REPLACE 0x20
|
2008-08-09 10:02:01 +04:00
|
|
|
#define PARTIAL_INPUT 0x100
|
2008-02-21 11:42:10 +03:00
|
|
|
|
2007-12-10 08:01:47 +03:00
|
|
|
/*
|
|
|
|
* Dispatch data and logic
|
|
|
|
*/
|
|
|
|
|
2008-08-07 18: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 08:57:04 +03:00
|
|
|
|
2008-08-07 18:53:30 +04:00
|
|
|
static st_table *transcoder_table;
|
2007-12-25 08:57:04 +03:00
|
|
|
|
2008-08-07 18:53:30 +04:00
|
|
|
static transcoder_entry_t *
|
|
|
|
make_transcoder_entry(const char *from, const char *to)
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
2008-08-07 18: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 08:57:04 +03:00
|
|
|
}
|
2007-12-10 08:01:47 +03:00
|
|
|
|
2007-12-25 08:57:04 +03:00
|
|
|
void
|
|
|
|
rb_register_transcoder(const rb_transcoder *tr)
|
|
|
|
{
|
|
|
|
const char *const from_e = tr->from_encoding;
|
|
|
|
const char *const to_e = tr->to_encoding;
|
|
|
|
|
2008-08-07 18:53:30 +04:00
|
|
|
transcoder_entry_t *entry;
|
|
|
|
|
|
|
|
entry = make_transcoder_entry(from_e, to_e);
|
|
|
|
if (entry->transcoder) {
|
2007-12-25 08:57:04 +03:00
|
|
|
rb_raise(rb_eArgError, "transcoder from %s to %s has been already registered",
|
|
|
|
from_e, to_e);
|
|
|
|
}
|
2008-08-07 18:53:30 +04:00
|
|
|
|
|
|
|
entry->transcoder = tr;
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
2007-12-24 16:51:19 +03:00
|
|
|
static void
|
2007-12-25 08:57:04 +03:00
|
|
|
declare_transcoder(const char *to, const char *from, const char *lib)
|
2007-12-24 16:51:19 +03:00
|
|
|
{
|
2008-08-07 18:53:30 +04:00
|
|
|
transcoder_entry_t *entry;
|
2007-12-25 08:57:04 +03:00
|
|
|
|
2008-08-07 18:53:30 +04:00
|
|
|
entry = make_transcoder_entry(from, to);
|
|
|
|
entry->lib = lib;
|
2007-12-24 16:51:19 +03:00
|
|
|
}
|
|
|
|
|
2007-12-25 08:57:04 +03: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 08:01:47 +03:00
|
|
|
{
|
2007-12-25 08:57:04 +03: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 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
2008-01-01 15:24:04 +03:00
|
|
|
#define encoding_equal(enc1, enc2) (STRCASECMP(enc1, enc2) == 0)
|
2007-12-25 08:57:04 +03:00
|
|
|
|
2008-08-07 18:53:30 +04:00
|
|
|
typedef struct search_path_queue_tag {
|
|
|
|
struct search_path_queue_tag *next;
|
|
|
|
const char *enc;
|
|
|
|
} search_path_queue_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
st_table *visited;
|
|
|
|
search_path_queue_t *queue;
|
|
|
|
search_path_queue_t **queue_last_ptr;
|
|
|
|
const char *base_enc;
|
|
|
|
} search_path_bfs_t;
|
|
|
|
|
|
|
|
static int
|
|
|
|
transcode_search_path_i(st_data_t key, st_data_t val, st_data_t arg)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
while (bfs.queue) {
|
|
|
|
q = bfs.queue;
|
|
|
|
bfs.queue = q->next;
|
|
|
|
xfree(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
const char *enc = to;
|
|
|
|
int depth = 0;
|
|
|
|
while (1) {
|
|
|
|
st_lookup(bfs.visited, (st_data_t)enc, &val);
|
|
|
|
if (!val)
|
|
|
|
break;
|
|
|
|
depth++;
|
|
|
|
enc = (const char *)val;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
transcode_dispatch_cb(const char *from, const char *to, int depth, void *arg)
|
|
|
|
{
|
|
|
|
const rb_transcoder **first_transcoder_ptr = (const rb_transcoder **)arg;
|
|
|
|
|
|
|
|
transcoder_entry_t *entry;
|
|
|
|
|
|
|
|
if (!*first_transcoder_ptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
entry = get_transcoder_entry(from, to);
|
|
|
|
if (!entry)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
if (!entry->transcoder && 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) goto failed;
|
|
|
|
memcpy(path, transcoder_lib_prefix, sizeof(transcoder_lib_prefix) - 1);
|
|
|
|
memcpy(path + sizeof(transcoder_lib_prefix) - 1, lib, len + 1);
|
|
|
|
if (!rb_require(path)) goto failed;
|
|
|
|
}
|
|
|
|
if (!entry->transcoder)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
if (depth == 0)
|
|
|
|
*first_transcoder_ptr = entry->transcoder;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
*first_transcoder_ptr = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-21 00:40:08 +03:00
|
|
|
static const rb_transcoder *
|
2008-08-01 00:35:35 +04:00
|
|
|
transcode_dispatch(const char *from_encoding, const char *to_encoding)
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
2008-08-07 18:53:30 +04:00
|
|
|
const rb_transcoder *first_transcoder = (rb_transcoder *)1;
|
|
|
|
|
|
|
|
if (transcode_search_path(from_encoding, to_encoding, transcode_dispatch_cb, (void *)&first_transcoder)) {
|
|
|
|
return first_transcoder;
|
2007-12-15 08:42:25 +03:00
|
|
|
}
|
2008-08-07 18:53:30 +04:00
|
|
|
return NULL;
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
2008-08-01 00:35:35 +04:00
|
|
|
static void
|
|
|
|
output_replacement_character(unsigned char **out_pp, rb_encoding *enc)
|
2008-07-14 10:27:26 +04:00
|
|
|
{
|
2008-08-01 00:35:35 +04:00
|
|
|
unsigned char *out_p = *out_pp;
|
2008-07-14 10: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-08-01 00:35:35 +04:00
|
|
|
if (rb_utf8_encoding() == enc) {
|
|
|
|
*out_p++ = 0xEF;
|
|
|
|
*out_p++ = 0xBF;
|
|
|
|
*out_p++ = 0xBD;
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
2008-07-31 12:12:08 +04:00
|
|
|
else if (utf16be_encoding == enc) {
|
2008-08-01 00:35:35 +04:00
|
|
|
*out_p++ = 0xFF;
|
|
|
|
*out_p++ = 0xFD;
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
2008-07-31 12:12:08 +04:00
|
|
|
else if (utf16le_encoding == enc) {
|
2008-08-01 00:35:35 +04:00
|
|
|
*out_p++ = 0xFD;
|
|
|
|
*out_p++ = 0xFF;
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
2008-07-31 12:12:08 +04:00
|
|
|
else if (utf32be_encoding == enc) {
|
2008-08-01 00:35:35 +04:00
|
|
|
*out_p++ = 0x00;
|
|
|
|
*out_p++ = 0x00;
|
|
|
|
*out_p++ = 0xFF;
|
|
|
|
*out_p++ = 0xFD;
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
2008-07-31 12:12:08 +04:00
|
|
|
else if (utf32le_encoding == enc) {
|
2008-08-01 00:35:35 +04:00
|
|
|
*out_p++ = 0xFD;
|
|
|
|
*out_p++ = 0xFF;
|
|
|
|
*out_p++ = 0x00;
|
|
|
|
*out_p++ = 0x00;
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-08-01 00:35:35 +04:00
|
|
|
*out_p++ = '?';
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
2008-08-01 00:35:35 +04:00
|
|
|
*out_pp = out_p;
|
|
|
|
return;
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
2007-12-10 08:01:47 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Transcoding engine logic
|
|
|
|
*/
|
2008-08-09 10:02:01 +04:00
|
|
|
|
|
|
|
static const unsigned char *
|
2008-08-10 06:33:05 +04:00
|
|
|
transcode_char_start(rb_transcoding *tc,
|
2008-08-10 04:23:36 +04:00
|
|
|
const unsigned char *in_start,
|
|
|
|
const unsigned char *inchar_start,
|
2008-08-10 03:33:37 +04:00
|
|
|
const unsigned char *in_p,
|
2008-08-10 04:23:36 +04:00
|
|
|
size_t *char_len_ptr)
|
2008-08-09 10:02:01 +04:00
|
|
|
{
|
|
|
|
const unsigned char *ptr;
|
2008-08-10 06:33:05 +04:00
|
|
|
if (inchar_start - in_start < tc->readlen) {
|
|
|
|
MEMCPY(TRANSCODING_READBUF(tc) + tc->readlen,
|
2008-08-10 04:23:36 +04:00
|
|
|
inchar_start, unsigned char, in_p - inchar_start);
|
2008-08-10 06:33:05 +04:00
|
|
|
ptr = TRANSCODING_READBUF(tc);
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-08-10 06:33:05 +04:00
|
|
|
ptr = inchar_start - tc->readlen;
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
2008-08-10 06:33:05 +04:00
|
|
|
*char_len_ptr = tc->readlen + (in_p - inchar_start);
|
2008-08-09 10:02:01 +04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
transcode_invalid_input,
|
|
|
|
transcode_undefined_conversion,
|
|
|
|
transcode_obuf_full,
|
|
|
|
transcode_ibuf_empty,
|
|
|
|
transcode_finished,
|
|
|
|
} transcode_result_t;
|
|
|
|
|
|
|
|
static transcode_result_t
|
2008-08-10 06:17:56 +04:00
|
|
|
transcode_restartable0(const unsigned char **in_pos, unsigned char **out_pos,
|
2008-08-10 03:33:37 +04:00
|
|
|
const unsigned char *in_stop, unsigned char *out_stop,
|
2008-08-10 06:33:05 +04:00
|
|
|
rb_transcoding *tc,
|
2008-08-09 10:02:01 +04:00
|
|
|
const int opt)
|
|
|
|
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
2008-08-10 06:33:05 +04:00
|
|
|
const rb_transcoder *tr = tc->transcoder;
|
|
|
|
int unitlen = tr->input_unit_length;
|
2008-08-10 06:17:56 +04:00
|
|
|
int feedlen = 0;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
2008-08-10 04:23:36 +04:00
|
|
|
const unsigned char *inchar_start;
|
2008-08-10 03:33:37 +04:00
|
|
|
const unsigned char *in_p;
|
2008-08-10 04:23:36 +04:00
|
|
|
|
2008-08-09 10:02:01 +04:00
|
|
|
unsigned char *out_p;
|
2008-08-09 14:32:02 +04:00
|
|
|
const BYTE_LOOKUP *next_table;
|
|
|
|
VALUE next_info;
|
|
|
|
unsigned char next_byte;
|
|
|
|
|
2008-08-10 03: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 10:02:01 +04:00
|
|
|
|
|
|
|
if (!out_pos) {
|
|
|
|
out_pos = &empty_ptr;
|
|
|
|
out_stop = empty_ptr;
|
|
|
|
}
|
2008-08-09 14:32:02 +04:00
|
|
|
|
2008-08-10 04:23:36 +04:00
|
|
|
in_p = inchar_start = *in_pos;
|
|
|
|
|
2008-08-10 03:33:37 +04:00
|
|
|
out_p = *out_pos;
|
2008-08-10 06:33:05 +04:00
|
|
|
next_table = tc->next_table;
|
|
|
|
next_info = tc->next_info;
|
|
|
|
next_byte = tc->next_byte;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
|
|
|
#define SUSPEND(ret, num) \
|
|
|
|
do { \
|
2008-08-10 06:33:05 +04:00
|
|
|
tc->resume_position = (num); \
|
2008-08-10 04:23:36 +04:00
|
|
|
if (0 < in_p - inchar_start) \
|
2008-08-10 06:33:05 +04:00
|
|
|
MEMMOVE(TRANSCODING_READBUF(tc)+tc->readlen, \
|
2008-08-10 04:23:36 +04:00
|
|
|
inchar_start, unsigned char, in_p - inchar_start); \
|
2008-08-10 03:33:37 +04:00
|
|
|
*in_pos = in_p; \
|
2008-08-09 10:02:01 +04:00
|
|
|
*out_pos = out_p; \
|
2008-08-10 06:33:05 +04:00
|
|
|
tc->readlen += in_p - inchar_start; \
|
2008-08-10 06:17:56 +04:00
|
|
|
if (feedlen) { \
|
2008-08-10 06:33:05 +04:00
|
|
|
tc->readlen -= feedlen; \
|
|
|
|
tc->feedlen = feedlen; \
|
2008-08-10 06:17:56 +04:00
|
|
|
} \
|
2008-08-10 06:33:05 +04:00
|
|
|
tc->next_table = next_table; \
|
|
|
|
tc->next_info = next_info; \
|
|
|
|
tc->next_byte = next_byte; \
|
2008-08-09 10:02:01 +04:00
|
|
|
return ret; \
|
|
|
|
resume_label ## num:; \
|
|
|
|
} while (0)
|
|
|
|
|
2008-08-10 06:33:05 +04:00
|
|
|
switch (tc->resume_position) {
|
2008-08-09 10:02:01 +04:00
|
|
|
case 0: break;
|
|
|
|
case 1: goto resume_label1;
|
|
|
|
case 2: goto resume_label2;
|
|
|
|
case 3: goto resume_label3;
|
|
|
|
case 4: goto resume_label4;
|
|
|
|
case 5: goto resume_label5;
|
|
|
|
case 6: goto resume_label6;
|
|
|
|
case 7: goto resume_label7;
|
|
|
|
case 8: goto resume_label8;
|
|
|
|
case 9: goto resume_label9;
|
|
|
|
case 10: goto resume_label10;
|
|
|
|
case 11: goto resume_label11;
|
|
|
|
case 12: goto resume_label12;
|
|
|
|
case 13: goto resume_label13;
|
|
|
|
case 14: goto resume_label14;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2008-08-10 03:33:37 +04:00
|
|
|
if (in_stop <= in_p) {
|
2008-08-09 10:02:01 +04:00
|
|
|
if (!(opt & PARTIAL_INPUT))
|
|
|
|
break;
|
|
|
|
SUSPEND(transcode_ibuf_empty, 7);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-08-10 06:33:05 +04:00
|
|
|
tc->readlen = 0;
|
2008-08-10 04:23:36 +04:00
|
|
|
inchar_start = in_p;
|
2008-08-10 06:33:05 +04:00
|
|
|
next_table = tr->conv_tree_start;
|
2008-08-10 03:33:37 +04:00
|
|
|
next_byte = (unsigned char)*in_p++;
|
2007-12-10 08:01:47 +03:00
|
|
|
follow_byte:
|
2008-08-08 19: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 12:26:55 +03:00
|
|
|
follow_info:
|
2007-12-11 07:57:51 +03:00
|
|
|
switch (next_info & 0x1F) {
|
2008-08-09 10:02:01 +04:00
|
|
|
case NOMAP: /* xxx: copy last byte only? */
|
|
|
|
while (out_stop - out_p < 1) { SUSPEND(transcode_obuf_full, 3); }
|
2007-12-10 11:46:06 +03:00
|
|
|
*out_p++ = next_byte;
|
|
|
|
continue;
|
|
|
|
case 0x00: case 0x04: case 0x08: case 0x0C:
|
|
|
|
case 0x10: case 0x14: case 0x18: case 0x1C:
|
2008-08-10 03:33:37 +04:00
|
|
|
while (in_p >= in_stop) {
|
2008-08-09 10:02:01 +04:00
|
|
|
if (!(opt & PARTIAL_INPUT))
|
|
|
|
goto invalid;
|
|
|
|
SUSPEND(transcode_ibuf_empty, 5);
|
2007-12-10 11:46:06 +03:00
|
|
|
}
|
2008-08-10 03:33:37 +04:00
|
|
|
next_byte = (unsigned char)*in_p++;
|
2007-12-28 12:26:55 +03:00
|
|
|
next_table = (const BYTE_LOOKUP *)next_info;
|
2007-12-10 11:46:06 +03:00
|
|
|
goto follow_byte;
|
|
|
|
case ZERObt: /* drop input */
|
|
|
|
continue;
|
|
|
|
case ONEbt:
|
2008-08-09 10:02:01 +04:00
|
|
|
while (out_stop - out_p < 1) { SUSPEND(transcode_obuf_full, 9); }
|
2007-12-10 11:46:06 +03:00
|
|
|
*out_p++ = getBT1(next_info);
|
|
|
|
continue;
|
|
|
|
case TWObt:
|
2008-08-09 10:02:01 +04:00
|
|
|
while (out_stop - out_p < 2) { SUSPEND(transcode_obuf_full, 10); }
|
2007-12-10 11:46:06 +03:00
|
|
|
*out_p++ = getBT1(next_info);
|
|
|
|
*out_p++ = getBT2(next_info);
|
|
|
|
continue;
|
2008-08-09 10:02:01 +04:00
|
|
|
case THREEbt:
|
|
|
|
while (out_stop - out_p < 3) { SUSPEND(transcode_obuf_full, 11); }
|
|
|
|
*out_p++ = getBT1(next_info);
|
|
|
|
*out_p++ = getBT2(next_info);
|
|
|
|
*out_p++ = getBT3(next_info);
|
|
|
|
continue;
|
2007-12-10 11:46:06 +03:00
|
|
|
case FOURbt:
|
2008-08-09 10:02:01 +04:00
|
|
|
while (out_stop - out_p < 4) { SUSPEND(transcode_obuf_full, 12); }
|
2007-12-10 11:46:06 +03:00
|
|
|
*out_p++ = getBT0(next_info);
|
|
|
|
*out_p++ = getBT1(next_info);
|
|
|
|
*out_p++ = getBT2(next_info);
|
|
|
|
*out_p++ = getBT3(next_info);
|
|
|
|
continue;
|
2007-12-28 12:26:55 +03:00
|
|
|
case FUNii:
|
2008-08-10 06:33:05 +04:00
|
|
|
next_info = (VALUE)(*tr->func_ii)(tc, next_info);
|
2007-12-28 12:26:55 +03:00
|
|
|
goto follow_info;
|
2008-01-20 09:12:48 +03:00
|
|
|
case FUNsi:
|
2008-08-09 10:02:01 +04:00
|
|
|
{
|
2008-08-10 03:33:37 +04:00
|
|
|
const unsigned char *char_start;
|
2008-08-10 04:23:36 +04:00
|
|
|
size_t char_len;
|
2008-08-10 06:33:05 +04:00
|
|
|
char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
|
|
|
|
next_info = (VALUE)(*tr->func_si)(tc, char_start, (size_t)char_len);
|
2008-08-09 10:02:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-01-20 09:12:48 +03:00
|
|
|
case FUNio:
|
2008-08-10 06:33:05 +04:00
|
|
|
while (out_stop - out_p < tr->max_output) { SUSPEND(transcode_obuf_full, 13); }
|
|
|
|
out_p += (VALUE)(*tr->func_io)(tc, next_info, out_p);
|
2008-01-20 09:12:48 +03:00
|
|
|
break;
|
|
|
|
case FUNso:
|
2008-08-09 10:02:01 +04:00
|
|
|
{
|
2008-08-10 03:33:37 +04:00
|
|
|
const unsigned char *char_start;
|
2008-08-10 04:23:36 +04:00
|
|
|
size_t char_len;
|
2008-08-10 06:33:05 +04:00
|
|
|
while (out_stop - out_p < tr->max_output) { SUSPEND(transcode_obuf_full, 14); }
|
|
|
|
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);
|
2008-08-09 10:02:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2007-12-27 11:27:19 +03:00
|
|
|
case INVALID:
|
2008-08-10 03:33:37 +04:00
|
|
|
{
|
2008-08-10 06:33:05 +04:00
|
|
|
if (tc->readlen + (in_p - inchar_start) <= unitlen) {
|
|
|
|
while ((opt & PARTIAL_INPUT) && tc->readlen + (in_stop - inchar_start) < unitlen) {
|
2008-08-10 03:33:37 +04:00
|
|
|
in_p = in_stop;
|
|
|
|
SUSPEND(transcode_ibuf_empty, 8);
|
|
|
|
}
|
2008-08-10 06:33:05 +04:00
|
|
|
if (tc->readlen + (in_stop - inchar_start) <= unitlen) {
|
2008-08-10 03:33:37 +04:00
|
|
|
in_p = in_stop;
|
|
|
|
}
|
|
|
|
else {
|
2008-08-10 06:33:05 +04:00
|
|
|
in_p = inchar_start + (unitlen - tc->readlen);
|
2008-08-10 03:33:37 +04:00
|
|
|
}
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
2008-08-10 03:33:37 +04:00
|
|
|
else {
|
2008-08-10 06:17:56 +04:00
|
|
|
int found_len; /* including the last byte which cuases invalid */
|
|
|
|
int invalid_len;
|
2008-08-10 04:23:36 +04:00
|
|
|
int step;
|
2008-08-10 06:33:05 +04:00
|
|
|
found_len = tc->readlen + (in_p - inchar_start);
|
2008-08-10 06:17:56 +04:00
|
|
|
invalid_len = ((found_len - 1) / unitlen) * unitlen;
|
|
|
|
step = invalid_len - found_len;
|
|
|
|
if (step < -1) {
|
|
|
|
if (-step <= in_p - *in_pos) {
|
|
|
|
in_p += step;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
feedlen = -step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
in_p += step;
|
|
|
|
}
|
2008-08-10 03:33:37 +04:00
|
|
|
}
|
|
|
|
goto invalid;
|
2008-08-08 19:48:17 +04:00
|
|
|
}
|
2007-12-10 11:46:06 +03:00
|
|
|
case UNDEF:
|
2008-07-14 10:27:26 +04:00
|
|
|
goto undef;
|
2007-12-11 07:57:51 +03:00
|
|
|
}
|
|
|
|
continue;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
2007-12-27 11:27:19 +03:00
|
|
|
invalid:
|
2008-08-09 10:02:01 +04:00
|
|
|
SUSPEND(transcode_invalid_input, 1);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
undef:
|
|
|
|
SUSPEND(transcode_undefined_conversion, 2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2008-08-10 06:33:05 +04:00
|
|
|
if (tr->finish_func) {
|
|
|
|
while (out_stop - out_p < tr->max_output) {
|
2008-08-09 10:02:01 +04:00
|
|
|
SUSPEND(transcode_obuf_full, 4);
|
|
|
|
}
|
2008-08-10 06:33:05 +04:00
|
|
|
out_p += tr->finish_func(tc, out_p);
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
while (1)
|
|
|
|
SUSPEND(transcode_finished, 6);
|
|
|
|
#undef SUSPEND
|
|
|
|
}
|
|
|
|
|
2008-08-10 06:17:56 +04:00
|
|
|
static transcode_result_t
|
|
|
|
transcode_restartable(const unsigned char **in_pos, unsigned char **out_pos,
|
|
|
|
const unsigned char *in_stop, unsigned char *out_stop,
|
2008-08-10 06:33:05 +04:00
|
|
|
rb_transcoding *tc,
|
2008-08-10 06:17:56 +04:00
|
|
|
const int opt)
|
|
|
|
{
|
2008-08-10 06:33:05 +04:00
|
|
|
if (tc->feedlen) {
|
|
|
|
unsigned char *feed_buf = ALLOCA_N(unsigned char, tc->feedlen);
|
2008-08-10 06:17:56 +04:00
|
|
|
const unsigned char *feed_pos = feed_buf;
|
2008-08-10 06:33:05 +04:00
|
|
|
const unsigned char *feed_stop = feed_buf + tc->feedlen;
|
2008-08-10 06:17:56 +04:00
|
|
|
transcode_result_t res;
|
|
|
|
|
2008-08-10 06:33:05 +04:00
|
|
|
MEMCPY(feed_buf, TRANSCODING_READBUF(tc) + tc->readlen,
|
|
|
|
unsigned char, tc->feedlen);
|
|
|
|
tc->feedlen = 0;
|
|
|
|
res = transcode_restartable0(&feed_pos, out_pos, feed_stop, out_stop, tc, opt);
|
2008-08-10 06:17:56 +04:00
|
|
|
if (res != transcode_ibuf_empty) {
|
2008-08-10 06:33:05 +04:00
|
|
|
MEMCPY(TRANSCODING_READBUF(tc) + tc->readlen + tc->feedlen,
|
2008-08-10 06:17:56 +04:00
|
|
|
feed_pos, unsigned char, feed_stop - feed_pos);
|
2008-08-10 06:33:05 +04:00
|
|
|
tc->feedlen += feed_stop - feed_pos;
|
2008-08-10 06:17:56 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2008-08-10 06:33:05 +04:00
|
|
|
return transcode_restartable0(in_pos, out_pos, in_stop, out_stop, tc, opt);
|
2008-08-10 06:17:56 +04:00
|
|
|
}
|
|
|
|
|
2008-08-09 10:02:01 +04:00
|
|
|
static void
|
|
|
|
more_output_buffer(
|
2008-08-09 17:34:21 +04:00
|
|
|
VALUE destination,
|
|
|
|
unsigned char *(*resize_destination)(VALUE, int, int),
|
2008-08-10 06:33:05 +04:00
|
|
|
rb_transcoding *tc,
|
2008-08-09 10:02:01 +04:00
|
|
|
unsigned char **out_start_ptr,
|
|
|
|
unsigned char **out_pos,
|
|
|
|
unsigned char **out_stop_ptr)
|
|
|
|
{
|
|
|
|
size_t len = (*out_pos - *out_start_ptr);
|
2008-08-10 06:33:05 +04:00
|
|
|
size_t new_len = (len + tc->transcoder->max_output) * 2;
|
2008-08-09 17:34:21 +04:00
|
|
|
*out_start_ptr = resize_destination(destination, len, new_len);
|
2008-08-09 10:02:01 +04:00
|
|
|
*out_pos = *out_start_ptr + len;
|
|
|
|
*out_stop_ptr = *out_start_ptr + new_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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 17:34:21 +04:00
|
|
|
VALUE destination,
|
|
|
|
unsigned char *(*resize_destination)(VALUE, int, int),
|
2008-08-10 06:33:05 +04:00
|
|
|
rb_transcoding *tc,
|
2008-08-09 10:02:01 +04:00
|
|
|
const int opt)
|
|
|
|
{
|
2008-08-10 06:33:05 +04:00
|
|
|
const rb_transcoder *tr = tc->transcoder;
|
2008-08-09 10:02:01 +04:00
|
|
|
transcode_result_t ret;
|
|
|
|
unsigned char *out_start = *out_pos;
|
|
|
|
|
2008-08-10 06:33:05 +04:00
|
|
|
tc->resume_position = 0;
|
|
|
|
tc->readlen = 0;
|
|
|
|
tc->feedlen = 0;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
2008-08-10 06:33:05 +04:00
|
|
|
if (sizeof(tc->readbuf.ary) < tr->max_input) {
|
|
|
|
tc->readbuf.ptr = xmalloc(tr->max_input);
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
#define CLEANUP \
|
|
|
|
do { \
|
2008-08-10 06:33:05 +04:00
|
|
|
if (sizeof(tc->readbuf.ary) < tr->max_input) \
|
|
|
|
xfree(tc->readbuf.ptr); \
|
2008-08-09 10:02:01 +04:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
resume:
|
2008-08-10 06:33:05 +04:00
|
|
|
ret = transcode_restartable(in_pos, out_pos, in_stop, out_stop, tc, opt);
|
2008-08-09 10:02:01 +04:00
|
|
|
if (ret == transcode_invalid_input) {
|
2007-12-27 11:27:19 +03:00
|
|
|
/* deal with invalid byte sequence */
|
2008-02-21 11:42:10 +03:00
|
|
|
/* todo: add more alternative behaviors */
|
|
|
|
if (opt&INVALID_IGNORE) {
|
2008-08-09 10:02:01 +04:00
|
|
|
goto resume;
|
2008-02-21 11:42:10 +03:00
|
|
|
}
|
2008-07-14 10:27:26 +04:00
|
|
|
else if (opt&INVALID_REPLACE) {
|
2008-08-10 06:33:05 +04:00
|
|
|
if (out_stop - *out_pos < tr->max_output)
|
|
|
|
more_output_buffer(destination, resize_destination, tc, &out_start, out_pos, &out_stop);
|
|
|
|
output_replacement_character(out_pos, rb_enc_find(tr->to_encoding));
|
2008-08-09 10:02:01 +04:00
|
|
|
goto resume;
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
2008-08-09 10:02:01 +04:00
|
|
|
CLEANUP;
|
2008-08-05 07:34:52 +04:00
|
|
|
rb_raise(TRANSCODE_ERROR, "invalid byte sequence");
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
if (ret == transcode_undefined_conversion) {
|
2008-07-14 10: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 10:02:01 +04:00
|
|
|
goto resume;
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
|
|
|
else if (opt&UNDEF_REPLACE) {
|
2008-08-10 06:33:05 +04:00
|
|
|
if (out_stop - *out_pos < tr->max_output)
|
|
|
|
more_output_buffer(destination, resize_destination, tc, &out_start, out_pos, &out_stop);
|
|
|
|
output_replacement_character(out_pos, rb_enc_find(tr->to_encoding));
|
2008-08-09 10:02:01 +04:00
|
|
|
goto resume;
|
2008-07-14 10:27:26 +04:00
|
|
|
}
|
2008-08-09 10:02:01 +04:00
|
|
|
CLEANUP;
|
|
|
|
rb_raise(TRANSCODE_ERROR, "conversion undefined for byte sequence (maybe invalid byte sequence)");
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
2008-08-09 10:02:01 +04:00
|
|
|
if (ret == transcode_obuf_full) {
|
2008-08-10 06:33:05 +04:00
|
|
|
more_output_buffer(destination, resize_destination, tc, &out_start, out_pos, &out_stop);
|
2008-08-09 10:02:01 +04:00
|
|
|
goto resume;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLEANUP;
|
|
|
|
return;
|
|
|
|
#undef CLEANUP
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* sample transcode_loop implementation in byte-by-byte stream style */
|
|
|
|
static void
|
|
|
|
transcode_loop(const unsigned char **in_pos, unsigned char **out_pos,
|
|
|
|
const unsigned char *in_stop, unsigned char *out_stop,
|
2008-08-09 17:34:21 +04:00
|
|
|
VALUE destination,
|
2008-08-10 06:17:56 +04:00
|
|
|
unsigned char *(*resize_destination)(VALUE, int, int),
|
2008-08-10 06:33:05 +04:00
|
|
|
rb_transcoding *tc,
|
2008-08-09 10:02:01 +04:00
|
|
|
const int opt)
|
|
|
|
{
|
2008-08-10 06:33:05 +04:00
|
|
|
const rb_transcoder *tr = tc->transcoder;
|
2008-08-09 10:02:01 +04:00
|
|
|
transcode_result_t ret;
|
|
|
|
unsigned char *out_start = *out_pos;
|
|
|
|
const unsigned char *ptr;
|
|
|
|
|
2008-08-10 06:33:05 +04:00
|
|
|
tc->resume_position = 0;
|
|
|
|
tc->readlen = 0;
|
|
|
|
tc->feedlen = 0;
|
2008-08-09 10:02:01 +04:00
|
|
|
|
2008-08-10 06:33:05 +04:00
|
|
|
if (sizeof(tc->readbuf.ary) < tr->max_input) {
|
|
|
|
tc->readbuf.ptr = xmalloc(tr->max_input);
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
#define CLEANUP \
|
|
|
|
do { \
|
2008-08-10 06:33:05 +04:00
|
|
|
if (sizeof(tc->readbuf.ary) < tr->max_input) \
|
|
|
|
xfree(tc->readbuf.ptr); \
|
2008-08-09 10:02:01 +04:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
ret = transcode_ibuf_empty;
|
|
|
|
ptr = *in_pos;
|
|
|
|
while (ret != transcode_finished) {
|
|
|
|
unsigned char input_byte;
|
|
|
|
const unsigned char *p = &input_byte;
|
|
|
|
|
|
|
|
if (ret == transcode_ibuf_empty) {
|
|
|
|
if (ptr < in_stop) {
|
|
|
|
input_byte = *ptr;
|
2008-08-10 06:33:05 +04:00
|
|
|
ret = transcode_restartable(&p, out_pos, p+1, out_stop, tc, opt|PARTIAL_INPUT);
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-08-10 06:33:05 +04:00
|
|
|
ret = transcode_restartable(NULL, out_pos, NULL, out_stop, tc, opt);
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-08-10 06:33:05 +04:00
|
|
|
ret = transcode_restartable(NULL, out_pos, NULL, out_stop, tc, opt|PARTIAL_INPUT);
|
2008-08-09 10:02:01 +04:00
|
|
|
}
|
|
|
|
if (&input_byte != p)
|
|
|
|
ptr += p - &input_byte;
|
|
|
|
switch (ret) {
|
|
|
|
case transcode_invalid_input:
|
|
|
|
/* deal with invalid byte sequence */
|
|
|
|
/* todo: add more alternative behaviors */
|
|
|
|
if (opt&INVALID_IGNORE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (opt&INVALID_REPLACE) {
|
2008-08-10 06:33:05 +04:00
|
|
|
if (out_stop - *out_pos < tr->max_output)
|
|
|
|
more_output_buffer(destination, resize_destination, tc, &out_start, out_pos, &out_stop);
|
|
|
|
output_replacement_character(out_pos, rb_enc_find(tr->to_encoding));
|
2008-08-09 10:02:01 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
CLEANUP;
|
|
|
|
rb_raise(TRANSCODE_ERROR, "invalid byte sequence");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case transcode_undefined_conversion:
|
|
|
|
/* 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-10 06:33:05 +04:00
|
|
|
if (out_stop - *out_pos < tr->max_output)
|
|
|
|
more_output_buffer(destination, resize_destination, tc, &out_start, out_pos, &out_stop);
|
|
|
|
output_replacement_character(out_pos, rb_enc_find(tr->to_encoding));
|
2008-08-09 10:02:01 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
CLEANUP;
|
|
|
|
rb_raise(TRANSCODE_ERROR, "conversion undefined for byte sequence (maybe invalid byte sequence)");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case transcode_obuf_full:
|
2008-08-10 06:33:05 +04:00
|
|
|
more_output_buffer(destination, resize_destination, tc, &out_start, out_pos, &out_stop);
|
2008-08-09 10:02:01 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case transcode_ibuf_empty:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case transcode_finished:
|
|
|
|
break;
|
|
|
|
}
|
2008-08-07 18:53:30 +04:00
|
|
|
}
|
2008-08-09 10:02:01 +04:00
|
|
|
CLEANUP;
|
|
|
|
*in_pos = in_stop;
|
|
|
|
return;
|
|
|
|
#undef CLEANUP
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
2008-08-09 10:02:01 +04:00
|
|
|
#endif
|
2007-12-10 08:01:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* String-specific code
|
|
|
|
*/
|
|
|
|
|
2008-01-21 06:35:05 +03:00
|
|
|
static unsigned char *
|
2008-08-09 17:34:21 +04:00
|
|
|
str_transcoding_resize(VALUE destination, int len, int new_len)
|
2007-12-10 11:46:06 +03:00
|
|
|
{
|
2008-08-09 17:34:21 +04:00
|
|
|
rb_str_resize(destination, new_len);
|
|
|
|
return (unsigned char *)RSTRING_PTR(destination);
|
2007-12-10 11:46:06 +03:00
|
|
|
}
|
|
|
|
|
2007-12-18 11:04:26 +03:00
|
|
|
static int
|
|
|
|
str_transcode(int argc, VALUE *argv, VALUE *self)
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
|
|
|
VALUE dest;
|
2007-12-18 11:04:26 +03:00
|
|
|
VALUE str = *self;
|
2007-12-10 11:46:06 +03:00
|
|
|
long blen, slen;
|
2008-07-14 13:47:33 +04:00
|
|
|
unsigned char *buf, *bp, *sp;
|
|
|
|
const unsigned char *fromp;
|
2007-12-10 08:01:47 +03:00
|
|
|
rb_encoding *from_enc, *to_enc;
|
2007-12-10 11:46:06 +03:00
|
|
|
const char *from_e, *to_e;
|
2007-12-10 15:47:55 +03:00
|
|
|
int from_encidx, to_encidx;
|
|
|
|
VALUE from_encval, to_encval;
|
2008-08-10 06:33:05 +04:00
|
|
|
const rb_transcoder *tr;
|
|
|
|
rb_transcoding tc;
|
2007-12-15 08:42:25 +03:00
|
|
|
int final_encoding = 0;
|
2008-02-21 11:42:10 +03: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 08:01:47 +03:00
|
|
|
|
2008-02-21 11:42:10 +03:00
|
|
|
argc--;
|
|
|
|
v = rb_hash_aref(opt, sym_invalid);
|
|
|
|
if (NIL_P(v)) {
|
|
|
|
}
|
|
|
|
else if (v==sym_ignore) {
|
|
|
|
options |= INVALID_IGNORE;
|
|
|
|
}
|
2008-07-14 10:27:26 +04:00
|
|
|
else if (v==sym_replace) {
|
|
|
|
options |= INVALID_REPLACE;
|
|
|
|
v = rb_hash_aref(opt, sym_replace);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "unknown value for invalid: setting");
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
rb_raise(rb_eArgError, "unknown value for undef: setting");
|
|
|
|
}
|
2008-02-21 11:42:10 +03:00
|
|
|
}
|
2007-12-27 19:55:06 +03:00
|
|
|
if (argc < 1 || argc > 2) {
|
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
2007-12-10 15:47:55 +03:00
|
|
|
if ((to_encidx = rb_to_encoding_index(to_encval = argv[0])) < 0) {
|
|
|
|
to_enc = 0;
|
2007-12-25 08:57:04 +03:00
|
|
|
to_encidx = 0;
|
2007-12-10 15:47:55 +03: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 08:01:47 +03:00
|
|
|
if (argc==1) {
|
2007-12-15 08:42:25 +03:00
|
|
|
from_encidx = rb_enc_get_index(str);
|
2007-12-10 15:47:55 +03: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 08:01:47 +03:00
|
|
|
}
|
|
|
|
else {
|
2007-12-10 15:47:55 +03:00
|
|
|
from_enc = rb_enc_from_index(from_encidx);
|
|
|
|
from_e = rb_enc_name(from_enc);
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
2007-12-10 15:47:55 +03:00
|
|
|
if (from_enc && from_enc == to_enc) {
|
2007-12-18 11:04:26 +03:00
|
|
|
return -1;
|
2007-12-10 11:46:06 +03:00
|
|
|
}
|
2007-12-10 15:47:55 +03:00
|
|
|
if (from_enc && to_enc && rb_enc_asciicompat(from_enc) && rb_enc_asciicompat(to_enc)) {
|
2007-12-28 12:26:55 +03:00
|
|
|
if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT) {
|
2007-12-18 11:04:26 +03:00
|
|
|
return to_encidx;
|
|
|
|
}
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
2008-01-21 00:40:08 +03:00
|
|
|
if (encoding_equal(from_e, to_e)) {
|
2007-12-18 11:04:26 +03:00
|
|
|
return -1;
|
2007-12-10 15:47:55 +03:00
|
|
|
}
|
2007-12-10 08:01:47 +03:00
|
|
|
|
2008-06-07 16:38:03 +04:00
|
|
|
do { /* loop for multistep transcoding */
|
2007-12-27 19:55:06 +03:00
|
|
|
/* later, maybe use smaller intermediate strings for very long strings */
|
2008-08-10 06:33:05 +04:00
|
|
|
if (!(tr = transcode_dispatch(from_e, to_e))) {
|
2007-12-15 08:42:25 +03:00
|
|
|
rb_raise(rb_eArgError, "transcoding not supported (from %s to %s)", from_e, to_e);
|
|
|
|
}
|
2007-12-10 08:01:47 +03:00
|
|
|
|
2008-08-10 06:33:05 +04:00
|
|
|
tc.transcoder = tr;
|
|
|
|
memset(tc.stateful, 0, sizeof(tc.stateful));
|
2008-01-21 08:36:16 +03:00
|
|
|
|
2008-01-21 06:35:05 +03:00
|
|
|
fromp = sp = (unsigned char *)RSTRING_PTR(str);
|
2007-12-15 08:42:25 +03:00
|
|
|
slen = RSTRING_LEN(str);
|
|
|
|
blen = slen + 30; /* len + margin */
|
|
|
|
dest = rb_str_tmp_new(blen);
|
2008-01-21 06:35:05 +03:00
|
|
|
bp = (unsigned char *)RSTRING_PTR(dest);
|
2007-12-15 08:42:25 +03:00
|
|
|
|
2008-08-10 06:33:05 +04:00
|
|
|
transcode_loop(&fromp, &bp, (sp+slen), (bp+blen), dest, str_transcoding_resize, &tc, options);
|
2007-12-15 08:42:25 +03:00
|
|
|
if (fromp != sp+slen) {
|
2008-07-12 17:17:29 +04:00
|
|
|
rb_raise(rb_eArgError, "not fully converted, %"PRIdPTRDIFF" bytes left", sp+slen-fromp);
|
2007-12-15 08:42:25 +03:00
|
|
|
}
|
2008-01-21 06:35:05 +03:00
|
|
|
buf = (unsigned char *)RSTRING_PTR(dest);
|
2007-12-15 08:42:25 +03:00
|
|
|
*bp = '\0';
|
|
|
|
rb_str_set_len(dest, bp - buf);
|
|
|
|
|
2008-08-10 06:33:05 +04:00
|
|
|
if (encoding_equal(tr->to_encoding, to_e)) {
|
2007-12-15 08:42:25 +03:00
|
|
|
final_encoding = 1;
|
|
|
|
}
|
|
|
|
else {
|
2008-08-10 06:33:05 +04:00
|
|
|
from_e = tr->to_encoding;
|
2007-12-15 08:42:25 +03:00
|
|
|
str = dest;
|
|
|
|
}
|
2008-06-07 16:38:03 +04:00
|
|
|
} while (!final_encoding);
|
2007-12-10 11:46:06 +03:00
|
|
|
/* set encoding */
|
2007-12-10 15:47:55 +03:00
|
|
|
if (!to_enc) {
|
2007-12-25 08:57:04 +03:00
|
|
|
to_encidx = rb_define_dummy_encoding(to_e);
|
2007-12-10 15:47:55 +03:00
|
|
|
}
|
2007-12-18 11:04:26 +03:00
|
|
|
*self = dest;
|
2007-12-10 08:01:47 +03:00
|
|
|
|
2007-12-18 11:04:26 +03:00
|
|
|
return to_encidx;
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
2008-08-06 00:26:45 +04:00
|
|
|
static inline VALUE
|
|
|
|
str_encode_associate(VALUE str, int encidx)
|
|
|
|
{
|
|
|
|
int cr = 0;
|
|
|
|
|
|
|
|
rb_enc_associate_index(str, encidx);
|
|
|
|
|
|
|
|
/* transcoded string never be broken. */
|
|
|
|
if (rb_enc_asciicompat(rb_enc_from_index(encidx))) {
|
|
|
|
rb_str_coderange_scan_restartable(RSTRING_PTR(str), RSTRING_END(str), 0, &cr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cr = ENC_CODERANGE_VALID;
|
|
|
|
}
|
|
|
|
ENC_CODERANGE_SET(str, cr);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-12-10 08:01:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-03-05 11:45:51 +03:00
|
|
|
* str.encode!(encoding [, options] ) => str
|
|
|
|
* str.encode!(to_encoding, from_encoding [, options] ) => str
|
2007-12-10 08:01:47 +03:00
|
|
|
*
|
2008-03-05 11:45:51 +03:00
|
|
|
* The first form transcodes the contents of <i>str</i> from
|
2007-12-10 08:01:47 +03:00
|
|
|
* str.encoding to +encoding+.
|
2008-03-05 11:45:51 +03:00
|
|
|
* The second form transcodes the contents of <i>str</i> from
|
2007-12-10 08:01:47 +03:00
|
|
|
* from_encoding to to_encoding.
|
2008-03-05 11:45:51 +03:00
|
|
|
* The options Hash gives details for conversion. See String#encode
|
|
|
|
* for details.
|
2007-12-10 08:01:47 +03:00
|
|
|
* Returns the string even if no changes were made.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2008-05-20 20:37:13 +04:00
|
|
|
str_encode_bang(int argc, VALUE *argv, VALUE str)
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
2007-12-18 11:04:26 +03:00
|
|
|
VALUE newstr = str;
|
|
|
|
int encidx = str_transcode(argc, argv, &newstr);
|
|
|
|
|
2007-12-21 11:49:08 +03:00
|
|
|
if (encidx < 0) return str;
|
2007-12-10 11:46:06 +03:00
|
|
|
rb_str_shared_replace(str, newstr);
|
2008-08-06 00:26:45 +04:00
|
|
|
return str_encode_associate(str, encidx);
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-03-05 11:45:51 +03:00
|
|
|
* str.encode(encoding [, options] ) => str
|
|
|
|
* str.encode(to_encoding, from_encoding [, options] ) => str
|
2007-12-10 08:01:47 +03:00
|
|
|
*
|
2008-03-05 11:45:51 +03:00
|
|
|
* The first form returns a copy of <i>str</i> transcoded
|
2007-12-10 08:01:47 +03:00
|
|
|
* to encoding +encoding+.
|
2008-03-05 11:45:51 +03:00
|
|
|
* The second form returns a copy of <i>str</i> transcoded
|
2007-12-10 08:01:47 +03:00
|
|
|
* from from_encoding to to_encoding.
|
2008-03-05 11:45:51 +03:00
|
|
|
* The options Hash gives details for conversion. Details
|
|
|
|
* to be added.
|
2007-12-10 08:01:47 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2008-05-20 20:37:13 +04:00
|
|
|
str_encode(int argc, VALUE *argv, VALUE str)
|
2007-12-10 08:01:47 +03:00
|
|
|
{
|
2008-08-06 00:26:45 +04:00
|
|
|
VALUE newstr = str;
|
|
|
|
int encidx = str_transcode(argc, argv, &newstr);
|
|
|
|
|
|
|
|
if (encidx < 0) return rb_str_dup(str);
|
|
|
|
RBASIC(newstr)->klass = rb_obj_class(str);
|
|
|
|
return str_encode_associate(newstr, encidx);
|
2008-05-20 20:37:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_transcode(VALUE str, VALUE to)
|
|
|
|
{
|
2008-05-24 03:15:14 +04:00
|
|
|
return str_encode(1, &to, str);
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_transcode(void)
|
|
|
|
{
|
2007-12-25 08:57:04 +03:00
|
|
|
transcoder_table = st_init_strcasetable();
|
|
|
|
|
2008-02-21 11:42:10 +03:00
|
|
|
sym_invalid = ID2SYM(rb_intern("invalid"));
|
2008-07-14 10:27:26 +04:00
|
|
|
sym_undef = ID2SYM(rb_intern("undef"));
|
2008-02-21 11:42:10 +03:00
|
|
|
sym_ignore = ID2SYM(rb_intern("ignore"));
|
2008-07-14 10:27:26 +04:00
|
|
|
sym_replace = ID2SYM(rb_intern("replace"));
|
2008-02-21 11:42:10 +03:00
|
|
|
|
2008-05-20 20:37:13 +04:00
|
|
|
rb_define_method(rb_cString, "encode", str_encode, -1);
|
|
|
|
rb_define_method(rb_cString, "encode!", str_encode_bang, -1);
|
2007-12-10 08:01:47 +03:00
|
|
|
}
|