Index: libpcre/config.h.in =================================================================== --- libpcre/config.h.in (revision 6014) +++ libpcre/config.h.in (working copy) @@ -23,15 +23,9 @@ /* Define to 1 if you have the `bcopy' function. */ #undef HAVE_BCOPY -/* Define to 1 if you have the header file. */ -#undef HAVE_BITS_TYPE_TRAITS_H - /* Define to 1 if you have the header file. */ #undef HAVE_DIRENT_H -/* Define to 1 if you have the header file. */ -#undef HAVE_DLFCN_H - /* Define to 1 if you have the header file. */ #undef HAVE_INTTYPES_H @@ -56,9 +50,6 @@ /* Define to 1 if you have the `strerror' function. */ #undef HAVE_STRERROR -/* Define to 1 if you have the header file. */ -#undef HAVE_STRING - /* Define to 1 if you have the header file. */ #undef HAVE_STRINGS_H @@ -77,9 +68,6 @@ /* Define to 1 if you have the header file. */ #undef HAVE_SYS_TYPES_H -/* Define to 1 if you have the header file. */ -#undef HAVE_TYPE_TRAITS_H - /* Define to 1 if you have the header file. */ #undef HAVE_UNISTD_H @@ -97,6 +85,9 @@ "configure" can be used to override this default. */ #undef LINK_SIZE +/* Mac OS X on Intel */ +#undef MACOSX + /* The value of MATCH_LIMIT determines the default number of times the internal match() function can be called during a single execution of pcre_exec(). There is a runtime interface for setting a different limit. Index: Makefile.in =================================================================== --- Makefile.in (revision 6014) +++ Makefile.in (working copy) @@ -60,9 +60,9 @@ INSTALLUMIT=@INSTALLUMIT@ ifneq (@LIBLUA_LIBS@,) -NSE_SRC=nse_main.cc nse_auxiliar.cc nse_nsock.cc nse_init.cc nse_nmaplib.cc nse_debug.cc nse_string.cc -NSE_HDRS=nse_main.h nse_auxiliar.h nse_nsock.h nse_init.h nse_nmaplib.h nse_debug.h nse_macros.h nse_string.h -NSE_OBJS=nse_main.o nse_auxiliar.o nse_nsock.o nse_init.o nse_nmaplib.o nse_debug.o nse_string.o +NSE_SRC=nse_main.cc nse_auxiliar.cc nse_nsock.cc nse_init.cc nse_nmaplib.cc nse_debug.cc nse_pcrelib.cc nse_string.cc +NSE_HDRS=nse_main.h nse_auxiliar.h nse_nsock.h nse_init.h nse_nmaplib.h nse_debug.h nse_macros.h nse_pcrelib.h nse_string.h +NSE_OBJS=nse_main.o nse_auxiliar.o nse_nsock.o nse_init.o nse_nmaplib.o nse_debug.o nse_pcrelib.o nse_string.o NSESTDLIB=nsestdlib endif Index: scripts/ircServerInfo.nse =================================================================== --- scripts/ircServerInfo.nse (revision 6014) +++ scripts/ircServerInfo.nse (working copy) @@ -10,7 +10,6 @@ require("stdnse") require "shortport" -require "pcre" portrule = shortport.port_or_service(6667, "irc") Index: nse_init.cc =================================================================== --- nse_init.cc (revision 6014) +++ nse_init.cc (working copy) @@ -4,6 +4,7 @@ #include "nse_debug.h" // 3rd Party libs +#include "nse_pcrelib.h" #include "nbase.h" @@ -45,6 +46,7 @@ {LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_DBLIBNAME, luaopen_debug}, + {NSE_PCRELIBNAME, luaopen_pcrelib}, {NULL, NULL} }; Index: nse_pcrelib.h =================================================================== --- nse_pcrelib.h (revision 0) +++ nse_pcrelib.h (revision 0) @@ -0,0 +1,9 @@ +#ifndef NSE_PCRELIB +#define NSE_PCRELIB + +#define NSE_PCRELIBNAME "pcre" + +LUALIB_API int luaopen_pcrelib (lua_State *L); + +#endif + Index: nselib/pcre.h =================================================================== --- nselib/pcre.h (revision 6014) +++ nselib/pcre.h (working copy) @@ -1,17 +0,0 @@ -#ifndef PCRE_H -#define PCRE_H - -#ifdef WIN32 -#define _CRT_SECURE_NO_DEPRECATE 1 -#define _CRT_SECURE_NO_WARNINGS 1 /* otherwise msvc++ complains even for -safe operations, and request us to use their str*_s() functions */ -#pragma warning(disable: 4996) -#define vsnprintf _vsnprintf -#define strdup _strdup -#endif /* WIN32 */ -#define NSE_PCRELIBNAME "pcre" - -LUALIB_API int luaopen_pcre(lua_State *L); - -#endif - Index: nselib/configure =================================================================== --- nselib/configure (revision 6014) +++ nselib/configure (working copy) @@ -837,9 +837,6 @@ LIBTOOL LIBTOOL_DEPS LUAINCLUDE -PCRE_INCLUDE -PCRE_LD -PCRE_LIBS LIBOBJS LTLIBOBJS' ac_subst_files='' @@ -1443,9 +1440,6 @@ --with-pic try to use only PIC/non-PIC objects [default=use both] --with-tags[=TAGS] include additional configurations [automatic] - --with-libpcre=DIR Use an existing (compiled) pcre lib from DIR/include - and DIR/lib. - --with-libpcre=included Always use the version included with Nmap Some influential environment variables: CC C compiler command @@ -3637,7 +3631,7 @@ ;; *-*-irix6*) # Find out which ABI we are using. - echo '#line 3640 "configure"' > conftest.$ac_ext + echo '#line 3634 "configure"' > conftest.$ac_ext if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5 (eval $ac_compile) 2>&5 ac_status=$? @@ -6259,11 +6253,11 @@ -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:6262: $lt_compile\"" >&5) + (eval echo "\"\$as_me:6256: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:6266: \$? = $ac_status" >&5 + echo "$as_me:6260: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -6527,11 +6521,11 @@ -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:6530: $lt_compile\"" >&5) + (eval echo "\"\$as_me:6524: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:6534: \$? = $ac_status" >&5 + echo "$as_me:6528: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -6631,11 +6625,11 @@ -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:6634: $lt_compile\"" >&5) + (eval echo "\"\$as_me:6628: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:6638: \$? = $ac_status" >&5 + echo "$as_me:6632: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -8928,7 +8922,7 @@ lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext < conftest.$ac_ext <&5) + (eval echo "\"\$as_me:11365: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:11375: \$? = $ac_status" >&5 + echo "$as_me:11369: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -11472,11 +11466,11 @@ -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:11475: $lt_compile\"" >&5) + (eval echo "\"\$as_me:11469: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:11479: \$? = $ac_status" >&5 + echo "$as_me:11473: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -13033,11 +13027,11 @@ -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:13036: $lt_compile\"" >&5) + (eval echo "\"\$as_me:13030: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:13040: \$? = $ac_status" >&5 + echo "$as_me:13034: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -13137,11 +13131,11 @@ -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:13140: $lt_compile\"" >&5) + (eval echo "\"\$as_me:13134: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:13144: \$? = $ac_status" >&5 + echo "$as_me:13138: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -15328,11 +15322,11 @@ -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:15331: $lt_compile\"" >&5) + (eval echo "\"\$as_me:15325: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:15335: \$? = $ac_status" >&5 + echo "$as_me:15329: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -15596,11 +15590,11 @@ -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:15599: $lt_compile\"" >&5) + (eval echo "\"\$as_me:15593: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:15603: \$? = $ac_status" >&5 + echo "$as_me:15597: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -15700,11 +15694,11 @@ -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:15703: $lt_compile\"" >&5) + (eval echo "\"\$as_me:15697: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:15707: \$? = $ac_status" >&5 + echo "$as_me:15701: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -18529,39 +18523,6 @@ -#needed for pcre - libpcre - -# First we test whether they specified libpcre explicitly - -# Check whether --with-libpcre was given. -if test "${with_libpcre+set}" = set; then - withval=$with_libpcre; case "$with_libpcre" in - yes) - ;; - included) - PCRE_INCLUDE_DIR="../libpcre" - PCRE_LD_DIR="../libpcre" - ;; - *) - PCRE_INCLUDE_DIR="$with_libpcre/include" - PCRE_LD_DIR="$with_libpcre/lib" - ;; - esac -else - PCRE_INCLUDE_DIR="../libpcre";PCRE_LD_DIR="../libpcre" - -fi - -PCRE_INCLUDE="-I$PCRE_INCLUDE_DIR" -PCRE_LD="-L$PCRE_LD_DIR" -PCRE_LIBS="-lpcre" - -# AC_SUBST(PCREDIR) - - - - - ac_config_files="$ac_config_files Makefile" cat >confcache <<\_ACEOF @@ -19262,14 +19223,11 @@ LIBTOOL!$LIBTOOL$ac_delim LIBTOOL_DEPS!$LIBTOOL_DEPS$ac_delim LUAINCLUDE!$LUAINCLUDE$ac_delim -PCRE_INCLUDE!$PCRE_INCLUDE$ac_delim -PCRE_LD!$PCRE_LD$ac_delim -PCRE_LIBS!$PCRE_LIBS$ac_delim LIBOBJS!$LIBOBJS$ac_delim LTLIBOBJS!$LTLIBOBJS$ac_delim _ACEOF - if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 75; then + if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 72; then break elif $ac_last_try; then { { echo "$as_me:$LINENO: error: could not make $CONFIG_STATUS" >&5 Index: nselib/Makefile.in =================================================================== --- nselib/Makefile.in (revision 6014) +++ nselib/Makefile.in (working copy) @@ -12,21 +12,15 @@ LIBTOOL= ./libtool LTFLAGS = --tag=CC --silent -all: bit.so pcre.so +all: bit.so bit.so: bit.c @LIBTOOL_DEPS@ $(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) @LUAINCLUDE@ $(CFLAGS) -c bit.c $(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -avoid-version -module -rpath /usr/local/lib -o bit.la bit.lo mv .libs/bit.so bit.so -pcre.so: pcre.c @LIBTOOL_DEPS@ - $(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) @PCRE_INCLUDE@ @LUAINCLUDE@ $(CFLAGS) -c pcre.c - $(LIBTOOL) $(LTFLAGS) --mode=link $(CC) @PCRE_LD@ @PCRE_LIBS@ -avoid-version -module -rpath /usr/local/lib -o pcre.la pcre.lo - mv .libs/pcre.so pcre.so - - clean: - rm -f bit.so pcre.so *.la *.lo + rm -f bit.so *.la *.lo rm -rf .libs distclean: clean Index: nselib/configure.ac =================================================================== --- nselib/configure.ac (revision 6014) +++ nselib/configure.ac (working copy) @@ -19,36 +19,6 @@ AC_CHECK_HEADER([lua.h],,[AC_MSG_NOTICE(using lua-includefiles provided with nmap);[LUAINCLUDE=-I../liblua/]],) AC_SUBST(LUAINCLUDE) -#needed for pcre - libpcre - -# First we test whether they specified libpcre explicitly -AC_ARG_WITH(libpcre, -AC_HELP_STRING([--with-libpcre=DIR], [Use an existing (compiled) pcre lib from DIR/include and DIR/lib.]) -AC_HELP_STRING([--with-libpcre=included], [Always use the version included with Nmap]), -[ case "$with_libpcre" in - yes) - ;; - included) - PCRE_INCLUDE_DIR="../libpcre" - PCRE_LD_DIR="../libpcre" - ;; - *) - PCRE_INCLUDE_DIR="$with_libpcre/include" - PCRE_LD_DIR="$with_libpcre/lib" - ;; - esac], -[PCRE_INCLUDE_DIR="../libpcre";PCRE_LD_DIR="../libpcre"] -) -PCRE_INCLUDE="-I$PCRE_INCLUDE_DIR" -PCRE_LD="-L$PCRE_LD_DIR" -PCRE_LIBS="-lpcre" - -# AC_SUBST(PCREDIR) -AC_SUBST(PCRE_INCLUDE) -AC_SUBST(PCRE_LD) -AC_SUBST(PCRE_LIBS) - - AC_CONFIG_FILES([Makefile]) AC_OUTPUT Index: nselib/pcre.c =================================================================== --- nselib/pcre.c (revision 6014) +++ nselib/pcre.c (working copy) @@ -1,400 +0,0 @@ -/* lrexlib.c - PCRE regular expression library */ -/* Reuben Thomas nov00-18dec04 */ -/* Shmuel Zeigerman may04-18dec04 */ - - -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif -#include "lua.h" -#include "lauxlib.h" - -#ifdef __cplusplus -} -#endif - -#include -#include - -#include "pcre.h" - -static int Snprintf(char *s, size_t n, const char *fmt, ...) -{ - va_list ap; - int ret; - - va_start(ap, fmt); - ret = vsnprintf(s, n, fmt, ap); - va_end(ap); - - if (ret < 0 || (unsigned) ret >= n) - s[n - 1] = '\0'; - - return ret; -} - -static void L_lua_error(lua_State *L, const char *message) -{ - int status; - - lua_pushstring(L, message); - status = lua_error(L); -} - -static int get_startoffset(lua_State *L, int stackpos, size_t len) -{ - int startoffset = luaL_optint(L, stackpos, 1); - if(startoffset > 0) - startoffset--; - else if(startoffset < 0) { - startoffset += (int) len; - if(startoffset < 0) - startoffset = 0; - } - return startoffset; -} - -static int udata_tostring (lua_State *L, const char* type_handle, - const char* type_name) -{ - char buf[256]; - void *udata = luaL_checkudata(L, 1, type_handle); - - if(udata) { - (void)Snprintf(buf, 255, "%s (%p)", type_name, udata); - lua_pushstring(L, buf); - } - else { - (void)Snprintf(buf, 255, "must be userdata of type '%s'", type_name); - (void)luaL_argerror(L, 1, buf); - } - - free(udata); - return 1; -} - -typedef struct { const char* key; lua_Number val; } flags_pair; - -static int get_flags (lua_State *L, const flags_pair *arr) -{ - const flags_pair *p; - lua_newtable(L); - for(p=arr; p->key != NULL; p++) { - lua_pushstring(L, p->key); - lua_pushnumber(L, p->val); - lua_rawset(L, -3); - } - return 1; -} - -static void createmeta(lua_State *L, const char *name) -{ - (void)luaL_newmetatable(L, name); /* create new metatable */ - lua_pushliteral(L, "__index"); - lua_pushvalue(L, -2); /* push metatable */ - lua_rawset(L, -3); /* metatable.__index = metatable */ -} - -const char pcre_handle[] = "pcre_regex_handle"; -const char pcre_typename[] = "pcre_regex"; - -typedef struct { - pcre *pr; - pcre_extra *extra; - int *match; - int ncapt; - const unsigned char *tables; -} pcre2; /* a better name is needed */ - -static const unsigned char *Lpcre_maketables(lua_State *L, int stackpos) -{ - const unsigned char *tables; - char old_locale[256]; - char *locale = strdup(luaL_checkstring(L, stackpos)); - - if(locale == NULL) - L_lua_error(L, "cannot set locale"); - - strncpy(old_locale, setlocale(LC_CTYPE, NULL), 255); /* store the locale */ - - if(setlocale(LC_CTYPE, locale) == NULL) /* set new locale */ - L_lua_error(L, "cannot set locale"); - - tables = pcre_maketables(); /* make tables with new locale */ - (void)setlocale(LC_CTYPE, old_locale); /* restore the old locale */ - - free(locale); - return tables; -} - -static int Lpcre_comp(lua_State *L) -{ - char buf[256]; - const char *error; - int erroffset; - pcre2 *ud; - char *pattern = strdup(luaL_checkstring(L, 1)); - int cflags = luaL_optint(L, 2, 0); - const unsigned char *tables = NULL; - - if(lua_gettop(L) > 2 && !lua_isnil(L, 3)) - tables = Lpcre_maketables(L, 3); - if(tables == NULL) - L_lua_error(L, "PCRE compilation failed"); - - ud = (pcre2*)lua_newuserdata(L, sizeof(pcre2)); - luaL_getmetatable(L, pcre_handle); - (void)lua_setmetatable(L, -2); - ud->match = NULL; - ud->extra = NULL; - ud->tables = tables; /* keep this for eventual freeing */ - - ud->pr = pcre_compile(pattern, cflags, &error, &erroffset, tables); - if(!ud->pr) { - (void)Snprintf(buf, 255, "%s (pattern offset: %d)", error, erroffset+1); - /* show offset 1-based as it's common in Lua */ - L_lua_error(L, buf); - } - - ud->extra = pcre_study(ud->pr, 0, &error); - if(error) L_lua_error(L, error); - - pcre_fullinfo(ud->pr, ud->extra, PCRE_INFO_CAPTURECOUNT, &ud->ncapt); - /* since some platforms have problems with nbase and exporting symbols we - * emulate it - */ - if(((ud->ncapt + 1) * 3 * sizeof(int))<0){ - L_lua_error(L, "PCRE: negative argument to malloc"); - } - /* need (2 ints per capture, plus one for substring match) * 3/2 */ - ud->match = (int *) malloc((ud->ncapt + 1) * 3 * sizeof(int)); - if(ud->match==NULL){ - L_lua_error(L, "PCRE: malloc failed!"); - } - return 1; -} - -static void Lpcre_getargs(lua_State *L, pcre2 **pud, const char **text, - size_t *text_len) -{ - *pud = (pcre2 *)luaL_checkudata(L, 1, pcre_handle); - if(*pud == NULL) - (void)luaL_argerror(L, 1, ("compiled regexp expected")); - *text = luaL_checklstring(L, 2, text_len); -} - -typedef void (*Lpcre_push_matches) (lua_State *L, const char *text, pcre2 *ud); - -static void Lpcre_push_substrings (lua_State *L, const char *text, pcre2 *ud) -{ - unsigned int i, j; - unsigned int namecount; - unsigned char *name_table; - int name_entry_size; - unsigned char *tabptr; - const int *match = ud->match; - - lua_newtable(L); - for (i = 1; i <= (unsigned) ud->ncapt; i++) { - j = i * 2; - if (match[j] >= 0) - lua_pushlstring(L, text + match[j], (size_t)(match[j + 1] - match[j])); - else - lua_pushboolean(L, 0); - lua_rawseti(L, -2, i); - } - - /* now do named subpatterns - NJG */ - (void)pcre_fullinfo(ud->pr, ud->extra, PCRE_INFO_NAMECOUNT, &namecount); - if (namecount <= 0) - return; - (void)pcre_fullinfo(ud->pr, ud->extra, PCRE_INFO_NAMETABLE, &name_table); - (void)pcre_fullinfo(ud->pr, ud->extra, PCRE_INFO_NAMEENTRYSIZE, &name_entry_size); - tabptr = name_table; - for (i = 0; i < namecount; i++) { - unsigned int n = (tabptr[0] << 8) | tabptr[1]; /* number of the capturing parenthesis */ - if (n > 0 && n <= (unsigned) ud->ncapt) { /* check range */ - unsigned int j = n * 2; - lua_pushstring(L, strdup((char*)tabptr + 2)); /* name of the capture, zero terminated */ - if (match[j] >= 0) - lua_pushlstring(L, text + match[j], match[j + 1] - match[j]); - else - lua_pushboolean(L, 0); - lua_rawset(L, -3); - } - tabptr += name_entry_size; - } -} - -static void Lpcre_push_offsets (lua_State *L, const char *text, pcre2 * ud) -{ - unsigned int i, j, k; - if(text) { - /* suppress compiler warning */ - } - lua_newtable(L); - for (i=1, j=1; i <= (unsigned) ud->ncapt; i++) { - k = i * 2; - if (ud->match[k] >= 0) { - lua_pushnumber(L, ud->match[k] + 1); - lua_rawseti(L, -2, j++); - lua_pushnumber(L, ud->match[k+1]); - lua_rawseti(L, -2, j++); - } - else { - lua_pushboolean(L, 0); - lua_rawseti(L, -2, j++); - lua_pushboolean(L, 0); - lua_rawseti(L, -2, j++); - } - } -} - -static int Lpcre_match_generic(lua_State *L, Lpcre_push_matches push_matches) -{ - int res; - const char *text; - pcre2 *ud; - size_t elen; - int startoffset; - int eflags = luaL_optint(L, 4, 0); - - Lpcre_getargs(L, &ud, &text, &elen); - startoffset = get_startoffset(L, 3, elen); - - res = pcre_exec(ud->pr, ud->extra, text, (int)elen, startoffset, eflags, - ud->match, (ud->ncapt + 1) * 3); - if (res >= 0) { - lua_pushnumber(L, (lua_Number) ud->match[0] + 1); - lua_pushnumber(L, (lua_Number) ud->match[1]); - (*push_matches)(L, text, ud); - return 3; - } - return 0; -} - -static int Lpcre_match(lua_State *L) -{ - return Lpcre_match_generic(L, Lpcre_push_substrings); -} - -static int Lpcre_exec(lua_State *L) -{ - return Lpcre_match_generic(L, Lpcre_push_offsets); -} - -static int Lpcre_gmatch(lua_State *L) -{ - int res; - size_t len; - int nmatch = 0, limit = 0; - const char *text; - pcre2 *ud; - int maxmatch = luaL_optint(L, 4, 0); - int eflags = luaL_optint(L, 5, 0); - int startoffset = 0; - Lpcre_getargs(L, &ud, &text, &len); - luaL_checktype(L, 3, LUA_TFUNCTION); - - if(maxmatch > 0) /* this must be stated in the docs */ - limit = 1; - - while (!limit || nmatch < maxmatch) { - res = pcre_exec(ud->pr, ud->extra, text, (int)len, startoffset, eflags, - ud->match, (ud->ncapt + 1) * 3); - if (res >= 0) { - nmatch++; - lua_pushvalue(L, 3); - lua_pushlstring(L, text + ud->match[0], ud->match[1] - ud->match[0]); - Lpcre_push_substrings(L, text, ud); - lua_call(L, 2, 1); - if(lua_toboolean(L, -1)) - break; - lua_pop(L, 1); - startoffset = ud->match[1]; - } else - break; - } - lua_pushnumber(L, nmatch); - return 1; -} - -static int Lpcre_gc (lua_State *L) -{ - pcre2 *ud = (pcre2 *)luaL_checkudata(L, 1, pcre_handle); - if (ud) { - if(ud->pr) pcre_free(ud->pr); - if(ud->extra) pcre_free(ud->extra); - if(ud->tables) pcre_free((void *)ud->tables); - if(ud->match) free(ud->match); - } - return 0; -} - -static int Lpcre_tostring (lua_State *L) { - return udata_tostring(L, pcre_handle, pcre_typename); -} - -static int Lpcre_vers (lua_State *L) -{ - lua_pushstring(L, pcre_version()); - return 1; -} - -static flags_pair pcre_flags[] = -{ - { "CASELESS", PCRE_CASELESS }, - { "MULTILINE", PCRE_MULTILINE }, - { "DOTALL", PCRE_DOTALL }, - { "EXTENDED", PCRE_EXTENDED }, - { "ANCHORED", PCRE_ANCHORED }, - { "DOLLAR_ENDONLY", PCRE_DOLLAR_ENDONLY }, - { "EXTRA", PCRE_EXTRA }, - { "NOTBOL", PCRE_NOTBOL }, - { "NOTEOL", PCRE_NOTEOL }, - { "UNGREEDY", PCRE_UNGREEDY }, - { "NOTEMPTY", PCRE_NOTEMPTY }, - { "UTF8", PCRE_UTF8 }, -#if PCRE_MAJOR >= 4 - { "NO_AUTO_CAPTURE", PCRE_NO_AUTO_CAPTURE }, - { "NO_UTF8_CHECK", PCRE_NO_UTF8_CHECK }, -#endif - { NULL, 0 } -}; - -static int Lpcre_get_flags (lua_State *L) { - return get_flags(L, pcre_flags); -} - -static const luaL_reg pcremeta[] = { - {"exec", Lpcre_exec}, - {"match", Lpcre_match}, - {"gmatch", Lpcre_gmatch}, - {"__gc", Lpcre_gc}, - {"__tostring", Lpcre_tostring}, - {NULL, NULL} -}; - -/* Open the library */ -static const luaL_reg pcrelib[] = { - {"new", Lpcre_comp}, - {"flags", Lpcre_get_flags}, - {"version", Lpcre_vers}, - {NULL, NULL} -}; - -LUALIB_API int luaopen_pcre(lua_State *L) -{ - createmeta(L, pcre_handle); - luaL_openlib(L, NULL, pcremeta, 0); - lua_pop(L, 1); - luaL_openlib(L, NSE_PCRELIBNAME, pcrelib, 0); - - return 1; -} - Index: mswin32/nmap.vcproj =================================================================== --- mswin32/nmap.vcproj (revision 6014) +++ mswin32/nmap.vcproj (working copy) @@ -286,6 +286,10 @@ > + + @@ -471,6 +475,10 @@ > + + Index: nse_pcrelib.cc =================================================================== --- nse_pcrelib.cc (revision 0) +++ nse_pcrelib.cc (revision 0) @@ -0,0 +1,374 @@ +/* lrexlib.c - PCRE regular expression library */ +/* Reuben Thomas nov00-18dec04 */ +/* Shmuel Zeigerman may04-18dec04 */ + + +#include +#include +#include + +extern "C" { +#include "lua.h" +#include "lauxlib.h" +} + +#include +#include + +#include "nbase.h" +#include "nmap_error.h" + +#include "nse_pcrelib.h" + +static void L_lua_error(lua_State *L, const char *message) +{ + int status; + + lua_pushstring(L, message); + status = lua_error(L); +} + +static int get_startoffset(lua_State *L, int stackpos, size_t len) +{ + int startoffset = luaL_optint(L, stackpos, 1); + if(startoffset > 0) + startoffset--; + else if(startoffset < 0) { + startoffset += len; + if(startoffset < 0) + startoffset = 0; + } + return startoffset; +} + +static int udata_tostring (lua_State *L, const char* type_handle, + const char* type_name) +{ + char buf[256]; + void *udata = luaL_checkudata(L, 1, type_handle); + + if(udata) { + (void)snprintf(buf, 255, "%s (%p)", type_name, udata); + lua_pushstring(L, buf); + } + else { + (void)snprintf(buf, 255, "must be userdata of type '%s'", type_name); + (void)luaL_argerror(L, 1, buf); + } + + free(udata); + return 1; +} + +typedef struct { const char* key; lua_Number val; } flags_pair; + +static int get_flags (lua_State *L, const flags_pair *arr) +{ + const flags_pair *p; + lua_newtable(L); + for(p=arr; p->key != NULL; p++) { + lua_pushstring(L, p->key); + lua_pushnumber(L, p->val); + lua_rawset(L, -3); + } + return 1; +} + +static void createmeta(lua_State *L, const char *name) +{ + (void)luaL_newmetatable(L, name); /* create new metatable */ + lua_pushliteral(L, "__index"); + lua_pushvalue(L, -2); /* push metatable */ + lua_rawset(L, -3); /* metatable.__index = metatable */ +} + +const char pcre_handle[] = "pcre_regex_handle"; +const char pcre_typename[] = "pcre_regex"; + +typedef struct { + pcre *pr; + pcre_extra *extra; + int *match; + int ncapt; + const unsigned char *tables; +} pcre2; /* a better name is needed */ + +static const unsigned char *Lpcre_maketables(lua_State *L, int stackpos) +{ + const unsigned char *tables; + char old_locale[256]; + char *locale = strdup(luaL_checkstring(L, stackpos)); + + if(locale == NULL) + L_lua_error(L, "cannot set locale"); + + strncpy(old_locale, setlocale(LC_CTYPE, NULL), 255); /* store the locale */ + + if(setlocale(LC_CTYPE, locale) == NULL) /* set new locale */ + L_lua_error(L, "cannot set locale"); + + tables = pcre_maketables(); /* make tables with new locale */ + (void)setlocale(LC_CTYPE, old_locale); /* restore the old locale */ + + free(locale); + return tables; +} + +static int Lpcre_comp(lua_State *L) +{ + char buf[256]; + const char *error; + int erroffset; + pcre2 *ud; + char *pattern = strdup(luaL_checkstring(L, 1)); + int cflags = luaL_optint(L, 2, 0); + const unsigned char *tables = NULL; + + if(lua_gettop(L) > 2 && !lua_isnil(L, 3)) + tables = Lpcre_maketables(L, 3); + if(tables == NULL) + L_lua_error(L, "PCRE compilation failed"); + + ud = (pcre2*)lua_newuserdata(L, sizeof(pcre2)); + luaL_getmetatable(L, pcre_handle); + (void)lua_setmetatable(L, -2); + ud->match = NULL; + ud->extra = NULL; + ud->tables = tables; /* keep this for eventual freeing */ + + ud->pr = pcre_compile(pattern, cflags, &error, &erroffset, tables); + if(!ud->pr) { + (void)snprintf(buf, 255, "%s (pattern offset: %d)", error, erroffset+1); + /* show offset 1-based as it's common in Lua */ + L_lua_error(L, buf); + } + + ud->extra = pcre_study(ud->pr, 0, &error); + if(error) L_lua_error(L, error); + + pcre_fullinfo(ud->pr, ud->extra, PCRE_INFO_CAPTURECOUNT, &ud->ncapt); + /* need (2 ints per capture, plus one for substring match) * 3/2 */ + ud->match = (int *) safe_malloc((ud->ncapt + 1) * 3 * sizeof(int)); + + return 1; +} + +static void Lpcre_getargs(lua_State *L, pcre2 **pud, const char **text, + size_t *text_len) +{ + *pud = (pcre2 *)luaL_checkudata(L, 1, pcre_handle); + if(*pud == NULL) + (void)luaL_argerror(L, 1, ("compiled regexp expected")); + *text = luaL_checklstring(L, 2, text_len); +} + +typedef void (*Lpcre_push_matches) (lua_State *L, const char *text, pcre2 *ud); + +static void Lpcre_push_substrings (lua_State *L, const char *text, pcre2 *ud) +{ + unsigned int i, j; + unsigned int namecount; + unsigned char *name_table; + int name_entry_size; + unsigned char *tabptr; + const int *match = ud->match; + + lua_newtable(L); + for (i = 1; i <= (unsigned) ud->ncapt; i++) { + j = i * 2; + if (match[j] >= 0) + lua_pushlstring(L, text + match[j], (size_t)(match[j + 1] - match[j])); + else + lua_pushboolean(L, 0); + lua_rawseti(L, -2, i); + } + + /* now do named subpatterns - NJG */ + (void)pcre_fullinfo(ud->pr, ud->extra, PCRE_INFO_NAMECOUNT, &namecount); + if (namecount <= 0) + return; + (void)pcre_fullinfo(ud->pr, ud->extra, PCRE_INFO_NAMETABLE, &name_table); + (void)pcre_fullinfo(ud->pr, ud->extra, PCRE_INFO_NAMEENTRYSIZE, &name_entry_size); + tabptr = name_table; + for (i = 0; i < namecount; i++) { + unsigned int n = (tabptr[0] << 8) | tabptr[1]; /* number of the capturing parenthesis */ + if (n > 0 && n <= (unsigned) ud->ncapt) { /* check range */ + unsigned int j = n * 2; + lua_pushstring(L, strdup((char*)tabptr + 2)); /* name of the capture, zero terminated */ + if (match[j] >= 0) + lua_pushlstring(L, text + match[j], match[j + 1] - match[j]); + else + lua_pushboolean(L, 0); + lua_rawset(L, -3); + } + tabptr += name_entry_size; + } +} + +static void Lpcre_push_offsets (lua_State *L, const char *text, pcre2 * ud) +{ + unsigned int i, j, k; + if(text) { + /* suppress compiler warning */ + } + lua_newtable(L); + for (i=1, j=1; i <= (unsigned) ud->ncapt; i++) { + k = i * 2; + if (ud->match[k] >= 0) { + lua_pushnumber(L, ud->match[k] + 1); + lua_rawseti(L, -2, j++); + lua_pushnumber(L, ud->match[k+1]); + lua_rawseti(L, -2, j++); + } + else { + lua_pushboolean(L, 0); + lua_rawseti(L, -2, j++); + lua_pushboolean(L, 0); + lua_rawseti(L, -2, j++); + } + } +} + +static int Lpcre_match_generic(lua_State *L, Lpcre_push_matches push_matches) +{ + int res; + const char *text; + pcre2 *ud; + size_t elen; + int startoffset; + int eflags = luaL_optint(L, 4, 0); + + Lpcre_getargs(L, &ud, &text, &elen); + startoffset = get_startoffset(L, 3, elen); + + res = pcre_exec(ud->pr, ud->extra, text, (int)elen, startoffset, eflags, + ud->match, (ud->ncapt + 1) * 3); + if (res >= 0) { + lua_pushnumber(L, (lua_Number) ud->match[0] + 1); + lua_pushnumber(L, (lua_Number) ud->match[1]); + (*push_matches)(L, text, ud); + return 3; + } + return 0; +} + +static int Lpcre_match(lua_State *L) +{ + return Lpcre_match_generic(L, Lpcre_push_substrings); +} + +static int Lpcre_exec(lua_State *L) +{ + return Lpcre_match_generic(L, Lpcre_push_offsets); +} + +static int Lpcre_gmatch(lua_State *L) +{ + int res; + size_t len; + int nmatch = 0, limit = 0; + const char *text; + pcre2 *ud; + int maxmatch = luaL_optint(L, 4, 0); + int eflags = luaL_optint(L, 5, 0); + int startoffset = 0; + Lpcre_getargs(L, &ud, &text, &len); + luaL_checktype(L, 3, LUA_TFUNCTION); + + if(maxmatch > 0) /* this must be stated in the docs */ + limit = 1; + + while (!limit || nmatch < maxmatch) { + res = pcre_exec(ud->pr, ud->extra, text, (int)len, startoffset, eflags, + ud->match, (ud->ncapt + 1) * 3); + if (res >= 0) { + nmatch++; + lua_pushvalue(L, 3); + lua_pushlstring(L, text + ud->match[0], ud->match[1] - ud->match[0]); + Lpcre_push_substrings(L, text, ud); + lua_call(L, 2, 1); + if(lua_toboolean(L, -1)) + break; + lua_pop(L, 1); + startoffset = ud->match[1]; + } else + break; + } + lua_pushnumber(L, nmatch); + return 1; +} + +static int Lpcre_gc (lua_State *L) +{ + pcre2 *ud = (pcre2 *)luaL_checkudata(L, 1, pcre_handle); + if (ud) { + if(ud->pr) pcre_free(ud->pr); + if(ud->extra) pcre_free(ud->extra); + if(ud->tables) pcre_free((void *)ud->tables); + if(ud->match) free(ud->match); + } + return 0; +} + +static int Lpcre_tostring (lua_State *L) { + return udata_tostring(L, pcre_handle, pcre_typename); +} + +static int Lpcre_vers (lua_State *L) +{ + lua_pushstring(L, pcre_version()); + return 1; +} + +static flags_pair pcre_flags[] = +{ + { "CASELESS", PCRE_CASELESS }, + { "MULTILINE", PCRE_MULTILINE }, + { "DOTALL", PCRE_DOTALL }, + { "EXTENDED", PCRE_EXTENDED }, + { "ANCHORED", PCRE_ANCHORED }, + { "DOLLAR_ENDONLY", PCRE_DOLLAR_ENDONLY }, + { "EXTRA", PCRE_EXTRA }, + { "NOTBOL", PCRE_NOTBOL }, + { "NOTEOL", PCRE_NOTEOL }, + { "UNGREEDY", PCRE_UNGREEDY }, + { "NOTEMPTY", PCRE_NOTEMPTY }, + { "UTF8", PCRE_UTF8 }, +#if PCRE_MAJOR >= 4 + { "NO_AUTO_CAPTURE", PCRE_NO_AUTO_CAPTURE }, + { "NO_UTF8_CHECK", PCRE_NO_UTF8_CHECK }, +#endif + { NULL, 0 } +}; + +static int Lpcre_get_flags (lua_State *L) { + return get_flags(L, pcre_flags); +} + +static const luaL_reg pcremeta[] = { + {"exec", Lpcre_exec}, + {"match", Lpcre_match}, + {"gmatch", Lpcre_gmatch}, + {"__gc", Lpcre_gc}, + {"__tostring", Lpcre_tostring}, + {NULL, NULL} +}; + +/* Open the library */ +static const luaL_reg pcrelib[] = { + {"new", Lpcre_comp}, + {"flags", Lpcre_get_flags}, + {"version", Lpcre_vers}, + {NULL, NULL} +}; + +LUALIB_API int luaopen_pcrelib(lua_State *L) +{ + createmeta(L, pcre_handle); + luaL_openlib(L, NULL, pcremeta, 0); + lua_pop(L, 1); + luaL_openlib(L, NSE_PCRELIBNAME, pcrelib, 0); + + return 1; +} +