]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/transcode.c
Merge changes from CUPS 1.4svn-r8606.
[thirdparty/cups.git] / cups / transcode.c
CommitLineData
ef416fc2 1/*
75bd9771 2 * "$Id: transcode.c 7560 2008-05-13 06:34:04Z mike $"
ef416fc2 3 *
4 * Transcoding support for the Common UNIX Printing System (CUPS).
5 *
e07d4801 6 * Copyright 2007-2009 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
e07d4801 213 DEBUG_printf(("7_cupsCharmapGet(encoding=%d)", 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 {
e07d4801 221 DEBUG_puts("8_cupsCharmapGet: Bad encoding, returning NULL!");
e1d6a774 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
f11a948a 266 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)",
e1d6a774 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 {
f11a948a 274 DEBUG_puts("3cupsCharsetToUTF8: 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
e07d4801
MS
476 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest,
477 src, maxout));
c9fc04c6 478
e1d6a774 479 if (dest)
480 *dest = 0;
481
482 if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
c9fc04c6 483 {
e07d4801 484 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
c9fc04c6 485
ef416fc2 486 return (-1);
c9fc04c6 487 }
ef416fc2 488
489 /*
cda47a96 490 * Convert input UTF-8 to output UTF-32...
ef416fc2 491 */
e1d6a774 492
e1d6a774 493 for (i = maxout - 1; *src && i > 0; i --)
ef416fc2 494 {
e1d6a774 495 ch = *src++;
ef416fc2 496
497 /*
498 * Convert UTF-8 character(s) to UTF-32 character...
499 */
e1d6a774 500
501 if (!(ch & 0x80))
ef416fc2 502 {
503 /*
504 * One-octet UTF-8 <= 127 (US-ASCII)...
505 */
e1d6a774 506
507 *dest++ = ch;
c9fc04c6 508
e07d4801 509 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src[-1], ch));
2abf387c 510 continue;
ef416fc2 511 }
512 else if ((ch & 0xe0) == 0xc0)
513 {
514 /*
515 * Two-octet UTF-8 <= 2047 (Latin-x)...
516 */
e1d6a774 517
518 next = *src++;
c9fc04c6
MS
519 if ((next & 0xc0) != 0x80)
520 {
e07d4801 521 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 522
ef416fc2 523 return (-1);
c9fc04c6 524 }
e1d6a774 525
ef416fc2 526 ch32 = ((ch & 0x1f) << 6) | (next & 0x3f);
527
528 /*
529 * Check for non-shortest form (invalid UTF-8)...
530 */
e1d6a774 531
532 if (ch32 < 0x80)
c9fc04c6 533 {
e07d4801 534 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 535
ef416fc2 536 return (-1);
c9fc04c6 537 }
e1d6a774 538
539 *dest++ = ch32;
c9fc04c6 540
e07d4801 541 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
c9fc04c6 542 src[-2], src[-1], (unsigned)ch32));
ef416fc2 543 }
544 else if ((ch & 0xf0) == 0xe0)
545 {
546 /*
547 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
548 */
e1d6a774 549
550 next = *src++;
c9fc04c6
MS
551 if ((next & 0xc0) != 0x80)
552 {
e07d4801 553 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 554
ef416fc2 555 return (-1);
c9fc04c6 556 }
e1d6a774 557
558 ch32 = ((ch & 0x0f) << 6) | (next & 0x3f);
559
560 next = *src++;
c9fc04c6
MS
561 if ((next & 0xc0) != 0x80)
562 {
e07d4801 563 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 564
ef416fc2 565 return (-1);
c9fc04c6 566 }
e1d6a774 567
568 ch32 = (ch32 << 6) | (next & 0x3f);
ef416fc2 569
570 /*
571 * Check for non-shortest form (invalid UTF-8)...
572 */
e1d6a774 573
574 if (ch32 < 0x800)
c9fc04c6 575 {
e07d4801 576 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 577
ef416fc2 578 return (-1);
c9fc04c6 579 }
e1d6a774 580
581 *dest++ = ch32;
c9fc04c6 582
e07d4801 583 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
c9fc04c6 584 src[-3], src[-2], src[-1], (unsigned)ch32));
ef416fc2 585 }
586 else if ((ch & 0xf8) == 0xf0)
587 {
588 /*
e1d6a774 589 * Four-octet UTF-8...
ef416fc2 590 */
e1d6a774 591
592 next = *src++;
c9fc04c6
MS
593 if ((next & 0xc0) != 0x80)
594 {
e07d4801 595 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 596
ef416fc2 597 return (-1);
c9fc04c6 598 }
e1d6a774 599
600 ch32 = ((ch & 0x07) << 6) | (next & 0x3f);
601
602 next = *src++;
c9fc04c6
MS
603 if ((next & 0xc0) != 0x80)
604 {
e07d4801 605 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 606
e1d6a774 607 return (-1);
c9fc04c6 608 }
e1d6a774 609
610 ch32 = (ch32 << 6) | (next & 0x3f);
611
612 next = *src++;
c9fc04c6
MS
613 if ((next & 0xc0) != 0x80)
614 {
e07d4801 615 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 616
e1d6a774 617 return (-1);
c9fc04c6 618 }
e1d6a774 619
620 ch32 = (ch32 << 6) | (next & 0x3f);
621
ef416fc2 622 /*
e1d6a774 623 * Check for non-shortest form (invalid UTF-8)...
ef416fc2 624 */
e1d6a774 625
626 if (ch32 < 0x10000)
c9fc04c6 627 {
e07d4801 628 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 629
e1d6a774 630 return (-1);
c9fc04c6 631 }
e1d6a774 632
633 *dest++ = ch32;
c9fc04c6 634
e07d4801 635 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
c9fc04c6 636 src[-4], src[-3], src[-2], src[-1], (unsigned)ch32));
ef416fc2 637 }
638 else
639 {
640 /*
e1d6a774 641 * More than 4-octet (invalid UTF-8 sequence)...
ef416fc2 642 */
e1d6a774 643
e07d4801 644 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 645
ef416fc2 646 return (-1);
647 }
648
649 /*
650 * Check for UTF-16 surrogate (illegal UTF-8)...
651 */
ef416fc2 652
2abf387c 653 if (ch32 >= 0xd800 && ch32 <= 0xdfff)
ef416fc2 654 return (-1);
655 }
e1d6a774 656
ef416fc2 657 *dest = 0;
e1d6a774 658
e07d4801 659 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout - 1 - i));
c9fc04c6
MS
660
661 return (maxout - 1 - i);
ef416fc2 662}
663
e1d6a774 664
ef416fc2 665/*
666 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
667 *
668 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
669 *
670 * UTF-32 char UTF-8 char(s)
671 * --------------------------------------------------
e1d6a774 672 * 0 to 127 = 0xxxxxxx (US-ASCII)
ef416fc2 673 * 128 to 2047 = 110xxxxx 10yyyyyy
674 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
e1d6a774 675 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
ef416fc2 676 *
677 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
678 * which would convert to five- or six-octet UTF-8 sequences...
ef416fc2 679 */
e1d6a774 680
681int /* O - Count or -1 on error */
682cupsUTF32ToUTF8(
683 cups_utf8_t *dest, /* O - Target string */
684 const cups_utf32_t *src, /* I - Source string */
685 const int maxout) /* I - Max output */
ef416fc2 686{
e1d6a774 687 cups_utf8_t *start; /* Start of destination string */
688 int i; /* Looping variable */
689 int swap; /* Byte-swap input to output */
690 cups_utf32_t ch; /* Character value */
691
ef416fc2 692
693 /*
694 * Check for valid arguments and clear output...
695 */
e1d6a774 696
e07d4801 697 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest, src,
c9fc04c6
MS
698 maxout));
699
e1d6a774 700 if (dest)
701 *dest = '\0';
702
703 if (!dest || !src || maxout < 1)
c9fc04c6 704 {
e07d4801 705 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
c9fc04c6 706
ef416fc2 707 return (-1);
c9fc04c6 708 }
ef416fc2 709
710 /*
711 * Check for leading BOM in UTF-32 and inverted BOM...
712 */
e1d6a774 713
714 start = dest;
715 swap = *src == 0xfffe0000;
716
e07d4801 717 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap));
c9fc04c6 718
e1d6a774 719 if (*src == 0xfffe0000 || *src == 0xfeff)
720 src ++;
ef416fc2 721
722 /*
723 * Convert input UTF-32 to output UTF-8...
724 */
e1d6a774 725
726 for (i = maxout - 1; *src && i > 0;)
ef416fc2 727 {
e1d6a774 728 ch = *src++;
ef416fc2 729
730 /*
731 * Byte swap input UTF-32, if necessary...
e1d6a774 732 * (only byte-swapping 24 of 32 bits)
ef416fc2 733 */
e1d6a774 734
ef416fc2 735 if (swap)
736 ch = ((ch >> 24) | ((ch >> 8) & 0xff00) | ((ch << 8) & 0xff0000));
737
738 /*
e1d6a774 739 * Check for beyond Plane 16 (invalid UTF-32)...
ef416fc2 740 */
ef416fc2 741
ef416fc2 742 if (ch > 0x10ffff)
c9fc04c6 743 {
e07d4801 744 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
c9fc04c6 745
ef416fc2 746 return (-1);
c9fc04c6 747 }
ef416fc2 748
ef416fc2 749 /*
750 * Convert UTF-32 character to UTF-8 character(s)...
751 */
e1d6a774 752
753 if (ch < 0x80)
ef416fc2 754 {
755 /*
756 * One-octet UTF-8 <= 127 (US-ASCII)...
757 */
e1d6a774 758
759 *dest++ = (cups_utf8_t)ch;
760 i --;
c9fc04c6 761
e07d4801 762 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch, dest[-1]));
ef416fc2 763 }
e1d6a774 764 else if (ch < 0x800)
ef416fc2 765 {
766 /*
767 * Two-octet UTF-8 <= 2047 (Latin-x)...
768 */
e1d6a774 769
770 if (i < 2)
c9fc04c6 771 {
e07d4801 772 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
c9fc04c6 773
e1d6a774 774 return (-1);
c9fc04c6 775 }
e1d6a774 776
777 *dest++ = (cups_utf8_t)(0xc0 | ((ch >> 6) & 0x1f));
778 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
779 i -= 2;
c9fc04c6 780
e07d4801 781 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch,
c9fc04c6 782 dest[-2], dest[-1]));
ef416fc2 783 }
e1d6a774 784 else if (ch < 0x10000)
ef416fc2 785 {
786 /*
787 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
788 */
e1d6a774 789
790 if (i < 3)
c9fc04c6 791 {
e07d4801 792 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
c9fc04c6 793
e1d6a774 794 return (-1);
c9fc04c6 795 }
e1d6a774 796
797 *dest++ = (cups_utf8_t)(0xe0 | ((ch >> 12) & 0x0f));
798 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
799 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
800 i -= 3;
c9fc04c6 801
e07d4801 802 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch,
c9fc04c6 803 dest[-3], dest[-2], dest[-1]));
e1d6a774 804 }
805 else
806 {
807 /*
808 * Four-octet UTF-8...
809 */
810
811 if (i < 4)
e07d4801
MS
812 {
813 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
814
e1d6a774 815 return (-1);
e07d4801 816 }
e1d6a774 817
818 *dest++ = (cups_utf8_t)(0xf0 | ((ch >> 18) & 0x07));
819 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 12) & 0x3f));
820 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
821 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
822 i -= 4;
c9fc04c6 823
e07d4801 824 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
c9fc04c6 825 (unsigned)ch, dest[-4], dest[-3], dest[-2], dest[-1]));
ef416fc2 826 }
827 }
e1d6a774 828
ef416fc2 829 *dest = '\0';
e1d6a774 830
e07d4801 831 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest - start)));
c9fc04c6 832
e1d6a774 833 return ((int)(dest - start));
ef416fc2 834}
835
e1d6a774 836
ef416fc2 837/*
e1d6a774 838 * 'compare_wide()' - Compare key for wide (VBCS) match.
839 */
840
841static int
842compare_wide(const void *k1, /* I - Key char */
843 const void *k2) /* I - Map char */
844{
845 cups_vbcs_t key; /* Legacy key character */
846 cups_vbcs_t map; /* Legacy map character */
847
848
849 key = *((cups_vbcs_t *)k1);
850 map = ((_cups_wide2uni_t *)k2)->widechar;
851
852 return ((int)(key - map));
853}
854
855
856/*
857 * 'conv_sbcs_to_utf8()' - Convert legacy SBCS to UTF-8.
ef416fc2 858 */
e1d6a774 859
860static int /* O - Count or -1 on error */
861conv_sbcs_to_utf8(
862 cups_utf8_t *dest, /* O - Target string */
863 const cups_sbcs_t *src, /* I - Source string */
864 int maxout, /* I - Max output */
865 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 866{
e1d6a774 867 _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
868 cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
869 cups_sbcs_t legchar; /* Legacy character value */
870 cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
871 *workptr; /* Pointer into string */
872
ef416fc2 873
874 /*
e1d6a774 875 * Find legacy charset map in cache...
ef416fc2 876 */
e1d6a774 877
d6ae789d 878 if ((cmap = (_cups_cmap_t *)get_charmap(encoding)) == NULL)
ef416fc2 879 return (-1);
ef416fc2 880
881 /*
e1d6a774 882 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
ef416fc2 883 */
ef416fc2 884
e1d6a774 885 work[0] = 0xfeff;
886 for (workptr = work + 1; *src && workptr < (work + CUPS_MAX_USTRING - 1);)
ef416fc2 887 {
e1d6a774 888 legchar = *src++;
ef416fc2 889
890 /*
e1d6a774 891 * Convert ASCII verbatim (optimization)...
ef416fc2 892 */
ef416fc2 893
e1d6a774 894 if (legchar < 0x80)
895 *workptr++ = (cups_utf32_t)legchar;
896 else
ef416fc2 897 {
e1d6a774 898 /*
899 * Convert unknown character to Replacement Character...
900 */
ef416fc2 901
e1d6a774 902 crow = cmap->char2uni + legchar;
903
904 if (!*crow)
905 *workptr++ = 0xfffd;
906 else
907 *workptr++ = (cups_utf32_t)*crow;
ef416fc2 908 }
ef416fc2 909 }
e1d6a774 910
911 *workptr = 0;
912
913 /*
914 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
915 */
916
d6ae789d 917 cmap->used --;
e1d6a774 918
919 return (cupsUTF32ToUTF8(dest, work, maxout));
ef416fc2 920}
921
e1d6a774 922
ef416fc2 923/*
e1d6a774 924 * 'conv_utf8_to_sbcs()' - Convert UTF-8 to legacy SBCS.
ef416fc2 925 */
e1d6a774 926
927static int /* O - Count or -1 on error */
928conv_utf8_to_sbcs(
929 cups_sbcs_t *dest, /* O - Target string */
930 const cups_utf8_t *src, /* I - Source string */
931 int maxout, /* I - Max output */
932 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 933{
e1d6a774 934 cups_sbcs_t *start; /* Start of destination string */
935 _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
936 cups_sbcs_t *srow; /* Pointer to SBCS row in 'uni2char' */
937 cups_utf32_t unichar; /* Character value */
938 cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
939 *workptr; /* Pointer into string */
940
ef416fc2 941
942 /*
e1d6a774 943 * Find legacy charset map in cache...
ef416fc2 944 */
e1d6a774 945
d6ae789d 946 if ((cmap = (_cups_cmap_t *)get_charmap(encoding)) == NULL)
ef416fc2 947 return (-1);
ef416fc2 948
949 /*
e1d6a774 950 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
ef416fc2 951 */
e1d6a774 952
953 if (cupsUTF8ToUTF32(work, src, CUPS_MAX_USTRING) < 0)
954 return (-1);
ef416fc2 955
956 /*
e1d6a774 957 * Convert internal UCS-4 to SBCS legacy charset (and delete BOM)...
ef416fc2 958 */
e1d6a774 959
58dc1933 960 for (workptr = work, start = dest; *workptr && maxout > 0; maxout --)
ef416fc2 961 {
e1d6a774 962 unichar = *workptr++;
963 if (!unichar)
ef416fc2 964 break;
ef416fc2 965
966 /*
e1d6a774 967 * Convert ASCII verbatim (optimization)...
ef416fc2 968 */
ef416fc2 969
e1d6a774 970 if (unichar < 0x80)
971 {
972 *dest++ = (cups_sbcs_t)unichar;
973 continue;
974 }
ef416fc2 975
976 /*
e1d6a774 977 * Convert unknown character to visible replacement...
ef416fc2 978 */
ef416fc2 979
e1d6a774 980 srow = cmap->uni2char[(int)((unichar >> 8) & 0xff)];
ef416fc2 981
e1d6a774 982 if (srow)
983 srow += (int)(unichar & 0xff);
ef416fc2 984
e1d6a774 985 if (!srow || !*srow)
986 *dest++ = '?';
987 else
988 *dest++ = *srow;
ef416fc2 989 }
ef416fc2 990
e1d6a774 991 *dest = '\0';
992
d6ae789d 993 cmap->used --;
e1d6a774 994
995 return ((int)(dest - start));
ef416fc2 996}
997
e1d6a774 998
ef416fc2 999/*
e1d6a774 1000 * 'conv_utf8_to_vbcs()' - Convert UTF-8 to legacy DBCS/VBCS.
ef416fc2 1001 */
e1d6a774 1002
1003static int /* O - Count or -1 on error */
1004conv_utf8_to_vbcs(
1005 cups_sbcs_t *dest, /* O - Target string */
1006 const cups_utf8_t *src, /* I - Source string */
1007 int maxout, /* I - Max output */
1008 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 1009{
e1d6a774 1010 cups_sbcs_t *start; /* Start of destination string */
1011 _cups_vmap_t *vmap; /* Legacy DBCS / Unicode Charset Map */
1012 cups_vbcs_t *vrow; /* Pointer to VBCS row in 'uni2char' */
1013 cups_utf32_t unichar; /* Character value */
1014 cups_vbcs_t legchar; /* Legacy character value */
1015 cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
1016 *workptr; /* Pointer into string */
ef416fc2 1017
ef416fc2 1018
e07d4801
MS
1019 DEBUG_printf(("7conv_utf8_to_vbcs(dest=%p, src=\"%s\", maxout=%d, "
1020 "encoding=%d)", dest, src, maxout, encoding));
c9fc04c6 1021
ef416fc2 1022 /*
e1d6a774 1023 * Find legacy charset map in cache...
ef416fc2 1024 */
ef416fc2 1025
d6ae789d 1026 if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL)
c9fc04c6 1027 {
e07d4801 1028 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (no charmap)");
c9fc04c6 1029
e1d6a774 1030 return (-1);
c9fc04c6 1031 }
ef416fc2 1032
1033 /*
e1d6a774 1034 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
ef416fc2 1035 */
e1d6a774 1036
1037 if (cupsUTF8ToUTF32(work, src, CUPS_MAX_USTRING) < 0)
c9fc04c6 1038 {
e07d4801 1039 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (Unable to convert to UTF-32)");
c9fc04c6 1040
e1d6a774 1041 return (-1);
c9fc04c6 1042 }
ef416fc2 1043
1044 /*
e1d6a774 1045 * Convert internal UCS-4 to VBCS legacy charset (and delete BOM)...
ef416fc2 1046 */
e1d6a774 1047
58dc1933 1048 for (start = dest, workptr = work; *workptr && maxout > 0; maxout --)
ef416fc2 1049 {
e1d6a774 1050 unichar = *workptr++;
ef416fc2 1051
1052 /*
e1d6a774 1053 * Convert ASCII verbatim (optimization)...
ef416fc2 1054 */
e1d6a774 1055
1056 if (unichar < 0x80)
1057 {
b86bc4cf 1058 *dest++ = (cups_sbcs_t)unichar;
c9fc04c6 1059
e07d4801 1060 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X", (unsigned)unichar,
c9fc04c6
MS
1061 dest[-1]));
1062
e1d6a774 1063 continue;
1064 }
ef416fc2 1065
1066 /*
e1d6a774 1067 * Convert unknown character to visible replacement...
ef416fc2 1068 */
e1d6a774 1069
1070 vrow = vmap->uni2char[(int)((unichar >> 8) & 0xff)];
1071
1072 if (vrow)
1073 vrow += (int)(unichar & 0xff);
1074
1075 if (!vrow || !*vrow)
1076 legchar = (cups_vbcs_t)'?';
1077 else
1078 legchar = (cups_vbcs_t)*vrow;
ef416fc2 1079
1080 /*
e1d6a774 1081 * Save n-byte legacy character...
ef416fc2 1082 */
e1d6a774 1083
1084 if (legchar > 0xffffff)
ef416fc2 1085 {
e1d6a774 1086 if (maxout < 5)
c9fc04c6 1087 {
e07d4801 1088 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (out of space)");
c9fc04c6 1089
e1d6a774 1090 return (-1);
c9fc04c6 1091 }
e1d6a774 1092
1093 *dest++ = (cups_sbcs_t)(legchar >> 24);
1094 *dest++ = (cups_sbcs_t)(legchar >> 16);
1095 *dest++ = (cups_sbcs_t)(legchar >> 8);
1096 *dest++ = (cups_sbcs_t)legchar;
1097
1098 maxout -= 3;
c9fc04c6 1099
e07d4801 1100 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X %02X %02X",
c9fc04c6 1101 (unsigned)unichar, dest[-4], dest[-3], dest[-2], dest[-1]));
ef416fc2 1102 }
e1d6a774 1103 else if (legchar > 0xffff)
1104 {
1105 if (maxout < 4)
c9fc04c6 1106 {
e07d4801 1107 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (out of space)");
c9fc04c6 1108
e1d6a774 1109 return (-1);
c9fc04c6 1110 }
ef416fc2 1111
e1d6a774 1112 *dest++ = (cups_sbcs_t)(legchar >> 16);
1113 *dest++ = (cups_sbcs_t)(legchar >> 8);
1114 *dest++ = (cups_sbcs_t)legchar;
ef416fc2 1115
e1d6a774 1116 maxout -= 2;
c9fc04c6 1117
e07d4801 1118 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X %02X",
c9fc04c6 1119 (unsigned)unichar, dest[-3], dest[-2], dest[-1]));
e1d6a774 1120 }
1121 else if (legchar > 0xff)
1122 {
1123 *dest++ = (cups_sbcs_t)(legchar >> 8);
1124 *dest++ = (cups_sbcs_t)legchar;
1125
1126 maxout --;
c9fc04c6 1127
e07d4801 1128 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X",
c9fc04c6
MS
1129 (unsigned)unichar, dest[-2], dest[-1]));
1130 }
1131 else
1132 {
1133 *dest++ = legchar;
1134
e07d4801 1135 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X",
c9fc04c6 1136 (unsigned)unichar, dest[-1]));
e1d6a774 1137 }
ef416fc2 1138 }
e1d6a774 1139
1140 *dest = '\0';
1141
d6ae789d 1142 vmap->used --;
e1d6a774 1143
e07d4801 1144 DEBUG_printf(("8conv_utf8_to_vbcs: Returning %d characters",
c9fc04c6
MS
1145 (int)(dest - start)));
1146
e1d6a774 1147 return ((int)(dest - start));
ef416fc2 1148}
1149
e1d6a774 1150
ef416fc2 1151/*
e1d6a774 1152 * 'conv_vbcs_to_utf8()' - Convert legacy DBCS/VBCS to UTF-8.
ef416fc2 1153 */
e1d6a774 1154
1155static int /* O - Count or -1 on error */
1156conv_vbcs_to_utf8(
1157 cups_utf8_t *dest, /* O - Target string */
1158 const cups_sbcs_t *src, /* I - Source string */
1159 int maxout, /* I - Max output */
1160 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 1161{
e1d6a774 1162 _cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */
1163 cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
1164 _cups_wide2uni_t *wide2uni; /* Pointer to row in 'wide2uni' */
1165 cups_sbcs_t leadchar; /* Lead char of n-byte legacy char */
1166 cups_vbcs_t legchar; /* Legacy character value */
1167 cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
1168 *workptr; /* Pointer into string */
ef416fc2 1169
ef416fc2 1170
1171 /*
e1d6a774 1172 * Find legacy charset map in cache...
ef416fc2 1173 */
ef416fc2 1174
e07d4801 1175 DEBUG_printf(("7conv_vbcs_to_utf8(dest=%p, src=%p, maxout=%d, encoding=%d)",
c9fc04c6
MS
1176 dest, src, maxout, encoding));
1177
d6ae789d 1178 if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL)
c9fc04c6 1179 {
e07d4801 1180 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (NULL vmap)");
c9fc04c6 1181
e1d6a774 1182 return (-1);
c9fc04c6 1183 }
ef416fc2 1184
1185 /*
e1d6a774 1186 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
ef416fc2 1187 */
ef416fc2 1188
e1d6a774 1189 work[0] = 0xfeff;
1190 for (workptr = work + 1; *src && workptr < (work + CUPS_MAX_USTRING - 1);)
ef416fc2 1191 {
e1d6a774 1192 legchar = *src++;
1193 leadchar = (cups_sbcs_t)legchar;
ef416fc2 1194
1195 /*
e1d6a774 1196 * Convert ASCII verbatim (optimization)...
ef416fc2 1197 */
ef416fc2 1198
e1d6a774 1199 if (legchar < 0x80)
ef416fc2 1200 {
e1d6a774 1201 *workptr++ = (cups_utf32_t)legchar;
c9fc04c6 1202
e07d4801 1203 DEBUG_printf(("9conv_vbcs_to_utf8: %02X => %08X", src[-1],
c9fc04c6 1204 (unsigned)legchar));
e1d6a774 1205 continue;
ef416fc2 1206 }
1207
1208 /*
e1d6a774 1209 * Convert 2-byte legacy character...
ef416fc2 1210 */
e1d6a774 1211
1212 if (vmap->lead2char[(int)leadchar] == leadchar)
ef416fc2 1213 {
e1d6a774 1214 if (!*src)
c9fc04c6 1215 {
e07d4801 1216 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string)");
c9fc04c6 1217
e1d6a774 1218 return (-1);
c9fc04c6 1219 }
e1d6a774 1220
1221 legchar = (legchar << 8) | *src++;
1222
ef416fc2 1223 /*
e1d6a774 1224 * Convert unknown character to Replacement Character...
ef416fc2 1225 */
e1d6a774 1226
1227 crow = vmap->char2uni[(int)((legchar >> 8) & 0xff)];
1228 if (crow)
1229 crow += (int) (legchar & 0xff);
1230
1231 if (!crow || !*crow)
1232 *workptr++ = 0xfffd;
1233 else
1234 *workptr++ = (cups_utf32_t)*crow;
c9fc04c6 1235
e07d4801 1236 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X => %08X",
c9fc04c6 1237 src[-2], src[-1], (unsigned)workptr[-1]));
e1d6a774 1238 continue;
ef416fc2 1239 }
1240
1241 /*
e1d6a774 1242 * Fetch 3-byte or 4-byte legacy character...
ef416fc2 1243 */
e1d6a774 1244
1245 if (vmap->lead3char[(int)leadchar] == leadchar)
ef416fc2 1246 {
e1d6a774 1247 if (!*src || !src[1])
c9fc04c6 1248 {
e07d4801 1249 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string 2)");
c9fc04c6 1250
e1d6a774 1251 return (-1);
c9fc04c6 1252 }
e1d6a774 1253
1254 legchar = (legchar << 8) | *src++;
1255 legchar = (legchar << 8) | *src++;
ef416fc2 1256 }
e1d6a774 1257 else if (vmap->lead4char[(int)leadchar] == leadchar)
1258 {
1259 if (!*src || !src[1] || !src[2])
c9fc04c6 1260 {
e07d4801 1261 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string 3)");
c9fc04c6 1262
e1d6a774 1263 return (-1);
c9fc04c6 1264 }
e1d6a774 1265
1266 legchar = (legchar << 8) | *src++;
1267 legchar = (legchar << 8) | *src++;
1268 legchar = (legchar << 8) | *src++;
1269 }
1270 else
c9fc04c6 1271 {
e07d4801 1272 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (bad character)");
c9fc04c6 1273
e1d6a774 1274 return (-1);
c9fc04c6 1275 }
ef416fc2 1276
1277 /*
e1d6a774 1278 * Find 3-byte or 4-byte legacy character...
ef416fc2 1279 */
e1d6a774 1280
1281 wide2uni = (_cups_wide2uni_t *)bsearch(&legchar,
1282 vmap->wide2uni,
1283 vmap->widecount,
1284 sizeof(_cups_wide2uni_t),
1285 compare_wide);
ef416fc2 1286
1287 /*
e1d6a774 1288 * Convert unknown character to Replacement Character...
ef416fc2 1289 */
e1d6a774 1290
1291 if (!wide2uni || !wide2uni->unichar)
1292 *workptr++ = 0xfffd;
1293 else
1294 *workptr++ = wide2uni->unichar;
c9fc04c6
MS
1295
1296 if (vmap->lead3char[(int)leadchar] == leadchar)
e07d4801 1297 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X %02X => %08X",
c9fc04c6
MS
1298 src[-3], src[-2], src[-1], (unsigned)workptr[-1]));
1299 else
e07d4801 1300 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X %02X %02X => %08X",
c9fc04c6 1301 src[-4], src[-3], src[-2], src[-1], (unsigned)workptr[-1]));
ef416fc2 1302 }
e1d6a774 1303
1304 *workptr = 0;
1305
d6ae789d 1306 vmap->used --;
e1d6a774 1307
e07d4801 1308 DEBUG_printf(("9conv_vbcs_to_utf8: Converting %d UTF-32 characters to UTF-8",
c9fc04c6
MS
1309 (int)(workptr - work)));
1310
e1d6a774 1311 /*
1312 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
1313 */
1314
1315 return (cupsUTF32ToUTF8(dest, work, maxout));
ef416fc2 1316}
1317
e1d6a774 1318
ef416fc2 1319/*
e1d6a774 1320 * 'free_sbcs_charmap()' - Free memory used by a single byte character set.
ef416fc2 1321 */
e1d6a774 1322
1323static void
1324free_sbcs_charmap(_cups_cmap_t *cmap) /* I - Character set */
ef416fc2 1325{
e1d6a774 1326 int i; /* Looping variable */
ef416fc2 1327
ef416fc2 1328
e1d6a774 1329 for (i = 0; i < 256; i ++)
1330 if (cmap->uni2char[i])
1331 free(cmap->uni2char[i]);
1332
1333 free(cmap);
1334}
1335
1336
1337/*
1338 * 'free_vbcs_charmap()' - Free memory used by a variable byte character set.
1339 */
1340
1341static void
1342free_vbcs_charmap(_cups_vmap_t *vmap) /* I - Character set */
1343{
1344 int i; /* Looping variable */
1345
1346
1347 for (i = 0; i < 256; i ++)
1348 if (vmap->char2uni[i])
1349 free(vmap->char2uni[i]);
1350
1351 for (i = 0; i < 256; i ++)
1352 if (vmap->uni2char[i])
1353 free(vmap->uni2char[i]);
1354
1355 if (vmap->wide2uni)
1356 free(vmap->wide2uni);
1357
1358 free(vmap);
1359}
1360
1361
d6ae789d 1362/*
1363 * 'get_charmap()' - Lookup or get a character set map (private).
1364 *
1365 * This code handles single-byte (SBCS), double-byte (DBCS), and
1366 * variable-byte (VBCS) character sets _without_ charset escapes...
1367 * This code does not handle multiple-byte character sets (MBCS)
1368 * (such as ISO-2022-JP) with charset switching via escapes...
1369 */
1370
1371
d09495fa 1372static void * /* O - Charset map pointer */
d6ae789d 1373get_charmap(
1374 const cups_encoding_t encoding) /* I - Encoding */
1375{
1376 char filename[1024]; /* Filename for charset map file */
1377 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1378
1379
e07d4801 1380 DEBUG_printf(("7get_charmap(encoding=%d)", encoding));
c9fc04c6 1381
d6ae789d 1382 /*
1383 * Get the data directory and charset map name...
1384 */
1385
1386 snprintf(filename, sizeof(filename), "%s/charmaps/%s.txt",
1387 cg->cups_datadir, _cupsEncodingName(encoding));
1388
e07d4801 1389 DEBUG_printf(("9get_charmap: filename=\"%s\"", filename));
d6ae789d 1390
1391 /*
1392 * Read charset map input file into cache...
1393 */
1394
1395 if (encoding < CUPS_ENCODING_SBCS_END)
1396 return (get_sbcs_charmap(encoding, filename));
1397 else if (encoding < CUPS_ENCODING_VBCS_END)
1398 return (get_vbcs_charmap(encoding, filename));
1399 else
1400 return (NULL);
1401}
1402
1403
e1d6a774 1404/*
1405 * 'get_charmap_count()' - Count lines in a charmap file.
1406 */
1407
1408static int /* O - Count or -1 on error */
1409get_charmap_count(cups_file_t *fp) /* I - File to read from */
1410{
1411 int count; /* Number of lines */
1412 char line[256]; /* Line from input map file */
ef416fc2 1413
ef416fc2 1414
1415 /*
e1d6a774 1416 * Count lines in map input file...
ef416fc2 1417 */
ef416fc2 1418
e1d6a774 1419 count = 0;
ef416fc2 1420
e1d6a774 1421 while (cupsFileGets(fp, line, sizeof(line)))
1422 if (line[0] == '0')
1423 count ++;
ef416fc2 1424
e1d6a774 1425 /*
1426 * Return the number of lines...
1427 */
1428
1429 if (count > 0)
1430 return (count);
1431 else
1432 return (-1);
ef416fc2 1433}
1434
e1d6a774 1435
ef416fc2 1436/*
e1d6a774 1437 * 'get_sbcs_charmap()' - Get SBCS Charmap.
ef416fc2 1438 */
e1d6a774 1439
1440static _cups_cmap_t * /* O - Charmap or 0 on error */
1441get_sbcs_charmap(
1442 const cups_encoding_t encoding, /* I - Charmap Encoding */
1443 const char *filename) /* I - Charmap Filename */
ef416fc2 1444{
e1d6a774 1445 unsigned long legchar; /* Legacy character value */
1446 cups_utf32_t unichar; /* Unicode character value */
1447 _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
1448 cups_file_t *fp; /* Charset map file pointer */
1449 char *s; /* Line parsing pointer */
1450 cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
1451 cups_sbcs_t *srow; /* Pointer to SBCS row in 'uni2char' */
1452 char line[256]; /* Line from charset map file */
e1d6a774 1453
ef416fc2 1454
1455 /*
e1d6a774 1456 * See if we already have this SBCS charset map loaded...
ef416fc2 1457 */
e1d6a774 1458
e07d4801 1459 DEBUG_printf(("7get_sbcs_charmap(encoding=%d, filename=\"%s\")", encoding,
c9fc04c6
MS
1460 filename));
1461
d6ae789d 1462 for (cmap = cmap_cache; cmap; cmap = cmap->next)
e1d6a774 1463 {
1464 if (cmap->encoding == encoding)
1465 {
1466 cmap->used ++;
e07d4801 1467 DEBUG_printf(("8get_sbcs_charmap: Returning existing cmap=%p", cmap));
d6ae789d 1468
e1d6a774 1469 return ((void *)cmap);
1470 }
1471 }
ef416fc2 1472
1473 /*
e1d6a774 1474 * Open SBCS charset map input file...
ef416fc2 1475 */
e1d6a774 1476
1477 if ((fp = cupsFileOpen(filename, "r")) == NULL)
c9fc04c6 1478 {
e07d4801 1479 DEBUG_printf(("8get_sbcs_charmap: Returning NULL (%s)", strerror(errno)));
c9fc04c6 1480
e1d6a774 1481 return (NULL);
c9fc04c6 1482 }
ef416fc2 1483
1484 /*
e1d6a774 1485 * Allocate memory for SBCS charset map...
ef416fc2 1486 */
e1d6a774 1487
1488 if ((cmap = (_cups_cmap_t *)calloc(1, sizeof(_cups_cmap_t))) == NULL)
1489 {
1490 cupsFileClose(fp);
e07d4801 1491 DEBUG_puts("8get_sbcs_charmap: Returning NULL (Unable to allocate memory)");
d6ae789d 1492
e1d6a774 1493 return (NULL);
1494 }
1495
1496 cmap->used ++;
1497 cmap->encoding = encoding;
ef416fc2 1498
1499 /*
e1d6a774 1500 * Save SBCS charset map into memory for transcoding...
ef416fc2 1501 */
e1d6a774 1502
1503 while (cupsFileGets(fp, line, sizeof(line)))
ef416fc2 1504 {
e1d6a774 1505 if (line[0] != '0')
1506 continue;
1507
1508 legchar = strtol(line, &s, 16);
1509 if (legchar < 0 || legchar > 0xff)
1510 goto sbcs_error;
1511
1512 unichar = strtol(s, NULL, 16);
bf3816c7 1513 if (unichar < 0 || unichar > 0x10ffff)
e1d6a774 1514 goto sbcs_error;
ef416fc2 1515
1516 /*
e1d6a774 1517 * Save legacy to Unicode mapping in direct lookup table...
ef416fc2 1518 */
e1d6a774 1519
1520 crow = cmap->char2uni + legchar;
1521 *crow = (cups_ucs2_t)(unichar & 0xffff);
ef416fc2 1522
1523 /*
e1d6a774 1524 * Save Unicode to legacy mapping in indirect lookup table...
ef416fc2 1525 */
e1d6a774 1526
1527 srow = cmap->uni2char[(unichar >> 8) & 0xff];
1528 if (!srow)
ef416fc2 1529 {
e1d6a774 1530 srow = (cups_sbcs_t *)calloc(256, sizeof(cups_sbcs_t));
1531 if (!srow)
1532 goto sbcs_error;
1533
1534 cmap->uni2char[(unichar >> 8) & 0xff] = srow;
ef416fc2 1535 }
1536
e1d6a774 1537 srow += unichar & 0xff;
1538
ef416fc2 1539 /*
e1d6a774 1540 * Convert Replacement Character to visible replacement...
ef416fc2 1541 */
e1d6a774 1542
1543 if (unichar == 0xfffd)
1544 legchar = (unsigned long)'?';
ef416fc2 1545
1546 /*
e1d6a774 1547 * First (oldest) legacy character uses Unicode mapping cell...
ef416fc2 1548 */
ef416fc2 1549
e1d6a774 1550 if (!*srow)
1551 *srow = (cups_sbcs_t)legchar;
1552 }
ef416fc2 1553
e1d6a774 1554 cupsFileClose(fp);
1555
ef416fc2 1556 /*
e1d6a774 1557 * Add it to the cache and return...
ef416fc2 1558 */
e1d6a774 1559
d6ae789d 1560 cmap->next = cmap_cache;
1561 cmap_cache = cmap;
e1d6a774 1562
e07d4801 1563 DEBUG_printf(("8get_sbcs_charmap: Returning new cmap=%p", cmap));
e1d6a774 1564
1565 return (cmap);
ef416fc2 1566
1567 /*
e1d6a774 1568 * If we get here, there was an error in the cmap file...
ef416fc2 1569 */
e1d6a774 1570
1571 sbcs_error:
1572
1573 free_sbcs_charmap(cmap);
1574
1575 cupsFileClose(fp);
1576
e07d4801 1577 DEBUG_puts("8get_sbcs_charmap: Returning NULL (Read/format error)");
e1d6a774 1578
1579 return (NULL);
1580}
1581
1582
1583/*
1584 * 'get_vbcs_charmap()' - Get DBCS/VBCS Charmap.
1585 */
1586
1587static _cups_vmap_t * /* O - Charmap or 0 on error */
1588get_vbcs_charmap(
1589 const cups_encoding_t encoding, /* I - Charmap Encoding */
1590 const char *filename) /* I - Charmap Filename */
1591{
1592 _cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */
1593 cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
1594 cups_vbcs_t *vrow; /* Pointer to VBCS row in 'uni2char' */
1595 _cups_wide2uni_t *wide2uni; /* Pointer to row in 'wide2uni' */
1596 cups_sbcs_t leadchar; /* Lead char of 2-byte legacy char */
1597 unsigned long legchar; /* Legacy character value */
1598 cups_utf32_t unichar; /* Unicode character value */
1599 int mapcount; /* Count of lines in charmap file */
1600 cups_file_t *fp; /* Charset map file pointer */
1601 char *s; /* Line parsing pointer */
1602 char line[256]; /* Line from charset map file */
1603 int i; /* Loop variable */
09a101d6 1604 int legacy; /* 32-bit legacy char */
e1d6a774 1605
1606
e07d4801 1607 DEBUG_printf(("7get_vbcs_charmap(encoding=%d, filename=\"%s\")\n",
e1d6a774 1608 encoding, filename));
ef416fc2 1609
1610 /*
e1d6a774 1611 * See if we already have this DBCS/VBCS charset map loaded...
ef416fc2 1612 */
ef416fc2 1613
d6ae789d 1614 for (vmap = vmap_cache; vmap; vmap = vmap->next)
e1d6a774 1615 {
1616 if (vmap->encoding == encoding)
ef416fc2 1617 {
e1d6a774 1618 vmap->used ++;
e07d4801 1619 DEBUG_printf(("8get_vbcs_charmap: Returning existing vmap=%p", vmap));
d6ae789d 1620
e1d6a774 1621 return ((void *)vmap);
ef416fc2 1622 }
ef416fc2 1623 }
ef416fc2 1624
1625 /*
e1d6a774 1626 * Open VBCS charset map input file...
ef416fc2 1627 */
ef416fc2 1628
e1d6a774 1629 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1630 {
e07d4801 1631 DEBUG_printf(("8get_vbcs_charmap: Returning NULL (%s)", strerror(errno)));
d6ae789d 1632
e1d6a774 1633 return (NULL);
1634 }
ef416fc2 1635
1636 /*
e1d6a774 1637 * Count lines in charmap file...
ef416fc2 1638 */
e1d6a774 1639
1640 if ((mapcount = get_charmap_count(fp)) <= 0)
1641 {
e07d4801 1642 DEBUG_puts("8get_vbcs_charmap: Unable to get charmap count!");
d6ae789d 1643
91c84a35
MS
1644 cupsFileClose(fp);
1645
e1d6a774 1646 return (NULL);
1647 }
1648
e07d4801 1649 DEBUG_printf(("8get_vbcs_charmap: mapcount=%d", mapcount));
ef416fc2 1650
1651 /*
e1d6a774 1652 * Allocate memory for DBCS/VBCS charset map...
ef416fc2 1653 */
e1d6a774 1654
1655 if ((vmap = (_cups_vmap_t *)calloc(1, sizeof(_cups_vmap_t))) == NULL)
1656 {
e07d4801 1657 DEBUG_puts("8get_vbcs_charmap: Unable to allocate memory!");
d6ae789d 1658
91c84a35
MS
1659 cupsFileClose(fp);
1660
e1d6a774 1661 return (NULL);
1662 }
1663
1664 vmap->used ++;
1665 vmap->encoding = encoding;
ef416fc2 1666
1667 /*
e1d6a774 1668 * Save DBCS/VBCS charset map into memory for transcoding...
ef416fc2 1669 */
e1d6a774 1670
e1d6a774 1671 wide2uni = NULL;
1672
1673 cupsFileRewind(fp);
1674
09a101d6 1675 i = 0;
1676 legacy = 0;
e1d6a774 1677
1678 while (cupsFileGets(fp, line, sizeof(line)))
ef416fc2 1679 {
e1d6a774 1680 if (line[0] != '0')
1681 continue;
1682
1683 legchar = strtoul(line, &s, 16);
1684 if (legchar == ULONG_MAX)
1685 goto vbcs_error;
1686
1687 unichar = strtol(s, NULL, 16);
bf3816c7 1688 if (unichar < 0 || unichar > 0x10ffff)
e1d6a774 1689 goto vbcs_error;
1690
1691 i ++;
1692
e07d4801
MS
1693 DEBUG_printf(("9get_vbcs_charmap: i=%d, legchar=0x%08lx, unichar=0x%04x", i,
1694 legchar, (unsigned)unichar));
ef416fc2 1695
1696 /*
e1d6a774 1697 * Save lead char of 2/3/4-byte legacy char...
ef416fc2 1698 */
e1d6a774 1699
c9fc04c6 1700 if (legchar > 0xffffff)
ef416fc2 1701 {
c9fc04c6
MS
1702 leadchar = (cups_sbcs_t)(legchar >> 24);
1703 vmap->lead4char[leadchar] = leadchar;
e1d6a774 1704 }
c9fc04c6 1705 else if (legchar > 0xffff)
e1d6a774 1706 {
1707 leadchar = (cups_sbcs_t)(legchar >> 16);
1708 vmap->lead3char[leadchar] = leadchar;
1709 }
bf3816c7 1710 else
e1d6a774 1711 {
c9fc04c6
MS
1712 leadchar = (cups_sbcs_t)(legchar >> 8);
1713 vmap->lead2char[leadchar] = leadchar;
ef416fc2 1714 }
1715
1716 /*
e1d6a774 1717 * Save Legacy to Unicode mapping...
ef416fc2 1718 */
e1d6a774 1719
1720 if (legchar <= 0xffff)
ef416fc2 1721 {
ef416fc2 1722 /*
e1d6a774 1723 * Save DBCS 16-bit to Unicode mapping in indirect lookup table...
ef416fc2 1724 */
e1d6a774 1725
1726 crow = vmap->char2uni[(int)leadchar];
1727 if (!crow)
1728 {
1729 crow = (cups_ucs2_t *)calloc(256, sizeof(cups_ucs2_t));
1730 if (!crow)
1731 goto vbcs_error;
1732
1733 vmap->char2uni[(int)leadchar] = crow;
1734 }
1735
1736 crow[(int)(legchar & 0xff)] = (cups_ucs2_t)unichar;
1737 }
1738 else
1739 {
1740 /*
1741 * Save VBCS 32-bit to Unicode mapping in sorted list table...
1742 */
1743
09a101d6 1744 if (!legacy)
e1d6a774 1745 {
09a101d6 1746 legacy = 1;
e1d6a774 1747 vmap->widecount = (mapcount - i + 1);
1748 wide2uni = (_cups_wide2uni_t *)calloc(vmap->widecount,
1749 sizeof(_cups_wide2uni_t));
1750 if (!wide2uni)
1751 goto vbcs_error;
1752
1753 vmap->wide2uni = wide2uni;
1754 }
1755
1756 wide2uni->widechar = (cups_vbcs_t)legchar;
1757 wide2uni->unichar = (cups_ucs2_t)unichar;
1758 wide2uni ++;
ef416fc2 1759 }
1760
1761 /*
e1d6a774 1762 * Save Unicode to legacy mapping in indirect lookup table...
ef416fc2 1763 */
e1d6a774 1764
1765 vrow = vmap->uni2char[(int)((unichar >> 8) & 0xff)];
1766 if (!vrow)
ef416fc2 1767 {
e1d6a774 1768 vrow = (cups_vbcs_t *)calloc(256, sizeof(cups_vbcs_t));
1769 if (!vrow)
1770 goto vbcs_error;
1771
1772 vmap->uni2char[(int) ((unichar >> 8) & 0xff)] = vrow;
ef416fc2 1773 }
e1d6a774 1774
1775 vrow += (int)(unichar & 0xff);
ef416fc2 1776
1777 /*
e1d6a774 1778 * Convert Replacement Character to visible replacement...
ef416fc2 1779 */
e1d6a774 1780
1781 if (unichar == 0xfffd)
1782 legchar = (unsigned long)'?';
ef416fc2 1783
1784 /*
e1d6a774 1785 * First (oldest) legacy character uses Unicode mapping cell...
ef416fc2 1786 */
e1d6a774 1787
1788 if (!*vrow)
1789 *vrow = (cups_vbcs_t)legchar;
ef416fc2 1790 }
e1d6a774 1791
1792 vmap->charcount = (i - vmap->widecount);
1793
1794 cupsFileClose(fp);
ef416fc2 1795
1796 /*
e1d6a774 1797 * Add it to the cache and return...
ef416fc2 1798 */
ef416fc2 1799
c9fc04c6 1800 vmap->next = vmap_cache;
d6ae789d 1801 vmap_cache = vmap;
e1d6a774 1802
e07d4801 1803 DEBUG_printf(("8get_vbcs_charmap: Returning new vmap=%p", vmap));
e1d6a774 1804
1805 return (vmap);
1806
1807 /*
1808 * If we get here, the file contains errors...
1809 */
1810
1811 vbcs_error:
1812
1813 free_vbcs_charmap(vmap);
1814
1815 cupsFileClose(fp);
1816
e07d4801 1817 DEBUG_puts("8get_vbcs_charmap: Returning NULL (Read/format error)");
e1d6a774 1818
1819 return (NULL);
ef416fc2 1820}
1821
1822
1823/*
75bd9771 1824 * End of "$Id: transcode.c 7560 2008-05-13 06:34:04Z mike $"
ef416fc2 1825 */