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.

174 lines
5.3 KiB

  1. /* Copyright (c) 1991-1992 Microsoft Corporation */
  2. /* mmiomisc.c
  3. *
  4. * Miscellaneous utility functions.
  5. *
  6. * AsciiStrToUnicodeStr Convert ASCII string Unicode
  7. * CopyLPWSTRA Convert Unicode string to ASCII
  8. *
  9. * See also WinCom, which defines:
  10. *
  11. * lstrncpy copy a string (up to n characters)
  12. * lstrncat concatenate strings (up to n characters)
  13. * lstrncmp compare strings (up to n characters)
  14. * lmemcpy copy a memory block
  15. * hmemcpy copy a huge memory block
  16. * HPSTR the type "char huge *"
  17. * SEEK_SET/CUR/END constants used for seeking
  18. */
  19. #include <nt.h>
  20. #include <ntrtl.h>
  21. #include <nturtl.h>
  22. #include "winmmi.h"
  23. #include "mmioi.h"
  24. /*--------------------------------------------------------------------*\
  25. * Function prototypes
  26. \*--------------------------------------------------------------------*/
  27. // extern int wcslen(LPCWSTR pwsz);
  28. /**************************************************************************\
  29. * AllocUnicodeStr
  30. *
  31. *
  32. * Returns a UNICODE version of the given ASCII source string, or NULL if
  33. * no storage is available.
  34. * Users must call FreeUnicodeStr to free the allocated storage.
  35. *
  36. * 28-Apr-1992 StephenE Created
  37. \**************************************************************************/
  38. LPWSTR AllocUnicodeStr( LPCSTR lpSourceStr )
  39. {
  40. PBYTE pByte; // Ascii version of szFileName
  41. ULONG cbDst; // Length of lpSourceStr as a byte count
  42. cbDst = (strlen( lpSourceStr ) * sizeof(WCHAR)) + sizeof(WCHAR);
  43. pByte = HeapAlloc( hHeap, 0, cbDst );
  44. if ( pByte == (PBYTE)NULL ) {
  45. return (LPWSTR)NULL;
  46. }
  47. AsciiStrToUnicodeStr( pByte, pByte + cbDst, lpSourceStr );
  48. return (LPWSTR)pByte;
  49. }
  50. BOOL FreeUnicodeStr( LPWSTR lpStr )
  51. {
  52. return HeapFree( hHeap, 0, (PBYTE)lpStr );
  53. }
  54. /**************************************************************************\
  55. * AllocAsciiStr
  56. *
  57. *
  58. * Returns a ASCII version of the given UNICODE source string, or NULL if
  59. * no storage is available.
  60. * Users must call FreeAsciiStr to free the allocated storage.
  61. *
  62. * 28-Apr-1992 StrphenE Created
  63. \**************************************************************************/
  64. LPSTR AllocAsciiStr( LPCWSTR lpSourceStr )
  65. {
  66. PBYTE pByte; // Ascii version of szFileName
  67. ULONG cbDst; // Length of lpSourceStr as a byte count
  68. cbDst = (wcslen( lpSourceStr ) * sizeof(WCHAR)) + sizeof(WCHAR);
  69. pByte = HeapAlloc( hHeap, 0, cbDst );
  70. if ( pByte == (PBYTE)NULL ) {
  71. return (LPSTR)NULL;
  72. }
  73. UnicodeStrToAsciiStr( pByte, pByte + cbDst, lpSourceStr );
  74. return (LPSTR)pByte;
  75. }
  76. BOOL FreeAsciiStr( LPSTR lpStr )
  77. {
  78. return HeapFree( hHeap, 0, (PBYTE)lpStr );
  79. }
  80. /**************************************************************************\
  81. * AsciiStrToUnicodeStr
  82. *
  83. * Translate ANSI 'psrc' to UNICODE 'pdst' without destination going beyond
  84. * 'pmax'
  85. *
  86. * Return DWORD-aligned ptr beyond end of pdst, 0 if failed.
  87. *
  88. * 27-Aug-1991 IanJa Created
  89. \**************************************************************************/
  90. PBYTE AsciiStrToUnicodeStr( PBYTE pdst, PBYTE pmax, LPCSTR psrc )
  91. {
  92. int cbSrc;
  93. ULONG cbDst;
  94. cbSrc = strlen( psrc ) + sizeof(CHAR);
  95. /*
  96. * The destination UNICODE string will never be more than twice the
  97. * length of the ANSI source string. (It may sometimes be less, but
  98. * it's not worth computing it exactly now).
  99. */
  100. if ((pdst + (cbSrc * sizeof(WCHAR))) <= pmax) {
  101. /*
  102. * RtlMultiByteToUnicodeN() returns the exact number of
  103. * destination bytes.
  104. */
  105. RtlMultiByteToUnicodeN( (LPWSTR)pdst, // Unicode str
  106. (ULONG)(pmax - pdst), // max len of pdst
  107. &cbDst, // bytes in unicode str
  108. (PCHAR)psrc, // Source string
  109. cbSrc // bytes in source str
  110. );
  111. return pdst + ((cbDst + 3) & ~3);
  112. }
  113. return 0;
  114. }
  115. /**************************************************************************\
  116. * UnicodeStrToAsciiStr
  117. *
  118. * Translate UNICODE 'psrc' to ANSI 'pdst' without destination going beyond
  119. * 'pmax'
  120. *
  121. * Return DWORD-aligned ptr beyond end of pdst, 0 if failed.
  122. *
  123. * 27-Aug-1991 IanJa Created
  124. \**************************************************************************/
  125. PBYTE UnicodeStrToAsciiStr( PBYTE pdst, PBYTE pmax, LPCWSTR psrc)
  126. {
  127. int cbSrc;
  128. ULONG cbDst;
  129. cbSrc = (wcslen(psrc) * sizeof(WCHAR)) + sizeof(WCHAR);
  130. /*
  131. * The destination ANSI string will never be longer than the UNICODE
  132. * source string (in bytes). It is normally closer to half the length,
  133. * but due to the possibility of pre-composed characters, the upper
  134. * bound of the ANSI length is the UNICODE length (in bytes).
  135. */
  136. if ((pdst + cbSrc ) <= pmax) {
  137. /*
  138. * RtlUnicodeToMultiByteN() returns the exact number of
  139. * destination bytes.
  140. */
  141. RtlUnicodeToMultiByteN( (LPSTR)pdst, // ansi string
  142. (ULONG)(pmax - pdst), // max len of pdst
  143. &cbDst, // bytes copied
  144. (LPWSTR)psrc,
  145. cbSrc);
  146. return pdst + ((cbDst + 3) & ~3);
  147. }
  148. return 0;
  149. }