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.

268 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 1991-1992 Microsoft Corporation
  3. Module Name:
  4. XsUnicod.c
  5. Abstract:
  6. This module contains Unicode routines for XACTSRV.
  7. Author:
  8. Shanku Niyogi (w-shankn) 27-Sep-1990
  9. Revision History:
  10. --*/
  11. #include "xactsrvp.h"
  12. #include <prefix.h> // PREFIX_ equates.
  13. LPWSTR
  14. XsDupStrToWStr(
  15. IN LPSTR Src
  16. )
  17. /*++
  18. Routine Description:
  19. This routine is an ANSI->Unicode equivalent of the run-time strdup
  20. function.
  21. Arguments:
  22. Src - A pointer to the source string.
  23. Return Value:
  24. LPWSTR - Pointer to the destination string if successful, NULL otherwise.
  25. Memory must be freed with NetpMemoryFree.
  26. --*/
  27. {
  28. LPWSTR dest = NULL;
  29. if (( dest = NetpMemoryAllocate(sizeof(WCHAR) * ( strlen( Src ) + 1 )))
  30. == NULL ) {
  31. return NULL;
  32. }
  33. NetpCopyStrToWStr( dest, Src );
  34. return dest;
  35. } // XsDupStrToWStr
  36. LPSTR
  37. XsDupWStrToStr(
  38. IN LPWSTR Src
  39. )
  40. /*++
  41. Routine Description:
  42. This routine is a Unicode->ANSI equivalent of the run-time strdup
  43. function.
  44. Arguments:
  45. Src - A pointer to the source string.
  46. Return Value:
  47. LPSTR - Pointer to the destination string if successful, NULL otherwise.
  48. Memory must be freed with NetpMemoryFree.
  49. --*/
  50. {
  51. LPSTR dest = NULL;
  52. if (( dest = NetpMemoryAllocate( NetpUnicodeToDBCSLen( Src ) + 1 )) == NULL ) {
  53. return NULL;
  54. }
  55. NetpCopyWStrToStrDBCS( dest, Src );
  56. return dest;
  57. } // XsDupWStrToStr
  58. LPSTR
  59. XsDupStrToStr(
  60. IN LPSTR Src
  61. )
  62. /*++
  63. Routine Description:
  64. This routine is equivalent to the run-time strdup function, but allocates
  65. memory using NetpMemory functions.
  66. Arguments:
  67. Src - A pointer to the source string.
  68. Return Value:
  69. LPSTR - Pointer to the destination string if successful, NULL otherwise.
  70. Memory must be freed with NetpMemoryFree.
  71. --*/
  72. {
  73. LPSTR dest = NULL;
  74. if (( dest = NetpMemoryAllocate( strlen( Src ) + 1 )) == NULL ) {
  75. return NULL;
  76. }
  77. strcpy( dest, Src );
  78. return dest;
  79. } // XsDupStrToStr
  80. #ifdef UNICODE
  81. VOID
  82. XsCopyTBufToBuf(
  83. OUT LPBYTE Dest,
  84. IN LPBYTE Src,
  85. IN DWORD DestSize
  86. )
  87. /*++
  88. Routine Description:
  89. This routine is a Unicode->ANSI equivalent of the run-time memcpy
  90. function.
  91. Arguments:
  92. Dest - A pointer to the destination buffer.
  93. Src - A pointer to the source buffer.
  94. DestSize - The size, in bytes, of the destination buffer.
  95. Return Value:
  96. none.
  97. --*/
  98. {
  99. DWORD finalDestSize;
  100. NTSTATUS ntStatus;
  101. DWORD srcSize;
  102. if ( (Dest == NULL) || (Src == NULL) || (DestSize == 0) ) {
  103. return;
  104. }
  105. srcSize = WCSSIZE( (LPWSTR) Src );
  106. NetpAssert( srcSize > 0 );
  107. ntStatus = RtlUnicodeToOemN(
  108. (PCHAR) Dest, // OEM string
  109. (ULONG) DestSize, // max bytes in OEM string
  110. (PULONG) & finalDestSize, // bytes in OEM string
  111. (PWSTR) Src, // UNICODE string
  112. (ULONG) srcSize // bytes in UNICODE string
  113. );
  114. if ( !NT_SUCCESS( ntStatus ) ) {
  115. IF_DEBUG(ERRORS) {
  116. NetpKdPrint(( PREFIX_XACTSRV
  117. "XsCopyTBufToBuf: unexpected return code from "
  118. "RtlUnicodeToOemN: " FORMAT_NTSTATUS ".\n",
  119. ntStatus ));
  120. }
  121. }
  122. return;
  123. } // XsCopyTBufToBuf
  124. VOID
  125. XsCopyBufToTBuf(
  126. OUT LPBYTE Dest,
  127. IN LPBYTE Src,
  128. IN DWORD SrcSize
  129. )
  130. /*++
  131. Routine Description:
  132. This routine is a ANSI->Unicode equivalent of the run-time memcpy
  133. function.
  134. Arguments:
  135. Dest - A pointer to the destination buffer.
  136. Src - A pointer to the source buffer.
  137. SrcSize - The size, in bytes, of the source buffer.
  138. Return Value:
  139. none.
  140. --*/
  141. {
  142. DWORD finalDestSize;
  143. DWORD destSize = SrcSize * sizeof(WCHAR); // max byte count for dest.
  144. NTSTATUS ntStatus;
  145. if ( (Dest == NULL) || (Src == NULL) || (SrcSize == 0) ) {
  146. return;
  147. }
  148. NetpAssert( destSize > 0 );
  149. ntStatus = RtlOemToUnicodeN(
  150. (PWSTR) Dest, // UNICODE string
  151. (ULONG) destSize, // max bytes in UNICODE buffer
  152. (PULONG) & finalDestSize, // final bytes in UNICODE buffer
  153. (PCHAR) Src, // OEM string
  154. (ULONG) SrcSize // bytes in OEM string
  155. );
  156. if ( !NT_SUCCESS( ntStatus ) ) {
  157. IF_DEBUG(ERRORS) {
  158. NetpKdPrint(( PREFIX_XACTSRV
  159. "XsCopyBufToTBuf: unexpected return code from "
  160. "RtlOemToUnicodeN: " FORMAT_NTSTATUS ".\n",
  161. ntStatus ));
  162. }
  163. }
  164. } // XsCopyBufToTBuf
  165. #endif // def UNICODE