xtables-addons: update to version 2.5
[openwrt/svn-archive/archive.git] / package / network / utils / xtables-addons / patches / 200-add-lua-packetscript.patch
1 --- /dev/null
2 +++ b/extensions/LUA/byte_array.c
3 @@ -0,0 +1,145 @@
4 +/*
5 + * Copyright (C) 2010 University of Basel <http://cn.cs.unibas.ch/>
6 + * by Andre Graf <andre@dergraf.org>
7 + *
8 + * This program is free software; you can redistribute it and/or modify
9 + * it under the terms of the GNU General Public License as published by
10 + * the Free Software Foundation; either version 2 of the License, or
11 + * (at your option) any later version.
12 + *
13 + * This program is distributed in the hope that it will be useful,
14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 + * GNU General Public License for more details.
17 + *
18 + * You should have received a copy of the GNU General Public License
19 + * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 + */
21 +#include "controller.h"
22 +
23 +/* Initialization helper function. This function should be used whenever
24 + * a new byte array need to be initialized. Depending on the arguments it
25 + * initializes the array in a different way. Have a look at the inline
26 + * comments */
27 +lua_packet_segment * init_byte_array(lua_State *L, unsigned char * start, int length, int do_copy)
28 +{
29 + lua_packet_segment *array;
30 +
31 + if (length < 0)
32 + luaL_error(L, "init_byte_array, requested size < 0");
33 +
34 + if (start && do_copy) {
35 + /* we have a start address where we copy from */
36 + array = lua_newuserdata(L, sizeof(lua_packet_segment) + length);
37 + array->start = (unsigned char *)array + sizeof(lua_packet_segment); /* aligning pointer */
38 + memcpy(array->start, start, length);
39 + }else if (start && !do_copy) {
40 + /* just link the start pointer, in this case you have to free the memory yourself */
41 + array = lua_newuserdata(L, sizeof(lua_packet_segment));
42 + array->start = start;
43 + }else{
44 + /* create an empty array, fully managed by Lua */
45 + array = lua_newuserdata(L, sizeof(lua_packet_segment) + length);
46 + array->start = (unsigned char *)array + sizeof(lua_packet_segment); /* aligning pointer */
47 + memset(array->start, 0, length);
48 + }
49 +
50 + array->length = length;
51 + array->offset = 0;
52 + array->changes = NULL;
53 +
54 + luaL_getmetatable(L, LUA_BYTE_ARRAY);
55 + lua_setmetatable(L, -2);
56 +
57 + return array;
58 +}
59 +
60 +
61 +
62 +/* LUA_API: get one byte of the given byte array
63 + * access-pattern: array[<index>] */
64 +static int32_t get_byte_array(lua_State *L)
65 +{
66 + lua_packet_segment * array = checkbytearray(L, 1);
67 + int32_t index = luaL_checkinteger(L, 2); /* array starts with index 0 (not 1 as usual in Lua) */
68 +
69 + luaL_argcheck(L, 0 <= index && index < array->length, 1, "index out of range");
70 + lua_pushinteger(L, (array->start + array->offset)[index]);
71 +
72 + return 1;
73 +}
74 +
75 +/* LUA_API: set one byte of the given byte array
76 + * access-pattern: array[<index>]= 0xFF */
77 +static int32_t set_byte_array(lua_State *L)
78 +{
79 + lua_packet_segment * array = checkbytearray(L, 1);
80 + uint8_t byte;
81 + int32_t index = luaL_checkinteger(L, 2); /* array starts with index 0 (not 1 as usual in Lua) */
82 + int32_t val = luaL_checkinteger(L, 3);
83 + uint32_t nob = 1 << CHAR_BIT; /* we should use something like 1 << CHAR_BIT */
84 +
85 + luaL_argcheck(L, 0 <= index && index < array->length, 1, "index out of range");
86 + luaL_argcheck(L, 0 <= val && val < nob, 2, "cannot cast value to char");
87 +
88 + byte = (uint8_t)val;
89 +
90 + (array->start + array->offset)[index] = byte;
91 +
92 + return 0;
93 +}
94 +
95 +/* LUA_API: get size of the given byte array
96 + * access-pattern: #array (__length meta-method) */
97 +static int32_t get_byte_array_size(lua_State *L)
98 +{
99 + lua_packet_segment * array = checkbytearray(L, 1);
100 +
101 + lua_pushnumber(L, array->length);
102 +
103 + return 1;
104 +}
105 +
106 +
107 +/* LUA_API: converts a given byte array to a string.
108 + * access-pattern: implicit through functions calling the
109 + * __to_string() metamethod , e.g. print32_t */
110 +static int32_t byte_array_to_string(lua_State *L)
111 +{
112 + lua_packet_segment * array = checkbytearray(L, 1);
113 + uint8_t buf[(array->length * 3) + 255];
114 + uint8_t hexval[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
115 + char res[255 + (array->length * 3)]; /* make sure the buffer is big enough*/
116 + int32_t i, n;
117 + uint8_t *ptr = array->start + array->offset;
118 +
119 + for (i = 0; i < array->length; i++) {
120 + buf[i * 3] = hexval[(ptr[i] >> 4) & 0xF];
121 + buf[(i * 3) + 1] = hexval[ptr[i] & 0x0F];
122 + buf[(i * 3) + 2] = ' '; /* seperator */
123 + }
124 +
125 + buf[array->length * 3] = '\0';
126 + n = sprintf(res, "byte_array: length: %d value: %s", array->length, buf);
127 +
128 + lua_pushlstring(L, res, n);
129 +
130 + return 1;
131 +}
132 +
133 +static const struct luaL_Reg bytearray_lib_m [] = {
134 + { "__len", get_byte_array_size },
135 + { "__newindex", set_byte_array },
136 + { "__index", get_byte_array },
137 + { "__tostring", byte_array_to_string },
138 + { NULL, NULL }
139 +};
140 +
141 +void luaopen_bytearraylib(lua_State *L)
142 +{
143 + luaL_newmetatable(L, LUA_BYTE_ARRAY);
144 + luaL_register(L, NULL, bytearray_lib_m);
145 + lua_pop(L, 1);
146 +}
147 +
148 +
149 --- /dev/null
150 +++ b/extensions/LUA/controller.c
151 @@ -0,0 +1,604 @@
152 +/*
153 + * Copyright (C) 2010 University of Basel <http://cn.cs.unibas.ch/>
154 + * by Andre Graf <andre@dergraf.org>
155 + *
156 + * This program is free software; you can redistribute it and/or modify
157 + * it under the terms of the GNU General Public License as published by
158 + * the Free Software Foundation; either version 2 of the License, or
159 + * (at your option) any later version.
160 + *
161 + * This program is distributed in the hope that it will be useful,
162 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
163 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
164 + * GNU General Public License for more details.
165 + *
166 + * You should have received a copy of the GNU General Public License
167 + * along with this program; if not, see <http://www.gnu.org/licenses/>.
168 + */
169 +
170 +#if defined(__KERNEL__)
171 + #include <linux/mm.h>
172 +#endif
173 +#include "controller.h"
174 +
175 +/* the array 'supported_protocols' holds all pointers to the
176 + * static and dynamic protocol buffers. It is filled by the
177 + * call to register_protbuf */
178 +static struct protocol_buf * supported_protocols[MAX_NR_OF_PROTOCOLS];
179 +
180 +/* C_API: the function 'get_protocol_buf' returns the pointer
181 + * to the protocol buffer of a given protocol id. */
182 +struct protocol_buf * get_protocol_buf(uint32_t protocol_id)
183 +{
184 + return (struct protocol_buf *)supported_protocols[protocol_id];
185 +}
186 +
187 +
188 +/* LUA_INT: the function 'gc_packet_segment' is triggered by the
189 + * garbage collector whenever a userdata annotated with one of
190 + * the protocol buffer metatable should be collected. */
191 +static int32_t gc_packet_segment(lua_State *L)
192 +{
193 + lua_packet_segment * seg = (lua_packet_segment *)lua_touserdata(L, 1);
194 + if (seg && seg->changes) {
195 + seg->changes->ref_count--;
196 + if (seg->changes->ref_count <= 0) {
197 + kfree(seg->changes->field_length_changes);
198 + kfree(seg->changes->field_offset_changes);
199 + kfree(seg->changes);
200 + seg->changes = NULL;
201 + }
202 + }
203 + return 0;
204 +}
205 +
206 +
207 +/* LUA_API: the function 'set_raw' is used to set the bytes of a segment
208 + * in 'raw' mode. The function is per default available in each protocol
209 + * buffer until it gets overridden by a specific setter function inside
210 + * a protocol buffer.
211 + *
212 + * Parameters:
213 + * 1. lua_packet_segment (implicit)
214 + * 2. int32_t byte_value
215 + *
216 + * Upvalues:
217 + * 1. struct protocol_buf*
218 + * 2. int32_t field index, not used in this function
219 + *
220 + * Return: void
221 + */
222 +static int32_t set_raw(lua_State *L)
223 +{
224 + int32_t i;
225 + uint32_t nob;
226 + uint8_t byte;
227 + uint8_t *ptr;
228 + struct protocol_buf * prot_buf = (struct protocol_buf *)lua_topointer(L, lua_upvalueindex(1));
229 + lua_packet_segment * seg = checkpacketseg(L, 1, prot_buf->name);
230 +
231 + int32_t val = luaL_checkinteger(L, 2);
232 +
233 + nob = 1 << CHAR_BIT;
234 +
235 + luaL_argcheck(L, 0 <= val && val < nob, 2, "cannot cast value to char");
236 +
237 + byte = (uint8_t)val;
238 + ptr = seg->start + seg->offset;
239 +
240 + for (i = 0; i < seg->length; i++)
241 + ptr[i] = byte;
242 +
243 + return 0;
244 +}
245 +
246 +/* LUA_API: the function 'get_raw' is used to get the bytes of a segment
247 + * in 'raw' mode. The function is per default available in each protocol
248 + * buffer until it gets overridden by a specific getter function inside
249 + * a protocol buffer.
250 + *
251 + * Parameters:
252 + * 1. lua_packet_segment (implicit)
253 + * 2. uint32_t offset
254 + * 3. uint32_t length
255 + *
256 + * Upvalues:
257 + * 1. struct protocol_buf*
258 + * 2. int32_t field index, not used in this function
259 + *
260 + * Return:
261 + * the byte array representing the given array
262 + */
263 +static int32_t get_raw(lua_State *L)
264 +{
265 + struct protocol_buf * prot_buf = (struct protocol_buf *)lua_topointer(L, lua_upvalueindex(1));
266 + lua_packet_segment * seg = checkpacketseg(L, 1, prot_buf->name);
267 +
268 + init_byte_array(L, seg->start + seg->offset, seg->length, 1);
269 +
270 + return 1;
271 +}
272 +/* LUA_API: The function 'get_segment' is used to get a new segment in 'raw' mode.
273 + * Typically this function is applied on another raw segment in order
274 + * to extract a part of the segment as new segment.
275 + *
276 + * Parameters:
277 + * 1. lua_packet_segment, implicit through object oriented access seg:raw(..)
278 + * 2. uint32_t offset, this indicates where to start the new segment, see e.g below.
279 + * 3. uint32_t length, this indicates the size of the new segment
280 + *
281 + * Upvalues:
282 + * 1. struct protocol_buf*
283 + * 2. int32_t field index, not used in this function
284 + *
285 + * Return:
286 + * 1. A lua_packet_segment annotated with the according metatable or False in
287 + * case the input data is not valid
288 + *
289 + * Example:
290 + *
291 + * +------------------------+---------------------------------------+
292 + * | function call | resulting lua_packet_segment |
293 + * +========================+===+===+===+===+===+===+===+===+===+===+
294 + * | seg = packet:raw(0,10) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
295 + * +------------------------+---+---+---+---+---+---+---+---+---+---+
296 + * | 1st_half = seg:raw(0,5)| 0 | 1 | 2 | 3 | 4 | |
297 + * +------------------------+---+---+---+---+---+---+---+---+---+---+
298 + * | 2nd_half = seg:raw(5,5)| | 5 | 6 | 7 | 8 | 9 |
299 + * +------------------------+-------------------+---+---+---+---+---+
300 + */
301 +static int32_t get_segment(lua_State *L)
302 +{
303 + struct protocol_buf * prot_buf = (struct protocol_buf *)lua_topointer(L, lua_upvalueindex(1));
304 + lua_packet_segment * seg = checkpacketseg(L, 1, prot_buf->name);
305 + uint32_t offset = luaL_checkinteger(L, 2);
306 + uint32_t length = luaL_checkinteger(L, 3);
307 + lua_packet_segment * new = (lua_packet_segment *)lua_newuserdata(L, sizeof(lua_packet_segment));
308 +
309 + new->start = seg->start;
310 + new->offset = seg->offset + offset;
311 + new->changes = NULL;
312 + /* we allow a seg->length == 0 , this enables processing packets where the packetsize is not fixed (0 = not fixed)*/
313 + if (seg->length != 0 && length > seg->length) {
314 + lua_pushboolean(L, 0);
315 + return 1;
316 + }
317 +
318 + new->length = length;
319 + luaL_getmetatable(L, prot_buf->name);
320 + lua_setmetatable(L, -2);
321 +
322 + return 1;
323 +}
324 +
325 +/* LUA_API: the function 'get_segment_size' is used to get the size of a segment.
326 + *
327 + * Parameters:
328 + * 1. lua_packet_segment, implicit through object oriented access seg:raw(..)
329 + *
330 + * Upvalues:
331 + * 1. struct protocol_buf*
332 + * 2. int32_t field index, not used in this function
333 + *
334 + * Return:
335 + * 1. Size as lua_Number
336 + */
337 +static int32_t get_segment_size(lua_State *L)
338 +{
339 + struct protocol_buf * prot_buf = (struct protocol_buf *)lua_topointer(L, lua_upvalueindex(1));
340 + lua_packet_segment * seg = checkpacketseg(L, 1, prot_buf->name);
341 +
342 + lua_pushnumber(L, seg->length);
343 + return 1;
344 +}
345 +
346 +/* LUA_API: the function 'get_segment_offset' is used to get the real offset
347 + * of a segment. This function returns the offset of the segment to the start
348 + * of the buffer. This means the following
349 + * seg1 = packet:raw(2,10)
350 + * seg2 = seg1:raw(3,5)
351 + * offset = seg2:get_offset()
352 + *
353 + * will give an offset of 5, since the seg1 starts at offset 2, and seg2 starts
354 + * at offset (seg1:get_offset() + 3).
355 + *
356 + * Parameters:
357 + * 1. lua_packet_segment, implicit through object oriented access seg:raw(..)
358 + *
359 + * Upvalues:
360 + * 1. struct protocol_buf*
361 + * 2. int32_t field index, not used in this function
362 + *
363 + * Return:
364 + * 1. Offset as lua_Number
365 + */
366 +static int32_t get_segment_offset(lua_State *L)
367 +{
368 + struct protocol_buf * prot_buf = (struct protocol_buf *)lua_topointer(L, lua_upvalueindex(1));
369 + lua_packet_segment * seg = checkpacketseg(L, 1, prot_buf->name);
370 +
371 + lua_pushnumber(L, seg->offset);
372 + return 1;
373 +}
374 +
375 +/* LUA_API: overwrites the __tostring function of a lua_packet_segment.
376 + * this will print32_t a nicely formated string, including length,
377 + * offset and name of the protocol buffer.
378 + *
379 + * Parameters:
380 + * 1. lua_packet_segment (implicit)
381 + *
382 + * Returns:
383 + * 1. the representing string
384 + */
385 +static int32_t packet_segment_tostring(lua_State *L)
386 +{
387 + struct protocol_buf * prot_buf = (struct protocol_buf *)lua_topointer(L, lua_upvalueindex(1));
388 + lua_packet_segment * seg = checkpacketseg(L, 1, prot_buf->name);
389 + int32_t n;
390 + char buf[128];
391 +
392 + n = sprintf(buf, "type: %s, offset: %d, length: %d", prot_buf->name, seg->offset, seg->length);
393 + lua_pushlstring(L, buf, n);
394 +
395 + return 1;
396 +}
397 +
398 +
399 +static const struct luaL_Reg seg_access_functions [] = {
400 + { "set", set_raw },
401 + { "get", get_raw },
402 + { "raw", get_segment },
403 + { "get_offset", get_segment_offset },
404 + { "get_size", get_segment_size },
405 + { "to_bytes", get_raw },
406 + { "__tostring", packet_segment_tostring },
407 + { "__gc", gc_packet_segment },
408 + { NULL, NULL }
409 +};
410 +
411 +/* C_API: the function 'get_metatable_from_protocol_type' is a helper
412 + * used in controller.c as well as it may find usage in the static
413 + * protocol buffers and byte array implementation. */
414 +void get_metatable_from_protocol_type(lua_State *L, int32_t type)
415 +{
416 + char * table;
417 + lua_getglobal(L, SUPPORTED_PROTOCOL_TABLE);
418 + lua_rawgeti(L, -1, type);
419 + table = (char *)luaL_checkstring(L, -1);
420 + lua_pop(L, 2); /* pop the table SUPPORTED_PROTOCOL_TABLE and the string pushed by lua_gettable */
421 + luaL_getmetatable(L, table);
422 + return;
423 +}
424 +
425 +/* C_INT: the function 'payload_contains_protocol' is used internally.
426 + * Depending if static or dynamic protocol buffer it calls the right
427 + * validation function. */
428 +static int32_t payload_contains_protocol(lua_State *L, struct protocol_buf *prot_buf, lua_packet_segment *seg, uint32_t prot_type)
429 +{
430 + if (prot_buf->is_dynamic)
431 + return has_protocol_dynamic(L, prot_buf, seg, prot_type);
432 + else
433 + return prot_buf->has_protocol(L, prot_buf, seg, prot_type);
434 +}
435 +
436 +/* C_INT: the function 'protocol_get_field_changes' is used interally.
437 + * It requests the field_changes struct calling the protocol buffers
438 + * 'get_field_changes' function. This funciton is called, whenever
439 + * the payload field with a given protocol type is requested inside
440 + * the function 'get_protocol_field' */
441 +static struct field_changes * protocol_get_field_changes(lua_State *L, struct protocol_buf *prot_buf, lua_packet_segment * seg)
442 +{
443 + struct field_changes * changes = NULL;
444 +
445 + if (prot_buf->get_field_changes) {
446 + if (prot_buf->is_dynamic)
447 + changes = get_field_changes_dynamic(L, prot_buf, seg);
448 + else
449 + changes = prot_buf->get_field_changes(L, seg);
450 + /* is already 1 when set by helper 'get_allocated_field_changes,
451 + * since not every prot_buf may use this function we enforce it. */
452 + changes->ref_count = 1;
453 + }
454 + return changes;
455 +}
456 +
457 +/* C_INT: the function 'get_field_offset_in_bytes' wrapps the logic of
458 + * calculating the new length with considering the optional field_changes. */
459 +static int32_t get_field_offset_in_bytes(struct protocol_field * field, lua_packet_segment * seg, int32_t field_index)
460 +{
461 + uint32_t nr_of_bits, nr_of_bytes, field_offset;
462 +
463 + field_offset = field->offset;
464 + /* do we need to manipulate the default values stored inside the protocol buffer ?? */
465 + if (seg->changes)
466 + field_offset += seg->changes->field_offset_changes[field_index];
467 + /* how many bits remain */
468 + nr_of_bits = field_offset & (CHAR_BIT - 1);
469 + /* assuming CHAR_BIT == 2 ^ 3 */
470 + nr_of_bytes = (field_offset - nr_of_bits) >> 3;
471 +
472 + return seg->offset + nr_of_bytes;
473 +}
474 +
475 +/* C_INT: the function 'get_field_length_in_bytes' wrapps the logic of
476 + * calculating the new offset with considering the optional field_changes. */
477 +static int32_t get_field_length_in_bytes(struct protocol_field * field, lua_packet_segment * seg, int32_t field_index)
478 +{
479 + uint32_t nr_of_bits, nr_of_bytes, field_length;
480 +
481 + field_length = field->length;
482 + /* if the field length is smaller than 1 byte, we take the size of one byte
483 + * we treat the case where field_length == 0 in a special way ...*/
484 + if (field_length < CHAR_BIT && field_length > 0)
485 + field_length = CHAR_BIT;
486 +
487 + /* do we need to manipulate the default values stored inside the protocol buffer ?? */
488 + if (seg->changes)
489 + field_length += seg->changes->field_length_changes[field_index];
490 + /* how many bits remain */
491 + nr_of_bits = field_length & (CHAR_BIT - 1);
492 + /* assuming CHAR_BIT == 2 ^ 3 */
493 + nr_of_bytes = (field_length - nr_of_bits) >> 3;
494 + return nr_of_bytes;
495 +}
496 +
497 +/* C_INT: the function 'initialize_field_getter_and_setter' initializes
498 + * the setter and getter function of the field, considering the optional
499 + * field manipulator functions defined inside the protocol buffers. */
500 +static void initialize_field_getter_and_setter(lua_State *L, struct protocol_buf *prot_buf, int32_t field_index)
501 +{
502 + /* lets check if there is a metatable on top of the stack */
503 + struct protocol_field * f = (struct protocol_field *)&prot_buf->protocol_fields[field_index];
504 +
505 + if (!lua_istable(L, -1)) luaL_error(L, "cannot initialize getter and setter for field %s->%s, "
506 + "not a table on top of the stack, is '%s'", prot_buf->name, f->name, lua_typename(L, lua_type(L, -1)));
507 +
508 + /* is there a 'getter' to initialize ? */
509 + lua_pushlightuserdata(L, prot_buf); /* push upvalue 1 */
510 + lua_pushinteger(L, field_index); /* push upvalue 2 */
511 + if (f->get) {
512 + if (prot_buf->is_dynamic)
513 + lua_pushcclosure(L, field_dynamic_getter, 2);
514 + else
515 + lua_pushcclosure(L, f->get, 2);
516 + }else
517 + /* there is no specific getter defined - fall back to 'get_raw' */
518 + lua_pushcclosure(L, get_raw, 2);
519 + /* set the metatable field 'get' */
520 + lua_setfield(L, -2, "get");
521 +
522 + /* is there a 'setter' to initialize ? */
523 + lua_pushlightuserdata(L, prot_buf); /* push upvalue 1 */
524 + lua_pushinteger(L, field_index); /* push upvalue 2 */
525 + if (f->set) {
526 + if (prot_buf->is_dynamic)
527 + lua_pushcclosure(L, field_dynamic_setter, 2);
528 + else
529 + lua_pushcclosure(L, f->set, 2);
530 + }else
531 + /* there is no specific setter defined - fall back to 'set_raw' */
532 + lua_pushcclosure(L, set_raw, 2);
533 + /* set the metatable field 'set' */
534 + lua_setfield(L, -2, "set");
535 +}
536 +
537 +/* LUA_API: 'get_protocol_field' is used in Lua as a closure for each field of a protocol
538 + * buffer. E.g a call to ip = packet:data(packet_ip) will go to this function,
539 + * and trigger the conversion of the raw packet to a ip packet. Each call
540 + * to a field function of an IP packet, like ip:daddr() uses this function
541 + * to to return the right data. In each case you will end up either with a
542 + * new packet segment (annotated with the proper metatable) or a boolean
543 + * value (False) if something went wrong. In the case everything went fine,
544 + * the newly created lua_packet_segment is annotated with the proper
545 + * metatable where the fields get and set also contain the specific getter
546 + * and setter functions given by the protocol buffer. E.g. the function call
547 + * ip:daddr():get() or ip:daddr():set(...) will call the proper function
548 + * defined inside the corresponding field definition.
549 + *
550 + * Parameters:
551 + * 1. lua_packet_segment, implicit through object oriented access seg:raw(..)
552 + * 2. type of the protocol buffer, optional, and only used if the accessed
553 + * field is the payload field. If a type is provided for the access of the
554 + * payload field, the function tries to convert the data pointed to by the
555 + * payload field to the given type. To check if such a conversion is
556 + * possible, it calls the function pointed to by the protocol buffer member
557 + * has_protocol. If this function returns True, the conversion takes place.
558 + *
559 + * Upvalues:
560 + * 1. struct protocol_buf*
561 + * 2. int32_t field index
562 + *
563 + * Return:
564 + * 1. A lua_packet_segment annotated with the according metatable or False in
565 + * case the input data is not valid
566 + */
567 +static int32_t get_protocol_field(lua_State *L)
568 +{
569 + int32_t prot_type;
570 + lua_packet_segment * seg, *new;
571 + struct protocol_buf * prot_buf = (struct protocol_buf *)lua_topointer(L, lua_upvalueindex(1));
572 + int32_t field_index = lua_tointeger(L, lua_upvalueindex(2));
573 + struct protocol_field * field = &prot_buf->protocol_fields[field_index];
574 +
575 + /* get the current packet segment */
576 + seg = checkpacketseg(L, 1, prot_buf->name);
577 +
578 + /* initialize the new packet segment */
579 + new = (lua_packet_segment *)lua_newuserdata(L, sizeof(lua_packet_segment));
580 + new->start = seg->start; /* the start is unchanged */
581 + new->offset = get_field_offset_in_bytes(field, seg, field_index);
582 + new->length = get_field_length_in_bytes(field, seg, field_index);
583 +
584 + /* if new->length == 0 then no configuration was done, we guess the size by subtracting the
585 + * new offset from the packet length. since the old length is getting initialized by the
586 + * netfilter extension this assumption holds for the very last field of the protocol.
587 + * this 'feature' should be used by protocol buffers containing a payload, whereas the
588 + * payload field is the last field of the buffer. However, at compile-time unknown field
589 + * sizes (and offsets) of fields not being placed at the end of the protocol should be
590 + * initialized using the 'get_field_changes' hook system. */
591 + if (new->length == 0)
592 + new->length = (seg->length + seg->offset) - (new->offset);
593 + /*
594 + printf("%s->%s:: seg->offset %i, seg->length %i, new->offset %i, new->length %i\n",
595 + prot_buf->name, field->name, seg->offset, seg->length, new->offset, new->length);
596 + */
597 + /* special care for packet payload requests */
598 + if (prot_buf->payload_field != NULL && strcmp(prot_buf->payload_field, field->name) == 0) {
599 + /* we know the payload field is requested */
600 + /* the requested payload can be delivered either as a common segment or as
601 + * an other packet type, such a conversion needs an extra protocol parameter
602 + * ... so lets check */
603 +
604 + if (lua_isnumber(L, 2)) {
605 + /* we have an extra parameter, ... lets see if it is a valid protocol
606 + * the parameter is the index of the 'supported_protocols'-array member */
607 + prot_type = lua_tointeger(L, 2);
608 + if (prot_type >= 0 && prot_type < PACKET_SENTINEL) {
609 + /* we are sure the purpose of the request is to get the payload data,
610 + * converted to the given protocol. lets check if the payload contains
611 + * data of the given protocol */
612 + if (payload_contains_protocol(L, prot_buf, seg, prot_type)) {
613 + /* success, we can push the metatable for the given protocol */
614 + get_metatable_from_protocol_type(L, prot_type);
615 + if (!lua_isnil(L, -1)) /* check if the metatable was found */
616 + /* perhaps the field offsets and lengths of the containing protocol
617 + * are not set correctly. request the optional 'field_changes' structure
618 + * holding the changes for lengths and offsets. */
619 + new->changes = protocol_get_field_changes(L, get_protocol_buf(prot_type), new);
620 + else{
621 + /* failed, the requested protocol is not available
622 + * we push false and return */
623 + lua_pop(L, 1); /* pop the userdata */
624 + lua_pushboolean(L, 0);
625 + return 1;
626 + }
627 + }else{
628 + /* payload does not carry the provided protocol */
629 + /* we push false and return */
630 + lua_pop(L, 1); /* pop the userdata */
631 + lua_pushboolean(L, 0);
632 + return 1;
633 + }
634 + }else{
635 + /* unknown protocol */
636 + lua_pop(L, 1); /* pop the userdata */
637 + luaL_error(L, "provided protocol is unknown");
638 + }
639 + }
640 + }
641 +
642 + /* if there is still the 'new' userdata on the top, we push our own metatable */
643 + if (lua_isuserdata(L, -1)) {
644 + luaL_getmetatable(L, prot_buf->name);
645 + new->changes = seg->changes;
646 + if (seg->changes)
647 + new->changes->ref_count++;
648 + }
649 +
650 + /* a new packet segment is at index -2 , and the proper metatable at index -1 of the stack
651 + * lets set the propper setter and getter function for the requested field */
652 + initialize_field_getter_and_setter(L, prot_buf, field_index);
653 +
654 + lua_setmetatable(L, -2);
655 + return 1;
656 +}
657 +
658 +/* C_API: 'register_protbuf' is only used internally. This function takes a
659 + * pointer to a fully initialized protocol buffer struct and registers it
660 + * inside the Lua state. Registering means:
661 + *
662 + * 1. it creates a new metatable with the name of the protocol buffer.
663 + * 2. it registers the default functions which are stored in the luaL_Reg
664 + * array seg_access_functions.
665 + * 3. it loops over the protocol fields stored at prot_buf->protocol_fields
666 + * and registers a new function (using the field name) inside the
667 + * metatable. Each field points to the function 'get_protocol_field'
668 + * which acts as a closure taking a pointer to the protocol buffer as
669 + * well as the index of the field as upvalues.
670 + * 4. The protocol index, serves as numerical identifier of this protocol
671 + * buffer or even of the protocol itself. This index is stored as a
672 + * global value inside the Lua state as well as inside the Lua table
673 + * 'supported_protocols'. Assuming the name of a procotol buffer is
674 + * "packet_ip" the following statements are true:
675 + *
676 + * supported_protocols[protocol_index] == "packet_ip"
677 + * packet_ip == protocol_index
678 + *
679 + * This allows you to get all registered protocols from within Lua. This
680 + * is especially usefull for the dynamic protocol buffers where you have
681 + * to provide your own "has_protocol"-function, which probably needs the
682 + * information on which protocols it is able to contain.
683 + */
684 +void register_protbuf(lua_State *L, struct protocol_buf * prot_buf, uint32_t protocol_index)
685 +{
686 + int32_t field_index;
687 + luaL_Reg *reg = (struct luaL_Reg *)seg_access_functions;
688 + struct protocol_field * field = prot_buf->protocol_fields;
689 +
690 + luaL_newmetatable(L, prot_buf->name);
691 +
692 + /* metatable.__index = metatable */
693 + lua_pushvalue(L, -1); /* duplicates the metatable */
694 + lua_setfield(L, -2, "__index");
695 +
696 + /* pushing default functions */
697 + for (; reg->name; reg++) {
698 + lua_pushlightuserdata(L, (void *)prot_buf);
699 + lua_pushcclosure(L, reg->func, 1);
700 + lua_setfield(L, -2, reg->name);
701 + }
702 +
703 + /* pushing functions specific to the protocol buffer */
704 + for (field_index = 0; field->name; field++, field_index++) {
705 + lua_pushlightuserdata(L, (void *)prot_buf); /* upvalue: prot_buf */
706 + lua_pushinteger(L, field_index); /* upvalue: index of protocol field */
707 + lua_pushcclosure(L, get_protocol_field, 2);
708 + lua_setfield(L, -2, field->name);
709 + }
710 + /* pop the metatable */
711 + lua_pop(L, 1);
712 +
713 + /* registering the array-index as the protocol_id*/
714 + lua_getglobal(L, "_G");
715 + lua_pushinteger(L, protocol_index);
716 + lua_setfield(L, -2, prot_buf->name);
717 + lua_pop(L, 1); /* pop _G */
718 +
719 + lua_getglobal(L, SUPPORTED_PROTOCOL_TABLE);
720 + lua_pushstring(L, prot_buf->name);
721 + lua_rawseti(L, -2, protocol_index);
722 +
723 + lua_pop(L, 1); /* pop SUPPORTED_PROTOCOL_TABLE */
724 +
725 + supported_protocols[protocol_index] = prot_buf;
726 +}
727 +
728 +void luaopen_controller(lua_State *L)
729 +{
730 + /* registering a table inside the _G with table[protocol_index] = prot_buf->name */
731 + lua_getglobal(L, "_G");
732 + lua_newtable(L);
733 + lua_setfield(L, -2, SUPPORTED_PROTOCOL_TABLE);
734 + lua_pop(L, 1); /* pop _G */
735 +
736 + luaopen_protbuf_raw(L);
737 + luaopen_protbuf_eth(L);
738 + luaopen_protbuf_ip(L);
739 + luaopen_protbuf_icmp(L);
740 + luaopen_protbuf_tcp(L);
741 + luaopen_protbuf_tcp_options(L);
742 + luaopen_protbuf_udp(L);
743 + luaopen_protbuf_tftp(L);
744 + luaopen_protbuf_dynamic(L);
745 + /* should follow all other static buffers */
746 +#if defined(__KERNEL__)
747 + luaopen_nflib(L);
748 +#endif
749 +
750 + luaopen_bytearraylib(L);
751 +}
752 +
753 +
754 +
755 +
756 --- /dev/null
757 +++ b/extensions/LUA/controller.h
758 @@ -0,0 +1,264 @@
759 +/*
760 + * Copyright (C) 2010 University of Basel <http://cn.cs.unibas.ch/>
761 + * by Andre Graf <andre@dergraf.org>
762 + *
763 + * This program is free software; you can redistribute it and/or modify
764 + * it under the terms of the GNU General Public License as published by
765 + * the Free Software Foundation; either version 2 of the License, or
766 + * (at your option) any later version.
767 + *
768 + * This program is distributed in the hope that it will be useful,
769 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
770 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
771 + * GNU General Public License for more details.
772 + *
773 + * You should have received a copy of the GNU General Public License
774 + * along with this program; if not, see <http://www.gnu.org/licenses/>.
775 + */
776 +
777 +#ifndef CONTROLLER_H_
778 +#define CONTROLLER_H_
779 +
780 +#include "stdlib.h" /* wrapper */
781 +#include "string.h" /* wrapper */
782 +#include "lua.h"
783 +#include "lualib.h"
784 +#include "lauxlib.h"
785 +
786 +#if defined(__KERNEL__)
787 +#include <linux/skbuff.h>
788 +#include <linux/slab.h>
789 +#include <linux/vmalloc.h>
790 +#endif
791 +
792 +
793 +/* to compile the stuff in userspace (for testing)*/
794 +#if !defined(__KERNEL__)
795 +#include <stdint.h>
796 +#define pr_debug printf;
797 +
798 +#define kmalloc(size, type) malloc(size)
799 +#define kfree(ptr) free(ptr)
800 +
801 +#endif
802 +
803 +
804 +/**********************************************************************/
805 +/* nf Lua configuration */
806 +/**********************************************************************/
807 +#define MAX_NR_OF_PROTOCOLS 16
808 +#define SUPPORTED_PROTOCOL_TABLE "supported_protocols"
809 +
810 +#define MAX_NR_OF_FIELDS_IN_DYN_PROT_BUF 32
811 +
812 +
813 +/**********************************************************************/
814 +/* Static Protocol Buffer configuration */
815 +/**********************************************************************/
816 +
817 +/* the definitions of the stringified expression of the prot_bufs...
818 + * make sure all static prot_bufs are listed and are unique */
819 +#define LUA_PACKET_SEG_RAW "packet_raw"
820 +#define LUA_PACKET_SEG_ETH "packet_eth"
821 +#define LUA_PACKET_SEG_ICMP "packet_icmp"
822 +#define LUA_PACKET_SEG_IP "packet_ip"
823 +#define LUA_PACKET_SEG_TCP "packet_tcp"
824 +#define LUA_PACKET_SEG_TCP_OPT "packet_tcp_opt"
825 +#define LUA_PACKET_SEG_UDP "packet_udp"
826 +#define LUA_PACKET_SEG_TFTP "packet_tftp"
827 +
828 +/* the enum holding all static prot_bufs... make sure it contains all
829 + * static prot_bufs */
830 +enum PROT_BUF {
831 + PACKET_RAW,
832 + PACKET_ETH,
833 + PACKET_IP,
834 + PACKET_ICMP,
835 + PACKET_TCP,
836 + PACKET_TCP_OPTIONS,
837 + PACKET_UDP,
838 + PACKET_TFTP,
839 + PACKET_DYNAMIC,
840 + PACKET_SENTINEL
841 +};
842 +
843 +/* the luaopen-function of the prot_bufs... make sure it is called
844 + * inside luaopen_controller */
845 +void luaopen_protbuf_raw(lua_State *L);
846 +void luaopen_protbuf_eth(lua_State *L);
847 +void luaopen_protbuf_ip(lua_State *L);
848 +void luaopen_protbuf_icmp(lua_State *L);
849 +void luaopen_protbuf_tcp(lua_State *L);
850 +void luaopen_protbuf_tcp_options(lua_State *L);
851 +void luaopen_protbuf_udp(lua_State *L);
852 +void luaopen_protbuf_tftp(lua_State *L);
853 +void luaopen_protbuf_dynamic(lua_State *L);
854 +
855 +/**********************************************************************/
856 +/* field changes */
857 +/**********************************************************************/
858 +struct field_changes {
859 + int ref_count;
860 + int *field_length_changes;
861 + int *field_offset_changes;
862 +};
863 +
864 +/**********************************************************************/
865 +/* lua packet segment */
866 +/* ------------------ */
867 +/* The struct lua_packet_segment is the integral part of a Lua packet.*/
868 +/* At the very beginning, when a new packet arrives in `lua_tg`_ such */
869 +/* a struct is initialized. The field start then points to the lowest */
870 +/* available header inside the sk_buff structure. During packet */
871 +/* processing the start pointer remains the same, only the offset and */
872 +/* length value change. */
873 +/**********************************************************************/
874 +#define checkpacketseg(L, i, seg_type) \
875 + (lua_packet_segment *)luaL_checkudata(L, i, seg_type)
876 +
877 +typedef struct lua_packet_segment {
878 + unsigned int offset;
879 + unsigned int length;
880 + struct field_changes * changes;
881 + unsigned char * start; /* need to be at the end because of the memory alignment */
882 +} lua_packet_segment;
883 +
884 +/**********************************************************************/
885 +/* protocol field */
886 +/* -------------- */
887 +/* This structure is a container for the field definitions used by the*/
888 +/* protocol buffer. Each protocol field is expressed using this struct*/
889 +/* Have a look at the protocol buffers to see how the struct gets */
890 +/* initialized. */
891 +/* */
892 +/* name: */
893 +/* This member expresses the name of the field, ending */
894 +/* in its own Lua function to access the field. */
895 +/* offset / length: */
896 +/* These members do specify the position inside the protocol header */
897 +/* in bits (not bytes!). */
898 +/* get / set: */
899 +/* The get and set functions take a function pointer pointing to the*/
900 +/* specific getter and setter function for this field. */
901 +/**********************************************************************/
902 +struct protocol_field {
903 + const char * name;
904 + uint32_t offset;
905 + uint32_t length;
906 + lua_CFunction get;
907 + lua_CFunction set;
908 +};
909 +#define PROT_FIELD_SENTINEL { NULL, 0, 0, NULL, NULL }
910 +
911 +
912 +/**********************************************************************/
913 +/* protocol_buf */
914 +/**********************************************************************/
915 +/* This structure is a container for all the information needed for a
916 + * protocol buffer. It gets initialized in each protocol buffer header
917 + * file or for the dynamic protocol buffers on runtime using the
918 + * 'register_dynamic_protocol_buffer' function.
919 + *
920 + * name:
921 + * This member is used throughout the system. It is also exported
922 + * to Lua as a variable name holding the index of the 'supported_protocols'
923 + * array. The name is also used as the name of the generated Lua
924 + * metatable, that is why inside the macro checkpacketseg_ it
925 + * is always the name of a protocol buffer that is passed as the
926 + * second parameter.
927 + * payload_field:
928 + * This member holds the string of the field responsible for payload
929 + * data. The payload field of a protocol has an extra property, since
930 + * it can be used to invoke another protocol buffer that is applied to
931 + * the payload content.
932 + * has_protocol:
933 + * This member is used together with the payload_field. Since we must
934 + * be sure that the payload content does really contain a protocol
935 + * of type X. The function pointed to by has_protocol checks if the
936 + * protocol buffer X can be applied on the payload_data.
937 + * protocol_fields:
938 + * This member points to the array of 'protocol_field' structures
939 + * get_field_changes:
940 + * This member is optional. It is used to return a pointer to an initialized
941 + * field_changes struct. The function is called, whenever the payload field
942 + * is requested with a given protocol type. Usually this function will
943 + * initialize the field_changes struct depending on the content of the
944 + * payload data. e.g.
945 + * tcp = ip:data(packet_tcp)
946 + * such a request will call the 'get_field_changes' function of the tcp
947 + * protocol buffer. This enables, that the tcp options field have the proper
948 + * length as well as the tcp data start at the right offset.
949 + */
950 +struct protocol_buf {
951 + int is_dynamic;
952 + const char * name;
953 + char * payload_field;
954 + int (*has_protocol)(lua_State *L, struct protocol_buf *prot_buf, lua_packet_segment * seg, int type);
955 + struct protocol_field * protocol_fields;
956 + struct field_changes * (*get_field_changes)(lua_State *L, lua_packet_segment * seg);
957 +};
958 +
959 +/**********************************************************************/
960 +/* lua byte array library */
961 +/**********************************************************************/
962 +#define LUA_BYTE_ARRAY "byte_array"
963 +#define checkbytearray(L, i) \
964 + (lua_packet_segment *)luaL_checkudata(L, i, LUA_BYTE_ARRAY)
965 +lua_packet_segment * init_byte_array(lua_State *L, unsigned char * start, int length, int do_copy);
966 +void luaopen_bytearraylib(lua_State *L);
967 +
968 +
969 +/**********************************************************************/
970 +/* lua netfilter environment library */
971 +/**********************************************************************/
972 +#define NETFILTER_LIB "nf"
973 +#if defined(__KERNEL__)
974 + struct lua_env {
975 + lua_State *L;
976 + /* perhaps more to come here (e.g. a state per CPU) */
977 + };
978 + #define LUA_ENV "lua_env"
979 + #define checkluaenv(L, i) \
980 + (struct lua_env *)luaL_checkudata(L, i, LUA_ENV)
981 +
982 + void luaopen_nflib(lua_State *L);
983 +#endif
984 +
985 +void cleanup_dynamic_prot_bufs(void); /* freeing all dynamic prot bufs */
986 +/**********************************************************************/
987 +/* lua protbuf helpers */
988 +/**********************************************************************/
989 +int get_1_bit_generic(lua_State *L);
990 +int set_1_bit_generic(lua_State *L);
991 +int get_lower_4_bit_generic(lua_State *L);
992 +int set_lower_4_bit_generic(lua_State *L);
993 +int get_upper_4_bit_generic(lua_State *L);
994 +int set_upper_4_bit_generic(lua_State *L);
995 +int get_8_bit_generic(lua_State *L);
996 +int set_8_bit_generic(lua_State *L);
997 +int get_16_bit_generic(lua_State *L);
998 +int set_16_bit_generic(lua_State *L);
999 +int get_32_bit_generic(lua_State *L);
1000 +int set_32_bit_generic(lua_State *L);
1001 +int set_data_generic(lua_State *L);
1002 +int get_string_generic(lua_State *L);
1003 +int get_byte_generic_str(lua_State *L);
1004 +struct field_changes * get_allocated_field_changes(lua_State *L, int nr_of_fields);
1005 +
1006 +/* only used by the dynamic prot buf subsystem */
1007 +#define MAX_NR_OF_DYN_PROT_BUFS 16
1008 +int field_dynamic_setter(lua_State *L);
1009 +int field_dynamic_getter(lua_State *L);
1010 +int has_protocol_dynamic(lua_State *L, struct protocol_buf * prot_buf, lua_packet_segment * seg, int type);
1011 +struct field_changes * get_field_changes_dynamic(lua_State *L, struct protocol_buf *prot_buf, lua_packet_segment * seg);
1012 +
1013 +/**********************************************************************/
1014 +/* lua controller API */
1015 +/**********************************************************************/
1016 +void luaopen_controller(lua_State *L);
1017 +struct protocol_buf * get_protocol_buf(unsigned int protocol_id);
1018 +void get_metatable_from_protocol_type(lua_State *L, int type);
1019 +void register_protbuf(lua_State *L, struct protocol_buf * prot_buf, unsigned int protocol_index);
1020 +
1021 +
1022 +#endif /* CONTROLLER_H_ */
1023 --- /dev/null
1024 +++ b/extensions/LUA/Kbuild
1025 @@ -0,0 +1,49 @@
1026 +# -*- Makefile -*-
1027 +
1028 +# Adding debug options
1029 +EXTRA_CFLAGS += -DDEBUG
1030 +
1031 +obj-m += xt_LUA.o
1032 +
1033 +EXTRA_CFLAGS += -I$(src)/prot_buf_new
1034 +xt_LUA-y += xt_LUA_target.o \
1035 +
1036 +xt_LUA-y += nf_lua.o \
1037 + prot_buf_helpers.o \
1038 + byte_array.o \
1039 + controller.o \
1040 + prot_buf_ethernet.o \
1041 + prot_buf_icmp.o \
1042 + prot_buf_ip.o \
1043 + prot_buf_raw.o \
1044 + prot_buf_tcp.o \
1045 + prot_buf_udp.o \
1046 + prot_buf_tftp.o \
1047 + prot_buf_dynamic.o \
1048 +
1049 +
1050 +# Adding Lua Support
1051 +EXTRA_CFLAGS += -I$(src)/lua -I$(src)/lua/include
1052 +xt_LUA-y += lua/lapi.o \
1053 + lua/lbaselib.o \
1054 + lua/lcode.o \
1055 + lua/ldebug.o \
1056 + lua/ldo.o \
1057 + lua/ldump.o \
1058 + lua/lfunc.o \
1059 + lua/lgc.o \
1060 + lua/llex.o \
1061 + lua/lmem.o \
1062 + lua/lobject.o \
1063 + lua/lopcodes.o \
1064 + lua/lparser.o \
1065 + lua/lstate.o \
1066 + lua/lstring.o \
1067 + lua/lstrlib.o \
1068 + lua/ltable.o \
1069 + lua/ltablib.o \
1070 + lua/ltm.o \
1071 + lua/lundump.o \
1072 + lua/lvm.o \
1073 + lua/lzio.o \
1074 + lua/lauxlib.o \
1075 --- /dev/null
1076 +++ b/extensions/LUA/libxt_LUA.c
1077 @@ -0,0 +1,191 @@
1078 +/*
1079 + * Copyright (C) 2010 University of Basel <http://cn.cs.unibas.ch/>
1080 + * by Andre Graf <andre@dergraf.org>
1081 + *
1082 + * This program is free software; you can redistribute it and/or modify
1083 + * it under the terms of the GNU General Public License as published by
1084 + * the Free Software Foundation; either version 2 of the License, or
1085 + * (at your option) any later version.
1086 + *
1087 + * This program is distributed in the hope that it will be useful,
1088 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1089 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1090 + * GNU General Public License for more details.
1091 + *
1092 + * You should have received a copy of the GNU General Public License
1093 + * along with this program; if not, see <http://www.gnu.org/licenses/>.
1094 + */
1095 +
1096 +#include <getopt.h>
1097 +#include <stdio.h>
1098 +#include <stdbool.h>
1099 +#include <stdlib.h>
1100 +#include <string.h>
1101 +#include <xtables.h>
1102 +#include <linux/netfilter.h>
1103 +#include <linux/netfilter/x_tables.h>
1104 +#include "xt_LUA.h"
1105 +
1106 +enum {
1107 + FLAG_SCRIPT = 1 << 0,
1108 + FLAG_STATE = 1 << 1,
1109 + FLAG_FUNCTION = 1 << 2,
1110 +};
1111 +
1112 +static const struct option lua_tg_opts[] = {
1113 + { .name = "script", .has_arg = true, .val = 's' },
1114 + { .name = "state", .has_arg = true, .val = 'l' },
1115 + { .name = "function", .has_arg = true, .val = 'f' },
1116 + { NULL },
1117 +};
1118 +
1119 +
1120 +static void lua_tg_help(void)
1121 +{
1122 + printf(
1123 + "LUA target options:\n"
1124 + " --script SCRIPT Process packet with the Lua script given by SCRIPT\n"
1125 + " \n"
1126 + " --state ID Process packet within the Lua state given by ID.\n"
1127 + " Omitting --state infers the ID 0, which can be\n"
1128 + " refered to the 'global' state.\n"
1129 + " \n"
1130 + " --function FUNCTION Name of the function that processes the Lua packet\n"
1131 + "\n");
1132 +}
1133 +
1134 +static void
1135 +lua_tg_init(struct xt_entry_target *target)
1136 +{
1137 + struct xt_lua_tginfo *info = (void *)target->data;
1138 +
1139 + info->state_id = 0;
1140 + strncpy(info->function, "process_packet\0", sizeof("process_packet\0"));
1141 +}
1142 +
1143 +static int
1144 +lua_tg_parse(int32_t c, char **argv, int32_t invert, uint32_t *flags,
1145 + const void *entry, struct xt_entry_target **target)
1146 +{
1147 + struct xt_lua_tginfo *info = (void *)(*target)->data;
1148 + char buf[MAX_SCRIPT_SIZE];
1149 + long script_size;
1150 + uint32_t state_id;
1151 + FILE *file;
1152 +
1153 + switch (c) {
1154 + case 's':
1155 + if (*flags & FLAG_SCRIPT)
1156 + xtables_error(PARAMETER_PROBLEM,
1157 + "LUA: Cannot specify --script more than once");
1158 +
1159 + if (strlen(optarg) > sizeof(info->filename))
1160 + xtables_error(PARAMETER_PROBLEM,
1161 + "LUA: Maximum script length is %zu",
1162 + sizeof(info->filename));
1163 +
1164 + if (strchr(optarg, '\n'))
1165 + xtables_error(PARAMETER_PROBLEM,
1166 + "LUA: Newlines not allowed in script name");
1167 + file = fopen(optarg, "rb");
1168 + if (file != NULL) {
1169 + fseek(file, 0, SEEK_END);
1170 + script_size = ftell(file);
1171 + if (script_size > MAX_SCRIPT_SIZE)
1172 + xtables_error(PARAMETER_PROBLEM,
1173 + "LUA: The size of the script is too big");
1174 +
1175 + fseek(file, 0, SEEK_SET);
1176 + fread(buf, script_size, 1, file);
1177 + fclose(file);
1178 + } else
1179 + xtables_error(PARAMETER_PROBLEM,
1180 + "LUA: Cannot open script %s", optarg);
1181 +
1182 + strncpy(info->filename, optarg, sizeof(info->filename));
1183 + strncpy(info->buf, buf, sizeof(info->buf));
1184 + info->script_size = script_size;
1185 +
1186 + *flags |= FLAG_SCRIPT;
1187 + return true;
1188 +
1189 + case 'l':
1190 + if (*flags & FLAG_STATE)
1191 + xtables_error(PARAMETER_PROBLEM,
1192 + "LUA: Cannot specify --state more than once");
1193 +
1194 + if (!xtables_strtoui(optarg, NULL, &state_id, 0, 8))
1195 + xtables_error(PARAMETER_PROBLEM,
1196 + "LUA: Invalid --state %s", optarg);
1197 +
1198 + info->state_id = state_id;
1199 + *flags |= FLAG_STATE;
1200 + return true;
1201 +
1202 + case 'f':
1203 + if (*flags & FLAG_FUNCTION)
1204 + xtables_error(PARAMETER_PROBLEM,
1205 + "LUA: Cannot specify --function more than once");
1206 + if (strlen(optarg) > sizeof(info->function))
1207 + xtables_error(PARAMETER_PROBLEM,
1208 + "LUA: Maximum function length is %zu",
1209 + sizeof(info->function));
1210 +
1211 + if (strchr(optarg, '\n'))
1212 + xtables_error(PARAMETER_PROBLEM,
1213 + "LUA: Newlines not allowed in function name");
1214 +
1215 + strncpy(info->function, optarg, sizeof(info->function));
1216 +
1217 + *flags |= FLAG_FUNCTION;
1218 + return true;
1219 + }
1220 +
1221 + return false;
1222 +}
1223 +
1224 +static void
1225 +lua_tg_check(uint32_t flags)
1226 +{
1227 + if (flags == 0)
1228 + xtables_error(PARAMETER_PROBLEM, "LUA: --script parameter required");
1229 +}
1230 +
1231 +static void
1232 +lua_tg_print(const void *entry, const struct xt_entry_target *target,
1233 + int32_t numeric)
1234 +{
1235 + const struct xt_lua_tginfo *info = (const void *)target->data;
1236 +
1237 + printf("LUA script: %s ", info->filename);
1238 +}
1239 +
1240 +static void
1241 +lua_tg_save(const void *entry, const struct xt_entry_target *target)
1242 +{
1243 + const struct xt_lua_tginfo *info = (const void *)target->data;
1244 +
1245 + printf("--script %s ", info->filename);
1246 +}
1247 +
1248 +static struct xtables_target lua_tg_reg = {
1249 + .name = "LUA",
1250 + .version = XTABLES_VERSION,
1251 + .revision = 0,
1252 + .family = NFPROTO_UNSPEC,
1253 + .size = XT_ALIGN(sizeof(struct xt_lua_tginfo)),
1254 + .userspacesize = XT_ALIGN(sizeof(struct xt_lua_tginfo)),
1255 + .help = lua_tg_help,
1256 + .init = lua_tg_init,
1257 + .parse = lua_tg_parse,
1258 + .final_check = lua_tg_check,
1259 + .print = lua_tg_print,
1260 + .save = lua_tg_save,
1261 + .extra_opts = lua_tg_opts,
1262 +};
1263 +
1264 +static __attribute__((constructor)) void lua_tg_ldr(void)
1265 +{
1266 + xtables_register_target(&lua_tg_reg);
1267 +}
1268 +
1269 --- /dev/null
1270 +++ b/extensions/LUA/libxt_LUA.man
1271 @@ -0,0 +1 @@
1272 +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
1273 --- /dev/null
1274 +++ b/extensions/LUA/lua/include/ctype.h
1275 @@ -0,0 +1,11 @@
1276 +#include <linux/ctype.h>
1277 +#undef isalnum
1278 +#define isalnum(c) (((__ismask(c)&(_U|_L|_D)) != 0) && (c > 0))
1279 +#undef isalpha
1280 +#define isalpha(c) (((__ismask(c)&(_U|_L)) != 0) && (c > 0))
1281 +#undef iscntrl
1282 +#define iscntrl(c) (((__ismask(c)&(_C)) != 0) && (c > 0))
1283 +#undef isdigit
1284 +#define isdigit(c) (((__ismask(c)&(_D)) != 0) && (c > 0))
1285 +#undef isspace
1286 +#define isspace(c) (((__ismask(c)&(_S)) != 0) && (c > 0))
1287 --- /dev/null
1288 +++ b/extensions/LUA/lua/include/errno.h
1289 @@ -0,0 +1 @@
1290 +#include <linux/errno.h>
1291 --- /dev/null
1292 +++ b/extensions/LUA/lua/include/locale.h
1293 @@ -0,0 +1,5 @@
1294 +struct lconv {
1295 + char * decimal_point ;
1296 +} ;
1297 +
1298 +#define localeconv() NULL
1299 --- /dev/null
1300 +++ b/extensions/LUA/lua/include/setjmp.h
1301 @@ -0,0 +1,26 @@
1302 +/*
1303 + * arch/um/include/sysdep-i386/archsetjmp.h
1304 + */
1305 +
1306 +#ifndef _KLIBC_ARCHSETJMP_H
1307 +#define _KLIBC_ARCHSETJMP_H
1308 +
1309 +struct __jmp_buf {
1310 + unsigned int __ebx;
1311 + unsigned int __esp;
1312 + unsigned int __ebp;
1313 + unsigned int __esi;
1314 + unsigned int __edi;
1315 + unsigned int __eip;
1316 +};
1317 +
1318 +typedef struct __jmp_buf jmp_buf[1];
1319 +
1320 +#define JB_IP __eip
1321 +#define JB_SP __esp
1322 +
1323 +int setjmp(jmp_buf);
1324 +void longjmp(jmp_buf, int);
1325 +
1326 +#endif /* _SETJMP_H */
1327 +
1328 --- /dev/null
1329 +++ b/extensions/LUA/lua/include/stdio.h
1330 @@ -0,0 +1 @@
1331 +#include <linux/kernel.h>
1332 --- /dev/null
1333 +++ b/extensions/LUA/lua/include/stdlib.h
1334 @@ -0,0 +1,7 @@
1335 +#include <linux/kernel.h>
1336 +
1337 +#define exit(E) return
1338 +#define strtoul simple_strtoul
1339 +#define strcoll strcmp
1340 +
1341 +#define CHAR_BIT 8
1342 --- /dev/null
1343 +++ b/extensions/LUA/lua/include/string.h
1344 @@ -0,0 +1 @@
1345 +#include <linux/string.h>
1346 --- /dev/null
1347 +++ b/extensions/LUA/lua/lapi.c
1348 @@ -0,0 +1,1086 @@
1349 +/*
1350 +** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
1351 +** Lua API
1352 +** See Copyright Notice in lua.h
1353 +*/
1354 +
1355 +#include <stdarg.h>
1356 +#include <math.h>
1357 +#include <assert.h>
1358 +#include <string.h>
1359 +
1360 +#define lapi_c
1361 +#define LUA_CORE
1362 +
1363 +#include "lua.h"
1364 +
1365 +#include "lapi.h"
1366 +#include "ldebug.h"
1367 +#include "ldo.h"
1368 +#include "lfunc.h"
1369 +#include "lgc.h"
1370 +#include "lmem.h"
1371 +#include "lobject.h"
1372 +#include "lstate.h"
1373 +#include "lstring.h"
1374 +#include "ltable.h"
1375 +#include "ltm.h"
1376 +#include "lundump.h"
1377 +#include "lvm.h"
1378 +
1379 +
1380 +
1381 +const char lua_ident[] =
1382 + "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n"
1383 + "$Authors: " LUA_AUTHORS " $\n"
1384 + "$URL: www.lua.org $\n";
1385 +
1386 +
1387 +
1388 +#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
1389 +
1390 +#define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
1391 +
1392 +#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
1393 +
1394 +
1395 +
1396 +static TValue *index2adr (lua_State *L, int idx) {
1397 + if (idx > 0) {
1398 + TValue *o = L->base + (idx - 1);
1399 + api_check(L, idx <= L->ci->top - L->base);
1400 + if (o >= L->top) return cast(TValue *, luaO_nilobject);
1401 + else return o;
1402 + }
1403 + else if (idx > LUA_REGISTRYINDEX) {
1404 + api_check(L, idx != 0 && -idx <= L->top - L->base);
1405 + return L->top + idx;
1406 + }
1407 + else switch (idx) { /* pseudo-indices */
1408 + case LUA_REGISTRYINDEX: return registry(L);
1409 + case LUA_ENVIRONINDEX: {
1410 + Closure *func = curr_func(L);
1411 + sethvalue(L, &L->env, func->c.env);
1412 + return &L->env;
1413 + }
1414 + case LUA_GLOBALSINDEX: return gt(L);
1415 + default: {
1416 + Closure *func = curr_func(L);
1417 + idx = LUA_GLOBALSINDEX - idx;
1418 + return (idx <= func->c.nupvalues)
1419 + ? &func->c.upvalue[idx-1]
1420 + : cast(TValue *, luaO_nilobject);
1421 + }
1422 + }
1423 +}
1424 +
1425 +
1426 +static Table *getcurrenv (lua_State *L) {
1427 + if (L->ci == L->base_ci) /* no enclosing function? */
1428 + return hvalue(gt(L)); /* use global table as environment */
1429 + else {
1430 + Closure *func = curr_func(L);
1431 + return func->c.env;
1432 + }
1433 +}
1434 +
1435 +
1436 +void luaA_pushobject (lua_State *L, const TValue *o) {
1437 + setobj2s(L, L->top, o);
1438 + api_incr_top(L);
1439 +}
1440 +
1441 +
1442 +LUA_API int lua_checkstack (lua_State *L, int size) {
1443 + int res = 1;
1444 + lua_lock(L);
1445 + if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)
1446 + res = 0; /* stack overflow */
1447 + else if (size > 0) {
1448 + luaD_checkstack(L, size);
1449 + if (L->ci->top < L->top + size)
1450 + L->ci->top = L->top + size;
1451 + }
1452 + lua_unlock(L);
1453 + return res;
1454 +}
1455 +
1456 +
1457 +LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
1458 + int i;
1459 + if (from == to) return;
1460 + lua_lock(to);
1461 + api_checknelems(from, n);
1462 + api_check(from, G(from) == G(to));
1463 + api_check(from, to->ci->top - to->top >= n);
1464 + from->top -= n;
1465 + for (i = 0; i < n; i++) {
1466 + setobj2s(to, to->top++, from->top + i);
1467 + }
1468 + lua_unlock(to);
1469 +}
1470 +
1471 +
1472 +LUA_API void lua_setlevel (lua_State *from, lua_State *to) {
1473 + to->nCcalls = from->nCcalls;
1474 +}
1475 +
1476 +
1477 +LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
1478 + lua_CFunction old;
1479 + lua_lock(L);
1480 + old = G(L)->panic;
1481 + G(L)->panic = panicf;
1482 + lua_unlock(L);
1483 + return old;
1484 +}
1485 +
1486 +
1487 +LUA_API lua_State *lua_newthread (lua_State *L) {
1488 + lua_State *L1;
1489 + lua_lock(L);
1490 + luaC_checkGC(L);
1491 + L1 = luaE_newthread(L);
1492 + setthvalue(L, L->top, L1);
1493 + api_incr_top(L);
1494 + lua_unlock(L);
1495 + luai_userstatethread(L, L1);
1496 + return L1;
1497 +}
1498 +
1499 +
1500 +
1501 +/*
1502 +** basic stack manipulation
1503 +*/
1504 +
1505 +
1506 +LUA_API int lua_gettop (lua_State *L) {
1507 + return cast_int(L->top - L->base);
1508 +}
1509 +
1510 +
1511 +LUA_API void lua_settop (lua_State *L, int idx) {
1512 + lua_lock(L);
1513 + if (idx >= 0) {
1514 + api_check(L, idx <= L->stack_last - L->base);
1515 + while (L->top < L->base + idx)
1516 + setnilvalue(L->top++);
1517 + L->top = L->base + idx;
1518 + }
1519 + else {
1520 + api_check(L, -(idx+1) <= (L->top - L->base));
1521 + L->top += idx+1; /* `subtract' index (index is negative) */
1522 + }
1523 + lua_unlock(L);
1524 +}
1525 +
1526 +
1527 +LUA_API void lua_remove (lua_State *L, int idx) {
1528 + StkId p;
1529 + lua_lock(L);
1530 + p = index2adr(L, idx);
1531 + api_checkvalidindex(L, p);
1532 + while (++p < L->top) setobjs2s(L, p-1, p);
1533 + L->top--;
1534 + lua_unlock(L);
1535 +}
1536 +
1537 +
1538 +LUA_API void lua_insert (lua_State *L, int idx) {
1539 + StkId p;
1540 + StkId q;
1541 + lua_lock(L);
1542 + p = index2adr(L, idx);
1543 + api_checkvalidindex(L, p);
1544 + for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
1545 + setobjs2s(L, p, L->top);
1546 + lua_unlock(L);
1547 +}
1548 +
1549 +
1550 +LUA_API void lua_replace (lua_State *L, int idx) {
1551 + StkId o;
1552 + lua_lock(L);
1553 + /* explicit test for incompatible code */
1554 + if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
1555 + luaG_runerror(L, "no calling environment");
1556 + api_checknelems(L, 1);
1557 + o = index2adr(L, idx);
1558 + api_checkvalidindex(L, o);
1559 + if (idx == LUA_ENVIRONINDEX) {
1560 + Closure *func = curr_func(L);
1561 + api_check(L, ttistable(L->top - 1));
1562 + func->c.env = hvalue(L->top - 1);
1563 + luaC_barrier(L, func, L->top - 1);
1564 + }
1565 + else {
1566 + setobj(L, o, L->top - 1);
1567 + if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
1568 + luaC_barrier(L, curr_func(L), L->top - 1);
1569 + }
1570 + L->top--;
1571 + lua_unlock(L);
1572 +}
1573 +
1574 +
1575 +LUA_API void lua_pushvalue (lua_State *L, int idx) {
1576 + lua_lock(L);
1577 + setobj2s(L, L->top, index2adr(L, idx));
1578 + api_incr_top(L);
1579 + lua_unlock(L);
1580 +}
1581 +
1582 +
1583 +
1584 +/*
1585 +** access functions (stack -> C)
1586 +*/
1587 +
1588 +
1589 +LUA_API int lua_type (lua_State *L, int idx) {
1590 + StkId o = index2adr(L, idx);
1591 + return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
1592 +}
1593 +
1594 +
1595 +LUA_API const char *lua_typename (lua_State *L, int t) {
1596 + UNUSED(L);
1597 + return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
1598 +}
1599 +
1600 +
1601 +LUA_API int lua_iscfunction (lua_State *L, int idx) {
1602 + StkId o = index2adr(L, idx);
1603 + return iscfunction(o);
1604 +}
1605 +
1606 +
1607 +LUA_API int lua_isnumber (lua_State *L, int idx) {
1608 + TValue n;
1609 + const TValue *o = index2adr(L, idx);
1610 + return tonumber(o, &n);
1611 +}
1612 +
1613 +
1614 +LUA_API int lua_isstring (lua_State *L, int idx) {
1615 + int t = lua_type(L, idx);
1616 + return (t == LUA_TSTRING || t == LUA_TNUMBER);
1617 +}
1618 +
1619 +
1620 +LUA_API int lua_isuserdata (lua_State *L, int idx) {
1621 + const TValue *o = index2adr(L, idx);
1622 + return (ttisuserdata(o) || ttislightuserdata(o));
1623 +}
1624 +
1625 +
1626 +LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
1627 + StkId o1 = index2adr(L, index1);
1628 + StkId o2 = index2adr(L, index2);
1629 + return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
1630 + : luaO_rawequalObj(o1, o2);
1631 +}
1632 +
1633 +
1634 +LUA_API int lua_equal (lua_State *L, int index1, int index2) {
1635 + StkId o1, o2;
1636 + int i;
1637 + lua_lock(L); /* may call tag method */
1638 + o1 = index2adr(L, index1);
1639 + o2 = index2adr(L, index2);
1640 + i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
1641 + lua_unlock(L);
1642 + return i;
1643 +}
1644 +
1645 +
1646 +LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
1647 + StkId o1, o2;
1648 + int i;
1649 + lua_lock(L); /* may call tag method */
1650 + o1 = index2adr(L, index1);
1651 + o2 = index2adr(L, index2);
1652 + i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
1653 + : luaV_lessthan(L, o1, o2);
1654 + lua_unlock(L);
1655 + return i;
1656 +}
1657 +
1658 +
1659 +
1660 +LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
1661 + TValue n;
1662 + const TValue *o = index2adr(L, idx);
1663 + if (tonumber(o, &n))
1664 + return nvalue(o);
1665 + else
1666 + return 0;
1667 +}
1668 +
1669 +
1670 +LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
1671 + TValue n;
1672 + const TValue *o = index2adr(L, idx);
1673 + if (tonumber(o, &n)) {
1674 + lua_Integer res;
1675 + lua_Number num = nvalue(o);
1676 + lua_number2integer(res, num);
1677 + return res;
1678 + }
1679 + else
1680 + return 0;
1681 +}
1682 +
1683 +
1684 +LUA_API int lua_toboolean (lua_State *L, int idx) {
1685 + const TValue *o = index2adr(L, idx);
1686 + return !l_isfalse(o);
1687 +}
1688 +
1689 +
1690 +LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
1691 + StkId o = index2adr(L, idx);
1692 + if (!ttisstring(o)) {
1693 + lua_lock(L); /* `luaV_tostring' may create a new string */
1694 + if (!luaV_tostring(L, o)) { /* conversion failed? */
1695 + if (len != NULL) *len = 0;
1696 + lua_unlock(L);
1697 + return NULL;
1698 + }
1699 + luaC_checkGC(L);
1700 + o = index2adr(L, idx); /* previous call may reallocate the stack */
1701 + lua_unlock(L);
1702 + }
1703 + if (len != NULL) *len = tsvalue(o)->len;
1704 + return svalue(o);
1705 +}
1706 +
1707 +
1708 +LUA_API size_t lua_objlen (lua_State *L, int idx) {
1709 + StkId o = index2adr(L, idx);
1710 + switch (ttype(o)) {
1711 + case LUA_TSTRING: return tsvalue(o)->len;
1712 + case LUA_TUSERDATA: return uvalue(o)->len;
1713 + case LUA_TTABLE: return luaH_getn(hvalue(o));
1714 + case LUA_TNUMBER: {
1715 + size_t l;
1716 + lua_lock(L); /* `luaV_tostring' may create a new string */
1717 + l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
1718 + lua_unlock(L);
1719 + return l;
1720 + }
1721 + default: return 0;
1722 + }
1723 +}
1724 +
1725 +
1726 +LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
1727 + StkId o = index2adr(L, idx);
1728 + return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
1729 +}
1730 +
1731 +
1732 +LUA_API void *lua_touserdata (lua_State *L, int idx) {
1733 + StkId o = index2adr(L, idx);
1734 + switch (ttype(o)) {
1735 + case LUA_TUSERDATA: return (rawuvalue(o) + 1);
1736 + case LUA_TLIGHTUSERDATA: return pvalue(o);
1737 + default: return NULL;
1738 + }
1739 +}
1740 +
1741 +
1742 +LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
1743 + StkId o = index2adr(L, idx);
1744 + return (!ttisthread(o)) ? NULL : thvalue(o);
1745 +}
1746 +
1747 +
1748 +LUA_API const void *lua_topointer (lua_State *L, int idx) {
1749 + StkId o = index2adr(L, idx);
1750 + switch (ttype(o)) {
1751 + case LUA_TTABLE: return hvalue(o);
1752 + case LUA_TFUNCTION: return clvalue(o);
1753 + case LUA_TTHREAD: return thvalue(o);
1754 + case LUA_TUSERDATA:
1755 + case LUA_TLIGHTUSERDATA:
1756 + return lua_touserdata(L, idx);
1757 + default: return NULL;
1758 + }
1759 +}
1760 +
1761 +
1762 +
1763 +/*
1764 +** push functions (C -> stack)
1765 +*/
1766 +
1767 +
1768 +LUA_API void lua_pushnil (lua_State *L) {
1769 + lua_lock(L);
1770 + setnilvalue(L->top);
1771 + api_incr_top(L);
1772 + lua_unlock(L);
1773 +}
1774 +
1775 +
1776 +LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
1777 + lua_lock(L);
1778 + setnvalue(L->top, n);
1779 + api_incr_top(L);
1780 + lua_unlock(L);
1781 +}
1782 +
1783 +
1784 +LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
1785 + lua_lock(L);
1786 + setnvalue(L->top, cast_num(n));
1787 + api_incr_top(L);
1788 + lua_unlock(L);
1789 +}
1790 +
1791 +
1792 +LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
1793 + lua_lock(L);
1794 + luaC_checkGC(L);
1795 + setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
1796 + api_incr_top(L);
1797 + lua_unlock(L);
1798 +}
1799 +
1800 +
1801 +LUA_API void lua_pushstring (lua_State *L, const char *s) {
1802 + if (s == NULL)
1803 + lua_pushnil(L);
1804 + else
1805 + lua_pushlstring(L, s, strlen(s));
1806 +}
1807 +
1808 +
1809 +LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
1810 + va_list argp) {
1811 + const char *ret;
1812 + lua_lock(L);
1813 + luaC_checkGC(L);
1814 + ret = luaO_pushvfstring(L, fmt, argp);
1815 + lua_unlock(L);
1816 + return ret;
1817 +}
1818 +
1819 +
1820 +LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
1821 + const char *ret;
1822 + va_list argp;
1823 + lua_lock(L);
1824 + luaC_checkGC(L);
1825 + va_start(argp, fmt);
1826 + ret = luaO_pushvfstring(L, fmt, argp);
1827 + va_end(argp);
1828 + lua_unlock(L);
1829 + return ret;
1830 +}
1831 +
1832 +
1833 +LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
1834 + Closure *cl;
1835 + lua_lock(L);
1836 + luaC_checkGC(L);
1837 + api_checknelems(L, n);
1838 + cl = luaF_newCclosure(L, n, getcurrenv(L));
1839 + cl->c.f = fn;
1840 + L->top -= n;
1841 + while (n--)
1842 + setobj2n(L, &cl->c.upvalue[n], L->top+n);
1843 + setclvalue(L, L->top, cl);
1844 + lua_assert(iswhite(obj2gco(cl)));
1845 + api_incr_top(L);
1846 + lua_unlock(L);
1847 +}
1848 +
1849 +
1850 +LUA_API void lua_pushboolean (lua_State *L, int b) {
1851 + lua_lock(L);
1852 + setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
1853 + api_incr_top(L);
1854 + lua_unlock(L);
1855 +}
1856 +
1857 +
1858 +LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
1859 + lua_lock(L);
1860 + setpvalue(L->top, p);
1861 + api_incr_top(L);
1862 + lua_unlock(L);
1863 +}
1864 +
1865 +
1866 +LUA_API int lua_pushthread (lua_State *L) {
1867 + lua_lock(L);
1868 + setthvalue(L, L->top, L);
1869 + api_incr_top(L);
1870 + lua_unlock(L);
1871 + return (G(L)->mainthread == L);
1872 +}
1873 +
1874 +
1875 +
1876 +/*
1877 +** get functions (Lua -> stack)
1878 +*/
1879 +
1880 +
1881 +LUA_API void lua_gettable (lua_State *L, int idx) {
1882 + StkId t;
1883 + lua_lock(L);
1884 + t = index2adr(L, idx);
1885 + api_checkvalidindex(L, t);
1886 + luaV_gettable(L, t, L->top - 1, L->top - 1);
1887 + lua_unlock(L);
1888 +}
1889 +
1890 +
1891 +LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
1892 + StkId t;
1893 + TValue key;
1894 + lua_lock(L);
1895 + t = index2adr(L, idx);
1896 + api_checkvalidindex(L, t);
1897 + setsvalue(L, &key, luaS_new(L, k));
1898 + luaV_gettable(L, t, &key, L->top);
1899 + api_incr_top(L);
1900 + lua_unlock(L);
1901 +}
1902 +
1903 +
1904 +LUA_API void lua_rawget (lua_State *L, int idx) {
1905 + StkId t;
1906 + lua_lock(L);
1907 + t = index2adr(L, idx);
1908 + api_check(L, ttistable(t));
1909 + setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
1910 + lua_unlock(L);
1911 +}
1912 +
1913 +
1914 +LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
1915 + StkId o;
1916 + lua_lock(L);
1917 + o = index2adr(L, idx);
1918 + api_check(L, ttistable(o));
1919 + setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
1920 + api_incr_top(L);
1921 + lua_unlock(L);
1922 +}
1923 +
1924 +
1925 +LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
1926 + lua_lock(L);
1927 + luaC_checkGC(L);
1928 + sethvalue(L, L->top, luaH_new(L, narray, nrec));
1929 + api_incr_top(L);
1930 + lua_unlock(L);
1931 +}
1932 +
1933 +
1934 +LUA_API int lua_getmetatable (lua_State *L, int objindex) {
1935 + const TValue *obj;
1936 + Table *mt = NULL;
1937 + int res;
1938 + lua_lock(L);
1939 + obj = index2adr(L, objindex);
1940 + switch (ttype(obj)) {
1941 + case LUA_TTABLE:
1942 + mt = hvalue(obj)->metatable;
1943 + break;
1944 + case LUA_TUSERDATA:
1945 + mt = uvalue(obj)->metatable;
1946 + break;
1947 + default:
1948 + mt = G(L)->mt[ttype(obj)];
1949 + break;
1950 + }
1951 + if (mt == NULL)
1952 + res = 0;
1953 + else {
1954 + sethvalue(L, L->top, mt);
1955 + api_incr_top(L);
1956 + res = 1;
1957 + }
1958 + lua_unlock(L);
1959 + return res;
1960 +}
1961 +
1962 +
1963 +LUA_API void lua_getfenv (lua_State *L, int idx) {
1964 + StkId o;
1965 + lua_lock(L);
1966 + o = index2adr(L, idx);
1967 + api_checkvalidindex(L, o);
1968 + switch (ttype(o)) {
1969 + case LUA_TFUNCTION:
1970 + sethvalue(L, L->top, clvalue(o)->c.env);
1971 + break;
1972 + case LUA_TUSERDATA:
1973 + sethvalue(L, L->top, uvalue(o)->env);
1974 + break;
1975 + case LUA_TTHREAD:
1976 + setobj2s(L, L->top, gt(thvalue(o)));
1977 + break;
1978 + default:
1979 + setnilvalue(L->top);
1980 + break;
1981 + }
1982 + api_incr_top(L);
1983 + lua_unlock(L);
1984 +}
1985 +
1986 +
1987 +/*
1988 +** set functions (stack -> Lua)
1989 +*/
1990 +
1991 +
1992 +LUA_API void lua_settable (lua_State *L, int idx) {
1993 + StkId t;
1994 + lua_lock(L);
1995 + api_checknelems(L, 2);
1996 + t = index2adr(L, idx);
1997 + api_checkvalidindex(L, t);
1998 + luaV_settable(L, t, L->top - 2, L->top - 1);
1999 + L->top -= 2; /* pop index and value */
2000 + lua_unlock(L);
2001 +}
2002 +
2003 +
2004 +LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
2005 + StkId t;
2006 + TValue key;
2007 + lua_lock(L);
2008 + api_checknelems(L, 1);
2009 + t = index2adr(L, idx);
2010 + api_checkvalidindex(L, t);
2011 + setsvalue(L, &key, luaS_new(L, k));
2012 + luaV_settable(L, t, &key, L->top - 1);
2013 + L->top--; /* pop value */
2014 + lua_unlock(L);
2015 +}
2016 +
2017 +
2018 +LUA_API void lua_rawset (lua_State *L, int idx) {
2019 + StkId t;
2020 + lua_lock(L);
2021 + api_checknelems(L, 2);
2022 + t = index2adr(L, idx);
2023 + api_check(L, ttistable(t));
2024 + setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
2025 + luaC_barriert(L, hvalue(t), L->top-1);
2026 + L->top -= 2;
2027 + lua_unlock(L);
2028 +}
2029 +
2030 +
2031 +LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
2032 + StkId o;
2033 + lua_lock(L);
2034 + api_checknelems(L, 1);
2035 + o = index2adr(L, idx);
2036 + api_check(L, ttistable(o));
2037 + setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
2038 + luaC_barriert(L, hvalue(o), L->top-1);
2039 + L->top--;
2040 + lua_unlock(L);
2041 +}
2042 +
2043 +
2044 +LUA_API int lua_setmetatable (lua_State *L, int objindex) {
2045 + TValue *obj;
2046 + Table *mt;
2047 + lua_lock(L);
2048 + api_checknelems(L, 1);
2049 + obj = index2adr(L, objindex);
2050 + api_checkvalidindex(L, obj);
2051 + if (ttisnil(L->top - 1))
2052 + mt = NULL;
2053 + else {
2054 + api_check(L, ttistable(L->top - 1));
2055 + mt = hvalue(L->top - 1);
2056 + }
2057 + switch (ttype(obj)) {
2058 + case LUA_TTABLE: {
2059 + hvalue(obj)->metatable = mt;
2060 + if (mt)
2061 + luaC_objbarriert(L, hvalue(obj), mt);
2062 + break;
2063 + }
2064 + case LUA_TUSERDATA: {
2065 + uvalue(obj)->metatable = mt;
2066 + if (mt)
2067 + luaC_objbarrier(L, rawuvalue(obj), mt);
2068 + break;
2069 + }
2070 + default: {
2071 + G(L)->mt[ttype(obj)] = mt;
2072 + break;
2073 + }
2074 + }
2075 + L->top--;
2076 + lua_unlock(L);
2077 + return 1;
2078 +}
2079 +
2080 +
2081 +LUA_API int lua_setfenv (lua_State *L, int idx) {
2082 + StkId o;
2083 + int res = 1;
2084 + lua_lock(L);
2085 + api_checknelems(L, 1);
2086 + o = index2adr(L, idx);
2087 + api_checkvalidindex(L, o);
2088 + api_check(L, ttistable(L->top - 1));
2089 + switch (ttype(o)) {
2090 + case LUA_TFUNCTION:
2091 + clvalue(o)->c.env = hvalue(L->top - 1);
2092 + break;
2093 + case LUA_TUSERDATA:
2094 + uvalue(o)->env = hvalue(L->top - 1);
2095 + break;
2096 + case LUA_TTHREAD:
2097 + sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
2098 + break;
2099 + default:
2100 + res = 0;
2101 + break;
2102 + }
2103 + if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
2104 + L->top--;
2105 + lua_unlock(L);
2106 + return res;
2107 +}
2108 +
2109 +
2110 +/*
2111 +** `load' and `call' functions (run Lua code)
2112 +*/
2113 +
2114 +
2115 +#define adjustresults(L,nres) \
2116 + { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
2117 +
2118 +
2119 +#define checkresults(L,na,nr) \
2120 + api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
2121 +
2122 +
2123 +LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
2124 + StkId func;
2125 + lua_lock(L);
2126 + api_checknelems(L, nargs+1);
2127 + checkresults(L, nargs, nresults);
2128 + func = L->top - (nargs+1);
2129 + luaD_call(L, func, nresults);
2130 + adjustresults(L, nresults);
2131 + lua_unlock(L);
2132 +}
2133 +
2134 +
2135 +
2136 +/*
2137 +** Execute a protected call.
2138 +*/
2139 +struct CallS { /* data to `f_call' */
2140 + StkId func;
2141 + int nresults;
2142 +};
2143 +
2144 +
2145 +static void f_call (lua_State *L, void *ud) {
2146 + struct CallS *c = cast(struct CallS *, ud);
2147 + luaD_call(L, c->func, c->nresults);
2148 +}
2149 +
2150 +
2151 +
2152 +LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
2153 + struct CallS c;
2154 + int status;
2155 + ptrdiff_t func;
2156 + lua_lock(L);
2157 + api_checknelems(L, nargs+1);
2158 + checkresults(L, nargs, nresults);
2159 + if (errfunc == 0)
2160 + func = 0;
2161 + else {
2162 + StkId o = index2adr(L, errfunc);
2163 + api_checkvalidindex(L, o);
2164 + func = savestack(L, o);
2165 + }
2166 + c.func = L->top - (nargs+1); /* function to be called */
2167 + c.nresults = nresults;
2168 + status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
2169 + adjustresults(L, nresults);
2170 + lua_unlock(L);
2171 + return status;
2172 +}
2173 +
2174 +
2175 +/*
2176 +** Execute a protected C call.
2177 +*/
2178 +struct CCallS { /* data to `f_Ccall' */
2179 + lua_CFunction func;
2180 + void *ud;
2181 +};
2182 +
2183 +
2184 +static void f_Ccall (lua_State *L, void *ud) {
2185 + struct CCallS *c = cast(struct CCallS *, ud);
2186 + Closure *cl;
2187 + cl = luaF_newCclosure(L, 0, getcurrenv(L));
2188 + cl->c.f = c->func;
2189 + setclvalue(L, L->top, cl); /* push function */
2190 + api_incr_top(L);
2191 + setpvalue(L->top, c->ud); /* push only argument */
2192 + api_incr_top(L);
2193 + luaD_call(L, L->top - 2, 0);
2194 +}
2195 +
2196 +
2197 +LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
2198 + struct CCallS c;
2199 + int status;
2200 + lua_lock(L);
2201 + c.func = func;
2202 + c.ud = ud;
2203 + status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
2204 + lua_unlock(L);
2205 + return status;
2206 +}
2207 +
2208 +
2209 +LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
2210 + const char *chunkname) {
2211 + ZIO z;
2212 + int status;
2213 + lua_lock(L);
2214 + if (!chunkname) chunkname = "?";
2215 + luaZ_init(L, &z, reader, data);
2216 + status = luaD_protectedparser(L, &z, chunkname);
2217 + lua_unlock(L);
2218 + return status;
2219 +}
2220 +
2221 +
2222 +LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
2223 + int status;
2224 + TValue *o;
2225 + lua_lock(L);
2226 + api_checknelems(L, 1);
2227 + o = L->top - 1;
2228 + if (isLfunction(o))
2229 + status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
2230 + else
2231 + status = 1;
2232 + lua_unlock(L);
2233 + return status;
2234 +}
2235 +
2236 +
2237 +LUA_API int lua_status (lua_State *L) {
2238 + return L->status;
2239 +}
2240 +
2241 +
2242 +/*
2243 +** Garbage-collection function
2244 +*/
2245 +
2246 +LUA_API int lua_gc (lua_State *L, int what, int data) {
2247 + int res = 0;
2248 + global_State *g;
2249 + lua_lock(L);
2250 + g = G(L);
2251 + switch (what) {
2252 + case LUA_GCSTOP: {
2253 + g->GCthreshold = MAX_LUMEM;
2254 + break;
2255 + }
2256 + case LUA_GCRESTART: {
2257 + g->GCthreshold = g->totalbytes;
2258 + break;
2259 + }
2260 + case LUA_GCCOLLECT: {
2261 + luaC_fullgc(L);
2262 + break;
2263 + }
2264 + case LUA_GCCOUNT: {
2265 + /* GC values are expressed in Kbytes: #bytes/2^10 */
2266 + res = cast_int(g->totalbytes >> 10);
2267 + break;
2268 + }
2269 + case LUA_GCCOUNTB: {
2270 + res = cast_int(g->totalbytes & 0x3ff);
2271 + break;
2272 + }
2273 + case LUA_GCSTEP: {
2274 + lu_mem a = (cast(lu_mem, data) << 10);
2275 + if (a <= g->totalbytes)
2276 + g->GCthreshold = g->totalbytes - a;
2277 + else
2278 + g->GCthreshold = 0;
2279 + while (g->GCthreshold <= g->totalbytes) {
2280 + luaC_step(L);
2281 + if (g->gcstate == GCSpause) { /* end of cycle? */
2282 + res = 1; /* signal it */
2283 + break;
2284 + }
2285 + }
2286 + break;
2287 + }
2288 + case LUA_GCSETPAUSE: {
2289 + res = g->gcpause;
2290 + g->gcpause = data;
2291 + break;
2292 + }
2293 + case LUA_GCSETSTEPMUL: {
2294 + res = g->gcstepmul;
2295 + g->gcstepmul = data;
2296 + break;
2297 + }
2298 + default: res = -1; /* invalid option */
2299 + }
2300 + lua_unlock(L);
2301 + return res;
2302 +}
2303 +
2304 +
2305 +
2306 +/*
2307 +** miscellaneous functions
2308 +*/
2309 +
2310 +
2311 +LUA_API int lua_error (lua_State *L) {
2312 + lua_lock(L);
2313 + api_checknelems(L, 1);
2314 + luaG_errormsg(L);
2315 + lua_unlock(L);
2316 + return 0; /* to avoid warnings */
2317 +}
2318 +
2319 +
2320 +LUA_API int lua_next (lua_State *L, int idx) {
2321 + StkId t;
2322 + int more;
2323 + lua_lock(L);
2324 + t = index2adr(L, idx);
2325 + api_check(L, ttistable(t));
2326 + more = luaH_next(L, hvalue(t), L->top - 1);
2327 + if (more) {
2328 + api_incr_top(L);
2329 + }
2330 + else /* no more elements */
2331 + L->top -= 1; /* remove key */
2332 + lua_unlock(L);
2333 + return more;
2334 +}
2335 +
2336 +
2337 +LUA_API void lua_concat (lua_State *L, int n) {
2338 + lua_lock(L);
2339 + api_checknelems(L, n);
2340 + if (n >= 2) {
2341 + luaC_checkGC(L);
2342 + luaV_concat(L, n, cast_int(L->top - L->base) - 1);
2343 + L->top -= (n-1);
2344 + }
2345 + else if (n == 0) { /* push empty string */
2346 + setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
2347 + api_incr_top(L);
2348 + }
2349 + /* else n == 1; nothing to do */
2350 + lua_unlock(L);
2351 +}
2352 +
2353 +
2354 +LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
2355 + lua_Alloc f;
2356 + lua_lock(L);
2357 + if (ud) *ud = G(L)->ud;
2358 + f = G(L)->frealloc;
2359 + lua_unlock(L);
2360 + return f;
2361 +}
2362 +
2363 +
2364 +LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
2365 + lua_lock(L);
2366 + G(L)->ud = ud;
2367 + G(L)->frealloc = f;
2368 + lua_unlock(L);
2369 +}
2370 +
2371 +
2372 +LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
2373 + Udata *u;
2374 + lua_lock(L);
2375 + luaC_checkGC(L);
2376 + u = luaS_newudata(L, size, getcurrenv(L));
2377 + setuvalue(L, L->top, u);
2378 + api_incr_top(L);
2379 + lua_unlock(L);
2380 + return u + 1;
2381 +}
2382 +
2383 +
2384 +
2385 +
2386 +static const char *aux_upvalue (StkId fi, int n, TValue **val) {
2387 + Closure *f;
2388 + if (!ttisfunction(fi)) return NULL;
2389 + f = clvalue(fi);
2390 + if (f->c.isC) {
2391 + if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
2392 + *val = &f->c.upvalue[n-1];
2393 + return "";
2394 + }
2395 + else {
2396 + Proto *p = f->l.p;
2397 + if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
2398 + *val = f->l.upvals[n-1]->v;
2399 + return getstr(p->upvalues[n-1]);
2400 + }
2401 +}
2402 +
2403 +
2404 +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
2405 + const char *name;
2406 + TValue *val;
2407 + lua_lock(L);
2408 + name = aux_upvalue(index2adr(L, funcindex), n, &val);
2409 + if (name) {
2410 + setobj2s(L, L->top, val);
2411 + api_incr_top(L);
2412 + }
2413 + lua_unlock(L);
2414 + return name;
2415 +}
2416 +
2417 +
2418 +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
2419 + const char *name;
2420 + TValue *val;
2421 + StkId fi;
2422 + lua_lock(L);
2423 + fi = index2adr(L, funcindex);
2424 + api_checknelems(L, 1);
2425 + name = aux_upvalue(fi, n, &val);
2426 + if (name) {
2427 + L->top--;
2428 + setobj(L, val, L->top);
2429 + luaC_barrier(L, clvalue(fi), L->top);
2430 + }
2431 + lua_unlock(L);
2432 + return name;
2433 +}
2434 +
2435 --- /dev/null
2436 +++ b/extensions/LUA/lua/lapi.h
2437 @@ -0,0 +1,16 @@
2438 +/*
2439 +** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $
2440 +** Auxiliary functions from Lua API
2441 +** See Copyright Notice in lua.h
2442 +*/
2443 +
2444 +#ifndef lapi_h
2445 +#define lapi_h
2446 +
2447 +
2448 +#include "lobject.h"
2449 +
2450 +
2451 +LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o);
2452 +
2453 +#endif
2454 --- /dev/null
2455 +++ b/extensions/LUA/lua/lauxlib.c
2456 @@ -0,0 +1,674 @@
2457 +/*
2458 +** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $
2459 +** Auxiliary functions for building Lua libraries
2460 +** See Copyright Notice in lua.h
2461 +*/
2462 +
2463 +#include <stdarg.h>
2464 +
2465 +#if !defined(__KERNEL__)
2466 +#include <ctype.h>
2467 +#include <errno.h>
2468 +#include <stdio.h>
2469 +#include <stdlib.h>
2470 +#include <string.h>
2471 +#else
2472 +#include <linux/ctype.h>
2473 +#include <linux/errno.h>
2474 +#include <linux/kernel.h>
2475 +#include <linux/slab.h>
2476 +#include <linux/string.h>
2477 +#endif
2478 +
2479 +/* This file uses only the official API of Lua.
2480 +** Any function declared here could be written as an application function.
2481 +*/
2482 +
2483 +#define lauxlib_c
2484 +#define LUA_LIB
2485 +
2486 +#include "lua.h"
2487 +
2488 +#include "lauxlib.h"
2489 +
2490 +
2491 +#define FREELIST_REF 0 /* free list of references */
2492 +
2493 +
2494 +/* convert a stack index to positive */
2495 +#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
2496 + lua_gettop(L) + (i) + 1)
2497 +
2498 +
2499 +/*
2500 +** {======================================================
2501 +** Error-report functions
2502 +** =======================================================
2503 +*/
2504 +
2505 +
2506 +LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
2507 + lua_Debug ar;
2508 + if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
2509 + return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
2510 + lua_getinfo(L, "n", &ar);
2511 + if (strcmp(ar.namewhat, "method") == 0) {
2512 + narg--; /* do not count `self' */
2513 + if (narg == 0) /* error is in the self argument itself? */
2514 + return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
2515 + ar.name, extramsg);
2516 + }
2517 + if (ar.name == NULL)
2518 + ar.name = "?";
2519 + return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
2520 + narg, ar.name, extramsg);
2521 +}
2522 +
2523 +
2524 +LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
2525 + const char *msg = lua_pushfstring(L, "%s expected, got %s",
2526 + tname, luaL_typename(L, narg));
2527 + return luaL_argerror(L, narg, msg);
2528 +}
2529 +
2530 +
2531 +static void tag_error (lua_State *L, int narg, int tag) {
2532 + luaL_typerror(L, narg, lua_typename(L, tag));
2533 +}
2534 +
2535 +
2536 +LUALIB_API void luaL_where (lua_State *L, int level) {
2537 + lua_Debug ar;
2538 + if (lua_getstack(L, level, &ar)) { /* check function at level */
2539 + lua_getinfo(L, "Sl", &ar); /* get info about it */
2540 + if (ar.currentline > 0) { /* is there info? */
2541 + lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
2542 + return;
2543 + }
2544 + }
2545 + lua_pushliteral(L, ""); /* else, no information available... */
2546 +}
2547 +
2548 +
2549 +LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
2550 + va_list argp;
2551 + va_start(argp, fmt);
2552 + luaL_where(L, 1);
2553 + lua_pushvfstring(L, fmt, argp);
2554 + va_end(argp);
2555 + lua_concat(L, 2);
2556 + return lua_error(L);
2557 +}
2558 +
2559 +/* }====================================================== */
2560 +
2561 +
2562 +LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
2563 + const char *const lst[]) {
2564 + const char *name = (def) ? luaL_optstring(L, narg, def) :
2565 + luaL_checkstring(L, narg);
2566 + int i;
2567 + for (i=0; lst[i]; i++)
2568 + if (strcmp(lst[i], name) == 0)
2569 + return i;
2570 + return luaL_argerror(L, narg,
2571 + lua_pushfstring(L, "invalid option " LUA_QS, name));
2572 +}
2573 +
2574 +
2575 +LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
2576 + lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
2577 + if (!lua_isnil(L, -1)) /* name already in use? */
2578 + return 0; /* leave previous value on top, but return 0 */
2579 + lua_pop(L, 1);
2580 + lua_newtable(L); /* create metatable */
2581 + lua_pushvalue(L, -1);
2582 + lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
2583 + return 1;
2584 +}
2585 +
2586 +
2587 +LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
2588 + void *p = lua_touserdata(L, ud);
2589 + if (p != NULL) { /* value is a userdata? */
2590 + if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
2591 + lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
2592 + if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
2593 + lua_pop(L, 2); /* remove both metatables */
2594 + return p;
2595 + }
2596 + }
2597 + }
2598 + luaL_typerror(L, ud, tname); /* else error */
2599 + return NULL; /* to avoid warnings */
2600 +}
2601 +
2602 +
2603 +LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
2604 + if (!lua_checkstack(L, space))
2605 + luaL_error(L, "stack overflow (%s)", mes);
2606 +}
2607 +
2608 +
2609 +LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
2610 + if (lua_type(L, narg) != t)
2611 + tag_error(L, narg, t);
2612 +}
2613 +
2614 +
2615 +LUALIB_API void luaL_checkany (lua_State *L, int narg) {
2616 + if (lua_type(L, narg) == LUA_TNONE)
2617 + luaL_argerror(L, narg, "value expected");
2618 +}
2619 +
2620 +
2621 +LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
2622 + const char *s = lua_tolstring(L, narg, len);
2623 + if (!s) tag_error(L, narg, LUA_TSTRING);
2624 + return s;
2625 +}
2626 +
2627 +
2628 +LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
2629 + const char *def, size_t *len) {
2630 + if (lua_isnoneornil(L, narg)) {
2631 + if (len)
2632 + *len = (def ? strlen(def) : 0);
2633 + return def;
2634 + }
2635 + else return luaL_checklstring(L, narg, len);
2636 +}
2637 +
2638 +
2639 +LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
2640 + lua_Number d = lua_tonumber(L, narg);
2641 + if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
2642 + tag_error(L, narg, LUA_TNUMBER);
2643 + return d;
2644 +}
2645 +
2646 +
2647 +LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
2648 + return luaL_opt(L, luaL_checknumber, narg, def);
2649 +}
2650 +
2651 +
2652 +LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
2653 + lua_Integer d = lua_tointeger(L, narg);
2654 + if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
2655 + tag_error(L, narg, LUA_TNUMBER);
2656 + return d;
2657 +}
2658 +
2659 +
2660 +LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
2661 + lua_Integer def) {
2662 + return luaL_opt(L, luaL_checkinteger, narg, def);
2663 +}
2664 +
2665 +
2666 +LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
2667 + if (!lua_getmetatable(L, obj)) /* no metatable? */
2668 + return 0;
2669 + lua_pushstring(L, event);
2670 + lua_rawget(L, -2);
2671 + if (lua_isnil(L, -1)) {
2672 + lua_pop(L, 2); /* remove metatable and metafield */
2673 + return 0;
2674 + }
2675 + else {
2676 + lua_remove(L, -2); /* remove only metatable */
2677 + return 1;
2678 + }
2679 +}
2680 +
2681 +
2682 +LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
2683 + obj = abs_index(L, obj);
2684 + if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
2685 + return 0;
2686 + lua_pushvalue(L, obj);
2687 + lua_call(L, 1, 1);
2688 + return 1;
2689 +}
2690 +
2691 +
2692 +LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
2693 + const luaL_Reg *l) {
2694 + luaI_openlib(L, libname, l, 0);
2695 +}
2696 +
2697 +
2698 +static int libsize (const luaL_Reg *l) {
2699 + int size = 0;
2700 + for (; l->name; l++) size++;
2701 + return size;
2702 +}
2703 +
2704 +
2705 +LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
2706 + const luaL_Reg *l, int nup) {
2707 + if (libname) {
2708 + int size = libsize(l);
2709 + /* check whether lib already exists */
2710 + luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
2711 + lua_getfield(L, -1, libname); /* get _LOADED[libname] */
2712 + if (!lua_istable(L, -1)) { /* not found? */
2713 + lua_pop(L, 1); /* remove previous result */
2714 + /* try global variable (and create one if it does not exist) */
2715 + if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
2716 + luaL_error(L, "name conflict for module " LUA_QS, libname);
2717 + lua_pushvalue(L, -1);
2718 + lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
2719 + }
2720 + lua_remove(L, -2); /* remove _LOADED table */
2721 + lua_insert(L, -(nup+1)); /* move library table to below upvalues */
2722 + }
2723 + for (; l->name; l++) {
2724 + int i;
2725 + for (i=0; i<nup; i++) /* copy upvalues to the top */
2726 + lua_pushvalue(L, -nup);
2727 + lua_pushcclosure(L, l->func, nup);
2728 + lua_setfield(L, -(nup+2), l->name);
2729 + }
2730 + lua_pop(L, nup); /* remove upvalues */
2731 +}
2732 +
2733 +
2734 +
2735 +/*
2736 +** {======================================================
2737 +** getn-setn: size for arrays
2738 +** =======================================================
2739 +*/
2740 +
2741 +#if defined(LUA_COMPAT_GETN)
2742 +
2743 +static int checkint (lua_State *L, int topop) {
2744 + int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
2745 + lua_pop(L, topop);
2746 + return n;
2747 +}
2748 +
2749 +
2750 +static void getsizes (lua_State *L) {
2751 + lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
2752 + if (lua_isnil(L, -1)) { /* no `size' table? */
2753 + lua_pop(L, 1); /* remove nil */
2754 + lua_newtable(L); /* create it */
2755 + lua_pushvalue(L, -1); /* `size' will be its own metatable */
2756 + lua_setmetatable(L, -2);
2757 + lua_pushliteral(L, "kv");
2758 + lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */
2759 + lua_pushvalue(L, -1);
2760 + lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */
2761 + }
2762 +}
2763 +
2764 +
2765 +LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
2766 + t = abs_index(L, t);
2767 + lua_pushliteral(L, "n");
2768 + lua_rawget(L, t);
2769 + if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */
2770 + lua_pushliteral(L, "n"); /* use it */
2771 + lua_pushinteger(L, n);
2772 + lua_rawset(L, t);
2773 + }
2774 + else { /* use `sizes' */
2775 + getsizes(L);
2776 + lua_pushvalue(L, t);
2777 + lua_pushinteger(L, n);
2778 + lua_rawset(L, -3); /* sizes[t] = n */
2779 + lua_pop(L, 1); /* remove `sizes' */
2780 + }
2781 +}
2782 +
2783 +
2784 +LUALIB_API int luaL_getn (lua_State *L, int t) {
2785 + int n;
2786 + t = abs_index(L, t);
2787 + lua_pushliteral(L, "n"); /* try t.n */
2788 + lua_rawget(L, t);
2789 + if ((n = checkint(L, 1)) >= 0) return n;
2790 + getsizes(L); /* else try sizes[t] */
2791 + lua_pushvalue(L, t);
2792 + lua_rawget(L, -2);
2793 + if ((n = checkint(L, 2)) >= 0) return n;
2794 + return (int)lua_objlen(L, t);
2795 +}
2796 +
2797 +#endif
2798 +
2799 +/* }====================================================== */
2800 +
2801 +
2802 +
2803 +LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
2804 + const char *r) {
2805 + const char *wild;
2806 + size_t l = strlen(p);
2807 + luaL_Buffer *b = (luaL_Buffer *)kmalloc(sizeof(luaL_Buffer) + BUFSIZ, GFP_ATOMIC);
2808 + if(!b) luaL_error(L, "luaL_gsub: cannot allocate memory");
2809 + luaL_buffinit(L, b);
2810 + while ((wild = strstr(s, p)) != NULL) {
2811 + luaL_addlstring(b, s, wild - s); /* push prefix */
2812 + luaL_addstring(b, r); /* push replacement in place of pattern */
2813 + s = wild + l; /* continue after `p' */
2814 + }
2815 + luaL_addstring(b, s); /* push last suffix */
2816 + luaL_pushresult(b);
2817 + kfree(b);
2818 + return lua_tostring(L, -1);
2819 +}
2820 +
2821 +
2822 +LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
2823 + const char *fname, int szhint) {
2824 + const char *e;
2825 + lua_pushvalue(L, idx);
2826 + do {
2827 + e = strchr(fname, '.');
2828 + if (e == NULL) e = fname + strlen(fname);
2829 + lua_pushlstring(L, fname, e - fname);
2830 + lua_rawget(L, -2);
2831 + if (lua_isnil(L, -1)) { /* no such field? */
2832 + lua_pop(L, 1); /* remove this nil */
2833 + lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
2834 + lua_pushlstring(L, fname, e - fname);
2835 + lua_pushvalue(L, -2);
2836 + lua_settable(L, -4); /* set new table into field */
2837 + }
2838 + else if (!lua_istable(L, -1)) { /* field has a non-table value? */
2839 + lua_pop(L, 2); /* remove table and value */
2840 + return fname; /* return problematic part of the name */
2841 + }
2842 + lua_remove(L, -2); /* remove previous table */
2843 + fname = e + 1;
2844 + } while (*e == '.');
2845 + return NULL;
2846 +}
2847 +
2848 +
2849 +
2850 +/*
2851 +** {======================================================
2852 +** Generic Buffer manipulation
2853 +** =======================================================
2854 +*/
2855 +
2856 +
2857 +#define bufflen(B) ((B)->p - (B)->buffer)
2858 +#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
2859 +
2860 +#define LIMIT (LUA_MINSTACK/2)
2861 +
2862 +
2863 +static int emptybuffer (luaL_Buffer *B) {
2864 + size_t l = bufflen(B);
2865 + if (l == 0) return 0; /* put nothing on stack */
2866 + else {
2867 + lua_pushlstring(B->L, B->buffer, l);
2868 + B->p = B->buffer;
2869 + B->lvl++;
2870 + return 1;
2871 + }
2872 +}
2873 +
2874 +
2875 +static void adjuststack (luaL_Buffer *B) {
2876 + if (B->lvl > 1) {
2877 + lua_State *L = B->L;
2878 + int toget = 1; /* number of levels to concat */
2879 + size_t toplen = lua_strlen(L, -1);
2880 + do {
2881 + size_t l = lua_strlen(L, -(toget+1));
2882 + if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
2883 + toplen += l;
2884 + toget++;
2885 + }
2886 + else break;
2887 + } while (toget < B->lvl);
2888 + lua_concat(L, toget);
2889 + B->lvl = B->lvl - toget + 1;
2890 + }
2891 +}
2892 +
2893 +
2894 +LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
2895 + if (emptybuffer(B))
2896 + adjuststack(B);
2897 + return B->buffer;
2898 +}
2899 +
2900 +
2901 +LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
2902 + while (l--)
2903 + luaL_addchar(B, *s++);
2904 +}
2905 +
2906 +
2907 +LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
2908 + luaL_addlstring(B, s, strlen(s));
2909 +}
2910 +
2911 +
2912 +LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
2913 + emptybuffer(B);
2914 + lua_concat(B->L, B->lvl);
2915 + B->lvl = 1;
2916 +}
2917 +
2918 +
2919 +LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
2920 + lua_State *L = B->L;
2921 + size_t vl;
2922 + const char *s = lua_tolstring(L, -1, &vl);
2923 + if (vl <= bufffree(B)) { /* fit into buffer? */
2924 + memcpy(B->p, s, vl); /* put it there */
2925 + B->p += vl;
2926 + lua_pop(L, 1); /* remove from stack */
2927 + }
2928 + else {
2929 + if (emptybuffer(B))
2930 + lua_insert(L, -2); /* put buffer before new value */
2931 + B->lvl++; /* add new value into B stack */
2932 + adjuststack(B);
2933 + }
2934 +}
2935 +
2936 +
2937 +LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
2938 + B->L = L;
2939 + B->p = B->buffer;
2940 + B->lvl = 0;
2941 +}
2942 +
2943 +/* }====================================================== */
2944 +
2945 +
2946 +LUALIB_API int luaL_ref (lua_State *L, int t) {
2947 + int ref;
2948 + t = abs_index(L, t);
2949 + if (lua_isnil(L, -1)) {
2950 + lua_pop(L, 1); /* remove from stack */
2951 + return LUA_REFNIL; /* `nil' has a unique fixed reference */
2952 + }
2953 + lua_rawgeti(L, t, FREELIST_REF); /* get first free element */
2954 + ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */
2955 + lua_pop(L, 1); /* remove it from stack */
2956 + if (ref != 0) { /* any free element? */
2957 + lua_rawgeti(L, t, ref); /* remove it from list */
2958 + lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
2959 + }
2960 + else { /* no free elements */
2961 + ref = (int)lua_objlen(L, t);
2962 + ref++; /* create new reference */
2963 + }
2964 + lua_rawseti(L, t, ref);
2965 + return ref;
2966 +}
2967 +
2968 +
2969 +LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
2970 + if (ref >= 0) {
2971 + t = abs_index(L, t);
2972 + lua_rawgeti(L, t, FREELIST_REF);
2973 + lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
2974 + lua_pushinteger(L, ref);
2975 + lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
2976 + }
2977 +}
2978 +
2979 +
2980 +
2981 +/*
2982 +** {======================================================
2983 +** Load functions
2984 +** =======================================================
2985 +*/
2986 +
2987 +#if !defined(__KERNEL__)
2988 +typedef struct LoadF {
2989 + int extraline;
2990 + FILE *f;
2991 + char buff[LUAL_BUFFERSIZE];
2992 +} LoadF;
2993 +
2994 +
2995 +static const char *getF (lua_State *L, void *ud, size_t *size) {
2996 + LoadF *lf = (LoadF *)ud;
2997 + (void)L;
2998 + if (lf->extraline) {
2999 + lf->extraline = 0;
3000 + *size = 1;
3001 + return "\n";
3002 + }
3003 + if (feof(lf->f)) return NULL;
3004 + *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
3005 + return (*size > 0) ? lf->buff : NULL;
3006 +}
3007 +
3008 +
3009 +static int errfile (lua_State *L, const char *what, int fnameindex) {
3010 + const char *serr = strerror(errno);
3011 + const char *filename = lua_tostring(L, fnameindex) + 1;
3012 + lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
3013 + lua_remove(L, fnameindex);
3014 + return LUA_ERRFILE;
3015 +}
3016 +
3017 +
3018 +LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
3019 + LoadF lf;
3020 + int status, readstatus;
3021 + int c;
3022 + int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
3023 + lf.extraline = 0;
3024 + if (filename == NULL) {
3025 + lua_pushliteral(L, "=stdin");
3026 + lf.f = stdin;
3027 + }
3028 + else {
3029 + lua_pushfstring(L, "@%s", filename);
3030 + lf.f = fopen(filename, "r");
3031 + if (lf.f == NULL) return errfile(L, "open", fnameindex);
3032 + }
3033 + c = getc(lf.f);
3034 + if (c == '#') { /* Unix exec. file? */
3035 + lf.extraline = 1;
3036 + while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
3037 + if (c == '\n') c = getc(lf.f);
3038 + }
3039 + if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
3040 + lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
3041 + if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
3042 + /* skip eventual `#!...' */
3043 + while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
3044 + lf.extraline = 0;
3045 + }
3046 + ungetc(c, lf.f);
3047 + status = lua_load(L, getF, &lf, lua_tostring(L, -1));
3048 + readstatus = ferror(lf.f);
3049 + if (filename) fclose(lf.f); /* close file (even in case of errors) */
3050 + if (readstatus) {
3051 + lua_settop(L, fnameindex); /* ignore results from `lua_load' */
3052 + return errfile(L, "read", fnameindex);
3053 + }
3054 + lua_remove(L, fnameindex);
3055 + return status;
3056 +}
3057 +#endif
3058 +
3059 +typedef struct LoadS {
3060 + const char *s;
3061 + size_t size;
3062 +} LoadS;
3063 +
3064 +
3065 +static const char *getS (lua_State *L, void *ud, size_t *size) {
3066 + LoadS *ls = (LoadS *)ud;
3067 + (void)L;
3068 + if (ls->size == 0) return NULL;
3069 + *size = ls->size;
3070 + ls->size = 0;
3071 + return ls->s;
3072 +}
3073 +
3074 +
3075 +LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
3076 + const char *name) {
3077 + LoadS ls;
3078 + ls.s = buff;
3079 + ls.size = size;
3080 + return lua_load(L, getS, &ls, name);
3081 +}
3082 +
3083 +
3084 +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
3085 + return luaL_loadbuffer(L, s, strlen(s), s);
3086 +}
3087 +
3088 +
3089 +
3090 +/* }====================================================== */
3091 +
3092 +
3093 +static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
3094 + (void)ud;
3095 + (void)osize;
3096 + if (nsize == 0) {
3097 +#if !defined(__KERNEL__)
3098 + free(ptr);
3099 +#else
3100 + kfree(ptr);
3101 +#endif
3102 + return NULL;
3103 + }
3104 + else
3105 +#if !defined(__KERNEL__)
3106 + return realloc(ptr, nsize);
3107 +#else
3108 + return krealloc(ptr, nsize, GFP_ATOMIC);
3109 +#endif
3110 +}
3111 +
3112 +
3113 +static int lpanic (lua_State *L) {
3114 + (void)L; /* to avoid warnings */
3115 +#if !defined(__KERNEL__)
3116 + fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
3117 +#else
3118 + printk( "PANIC: unprotected error in call to Lua API (%s)\n",
3119 +#endif
3120 + lua_tostring(L, -1));
3121 + return 0;
3122 +}
3123 +
3124 +
3125 +LUALIB_API lua_State *luaL_newstate (void) {
3126 + lua_State *L = lua_newstate(l_alloc, NULL);
3127 + if (L) lua_atpanic(L, &lpanic);
3128 + return L;
3129 +}
3130 +
3131 --- /dev/null
3132 +++ b/extensions/LUA/lua/lauxlib.h
3133 @@ -0,0 +1,184 @@
3134 +/*
3135 +** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $
3136 +** Auxiliary functions for building Lua libraries
3137 +** See Copyright Notice in lua.h
3138 +*/
3139 +
3140 +
3141 +#ifndef lauxlib_h
3142 +#define lauxlib_h
3143 +
3144 +
3145 +#include <stddef.h>
3146 +#include <linux/slab.h> /* for kmalloc and kfree when allocating luaL_Buffer */
3147 +
3148 +#if !defined(__KERNEL__)
3149 +#include <stdio.h>
3150 +#endif
3151 +
3152 +#include "lua.h"
3153 +
3154 +
3155 +#if defined(LUA_COMPAT_GETN)
3156 +LUALIB_API int (luaL_getn) (lua_State *L, int t);
3157 +LUALIB_API void (luaL_setn) (lua_State *L, int t, int n);
3158 +#else
3159 +#define luaL_getn(L,i) ((int)lua_objlen(L, i))
3160 +#define luaL_setn(L,i,j) ((void)0) /* no op! */
3161 +#endif
3162 +
3163 +#if defined(LUA_COMPAT_OPENLIB)
3164 +#define luaI_openlib luaL_openlib
3165 +#endif
3166 +
3167 +
3168 +/* extra error code for `luaL_load' */
3169 +#define LUA_ERRFILE (LUA_ERRERR+1)
3170 +
3171 +
3172 +typedef struct luaL_Reg {
3173 + const char *name;
3174 + lua_CFunction func;
3175 +} luaL_Reg;
3176 +
3177 +
3178 +
3179 +LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname,
3180 + const luaL_Reg *l, int nup);
3181 +LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
3182 + const luaL_Reg *l);
3183 +LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
3184 +LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
3185 +LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname);
3186 +LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
3187 +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg,
3188 + size_t *l);
3189 +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg,
3190 + const char *def, size_t *l);
3191 +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg);
3192 +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def);
3193 +
3194 +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
3195 +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
3196 + lua_Integer def);
3197 +
3198 +LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
3199 +LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
3200 +LUALIB_API void (luaL_checkany) (lua_State *L, int narg);
3201 +
3202 +LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname);
3203 +LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
3204 +
3205 +LUALIB_API void (luaL_where) (lua_State *L, int lvl);
3206 +LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
3207 +
3208 +LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
3209 + const char *const lst[]);
3210 +
3211 +LUALIB_API int (luaL_ref) (lua_State *L, int t);
3212 +LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
3213 +
3214 +#if !defined(__KERNEL__)
3215 +LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
3216 +#endif
3217 +
3218 +LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
3219 + const char *name);
3220 +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
3221 +
3222 +LUALIB_API lua_State *(luaL_newstate) (void);
3223 +
3224 +
3225 +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
3226 + const char *r);
3227 +
3228 +LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,
3229 + const char *fname, int szhint);
3230 +
3231 +
3232 +
3233 +
3234 +/*
3235 +** ===============================================================
3236 +** some useful macros
3237 +** ===============================================================
3238 +*/
3239 +
3240 +#define luaL_argcheck(L, cond,numarg,extramsg) \
3241 + ((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))
3242 +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
3243 +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
3244 +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
3245 +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
3246 +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
3247 +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
3248 +
3249 +#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
3250 +
3251 +#if !defined(__KERNEL__)
3252 +#define luaL_dofile(L, fn) \
3253 + (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
3254 +#endif
3255 +
3256 +#define luaL_dostring(L, s) \
3257 + (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
3258 +
3259 +#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
3260 +
3261 +#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
3262 +
3263 +/*
3264 +** {======================================================
3265 +** Generic Buffer manipulation
3266 +** =======================================================
3267 +*/
3268 +
3269 +
3270 +
3271 +typedef struct luaL_Buffer {
3272 + char *p; /* current position in buffer */
3273 + int lvl; /* number of strings in the stack (level) */
3274 + lua_State *L;
3275 + char buffer[LUAL_BUFFERSIZE];
3276 +} luaL_Buffer;
3277 +
3278 +#define luaL_addchar(B,c) \
3279 + ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
3280 + (*(B)->p++ = (char)(c)))
3281 +
3282 +/* compatibility only */
3283 +#define luaL_putchar(B,c) luaL_addchar(B,c)
3284 +
3285 +#define luaL_addsize(B,n) ((B)->p += (n))
3286 +
3287 +
3288 +LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
3289 +LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B);
3290 +LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
3291 +LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);
3292 +LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);
3293 +LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
3294 +
3295 +
3296 +/* }====================================================== */
3297 +
3298 +
3299 +/* compatibility with ref system */
3300 +
3301 +/* pre-defined references */
3302 +#define LUA_NOREF (-2)
3303 +#define LUA_REFNIL (-1)
3304 +
3305 +#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \
3306 + (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0))
3307 +
3308 +#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref))
3309 +
3310 +#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref))
3311 +
3312 +
3313 +#define luaL_reg luaL_Reg
3314 +
3315 +#endif
3316 +
3317 +
3318 --- /dev/null
3319 +++ b/extensions/LUA/lua/lbaselib.c
3320 @@ -0,0 +1,647 @@
3321 +/*
3322 +** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $
3323 +** Basic library
3324 +** See Copyright Notice in lua.h
3325 +*/
3326 +
3