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.

149 lines
2.6 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. unicode.c
  5. Abstract:
  6. Simplified Unicode-Ansi conversion functions.
  7. Author:
  8. Jim Schmidt (jimschm) 03-Aug-2001
  9. Revision History:
  10. <alias> <date> <comments>
  11. --*/
  12. #include "pch.h"
  13. #include "commonp.h"
  14. static WORD g_GlobalCodePage = CP_ACP;
  15. static DWORD g_WToAFlags;
  16. static DWORD g_AToWFlags;
  17. PWSTR
  18. SzConvertBufferBytesAToW (
  19. OUT PWSTR OutputBuffer,
  20. IN PCSTR InputString,
  21. IN UINT ByteCountInclNul
  22. )
  23. {
  24. INT rc;
  25. DWORD flags;
  26. if (g_GlobalCodePage == CP_UTF8) {
  27. flags = 0;
  28. } else {
  29. flags = g_AToWFlags;
  30. }
  31. rc = MultiByteToWideChar (
  32. g_GlobalCodePage,
  33. flags,
  34. InputString,
  35. ByteCountInclNul,
  36. OutputBuffer,
  37. ByteCountInclNul * 2
  38. );
  39. if (!rc && ByteCountInclNul) {
  40. return NULL;
  41. }
  42. return OutputBuffer + rc;
  43. }
  44. PSTR
  45. SzConvertBufferBytesWToA (
  46. OUT PSTR OutputBuffer,
  47. IN PCWSTR InputString,
  48. IN UINT ByteCountInclNul
  49. )
  50. {
  51. INT rc;
  52. DWORD flags;
  53. UINT logicalChars;
  54. if (g_GlobalCodePage == CP_UTF8) {
  55. flags = 0;
  56. } else {
  57. flags = g_WToAFlags;
  58. }
  59. logicalChars = ByteCountInclNul / sizeof (WCHAR);
  60. rc = WideCharToMultiByte (
  61. g_GlobalCodePage,
  62. flags,
  63. InputString,
  64. logicalChars,
  65. OutputBuffer,
  66. logicalChars * 3,
  67. NULL,
  68. NULL
  69. );
  70. if (!rc && logicalChars) {
  71. return NULL;
  72. }
  73. return (PSTR) ((PBYTE) OutputBuffer + rc);
  74. }
  75. PWSTR
  76. RealSzConvertBytesAToW (
  77. IN PCSTR AnsiString,
  78. IN UINT ByteCountInclNul
  79. )
  80. {
  81. PWSTR alloc;
  82. PWSTR result;
  83. DWORD error;
  84. alloc = SzAllocW (ByteCountInclNul);
  85. result = SzConvertBufferBytesAToW (alloc, AnsiString, ByteCountInclNul);
  86. if (!result) {
  87. error = GetLastError();
  88. SzFreeW (alloc);
  89. SetLastError (error);
  90. }
  91. return alloc;
  92. }
  93. PSTR
  94. RealSzConvertBytesWToA (
  95. IN PCWSTR UnicodeString,
  96. IN UINT ByteCountInclNul
  97. )
  98. {
  99. PSTR alloc;
  100. PSTR result;
  101. DWORD error;
  102. UINT logicalChars;
  103. logicalChars = ByteCountInclNul / sizeof (WCHAR);
  104. alloc = SzAllocA (logicalChars);
  105. result = SzConvertBufferBytesWToA (alloc, UnicodeString, ByteCountInclNul);
  106. if (!result) {
  107. error = GetLastError();
  108. SzFreeA (alloc);
  109. SetLastError (error);
  110. }
  111. return alloc;
  112. }