Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.4 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright(c) Microsoft Corp., 1994 **
  4. //*********************************************************************
  5. //
  6. // UTIL.C - common utility functions
  7. //
  8. // HISTORY:
  9. //
  10. #include "pre.h"
  11. LPWSTR WINAPI A2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars)
  12. {
  13. ASSERT(lpa != NULL);
  14. ASSERT(lpw != NULL);\
  15. // verify that no illegal character present
  16. // since lpw was allocated based on the size of lpa
  17. // don't worry about the number of chars
  18. lpw[0] = '\0';
  19. MultiByteToWideChar(CP_ACP, 0, lpa, -1, lpw, nChars);
  20. return lpw;
  21. }
  22. LPSTR WINAPI W2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars)
  23. {
  24. ASSERT(lpw != NULL);
  25. ASSERT(lpa != NULL);
  26. // verify that no illegal character present
  27. // since lpa was allocated based on the size of lpw
  28. // don't worry about the number of chars
  29. lpa[0] = '\0';
  30. WideCharToMultiByte(CP_ACP, 0, lpw, -1, lpa, nChars, NULL, NULL);
  31. return lpa;
  32. }
  33. HRESULT ConnectToConnectionPoint
  34. (
  35. IUnknown *punkThis,
  36. REFIID riidEvent,
  37. BOOL fConnect,
  38. IUnknown *punkTarget,
  39. DWORD *pdwCookie,
  40. IConnectionPoint **ppcpOut
  41. )
  42. {
  43. // We always need punkTarget, we only need punkThis on connect
  44. if (!punkTarget || (fConnect && !punkThis))
  45. {
  46. return E_FAIL;
  47. }
  48. if (ppcpOut)
  49. *ppcpOut = NULL;
  50. HRESULT hr;
  51. IConnectionPointContainer *pcpContainer;
  52. if (SUCCEEDED(hr = punkTarget->QueryInterface(IID_IConnectionPointContainer, (void **)&pcpContainer)))
  53. {
  54. IConnectionPoint *pcp;
  55. if(SUCCEEDED(hr = pcpContainer->FindConnectionPoint(riidEvent, &pcp)))
  56. {
  57. if(fConnect)
  58. {
  59. // Add us to the list of people interested...
  60. hr = pcp->Advise(punkThis, pdwCookie);
  61. if (FAILED(hr))
  62. *pdwCookie = 0;
  63. }
  64. else
  65. {
  66. // Remove us from the list of people interested...
  67. hr = pcp->Unadvise(*pdwCookie);
  68. *pdwCookie = 0;
  69. }
  70. if (ppcpOut && SUCCEEDED(hr))
  71. *ppcpOut = pcp;
  72. else
  73. pcp->Release();
  74. pcp = NULL;
  75. }
  76. pcpContainer->Release();
  77. pcpContainer = NULL;
  78. }
  79. return hr;
  80. }
  81. void WINAPI URLEncode(TCHAR* pszUrl, size_t bsize)
  82. {
  83. ASSERT(pszUrl);
  84. TCHAR* pszEncode = NULL;
  85. TCHAR* pszEStart = NULL;
  86. #ifdef UNICODE
  87. TCHAR* pszEEnd = (TCHAR*)wmemchr( pszUrl, TEXT('\0'), bsize );
  88. #else
  89. TCHAR* pszEEnd = (TCHAR*)memchr( pszUrl, '\0', bsize );
  90. #endif
  91. int iChr = sizeof(TCHAR);
  92. int iUrlLen = (int)(pszEEnd-pszUrl);
  93. pszEEnd = pszUrl;
  94. TCHAR c;
  95. if ((size_t)((iChr*iUrlLen)*3) <= bsize)
  96. {
  97. pszEncode = (TCHAR*)malloc(sizeof(TCHAR)*(iChr *iUrlLen)*3);
  98. if(pszEncode)
  99. {
  100. pszEStart = pszEncode;
  101. ZeroMemory(pszEncode, sizeof(TCHAR)*(iChr *iUrlLen)*3);
  102. for(; c = *(pszUrl); pszUrl++)
  103. {
  104. switch(c)
  105. {
  106. case ' ': //SPACE
  107. memcpy(pszEncode, TEXT("+"), sizeof(TCHAR)*1);
  108. pszEncode+=1;
  109. break;
  110. case '#':
  111. memcpy(pszEncode, TEXT("%23"), sizeof(TCHAR)*3);
  112. pszEncode+=3;
  113. break;
  114. case '&':
  115. memcpy(pszEncode, TEXT("%26"), sizeof(TCHAR)*3);
  116. pszEncode+=3;
  117. break;
  118. case '%':
  119. memcpy(pszEncode, TEXT("%25"), sizeof(TCHAR)*3);
  120. pszEncode+=3;
  121. break;
  122. case '=':
  123. memcpy(pszEncode, TEXT("%3D"), sizeof(TCHAR)*3);
  124. pszEncode+=3;
  125. break;
  126. case '<':
  127. memcpy(pszEncode, TEXT("%3C"), sizeof(TCHAR)*3);
  128. pszEncode+=3;
  129. break;
  130. case '+':
  131. memcpy(pszEncode, TEXT("%2B"), sizeof(TCHAR)*3);
  132. pszEncode += 3;
  133. break;
  134. default:
  135. *pszEncode++ = c;
  136. break;
  137. }
  138. }
  139. *pszEncode++ = '\0';
  140. memcpy(pszEEnd ,pszEStart, (size_t)(pszEncode - pszEStart));
  141. free(pszEStart);
  142. }
  143. }
  144. }
  145. //BUGBUG: Need to turn spaces into "+"
  146. void WINAPI URLAppendQueryPair
  147. (
  148. LPTSTR lpszQuery,
  149. LPTSTR lpszName,
  150. LPTSTR lpszValue
  151. )
  152. {
  153. // Append the Name
  154. lstrcat(lpszQuery, lpszName);
  155. lstrcat(lpszQuery, cszEquals);
  156. // Append the Value
  157. lstrcat(lpszQuery, lpszValue);
  158. // Append an Ampersand if this is NOT the last pair
  159. lstrcat(lpszQuery, cszAmpersand);
  160. }