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.

330 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1991-1992 Microsoft Corporation
  3. Module Name:
  4. AllocStr.c
  5. Abstract:
  6. This module contains routines to allocate copies of strings (and convert
  7. character sets in the process if necessary).
  8. Author:
  9. John Rogers (JohnRo) 02-Dec-1991
  10. Environment:
  11. Only runs under NT; has an NT-specific interface (with Win32 types).
  12. Requires ANSI C extensions: slash-slash comments, long external names.
  13. Revision History:
  14. 02-Dec-1991 JohnRo
  15. Created.
  16. 10-Mar-1992 rfirth
  17. Added NetpAllocWStrFromWStr
  18. 06-Jan-1992 JohnRo
  19. Added NetpAlloc{type}From{type} routines and macros.
  20. (Got NetpAllocStrFromWStr from CliffV's NetpLogonAnsiToUnicode; got
  21. NetpAllocWStrFromStr from his NetpLogonUnicodeToAnsi.) Thanks Cliff!
  22. Corrected Abstract and added Environment to this file.
  23. 13-Mar-1992 JohnRo
  24. Added NetpAllocStringFromTStr() for NetpGetDomainID().
  25. 29-Apr-1992 JohnRo
  26. Fixed NetpAllocTStrFromString() in UNICODE build.
  27. 03-Aug-1992 JohnRo
  28. RAID 1895: Net APIs and svcs should use OEM char set.
  29. Avoid compiler warnings.
  30. 13-Feb-1995 FloydR
  31. Deleted NetpAllocStringFromTStr() - unused
  32. --*/
  33. // These must be included first:
  34. #include <nt.h> // IN, LPVOID, PSTRING, etc.
  35. #include <windef.h> // Win32 type definitions
  36. #include <lmcons.h> // NET_API_STATUS.
  37. // These may be included in any order:
  38. #include <align.h> // ALIGN_ macros.
  39. #include <lmapibuf.h> // NetApiBufferAllocate(), etc.
  40. #include <netdebug.h> // NetpAssert().
  41. #include <netlib.h> // NetpPointerPlusSomeBytes().
  42. #include <netlibnt.h> // Some of my prototypes.
  43. #include <ntrtl.h>
  44. #include <tstring.h> // NetpNCopyStrToTStr(), some of my prototypes.
  45. #include <winerror.h> // NO_ERROR.
  46. LPSTR
  47. NetpAllocStrFromWStr (
  48. IN LPWSTR Unicode
  49. )
  50. /*++
  51. Routine Description:
  52. Convert an UNICODE (zero terminated) string to the corresponding OEM
  53. string.
  54. Arguments:
  55. Unicode - Specifies the UNICODE zero terminated string to convert.
  56. Return Value:
  57. NULL - There was some error in the conversion.
  58. Otherwise, it returns a pointer to the zero terminated OEM string in
  59. an allocated buffer. The buffer must be freed using NetApiBufferFree.
  60. --*/
  61. {
  62. OEM_STRING OemString;
  63. NET_API_STATUS ApiStatus;
  64. UNICODE_STRING UnicodeString;
  65. RtlInitUnicodeString( &UnicodeString, Unicode );
  66. OemString.MaximumLength =
  67. (USHORT) RtlUnicodeStringToOemSize( &UnicodeString );
  68. ApiStatus = NetApiBufferAllocate(
  69. OemString.MaximumLength,
  70. (LPVOID *) (LPVOID) & OemString.Buffer );
  71. if (ApiStatus != NO_ERROR) {
  72. NetpAssert( ApiStatus == ERROR_NOT_ENOUGH_MEMORY );
  73. return (NULL);
  74. }
  75. NetpAssert( OemString.Buffer != NULL );
  76. if(!NT_SUCCESS( RtlUnicodeStringToOemString( &OemString,
  77. &UnicodeString,
  78. FALSE))){
  79. (void) NetApiBufferFree( OemString.Buffer );
  80. return NULL;
  81. }
  82. return OemString.Buffer;
  83. } // NetpAllocStrFromWStr
  84. LPWSTR
  85. NetpAllocWStrFromStr(
  86. IN LPSTR Oem
  87. )
  88. /*++
  89. Routine Description:
  90. Convert an Oem (zero terminated) string to the corresponding UNICODE
  91. string.
  92. Arguments:
  93. Oem - Specifies the Oem zero terminated string to convert.
  94. Return Value:
  95. NULL - There was some error in the conversion.
  96. Otherwise, it returns a pointer to the zero terminated UNICODE string in
  97. an allocated buffer. The buffer must be freed using NetApiBufferFree.
  98. --*/
  99. {
  100. OEM_STRING OemString;
  101. NET_API_STATUS ApiStatus;
  102. UNICODE_STRING UnicodeString;
  103. RtlInitString( &OemString, Oem );
  104. UnicodeString.MaximumLength =
  105. (USHORT) RtlOemStringToUnicodeSize( &OemString );
  106. ApiStatus = NetApiBufferAllocate(
  107. UnicodeString.MaximumLength,
  108. (LPVOID *) & UnicodeString.Buffer );
  109. if (ApiStatus != NO_ERROR) {
  110. return (NULL);
  111. }
  112. NetpAssert(UnicodeString.Buffer != NULL);
  113. if(!NT_SUCCESS( RtlOemStringToUnicodeString( &UnicodeString,
  114. &OemString,
  115. FALSE))){
  116. (void) NetApiBufferFree( UnicodeString.Buffer );
  117. return NULL;
  118. }
  119. return UnicodeString.Buffer;
  120. } // NetpAllocWStrFromStr
  121. LPWSTR
  122. NetpAllocWStrFromWStr(
  123. IN LPWSTR Unicode
  124. )
  125. /*++
  126. Routine Description:
  127. Allocate and copy unicode string (wide character strdup)
  128. Arguments:
  129. Unicode - pointer to wide character string to make copy of
  130. Return Value:
  131. NULL - There was some error in the conversion.
  132. Otherwise, it returns a pointer to the zero terminated UNICODE string in
  133. an allocated buffer. The buffer must be freed using NetApiBufferFree.
  134. --*/
  135. {
  136. NET_API_STATUS status;
  137. DWORD size;
  138. LPWSTR ptr;
  139. size = WCSSIZE(Unicode);
  140. status = NetApiBufferAllocate(size, (LPVOID *) (LPVOID) &ptr);
  141. if (status != NO_ERROR) {
  142. return NULL;
  143. }
  144. RtlCopyMemory(ptr, Unicode, size);
  145. return ptr;
  146. } // NetpAllocWStrFromWStr
  147. LPWSTR
  148. NetpAllocWStrFromAStr(
  149. IN LPCSTR Ansi
  150. )
  151. /*++
  152. Routine Description:
  153. Convert an Oem (zero terminated) string to the corresponding UNICODE
  154. string.
  155. Arguments:
  156. Oem - Specifies the Oem zero terminated string to convert.
  157. Return Value:
  158. NULL - There was some error in the conversion.
  159. Otherwise, it returns a pointer to the zero terminated UNICODE string in
  160. an allocated buffer. The buffer must be freed using NetApiBufferFree.
  161. --*/
  162. {
  163. ANSI_STRING AnsiString;
  164. NET_API_STATUS ApiStatus;
  165. UNICODE_STRING UnicodeString;
  166. RtlInitString( &AnsiString, Ansi );
  167. UnicodeString.MaximumLength =
  168. (USHORT) RtlAnsiStringToUnicodeSize( &AnsiString );
  169. ApiStatus = NetApiBufferAllocate(
  170. UnicodeString.MaximumLength,
  171. (LPVOID *) & UnicodeString.Buffer );
  172. if (ApiStatus != NO_ERROR) {
  173. return (NULL);
  174. }
  175. NetpAssert(UnicodeString.Buffer != NULL);
  176. if(!NT_SUCCESS( RtlAnsiStringToUnicodeString( &UnicodeString,
  177. &AnsiString,
  178. FALSE))){
  179. (void) NetApiBufferFree( UnicodeString.Buffer );
  180. return NULL;
  181. }
  182. return UnicodeString.Buffer;
  183. } // NetpAllocWStrFromAStr
  184. LPSTR
  185. NetpAllocAStrFromWStr (
  186. IN LPCWSTR Unicode
  187. )
  188. /*++
  189. Routine Description:
  190. Convert an UNICODE (zero terminated) string to the corresponding ANSI
  191. string.
  192. Arguments:
  193. Unicode - Specifies the UNICODE zero terminated string to convert.
  194. Return Value:
  195. NULL - There was some error in the conversion.
  196. Otherwise, it returns a pointer to the zero terminated ANSI string in
  197. an allocated buffer. The buffer must be freed using NetApiBufferFree.
  198. --*/
  199. {
  200. ANSI_STRING AnsiString;
  201. NET_API_STATUS ApiStatus;
  202. UNICODE_STRING UnicodeString;
  203. RtlInitUnicodeString( &UnicodeString, Unicode );
  204. AnsiString.MaximumLength =
  205. (USHORT) RtlUnicodeStringToAnsiSize( &UnicodeString );
  206. ApiStatus = NetApiBufferAllocate(
  207. AnsiString.MaximumLength,
  208. (LPVOID *) (LPVOID) & AnsiString.Buffer );
  209. if (ApiStatus != NO_ERROR) {
  210. NetpAssert( ApiStatus == ERROR_NOT_ENOUGH_MEMORY );
  211. return (NULL);
  212. }
  213. NetpAssert( AnsiString.Buffer != NULL );
  214. if(!NT_SUCCESS( RtlUnicodeStringToAnsiString( &AnsiString,
  215. &UnicodeString,
  216. FALSE))){
  217. (void) NetApiBufferFree( AnsiString.Buffer );
  218. return NULL;
  219. }
  220. return AnsiString.Buffer;
  221. } // NetpAllocStrFromWStr