UTF32Encoding.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // UTF32Encoding.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/UTF32Encoding.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Text
  8. // Module: UTF32Encoding
  9. //
  10. // Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/UTF32Encoding.h"
  16. #include "Poco/ByteOrder.h"
  17. #include "Poco/String.h"
  18. namespace Poco {
  19. const char* UTF32Encoding::_names[] =
  20. {
  21. "UTF-32",
  22. "UTF32",
  23. NULL
  24. };
  25. const TextEncoding::CharacterMap UTF32Encoding::_charMap =
  26. {
  27. /* 00 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  28. /* 10 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  29. /* 20 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  30. /* 30 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  31. /* 40 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  32. /* 50 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  33. /* 60 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  34. /* 70 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  35. /* 80 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  36. /* 90 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  37. /* a0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  38. /* b0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  39. /* c0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  40. /* d0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  41. /* e0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  42. /* f0 */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  43. };
  44. UTF32Encoding::UTF32Encoding(ByteOrderType byteOrder)
  45. {
  46. setByteOrder(byteOrder);
  47. }
  48. UTF32Encoding::UTF32Encoding(int byteOrderMark)
  49. {
  50. setByteOrder(byteOrderMark);
  51. }
  52. UTF32Encoding::~UTF32Encoding()
  53. {
  54. }
  55. UTF32Encoding::ByteOrderType UTF32Encoding::getByteOrder() const
  56. {
  57. #if defined(POCO_ARCH_BIG_ENDIAN)
  58. return _flipBytes ? LITTLE_ENDIAN_BYTE_ORDER : BIG_ENDIAN_BYTE_ORDER;
  59. #else
  60. return _flipBytes ? BIG_ENDIAN_BYTE_ORDER : LITTLE_ENDIAN_BYTE_ORDER;
  61. #endif
  62. }
  63. void UTF32Encoding::setByteOrder(ByteOrderType byteOrder)
  64. {
  65. #if defined(POCO_ARCH_BIG_ENDIAN)
  66. _flipBytes = byteOrder == LITTLE_ENDIAN_BYTE_ORDER;
  67. #else
  68. _flipBytes = byteOrder == BIG_ENDIAN_BYTE_ORDER;;
  69. #endif
  70. }
  71. void UTF32Encoding::setByteOrder(int byteOrderMark)
  72. {
  73. _flipBytes = byteOrderMark != 0xFEFF;
  74. }
  75. const char* UTF32Encoding::canonicalName() const
  76. {
  77. return _names[0];
  78. }
  79. bool UTF32Encoding::isA(const std::string& encodingName) const
  80. {
  81. for (const char** name = _names; *name; ++name)
  82. {
  83. if (Poco::icompare(encodingName, *name) == 0)
  84. return true;
  85. }
  86. return false;
  87. }
  88. const TextEncoding::CharacterMap& UTF32Encoding::characterMap() const
  89. {
  90. return _charMap;
  91. }
  92. int UTF32Encoding::convert(const unsigned char* bytes) const
  93. {
  94. UInt32 uc;
  95. unsigned char* p = (unsigned char*) &uc;
  96. *p++ = *bytes++;
  97. *p++ = *bytes++;
  98. *p++ = *bytes++;
  99. *p++ = *bytes++;
  100. if (_flipBytes)
  101. {
  102. ByteOrder::flipBytes(uc);
  103. }
  104. return uc;
  105. }
  106. int UTF32Encoding::convert(int ch, unsigned char* bytes, int length) const
  107. {
  108. if (bytes && length >= 4)
  109. {
  110. UInt32 ch1 = _flipBytes ? ByteOrder::flipBytes((UInt32) ch) : (UInt32) ch;
  111. unsigned char* p = (unsigned char*) &ch1;
  112. *bytes++ = *p++;
  113. *bytes++ = *p++;
  114. *bytes++ = *p++;
  115. *bytes++ = *p++;
  116. }
  117. return 4;
  118. }
  119. int UTF32Encoding::queryConvert(const unsigned char* bytes, int length) const
  120. {
  121. int ret = -4;
  122. if (length >= 4)
  123. {
  124. UInt32 uc;
  125. unsigned char* p = (unsigned char*) &uc;
  126. *p++ = *bytes++;
  127. *p++ = *bytes++;
  128. *p++ = *bytes++;
  129. *p++ = *bytes++;
  130. if (_flipBytes)
  131. ByteOrder::flipBytes(uc);
  132. return uc;
  133. }
  134. return ret;
  135. }
  136. int UTF32Encoding::sequenceLength(const unsigned char* bytes, int length) const
  137. {
  138. return 4;
  139. }
  140. } // namespace Poco