aboutsummaryrefslogtreecommitdiff
path: root/Src/omBrowser/addressEncoder.cpp
blob: 199cc7012ed0791fdc22e164d64f2152e79733fb (plain) (blame)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include "main.h"
#include "./addressEncoder.h"


#include <wininet.h>
#include <strsafe.h>


typedef struct __ENCODEBUFFER
{
	LPWSTR buffer;
	size_t bufferMax;
	LPWSTR cursor;
	size_t remaining; 
} ENCODEBUFFER;

HRESULT AddressEncoder_ReAllocBuffer(ENCODEBUFFER *decoder, size_t cchBufferSize)
{
	if (NULL == decoder) 
		return E_INVALIDARG;

	if (cchBufferSize == decoder->bufferMax)
		return S_FALSE;

	if (cchBufferSize < decoder->bufferMax)
		return E_FAIL;

	LPWSTR test = Plugin_ReAllocString(decoder->buffer, cchBufferSize);
	if (NULL == test)
		return E_OUTOFMEMORY;
		
	decoder->cursor = test  + (decoder->cursor - decoder->buffer);
	decoder->remaining += (cchBufferSize - decoder->bufferMax); 
	decoder->buffer = test;
	decoder->bufferMax = cchBufferSize;
	
	return S_OK;
}
HRESULT AddressEncoder_AppendAnsiString(ENCODEBUFFER *decoder, LPCSTR pszString, size_t cchString)
{
	if (NULL == decoder)
		return E_INVALIDARG;

	INT cchConverted; 
	while(0 ==(cchConverted = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pszString, (int)cchString, decoder->cursor, (int)decoder->remaining)))
	{
		DWORD errorCode = GetLastError();
		if (ERROR_INSUFFICIENT_BUFFER == errorCode)
		{
			INT cchNeed = MultiByteToWideChar(CP_UTF8, 0, pszString, (int)cchString, NULL, 0) - (INT)decoder->remaining;
			if (cchNeed < 32) cchNeed = 32;
			HRESULT hr = AddressEncoder_ReAllocBuffer(decoder, decoder->bufferMax + cchNeed);
			if (FAILED(hr)) 
				return hr;
		}
		else
		{
			return HRESULT_FROM_WIN32(errorCode);
		}
	}

	if (0 != cchConverted)
	{
		decoder->cursor += cchConverted;
		decoder->remaining -= cchConverted;
	}

	return S_OK;
}

HRESULT AddressEncoder_AppendString(ENCODEBUFFER *decoder, LPCWSTR pszString, size_t cchString)
{
	if (NULL == decoder)
		return E_INVALIDARG;

	LPWSTR cursor;
	size_t remaining;

	HRESULT hr;
	if (cchString >= decoder->remaining)
	{
		hr = AddressEncoder_ReAllocBuffer(decoder, decoder->bufferMax + (cchString - decoder->remaining) + 1);
		if (FAILED(hr)) return hr;
	}

	hr = StringCchCopyNEx(decoder->cursor, decoder->remaining, pszString, cchString, &cursor, &remaining, 0);
	if (SUCCEEDED(hr))
	{
		decoder->cursor = cursor;
		decoder->remaining = remaining;
	}
	return hr;
}

