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
|
/*
IEView Plugin for Miranda IM
Copyright (C) 2005-2010 Piotr Piastucki
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.
*/
#include "ieview_common.h"
wchar_t Utils::base_dir[MAX_PATH];
unsigned Utils::hookNum = 0;
unsigned Utils::serviceNum = 0;
HANDLE* Utils::hHooks = NULL;
HANDLE* Utils::hServices = NULL;
const wchar_t *Utils::getBaseDir() {
char temp[MAX_PATH];
base_dir[0] = '\0';
long tlen = PathToAbsolute("miranda32.exe", temp);
if (tlen) {
temp[tlen - 13]=0;
MultiByteToWideChar(CP_ACP, 0, temp, (int)strlen(temp), base_dir, MAX_PATH);
}
return base_dir;
}
wchar_t* Utils::toAbsolute(wchar_t* relative) {
const wchar_t* bdir = getBaseDir();
long len = (int)wcslen(bdir);
long tlen = len + (int)wcslen(relative);
wchar_t* result = new wchar_t[tlen + 1];
if(result){
wcscpy(result,bdir);
wcscpy(result + len,relative);
}
return result;
}
static int countNoWhitespace(const wchar_t *str) {
int c;
for (c=0; *str!='\n' && *str!='\r' && *str!='\t' && *str!=' ' && *str!='\0'; str++, c++);
return c;
}
void Utils::appendText(char **str, int *sizeAlloced, const char *fmt, ...) {
va_list vararg;
char *p;
int size, len;
if (str == NULL) return;
if (*str==NULL || *sizeAlloced<=0) {
*sizeAlloced = size = 2048;
*str = (char *) malloc(size);
len = 0;
}
else {
len = (int)strlen(*str);
size = *sizeAlloced - len;
}
if (size < 128) {
size += 2048;
(*sizeAlloced) += 2048;
*str = (char *) realloc(*str, *sizeAlloced);
}
p = *str + len;
va_start(vararg, fmt);
while (_vsnprintf(p, size - 1, fmt, vararg) == -1) {
size += 2048;
(*sizeAlloced) += 2048;
*str = (char *) realloc(*str, *sizeAlloced);
p = *str + len;
}
p[ size - 1 ] = '\0';
va_end(vararg);
}
void Utils::appendText(wchar_t **str, int *sizeAlloced, const wchar_t *fmt, ...) {
va_list vararg;
wchar_t *p;
int size, len;
if (str == NULL) return;
if (*str==NULL || *sizeAlloced<=0) {
*sizeAlloced = size = 2048;
*str = (wchar_t *) malloc(size);
len = 0;
}
else {
len = (int)wcslen(*str);
size = *sizeAlloced - sizeof(wchar_t) * len;
}
if (size < 128) {
size += 2048;
(*sizeAlloced) += 2048;
*str = (wchar_t *) realloc(*str, *sizeAlloced);
}
p = *str + len;
va_start(vararg, fmt);
while (_vsnwprintf(p, size / sizeof(wchar_t) - 1, fmt, vararg) == -1) {
size += 2048;
(*sizeAlloced) += 2048;
*str = (wchar_t *) realloc(*str, *sizeAlloced);
p = *str + len;
}
p[ size / sizeof(wchar_t) - 1] = '\0';
va_end(vararg);
}
char *Utils::dupString(const char *a) {
if (a!=NULL) {
char *b = new char[strlen(a)+1];
strcpy(b, a);
return b;
}
return NULL;
}
char *Utils::dupString(const char *a, int l) {
if (a!=NULL) {
char *b = new char[l+1];
strncpy(b, a, l);
b[l] ='\0';
return b;
}
return NULL;
}
wchar_t *Utils::dupString(const wchar_t *a) {
if (a!=NULL) {
wchar_t *b = new wchar_t[wcslen(a)+1];
wcscpy(b, a);
return b;
}
return NULL;
}
wchar_t *Utils::dupString(const wchar_t *a, int l) {
if (a!=NULL) {
wchar_t *b = new wchar_t[l+1];
wcsncpy(b, a, l);
b[l] ='\0';
return b;
}
return NULL;
}
wchar_t *Utils::convertToWCS(const char *a) {
if (a!=NULL) {
int len = (int)strlen(a)+1;
wchar_t *b = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, a, len, b, len);
return b;
}
return NULL;
}
wchar_t *Utils::convertToWCS(const char *a, int cp) {
if (a!=NULL) {
int len = (int)strlen(a)+1;
wchar_t *b = new wchar_t[len];
MultiByteToWideChar(cp, 0, a, len, b, len);
return b;
}
return NULL;
}
char *Utils::convertToString(const wchar_t *a) {
if (a!=NULL) {
int len = (int)wcslen(a)+1;
char *b = new char[len];
WideCharToMultiByte(CP_ACP, 0, a, len, b, len, NULL, FALSE);
return b;
}
return NULL;
}
char *Utils::convertToString(const wchar_t *a, int cp) {
if (a!=NULL) {
int len = (int)wcslen(a)+1;
char *b = new char[len];
WideCharToMultiByte(cp, 0, a, len, b, len, NULL, FALSE);
return b;
}
return NULL;
}
void Utils::convertPath(char *path) {
if (path != NULL) {
for (; *path!='\0'; path++) {
if (*path == '\\') *path = '/';
}
}
}
void Utils::convertPath(wchar_t *path) {
if (path != NULL) {
for (; *path!='\0'; path++) {
if (*path == '\\') *path = '/';
}
}
}
DWORD Utils::safe_wcslen(wchar_t *msg, DWORD maxLen) {
DWORD i;
for (i = 0; i < maxLen; i++) {
if (msg[i] == (wchar_t)0)
return i;
}
return 0;
}
int Utils::detectURL(const wchar_t *text) {
int i;
for (i=0;text[i]!='\0';i++) {
if (!((text[i] >= '0' && text[i]<='9') || iswalpha(text[i]))) {
break;
}
}
if (i > 0 && text[i]==':' && text[i+1]=='/' && text[i+2]=='/') {
i += countNoWhitespace(text+i);
for (; i > 0; i --) {
if ((text[i-1] >= '0' && text[i-1]<='9') || iswalpha(text[i-1]) || text[i-1]=='/') {
break;
}
}
return i;
}
return 0;
}
char *Utils::escapeString(const char *a) {
int i, l, len;
char *out;
if (a == NULL) {
return NULL;
}
len = (int)strlen(a);
for (i = l = 0; i < len; i++, l++) {
if (a[i] == '\\' || a[i] == '\n' || a[i] == '\r' || a[i] == '\"'
|| a[i] == '\'' || a[i] == '\b' || a[i] == '\t' || a[i] == '\f') {
l++;
}
}
out = new char[l+1];
for (i = l = 0; i < len; i++, l++) {
if (a[i] == '\\' || a[i] == '\n' || a[i] == '\r' || a[i] == '\"'
|| a[i] == '\'' || a[i] == '\b' || a[i] == '\t' || a[i] == '\f') {
out[l++] = '\\';
}
out[l] = a[i];
}
out[l] = '\0';
return out;
}
struct FORK_ARG {
HANDLE hEvent;
void (__cdecl *threadcode)(void*);
void *arg;
};
static void __cdecl forkthread_r(struct FORK_ARG *fa)
{
void (*callercode)(void*) = fa->threadcode;
void *arg = fa->arg;
Thread_Push((HINSTANCE)callercode);
SetEvent(fa->hEvent);
callercode(arg);
Thread_Pop();
return;
}
unsigned long Utils::forkThread(void (__cdecl *threadcode)(void*), unsigned long stacksize, void *arg) {
unsigned long rc;
struct FORK_ARG fa;
fa.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
fa.threadcode = threadcode;
fa.arg = arg;
rc = _beginthread((void (__cdecl *)(void*))forkthread_r, stacksize, &fa);
if ((unsigned long) -1L != rc) {
WaitForSingleObject(fa.hEvent, INFINITE);
}
CloseHandle(fa.hEvent);
return rc;
}
HANDLE Utils::hookEvent_Ex(const char *name, MIRANDAHOOK hook) {
hookNum ++;
hHooks = (HANDLE *) mir_realloc(hHooks, sizeof(HANDLE) * (hookNum));
hHooks[hookNum - 1] = HookEvent(name, hook);
return hHooks[hookNum - 1] ;
}
HANDLE Utils::createServiceFunction_Ex(const char *name, MIRANDASERVICE service) {
serviceNum++;
hServices = (HANDLE *) mir_realloc(hServices, sizeof(HANDLE) * (serviceNum));
hServices[serviceNum - 1] = CreateServiceFunction(name, service);
return hServices[serviceNum - 1] ;
}
void Utils::unhookEvents_Ex() {
unsigned i;
for (i=0; i<hookNum ; ++i) {
if (hHooks[i] != NULL) {
UnhookEvent(hHooks[i]);
}
}
mir_free(hHooks);
hookNum = 0;
hHooks = NULL;
}
void Utils::destroyServices_Ex() {
unsigned i;
for (i=0; i<serviceNum; ++i) {
if (hServices[i] != NULL) {
DestroyServiceFunction(hServices[i]);
}
}
mir_free(hServices);
serviceNum = 0;
hServices = NULL;
}
wchar_t *Utils::urlEncode(const wchar_t *text) {
char *utf8 = mir_utf8encodeT(text);
wchar_t *result = urlEncode(utf8);
mir_free(utf8);
return result;
}
wchar_t *Utils::urlEncode(const char *text) {
char *pszReturnString = (char *)CallService(MS_NETLIB_URLENCODE, 0, (LPARAM)text);
wchar_t *result = convertToWCS(pszReturnString);
HeapFree(GetProcessHeap(), 0, pszReturnString);
return result;
}
void Utils::appendIcon(char **str, int *sizeAlloced, const char *iconFile) {
Utils::appendText(str, sizeAlloced, "<img class=\"img\" src=\"file://%s/plugins/ieview/%s\"/> ", workingDirUtf8, iconFile);
}
|