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