HRESULT AddressEncoder_GetEscapeBlock(LPCWSTR pszEscape, LPCWSTR *ppszEnd, size_t *pcchEscapeLen, LPSTR pszBuffer, UINT *pcbBufferMax)
{
	if (NULL == pszEscape || (NULL != pszBuffer && NULL == pcbBufferMax))
		return E_INVALIDARG;

	UINT cbBinary = 0;
	WORD charInfo;
	WCHAR szDigit[3] = {0};

	HRESULT hr = S_OK;

	LPCWSTR cursor = pszEscape;
	while (L'%' == *cursor)
	{
		LPCWSTR testChar = CharNext(cursor);
		if (L'\0' == *testChar || 
			FALSE == GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, testChar, 1, &charInfo) ||
			0 == (C1_XDIGIT & charInfo))
		{
			break;
		}
		szDigit[0] = *testChar;

		testChar = CharNext(testChar);
		if (L'\0' == *testChar || 
			FALSE == GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, testChar, 1, &charInfo) ||
			0 == (C1_XDIGIT & charInfo))
		{
			break;
		}

		szDigit[1] = *testChar;
		CharUpperBuff(szDigit, 2);

		BYTE binaryData = ((szDigit[0] - ((szDigit[0] <= L'9' ? L'0' : L'A' - 10))) << 4) & 0xf0;
		binaryData += (szDigit[1] - ((szDigit[1] <= L'9' ? L'0' : L'A' - 10))) & 0x0f;
		if (NULL != pszBuffer)
		{
			if (cbBinary < *pcbBufferMax)
				pszBuffer[cbBinary] = binaryData;
			else
				hr = E_OUTOFMEMORY;
		}
		cbBinary++;

		cursor = CharNext(testChar);
	}

	if (cursor == pszEscape)
		hr = HRESULT_FROM_WIN32(ERROR_INVALID_BLOCK_LENGTH);

	if (NULL != ppszEnd)
		*ppszEnd = cursor;
	
	if (NULL != pcchEscapeLen)
		*pcchEscapeLen = (size_t)(cursor - pszEscape);

	if (NULL != pcbBufferMax)
		*pcbBufferMax = cbBinary;
	
	return hr;
}

