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.

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