Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluaiconv.c
More file actions
Latest commit
240 lines (195 loc) · 6.46 KB
/
Copy pathluaiconv.c
File metadata and controls
240 lines (195 loc) · 6.46 KB
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
/*
* luaiconv - Performs character set conversions in Lua
* (c) 2005-09 Alexandre Erwin Ittner <aittner@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* If you use this package in a product, an acknowledgment in the product
* documentation would be greatly appreciated (but it is not required).
*
* $Id: luaiconv.c 65 2009-08-06 16:30:06Z dermeister $
*
*/
#include<lua.h>
#include<lauxlib.h>
#include<stdlib.h>
#include<iconv.h>
#include<errno.h>
#defineLIB_NAME "iconv"
#defineLIB_VERSION LIB_NAME " 6"
#defineICONV_TYPENAME "iconv_t"
/* Compatibility between Lua 5.1+ and Lua 5.0 */
#ifndefLUA_VERSION_NUM
#defineLUA_VERSION_NUM 0
#endif
#ifLUA_VERSION_NUM<501
#defineluaL_register(a, b, c) luaL_openlib((a), (b), (c), 0)
#endif
/* Emulates lua_(un)boxpointer from Lua 5.0 (don't exists on Lua 5.1) */
#defineboxptr(L, p) (*(void**)(lua_newuserdata(L, sizeof(void*))) = (p))
#defineunboxptr(L, i) (*(void**)(lua_touserdata(L, i)))
/* Table assumed on top */
#definetblseticons(L, c, v) \
lua_pushliteral(L, c); \
lua_pushnumber(L, v); \
lua_settable(L, -3);
#defineERROR_NO_MEMORY 1
#defineERROR_INVALID 2
#defineERROR_INCOMPLETE 3
#defineERROR_UNKNOWN 4
staticvoidpush_iconv_t(lua_State*L, iconv_tcd) {
boxptr(L, cd);
luaL_getmetatable(L, ICONV_TYPENAME);
lua_setmetatable(L, -2);
}
staticiconv_tget_iconv_t(lua_State*L, inti) {
if (luaL_checkudata(L, i, ICONV_TYPENAME) !=NULL) {
iconv_tcd=unboxptr(L, i);
if (cd== (iconv_t) NULL)
luaL_error(L, "attempt to use an invalid "ICONV_TYPENAME);
returncd;
}
luaL_typerror(L, i, ICONV_TYPENAME);
returnNULL;
}
staticintLiconv_open(lua_State*L) {
constchar*tocode=luaL_checkstring(L, 1);
constchar*fromcode=luaL_checkstring(L, 2);
iconv_tcd=iconv_open(tocode, fromcode);
if (cd!= (iconv_t)(-1))
push_iconv_t(L, cd); /* ok */
else
lua_pushnil(L); /* erro */
return1;
}
staticintLiconv(lua_State*L) {
iconv_tcd=get_iconv_t(L, 1);
size_tibleft=lua_strlen(L, 2);
char*inbuf= (char*) luaL_checkstring(L, 2);
char*outbuf;
char*outbufs;
size_tobsize= (ibleft>256) ? ibleft : 256;
size_tobleft=obsize;
size_tret=-1;
inthasone=0;
outbuf= (char*) malloc(obsize*sizeof(char));
if (outbuf==NULL) {
lua_pushstring(L, "");
lua_pushnumber(L, ERROR_NO_MEMORY);
return2;
}
outbufs=outbuf;
do {
ret=iconv(cd, &inbuf, &ibleft, &outbuf, &obleft);
if (ret== (size_t)(-1)) {
lua_pushlstring(L, outbufs, obsize-obleft);
if (hasone==1)
lua_concat(L, 2);
hasone=1;
if (errno==EILSEQ) {
lua_pushnumber(L, ERROR_INVALID);
free(outbufs);
return2; /* Invalid character sequence */
} elseif (errno==EINVAL) {
lua_pushnumber(L, ERROR_INCOMPLETE);
free(outbufs);
return2; /* Incomplete character sequence */
} elseif (errno==E2BIG) {
obleft=obsize;
outbuf=outbufs;
} else {
lua_pushnumber(L, ERROR_UNKNOWN);
free(outbufs);
return2; /* Unknown error */
}
}
} while (ret!= (size_t) 0);
lua_pushlstring(L, outbufs, obsize-obleft);
if (hasone==1)
lua_concat(L, 2);
free(outbufs);
return1; /* Done */
}
#ifdefHAS_ICONVLIST/* a GNU extension? */
staticintpush_one(unsigned intcnt, char*names[], void*data) {
lua_State*L= (lua_State*) data;
intn= (int) lua_tonumber(L, -1);
inti;
/* Stack: <tbl> n */
lua_remove(L, -1);
for (i=0; i<cnt; i++) {
/* Stack> <tbl> */
lua_pushnumber(L, n++);
lua_pushstring(L, names[i]);
/* Stack: <tbl> n <str> */
lua_settable(L, -3);
}
lua_pushnumber(L, n);
/* Stack: <tbl> n */
return0;
}
staticintLiconvlist(lua_State*L) {
lua_newtable(L);
lua_pushnumber(L, 1);
/* Stack: <tbl> 1 */
iconvlist(push_one, (void*) L);
/* Stack: <tbl> n */
lua_remove(L, -1);
return1;
}
#endif
staticintLiconv_close(lua_State*L) {
iconv_tcd=get_iconv_t(L, 1);
if (iconv_close(cd) ==0)
lua_pushboolean(L, 1); /* ok */
else
lua_pushnil(L); /* erro */
return1;
}
staticconstluaL_reginconvFuncs[] = {
{ "open", Liconv_open },
{ "new", Liconv_open },
{ "iconv", Liconv },
#ifdefHAS_ICONVLIST
{ "list", Liconvlist },
#endif
{ NULL, NULL }
};
staticconstluaL_regiconvMT[] = {
{ "__gc", Liconv_close },
{ NULL, NULL }
};
intluaopen_iconv(lua_State*L) {
luaL_register(L, LIB_NAME, inconvFuncs);
tblseticons(L, "ERROR_NO_MEMORY", ERROR_NO_MEMORY);
tblseticons(L, "ERROR_INVALID", ERROR_INVALID);
tblseticons(L, "ERROR_INCOMPLETE", ERROR_INCOMPLETE);
tblseticons(L, "ERROR_UNKNOWN", ERROR_UNKNOWN);
lua_pushliteral(L, "VERSION");
lua_pushstring(L, LIB_VERSION);
lua_settable(L, -3);
luaL_newmetatable(L, ICONV_TYPENAME);
lua_pushliteral(L, "__index");
lua_pushvalue(L, -3);
lua_settable(L, -3);
luaL_openlib(L, NULL, iconvMT, 0);
lua_pop(L, 1);
return1;
}