HRESULT AddressEncoder_DecodeString(LPCWSTR pszUrl, LPWSTR *ppResult)
{	
	if (NULL == pszUrl)
	{
		*ppResult = NULL;
		return S_FALSE;
	}

	UINT cchUrl = 0;
	UINT escapeSize  = 0;
	UINT escapeBlockSize,escapeBlockMaxSize = 0;
	LPCWSTR escapeBlockEnd;
	for (LPCWSTR cursor = pszUrl; L'\0' != *cursor;)
	{		
		if (L'%' == *cursor && SUCCEEDED(AddressEncoder_GetEscapeBlock(cursor, &escapeBlockEnd, NULL, NULL, &escapeBlockSize)))
		{
			escapeSize += escapeBlockSize;
			if (escapeBlockSize > escapeBlockMaxSize)
				escapeBlockMaxSize = escapeBlockSize;

			cursor = escapeBlockEnd;
		}
		else
		{
			cchUrl++;
			cursor = CharNext(cursor);
		}
	}

	if (0 == escapeSize)
	{
		*ppResult = Plugin_CopyString(pszUrl);
		if (NULL == *ppResult) return E_OUTOFMEMORY;
		return S_FALSE;
	}

	HRESULT hr = S_OK;
		
	ENCODEBUFFER decoder;
	ZeroMemory(&decoder, sizeof(decoder));
	
	LPSTR escapeBuffer = Plugin_MallocAnsiString(escapeBlockMaxSize);
	if (NULL == escapeBuffer)
	{
		hr = E_OUTOFMEMORY;
	}
	else
	{
		hr = AddressEncoder_ReAllocBuffer(&decoder, cchUrl + (escapeSize + 1)* sizeof(WCHAR));
		if (SUCCEEDED(hr))
		{
			LPCWSTR cursor = pszUrl; 
			LPCWSTR copyBlock =  cursor;
			
			for (;;)
			{		
				escapeBlockSize = escapeBlockMaxSize;
				if (L'%' == *cursor && SUCCEEDED(AddressEncoder_GetEscapeBlock(cursor, &escapeBlockEnd, NULL, escapeBuffer, &escapeBlockSize)))
				{
					if (copyBlock != cursor)
					{
						hr = AddressEncoder_AppendString(&decoder, copyBlock, cursor - copyBlock);
						if (FAILED(hr)) 
							break;
						copyBlock = cursor;
					}

					HRESULT convertResult = AddressEncoder_AppendAnsiString(&decoder, escapeBuffer, escapeBlockSize);
					if (L'\0' == *cursor)
						break;
					
					cursor = escapeBlockEnd;
					if (SUCCEEDED(convertResult))
					{
						copyBlock = cursor;
					}
					
					
					
					continue;
				}
				
				if (L'\0' == *cursor)
				{
					if (copyBlock != cursor)
						hr = AddressEncoder_AppendString(&decoder, copyBlock, cursor - copyBlock);
					break;
				}
				else
					cursor = CharNext(cursor);
			}

		}

	}

	if (NULL != escapeBuffer)
		Plugin_FreeAnsiString(escapeBuffer);

	if (FAILED(hr))
	{
		Plugin_FreeString(decoder.buffer);
		decoder.buffer = NULL;
	}
	else
	{
		*decoder.cursor = L'\0';
	}

	*ppResult = decoder.buffer;
	
	return hr;
}
HRESULT AddressEncoder_GetWideBlock(LPCWSTR pszWide, LPCWSTR *pszEnd, LPWSTR pszBuffer, size_t *pcchBufferMax)
{
	LPCWSTR cursor = pszWide;
	if (NULL == pszWide) 
		return E_INVALIDARG;
	
	if (NULL != pszBuffer && NULL == pcchBufferMax) 
		return E_INVALIDARG;
	
	while (L'\0' == *cursor || *cursor > 0xFF)
	{
		if (L'\0' == *cursor)
			break;
		cursor = CharNext(cursor);
	}

	if (NULL != pszEnd)
		*pszEnd = cursor;

	HRESULT hr = S_OK;
	size_t cchBuffer = 0;

	if (cursor == pszWide)
	{
		hr = S_FALSE;
	}
	else
	{
		size_t bytesCount = WideCharToMultiByte(CP_UTF8, 0, pszWide, (int)(cursor - pszWide), NULL, 0, NULL, NULL);
		if (0 == bytesCount)
		{
			DWORD errorCode = GetLastError();
			if (ERROR_SUCCESS != errorCode)
				hr = HRESULT_FROM_WIN32(errorCode);
		}
		else
		{
			cchBuffer = 3 * bytesCount;
			if (NULL != pszBuffer)
			{
				if (*pcchBufferMax >= cchBuffer)
				{
					LPWSTR p = pszBuffer;
					BYTE *bytes = ((BYTE*)(pszBuffer + *pcchBufferMax)) - bytesCount;
					WideCharToMultiByte(CP_UTF8, 0, pszWide,  (int)(cursor - pszWide), (LPSTR)bytes, (int)bytesCount, NULL, NULL);
					for (size_t i = 0; i < bytesCount; i++)
					{
						BYTE b = bytes[i];
						*p++ = L'%';
						BYTE c = (b >> 4) & 0x0F;
						*p++ = (c < 10) ? (L'0' + c) : (L'A' + (c -10));
						c = b & 0x0F;
						*p++ = (c < 10) ? (L'0' + c) : (L'A' + (c -10));
					}

				}
				else
				{
					hr = E_OUTOFMEMORY;
				}
			}
		}
	}

	if (NULL != pcchBufferMax)
	{
		*pcchBufferMax = cchBuffer;
	}


	return hr;
}
HRESULT AddressEncoder_EncodeWideChars(LPCWSTR pszAddress, size_t cchAddress, LPWSTR *ppResult)
{
	if (NULL == ppResult)
		return E_POINTER;
	
	if (NULL == pszAddress)
	{
		*ppResult = NULL;
		return S_FALSE;
	}

	LPCWSTR blockEnd;
	size_t blockSize;
	size_t cchResultMax = 0;
	BOOL needEncode = FALSE;
	for (LPCWSTR cursor = pszAddress; L'\0' != *cursor;)
	{
		if (*cursor > 0xFF && SUCCEEDED(AddressEncoder_GetWideBlock(cursor, &blockEnd, NULL, &blockSize)))
		{
			cursor = blockEnd;
			cchResultMax += blockSize;
			needEncode = TRUE;
		}
		else
		{
			cursor = CharNext(cursor);
			cchResultMax++;
		}
	}

	if (FALSE == needEncode)
	{
		*ppResult = NULL;
		return S_FALSE;
	}

	HRESULT hr;
	cchResultMax++;
	LPWSTR result = Plugin_MallocString(cchResultMax);
	if (NULL == result)
		hr = E_OUTOFMEMORY;
	else
	{
		LPWSTR cursor = result;
		size_t remaining = cchResultMax;
		LPCWSTR address = pszAddress;
		LPCWSTR addressBlock = address;	

		for (;;)
		{		
			if (*address > 0xFF)
			{
				if (addressBlock != address)
				{
					hr = StringCchCopyNEx(cursor, remaining, addressBlock, (size_t)(address - addressBlock), &cursor, &remaining, 0);
					if (FAILED(hr))	break;
				}

				blockSize = remaining;
				hr = AddressEncoder_GetWideBlock(address, &address, cursor, &blockSize);
				if (FAILED(hr)) break;

				cursor += blockSize;
				remaining -= blockSize;

				addressBlock = address;
				continue;
			}
			
			if (L'\0' == *address)
			{
				if (addressBlock != address)
				{
					hr = StringCchCopyNEx(cursor, remaining, addressBlock, (size_t)(address - addressBlock), &cursor, &remaining, 0);
				}
				break;
			}
			else
				address = CharNext(address);
		}

		*cursor = L'\0';
	}

	

	if (FAILED(hr))
	{
		Plugin_FreeString(result);
		result = NULL;
	}

	*ppResult = result;
	return hr;
}

