2014-05-05 17:30:43 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2003-07-03 20:55:23 +00:00
|
|
|
#include "xpcom-private.h"
|
|
|
|
|
2004-11-08 05:20:05 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 19:10:24 +00:00
|
|
|
// XP_MACOSX or ANDROID
|
2004-11-08 05:20:05 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 19:10:24 +00:00
|
|
|
#if defined(XP_MACOSX) || defined(ANDROID)
|
2004-11-08 05:20:05 +00:00
|
|
|
|
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
NS_CopyNativeToUnicode(const nsACString& aInput, nsAString& aOutput)
|
2004-11-08 05:20:05 +00:00
|
|
|
{
|
2014-05-14 20:15:46 +00:00
|
|
|
CopyUTF8toUTF16(aInput, aOutput);
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2004-11-08 05:20:05 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
NS_CopyUnicodeToNative(const nsAString& aInput, nsACString& aOutput)
|
2004-11-08 05:20:05 +00:00
|
|
|
{
|
2014-05-14 20:15:46 +00:00
|
|
|
CopyUTF16toUTF8(aInput, aOutput);
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2004-11-08 05:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_StartupNativeCharsetUtils()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_ShutdownNativeCharsetUtils()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-06 00:29:46 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// XP_UNIX
|
|
|
|
//-----------------------------------------------------------------------------
|
2004-11-08 05:20:05 +00:00
|
|
|
#elif defined(XP_UNIX)
|
2002-06-10 18:50:11 +00:00
|
|
|
|
|
|
|
#include <stdlib.h> // mbtowc, wctomb
|
|
|
|
#include <locale.h> // setlocale
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
#include "mozilla/Mutex.h"
|
2002-06-10 18:50:11 +00:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsAString.h"
|
2004-02-19 02:44:03 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2002-06-10 18:50:11 +00:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2002-06-10 18:50:11 +00:00
|
|
|
//
|
2003-06-03 12:37:44 +00:00
|
|
|
// choose a conversion library. we used to use mbrtowc/wcrtomb under Linux,
|
|
|
|
// but that doesn't work for non-BMP characters whether we use '-fshort-wchar'
|
2014-05-05 17:30:43 +00:00
|
|
|
// or not (see bug 206811 and
|
2003-06-03 12:37:44 +00:00
|
|
|
// news://news.mozilla.org:119/bajml3$fvr1@ripley.netscape.com). we now use
|
2014-05-05 17:30:43 +00:00
|
|
|
// iconv for all platforms where nltypes.h and nllanginfo.h are present
|
2003-06-03 12:37:44 +00:00
|
|
|
// along with iconv.
|
2002-06-10 18:50:11 +00:00
|
|
|
//
|
2004-05-13 03:12:49 +00:00
|
|
|
#if defined(HAVE_ICONV) && defined(HAVE_NL_TYPES_H) && defined(HAVE_LANGINFO_CODESET)
|
2002-06-10 18:50:11 +00:00
|
|
|
#define USE_ICONV 1
|
|
|
|
#else
|
|
|
|
#define USE_STDCONV 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
2014-05-14 20:15:46 +00:00
|
|
|
isolatin1_to_utf16(const char** aInput, uint32_t* aInputLeft,
|
|
|
|
char16_t** aOutput, uint32_t* aOutputLeft)
|
2002-06-10 18:50:11 +00:00
|
|
|
{
|
2014-05-14 20:15:46 +00:00
|
|
|
while (*aInputLeft && *aOutputLeft) {
|
|
|
|
**aOutput = (unsigned char)** aInput;
|
|
|
|
(*aInput)++;
|
|
|
|
(*aInputLeft)--;
|
|
|
|
(*aOutput)++;
|
|
|
|
(*aOutputLeft)--;
|
2014-05-05 17:30:43 +00:00
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-05-14 20:15:46 +00:00
|
|
|
utf16_to_isolatin1(const char16_t** aInput, uint32_t* aInputLeft,
|
|
|
|
char** aOutput, uint32_t* aOutputLeft)
|
2002-06-10 18:50:11 +00:00
|
|
|
{
|
2014-05-14 20:15:46 +00:00
|
|
|
while (*aInputLeft && *aOutputLeft) {
|
|
|
|
**aOutput = (unsigned char)**aInput;
|
|
|
|
(*aInput)++;
|
|
|
|
(*aInputLeft)--;
|
|
|
|
(*aOutput)++;
|
|
|
|
(*aOutputLeft)--;
|
2014-05-05 17:30:43 +00:00
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// conversion using iconv
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#if defined(USE_ICONV)
|
|
|
|
#include <nl_types.h> // CODESET
|
|
|
|
#include <langinfo.h> // nl_langinfo
|
|
|
|
#include <iconv.h> // iconv_open, iconv, iconv_close
|
|
|
|
#include <errno.h>
|
2006-03-22 04:26:30 +00:00
|
|
|
#include "plstr.h"
|
2002-06-10 18:50:11 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_ICONV_WITH_CONST_INPUT)
|
|
|
|
#define ICONV_INPUT(x) (x)
|
|
|
|
#else
|
|
|
|
#define ICONV_INPUT(x) ((char **)x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// solaris definitely needs this, but we'll enable it by default
|
2003-07-30 05:58:28 +00:00
|
|
|
// just in case... but we know for sure that iconv(3) in glibc
|
|
|
|
// doesn't need this.
|
|
|
|
#if !defined(__GLIBC__)
|
2002-06-10 18:50:11 +00:00
|
|
|
#define ENABLE_UTF8_FALLBACK_SUPPORT
|
2003-07-30 05:58:28 +00:00
|
|
|
#endif
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
#define INVALID_ICONV_T ((iconv_t)-1)
|
2002-06-10 18:50:11 +00:00
|
|
|
|
|
|
|
static inline size_t
|
|
|
|
xp_iconv(iconv_t converter,
|
2014-05-14 20:15:46 +00:00
|
|
|
const char** aInput, size_t* aInputLeft,
|
|
|
|
char** aOutput, size_t* aOutputLeft)
|
|
|
|
{
|
|
|
|
size_t res, outputAvail = aOutputLeft ? *aOutputLeft : 0;
|
|
|
|
res = iconv(converter, ICONV_INPUT(aInput), aInputLeft, aOutput, aOutputLeft);
|
|
|
|
if (res == (size_t)-1) {
|
2014-05-05 17:30:43 +00:00
|
|
|
// on some platforms (e.g., linux) iconv will fail with
|
|
|
|
// E2BIG if it cannot convert _all_ of its input. it'll
|
|
|
|
// still adjust all of the in/out params correctly, so we
|
|
|
|
// can ignore this error. the assumption is that we will
|
|
|
|
// be called again to complete the conversion.
|
2014-05-14 20:15:46 +00:00
|
|
|
if ((errno == E2BIG) && (*aOutputLeft < outputAvail)) {
|
2014-05-05 17:30:43 +00:00
|
|
|
res = 0;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
}
|
|
|
|
return res;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
2002-06-13 21:14:24 +00:00
|
|
|
static inline void
|
|
|
|
xp_iconv_reset(iconv_t converter)
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
// NOTE: the man pages on Solaris claim that you can pass nullptr
|
|
|
|
// for all parameter to reset the converter, but beware the
|
|
|
|
// evil Solaris crash if you go down this route >:-)
|
2002-06-13 21:14:24 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
const char* zero_char_in_ptr = nullptr;
|
|
|
|
char* zero_char_out_ptr = nullptr;
|
|
|
|
size_t zero_size_in = 0;
|
|
|
|
size_t zero_size_out = 0;
|
2014-05-05 17:30:43 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
xp_iconv(converter,
|
|
|
|
&zero_char_in_ptr,
|
|
|
|
&zero_size_in,
|
|
|
|
&zero_char_out_ptr,
|
|
|
|
&zero_size_out);
|
2002-06-13 21:14:24 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 18:50:11 +00:00
|
|
|
static inline iconv_t
|
2014-05-14 20:15:46 +00:00
|
|
|
xp_iconv_open(const char** to_list, const char** from_list)
|
2002-06-10 18:50:11 +00:00
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
iconv_t res;
|
2014-05-14 20:15:46 +00:00
|
|
|
const char** from_name;
|
|
|
|
const char** to_name;
|
2014-05-05 17:30:43 +00:00
|
|
|
|
|
|
|
// try all possible combinations to locate a converter.
|
|
|
|
to_name = to_list;
|
|
|
|
while (*to_name) {
|
|
|
|
if (**to_name) {
|
|
|
|
from_name = from_list;
|
|
|
|
while (*from_name) {
|
|
|
|
if (**from_name) {
|
|
|
|
res = iconv_open(*to_name, *from_name);
|
2014-05-14 20:15:46 +00:00
|
|
|
if (res != INVALID_ICONV_T) {
|
2014-05-05 17:30:43 +00:00
|
|
|
return res;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
from_name++;
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
to_name++;
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
return INVALID_ICONV_T;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
/*
|
2014-01-04 15:02:17 +00:00
|
|
|
* char16_t[] is NOT a UCS-2 array BUT a UTF-16 string. Therefore, we
|
2003-07-30 05:58:28 +00:00
|
|
|
* have to use UTF-16 with iconv(3) on platforms where it's supported.
|
2014-05-05 17:30:43 +00:00
|
|
|
* However, the way UTF-16 and UCS-2 are interpreted varies across platforms
|
2003-07-30 05:58:28 +00:00
|
|
|
* and implementations of iconv(3). On Tru64, it also depends on the environment
|
2014-05-05 17:30:43 +00:00
|
|
|
* variable. To avoid the trouble arising from byte-swapping
|
|
|
|
* (bug 208809), we have to try UTF-16LE/BE and UCS-2LE/BE before falling
|
|
|
|
* back to UTF-16 and UCS-2 and variants. We assume that UTF-16 and UCS-2
|
2003-07-30 05:58:28 +00:00
|
|
|
* on systems without UTF-16LE/BE and UCS-2LE/BE have the native endianness,
|
|
|
|
* which isn't the case of glibc 2.1.x, for which we use 'UNICODELITTLE'
|
|
|
|
* and 'UNICODEBIG'. It's also not true of Tru64 V4 when the environment
|
2014-05-05 17:30:43 +00:00
|
|
|
* variable ICONV_BYTEORDER is set to 'big-endian', about which not much
|
2003-07-30 05:58:28 +00:00
|
|
|
* can be done other than adding a note in the release notes. (bug 206811)
|
|
|
|
*/
|
2014-05-14 20:15:46 +00:00
|
|
|
static const char* UTF_16_NAMES[] = {
|
2003-07-30 05:58:28 +00:00
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
2014-05-05 17:30:43 +00:00
|
|
|
"UTF-16LE",
|
2003-07-30 05:58:28 +00:00
|
|
|
#if defined(__GLIBC__)
|
2014-05-05 17:30:43 +00:00
|
|
|
"UNICODELITTLE",
|
2003-07-30 05:58:28 +00:00
|
|
|
#endif
|
2014-05-05 17:30:43 +00:00
|
|
|
"UCS-2LE",
|
2003-07-30 05:58:28 +00:00
|
|
|
#else
|
2014-05-05 17:30:43 +00:00
|
|
|
"UTF-16BE",
|
2003-07-30 05:58:28 +00:00
|
|
|
#if defined(__GLIBC__)
|
2014-05-05 17:30:43 +00:00
|
|
|
"UNICODEBIG",
|
2003-07-30 05:58:28 +00:00
|
|
|
#endif
|
2014-05-05 17:30:43 +00:00
|
|
|
"UCS-2BE",
|
2003-07-30 05:58:28 +00:00
|
|
|
#endif
|
2014-05-05 17:30:43 +00:00
|
|
|
"UTF-16",
|
|
|
|
"UCS-2",
|
|
|
|
"UCS2",
|
|
|
|
"UCS_2",
|
|
|
|
"ucs-2",
|
|
|
|
"ucs2",
|
|
|
|
"ucs_2",
|
|
|
|
nullptr
|
2002-06-10 18:50:11 +00:00
|
|
|
};
|
|
|
|
|
2003-07-30 05:58:28 +00:00
|
|
|
#if defined(ENABLE_UTF8_FALLBACK_SUPPORT)
|
2014-05-14 20:15:46 +00:00
|
|
|
static const char* UTF_8_NAMES[] = {
|
2014-05-05 17:30:43 +00:00
|
|
|
"UTF-8",
|
|
|
|
"UTF8",
|
|
|
|
"UTF_8",
|
|
|
|
"utf-8",
|
|
|
|
"utf8",
|
|
|
|
"utf_8",
|
|
|
|
nullptr
|
2002-06-10 18:50:11 +00:00
|
|
|
};
|
2003-07-30 05:58:28 +00:00
|
|
|
#endif
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
static const char* ISO_8859_1_NAMES[] = {
|
2014-05-05 17:30:43 +00:00
|
|
|
"ISO-8859-1",
|
2003-07-30 05:58:28 +00:00
|
|
|
#if !defined(__GLIBC__)
|
2014-05-05 17:30:43 +00:00
|
|
|
"ISO8859-1",
|
|
|
|
"ISO88591",
|
|
|
|
"ISO_8859_1",
|
|
|
|
"ISO8859_1",
|
|
|
|
"iso-8859-1",
|
|
|
|
"iso8859-1",
|
|
|
|
"iso88591",
|
|
|
|
"iso_8859_1",
|
|
|
|
"iso8859_1",
|
2003-07-30 05:58:28 +00:00
|
|
|
#endif
|
2014-05-05 17:30:43 +00:00
|
|
|
nullptr
|
2002-06-10 18:50:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsNativeCharsetConverter
|
|
|
|
{
|
|
|
|
public:
|
2014-05-05 17:30:43 +00:00
|
|
|
nsNativeCharsetConverter();
|
|
|
|
~nsNativeCharsetConverter();
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
nsresult NativeToUnicode(const char** aInput, uint32_t* aInputLeft,
|
|
|
|
char16_t** aOutput, uint32_t* aOutputLeft);
|
|
|
|
nsresult UnicodeToNative(const char16_t** aInput, uint32_t* aInputLeft,
|
|
|
|
char** aOutput, uint32_t* aOutputLeft);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
static void GlobalInit();
|
|
|
|
static void GlobalShutdown();
|
|
|
|
static bool IsNativeUTF8();
|
2002-06-10 18:50:11 +00:00
|
|
|
|
|
|
|
private:
|
2014-05-05 17:30:43 +00:00
|
|
|
static iconv_t gNativeToUnicode;
|
|
|
|
static iconv_t gUnicodeToNative;
|
2002-06-10 18:50:11 +00:00
|
|
|
#if defined(ENABLE_UTF8_FALLBACK_SUPPORT)
|
2014-05-05 17:30:43 +00:00
|
|
|
static iconv_t gNativeToUTF8;
|
|
|
|
static iconv_t gUTF8ToNative;
|
|
|
|
static iconv_t gUnicodeToUTF8;
|
|
|
|
static iconv_t gUTF8ToUnicode;
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif
|
2014-05-14 20:15:46 +00:00
|
|
|
static Mutex* gLock;
|
2014-05-05 17:30:43 +00:00
|
|
|
static bool gInitialized;
|
|
|
|
static bool gIsNativeUTF8;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
static void LazyInit();
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
static void Lock()
|
|
|
|
{
|
|
|
|
if (gLock) {
|
|
|
|
gLock->Lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void Unlock()
|
|
|
|
{
|
|
|
|
if (gLock) {
|
|
|
|
gLock->Unlock();
|
|
|
|
}
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
iconv_t nsNativeCharsetConverter::gNativeToUnicode = INVALID_ICONV_T;
|
|
|
|
iconv_t nsNativeCharsetConverter::gUnicodeToNative = INVALID_ICONV_T;
|
|
|
|
#if defined(ENABLE_UTF8_FALLBACK_SUPPORT)
|
|
|
|
iconv_t nsNativeCharsetConverter::gNativeToUTF8 = INVALID_ICONV_T;
|
|
|
|
iconv_t nsNativeCharsetConverter::gUTF8ToNative = INVALID_ICONV_T;
|
|
|
|
iconv_t nsNativeCharsetConverter::gUnicodeToUTF8 = INVALID_ICONV_T;
|
|
|
|
iconv_t nsNativeCharsetConverter::gUTF8ToUnicode = INVALID_ICONV_T;
|
|
|
|
#endif
|
2014-05-14 20:15:46 +00:00
|
|
|
Mutex* nsNativeCharsetConverter::gLock = nullptr;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool nsNativeCharsetConverter::gInitialized = false;
|
|
|
|
bool nsNativeCharsetConverter::gIsNativeUTF8 = false;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
nsNativeCharsetConverter::LazyInit()
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
// LazyInit may be called before NS_StartupNativeCharsetUtils, but
|
|
|
|
// the setlocale it does has to be called before nl_langinfo. Like in
|
|
|
|
// NS_StartupNativeCharsetUtils, assume we are called early enough that
|
|
|
|
// we are the first to care about the locale's charset.
|
2014-05-14 20:15:46 +00:00
|
|
|
if (!gLock) {
|
2014-05-05 17:30:43 +00:00
|
|
|
setlocale(LC_CTYPE, "");
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
|
|
|
const char* blank_list[] = { "", nullptr };
|
|
|
|
const char** native_charset_list = blank_list;
|
|
|
|
const char* native_charset = nl_langinfo(CODESET);
|
|
|
|
if (!native_charset) {
|
2014-05-05 17:30:43 +00:00
|
|
|
NS_ERROR("native charset is unknown");
|
|
|
|
// fallback to ISO-8859-1
|
|
|
|
native_charset_list = ISO_8859_1_NAMES;
|
2014-05-14 20:15:46 +00:00
|
|
|
} else {
|
2014-05-05 17:30:43 +00:00
|
|
|
native_charset_list[0] = native_charset;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
|
|
|
|
// Most, if not all, Unixen supporting UTF-8 and nl_langinfo(CODESET)
|
|
|
|
// return 'UTF-8' (or 'utf-8')
|
2014-05-14 20:15:46 +00:00
|
|
|
if (!PL_strcasecmp(native_charset, "UTF-8")) {
|
2014-05-05 17:30:43 +00:00
|
|
|
gIsNativeUTF8 = true;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
|
|
|
|
gNativeToUnicode = xp_iconv_open(UTF_16_NAMES, native_charset_list);
|
|
|
|
gUnicodeToNative = xp_iconv_open(native_charset_list, UTF_16_NAMES);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
|
|
|
#if defined(ENABLE_UTF8_FALLBACK_SUPPORT)
|
2014-05-05 17:30:43 +00:00
|
|
|
if (gNativeToUnicode == INVALID_ICONV_T) {
|
|
|
|
gNativeToUTF8 = xp_iconv_open(UTF_8_NAMES, native_charset_list);
|
|
|
|
gUTF8ToUnicode = xp_iconv_open(UTF_16_NAMES, UTF_8_NAMES);
|
|
|
|
NS_ASSERTION(gNativeToUTF8 != INVALID_ICONV_T, "no native to utf-8 converter");
|
|
|
|
NS_ASSERTION(gUTF8ToUnicode != INVALID_ICONV_T, "no utf-8 to utf-16 converter");
|
|
|
|
}
|
|
|
|
if (gUnicodeToNative == INVALID_ICONV_T) {
|
|
|
|
gUnicodeToUTF8 = xp_iconv_open(UTF_8_NAMES, UTF_16_NAMES);
|
|
|
|
gUTF8ToNative = xp_iconv_open(native_charset_list, UTF_8_NAMES);
|
|
|
|
NS_ASSERTION(gUnicodeToUTF8 != INVALID_ICONV_T, "no utf-16 to utf-8 converter");
|
|
|
|
NS_ASSERTION(gUTF8ToNative != INVALID_ICONV_T, "no utf-8 to native converter");
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
#else
|
2014-05-05 17:30:43 +00:00
|
|
|
NS_ASSERTION(gNativeToUnicode != INVALID_ICONV_T, "no native to utf-16 converter");
|
|
|
|
NS_ASSERTION(gUnicodeToNative != INVALID_ICONV_T, "no utf-16 to native converter");
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
/*
|
|
|
|
* On Solaris 8 (and newer?), the iconv modules converting to UCS-2
|
|
|
|
* prepend a byte order mark unicode character (BOM, u+FEFF) during
|
|
|
|
* the first use of the iconv converter. The same is the case of
|
|
|
|
* glibc 2.2.9x and Tru64 V5 (see bug 208809) when 'UTF-16' is used.
|
|
|
|
* However, we use 'UTF-16LE/BE' in both cases, instead so that we
|
|
|
|
* should be safe. But just in case...
|
|
|
|
*
|
|
|
|
* This dummy conversion gets rid of the BOMs and fixes bug 153562.
|
|
|
|
*/
|
|
|
|
char dummy_input[1] = { ' ' };
|
|
|
|
char dummy_output[4];
|
|
|
|
|
|
|
|
if (gNativeToUnicode != INVALID_ICONV_T) {
|
2014-05-14 20:15:46 +00:00
|
|
|
const char* input = dummy_input;
|
2014-05-05 17:30:43 +00:00
|
|
|
size_t input_left = sizeof(dummy_input);
|
2014-05-14 20:15:46 +00:00
|
|
|
char* output = dummy_output;
|
2014-05-05 17:30:43 +00:00
|
|
|
size_t output_left = sizeof(dummy_output);
|
|
|
|
|
|
|
|
xp_iconv(gNativeToUnicode, &input, &input_left, &output, &output_left);
|
|
|
|
}
|
2002-07-30 00:58:36 +00:00
|
|
|
#if defined(ENABLE_UTF8_FALLBACK_SUPPORT)
|
2014-05-05 17:30:43 +00:00
|
|
|
if (gUTF8ToUnicode != INVALID_ICONV_T) {
|
2014-05-14 20:15:46 +00:00
|
|
|
const char* input = dummy_input;
|
2014-05-05 17:30:43 +00:00
|
|
|
size_t input_left = sizeof(dummy_input);
|
2014-05-14 20:15:46 +00:00
|
|
|
char* output = dummy_output;
|
2014-05-05 17:30:43 +00:00
|
|
|
size_t output_left = sizeof(dummy_output);
|
|
|
|
|
|
|
|
xp_iconv(gUTF8ToUnicode, &input, &input_left, &output, &output_left);
|
|
|
|
}
|
2002-07-30 00:58:36 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
gInitialized = true;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNativeCharsetConverter::GlobalInit()
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
gLock = new Mutex("nsNativeCharsetConverter.gLock");
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNativeCharsetConverter::GlobalShutdown()
|
|
|
|
{
|
2014-08-08 14:04:45 +00:00
|
|
|
delete gLock;
|
|
|
|
gLock = nullptr;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
if (gNativeToUnicode != INVALID_ICONV_T) {
|
|
|
|
iconv_close(gNativeToUnicode);
|
|
|
|
gNativeToUnicode = INVALID_ICONV_T;
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
if (gUnicodeToNative != INVALID_ICONV_T) {
|
|
|
|
iconv_close(gUnicodeToNative);
|
|
|
|
gUnicodeToNative = INVALID_ICONV_T;
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
|
|
|
|
#if defined(ENABLE_UTF8_FALLBACK_SUPPORT)
|
2014-05-05 17:30:43 +00:00
|
|
|
if (gNativeToUTF8 != INVALID_ICONV_T) {
|
|
|
|
iconv_close(gNativeToUTF8);
|
|
|
|
gNativeToUTF8 = INVALID_ICONV_T;
|
|
|
|
}
|
|
|
|
if (gUTF8ToNative != INVALID_ICONV_T) {
|
|
|
|
iconv_close(gUTF8ToNative);
|
|
|
|
gUTF8ToNative = INVALID_ICONV_T;
|
|
|
|
}
|
|
|
|
if (gUnicodeToUTF8 != INVALID_ICONV_T) {
|
|
|
|
iconv_close(gUnicodeToUTF8);
|
|
|
|
gUnicodeToUTF8 = INVALID_ICONV_T;
|
|
|
|
}
|
|
|
|
if (gUTF8ToUnicode != INVALID_ICONV_T) {
|
|
|
|
iconv_close(gUTF8ToUnicode);
|
|
|
|
gUTF8ToUnicode = INVALID_ICONV_T;
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
gInitialized = false;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsNativeCharsetConverter::nsNativeCharsetConverter()
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
Lock();
|
2014-05-14 20:15:46 +00:00
|
|
|
if (!gInitialized) {
|
2014-05-05 17:30:43 +00:00
|
|
|
LazyInit();
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsNativeCharsetConverter::~nsNativeCharsetConverter()
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
// reset converters for next time
|
2014-05-14 20:15:46 +00:00
|
|
|
if (gNativeToUnicode != INVALID_ICONV_T) {
|
2014-05-05 17:30:43 +00:00
|
|
|
xp_iconv_reset(gNativeToUnicode);
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
|
|
|
if (gUnicodeToNative != INVALID_ICONV_T) {
|
2014-05-05 17:30:43 +00:00
|
|
|
xp_iconv_reset(gUnicodeToNative);
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
#if defined(ENABLE_UTF8_FALLBACK_SUPPORT)
|
2014-05-14 20:15:46 +00:00
|
|
|
if (gNativeToUTF8 != INVALID_ICONV_T) {
|
2014-05-05 17:30:43 +00:00
|
|
|
xp_iconv_reset(gNativeToUTF8);
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
|
|
|
if (gUTF8ToNative != INVALID_ICONV_T) {
|
2014-05-05 17:30:43 +00:00
|
|
|
xp_iconv_reset(gUTF8ToNative);
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
|
|
|
if (gUnicodeToUTF8 != INVALID_ICONV_T) {
|
2014-05-05 17:30:43 +00:00
|
|
|
xp_iconv_reset(gUnicodeToUTF8);
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
|
|
|
if (gUTF8ToUnicode != INVALID_ICONV_T) {
|
2014-05-05 17:30:43 +00:00
|
|
|
xp_iconv_reset(gUTF8ToUnicode);
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif
|
2014-05-05 17:30:43 +00:00
|
|
|
Unlock();
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
nsNativeCharsetConverter::NativeToUnicode(const char** aInput,
|
|
|
|
uint32_t* aInputLeft,
|
|
|
|
char16_t** aOutput,
|
|
|
|
uint32_t* aOutputLeft)
|
2002-06-10 18:50:11 +00:00
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
size_t res = 0;
|
2014-05-14 20:15:46 +00:00
|
|
|
size_t inLeft = (size_t)*aInputLeft;
|
|
|
|
size_t outLeft = (size_t)*aOutputLeft * 2;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
if (gNativeToUnicode != INVALID_ICONV_T) {
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
res = xp_iconv(gNativeToUnicode, aInput, &inLeft, (char**)aOutput, &outLeft);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
*aInputLeft = inLeft;
|
|
|
|
*aOutputLeft = outLeft / 2;
|
|
|
|
if (res != (size_t)-1) {
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
NS_WARNING("conversion from native to utf-16 failed");
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// reset converter
|
|
|
|
xp_iconv_reset(gNativeToUnicode);
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
#if defined(ENABLE_UTF8_FALLBACK_SUPPORT)
|
2014-05-05 17:30:43 +00:00
|
|
|
else if ((gNativeToUTF8 != INVALID_ICONV_T) &&
|
|
|
|
(gUTF8ToUnicode != INVALID_ICONV_T)) {
|
|
|
|
// convert first to UTF8, then from UTF8 to UCS2
|
2014-05-14 20:15:46 +00:00
|
|
|
const char* in = *aInput;
|
2014-05-05 17:30:43 +00:00
|
|
|
|
|
|
|
char ubuf[1024];
|
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
// we assume we're always called with enough space in |aOutput|,
|
2014-05-05 17:30:43 +00:00
|
|
|
// so convert many chars at a time...
|
|
|
|
while (inLeft) {
|
2014-05-14 20:15:46 +00:00
|
|
|
char* p = ubuf;
|
2014-05-05 17:30:43 +00:00
|
|
|
size_t n = sizeof(ubuf);
|
|
|
|
res = xp_iconv(gNativeToUTF8, &in, &inLeft, &p, &n);
|
2014-05-14 20:15:46 +00:00
|
|
|
if (res == (size_t)-1) {
|
2014-05-05 17:30:43 +00:00
|
|
|
NS_ERROR("conversion from native to utf-8 failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(outLeft > 0, "bad assumption");
|
|
|
|
p = ubuf;
|
|
|
|
n = sizeof(ubuf) - n;
|
2014-05-14 20:15:46 +00:00
|
|
|
res = xp_iconv(gUTF8ToUnicode, (const char**)&p, &n,
|
|
|
|
(char**)aOutput, &outLeft);
|
|
|
|
if (res == (size_t)-1) {
|
2014-05-05 17:30:43 +00:00
|
|
|
NS_ERROR("conversion from utf-8 to utf-16 failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
(*aInput) += (*aInputLeft - inLeft);
|
|
|
|
*aInputLeft = inLeft;
|
|
|
|
*aOutputLeft = outLeft / 2;
|
2004-01-04 08:20:52 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
if (res != (size_t)-1) {
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// reset converters
|
|
|
|
xp_iconv_reset(gNativeToUTF8);
|
|
|
|
xp_iconv_reset(gUTF8ToUnicode);
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// fallback: zero-pad and hope for the best
|
|
|
|
// XXX This is lame and we have to do better.
|
2014-05-14 20:15:46 +00:00
|
|
|
isolatin1_to_utf16(aInput, aInputLeft, aOutput, aOutputLeft);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
nsNativeCharsetConverter::UnicodeToNative(const char16_t** aInput,
|
|
|
|
uint32_t* aInputLeft,
|
|
|
|
char** aOutput,
|
|
|
|
uint32_t* aOutputLeft)
|
2002-06-10 18:50:11 +00:00
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
size_t res = 0;
|
2014-05-14 20:15:46 +00:00
|
|
|
size_t inLeft = (size_t)*aInputLeft * 2;
|
|
|
|
size_t outLeft = (size_t)*aOutputLeft;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
if (gUnicodeToNative != INVALID_ICONV_T) {
|
2014-05-14 20:15:46 +00:00
|
|
|
res = xp_iconv(gUnicodeToNative, (const char**)aInput, &inLeft,
|
|
|
|
aOutput, &outLeft);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
*aInputLeft = inLeft / 2;
|
|
|
|
*aOutputLeft = outLeft;
|
|
|
|
if (res != (size_t)-1) {
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
NS_ERROR("iconv failed");
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// reset converter
|
|
|
|
xp_iconv_reset(gUnicodeToNative);
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
#if defined(ENABLE_UTF8_FALLBACK_SUPPORT)
|
2014-05-05 17:30:43 +00:00
|
|
|
else if ((gUnicodeToUTF8 != INVALID_ICONV_T) &&
|
|
|
|
(gUTF8ToNative != INVALID_ICONV_T)) {
|
2014-05-14 20:15:46 +00:00
|
|
|
const char* in = (const char*)*aInput;
|
2014-05-05 17:30:43 +00:00
|
|
|
|
|
|
|
char ubuf[6]; // max utf-8 char length (really only needs to be 4 bytes)
|
|
|
|
|
|
|
|
// convert one uchar at a time...
|
|
|
|
while (inLeft && outLeft) {
|
2014-05-14 20:15:46 +00:00
|
|
|
char* p = ubuf;
|
2014-05-05 17:30:43 +00:00
|
|
|
size_t n = sizeof(ubuf), one_uchar = sizeof(char16_t);
|
|
|
|
res = xp_iconv(gUnicodeToUTF8, &in, &one_uchar, &p, &n);
|
2014-05-14 20:15:46 +00:00
|
|
|
if (res == (size_t)-1) {
|
2014-05-05 17:30:43 +00:00
|
|
|
NS_ERROR("conversion from utf-16 to utf-8 failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p = ubuf;
|
|
|
|
n = sizeof(ubuf) - n;
|
2014-05-14 20:15:46 +00:00
|
|
|
res = xp_iconv(gUTF8ToNative, (const char**)&p, &n, aOutput, &outLeft);
|
|
|
|
if (res == (size_t)-1) {
|
2014-05-05 17:30:43 +00:00
|
|
|
if (errno == E2BIG) {
|
|
|
|
// not enough room for last uchar... back up and return.
|
|
|
|
in -= sizeof(char16_t);
|
|
|
|
res = 0;
|
2014-05-14 20:15:46 +00:00
|
|
|
} else {
|
2014-05-05 17:30:43 +00:00
|
|
|
NS_ERROR("conversion from utf-8 to native failed");
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
inLeft -= sizeof(char16_t);
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
(*aInput) += (*aInputLeft - inLeft / 2);
|
|
|
|
*aInputLeft = inLeft / 2;
|
|
|
|
*aOutputLeft = outLeft;
|
|
|
|
if (res != (size_t)-1) {
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
|
|
|
|
// reset converters
|
|
|
|
xp_iconv_reset(gUnicodeToUTF8);
|
|
|
|
xp_iconv_reset(gUTF8ToNative);
|
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// fallback: truncate and hope for the best
|
|
|
|
// XXX This is lame and we have to do better.
|
2014-05-14 20:15:46 +00:00
|
|
|
utf16_to_isolatin1(aInput, aInputLeft, aOutput, aOutputLeft);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2006-03-29 04:53:21 +00:00
|
|
|
nsNativeCharsetConverter::IsNativeUTF8()
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
if (!gInitialized) {
|
|
|
|
Lock();
|
2014-05-14 20:15:46 +00:00
|
|
|
if (!gInitialized) {
|
2014-05-05 17:30:43 +00:00
|
|
|
LazyInit();
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
return gIsNativeUTF8;
|
2006-03-29 04:53:21 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif // USE_ICONV
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// conversion using mb[r]towc/wc[r]tomb
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#if defined(USE_STDCONV)
|
|
|
|
#if defined(HAVE_WCRTOMB) || defined(HAVE_MBRTOWC)
|
|
|
|
#include <wchar.h> // mbrtowc, wcrtomb
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class nsNativeCharsetConverter
|
|
|
|
{
|
|
|
|
public:
|
2014-05-05 17:30:43 +00:00
|
|
|
nsNativeCharsetConverter();
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
nsresult NativeToUnicode(const char** aInput, uint32_t* aInputLeft,
|
|
|
|
char16_t** aOutput, uint32_t* aOutputLeft);
|
|
|
|
nsresult UnicodeToNative(const char16_t** aInput, uint32_t* aInputLeft,
|
|
|
|
char** aOutput, uint32_t* aOutputLeft);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
static void GlobalInit();
|
|
|
|
static void GlobalShutdown() { }
|
|
|
|
static bool IsNativeUTF8();
|
2002-06-10 18:50:11 +00:00
|
|
|
|
|
|
|
private:
|
2014-05-05 17:30:43 +00:00
|
|
|
static bool gWCharIsUnicode;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_WCRTOMB) || defined(HAVE_MBRTOWC)
|
2014-05-05 17:30:43 +00:00
|
|
|
mbstate_t ps;
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool nsNativeCharsetConverter::gWCharIsUnicode = false;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
|
|
|
nsNativeCharsetConverter::nsNativeCharsetConverter()
|
|
|
|
{
|
|
|
|
#if defined(HAVE_WCRTOMB) || defined(HAVE_MBRTOWC)
|
2014-05-05 17:30:43 +00:00
|
|
|
memset(&ps, 0, sizeof(ps));
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNativeCharsetConverter::GlobalInit()
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
// verify that wchar_t for the current locale is actually unicode.
|
|
|
|
// if it is not, then we should avoid calling mbtowc/wctomb and
|
|
|
|
// just fallback on zero-pad/truncation conversion.
|
|
|
|
//
|
|
|
|
// this test cannot be done at build time because the encoding of
|
|
|
|
// wchar_t may depend on the runtime locale. sad, but true!!
|
|
|
|
//
|
|
|
|
// so, if wchar_t is unicode then converting an ASCII character
|
|
|
|
// to wchar_t should not change its numeric value. we'll just
|
|
|
|
// check what happens with the ASCII 'a' character.
|
|
|
|
//
|
|
|
|
// this test is not perfect... obviously, it could yield false
|
|
|
|
// positives, but then at least ASCII text would be converted
|
|
|
|
// properly (or maybe just the 'a' character) -- oh well :(
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
char a = 'a';
|
|
|
|
unsigned int w = 0;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
int res = mbtowc((wchar_t*)&w, &a, 1);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
gWCharIsUnicode = (res != -1 && w == 'a');
|
2002-06-10 18:50:11 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-05-14 20:15:46 +00:00
|
|
|
if (!gWCharIsUnicode) {
|
2014-05-05 17:30:43 +00:00
|
|
|
NS_WARNING("wchar_t is not unicode (unicode conversion will be lossy)");
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
nsNativeCharsetConverter::NativeToUnicode(const char** aInput,
|
|
|
|
uint32_t* aInputLeft,
|
|
|
|
char16_t** aOutput,
|
|
|
|
uint32_t* aOutputLeft)
|
2002-06-10 18:50:11 +00:00
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
if (gWCharIsUnicode) {
|
|
|
|
int incr;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// cannot use wchar_t here since it may have been redefined (e.g.,
|
|
|
|
// via -fshort-wchar). hopefully, sizeof(tmp) is sufficient XP.
|
|
|
|
unsigned int tmp = 0;
|
2014-05-14 20:15:46 +00:00
|
|
|
while (*aInputLeft && *aOutputLeft) {
|
2002-06-10 18:50:11 +00:00
|
|
|
#ifdef HAVE_MBRTOWC
|
2014-05-14 20:15:46 +00:00
|
|
|
incr = (int)mbrtowc((wchar_t*)&tmp, *aInput, *aInputLeft, &ps);
|
2002-06-10 18:50:11 +00:00
|
|
|
#else
|
2014-05-05 17:30:43 +00:00
|
|
|
// XXX is this thread-safe?
|
2014-05-14 20:15:46 +00:00
|
|
|
incr = (int)mbtowc((wchar_t*)&tmp, *aInput, *aInputLeft);
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif
|
2014-05-05 17:30:43 +00:00
|
|
|
if (incr < 0) {
|
|
|
|
NS_WARNING("mbtowc failed: possible charset mismatch");
|
|
|
|
// zero-pad and hope for the best
|
2014-05-14 20:15:46 +00:00
|
|
|
tmp = (unsigned char)**aInput;
|
2014-05-05 17:30:43 +00:00
|
|
|
incr = 1;
|
|
|
|
}
|
2014-05-14 20:15:46 +00:00
|
|
|
** aOutput = (char16_t)tmp;
|
|
|
|
(*aInput) += incr;
|
|
|
|
(*aInputLeft) -= incr;
|
|
|
|
(*aOutput)++;
|
|
|
|
(*aOutputLeft)--;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
2014-05-14 20:15:46 +00:00
|
|
|
} else {
|
2014-05-05 17:30:43 +00:00
|
|
|
// wchar_t isn't unicode, so the best we can do is treat the
|
|
|
|
// input as if it is isolatin1 :(
|
2014-05-14 20:15:46 +00:00
|
|
|
isolatin1_to_utf16(aInput, aInputLeft, aOutput, aOutputLeft);
|
2014-05-05 17:30:43 +00:00
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
nsNativeCharsetConverter::UnicodeToNative(const char16_t** aInput,
|
|
|
|
uint32_t* aInputLeft,
|
|
|
|
char** aOutput,
|
|
|
|
uint32_t* aOutputLeft)
|
2002-06-10 18:50:11 +00:00
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
if (gWCharIsUnicode) {
|
|
|
|
int incr;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
while (*aInputLeft && *aOutputLeft >= MB_CUR_MAX) {
|
2002-06-10 18:50:11 +00:00
|
|
|
#ifdef HAVE_WCRTOMB
|
2014-05-14 20:15:46 +00:00
|
|
|
incr = (int)wcrtomb(*aOutput, (wchar_t)**aInput, &ps);
|
2002-06-10 18:50:11 +00:00
|
|
|
#else
|
2014-05-05 17:30:43 +00:00
|
|
|
// XXX is this thread-safe?
|
2014-05-14 20:15:46 +00:00
|
|
|
incr = (int)wctomb(*aOutput, (wchar_t)**aInput);
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif
|
2014-05-05 17:30:43 +00:00
|
|
|
if (incr < 0) {
|
|
|
|
NS_WARNING("mbtowc failed: possible charset mismatch");
|
2014-05-14 20:15:46 +00:00
|
|
|
** aOutput = (unsigned char)**aInput; // truncate
|
2014-05-05 17:30:43 +00:00
|
|
|
incr = 1;
|
|
|
|
}
|
|
|
|
// most likely we're dead anyways if this assertion should fire
|
2014-05-14 20:15:46 +00:00
|
|
|
NS_ASSERTION(uint32_t(incr) <= *aOutputLeft, "wrote beyond end of string");
|
|
|
|
(*aOutput) += incr;
|
|
|
|
(*aOutputLeft) -= incr;
|
|
|
|
(*aInput)++;
|
|
|
|
(*aInputLeft)--;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
2014-05-14 20:15:46 +00:00
|
|
|
} else {
|
2014-05-05 17:30:43 +00:00
|
|
|
// wchar_t isn't unicode, so the best we can do is treat the
|
|
|
|
// input as if it is isolatin1 :(
|
2014-05-14 20:15:46 +00:00
|
|
|
utf16_to_isolatin1(aInput, aInputLeft, aOutput, aOutputLeft);
|
2014-05-05 17:30:43 +00:00
|
|
|
}
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
2006-03-29 04:53:21 +00:00
|
|
|
// XXX : for now, return false
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2006-03-29 04:53:21 +00:00
|
|
|
nsNativeCharsetConverter::IsNativeUTF8()
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
return false;
|
2006-03-29 04:53:21 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 18:50:11 +00:00
|
|
|
#endif // USE_STDCONV
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// API implementation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
NS_CopyNativeToUnicode(const nsACString& aInput, nsAString& aOutput)
|
2002-06-10 18:50:11 +00:00
|
|
|
{
|
2014-05-14 20:15:46 +00:00
|
|
|
aOutput.Truncate();
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
uint32_t inputLen = aInput.Length();
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
nsACString::const_iterator iter;
|
2014-05-14 20:15:46 +00:00
|
|
|
aInput.BeginReading(iter);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
//
|
|
|
|
// OPTIMIZATION: preallocate space for largest possible result; convert
|
|
|
|
// directly into the result buffer to avoid intermediate buffer copy.
|
|
|
|
//
|
|
|
|
// this will generally result in a larger allocation, but that seems
|
|
|
|
// better than an extra buffer copy.
|
|
|
|
//
|
2015-01-28 09:00:40 +00:00
|
|
|
if (!aOutput.SetLength(inputLen, fallible)) {
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
nsAString::iterator out_iter;
|
2014-05-14 20:15:46 +00:00
|
|
|
aOutput.BeginWriting(out_iter);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
char16_t* result = out_iter.get();
|
2014-05-05 17:30:43 +00:00
|
|
|
uint32_t resultLeft = inputLen;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
const char* buf = iter.get();
|
2014-05-05 17:30:43 +00:00
|
|
|
uint32_t bufLeft = inputLen;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
nsNativeCharsetConverter conv;
|
|
|
|
nsresult rv = conv.NativeToUnicode(&buf, &bufLeft, &result, &resultLeft);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_ASSERTION(bufLeft == 0, "did not consume entire input buffer");
|
2014-05-14 20:15:46 +00:00
|
|
|
aOutput.SetLength(inputLen - resultLeft);
|
2014-05-05 17:30:43 +00:00
|
|
|
}
|
|
|
|
return rv;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
NS_CopyUnicodeToNative(const nsAString& aInput, nsACString& aOutput)
|
2002-06-10 18:50:11 +00:00
|
|
|
{
|
2014-05-14 20:15:46 +00:00
|
|
|
aOutput.Truncate();
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
nsAString::const_iterator iter, end;
|
2014-05-14 20:15:46 +00:00
|
|
|
aInput.BeginReading(iter);
|
|
|
|
aInput.EndReading(end);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// cannot easily avoid intermediate buffer copy.
|
|
|
|
char temp[4096];
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
nsNativeCharsetConverter conv;
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
const char16_t* buf = iter.get();
|
2014-05-05 17:30:43 +00:00
|
|
|
uint32_t bufLeft = Distance(iter, end);
|
|
|
|
while (bufLeft) {
|
2014-05-14 20:15:46 +00:00
|
|
|
char* p = temp;
|
2014-05-05 17:30:43 +00:00
|
|
|
uint32_t tempLeft = sizeof(temp);
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
nsresult rv = conv.UnicodeToNative(&buf, &bufLeft, &p, &tempLeft);
|
2014-05-14 20:15:46 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2004-02-19 02:44:03 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
if (tempLeft < sizeof(temp)) {
|
|
|
|
aOutput.Append(temp, sizeof(temp) - tempLeft);
|
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2006-03-29 04:53:21 +00:00
|
|
|
NS_IsNativeUTF8()
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
return nsNativeCharsetConverter::IsNativeUTF8();
|
2006-03-29 04:53:21 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 18:50:11 +00:00
|
|
|
void
|
|
|
|
NS_StartupNativeCharsetUtils()
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
//
|
|
|
|
// need to initialize the locale or else charset conversion will fail.
|
|
|
|
// better not delay this in case some other component alters the locale
|
|
|
|
// settings.
|
|
|
|
//
|
|
|
|
// XXX we assume that we are called early enough that we should
|
|
|
|
// always be the first to care about the locale's charset.
|
|
|
|
//
|
|
|
|
setlocale(LC_CTYPE, "");
|
2002-06-10 18:50:11 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
nsNativeCharsetConverter::GlobalInit();
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_ShutdownNativeCharsetUtils()
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
nsNativeCharsetConverter::GlobalShutdown();
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
2002-10-06 00:29:46 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// XP_WIN
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
|
|
|
|
#include <windows.h>
|
2014-02-06 16:42:58 +00:00
|
|
|
#include "nsString.h"
|
2002-10-06 00:29:46 +00:00
|
|
|
#include "nsAString.h"
|
2006-06-22 19:07:30 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2013-05-07 15:52:52 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
NS_CopyNativeToUnicode(const nsACString& aInput, nsAString& aOutput)
|
2002-10-06 00:29:46 +00:00
|
|
|
{
|
2014-05-14 20:15:46 +00:00
|
|
|
uint32_t inputLen = aInput.Length();
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
nsACString::const_iterator iter;
|
2014-05-14 20:15:46 +00:00
|
|
|
aInput.BeginReading(iter);
|
2004-02-19 02:44:03 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
const char* buf = iter.get();
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// determine length of result
|
|
|
|
uint32_t resultLen = 0;
|
|
|
|
int n = ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, nullptr, 0);
|
2014-05-14 20:15:46 +00:00
|
|
|
if (n > 0) {
|
2014-05-05 17:30:43 +00:00
|
|
|
resultLen += n;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// allocate sufficient space
|
2015-01-28 09:00:40 +00:00
|
|
|
if (!aOutput.SetLength(resultLen, fallible)) {
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
if (resultLen > 0) {
|
|
|
|
nsAString::iterator out_iter;
|
2014-05-14 20:15:46 +00:00
|
|
|
aOutput.BeginWriting(out_iter);
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
char16_t* result = out_iter.get();
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, wwc(result), resultLen);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2002-10-06 00:29:46 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
NS_CopyUnicodeToNative(const nsAString& aInput, nsACString& aOutput)
|
2002-10-06 00:29:46 +00:00
|
|
|
{
|
2014-05-14 20:15:46 +00:00
|
|
|
uint32_t inputLen = aInput.Length();
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
nsAString::const_iterator iter;
|
2014-05-14 20:15:46 +00:00
|
|
|
aInput.BeginReading(iter);
|
2004-02-19 02:44:03 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
char16ptr_t buf = iter.get();
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// determine length of result
|
|
|
|
uint32_t resultLen = 0;
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
int n = ::WideCharToMultiByte(CP_ACP, 0, buf, inputLen, nullptr, 0,
|
|
|
|
nullptr, nullptr);
|
2014-05-14 20:15:46 +00:00
|
|
|
if (n > 0) {
|
2014-05-05 17:30:43 +00:00
|
|
|
resultLen += n;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// allocate sufficient space
|
2015-01-28 09:00:40 +00:00
|
|
|
if (!aOutput.SetLength(resultLen, fallible)) {
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
if (resultLen > 0) {
|
|
|
|
nsACString::iterator out_iter;
|
2014-05-14 20:15:46 +00:00
|
|
|
aOutput.BeginWriting(out_iter);
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// default "defaultChar" is '?', which is an illegal character on windows
|
|
|
|
// file system. That will cause file uncreatable. Change it to '_'
|
|
|
|
const char defaultChar = '_';
|
2002-10-06 00:29:46 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
char* result = out_iter.get();
|
2004-02-19 02:44:03 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
::WideCharToMultiByte(CP_ACP, 0, buf, inputLen, result, resultLen,
|
|
|
|
&defaultChar, nullptr);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2002-10-06 00:29:46 +00:00
|
|
|
}
|
|
|
|
|
2012-01-04 03:09:29 +00:00
|
|
|
// moved from widget/windows/nsToolkit.cpp
|
2014-05-05 17:30:43 +00:00
|
|
|
int32_t
|
2014-05-14 20:15:46 +00:00
|
|
|
NS_ConvertAtoW(const char* aStrInA, int aBufferSize, char16_t* aStrOutW)
|
2002-10-06 00:29:46 +00:00
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
return MultiByteToWideChar(CP_ACP, 0, aStrInA, -1, wwc(aStrOutW), aBufferSize);
|
2002-10-06 00:29:46 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
int32_t
|
2014-05-14 20:15:46 +00:00
|
|
|
NS_ConvertWtoA(const char16_t* aStrInW, int aBufferSizeOut,
|
|
|
|
char* aStrOutA, const char* aDefault)
|
2002-10-06 00:29:46 +00:00
|
|
|
{
|
2014-05-14 20:15:46 +00:00
|
|
|
if ((!aStrInW) || (!aStrOutA) || (aBufferSizeOut <= 0)) {
|
2014-05-05 17:30:43 +00:00
|
|
|
return 0;
|
2014-05-14 20:15:46 +00:00
|
|
|
}
|
2006-03-22 04:26:30 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
int numCharsConverted = WideCharToMultiByte(CP_ACP, 0, char16ptr_t(aStrInW), -1,
|
|
|
|
aStrOutA, aBufferSizeOut,
|
|
|
|
aDefault, nullptr);
|
2006-03-22 04:26:30 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
if (!numCharsConverted) {
|
|
|
|
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
// Overflow, add missing null termination but return 0
|
2014-05-14 20:15:46 +00:00
|
|
|
aStrOutA[aBufferSizeOut - 1] = '\0';
|
|
|
|
} else {
|
2014-05-05 17:30:43 +00:00
|
|
|
// Other error, clear string and return 0
|
|
|
|
aStrOutA[0] = '\0';
|
2006-03-22 04:26:30 +00:00
|
|
|
}
|
2014-05-14 20:15:46 +00:00
|
|
|
} else if (numCharsConverted < aBufferSizeOut) {
|
2014-05-05 17:30:43 +00:00
|
|
|
// Add 2nd null (really necessary?)
|
|
|
|
aStrOutA[numCharsConverted] = '\0';
|
|
|
|
}
|
2006-03-22 04:26:30 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
return numCharsConverted;
|
2002-10-06 00:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
NS_CopyNativeToUnicode(const nsACString& aInput, nsAString& aOutput)
|
2002-10-06 00:29:46 +00:00
|
|
|
{
|
2014-05-14 20:15:46 +00:00
|
|
|
CopyASCIItoUTF16(aInput, aOutput);
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2002-10-06 00:29:46 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
NS_CopyUnicodeToNative(const nsAString& aInput, nsACString& aOutput)
|
2002-10-06 00:29:46 +00:00
|
|
|
{
|
2014-05-14 20:15:46 +00:00
|
|
|
LossyCopyUTF16toASCII(aInput, aOutput);
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2002-06-10 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_StartupNativeCharsetUtils()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_ShutdownNativeCharsetUtils()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|