addressEncoder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. #include "main.h"
  2. #include "./addressEncoder.h"
  3. #include <wininet.h>
  4. #include <strsafe.h>
  5. typedef struct __ENCODEBUFFER
  6. {
  7. LPWSTR buffer;
  8. size_t bufferMax;
  9. LPWSTR cursor;
  10. size_t remaining;
  11. } ENCODEBUFFER;
  12. HRESULT AddressEncoder_ReAllocBuffer(ENCODEBUFFER *decoder, size_t cchBufferSize)
  13. {
  14. if (NULL == decoder)
  15. return E_INVALIDARG;
  16. if (cchBufferSize == decoder->bufferMax)
  17. return S_FALSE;
  18. if (cchBufferSize < decoder->bufferMax)
  19. return E_FAIL;
  20. LPWSTR test = Plugin_ReAllocString(decoder->buffer, cchBufferSize);
  21. if (NULL == test)
  22. return E_OUTOFMEMORY;
  23. decoder->cursor = test + (decoder->cursor - decoder->buffer);
  24. decoder->remaining += (cchBufferSize - decoder->bufferMax);
  25. decoder->buffer = test;
  26. decoder->bufferMax = cchBufferSize;
  27. return S_OK;
  28. }
  29. HRESULT AddressEncoder_AppendAnsiString(ENCODEBUFFER *decoder, LPCSTR pszString, size_t cchString)
  30. {
  31. if (NULL == decoder)
  32. return E_INVALIDARG;
  33. INT cchConverted;
  34. while(0 ==(cchConverted = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pszString, (int)cchString, decoder->cursor, (int)decoder->remaining)))
  35. {
  36. DWORD errorCode = GetLastError();
  37. if (ERROR_INSUFFICIENT_BUFFER == errorCode)
  38. {
  39. INT cchNeed = MultiByteToWideChar(CP_UTF8, 0, pszString, (int)cchString, NULL, 0) - (INT)decoder->remaining;
  40. if (cchNeed < 32) cchNeed = 32;
  41. HRESULT hr = AddressEncoder_ReAllocBuffer(decoder, decoder->bufferMax + cchNeed);
  42. if (FAILED(hr))
  43. return hr;
  44. }
  45. else
  46. {
  47. return HRESULT_FROM_WIN32(errorCode);
  48. }
  49. }
  50. if (0 != cchConverted)
  51. {
  52. decoder->cursor += cchConverted;
  53. decoder->remaining -= cchConverted;
  54. }
  55. return S_OK;
  56. }
  57. HRESULT AddressEncoder_AppendString(ENCODEBUFFER *decoder, LPCWSTR pszString, size_t cchString)
  58. {
  59. if (NULL == decoder)
  60. return E_INVALIDARG;
  61. LPWSTR cursor;
  62. size_t remaining;
  63. HRESULT hr;
  64. if (cchString >= decoder->remaining)
  65. {
  66. hr = AddressEncoder_ReAllocBuffer(decoder, decoder->bufferMax + (cchString - decoder->remaining) + 1);
  67. if (FAILED(hr)) return hr;
  68. }
  69. hr = StringCchCopyNEx(decoder->cursor, decoder->remaining, pszString, cchString, &cursor, &remaining, 0);
  70. if (SUCCEEDED(hr))
  71. {
  72. decoder->cursor = cursor;
  73. decoder->remaining = remaining;
  74. }
  75. return hr;
  76. }
  77. HRESULT AddressEncoder_GetEscapeBlock(LPCWSTR pszEscape, LPCWSTR *ppszEnd, size_t *pcchEscapeLen, LPSTR pszBuffer, UINT *pcbBufferMax)
  78. {
  79. if (NULL == pszEscape || (NULL != pszBuffer && NULL == pcbBufferMax))
  80. return E_INVALIDARG;
  81. UINT cbBinary = 0;
  82. WORD charInfo;
  83. WCHAR szDigit[3] = {0};
  84. HRESULT hr = S_OK;
  85. LPCWSTR cursor = pszEscape;
  86. while (L'%' == *cursor)
  87. {
  88. LPCWSTR testChar = CharNext(cursor);
  89. if (L'\0' == *testChar ||
  90. FALSE == GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, testChar, 1, &charInfo) ||
  91. 0 == (C1_XDIGIT & charInfo))
  92. {
  93. break;
  94. }
  95. szDigit[0] = *testChar;
  96. testChar = CharNext(testChar);
  97. if (L'\0' == *testChar ||
  98. FALSE == GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, testChar, 1, &charInfo) ||
  99. 0 == (C1_XDIGIT & charInfo))
  100. {
  101. break;
  102. }
  103. szDigit[1] = *testChar;
  104. CharUpperBuff(szDigit, 2);
  105. BYTE binaryData = ((szDigit[0] - ((szDigit[0] <= L'9' ? L'0' : L'A' - 10))) << 4) & 0xf0;
  106. binaryData += (szDigit[1] - ((szDigit[1] <= L'9' ? L'0' : L'A' - 10))) & 0x0f;
  107. if (NULL != pszBuffer)
  108. {
  109. if (cbBinary < *pcbBufferMax)
  110. pszBuffer[cbBinary] = binaryData;
  111. else
  112. hr = E_OUTOFMEMORY;
  113. }
  114. cbBinary++;
  115. cursor = CharNext(testChar);
  116. }
  117. if (cursor == pszEscape)
  118. hr = HRESULT_FROM_WIN32(ERROR_INVALID_BLOCK_LENGTH);
  119. if (NULL != ppszEnd)
  120. *ppszEnd = cursor;
  121. if (NULL != pcchEscapeLen)
  122. *pcchEscapeLen = (size_t)(cursor - pszEscape);
  123. if (NULL != pcbBufferMax)
  124. *pcbBufferMax = cbBinary;
  125. return hr;
  126. }
  127. HRESULT AddressEncoder_DecodeString(LPCWSTR pszUrl, LPWSTR *ppResult)
  128. {
  129. if (NULL == pszUrl)
  130. {
  131. *ppResult = NULL;
  132. return S_FALSE;
  133. }
  134. UINT cchUrl = 0;
  135. UINT escapeSize = 0;
  136. UINT escapeBlockSize,escapeBlockMaxSize = 0;
  137. LPCWSTR escapeBlockEnd;
  138. for (LPCWSTR cursor = pszUrl; L'\0' != *cursor;)
  139. {
  140. if (L'%' == *cursor && SUCCEEDED(AddressEncoder_GetEscapeBlock(cursor, &escapeBlockEnd, NULL, NULL, &escapeBlockSize)))
  141. {
  142. escapeSize += escapeBlockSize;
  143. if (escapeBlockSize > escapeBlockMaxSize)
  144. escapeBlockMaxSize = escapeBlockSize;
  145. cursor = escapeBlockEnd;
  146. }
  147. else
  148. {
  149. cchUrl++;
  150. cursor = CharNext(cursor);
  151. }
  152. }
  153. if (0 == escapeSize)
  154. {
  155. *ppResult = Plugin_CopyString(pszUrl);
  156. if (NULL == *ppResult) return E_OUTOFMEMORY;
  157. return S_FALSE;
  158. }
  159. HRESULT hr = S_OK;
  160. ENCODEBUFFER decoder;
  161. ZeroMemory(&decoder, sizeof(decoder));
  162. LPSTR escapeBuffer = Plugin_MallocAnsiString(escapeBlockMaxSize);
  163. if (NULL == escapeBuffer)
  164. {
  165. hr = E_OUTOFMEMORY;
  166. }
  167. else
  168. {
  169. hr = AddressEncoder_ReAllocBuffer(&decoder, cchUrl + (escapeSize + 1)* sizeof(WCHAR));
  170. if (SUCCEEDED(hr))
  171. {
  172. LPCWSTR cursor = pszUrl;
  173. LPCWSTR copyBlock = cursor;
  174. for (;;)
  175. {
  176. escapeBlockSize = escapeBlockMaxSize;
  177. if (L'%' == *cursor && SUCCEEDED(AddressEncoder_GetEscapeBlock(cursor, &escapeBlockEnd, NULL, escapeBuffer, &escapeBlockSize)))
  178. {
  179. if (copyBlock != cursor)
  180. {
  181. hr = AddressEncoder_AppendString(&decoder, copyBlock, cursor - copyBlock);
  182. if (FAILED(hr))
  183. break;
  184. copyBlock = cursor;
  185. }
  186. HRESULT convertResult = AddressEncoder_AppendAnsiString(&decoder, escapeBuffer, escapeBlockSize);
  187. if (L'\0' == *cursor)
  188. break;
  189. cursor = escapeBlockEnd;
  190. if (SUCCEEDED(convertResult))
  191. {
  192. copyBlock = cursor;
  193. }
  194. continue;
  195. }
  196. if (L'\0' == *cursor)
  197. {
  198. if (copyBlock != cursor)
  199. hr = AddressEncoder_AppendString(&decoder, copyBlock, cursor - copyBlock);
  200. break;
  201. }
  202. else
  203. cursor = CharNext(cursor);
  204. }
  205. }
  206. }
  207. if (NULL != escapeBuffer)
  208. Plugin_FreeAnsiString(escapeBuffer);
  209. if (FAILED(hr))
  210. {
  211. Plugin_FreeString(decoder.buffer);
  212. decoder.buffer = NULL;
  213. }
  214. else
  215. {
  216. *decoder.cursor = L'\0';
  217. }
  218. *ppResult = decoder.buffer;
  219. return hr;
  220. }
  221. HRESULT AddressEncoder_GetWideBlock(LPCWSTR pszWide, LPCWSTR *pszEnd, LPWSTR pszBuffer, size_t *pcchBufferMax)
  222. {
  223. LPCWSTR cursor = pszWide;
  224. if (NULL == pszWide)
  225. return E_INVALIDARG;
  226. if (NULL != pszBuffer && NULL == pcchBufferMax)
  227. return E_INVALIDARG;
  228. while (L'\0' == *cursor || *cursor > 0xFF)
  229. {
  230. if (L'\0' == *cursor)
  231. break;
  232. cursor = CharNext(cursor);
  233. }
  234. if (NULL != pszEnd)
  235. *pszEnd = cursor;
  236. HRESULT hr = S_OK;
  237. size_t cchBuffer = 0;
  238. if (cursor == pszWide)
  239. {
  240. hr = S_FALSE;
  241. }
  242. else
  243. {
  244. size_t bytesCount = WideCharToMultiByte(CP_UTF8, 0, pszWide, (int)(cursor - pszWide), NULL, 0, NULL, NULL);
  245. if (0 == bytesCount)
  246. {
  247. DWORD errorCode = GetLastError();
  248. if (ERROR_SUCCESS != errorCode)
  249. hr = HRESULT_FROM_WIN32(errorCode);
  250. }
  251. else
  252. {
  253. cchBuffer = 3 * bytesCount;
  254. if (NULL != pszBuffer)
  255. {
  256. if (*pcchBufferMax >= cchBuffer)
  257. {
  258. LPWSTR p = pszBuffer;
  259. BYTE *bytes = ((BYTE*)(pszBuffer + *pcchBufferMax)) - bytesCount;
  260. WideCharToMultiByte(CP_UTF8, 0, pszWide, (int)(cursor - pszWide), (LPSTR)bytes, (int)bytesCount, NULL, NULL);
  261. for (size_t i = 0; i < bytesCount; i++)
  262. {
  263. BYTE b = bytes[i];
  264. *p++ = L'%';
  265. BYTE c = (b >> 4) & 0x0F;
  266. *p++ = (c < 10) ? (L'0' + c) : (L'A' + (c -10));
  267. c = b & 0x0F;
  268. *p++ = (c < 10) ? (L'0' + c) : (L'A' + (c -10));
  269. }
  270. }
  271. else
  272. {
  273. hr = E_OUTOFMEMORY;
  274. }
  275. }
  276. }
  277. }
  278. if (NULL != pcchBufferMax)
  279. {
  280. *pcchBufferMax = cchBuffer;
  281. }
  282. return hr;
  283. }
  284. HRESULT AddressEncoder_EncodeWideChars(LPCWSTR pszAddress, size_t cchAddress, LPWSTR *ppResult)
  285. {
  286. if (NULL == ppResult)
  287. return E_POINTER;
  288. if (NULL == pszAddress)
  289. {
  290. *ppResult = NULL;
  291. return S_FALSE;
  292. }
  293. LPCWSTR blockEnd;
  294. size_t blockSize;
  295. size_t cchResultMax = 0;
  296. BOOL needEncode = FALSE;
  297. for (LPCWSTR cursor = pszAddress; L'\0' != *cursor;)
  298. {
  299. if (*cursor > 0xFF && SUCCEEDED(AddressEncoder_GetWideBlock(cursor, &blockEnd, NULL, &blockSize)))
  300. {
  301. cursor = blockEnd;
  302. cchResultMax += blockSize;
  303. needEncode = TRUE;
  304. }
  305. else
  306. {
  307. cursor = CharNext(cursor);
  308. cchResultMax++;
  309. }
  310. }
  311. if (FALSE == needEncode)
  312. {
  313. *ppResult = NULL;
  314. return S_FALSE;
  315. }
  316. HRESULT hr;
  317. cchResultMax++;
  318. LPWSTR result = Plugin_MallocString(cchResultMax);
  319. if (NULL == result)
  320. hr = E_OUTOFMEMORY;
  321. else
  322. {
  323. LPWSTR cursor = result;
  324. size_t remaining = cchResultMax;
  325. LPCWSTR address = pszAddress;
  326. LPCWSTR addressBlock = address;
  327. for (;;)
  328. {
  329. if (*address > 0xFF)
  330. {
  331. if (addressBlock != address)
  332. {
  333. hr = StringCchCopyNEx(cursor, remaining, addressBlock, (size_t)(address - addressBlock), &cursor, &remaining, 0);
  334. if (FAILED(hr)) break;
  335. }
  336. blockSize = remaining;
  337. hr = AddressEncoder_GetWideBlock(address, &address, cursor, &blockSize);
  338. if (FAILED(hr)) break;
  339. cursor += blockSize;
  340. remaining -= blockSize;
  341. addressBlock = address;
  342. continue;
  343. }
  344. if (L'\0' == *address)
  345. {
  346. if (addressBlock != address)
  347. {
  348. hr = StringCchCopyNEx(cursor, remaining, addressBlock, (size_t)(address - addressBlock), &cursor, &remaining, 0);
  349. }
  350. break;
  351. }
  352. else
  353. address = CharNext(address);
  354. }
  355. *cursor = L'\0';
  356. }
  357. if (FAILED(hr))
  358. {
  359. Plugin_FreeString(result);
  360. result = NULL;
  361. }
  362. *ppResult = result;
  363. return hr;
  364. }
  365. HRESULT AddressEncoder_EncodeString(LPCWSTR pszAddress, LPWSTR pszBuffer, size_t *pcchBufferMax, UINT flags)
  366. {
  367. if (NULL == pszBuffer || NULL == pcchBufferMax)
  368. return E_INVALIDARG;
  369. if (NULL == pszAddress || L'\0' == *pszAddress)
  370. {
  371. *pszBuffer = L'\0';
  372. *pcchBufferMax = 0;
  373. return S_OK;
  374. }
  375. INT cchAddress = lstrlen(pszAddress);
  376. LPCWSTR begin, end;
  377. begin = pszAddress;
  378. end = pszAddress + cchAddress;
  379. WORD charType;
  380. while (L'\0' != *begin &&
  381. FALSE != GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, begin, 1, &charType) && 0 != (C1_SPACE & charType))
  382. {
  383. begin = CharNext(begin);
  384. }
  385. while (begin != end &&
  386. FALSE != GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, begin, 1, &charType) && 0 != (C1_SPACE & charType))
  387. {
  388. end = CharPrev(begin, end);
  389. }
  390. if (end <= begin)
  391. {
  392. *pszBuffer = L'\0';
  393. *pcchBufferMax = 0;
  394. return S_OK;
  395. }
  396. LPWSTR encoded;
  397. HRESULT hr = AddressEncoder_EncodeWideChars(begin, (end - begin), &encoded);
  398. if (FAILED(hr)) return hr;
  399. if (S_OK == hr)
  400. {
  401. begin = encoded;
  402. end = begin + lstrlen(begin);
  403. }
  404. DWORD bufferLen = (DWORD)(*pcchBufferMax);
  405. if (FALSE == InternetCanonicalizeUrl(begin, pszBuffer, &bufferLen, flags))
  406. {
  407. DWORD errorCode = GetLastError();
  408. if (ERROR_INSUFFICIENT_BUFFER == errorCode)
  409. {
  410. *pcchBufferMax = bufferLen;
  411. hr = ENC_E_INSUFFICIENT_BUFFER;
  412. }
  413. else
  414. {
  415. size_t cchNeeded = (end - begin);
  416. if (cchNeeded < *pcchBufferMax)
  417. {
  418. hr = StringCchCopyN(pszBuffer, *pcchBufferMax, begin, cchNeeded);
  419. if (STRSAFE_E_INSUFFICIENT_BUFFER == hr)
  420. {
  421. hr = ENC_E_INSUFFICIENT_BUFFER;
  422. }
  423. }
  424. else
  425. {
  426. hr = ENC_E_INSUFFICIENT_BUFFER;
  427. }
  428. *pcchBufferMax = cchNeeded + 1;
  429. }
  430. }
  431. else
  432. hr = S_OK;
  433. Plugin_FreeString(encoded);
  434. return hr;
  435. }