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.

279 lines
5.7 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. charset.cxx
  5. Abstract:
  6. Contains some functions to do Unicode <-> Ansi/MBCS convertsions.
  7. Author:
  8. Danilo Almeida (t-danal) 06-17-96
  9. Revision History:
  10. --*/
  11. //
  12. // INCLUDES
  13. //
  14. #include "iis.hxx"
  15. /*
  16. * AnsiBytesFromUnicode
  17. *
  18. * Description:
  19. * Given a Unicode string, returns number of bytes needed for Ansi version
  20. *
  21. * In:
  22. * pwszUnicode - pointer to Unicode string
  23. */
  24. int
  25. AnsiBytesFromUnicode(
  26. LPCWSTR pwszUnicode
  27. )
  28. {
  29. return WideCharToMultiByte(CP_ACP,
  30. 0,
  31. pwszUnicode,
  32. -1,
  33. NULL,
  34. 0,
  35. NULL,
  36. NULL);
  37. }
  38. /*
  39. * AllocAnsi
  40. *
  41. * Description:
  42. * Given a Unicode string, allocate a new Ansi translation of that string
  43. *
  44. * In:
  45. * pwszUnicode - pointer to original Unicode string
  46. * ppszAnsi - pointer to cell to hold new MCBS string addr
  47. *
  48. * Out:
  49. * ppszAnsi - contains new MBCS string
  50. *
  51. * Returns:
  52. * Error code or 0 if successful.
  53. *
  54. * Notes:
  55. * The client must free the allocated string with FreeAnsi.
  56. */
  57. UINT
  58. AllocAnsi(
  59. LPCWSTR pwszUnicode,
  60. LPSTR* ppszAnsi
  61. )
  62. {
  63. UINT err;
  64. BYTE * pbAlloc;
  65. INT cbUnicode;
  66. INT cbAnsi;
  67. if (pwszUnicode == NULL)
  68. {
  69. *ppszAnsi = NULL;
  70. return 0;
  71. }
  72. cbAnsi = AnsiBytesFromUnicode(pwszUnicode);
  73. err = MyAllocMem(cbAnsi, &pbAlloc);
  74. if (err)
  75. return err;
  76. cbUnicode = (DWORD)wcslen(pwszUnicode)+1;
  77. *ppszAnsi = (LPSTR)pbAlloc;
  78. err = (UINT) !WideCharToMultiByte(CP_ACP,
  79. 0,
  80. pwszUnicode,
  81. cbUnicode,
  82. *ppszAnsi,
  83. cbAnsi,
  84. NULL,
  85. NULL);
  86. if (err)
  87. {
  88. *ppszAnsi = NULL;
  89. MyFreeMem(pbAlloc);
  90. return ( (UINT)GetLastError() );
  91. }
  92. return 0;
  93. }
  94. /*
  95. * FreeAnsi
  96. *
  97. * Description:
  98. * Deallocates an Ansi string allocated by AllocAnsi
  99. *
  100. * In:
  101. * pszAnsi - pointer to the Ansi string
  102. *
  103. * Out:
  104. * pszAnsi - invalid pointer - string has been freed
  105. */
  106. VOID
  107. FreeAnsi(LPSTR pszAnsi)
  108. {
  109. if (pszAnsi != NULL)
  110. MyFreeMem((LPBYTE)pszAnsi);
  111. }
  112. /*
  113. * AllocUnicode
  114. *
  115. * Description:
  116. * Given an Ansi string, allocates an Unicode version of that string
  117. *
  118. * In:
  119. * pszAnsi - pointer to original MBCS string
  120. * ppwszUnicode - pointer to new Unicode string address
  121. *
  122. * Out:
  123. * ppwszUnicode - points to new Unicode string
  124. *
  125. * Returns:
  126. * Error code or 0 if successful.
  127. *
  128. * Notes:
  129. * The client must free the allocated string with FreeUnicode.
  130. */
  131. UINT
  132. AllocUnicode(
  133. LPCSTR pszAnsi,
  134. LPWSTR * ppwszUnicode )
  135. {
  136. UINT err;
  137. BYTE * pbAlloc;
  138. INT cbAnsi;
  139. if (pszAnsi == NULL)
  140. {
  141. *ppwszUnicode = NULL;
  142. return 0;
  143. }
  144. // Allocate space for Unicode string (may be a little extra if MBCS)
  145. cbAnsi = (DWORD)strlen(pszAnsi)+1;
  146. err = MyAllocMem(sizeof(WCHAR) * cbAnsi, &pbAlloc);
  147. if (err)
  148. return err;
  149. *ppwszUnicode = (LPWSTR)pbAlloc;
  150. err = (UINT) !MultiByteToWideChar(CP_ACP,
  151. MB_PRECOMPOSED,
  152. pszAnsi,
  153. cbAnsi,
  154. *ppwszUnicode,
  155. cbAnsi);
  156. if (err)
  157. {
  158. *ppwszUnicode = NULL;
  159. MyFreeMem(pbAlloc);
  160. return ( (UINT)GetLastError() );
  161. }
  162. return 0;
  163. }
  164. /*
  165. * AllocUnicode2
  166. *
  167. * Description:
  168. * Given a MBCS string, allocates a new Unicode version of that string
  169. *
  170. * In:
  171. * pszAnsi - pointer to original MBCS string
  172. * cbAnsi - number of bytes to convert
  173. * ppwszUnicode - pointer to where to return new Unicode string address
  174. *
  175. * Out:
  176. * ppwszUnicode - contains new Unicode string
  177. *
  178. * Returns:
  179. * Returns number of characters written.
  180. *
  181. * Notes:
  182. * The client must free the allocated string with FreeUnicode.
  183. */
  184. int
  185. AllocUnicode2(
  186. LPCSTR pszAnsi,
  187. int cbAnsi,
  188. LPWSTR * ppwszUnicode)
  189. {
  190. UINT err;
  191. BYTE * pbAlloc;
  192. INT cwch;
  193. *ppwszUnicode = NULL;
  194. SetLastError(ERROR_SUCCESS);
  195. if (cbAnsi == 0)
  196. return 0;
  197. err = MyAllocMem(sizeof(WCHAR) * cbAnsi, &pbAlloc);
  198. if (err)
  199. {
  200. SetLastError(err);
  201. return 0;
  202. }
  203. *ppwszUnicode = (LPWSTR)pbAlloc;
  204. cwch = MultiByteToWideChar(CP_ACP,
  205. MB_PRECOMPOSED,
  206. pszAnsi,
  207. cbAnsi,
  208. *ppwszUnicode,
  209. cbAnsi);
  210. if (cwch == 0)
  211. {
  212. *ppwszUnicode = NULL;
  213. MyFreeMem(pbAlloc);
  214. }
  215. return cwch;
  216. }
  217. /*
  218. * FreeUnicode
  219. *
  220. * Description:
  221. * Deallocates a Unicode string allocatedd by AllocUnicode/AllocUnicode2
  222. *
  223. * In:
  224. * pwszUnicode - pointer to the Unicode string
  225. *
  226. * Out:
  227. * pwszUnicode - invalid pointer - string has been freed
  228. */
  229. VOID
  230. FreeUnicode( LPWSTR pwszUnicode )
  231. {
  232. if (pwszUnicode != NULL)
  233. MyFreeMem((LPBYTE)pwszUnicode);
  234. }