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