ECStringCompat.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #ifndef ECSTRINGCOMPAT_H
  18. #define ECSTRINGCOMPAT_H
  19. #include <kopano/zcdefs.h>
  20. #include <kopano/kcodes.h>
  21. #include "kcore.hpp"
  22. #include "SOAPUtils.h"
  23. #include <mapidefs.h>
  24. struct soap;
  25. struct user;
  26. struct group;
  27. struct company;
  28. namespace KC {
  29. class convert_context;
  30. /**
  31. * This class is responsible of converting string encodings from
  32. * UTF8 to WTF1252 and vice versa. WTF1252 is a string with characters
  33. * from the windows-1252 codepage encoded as UTF8. So the difference
  34. * with UTF8 is that is a string with true unicode code points.
  35. */
  36. class ECStringCompat _kc_final {
  37. public:
  38. static char *WTF1252_to_WINDOWS1252(soap *lpsoap, const char *szWTF1252, convert_context *lpConverter = NULL);
  39. static char *WTF1252_to_UTF8(struct soap *lpsoap, const char *szWTF1252, convert_context *lpConverter = NULL);
  40. static char *UTF8_to_WTF1252(struct soap *lpsoap, const char *szUTF8, convert_context *lpConverter = NULL);
  41. ECStringCompat(bool fUnicode = false);
  42. ECStringCompat(const ECStringCompat &) = delete;
  43. ~ECStringCompat();
  44. void operator=(const ECStringCompat &) = delete;
  45. /**
  46. * Convert the input data to true UTF8. If ulClientCaps contains
  47. * KOPANO_CAP_UNICODE, the input data is expected to be in UTF8,
  48. * so no conversion is needed. If convert is set to true, the input
  49. * data is expected to be in WTF1252, and the data is converted
  50. * accordingly.
  51. *
  52. * @param[in] szIn The input data.
  53. *
  54. * @return The input data encoded in UTF8.
  55. */
  56. char *to_UTF8(soap *lpsoap, const char *szIn) const;
  57. /**
  58. * Convert the data from UTF8 to either UTF8 ot WTF1252. If culClientCaps
  59. * contains KOPANO_CAP_UNICODE, the output data will not be converted and
  60. * will be in UTF8. Otherwise the data will be encoded in WTF1252.
  61. *
  62. *
  63. * @param[in] szIn The input data in UTF8.
  64. *
  65. * @return The input data encoded in UTF8 or WTF1252 depending on the
  66. * current convert setting.
  67. */
  68. char *from_UTF8(soap *lpsoap, const char *szIn) const;
  69. /**
  70. * Convert and copy the data from UTF8 to either UTF8 or WTF1252. If
  71. * ulClientCaps contains KOPANO_CAP_UNICODE, the output data will not be
  72. * converted and will be in UTF8. Otherwise the data will be encoded in
  73. * WTF1252.
  74. *
  75. * This function is intended to be used when a copy should be made
  76. * regardless of the convert setting. So when the value is going to be
  77. * used as a result and gsoap is going to use it, this function should
  78. * be used unless the original string is static.
  79. *
  80. * @param[in] szIn The input data in UTF8.
  81. *
  82. * @return The input data encoded in UTF8 or WTF1252 depending on the
  83. * current convert setting.
  84. */
  85. char *from_UTF8_cpy(soap *lpsoap, const char *szIn)const ;
  86. /**
  87. * Returns the prop type needed for strings that are returned to the client.
  88. *
  89. * @retval PT_STRING8 when the client does not support unicode.
  90. * @retval PT_UNICODE when the client does support unicoce.
  91. */
  92. ULONG string_prop_type() const;
  93. private:
  94. convert_context *m_lpConverter;
  95. bool m_fUnicode;
  96. };
  97. enum EncodingFixDirection { In, Out };
  98. ECRESULT FixPropEncoding(struct soap *soap, const ECStringCompat &stringCompat, enum EncodingFixDirection type, struct propVal *lpProp, bool bNoTagUpdate = false);
  99. ECRESULT FixRestrictionEncoding(struct soap *soap, const ECStringCompat &stringCompat, enum EncodingFixDirection type, struct restrictTable *lpRestrict);
  100. ECRESULT FixRowSetEncoding(struct soap *soap, const ECStringCompat &stringCompat, enum EncodingFixDirection type, struct rowSet *lpRowSet);
  101. ECRESULT FixUserEncoding(struct soap *soap, const ECStringCompat &stringCompat, enum EncodingFixDirection type, struct user *lpUser);
  102. ECRESULT FixGroupEncoding(struct soap *soap, const ECStringCompat &stringCompat, enum EncodingFixDirection type, struct group *lpGroup);
  103. ECRESULT FixCompanyEncoding(struct soap *soap, const ECStringCompat &stringCompat, enum EncodingFixDirection type, struct company *lpCompany);
  104. ECRESULT FixNotificationsEncoding(struct soap *soap, const ECStringCompat &stringCompat, struct notificationArray *notifications);
  105. // inlines
  106. inline char *ECStringCompat::to_UTF8(soap *lpsoap, const char *szIn) const
  107. {
  108. return m_fUnicode ? (char*)szIn : WTF1252_to_UTF8(lpsoap, szIn, m_lpConverter);
  109. }
  110. inline char *ECStringCompat::from_UTF8(soap *lpsoap, const char *szIn) const
  111. {
  112. return m_fUnicode ? (char*)szIn : UTF8_to_WTF1252(lpsoap, szIn, m_lpConverter);
  113. }
  114. inline char *ECStringCompat::from_UTF8_cpy(soap *lpsoap, const char *szIn) const
  115. {
  116. return m_fUnicode ? s_strcpy(lpsoap, szIn) : UTF8_to_WTF1252(lpsoap, szIn);
  117. }
  118. inline ULONG ECStringCompat::string_prop_type() const
  119. {
  120. return m_fUnicode ? PT_UNICODE : PT_STRING8;
  121. }
  122. } /* namespace */
  123. #endif // ndef ECSTRINGCOMPAT_H