Hash :
58849cb5
Author :
Date :
2000-12-01T20:10:31
Define the version number in the library as well.
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
/* Copyright (C) 1999-2000 Free Software Foundation, Inc.
This file is part of the GNU ICONV Library.
The GNU ICONV 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 ICONV 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 ICONV 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. */
#include <iconv.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "libcharset.h"
/*
* Consider those system dependent encodings that are needed for the
* current system.
*/
#ifdef _AIX
#define USE_AIX
#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.
*/
#include "cjk_variants.h"
#include "translit.h"
/*
* 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_ifuncs,xxx_ofuncs1,xxx_ofuncs2) \
ei_##xxx ,
#include "encodings.def"
#ifdef USE_AIX
#include "encodings_aix.def"
#endif
#include "encodings_local.def"
#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_ifuncs,xxx_ofuncs1,xxx_ofuncs2) \
{ xxx_ifuncs, xxx_ofuncs1,xxx_ofuncs2, ei_##xxx##_oflags },
#include "encodings.def"
#ifdef USE_AIX
#include "encodings_aix.def"
#endif
#undef DEFENCODING
#define DEFENCODING(xxx_names,xxx,xxx_ifuncs,xxx_ofuncs1,xxx_ofuncs2) \
{ xxx_ifuncs, xxx_ofuncs1,xxx_ofuncs2, 0 },
#include "encodings_local.def"
#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) /* || ... */
static struct alias sysdep_aliases[] = {
#ifdef USE_AIX
#include "aliases_aix.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;
/* 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;
}
ap = aliases_lookup(buf,bp-buf);
if (ap == NULL) {
ap = aliases2_lookup(buf);
if (ap == NULL)
goto invalid;
}
if (ap->encoding_index == ei_local_char) {
tocode = locale_charset();
if (tocode != NULL)
continue;
goto invalid;
}
if (ap->encoding_index == ei_local_wchar_t) {
#if __STDC_ISO_10646__
if (sizeof(wchar_t) == 4) {
to_index = ei_ucs4internal;
break;
}
if (sizeof(wchar_t) == 2) {
to_index = ei_ucs2internal;
break;
}
if (sizeof(wchar_t) == 1) {
to_index = ei_iso8859_1;
break;
}
#endif
#if HAVE_MBRTOWC
to_wchar = 1;
tocode = locale_charset();
if (tocode != NULL)
continue;
#endif
goto invalid;
}
to_index = ap->encoding_index;
break;
}
for (from_wchar = 0;;) {
/* Search fromcode in the table. */
for (cp = fromcode, 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';
}
ap = aliases_lookup(buf,bp-buf);
if (ap == NULL) {
ap = aliases2_lookup(buf);
if (ap == NULL)
goto invalid;
}
if (ap->encoding_index == ei_local_char) {
fromcode = locale_charset();
if (fromcode != NULL)
continue;
goto invalid;
}
if (ap->encoding_index == ei_local_wchar_t) {
#if __STDC_ISO_10646__
if (sizeof(wchar_t) == 4) {
from_index = ei_ucs4internal;
break;
}
if (sizeof(wchar_t) == 2) {
from_index = ei_ucs2internal;
break;
}
if (sizeof(wchar_t) == 1) {
from_index = ei_iso8859_1;
break;
}
#endif
#if HAVE_WCRTOMB
from_wchar = 1;
fromcode = locale_charset();
if (fromcode != NULL)
continue;
#endif
goto invalid;
}
from_index = ap->encoding_index;
break;
}
cd = (struct conv_struct *) malloc(from_wchar != to_wchar
? sizeof(struct wchar_conv_struct)
: sizeof(struct conv_struct));
if (cd == NULL) {
errno = ENOMEM;
return (iconv_t)(-1);
}
cd->iindex = from_index;
cd->ifuncs = all_encodings[from_index].ifuncs;
cd->oindex = to_index;
cd->ofuncs = all_encodings[to_index].ofuncs;
cd->oflags = all_encodings[to_index].oflags;
/* Initialize the loop functions. */
#if HAVE_MBRTOWC
if (to_wchar) {
#if HAVE_WCRTOMB
if (from_wchar) {
cd->lfuncs.loop_convert = wchar_id_loop_convert;
cd->lfuncs.loop_reset = wchar_id_loop_reset;
} else
#endif
{
cd->lfuncs.loop_convert = wchar_to_loop_convert;
cd->lfuncs.loop_reset = wchar_to_loop_reset;
}
} else
#endif
{
#if HAVE_WCRTOMB
if (from_wchar) {
cd->lfuncs.loop_convert = wchar_from_loop_convert;
cd->lfuncs.loop_reset = wchar_from_loop_reset;
} else
#endif
{
cd->lfuncs.loop_convert = unicode_loop_convert;
cd->lfuncs.loop_reset = unicode_loop_reset;
}
}
/* Initialize the states. */
memset(&cd->istate,'\0',sizeof(state_t));
memset(&cd->ostate,'\0',sizeof(state_t));
/* Initialize the operation flags. */
cd->transliterate = transliterate;
/* Initialize additional fields. */
if (from_wchar != to_wchar) {
struct wchar_conv_struct * wcd = (struct wchar_conv_struct *) cd;
memset(&wcd->state,'\0',sizeof(mbstate_t));
}
/* Done. */
return (iconv_t)cd;
invalid:
errno = EINVAL;
return (iconv_t)(-1);
}
size_t iconv (iconv_t icd,
ICONV_CONST char* * inbuf, size_t *inbytesleft,
char* * outbuf, size_t *outbytesleft)
{
conv_t cd = (conv_t) icd;
if (inbuf == NULL || *inbuf == NULL)
return cd->lfuncs.loop_reset(icd,outbuf,outbytesleft);
else
return cd->lfuncs.loop_convert(icd,
(const char* *)inbuf,inbytesleft,
outbuf,outbytesleft);
}
int iconv_close (iconv_t icd)
{
conv_t cd = (conv_t) icd;
free(cd);
return 0;
}
#ifndef LIBICONV_PLUG
int iconvctl (iconv_t icd, int request, void* argument)
{
conv_t cd = (conv_t) icd;
switch (request) {
case ICONV_TRIVIALP:
*(int *)argument =
((cd->lfuncs.loop_convert == unicode_loop_convert
&& cd->iindex == cd->oindex)
|| cd->lfuncs.loop_convert == wchar_id_loop_convert
? 1 : 0);
return 0;
case ICONV_GET_TRANSLITERATE:
*(int *)argument = cd->transliterate;
return 0;
case ICONV_SET_TRANSLITERATE:
cd->transliterate = (*(const int *)argument ? 1 : 0);
return 0;
default:
errno = EINVAL;
return -1;
}
}
int _libiconv_version = _LIBICONV_VERSION;
#endif