File: nse_pcrelib.cc

package info (click to toggle)
nmap 6.40-0.1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 43,480 kB
  • sloc: ansic: 88,786; cpp: 62,018; sh: 19,422; python: 16,592; xml: 9,575; makefile: 2,543; perl: 2,211; yacc: 608; lex: 469; asm: 372; java: 45
file content (405 lines) | stat: -rw-r--r-- 9,839 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* lrexlib.c - PCRE regular expression library */
/* Reuben Thomas   nov00-18dec04 */
/* Shmuel Zeigerman   may04-18dec04 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern "C" {
  #include "lua.h"
  #include "lauxlib.h"
}

#include <locale.h>

#include "nbase.h"
#include "nmap_error.h"

#ifdef HAVE_PCRE_PCRE_H
# include <pcre/pcre.h>
#else
# include <pcre.h>
#endif

#include "nse_pcrelib.h"

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;
}

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)
		luaL_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 */
		luaL_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;
	const char *pattern = 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)
		luaL_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 */
		luaL_error(L, buf);
	}

	ud->extra = pcre_study(ud->pr, 0, &error);
	if(error) luaL_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, (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
#ifdef PCRE_AUTO_CALLOUT
	{ "AUTO_CALLOUT",    PCRE_AUTO_CALLOUT },
#endif
#ifdef PCRE_PARTIAL
	{ "PARTIAL",         PCRE_PARTIAL },
#endif
#ifdef PCRE_DFA_SHORTEST
	{ "DFA_SHORTEST",    PCRE_DFA_SHORTEST },
#endif
#ifdef PCRE_DFA_RESTART
	{ "DFA_RESTART",     PCRE_DFA_RESTART },
#endif
#ifdef PCRE_FIRSTLINE
	{ "FIRSTLINE",       PCRE_FIRSTLINE },
#endif
#ifdef PCRE_DUPNAMES
	{ "DUPNAMES",        PCRE_DUPNAMES },
#endif
#ifdef PCRE_NEWLINE_CR
	{ "NEWLINE_CR",      PCRE_NEWLINE_CR },
#endif
#ifdef PCRE_NEWLINE_LF
	{ "NEWLINE_LF",      PCRE_NEWLINE_LF },
#endif
#ifdef PCRE_NEWLINE_CRLF
	{ "NEWLINE_CRLF",    PCRE_NEWLINE_CRLF },
#endif
#ifdef PCRE_NEWLINE_ANY
	{ "NEWLINE_ANY",     PCRE_NEWLINE_ANY },
#endif
#ifdef PCRE_NEWLINE_ANYCRLF
	{ "NEWLINE_ANYCRLF", PCRE_NEWLINE_ANYCRLF },
#endif
#ifdef PCRE_BSR_ANYCRLF
	{ "BSR_ANYCRLF",     PCRE_BSR_ANYCRLF },
#endif
#ifdef PCRE_BSR_UNICODE
	{ "BSR_UNICODE",     PCRE_BSR_UNICODE },
#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)
{
	luaL_newmetatable(L, pcre_handle);
	lua_pushliteral(L, "__index");
	luaL_newlib(L, pcremeta);
	lua_rawset(L, -3);
	lua_pop(L, 1);

	luaL_newlib(L, pcrelib);
	
	return 1;
}