archiver-common.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <kopano/archiver-common.h>
  19. #include <kopano/stringutil.h>
  20. namespace KC {
  21. bool entryid_t::operator==(const entryid_t &other) const
  22. {
  23. return getUnwrapped().m_vEntryId == other.getUnwrapped().m_vEntryId;
  24. }
  25. bool entryid_t::operator<(const entryid_t &other) const
  26. {
  27. return getUnwrapped().m_vEntryId < other.getUnwrapped().m_vEntryId;
  28. }
  29. bool entryid_t::operator>(const entryid_t &other) const
  30. {
  31. return getUnwrapped().m_vEntryId > other.getUnwrapped().m_vEntryId;
  32. }
  33. bool entryid_t::wrap(const std::string &strPath)
  34. {
  35. if (!kc_istarts_with(strPath, "file://") &&
  36. !kc_istarts_with(strPath, "http://") &&
  37. !kc_istarts_with(strPath, "https://"))
  38. return false;
  39. m_vEntryId.insert(m_vEntryId.begin(), (LPBYTE)strPath.c_str(), (LPBYTE)strPath.c_str() + strPath.size() + 1); // Include NULL terminator
  40. return true;
  41. }
  42. bool entryid_t::unwrap(std::string *lpstrPath)
  43. {
  44. if (!isWrapped())
  45. return false;
  46. auto iter = std::find(m_vEntryId.begin(), m_vEntryId.end(), 0);
  47. if (iter == m_vEntryId.end())
  48. return false;
  49. if (lpstrPath)
  50. lpstrPath->assign((char*)&m_vEntryId.front(), iter - m_vEntryId.cbegin());
  51. m_vEntryId.erase(m_vEntryId.begin(), ++iter);
  52. return true;
  53. }
  54. bool entryid_t::isWrapped() const
  55. {
  56. // ba::istarts_with doesn't work well on unsigned char. So we use a temporary instead.
  57. const std::string strEntryId((char*)&m_vEntryId.front(), m_vEntryId.size());
  58. return kc_istarts_with(strEntryId, "file://") ||
  59. kc_istarts_with(strEntryId, "http://") ||
  60. kc_istarts_with(strEntryId, "https://");
  61. }
  62. entryid_t entryid_t::getUnwrapped() const
  63. {
  64. if (!isWrapped())
  65. return *this;
  66. entryid_t tmp(*this);
  67. tmp.unwrap(NULL);
  68. return tmp;
  69. }
  70. int abentryid_t::compare(const abentryid_t &other) const
  71. {
  72. if (size() < other.size())
  73. return -1;
  74. if (size() > other.size())
  75. return 1;
  76. if (size() <= 32) {
  77. // Too small, just compare the whole thing
  78. return memcmp(LPBYTE(*this), LPBYTE(other), size());
  79. }
  80. // compare the part before the legacy user id.
  81. int res = memcmp(LPBYTE(*this), LPBYTE(other), 28);
  82. if (res != 0)
  83. return res;
  84. // compare the part after the legacy user id.
  85. return memcmp(LPBYTE(*this) + 32, LPBYTE(other) + 32, size() - 32);
  86. }
  87. eResult MAPIErrorToArchiveError(HRESULT hr)
  88. {
  89. switch (hr) {
  90. case hrSuccess: return Success;
  91. case MAPI_E_NOT_ENOUGH_MEMORY: return OutOfMemory;
  92. case MAPI_E_INVALID_PARAMETER: return InvalidParameter;
  93. case MAPI_W_PARTIAL_COMPLETION: return PartialCompletion;
  94. default: return Failure;
  95. }
  96. }
  97. const char* ArchiveResultString(eResult result)
  98. {
  99. const char* retval = "Unknown result";
  100. switch (result)
  101. {
  102. case Success:
  103. retval = "Success";
  104. break;
  105. case OutOfMemory:
  106. retval = "Out of memory";
  107. break;
  108. case InvalidParameter:
  109. retval = "Invalid parameter";
  110. break;
  111. case PartialCompletion:
  112. retval = "Partial completion";
  113. break;
  114. case Failure:
  115. retval = "Failure";
  116. break;
  117. default:
  118. /* do nothing */;
  119. }
  120. return retval;
  121. }
  122. } /* namespace */