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
|
/*
"Spam Filter"-Plugin for Miranda IM
Copyright 2003-2006 Heiko Herkenrath
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 ("SpamFilter-License.txt"); if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// -- Defines
//#define DISABLE_REGEXP_SUPPORT
// -- Includes
#include "common.h"
#include "Utilities/PCRE/include/pcre.h"
// -- Variables
HMODULE hPcreDLL = NULL;
// -- Function Pointers
int (*dyn_pcre_config)(int, void*);
pcre* (*dyn_pcre_compile)(const char*, int, const char**, int*, const unsigned char*);
pcre_extra* (*dyn_pcre_study)(const pcre*, int, const char **);
int (*dyn_pcre_exec)(const pcre*, const pcre_extra*, const char*, int, int, int, int*, int);
void (**dyn_pointer_pcre_free)(void*);
#define dyn_pcre_free (*dyn_pointer_pcre_free);
// -----------------------------------------
BOOL RegExpExistsInString(const WCHAR* pszString, const WCHAR* pszRegEx, BOOL bCaseSensitive, BOOL* pbRegExMalformed)
{
if (pbRegExMalformed) *pbRegExMalformed = FALSE;
if (!pszString || !pszRegEx) return FALSE;
if (hPcreDLL)
{
int iRc;
pcre* pPcre;
pcre_extra* pExtra;
// Following are dummy buffers that are needed for the functions to work
const char* pszErr;
int iErrOffset;
// 1.) Interpret regular expression
#if defined(UNICODE)
{
char* pszRegExUtf8 = mir_utf8encodeW(pszRegEx);
if (pszRegExUtf8)
{
pPcre = dyn_pcre_compile(pszRegExUtf8, (bCaseSensitive ? 0 : PCRE_CASELESS)|PCRE_UTF8|PCRE_NO_UTF8_CHECK, &pszErr, &iErrOffset, NULL);
mir_free(pszRegExUtf8);
} else {
pPcre = NULL;
}
}
#else
pPcre = dyn_pcre_compile(pszRegEx, (bCaseSensitive ? 0 : PCRE_CASELESS), &pszErr, &iErrOffset, NULL);
#endif
if (pPcre)
{
// 2.) Study regular expression (not necessary)
pExtra = dyn_pcre_study(pPcre, 0, &pszErr);
// 3.) Search for regular expression
#if defined(UNICODE)
{
char* pszStringUtf8 = mir_utf8encodeW(pszString);
if (pszStringUtf8)
{
iRc = dyn_pcre_exec(pPcre, pExtra, (char*)pszStringUtf8, (int)lstrlenA(pszStringUtf8), 0, PCRE_NOTEMPTY|PCRE_NO_UTF8_CHECK, NULL, 0);
mir_free(pszStringUtf8);
} else {
iRc = 0;
}
}
#else
iRc = dyn_pcre_exec(pPcre, pExtra, pszString, (int)lstrlen(pszString), 0, PCRE_NOTEMPTY, NULL, 0);
#endif
// Release memory
dyn_pcre_free(pPcre);
if (pExtra) dyn_pcre_free(pExtra);
return (iRc >= 0);
} else {
if (pbRegExMalformed)
*pbRegExMalformed = TRUE;
#if !defined(_DEBUG)
if (!pbRegExMalformed)
#endif
{
WCHAR szOut[128];
mir_sntprintf(szOut, ARRAYSIZE(szOut), _T("Spam Filter: Syntax error in RegExp (%s).\r\n"), pszRegEx);
OutputDebugString(szOut);
}
}
}
// Default string search (fallback)
return (bCaseSensitive ? StrStr(pszString, pszRegEx) : StrStrI(pszString, pszRegEx)) ? TRUE : FALSE;
}
BOOL ValidateRegExp(const WCHAR* pszRegEx)
{
if (hPcreDLL)
{
pcre* pPcre;
char* pszErr;
int iErrOffset;
// Parameter check
if (!pszRegEx) return FALSE;
// Interpret regular expression
#if defined(UNICODE)
{
char* pszRegExUtf8 = mir_utf8encodeW(pszRegEx);
if (pszRegExUtf8) {
pPcre = dyn_pcre_compile(pszRegExUtf8, PCRE_UTF8|PCRE_NO_UTF8_CHECK, &pszErr, &iErrOffset, NULL);
mir_free(pszRegExUtf8);
} else {
pPcre = NULL;
}
}
#else
pPcre = dyn_pcre_compile(pszRegEx, 0, &pszErr, &iErrOffset, NULL);
#endif
if (pPcre) {
dyn_pcre_free(pPcre); // Release memory
return TRUE;
} else {
return FALSE;
}
} else {
return TRUE;
}
}
BOOL RegExpEnabled(void)
{
return (hPcreDLL ? TRUE : FALSE);
}
// ------------------------------------
void InitRegExp(void)
{
#if !defined(DISABLE_REGEXP_SUPPORT)
hPcreDLL = LoadLibrary(_T("PCRE"));
if (hPcreDLL)
{
// Exported functions
// GetProcAddress returns the pointer to the function
*(FARPROC*)&dyn_pcre_config = GetProcAddress(hPcreDLL, "pcre_config");
*(FARPROC*)&dyn_pcre_compile = GetProcAddress(hPcreDLL, "pcre_compile");
*(FARPROC*)&dyn_pcre_study = GetProcAddress(hPcreDLL, "pcre_study");
*(FARPROC*)&dyn_pcre_exec = GetProcAddress(hPcreDLL, "pcre_exec");
// Exported variables
// GetProcAddress returns the pointer to the variable containing the pointer to the function...jippieh! :-)
*(FARPROC*)&dyn_pointer_pcre_free = GetProcAddress(hPcreDLL, "pcre_free");
if (dyn_pcre_compile && dyn_pcre_exec && dyn_pointer_pcre_free && (*dyn_pointer_pcre_free))
{
#if defined(UNICODE)
int iUtf8Supported = 0;
if (dyn_pcre_config)
dyn_pcre_config(PCRE_CONFIG_UTF8, &iUtf8Supported);
if (iUtf8Supported != 0)
return;
#else
return;
#endif
}
// Error: Unloading
FreeLibrary(hPcreDLL);
hPcreDLL = NULL;
}
// Debug output
OutputDebugString(_T("Spam Filter: RegExp DLL (PCRE.DLL) not found or not compatible.\r\n"));
#else
// Debug output
OutputDebugString(_T("Spam Filter: RegExp support disabled.\r\n"));
#endif
}
void UninitRegExp(void)
{
if (hPcreDLL) FreeLibrary(hPcreDLL);
}
|