author | Ryan C. Gordon <icculus@icculus.org> |
Sun, 22 Dec 2013 03:01:08 -0500 | |
changeset 11 | b52e0f1798b8 |
parent 7 | 682d7ea1e7f3 |
child 12 | 6a2d5b34d5ca |
permissions | -rw-r--r-- |
0 | 1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
|
3 |
#include <string.h> |
|
4 |
#include <assert.h> |
|
5 |
#include "lua.h" |
|
6 |
#include "lauxlib.h" |
|
7 |
#include "lualib.h" |
|
8 |
#include "pkcs5_pbkdf2.h" |
|
9 |
#include "aes.h" |
|
10 |
#include "base64.h" |
|
11 |
#include "md5.h" |
|
11
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
12 |
#include <gtk/gtk.h> |
0 | 13 |
|
14 |
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) ) |
|
15 |
||
16 |
static lua_State *luaState = NULL; |
|
17 |
static const uint8_t zero16[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; |
|
18 |
static const char saltprefix[] = { 'S', 'a', 'l', 't', 'e', 'd', '_', '_' }; |
|
19 |
||
20 |
static inline int retvalStringBytes(lua_State *L, const uint8_t *str, size_t len) |
|
21 |
{ |
|
22 |
if (str != NULL) |
|
23 |
lua_pushlstring(L, (const char *) str, len); |
|
24 |
else |
|
25 |
lua_pushnil(L); |
|
26 |
return 1; |
|
27 |
} // retvalStringBytes |
|
28 |
||
11
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
29 |
static inline int retvalPointer(lua_State *L, void *ptr) |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
30 |
{ |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
31 |
if (ptr != NULL) |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
32 |
lua_pushlightuserdata(L, ptr); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
33 |
else |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
34 |
lua_pushnil(L); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
35 |
return 1; |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
36 |
} // retvalPointer |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
37 |
|
0 | 38 |
static inline void xorBlock(uint8_t *dst, const uint8_t *src) |
39 |
{ |
|
40 |
int i; |
|
41 |
for (i = 0; i < 16; i++, dst++, src++) |
|
42 |
*dst ^= *src; |
|
43 |
} // xorBlock |
|
44 |
||
45 |
static int decryptUsingKeyAndIvec(uint8_t *data, size_t *datalen, const uint8_t *key, const uint8_t *iv) |
|
46 |
{ |
|
47 |
const size_t blocks = *datalen / 16; |
|
48 |
uint8_t *block = data + ((blocks-1) * 16); // start at final block, work backwards |
|
49 |
const uint8_t *padding = &block[15]; |
|
50 |
uint8_t expkey[aesExpandedKeySize]; |
|
51 |
size_t i; |
|
52 |
||
53 |
if (blocks == 0) |
|
54 |
return 1; // nothing to do. |
|
55 |
||
56 |
aesExpandKey(key, expkey); |
|
57 |
||
58 |
for (i = 0; i < blocks-1; i++) |
|
59 |
{ |
|
60 |
aesDecrypt(block, expkey, block); // decrypt in place. |
|
61 |
xorBlock(block, block-16); |
|
62 |
block -= 16; |
|
63 |
} |
|
64 |
aesDecrypt(block, expkey, block); // decrypt in place. |
|
65 |
xorBlock(block, iv); // xor against initial vector for final block. |
|
66 |
||
67 |
if (*padding > 16) |
|
68 |
return 0; // bad data? |
|
69 |
||
70 |
*datalen -= *padding; |
|
71 |
||
72 |
return 1; |
|
73 |
} // decryptBinaryUsingKeyAndIvec |
|
74 |
||
75 |
||
76 |
static inline int isSalted(const uint8_t *data, const size_t datalen) |
|
77 |
{ |
|
78 |
return ( (datalen > sizeof (saltprefix)) && |
|
79 |
(memcmp(data, saltprefix, sizeof (saltprefix)) == 0) ); |
|
80 |
} // isSalted |
|
81 |
||
82 |
||
83 |
static int decryptUsingPBKDF2(lua_State *L) |
|
84 |
{ |
|
85 |
const char *base64 = luaL_checkstring(L, 1); |
|
86 |
const char *password = luaL_checkstring(L, 2); |
|
87 |
const int iterations = luaL_checkinteger(L, 3); |
|
88 |
size_t datalen = strlen(base64); |
|
89 |
uint8_t *dataptr = (uint8_t *) malloc(datalen); |
|
90 |
uint8_t *data = dataptr; |
|
91 |
base64_decodestate base64state; |
|
92 |
||
93 |
base64_init_decodestate(&base64state); |
|
94 |
datalen = base64_decode_block(base64, (int) datalen, data, &base64state); |
|
95 |
||
96 |
const uint8_t *salt = zero16; |
|
97 |
int saltlen = sizeof (zero16); |
|
98 |
if (isSalted(data, datalen)) |
|
99 |
{ |
|
100 |
salt = data + 8; |
|
101 |
saltlen = 8; |
|
102 |
data += 16; |
|
103 |
datalen -= 16; |
|
104 |
} // if |
|
105 |
||
106 |
uint8_t output[32]; |
|
107 |
pkcs5_pbkdf2(password, strlen(password), salt, saltlen, output, sizeof (output), (unsigned int) iterations); |
|
108 |
||
109 |
const uint8_t *aeskey = &output[0]; |
|
110 |
const uint8_t *aesiv = &output[16]; |
|
111 |
if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv)) |
|
112 |
retvalStringBytes(L, data, datalen); |
|
113 |
else |
|
114 |
lua_pushnil(L); |
|
115 |
||
116 |
free(dataptr); |
|
117 |
return 1; |
|
118 |
} // decryptUsingPBKDF2 |
|
119 |
||
120 |
||
121 |
static int decryptBase64UsingKey(lua_State *L) |
|
122 |
{ |
|
123 |
size_t keylen = 0; |
|
124 |
const char *base64 = luaL_checkstring(L, 1); |
|
125 |
const uint8_t *key = (const uint8_t *) luaL_checklstring(L, 2, &keylen); |
|
126 |
size_t datalen = strlen(base64); |
|
127 |
uint8_t *dataptr = (uint8_t *) malloc(datalen); |
|
128 |
uint8_t *data = dataptr; |
|
129 |
base64_decodestate base64state; |
|
130 |
||
131 |
base64_init_decodestate(&base64state); |
|
132 |
datalen = base64_decode_block(base64, (int) datalen, data, &base64state); |
|
133 |
||
134 |
uint8_t aeskey[16]; |
|
135 |
uint8_t aesiv[16]; |
|
136 |
MD5_CTX md5; |
|
137 |
||
138 |
if (isSalted(data, datalen)) |
|
139 |
{ |
|
140 |
const uint8_t *salt = data + 8; |
|
141 |
const size_t saltlen = 8; |
|
142 |
data += 16; |
|
143 |
datalen -= 16; |
|
144 |
||
145 |
assert(aesNr == 10); // AES-256 needs more rounds. |
|
146 |
assert(aesNk == 4); // hashing size is hardcoded later. |
|
147 |
uint8_t hashing[32]; |
|
148 |
||
149 |
MD5_init(&md5); |
|
150 |
MD5_append(&md5, key, keylen); |
|
151 |
MD5_append(&md5, salt, saltlen); |
|
152 |
MD5_finish(&md5, hashing); |
|
153 |
||
154 |
MD5_init(&md5); |
|
155 |
MD5_append(&md5, hashing, 16); |
|
156 |
MD5_append(&md5, key, keylen); |
|
157 |
MD5_append(&md5, salt, saltlen); |
|
158 |
MD5_finish(&md5, &hashing[16]); |
|
159 |
||
160 |
memcpy(aeskey, hashing, 4 * aesNk); |
|
161 |
memcpy(aesiv, &hashing[4 * aesNk], 16); |
|
162 |
} // if |
|
163 |
else |
|
164 |
{ |
|
165 |
MD5_init(&md5); |
|
166 |
MD5_append(&md5, key, keylen); |
|
167 |
MD5_finish(&md5, aeskey); |
|
168 |
memset(aesiv, '\0', sizeof (aesiv)); |
|
169 |
} // else |
|
170 |
||
171 |
if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv)) |
|
172 |
retvalStringBytes(L, data, datalen); |
|
173 |
else |
|
174 |
lua_pushnil(L); |
|
175 |
||
176 |
free(dataptr); |
|
177 |
return 1; |
|
178 |
} // decryptBase64UsingKey |
|
179 |
||
180 |
||
11
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
181 |
static int makeGuiMenu(lua_State *L) |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
182 |
{ |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
183 |
return retvalPointer(L, gtk_menu_new()); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
184 |
} // makeGuiMenu |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
185 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
186 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
187 |
static void clickedMenuItem(void *arg) |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
188 |
{ |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
189 |
// This is the callback from GTK+; now call into our actual Lua callback! |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
190 |
const int callback = (int) ((size_t)arg); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
191 |
lua_rawgeti(luaState, LUA_REGISTRYINDEX, callback); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
192 |
lua_call(luaState, 0, 0); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
193 |
} // clickedMenuItem |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
194 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
195 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
196 |
// !!! FIXME: on destruction of a menu item, we need to luaL_unref(L, LUA_REGISTRYINDEX, callback)... |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
197 |
static int appendGuiMenuItem(lua_State *L) |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
198 |
{ |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
199 |
const int argc = lua_gettop(L); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
200 |
GtkWidget *menu = (GtkWidget *) lua_touserdata(L, 1); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
201 |
const char *label = luaL_checkstring(L, 2); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
202 |
GtkWidget *item = gtk_menu_item_new_with_label(label); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
203 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
204 |
if ((argc >= 3) && (!lua_isnil(L, 3))) |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
205 |
{ |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
206 |
assert(lua_isfunction(L, 3)); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
207 |
lua_pushvalue(L, 3); // copy the Lua callback (luaL_ref() pops it). |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
208 |
const int callback = luaL_ref(L, LUA_REGISTRYINDEX); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
209 |
gtk_signal_connect_object(GTK_OBJECT(item), "activate", GTK_SIGNAL_FUNC(clickedMenuItem), (gpointer) ((size_t)callback)); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
210 |
} // if |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
211 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
212 |
gtk_widget_show(item); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
213 |
gtk_menu_append(menu, item); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
214 |
return retvalPointer(L, item); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
215 |
} // appendGuiMenuItem |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
216 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
217 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
218 |
static int setGuiMenuItemSubmenu(lua_State *L) |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
219 |
{ |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
220 |
GtkMenuItem *item = (GtkMenuItem *) lua_touserdata(L, 1); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
221 |
GtkWidget *submenu = (GtkWidget *) lua_touserdata(L, 2); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
222 |
gtk_menu_item_set_submenu(item, submenu); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
223 |
return 0; |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
224 |
} // setGuiMenuItemSubmenu |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
225 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
226 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
227 |
static int popupGuiMenu(lua_State *L) |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
228 |
{ |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
229 |
GtkMenu *menu = (GtkMenu *) lua_touserdata(L, 1); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
230 |
gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time()); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
231 |
return 0; |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
232 |
} // popupGuiMenu |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
233 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
234 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
235 |
static int giveControlToGui(lua_State *L) |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
236 |
{ |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
237 |
gtk_main(); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
238 |
return 0; |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
239 |
} // giveControlToGui |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
240 |
|
7
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
241 |
|
0 | 242 |
static void *luaAlloc(void *ud, void *ptr, size_t osize, size_t nsize) |
243 |
{ |
|
244 |
if (nsize == 0) |
|
245 |
{ |
|
246 |
free(ptr); |
|
247 |
return NULL; |
|
248 |
} // if |
|
249 |
return realloc(ptr, nsize); |
|
250 |
} // luaAlloc |
|
251 |
||
252 |
||
253 |
static inline void luaSetCFunc(lua_State *L, lua_CFunction f, const char *sym) |
|
254 |
{ |
|
255 |
lua_pushcfunction(L, f); |
|
256 |
lua_setglobal(luaState, sym); |
|
257 |
} // luaSetCFunc |
|
258 |
||
259 |
||
260 |
static int luaFatal(lua_State *L) |
|
261 |
{ |
|
262 |
const char *errstr = lua_tostring(L, -1); |
|
263 |
fprintf(stderr, "Lua panic: %s\n", errstr ? errstr : "(?)"); |
|
264 |
fflush(stderr); |
|
265 |
exit(1); |
|
266 |
} // luaFatal |
|
267 |
||
268 |
||
11
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
269 |
static void deinitLua(void) |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
270 |
{ |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
271 |
if (luaState != NULL) |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
272 |
{ |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
273 |
lua_close(luaState); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
274 |
luaState = NULL; |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
275 |
} // if |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
276 |
} // deinitLua |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
277 |
|
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
278 |
|
7
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
279 |
static int initLua(const int argc, char **argv) |
0 | 280 |
{ |
11
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
281 |
atexit(deinitLua); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
282 |
|
0 | 283 |
assert(luaState == NULL); |
284 |
luaState = lua_newstate(luaAlloc, NULL); |
|
285 |
||
286 |
lua_atpanic(luaState, luaFatal); |
|
287 |
assert(lua_checkstack(luaState, 20)); // Just in case. |
|
2
16a2d269fd41
Just use the built-in luaL_openlibs()
Ryan C. Gordon <icculus@icculus.org>
parents:
1
diff
changeset
|
288 |
luaL_openlibs(luaState); |
0 | 289 |
|
290 |
// Set up initial C functions, etc we want to expose to Lua code... |
|
291 |
luaSetCFunc(luaState, decryptUsingPBKDF2, "decryptUsingPBKDF2"); |
|
292 |
luaSetCFunc(luaState, decryptBase64UsingKey, "decryptBase64UsingKey"); |
|
11
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
293 |
luaSetCFunc(luaState, makeGuiMenu, "makeGuiMenu"); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
294 |
luaSetCFunc(luaState, appendGuiMenuItem, "appendGuiMenuItem"); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
295 |
luaSetCFunc(luaState, setGuiMenuItemSubmenu, "setGuiMenuItemSubmenu"); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
296 |
luaSetCFunc(luaState, popupGuiMenu, "popupGuiMenu"); |
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
297 |
luaSetCFunc(luaState, giveControlToGui, "giveControlToGui"); |
0 | 298 |
|
7
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
299 |
// Set up argv table... |
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
300 |
lua_newtable(luaState); |
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
301 |
int i; |
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
302 |
for (i = 0; i < argc; i++) |
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
303 |
{ |
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
304 |
lua_pushinteger(luaState, i+1); |
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
305 |
lua_pushstring(luaState, argv[i]); |
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
306 |
lua_settable(luaState, -3); |
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
307 |
} // for |
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
308 |
lua_setglobal(luaState, "argv"); |
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
309 |
|
3
32af809e8c0b
Corrected comment. It's basically just moving to Lua as the mainline.
Ryan C. Gordon <icculus@icculus.org>
parents:
2
diff
changeset
|
310 |
// Transfer control to Lua... |
0 | 311 |
if (luaL_dofile(luaState, "1pass.lua") != 0) |
312 |
{ |
|
313 |
const char *msg = lua_tostring(luaState, -1); |
|
314 |
fprintf(stderr, "1pass.lua didn't run: %s\n", msg); |
|
315 |
lua_pop(luaState, 1); |
|
316 |
return 0; |
|
317 |
} // if |
|
318 |
||
319 |
return 1; |
|
320 |
} // initLua |
|
321 |
||
322 |
||
323 |
int main(int argc, char **argv) |
|
324 |
{ |
|
11
b52e0f1798b8
Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
325 |
gtk_init(&argc, &argv); |
0 | 326 |
|
7
682d7ea1e7f3
Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
327 |
if (!initLua(argc, argv)) // this will move control to 1pass.lua |
0 | 328 |
return 1; |
329 |
||
330 |
return 0; |
|
331 |
} // main |
|
332 |
||
333 |
// end of 1pass.c ... |
|
334 |