📄 iconv.c
字号:
/*
* Copyright (C) 1999-2002 Free Software Foundation, Inc.
* This file is part of the GNU LIBICONV Library.
*
* The GNU LIBICONV Library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* The GNU LIBICONV Library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the GNU LIBICONV Library; see the file COPYING.LIB.
* If not, write to the Free Software Foundation, Inc., 59 Temple Place -
* Suite 330, Boston, MA 02111-1307, USA.
*/
#define LIB_TINY 1 //For making small Phar Lap library
#if LIB_TINY
#define LIB_TINY_INCLUDE_J 1 //For including SJIS
#define LIB_TINY_INCLUDE_CP932 1 //For including MS Code Page 932 (Japanese)
#define LIB_TINY_INCLUDE_C 0 //For including GB2312-80
#define LIB_TINY_INCLUDE_K 0 //For including KS X 1001:1992
#define LIB_TINY_INCLUDE_B5 0 //For including Big 5 (Taiwan)
#define LIB_TINY_INCLUDE_B5HK 0 //For including Big 5 extensions in Hong Kong
#else
#define LIB_TINY_INCLUDE_J 1 //For including SJIS
#define LIB_TINY_INCLUDE_CP932 1 //For including MS Code Page 932 (Japanese)
#define LIB_TINY_INCLUDE_C 1 //For including GB2312-80
#define LIB_TINY_INCLUDE_K 1 //For including KS X 1001:1992
#define LIB_TINY_INCLUDE_B5 1 //For including Big 5 (Taiwan)
#define LIB_TINY_INCLUDE_B5HK 1 //For including Big 5 extensions in Hong Kong
#endif
#if LIB_TINY_INCLUDE_B5HK && !LIB_TINY_INCLUDE_B5
#error "Can't have big 5 extensions without big 5"
#endif
#define INCLUDE_ANY_JAPANESE (LIB_TINY_INCLUDE_J || LIB_TINY_INCLUDE_CP932)
#define INCLUDE_ANY_CJK (INCLUDE_ANY_JAPANESE || LIB_TINY_INCLUDE_C \
|| LIB_TINY_INCLUDE_K || LIB_TINY_INCLUDE_B5)
#define INCLUDE_ANY_JAPANESE (LIB_TINY_INCLUDE_J || LIB_TINY_INCLUDE_CP932)
#include <iconv.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#if !LIB_TINY
#include "libcharset.h"
#endif //!LIB_TINY
#if ENABLE_EXTRA
/*
* Consider all system dependent encodings, for any system,
* and the extra encodings.
*/
#define USE_AIX
#define USE_OSF1
#define USE_DOS
#define USE_EXTRA
#else
#if !LIB_TINY
/*
* Consider those system dependent encodings that are needed for the
* current system.
*/
#ifdef _AIX
#define USE_AIX
#endif
#ifdef __osf__
#define USE_OSF1
#endif
#if defined(__DJGPP__) || (defined(_WIN32) && (defined(_MSC_VER) || defined(__MINGW32__)))
#define USE_DOS
#endif
#endif
#endif
/*
* Data type for general conversion loop.
*/
struct loop_funcs {
size_t (*loop_convert) (iconv_t icd,
const char* * inbuf, size_t *inbytesleft,
char* * outbuf, size_t *outbytesleft);
size_t (*loop_reset) (iconv_t icd,
char* * outbuf, size_t *outbytesleft);
};
/*
* Converters.
*/
#include "converters.h"
/*
* Transliteration tables.
*/
#if INCLUDE_ANY_CJK
#include "cjk_variants.h"
#endif //INCLUDE_ANY_CJK
#if !LIB_TINY
#include "translit.h"
#endif //!LIB_TINY
/*
* Table of all supported encodings.
*/
struct encoding {
struct mbtowc_funcs ifuncs; /* conversion multibyte -> unicode */
struct wctomb_funcs ofuncs; /* conversion unicode -> multibyte */
int oflags; /* flags for unicode -> multibyte conversion */
};
enum {
#define DEFENCODING(xxx_names,xxx,xxx_ifuncs1,xxx_ifuncs2,xxx_ofuncs1,xxx_ofuncs2) \
ei_##xxx ,
#include "encodings.defn"
#ifdef USE_AIX
#include "encodings_aix.def"
#endif
#ifdef USE_OSF1
#include "encodings_osf1.def"
#endif
#ifdef USE_DOS
#include "encodings_dos.def"
#endif
#ifdef USE_EXTRA
#include "encodings_extra.def"
#endif
#if !LIB_TINY
#include "encodings_local.def"
#endif //!LIB_TINY
#undef DEFENCODING
ei_for_broken_compilers_that_dont_like_trailing_commas
};
#include "flags.h"
static struct encoding const all_encodings[] = {
#define DEFENCODING(xxx_names,xxx,xxx_ifuncs1,xxx_ifuncs2,xxx_ofuncs1,xxx_ofuncs2) \
{ xxx_ifuncs1,xxx_ifuncs2, xxx_ofuncs1,xxx_ofuncs2, ei_##xxx##_oflags },
#include "encodings.defn"
#ifdef USE_AIX
#include "encodings_aix.def"
#endif
#ifdef USE_OSF1
#include "encodings_osf1.def"
#endif
#ifdef USE_DOS
#include "encodings_dos.def"
#endif
#ifdef USE_EXTRA
#include "encodings_extra.def"
#endif
#undef DEFENCODING
#define DEFENCODING(xxx_names,xxx,xxx_ifuncs1,xxx_ifuncs2,xxx_ofuncs1,xxx_ofuncs2) \
{ xxx_ifuncs1,xxx_ifuncs2, xxx_ofuncs1,xxx_ofuncs2, 0 },
#if !LIB_TINY
#include "encodings_local.def"
#endif //!LIB_TINY
#undef DEFENCODING
};
/*
* Conversion loops.
*/
#include "loops.h"
/*
* Alias lookup function.
* Defines
* struct alias { const char* name; unsigned int encoding_index; };
* const struct alias * aliases_lookup (const char *str, unsigned int len);
* #define MAX_WORD_LENGTH ...
*/
#include "aliases.h"
/*
* System dependent alias lookup function.
* Defines
* const struct alias * aliases2_lookup (const char *str);
*/
#if defined(USE_AIX) || defined(USE_OSF1) || defined(USE_DOS) || defined(USE_EXTRA) /* || ... */
static struct alias sysdep_aliases[] = {
#ifdef USE_AIX
#include "aliases_aix.h"
#endif
#ifdef USE_OSF1
#include "aliases_osf1.h"
#endif
#ifdef USE_DOS
#include "aliases_dos.h"
#endif
#ifdef USE_EXTRA
#include "aliases_extra.h"
#endif
};
#ifdef __GNUC__
__inline
#endif
const struct alias *
aliases2_lookup (register const char *str)
{
struct alias * ptr;
unsigned int count;
for (ptr = sysdep_aliases, count = sizeof(sysdep_aliases)/sizeof(sysdep_aliases[0]); count > 0; ptr++, count--)
if (!strcmp(str,ptr->name))
return ptr;
return NULL;
}
#else
#define aliases2_lookup(str) NULL
#endif
#if 0
/* Like !strcasecmp, except that the both strings can be assumed to be ASCII
and the first string can be assumed to be in uppercase. */
static int strequal (const char* str1, const char* str2)
{
unsigned char c1;
unsigned char c2;
for (;;) {
c1 = * (unsigned char *) str1++;
c2 = * (unsigned char *) str2++;
if (c1 == 0)
break;
if (c2 >= 'a' && c2 <= 'z')
c2 -= 'a'-'A';
if (c1 != c2)
break;
}
return (c1 == c2);
}
#endif
iconv_t iconv_open (const char* tocode, const char* fromcode)
{
struct conv_struct * cd;
char buf[MAX_WORD_LENGTH+10+1];
const char* cp;
char* bp;
const struct alias * ap;
unsigned int count;
unsigned int from_index;
int from_wchar;
unsigned int to_index;
int to_wchar;
int transliterate = 0;
int discard_ilseq = 0;
/* Before calling aliases_lookup, convert the input string to upper case,
* and check whether it's entirely ASCII (we call gperf with option "-7"
* to achieve a smaller table) and non-empty. If it's not entirely ASCII,
* or if it's too long, it is not a valid encoding name.
*/
for (to_wchar = 0;;) {
/* Search tocode in the table. */
for (cp = tocode, bp = buf, count = MAX_WORD_LENGTH+10+1; ; cp++, bp++) {
unsigned char c = * (unsigned char *) cp;
if (c >= 0x80)
goto invalid;
if (c >= 'a' && c <= 'z')
c -= 'a'-'A';
*bp = c;
if (c == '\0')
break;
if (--count == 0)
goto invalid;
}
if (bp-buf >= 10 && memcmp(bp-10,"//TRANSLIT",10)==0) {
bp -= 10;
*bp = '\0';
transliterate = 1;
}
if (bp-buf >= 8 && memcmp(bp-8,"//IGNORE",8)==0) {
bp -= 8;
*bp = '\0';
discard_ilseq = 1;
}
if (buf[0] == '\0') {
#if !LIB_TINY
tocode = locale_charset();
/* Avoid an endless loop that could occur when using an older version
of localcharset.c. */
if (tocode[0] == '\0')
#endif //!LIB_TINY
goto invalid;
#if !LIB_TINY
continue;
#endif //!LIB_TINY
}
ap = aliases_lookup(buf,bp-buf);
if (ap == NULL) {
ap = aliases2_lookup(buf);
if (ap == NULL)
goto invalid;
}
#if !LIB_TINY
if (ap->encoding_index == ei_local_char) {
tocode = locale_charset();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -