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-07-14 02:27:26 -04:00
|
|
|
static VALUE sym_invalid, sym_undef, sym_ignore, sym_replace;
|
2008-02-21 03:42:10 -05:00
|
|
|
#define INVALID_IGNORE 0x1
|
2008-07-14 02:27:26 -04:00
|
|
|
#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
|
|
|
|
*/
|
|
|
|
|
2007-12-25 00:57:04 -05:00
|
|
|
static st_table *transcoder_table, *transcoder_lib_table;
|
|
|
|
|
|
|
|
#define TRANSCODER_INTERNAL_SEPARATOR '\t'
|
|
|
|
|
|
|
|
static char *
|
|
|
|
transcoder_key(const char *from_e, const char *to_e)
|
2007-12-10 00:01:47 -05:00
|
|
|
{
|
2007-12-25 00:57:04 -05:00
|
|
|
int to_len = strlen(to_e);
|
|
|
|
int from_len = strlen(from_e);
|
|
|
|
char *const key = xmalloc(to_len + from_len + 2);
|
|
|
|
|
|
|
|
memcpy(key, to_e, to_len);
|
|
|
|
memcpy(key + to_len + 1, from_e, from_len + 1);
|
|
|
|
key[to_len] = TRANSCODER_INTERNAL_SEPARATOR;
|
|
|
|
return key;
|
|
|
|
}
|
2007-12-10 00:01:47 -05:00
|
|
|
|
2007-12-25 00:57:04 -05:00
|
|
|
void
|
|
|
|
rb_register_transcoder(const rb_transcoder *tr)
|
|
|
|
{
|
|
|
|
st_data_t k, val = 0;
|
|
|
|
const char *const from_e = tr->from_encoding;
|
|
|
|
const char *const to_e = tr->to_encoding;
|
|
|
|
char *const key = transcoder_key(from_e, to_e);
|
|
|
|
|
|
|
|
if (st_lookup(transcoder_table, (st_data_t)key, &val)) {
|
|
|
|
xfree(key);
|
|
|
|
rb_raise(rb_eArgError, "transcoder from %s to %s has been already registered",
|
|
|
|
from_e, to_e);
|
|
|
|
}
|
|
|
|
k = (st_data_t)key;
|
|
|
|
if (st_delete(transcoder_lib_table, &k, &val)) {
|
|
|
|
xfree((char *)k);
|
|
|
|
}
|
|
|
|
st_insert(transcoder_table, (st_data_t)key, (st_data_t)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
|
|
|
{
|
2007-12-25 00:57:04 -05:00
|
|
|
const char *const key = transcoder_key(to, from);
|
|
|
|
st_data_t k = (st_data_t)key, val;
|
|
|
|
|
|
|
|
if (st_delete(transcoder_lib_table, &k, &val)) {
|
|
|
|
xfree((char *)k);
|
2007-12-24 08:51:19 -05:00
|
|
|
}
|
2007-12-25 00:57:04 -05:00
|
|
|
st_insert(transcoder_lib_table, (st_data_t)key, (st_data_t)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-01-20 16:40:08 -05:00
|
|
|
static const rb_transcoder *
|
2008-07-31 16:35:35 -04:00
|
|
|
transcode_dispatch(const char *from_encoding, const char *to_encoding)
|
2007-12-10 00:01:47 -05:00
|
|
|
{
|
2007-12-25 00:57:04 -05:00
|
|
|
char *const key = transcoder_key(from_encoding, to_encoding);
|
|
|
|
st_data_t k, val = 0;
|
|
|
|
|
2007-12-27 11:55:06 -05:00
|
|
|
while (!st_lookup(transcoder_table, (k = (st_data_t)key), &val) &&
|
2007-12-25 01:21:35 -05:00
|
|
|
st_delete(transcoder_lib_table, &k, &val)) {
|
2007-12-25 00:57:04 -05:00
|
|
|
const char *const lib = (const char *)val;
|
|
|
|
int len = strlen(lib);
|
|
|
|
char path[sizeof(transcoder_lib_prefix) + MAX_TRANSCODER_LIBNAME_LEN];
|
|
|
|
|
|
|
|
xfree((char *)k);
|
|
|
|
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;
|
2007-12-25 01:21:35 -05:00
|
|
|
}
|
|
|
|
if (!val) {
|
2007-12-25 00:57:04 -05:00
|
|
|
if (!st_lookup(transcoder_table, (st_data_t)key, &val)) {
|
2008-08-04 01:08:57 -04:00
|
|
|
xfree(key);
|
2007-12-25 00:57:04 -05:00
|
|
|
/* multistep logic, via UTF-8 */
|
|
|
|
if (!encoding_equal(from_encoding, "UTF-8") &&
|
|
|
|
!encoding_equal(to_encoding, "UTF-8") &&
|
|
|
|
transcode_dispatch("UTF-8", to_encoding)) { /* check that we have a second step */
|
|
|
|
return transcode_dispatch(from_encoding, "UTF-8"); /* return first step */
|
|
|
|
}
|
|
|
|
return NULL;
|
2007-12-15 00:42:25 -05:00
|
|
|
}
|
|
|
|
}
|
2008-08-04 01:08:57 -04:00
|
|
|
xfree(key);
|
2007-12-25 00:57:04 -05:00
|
|
|
return (rb_transcoder *)val;
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
|
|
|
|
2008-07-31 16:35:35 -04:00
|
|
|
static void
|
|
|
|
output_replacement_character(unsigned char **out_pp, rb_encoding *enc)
|
2008-07-14 02:27:26 -04:00
|
|
|
{
|
2008-07-31 16:35:35 -04:00
|
|
|
unsigned char *out_p = *out_pp;
|
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) {
|
|
|
|
*out_p++ = 0xEF;
|
|
|
|
*out_p++ = 0xBF;
|
|
|
|
*out_p++ = 0xBD;
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-07-31 04:12:08 -04:00
|
|
|
else if (utf16be_encoding == enc) {
|
2008-07-31 16:35:35 -04:00
|
|
|
*out_p++ = 0xFF;
|
|
|
|
*out_p++ = 0xFD;
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-07-31 04:12:08 -04:00
|
|
|
else if (utf16le_encoding == enc) {
|
2008-07-31 16:35:35 -04:00
|
|
|
*out_p++ = 0xFD;
|
|
|
|
*out_p++ = 0xFF;
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-07-31 04:12:08 -04:00
|
|
|
else if (utf32be_encoding == enc) {
|
2008-07-31 16:35:35 -04:00
|
|
|
*out_p++ = 0x00;
|
|
|
|
*out_p++ = 0x00;
|
|
|
|
*out_p++ = 0xFF;
|
|
|
|
*out_p++ = 0xFD;
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-07-31 04:12:08 -04:00
|
|
|
else if (utf32le_encoding == enc) {
|
2008-07-31 16:35:35 -04:00
|
|
|
*out_p++ = 0xFD;
|
|
|
|
*out_p++ = 0xFF;
|
|
|
|
*out_p++ = 0x00;
|
|
|
|
*out_p++ = 0x00;
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
|
|
|
else {
|
2008-07-31 16:35:35 -04:00
|
|
|
*out_p++ = '?';
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2008-07-31 16:35:35 -04:00
|
|
|
*out_pp = out_p;
|
|
|
|
return;
|
2008-07-14 02:27:26 -04:00
|
|
|
}
|
2007-12-10 00:01:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Transcoding engine logic
|
|
|
|
*/
|
|
|
|
static void
|
2008-07-14 05:47:33 -04:00
|
|
|
transcode_loop(const unsigned char **in_pos, unsigned char **out_pos,
|
|
|
|
const unsigned char *in_stop, unsigned char *out_stop,
|
2007-12-25 00:57:04 -05:00
|
|
|
const rb_transcoder *my_transcoder,
|
2008-02-21 03:42:10 -05:00
|
|
|
rb_transcoding *my_transcoding,
|
|
|
|
const int opt)
|
2007-12-10 00:01:47 -05:00
|
|
|
{
|
2008-07-14 05:47:33 -04:00
|
|
|
const unsigned char *in_p = *in_pos;
|
|
|
|
unsigned char *out_p = *out_pos;
|
2007-12-10 03:46:06 -05:00
|
|
|
const BYTE_LOOKUP *conv_tree_start = my_transcoder->conv_tree_start;
|
|
|
|
const BYTE_LOOKUP *next_table;
|
2008-07-14 05:47:33 -04:00
|
|
|
const unsigned char *char_start;
|
2007-12-10 00:01:47 -05:00
|
|
|
unsigned int next_offset;
|
2007-12-10 03:46:06 -05:00
|
|
|
VALUE next_info;
|
2007-12-10 00:01:47 -05:00
|
|
|
unsigned char next_byte;
|
|
|
|
int from_utf8 = my_transcoder->from_utf8;
|
2008-01-20 22:35:05 -05:00
|
|
|
unsigned char *out_s = out_stop - my_transcoder->max_output + 1;
|
2008-07-14 02:27:26 -04:00
|
|
|
rb_encoding *to_encoding = rb_enc_find(my_transcoder->to_encoding);
|
2008-07-14 05:47:33 -04:00
|
|
|
|
2007-12-10 00:01:47 -05:00
|
|
|
while (in_p < in_stop) {
|
2008-01-20 01:12:48 -05:00
|
|
|
char_start = in_p;
|
2007-12-10 23:57:51 -05:00
|
|
|
next_table = conv_tree_start;
|
|
|
|
if (out_p >= out_s) {
|
2007-12-10 03:46:06 -05:00
|
|
|
int len = (out_p - *out_pos);
|
|
|
|
int new_len = (len + my_transcoder->max_output) * 2;
|
|
|
|
*out_pos = (*my_transcoding->flush_func)(my_transcoding, len, new_len);
|
|
|
|
out_p = *out_pos + len;
|
|
|
|
out_s = *out_pos + new_len - my_transcoder->max_output;
|
2007-12-10 23:57:51 -05:00
|
|
|
}
|
|
|
|
next_byte = (unsigned char)*in_p++;
|
2007-12-10 00:01:47 -05:00
|
|
|
follow_byte:
|
2007-12-10 23:57:51 -05:00
|
|
|
next_offset = next_table->base[next_byte];
|
|
|
|
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) {
|
2007-12-10 03:46:06 -05:00
|
|
|
case NOMAP:
|
|
|
|
*out_p++ = next_byte;
|
|
|
|
continue;
|
|
|
|
case 0x00: case 0x04: case 0x08: case 0x0C:
|
|
|
|
case 0x10: case 0x14: case 0x18: case 0x1C:
|
|
|
|
if (in_p >= in_stop) {
|
|
|
|
/* todo: deal with the case of backtracking */
|
|
|
|
/* todo: deal with incomplete input (streaming) */
|
2007-12-27 03:27:19 -05:00
|
|
|
goto invalid;
|
2007-12-10 03:46:06 -05:00
|
|
|
}
|
|
|
|
next_byte = (unsigned char)*in_p++;
|
|
|
|
if (from_utf8) {
|
|
|
|
if ((next_byte&0xC0) == 0x80)
|
|
|
|
next_byte -= 0x80;
|
2008-03-05 03:45:51 -05:00
|
|
|
else {
|
|
|
|
in_p--; /* may need to add more code later to revert other things */
|
2007-12-27 03:27:19 -05:00
|
|
|
goto invalid;
|
2008-03-05 03:45:51 -05:00
|
|
|
}
|
2007-12-10 03:46:06 -05:00
|
|
|
}
|
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;
|
2007-12-15 00:42:25 -05:00
|
|
|
/* maybe rewrite the following cases to use fallthrough???? */
|
2007-12-10 03:46:06 -05:00
|
|
|
case ZERObt: /* drop input */
|
|
|
|
continue;
|
|
|
|
case ONEbt:
|
|
|
|
*out_p++ = getBT1(next_info);
|
|
|
|
continue;
|
|
|
|
case TWObt:
|
|
|
|
*out_p++ = getBT1(next_info);
|
|
|
|
*out_p++ = getBT2(next_info);
|
|
|
|
continue;
|
|
|
|
case FOURbt:
|
|
|
|
*out_p++ = getBT0(next_info);
|
|
|
|
case THREEbt: /* fall through */
|
|
|
|
*out_p++ = getBT1(next_info);
|
|
|
|
*out_p++ = getBT2(next_info);
|
|
|
|
*out_p++ = getBT3(next_info);
|
|
|
|
continue;
|
2007-12-28 04:26:55 -05:00
|
|
|
case FUNii:
|
2008-01-20 22:35:05 -05:00
|
|
|
next_info = (VALUE)(*my_transcoder->func_ii)(next_info);
|
2007-12-28 04:26:55 -05:00
|
|
|
goto follow_info;
|
2008-01-20 01:12:48 -05:00
|
|
|
case FUNsi:
|
2008-01-20 22:35:05 -05:00
|
|
|
next_info = (VALUE)(*my_transcoder->func_si)(char_start);
|
2008-01-20 01:12:48 -05:00
|
|
|
goto follow_info;
|
|
|
|
break;
|
|
|
|
case FUNio:
|
2008-01-20 22:35:05 -05:00
|
|
|
out_p += (VALUE)(*my_transcoder->func_io)(next_info, out_p);
|
2008-01-20 01:12:48 -05:00
|
|
|
break;
|
|
|
|
case FUNso:
|
2008-01-20 22:35:05 -05:00
|
|
|
out_p += (VALUE)(*my_transcoder->func_so)(char_start, out_p);
|
2008-01-20 01:12:48 -05:00
|
|
|
break;
|
2007-12-27 03:27:19 -05:00
|
|
|
case INVALID:
|
|
|
|
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;
|
2007-12-27 03:27:19 -05:00
|
|
|
invalid:
|
|
|
|
/* deal with invalid byte sequence */
|
2008-02-21 03:42:10 -05:00
|
|
|
/* todo: add more alternative behaviors */
|
|
|
|
if (opt&INVALID_IGNORE) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-14 02:27:26 -04:00
|
|
|
else if (opt&INVALID_REPLACE) {
|
2008-07-31 16:35:35 -04:00
|
|
|
output_replacement_character(&out_p, to_encoding);
|
2008-07-14 02:27:26 -04:00
|
|
|
continue;
|
|
|
|
}
|
2008-08-04 23:34:52 -04:00
|
|
|
rb_raise(TRANSCODE_ERROR, "invalid byte sequence");
|
2007-12-10 23:57:51 -05:00
|
|
|
continue;
|
2008-07-14 02:27:26 -04:00
|
|
|
undef:
|
|
|
|
/* valid character in from encoding
|
|
|
|
* but no related character(s) in to encoding */
|
|
|
|
/* todo: add more alternative behaviors */
|
|
|
|
if (opt&UNDEF_IGNORE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (opt&UNDEF_REPLACE) {
|
2008-07-31 16:35:35 -04:00
|
|
|
output_replacement_character(&out_p, to_encoding);
|
2008-07-14 02:27:26 -04:00
|
|
|
continue;
|
|
|
|
}
|
2008-08-04 23:34:52 -04:00
|
|
|
rb_raise(TRANSCODE_ERROR, "conversion undefined for byte sequence (maybe invalid byte sequence)");
|
2008-07-14 02:27:26 -04:00
|
|
|
continue;
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|
|
|
|
/* cleanup */
|
|
|
|
*in_pos = in_p;
|
|
|
|
*out_pos = out_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* String-specific code
|
|
|
|
*/
|
|
|
|
|
2008-01-20 22:35:05 -05:00
|
|
|
static unsigned char *
|
2007-12-25 00:57:04 -05:00
|
|
|
str_transcoding_resize(rb_transcoding *my_transcoding, int len, int new_len)
|
2007-12-10 03:46:06 -05:00
|
|
|
{
|
|
|
|
VALUE dest_string = my_transcoding->ruby_string_dest;
|
|
|
|
rb_str_resize(dest_string, new_len);
|
2008-01-20 22:35:05 -05:00
|
|
|
return (unsigned char *)RSTRING_PTR(dest_string);
|
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-01-20 16:40:08 -05:00
|
|
|
const rb_transcoder *my_transcoder;
|
2007-12-25 00:57:04 -05:00
|
|
|
rb_transcoding my_transcoding;
|
2007-12-15 00:42:25 -05:00
|
|
|
int final_encoding = 0;
|
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 {
|
|
|
|
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 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-06-07 08:38:03 -04:00
|
|
|
do { /* loop for multistep transcoding */
|
2007-12-27 11:55:06 -05:00
|
|
|
/* later, maybe use smaller intermediate strings for very long strings */
|
2007-12-15 00:42:25 -05:00
|
|
|
if (!(my_transcoder = transcode_dispatch(from_e, to_e))) {
|
|
|
|
rb_raise(rb_eArgError, "transcoding not supported (from %s to %s)", from_e, to_e);
|
|
|
|
}
|
2007-12-10 00:01:47 -05:00
|
|
|
|
2008-01-21 00:36:16 -05:00
|
|
|
my_transcoding.transcoder = my_transcoder;
|
|
|
|
|
2007-12-27 11:55:06 -05:00
|
|
|
if (my_transcoder->preprocessor) {
|
2008-01-20 22:35:05 -05:00
|
|
|
fromp = sp = (unsigned char *)RSTRING_PTR(str);
|
2007-12-24 08:51:19 -05:00
|
|
|
slen = RSTRING_LEN(str);
|
|
|
|
blen = slen + 30; /* len + margin */
|
|
|
|
dest = rb_str_tmp_new(blen);
|
2008-01-20 22:35:05 -05:00
|
|
|
bp = (unsigned char *)RSTRING_PTR(dest);
|
2007-12-24 08:51:19 -05:00
|
|
|
my_transcoding.ruby_string_dest = dest;
|
2008-01-20 16:40:08 -05:00
|
|
|
(*my_transcoder->preprocessor)(&fromp, &bp, (sp+slen), (bp+blen), &my_transcoding);
|
2007-12-24 08:51:19 -05:00
|
|
|
if (fromp != sp+slen) {
|
2008-07-12 09:17:29 -04:00
|
|
|
rb_raise(rb_eArgError, "not fully converted, %"PRIdPTRDIFF" bytes left", sp+slen-fromp);
|
2007-12-24 08:51:19 -05:00
|
|
|
}
|
2008-07-14 05:47:33 -04:00
|
|
|
rb_str_set_len(dest, (char *)bp - RSTRING_PTR(dest));
|
2007-12-24 08:51:19 -05:00
|
|
|
str = dest;
|
|
|
|
}
|
2008-01-20 22:35:05 -05:00
|
|
|
fromp = sp = (unsigned char *)RSTRING_PTR(str);
|
2007-12-15 00:42:25 -05:00
|
|
|
slen = RSTRING_LEN(str);
|
|
|
|
blen = slen + 30; /* len + margin */
|
|
|
|
dest = rb_str_tmp_new(blen);
|
2008-01-20 22:35:05 -05:00
|
|
|
bp = (unsigned char *)RSTRING_PTR(dest);
|
2007-12-15 00:42:25 -05:00
|
|
|
my_transcoding.ruby_string_dest = dest;
|
|
|
|
my_transcoding.flush_func = str_transcoding_resize;
|
|
|
|
|
2008-02-21 03:42:10 -05:00
|
|
|
transcode_loop(&fromp, &bp, (sp+slen), (bp+blen), my_transcoder, &my_transcoding, options);
|
2007-12-15 00:42:25 -05:00
|
|
|
if (fromp != sp+slen) {
|
2008-07-12 09:17:29 -04:00
|
|
|
rb_raise(rb_eArgError, "not fully converted, %"PRIdPTRDIFF" bytes left", sp+slen-fromp);
|
2007-12-15 00:42:25 -05:00
|
|
|
}
|
2008-01-20 22:35:05 -05:00
|
|
|
buf = (unsigned char *)RSTRING_PTR(dest);
|
2007-12-15 00:42:25 -05:00
|
|
|
*bp = '\0';
|
|
|
|
rb_str_set_len(dest, bp - buf);
|
2007-12-27 11:55:06 -05:00
|
|
|
if (my_transcoder->postprocessor) {
|
2007-12-24 08:51:19 -05:00
|
|
|
str = dest;
|
2008-01-20 22:35:05 -05:00
|
|
|
fromp = sp = (unsigned char *)RSTRING_PTR(str);
|
2007-12-24 08:51:19 -05:00
|
|
|
slen = RSTRING_LEN(str);
|
|
|
|
blen = slen + 30; /* len + margin */
|
|
|
|
dest = rb_str_tmp_new(blen);
|
2008-01-20 22:35:05 -05:00
|
|
|
bp = (unsigned char *)RSTRING_PTR(dest);
|
2007-12-24 08:51:19 -05:00
|
|
|
my_transcoding.ruby_string_dest = dest;
|
2008-01-20 16:40:08 -05:00
|
|
|
(*my_transcoder->postprocessor)(&fromp, &bp, (sp+slen), (bp+blen), &my_transcoding);
|
2007-12-24 08:51:19 -05:00
|
|
|
if (fromp != sp+slen) {
|
2008-07-12 09:17:29 -04:00
|
|
|
rb_raise(rb_eArgError, "not fully converted, %"PRIdPTRDIFF" bytes left", sp+slen-fromp);
|
2007-12-24 08:51:19 -05:00
|
|
|
}
|
2008-01-20 22:35:05 -05:00
|
|
|
buf = (unsigned char *)RSTRING_PTR(dest);
|
2007-12-24 08:51:19 -05:00
|
|
|
rb_str_set_len(dest, bp - buf);
|
|
|
|
}
|
2007-12-15 00:42:25 -05:00
|
|
|
|
|
|
|
if (encoding_equal(my_transcoder->to_encoding, to_e)) {
|
|
|
|
final_encoding = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
from_e = my_transcoder->to_encoding;
|
|
|
|
str = dest;
|
|
|
|
}
|
2008-06-07 08:38:03 -04:00
|
|
|
} while (!final_encoding);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_transcode(void)
|
|
|
|
{
|
2007-12-25 00:57:04 -05:00
|
|
|
transcoder_table = st_init_strcasetable();
|
|
|
|
transcoder_lib_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);
|
2007-12-10 00:01:47 -05:00
|
|
|
}
|