2010-06-03 22:32:25 +02:00
|
|
|
// Copyright (c) 2010, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and 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.
|
|
|
|
|
|
|
|
/// @file charset_conv.cpp
|
|
|
|
/// @brief Wrapper for libiconv to present a more C++-friendly API
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
2010-06-14 21:26:27 +02:00
|
|
|
#include <errno.h>
|
2012-09-25 02:09:42 +02:00
|
|
|
#include <cstdint>
|
2010-06-14 20:02:58 +02:00
|
|
|
|
2011-07-15 06:04:34 +02:00
|
|
|
#include <cassert>
|
2014-07-05 17:25:03 +02:00
|
|
|
#include <boost/range/algorithm.hpp>
|
2010-06-03 22:32:25 +02:00
|
|
|
|
|
|
|
#include <libaegisub/charset_conv.h>
|
|
|
|
#include <iconv.h>
|
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
#include "charset_6937.h"
|
|
|
|
|
2010-06-03 22:32:25 +02:00
|
|
|
// Check if we can use advanced fallback capabilities added in GNU's iconv
|
|
|
|
// implementation
|
|
|
|
#if !defined(_LIBICONV_VERSION) || _LIBICONV_VERSION < 0x010A || defined(LIBICONV_PLUG)
|
|
|
|
#define ICONV_POSIX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const iconv_t iconv_invalid = (iconv_t)-1;
|
|
|
|
static const size_t iconv_failed = (size_t)-1;
|
|
|
|
|
|
|
|
namespace {
|
2014-05-30 15:16:50 +02:00
|
|
|
using namespace agi::charset;
|
2012-03-29 21:04:49 +02:00
|
|
|
|
2014-05-30 15:16:50 +02:00
|
|
|
Converter *get_converter(bool subst, const char *src, const char *dst);
|
2010-06-03 22:32:25 +02:00
|
|
|
|
|
|
|
/// @brief Map a user-friendly encoding name to the real encoding name
|
2014-07-05 17:25:03 +02:00
|
|
|
const char *get_real_encoding_name(const char *name) {
|
2014-12-14 17:19:22 +01:00
|
|
|
struct pair { const char *pretty; const char *real; };
|
|
|
|
static pair pretty_names[] = {
|
2014-07-05 17:25:03 +02:00
|
|
|
# define ADD(pretty, real) pair{pretty, real},
|
2012-03-29 21:04:49 +02:00
|
|
|
# include <libaegisub/charsets.def>
|
|
|
|
# undef ADD
|
2014-12-14 17:19:22 +01:00
|
|
|
};
|
2014-07-05 17:25:03 +02:00
|
|
|
|
2014-12-14 17:19:22 +01:00
|
|
|
static bool init = false;
|
|
|
|
if (!init) {
|
|
|
|
init = true;
|
|
|
|
boost::sort(pretty_names, [](pair a, pair b) {
|
|
|
|
return strcmp(a.pretty, b.pretty) < 0;
|
|
|
|
});
|
|
|
|
}
|
2014-07-05 17:25:03 +02:00
|
|
|
|
2014-12-14 17:19:22 +01:00
|
|
|
auto enc = boost::lower_bound(pretty_names, name, [](pair a, const char *b) {
|
2014-07-05 17:25:03 +02:00
|
|
|
return strcmp(a.pretty, b) < 0;
|
2014-12-14 17:19:22 +01:00
|
|
|
});
|
2014-07-05 17:25:03 +02:00
|
|
|
|
2014-12-14 17:19:22 +01:00
|
|
|
if (enc != std::end(pretty_names) && strcmp(enc->pretty, name) == 0)
|
|
|
|
return enc->real;
|
|
|
|
return name;
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
|
|
|
|
2014-05-30 15:16:50 +02:00
|
|
|
size_t get_bom_size(Iconv& cd) {
|
2012-03-29 21:04:49 +02:00
|
|
|
// Most (but not all) iconv implementations automatically insert a BOM
|
|
|
|
// at the beginning of text converted to UTF-8, UTF-16 and UTF-32, but
|
|
|
|
// we usually don't want this, as some of the wxString using code
|
|
|
|
// assumes there is no BOM (as the exact encoding is known externally)
|
|
|
|
// As such, when doing conversions we will strip the BOM if it exists,
|
|
|
|
// then manually add it when writing files
|
|
|
|
|
|
|
|
char buff[8];
|
|
|
|
const char* src = "";
|
|
|
|
char *dst = buff;
|
|
|
|
size_t srcLen = 1;
|
|
|
|
size_t dstLen = 8;
|
2010-09-09 08:47:50 +02:00
|
|
|
|
2014-05-30 15:16:50 +02:00
|
|
|
size_t res = cd(&src, &srcLen, &dst, &dstLen);
|
2012-03-29 21:04:49 +02:00
|
|
|
assert(res != iconv_failed);
|
|
|
|
assert(srcLen == 0);
|
2010-09-09 08:47:50 +02:00
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
size_t size = 0;
|
|
|
|
for (src = buff; src < dst; ++src) {
|
|
|
|
if (*src) ++size;
|
2010-09-09 08:47:50 +02:00
|
|
|
}
|
2012-03-29 21:04:49 +02:00
|
|
|
if (size) {
|
|
|
|
// If there is a BOM, it will always be at least as big as the NUL
|
|
|
|
size = std::max(size, (8 - dstLen) / 2);
|
2010-09-09 08:47:50 +02:00
|
|
|
}
|
2012-03-29 21:04:49 +02:00
|
|
|
return size;
|
2010-09-09 08:47:50 +02:00
|
|
|
}
|
2012-03-29 21:04:49 +02:00
|
|
|
|
2014-05-30 15:16:50 +02:00
|
|
|
void eat_bom(Iconv& cd, size_t bomSize, const char** inbuf, size_t* inbytesleft, char** outbuf, size_t* outbytesleft) {
|
2012-03-29 21:04:49 +02:00
|
|
|
// If this encoding has a forced BOM (i.e. it's UTF-16 or UTF-32 without
|
|
|
|
// a specified byte order), skip over it
|
|
|
|
if (bomSize > 0 && inbytesleft && *inbytesleft) {
|
|
|
|
// libiconv marks the bom as written after writing the first
|
|
|
|
// character after the bom rather than when it writes the bom, so
|
|
|
|
// convert at least one extra character
|
|
|
|
char bom[8];
|
|
|
|
char *dst = bom;
|
|
|
|
size_t dstSize = std::min((size_t)8, bomSize + *outbytesleft);
|
|
|
|
const char *src = *inbuf;
|
|
|
|
size_t srcSize = *inbytesleft;
|
2014-05-30 15:16:50 +02:00
|
|
|
cd(&src, &srcSize, &dst, &dstSize);
|
2012-03-29 21:04:49 +02:00
|
|
|
}
|
2010-09-09 08:47:50 +02:00
|
|
|
}
|
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
// Calculate the size of NUL in the given character set
|
2014-05-30 15:16:50 +02:00
|
|
|
size_t nul_size(const char *encoding) {
|
2012-03-29 21:04:49 +02:00
|
|
|
// We need a character set to convert from with a known encoding of NUL
|
|
|
|
// UTF-8 seems like the obvious choice
|
2014-05-30 15:16:50 +02:00
|
|
|
std::unique_ptr<Converter> cd(get_converter(false, "UTF-8", encoding));
|
2012-03-29 21:04:49 +02:00
|
|
|
|
|
|
|
char dbuff[4];
|
|
|
|
char sbuff[] = "";
|
|
|
|
char* dst = dbuff;
|
|
|
|
const char* src = sbuff;
|
|
|
|
size_t dstLen = sizeof(dbuff);
|
|
|
|
size_t srcLen = 1;
|
2010-09-09 08:47:50 +02:00
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
size_t ret = cd->Convert(&src, &srcLen, &dst, &dstLen);
|
|
|
|
assert(ret != iconv_failed);
|
|
|
|
assert(dst - dbuff > 0);
|
2010-09-09 08:47:50 +02:00
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
return dst - dbuff;
|
2010-09-09 08:47:50 +02:00
|
|
|
}
|
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
#ifdef ICONV_POSIX
|
2014-05-30 15:16:50 +02:00
|
|
|
class ConverterImpl final : public Converter {
|
2012-03-29 21:04:49 +02:00
|
|
|
size_t bomSize;
|
2014-05-30 15:16:50 +02:00
|
|
|
Iconv cd;
|
2012-03-29 21:04:49 +02:00
|
|
|
public:
|
|
|
|
// subst is not used here because POSIX doesn't let you disable substitution
|
|
|
|
ConverterImpl(bool, const char* sourceEncoding, const char* destEncoding)
|
|
|
|
{
|
|
|
|
const char *dstEnc = get_real_encoding_name(destEncoding);
|
2014-05-30 15:16:50 +02:00
|
|
|
cd = Iconv("utf-8", dstEnc);
|
2010-07-06 21:23:10 +02:00
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
bomSize = get_bom_size(cd);
|
2014-05-30 15:16:50 +02:00
|
|
|
cd = Iconv(get_real_encoding_name(sourceEncoding), dstEnc);
|
2010-07-06 21:23:10 +02:00
|
|
|
}
|
2014-05-30 15:16:50 +02:00
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
size_t Convert(const char** inbuf, size_t* inbytesleft, char** outbuf, size_t* outbytesleft) {
|
|
|
|
eat_bom(cd, bomSize, inbuf, inbytesleft, outbuf, outbytesleft);
|
2010-07-06 21:23:10 +02:00
|
|
|
|
2014-05-30 15:16:50 +02:00
|
|
|
size_t res = cd(inbuf, inbytesleft, outbuf, outbytesleft);
|
2010-09-09 08:47:50 +02:00
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
// This loop never does anything useful with a POSIX-compliant iconv
|
|
|
|
// implementation, but those don't seem to actually exist
|
|
|
|
while (res == iconv_failed && errno != E2BIG) {
|
|
|
|
++*inbuf;
|
|
|
|
--*inbytesleft;
|
2014-05-30 15:16:50 +02:00
|
|
|
res = cd(inbuf, inbytesleft, outbuf, outbytesleft);
|
2012-03-29 21:04:49 +02:00
|
|
|
}
|
2010-07-06 21:23:10 +02:00
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
return res;
|
2010-07-06 21:23:10 +02:00
|
|
|
}
|
2012-03-29 21:04:49 +02:00
|
|
|
};
|
2010-06-03 22:32:25 +02:00
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
#else
|
2010-06-03 22:32:25 +02:00
|
|
|
|
2014-05-30 15:16:50 +02:00
|
|
|
class ConverterImpl final : public iconv_fallbacks, public Converter {
|
2012-03-29 21:04:49 +02:00
|
|
|
size_t bomSize;
|
|
|
|
char invalidRep[8];
|
|
|
|
size_t invalidRepSize;
|
2014-05-30 15:16:50 +02:00
|
|
|
Iconv cd;
|
2012-03-29 21:04:49 +02:00
|
|
|
static void fallback(
|
|
|
|
unsigned int code,
|
|
|
|
void (*callback) (const char *buf, size_t buflen, void* callback_arg),
|
|
|
|
void *callback_arg,
|
|
|
|
void *convPtr)
|
|
|
|
{
|
|
|
|
// At some point in the future, this should probably switch to a real mapping
|
|
|
|
// For now, there's just three cases: BOM to nothing, '\' to itself
|
|
|
|
// (for Shift-JIS, which does not have \) and everything else to '?'
|
|
|
|
if (code == 0xFEFF) return;
|
|
|
|
if (code == 0x5C) callback("\\", 1, callback_arg);
|
|
|
|
else {
|
|
|
|
ConverterImpl *self = static_cast<ConverterImpl *>(convPtr);
|
|
|
|
callback(self->invalidRep, self->invalidRepSize, callback_arg);
|
|
|
|
}
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
2014-05-30 15:16:50 +02:00
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
public:
|
|
|
|
ConverterImpl(bool subst, const char* sourceEncoding, const char* destEncoding)
|
|
|
|
{
|
|
|
|
const char *dstEnc = get_real_encoding_name(destEncoding);
|
2014-05-30 15:16:50 +02:00
|
|
|
cd = Iconv("utf-8", dstEnc);
|
2012-03-29 21:04:49 +02:00
|
|
|
|
|
|
|
bomSize = get_bom_size(cd);
|
|
|
|
|
|
|
|
// Get fallback character
|
|
|
|
const char sbuff[] = "?";
|
|
|
|
const char *src = sbuff;
|
|
|
|
char *dst = invalidRep;
|
|
|
|
size_t dstLen = 4;
|
|
|
|
size_t srcLen = 1;
|
|
|
|
|
|
|
|
size_t res = Convert(&src, &srcLen, &dst, &dstLen);
|
|
|
|
assert(res != iconv_failed);
|
|
|
|
assert(srcLen == 0);
|
|
|
|
|
|
|
|
invalidRepSize = 4 - dstLen;
|
|
|
|
|
2014-05-30 15:16:50 +02:00
|
|
|
cd = Iconv(get_real_encoding_name(sourceEncoding), dstEnc);
|
2012-03-29 21:04:49 +02:00
|
|
|
|
|
|
|
if (subst) {
|
|
|
|
data = this;
|
2012-11-13 15:07:39 +01:00
|
|
|
mb_to_uc_fallback = nullptr;
|
|
|
|
mb_to_wc_fallback = nullptr;
|
2012-03-29 21:04:49 +02:00
|
|
|
uc_to_mb_fallback = fallback;
|
2012-11-13 15:07:39 +01:00
|
|
|
wc_to_mb_fallback = nullptr;
|
2012-03-29 21:04:49 +02:00
|
|
|
|
|
|
|
int transliterate = 1;
|
|
|
|
iconvctl(cd, ICONV_SET_TRANSLITERATE, &transliterate);
|
2012-03-31 02:28:55 +02:00
|
|
|
iconvctl(cd, ICONV_SET_FALLBACKS, static_cast<iconv_fallbacks*>(this));
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-30 15:16:50 +02:00
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
size_t Convert(const char** inbuf, size_t* inbytesleft, char** outbuf, size_t* outbytesleft) override {
|
2012-03-29 21:04:49 +02:00
|
|
|
eat_bom(cd, bomSize, inbuf, inbytesleft, outbuf, outbytesleft);
|
2014-05-30 15:16:50 +02:00
|
|
|
size_t res = cd(inbuf, inbytesleft, outbuf, outbytesleft);
|
2012-03-29 21:04:49 +02:00
|
|
|
|
|
|
|
if (res == iconv_failed && errno == E2BIG && *outbytesleft == 0) {
|
|
|
|
// libiconv checks if there are any bytes left in the output buffer
|
|
|
|
// before checking if the conversion would actually write any
|
|
|
|
// characters to the output buffer, resulting in occasional invalid
|
|
|
|
// E2BIG false positives
|
|
|
|
char buff[8];
|
|
|
|
size_t buffsize = 8;
|
|
|
|
char* out = buff;
|
|
|
|
const char* in = *inbuf;
|
|
|
|
size_t insize = *inbytesleft;
|
|
|
|
|
2014-05-30 15:16:50 +02:00
|
|
|
res = cd(&in, &insize, &out, &buffsize);
|
2012-03-29 21:04:49 +02:00
|
|
|
// If no bytes of the output buffer were used, the original
|
|
|
|
// conversion may have been successful
|
|
|
|
if (buffsize != 8) {
|
|
|
|
errno = E2BIG;
|
|
|
|
res = iconv_failed;
|
|
|
|
}
|
|
|
|
}
|
2010-06-03 22:32:25 +02:00
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
2010-09-09 08:47:50 +02:00
|
|
|
#endif
|
2010-06-03 22:32:25 +02:00
|
|
|
|
2014-05-30 15:16:50 +02:00
|
|
|
Converter *get_converter(bool subst, const char *src, const char *dst) {
|
2012-03-29 21:04:49 +02:00
|
|
|
try {
|
|
|
|
return new ConverterImpl(subst, src, dst);
|
|
|
|
}
|
2014-05-30 15:16:50 +02:00
|
|
|
catch (UnsupportedConversion const&) {
|
2012-03-29 21:04:49 +02:00
|
|
|
if (strcmp(dst, "ISO-6937-2"))
|
|
|
|
throw;
|
2014-05-30 15:16:50 +02:00
|
|
|
return new Converter6937(subst, src);
|
2012-03-29 21:04:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace {
|
2010-06-03 22:32:25 +02:00
|
|
|
|
2012-03-29 21:04:49 +02:00
|
|
|
namespace agi { namespace charset {
|
2014-05-30 15:16:50 +02:00
|
|
|
Iconv::Iconv() : cd(iconv_invalid) { }
|
|
|
|
|
|
|
|
Iconv::Iconv(const char *source, const char *dest)
|
|
|
|
: cd(iconv_open(dest, source))
|
|
|
|
{
|
|
|
|
if (cd == iconv_invalid)
|
|
|
|
throw UnsupportedConversion(std::string("Cannot convert from ") + source + " to " + dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iconv::~Iconv() {
|
|
|
|
if (cd != iconv_invalid) iconv_close(cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Iconv::operator()(const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) {
|
2020-03-20 11:30:08 +01:00
|
|
|
struct InbufConstCast{
|
|
|
|
const char** p;
|
|
|
|
InbufConstCast(const char** p) : p(p) {}
|
|
|
|
operator const char** () const { return p; }
|
|
|
|
operator char** () const { return const_cast<char**>(p); }
|
|
|
|
} inbuf_const_cast(inbuf);
|
|
|
|
return iconv(cd, inbuf_const_cast, inbytesleft, outbuf, outbytesleft);
|
2014-05-30 15:16:50 +02:00
|
|
|
}
|
2010-06-03 22:32:25 +02:00
|
|
|
|
|
|
|
IconvWrapper::IconvWrapper(const char* sourceEncoding, const char* destEncoding, bool enableSubst)
|
2013-12-12 01:29:48 +01:00
|
|
|
: conv(get_converter(enableSubst, sourceEncoding, destEncoding))
|
2010-06-03 22:32:25 +02:00
|
|
|
{
|
2010-09-09 08:47:50 +02:00
|
|
|
// These need to be set only after we verify that the source and dest
|
2010-06-03 22:32:25 +02:00
|
|
|
// charsets are valid
|
2012-03-29 21:04:49 +02:00
|
|
|
toNulLen = nul_size(destEncoding);
|
|
|
|
fromNulLen = nul_size(sourceEncoding);
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
2014-05-30 15:16:50 +02:00
|
|
|
|
|
|
|
IconvWrapper::~IconvWrapper() { }
|
2010-06-03 22:32:25 +02:00
|
|
|
|
2014-03-21 21:12:56 +01:00
|
|
|
std::string IconvWrapper::Convert(const char *source, size_t len) {
|
2010-06-03 22:32:25 +02:00
|
|
|
std::string dest;
|
2014-03-21 21:12:56 +01:00
|
|
|
Convert(source, len, dest);
|
2010-06-03 22:32:25 +02:00
|
|
|
return dest;
|
|
|
|
}
|
2014-05-30 15:16:50 +02:00
|
|
|
|
2014-03-21 21:12:56 +01:00
|
|
|
void IconvWrapper::Convert(const char *src, size_t srcLen, std::string &dest) {
|
2010-07-07 04:41:40 +02:00
|
|
|
char buff[512];
|
|
|
|
|
|
|
|
size_t res;
|
|
|
|
do {
|
|
|
|
char *dst = buff;
|
|
|
|
size_t dstLen = sizeof(buff);
|
2010-09-09 08:47:50 +02:00
|
|
|
res = conv->Convert(&src, &srcLen, &dst, &dstLen);
|
2012-11-13 15:07:39 +01:00
|
|
|
if (res == 0) conv->Convert(nullptr, nullptr, &dst, &dstLen);
|
2010-07-07 04:41:40 +02:00
|
|
|
|
|
|
|
dest.append(buff, sizeof(buff) - dstLen);
|
|
|
|
} while (res == iconv_failed && errno == E2BIG);
|
2014-04-16 04:33:37 +02:00
|
|
|
|
2010-07-07 04:41:40 +02:00
|
|
|
if (res == iconv_failed) {
|
|
|
|
switch (errno) {
|
2014-12-13 17:19:17 +01:00
|
|
|
case EILSEQ:
|
2010-07-07 04:41:40 +02:00
|
|
|
case EINVAL:
|
|
|
|
throw BadInput(
|
|
|
|
"One or more characters in the input string were not valid "
|
|
|
|
"characters in the given input encoding");
|
|
|
|
default:
|
|
|
|
throw ConversionFailure("An unknown conversion failure occurred");
|
|
|
|
}
|
|
|
|
}
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t IconvWrapper::Convert(const char* source, size_t sourceSize, char *dest, size_t destSize) {
|
2012-11-13 15:07:39 +01:00
|
|
|
if (sourceSize == (size_t)-1)
|
2010-06-03 22:32:25 +02:00
|
|
|
sourceSize = SrcStrLen(source);
|
2010-07-06 21:23:16 +02:00
|
|
|
|
2010-09-09 08:47:50 +02:00
|
|
|
size_t res = conv->Convert(&source, &sourceSize, &dest, &destSize);
|
2012-11-13 15:07:39 +01:00
|
|
|
if (res == 0) res = conv->Convert(nullptr, nullptr, &dest, &destSize);
|
2010-06-03 22:32:25 +02:00
|
|
|
|
|
|
|
if (res == iconv_failed) {
|
|
|
|
switch (errno) {
|
|
|
|
case E2BIG:
|
|
|
|
throw BufferTooSmall(
|
|
|
|
"Destination buffer was not large enough to fit converted "
|
|
|
|
"string.");
|
|
|
|
case EINVAL:
|
2014-12-13 17:19:17 +01:00
|
|
|
case EILSEQ:
|
2010-06-03 22:32:25 +02:00
|
|
|
throw BadInput(
|
|
|
|
"One or more characters in the input string were not valid "
|
|
|
|
"characters in the given input encoding");
|
|
|
|
default:
|
2010-07-07 04:41:40 +02:00
|
|
|
throw ConversionFailure("An unknown conversion failure occurred");
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t IconvWrapper::Convert(const char** source, size_t* sourceSize, char** dest, size_t* destSize) {
|
2010-09-09 08:47:50 +02:00
|
|
|
return conv->Convert(source, sourceSize, dest, destSize);
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t IconvWrapper::RequiredBufferSize(std::string const& str) {
|
|
|
|
return RequiredBufferSize(str.data(), str.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t IconvWrapper::RequiredBufferSize(const char* src, size_t srcLen) {
|
2019-05-16 17:04:53 +02:00
|
|
|
char buff[65536];
|
2010-06-03 22:32:25 +02:00
|
|
|
size_t charsWritten = 0;
|
|
|
|
size_t res;
|
|
|
|
|
|
|
|
do {
|
|
|
|
char* dst = buff;
|
|
|
|
size_t dstSize = sizeof(buff);
|
2010-09-09 08:47:50 +02:00
|
|
|
res = conv->Convert(&src, &srcLen, &dst, &dstSize);
|
2012-11-13 15:07:39 +01:00
|
|
|
conv->Convert(nullptr, nullptr, &dst, &dstSize);
|
2010-06-03 22:32:25 +02:00
|
|
|
|
|
|
|
charsWritten += dst - buff;
|
|
|
|
} while (res == iconv_failed && errno == E2BIG);
|
|
|
|
|
|
|
|
if (res == iconv_failed) {
|
|
|
|
switch (errno) {
|
|
|
|
case EINVAL:
|
2014-12-13 17:19:17 +01:00
|
|
|
case EILSEQ:
|
2010-06-03 22:32:25 +02:00
|
|
|
throw BadInput(
|
|
|
|
"One or more characters in the input string were not valid "
|
|
|
|
"characters in the given input encoding");
|
|
|
|
default:
|
2014-12-13 17:19:17 +01:00
|
|
|
throw ConversionFailure("An unknown conversion failure occurred");
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return charsWritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t mbstrlen(const char* str, size_t nulLen) {
|
|
|
|
const char *ptr;
|
|
|
|
switch (nulLen) {
|
|
|
|
case 1:
|
|
|
|
return strlen(str);
|
|
|
|
case 2:
|
|
|
|
for (ptr = str; *reinterpret_cast<const uint16_t *>(ptr) != 0; ptr += 2) ;
|
|
|
|
return ptr - str;
|
|
|
|
case 4:
|
|
|
|
for (ptr = str; *reinterpret_cast<const uint32_t *>(ptr) != 0; ptr += 4) ;
|
|
|
|
return ptr - str;
|
|
|
|
default:
|
|
|
|
return (size_t)-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t IconvWrapper::SrcStrLen(const char* str) {
|
|
|
|
return mbstrlen(str, fromNulLen);
|
|
|
|
|
|
|
|
}
|
|
|
|
size_t IconvWrapper::DstStrLen(const char* str) {
|
|
|
|
return mbstrlen(str, toNulLen);
|
|
|
|
}
|
2011-12-22 22:13:39 +01:00
|
|
|
|
|
|
|
bool IsConversionSupported(const char *src, const char *dst) {
|
|
|
|
iconv_t cd = iconv_open(dst, src);
|
|
|
|
bool supported = cd != iconv_invalid;
|
2019-10-06 17:47:53 +02:00
|
|
|
if (supported) iconv_close(cd);
|
2011-12-22 22:13:39 +01:00
|
|
|
return supported;
|
|
|
|
}
|
|
|
|
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
|
|
|
}
|