14#ifndef OPENVPN_COMMON_UNICODE_H
15#define OPENVPN_COMMON_UNICODE_H
43 const size_t max_len_flags = 0)
45 const size_t max_len = max_len_flags & ((size_t)
UTF8_NO_CTRL - 1);
46 size_t unicode_len = 0;
49 const unsigned char c = *source;
53 if ((
size_t)length > size)
68 if (max_len && unicode_len > max_len)
74template <
typename STRING>
88template <
typename STRING>
91 const size_t size =
str.length();
97 if (pos + len > size || !
isLegalUTF8((
const unsigned char *)&
str[pos], len))
111template <
typename STRING>
116 str =
str.substr(0, max_len);
128template <
typename STRING>
132 const size_t size =
str.length();
133 const size_t max_len = max_len_flags & ((size_t)
UTF8_FILTER - 1);
136 ret.reserve(std::min(
str.length(), max_len) + 3);
139 if (!max_len || upos < max_len)
141 unsigned char c =
str[pos];
143 if (pos + len <= size
144 && c >= 0x20 && c != 0x7F
148 ret.append(
str, pos, len);
153 if (c ==
'\r' || c ==
'\n' || c ==
'\t')
178template <
typename STRING>
181 const size_t size =
str.length();
203 throw unicode_src_overflow();
205 throw unicode_dest_overflow();
207 throw unicode_malformed();
212template <
typename STRING>
215 std::unique_ptr<UTF16[]> utf16_dest(
new UTF16[
str.length()]);
217 UTF16 *dest = utf16_dest.get();
226 for (
const UTF16 *s = utf16_dest.get(); s < dest; ++s)
228 *d++ =
static_cast<UTF8>(*s & 0xFF);
229 *d++ =
static_cast<UTF8>((*s >> 8) & 0xFF);
248 std::string
str(
const char *malformed)
251 return std::string((
char *)
data,
len);
258 :
str((unsigned char *)str_arg.c_str()),
259 size(str_arg.length())
272 c.
len = std::min(len, (
unsigned int)
sizeof(c.
data));
static Ptr Create(ArgsT &&...args)
Creates a new instance of RcEnable with the given arguments.
const unsigned char * str
UTF8Iterator(const std::string &str_arg)
#define OPENVPN_SIMPLE_EXCEPTION(C)
constexpr BufferFlags ARRAY(1u<< 3)
if enabled, use as array
STRING utf8_printable(const STRING &str, size_t max_len_flags)
void conversion_result_throw(const ConversionResult res)
const char trailingBytesForUTF8[256]
bool is_valid_utf8_uchar_buf(const unsigned char *source, size_t size, const size_t max_len_flags=0)
bool is_valid_utf8(const STRING &str, const size_t max_len_flags=0)
ConversionResult ConvertUTF8toUTF16(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
int utf8_index(STRING &str, size_t &index)
size_t utf8_length(const STRING &str)
void utf8_truncate(STRING &str, size_t max_len)
BufferPtr string_to_utf16(const STRING &str)
bool isLegalUTF8(const UTF8 *source, int length)
std::string str(const char *malformed)
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'