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 */
92 size_t srclen
, /* Length of source string */
93 outBytesLeft
; /* Bytes remaining in output buffer */
94 #endif /* HAVE_ICONV_H */
98 * Check for valid arguments...
101 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)",
102 dest
, src
, maxout
, encoding
));
104 if (!dest
|| !src
|| maxout
< 1)
109 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
114 * Handle identity conversions...
117 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
118 encoding
>= CUPS_ENCODING_VBCS_END
)
120 strlcpy((char *)dest
, src
, maxout
);
121 return ((int)strlen((char *)dest
));
125 * Handle ISO-8859-1 to UTF-8 directly...
130 if (encoding
== CUPS_ISO8859_1
)
132 int ch
; /* Character from string */
133 cups_utf8_t
*destend
; /* End of UTF-8 buffer */
136 destend
= dest
+ maxout
- 2;
138 while (*src
&& destptr
< destend
)
144 *destptr
++ = 0xc0 | (ch
>> 6);
145 *destptr
++ = 0x80 | (ch
& 0x3f);
153 return ((int)(destptr
- dest
));
157 * Convert input legacy charset to UTF-8...
161 _cupsMutexLock(&map_mutex
);
163 if (map_encoding
!= encoding
)
167 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
168 map_to_utf8
= iconv_open("UTF-8", _cupsEncodingName(encoding
));
169 map_encoding
= encoding
;
172 if (map_to_utf8
!= (iconv_t
)-1)
174 srclen
= strlen(src
);
175 outBytesLeft
= maxout
- 1;
177 iconv(map_to_utf8
, (char **)&src
, &srclen
, (char **)&destptr
,
181 _cupsMutexUnlock(&map_mutex
);
183 return ((int)(destptr
- dest
));
186 _cupsMutexUnlock(&map_mutex
);
187 #endif /* HAVE_ICONV_H */
190 * No iconv() support, so error out...
200 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
203 int /* O - Count or -1 on error */
205 char *dest
, /* O - Target string */
206 const cups_utf8_t
*src
, /* I - Source string */
207 const int maxout
, /* I - Max output */
208 const cups_encoding_t encoding
) /* I - Encoding */
210 char *destptr
; /* Pointer into destination */
212 size_t srclen
, /* Length of source string */
213 outBytesLeft
; /* Bytes remaining in output buffer */
214 #endif /* HAVE_ICONV_H */
218 * Check for valid arguments...
221 if (!dest
|| !src
|| maxout
< 1)
230 * Handle identity conversions...
233 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
234 encoding
>= CUPS_ENCODING_VBCS_END
)
236 strlcpy(dest
, (char *)src
, maxout
);
237 return ((int)strlen(dest
));
241 * Handle UTF-8 to ISO-8859-1 directly...
246 if (encoding
== CUPS_ISO8859_1
)
248 int ch
; /* Character from string */
249 char *destend
; /* End of ISO-8859-1 buffer */
252 destend
= dest
+ maxout
- 1;
254 while (*src
&& destptr
< destend
)
258 if ((ch
& 0xe0) == 0xc0)
260 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
267 else if ((ch
& 0xf0) == 0xe0 ||
270 else if (!(ch
& 0x80))
276 return ((int)(destptr
- dest
));
281 * Convert input UTF-8 to legacy charset...
284 _cupsMutexLock(&map_mutex
);
286 if (map_encoding
!= encoding
)
290 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
291 map_to_utf8
= iconv_open("UTF-8", _cupsEncodingName(encoding
));
292 map_encoding
= encoding
;
295 if (map_from_utf8
!= (iconv_t
)-1)
297 srclen
= strlen((char *)src
);
298 outBytesLeft
= maxout
- 1;
300 iconv(map_from_utf8
, (char **)&src
, &srclen
, &destptr
, &outBytesLeft
);
303 _cupsMutexUnlock(&map_mutex
);
305 return ((int)(destptr
- dest
));
308 _cupsMutexUnlock(&map_mutex
);
309 #endif /* HAVE_ICONV_H */
312 * No iconv() support, so error out...
322 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
324 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
326 * UTF-32 char UTF-8 char(s)
327 * --------------------------------------------------
328 * 0 to 127 = 0xxxxxxx (US-ASCII)
329 * 128 to 2047 = 110xxxxx 10yyyyyy
330 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
331 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
333 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
334 * which would convert to five- or six-octet UTF-8 sequences...
337 int /* O - Count or -1 on error */
339 cups_utf32_t
*dest
, /* O - Target string */
340 const cups_utf8_t
*src
, /* I - Source string */
341 const int maxout
) /* I - Max output */
343 int i
; /* Looping variable */
344 cups_utf8_t ch
; /* Character value */
345 cups_utf8_t next
; /* Next character value */
346 cups_utf32_t ch32
; /* UTF-32 character value */
350 * Check for valid arguments and clear output...
353 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest
,
359 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
361 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
367 * Convert input UTF-8 to output UTF-32...
370 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
375 * Convert UTF-8 character(s) to UTF-32 character...
381 * One-octet UTF-8 <= 127 (US-ASCII)...
386 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src
[-1], ch
));
389 else if ((ch
& 0xe0) == 0xc0)
392 * Two-octet UTF-8 <= 2047 (Latin-x)...
396 if ((next
& 0xc0) != 0x80)
398 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
403 ch32
= ((ch
& 0x1f) << 6) | (next
& 0x3f);
406 * Check for non-shortest form (invalid UTF-8)...
411 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
418 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
419 src
[-2], src
[-1], (unsigned)ch32
));
421 else if ((ch
& 0xf0) == 0xe0)
424 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
428 if ((next
& 0xc0) != 0x80)
430 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
435 ch32
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
438 if ((next
& 0xc0) != 0x80)
440 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
445 ch32
= (ch32
<< 6) | (next
& 0x3f);
448 * Check for non-shortest form (invalid UTF-8)...
453 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
460 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
461 src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
463 else if ((ch
& 0xf8) == 0xf0)
466 * Four-octet UTF-8...
470 if ((next
& 0xc0) != 0x80)
472 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
477 ch32
= ((ch
& 0x07) << 6) | (next
& 0x3f);
480 if ((next
& 0xc0) != 0x80)
482 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
487 ch32
= (ch32
<< 6) | (next
& 0x3f);
490 if ((next
& 0xc0) != 0x80)
492 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
497 ch32
= (ch32
<< 6) | (next
& 0x3f);
500 * Check for non-shortest form (invalid UTF-8)...
505 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
512 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
513 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
518 * More than 4-octet (invalid UTF-8 sequence)...
521 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
527 * Check for UTF-16 surrogate (illegal UTF-8)...
530 if (ch32
>= 0xd800 && ch32
<= 0xdfff)
536 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout
- 1 - i
));
538 return (maxout
- 1 - i
);
543 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
545 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
547 * UTF-32 char UTF-8 char(s)
548 * --------------------------------------------------
549 * 0 to 127 = 0xxxxxxx (US-ASCII)
550 * 128 to 2047 = 110xxxxx 10yyyyyy
551 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
552 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
554 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
555 * which would convert to five- or six-octet UTF-8 sequences...
558 int /* O - Count or -1 on error */
560 cups_utf8_t
*dest
, /* O - Target string */
561 const cups_utf32_t
*src
, /* I - Source string */
562 const int maxout
) /* I - Max output */
564 cups_utf8_t
*start
; /* Start of destination string */
565 int i
; /* Looping variable */
566 int swap
; /* Byte-swap input to output */
567 cups_utf32_t ch
; /* Character value */
571 * Check for valid arguments and clear output...
574 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest
, src
,
580 if (!dest
|| !src
|| maxout
< 1)
582 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
588 * Check for leading BOM in UTF-32 and inverted BOM...
592 swap
= *src
== 0xfffe0000;
594 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap
));
596 if (*src
== 0xfffe0000 || *src
== 0xfeff)
600 * Convert input UTF-32 to output UTF-8...
603 for (i
= maxout
- 1; *src
&& i
> 0;)
608 * Byte swap input UTF-32, if necessary...
609 * (only byte-swapping 24 of 32 bits)
613 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
616 * Check for beyond Plane 16 (invalid UTF-32)...
621 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
627 * Convert UTF-32 character to UTF-8 character(s)...
633 * One-octet UTF-8 <= 127 (US-ASCII)...
636 *dest
++ = (cups_utf8_t
)ch
;
639 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch
, dest
[-1]));
644 * Two-octet UTF-8 <= 2047 (Latin-x)...
649 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
654 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
655 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
658 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch
,
659 dest
[-2], dest
[-1]));
661 else if (ch
< 0x10000)
664 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
669 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
674 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
675 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
676 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
679 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch
,
680 dest
[-3], dest
[-2], dest
[-1]));
685 * Four-octet UTF-8...
690 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
695 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
696 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
697 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
698 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
701 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
702 (unsigned)ch
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
708 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest
- start
)));
710 return ((int)(dest
- start
));
715 * 'compare_wide()' - Compare key for wide (VBCS) match.
719 compare_wide(const void *k1
, /* I - Key char */
720 const void *k2
) /* I - Map char */
722 cups_vbcs_t key
; /* Legacy key character */
723 cups_vbcs_t map
; /* Legacy map character */
726 key
= *((cups_vbcs_t
*)k1
);
727 map
= ((_cups_wide2uni_t
*)k2
)->widechar
;
729 return ((int)(key
- map
));
734 * 'conv_sbcs_to_utf8()' - Convert legacy SBCS to UTF-8.
737 static int /* O - Count or -1 on error */
739 cups_utf8_t
*dest
, /* O - Target string */
740 const cups_sbcs_t
*src
, /* I - Source string */
741 int maxout
, /* I - Max output */
742 const cups_encoding_t encoding
) /* I - Encoding */
744 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
745 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
746 cups_sbcs_t legchar
; /* Legacy character value */
747 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
748 *workptr
; /* Pointer into string */
752 * Find legacy charset map in cache...
755 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
759 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
763 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
768 * Convert ASCII verbatim (optimization)...
772 *workptr
++ = (cups_utf32_t
)legchar
;
776 * Convert unknown character to Replacement Character...
779 crow
= cmap
->char2uni
+ legchar
;
784 *workptr
++ = (cups_utf32_t
)*crow
;
791 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
796 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
801 * 'conv_utf8_to_sbcs()' - Convert UTF-8 to legacy SBCS.
804 static int /* O - Count or -1 on error */
806 cups_sbcs_t
*dest
, /* O - Target string */
807 const cups_utf8_t
*src
, /* I - Source string */
808 int maxout
, /* I - Max output */
809 const cups_encoding_t encoding
) /* I - Encoding */
811 cups_sbcs_t
*start
; /* Start of destination string */
812 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
813 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
814 cups_utf32_t unichar
; /* Character value */
815 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
816 *workptr
; /* Pointer into string */
820 * Find legacy charset map in cache...
823 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
827 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
830 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
834 * Convert internal UCS-4 to SBCS legacy charset (and delete BOM)...
837 for (workptr
= work
, start
= dest
; *workptr
&& maxout
> 0; maxout
--)
839 unichar
= *workptr
++;
844 * Convert ASCII verbatim (optimization)...
849 *dest
++ = (cups_sbcs_t
)unichar
;
854 * Convert unknown character to visible replacement...
857 srow
= cmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
860 srow
+= (int)(unichar
& 0xff);
872 return ((int)(dest
- start
));
877 * 'conv_utf8_to_vbcs()' - Convert UTF-8 to legacy DBCS/VBCS.
880 static int /* O - Count or -1 on error */
882 cups_sbcs_t
*dest
, /* O - Target string */
883 const cups_utf8_t
*src
, /* I - Source string */
884 int maxout
, /* I - Max output */
885 const cups_encoding_t encoding
) /* I - Encoding */
887 cups_sbcs_t
*start
; /* Start of destination string */
888 _cups_vmap_t
*vmap
; /* Legacy DBCS / Unicode Charset Map */
889 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
890 cups_utf32_t unichar
; /* Character value */
891 cups_vbcs_t legchar
; /* Legacy character value */
892 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
893 *workptr
; /* Pointer into string */
896 DEBUG_printf(("7conv_utf8_to_vbcs(dest=%p, src=\"%s\", maxout=%d, "
897 "encoding=%d)", dest
, src
, maxout
, encoding
));
900 * Find legacy charset map in cache...
903 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
905 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (no charmap)");
911 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
914 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
916 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (Unable to convert to UTF-32)");
922 * Convert internal UCS-4 to VBCS legacy charset (and delete BOM)...
925 for (start
= dest
, workptr
= work
; *workptr
&& maxout
> 0; maxout
--)
927 unichar
= *workptr
++;
930 * Convert ASCII verbatim (optimization)...
935 *dest
++ = (cups_sbcs_t
)unichar
;
937 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X", (unsigned)unichar
,
944 * Convert unknown character to visible replacement...
947 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
950 vrow
+= (int)(unichar
& 0xff);
953 legchar
= (cups_vbcs_t
)'?';
955 legchar
= (cups_vbcs_t
)*vrow
;
958 * Save n-byte legacy character...
961 if (legchar
> 0xffffff)
965 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (out of space)");
970 *dest
++ = (cups_sbcs_t
)(legchar
>> 24);
971 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
972 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
973 *dest
++ = (cups_sbcs_t
)legchar
;
977 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X %02X %02X",
978 (unsigned)unichar
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
980 else if (legchar
> 0xffff)
984 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (out of space)");
989 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
990 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
991 *dest
++ = (cups_sbcs_t
)legchar
;
995 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X %02X",
996 (unsigned)unichar
, dest
[-3], dest
[-2], dest
[-1]));
998 else if (legchar
> 0xff)
1000 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
1001 *dest
++ = (cups_sbcs_t
)legchar
;
1005 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X",
1006 (unsigned)unichar
, dest
[-2], dest
[-1]));
1010 *dest
++ = (cups_sbcs_t
)legchar
;
1012 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X",
1013 (unsigned)unichar
, dest
[-1]));
1021 DEBUG_printf(("8conv_utf8_to_vbcs: Returning %d characters",
1022 (int)(dest
- start
)));
1024 return ((int)(dest
- start
));
1029 * 'conv_vbcs_to_utf8()' - Convert legacy DBCS/VBCS to UTF-8.
1032 static int /* O - Count or -1 on error */
1034 cups_utf8_t
*dest
, /* O - Target string */
1035 const cups_sbcs_t
*src
, /* I - Source string */
1036 int maxout
, /* I - Max output */
1037 const cups_encoding_t encoding
) /* I - Encoding */
1039 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
1040 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1041 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
1042 cups_sbcs_t leadchar
; /* Lead char of n-byte legacy char */
1043 cups_vbcs_t legchar
; /* Legacy character value */
1044 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
1045 *workptr
; /* Pointer into string */
1049 * Find legacy charset map in cache...
1052 DEBUG_printf(("7conv_vbcs_to_utf8(dest=%p, src=%p, maxout=%d, encoding=%d)",
1053 dest
, src
, maxout
, encoding
));
1055 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
1057 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (NULL vmap)");
1063 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
1067 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
1070 leadchar
= (cups_sbcs_t
)legchar
;
1073 * Convert ASCII verbatim (optimization)...
1078 *workptr
++ = (cups_utf32_t
)legchar
;
1080 DEBUG_printf(("9conv_vbcs_to_utf8: %02X => %08X", src
[-1],
1081 (unsigned)legchar
));
1086 * Convert 2-byte legacy character...
1089 if (vmap
->lead2char
[(int)leadchar
] == leadchar
)
1093 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string)");
1098 legchar
= (legchar
<< 8) | *src
++;
1101 * Convert unknown character to Replacement Character...
1104 crow
= vmap
->char2uni
[(int)((legchar
>> 8) & 0xff)];
1106 crow
+= (int) (legchar
& 0xff);
1108 if (!crow
|| !*crow
)
1109 *workptr
++ = 0xfffd;
1111 *workptr
++ = (cups_utf32_t
)*crow
;
1113 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X => %08X",
1114 src
[-2], src
[-1], (unsigned)workptr
[-1]));
1119 * Fetch 3-byte or 4-byte legacy character...
1122 if (vmap
->lead3char
[(int)leadchar
] == leadchar
)
1124 if (!*src
|| !src
[1])
1126 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string 2)");
1131 legchar
= (legchar
<< 8) | *src
++;
1132 legchar
= (legchar
<< 8) | *src
++;
1134 else if (vmap
->lead4char
[(int)leadchar
] == leadchar
)
1136 if (!*src
|| !src
[1] || !src
[2])
1138 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string 3)");
1143 legchar
= (legchar
<< 8) | *src
++;
1144 legchar
= (legchar
<< 8) | *src
++;
1145 legchar
= (legchar
<< 8) | *src
++;
1149 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (bad character)");
1155 * Find 3-byte or 4-byte legacy character...
1158 wide2uni
= (_cups_wide2uni_t
*)bsearch(&legchar
,
1161 sizeof(_cups_wide2uni_t
),
1165 * Convert unknown character to Replacement Character...
1168 if (!wide2uni
|| !wide2uni
->unichar
)
1169 *workptr
++ = 0xfffd;
1171 *workptr
++ = wide2uni
->unichar
;
1173 if (vmap
->lead3char
[(int)leadchar
] == leadchar
)
1174 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X %02X => %08X",
1175 src
[-3], src
[-2], src
[-1], (unsigned)workptr
[-1]));
1177 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X %02X %02X => %08X",
1178 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)workptr
[-1]));
1185 DEBUG_printf(("9conv_vbcs_to_utf8: Converting %d UTF-32 characters to UTF-8",
1186 (int)(workptr
- work
)));
1189 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
1192 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
1197 * 'free_sbcs_charmap()' - Free memory used by a single byte character set.
1201 free_sbcs_charmap(_cups_cmap_t
*cmap
) /* I - Character set */
1203 int i
; /* Looping variable */
1206 for (i
= 0; i
< 256; i
++)
1207 if (cmap
->uni2char
[i
])
1208 free(cmap
->uni2char
[i
]);
1215 * 'free_vbcs_charmap()' - Free memory used by a variable byte character set.
1219 free_vbcs_charmap(_cups_vmap_t
*vmap
) /* I - Character set */
1221 int i
; /* Looping variable */
1224 for (i
= 0; i
< 256; i
++)
1225 if (vmap
->char2uni
[i
])
1226 free(vmap
->char2uni
[i
]);
1228 for (i
= 0; i
< 256; i
++)
1229 if (vmap
->uni2char
[i
])
1230 free(vmap
->uni2char
[i
]);
1233 free(vmap
->wide2uni
);
1240 * 'get_charmap()' - Lookup or get a character set map (private).
1242 * This code handles single-byte (SBCS), double-byte (DBCS), and
1243 * variable-byte (VBCS) character sets _without_ charset escapes...
1244 * This code does not handle multiple-byte character sets (MBCS)
1245 * (such as ISO-2022-JP) with charset switching via escapes...
1249 static void * /* O - Charset map pointer */
1251 const cups_encoding_t encoding
) /* I - Encoding */
1253 char filename
[1024]; /* Filename for charset map file */
1254 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1257 DEBUG_printf(("7get_charmap(encoding=%d)", encoding
));
1260 * Get the data directory and charset map name...
1263 snprintf(filename
, sizeof(filename
), "%s/charmaps/%s.txt",
1264 cg
->cups_datadir
, _cupsEncodingName(encoding
));
1266 DEBUG_printf(("9get_charmap: filename=\"%s\"", filename
));
1269 * Read charset map input file into cache...
1272 if (encoding
< CUPS_ENCODING_SBCS_END
)
1273 return (get_sbcs_charmap(encoding
, filename
));
1274 else if (encoding
< CUPS_ENCODING_VBCS_END
)
1275 return (get_vbcs_charmap(encoding
, filename
));
1282 * 'get_charmap_count()' - Count lines in a charmap file.
1285 static int /* O - Count or -1 on error */
1286 get_charmap_count(cups_file_t
*fp
) /* I - File to read from */
1288 int count
; /* Number of lines */
1289 char line
[256]; /* Line from input map file */
1293 * Count lines in map input file...
1298 while (cupsFileGets(fp
, line
, sizeof(line
)))
1303 * Return the number of lines...
1314 * 'get_sbcs_charmap()' - Get SBCS Charmap.
1317 static _cups_cmap_t
* /* O - Charmap or 0 on error */
1319 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1320 const char *filename
) /* I - Charmap Filename */
1322 unsigned long legchar
; /* Legacy character value */
1323 cups_utf32_t unichar
; /* Unicode character value */
1324 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
1325 cups_file_t
*fp
; /* Charset map file pointer */
1326 char *s
; /* Line parsing pointer */
1327 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1328 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
1329 char line
[256]; /* Line from charset map file */
1333 * See if we already have this SBCS charset map loaded...
1336 DEBUG_printf(("7get_sbcs_charmap(encoding=%d, filename=\"%s\")", encoding
,
1339 for (cmap
= cmap_cache
; cmap
; cmap
= cmap
->next
)
1341 if (cmap
->encoding
== encoding
)
1344 DEBUG_printf(("8get_sbcs_charmap: Returning existing cmap=%p", cmap
));
1346 return ((void *)cmap
);
1351 * Open SBCS charset map input file...
1354 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1356 DEBUG_printf(("8get_sbcs_charmap: Returning NULL (%s)", strerror(errno
)));
1362 * Allocate memory for SBCS charset map...
1365 if ((cmap
= (_cups_cmap_t
*)calloc(1, sizeof(_cups_cmap_t
))) == NULL
)
1368 DEBUG_puts("8get_sbcs_charmap: Returning NULL (Unable to allocate memory)");
1374 cmap
->encoding
= encoding
;
1377 * Save SBCS charset map into memory for transcoding...
1380 while (cupsFileGets(fp
, line
, sizeof(line
)))
1385 legchar
= strtol(line
, &s
, 16);
1386 if (legchar
< 0 || legchar
> 0xff)
1389 unichar
= strtol(s
, NULL
, 16);
1390 if (unichar
< 0 || unichar
> 0x10ffff)
1394 * Save legacy to Unicode mapping in direct lookup table...
1397 crow
= cmap
->char2uni
+ legchar
;
1398 *crow
= (cups_ucs2_t
)(unichar
& 0xffff);
1401 * Save Unicode to legacy mapping in indirect lookup table...
1404 srow
= cmap
->uni2char
[(unichar
>> 8) & 0xff];
1407 srow
= (cups_sbcs_t
*)calloc(256, sizeof(cups_sbcs_t
));
1411 cmap
->uni2char
[(unichar
>> 8) & 0xff] = srow
;
1414 srow
+= unichar
& 0xff;
1417 * Convert Replacement Character to visible replacement...
1420 if (unichar
== 0xfffd)
1421 legchar
= (unsigned long)'?';
1424 * First (oldest) legacy character uses Unicode mapping cell...
1428 *srow
= (cups_sbcs_t
)legchar
;
1434 * Add it to the cache and return...
1437 cmap
->next
= cmap_cache
;
1440 DEBUG_printf(("8get_sbcs_charmap: Returning new cmap=%p", cmap
));
1445 * If we get here, there was an error in the cmap file...
1450 free_sbcs_charmap(cmap
);
1454 DEBUG_puts("8get_sbcs_charmap: Returning NULL (Read/format error)");
1461 * 'get_vbcs_charmap()' - Get DBCS/VBCS Charmap.
1464 static _cups_vmap_t
* /* O - Charmap or 0 on error */
1466 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1467 const char *filename
) /* I - Charmap Filename */
1469 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
1470 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1471 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
1472 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
1473 cups_sbcs_t leadchar
; /* Lead char of 2-byte legacy char */
1474 unsigned long legchar
; /* Legacy character value */
1475 cups_utf32_t unichar
; /* Unicode character value */
1476 int mapcount
; /* Count of lines in charmap file */
1477 cups_file_t
*fp
; /* Charset map file pointer */
1478 char *s
; /* Line parsing pointer */
1479 char line
[256]; /* Line from charset map file */
1480 int i
; /* Loop variable */
1481 int legacy
; /* 32-bit legacy char */
1484 DEBUG_printf(("7get_vbcs_charmap(encoding=%d, filename=\"%s\")\n",
1485 encoding
, filename
));
1488 * See if we already have this DBCS/VBCS charset map loaded...
1491 for (vmap
= vmap_cache
; vmap
; vmap
= vmap
->next
)
1493 if (vmap
->encoding
== encoding
)
1496 DEBUG_printf(("8get_vbcs_charmap: Returning existing vmap=%p", vmap
));
1498 return ((void *)vmap
);
1503 * Open VBCS charset map input file...
1506 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1508 DEBUG_printf(("8get_vbcs_charmap: Returning NULL (%s)", strerror(errno
)));
1514 * Count lines in charmap file...
1517 if ((mapcount
= get_charmap_count(fp
)) <= 0)
1519 DEBUG_puts("8get_vbcs_charmap: Unable to get charmap count!");
1526 DEBUG_printf(("8get_vbcs_charmap: mapcount=%d", mapcount
));
1529 * Allocate memory for DBCS/VBCS charset map...
1532 if ((vmap
= (_cups_vmap_t
*)calloc(1, sizeof(_cups_vmap_t
))) == NULL
)
1534 DEBUG_puts("8get_vbcs_charmap: Unable to allocate memory!");
1542 vmap
->encoding
= encoding
;
1545 * Save DBCS/VBCS charset map into memory for transcoding...
1555 while (cupsFileGets(fp
, line
, sizeof(line
)))
1560 legchar
= strtoul(line
, &s
, 16);
1561 if (legchar
== ULONG_MAX
)
1564 unichar
= strtol(s
, NULL
, 16);
1565 if (unichar
< 0 || unichar
> 0x10ffff)
1570 DEBUG_printf(("9get_vbcs_charmap: i=%d, legchar=0x%08lx, unichar=0x%04x", i
,
1571 legchar
, (unsigned)unichar
));
1574 * Save lead char of 2/3/4-byte legacy char...
1577 if (legchar
> 0xffffff)
1579 leadchar
= (cups_sbcs_t
)(legchar
>> 24);
1580 vmap
->lead4char
[leadchar
] = leadchar
;
1582 else if (legchar
> 0xffff)
1584 leadchar
= (cups_sbcs_t
)(legchar
>> 16);
1585 vmap
->lead3char
[leadchar
] = leadchar
;
1589 leadchar
= (cups_sbcs_t
)(legchar
>> 8);
1590 vmap
->lead2char
[leadchar
] = leadchar
;
1594 * Save Legacy to Unicode mapping...
1597 if (legchar
<= 0xffff)
1600 * Save DBCS 16-bit to Unicode mapping in indirect lookup table...
1603 crow
= vmap
->char2uni
[(int)leadchar
];
1606 crow
= (cups_ucs2_t
*)calloc(256, sizeof(cups_ucs2_t
));
1610 vmap
->char2uni
[(int)leadchar
] = crow
;
1613 crow
[(int)(legchar
& 0xff)] = (cups_ucs2_t
)unichar
;
1618 * Save VBCS 32-bit to Unicode mapping in sorted list table...
1624 vmap
->widecount
= (mapcount
- i
+ 1);
1625 wide2uni
= (_cups_wide2uni_t
*)calloc(vmap
->widecount
,
1626 sizeof(_cups_wide2uni_t
));
1630 vmap
->wide2uni
= wide2uni
;
1633 wide2uni
->widechar
= (cups_vbcs_t
)legchar
;
1634 wide2uni
->unichar
= (cups_ucs2_t
)unichar
;
1639 * Save Unicode to legacy mapping in indirect lookup table...
1642 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
1645 vrow
= (cups_vbcs_t
*)calloc(256, sizeof(cups_vbcs_t
));
1649 vmap
->uni2char
[(int) ((unichar
>> 8) & 0xff)] = vrow
;
1652 vrow
+= (int)(unichar
& 0xff);
1655 * Convert Replacement Character to visible replacement...
1658 if (unichar
== 0xfffd)
1659 legchar
= (unsigned long)'?';
1662 * First (oldest) legacy character uses Unicode mapping cell...
1666 *vrow
= (cups_vbcs_t
)legchar
;
1669 vmap
->charcount
= (i
- vmap
->widecount
);
1674 * Add it to the cache and return...
1677 vmap
->next
= vmap_cache
;
1680 DEBUG_printf(("8get_vbcs_charmap: Returning new vmap=%p", vmap
));
1685 * If we get here, the file contains errors...
1690 free_vbcs_charmap(vmap
);
1694 DEBUG_puts("8get_vbcs_charmap: Returning NULL (Read/format error)");
1701 * End of "$Id: transcode.c 7560 2008-05-13 06:34:04Z mike $"