summaryrefslogtreecommitdiff
path: root/plugins/NewAwaySysMod/CommonLibs/pcre.cpp
blob: 6800a7141816d47dcf585f11ab43ef5be7130dff (plain)
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
/*
	Pcre.cpp
	Copyright (c) 2007-2008 Chervov Dmitry

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#define CHARARRAY_CONVERT

#include "..\NewAwaySys\common.h"
#include <windows.h>
#include <stdio.h>
#include <crtdbg.h>
#include "newpluginapi.h"
#include "m_utils.h"
#include "TMyArray.h"
#include "CString.h"
#include "pcre.h"


HMODULE hPcreDLL = NULL;

static pcre* (*pcre_compile)(const char*, int, const char**, int*, const unsigned char*);
static int (*pcre_config)(int, void*);
static int (*pcre_exec)(const pcre*, const pcre_extra*, const char*, int, int, int, int*, int);
static void (*pcre_free)(void*);
static pcre_extra* (*pcre_study)(const pcre*, int, const char **);


typedef struct
{
	pcre *pPcre;
	pcre_extra *pExtra;
	TCString Pattern; // used when it's not a valid regexp
	int ID; // user-defined ID of the pattern; returned by PcreCheck on a match
} sPcreCompileData;

TMyArray<sPcreCompileData> PcreCompileData;


void FreePcreCompileData()
{
	int I;
	for (I = 0; I < PcreCompileData.GetSize(); I++)
	{
		if (PcreCompileData[I].pPcre)
		{
			pcre_free(PcreCompileData[I].pPcre);
			if (PcreCompileData[I].pExtra)
			{
				pcre_free(PcreCompileData[I].pExtra);
			}
		}
	}
	PcreCompileData.RemoveAll();
}


TCString CompileRegexp(TCString Regexp, int bAddAsUsualSubstring, int ID)
{
	TCString Result(_T(""));
	sPcreCompileData s = {0};
	int NewID = PcreCompileData.AddElem(s);
	PcreCompileData[NewID].ID = ID;
	if (hPcreDLL && !bAddAsUsualSubstring)
	{
		const char *Err;
		int ErrOffs;
		int Flags = PCRE_CASELESS;
		if (Regexp[0] == '/')
		{
			TCString OrigRegexp = Regexp;
			Regexp = Regexp.Right(Regexp.GetLen() - 1);
			TCHAR *pRegexpEnd = (TCHAR*)Regexp + Regexp.GetLen();
			TCHAR *p = _tcsrchr(Regexp.GetBuffer(), '/');
			if (!p)
			{
				Regexp = OrigRegexp;
			} else
			{
				*p = 0;
				Flags = 0;
				while (++p < pRegexpEnd)
				{
					switch (*p)
					{
						case 'i':
						{
							Flags |= PCRE_CASELESS;
						} break;
						case 'm':
						{
							Flags |= PCRE_MULTILINE;
						} break;
						case 's':
						{
							Flags |= PCRE_DOTALL;
						} break;
						case 'x':
						{
							Flags |= PCRE_EXTENDED;
						} break;
						case 'A':
						{
							Flags |= PCRE_ANCHORED;
						} break;
						case 'f':
						{
							Flags |= PCRE_FIRSTLINE;
						} break;
						case 'D':
						{
							Flags |= PCRE_DOLLAR_ENDONLY;
						} break;
						case 'U':
						{
							Flags |= PCRE_UNGREEDY;
						} break;
						case 'X':
						{
							Flags |= PCRE_EXTRA;
						} break;
						default:
						{
							Result += LogMessage(TranslateT("Warning, unknown pattern modifier '%c':\n%s"), *p, (TCHAR*)OrigRegexp) + _T("\n\n");
						} break;
					}
				}
			}
			Regexp.ReleaseBuffer();
		}
#ifdef _UNICODE
		PcreCompileData[NewID].pPcre = pcre_compile(WCHAR2UTF8(Regexp).GetData(), PCRE_UTF8 | PCRE_NO_UTF8_CHECK | Flags, &Err, &ErrOffs, NULL);
#else
		PcreCompileData[NewID].pPcre = pcre_compile(Regexp, Flags, &Err, &ErrOffs, NULL);
#endif
		if (PcreCompileData[NewID].pPcre)
		{
			PcreCompileData[NewID].pExtra = NULL;
			if (pcre_study)
			{
				PcreCompileData[NewID].pExtra = pcre_study(PcreCompileData[NewID].pPcre, 0, &Err);
			}
		} else
		{
			Result += LogMessage(TranslateT("Syntax error in regexp\n%s\nat offset %d: %s."), (TCHAR*)Regexp, ErrOffs, (TCHAR*)ANSI2TCHAR(Err)) + _T("\n\n");
			PcreCompileData[NewID].Pattern = Regexp;
	 	}
	} else
	{
		PcreCompileData[NewID].Pattern = Regexp;
	}
	return Result;
}


HMODULE LoadPcreLibrary(const char *szPath)
{
	_ASSERT(szPath);
	HMODULE hModule = LoadLibraryA(szPath);
	if (!hModule)
	{
		return NULL;
	}
	*(FARPROC*)&pcre_config = GetProcAddress(hModule, "pcre_config");
	*(FARPROC*)&pcre_compile = GetProcAddress(hModule, "pcre_compile");
	*(FARPROC*)&pcre_exec = GetProcAddress(hModule, "pcre_exec");
	*(FARPROC*)&pcre_study = GetProcAddress(hModule, "pcre_study");
	*(FARPROC*)&pcre_free = *(FARPROC*)GetProcAddress(hModule, "pcre_free"); // pcre_free is a pointer to a variable containing pointer to the function %)
	if (pcre_compile && pcre_exec && pcre_free)
	{
#ifdef _UNICODE
		int Utf8Supported = 0;
		if (pcre_config)
		{
			pcre_config(PCRE_CONFIG_UTF8, &Utf8Supported);
		}
		if (Utf8Supported)
		{
			return hModule;
		}
#else
		return hModule; 
#endif
	}
	FreeLibrary(hModule);
	return NULL;
}


void InitPcre()
{
	_ASSERT(!hPcreDLL);
	hPcreDLL = LoadPcreLibrary("pcre.dll");
	if (!hPcreDLL)
	{
		hPcreDLL = LoadPcreLibrary("pcre3.dll");
	}
	if (!hPcreDLL)
	{
		char path[MAX_PATH];
		GetModuleFileNameA(NULL, path, sizeof(path));
		char *p = strrchr(path, '\\');
		if (p)
		{
			strcpy(p + 1, "pcre.dll");
		} else
		{
			strcpy(path, "pcre.dll");
		}
		hPcreDLL = LoadPcreLibrary(path);
		if (!hPcreDLL)
		{
			if (p)
			{
				strcpy(p + 1, "pcre3.dll");
			} else
			{
				strcpy(path, "pcre3.dll");
			}
			hPcreDLL = LoadPcreLibrary(path);
		}
	}
}


void UninitPcre()
{
	if (hPcreDLL)
	{
		FreePcreCompileData();
		FreeLibrary(hPcreDLL);
	}
}


int PcreEnabled()
{
	return (int)hPcreDLL;
}


int PcreCheck(TCString Str, int StartingID)
{ // StartingID specifies the pattern from which to start checking, i.e. the check starts from the next pattern after the one that has ID == StartingID
	int I;
	if (StartingID == -1)
	{
		I = 0;
	} else
	{
		for (I = 0; I < PcreCompileData.GetSize(); I++)
		{
			if (PcreCompileData[I].ID == StartingID)
			{
				I++;
				break;
			}
		}
	}
	for (; I < PcreCompileData.GetSize(); I++)
	{
		if (hPcreDLL && PcreCompileData[I].pPcre)
		{
#ifdef _UNICODE
			CHARARRAY Utf8Str = WCHAR2UTF8(Str);
			int Res = pcre_exec(PcreCompileData[I].pPcre, PcreCompileData[I].pExtra, Utf8Str.GetData(), Utf8Str.GetSize() - 1, 0, PCRE_NOTEMPTY | PCRE_NO_UTF8_CHECK, NULL, 0);
#else
			int Res = pcre_exec(PcreCompileData[I].pPcre, PcreCompileData[I].pExtra, Str, Str.GetLen(), 0, PCRE_NOTEMPTY, NULL, 0);
#endif				
			if (Res >= 0)
			{
				return PcreCompileData[I].ID;
			}
		} else
		{
			if (_tcsstr(Str.ToLower(), PcreCompileData[I].Pattern.ToLower()))
			{
				return PcreCompileData[I].ID;
			}
		}
	}
	return -1;
}