8#include <botan/internal/charset.h>
10#include <botan/exceptn.h>
11#include <botan/internal/loadstor.h>
18void append_utf8_for(std::string& s, uint32_t c) {
19 if(c >= 0xD800 && c < 0xE000) {
20 throw Decoding_Error(
"Invalid Unicode character");
24 const uint8_t b0 =
static_cast<uint8_t
>(c);
25 s.push_back(
static_cast<char>(b0));
26 }
else if(c <= 0x7FF) {
27 const uint8_t b0 = 0xC0 |
static_cast<uint8_t
>(c >> 6);
28 const uint8_t b1 = 0x80 |
static_cast<uint8_t
>(c & 0x3F);
29 s.push_back(
static_cast<char>(b0));
30 s.push_back(
static_cast<char>(b1));
31 }
else if(c <= 0xFFFF) {
32 const uint8_t b0 = 0xE0 |
static_cast<uint8_t
>(c >> 12);
33 const uint8_t b1 = 0x80 |
static_cast<uint8_t
>((c >> 6) & 0x3F);
34 const uint8_t b2 = 0x80 |
static_cast<uint8_t
>(c & 0x3F);
35 s.push_back(
static_cast<char>(b0));
36 s.push_back(
static_cast<char>(b1));
37 s.push_back(
static_cast<char>(b2));
38 }
else if(c <= 0x10FFFF) {
39 const uint8_t b0 = 0xF0 |
static_cast<uint8_t
>(c >> 18);
40 const uint8_t b1 = 0x80 |
static_cast<uint8_t
>((c >> 12) & 0x3F);
41 const uint8_t b2 = 0x80 |
static_cast<uint8_t
>((c >> 6) & 0x3F);
42 const uint8_t b3 = 0x80 |
static_cast<uint8_t
>(c & 0x3F);
43 s.push_back(
static_cast<char>(b0));
44 s.push_back(
static_cast<char>(b1));
45 s.push_back(
static_cast<char>(b2));
46 s.push_back(
static_cast<char>(b3));
48 throw Decoding_Error(
"Invalid Unicode character");
59 const size_t chars = len / 2;
62 for(
size_t i = 0; i != chars; ++i) {
64 append_utf8_for(s, c);
75 const size_t chars = len / 4;
78 for(
size_t i = 0; i != chars; ++i) {
80 append_utf8_for(s, c);
91 for(
size_t i = 0; i != len; ++i) {
92 const uint32_t c =
static_cast<uint8_t
>(chars[i]);
93 append_utf8_for(s, c);
99 std::ostringstream oss;
105 }
else if(c ==
'\n') {
107 }
else if(c ==
'\r') {
109 }
else if(
static_cast<unsigned char>(c) >= 128) {
110 unsigned char z =
static_cast<unsigned char>(c);
111 oss <<
"\\x" << std::hex << std::uppercase << static_cast<int>(z);
std::string format_char_for_display(char c)
std::string ucs2_to_utf8(const uint8_t ucs2[], size_t len)
std::string latin1_to_utf8(const uint8_t chars[], size_t len)
std::string ucs4_to_utf8(const uint8_t ucs4[], size_t len)
constexpr auto load_be(ParamTs &&... params)