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.

302 lines
7.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: transhlp.cxx
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 12-05-95 JohannP (Johann Posch) Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <trans.h>
  18. HMODULE g_hLibMlang = NULL;
  19. //+---------------------------------------------------------------------------
  20. //
  21. // Function: SzW2ADynamic
  22. //
  23. // Synopsis: Convert Unicode string wzFrom to Ansi, and return the Ansi string.
  24. // The Ansi string will be written into szTo (whose size is cchTo bytes) if
  25. // szTo is nonNull AND it is large enough to hold the Ansi string.
  26. // If this is not the case, the Ansi string is dynamically allocated.
  27. // If 'fTaskMalloc', allocation is done thru IMalloc, otherwise it is done
  28. // thru new.
  29. //
  30. // Arguments: [wzFrom] --
  31. // [szTo] --
  32. // [cchTo] --
  33. // [fTaskMalloc] --
  34. //
  35. // Returns: If error returns NULL, otherwise returns
  36. // pointer to Ansi string (if different from szTo, it must be freed via
  37. // delete or IMalloc, as appropriate).
  38. //
  39. // History: x-xx-xx Clarg Created
  40. // 2-25-96 JohannP (Johann Posch) Modified
  41. //
  42. // Notes:
  43. //
  44. //----------------------------------------------------------------------------
  45. LPSTR SzW2ADynamic(LPCWSTR wzFrom, LPSTR szTo, int cchTo, BOOL fTaskMalloc)
  46. {
  47. int cchRequired;
  48. char *pszT = NULL;
  49. cchRequired = WideCharToMultiByte(CP_ACP, 0, wzFrom, -1, NULL, 0, NULL, NULL);
  50. cchRequired++;
  51. TransAssert((cchRequired > 0));
  52. if (szTo && cchTo && (cchTo >= cchRequired))
  53. {
  54. // szTo has enough space
  55. pszT = szTo;
  56. }
  57. else
  58. {
  59. // szTo is not large enough; dynamically allocate the buffer
  60. if (fTaskMalloc)
  61. {
  62. pszT = (char*)CoTaskMemAlloc(sizeof(char) * cchRequired);
  63. }
  64. else
  65. {
  66. pszT = new char[cchRequired];
  67. }
  68. if (!pszT)
  69. {
  70. return NULL;
  71. }
  72. }
  73. if (!WideCharToMultiByte(CP_ACP, 0, wzFrom, -1, pszT, cchRequired, NULL, NULL))
  74. {
  75. //TransAssert((0));
  76. if (pszT != szTo)
  77. {
  78. (fTaskMalloc ? CoTaskMemFree(pszT) : delete pszT);
  79. }
  80. pszT = NULL;
  81. }
  82. return pszT;
  83. }
  84. //+---------------------------------------------------------------------------
  85. //
  86. // Function: WzA2WDynamic
  87. //
  88. // Synopsis: Convert Ansi string szFrom to Unicode, and return the Unicode string.
  89. // The Unicode string will be written into wzTo (whose size is cwchTo bytes)
  90. // if wzTo is nonNull AND it is large enough to hold the Unicode string.
  91. // If this is not the case, the Unicode string is dynamically allocated.
  92. // If 'fTaskMalloc', allocation is done thru IMalloc, otherwise it is done
  93. // thru new.
  94. //
  95. // Arguments: [szFrom] --
  96. // [wzTo] --
  97. // [cwchTo] --
  98. // [fTaskMalloc] --
  99. //
  100. // Returns: If error returns NULL, otherwise returns
  101. // pointer to Unicode string (if different from wzTo, it must be freed via
  102. // delete or IMalloc, as appropriate).
  103. //
  104. // History: x-xx-xx Clarg Created
  105. // 2-25-96 JohannP (Johann Posch) modified
  106. //
  107. // Notes:
  108. //
  109. //----------------------------------------------------------------------------
  110. LPWSTR WzA2WDynamic(LPCSTR szFrom, LPWSTR wzTo, int cwchTo, BOOL fTaskMalloc)
  111. {
  112. int cwchRequired;
  113. WCHAR *pwzT = NULL;
  114. cwchRequired = MultiByteToWideChar(CP_ACP, 0, szFrom, -1, NULL, 0);
  115. cwchRequired++;
  116. TransAssert((cwchRequired > 0));
  117. if (wzTo && cwchTo && (cwchTo >= cwchRequired))
  118. {
  119. // wzTo has enough space
  120. pwzT = wzTo;
  121. }
  122. else
  123. {
  124. // wzTo is not large enough; dynamically allocate the buffer
  125. if (fTaskMalloc)
  126. {
  127. pwzT = (WCHAR*)CoTaskMemAlloc(sizeof(WCHAR) * cwchRequired);
  128. }
  129. else
  130. {
  131. pwzT = new WCHAR[cwchRequired];
  132. }
  133. if (!pwzT)
  134. {
  135. return NULL;
  136. }
  137. }
  138. if (!MultiByteToWideChar(CP_ACP, 0, szFrom, -1, pwzT, cwchRequired))
  139. {
  140. //Assert(0);
  141. if (pwzT != wzTo)
  142. {
  143. (fTaskMalloc ? CoTaskMemFree(pwzT) : delete pwzT);
  144. }
  145. pwzT = NULL;
  146. }
  147. return pwzT;
  148. }
  149. //+---------------------------------------------------------------------------
  150. //
  151. // Function: OLESTRDuplicate
  152. //
  153. // Synopsis:
  154. //
  155. // Arguments: [ws] --
  156. //
  157. // Returns:
  158. //
  159. // History: 2-25-96 JohannP (Johann Posch) Created
  160. //
  161. // Notes:
  162. //
  163. //----------------------------------------------------------------------------
  164. LPWSTR OLESTRDuplicate(LPCWSTR ws)
  165. {
  166. DWORD cLen;
  167. LPWSTR wsNew = NULL;
  168. if (ws)
  169. {
  170. wsNew = (LPWSTR) new WCHAR [wcslen(ws) + 1];
  171. if (wsNew)
  172. {
  173. wcscpy(wsNew, ws);
  174. }
  175. }
  176. return wsNew;
  177. }
  178. //+-------------------------------------------------------------------------
  179. //
  180. // Function: WideCharToMultiByteWithMlang
  181. //
  182. // Synopsis: Convert Unicode string wzFrom to Ansi by uring MLANG,
  183. //
  184. // History: 5-29-97 DanpoZ (Danpo Zhang) Created
  185. //
  186. // Notes:
  187. //
  188. //--------------------------------------------------------------------------
  189. void WideCharToMultiByteWithMlang(
  190. LPCWSTR lpwszWide,
  191. LPSTR lpszAnsi,
  192. int cchAnsi,
  193. DWORD dwCodePage)
  194. {
  195. INT cchOut;
  196. INT cchIn = wcslen(lpwszWide);
  197. HRESULT hr = NOERROR;
  198. typedef HRESULT (WINAPI * pfnMLANGW2A)(
  199. LPDWORD, DWORD, LPCWSTR, LPINT, LPSTR, LPINT);
  200. static pfnMLANGW2A pfnConvertINetUnicodeToMultiByte = NULL;
  201. if(!g_hLibMlang)
  202. {
  203. g_hLibMlang = LoadLibraryA("mlang.dll");
  204. if(!g_hLibMlang)
  205. {
  206. goto End;
  207. }
  208. }
  209. if (!pfnConvertINetUnicodeToMultiByte)
  210. {
  211. pfnConvertINetUnicodeToMultiByte= (pfnMLANGW2A)GetProcAddress(
  212. g_hLibMlang, "ConvertINetUnicodeToMultiByte");
  213. if (!pfnConvertINetUnicodeToMultiByte)
  214. {
  215. goto End;
  216. }
  217. }
  218. // first call to get the lenth of the Multi-Byte string
  219. hr = pfnConvertINetUnicodeToMultiByte(
  220. NULL, dwCodePage, lpwszWide, &cchIn, NULL, &cchOut);
  221. if( !FAILED(hr) && cchOut <= cchAnsi )
  222. {
  223. hr = pfnConvertINetUnicodeToMultiByte(
  224. NULL, dwCodePage, lpwszWide, &cchIn, lpszAnsi, &cchOut);
  225. }
  226. End:;
  227. }
  228. DWORD StrLenMultiByteWithMlang(
  229. LPCWSTR lpwszWide,
  230. DWORD dwCodePage)
  231. {
  232. INT cchOut;
  233. INT cchIn = wcslen(lpwszWide);
  234. HRESULT hr = NOERROR;
  235. typedef HRESULT (WINAPI * pfnMLANGW2A)(
  236. LPDWORD, DWORD, LPCWSTR, LPINT, LPSTR, LPINT);
  237. static pfnMLANGW2A pfnConvertINetUnicodeToMultiByte = NULL;
  238. if(!g_hLibMlang)
  239. {
  240. g_hLibMlang = LoadLibraryA("mlang.dll");
  241. if(!g_hLibMlang)
  242. {
  243. goto Error;
  244. }
  245. }
  246. if (!pfnConvertINetUnicodeToMultiByte)
  247. {
  248. pfnConvertINetUnicodeToMultiByte= (pfnMLANGW2A)GetProcAddress(
  249. g_hLibMlang, "ConvertINetUnicodeToMultiByte");
  250. if (!pfnConvertINetUnicodeToMultiByte)
  251. {
  252. goto Error;
  253. }
  254. }
  255. // first call to get the lenth of the Multi-Byte string
  256. hr = pfnConvertINetUnicodeToMultiByte(
  257. NULL, dwCodePage, lpwszWide, &cchIn, NULL, &cchOut);
  258. if( !FAILED(hr) )
  259. {
  260. return cchOut;
  261. }
  262. Error:
  263. return 0;
  264. }