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.

218 lines
4.4 KiB

  1. /*
  2. * String.c
  3. *
  4. * Author: BreenH
  5. *
  6. * String utilities in the NT flavor.
  7. */
  8. /*
  9. * Includes
  10. */
  11. #include "precomp.h"
  12. #include "tsutilnt.h"
  13. /*
  14. * Function Implementations
  15. */
  16. NTSTATUS NTAPI
  17. NtAllocateAndCopyStringA(
  18. PSTR *ppDestination,
  19. PCSTR pString
  20. )
  21. {
  22. NTSTATUS Status;
  23. PSTR pCopy;
  24. ULONG cbString;
  25. ASSERT(ppDestination != NULL);
  26. cbString = (lstrlenA(pString) + 1) * sizeof(CHAR);
  27. pCopy = LocalAlloc(LMEM_FIXED, cbString);
  28. if (pCopy != NULL)
  29. {
  30. RtlCopyMemory(pCopy, pString, cbString);
  31. *ppDestination = pCopy;
  32. Status = STATUS_SUCCESS;
  33. }
  34. else
  35. {
  36. Status = STATUS_NO_MEMORY;
  37. }
  38. return(Status);
  39. }
  40. NTSTATUS NTAPI
  41. NtAllocateAndCopyStringW(
  42. PWSTR *ppDestination,
  43. PCWSTR pString
  44. )
  45. {
  46. NTSTATUS Status;
  47. PWSTR pCopy;
  48. ULONG cbString;
  49. ASSERT(ppDestination != NULL);
  50. ASSERT(pString != NULL);
  51. cbString = (lstrlenW(pString) + 1) * sizeof(WCHAR);
  52. pCopy = LocalAlloc(LMEM_FIXED, cbString);
  53. if (pCopy != NULL)
  54. {
  55. RtlCopyMemory(pCopy, pString, cbString);
  56. *ppDestination = pCopy;
  57. Status = STATUS_SUCCESS;
  58. }
  59. else
  60. {
  61. Status = STATUS_NO_MEMORY;
  62. }
  63. return(Status);
  64. }
  65. NTSTATUS NTAPI
  66. NtConvertAnsiToUnicode(
  67. PWSTR *ppUnicodeString,
  68. PCSTR pAnsiString
  69. )
  70. {
  71. NTSTATUS Status;
  72. PWSTR pUnicodeString;
  73. ULONG cbAnsiString;
  74. ULONG cbBytesWritten;
  75. ULONG cbUnicodeString;
  76. ASSERT(ppUnicodeString != NULL);
  77. ASSERT(pAnsiString != NULL);
  78. //
  79. // Get the number of bytes in the ANSI string, then get the number of
  80. // bytes needed for the Unicode version. None of the Rtl... APIs include
  81. // the NULL terminator in their calculations.
  82. //
  83. cbAnsiString = lstrlenA(pAnsiString);
  84. Status = RtlMultiByteToUnicodeSize(
  85. &cbUnicodeString,
  86. (PCHAR)pAnsiString,
  87. cbAnsiString
  88. );
  89. if (Status == STATUS_SUCCESS)
  90. {
  91. //
  92. // Allocate a buffer for the Unicode string and its NULL terminator,
  93. // then convert the string.
  94. //
  95. cbUnicodeString += sizeof(WCHAR);
  96. pUnicodeString = (PWSTR)LocalAlloc(LPTR, cbUnicodeString);
  97. if (pUnicodeString != NULL)
  98. {
  99. Status = RtlMultiByteToUnicodeN(
  100. pUnicodeString,
  101. cbUnicodeString,
  102. &cbBytesWritten,
  103. (PCHAR)pAnsiString,
  104. cbAnsiString
  105. );
  106. if (Status == STATUS_SUCCESS)
  107. {
  108. *ppUnicodeString = pUnicodeString;
  109. }
  110. else
  111. {
  112. LocalFree(pUnicodeString);
  113. }
  114. }
  115. else
  116. {
  117. Status = STATUS_NO_MEMORY;
  118. }
  119. }
  120. return(Status);
  121. }
  122. NTSTATUS NTAPI
  123. NtConvertUnicodeToAnsi(
  124. PSTR *ppAnsiString,
  125. PCWSTR pUnicodeString
  126. )
  127. {
  128. NTSTATUS Status;
  129. PSTR pAnsiString;
  130. ULONG cbAnsiString;
  131. ULONG cbBytesWritten;
  132. ULONG cbUnicodeString;
  133. ASSERT(ppAnsiString != NULL);
  134. ASSERT(pUnicodeString != NULL);
  135. //
  136. // Get the number of bytes in the ANSI string, then get the number of
  137. // bytes needed for the Unicode version. None of the Rtl... APIs include
  138. // the NULL terminator in their calculations.
  139. //
  140. cbUnicodeString = lstrlenW(pUnicodeString) * sizeof(WCHAR);
  141. Status = RtlUnicodeToMultiByteSize(
  142. &cbAnsiString,
  143. (PWSTR)pUnicodeString,
  144. cbUnicodeString
  145. );
  146. if (Status == STATUS_SUCCESS)
  147. {
  148. //
  149. // Allocate a buffer for the Unicode string and its NULL terminator,
  150. // then convert the string.
  151. //
  152. cbAnsiString += sizeof(CHAR);
  153. pAnsiString = (PSTR)LocalAlloc(LPTR, cbAnsiString);
  154. if (pAnsiString != NULL)
  155. {
  156. Status = RtlUnicodeToMultiByteN(
  157. pAnsiString,
  158. cbAnsiString,
  159. &cbBytesWritten,
  160. (PWSTR)pUnicodeString,
  161. cbUnicodeString
  162. );
  163. if (Status == STATUS_SUCCESS)
  164. {
  165. *ppAnsiString = pAnsiString;
  166. }
  167. else
  168. {
  169. LocalFree(pAnsiString);
  170. }
  171. }
  172. else
  173. {
  174. Status = STATUS_NO_MEMORY;
  175. }
  176. }
  177. return(Status);
  178. }