]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/transcode.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / transcode.c
1 /*
2 * "$Id: transcode.c 6038 2006-10-14 15:53:10Z mike $"
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 *
26 * _cupsCharmapFlush() - Flush all character set maps out of cache.
27 * _cupsCharmapFree() - Free a character set map.
28 * _cupsCharmapGet() - Get a character set map.
29 * cupsCharsetToUTF8() - Convert legacy character set to UTF-8.
30 * cupsUTF8ToCharset() - Convert UTF-8 to legacy character set.
31 * cupsUTF8ToUTF32() - Convert UTF-8 to UTF-32.
32 * cupsUTF32ToUTF8() - Convert UTF-32 to UTF-8.
33 * compare_wide() - Compare key for wide (VBCS) match.
34 * conv_sbcs_to_utf8() - Convert legacy SBCS to UTF-8.
35 * conv_utf8_to_sbcs() - Convert UTF-8 to legacy SBCS.
36 * conv_utf8_to_vbcs() - Convert UTF-8 to legacy DBCS/VBCS.
37 * conv_vbcs_to_utf8() - Convert legacy DBCS/VBCS to UTF-8.
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.
40 * get_charmap() - Lookup or get a character set map (private).
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.
44 */
45
46 /*
47 * Include necessary headers...
48 */
49
50 #include "globals.h"
51 #include "debug.h"
52 #include <limits.h>
53 #include <stdlib.h>
54 #include <errno.h>
55 #include <time.h>
56
57
58 /*
59 * Local globals...
60 */
61
62 #ifdef HAVE_PTHREAD_H
63 static pthread_mutex_t map_mutex = PTHREAD_MUTEX_INITIALIZER;
64 /* Mutex to control access to maps */
65 #endif /* HAVE_PTHREAD_H */
66 static _cups_cmap_t *cmap_cache = NULL;
67 /* SBCS Charmap Cache */
68 static _cups_vmap_t *vmap_cache = NULL;
69 /* VBCS Charmap Cache */
70
71
72 /*
73 * Local functions...
74 */
75
76 static int compare_wide(const void *k1, const void *k2);
77 static int conv_sbcs_to_utf8(cups_utf8_t *dest,
78 const cups_sbcs_t *src,
79 int maxout,
80 const cups_encoding_t encoding);
81 static int conv_utf8_to_sbcs(cups_sbcs_t *dest,
82 const cups_utf8_t *src,
83 int maxout,
84 const cups_encoding_t encoding);
85 static int conv_utf8_to_vbcs(cups_sbcs_t *dest,
86 const cups_utf8_t *src,
87 int maxout,
88 const cups_encoding_t encoding);
89 static int conv_vbcs_to_utf8(cups_utf8_t *dest,
90 const cups_sbcs_t *src,
91 int maxout,
92 const cups_encoding_t encoding);
93 static void free_sbcs_charmap(_cups_cmap_t *sbcs);
94 static void free_vbcs_charmap(_cups_vmap_t *vbcs);
95 static void *get_charmap(const cups_encoding_t encoding);
96 static int get_charmap_count(cups_file_t *fp);
97 static _cups_cmap_t *get_sbcs_charmap(const cups_encoding_t encoding,
98 const char *filename);
99 static _cups_vmap_t *get_vbcs_charmap(const cups_encoding_t encoding,
100 const char *filename);
101
102
103 /*
104 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
105 */
106
107 void
108 _cupsCharmapFlush(void)
109 {
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 */
114
115
116 #ifdef HAVE_PTHREAD_H
117 pthread_mutex_lock(&map_mutex);
118 #endif /* HAVE_PTHREAD_H */
119
120 /*
121 * Loop through SBCS charset map cache, free all memory...
122 */
123
124 for (cmap = cmap_cache; cmap; cmap = cnext)
125 {
126 cnext = cmap->next;
127
128 free_sbcs_charmap(cmap);
129 }
130
131 cmap_cache = NULL;
132
133 /*
134 * Loop through DBCS/VBCS charset map cache, free all memory...
135 */
136
137 for (vmap = vmap_cache; vmap; vmap = vnext)
138 {
139 vnext = vmap->next;
140
141 free_vbcs_charmap(vmap);
142
143 free(vmap);
144 }
145
146 vmap_cache = NULL;
147
148 #ifdef HAVE_PTHREAD_H
149 pthread_mutex_unlock(&map_mutex);
150 #endif /* HAVE_PTHREAD_H */
151 }
152
153
154 /*
155 * '_cupsCharmapFree()' - Free a character set map.
156 *
157 * This does not actually free; use '_cupsCharmapFlush()' for that.
158 */
159
160 void
161 _cupsCharmapFree(
162 const cups_encoding_t encoding) /* I - Encoding */
163 {
164 _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
165 _cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */
166
167
168 /*
169 * See if we already have this SBCS charset map loaded...
170 */
171
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)
177 {
178 if (cmap->encoding == encoding)
179 {
180 if (cmap->used > 0)
181 cmap->used --;
182 break;
183 }
184 }
185
186 /*
187 * See if we already have this DBCS/VBCS charset map loaded...
188 */
189
190 for (vmap = vmap_cache; vmap; vmap = vmap->next)
191 {
192 if (vmap->encoding == encoding)
193 {
194 if (vmap->used > 0)
195 vmap->used --;
196 break;
197 }
198 }
199
200 #ifdef HAVE_PTHREAD_H
201 pthread_mutex_unlock(&map_mutex);
202 #endif /* HAVE_PTHREAD_H */
203 }
204
205
206 /*
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...
213 */
214
215 void * /* O - Charset map pointer */
216 _cupsCharmapGet(
217 const cups_encoding_t encoding) /* I - Encoding */
218 {
219 void *charmap; /* Charset map pointer */
220
221
222 DEBUG_printf(("_cupsCharmapGet(encoding=%d)\n", encoding));
223
224 /*
225 * Check for valid arguments...
226 */
227
228 if (encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END)
229 {
230 DEBUG_puts(" Bad encoding, returning NULL!");
231 return (NULL);
232 }
233
234 /*
235 * Lookup or get the charset map pointer and return...
236 */
237
238 #ifdef HAVE_PTHREAD_H
239 pthread_mutex_lock(&map_mutex);
240 #endif /* HAVE_PTHREAD_H */
241
242 charmap = get_charmap(encoding);
243
244 #ifdef HAVE_PTHREAD_H
245 pthread_mutex_unlock(&map_mutex);
246 #endif /* HAVE_PTHREAD_H */
247
248 return (charmap);
249 }
250
251
252 /*
253 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
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 */
260
261 int /* O - Count or -1 on error */
262 cupsCharsetToUTF8(
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 */
267 {
268 int bytes; /* Number of bytes converted */
269
270
271 /*
272 * Check for valid arguments...
273 */
274
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
281 if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
282 {
283 DEBUG_puts(" Bad arguments, returning -1");
284 return (-1);
285 }
286
287 /*
288 * Handle identity conversions...
289 */
290
291 if (encoding == CUPS_UTF8 ||
292 encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END)
293 {
294 strlcpy((char *)dest, src, maxout);
295 return (strlen((char *)dest));
296 }
297
298 /*
299 * Convert input legacy charset to UTF-8...
300 */
301
302 #ifdef HAVE_PTHREAD_H
303 pthread_mutex_lock(&map_mutex);
304 #endif /* HAVE_PTHREAD_H */
305
306 if (encoding < CUPS_ENCODING_SBCS_END)
307 bytes = conv_sbcs_to_utf8(dest, (cups_sbcs_t *)src, maxout, encoding);
308 else if (encoding < CUPS_ENCODING_VBCS_END)
309 bytes = conv_vbcs_to_utf8(dest, (cups_sbcs_t *)src, maxout, encoding);
310 else
311 {
312 DEBUG_puts(" Bad encoding, returning -1");
313 bytes = -1;
314 }
315
316 #ifdef HAVE_PTHREAD_H
317 pthread_mutex_unlock(&map_mutex);
318 #endif /* HAVE_PTHREAD_H */
319
320 return (bytes);
321 }
322
323
324 /*
325 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
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 */
332
333 int /* O - Count or -1 on error */
334 cupsUTF8ToCharset(
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 */
339 {
340 int bytes; /* Number of bytes converted */
341
342
343 /*
344 * Check for valid arguments...
345 */
346
347 if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
348 {
349 if (dest)
350 *dest = '\0';
351
352 return (-1);
353 }
354
355 /*
356 * Handle identity conversions...
357 */
358
359 if (encoding == CUPS_UTF8 ||
360 encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END)
361 {
362 strlcpy(dest, (char *)src, maxout);
363 return (strlen(dest));
364 }
365
366 /*
367 * Convert input UTF-8 to legacy charset...
368 */
369
370 #ifdef HAVE_PTHREAD_H
371 pthread_mutex_lock(&map_mutex);
372 #endif /* HAVE_PTHREAD_H */
373
374 if (encoding < CUPS_ENCODING_SBCS_END)
375 bytes = conv_utf8_to_sbcs((cups_sbcs_t *)dest, src, maxout, encoding);
376 else if (encoding < CUPS_ENCODING_VBCS_END)
377 bytes = conv_utf8_to_vbcs((cups_sbcs_t *)dest, src, maxout, encoding);
378 else
379 bytes = -1;
380
381 #ifdef HAVE_PTHREAD_H
382 pthread_mutex_unlock(&map_mutex);
383 #endif /* HAVE_PTHREAD_H */
384
385 return (bytes);
386 }
387
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 * --------------------------------------------------
396 * 0 to 127 = 0xxxxxxx (US-ASCII)
397 * 128 to 2047 = 110xxxxx 10yyyyyy
398 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
399 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
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...
403 */
404
405 int /* O - Count or -1 on error */
406 cupsUTF8ToUTF32(
407 cups_utf32_t *dest, /* O - Target string */
408 const cups_utf8_t *src, /* I - Source string */
409 const int maxout) /* I - Max output */
410 {
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
416
417 /*
418 * Check for valid arguments and clear output...
419 */
420
421 if (dest)
422 *dest = 0;
423
424 if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
425 return (-1);
426
427 /*
428 * Convert input UTF-8 to output UTF-32 (and insert BOM)...
429 */
430
431 *dest++ = 0xfeff;
432
433 for (i = maxout - 1; *src && i > 0; i --)
434 {
435 ch = *src++;
436
437 /*
438 * Convert UTF-8 character(s) to UTF-32 character...
439 */
440
441 if (!(ch & 0x80))
442 {
443 /*
444 * One-octet UTF-8 <= 127 (US-ASCII)...
445 */
446
447 *dest++ = ch;
448 continue;
449 }
450 else if ((ch & 0xe0) == 0xc0)
451 {
452 /*
453 * Two-octet UTF-8 <= 2047 (Latin-x)...
454 */
455
456 next = *src++;
457 if (!next)
458 return (-1);
459
460 ch32 = ((ch & 0x1f) << 6) | (next & 0x3f);
461
462 /*
463 * Check for non-shortest form (invalid UTF-8)...
464 */
465
466 if (ch32 < 0x80)
467 return (-1);
468
469 *dest++ = ch32;
470 }
471 else if ((ch & 0xf0) == 0xe0)
472 {
473 /*
474 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
475 */
476
477 next = *src++;
478 if (!next)
479 return (-1);
480
481 ch32 = ((ch & 0x0f) << 6) | (next & 0x3f);
482
483 next = *src++;
484 if (!next)
485 return (-1);
486
487 ch32 = (ch32 << 6) | (next & 0x3f);
488
489 /*
490 * Check for non-shortest form (invalid UTF-8)...
491 */
492
493 if (ch32 < 0x800)
494 return (-1);
495
496 *dest++ = ch32;
497 }
498 else if ((ch & 0xf8) == 0xf0)
499 {
500 /*
501 * Four-octet UTF-8...
502 */
503
504 next = *src++;
505 if (!next)
506 return (-1);
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
522 /*
523 * Check for non-shortest form (invalid UTF-8)...
524 */
525
526 if (ch32 < 0x10000)
527 return (-1);
528
529 *dest++ = ch32;
530 }
531 else
532 {
533 /*
534 * More than 4-octet (invalid UTF-8 sequence)...
535 */
536
537 return (-1);
538 }
539
540 /*
541 * Check for UTF-16 surrogate (illegal UTF-8)...
542 */
543
544 if (ch32 >= 0xd800 && ch32 <= 0xdfff)
545 return (-1);
546 }
547
548 *dest = 0;
549
550 return (i);
551 }
552
553
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 * --------------------------------------------------
561 * 0 to 127 = 0xxxxxxx (US-ASCII)
562 * 128 to 2047 = 110xxxxx 10yyyyyy
563 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
564 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
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...
568 */
569
570 int /* O - Count or -1 on error */
571 cupsUTF32ToUTF8(
572 cups_utf8_t *dest, /* O - Target string */
573 const cups_utf32_t *src, /* I - Source string */
574 const int maxout) /* I - Max output */
575 {
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
581
582 /*
583 * Check for valid arguments and clear output...
584 */
585
586 if (dest)
587 *dest = '\0';
588
589 if (!dest || !src || maxout < 1)
590 return (-1);
591
592 /*
593 * Check for leading BOM in UTF-32 and inverted BOM...
594 */
595
596 start = dest;
597 swap = *src == 0xfffe0000;
598
599 if (*src == 0xfffe0000 || *src == 0xfeff)
600 src ++;
601
602 /*
603 * Convert input UTF-32 to output UTF-8...
604 */
605
606 for (i = maxout - 1; *src && i > 0;)
607 {
608 ch = *src++;
609
610 /*
611 * Byte swap input UTF-32, if necessary...
612 * (only byte-swapping 24 of 32 bits)
613 */
614
615 if (swap)
616 ch = ((ch >> 24) | ((ch >> 8) & 0xff00) | ((ch << 8) & 0xff0000));
617
618 /*
619 * Check for beyond Plane 16 (invalid UTF-32)...
620 */
621
622 if (ch > 0x10ffff)
623 return (-1);
624
625 /*
626 * Convert UTF-32 character to UTF-8 character(s)...
627 */
628
629 if (ch < 0x80)
630 {
631 /*
632 * One-octet UTF-8 <= 127 (US-ASCII)...
633 */
634
635 *dest++ = (cups_utf8_t)ch;
636 i --;
637 }
638 else if (ch < 0x800)
639 {
640 /*
641 * Two-octet UTF-8 <= 2047 (Latin-x)...
642 */
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;
650 }
651 else if (ch < 0x10000)
652 {
653 /*
654 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
655 */
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;
679 }
680 }
681
682 *dest = '\0';
683
684 return ((int)(dest - start));
685 }
686
687
688 /*
689 * 'compare_wide()' - Compare key for wide (VBCS) match.
690 */
691
692 static int
693 compare_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.
709 */
710
711 static int /* O - Count or -1 on error */
712 conv_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 */
717 {
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
724
725 /*
726 * Find legacy charset map in cache...
727 */
728
729 if ((cmap = (_cups_cmap_t *)get_charmap(encoding)) == NULL)
730 return (-1);
731
732 /*
733 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
734 */
735
736 work[0] = 0xfeff;
737 for (workptr = work + 1; *src && workptr < (work + CUPS_MAX_USTRING - 1);)
738 {
739 legchar = *src++;
740
741 /*
742 * Convert ASCII verbatim (optimization)...
743 */
744
745 if (legchar < 0x80)
746 *workptr++ = (cups_utf32_t)legchar;
747 else
748 {
749 /*
750 * Convert unknown character to Replacement Character...
751 */
752
753 crow = cmap->char2uni + legchar;
754
755 if (!*crow)
756 *workptr++ = 0xfffd;
757 else
758 *workptr++ = (cups_utf32_t)*crow;
759 }
760 }
761
762 *workptr = 0;
763
764 /*
765 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
766 */
767
768 cmap->used --;
769
770 return (cupsUTF32ToUTF8(dest, work, maxout));
771 }
772
773
774 /*
775 * 'conv_utf8_to_sbcs()' - Convert UTF-8 to legacy SBCS.
776 */
777
778 static int /* O - Count or -1 on error */
779 conv_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 */
784 {
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
792
793 /*
794 * Find legacy charset map in cache...
795 */
796
797 if ((cmap = (_cups_cmap_t *)get_charmap(encoding)) == NULL)
798 return (-1);
799
800 /*
801 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
802 */
803
804 if (cupsUTF8ToUTF32(work, src, CUPS_MAX_USTRING) < 0)
805 return (-1);
806
807 /*
808 * Convert internal UCS-4 to SBCS legacy charset (and delete BOM)...
809 */
810
811 for (workptr = work + 1, start = dest; *workptr && maxout > 1; maxout --)
812 {
813 unichar = *workptr++;
814 if (!unichar)
815 break;
816
817 /*
818 * Convert ASCII verbatim (optimization)...
819 */
820
821 if (unichar < 0x80)
822 {
823 *dest++ = (cups_sbcs_t)unichar;
824 continue;
825 }
826
827 /*
828 * Convert unknown character to visible replacement...
829 */
830
831 srow = cmap->uni2char[(int)((unichar >> 8) & 0xff)];
832
833 if (srow)
834 srow += (int)(unichar & 0xff);
835
836 if (!srow || !*srow)
837 *dest++ = '?';
838 else
839 *dest++ = *srow;
840 }
841
842 *dest = '\0';
843
844 cmap->used --;
845
846 return ((int)(dest - start));
847 }
848
849
850 /*
851 * 'conv_utf8_to_vbcs()' - Convert UTF-8 to legacy DBCS/VBCS.
852 */
853
854 static int /* O - Count or -1 on error */
855 conv_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 */
860 {
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 */
868
869
870 /*
871 * Find legacy charset map in cache...
872 */
873
874 if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL)
875 return (-1);
876
877 /*
878 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
879 */
880
881 if (cupsUTF8ToUTF32(work, src, CUPS_MAX_USTRING) < 0)
882 return (-1);
883
884 /*
885 * Convert internal UCS-4 to VBCS legacy charset (and delete BOM)...
886 */
887
888 for (start = dest, workptr = work + 1; *workptr && maxout > 1; maxout --)
889 {
890 unichar = *workptr++;
891 if (!unichar)
892 break;
893
894 /*
895 * Convert ASCII verbatim (optimization)...
896 */
897
898 if (unichar < 0x80)
899 {
900 *dest++ = (cups_vbcs_t)unichar;
901 continue;
902 }
903
904 /*
905 * Convert unknown character to visible replacement...
906 */
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;
917
918 /*
919 * Save n-byte legacy character...
920 */
921
922 if (legchar > 0xffffff)
923 {
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;
933 }
934 else if (legchar > 0xffff)
935 {
936 if (maxout < 4)
937 return (-1);
938
939 *dest++ = (cups_sbcs_t)(legchar >> 16);
940 *dest++ = (cups_sbcs_t)(legchar >> 8);
941 *dest++ = (cups_sbcs_t)legchar;
942
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 }
952 }
953
954 *dest = '\0';
955
956 vmap->used --;
957
958 return ((int)(dest - start));
959 }
960
961
962 /*
963 * 'conv_vbcs_to_utf8()' - Convert legacy DBCS/VBCS to UTF-8.
964 */
965
966 static int /* O - Count or -1 on error */
967 conv_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 */
972 {
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 */
980
981
982 /*
983 * Find legacy charset map in cache...
984 */
985
986 if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL)
987 return (-1);
988
989 /*
990 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
991 */
992
993 work[0] = 0xfeff;
994 for (workptr = work + 1; *src && workptr < (work + CUPS_MAX_USTRING - 1);)
995 {
996 legchar = *src++;
997 leadchar = (cups_sbcs_t)legchar;
998
999 /*
1000 * Convert ASCII verbatim (optimization)...
1001 */
1002
1003 if (legchar < 0x80)
1004 {
1005 *workptr++ = (cups_utf32_t)legchar;
1006 continue;
1007 }
1008
1009 /*
1010 * Convert 2-byte legacy character...
1011 */
1012
1013 if (vmap->lead2char[(int)leadchar] == leadchar)
1014 {
1015 if (!*src)
1016 return (-1);
1017
1018 legchar = (legchar << 8) | *src++;
1019
1020 /*
1021 * Convert unknown character to Replacement Character...
1022 */
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;
1033 }
1034
1035 /*
1036 * Fetch 3-byte or 4-byte legacy character...
1037 */
1038
1039 if (vmap->lead3char[(int)leadchar] == leadchar)
1040 {
1041 if (!*src || !src[1])
1042 return (-1);
1043
1044 legchar = (legchar << 8) | *src++;
1045 legchar = (legchar << 8) | *src++;
1046 }
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);
1058
1059 /*
1060 * Find 3-byte or 4-byte legacy character...
1061 */
1062
1063 wide2uni = (_cups_wide2uni_t *)bsearch(&legchar,
1064 vmap->wide2uni,
1065 vmap->widecount,
1066 sizeof(_cups_wide2uni_t),
1067 compare_wide);
1068
1069 /*
1070 * Convert unknown character to Replacement Character...
1071 */
1072
1073 if (!wide2uni || !wide2uni->unichar)
1074 *workptr++ = 0xfffd;
1075 else
1076 *workptr++ = wide2uni->unichar;
1077 }
1078
1079 *workptr = 0;
1080
1081 vmap->used --;
1082
1083 /*
1084 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
1085 */
1086
1087 return (cupsUTF32ToUTF8(dest, work, maxout));
1088 }
1089
1090
1091 /*
1092 * 'free_sbcs_charmap()' - Free memory used by a single byte character set.
1093 */
1094
1095 static void
1096 free_sbcs_charmap(_cups_cmap_t *cmap) /* I - Character set */
1097 {
1098 int i; /* Looping variable */
1099
1100
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
1113 static void
1114 free_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
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
1144 static void * /* O - Charset map pointer */
1145 get_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
1174 /*
1175 * 'get_charmap_count()' - Count lines in a charmap file.
1176 */
1177
1178 static int /* O - Count or -1 on error */
1179 get_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 */
1183
1184
1185 /*
1186 * Count lines in map input file...
1187 */
1188
1189 count = 0;
1190
1191 while (cupsFileGets(fp, line, sizeof(line)))
1192 if (line[0] == '0')
1193 count ++;
1194
1195 /*
1196 * Return the number of lines...
1197 */
1198
1199 if (count > 0)
1200 return (count);
1201 else
1202 return (-1);
1203 }
1204
1205
1206 /*
1207 * 'get_sbcs_charmap()' - Get SBCS Charmap.
1208 */
1209
1210 static _cups_cmap_t * /* O - Charmap or 0 on error */
1211 get_sbcs_charmap(
1212 const cups_encoding_t encoding, /* I - Charmap Encoding */
1213 const char *filename) /* I - Charmap Filename */
1214 {
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 */
1223
1224
1225 /*
1226 * See if we already have this SBCS charset map loaded...
1227 */
1228
1229 for (cmap = cmap_cache; cmap; cmap = cmap->next)
1230 {
1231 if (cmap->encoding == encoding)
1232 {
1233 cmap->used ++;
1234 DEBUG_printf((" returning existing cmap=%p\n", cmap));
1235
1236 return ((void *)cmap);
1237 }
1238 }
1239
1240 /*
1241 * Open SBCS charset map input file...
1242 */
1243
1244 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1245 return (NULL);
1246
1247 /*
1248 * Allocate memory for SBCS charset map...
1249 */
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!");
1255
1256 return (NULL);
1257 }
1258
1259 cmap->used ++;
1260 cmap->encoding = encoding;
1261
1262 /*
1263 * Save SBCS charset map into memory for transcoding...
1264 */
1265
1266 while (cupsFileGets(fp, line, sizeof(line)))
1267 {
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;
1278
1279 /*
1280 * Save legacy to Unicode mapping in direct lookup table...
1281 */
1282
1283 crow = cmap->char2uni + legchar;
1284 *crow = (cups_ucs2_t)(unichar & 0xffff);
1285
1286 /*
1287 * Save Unicode to legacy mapping in indirect lookup table...
1288 */
1289
1290 srow = cmap->uni2char[(unichar >> 8) & 0xff];
1291 if (!srow)
1292 {
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;
1298 }
1299
1300 srow += unichar & 0xff;
1301
1302 /*
1303 * Convert Replacement Character to visible replacement...
1304 */
1305
1306 if (unichar == 0xfffd)
1307 legchar = (unsigned long)'?';
1308
1309 /*
1310 * First (oldest) legacy character uses Unicode mapping cell...
1311 */
1312
1313 if (!*srow)
1314 *srow = (cups_sbcs_t)legchar;
1315 }
1316
1317 cupsFileClose(fp);
1318
1319 /*
1320 * Add it to the cache and return...
1321 */
1322
1323 cmap->next = cmap_cache;
1324 cmap_cache = cmap;
1325
1326 DEBUG_printf((" returning new cmap=%p\n", cmap));
1327
1328 return (cmap);
1329
1330 /*
1331 * If we get here, there was an error in the cmap file...
1332 */
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
1350 static _cups_vmap_t * /* O - Charmap or 0 on error */
1351 get_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 */
1368
1369
1370 DEBUG_printf(("get_vbcs_charmap(encoding=%d, filename=\"%s\")\n",
1371 encoding, filename));
1372
1373 /*
1374 * See if we already have this DBCS/VBCS charset map loaded...
1375 */
1376
1377 for (vmap = vmap_cache; vmap; vmap = vmap->next)
1378 {
1379 if (vmap->encoding == encoding)
1380 {
1381 vmap->used ++;
1382 DEBUG_printf((" returning existing vmap=%p\n", vmap));
1383
1384 return ((void *)vmap);
1385 }
1386 }
1387
1388 /*
1389 * Open VBCS charset map input file...
1390 */
1391
1392 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1393 {
1394 DEBUG_printf((" Unable to open file: %s\n", strerror(errno)));
1395
1396 return (NULL);
1397 }
1398
1399 /*
1400 * Count lines in charmap file...
1401 */
1402
1403 if ((mapcount = get_charmap_count(fp)) <= 0)
1404 {
1405 DEBUG_puts(" Unable to get charmap count!");
1406
1407 return (NULL);
1408 }
1409
1410 DEBUG_printf((" mapcount=%d\n", mapcount));
1411
1412 /*
1413 * Allocate memory for DBCS/VBCS charset map...
1414 */
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!");
1420
1421 return (NULL);
1422 }
1423
1424 vmap->used ++;
1425 vmap->encoding = encoding;
1426
1427 /*
1428 * Save DBCS/VBCS charset map into memory for transcoding...
1429 */
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)))
1440 {
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)); */
1456
1457 /*
1458 * Save lead char of 2/3/4-byte legacy char...
1459 */
1460
1461 if (legchar > 0xff && legchar <= 0xffff)
1462 {
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;
1477 }
1478
1479 /*
1480 * Save Legacy to Unicode mapping...
1481 */
1482
1483 if (legchar <= 0xffff)
1484 {
1485 /*
1486 * Save DBCS 16-bit to Unicode mapping in indirect lookup table...
1487 */
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 ++;
1522 }
1523
1524 /*
1525 * Save Unicode to legacy mapping in indirect lookup table...
1526 */
1527
1528 vrow = vmap->uni2char[(int)((unichar >> 8) & 0xff)];
1529 if (!vrow)
1530 {
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;
1536 }
1537
1538 vrow += (int)(unichar & 0xff);
1539
1540 /*
1541 * Convert Replacement Character to visible replacement...
1542 */
1543
1544 if (unichar == 0xfffd)
1545 legchar = (unsigned long)'?';
1546
1547 /*
1548 * First (oldest) legacy character uses Unicode mapping cell...
1549 */
1550
1551 if (!*vrow)
1552 *vrow = (cups_vbcs_t)legchar;
1553 }
1554
1555 vmap->charcount = (i - vmap->widecount);
1556
1557 cupsFileClose(fp);
1558
1559 /*
1560 * Add it to the cache and return...
1561 */
1562
1563 vmap->next = vmap_cache;
1564 vmap_cache = vmap;
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);
1583 }
1584
1585
1586 /*
1587 * End of "$Id: transcode.c 6038 2006-10-14 15:53:10Z mike $"
1588 */