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.

355 lines
8.3 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. ULONG NewLength;
  104. RtlInitString( &OemString, Oem );
  105. NewLength = RtlOemStringToUnicodeSize( &OemString );
  106. if (NewLength > MAXUSHORT)
  107. {
  108. return NULL;
  109. }
  110. UnicodeString.MaximumLength = (USHORT) NewLength;
  111. ApiStatus = NetApiBufferAllocate(
  112. UnicodeString.MaximumLength,
  113. (LPVOID *) & UnicodeString.Buffer );
  114. if (ApiStatus != NO_ERROR)
  115. {
  116. return NULL;
  117. }
  118. NetpAssert(UnicodeString.Buffer != NULL);
  119. if(!NT_SUCCESS( RtlOemStringToUnicodeString( &UnicodeString,
  120. &OemString,
  121. FALSE))){
  122. (void) NetApiBufferFree( UnicodeString.Buffer );
  123. return NULL;
  124. }
  125. return UnicodeString.Buffer;
  126. } // NetpAllocWStrFromStr
  127. LPWSTR
  128. NetpAllocWStrFromWStr(
  129. IN LPWSTR Unicode
  130. )
  131. /*++
  132. Routine Description:
  133. Allocate and copy unicode string (wide character strdup)
  134. Arguments:
  135. Unicode - pointer to wide character string to make copy of
  136. Return Value:
  137. NULL - There was some error in the conversion.
  138. Otherwise, it returns a pointer to the zero terminated UNICODE string in
  139. an allocated buffer. The buffer must be freed using NetApiBufferFree.
  140. --*/
  141. {
  142. NET_API_STATUS status;
  143. DWORD size;
  144. LPWSTR ptr;
  145. size = WCSSIZE(Unicode);
  146. status = NetApiBufferAllocate(size, (LPVOID *) (LPVOID) &ptr);
  147. if (status != NO_ERROR) {
  148. return NULL;
  149. }
  150. RtlCopyMemory(ptr, Unicode, size);
  151. return ptr;
  152. } // NetpAllocWStrFromWStr
  153. LPWSTR
  154. NetpAllocWStrFromAStr(
  155. IN LPCSTR Ansi
  156. )
  157. /*++
  158. Routine Description:
  159. Convert an Oem (zero terminated) string to the corresponding UNICODE
  160. string.
  161. Arguments:
  162. Oem - Specifies the Oem zero terminated string to convert.
  163. Return Value:
  164. NULL - There was some error in the conversion.
  165. Otherwise, it returns a pointer to the zero terminated UNICODE string in
  166. an allocated buffer. The buffer must be freed using NetApiBufferFree.
  167. --*/
  168. {
  169. ANSI_STRING AnsiString;
  170. NET_API_STATUS ApiStatus;
  171. UNICODE_STRING UnicodeString;
  172. ULONG NewLength;
  173. RtlInitString( &AnsiString, Ansi );
  174. NewLength = RtlAnsiStringToUnicodeSize( &AnsiString );
  175. if (NewLength > MAXUSHORT)
  176. {
  177. return NULL;
  178. }
  179. UnicodeString.MaximumLength = (USHORT) NewLength;
  180. ApiStatus = NetApiBufferAllocate(
  181. UnicodeString.MaximumLength,
  182. (LPVOID *) & UnicodeString.Buffer );
  183. if (ApiStatus != NO_ERROR)
  184. {
  185. return NULL;
  186. }
  187. NetpAssert(UnicodeString.Buffer != NULL);
  188. if(!NT_SUCCESS( RtlAnsiStringToUnicodeString( &UnicodeString,
  189. &AnsiString,
  190. FALSE))){
  191. (void) NetApiBufferFree( UnicodeString.Buffer );
  192. return NULL;
  193. }
  194. return UnicodeString.Buffer;
  195. } // NetpAllocWStrFromAStr
  196. LPSTR
  197. NetpAllocAStrFromWStr (
  198. IN LPCWSTR Unicode
  199. )
  200. /*++
  201. Routine Description:
  202. Convert an UNICODE (zero terminated) string to the corresponding ANSI
  203. string.
  204. Arguments:
  205. Unicode - Specifies the UNICODE zero terminated string to convert.
  206. Return Value:
  207. NULL - There was some error in the conversion.
  208. Otherwise, it returns a pointer to the zero terminated ANSI string in
  209. an allocated buffer. The buffer must be freed using NetApiBufferFree.
  210. --*/
  211. {
  212. ANSI_STRING AnsiString;
  213. NET_API_STATUS ApiStatus;
  214. UNICODE_STRING UnicodeString;
  215. if (!NT_SUCCESS(RtlInitUnicodeStringEx( &UnicodeString, Unicode )))
  216. {
  217. return NULL;
  218. }
  219. AnsiString.MaximumLength =
  220. (USHORT) RtlUnicodeStringToAnsiSize( &UnicodeString );
  221. ApiStatus = NetApiBufferAllocate(
  222. AnsiString.MaximumLength,
  223. (LPVOID *) (LPVOID) & AnsiString.Buffer );
  224. if (ApiStatus != NO_ERROR)
  225. {
  226. NetpAssert( ApiStatus == ERROR_NOT_ENOUGH_MEMORY );
  227. return NULL;
  228. }
  229. NetpAssert( AnsiString.Buffer != NULL );
  230. if(!NT_SUCCESS( RtlUnicodeStringToAnsiString( &AnsiString,
  231. &UnicodeString,
  232. FALSE))){
  233. (void) NetApiBufferFree( AnsiString.Buffer );
  234. return NULL;
  235. }
  236. return AnsiString.Buffer;
  237. } // NetpAllocStrFromWStr