2 * "$Id: transcode.c 7560 2008-05-13 06:34:04Z mike $"
4 * Transcoding support for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * _cupsCharmapFlush() - Flush all character set maps out of cache.
20 * cupsCharsetToUTF8() - Convert legacy character set to UTF-8.
21 * cupsUTF8ToCharset() - Convert UTF-8 to legacy character set.
22 * cupsUTF8ToUTF32() - Convert UTF-8 to UTF-32.
23 * cupsUTF32ToUTF8() - Convert UTF-32 to UTF-8.
27 * Include necessary headers...
30 #include "cups-private.h"
35 #endif /* HAVE_ICONV_H */
43 static _cups_mutex_t map_mutex
= _CUPS_MUTEX_INITIALIZER
;
44 /* Mutex to control access to maps */
45 static iconv_t map_from_utf8
= (iconv_t
)-1;
46 /* Convert from UTF-8 to charset */
47 static iconv_t map_to_utf8
= (iconv_t
)-1;
48 /* Convert from charset to UTF-8 */
49 static cups_encoding_t map_encoding
= CUPS_AUTO_ENCODING
;
50 /* Which charset is cached */
51 #endif /* HAVE_ICONV_H */
55 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
59 _cupsCharmapFlush(void)
62 if (map_from_utf8
!= (iconv_t
)-1)
64 iconv_close(map_from_utf8
);
65 map_from_utf8
= (iconv_t
)-1;
68 if (map_to_utf8
!= (iconv_t
)-1)
70 iconv_close(map_to_utf8
);
71 map_to_utf8
= (iconv_t
)-1;
74 map_encoding
= CUPS_AUTO_ENCODING
;
75 #endif /* HAVE_ICONV_H */
80 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
83 int /* O - Count or -1 on error */
85 cups_utf8_t
*dest
, /* O - Target string */
86 const char *src
, /* I - Source string */
87 const int maxout
, /* I - Max output */
88 const cups_encoding_t encoding
) /* I - Encoding */
90 cups_utf8_t
*destptr
; /* Pointer into UTF-8 buffer */
91 int bytes
; /* Number of bytes converted */
92 size_t srclen
, /* Length of source string */
93 outBytesLeft
; /* Bytes remaining in output buffer */
97 * Check for valid arguments...
100 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)",
101 dest
, src
, maxout
, encoding
));
103 if (!dest
|| !src
|| maxout
< 1)
108 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
113 * Handle identity conversions...
116 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
117 encoding
>= CUPS_ENCODING_VBCS_END
)
119 strlcpy((char *)dest
, src
, maxout
);
120 return ((int)strlen((char *)dest
));
124 * Handle ISO-8859-1 to UTF-8 directly...
129 if (encoding
== CUPS_ISO8859_1
)
131 int ch
; /* Character from string */
132 cups_utf8_t
*destend
; /* End of UTF-8 buffer */
135 destend
= dest
+ maxout
- 2;
137 while (*src
&& destptr
< destend
)
143 *destptr
++ = 0xc0 | (ch
>> 6);
144 *destptr
++ = 0x80 | (ch
& 0x3f);
152 return ((int)(destptr
- dest
));
156 * Convert input legacy charset to UTF-8...
160 _cupsMutexLock(&map_mutex
);
162 if (map_encoding
!= encoding
)
166 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
167 map_to_utf8
= iconv_open("UTF-8", _cupsEncodingName(encoding
));
168 map_encoding
= encoding
;
171 if (map_to_utf8
!= (iconv_t
)-1)
173 srclen
= strlen(src
);
174 outBytesLeft
= maxout
- 1;
175 bytes
= (int)iconv(map_to_utf8
, (char **)&src
, &srclen
,
176 (char **)&destptr
, &outBytesLeft
);
179 _cupsMutexUnlock(&map_mutex
);
181 return ((int)(destptr
- dest
));
184 _cupsMutexUnlock(&map_mutex
);
185 #endif /* HAVE_ICONV_H */
188 * No iconv() support, so error out...
198 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
201 int /* O - Count or -1 on error */
203 char *dest
, /* O - Target string */
204 const cups_utf8_t
*src
, /* I - Source string */
205 const int maxout
, /* I - Max output */
206 const cups_encoding_t encoding
) /* I - Encoding */
208 char *destptr
; /* Pointer into destination */
209 int bytes
; /* Number of bytes converted */
210 size_t srclen
, /* Length of source string */
211 outBytesLeft
; /* Bytes remaining in output buffer */
215 * Check for valid arguments...
218 if (!dest
|| !src
|| maxout
< 1)
227 * Handle identity conversions...
230 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
231 encoding
>= CUPS_ENCODING_VBCS_END
)
233 strlcpy(dest
, (char *)src
, maxout
);
234 return ((int)strlen(dest
));
238 * Handle UTF-8 to ISO-8859-1 directly...
243 if (encoding
== CUPS_ISO8859_1
)
245 int ch
; /* Character from string */
246 char *destend
; /* End of ISO-8859-1 buffer */
249 destend
= dest
+ maxout
- 1;
251 while (*src
&& destptr
< destend
)
255 if ((ch
& 0xe0) == 0xc0)
257 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
264 else if ((ch
& 0xf0) == 0xe0 ||
267 else if (!(ch
& 0x80))
273 return ((int)(destptr
- dest
));
278 * Convert input UTF-8 to legacy charset...
281 _cupsMutexLock(&map_mutex
);
283 if (map_encoding
!= encoding
)
287 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
288 map_to_utf8
= iconv_open("UTF-8", _cupsEncodingName(encoding
));
289 map_encoding
= encoding
;
292 if (map_from_utf8
!= (iconv_t
)-1)
294 srclen
= strlen((char *)src
);
295 outBytesLeft
= maxout
- 1;
296 bytes
= (int)iconv(map_from_utf8
, (char **)&src
, &srclen
,
297 &destptr
, &outBytesLeft
);
300 _cupsMutexUnlock(&map_mutex
);
302 return ((int)(destptr
- dest
));
305 _cupsMutexUnlock(&map_mutex
);
306 #endif /* HAVE_ICONV_H */
309 * No iconv() support, so error out...
319 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
321 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
323 * UTF-32 char UTF-8 char(s)
324 * --------------------------------------------------
325 * 0 to 127 = 0xxxxxxx (US-ASCII)
326 * 128 to 2047 = 110xxxxx 10yyyyyy
327 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
328 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
330 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
331 * which would convert to five- or six-octet UTF-8 sequences...
334 int /* O - Count or -1 on error */
336 cups_utf32_t
*dest
, /* O - Target string */
337 const cups_utf8_t
*src
, /* I - Source string */
338 const int maxout
) /* I - Max output */
340 int i
; /* Looping variable */
341 cups_utf8_t ch
; /* Character value */
342 cups_utf8_t next
; /* Next character value */
343 cups_utf32_t ch32
; /* UTF-32 character value */
347 * Check for valid arguments and clear output...
350 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest
,
356 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
358 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
364 * Convert input UTF-8 to output UTF-32...
367 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
372 * Convert UTF-8 character(s) to UTF-32 character...
378 * One-octet UTF-8 <= 127 (US-ASCII)...
383 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src
[-1], ch
));
386 else if ((ch
& 0xe0) == 0xc0)
389 * Two-octet UTF-8 <= 2047 (Latin-x)...
393 if ((next
& 0xc0) != 0x80)
395 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
400 ch32
= ((ch
& 0x1f) << 6) | (next
& 0x3f);
403 * Check for non-shortest form (invalid UTF-8)...
408 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
415 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
416 src
[-2], src
[-1], (unsigned)ch32
));
418 else if ((ch
& 0xf0) == 0xe0)
421 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
425 if ((next
& 0xc0) != 0x80)
427 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
432 ch32
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
435 if ((next
& 0xc0) != 0x80)
437 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
442 ch32
= (ch32
<< 6) | (next
& 0x3f);
445 * Check for non-shortest form (invalid UTF-8)...
450 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
457 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
458 src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
460 else if ((ch
& 0xf8) == 0xf0)
463 * Four-octet UTF-8...
467 if ((next
& 0xc0) != 0x80)
469 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
474 ch32
= ((ch
& 0x07) << 6) | (next
& 0x3f);
477 if ((next
& 0xc0) != 0x80)
479 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
484 ch32
= (ch32
<< 6) | (next
& 0x3f);
487 if ((next
& 0xc0) != 0x80)
489 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
494 ch32
= (ch32
<< 6) | (next
& 0x3f);
497 * Check for non-shortest form (invalid UTF-8)...
502 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
509 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
510 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
515 * More than 4-octet (invalid UTF-8 sequence)...
518 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
524 * Check for UTF-16 surrogate (illegal UTF-8)...
527 if (ch32
>= 0xd800 && ch32
<= 0xdfff)
533 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout
- 1 - i
));
535 return (maxout
- 1 - i
);
540 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
542 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
544 * UTF-32 char UTF-8 char(s)
545 * --------------------------------------------------
546 * 0 to 127 = 0xxxxxxx (US-ASCII)
547 * 128 to 2047 = 110xxxxx 10yyyyyy
548 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
549 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
551 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
552 * which would convert to five- or six-octet UTF-8 sequences...
555 int /* O - Count or -1 on error */
557 cups_utf8_t
*dest
, /* O - Target string */
558 const cups_utf32_t
*src
, /* I - Source string */
559 const int maxout
) /* I - Max output */
561 cups_utf8_t
*start
; /* Start of destination string */
562 int i
; /* Looping variable */
563 int swap
; /* Byte-swap input to output */
564 cups_utf32_t ch
; /* Character value */
568 * Check for valid arguments and clear output...
571 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest
, src
,
577 if (!dest
|| !src
|| maxout
< 1)
579 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
585 * Check for leading BOM in UTF-32 and inverted BOM...
589 swap
= *src
== 0xfffe0000;
591 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap
));
593 if (*src
== 0xfffe0000 || *src
== 0xfeff)
597 * Convert input UTF-32 to output UTF-8...
600 for (i
= maxout
- 1; *src
&& i
> 0;)
605 * Byte swap input UTF-32, if necessary...
606 * (only byte-swapping 24 of 32 bits)
610 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
613 * Check for beyond Plane 16 (invalid UTF-32)...
618 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
624 * Convert UTF-32 character to UTF-8 character(s)...
630 * One-octet UTF-8 <= 127 (US-ASCII)...
633 *dest
++ = (cups_utf8_t
)ch
;
636 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch
, dest
[-1]));
641 * Two-octet UTF-8 <= 2047 (Latin-x)...
646 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
651 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
652 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
655 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch
,
656 dest
[-2], dest
[-1]));
658 else if (ch
< 0x10000)
661 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
666 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
671 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
672 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
673 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
676 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch
,
677 dest
[-3], dest
[-2], dest
[-1]));
682 * Four-octet UTF-8...
687 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
692 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
693 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
694 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
695 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
698 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
699 (unsigned)ch
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
705 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest
- start
)));
707 return ((int)(dest
- start
));
712 * 'compare_wide()' - Compare key for wide (VBCS) match.
716 compare_wide(const void *k1
, /* I - Key char */
717 const void *k2
) /* I - Map char */
719 cups_vbcs_t key
; /* Legacy key character */
720 cups_vbcs_t map
; /* Legacy map character */
723 key
= *((cups_vbcs_t
*)k1
);
724 map
= ((_cups_wide2uni_t
*)k2
)->widechar
;
726 return ((int)(key
- map
));
731 * 'conv_sbcs_to_utf8()' - Convert legacy SBCS to UTF-8.
734 static int /* O - Count or -1 on error */
736 cups_utf8_t
*dest
, /* O - Target string */
737 const cups_sbcs_t
*src
, /* I - Source string */
738 int maxout
, /* I - Max output */
739 const cups_encoding_t encoding
) /* I - Encoding */
741 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
742 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
743 cups_sbcs_t legchar
; /* Legacy character value */
744 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
745 *workptr
; /* Pointer into string */
749 * Find legacy charset map in cache...
752 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
756 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
760 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
765 * Convert ASCII verbatim (optimization)...
769 *workptr
++ = (cups_utf32_t
)legchar
;
773 * Convert unknown character to Replacement Character...
776 crow
= cmap
->char2uni
+ legchar
;
781 *workptr
++ = (cups_utf32_t
)*crow
;
788 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
793 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
798 * 'conv_utf8_to_sbcs()' - Convert UTF-8 to legacy SBCS.
801 static int /* O - Count or -1 on error */
803 cups_sbcs_t
*dest
, /* O - Target string */
804 const cups_utf8_t
*src
, /* I - Source string */
805 int maxout
, /* I - Max output */
806 const cups_encoding_t encoding
) /* I - Encoding */
808 cups_sbcs_t
*start
; /* Start of destination string */
809 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
810 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
811 cups_utf32_t unichar
; /* Character value */
812 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
813 *workptr
; /* Pointer into string */
817 * Find legacy charset map in cache...
820 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
824 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
827 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
831 * Convert internal UCS-4 to SBCS legacy charset (and delete BOM)...
834 for (workptr
= work
, start
= dest
; *workptr
&& maxout
> 0; maxout
--)
836 unichar
= *workptr
++;
841 * Convert ASCII verbatim (optimization)...
846 *dest
++ = (cups_sbcs_t
)unichar
;
851 * Convert unknown character to visible replacement...
854 srow
= cmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
857 srow
+= (int)(unichar
& 0xff);
869 return ((int)(dest
- start
));
874 * 'conv_utf8_to_vbcs()' - Convert UTF-8 to legacy DBCS/VBCS.
877 static int /* O - Count or -1 on error */
879 cups_sbcs_t
*dest
, /* O - Target string */
880 const cups_utf8_t
*src
, /* I - Source string */
881 int maxout
, /* I - Max output */
882 const cups_encoding_t encoding
) /* I - Encoding */
884 cups_sbcs_t
*start
; /* Start of destination string */
885 _cups_vmap_t
*vmap
; /* Legacy DBCS / Unicode Charset Map */
886 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
887 cups_utf32_t unichar
; /* Character value */
888 cups_vbcs_t legchar
; /* Legacy character value */
889 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
890 *workptr
; /* Pointer into string */
893 DEBUG_printf(("7conv_utf8_to_vbcs(dest=%p, src=\"%s\", maxout=%d, "
894 "encoding=%d)", dest
, src
, maxout
, encoding
));
897 * Find legacy charset map in cache...
900 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
902 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (no charmap)");
908 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
911 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
913 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (Unable to convert to UTF-32)");
919 * Convert internal UCS-4 to VBCS legacy charset (and delete BOM)...
922 for (start
= dest
, workptr
= work
; *workptr
&& maxout
> 0; maxout
--)
924 unichar
= *workptr
++;
927 * Convert ASCII verbatim (optimization)...
932 *dest
++ = (cups_sbcs_t
)unichar
;
934 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X", (unsigned)unichar
,
941 * Convert unknown character to visible replacement...
944 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
947 vrow
+= (int)(unichar
& 0xff);
950 legchar
= (cups_vbcs_t
)'?';
952 legchar
= (cups_vbcs_t
)*vrow
;
955 * Save n-byte legacy character...
958 if (legchar
> 0xffffff)
962 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (out of space)");
967 *dest
++ = (cups_sbcs_t
)(legchar
>> 24);
968 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
969 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
970 *dest
++ = (cups_sbcs_t
)legchar
;
974 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X %02X %02X",
975 (unsigned)unichar
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
977 else if (legchar
> 0xffff)
981 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (out of space)");
986 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
987 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
988 *dest
++ = (cups_sbcs_t
)legchar
;
992 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X %02X",
993 (unsigned)unichar
, dest
[-3], dest
[-2], dest
[-1]));
995 else if (legchar
> 0xff)
997 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
998 *dest
++ = (cups_sbcs_t
)legchar
;
1002 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X",
1003 (unsigned)unichar
, dest
[-2], dest
[-1]));
1007 *dest
++ = (cups_sbcs_t
)legchar
;
1009 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X",
1010 (unsigned)unichar
, dest
[-1]));
1018 DEBUG_printf(("8conv_utf8_to_vbcs: Returning %d characters",
1019 (int)(dest
- start
)));
1021 return ((int)(dest
- start
));
1026 * 'conv_vbcs_to_utf8()' - Convert legacy DBCS/VBCS to UTF-8.
1029 static int /* O - Count or -1 on error */
1031 cups_utf8_t
*dest
, /* O - Target string */
1032 const cups_sbcs_t
*src
, /* I - Source string */
1033 int maxout
, /* I - Max output */
1034 const cups_encoding_t encoding
) /* I - Encoding */
1036 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
1037 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1038 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
1039 cups_sbcs_t leadchar
; /* Lead char of n-byte legacy char */
1040 cups_vbcs_t legchar
; /* Legacy character value */
1041 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
1042 *workptr
; /* Pointer into string */
1046 * Find legacy charset map in cache...
1049 DEBUG_printf(("7conv_vbcs_to_utf8(dest=%p, src=%p, maxout=%d, encoding=%d)",
1050 dest
, src
, maxout
, encoding
));
1052 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
1054 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (NULL vmap)");
1060 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
1064 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
1067 leadchar
= (cups_sbcs_t
)legchar
;
1070 * Convert ASCII verbatim (optimization)...
1075 *workptr
++ = (cups_utf32_t
)legchar
;
1077 DEBUG_printf(("9conv_vbcs_to_utf8: %02X => %08X", src
[-1],
1078 (unsigned)legchar
));
1083 * Convert 2-byte legacy character...
1086 if (vmap
->lead2char
[(int)leadchar
] == leadchar
)
1090 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string)");
1095 legchar
= (legchar
<< 8) | *src
++;
1098 * Convert unknown character to Replacement Character...
1101 crow
= vmap
->char2uni
[(int)((legchar
>> 8) & 0xff)];
1103 crow
+= (int) (legchar
& 0xff);
1105 if (!crow
|| !*crow
)
1106 *workptr
++ = 0xfffd;
1108 *workptr
++ = (cups_utf32_t
)*crow
;
1110 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X => %08X",
1111 src
[-2], src
[-1], (unsigned)workptr
[-1]));
1116 * Fetch 3-byte or 4-byte legacy character...
1119 if (vmap
->lead3char
[(int)leadchar
] == leadchar
)
1121 if (!*src
|| !src
[1])
1123 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string 2)");
1128 legchar
= (legchar
<< 8) | *src
++;
1129 legchar
= (legchar
<< 8) | *src
++;
1131 else if (vmap
->lead4char
[(int)leadchar
] == leadchar
)
1133 if (!*src
|| !src
[1] || !src
[2])
1135 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string 3)");
1140 legchar
= (legchar
<< 8) | *src
++;
1141 legchar
= (legchar
<< 8) | *src
++;
1142 legchar
= (legchar
<< 8) | *src
++;
1146 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (bad character)");
1152 * Find 3-byte or 4-byte legacy character...
1155 wide2uni
= (_cups_wide2uni_t
*)bsearch(&legchar
,
1158 sizeof(_cups_wide2uni_t
),
1162 * Convert unknown character to Replacement Character...
1165 if (!wide2uni
|| !wide2uni
->unichar
)
1166 *workptr
++ = 0xfffd;
1168 *workptr
++ = wide2uni
->unichar
;
1170 if (vmap
->lead3char
[(int)leadchar
] == leadchar
)
1171 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X %02X => %08X",
1172 src
[-3], src
[-2], src
[-1], (unsigned)workptr
[-1]));
1174 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X %02X %02X => %08X",
1175 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)workptr
[-1]));
1182 DEBUG_printf(("9conv_vbcs_to_utf8: Converting %d UTF-32 characters to UTF-8",
1183 (int)(workptr
- work
)));
1186 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
1189 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
1194 * 'free_sbcs_charmap()' - Free memory used by a single byte character set.
1198 free_sbcs_charmap(_cups_cmap_t
*cmap
) /* I - Character set */
1200 int i
; /* Looping variable */
1203 for (i
= 0; i
< 256; i
++)
1204 if (cmap
->uni2char
[i
])
1205 free(cmap
->uni2char
[i
]);
1212 * 'free_vbcs_charmap()' - Free memory used by a variable byte character set.
1216 free_vbcs_charmap(_cups_vmap_t
*vmap
) /* I - Character set */
1218 int i
; /* Looping variable */
1221 for (i
= 0; i
< 256; i
++)
1222 if (vmap
->char2uni
[i
])
1223 free(vmap
->char2uni
[i
]);
1225 for (i
= 0; i
< 256; i
++)
1226 if (vmap
->uni2char
[i
])
1227 free(vmap
->uni2char
[i
]);
1230 free(vmap
->wide2uni
);
1237 * 'get_charmap()' - Lookup or get a character set map (private).
1239 * This code handles single-byte (SBCS), double-byte (DBCS), and
1240 * variable-byte (VBCS) character sets _without_ charset escapes...
1241 * This code does not handle multiple-byte character sets (MBCS)
1242 * (such as ISO-2022-JP) with charset switching via escapes...
1246 static void * /* O - Charset map pointer */
1248 const cups_encoding_t encoding
) /* I - Encoding */
1250 char filename
[1024]; /* Filename for charset map file */
1251 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1254 DEBUG_printf(("7get_charmap(encoding=%d)", encoding
));
1257 * Get the data directory and charset map name...
1260 snprintf(filename
, sizeof(filename
), "%s/charmaps/%s.txt",
1261 cg
->cups_datadir
, _cupsEncodingName(encoding
));
1263 DEBUG_printf(("9get_charmap: filename=\"%s\"", filename
));
1266 * Read charset map input file into cache...
1269 if (encoding
< CUPS_ENCODING_SBCS_END
)
1270 return (get_sbcs_charmap(encoding
, filename
));
1271 else if (encoding
< CUPS_ENCODING_VBCS_END
)
1272 return (get_vbcs_charmap(encoding
, filename
));
1279 * 'get_charmap_count()' - Count lines in a charmap file.
1282 static int /* O - Count or -1 on error */
1283 get_charmap_count(cups_file_t
*fp
) /* I - File to read from */
1285 int count
; /* Number of lines */
1286 char line
[256]; /* Line from input map file */
1290 * Count lines in map input file...
1295 while (cupsFileGets(fp
, line
, sizeof(line
)))
1300 * Return the number of lines...
1311 * 'get_sbcs_charmap()' - Get SBCS Charmap.
1314 static _cups_cmap_t
* /* O - Charmap or 0 on error */
1316 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1317 const char *filename
) /* I - Charmap Filename */
1319 unsigned long legchar
; /* Legacy character value */
1320 cups_utf32_t unichar
; /* Unicode character value */
1321 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
1322 cups_file_t
*fp
; /* Charset map file pointer */
1323 char *s
; /* Line parsing pointer */
1324 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1325 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
1326 char line
[256]; /* Line from charset map file */
1330 * See if we already have this SBCS charset map loaded...
1333 DEBUG_printf(("7get_sbcs_charmap(encoding=%d, filename=\"%s\")", encoding
,
1336 for (cmap
= cmap_cache
; cmap
; cmap
= cmap
->next
)
1338 if (cmap
->encoding
== encoding
)
1341 DEBUG_printf(("8get_sbcs_charmap: Returning existing cmap=%p", cmap
));
1343 return ((void *)cmap
);
1348 * Open SBCS charset map input file...
1351 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1353 DEBUG_printf(("8get_sbcs_charmap: Returning NULL (%s)", strerror(errno
)));
1359 * Allocate memory for SBCS charset map...
1362 if ((cmap
= (_cups_cmap_t
*)calloc(1, sizeof(_cups_cmap_t
))) == NULL
)
1365 DEBUG_puts("8get_sbcs_charmap: Returning NULL (Unable to allocate memory)");
1371 cmap
->encoding
= encoding
;
1374 * Save SBCS charset map into memory for transcoding...
1377 while (cupsFileGets(fp
, line
, sizeof(line
)))
1382 legchar
= strtol(line
, &s
, 16);
1383 if (legchar
< 0 || legchar
> 0xff)
1386 unichar
= strtol(s
, NULL
, 16);
1387 if (unichar
< 0 || unichar
> 0x10ffff)
1391 * Save legacy to Unicode mapping in direct lookup table...
1394 crow
= cmap
->char2uni
+ legchar
;
1395 *crow
= (cups_ucs2_t
)(unichar
& 0xffff);
1398 * Save Unicode to legacy mapping in indirect lookup table...
1401 srow
= cmap
->uni2char
[(unichar
>> 8) & 0xff];
1404 srow
= (cups_sbcs_t
*)calloc(256, sizeof(cups_sbcs_t
));
1408 cmap
->uni2char
[(unichar
>> 8) & 0xff] = srow
;
1411 srow
+= unichar
& 0xff;
1414 * Convert Replacement Character to visible replacement...
1417 if (unichar
== 0xfffd)
1418 legchar
= (unsigned long)'?';
1421 * First (oldest) legacy character uses Unicode mapping cell...
1425 *srow
= (cups_sbcs_t
)legchar
;
1431 * Add it to the cache and return...
1434 cmap
->next
= cmap_cache
;
1437 DEBUG_printf(("8get_sbcs_charmap: Returning new cmap=%p", cmap
));
1442 * If we get here, there was an error in the cmap file...
1447 free_sbcs_charmap(cmap
);
1451 DEBUG_puts("8get_sbcs_charmap: Returning NULL (Read/format error)");
1458 * 'get_vbcs_charmap()' - Get DBCS/VBCS Charmap.
1461 static _cups_vmap_t
* /* O - Charmap or 0 on error */
1463 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1464 const char *filename
) /* I - Charmap Filename */
1466 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
1467 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1468 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
1469 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
1470 cups_sbcs_t leadchar
; /* Lead char of 2-byte legacy char */
1471 unsigned long legchar
; /* Legacy character value */
1472 cups_utf32_t unichar
; /* Unicode character value */
1473 int mapcount
; /* Count of lines in charmap file */
1474 cups_file_t
*fp
; /* Charset map file pointer */
1475 char *s
; /* Line parsing pointer */
1476 char line
[256]; /* Line from charset map file */
1477 int i
; /* Loop variable */
1478 int legacy
; /* 32-bit legacy char */
1481 DEBUG_printf(("7get_vbcs_charmap(encoding=%d, filename=\"%s\")\n",
1482 encoding
, filename
));
1485 * See if we already have this DBCS/VBCS charset map loaded...
1488 for (vmap
= vmap_cache
; vmap
; vmap
= vmap
->next
)
1490 if (vmap
->encoding
== encoding
)
1493 DEBUG_printf(("8get_vbcs_charmap: Returning existing vmap=%p", vmap
));
1495 return ((void *)vmap
);
1500 * Open VBCS charset map input file...
1503 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1505 DEBUG_printf(("8get_vbcs_charmap: Returning NULL (%s)", strerror(errno
)));
1511 * Count lines in charmap file...
1514 if ((mapcount
= get_charmap_count(fp
)) <= 0)
1516 DEBUG_puts("8get_vbcs_charmap: Unable to get charmap count!");
1523 DEBUG_printf(("8get_vbcs_charmap: mapcount=%d", mapcount
));
1526 * Allocate memory for DBCS/VBCS charset map...
1529 if ((vmap
= (_cups_vmap_t
*)calloc(1, sizeof(_cups_vmap_t
))) == NULL
)
1531 DEBUG_puts("8get_vbcs_charmap: Unable to allocate memory!");
1539 vmap
->encoding
= encoding
;
1542 * Save DBCS/VBCS charset map into memory for transcoding...
1552 while (cupsFileGets(fp
, line
, sizeof(line
)))
1557 legchar
= strtoul(line
, &s
, 16);
1558 if (legchar
== ULONG_MAX
)
1561 unichar
= strtol(s
, NULL
, 16);
1562 if (unichar
< 0 || unichar
> 0x10ffff)
1567 DEBUG_printf(("9get_vbcs_charmap: i=%d, legchar=0x%08lx, unichar=0x%04x", i
,
1568 legchar
, (unsigned)unichar
));
1571 * Save lead char of 2/3/4-byte legacy char...
1574 if (legchar
> 0xffffff)
1576 leadchar
= (cups_sbcs_t
)(legchar
>> 24);
1577 vmap
->lead4char
[leadchar
] = leadchar
;
1579 else if (legchar
> 0xffff)
1581 leadchar
= (cups_sbcs_t
)(legchar
>> 16);
1582 vmap
->lead3char
[leadchar
] = leadchar
;
1586 leadchar
= (cups_sbcs_t
)(legchar
>> 8);
1587 vmap
->lead2char
[leadchar
] = leadchar
;
1591 * Save Legacy to Unicode mapping...
1594 if (legchar
<= 0xffff)
1597 * Save DBCS 16-bit to Unicode mapping in indirect lookup table...
1600 crow
= vmap
->char2uni
[(int)leadchar
];
1603 crow
= (cups_ucs2_t
*)calloc(256, sizeof(cups_ucs2_t
));
1607 vmap
->char2uni
[(int)leadchar
] = crow
;
1610 crow
[(int)(legchar
& 0xff)] = (cups_ucs2_t
)unichar
;
1615 * Save VBCS 32-bit to Unicode mapping in sorted list table...
1621 vmap
->widecount
= (mapcount
- i
+ 1);
1622 wide2uni
= (_cups_wide2uni_t
*)calloc(vmap
->widecount
,
1623 sizeof(_cups_wide2uni_t
));
1627 vmap
->wide2uni
= wide2uni
;
1630 wide2uni
->widechar
= (cups_vbcs_t
)legchar
;
1631 wide2uni
->unichar
= (cups_ucs2_t
)unichar
;
1636 * Save Unicode to legacy mapping in indirect lookup table...
1639 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
1642 vrow
= (cups_vbcs_t
*)calloc(256, sizeof(cups_vbcs_t
));
1646 vmap
->uni2char
[(int) ((unichar
>> 8) & 0xff)] = vrow
;
1649 vrow
+= (int)(unichar
& 0xff);
1652 * Convert Replacement Character to visible replacement...
1655 if (unichar
== 0xfffd)
1656 legchar
= (unsigned long)'?';
1659 * First (oldest) legacy character uses Unicode mapping cell...
1663 *vrow
= (cups_vbcs_t
)legchar
;
1666 vmap
->charcount
= (i
- vmap
->widecount
);
1671 * Add it to the cache and return...
1674 vmap
->next
= vmap_cache
;
1677 DEBUG_printf(("8get_vbcs_charmap: Returning new vmap=%p", vmap
));
1682 * If we get here, the file contains errors...
1687 free_vbcs_charmap(vmap
);
1691 DEBUG_puts("8get_vbcs_charmap: Returning NULL (Read/format error)");
1698 * End of "$Id: transcode.c 7560 2008-05-13 06:34:04Z mike $"