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.

172 lines
4.8 KiB

  1. // AZipValue.cpp -- CAbstractZipValue class definition
  2. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  3. // 1999. This computer program includes Confidential, Proprietary
  4. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  5. // use, disclosure, and/or reproduction is prohibited unless authorized
  6. // in writing. All Rights Reserved.
  7. #include "NoWarning.h"
  8. #include <windows.h> // for zip_public.h
  9. #include <scuArrayP.h>
  10. #include <slbZip.h>
  11. #include "AZipValue.h"
  12. #include "TransactionWrap.h"
  13. using namespace std;
  14. using namespace cci;
  15. /////////////////////////// LOCAL/HELPER /////////////////////////////////
  16. namespace
  17. {
  18. struct ZipBuffer
  19. {
  20. ZipBuffer()
  21. : m_pbData(0),
  22. m_uLength(0)
  23. {};
  24. ~ZipBuffer() throw()
  25. {
  26. try
  27. {
  28. if (m_pbData)
  29. free(m_pbData);
  30. }
  31. catch (...)
  32. {
  33. }
  34. };
  35. BYTE *m_pbData;
  36. UINT m_uLength;
  37. };
  38. std::string
  39. AsString(ZipBuffer const &rzb)
  40. {
  41. return string(reinterpret_cast<char *>(rzb.m_pbData),
  42. rzb.m_uLength);
  43. };
  44. } // namespace
  45. /////////////////////////// PUBLIC /////////////////////////////////
  46. // Types
  47. // C'tors/D'tors
  48. CAbstractZipValue::~CAbstractZipValue()
  49. {}
  50. // Operators
  51. // Operations
  52. void
  53. CAbstractZipValue::Value(ValueType const &rhs)
  54. {
  55. CTransactionWrap wrap(m_hcard);
  56. bool fReplaceData = !m_avData.IsCached() ||
  57. (m_avData.Value() != rhs);
  58. if (fReplaceData)
  59. {
  60. DoValue(Zip(rhs, m_fAlwaysZip));
  61. m_avData.Value(rhs);
  62. }
  63. }
  64. // Access
  65. string
  66. CAbstractZipValue::Value()
  67. {
  68. CTransactionWrap wrap(m_hcard);
  69. if (!m_avData.IsCached())
  70. m_avData.Value(UnZip(DoValue()));
  71. return m_avData.Value();
  72. }
  73. // Predicates
  74. // Static Variables
  75. /////////////////////////// PROTECTED /////////////////////////////////
  76. // C'tors/D'tors
  77. CAbstractZipValue::CAbstractZipValue(CAbstractCard const &racard,
  78. ObjectAccess oa,
  79. bool fAlwaysZip)
  80. : CProtectableCrypt(racard, oa),
  81. m_fAlwaysZip(fAlwaysZip),
  82. m_avData()
  83. {}
  84. // Operators
  85. // Operations
  86. // Access
  87. // Predicates
  88. // Static Variables
  89. /////////////////////////// PRIVATE /////////////////////////////////
  90. // C'tors/D'tors
  91. // Operators
  92. // Operations
  93. // Compress (zip) the data, returning the smaller of the zipped or
  94. // the original data.
  95. CAbstractZipValue::ZipCapsule
  96. CAbstractZipValue::Zip(std::string const &rsData,
  97. bool fAlwaysZip)
  98. {
  99. ZipBuffer zb;
  100. size_t const cTempLength =
  101. rsData.size() * sizeof string::value_type;
  102. scu::AutoArrayPtr<BYTE> aabTemp(new BYTE[cTempLength]);
  103. memcpy(aabTemp.Get(), rsData.data(), cTempLength);
  104. CompressBuffer(aabTemp.Get(), cTempLength, &zb.m_pbData, &zb.m_uLength);
  105. return (fAlwaysZip || (cTempLength > zb.m_uLength))
  106. ? ZipCapsule(AsString(zb), true)
  107. : ZipCapsule(rsData, false);
  108. }
  109. string
  110. CAbstractZipValue::UnZip(ZipCapsule const &rzc)
  111. {
  112. std::string strTemp(rzc.Data());
  113. if (rzc.IsCompressed())
  114. {
  115. // Need to decompress
  116. ZipBuffer zb;
  117. size_t cTempLength =
  118. strTemp.size() * sizeof string::value_type;
  119. scu::AutoArrayPtr<BYTE> aabTemp(new BYTE[cTempLength]);
  120. memcpy(aabTemp.Get(), strTemp.data(), cTempLength);
  121. DecompressBuffer(aabTemp.Get(), cTempLength,
  122. &zb.m_pbData, &zb.m_uLength);
  123. strTemp = AsString(zb);
  124. }
  125. return strTemp;
  126. }
  127. // Access
  128. // Predicates
  129. // Static Variables