ECSyncUtil.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <memory>
  19. #include "ECSyncUtil.h"
  20. #include <kopano/mapi_ptr.h>
  21. #include <kopano/memory.hpp>
  22. #include <mapix.h>
  23. using namespace KCHL;
  24. namespace KC {
  25. HRESULT HrDecodeSyncStateStream(LPSTREAM lpStream, ULONG *lpulSyncId, ULONG *lpulChangeId, PROCESSEDCHANGESSET *lpSetProcessChanged)
  26. {
  27. HRESULT hr = hrSuccess;
  28. STATSTG stat;
  29. ULONG ulSyncId = 0;
  30. ULONG ulChangeId = 0;
  31. ULONG ulChangeCount = 0;
  32. ULONG ulProcessedChangeId = 0;
  33. ULONG ulSourceKeySize = 0;
  34. LARGE_INTEGER liPos = {{0, 0}};
  35. PROCESSEDCHANGESSET setProcessedChanged;
  36. hr = lpStream->Stat(&stat, STATFLAG_NONAME);
  37. if(hr != hrSuccess)
  38. return hr;
  39. if (stat.cbSize.HighPart == 0 && stat.cbSize.LowPart == 0) {
  40. ulSyncId = 0;
  41. ulChangeId = 0;
  42. } else {
  43. if (stat.cbSize.HighPart != 0 || stat.cbSize.LowPart < 8)
  44. return MAPI_E_INVALID_PARAMETER;
  45. hr = lpStream->Seek(liPos, STREAM_SEEK_SET, NULL);
  46. if (hr != hrSuccess)
  47. return hr;
  48. hr = lpStream->Read(&ulSyncId, 4, NULL);
  49. if (hr != hrSuccess)
  50. return hr;
  51. hr = lpStream->Read(&ulChangeId, 4, NULL);
  52. if (hr != hrSuccess)
  53. return hr;
  54. // Following the sync ID and the change ID is the list of changes that were already processed for
  55. // this sync ID / change ID combination. This allows us partial processing of items retrieved from
  56. // the server.
  57. if (lpSetProcessChanged != NULL && lpStream->Read(&ulChangeCount, 4, NULL) == hrSuccess) {
  58. // The stream contains a list of already processed items, read them
  59. for (ULONG i = 0; i < ulChangeCount; ++i) {
  60. std::unique_ptr<char[]> lpData;
  61. hr = lpStream->Read(&ulProcessedChangeId, 4, NULL);
  62. if (hr != hrSuccess)
  63. /* Not the amount of expected bytes are there */
  64. return hr;
  65. hr = lpStream->Read(&ulSourceKeySize, 4, NULL);
  66. if (hr != hrSuccess)
  67. return hr;
  68. if (ulSourceKeySize > 1024)
  69. // Stupidly large source key, the stream must be bad.
  70. return MAPI_E_INVALID_PARAMETER;
  71. lpData.reset(new char[ulSourceKeySize]);
  72. hr = lpStream->Read(lpData.get(), ulSourceKeySize, NULL);
  73. if(hr != hrSuccess)
  74. return hr;
  75. setProcessedChanged.insert(std::pair<unsigned int, std::string>(ulProcessedChangeId, std::string(lpData.get(), ulSourceKeySize)));
  76. }
  77. }
  78. }
  79. if (lpulSyncId)
  80. *lpulSyncId = ulSyncId;
  81. if (lpulChangeId)
  82. *lpulChangeId = ulChangeId;
  83. if (lpSetProcessChanged)
  84. lpSetProcessChanged->insert(setProcessedChanged.begin(), setProcessedChanged.end());
  85. return hrSuccess;
  86. }
  87. HRESULT ResetStream(LPSTREAM lpStream)
  88. {
  89. LARGE_INTEGER liPos = {{0, 0}};
  90. ULARGE_INTEGER uliSize = {{8, 0}};
  91. HRESULT hr = lpStream->Seek(liPos, STREAM_SEEK_SET, NULL);
  92. if (hr != hrSuccess)
  93. return hr;
  94. hr = lpStream->SetSize(uliSize);
  95. if (hr != hrSuccess)
  96. return hr;
  97. hr = lpStream->Write("\0\0\0\0\0\0\0\0", 8, NULL);
  98. if (hr != hrSuccess)
  99. return hr;
  100. return lpStream->Seek(liPos, STREAM_SEEK_SET, NULL);
  101. }
  102. HRESULT CreateNullStatusStream(LPSTREAM *lppStream)
  103. {
  104. StreamPtr ptrStream;
  105. HRESULT hr = CreateStreamOnHGlobal(GlobalAlloc(GPTR, 8), true, &~ptrStream);
  106. if (hr != hrSuccess)
  107. return hr;
  108. hr = ResetStream(ptrStream);
  109. if (hr != hrSuccess)
  110. return hr;
  111. return ptrStream->QueryInterface(IID_IStream,
  112. reinterpret_cast<LPVOID *>(lppStream));
  113. }
  114. HRESULT HrGetOneBinProp(IMAPIProp *lpProp, ULONG ulPropTag, LPSPropValue *lppPropValue)
  115. {
  116. HRESULT hr = hrSuccess;
  117. object_ptr<IStream> lpStream;
  118. memory_ptr<SPropValue> lpPropValue;
  119. STATSTG sStat;
  120. ULONG ulRead = 0;
  121. if (lpProp == nullptr)
  122. return MAPI_E_INVALID_PARAMETER;
  123. hr = lpProp->OpenProperty(ulPropTag, &IID_IStream, 0, 0, &~lpStream);
  124. if(hr != hrSuccess)
  125. return hr;
  126. hr = lpStream->Stat(&sStat, 0);
  127. if(hr != hrSuccess)
  128. return hr;
  129. hr = MAPIAllocateBuffer(sizeof(SPropValue), &~lpPropValue);
  130. if(hr != hrSuccess)
  131. return hr;
  132. hr = MAPIAllocateMore(sStat.cbSize.LowPart, lpPropValue, (void **) &lpPropValue->Value.bin.lpb);
  133. if(hr != hrSuccess)
  134. return hr;
  135. hr = lpStream->Read(lpPropValue->Value.bin.lpb, sStat.cbSize.LowPart, &ulRead);
  136. if(hr != hrSuccess)
  137. return hr;
  138. lpPropValue->Value.bin.cb = ulRead;
  139. *lppPropValue = lpPropValue.release();
  140. return hrSuccess;
  141. }
  142. } /* namespace */