]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/transcode.c
Merge fixes from CUPS 1.4svn-r7555.
[thirdparty/cups.git] / cups / transcode.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: transcode.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
4 * Transcoding support for the Common UNIX Printing System (CUPS).
5 *
91c84a35 6 * Copyright 2007-2008 by Apple Inc.
b86bc4cf 7 * Copyright 1997-2007 by Easy Software Products.
ef416fc2 8 *
bc44d920 9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
bc44d920 15 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 16 *
17 * Contents:
18 *
fa73b229 19 * _cupsCharmapFlush() - Flush all character set maps out of cache.
e1d6a774 20 * _cupsCharmapFree() - Free a character set map.
21 * _cupsCharmapGet() - Get a character set map.
ef416fc2 22 * cupsCharsetToUTF8() - Convert legacy character set to UTF-8.
e1d6a774 23 * cupsUTF8ToCharset() - Convert UTF-8 to legacy character set.
ef416fc2 24 * cupsUTF8ToUTF32() - Convert UTF-8 to UTF-32.
25 * cupsUTF32ToUTF8() - Convert UTF-32 to UTF-8.
e1d6a774 26 * compare_wide() - Compare key for wide (VBCS) match.
27 * conv_sbcs_to_utf8() - Convert legacy SBCS to UTF-8.
ef416fc2 28 * conv_utf8_to_sbcs() - Convert UTF-8 to legacy SBCS.
29 * conv_utf8_to_vbcs() - Convert UTF-8 to legacy DBCS/VBCS.
ef416fc2 30 * conv_vbcs_to_utf8() - Convert legacy DBCS/VBCS to UTF-8.
e1d6a774 31 * free_sbcs_charmap() - Free memory used by a single byte character set.
32 * free_vbcs_charmap() - Free memory used by a variable byte character set.
d6ae789d 33 * get_charmap() - Lookup or get a character set map (private).
e1d6a774 34 * get_charmap_count() - Count lines in a charmap file.
35 * get_sbcs_charmap() - Get SBCS Charmap.
36 * get_vbcs_charmap() - Get DBCS/VBCS Charmap.
ef416fc2 37 */
38
39/*
40 * Include necessary headers...
41 */
42
43#include "globals.h"
e1d6a774 44#include "debug.h"
e53920b9 45#include <limits.h>
ef416fc2 46#include <stdlib.h>
47#include <errno.h>
48#include <time.h>
49
50
d6ae789d 51/*
52 * Local globals...
53 */
54
55#ifdef HAVE_PTHREAD_H
56static pthread_mutex_t map_mutex = PTHREAD_MUTEX_INITIALIZER;
57 /* Mutex to control access to maps */
58#endif /* HAVE_PTHREAD_H */
59static _cups_cmap_t *cmap_cache = NULL;
60 /* SBCS Charmap Cache */
61static _cups_vmap_t *vmap_cache = NULL;
62 /* VBCS Charmap Cache */
63
64
ef416fc2 65/*
e1d6a774 66 * Local functions...
ef416fc2 67 */
68
e1d6a774 69static int compare_wide(const void *k1, const void *k2);
70static int conv_sbcs_to_utf8(cups_utf8_t *dest,
71 const cups_sbcs_t *src,
72 int maxout,
73 const cups_encoding_t encoding);
74static int conv_utf8_to_sbcs(cups_sbcs_t *dest,
75 const cups_utf8_t *src,
76 int maxout,
77 const cups_encoding_t encoding);
78static int conv_utf8_to_vbcs(cups_sbcs_t *dest,
79 const cups_utf8_t *src,
80 int maxout,
81 const cups_encoding_t encoding);
82static int conv_vbcs_to_utf8(cups_utf8_t *dest,
83 const cups_sbcs_t *src,
84 int maxout,
85 const cups_encoding_t encoding);
86static void free_sbcs_charmap(_cups_cmap_t *sbcs);
87static void free_vbcs_charmap(_cups_vmap_t *vbcs);
d6ae789d 88static void *get_charmap(const cups_encoding_t encoding);
e1d6a774 89static int get_charmap_count(cups_file_t *fp);
90static _cups_cmap_t *get_sbcs_charmap(const cups_encoding_t encoding,
91 const char *filename);
92static _cups_vmap_t *get_vbcs_charmap(const cups_encoding_t encoding,
93 const char *filename);
94
ef416fc2 95
96/*
e1d6a774 97 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
ef416fc2 98 */
99
e1d6a774 100void
d6ae789d 101_cupsCharmapFlush(void)
ef416fc2 102{
e1d6a774 103 _cups_cmap_t *cmap, /* Legacy SBCS / Unicode Charset Map */
104 *cnext; /* Next Legacy SBCS Charset Map */
105 _cups_vmap_t *vmap, /* Legacy VBCS / Unicode Charset Map */
106 *vnext; /* Next Legacy VBCS Charset Map */
ef416fc2 107
108
d6ae789d 109#ifdef HAVE_PTHREAD_H
110 pthread_mutex_lock(&map_mutex);
111#endif /* HAVE_PTHREAD_H */
112
ef416fc2 113 /*
e1d6a774 114 * Loop through SBCS charset map cache, free all memory...
ef416fc2 115 */
116
d6ae789d 117 for (cmap = cmap_cache; cmap; cmap = cnext)
e1d6a774 118 {
119 cnext = cmap->next;
ef416fc2 120
e1d6a774 121 free_sbcs_charmap(cmap);
122 }
ef416fc2 123
d6ae789d 124 cmap_cache = NULL;
ef416fc2 125
126 /*
e1d6a774 127 * Loop through DBCS/VBCS charset map cache, free all memory...
ef416fc2 128 */
129
d6ae789d 130 for (vmap = vmap_cache; vmap; vmap = vnext)
e1d6a774 131 {
132 vnext = vmap->next;
133
134 free_vbcs_charmap(vmap);
e1d6a774 135 }
136
d6ae789d 137 vmap_cache = NULL;
138
139#ifdef HAVE_PTHREAD_H
140 pthread_mutex_unlock(&map_mutex);
141#endif /* HAVE_PTHREAD_H */
ef416fc2 142}
143
e1d6a774 144
ef416fc2 145/*
e1d6a774 146 * '_cupsCharmapFree()' - Free a character set map.
ef416fc2 147 *
e1d6a774 148 * This does not actually free; use '_cupsCharmapFlush()' for that.
ef416fc2 149 */
e1d6a774 150
ef416fc2 151void
e1d6a774 152_cupsCharmapFree(
153 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 154{
e1d6a774 155 _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
156 _cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */
e1d6a774 157
ef416fc2 158
159 /*
160 * See if we already have this SBCS charset map loaded...
161 */
e1d6a774 162
d6ae789d 163#ifdef HAVE_PTHREAD_H
164 pthread_mutex_lock(&map_mutex);
165#endif /* HAVE_PTHREAD_H */
166
167 for (cmap = cmap_cache; cmap; cmap = cmap->next)
ef416fc2 168 {
169 if (cmap->encoding == encoding)
170 {
171 if (cmap->used > 0)
172 cmap->used --;
d6ae789d 173 break;
ef416fc2 174 }
175 }
176
177 /*
178 * See if we already have this DBCS/VBCS charset map loaded...
179 */
e1d6a774 180
d6ae789d 181 for (vmap = vmap_cache; vmap; vmap = vmap->next)
ef416fc2 182 {
183 if (vmap->encoding == encoding)
184 {
185 if (vmap->used > 0)
186 vmap->used --;
d6ae789d 187 break;
ef416fc2 188 }
189 }
d6ae789d 190
191#ifdef HAVE_PTHREAD_H
192 pthread_mutex_unlock(&map_mutex);
193#endif /* HAVE_PTHREAD_H */
fa73b229 194}
195
196
197/*
e1d6a774 198 * '_cupsCharmapGet()' - Get a character set map.
199 *
200 * This code handles single-byte (SBCS), double-byte (DBCS), and
201 * variable-byte (VBCS) character sets _without_ charset escapes...
202 * This code does not handle multiple-byte character sets (MBCS)
203 * (such as ISO-2022-JP) with charset switching via escapes...
fa73b229 204 */
205
e1d6a774 206void * /* O - Charset map pointer */
207_cupsCharmapGet(
208 const cups_encoding_t encoding) /* I - Encoding */
fa73b229 209{
d6ae789d 210 void *charmap; /* Charset map pointer */
e1d6a774 211
fa73b229 212
e1d6a774 213 DEBUG_printf(("_cupsCharmapGet(encoding=%d)\n", encoding));
ef416fc2 214
215 /*
e1d6a774 216 * Check for valid arguments...
ef416fc2 217 */
e1d6a774 218
219 if (encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END)
ef416fc2 220 {
e1d6a774 221 DEBUG_puts(" Bad encoding, returning NULL!");
222 return (NULL);
ef416fc2 223 }
ef416fc2 224
225 /*
d6ae789d 226 * Lookup or get the charset map pointer and return...
ef416fc2 227 */
e1d6a774 228
d6ae789d 229#ifdef HAVE_PTHREAD_H
230 pthread_mutex_lock(&map_mutex);
231#endif /* HAVE_PTHREAD_H */
e1d6a774 232
d6ae789d 233 charmap = get_charmap(encoding);
e1d6a774 234
d6ae789d 235#ifdef HAVE_PTHREAD_H
236 pthread_mutex_unlock(&map_mutex);
237#endif /* HAVE_PTHREAD_H */
e1d6a774 238
d6ae789d 239 return (charmap);
ef416fc2 240}
241
e1d6a774 242
ef416fc2 243/*
e1d6a774 244 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
ef416fc2 245 *
246 * This code handles single-byte (SBCS), double-byte (DBCS), and
247 * variable-byte (VBCS) character sets _without_ charset escapes...
248 * This code does not handle multiple-byte character sets (MBCS)
249 * (such as ISO-2022-JP) with charset switching via escapes...
250 */
e1d6a774 251
252int /* O - Count or -1 on error */
253cupsCharsetToUTF8(
254 cups_utf8_t *dest, /* O - Target string */
255 const char *src, /* I - Source string */
256 const int maxout, /* I - Max output */
257 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 258{
d6ae789d 259 int bytes; /* Number of bytes converted */
260
261
ef416fc2 262 /*
263 * Check for valid arguments...
264 */
265
e1d6a774 266 DEBUG_printf(("cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)\n",
267 dest, src, maxout, encoding));
268
269 if (dest)
270 *dest = '\0';
271
ef416fc2 272 if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
e1d6a774 273 {
274 DEBUG_puts(" Bad arguments, returning -1");
ef416fc2 275 return (-1);
e1d6a774 276 }
ef416fc2 277
278 /*
279 * Handle identity conversions...
280 */
281
282 if (encoding == CUPS_UTF8 ||
283 encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END)
284 {
e1d6a774 285 strlcpy((char *)dest, src, maxout);
b86bc4cf 286 return ((int)strlen((char *)dest));
ef416fc2 287 }
288
411affcf 289 /*
290 * Handle ISO-8859-1 to UTF-8 directly...
291 */
292
293 if (encoding == CUPS_ISO8859_1)
294 {
295 int ch; /* Character from string */
296 cups_utf8_t *destptr, /* Pointer into UTF-8 buffer */
297 *destend; /* End of UTF-8 buffer */
298
299
300 destptr = dest;
301 destend = dest + maxout - 2;
302
303 while (*src && destptr < destend)
304 {
305 ch = *src++ & 255;
306
307 if (ch & 128)
308 {
309 *destptr++ = 0xc0 | (ch >> 6);
310 *destptr++ = 0x80 | (ch & 0x3f);
311 }
312 else
313 *destptr++ = ch;
314 }
315
316 *destptr = '\0';
317
b86bc4cf 318 return ((int)(destptr - dest));
411affcf 319 }
320
ef416fc2 321 /*
e1d6a774 322 * Convert input legacy charset to UTF-8...
ef416fc2 323 */
e1d6a774 324
d6ae789d 325#ifdef HAVE_PTHREAD_H
326 pthread_mutex_lock(&map_mutex);
327#endif /* HAVE_PTHREAD_H */
328
ef416fc2 329 if (encoding < CUPS_ENCODING_SBCS_END)
d6ae789d 330 bytes = conv_sbcs_to_utf8(dest, (cups_sbcs_t *)src, maxout, encoding);
ef416fc2 331 else
91c84a35 332 bytes = conv_vbcs_to_utf8(dest, (cups_sbcs_t *)src, maxout, encoding);
d6ae789d 333
334#ifdef HAVE_PTHREAD_H
335 pthread_mutex_unlock(&map_mutex);
336#endif /* HAVE_PTHREAD_H */
337
338 return (bytes);
ef416fc2 339}
340
e1d6a774 341
ef416fc2 342/*
e1d6a774 343 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
ef416fc2 344 *
345 * This code handles single-byte (SBCS), double-byte (DBCS), and
346 * variable-byte (VBCS) character sets _without_ charset escapes...
347 * This code does not handle multiple-byte character sets (MBCS)
348 * (such as ISO-2022-JP) with charset switching via escapes...
349 */
e1d6a774 350
351int /* O - Count or -1 on error */
352cupsUTF8ToCharset(
353 char *dest, /* O - Target string */
354 const cups_utf8_t *src, /* I - Source string */
355 const int maxout, /* I - Max output */
356 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 357{
d6ae789d 358 int bytes; /* Number of bytes converted */
359
360
ef416fc2 361 /*
362 * Check for valid arguments...
363 */
364
365 if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
e1d6a774 366 {
367 if (dest)
368 *dest = '\0';
369
ef416fc2 370 return (-1);
e1d6a774 371 }
ef416fc2 372
373 /*
374 * Handle identity conversions...
375 */
376
377 if (encoding == CUPS_UTF8 ||
378 encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END)
379 {
e1d6a774 380 strlcpy(dest, (char *)src, maxout);
b86bc4cf 381 return ((int)strlen(dest));
ef416fc2 382 }
383
411affcf 384 /*
385 * Handle UTF-8 to ISO-8859-1 directly...
386 */
387
388 if (encoding == CUPS_ISO8859_1)
389 {
390 int ch; /* Character from string */
391 char *destptr, /* Pointer into ISO-8859-1 buffer */
392 *destend; /* End of ISO-8859-1 buffer */
393
394
395 destptr = dest;
396 destend = dest + maxout - 1;
397
398 while (*src && destptr < destend)
399 {
400 ch = *src++;
401
402 if ((ch & 0xe0) == 0xc0)
403 {
404 ch = ((ch & 0x1f) << 6) | (*src++ & 0x3f);
405
406 if (ch < 256)
407 *destptr++ = ch;
408 else
409 *destptr++ = '?';
410 }
411 else if ((ch & 0xf0) == 0xe0 ||
412 (ch & 0xf8) == 0xf0)
413 *destptr++ = '?';
414 else if (!(ch & 0x80))
415 *destptr++ = ch;
416 }
417
418 *destptr = '\0';
419
b86bc4cf 420 return ((int)(destptr - dest));
411affcf 421 }
422
ef416fc2 423 /*
e1d6a774 424 * Convert input UTF-8 to legacy charset...
ef416fc2 425 */
e1d6a774 426
d6ae789d 427#ifdef HAVE_PTHREAD_H
428 pthread_mutex_lock(&map_mutex);
429#endif /* HAVE_PTHREAD_H */
430
ef416fc2 431 if (encoding < CUPS_ENCODING_SBCS_END)
d6ae789d 432 bytes = conv_utf8_to_sbcs((cups_sbcs_t *)dest, src, maxout, encoding);
ef416fc2 433 else
91c84a35 434 bytes = conv_utf8_to_vbcs((cups_sbcs_t *)dest, src, maxout, encoding);
d6ae789d 435
436#ifdef HAVE_PTHREAD_H
437 pthread_mutex_unlock(&map_mutex);
438#endif /* HAVE_PTHREAD_H */
439
440 return (bytes);
ef416fc2 441}
442
ef416fc2 443
444/*
445 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
446 *
447 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
448 *
449 * UTF-32 char UTF-8 char(s)
450 * --------------------------------------------------
e1d6a774 451 * 0 to 127 = 0xxxxxxx (US-ASCII)
ef416fc2 452 * 128 to 2047 = 110xxxxx 10yyyyyy
453 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
e1d6a774 454 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
ef416fc2 455 *
456 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
457 * which would convert to five- or six-octet UTF-8 sequences...
ef416fc2 458 */
e1d6a774 459
460int /* O - Count or -1 on error */
461cupsUTF8ToUTF32(
462 cups_utf32_t *dest, /* O - Target string */
463 const cups_utf8_t *src, /* I - Source string */
464 const int maxout) /* I - Max output */
ef416fc2 465{
e1d6a774 466 int i; /* Looping variable */
467 cups_utf8_t ch; /* Character value */
468 cups_utf8_t next; /* Next character value */
469 cups_utf32_t ch32; /* UTF-32 character value */
470
ef416fc2 471
472 /*
473 * Check for valid arguments and clear output...
474 */
e1d6a774 475
476 if (dest)
477 *dest = 0;
478
479 if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
ef416fc2 480 return (-1);
ef416fc2 481
482 /*
483 * Convert input UTF-8 to output UTF-32 (and insert BOM)...
484 */
e1d6a774 485
486 *dest++ = 0xfeff;
e1d6a774 487
488 for (i = maxout - 1; *src && i > 0; i --)
ef416fc2 489 {
e1d6a774 490 ch = *src++;
ef416fc2 491
492 /*
493 * Convert UTF-8 character(s) to UTF-32 character...
494 */
e1d6a774 495
496 if (!(ch & 0x80))
ef416fc2 497 {
498 /*
499 * One-octet UTF-8 <= 127 (US-ASCII)...
500 */
e1d6a774 501
502 *dest++ = ch;
2abf387c 503 continue;
ef416fc2 504 }
505 else if ((ch & 0xe0) == 0xc0)
506 {
507 /*
508 * Two-octet UTF-8 <= 2047 (Latin-x)...
509 */
e1d6a774 510
511 next = *src++;
512 if (!next)
ef416fc2 513 return (-1);
e1d6a774 514
ef416fc2 515 ch32 = ((ch & 0x1f) << 6) | (next & 0x3f);
516
517 /*
518 * Check for non-shortest form (invalid UTF-8)...
519 */
e1d6a774 520
521 if (ch32 < 0x80)
ef416fc2 522 return (-1);
e1d6a774 523
524 *dest++ = ch32;
ef416fc2 525 }
526 else if ((ch & 0xf0) == 0xe0)
527 {
528 /*
529 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
530 */
e1d6a774 531
532 next = *src++;
533 if (!next)
ef416fc2 534 return (-1);
e1d6a774 535
536 ch32 = ((ch & 0x0f) << 6) | (next & 0x3f);
537
538 next = *src++;
539 if (!next)
ef416fc2 540 return (-1);
e1d6a774 541
542 ch32 = (ch32 << 6) | (next & 0x3f);
ef416fc2 543
544 /*
545 * Check for non-shortest form (invalid UTF-8)...
546 */
e1d6a774 547
548 if (ch32 < 0x800)
ef416fc2 549 return (-1);
e1d6a774 550
551 *dest++ = ch32;
ef416fc2 552 }
553 else if ((ch & 0xf8) == 0xf0)
554 {
555 /*
e1d6a774 556 * Four-octet UTF-8...
ef416fc2 557 */
e1d6a774 558
559 next = *src++;
560 if (!next)
ef416fc2 561 return (-1);
e1d6a774 562
563 ch32 = ((ch & 0x07) << 6) | (next & 0x3f);
564
565 next = *src++;
566 if (!next)
567 return (-1);
568
569 ch32 = (ch32 << 6) | (next & 0x3f);
570
571 next = *src++;
572 if (!next)
573 return (-1);
574
575 ch32 = (ch32 << 6) | (next & 0x3f);
576
ef416fc2 577 /*
e1d6a774 578 * Check for non-shortest form (invalid UTF-8)...
ef416fc2 579 */
e1d6a774 580
581 if (ch32 < 0x10000)
582 return (-1);
583
584 *dest++ = ch32;
ef416fc2 585 }
586 else
587 {
588 /*
e1d6a774 589 * More than 4-octet (invalid UTF-8 sequence)...
ef416fc2 590 */
e1d6a774 591
ef416fc2 592 return (-1);
593 }
594
595 /*
596 * Check for UTF-16 surrogate (illegal UTF-8)...
597 */
ef416fc2 598
2abf387c 599 if (ch32 >= 0xd800 && ch32 <= 0xdfff)
ef416fc2 600 return (-1);
601 }
e1d6a774 602
ef416fc2 603 *dest = 0;
e1d6a774 604
ef416fc2 605 return (i);
606}
607
e1d6a774 608
ef416fc2 609/*
610 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
611 *
612 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
613 *
614 * UTF-32 char UTF-8 char(s)
615 * --------------------------------------------------
e1d6a774 616 * 0 to 127 = 0xxxxxxx (US-ASCII)
ef416fc2 617 * 128 to 2047 = 110xxxxx 10yyyyyy
618 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
e1d6a774 619 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
ef416fc2 620 *
621 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
622 * which would convert to five- or six-octet UTF-8 sequences...
ef416fc2 623 */
e1d6a774 624
625int /* O - Count or -1 on error */
626cupsUTF32ToUTF8(
627 cups_utf8_t *dest, /* O - Target string */
628 const cups_utf32_t *src, /* I - Source string */
629 const int maxout) /* I - Max output */
ef416fc2 630{
e1d6a774 631 cups_utf8_t *start; /* Start of destination string */
632 int i; /* Looping variable */
633 int swap; /* Byte-swap input to output */
634 cups_utf32_t ch; /* Character value */
635
ef416fc2 636
637 /*
638 * Check for valid arguments and clear output...
639 */
e1d6a774 640
641 if (dest)
642 *dest = '\0';
643
644 if (!dest || !src || maxout < 1)
ef416fc2 645 return (-1);
ef416fc2 646
647 /*
648 * Check for leading BOM in UTF-32 and inverted BOM...
649 */
e1d6a774 650
651 start = dest;
652 swap = *src == 0xfffe0000;
653
654 if (*src == 0xfffe0000 || *src == 0xfeff)
655 src ++;
ef416fc2 656
657 /*
658 * Convert input UTF-32 to output UTF-8...
659 */
e1d6a774 660
661 for (i = maxout - 1; *src && i > 0;)
ef416fc2 662 {
e1d6a774 663 ch = *src++;
ef416fc2 664
665 /*
666 * Byte swap input UTF-32, if necessary...
e1d6a774 667 * (only byte-swapping 24 of 32 bits)
ef416fc2 668 */
e1d6a774 669
ef416fc2 670 if (swap)
671 ch = ((ch >> 24) | ((ch >> 8) & 0xff00) | ((ch << 8) & 0xff0000));
672
673 /*
e1d6a774 674 * Check for beyond Plane 16 (invalid UTF-32)...
ef416fc2 675 */
ef416fc2 676
ef416fc2 677 if (ch > 0x10ffff)
678 return (-1);
679
ef416fc2 680 /*
681 * Convert UTF-32 character to UTF-8 character(s)...
682 */
e1d6a774 683
684 if (ch < 0x80)
ef416fc2 685 {
686 /*
687 * One-octet UTF-8 <= 127 (US-ASCII)...
688 */
e1d6a774 689
690 *dest++ = (cups_utf8_t)ch;
691 i --;
ef416fc2 692 }
e1d6a774 693 else if (ch < 0x800)
ef416fc2 694 {
695 /*
696 * Two-octet UTF-8 <= 2047 (Latin-x)...
697 */
e1d6a774 698
699 if (i < 2)
700 return (-1);
701
702 *dest++ = (cups_utf8_t)(0xc0 | ((ch >> 6) & 0x1f));
703 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
704 i -= 2;
ef416fc2 705 }
e1d6a774 706 else if (ch < 0x10000)
ef416fc2 707 {
708 /*
709 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
710 */
e1d6a774 711
712 if (i < 3)
713 return (-1);
714
715 *dest++ = (cups_utf8_t)(0xe0 | ((ch >> 12) & 0x0f));
716 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
717 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
718 i -= 3;
719 }
720 else
721 {
722 /*
723 * Four-octet UTF-8...
724 */
725
726 if (i < 4)
727 return (-1);
728
729 *dest++ = (cups_utf8_t)(0xf0 | ((ch >> 18) & 0x07));
730 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 12) & 0x3f));
731 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
732 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
733 i -= 4;
ef416fc2 734 }
735 }
e1d6a774 736
ef416fc2 737 *dest = '\0';
e1d6a774 738
739 return ((int)(dest - start));
ef416fc2 740}
741
e1d6a774 742
ef416fc2 743/*
e1d6a774 744 * 'compare_wide()' - Compare key for wide (VBCS) match.
745 */
746
747static int
748compare_wide(const void *k1, /* I - Key char */
749 const void *k2) /* I - Map char */
750{
751 cups_vbcs_t key; /* Legacy key character */
752 cups_vbcs_t map; /* Legacy map character */
753
754
755 key = *((cups_vbcs_t *)k1);
756 map = ((_cups_wide2uni_t *)k2)->widechar;
757
758 return ((int)(key - map));
759}
760
761
762/*
763 * 'conv_sbcs_to_utf8()' - Convert legacy SBCS to UTF-8.
ef416fc2 764 */
e1d6a774 765
766static int /* O - Count or -1 on error */
767conv_sbcs_to_utf8(
768 cups_utf8_t *dest, /* O - Target string */
769 const cups_sbcs_t *src, /* I - Source string */
770 int maxout, /* I - Max output */
771 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 772{
e1d6a774 773 _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
774 cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
775 cups_sbcs_t legchar; /* Legacy character value */
776 cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
777 *workptr; /* Pointer into string */
778
ef416fc2 779
780 /*
e1d6a774 781 * Find legacy charset map in cache...
ef416fc2 782 */
e1d6a774 783
d6ae789d 784 if ((cmap = (_cups_cmap_t *)get_charmap(encoding)) == NULL)
ef416fc2 785 return (-1);
ef416fc2 786
787 /*
e1d6a774 788 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
ef416fc2 789 */
ef416fc2 790
e1d6a774 791 work[0] = 0xfeff;
792 for (workptr = work + 1; *src && workptr < (work + CUPS_MAX_USTRING - 1);)
ef416fc2 793 {
e1d6a774 794 legchar = *src++;
ef416fc2 795
796 /*
e1d6a774 797 * Convert ASCII verbatim (optimization)...
ef416fc2 798 */
ef416fc2 799
e1d6a774 800 if (legchar < 0x80)
801 *workptr++ = (cups_utf32_t)legchar;
802 else
ef416fc2 803 {
e1d6a774 804 /*
805 * Convert unknown character to Replacement Character...
806 */
ef416fc2 807
e1d6a774 808 crow = cmap->char2uni + legchar;
809
810 if (!*crow)
811 *workptr++ = 0xfffd;
812 else
813 *workptr++ = (cups_utf32_t)*crow;
ef416fc2 814 }
ef416fc2 815 }
e1d6a774 816
817 *workptr = 0;
818
819 /*
820 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
821 */
822
d6ae789d 823 cmap->used --;
e1d6a774 824
825 return (cupsUTF32ToUTF8(dest, work, maxout));
ef416fc2 826}
827
e1d6a774 828
ef416fc2 829/*
e1d6a774 830 * 'conv_utf8_to_sbcs()' - Convert UTF-8 to legacy SBCS.
ef416fc2 831 */
e1d6a774 832
833static int /* O - Count or -1 on error */
834conv_utf8_to_sbcs(
835 cups_sbcs_t *dest, /* O - Target string */
836 const cups_utf8_t *src, /* I - Source string */
837 int maxout, /* I - Max output */
838 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 839{
e1d6a774 840 cups_sbcs_t *start; /* Start of destination string */
841 _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
842 cups_sbcs_t *srow; /* Pointer to SBCS row in 'uni2char' */
843 cups_utf32_t unichar; /* Character value */
844 cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
845 *workptr; /* Pointer into string */
846
ef416fc2 847
848 /*
e1d6a774 849 * Find legacy charset map in cache...
ef416fc2 850 */
e1d6a774 851
d6ae789d 852 if ((cmap = (_cups_cmap_t *)get_charmap(encoding)) == NULL)
ef416fc2 853 return (-1);
ef416fc2 854
855 /*
e1d6a774 856 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
ef416fc2 857 */
e1d6a774 858
859 if (cupsUTF8ToUTF32(work, src, CUPS_MAX_USTRING) < 0)
860 return (-1);
ef416fc2 861
862 /*
e1d6a774 863 * Convert internal UCS-4 to SBCS legacy charset (and delete BOM)...
ef416fc2 864 */
e1d6a774 865
866 for (workptr = work + 1, start = dest; *workptr && maxout > 1; maxout --)
ef416fc2 867 {
e1d6a774 868 unichar = *workptr++;
869 if (!unichar)
ef416fc2 870 break;
ef416fc2 871
872 /*
e1d6a774 873 * Convert ASCII verbatim (optimization)...
ef416fc2 874 */
ef416fc2 875
e1d6a774 876 if (unichar < 0x80)
877 {
878 *dest++ = (cups_sbcs_t)unichar;
879 continue;
880 }
ef416fc2 881
882 /*
e1d6a774 883 * Convert unknown character to visible replacement...
ef416fc2 884 */
ef416fc2 885
e1d6a774 886 srow = cmap->uni2char[(int)((unichar >> 8) & 0xff)];
ef416fc2 887
e1d6a774 888 if (srow)
889 srow += (int)(unichar & 0xff);
ef416fc2 890
e1d6a774 891 if (!srow || !*srow)
892 *dest++ = '?';
893 else
894 *dest++ = *srow;
ef416fc2 895 }
ef416fc2 896
e1d6a774 897 *dest = '\0';
898
d6ae789d 899 cmap->used --;
e1d6a774 900
901 return ((int)(dest - start));
ef416fc2 902}
903
e1d6a774 904
ef416fc2 905/*
e1d6a774 906 * 'conv_utf8_to_vbcs()' - Convert UTF-8 to legacy DBCS/VBCS.
ef416fc2 907 */
e1d6a774 908
909static int /* O - Count or -1 on error */
910conv_utf8_to_vbcs(
911 cups_sbcs_t *dest, /* O - Target string */
912 const cups_utf8_t *src, /* I - Source string */
913 int maxout, /* I - Max output */
914 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 915{
e1d6a774 916 cups_sbcs_t *start; /* Start of destination string */
917 _cups_vmap_t *vmap; /* Legacy DBCS / Unicode Charset Map */
918 cups_vbcs_t *vrow; /* Pointer to VBCS row in 'uni2char' */
919 cups_utf32_t unichar; /* Character value */
920 cups_vbcs_t legchar; /* Legacy character value */
921 cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
922 *workptr; /* Pointer into string */
ef416fc2 923
ef416fc2 924
925 /*
e1d6a774 926 * Find legacy charset map in cache...
ef416fc2 927 */
ef416fc2 928
d6ae789d 929 if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL)
e1d6a774 930 return (-1);
ef416fc2 931
932 /*
e1d6a774 933 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
ef416fc2 934 */
e1d6a774 935
936 if (cupsUTF8ToUTF32(work, src, CUPS_MAX_USTRING) < 0)
937 return (-1);
ef416fc2 938
939 /*
e1d6a774 940 * Convert internal UCS-4 to VBCS legacy charset (and delete BOM)...
ef416fc2 941 */
e1d6a774 942
943 for (start = dest, workptr = work + 1; *workptr && maxout > 1; maxout --)
ef416fc2 944 {
e1d6a774 945 unichar = *workptr++;
946 if (!unichar)
ef416fc2 947 break;
ef416fc2 948
949 /*
e1d6a774 950 * Convert ASCII verbatim (optimization)...
ef416fc2 951 */
e1d6a774 952
953 if (unichar < 0x80)
954 {
b86bc4cf 955 *dest++ = (cups_sbcs_t)unichar;
e1d6a774 956 continue;
957 }
ef416fc2 958
959 /*
e1d6a774 960 * Convert unknown character to visible replacement...
ef416fc2 961 */
e1d6a774 962
963 vrow = vmap->uni2char[(int)((unichar >> 8) & 0xff)];
964
965 if (vrow)
966 vrow += (int)(unichar & 0xff);
967
968 if (!vrow || !*vrow)
969 legchar = (cups_vbcs_t)'?';
970 else
971 legchar = (cups_vbcs_t)*vrow;
ef416fc2 972
973 /*
e1d6a774 974 * Save n-byte legacy character...
ef416fc2 975 */
e1d6a774 976
977 if (legchar > 0xffffff)
ef416fc2 978 {
e1d6a774 979 if (maxout < 5)
980 return (-1);
981
982 *dest++ = (cups_sbcs_t)(legchar >> 24);
983 *dest++ = (cups_sbcs_t)(legchar >> 16);
984 *dest++ = (cups_sbcs_t)(legchar >> 8);
985 *dest++ = (cups_sbcs_t)legchar;
986
987 maxout -= 3;
ef416fc2 988 }
e1d6a774 989 else if (legchar > 0xffff)
990 {
991 if (maxout < 4)
992 return (-1);
ef416fc2 993
e1d6a774 994 *dest++ = (cups_sbcs_t)(legchar >> 16);
995 *dest++ = (cups_sbcs_t)(legchar >> 8);
996 *dest++ = (cups_sbcs_t)legchar;
ef416fc2 997
e1d6a774 998 maxout -= 2;
999 }
1000 else if (legchar > 0xff)
1001 {
1002 *dest++ = (cups_sbcs_t)(legchar >> 8);
1003 *dest++ = (cups_sbcs_t)legchar;
1004
1005 maxout --;
1006 }
ef416fc2 1007 }
e1d6a774 1008
1009 *dest = '\0';
1010
d6ae789d 1011 vmap->used --;
e1d6a774 1012
1013 return ((int)(dest - start));
ef416fc2 1014}
1015
e1d6a774 1016
ef416fc2 1017/*
e1d6a774 1018 * 'conv_vbcs_to_utf8()' - Convert legacy DBCS/VBCS to UTF-8.
ef416fc2 1019 */
e1d6a774 1020
1021static int /* O - Count or -1 on error */
1022conv_vbcs_to_utf8(
1023 cups_utf8_t *dest, /* O - Target string */
1024 const cups_sbcs_t *src, /* I - Source string */
1025 int maxout, /* I - Max output */
1026 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 1027{
e1d6a774 1028 _cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */
1029 cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
1030 _cups_wide2uni_t *wide2uni; /* Pointer to row in 'wide2uni' */
1031 cups_sbcs_t leadchar; /* Lead char of n-byte legacy char */
1032 cups_vbcs_t legchar; /* Legacy character value */
1033 cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
1034 *workptr; /* Pointer into string */
ef416fc2 1035
ef416fc2 1036
1037 /*
e1d6a774 1038 * Find legacy charset map in cache...
ef416fc2 1039 */
ef416fc2 1040
d6ae789d 1041 if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL)
e1d6a774 1042 return (-1);
ef416fc2 1043
1044 /*
e1d6a774 1045 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
ef416fc2 1046 */
ef416fc2 1047
e1d6a774 1048 work[0] = 0xfeff;
1049 for (workptr = work + 1; *src && workptr < (work + CUPS_MAX_USTRING - 1);)
ef416fc2 1050 {
e1d6a774 1051 legchar = *src++;
1052 leadchar = (cups_sbcs_t)legchar;
ef416fc2 1053
1054 /*
e1d6a774 1055 * Convert ASCII verbatim (optimization)...
ef416fc2 1056 */
ef416fc2 1057
e1d6a774 1058 if (legchar < 0x80)
ef416fc2 1059 {
e1d6a774 1060 *workptr++ = (cups_utf32_t)legchar;
1061 continue;
ef416fc2 1062 }
1063
1064 /*
e1d6a774 1065 * Convert 2-byte legacy character...
ef416fc2 1066 */
e1d6a774 1067
1068 if (vmap->lead2char[(int)leadchar] == leadchar)
ef416fc2 1069 {
e1d6a774 1070 if (!*src)
1071 return (-1);
1072
1073 legchar = (legchar << 8) | *src++;
1074
ef416fc2 1075 /*
e1d6a774 1076 * Convert unknown character to Replacement Character...
ef416fc2 1077 */
e1d6a774 1078
1079 crow = vmap->char2uni[(int)((legchar >> 8) & 0xff)];
1080 if (crow)
1081 crow += (int) (legchar & 0xff);
1082
1083 if (!crow || !*crow)
1084 *workptr++ = 0xfffd;
1085 else
1086 *workptr++ = (cups_utf32_t)*crow;
1087 continue;
ef416fc2 1088 }
1089
1090 /*
e1d6a774 1091 * Fetch 3-byte or 4-byte legacy character...
ef416fc2 1092 */
e1d6a774 1093
1094 if (vmap->lead3char[(int)leadchar] == leadchar)
ef416fc2 1095 {
e1d6a774 1096 if (!*src || !src[1])
1097 return (-1);
1098
1099 legchar = (legchar << 8) | *src++;
1100 legchar = (legchar << 8) | *src++;
ef416fc2 1101 }
e1d6a774 1102 else if (vmap->lead4char[(int)leadchar] == leadchar)
1103 {
1104 if (!*src || !src[1] || !src[2])
1105 return (-1);
1106
1107 legchar = (legchar << 8) | *src++;
1108 legchar = (legchar << 8) | *src++;
1109 legchar = (legchar << 8) | *src++;
1110 }
1111 else
1112 return (-1);
ef416fc2 1113
1114 /*
e1d6a774 1115 * Find 3-byte or 4-byte legacy character...
ef416fc2 1116 */
e1d6a774 1117
1118 wide2uni = (_cups_wide2uni_t *)bsearch(&legchar,
1119 vmap->wide2uni,
1120 vmap->widecount,
1121 sizeof(_cups_wide2uni_t),
1122 compare_wide);
ef416fc2 1123
1124 /*
e1d6a774 1125 * Convert unknown character to Replacement Character...
ef416fc2 1126 */
e1d6a774 1127
1128 if (!wide2uni || !wide2uni->unichar)
1129 *workptr++ = 0xfffd;
1130 else
1131 *workptr++ = wide2uni->unichar;
ef416fc2 1132 }
e1d6a774 1133
1134 *workptr = 0;
1135
d6ae789d 1136 vmap->used --;
e1d6a774 1137
1138 /*
1139 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
1140 */
1141
1142 return (cupsUTF32ToUTF8(dest, work, maxout));
ef416fc2 1143}
1144
e1d6a774 1145
ef416fc2 1146/*
e1d6a774 1147 * 'free_sbcs_charmap()' - Free memory used by a single byte character set.
ef416fc2 1148 */
e1d6a774 1149
1150static void
1151free_sbcs_charmap(_cups_cmap_t *cmap) /* I - Character set */
ef416fc2 1152{
e1d6a774 1153 int i; /* Looping variable */
ef416fc2 1154
ef416fc2 1155
e1d6a774 1156 for (i = 0; i < 256; i ++)
1157 if (cmap->uni2char[i])
1158 free(cmap->uni2char[i]);
1159
1160 free(cmap);
1161}
1162
1163
1164/*
1165 * 'free_vbcs_charmap()' - Free memory used by a variable byte character set.
1166 */
1167
1168static void
1169free_vbcs_charmap(_cups_vmap_t *vmap) /* I - Character set */
1170{
1171 int i; /* Looping variable */
1172
1173
1174 for (i = 0; i < 256; i ++)
1175 if (vmap->char2uni[i])
1176 free(vmap->char2uni[i]);
1177
1178 for (i = 0; i < 256; i ++)
1179 if (vmap->uni2char[i])
1180 free(vmap->uni2char[i]);
1181
1182 if (vmap->wide2uni)
1183 free(vmap->wide2uni);
1184
1185 free(vmap);
1186}
1187
1188
d6ae789d 1189/*
1190 * 'get_charmap()' - Lookup or get a character set map (private).
1191 *
1192 * This code handles single-byte (SBCS), double-byte (DBCS), and
1193 * variable-byte (VBCS) character sets _without_ charset escapes...
1194 * This code does not handle multiple-byte character sets (MBCS)
1195 * (such as ISO-2022-JP) with charset switching via escapes...
1196 */
1197
1198
d09495fa 1199static void * /* O - Charset map pointer */
d6ae789d 1200get_charmap(
1201 const cups_encoding_t encoding) /* I - Encoding */
1202{
1203 char filename[1024]; /* Filename for charset map file */
1204 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1205
1206
1207 /*
1208 * Get the data directory and charset map name...
1209 */
1210
1211 snprintf(filename, sizeof(filename), "%s/charmaps/%s.txt",
1212 cg->cups_datadir, _cupsEncodingName(encoding));
1213
1214 DEBUG_printf((" filename=\"%s\"\n", filename));
1215
1216 /*
1217 * Read charset map input file into cache...
1218 */
1219
1220 if (encoding < CUPS_ENCODING_SBCS_END)
1221 return (get_sbcs_charmap(encoding, filename));
1222 else if (encoding < CUPS_ENCODING_VBCS_END)
1223 return (get_vbcs_charmap(encoding, filename));
1224 else
1225 return (NULL);
1226}
1227
1228
e1d6a774 1229/*
1230 * 'get_charmap_count()' - Count lines in a charmap file.
1231 */
1232
1233static int /* O - Count or -1 on error */
1234get_charmap_count(cups_file_t *fp) /* I - File to read from */
1235{
1236 int count; /* Number of lines */
1237 char line[256]; /* Line from input map file */
ef416fc2 1238
ef416fc2 1239
1240 /*
e1d6a774 1241 * Count lines in map input file...
ef416fc2 1242 */
ef416fc2 1243
e1d6a774 1244 count = 0;
ef416fc2 1245
e1d6a774 1246 while (cupsFileGets(fp, line, sizeof(line)))
1247 if (line[0] == '0')
1248 count ++;
ef416fc2 1249
e1d6a774 1250 /*
1251 * Return the number of lines...
1252 */
1253
1254 if (count > 0)
1255 return (count);
1256 else
1257 return (-1);
ef416fc2 1258}
1259
e1d6a774 1260
ef416fc2 1261/*
e1d6a774 1262 * 'get_sbcs_charmap()' - Get SBCS Charmap.
ef416fc2 1263 */
e1d6a774 1264
1265static _cups_cmap_t * /* O - Charmap or 0 on error */
1266get_sbcs_charmap(
1267 const cups_encoding_t encoding, /* I - Charmap Encoding */
1268 const char *filename) /* I - Charmap Filename */
ef416fc2 1269{
e1d6a774 1270 unsigned long legchar; /* Legacy character value */
1271 cups_utf32_t unichar; /* Unicode character value */
1272 _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
1273 cups_file_t *fp; /* Charset map file pointer */
1274 char *s; /* Line parsing pointer */
1275 cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
1276 cups_sbcs_t *srow; /* Pointer to SBCS row in 'uni2char' */
1277 char line[256]; /* Line from charset map file */
e1d6a774 1278
ef416fc2 1279
1280 /*
e1d6a774 1281 * See if we already have this SBCS charset map loaded...
ef416fc2 1282 */
e1d6a774 1283
d6ae789d 1284 for (cmap = cmap_cache; cmap; cmap = cmap->next)
e1d6a774 1285 {
1286 if (cmap->encoding == encoding)
1287 {
1288 cmap->used ++;
1289 DEBUG_printf((" returning existing cmap=%p\n", cmap));
d6ae789d 1290
e1d6a774 1291 return ((void *)cmap);
1292 }
1293 }
ef416fc2 1294
1295 /*
e1d6a774 1296 * Open SBCS charset map input file...
ef416fc2 1297 */
e1d6a774 1298
1299 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1300 return (NULL);
ef416fc2 1301
1302 /*
e1d6a774 1303 * Allocate memory for SBCS charset map...
ef416fc2 1304 */
e1d6a774 1305
1306 if ((cmap = (_cups_cmap_t *)calloc(1, sizeof(_cups_cmap_t))) == NULL)
1307 {
1308 cupsFileClose(fp);
1309 DEBUG_puts(" Unable to allocate memory!");
d6ae789d 1310
e1d6a774 1311 return (NULL);
1312 }
1313
1314 cmap->used ++;
1315 cmap->encoding = encoding;
ef416fc2 1316
1317 /*
e1d6a774 1318 * Save SBCS charset map into memory for transcoding...
ef416fc2 1319 */
e1d6a774 1320
1321 while (cupsFileGets(fp, line, sizeof(line)))
ef416fc2 1322 {
e1d6a774 1323 if (line[0] != '0')
1324 continue;
1325
1326 legchar = strtol(line, &s, 16);
1327 if (legchar < 0 || legchar > 0xff)
1328 goto sbcs_error;
1329
1330 unichar = strtol(s, NULL, 16);
1331 if (unichar < 0 || unichar > 0xffff)
1332 goto sbcs_error;
ef416fc2 1333
1334 /*
e1d6a774 1335 * Save legacy to Unicode mapping in direct lookup table...
ef416fc2 1336 */
e1d6a774 1337
1338 crow = cmap->char2uni + legchar;
1339 *crow = (cups_ucs2_t)(unichar & 0xffff);
ef416fc2 1340
1341 /*
e1d6a774 1342 * Save Unicode to legacy mapping in indirect lookup table...
ef416fc2 1343 */
e1d6a774 1344
1345 srow = cmap->uni2char[(unichar >> 8) & 0xff];
1346 if (!srow)
ef416fc2 1347 {
e1d6a774 1348 srow = (cups_sbcs_t *)calloc(256, sizeof(cups_sbcs_t));
1349 if (!srow)
1350 goto sbcs_error;
1351
1352 cmap->uni2char[(unichar >> 8) & 0xff] = srow;
ef416fc2 1353 }
1354
e1d6a774 1355 srow += unichar & 0xff;
1356
ef416fc2 1357 /*
e1d6a774 1358 * Convert Replacement Character to visible replacement...
ef416fc2 1359 */
e1d6a774 1360
1361 if (unichar == 0xfffd)
1362 legchar = (unsigned long)'?';
ef416fc2 1363
1364 /*
e1d6a774 1365 * First (oldest) legacy character uses Unicode mapping cell...
ef416fc2 1366 */
ef416fc2 1367
e1d6a774 1368 if (!*srow)
1369 *srow = (cups_sbcs_t)legchar;
1370 }
ef416fc2 1371
e1d6a774 1372 cupsFileClose(fp);
1373
ef416fc2 1374 /*
e1d6a774 1375 * Add it to the cache and return...
ef416fc2 1376 */
e1d6a774 1377
d6ae789d 1378 cmap->next = cmap_cache;
1379 cmap_cache = cmap;
e1d6a774 1380
1381 DEBUG_printf((" returning new cmap=%p\n", cmap));
1382
1383 return (cmap);
ef416fc2 1384
1385 /*
e1d6a774 1386 * If we get here, there was an error in the cmap file...
ef416fc2 1387 */
e1d6a774 1388
1389 sbcs_error:
1390
1391 free_sbcs_charmap(cmap);
1392
1393 cupsFileClose(fp);
1394
1395 DEBUG_puts(" Error, returning NULL!");
1396
1397 return (NULL);
1398}
1399
1400
1401/*
1402 * 'get_vbcs_charmap()' - Get DBCS/VBCS Charmap.
1403 */
1404
1405static _cups_vmap_t * /* O - Charmap or 0 on error */
1406get_vbcs_charmap(
1407 const cups_encoding_t encoding, /* I - Charmap Encoding */
1408 const char *filename) /* I - Charmap Filename */
1409{
1410 _cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */
1411 cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
1412 cups_vbcs_t *vrow; /* Pointer to VBCS row in 'uni2char' */
1413 _cups_wide2uni_t *wide2uni; /* Pointer to row in 'wide2uni' */
1414 cups_sbcs_t leadchar; /* Lead char of 2-byte legacy char */
1415 unsigned long legchar; /* Legacy character value */
1416 cups_utf32_t unichar; /* Unicode character value */
1417 int mapcount; /* Count of lines in charmap file */
1418 cups_file_t *fp; /* Charset map file pointer */
1419 char *s; /* Line parsing pointer */
1420 char line[256]; /* Line from charset map file */
1421 int i; /* Loop variable */
09a101d6 1422 int legacy; /* 32-bit legacy char */
e1d6a774 1423
1424
1425 DEBUG_printf(("get_vbcs_charmap(encoding=%d, filename=\"%s\")\n",
1426 encoding, filename));
ef416fc2 1427
1428 /*
e1d6a774 1429 * See if we already have this DBCS/VBCS charset map loaded...
ef416fc2 1430 */
ef416fc2 1431
d6ae789d 1432 for (vmap = vmap_cache; vmap; vmap = vmap->next)
e1d6a774 1433 {
1434 if (vmap->encoding == encoding)
ef416fc2 1435 {
e1d6a774 1436 vmap->used ++;
1437 DEBUG_printf((" returning existing vmap=%p\n", vmap));
d6ae789d 1438
e1d6a774 1439 return ((void *)vmap);
ef416fc2 1440 }
ef416fc2 1441 }
ef416fc2 1442
1443 /*
e1d6a774 1444 * Open VBCS charset map input file...
ef416fc2 1445 */
ef416fc2 1446
e1d6a774 1447 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1448 {
1449 DEBUG_printf((" Unable to open file: %s\n", strerror(errno)));
d6ae789d 1450
e1d6a774 1451 return (NULL);
1452 }
ef416fc2 1453
1454 /*
e1d6a774 1455 * Count lines in charmap file...
ef416fc2 1456 */
e1d6a774 1457
1458 if ((mapcount = get_charmap_count(fp)) <= 0)
1459 {
1460 DEBUG_puts(" Unable to get charmap count!");
d6ae789d 1461
91c84a35
MS
1462 cupsFileClose(fp);
1463
e1d6a774 1464 return (NULL);
1465 }
1466
1467 DEBUG_printf((" mapcount=%d\n", mapcount));
ef416fc2 1468
1469 /*
e1d6a774 1470 * Allocate memory for DBCS/VBCS charset map...
ef416fc2 1471 */
e1d6a774 1472
1473 if ((vmap = (_cups_vmap_t *)calloc(1, sizeof(_cups_vmap_t))) == NULL)
1474 {
e1d6a774 1475 DEBUG_puts(" Unable to allocate memory!");
d6ae789d 1476
91c84a35
MS
1477 cupsFileClose(fp);
1478
e1d6a774 1479 return (NULL);
1480 }
1481
1482 vmap->used ++;
1483 vmap->encoding = encoding;
ef416fc2 1484
1485 /*
e1d6a774 1486 * Save DBCS/VBCS charset map into memory for transcoding...
ef416fc2 1487 */
e1d6a774 1488
1489 leadchar = 0;
1490 wide2uni = NULL;
1491
1492 cupsFileRewind(fp);
1493
09a101d6 1494 i = 0;
1495 legacy = 0;
e1d6a774 1496
1497 while (cupsFileGets(fp, line, sizeof(line)))
ef416fc2 1498 {
e1d6a774 1499 if (line[0] != '0')
1500 continue;
1501
1502 legchar = strtoul(line, &s, 16);
1503 if (legchar == ULONG_MAX)
1504 goto vbcs_error;
1505
1506 unichar = strtol(s, NULL, 16);
1507 if (unichar < 0 || unichar > 0xffff)
1508 goto vbcs_error;
1509
1510 i ++;
1511
1512/* DEBUG_printf((" i=%d, legchar=0x%08lx, unichar=0x%04x\n", i,
1513 legchar, (unsigned)unichar)); */
ef416fc2 1514
1515 /*
e1d6a774 1516 * Save lead char of 2/3/4-byte legacy char...
ef416fc2 1517 */
e1d6a774 1518
1519 if (legchar > 0xff && legchar <= 0xffff)
ef416fc2 1520 {
e1d6a774 1521 leadchar = (cups_sbcs_t)(legchar >> 8);
1522 vmap->lead2char[leadchar] = leadchar;
1523 }
1524
1525 if (legchar > 0xffff && legchar <= 0xffffff)
1526 {
1527 leadchar = (cups_sbcs_t)(legchar >> 16);
1528 vmap->lead3char[leadchar] = leadchar;
1529 }
1530
1531 if (legchar > 0xffffff)
1532 {
1533 leadchar = (cups_sbcs_t)(legchar >> 24);
1534 vmap->lead4char[leadchar] = leadchar;
ef416fc2 1535 }
1536
1537 /*
e1d6a774 1538 * Save Legacy to Unicode mapping...
ef416fc2 1539 */
e1d6a774 1540
1541 if (legchar <= 0xffff)
ef416fc2 1542 {
ef416fc2 1543 /*
e1d6a774 1544 * Save DBCS 16-bit to Unicode mapping in indirect lookup table...
ef416fc2 1545 */
e1d6a774 1546
1547 crow = vmap->char2uni[(int)leadchar];
1548 if (!crow)
1549 {
1550 crow = (cups_ucs2_t *)calloc(256, sizeof(cups_ucs2_t));
1551 if (!crow)
1552 goto vbcs_error;
1553
1554 vmap->char2uni[(int)leadchar] = crow;
1555 }
1556
1557 crow[(int)(legchar & 0xff)] = (cups_ucs2_t)unichar;
1558 }
1559 else
1560 {
1561 /*
1562 * Save VBCS 32-bit to Unicode mapping in sorted list table...
1563 */
1564
09a101d6 1565 if (!legacy)
e1d6a774 1566 {
09a101d6 1567 legacy = 1;
e1d6a774 1568 vmap->widecount = (mapcount - i + 1);
1569 wide2uni = (_cups_wide2uni_t *)calloc(vmap->widecount,
1570 sizeof(_cups_wide2uni_t));
1571 if (!wide2uni)
1572 goto vbcs_error;
1573
1574 vmap->wide2uni = wide2uni;
1575 }
1576
1577 wide2uni->widechar = (cups_vbcs_t)legchar;
1578 wide2uni->unichar = (cups_ucs2_t)unichar;
1579 wide2uni ++;
ef416fc2 1580 }
1581
1582 /*
e1d6a774 1583 * Save Unicode to legacy mapping in indirect lookup table...
ef416fc2 1584 */
e1d6a774 1585
1586 vrow = vmap->uni2char[(int)((unichar >> 8) & 0xff)];
1587 if (!vrow)
ef416fc2 1588 {
e1d6a774 1589 vrow = (cups_vbcs_t *)calloc(256, sizeof(cups_vbcs_t));
1590 if (!vrow)
1591 goto vbcs_error;
1592
1593 vmap->uni2char[(int) ((unichar >> 8) & 0xff)] = vrow;
ef416fc2 1594 }
e1d6a774 1595
1596 vrow += (int)(unichar & 0xff);
ef416fc2 1597
1598 /*
e1d6a774 1599 * Convert Replacement Character to visible replacement...
ef416fc2 1600 */
e1d6a774 1601
1602 if (unichar == 0xfffd)
1603 legchar = (unsigned long)'?';
ef416fc2 1604
1605 /*
e1d6a774 1606 * First (oldest) legacy character uses Unicode mapping cell...
ef416fc2 1607 */
e1d6a774 1608
1609 if (!*vrow)
1610 *vrow = (cups_vbcs_t)legchar;
ef416fc2 1611 }
e1d6a774 1612
1613 vmap->charcount = (i - vmap->widecount);
1614
1615 cupsFileClose(fp);
ef416fc2 1616
1617 /*
e1d6a774 1618 * Add it to the cache and return...
ef416fc2 1619 */
ef416fc2 1620
d6ae789d 1621 vmap->next = vmap_cache;
1622 vmap_cache = vmap;
e1d6a774 1623
1624 DEBUG_printf((" returning new vmap=%p\n", vmap));
1625
1626 return (vmap);
1627
1628 /*
1629 * If we get here, the file contains errors...
1630 */
1631
1632 vbcs_error:
1633
1634 free_vbcs_charmap(vmap);
1635
1636 cupsFileClose(fp);
1637
1638 DEBUG_puts(" Error, returning NULL!");
1639
1640 return (NULL);
ef416fc2 1641}
1642
1643
1644/*
bc44d920 1645 * End of "$Id: transcode.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 1646 */