blob: e6a2e5c1d66d673766946c8b15f5cdcb6c987c18 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
* Character Set Handling
* (C) 1999-2007 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/internal/charset.h>
#include <botan/exceptn.h>
#include <botan/internal/loadstor.h>
#include <cctype>
namespace Botan {
namespace {
void append_utf8_for(std::string& s, uint32_t c)
{
if(c >= 0xD800 && c < 0xE000)
throw Decoding_Error("Invalid Unicode character");
if(c <= 0x7F)
{
const uint8_t b0 = static_cast<uint8_t>(c);
s.push_back(static_cast<char>(b0));
}
else if(c <= 0x7FF)
{
const uint8_t b0 = 0xC0 | static_cast<uint8_t>(c >> 6);
const uint8_t b1 = 0x80 | static_cast<uint8_t>(c & 0x3F);
s.push_back(static_cast<char>(b0));
s.push_back(static_cast<char>(b1));
}
else if(c <= 0xFFFF)
{
const uint8_t b0 = 0xE0 | static_cast<uint8_t>(c >> 12);
const uint8_t b1 = 0x80 | static_cast<uint8_t>((c >> 6) & 0x3F);
const uint8_t b2 = 0x80 | static_cast<uint8_t>(c & 0x3F);
s.push_back(static_cast<char>(b0));
s.push_back(static_cast<char>(b1));
s.push_back(static_cast<char>(b2));
}
else if(c <= 0x10FFFF)
{
const uint8_t b0 = 0xF0 | static_cast<uint8_t>(c >> 18);
const uint8_t b1 = 0x80 | static_cast<uint8_t>((c >> 12) & 0x3F);
const uint8_t b2 = 0x80 | static_cast<uint8_t>((c >> 6) & 0x3F);
const uint8_t b3 = 0x80 | static_cast<uint8_t>(c & 0x3F);
s.push_back(static_cast<char>(b0));
s.push_back(static_cast<char>(b1));
s.push_back(static_cast<char>(b2));
s.push_back(static_cast<char>(b3));
}
else
throw Decoding_Error("Invalid Unicode character");
}
}
std::string ucs2_to_utf8(const uint8_t ucs2[], size_t len)
{
if(len % 2 != 0)
throw Decoding_Error("Invalid length for UCS-2 string");
const size_t chars = len / 2;
std::string s;
for(size_t i = 0; i != chars; ++i)
{
const uint16_t c = load_be<uint16_t>(ucs2, i);
append_utf8_for(s, c);
}
return s;
}
std::string ucs4_to_utf8(const uint8_t ucs4[], size_t len)
{
if(len % 4 != 0)
throw Decoding_Error("Invalid length for UCS-4 string");
const size_t chars = len / 4;
std::string s;
for(size_t i = 0; i != chars; ++i)
{
const uint32_t c = load_be<uint32_t>(ucs4, i);
append_utf8_for(s, c);
}
return s;
}
/*
* Convert from ISO 8859-1 to UTF-8
*/
std::string latin1_to_utf8(const std::string& iso8859)
{
std::string utf8;
for(size_t i = 0; i != iso8859.size(); ++i)
{
const uint8_t c = static_cast<uint8_t>(iso8859[i]);
if(c <= 0x7F)
utf8 += static_cast<char>(c);
else
{
utf8 += static_cast<char>((0xC0 | (c >> 6)));
utf8 += static_cast<char>((0x80 | (c & 0x3F)));
}
}
return utf8;
}
}
|