summaryrefslogtreecommitdiff
path: root/plugins/FingerPrintModPlus/src/utilities.cpp
blob: 35a5bf0647fc1b615b1d9de7ec0a3204873cf363 (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
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
/*
Fingerprint Mod+ (client version) icons module for Miranda IM

Copyright © 2006-2007 Artem Shpynov aka FYR, Bio, Faith Healer. 2009-2010 HierOS

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.
*/

//Start of header
#include "global.h"

LPWSTR make_unicode_string(LPCSTR utf8);
LPWSTR make_unicode_string_static(LPCSTR utf8, LPWSTR unicode, size_t unicode_len);
LPSTR make_utf8_string(LPCWSTR unicode);
LPSTR make_utf8_string_static(LPCWSTR unicode, LPSTR utf8, size_t utf_size);

LPVOID __fastcall SAFE_MALLOC(size_t size)
{
	LPVOID p = NULL;

	if(size)
	{
		p = malloc(size);

		if(p)
			ZeroMemory(p, size);
	}
	return p;
}

size_t __fastcall strlennull(LPCSTR string)
{
	if(string)
		return strlen(string);

	return 0;
}

int null_snprintf(LPSTR buffer, size_t count, LPCSTR fmt, ...)
{
	va_list va;
	int len;

	ZeroMemory(buffer, count);
	va_start(va, fmt);
	len = _vsnprintf(buffer, count-1, fmt, va);
	va_end(va);
	return len;
}

LPSTR __fastcall TranslateUtfStatic(LPCSTR src, LPSTR buf, size_t bufsize)
{ // this takes UTF-8 strings only!!!
	if(strlennull(src))
	{ // we can use unicode translate (0.5+)
		LPWSTR usrc = make_unicode_string(src);

		make_utf8_string_static(TranslateW(usrc), buf, bufsize);

		SAFE_FREE(usrc);
	}
	else
		buf[0] = '\0';

	return buf;
}

//unsigned char *make_utf8_string_static(LPCWSTR unicode, unsigned char *utf8, size_t utf_size)
LPSTR make_utf8_string_static(LPCWSTR unicode, LPSTR utf8, size_t utf_size)
{
	size_t index = 0;
	size_t out_index = 0;
	WORD c;

	c = (WORD)unicode[index++];
	while(c)
	{
		if(c < 0x080)
		{
			if(out_index + 1 >= utf_size) break;
			utf8[out_index++] = (CHAR)c;
		}
		else if(c < 0x800) 
		{
			if(out_index + 2 >= utf_size) break;
			utf8[out_index++] = 0xc0 | (c >> 6);
			utf8[out_index++] = 0x80 | (c & 0x3f);
		}
		else
		{
			if(out_index + 3 >= utf_size) break;
			utf8[out_index++] = 0xe0 | (c >> 12);
			utf8[out_index++] = 0x80 | ((c >> 6) & 0x3f);
			utf8[out_index++] = 0x80 | (c & 0x3f);
		}
		c = (WORD)unicode[index++];
	}
	utf8[out_index] = 0x00;

	return utf8;
}

LPSTR make_utf8_string(LPCWSTR unicode)
{
	size_t size = 0;
	size_t index = 0;
	LPSTR out;
	WORD c;

	if (!unicode) return NULL;

	/* first calculate the size of the target string */
	c = (WORD)unicode[index++];
	while(c)
	{
		if(c < 0x0080)
			size++;
		else if(c < 0x0800)
			size += 2;
		else
			size += 3;
		c = (WORD)unicode[index++];
	}

	//out = (unsigned char*)SAFE_MALLOC(size + 1);
	out = (LPSTR)SAFE_MALLOC(size + 1);
	if(out == NULL)
		return NULL;
	else
		return make_utf8_string_static(unicode, out, size + 1);
}

LPWSTR make_unicode_string_static(LPCSTR utf8, LPWSTR unicode, size_t unicode_len)
{
	size_t index = 0;
	size_t out_index = 0;
	BYTE c;

	c = (BYTE)utf8[index++];
	while(c)
	{
		if(out_index + 1 >= unicode_len) break;
		if ((c & 0x80) == 0)
		{
			unicode[out_index++] = c;
		} 
		else if ((c & 0xe0) == 0xe0)
		{
			unicode[out_index] = (c & 0x1F) << 12;
			c = (BYTE)utf8[index++];
			unicode[out_index] |= (c & 0x3F) << 6;
			c = (BYTE)utf8[index++];
			unicode[out_index++] |= (c & 0x3F);
		}
		else
		{
			unicode[out_index] = (c & 0x3F) << 6;
			c = (BYTE)utf8[index++];
			unicode[out_index++] |= (c & 0x3F);
		}
		c = (BYTE)utf8[index++];
	}
	unicode[out_index] = 0;

	return unicode;
}

LPWSTR make_unicode_string(LPCSTR utf8)
{
	size_t size = 0, index = 0;
	LPWSTR out;
	BYTE c;

	if (!utf8) return NULL;

	/* first calculate the size of the target string */
	c = (BYTE)utf8[index++];
	while(c)
	{
		if ((c & 0x80) == 0)
		{
			index += 0;
		}
		else if ((c & 0xe0) == 0xe0)
		{
			index += 2;
		}
		else
		{
			index++;
		}
		size++;
		c = (BYTE)utf8[index++];
	}

	out = (LPWSTR)SAFE_MALLOC((size + 1) * sizeof(WCHAR));
	if(out == NULL)
		return NULL;
	else
		return make_unicode_string_static(utf8, out, size + 1);
}

int UTF8_IsValid(LPCSTR pszInput)
{
	int nb, i;
	LPCSTR c = pszInput;

	if (!pszInput) return 0;

	for(c = pszInput; *c; c += (nb + 1))
	{
		if (!(*c & 0x80)) nb = 0;
		else if ((*c & 0xc0) == 0x80) return 0;
		else if ((*c & 0xe0) == 0xc0) nb = 1;
		else if ((*c & 0xf0) == 0xe0) nb = 2;
		else if ((*c & 0xf8) == 0xf0) nb = 3;
		else if ((*c & 0xfc) == 0xf8) nb = 4;
		else if ((*c & 0xfe) == 0xfc) nb = 5;

		for(i = 1; i <= nb; i++) // we this forward, do not cross end of string
			if ((*(c + i) & 0xc0) != 0x80)
				return 0;
	}

	return 1;
}

static BOOL bHasCP_UTF8 = FALSE;
int utf8_decode_static(LPCSTR from, LPSTR to, int to_size)
{
	int nResult = 0;
	// Validate the string
	if (!UTF8_IsValid(from))
		return 0;

	// Use the native conversion routines when available
	if(bHasCP_UTF8)
	{
		LPWSTR wszTemp = NULL;
		size_t inlen = strlennull(from);

		wszTemp = (LPWSTR)_alloca((inlen + 1) * sizeof(WCHAR));

		// Convert the UTF-8 string to UCS
		if(MultiByteToWideChar(CP_UTF8, 0, from, -1, wszTemp, (int)inlen + 1))
		{
			// Convert the UCS string to local ANSI codepage
			if(WideCharToMultiByte(CP_ACP, 0, wszTemp, -1, to, to_size, NULL, NULL))
			{
				nResult = 1;
			}
		}
	}
	else
	{
		size_t chars = strlennull(from) + 1;
		LPWSTR unicode = (LPWSTR)_alloca(chars * sizeof(WCHAR));
    
		make_unicode_string_static(from, unicode, chars);

		WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, unicode, -1, to, to_size, NULL, NULL);

		nResult = 1;
	}

	return nResult;
}

