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.

191 lines
3.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // iasattr.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Defines the API for manipulating IASATTRIBUTE structs.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 02/02/2000 Original version.
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include <polcypch.h>
  19. #include <iaspolcy.h>
  20. #include <iasattr.h>
  21. DWORD
  22. WINAPI
  23. IASAttributeAlloc(
  24. DWORD dwCount,
  25. IASATTRIBUTE** ppAttribute
  26. )
  27. {
  28. PIASATTRIBUTE* next = ppAttribute;
  29. for ( ; dwCount; --dwCount, ++next)
  30. {
  31. if (!(*next = (PIASATTRIBUTE)CoTaskMemAlloc(sizeof(IASATTRIBUTE))))
  32. {
  33. while (next > ppAttribute)
  34. {
  35. CoTaskMemFree(*--next);
  36. }
  37. return ERROR_NOT_ENOUGH_MEMORY;
  38. }
  39. memset(*next, 0, sizeof(IASATTRIBUTE));
  40. (*next)->dwReserved = 1;
  41. }
  42. return NO_ERROR;
  43. }
  44. DWORD
  45. WINAPI
  46. IASAttributeAddRef(
  47. PIASATTRIBUTE pAttribute
  48. )
  49. {
  50. if (pAttribute) { InterlockedIncrement((PLONG)&pAttribute->dwReserved); }
  51. return NO_ERROR;
  52. }
  53. DWORD
  54. WINAPI
  55. IASAttributeRelease(
  56. PIASATTRIBUTE pAttribute
  57. )
  58. {
  59. if (pAttribute && !InterlockedDecrement((PLONG)&pAttribute->dwReserved))
  60. {
  61. switch (pAttribute->Value.itType)
  62. {
  63. case IASTYPE_STRING:
  64. {
  65. CoTaskMemFree(pAttribute->Value.String.pszAnsi);
  66. CoTaskMemFree(pAttribute->Value.String.pszWide);
  67. break;
  68. }
  69. case IASTYPE_OCTET_STRING:
  70. {
  71. CoTaskMemFree(pAttribute->Value.OctetString.lpValue);
  72. break;
  73. }
  74. }
  75. CoTaskMemFree(pAttribute);
  76. }
  77. return ERROR_SUCCESS;
  78. }
  79. DWORD
  80. WINAPI
  81. IASAttributeUnicodeAlloc(
  82. PIASATTRIBUTE pAttribute
  83. )
  84. {
  85. if (!pAttribute ||
  86. pAttribute->Value.itType != IASTYPE_STRING ||
  87. pAttribute->Value.String.pszWide != NULL ||
  88. pAttribute->Value.String.pszAnsi == NULL)
  89. {
  90. return NO_ERROR;
  91. }
  92. // Find out how big a buffer we need.
  93. int lenW = MultiByteToWideChar(
  94. CP_ACP,
  95. 0,
  96. pAttribute->Value.String.pszAnsi,
  97. -1,
  98. NULL,
  99. 0);
  100. if (!lenW) { return GetLastError(); }
  101. LPWSTR wide = (LPWSTR)CoTaskMemAlloc(lenW * sizeof(WCHAR));
  102. if (!wide) { return ERROR_NOT_ENOUGH_MEMORY; }
  103. // Do the conversion.
  104. MultiByteToWideChar(
  105. CP_ACP,
  106. 0,
  107. pAttribute->Value.String.pszAnsi,
  108. -1,
  109. wide,
  110. lenW
  111. );
  112. CoTaskMemFree(
  113. InterlockedExchangePointer(
  114. (PVOID*)&pAttribute->Value.String.pszWide,
  115. wide
  116. )
  117. );
  118. return NO_ERROR;
  119. }
  120. DWORD
  121. WINAPI
  122. IASAttributeAnsiAlloc(
  123. PIASATTRIBUTE pAttribute
  124. )
  125. {
  126. if (!pAttribute ||
  127. pAttribute->Value.itType != IASTYPE_STRING ||
  128. pAttribute->Value.String.pszAnsi != NULL ||
  129. pAttribute->Value.String.pszWide == NULL)
  130. {
  131. return NO_ERROR;
  132. }
  133. // Find out how big a buffer we need.
  134. int lenA = WideCharToMultiByte(
  135. CP_ACP,
  136. 0,
  137. pAttribute->Value.String.pszWide,
  138. -1,
  139. NULL,
  140. 0,
  141. NULL,
  142. NULL
  143. );
  144. if (!lenA) { return GetLastError(); }
  145. LPSTR ansi = (LPSTR)CoTaskMemAlloc(lenA * sizeof(CHAR));
  146. if (!ansi) { return ERROR_NOT_ENOUGH_MEMORY; }
  147. // Do the conversion.
  148. WideCharToMultiByte(
  149. CP_ACP,
  150. 0,
  151. pAttribute->Value.String.pszWide,
  152. -1,
  153. ansi,
  154. lenA,
  155. NULL,
  156. NULL
  157. );
  158. CoTaskMemFree(
  159. InterlockedExchangePointer(
  160. (PVOID*)&pAttribute->Value.String.pszAnsi,
  161. ansi
  162. )
  163. );
  164. return NO_ERROR;
  165. }