1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-09 22:21:29 +00:00

Lua 5.2 upgrade [1] for NSE.

[1] http://seclists.org/nmap-dev/2012/q2/34
This commit is contained in:
batrick
2012-05-27 08:53:32 +00:00
parent a839e69449
commit 000f6dc4d9
553 changed files with 13477 additions and 8870 deletions

View File

@@ -1,5 +1,5 @@
/*
** $Id: ltablib.c,v 1.38.1.2 2007/12/28 15:32:23 roberto Exp $
** $Id: ltablib.c,v 1.63 2011/11/28 17:26:30 roberto Exp $
** Library for Table Manipulation
** See Copyright Notice in lua.h
*/
@@ -16,43 +16,11 @@
#include "lualib.h"
#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n))
static int foreachi (lua_State *L) {
int i;
int n = aux_getn(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
for (i=1; i <= n; i++) {
lua_pushvalue(L, 2); /* function */
lua_pushinteger(L, i); /* 1st argument */
lua_rawgeti(L, 1, i); /* 2nd argument */
lua_call(L, 2, 1);
if (!lua_isnil(L, -1))
return 1;
lua_pop(L, 1); /* remove nil result */
}
return 0;
}
static int foreach (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushnil(L); /* first key */
while (lua_next(L, 1)) {
lua_pushvalue(L, 2); /* function */
lua_pushvalue(L, -3); /* key */
lua_pushvalue(L, -3); /* value */
lua_call(L, 2, 1);
if (!lua_isnil(L, -1))
return 1;
lua_pop(L, 2); /* remove value and result */
}
return 0;
}
#define aux_getn(L,n) \
(luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
#if defined(LUA_COMPAT_MAXN)
static int maxn (lua_State *L) {
lua_Number max = 0;
luaL_checktype(L, 1, LUA_TTABLE);
@@ -67,24 +35,7 @@ static int maxn (lua_State *L) {
lua_pushnumber(L, max);
return 1;
}
static int getn (lua_State *L) {
lua_pushinteger(L, aux_getn(L, 1));
return 1;
}
static int setn (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
#ifndef luaL_setn
luaL_setn(L, 1, luaL_checkint(L, 2));
#else
luaL_error(L, LUA_QL("setn") " is obsolete");
#endif
lua_pushvalue(L, 1);
return 1;
}
static int tinsert (lua_State *L) {
@@ -109,7 +60,6 @@ static int tinsert (lua_State *L) {
return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
}
}
luaL_setn(L, 1, e); /* new size */
lua_rawseti(L, 1, pos); /* t[pos] = v */
return 0;
}
@@ -119,8 +69,7 @@ static int tremove (lua_State *L) {
int e = aux_getn(L, 1);
int pos = luaL_optint(L, 2, e);
if (!(1 <= pos && pos <= e)) /* position is outside bounds? */
return 0; /* nothing to remove */
luaL_setn(L, 1, e - 1); /* t.n = n-1 */
return 0; /* nothing to remove */
lua_rawgeti(L, 1, pos); /* result = t[pos] */
for ( ;pos<e; pos++) {
lua_rawgeti(L, 1, pos+1);
@@ -132,6 +81,15 @@ static int tremove (lua_State *L) {
}
static void addfield (lua_State *L, luaL_Buffer *b, int i) {
lua_rawgeti(L, 1, i);
if (!lua_isstring(L, -1))
luaL_error(L, "invalid value (%s) at index %d in table for "
LUA_QL("concat"), luaL_typename(L, -1), i);
luaL_addvalue(b);
}
static int tconcat (lua_State *L) {
luaL_Buffer b;
size_t lsep;
@@ -139,26 +97,67 @@ static int tconcat (lua_State *L) {
const char *sep = luaL_optlstring(L, 2, "", &lsep);
luaL_checktype(L, 1, LUA_TTABLE);
i = luaL_optint(L, 3, 1);
last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1));
last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1));
luaL_buffinit(L, &b);
for (; i <= last; i++) {
lua_rawgeti(L, 1, i);
luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings");
luaL_addvalue(&b);
if (i != last)
luaL_addlstring(&b, sep, lsep);
for (; i < last; i++) {
addfield(L, &b, i);
luaL_addlstring(&b, sep, lsep);
}
if (i == last) /* add last value (if interval was not empty) */
addfield(L, &b, i);
luaL_pushresult(&b);
return 1;
}
/*
** {======================================================
** Pack/unpack
** =======================================================
*/
static int pack (lua_State *L) {
int n = lua_gettop(L); /* number of elements to pack */
lua_createtable(L, n, 1); /* create result table */
lua_pushinteger(L, n);
lua_setfield(L, -2, "n"); /* t.n = number of elements */
if (n > 0) { /* at least one element? */
int i;
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1); /* insert first element */
lua_replace(L, 1); /* move table into index 1 */
for (i = n; i >= 2; i--) /* assign other elements */
lua_rawseti(L, 1, i);
}
return 1; /* return table */
}
static int unpack (lua_State *L) {
int i, e, n;
luaL_checktype(L, 1, LUA_TTABLE);
i = luaL_optint(L, 2, 1);
e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));
if (i > e) return 0; /* empty range */
n = e - i + 1; /* number of elements */
if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
return luaL_error(L, "too many results to unpack");
lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
while (i++ < e) /* push arg[i + 1...e] */
lua_rawgeti(L, 1, i);
return n;
}
/* }====================================================== */
/*
** {======================================================
** Quicksort
** (based on `Algorithms in MODULA-3', Robert Sedgewick;
** Addison-Wesley, 1993.)
** =======================================================
*/
@@ -179,77 +178,10 @@ static int sort_comp (lua_State *L, int a, int b) {
return res;
}
else /* a < b? */
return lua_lessthan(L, a, b);
return lua_compare(L, a, b, LUA_OPLT);
}
static int call_comp (lua_State *L)
{
int res;
lua_pushvalue(L, 2);
lua_pushvalue(L, -2);
lua_pushvalue(L, -4);
lua_call(L, 2, 1);
res = lua_toboolean(L, -1);
lua_pop(L, 1);
return res;
}
#define compare(L, c) (c ? call_comp(L) : lua_lessthan(L, -1, -2))
static int partition (lua_State *L, int p, int r, int c)
{
int i = p - 1, j;
lua_rawgeti(L, 1, r);
/* for r - p = 1 */
if (p - r == 1)
{
lua_rawgeti(L, 1, p);
if (!compare(L, c))
{
lua_rawseti(L, 1, r);
lua_rawseti(L, 1, p);
}
else
lua_pop(L, 2);
return i + 1;
}
for (j = p; j < r; j++)
{
lua_rawgeti(L, 1, j);
if (compare(L, c))
{
i++;
lua_rawgeti(L, 1, i);
lua_pushvalue(L, -2);
lua_rawseti(L, 1, i);
lua_rawseti(L, 1, j);
}
lua_pop(L, 1);
}
lua_pop(L, 1);
i++;
lua_rawgeti(L, 1, r);
lua_rawgeti(L, 1, i);
lua_rawseti(L, 1, r);
lua_rawseti(L, 1, i);
return i;
}
static void quicksort (lua_State *L, int p, int r, int c)
{
if (p < r)
{
int q = partition(L, p, r, c);
quicksort(L, p, q - 1, c);
quicksort(L, q + 1, r, c);
}
}
static int calls = 0;
static void auxsort (lua_State *L, int l, int u) {
calls++;
if (calls > 20)
printf("calls = %d; l = %d; u = %d;\n", calls, l, u);
while (l < u) { /* for tail recursion */
int i, j;
/* sort elements a[l], a[(l+u)/2] and a[u] */
@@ -283,12 +215,12 @@ static void auxsort (lua_State *L, int l, int u) {
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
/* repeat ++i until a[i] >= P */
while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
if (i>u) luaL_error(L, "invalid order function for sorting");
if (i>=u) luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[i] */
}
/* repeat --j until a[j] <= P */
while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
if (j<l) luaL_error(L, "invalid order function for sorting");
if (j<=l) luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[j] */
}
if (j<i) {
@@ -310,26 +242,14 @@ static void auxsort (lua_State *L, int l, int u) {
}
auxsort(L, j, i); /* call recursively the smaller one */
} /* repeat the routine for the larger one */
calls--;
}
static int sort (lua_State *L) {
int n = aux_getn(L, 1);
int c;
lua_settop(L, 2); /* make sure there is two arguments */
luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
if ((c = (!lua_isnil(L, 2)))) /* is there a 2nd argument? */
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
luaL_checktype(L, 2, LUA_TFUNCTION);
quicksort(L, 1, n, c);
return 0;
}
static int oldsort (lua_State *L) {
int n = aux_getn(L, 1);
lua_settop(L, 2); /* make sure there is two arguments */
luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
if (!lua_isnil(L, 2)) /* is there a 2nd argument? */
luaL_checktype(L, 2, LUA_TFUNCTION);
auxsort(L, 1, n);
return 0;
}
@@ -339,21 +259,25 @@ static int oldsort (lua_State *L) {
static const luaL_Reg tab_funcs[] = {
{"concat", tconcat},
{"foreach", foreach},
{"foreachi", foreachi},
{"getn", getn},
#if defined(LUA_COMPAT_MAXN)
{"maxn", maxn},
#endif
{"insert", tinsert},
{"pack", pack},
{"unpack", unpack},
{"remove", tremove},
{"setn", setn},
{"sort", sort},
{"oldsort", oldsort},
{NULL, NULL}
};
LUALIB_API int luaopen_table (lua_State *L) {
luaL_register(L, LUA_TABLIBNAME, tab_funcs);
LUAMOD_API int luaopen_table (lua_State *L) {
luaL_newlib(L, tab_funcs);
#if defined(LUA_COMPAT_UNPACK)
/* _G.unpack = table.unpack */
lua_getfield(L, -1, "unpack");
lua_setglobal(L, "unpack");
#endif
return 1;
}