-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.c
308 lines (257 loc) · 8.69 KB
/
conv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "conv.h"
#ifdef USE_ICU
#define convert(L, OP, TO, TEXT, TSIZE) character_convert(L, "UTF-8", TO, TEXT, TSIZE)
static inline int character_convert(lua_State *L, const char* from, const char* to, const char* text, size_t tsize)
{
UErrorCode err;
err = U_ZERO_ERROR;
ssize_t len = ucnv_convert(to, from, NULL, 0, text, tsize, &err);
if (U_FAILURE(err) && err != U_BUFFER_OVERFLOW_ERROR)
return luaL_error(L, "[ICU ERROR]: %s, %d", u_errorName(err), len);
char *buf;
if (len < xrio_max_block)
buf = alloca(len);
else
buf = lua_newuserdata(L, len);
err = U_ZERO_ERROR;
len = ucnv_convert(to, from, buf, len, text, tsize, &err);
if (U_FAILURE(err))
return luaL_error(L, "[ICU ERROR]: %s, %d", u_errorName(err), len);
lua_pushlstring(L, buf, len);
return 1;
}
int lconvert_convert(lua_State *L)
{
const char* to = luaL_checkstring(L, 1);
if (!to)
return luaL_error(L, "[ICU ERROR]: Invalid conversion character set. #1");
const char* from = luaL_checkstring(L, 2);
if (!from)
return luaL_error(L, "[ICU ERROR]: Invalid conversion character set. #2");
size_t size = 0;
const char* text = luaL_checklstring(L, 3, &size);
if (!text || size < 1)
return luaL_error(L, "[ICU ERROR]: Please fill in the text content to be converted.");
return character_convert(L, from, to, text, size);
}
int lconvert_from(lua_State *L)
{
const char* opcode = luaL_checkstring(L, 1);
if (!opcode)
return luaL_error(L, "[ICU ERROR]: Invalid conversion character set.");
size_t size = 0;
const char* text = luaL_checklstring(L, 2, &size);
if (!text || size < 1)
return luaL_error(L, "[ICU ERROR]: Please fill in the text content to be converted.");
return character_convert(L, opcode, "UTF-8", text, size);
}
int lconvert_to(lua_State *L)
{
const char* opcode = luaL_checkstring(L, 1);
if (!opcode)
return luaL_error(L, "[ICU ERROR]: Invalid conversion character set.");
size_t size = 0;
const char* text = luaL_checklstring(L, 2, &size);
if (!text || size < 1)
return luaL_error(L, "[ICU ERROR]: Please fill in the text content to be converted.");
return character_convert(L, "UTF-8", opcode, text, size);
}
#else
/* 实际转换代码 */
static inline int iconv_convert(lua_State *L, iconv_t cd, const char* text, size_t size) {
xrio_Buffer B;
xrio_buffinit(L, &B);
size_t osize = xrio_max_block;
char *buf = alloca(xrio_max_block);
char *out = buf;
char *in = (char *)text;
while ((size_t)-1 == iconv(cd, (char**)&in, &size, (char**)&out, &osize))
{
/* 只有空间不足继续尝试 */
if (errno != E2BIG) {
lua_pushboolean(L, 0);
lua_pushfstring(L, "[ICONV ERROR]: %s. (%d)", strerror(errno), errno);
iconv_close(cd);
xrio_reset(&B);
errno = 0;
return 2;
}
xrio_addlstring(&B, buf, xrio_max_block - osize);
out = buf; osize = xrio_max_block; errno = 0;
}
/* 结束 */
xrio_addlstring(&B, buf, xrio_max_block - osize);
xrio_pushresult(&B);
iconv_close(cd);
errno = 0;
return 1;
}
/* 增加自动判断 */
static inline iconv_t new_iconv(uint8_t mode, const char* opcode) {
if (mode == CONVERT_FROM)
return iconv_open("UTF-8", opcode);
if (mode == CONVERT_TO)
return iconv_open(opcode, "UTF-8");
return (iconv_t)-1;
}
// 开始转换
static inline int convert(lua_State *L, uint8_t mode, const char* opcode, const char* text, size_t size) {
iconv_t cd = new_iconv(mode, opcode);
if (cd == (iconv_t)-1)
return luaL_error(L, "[ICONV ERROR]: Unsupported characterset (%s).", opcode);
/* 初始化 */
iconv(cd, NULL, NULL, NULL, NULL);
return iconv_convert(L, cd, text, size);
}
int lconvert_convert(lua_State *L)
{
const char* to = luaL_checkstring(L, 1);
if (!to)
return luaL_error(L, "[ICONV ERROR]: Invalid conversion character set. #1");
const char* from = luaL_checkstring(L, 2);
if (!from)
return luaL_error(L, "[ICONV ERROR]: Invalid conversion character set. #2");
size_t size = 0;
const char* text = luaL_checklstring(L, 3, &size);
if (!text)
return luaL_error(L, "[ICONV ERROR]: Please fill in the text content to be converted.");
iconv_t cd = iconv_open(to, from);
if (cd == (iconv_t)-1)
return luaL_error(L, "[ICONV ERROR]: Unsupported characterset (%s, %s).", to, from);
return iconv_convert(L, cd, text, size);
}
// 从其他字符集转换为UTF-8
int lconvert_from(lua_State *L)
{
errno = 0;
const char* opcode = luaL_checkstring(L, 1);
if (!opcode)
return luaL_error(L, "[ICONV ERROR]: Invalid conversion character set.");
size_t size = 0;
const char* text = luaL_checklstring(L, 2, &size);
if (!text || size < 1)
return luaL_error(L, "[ICONV ERROR]: Please fill in the text content to be converted.");
return convert(L, CONVERT_FROM, opcode, text, size);
}
// 从UTF-8转换为其他字符集
int lconvert_to(lua_State *L)
{
errno = 0;
const char* opcode = luaL_checkstring(L, 1);
if (!opcode)
return luaL_error(L, "[ICONV ERROR]: Invalid conversion character set.");
size_t size = 0;
const char* text = luaL_checklstring(L, 2, &size);
if (!text || size < 1)
return luaL_error(L, "[ICONV ERROR]: Please fill in the text content to be converted.");
return convert(L, CONVERT_TO, opcode, text, size);
}
#endif
// 从UTF-8转换为GBK
static int lconvert_gbk(lua_State *L) {
size_t size = 0;
const char* text = luaL_checklstring(L, 1, &size);
return convert(L, CONVERT_TO, "GBK", text, size);
}
// 从UTF-8转换为GBK2312
static int lconvert_gb2312(lua_State *L) {
size_t size = 0;
const char* text = luaL_checklstring(L, 1, &size);
return convert(L, CONVERT_TO, "GB2312", text, size);
}
// 从UTF-8转换为GB18030
static int lconvert_gb18030(lua_State *L) {
size_t size = 0;
const char* text = luaL_checklstring(L, 1, &size);
return convert(L, CONVERT_TO, "GB18030", text, size);
}
// 从UTF-8转换为UTF-16
static int lconvert_u16(lua_State *L) {
size_t size = 0;
const char* text = luaL_checklstring(L, 1, &size);
return convert(L, CONVERT_TO, "UTF-16", text, size);
}
// 从UTF-8转换为UTF-32
static int lconvert_u32(lua_State *L) {
size_t size = 0;
const char* text = luaL_checklstring(L, 1, &size);
return convert(L, CONVERT_TO, "UTF-32", text, size);
}
// 从UTF-8转换为UCS-2
static int lconvert_ucs2(lua_State *L) {
size_t size = 0;
const char* text = luaL_checklstring(L, 1, &size);
return convert(L, CONVERT_TO, "UCS-2", text, size);
}
// 从UTF-8转换为UCS-2BE
static int lconvert_ucs2be(lua_State *L) {
size_t size = 0;
const char* text = luaL_checklstring(L, 1, &size);
return convert(L, CONVERT_TO, "UCS-2BE", text, size);
}
// 从UTF-8转换为UCS-2LE
static int lconvert_ucs2le(lua_State *L) {
size_t size = 0;
const char* text = luaL_checklstring(L, 1, &size);
return convert(L, CONVERT_TO, "UCS-2LE", text, size);
}
// 从UTF-8转换为UCS-4
static int lconvert_ucs4(lua_State *L) {
size_t size = 0;
const char* text = luaL_checklstring(L, 1, &size);
return convert(L, CONVERT_TO, "UCS-4", text, size);
}
// 从UTF-8转换为UCS-4BE
static int lconvert_ucs4be(lua_State *L) {
size_t size = 0;
const char* text = luaL_checklstring(L, 1, &size);
return convert(L, CONVERT_TO, "UCS-4BE", text, size);
}
// 从UTF-8转换为UCS-4LE
static int lconvert_ucs4le(lua_State *L) {
size_t size = 0;
const char* text = luaL_checklstring(L, 1, &size);
return convert(L, CONVERT_TO, "UCS-4LE", text, size);
}
/* 获取libiconv 大、小版本号 */
static inline void luaL_geticonv(lua_Integer* max, lua_Integer* min) {
if (max)
*max = _LIBICONV_VERSION >> 8;
if (min)
*min = (_LIBICONV_VERSION - ((_LIBICONV_VERSION >> 8) << 8));
}
// 初始化内置库
static inline void luaL_add_iconv_version(lua_State *L) {
/* 根据情况拿到大、小版本号 */
lua_Integer max = 0; lua_Integer min = 0;
luaL_geticonv(&max, &min);
/* 导入libiconv版本号 */
luaL_setkn(L, "LIBICONV_VERSION", max + /* 如果小版本是三位数 */ ( min > 99 ? min * 1e-3 : min * 1e-2 ));
/* 导入lua-iconv版本号 */
luaL_setkn(L, "VERSION", 0.1);
}
LUAMOD_API int luaopen_liconv(lua_State *L)
{
luaL_checkversion(L);
luaL_Reg iconv_libs[] = {
{ "convert", lconvert_convert },
{ "from", lconvert_from },
{ "to", lconvert_to },
/* 增加UTF-8转为常用的字符集方法 */
{ "u16", lconvert_u16 },
{ "u32", lconvert_u32 },
{ "gbk", lconvert_gbk },
{ "gb2312", lconvert_gb2312 },
{ "gb18030", lconvert_gb18030 },
{ "ucs2", lconvert_ucs2 },
{ "ucs2be", lconvert_ucs2be },
{ "ucs2le", lconvert_ucs2le },
{ "ucs4", lconvert_ucs4 },
{ "ucs4be", lconvert_ucs4be },
{ "ucs4le", lconvert_ucs4le },
{NULL, NULL},
};
luaL_newlib(L, iconv_libs);
luaL_add_iconv_version(L);
return 1;
}