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.

177 lines
3.7 KiB

  1. /*++
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. Module Name:
  4. dpapi.c
  5. Abstract:
  6. WMI data provider api set
  7. Author:
  8. 16-Jan-1997 AlanWar
  9. Revision History:
  10. --*/
  11. #include "wmiump.h"
  12. ULONG WmipCountedAnsiToCountedUnicode(
  13. PCHAR Ansi,
  14. PWCHAR Unicode
  15. )
  16. /*++
  17. Routine Description:
  18. Translate a counted ansi string into a counted unicode string.
  19. Conversion may be done inplace, that is Ansi == Unicode.
  20. Arguments:
  21. Ansi is the counted ansi string to convert to UNICODE
  22. Unicode is the buffer to place the converted string into
  23. Return Value:
  24. ERROR_SUCCESS or an error code
  25. --*/
  26. {
  27. PCHAR APtr;
  28. PWCHAR WPtr;
  29. ULONG AnsiSize, UnicodeSize;
  30. ULONG Status;
  31. AnsiSize = *((PUSHORT)Ansi);
  32. APtr = WmipAlloc(AnsiSize + 1);
  33. if (APtr != NULL)
  34. {
  35. memcpy(APtr, Ansi + sizeof(USHORT), AnsiSize);
  36. APtr[AnsiSize] = 0;
  37. WPtr = NULL;
  38. Status = AnsiToUnicode(APtr, &WPtr);
  39. if (Status == ERROR_SUCCESS)
  40. {
  41. UnicodeSize = (wcslen(WPtr)+1) * sizeof(WCHAR);
  42. *Unicode = (USHORT)UnicodeSize;
  43. memcpy(Unicode+1, WPtr, UnicodeSize);
  44. Status = ERROR_SUCCESS;
  45. WmipFree(WPtr);
  46. }
  47. WmipFree(APtr);
  48. } else {
  49. Status = ERROR_NOT_ENOUGH_MEMORY;
  50. }
  51. return(Status);
  52. }
  53. ULONG WmipCountedUnicodeToCountedAnsi(
  54. PWCHAR Unicode,
  55. PCHAR Ansi
  56. )
  57. /*++
  58. Routine Description:
  59. Translate a counted ansi string into a counted unicode string.
  60. Conversion may be done inplace, that is Ansi == Unicode.
  61. Arguments:
  62. Unicode is the counted unicode string to convert to ansi
  63. Ansi is the buffer to place the converted string into
  64. Return Value:
  65. ERROR_SUCCESS or an error code
  66. --*/
  67. {
  68. PCHAR APtr;
  69. PWCHAR WPtr;
  70. ULONG AnsiSize, UnicodeSize;
  71. ULONG Status;
  72. UnicodeSize = *Unicode;
  73. WPtr = WmipAlloc(UnicodeSize + sizeof(WCHAR));
  74. if (WPtr != NULL)
  75. {
  76. memcpy(WPtr, Unicode + 1, UnicodeSize);
  77. WPtr[UnicodeSize/sizeof(WCHAR)] = UNICODE_NULL;
  78. APtr = NULL;
  79. Status = UnicodeToAnsi(WPtr, &APtr, &AnsiSize);
  80. if (Status == ERROR_SUCCESS)
  81. {
  82. *((PUSHORT)Ansi) = (USHORT)AnsiSize;
  83. memcpy(Ansi+sizeof(USHORT), APtr, AnsiSize);
  84. Status = ERROR_SUCCESS;
  85. WmipFree(APtr);
  86. }
  87. WmipFree(WPtr);
  88. } else {
  89. Status = ERROR_NOT_ENOUGH_MEMORY;
  90. }
  91. return(Status);
  92. }
  93. ULONG WmipCopyStringToCountedUnicode(
  94. LPCWSTR String,
  95. PWCHAR CountedString,
  96. ULONG *BytesUsed,
  97. BOOLEAN ConvertFromAnsi
  98. )
  99. /*++
  100. Routine Description:
  101. This routine will copy an ansi ro unicode C string to a counted unicode
  102. string.
  103. Arguments:
  104. String is the ansi or unicode incoming string
  105. Counted string is a pointer to where to write counted unicode string
  106. *BytesUsed returns number of bytes used to build counted unicode string
  107. ConvertFromAnsi is TRUE if String is an ANSI string
  108. Return Value:
  109. ERROR_SUCCESS or an error code
  110. --*/
  111. {
  112. USHORT StringSize;
  113. PWCHAR StringPtr = CountedString+1;
  114. ULONG Status;
  115. if (ConvertFromAnsi)
  116. {
  117. StringSize = (strlen((PCHAR)String) +1) * sizeof(WCHAR);
  118. Status = AnsiToUnicode((PCHAR)String,
  119. &StringPtr);
  120. } else {
  121. StringSize = (wcslen(String) +1) * sizeof(WCHAR);
  122. wcscpy(StringPtr, String);
  123. Status = ERROR_SUCCESS;
  124. }
  125. *CountedString = StringSize;
  126. *BytesUsed = StringSize + sizeof(USHORT);
  127. return(Status);
  128. }