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
|
/* iso8859.c - Convert ISO8859-* to/from Unicode.
Copyright (C) 1999 Tom Tromey
The Gnome 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 Gnome 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 Gnome 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 <config.h>
#include <stdlib.h>
#include "unicode.h"
#include "convert.h"
#include "iso/iso8859-2.h"
#include "iso/iso8859-3.h"
#include "iso/iso8859-4.h"
#include "iso/iso8859-5.h"
#include "iso/iso8859-6.h"
#include "iso/iso8859-7.h"
#include "iso/iso8859-8.h"
#include "iso/iso8859-9.h"
#include "iso/iso8859-10.h"
#include "iso/iso8859-14.h"
#include "iso/iso8859-15.h"
#include "iso/windows-1252.h"
#include "iso/koi8-r.h"
#include "iso/koi8-u.h"
#include "iso/tis620.2533-1.h"
#include "iso/armscii-8.h"
#include "iso/georgian-academy.h"
#include "iso/georgian-ps.h"
static int
iso8859_2_init (void **privp)
{
*privp = (void *) iso8859_2_table;
return 1;
}
static int
iso8859_3_init (void **privp)
{
*privp = (void *) iso8859_3_table;
return 1;
}
static int
iso8859_4_init (void **privp)
{
*privp = (void *) iso8859_4_table;
return 1;
}
static int
iso8859_5_init (void **privp)
{
*privp = (void *) iso8859_5_table;
return 1;
}
static int
iso8859_6_init (void **privp)
{
*privp = (void *) iso8859_6_table;
return 1;
}
static int
iso8859_7_init (void **privp)
{
*privp = (void *) iso8859_7_table;
return 1;
}
static int
iso8859_8_init (void **privp)
{
*privp = (void *) iso8859_8_table;
return 1;
}
static int
iso8859_9_init (void **privp)
{
*privp = (void *) iso8859_9_table;
return 1;
}
static int
iso8859_10_init (void **privp)
{
*privp = (void *) iso8859_10_table;
return 1;
}
static int
iso8859_14_init (void **privp)
{
*privp = (void *) iso8859_14_table;
return 1;
}
static int
iso8859_15_init (void **privp)
{
*privp = (void *) iso8859_15_table;
return 1;
}
static int
windows_1252_init (void **privp)
{
*privp = (void *) windows_1252_table;
return 1;
}
static int
koi8_r_init (void **privp)
{
*privp = (void *) koi8_r_table;
return 1;
}
static int
koi8_u_init (void **privp)
{
*privp = (void *) koi8_u_table;
return 1;
}
static int
tis_620_init (void **privp)
{
*privp = (void *) tis_620_table;
return 1;
}
static int
armscii_8_init (void **privp)
{
*privp = (void *) armscii_8_table;
return 1;
}
static int
georgian_academy_init (void **privp)
{
*privp = (void *) georgian_academy_table;
return 1;
}
static int
georgian_ps_init (void **privp)
{
*privp = (void *) georgian_ps_table;
return 1;
}
static enum unicode_read_result
iso8859_read (void *arg, const char **inbuf, size_t *inbytesleft,
unicode_char_t **outbuf, size_t *outcharsleft)
{
while (*inbytesleft > 0 && *outcharsleft > 0)
{
unsigned char c = (unsigned char) **inbuf;
unsigned short *xlate = (unsigned short *) arg;
++*inbuf;
--*inbytesleft;
/* Special case the digits in 8859-6. */
if (xlate == iso8859_6_table
&& c >= 0x30
&& c <= 0x39)
{
**outbuf = ISO6_DIGIT_ZERO + c - 0x30;
}
else if (c < 0x80)
**outbuf = c;
else
**outbuf = (unicode_char_t) xlate[c - 0x80];
++*outbuf;
--*outcharsleft;
}
return unicode_read_ok;
}
static enum unicode_write_result
iso8859_write (void *arg,
unicode_char_t **inbuf, size_t *incharsleft,
char **outbuf, size_t *outbytesleft)
{
if (*outbytesleft <= 0)
return unicode_write_more_room;
while (*outbytesleft > 0 && *incharsleft > 0)
{
unicode_char_t ch = **inbuf;
++*inbuf;
--*incharsleft;
if (ch > 0xffff)
{
/* Can't appear in table. */
ch = '?';
}
else if (ch >= 0x80)
{
unsigned short *xlate = (unsigned short *) arg;
/* Special case the digits in 8859-6. */
if (xlate == iso8859_6_table
&& ch >= ISO6_DIGIT_ZERO
&& ch <= ISO6_DIGIT_ZERO + 9)
ch -= ISO6_DIGIT_ZERO - 0x30;
/* Special case for old mapping in 8859-7. */
else if (xlate == iso8859_7_table
&& ch == 0x02bd)
ch = 0xa1;
/* Special case for old mapping in 8859-7. */
else if (xlate == iso8859_7_table
&& ch == 0x02bc)
ch = 0xa2;
else
{
int i;
/* Search in table. This is slow, but maintaining a
backwards map is more expensive than it is
worthwhile. */
for (i = 0; i < 128; ++i)
{
if (ch == xlate[i])
{
ch = i + 0x80;
break;
}
}
if (i == 128)
ch = '?';
}
}
**outbuf = (char) (ch & 0xff);
++*outbuf;
--*outbytesleft;
}
return unicode_write_ok;
}
#define P3(One, Two, Three) One ## Two ## Three
#define ISO_DEFINE(Num) \
static char * n ## Num [] = { "8859-" # Num, \
"ISO-8859-" # Num, \
NULL }; \
unicode_encoding_t P3 (unicode_iso8859_, Num, _encoding) = \
{ \
n ## Num, \
P3 (iso8859_, Num, _init), \
NULL, /* Destroy. */ \
NULL, /* Reset. */ \
iso8859_read, \
iso8859_write, \
NULL \
}
ISO_DEFINE (2);
ISO_DEFINE (3);
ISO_DEFINE (4);
ISO_DEFINE (5);
ISO_DEFINE (6);
ISO_DEFINE (7);
ISO_DEFINE (8);
ISO_DEFINE (9);
ISO_DEFINE (10);
ISO_DEFINE (14);
ISO_DEFINE (15);
static char *wn[] = { "cp1252", "WINDOWS-1252", NULL };
unicode_encoding_t unicode_windows_1252_encoding =
{
wn,
windows_1252_init,
NULL, /* Destroy. */
NULL, /* Reset. */
iso8859_read,
iso8859_write,
NULL
};
static char *krn[] = { "koi8-r", NULL };
unicode_encoding_t unicode_koi8_r_encoding =
{
krn,
koi8_r_init,
NULL, /* Destroy. */
NULL, /* Reset. */
iso8859_read,
iso8859_write,
NULL
};
static char *kun[] = { "koi8-u", NULL };
unicode_encoding_t unicode_koi8_u_encoding =
{
kun,
koi8_u_init,
NULL, /* Destroy. */
NULL, /* Reset. */
iso8859_read,
iso8859_write,
NULL
};
static char *tisn[] = { "tis620.2533-1", NULL };
unicode_encoding_t unicode_tis_620_encoding =
{
tisn,
tis_620_init,
NULL, /* Destroy. */
NULL, /* Reset. */
iso8859_read,
iso8859_write,
NULL
};
static char *armn[] = { "armscii-8", NULL };
unicode_encoding_t unicode_armscii_8_encoding =
{
armn,
armscii_8_init,
NULL, /* Destroy. */
NULL, /* Reset. */
iso8859_read,
iso8859_write,
NULL
};
static char *gan[] = { "georgian-academy", NULL };
unicode_encoding_t unicode_georgian_academy_encoding =
{
gan,
georgian_academy_init,
NULL, /* Destroy. */
NULL, /* Reset. */
iso8859_read,
iso8859_write,
NULL
};
static char *gpn[] = { "georgian-ps", NULL };
unicode_encoding_t unicode_georgian_ps_encoding =
{
gpn,
georgian_ps_init,
NULL, /* Destroy. */
NULL, /* Reset. */
iso8859_read,
iso8859_write,
NULL
};
|