1/**********************************************************************
2
3  transcode_data.h -
4
5  $Author: yugui $
6  created at: Mon 10 Dec 2007 14:01:47 JST 2007
7
8  Copyright (C) 2007 Martin Duerst
9
10**********************************************************************/
11
12#include "ruby/ruby.h"
13
14#ifndef RUBY_TRANSCODE_DATA_H
15#define RUBY_TRANSCODE_DATA_H 1
16
17#if defined __GNUC__ && __GNUC__ >= 4
18#pragma GCC visibility push(default)
19#endif
20
21#define WORDINDEX_SHIFT_BITS 2
22#define WORDINDEX2INFO(widx)      ((widx) << WORDINDEX_SHIFT_BITS)
23#define INFO2WORDINDEX(info)      ((info) >> WORDINDEX_SHIFT_BITS)
24#define BYTE_LOOKUP_BASE(bl) ((bl)[0])
25#define BYTE_LOOKUP_INFO(bl) ((bl)[1])
26
27#define PType (unsigned int)
28
29#define NOMAP	(PType 0x01)	/* direct map */
30#define ONEbt	(0x02)		/* one byte payload */
31#define TWObt	(0x03)		/* two bytes payload */
32#define THREEbt	(0x05)		/* three bytes payload */
33#define FOURbt	(0x06)		/* four bytes payload, UTF-8 only, macros start at getBT0 */
34#define INVALID	(PType 0x07)	/* invalid byte sequence */
35#define UNDEF	(PType 0x09)	/* legal but undefined */
36#define ZERObt	(PType 0x0A)	/* zero bytes of payload, i.e. remove */
37#define FUNii	(PType 0x0B)	/* function from info to info */
38#define FUNsi	(PType 0x0D)	/* function from start to info */
39#define FUNio	(PType 0x0E)	/* function from info to output */
40#define FUNso	(PType 0x0F)	/* function from start to output */
41#define STR1	(PType 0x11)	/* string 4 <= len <= 259 bytes: 1byte length + content */
42#define GB4bt	(PType 0x12)	/* GB18030 four bytes payload */
43#define FUNsio  (PType 0x13)    /* function from start and info to output */
44
45#define STR1_LENGTH(byte_addr) (unsigned int)(*(byte_addr) + 4)
46#define STR1_BYTEINDEX(w) ((w) >> 6)
47#define makeSTR1(bi) (((bi) << 6) | STR1)
48#define makeSTR1LEN(len) ((len)-4)
49
50#define o1(b1)		(PType((((unsigned char)(b1))<<8)|ONEbt))
51#define o2(b1,b2)	(PType((((unsigned char)(b1))<<8)|(((unsigned char)(b2))<<16)|TWObt))
52#define o3(b1,b2,b3)	(PType(((((unsigned char)(b1))<<8)|(((unsigned char)(b2))<<16)|(((unsigned int)(unsigned char)(b3))<<24)|THREEbt)&0xffffffffU))
53#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))
54#define g4(b0,b1,b2,b3) (PType(((((unsigned char)(b0))<<8)|(((unsigned char)(b2))<<16)|((((unsigned char)(b1))&0x0f)<<24)|((((unsigned int)(unsigned char)(b3))&0x0f)<<28)|GB4bt)&0xffffffffU))
55#define funsio(diff)	(PType((((unsigned int)(diff))<<8)|FUNsio))
56
57#define getBT1(a)	((unsigned char)((a)>> 8))
58#define getBT2(a)	((unsigned char)((a)>>16))
59#define getBT3(a)	((unsigned char)((a)>>24))
60#define getBT0(a)	(((unsigned char)((a)>> 5)&0x07)|0xF0)   /* for UTF-8 only!!! */
61
62#define getGB4bt0(a)	((unsigned char)((a)>> 8))
63#define getGB4bt1(a)	(((unsigned char)((a)>>24)&0x0F)|0x30)
64#define getGB4bt2(a)	((unsigned char)((a)>>16))
65#define getGB4bt3(a)	(((unsigned char)((a)>>28)&0x0F)|0x30)
66
67#define o2FUNii(b1,b2)	(PType((((unsigned char)(b1))<<8)|(((unsigned char)(b2))<<16)|FUNii))
68
69/* do we need these??? maybe not, can be done with simple tables */
70#define ONETRAIL       /* legal but undefined if one more trailing UTF-8 */
71#define TWOTRAIL       /* legal but undefined if two more trailing UTF-8 */
72#define THREETRAIL     /* legal but undefined if three more trailing UTF-8 */
73
74typedef enum {
75  asciicompat_converter,        /* ASCII-compatible -> ASCII-compatible */
76  asciicompat_decoder,          /* ASCII-incompatible -> ASCII-compatible */
77  asciicompat_encoder           /* ASCII-compatible -> ASCII-incompatible */
78  /* ASCII-incompatible -> ASCII-incompatible is intentionally omitted. */
79} rb_transcoder_asciicompat_type_t;
80
81typedef struct rb_transcoder rb_transcoder;
82
83/* static structure, one per supported encoding pair */
84struct rb_transcoder {
85    const char *src_encoding;
86    const char *dst_encoding;
87    unsigned int conv_tree_start;
88    const unsigned char *byte_array;
89    unsigned int byte_array_length;
90    const unsigned int *word_array;
91    unsigned int word_array_length;
92    int word_size;
93    int input_unit_length;
94    int max_input;
95    int max_output;
96    rb_transcoder_asciicompat_type_t asciicompat_type;
97    size_t state_size;
98    int (*state_init_func)(void*); /* ret==0:success ret!=0:failure(errno) */
99    int (*state_fini_func)(void*); /* ret==0:success ret!=0:failure(errno) */
100    VALUE (*func_ii)(void*, VALUE); /* info  -> info   */
101    VALUE (*func_si)(void*, const unsigned char*, size_t); /* start -> info   */
102    ssize_t (*func_io)(void*, VALUE, const unsigned char*, size_t); /* info  -> output */
103    ssize_t (*func_so)(void*, const unsigned char*, size_t, unsigned char*, size_t); /* start -> output */
104    ssize_t (*finish_func)(void*, unsigned char*, size_t); /* -> output */
105    ssize_t (*resetsize_func)(void*); /* -> len */
106    ssize_t (*resetstate_func)(void*, unsigned char*, size_t); /* -> output */
107    ssize_t (*func_sio)(void*, const unsigned char*, size_t, VALUE, unsigned char*, size_t); /* start -> output */
108};
109
110void rb_declare_transcoder(const char *enc1, const char *enc2, const char *lib);
111void rb_register_transcoder(const rb_transcoder *);
112
113/*
114 * To get rid of collision of initializer symbols in statically-linked encodings
115 * and transcoders
116 */
117#if defined(EXTSTATIC) && EXTSTATIC
118# define TRANS_INIT(name) void Init_trans_ ## name(void)
119#else
120# define TRANS_INIT(name) void Init_ ## name(void)
121#endif
122
123#if defined __GNUC__ && __GNUC__ >= 4
124#pragma GCC visibility pop
125#endif
126
127#endif /* RUBY_TRANSCODE_DATA_H */
128