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.

171 lines
2.6 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. util.c
  5. Abstract:
  6. Utility functions.
  7. Author:
  8. Ovidiu Temereanca (ovidiut) 02-Jul-2000 Initial implementation
  9. Revision History:
  10. --*/
  11. #include "pch.h"
  12. VOID
  13. FreeString (
  14. IN PVOID String
  15. )
  16. {
  17. if (String) {
  18. HeapFree (GetProcessHeap (), 0, String);
  19. }
  20. }
  21. PSTR
  22. pAllocAndConvertToAnsi (
  23. IN PCWSTR Unicode,
  24. IN DWORD Size
  25. )
  26. {
  27. PSTR ansi;
  28. ansi = (PSTR) HeapAlloc (GetProcessHeap (), 0, Size);
  29. if (!ansi) {
  30. SetLastError (ERROR_NOT_ENOUGH_MEMORY);
  31. return NULL;
  32. }
  33. if (!WideCharToMultiByte (
  34. CP_ACP,
  35. 0,
  36. Unicode,
  37. Size,
  38. ansi,
  39. Size * sizeof (WCHAR),
  40. NULL,
  41. NULL
  42. )) {
  43. FreeString (ansi);
  44. return NULL;
  45. }
  46. return ansi;
  47. }
  48. PSTR
  49. ConvertToAnsiSz (
  50. IN PCWSTR Unicode
  51. )
  52. {
  53. DWORD size;
  54. if (!Unicode) {
  55. SetLastError (ERROR_SUCCESS);
  56. return NULL;
  57. }
  58. size = lstrlenW (Unicode) + 1;
  59. return pAllocAndConvertToAnsi (Unicode, size);
  60. }
  61. PSTR
  62. ConvertToAnsiMultiSz (
  63. IN PCWSTR MultiSzUnicode
  64. )
  65. {
  66. DWORD size;
  67. PCWSTR p;
  68. if (!MultiSzUnicode) {
  69. SetLastError (ERROR_SUCCESS);
  70. return NULL;
  71. }
  72. for (size = 1, p = MultiSzUnicode; *p; p = wcschr (p, 0) + 1) {
  73. size += lstrlenW (p) + 1;
  74. }
  75. return pAllocAndConvertToAnsi (MultiSzUnicode, size);
  76. }
  77. PWSTR
  78. pAllocAndConvertToUnicode (
  79. IN PCSTR Ansi,
  80. IN DWORD Size
  81. )
  82. {
  83. PWSTR unicode;
  84. unicode = (PWSTR) HeapAlloc (GetProcessHeap (), 0, Size * sizeof (WCHAR));
  85. if (!unicode) {
  86. SetLastError (ERROR_NOT_ENOUGH_MEMORY);
  87. return NULL;
  88. }
  89. if (!MultiByteToWideChar (
  90. CP_ACP,
  91. 0,
  92. Ansi,
  93. Size,
  94. unicode,
  95. Size
  96. )) {
  97. FreeString (unicode);
  98. unicode = NULL;
  99. }
  100. return unicode;
  101. }
  102. PWSTR
  103. ConvertToUnicodeSz (
  104. IN PCSTR Ansi
  105. )
  106. {
  107. DWORD size;
  108. if (!Ansi) {
  109. SetLastError (ERROR_SUCCESS);
  110. return NULL;
  111. }
  112. size = lstrlenA (Ansi) + 1;
  113. return pAllocAndConvertToUnicode (Ansi, size);
  114. }
  115. PWSTR
  116. ConvertToUnicodeMultiSz (
  117. IN PCSTR MultiSzAnsi
  118. )
  119. {
  120. DWORD size;
  121. PCSTR p;
  122. if (!MultiSzAnsi) {
  123. SetLastError (ERROR_SUCCESS);
  124. return NULL;
  125. }
  126. for (size = 1, p = MultiSzAnsi; *p; p = _mbschr (p, 0) + 1) {
  127. size += lstrlenA (p) + 1;
  128. }
  129. return pAllocAndConvertToUnicode (MultiSzAnsi, size);
  130. }