static LRESULT ControlAddStringUtf(HWND ctrl, DWORD msg, LPCSTR szString)
{
	char str[MAX_PATH];
	LPSTR szItem = TranslateUtfStatic(szString, str, MAX_PATH);
	LRESULT item = -1;

	if(gbUnicodeAPI)
	{
		LPWSTR wItem = make_unicode_string(szItem);

		item = SendMessageW(ctrl, msg, 0, (LPARAM)wItem);
		SAFE_FREE(wItem);
	}
	else
	{
		size_t size = strlennull(szItem) + 2;
		LPSTR aItem = (LPSTR)_alloca(size);

		if(utf8_decode_static(szItem, aItem, (int)size))
		item = SendMessageA(ctrl, msg, 0, (LPARAM)aItem);
	}
	return item;
}

static LRESULT ControlAddStringW(HWND ctrl, DWORD msg, LPCWSTR wszString)
{
	return SendMessageW(ctrl, msg, 0, (LPARAM)TranslateW(wszString));
}

LRESULT ComboBoxAddStringUtf(HWND hCombo, LPCSTR szString, DWORD data)
{
	LRESULT item = ControlAddStringUtf(hCombo, CB_ADDSTRING, szString);
	SendMessage(hCombo, CB_SETITEMDATA, item, data);

	return item;
}

LRESULT ComboBoxAddStringW(HWND hCombo, LPCWSTR wszString, DWORD data)
{
	LRESULT item = ControlAddStringW(hCombo, CB_ADDSTRING, wszString);
	SendMessageW(hCombo, CB_SETITEMDATA, item, data);

	return item;
}

LRESULT ListBoxAddStringUtf(HWND hList, LPCSTR szString)
{
	return ControlAddStringUtf(hList, LB_ADDSTRING, szString);
}

LRESULT ListBoxAddStringW(HWND hList, LPCWSTR wszString)
{
	return ControlAddStringW(hList, LB_ADDSTRING, wszString);
}