HRESULT AddressEncoder_EncodeString(LPCWSTR pszAddress, LPWSTR pszBuffer, size_t *pcchBufferMax, UINT flags)
{
	if (NULL == pszBuffer || NULL == pcchBufferMax) 
		return E_INVALIDARG;
	
	if (NULL == pszAddress || L'\0' == *pszAddress)
	{
		*pszBuffer = L'\0';
		*pcchBufferMax = 0;
		return S_OK;
	}

	INT cchAddress = lstrlen(pszAddress);
	LPCWSTR begin, end;
	begin = pszAddress;
	end = pszAddress + cchAddress;
	WORD charType;
	while (L'\0' != *begin &&
		FALSE != GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, begin, 1, &charType) && 0 != (C1_SPACE & charType))
	{
		begin = CharNext(begin);
	}
	while (begin != end && 
		FALSE != GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, begin, 1, &charType) && 0 != (C1_SPACE & charType))
	{
		end = CharPrev(begin, end);
	}

	if (end <= begin)
	{
		*pszBuffer = L'\0';
		*pcchBufferMax = 0;
		return S_OK;
	}

	LPWSTR encoded;
	HRESULT hr = AddressEncoder_EncodeWideChars(begin, (end - begin), &encoded);
	if (FAILED(hr)) return hr;

	if (S_OK == hr)
	{
		begin = encoded;
		end = begin + lstrlen(begin);
	}
	
	DWORD bufferLen = (DWORD)(*pcchBufferMax);
	if (FALSE == InternetCanonicalizeUrl(begin, pszBuffer, &bufferLen, flags))
	{
		DWORD errorCode = GetLastError();
		if (ERROR_INSUFFICIENT_BUFFER == errorCode)
		{
			*pcchBufferMax = bufferLen;
			hr = ENC_E_INSUFFICIENT_BUFFER;	
		}
		else 
		{
			size_t cchNeeded = (end - begin);
			if (cchNeeded < *pcchBufferMax)
			{
				hr = StringCchCopyN(pszBuffer, *pcchBufferMax, begin, cchNeeded);
				if (STRSAFE_E_INSUFFICIENT_BUFFER == hr)
				{ 
					hr = ENC_E_INSUFFICIENT_BUFFER;
				}
			}
			else
			{
				hr = ENC_E_INSUFFICIENT_BUFFER;
			}
			*pcchBufferMax = cchNeeded + 1;
		}
	}

	else
		hr = S_OK;
	
	
	Plugin_FreeString(encoded);
	return hr;
}