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.

201 lines
4.5 KiB

  1. /*++
  2. Copyright (c) 1991-1992 Microsoft Corporation
  3. Module Name:
  4. convert.c
  5. Abstract:
  6. Contains functions for converting unicode and ansi strings.
  7. Author:
  8. Dan Lafferty (danl) 04-Jan-1992
  9. Environment:
  10. User Mode -Win32
  11. Notes:
  12. optional-notes
  13. Revision History:
  14. 04-Jan-1992 danl
  15. created
  16. 20-May-1992 JohnRo
  17. Use CONST where possible.
  18. --*/
  19. #include <scpragma.h>
  20. #include <nt.h>
  21. #include <ntrtl.h>
  22. #include <nturtl.h>
  23. #include <windef.h>
  24. #include <winbase.h> // LocalAlloc
  25. #include <string.h>
  26. #include <scdebug.h> // SC_LOG
  27. #include <sclib.h> // My prototype.
  28. BOOL
  29. ScConvertToUnicode(
  30. OUT LPWSTR *UnicodeOut,
  31. IN LPCSTR AnsiIn
  32. )
  33. /*++
  34. Routine Description:
  35. This function translates an AnsiString into a Unicode string.
  36. A new string buffer is created by this function. If the call to
  37. this function is successful, the caller must take responsibility for
  38. the unicode string buffer that was allocated by this function.
  39. The allocated buffer should be free'd with a call to LocalFree.
  40. NOTE: This function allocates memory for the Unicode String.
  41. Arguments:
  42. AnsiIn - This is a pointer to an ansi string that is to be converted.
  43. UnicodeOut - This is a pointer to a location where the pointer to the
  44. unicode string is to be placed.
  45. Return Value:
  46. TRUE - The conversion was successful.
  47. FALSE - The conversion was unsuccessful. In this case a buffer for
  48. the unicode string was not allocated.
  49. --*/
  50. {
  51. NTSTATUS ntStatus;
  52. DWORD bufSize;
  53. UNICODE_STRING unicodeString;
  54. ANSI_STRING ansiString;
  55. //
  56. // Allocate a buffer for the unicode string.
  57. //
  58. bufSize = ((DWORD) strlen(AnsiIn) + 1) * sizeof(WCHAR);
  59. *UnicodeOut = (LPWSTR) LocalAlloc(LMEM_ZEROINIT, bufSize);
  60. if (*UnicodeOut == NULL) {
  61. SC_LOG(ERROR,"ScConvertToUnicode:LocalAlloc Failure %ld\n",GetLastError());
  62. return(FALSE);
  63. }
  64. //
  65. // Initialize the string structures
  66. //
  67. RtlInitAnsiString( &ansiString, AnsiIn);
  68. unicodeString.Buffer = *UnicodeOut;
  69. unicodeString.MaximumLength = (USHORT)bufSize;
  70. unicodeString.Length = 0;
  71. //
  72. // Call the conversion function.
  73. //
  74. ntStatus = RtlAnsiStringToUnicodeString (
  75. &unicodeString, // Destination
  76. &ansiString, // Source
  77. (BOOLEAN) FALSE); // Allocate the destination
  78. if (!NT_SUCCESS(ntStatus)) {
  79. SC_LOG(ERROR,
  80. "ScConvertToUnicode:RtlAnsiStringToUnicodeString Failure %lx\n",
  81. ntStatus);
  82. LocalFree(*UnicodeOut);
  83. *UnicodeOut = NULL;
  84. return(FALSE);
  85. }
  86. return(TRUE);
  87. }
  88. BOOL
  89. ScConvertToAnsi(
  90. OUT LPSTR AnsiOut,
  91. IN LPCWSTR UnicodeIn
  92. )
  93. /*++
  94. Routine Description:
  95. This function translates a UnicodeString into an Ansi string.
  96. BEWARE!
  97. It is assumped that the buffer pointed to by AnsiOut is large
  98. enough to hold the Unicode String. Check sizes first.
  99. If it is desired, UnicodeIn and AnsiIn can point to the same
  100. location. Since the ansi string will always be smaller than the
  101. unicode string, translation can take place in the same buffer.
  102. Arguments:
  103. UnicodeIn - This is a pointer to a unicode that is to be converted.
  104. AnsiOut - This is a pointer to a buffer that will contain the
  105. ansi string on return from this function call.
  106. Return Value:
  107. TRUE - The conversion was successful.
  108. FALSE - The conversion was unsuccessful.
  109. --*/
  110. {
  111. NTSTATUS ntStatus;
  112. UNICODE_STRING unicodeString;
  113. ANSI_STRING ansiString;
  114. //
  115. // Initialize the string structures
  116. //
  117. RtlInitUnicodeString( &unicodeString, UnicodeIn);
  118. ansiString.Buffer = AnsiOut;
  119. ansiString.MaximumLength = unicodeString.MaximumLength;
  120. ansiString.Length = 0;
  121. //
  122. // Call the conversion function.
  123. //
  124. ntStatus = RtlUnicodeStringToAnsiString (
  125. &ansiString, // Destination
  126. &unicodeString, // Source
  127. (BOOLEAN) FALSE); // Allocate the destination
  128. if (!NT_SUCCESS(ntStatus)) {
  129. SC_LOG(ERROR,"ScConvertToAnsi:RtlUnicodeStrintToAnsiString Failure %lx\n",
  130. ntStatus);
  131. return(FALSE);
  132. }
  133. return(TRUE);
  134. }