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.

176 lines
4.2 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // attrcvt.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file defines methods for converting attributes to
  12. // different formats.
  13. //
  14. // MODIFICATION HISTORY
  15. //
  16. // 02/26/1998 Original version.
  17. // 03/27/1998 InetAddr's are persisted as integers.
  18. // 08/24/1998 Make use of IASTL utility classes.
  19. //
  20. ///////////////////////////////////////////////////////////////////////////////
  21. #include <ias.h>
  22. #include <iastlutl.h>
  23. #include <iasutil.h>
  24. #include <varvec.h>
  25. #include <attrcvt.h>
  26. using IASTL::IASAttribute;
  27. PIASATTRIBUTE
  28. WINAPI
  29. IASAttributeFromVariant(
  30. VARIANT* src,
  31. IASTYPE type
  32. ) throw (_com_error)
  33. {
  34. using _com_util::CheckError;
  35. using _w32_util::CheckSuccess;
  36. // Allocate an attribute to hold the result.
  37. IASAttribute dst(true);
  38. // Switch off the destination type.
  39. switch (type)
  40. {
  41. case IASTYPE_BOOLEAN:
  42. {
  43. CheckError(VariantChangeType(src, src, NULL, VT_BOOL));
  44. dst->Value.Boolean = (V_BOOL(src) != VARIANT_FALSE) ? TRUE : FALSE;
  45. break;
  46. }
  47. case IASTYPE_INTEGER:
  48. case IASTYPE_INET_ADDR:
  49. case IASTYPE_ENUM:
  50. {
  51. CheckError(VariantChangeType(src, src, NULL, VT_I4));
  52. dst->Value.Integer = V_I4(src);
  53. break;
  54. }
  55. case IASTYPE_STRING:
  56. {
  57. CheckError(VariantChangeType(src, src, NULL, VT_BSTR));
  58. dst.setString(V_BSTR(src));
  59. break;
  60. }
  61. case IASTYPE_OCTET_STRING:
  62. case IASTYPE_PROV_SPECIFIC:
  63. {
  64. PBYTE value;
  65. DWORD length;
  66. if (V_VT(src) == (VT_ARRAY | VT_UI1) ||
  67. V_VT(src) == (VT_ARRAY | VT_I1))
  68. {
  69. // If we have a safearray of bytes, we'll use it as is ...
  70. CVariantVector<BYTE> octets(src);
  71. dst.setOctetString(octets.size(), octets.data());
  72. }
  73. else
  74. {
  75. // ... otherwise we'll coerce to a BSTR.
  76. CheckError(VariantChangeType(src, src, NULL, VT_BSTR));
  77. dst.setOctetString(V_BSTR(src));
  78. }
  79. break;
  80. }
  81. case IASTYPE_UTC_TIME:
  82. {
  83. CheckError(VariantChangeType(src, src, NULL, VT_DATE));
  84. SYSTEMTIME st;
  85. if (!VariantTimeToSystemTime(V_DATE(src), &st))
  86. {
  87. _com_issue_error(E_INVALIDARG);
  88. }
  89. CheckSuccess(SystemTimeToFileTime(&st, &dst->Value.UTCTime));
  90. break;
  91. }
  92. default:
  93. _com_issue_error(E_INVALIDARG);
  94. }
  95. // We don't set the type until the attribute has been properly initialized.
  96. // Otherwise IASAttributeRelease will have problems if we throw an
  97. // exception.
  98. dst->Value.itType = type;
  99. return dst.detach();
  100. }
  101. //////////
  102. // Convert an LDAP berval to a newly allocated IASATTRIBUTE.
  103. //////////
  104. PIASATTRIBUTE
  105. WINAPI
  106. IASAttributeFromBerVal(
  107. const berval& src,
  108. IASTYPE type
  109. ) throw (_com_error)
  110. {
  111. // Allocate an attribute.
  112. IASAttribute dst(true);
  113. // Convert the berval based on the IASTYPE.
  114. switch (type)
  115. {
  116. case IASTYPE_BOOLEAN:
  117. {
  118. dst->Value.Boolean =
  119. _strnicmp(src.bv_val, "TRUE", src.bv_len) ? FALSE : TRUE;
  120. break;
  121. }
  122. case IASTYPE_INTEGER:
  123. case IASTYPE_INET_ADDR:
  124. case IASTYPE_ENUM:
  125. {
  126. dst->Value.Integer = strtoul(src.bv_val, NULL, 10);
  127. break;
  128. }
  129. case IASTYPE_STRING:
  130. {
  131. dst.setString((PCSTR)src.bv_val);
  132. break;
  133. }
  134. case IASTYPE_OCTET_STRING:
  135. case IASTYPE_PROV_SPECIFIC:
  136. {
  137. dst.setOctetString(src.bv_len, (const BYTE*)src.bv_val);
  138. break;
  139. }
  140. case IASTYPE_UTC_TIME:
  141. default:
  142. _com_issue_error(E_INVALIDARG);
  143. }
  144. // We don't set the type until the attribute has been properly initialized.
  145. // Otherwise IASAttributeRelease will have problems if we throw an
  146. // exception.
  147. dst->Value.itType = type;
  148. return dst.detach();
  149. }