2014-03-23 14:46:58 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Alexey Vatchenko <av@bsdua.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <stdint.h> // IWYU pragma: keep
|
2016-05-04 06:05:47 +08:00
|
|
|
#include <sys/types.h>
|
2014-03-23 14:46:58 +08:00
|
|
|
#include <limits>
|
2016-05-04 06:05:47 +08:00
|
|
|
#include <string>
|
2016-04-21 14:00:54 +08:00
|
|
|
|
|
|
|
#include "utf8.h"
|
2014-03-23 14:46:58 +08:00
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
#define _NXT 0x80
|
|
|
|
#define _SEQ2 0xc0
|
|
|
|
#define _SEQ3 0xe0
|
|
|
|
#define _SEQ4 0xf0
|
2014-03-23 14:46:58 +08:00
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
#define _BOM 0xfeff
|
2014-03-23 14:46:58 +08:00
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
// We can tweak the following typedef to allow us to simulate Windows-style 16 bit wchar's on Unix.
|
2014-03-23 14:46:58 +08:00
|
|
|
typedef wchar_t utf8_wchar_t;
|
2016-10-10 05:36:08 +08:00
|
|
|
#define UTF8_WCHAR_MAX (wchar_t)std::numeric_limits<utf8_wchar_t>::max()
|
2014-03-23 14:46:58 +08:00
|
|
|
|
2016-03-03 03:23:18 +08:00
|
|
|
typedef std::basic_string<utf8_wchar_t> utf8_wstring_t;
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
static size_t utf8_to_wchar_internal(const char *in, size_t insize, utf8_wstring_t *result,
|
|
|
|
int flags);
|
|
|
|
static size_t wchar_to_utf8_internal(const utf8_wchar_t *in, size_t insize, char *out,
|
|
|
|
size_t outsize, int flags);
|
2014-03-23 14:46:58 +08:00
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
static bool safe_copy_wchar_to_utf8_wchar(const wchar_t *in, utf8_wchar_t *out, size_t count) {
|
2014-03-23 14:46:58 +08:00
|
|
|
bool result = true;
|
2016-05-04 06:05:47 +08:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
2014-03-23 14:46:58 +08:00
|
|
|
wchar_t c = in[i];
|
2016-05-04 06:05:47 +08:00
|
|
|
if (c > UTF8_WCHAR_MAX) {
|
2014-03-23 14:46:58 +08:00
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out[i] = c;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
bool wchar_to_utf8_string(const std::wstring &str, std::string *result) {
|
2014-03-23 14:46:58 +08:00
|
|
|
result->clear();
|
|
|
|
const size_t inlen = str.size();
|
2016-05-04 06:05:47 +08:00
|
|
|
if (inlen == 0) {
|
2014-03-23 14:46:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
const wchar_t *input = str.c_str();
|
|
|
|
size_t outlen = wchar_to_utf8(input, inlen, NULL, 0, 0);
|
2016-05-04 06:05:47 +08:00
|
|
|
if (outlen > 0) {
|
2014-03-23 14:46:58 +08:00
|
|
|
char *tmp = new char[outlen];
|
|
|
|
size_t outlen2 = wchar_to_utf8(input, inlen, tmp, outlen, 0);
|
2016-05-04 06:05:47 +08:00
|
|
|
if (outlen2 > 0) {
|
2014-03-23 14:46:58 +08:00
|
|
|
result->assign(tmp, outlen2);
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
delete[] tmp;
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
size_t utf8_to_wchar(const char *in, size_t insize, std::wstring *out, int flags) {
|
|
|
|
if (in == NULL || insize == 0) {
|
2014-03-23 14:46:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t result;
|
2016-05-04 06:05:47 +08:00
|
|
|
if (sizeof(wchar_t) == sizeof(utf8_wchar_t)) {
|
2016-03-03 03:23:18 +08:00
|
|
|
result = utf8_to_wchar_internal(in, insize, reinterpret_cast<utf8_wstring_t *>(out), flags);
|
2016-05-04 06:05:47 +08:00
|
|
|
} else if (out == NULL) {
|
2016-03-03 03:23:18 +08:00
|
|
|
result = utf8_to_wchar_internal(in, insize, NULL, flags);
|
2016-05-04 06:05:47 +08:00
|
|
|
} else {
|
|
|
|
// Allocate a temporary buffer to hold the output, invoke the conversion with the temporary,
|
|
|
|
// and then copy it back.
|
2016-03-03 03:23:18 +08:00
|
|
|
utf8_wstring_t tmp_output;
|
|
|
|
result = utf8_to_wchar_internal(in, insize, &tmp_output, flags);
|
|
|
|
out->insert(out->end(), tmp_output.begin(), tmp_output.end());
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out, size_t outsize, int flags) {
|
|
|
|
if (in == NULL || insize == 0 || (outsize == 0 && out != NULL)) {
|
2014-03-23 14:46:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t result;
|
2016-05-04 06:05:47 +08:00
|
|
|
if (sizeof(wchar_t) == sizeof(utf8_wchar_t)) {
|
|
|
|
result = wchar_to_utf8_internal(reinterpret_cast<const utf8_wchar_t *>(in), insize, out,
|
|
|
|
outsize, flags);
|
|
|
|
} else {
|
|
|
|
// Allocate a temporary buffer to hold the input the std::copy performs the size conversion.
|
|
|
|
// Note: insize may be 0.
|
2014-03-23 14:46:58 +08:00
|
|
|
utf8_wchar_t *tmp_input = new utf8_wchar_t[insize];
|
2016-05-04 06:05:47 +08:00
|
|
|
if (!safe_copy_wchar_to_utf8_wchar(in, tmp_input, insize)) {
|
|
|
|
// Our utf8_wchar_t is UCS-16 and there was an astral character.
|
2014-03-23 14:46:58 +08:00
|
|
|
result = 0;
|
2016-05-04 06:05:47 +08:00
|
|
|
} else {
|
|
|
|
// Invoke the conversion with the temporary, then clean up the input.
|
2014-03-23 14:46:58 +08:00
|
|
|
result = wchar_to_utf8_internal(tmp_input, insize, out, outsize, flags);
|
|
|
|
}
|
|
|
|
delete[] tmp_input;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __wchar_forbitten(utf8_wchar_t sym);
|
|
|
|
static int __utf8_forbitten(unsigned char octet);
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
static int __wchar_forbitten(utf8_wchar_t sym) {
|
|
|
|
// Surrogate pairs.
|
2016-05-04 12:31:32 +08:00
|
|
|
if (sym >= 0xd800 && sym <= 0xdfff) return -1;
|
|
|
|
return 0;
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
static int __utf8_forbitten(unsigned char octet) {
|
|
|
|
switch (octet) {
|
2014-03-23 14:46:58 +08:00
|
|
|
case 0xc0:
|
|
|
|
case 0xc1:
|
|
|
|
case 0xf5:
|
2016-05-04 06:05:47 +08:00
|
|
|
case 0xff: {
|
2016-05-04 12:31:32 +08:00
|
|
|
return -1;
|
2016-05-04 06:05:47 +08:00
|
|
|
}
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 12:31:32 +08:00
|
|
|
return 0;
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
/// This function translates UTF-8 string into UCS-2 or UCS-4 string (all symbols will be in local
|
|
|
|
/// machine byte order). It takes the following arguments:
|
|
|
|
///
|
|
|
|
/// in - input UTF-8 string. It can be null-terminated.
|
|
|
|
/// insize - size of input string in bytes.
|
|
|
|
/// out_string - result buffer for UCS-2/4 string.
|
|
|
|
///
|
|
|
|
/// RETURN VALUES
|
|
|
|
/// The function returns size of result buffer (in wide characters).
|
|
|
|
/// Zero is returned in case of error.
|
|
|
|
///
|
|
|
|
/// CAVEATS
|
|
|
|
/// 1. If UTF-8 string contains zero symbols, they will be translated
|
|
|
|
/// as regular symbols.
|
|
|
|
///
|
|
|
|
/// 2. If UTF8_IGNORE_ERROR or UTF8_SKIP_BOM flag is set, sizes may vary when `out' is NULL and not
|
|
|
|
/// NULL. It's because of special UTF-8 sequences which may result in forbitten (by RFC3629) UNICODE
|
|
|
|
/// characters. So, the caller must check return value every time and not prepare buffer in advance
|
|
|
|
/// (\0 terminate) but after calling this function.
|
|
|
|
static size_t utf8_to_wchar_internal(const char *in, size_t insize, utf8_wstring_t *out_string,
|
|
|
|
int flags) {
|
2014-03-23 14:46:58 +08:00
|
|
|
unsigned char *p, *lim;
|
2016-03-03 03:23:18 +08:00
|
|
|
utf8_wchar_t high;
|
2014-03-23 14:46:58 +08:00
|
|
|
size_t n, total, i, n_bits;
|
|
|
|
|
2016-05-04 12:31:32 +08:00
|
|
|
if (in == NULL || insize == 0) return 0;
|
2016-05-04 06:05:47 +08:00
|
|
|
|
|
|
|
if (out_string != NULL) out_string->clear();
|
2014-03-23 14:46:58 +08:00
|
|
|
|
|
|
|
total = 0;
|
|
|
|
p = (unsigned char *)in;
|
|
|
|
lim = p + insize;
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
for (; p < lim; p += n) {
|
2016-05-04 12:31:32 +08:00
|
|
|
if (__utf8_forbitten(*p) != 0 && (flags & UTF8_IGNORE_ERROR) == 0) return 0;
|
2014-03-23 14:46:58 +08:00
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
// Get number of bytes for one wide character.
|
|
|
|
n = 1; // default: 1 byte. Used when skipping bytes
|
2014-03-23 14:46:58 +08:00
|
|
|
if ((*p & 0x80) == 0)
|
|
|
|
high = (utf8_wchar_t)*p;
|
2016-05-04 06:05:47 +08:00
|
|
|
else if ((*p & 0xe0) == _SEQ2) {
|
2014-03-23 14:46:58 +08:00
|
|
|
n = 2;
|
|
|
|
high = (utf8_wchar_t)(*p & 0x1f);
|
2016-05-04 06:05:47 +08:00
|
|
|
} else if ((*p & 0xf0) == _SEQ3) {
|
2014-03-23 14:46:58 +08:00
|
|
|
n = 3;
|
|
|
|
high = (utf8_wchar_t)(*p & 0x0f);
|
2016-05-04 06:05:47 +08:00
|
|
|
} else if ((*p & 0xf8) == _SEQ4) {
|
2014-03-23 14:46:58 +08:00
|
|
|
n = 4;
|
|
|
|
high = (utf8_wchar_t)(*p & 0x07);
|
2016-05-04 06:05:47 +08:00
|
|
|
} else {
|
2016-05-04 12:31:32 +08:00
|
|
|
if ((flags & UTF8_IGNORE_ERROR) == 0) return 0;
|
2014-03-23 14:46:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
// Does the sequence header tell us truth about length?
|
2016-10-10 05:36:08 +08:00
|
|
|
if ((size_t)(lim - p) <= n - 1) {
|
2016-05-04 12:31:32 +08:00
|
|
|
if ((flags & UTF8_IGNORE_ERROR) == 0) return 0;
|
2014-03-23 14:46:58 +08:00
|
|
|
n = 1;
|
2016-05-04 06:05:47 +08:00
|
|
|
continue; // skip
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
// Validate sequence. All symbols must have higher bits set to 10xxxxxx.
|
|
|
|
if (n > 1) {
|
|
|
|
for (i = 1; i < n; i++) {
|
|
|
|
if ((p[i] & 0xc0) != _NXT) break;
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
2016-05-04 06:05:47 +08:00
|
|
|
if (i != n) {
|
2016-05-04 12:31:32 +08:00
|
|
|
if ((flags & UTF8_IGNORE_ERROR) == 0) return 0;
|
2014-03-23 14:46:58 +08:00
|
|
|
n = 1;
|
2016-05-04 06:05:47 +08:00
|
|
|
continue; // skip
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
total++;
|
2016-05-04 06:05:47 +08:00
|
|
|
if (out_string == NULL) continue;
|
2014-03-23 14:46:58 +08:00
|
|
|
|
|
|
|
uint32_t out_val = 0;
|
|
|
|
n_bits = 0;
|
2016-05-04 06:05:47 +08:00
|
|
|
for (i = 1; i < n; i++) {
|
2014-03-23 14:46:58 +08:00
|
|
|
out_val |= (utf8_wchar_t)(p[n - i] & 0x3f) << n_bits;
|
2016-05-04 06:05:47 +08:00
|
|
|
n_bits += 6; // 6 low bits in every byte
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
|
|
|
out_val |= high << n_bits;
|
|
|
|
|
|
|
|
bool skip = false;
|
2016-05-04 06:05:47 +08:00
|
|
|
if (__wchar_forbitten(out_val) != 0) {
|
|
|
|
if ((flags & UTF8_IGNORE_ERROR) == 0) {
|
|
|
|
return 0; // forbidden character
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
skip = true;
|
2016-05-04 06:05:47 +08:00
|
|
|
} else if (out_val == _BOM && (flags & UTF8_SKIP_BOM) != 0) {
|
2014-03-23 14:46:58 +08:00
|
|
|
skip = true;
|
|
|
|
}
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
if (skip) {
|
2014-03-23 14:46:58 +08:00
|
|
|
total--;
|
2016-10-10 05:36:08 +08:00
|
|
|
} else if (out_val > (uint32_t)UTF8_WCHAR_MAX) {
|
2016-05-04 06:05:47 +08:00
|
|
|
// wchar_t is UCS-2, but the UTF-8 specified an astral character.
|
2014-03-23 14:46:58 +08:00
|
|
|
return 0;
|
2016-05-04 06:05:47 +08:00
|
|
|
} else {
|
2016-03-03 03:23:18 +08:00
|
|
|
out_string->push_back(out_val);
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 12:31:32 +08:00
|
|
|
return total;
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
/// This function translates UCS-2/4 symbols (given in local machine byte order) into UTF-8 string.
|
|
|
|
/// It takes the following arguments:
|
|
|
|
///
|
|
|
|
/// in - input unicode string. It can be null-terminated.
|
|
|
|
/// insize - size of input string in wide characters.
|
|
|
|
/// out - result buffer for utf8 string. If out is NULL, function returns size of result buffer.
|
|
|
|
/// outsize - size of result buffer.
|
|
|
|
///
|
|
|
|
/// RETURN VALUES
|
|
|
|
/// The function returns size of result buffer (in bytes). Zero is returned in case of error.
|
|
|
|
///
|
|
|
|
/// CAVEATS
|
|
|
|
/// If UCS-4 string contains zero symbols, they will be translated as regular symbols.
|
|
|
|
static size_t wchar_to_utf8_internal(const utf8_wchar_t *in, size_t insize, char *out,
|
|
|
|
size_t outsize, int flags) {
|
2014-03-23 14:46:58 +08:00
|
|
|
const utf8_wchar_t *w, *wlim;
|
|
|
|
unsigned char *p, *lim;
|
|
|
|
size_t total, n;
|
|
|
|
|
2016-05-04 12:31:32 +08:00
|
|
|
if (in == NULL || insize == 0 || (outsize == 0 && out != NULL)) return 0;
|
2014-03-23 14:46:58 +08:00
|
|
|
|
|
|
|
w = in;
|
|
|
|
wlim = w + insize;
|
|
|
|
p = (unsigned char *)out;
|
|
|
|
lim = p + outsize;
|
|
|
|
total = 0;
|
2016-05-04 06:05:47 +08:00
|
|
|
for (; w < wlim; w++) {
|
|
|
|
if (__wchar_forbitten(*w) != 0) {
|
2016-05-05 06:19:47 +08:00
|
|
|
if ((flags & UTF8_IGNORE_ERROR) == 0) return 0;
|
|
|
|
continue;
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
if (*w == _BOM && (flags & UTF8_SKIP_BOM) != 0) continue;
|
2014-03-23 14:46:58 +08:00
|
|
|
|
|
|
|
const int32_t w_wide = *w;
|
2016-05-04 06:05:47 +08:00
|
|
|
if (w_wide < 0) {
|
2016-05-04 12:31:32 +08:00
|
|
|
if ((flags & UTF8_IGNORE_ERROR) == 0) return 0;
|
2014-03-23 14:46:58 +08:00
|
|
|
continue;
|
2016-05-05 06:19:47 +08:00
|
|
|
}
|
2016-05-23 10:00:13 +08:00
|
|
|
if (w_wide <= 0x0000007f)
|
|
|
|
n = 1;
|
|
|
|
else if (w_wide <= 0x000007ff)
|
|
|
|
n = 2;
|
|
|
|
else if (w_wide <= 0x0000ffff)
|
|
|
|
n = 3;
|
|
|
|
else if (w_wide <= 0x001fffff)
|
|
|
|
n = 4;
|
|
|
|
else if (w_wide <= 0x03ffffff)
|
|
|
|
n = 5;
|
|
|
|
else
|
|
|
|
n = 6; /// if (w_wide <= 0x7fffffff)
|
2014-03-23 14:46:58 +08:00
|
|
|
|
|
|
|
total += n;
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
if (out == NULL) continue;
|
2016-10-10 05:36:08 +08:00
|
|
|
if (size_t(lim - p) <= n - 1) return 0; // no space left
|
2014-03-23 14:46:58 +08:00
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
// Extract the wchar_t as big-endian. If wchar_t is UCS-16, the first two bytes will be 0.
|
2014-03-23 14:46:58 +08:00
|
|
|
unsigned char oc[4];
|
|
|
|
uint32_t w_tmp = *w;
|
|
|
|
oc[3] = w_tmp & 0xFF;
|
|
|
|
w_tmp >>= 8;
|
|
|
|
oc[2] = w_tmp & 0xFF;
|
|
|
|
w_tmp >>= 8;
|
|
|
|
oc[1] = w_tmp & 0xFF;
|
|
|
|
w_tmp >>= 8;
|
|
|
|
oc[0] = w_tmp & 0xFF;
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
switch (n) {
|
|
|
|
case 1: {
|
2014-03-23 14:46:58 +08:00
|
|
|
p[0] = oc[3];
|
|
|
|
break;
|
2016-05-04 06:05:47 +08:00
|
|
|
}
|
|
|
|
case 2: {
|
2014-03-23 14:46:58 +08:00
|
|
|
p[1] = _NXT | (oc[3] & 0x3f);
|
|
|
|
p[0] = _SEQ2 | (oc[3] >> 6) | ((oc[2] & 0x07) << 2);
|
|
|
|
break;
|
2016-05-04 06:05:47 +08:00
|
|
|
}
|
|
|
|
case 3: {
|
2014-03-23 14:46:58 +08:00
|
|
|
p[2] = _NXT | (oc[3] & 0x3f);
|
|
|
|
p[1] = _NXT | (oc[3] >> 6) | ((oc[2] & 0x0f) << 2);
|
|
|
|
p[0] = _SEQ3 | ((oc[2] & 0xf0) >> 4);
|
|
|
|
break;
|
2016-05-04 06:05:47 +08:00
|
|
|
}
|
|
|
|
case 4: {
|
2014-03-23 14:46:58 +08:00
|
|
|
p[3] = _NXT | (oc[3] & 0x3f);
|
|
|
|
p[2] = _NXT | (oc[3] >> 6) | ((oc[2] & 0x0f) << 2);
|
2016-05-04 06:05:47 +08:00
|
|
|
p[1] = _NXT | ((oc[2] & 0xf0) >> 4) | ((oc[1] & 0x03) << 4);
|
2014-03-23 14:46:58 +08:00
|
|
|
p[0] = _SEQ4 | ((oc[1] & 0x1f) >> 2);
|
|
|
|
break;
|
2016-05-04 06:05:47 +08:00
|
|
|
}
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 06:05:47 +08:00
|
|
|
// NOTE: do not check here for forbitten UTF-8 characters. They cannot appear here because
|
|
|
|
// we do proper convertion.
|
2014-03-23 14:46:58 +08:00
|
|
|
p += n;
|
|
|
|
}
|
|
|
|
|
2016-05-04 12:31:32 +08:00
|
|
|
return total;
|
2014-03-23 14:46:58 +08:00
|
|
|
}
|