-
Notifications
You must be signed in to change notification settings - Fork 117
/
lua_iconv.c
126 lines (111 loc) · 3.21 KB
/
lua_iconv.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
/**********************************************************************
*
* This file is part of Cardpeek, the smart card reader utility.
*
* Copyright 2009-2014 by Alain Pannetrat <[email protected]>
*
* Cardpeek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cardpeek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cardpeek. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <lauxlib.h>
#include "lua_iconv.h"
#include <iconv.h>
#include "a_string.h"
#include <string.h>
#include <errno.h>
#include "misc.h"
#include "win32/config.h"
static void lua_push_iconv(lua_State* L, iconv_t ic)
{
iconv_t *ptr = (iconv_t*)lua_newuserdata(L,sizeof(iconv_t));
*ptr = ic;
luaL_getmetatable(L, "iconv.type");
lua_setmetatable(L,-2);
}
static iconv_t luaL_check_iconv(lua_State *L, int p)
{
void *ud = luaL_checkudata(L,p,"iconv.type");
luaL_argcheck(L, ud != NULL, p, "`iconv' expected");
return *(iconv_t *)ud;
}
static int subr_iconv_open(lua_State *L)
{
const char *fromcode = luaL_checkstring(L,1);
const char *tocode = luaL_checkstring(L,2);
iconv_t ic = iconv_open(tocode,fromcode);
if (ic==(iconv_t)(-1))
{
lua_pushnil(L);
return 1;
}
lua_push_iconv(L,ic);
return 1;
}
static int subr_iconv_iconv(lua_State *L)
{
iconv_t ic = luaL_check_iconv(L,1);
ICONV_CONST char *src = (ICONV_CONST char *)luaL_checkstring(L,2);
size_t src_len = strlen(src);
char conv_block[16];
size_t conv_len;
char *conv_block_ptr;
a_string_t *dst = a_strnew(NULL);
while (src_len>0)
{
conv_len = 16;
conv_block_ptr = conv_block;
if (iconv(ic, &src, &src_len, &conv_block_ptr, &conv_len)==(size_t)(-1))
{
if (errno!=E2BIG)
{
a_strfree(dst);
lua_pushnil(L);
return 1;
}
}
a_strncat(dst,16-conv_len,conv_block);
}
lua_pushstring(L,a_strval(dst));
a_strfree(dst);
return 1;
}
static int subr_iconv_close(lua_State *L)
{
iconv_t ic = luaL_check_iconv(L,1);
iconv_close(ic);
return 0;
}
static const struct luaL_Reg iconvlib_m [] = {
{"__gc",subr_iconv_close},
{"open",subr_iconv_open},
{"iconv",subr_iconv_iconv},
{NULL,NULL}
};
static const struct luaL_Reg iconvlib_f [] = {
{"open",subr_iconv_open},
{"iconv",subr_iconv_iconv},
{NULL,NULL}
};
int luaopen_iconv(lua_State* L)
{
luaL_newmetatable(L, "iconv.type");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); /* pushes the metatable */
lua_settable(L, -3); /* metatable.__index = metatable */
luaL_setfuncs(L, iconvlib_m, 0);
luaL_newlib(L, iconvlib_f);
lua_setglobal(L,"iconv");
lua_pop(L,1); /* pop the metatable */
return 1;
}