3 #include "ACEXML/common/Encoding.h"
4 #include "ace/OS_NS_string.h"
6 const ACEXML_Char
* ACEXML_Encoding::encoding_names_
[8] = {
9 ACE_TEXT ("UCS-4_2143"),
10 ACE_TEXT ("UCS-4_3412"),
14 ACE_TEXT ("Unsupported Encoding")
17 const ACEXML_UTF8
ACEXML_Encoding::byte_order_mark_
[][4] = {
18 { '\x00', '\x00', '\xFE', '\xFF' }, // UCS-4, big-endian (1234 order)
19 { '\xFF', '\xFE', '\x00', '\x00' }, // UCS-4, little-endian (4321 order)
20 { '\x00', '\x00', '\xFF', '\xFE' }, // UCS-4, unusual octet order (2143)
21 { '\xFE', '\xFF', '\x00', '\x00' }, // UCS-4, unusual octet order (3412)
22 { '\xFE', '\xFF', '\xFF', '\xFF' }, // UTF-16, big-endian (3 & 4 != 0)
23 { '\xFF', '\xFE', '\xFF', '\xFF' }, // UTF-16, little-endian ( 3 & 4 != 0)
24 { '\xEF', '\xBB', '\xBF', '\xFF' } // UTF-8
27 const ACEXML_UTF8
ACEXML_Encoding::magic_values_
[][4] = {
28 { '\x00', '\x00', '\x00', '\x3c' }, //
29 { '\x3c', '\x00', '\x00', '\x00' }, // UCS-4 and variants
30 { '\x00', '\x00', '\x3c', '\x00' }, //
31 { '\x00', '\x3c', '\x00', '\x00' }, //
32 { '\x00', '\x3c', '\x00', '\x3f' }, // UTF-16BE
33 { '\x3c', '\x00', '\x3f', '\x00' }, // UTF-16LE
34 { '\x3c', '\x3f', '\x78', '\x6d' }, // UTF-8
38 ACEXML_Encoding::get_encoding (const char* input
)
40 if ((ACE_OS::memcmp (&ACEXML_Encoding::byte_order_mark_
[ACEXML_Encoding::UTF16BE
][0], input
, 2) == 0)
41 && (input
[2] != 0 || input
[3] != 0)) // 3 & 4 should not be both zero
42 return ACEXML_Encoding::encoding_names_
[ACEXML_Encoding::UTF16BE
];
43 else if ((ACE_OS::memcmp (&ACEXML_Encoding::byte_order_mark_
[ACEXML_Encoding::UTF16LE
][0], input
, 2) == 0)
44 && (input
[2] != 0 || input
[3] != 0)) // 3 & 4 should not be both zero
45 return ACEXML_Encoding::encoding_names_
[ACEXML_Encoding::UTF16LE
];
46 else if (ACE_OS::memcmp (&ACEXML_Encoding::byte_order_mark_
[ACEXML_Encoding::UTF8
][0], input
, 3) == 0)
47 return ACEXML_Encoding::encoding_names_
[ACEXML_Encoding::UTF8
];
48 else if (ACE_OS::memcmp (&ACEXML_Encoding::magic_values_
[ACEXML_Encoding::UTF16BE
][0], input
, 4) == 0)
49 return ACEXML_Encoding::encoding_names_
[ACEXML_Encoding::UTF16BE
];
50 else if (ACE_OS::memcmp (&ACEXML_Encoding::magic_values_
[ACEXML_Encoding::UTF16LE
][0], input
, 4) == 0)
51 return ACEXML_Encoding::encoding_names_
[ACEXML_Encoding::UTF16LE
];
52 else if (ACE_OS::memcmp (&ACEXML_Encoding::magic_values_
[ACEXML_Encoding::UTF8
][0], input
, 4) == 0)
53 return ACEXML_Encoding::encoding_names_
[ACEXML_Encoding::UTF8
];
56 // ACE_ERROR ((LM_ERROR, "Unknown encoding. Assuming UTF-8\n"));
57 return ACEXML_Encoding::encoding_names_
[ACEXML_Encoding::UTF8
];