2007-12-24 08:51:19 -05:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
transcode_data.h -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Mon 10 Dec 2007 14:01:47 JST 2007
|
|
|
|
|
|
|
|
Copyright (C) 2007 Martin Duerst
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "ruby/ruby.h"
|
|
|
|
|
|
|
|
#ifndef RUBY_TRANSCODE_DATA_H
|
|
|
|
#define RUBY_TRANSCODE_DATA_H 1
|
|
|
|
|
2008-09-03 08:35:29 -04:00
|
|
|
#define WORDINDEX_SHIFT_BITS 2
|
|
|
|
#define WORDINDEX2INFO(widx) ((widx) << WORDINDEX_SHIFT_BITS)
|
|
|
|
#define INFO2WORDINDEX(info) ((info) >> WORDINDEX_SHIFT_BITS)
|
2008-09-02 09:26:02 -04:00
|
|
|
#define BYTE_LOOKUP_BASE(bl) ((bl)[0])
|
|
|
|
#define BYTE_LOOKUP_INFO(bl) ((bl)[1])
|
2008-09-01 10:33:56 -04:00
|
|
|
|
2007-12-25 00:57:04 -05:00
|
|
|
#ifndef PType
|
2007-12-10 00:01:47 -05:00
|
|
|
/* data file needs to treat this as a pointer, to remove warnings */
|
2008-09-02 09:26:02 -04:00
|
|
|
#define PType (unsigned int)
|
2007-12-10 00:01:47 -05:00
|
|
|
#endif
|
|
|
|
|
2007-12-28 04:26:55 -05:00
|
|
|
#define NOMAP (PType 0x01) /* single byte direct map */
|
|
|
|
#define ONEbt (0x02) /* one byte payload */
|
|
|
|
#define TWObt (0x03) /* two bytes payload */
|
|
|
|
#define THREEbt (0x05) /* three bytes payload */
|
|
|
|
#define FOURbt (0x06) /* four bytes payload, UTF-8 only, macros start at getBT0 */
|
|
|
|
#define INVALID (PType 0x07) /* invalid byte sequence */
|
|
|
|
#define UNDEF (PType 0x09) /* legal but undefined */
|
|
|
|
#define ZERObt (PType 0x0A) /* zero bytes of payload, i.e. remove */
|
|
|
|
#define FUNii (PType 0x0B) /* function from info to info */
|
2008-01-20 01:12:48 -05:00
|
|
|
#define FUNsi (PType 0x0D) /* function from start to info */
|
|
|
|
#define FUNio (PType 0x0E) /* function from info to output */
|
|
|
|
#define FUNso (PType 0x0F) /* function from start to output */
|
2007-12-28 04:26:55 -05:00
|
|
|
|
|
|
|
#define o1(b1) (PType((((unsigned char)(b1))<<8)|ONEbt))
|
|
|
|
#define o2(b1,b2) (PType((((unsigned char)(b1))<<8)|(((unsigned char)(b2))<<16)|TWObt))
|
2008-09-01 15:01:27 -04:00
|
|
|
#define o3(b1,b2,b3) (PType(((((unsigned char)(b1))<<8)|(((unsigned char)(b2))<<16)|(((unsigned char)(b3))<<24)|THREEbt)&0xffffffffU))
|
|
|
|
#define o4(b0,b1,b2,b3) (PType(((((unsigned char)(b1))<< 8)|(((unsigned char)(b2))<<16)|(((unsigned char)(b3))<<24)|((((unsigned char)(b0))&0x07)<<5)|FOURbt)&0xffffffffU))
|
2007-12-28 04:26:55 -05:00
|
|
|
|
|
|
|
#define getBT1(a) (((a)>> 8)&0xFF)
|
|
|
|
#define getBT2(a) (((a)>>16)&0xFF)
|
|
|
|
#define getBT3(a) (((a)>>24)&0xFF)
|
|
|
|
#define getBT0(a) ((((a)>> 5)&0x07)|0xF0) /* for UTF-8 only!!! */
|
|
|
|
|
|
|
|
#define o2FUNii(b1,b2) (PType((((unsigned char)(b1))<<8)|(((unsigned char)(b2))<<16)|FUNii))
|
2007-12-10 00:01:47 -05:00
|
|
|
|
|
|
|
/* do we need these??? maybe not, can be done with simple tables */
|
|
|
|
#define ONETRAIL /* legal but undefined if one more trailing UTF-8 */
|
|
|
|
#define TWOTRAIL /* legal but undefined if two more trailing UTF-8 */
|
|
|
|
#define THREETRAIL /* legal but undefined if three more trailing UTF-8 */
|
|
|
|
|
2008-08-15 19:13:01 -04:00
|
|
|
typedef enum {
|
|
|
|
stateless_converter, /* stateless -> stateless */
|
|
|
|
stateful_decoder, /* stateful -> stateless */
|
|
|
|
stateful_encoder /* stateless -> stateful */
|
|
|
|
/* stateful -> stateful is intentionally ommitted. */
|
|
|
|
} rb_transcoder_stateful_type_t;
|
|
|
|
|
2008-08-11 18:00:48 -04:00
|
|
|
typedef struct rb_transcoder rb_transcoder;
|
|
|
|
|
2007-12-24 08:51:19 -05:00
|
|
|
/* dynamic structure, one per conversion (similar to iconv_t) */
|
|
|
|
/* may carry conversion state (e.g. for iso-2022-jp) */
|
2007-12-25 00:57:04 -05:00
|
|
|
typedef struct rb_transcoding {
|
2008-08-11 18:00:48 -04:00
|
|
|
const rb_transcoder *transcoder;
|
2008-08-07 10:53:30 -04:00
|
|
|
|
2008-08-10 08:26:01 -04:00
|
|
|
int flags;
|
|
|
|
|
2008-08-09 02:02:01 -04:00
|
|
|
int resume_position;
|
2008-09-02 09:26:02 -04:00
|
|
|
unsigned int next_table;
|
2008-08-09 03:35:28 -04:00
|
|
|
VALUE next_info;
|
|
|
|
unsigned char next_byte;
|
2008-08-13 04:28:39 -04:00
|
|
|
|
2008-08-10 21:06:21 -04:00
|
|
|
int recognized_len; /* already interpreted */
|
|
|
|
int readagain_len; /* not yet interpreted */
|
2008-08-09 02:02:01 -04:00
|
|
|
union {
|
|
|
|
unsigned char ary[8]; /* max_input <= sizeof(ary) */
|
2008-08-13 04:28:39 -04:00
|
|
|
unsigned char *ptr; /* length: max_input */
|
2008-08-10 21:06:21 -04:00
|
|
|
} readbuf; /* recognized_len + readagain_len used */
|
2008-08-09 02:02:01 -04:00
|
|
|
|
2008-08-13 04:28:39 -04:00
|
|
|
int writebuf_off;
|
|
|
|
int writebuf_len;
|
|
|
|
union {
|
|
|
|
unsigned char ary[8]; /* max_output <= sizeof(ary) */
|
|
|
|
unsigned char *ptr; /* length: max_output */
|
|
|
|
} writebuf;
|
|
|
|
|
2008-08-07 10:53:30 -04:00
|
|
|
unsigned char stateful[256]; /* opaque data for stateful encoding */
|
2007-12-25 00:57:04 -05:00
|
|
|
} rb_transcoding;
|
2008-08-09 02:02:01 -04:00
|
|
|
#define TRANSCODING_READBUF(tc) \
|
|
|
|
((tc)->transcoder->max_input <= sizeof((tc)->readbuf.ary) ? \
|
|
|
|
(tc)->readbuf.ary : \
|
|
|
|
(tc)->readbuf.ptr)
|
2008-08-13 04:28:39 -04:00
|
|
|
#define TRANSCODING_WRITEBUF(tc) \
|
|
|
|
((tc)->transcoder->max_output <= sizeof((tc)->writebuf.ary) ? \
|
|
|
|
(tc)->writebuf.ary : \
|
|
|
|
(tc)->writebuf.ptr)
|
2007-12-24 08:51:19 -05:00
|
|
|
|
|
|
|
/* static structure, one per supported encoding pair */
|
2008-08-11 18:00:48 -04:00
|
|
|
struct rb_transcoder {
|
2007-12-24 08:51:19 -05:00
|
|
|
const char *from_encoding;
|
|
|
|
const char *to_encoding;
|
2008-09-02 09:26:02 -04:00
|
|
|
unsigned int conv_tree_start;
|
2008-09-01 13:40:32 -04:00
|
|
|
const unsigned char *byte_array;
|
2008-09-03 07:48:17 -04:00
|
|
|
unsigned int byte_array_length;
|
2008-09-02 09:26:02 -04:00
|
|
|
const unsigned int *word_array;
|
2008-09-03 07:48:17 -04:00
|
|
|
unsigned int word_array_length;
|
2008-09-01 13:40:32 -04:00
|
|
|
int word_size;
|
2008-08-09 02:02:01 -04:00
|
|
|
int input_unit_length;
|
|
|
|
int max_input;
|
2007-12-24 08:51:19 -05:00
|
|
|
int max_output;
|
2008-08-15 19:13:01 -04:00
|
|
|
rb_transcoder_stateful_type_t stateful_type;
|
2008-08-07 10:53:30 -04:00
|
|
|
VALUE (*func_ii)(rb_transcoding*, VALUE); /* info -> info */
|
|
|
|
VALUE (*func_si)(rb_transcoding*, const unsigned char*, size_t); /* start -> info */
|
|
|
|
int (*func_io)(rb_transcoding*, VALUE, const unsigned char*); /* info -> output */
|
|
|
|
int (*func_so)(rb_transcoding*, const unsigned char*, size_t, unsigned char*); /* start -> output */
|
|
|
|
int (*finish_func)(rb_transcoding*, unsigned char*); /* -> output */
|
2008-08-14 11:56:39 -04:00
|
|
|
int (*resetsize_func)(rb_transcoding*); /* -> len */
|
|
|
|
int (*resetstate_func)(rb_transcoding*, unsigned char*); /* -> output */
|
2008-08-11 18:00:48 -04:00
|
|
|
};
|
2007-12-25 00:57:04 -05:00
|
|
|
|
|
|
|
void rb_declare_transcoder(const char *enc1, const char *enc2, const char *lib);
|
|
|
|
void rb_register_transcoder(const rb_transcoder *);
|
2007-12-24 08:51:19 -05:00
|
|
|
|
|
|
|
#endif /* RUBY_TRANSCODE_DATA_H */
|