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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/**********************************************************************
*
* Name: cpl_recode_iconv.cpp
* Project: CPL - Common Portability Library
* Purpose: Character set recoding and char/wchar_t conversions implemented
* using the iconv() functionality.
* Author: Andrey Kiselev, dron@ak4719.spb.edu
*
**********************************************************************
* Copyright (c) 2011, Andrey Kiselev <dron@ak4719.spb.edu>
* Copyright (c) 2011-2012, Even Rouault <even dot rouault at spatialys.com>
*
* 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.
**********************************************************************/
#include "cpl_port.h"
#include <algorithm>
#ifdef CPL_RECODE_ICONV
#include <iconv.h>
#include "cpl_string.h"
#ifndef ICONV_CPP_CONST
#define ICONV_CPP_CONST ICONV_CONST
#endif
constexpr size_t CPL_RECODE_DSTBUF_SIZE = 32768;
/* used by cpl_recode.cpp */
extern void CPLClearRecodeIconvWarningFlags();
extern char *CPLRecodeIconv(const char *, const char *,
const char *) CPL_RETURNS_NONNULL;
extern char *CPLRecodeFromWCharIconv(const wchar_t *, const char *,
const char *);
extern wchar_t *CPLRecodeToWCharIconv(const char *, const char *, const char *);
/************************************************************************/
/* CPLClearRecodeIconvWarningFlags() */
/************************************************************************/
static bool bHaveWarned1 = false;
static bool bHaveWarned2 = false;
void CPLClearRecodeIconvWarningFlags()
{
bHaveWarned1 = false;
bHaveWarned2 = false;
}
/************************************************************************/
/* CPLFixInputEncoding() */
/************************************************************************/
static const char *CPLFixInputEncoding(const char *pszSrcEncoding,
int nFirstByteVal)
{
// iconv on Alpine Linux seems to assume BE order, when it is not explicit
if (EQUAL(pszSrcEncoding, CPL_ENC_UCS2))
pszSrcEncoding = "UCS-2LE";
else if (EQUAL(pszSrcEncoding, CPL_ENC_UTF16) && nFirstByteVal != 0xFF &&
nFirstByteVal != 0xFE)
{
// Only force UTF-16LE if there's no starting endianness marker
pszSrcEncoding = "UTF-16LE";
}
return pszSrcEncoding;
}
/************************************************************************/
/* CPLRecodeIconv() */
/************************************************************************/
/**
* Convert a string from a source encoding to a destination encoding
* using the iconv() function.
*
* If an error occurs an error may, or may not be posted with CPLError().
*
* @param pszSource a NULL terminated string.
* @param pszSrcEncoding the source encoding.
* @param pszDstEncoding the destination encoding.
*
* @return a NULL terminated string which should be freed with CPLFree().
*/
char *CPLRecodeIconv(const char *pszSource, const char *pszSrcEncoding,
const char *pszDstEncoding)
{
pszSrcEncoding = CPLFixInputEncoding(
pszSrcEncoding, static_cast<unsigned char>(pszSource[0]));
iconv_t sConv;
sConv = iconv_open(pszDstEncoding, pszSrcEncoding);
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
// iconv_t might be a integer or a pointer, so we have to fallback to
// C-style cast
if (sConv == (iconv_t)(-1))
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
{
CPLError(CE_Warning, CPLE_AppDefined,
"Recode from %s to %s failed with the error: \"%s\".",
pszSrcEncoding, pszDstEncoding, strerror(errno));
return CPLStrdup(pszSource);
}
/* -------------------------------------------------------------------- */
/* XXX: There is a portability issue: iconv() function could be */
/* declared differently on different platforms. The second */
/* argument could be declared as char** (as POSIX defines) or */
/* as a const char**. Handle it with the ICONV_CPP_CONST macro here. */
/* -------------------------------------------------------------------- */
ICONV_CPP_CONST char *pszSrcBuf =
const_cast<ICONV_CPP_CONST char *>(pszSource);
size_t nSrcLen = strlen(pszSource);
size_t nDstCurLen = std::max(CPL_RECODE_DSTBUF_SIZE, nSrcLen);
size_t nDstLen = nDstCurLen;
char *pszDestination =
static_cast<char *>(CPLCalloc(nDstCurLen + 1, sizeof(char)));
char *pszDstBuf = pszDestination;
while (nSrcLen > 0)
{
size_t nConverted =
iconv(sConv, &pszSrcBuf, &nSrcLen, &pszDstBuf, &nDstLen);
if (nConverted == static_cast<size_t>(-1))
{
if (errno == EILSEQ)
{
// Skip the invalid sequence in the input string.
if (!bHaveWarned1)
{
bHaveWarned1 = true;
CPLError(CE_Warning, CPLE_AppDefined,
"One or several characters couldn't be converted "
"correctly from %s to %s. "
"This warning will not be emitted anymore",
pszSrcEncoding, pszDstEncoding);
}
if (nSrcLen == 0)
break;
nSrcLen--;
pszSrcBuf++;
continue;
}
else if (errno == E2BIG)
{
// We are running out of the output buffer.
// Dynamically increase the buffer size.
size_t nTmp = nDstCurLen;
nDstCurLen *= 2;
pszDestination = static_cast<char *>(
CPLRealloc(pszDestination, nDstCurLen + 1));
pszDstBuf = pszDestination + nTmp - nDstLen;
nDstLen += nTmp;
continue;
}
else
break;
}
}
pszDestination[nDstCurLen - nDstLen] = '\0';
iconv_close(sConv);
return pszDestination;
}
/************************************************************************/
/* CPLRecodeFromWCharIconv() */
/************************************************************************/
/**
* Convert wchar_t string to UTF-8.
*
* Convert a wchar_t string into a multibyte utf-8 string
* using the iconv() function.
*
* Note that the wchar_t type varies in size on different systems. On
* win32 it is normally 2 bytes, and on unix 4 bytes.
*
* If an error occurs an error may, or may not be posted with CPLError().
*
* @param pwszSource the source wchar_t string, terminated with a 0 wchar_t.
* @param pszSrcEncoding the source encoding, typically CPL_ENC_UCS2.
* @param pszDstEncoding the destination encoding, typically CPL_ENC_UTF8.
*
* @return a zero terminated multi-byte string which should be freed with
* CPLFree(), or NULL if an error occurs.
*/
char *CPLRecodeFromWCharIconv(const wchar_t *pwszSource,
const char *pszSrcEncoding,
const char *pszDstEncoding)
{
pszSrcEncoding = CPLFixInputEncoding(pszSrcEncoding, pwszSource[0]);
/* -------------------------------------------------------------------- */
/* What is the source length. */
/* -------------------------------------------------------------------- */
size_t nSrcLen = 0;
while (pwszSource[nSrcLen] != 0)
nSrcLen++;
/* -------------------------------------------------------------------- */
/* iconv() does not support wchar_t so we need to repack the */
/* characters according to the width of a character in the */
/* source encoding. For instance if wchar_t is 4 bytes but our */
/* source is UTF16 then we need to pack down into 2 byte */
/* characters before passing to iconv(). */
/* -------------------------------------------------------------------- */
const int nTargetCharWidth = CPLEncodingCharSize(pszSrcEncoding);
if (nTargetCharWidth < 1)
{
CPLError(CE_Warning, CPLE_AppDefined,
"Recode from %s with CPLRecodeFromWChar() failed because"
" the width of characters in the encoding are not known.",
pszSrcEncoding);
return CPLStrdup("");
}
GByte *pszIconvSrcBuf =
static_cast<GByte *>(CPLCalloc((nSrcLen + 1), nTargetCharWidth));
for (unsigned int iSrc = 0; iSrc <= nSrcLen; iSrc++)
{
if (nTargetCharWidth == 1)
pszIconvSrcBuf[iSrc] = static_cast<GByte>(pwszSource[iSrc]);
else if (nTargetCharWidth == 2)
(reinterpret_cast<short *>(pszIconvSrcBuf))[iSrc] =
static_cast<short>(pwszSource[iSrc]);
else if (nTargetCharWidth == 4)
(reinterpret_cast<GInt32 *>(pszIconvSrcBuf))[iSrc] =
pwszSource[iSrc];
}
/* -------------------------------------------------------------------- */
/* Create the iconv() translation object. */
/* -------------------------------------------------------------------- */
iconv_t sConv;
sConv = iconv_open(pszDstEncoding, pszSrcEncoding);
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
// iconv_t might be a integer or a pointer, so we have to fallback to
// C-style cast
if (sConv == (iconv_t)(-1))
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
{
CPLFree(pszIconvSrcBuf);
CPLError(CE_Warning, CPLE_AppDefined,
"Recode from %s to %s failed with the error: \"%s\".",
pszSrcEncoding, pszDstEncoding, strerror(errno));
return CPLStrdup("");
}
/* -------------------------------------------------------------------- */
/* XXX: There is a portability issue: iconv() function could be */
/* declared differently on different platforms. The second */
/* argument could be declared as char** (as POSIX defines) or */
/* as a const char**. Handle it with the ICONV_CPP_CONST macro here. */
/* -------------------------------------------------------------------- */
ICONV_CPP_CONST char *pszSrcBuf = const_cast<ICONV_CPP_CONST char *>(
reinterpret_cast<char *>(pszIconvSrcBuf));
/* iconv expects a number of bytes, not characters */
nSrcLen *= nTargetCharWidth;
/* -------------------------------------------------------------------- */
/* Allocate destination buffer. */
/* -------------------------------------------------------------------- */
size_t nDstCurLen = std::max(CPL_RECODE_DSTBUF_SIZE, nSrcLen + 1);
size_t nDstLen = nDstCurLen;
char *pszDestination =
static_cast<char *>(CPLCalloc(nDstCurLen, sizeof(char)));
char *pszDstBuf = pszDestination;
while (nSrcLen > 0)
{
const size_t nConverted =
iconv(sConv, &pszSrcBuf, &nSrcLen, &pszDstBuf, &nDstLen);
if (nConverted == static_cast<size_t>(-1))
{
if (errno == EILSEQ)
{
// Skip the invalid sequence in the input string.
nSrcLen -= nTargetCharWidth;
pszSrcBuf += nTargetCharWidth;
if (!bHaveWarned2)
{
bHaveWarned2 = true;
CPLError(CE_Warning, CPLE_AppDefined,
"One or several characters couldn't be converted "
"correctly from %s to %s. "
"This warning will not be emitted anymore",
pszSrcEncoding, pszDstEncoding);
}
continue;
}
else if (errno == E2BIG)
{
// We are running out of the output buffer.
// Dynamically increase the buffer size.
size_t nTmp = nDstCurLen;
nDstCurLen *= 2;
pszDestination =
static_cast<char *>(CPLRealloc(pszDestination, nDstCurLen));
pszDstBuf = pszDestination + nTmp - nDstLen;
nDstLen += nDstCurLen - nTmp;
continue;
}
else
break;
}
}
if (nDstLen == 0)
{
++nDstCurLen;
pszDestination =
static_cast<char *>(CPLRealloc(pszDestination, nDstCurLen));
++nDstLen;
}
pszDestination[nDstCurLen - nDstLen] = '\0';
iconv_close(sConv);
CPLFree(pszIconvSrcBuf);
return pszDestination;
}
/************************************************************************/
/* CPLRecodeToWCharIconv() */
/************************************************************************/
/**
* Convert UTF-8 string to a wchar_t string.
*
* Convert a 8bit, multi-byte per character input string into a wide
* character (wchar_t) string using the iconv() function.
*
* Note that the wchar_t type varies in size on different systems. On
* win32 it is normally 2 bytes, and on unix 4 bytes.
*
* If an error occurs an error may, or may not be posted with CPLError().
*
* @param pszSource input multi-byte character string.
* @param pszSrcEncoding source encoding, typically CPL_ENC_UTF8.
* @param pszDstEncoding destination encoding, typically CPL_ENC_UCS2.
*
* @return the zero terminated wchar_t string (to be freed with CPLFree()) or
* NULL on error.
*/
wchar_t *CPLRecodeToWCharIconv(const char *pszSource,
const char *pszSrcEncoding,
const char *pszDstEncoding)
{
return reinterpret_cast<wchar_t *>(
CPLRecodeIconv(pszSource, pszSrcEncoding, pszDstEncoding));
}
#endif /* CPL_RECODE_ICONV */
|