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