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.

315 lines
6.4 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995
  5. //
  6. // File: sconv.cxx
  7. //
  8. // Contents: Ansi to Unicode conversions
  9. //
  10. // History: KrishnaG Jan 22 1996
  11. //----------------------------------------------------------------------------
  12. #include "ldapc.hxx"
  13. #pragma hdrstop
  14. #define NULL_TERMINATED 0
  15. int
  16. AnsiToUnicodeString(
  17. LPSTR pAnsi,
  18. LPWSTR pUnicode,
  19. DWORD StringLength
  20. )
  21. {
  22. int iReturn;
  23. if( StringLength == NULL_TERMINATED )
  24. StringLength = strlen( pAnsi );
  25. iReturn = MultiByteToWideChar(CP_ACP,
  26. MB_PRECOMPOSED,
  27. pAnsi,
  28. StringLength,
  29. pUnicode,
  30. StringLength + 1);
  31. //
  32. // Ensure NULL termination.
  33. //
  34. pUnicode[StringLength] = 0;
  35. return iReturn;
  36. }
  37. int
  38. UnicodeToAnsiString(
  39. LPWSTR pUnicode,
  40. LPSTR pAnsi,
  41. DWORD StringLength
  42. )
  43. {
  44. LPSTR pTempBuf = NULL;
  45. INT rc = 0;
  46. if( StringLength == NULL_TERMINATED ) {
  47. //
  48. // StringLength is just the
  49. // number of characters in the string
  50. //
  51. StringLength = wcslen( pUnicode );
  52. }
  53. //
  54. // WideCharToMultiByte doesn't NULL terminate if we're copying
  55. // just part of the string, so terminate here.
  56. //
  57. pUnicode[StringLength] = 0;
  58. //
  59. // Include one for the NULL
  60. //
  61. StringLength++;
  62. //
  63. // Unfortunately, WideCharToMultiByte doesn't do conversion in place,
  64. // so allocate a temporary buffer, which we can then copy:
  65. //
  66. if( pAnsi == (LPSTR)pUnicode )
  67. {
  68. pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength );
  69. if(!pTempBuf)
  70. {
  71. return rc;
  72. }
  73. pAnsi = pTempBuf;
  74. }
  75. if( pAnsi )
  76. {
  77. rc = WideCharToMultiByte( CP_ACP,
  78. 0,
  79. pUnicode,
  80. StringLength - 1,
  81. pAnsi,
  82. StringLength,
  83. NULL,
  84. NULL );
  85. }
  86. /* If pTempBuf is non-null, we must copy the resulting string
  87. * so that it looks as if we did it in place:
  88. */
  89. if( pTempBuf && ( rc > 0 ) )
  90. {
  91. pAnsi = (LPSTR)pUnicode;
  92. strcpy( pAnsi, pTempBuf );
  93. pAnsi[StringLength - 1] = 0;
  94. }
  95. if(pTempBuf)
  96. {
  97. LocalFree( pTempBuf );
  98. }
  99. return rc;
  100. }
  101. LPWSTR
  102. AllocateUnicodeString(
  103. LPSTR pAnsiString
  104. )
  105. {
  106. LPWSTR pUnicodeString = NULL;
  107. if (!pAnsiString)
  108. return NULL;
  109. pUnicodeString = (LPWSTR)LocalAlloc(
  110. LPTR,
  111. strlen(pAnsiString)*sizeof(WCHAR) +sizeof(WCHAR)
  112. );
  113. if (pUnicodeString) {
  114. AnsiToUnicodeString(
  115. pAnsiString,
  116. pUnicodeString,
  117. NULL_TERMINATED
  118. );
  119. }
  120. return pUnicodeString;
  121. }
  122. void
  123. FreeUnicodeString(
  124. LPWSTR pUnicodeString
  125. )
  126. {
  127. if (!pUnicodeString)
  128. return;
  129. LocalFree(pUnicodeString);
  130. return;
  131. }
  132. DWORD
  133. ComputeMaxStrlenW(
  134. LPWSTR pString,
  135. DWORD cchBufMax
  136. )
  137. {
  138. DWORD cchLen;
  139. //
  140. // Include space for the NULL.
  141. //
  142. cchBufMax--;
  143. cchLen = wcslen(pString);
  144. if (cchLen > cchBufMax)
  145. return cchBufMax;
  146. return cchLen;
  147. }
  148. DWORD
  149. ComputeMaxStrlenA(
  150. LPSTR pString,
  151. DWORD cchBufMax
  152. )
  153. {
  154. DWORD cchLen;
  155. //
  156. // Include space for the NULL.
  157. //
  158. cchBufMax--;
  159. cchLen = lstrlenA(pString);
  160. if (cchLen > cchBufMax)
  161. return cchBufMax;
  162. return cchLen;
  163. }
  164. HRESULT
  165. UnicodeToUTF8String(
  166. LPCWSTR pUnicode,
  167. LPSTR *ppUTF8
  168. )
  169. {
  170. HRESULT hr = S_OK;
  171. int UnicodeLength = 0;
  172. LPSTR pUTF8Temp = NULL;
  173. int UTF8TempLength = 0;
  174. int UTF8Length = 0;
  175. if (!pUnicode || !ppUTF8)
  176. BAIL_ON_FAILURE(hr = E_INVALIDARG)
  177. UnicodeLength = wcslen(pUnicode);
  178. UTF8TempLength = LdapUnicodeToUTF8(
  179. pUnicode,
  180. UnicodeLength,
  181. NULL,
  182. 0
  183. );
  184. if(!UTF8TempLength)
  185. {
  186. BAIL_ON_FAILURE(hr = E_FAIL);
  187. }
  188. pUTF8Temp = (char*) AllocADsMem(UTF8TempLength + 1);
  189. if (!pUTF8Temp)
  190. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  191. UTF8Length = LdapUnicodeToUTF8(pUnicode,
  192. UnicodeLength,
  193. pUTF8Temp,
  194. UTF8TempLength + 1);
  195. if (UTF8Length == 0)
  196. BAIL_ON_FAILURE(hr = E_FAIL);
  197. *(pUTF8Temp+UTF8TempLength) = '\0';
  198. *ppUTF8 = pUTF8Temp;
  199. return(hr);
  200. error:
  201. if (pUTF8Temp)
  202. FreeADsMem(pUTF8Temp);
  203. return (hr);
  204. }
  205. HRESULT
  206. UTF8ToUnicodeString(
  207. LPCSTR pUTF8,
  208. LPWSTR *ppUnicode
  209. )
  210. {
  211. HRESULT hr = S_OK;
  212. int UTF8Length = 0;
  213. LPWSTR pUnicodeTemp = NULL;
  214. int UnicodeTempLength = 0;
  215. int UnicodeLength = 0;
  216. if (!pUTF8 || !ppUnicode)
  217. BAIL_ON_FAILURE(hr = E_INVALIDARG)
  218. UTF8Length = strlen(pUTF8);
  219. UnicodeTempLength = LdapUTF8ToUnicode(pUTF8,
  220. UTF8Length,
  221. NULL,
  222. 0);
  223. if(UnicodeTempLength == 0)
  224. {
  225. BAIL_ON_FAILURE(E_FAIL);
  226. }
  227. pUnicodeTemp = (PWCHAR) AllocADsMem(UnicodeTempLength + 1);
  228. if (!pUnicodeTemp)
  229. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  230. UnicodeLength = LdapUTF8ToUnicode(pUTF8,
  231. UTF8Length,
  232. pUnicodeTemp,
  233. UnicodeTempLength + 1);
  234. if (UnicodeLength == 0)
  235. BAIL_ON_FAILURE(hr = E_FAIL);
  236. *(pUnicodeTemp + UnicodeTempLength) = '\0';
  237. *ppUnicode = pUnicodeTemp;
  238. return(hr);
  239. error:
  240. if (pUnicodeTemp)
  241. FreeADsMem(pUnicodeTemp);
  242. return (hr);
  243. }
  244.