Source code of Windows XP (NT5)
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.

224 lines
3.9 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. //
  13. // ********* System Includes
  14. //
  15. #define UNICODE
  16. #define _UNICODE
  17. #define INC_OLE2
  18. // #define _OLEAUT32_
  19. // #define SECURITY_WIN32
  20. #include <windows.h>
  21. //
  22. // ********* CRunTime Includes
  23. //
  24. #include <stdlib.h>
  25. #include <limits.h>
  26. #include <io.h>
  27. #include <stdio.h>
  28. //
  29. // ********* Public ADs includes
  30. //
  31. #include "sconv.hxx"
  32. #define NULL_TERMINATED 0
  33. int
  34. AnsiToUnicodeString(
  35. LPSTR pAnsi,
  36. LPWSTR pUnicode,
  37. DWORD StringLength
  38. )
  39. {
  40. int iReturn;
  41. if( StringLength == NULL_TERMINATED )
  42. StringLength = strlen( pAnsi );
  43. iReturn = MultiByteToWideChar(CP_ACP,
  44. MB_PRECOMPOSED,
  45. pAnsi,
  46. StringLength + 1,
  47. pUnicode,
  48. StringLength + 1 );
  49. //
  50. // Ensure NULL termination.
  51. //
  52. pUnicode[StringLength] = 0;
  53. return iReturn;
  54. }
  55. int
  56. UnicodeToAnsiString(
  57. LPWSTR pUnicode,
  58. LPSTR pAnsi,
  59. DWORD StringLength
  60. )
  61. {
  62. LPSTR pTempBuf = NULL;
  63. INT rc = 0;
  64. if( StringLength == NULL_TERMINATED ) {
  65. //
  66. // StringLength is just the
  67. // number of characters in the string
  68. //
  69. StringLength = wcslen( pUnicode );
  70. }
  71. //
  72. // WideCharToMultiByte doesn't NULL terminate if we're copying
  73. // just part of the string, so terminate here.
  74. //
  75. pUnicode[StringLength] = 0;
  76. //
  77. // Include one for the NULL
  78. //
  79. StringLength++;
  80. //
  81. // Unfortunately, WideCharToMultiByte doesn't do conversion in place,
  82. // so allocate a temporary buffer, which we can then copy:
  83. //
  84. if( pAnsi == (LPSTR)pUnicode )
  85. {
  86. pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength );
  87. pAnsi = pTempBuf;
  88. }
  89. if( pAnsi )
  90. {
  91. rc = WideCharToMultiByte( CP_ACP,
  92. 0,
  93. pUnicode,
  94. StringLength,
  95. pAnsi,
  96. StringLength,
  97. NULL,
  98. NULL );
  99. }
  100. /* If pTempBuf is non-null, we must copy the resulting string
  101. * so that it looks as if we did it in place:
  102. */
  103. if( pTempBuf && ( rc > 0 ) )
  104. {
  105. pAnsi = (LPSTR)pUnicode;
  106. strcpy( pAnsi, pTempBuf );
  107. LocalFree( pTempBuf );
  108. }
  109. return rc;
  110. }
  111. LPWSTR
  112. AllocateUnicodeString(
  113. LPSTR pAnsiString
  114. )
  115. {
  116. LPWSTR pUnicodeString = NULL;
  117. if (!pAnsiString)
  118. return NULL;
  119. pUnicodeString = (LPWSTR)LocalAlloc(
  120. LPTR,
  121. strlen(pAnsiString)*sizeof(WCHAR) +sizeof(WCHAR)
  122. );
  123. if (pUnicodeString) {
  124. AnsiToUnicodeString(
  125. pAnsiString,
  126. pUnicodeString,
  127. NULL_TERMINATED
  128. );
  129. }
  130. return pUnicodeString;
  131. }
  132. void
  133. FreeUnicodeString(
  134. LPWSTR pUnicodeString
  135. )
  136. {
  137. if (!pUnicodeString)
  138. return;
  139. LocalFree(pUnicodeString);
  140. return;
  141. }
  142. DWORD
  143. ComputeMaxStrlenW(
  144. LPWSTR pString,
  145. DWORD cchBufMax
  146. )
  147. {
  148. DWORD cchLen;
  149. //
  150. // Include space for the NULL.
  151. //
  152. cchBufMax--;
  153. cchLen = wcslen(pString);
  154. if (cchLen > cchBufMax)
  155. return cchBufMax;
  156. return cchLen;
  157. }
  158. DWORD
  159. ComputeMaxStrlenA(
  160. LPSTR pString,
  161. DWORD cchBufMax
  162. )
  163. {
  164. DWORD cchLen;
  165. //
  166. // Include space for the NULL.
  167. //
  168. cchBufMax--;
  169. cchLen = lstrlenA(pString);
  170. if (cchLen > cchBufMax)
  171. return cchBufMax;
  172. return cchLen;
  173. }