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 size_t srclen
, /* Length of source string */
92 outBytesLeft
; /* Bytes remaining in output buffer */
96 * Check for valid arguments...
99 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)",
100 dest
, src
, maxout
, encoding
));
102 if (!dest
|| !src
|| maxout
< 1)
107 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
112 * Handle identity conversions...
115 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
116 encoding
>= CUPS_ENCODING_VBCS_END
)
118 strlcpy((char *)dest
, src
, maxout
);
119 return ((int)strlen((char *)dest
));
123 * Handle ISO-8859-1 to UTF-8 directly...
128 if (encoding
== CUPS_ISO8859_1
)
130 int ch
; /* Character from string */
131 cups_utf8_t
*destend
; /* End of UTF-8 buffer */
134 destend
= dest
+ maxout
- 2;
136 while (*src
&& destptr
< destend
)
142 *destptr
++ = 0xc0 | (ch
>> 6);
143 *destptr
++ = 0x80 | (ch
& 0x3f);
151 return ((int)(destptr
- dest
));
155 * Convert input legacy charset to UTF-8...
159 _cupsMutexLock(&map_mutex
);
161 if (map_encoding
!= encoding
)
165 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
166 map_to_utf8
= iconv_open("UTF-8", _cupsEncodingName(encoding
));
167 map_encoding
= encoding
;
170 if (map_to_utf8
!= (iconv_t
)-1)
172 srclen
= strlen(src
);
173 outBytesLeft
= maxout
- 1;
175 iconv(map_to_utf8
, (char **)&src
, &srclen
, (char **)&destptr
,
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 size_t srclen
, /* Length of source string */
210 outBytesLeft
; /* Bytes remaining in output buffer */
214 * Check for valid arguments...
217 if (!dest
|| !src
|| maxout
< 1)
226 * Handle identity conversions...
229 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
230 encoding
>= CUPS_ENCODING_VBCS_END
)
232 strlcpy(dest
, (char *)src
, maxout
);
233 return ((int)strlen(dest
));
237 * Handle UTF-8 to ISO-8859-1 directly...
242 if (encoding
== CUPS_ISO8859_1
)
244 int ch
; /* Character from string */
245 char *destend
; /* End of ISO-8859-1 buffer */
248 destend
= dest
+ maxout
- 1;
250 while (*src
&& destptr
< destend
)
254 if ((ch
& 0xe0) == 0xc0)
256 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
263 else if ((ch
& 0xf0) == 0xe0 ||
266 else if (!(ch
& 0x80))
272 return ((int)(destptr
- dest
));
277 * Convert input UTF-8 to legacy charset...
280 _cupsMutexLock(&map_mutex
);
282 if (map_encoding
!= encoding
)
286 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
287 map_to_utf8
= iconv_open("UTF-8", _cupsEncodingName(encoding
));
288 map_encoding
= encoding
;
291 if (map_from_utf8
!= (iconv_t
)-1)
293 srclen
= strlen((char *)src
);
294 outBytesLeft
= maxout
- 1;
296 iconv(map_from_utf8
, (char **)&src
, &srclen
, &destptr
, &outBytesLeft
);
299 _cupsMutexUnlock(&map_mutex
);
301 return ((int)(destptr
- dest
));
304 _cupsMutexUnlock(&map_mutex
);
305 #endif /* HAVE_ICONV_H */
308 * No iconv() support, so error out...
318 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
320 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
322 * UTF-32 char UTF-8 char(s)
323 * --------------------------------------------------
324 * 0 to 127 = 0xxxxxxx (US-ASCII)
325 * 128 to 2047 = 110xxxxx 10yyyyyy
326 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
327 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
329 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
330 * which would convert to five- or six-octet UTF-8 sequences...
333 int /* O - Count or -1 on error */
335 cups_utf32_t
*dest
, /* O - Target string */
336 const cups_utf8_t
*src
, /* I - Source string */
337 const int maxout
) /* I - Max output */
339 int i
; /* Looping variable */
340 cups_utf8_t ch
; /* Character value */
341 cups_utf8_t next
; /* Next character value */
342 cups_utf32_t ch32
; /* UTF-32 character value */
346 * Check for valid arguments and clear output...
349 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest
,
355 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
357 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
363 * Convert input UTF-8 to output UTF-32...
366 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
371 * Convert UTF-8 character(s) to UTF-32 character...
377 * One-octet UTF-8 <= 127 (US-ASCII)...
382 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src
[-1], ch
));
385 else if ((ch
& 0xe0) == 0xc0)
388 * Two-octet UTF-8 <= 2047 (Latin-x)...
392 if ((next
& 0xc0) != 0x80)
394 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
399 ch32
= ((ch
& 0x1f) << 6) | (next
& 0x3f);
402 * Check for non-shortest form (invalid UTF-8)...
407 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
414 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
415 src
[-2], src
[-1], (unsigned)ch32
));
417 else if ((ch
& 0xf0) == 0xe0)
420 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
424 if ((next
& 0xc0) != 0x80)
426 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
431 ch32
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
434 if ((next
& 0xc0) != 0x80)
436 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
441 ch32
= (ch32
<< 6) | (next
& 0x3f);
444 * Check for non-shortest form (invalid UTF-8)...
449 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
456 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
457 src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
459 else if ((ch
& 0xf8) == 0xf0)
462 * Four-octet UTF-8...
466 if ((next
& 0xc0) != 0x80)
468 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
473 ch32
= ((ch
& 0x07) << 6) | (next
& 0x3f);
476 if ((next
& 0xc0) != 0x80)
478 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
483 ch32
= (ch32
<< 6) | (next
& 0x3f);
486 if ((next
& 0xc0) != 0x80)
488 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
493 ch32
= (ch32
<< 6) | (next
& 0x3f);
496 * Check for non-shortest form (invalid UTF-8)...
501 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
508 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
509 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
514 * More than 4-octet (invalid UTF-8 sequence)...
517 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
523 * Check for UTF-16 surrogate (illegal UTF-8)...
526 if (ch32
>= 0xd800 && ch32
<= 0xdfff)
532 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout
- 1 - i
));
534 return (maxout
- 1 - i
);
539 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
541 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
543 * UTF-32 char UTF-8 char(s)
544 * --------------------------------------------------
545 * 0 to 127 = 0xxxxxxx (US-ASCII)
546 * 128 to 2047 = 110xxxxx 10yyyyyy
547 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
548 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
550 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
551 * which would convert to five- or six-octet UTF-8 sequences...
554 int /* O - Count or -1 on error */
556 cups_utf8_t
*dest
, /* O - Target string */
557 const cups_utf32_t
*src
, /* I - Source string */
558 const int maxout
) /* I - Max output */
560 cups_utf8_t
*start
; /* Start of destination string */
561 int i
; /* Looping variable */
562 int swap
; /* Byte-swap input to output */
563 cups_utf32_t ch
; /* Character value */
567 * Check for valid arguments and clear output...
570 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest
, src
,
576 if (!dest
|| !src
|| maxout
< 1)
578 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
584 * Check for leading BOM in UTF-32 and inverted BOM...
588 swap
= *src
== 0xfffe0000;
590 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap
));
592 if (*src
== 0xfffe0000 || *src
== 0xfeff)
596 * Convert input UTF-32 to output UTF-8...
599 for (i
= maxout
- 1; *src
&& i
> 0;)
604 * Byte swap input UTF-32, if necessary...
605 * (only byte-swapping 24 of 32 bits)
609 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
612 * Check for beyond Plane 16 (invalid UTF-32)...
617 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
623 * Convert UTF-32 character to UTF-8 character(s)...
629 * One-octet UTF-8 <= 127 (US-ASCII)...
632 *dest
++ = (cups_utf8_t
)ch
;
635 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch
, dest
[-1]));
640 * Two-octet UTF-8 <= 2047 (Latin-x)...
645 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
650 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
651 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
654 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch
,
655 dest
[-2], dest
[-1]));
657 else if (ch
< 0x10000)
660 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
665 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
670 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
671 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
672 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
675 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch
,
676 dest
[-3], dest
[-2], dest
[-1]));
681 * Four-octet UTF-8...
686 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
691 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
692 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
693 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
694 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
697 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
698 (unsigned)ch
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
704 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest
- start
)));
706 return ((int)(dest
- start
));
711 * 'compare_wide()' - Compare key for wide (VBCS) match.
715 compare_wide(const void *k1
, /* I - Key char */
716 const void *k2
) /* I - Map char */
718 cups_vbcs_t key
; /* Legacy key character */
719 cups_vbcs_t map
; /* Legacy map character */
722 key
= *((cups_vbcs_t
*)k1
);
723 map
= ((_cups_wide2uni_t
*)k2
)->widechar
;
725 return ((int)(key
- map
));
730 * 'conv_sbcs_to_utf8()' - Convert legacy SBCS to UTF-8.
733 static int /* O - Count or -1 on error */
735 cups_utf8_t
*dest
, /* O - Target string */
736 const cups_sbcs_t
*src
, /* I - Source string */
737 int maxout
, /* I - Max output */
738 const cups_encoding_t encoding
) /* I - Encoding */
740 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
741 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
742 cups_sbcs_t legchar
; /* Legacy character value */
743 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
744 *workptr
; /* Pointer into string */
748 * Find legacy charset map in cache...
751 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
755 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
759 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
764 * Convert ASCII verbatim (optimization)...
768 *workptr
++ = (cups_utf32_t
)legchar
;
772 * Convert unknown character to Replacement Character...
775 crow
= cmap
->char2uni
+ legchar
;
780 *workptr
++ = (cups_utf32_t
)*crow
;
787 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
792 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
797 * 'conv_utf8_to_sbcs()' - Convert UTF-8 to legacy SBCS.
800 static int /* O - Count or -1 on error */
802 cups_sbcs_t
*dest
, /* O - Target string */
803 const cups_utf8_t
*src
, /* I - Source string */
804 int maxout
, /* I - Max output */
805 const cups_encoding_t encoding
) /* I - Encoding */
807 cups_sbcs_t
*start
; /* Start of destination string */
808 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
809 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
810 cups_utf32_t unichar
; /* Character value */
811 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
812 *workptr
; /* Pointer into string */
816 * Find legacy charset map in cache...
819 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
823 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
826 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
830 * Convert internal UCS-4 to SBCS legacy charset (and delete BOM)...
833 for (workptr
= work
, start
= dest
; *workptr
&& maxout
> 0; maxout
--)
835 unichar
= *workptr
++;
840 * Convert ASCII verbatim (optimization)...
845 *dest
++ = (cups_sbcs_t
)unichar
;
850 * Convert unknown character to visible replacement...
853 srow
= cmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
856 srow
+= (int)(unichar
& 0xff);
868 return ((int)(dest
- start
));
873 * 'conv_utf8_to_vbcs()' - Convert UTF-8 to legacy DBCS/VBCS.
876 static int /* O - Count or -1 on error */
878 cups_sbcs_t
*dest
, /* O - Target string */
879 const cups_utf8_t
*src
, /* I - Source string */
880 int maxout
, /* I - Max output */
881 const cups_encoding_t encoding
) /* I - Encoding */
883 cups_sbcs_t
*start
; /* Start of destination string */
884 _cups_vmap_t
*vmap
; /* Legacy DBCS / Unicode Charset Map */
885 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
886 cups_utf32_t unichar
; /* Character value */
887 cups_vbcs_t legchar
; /* Legacy character value */
888 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
889 *workptr
; /* Pointer into string */
892 DEBUG_printf(("7conv_utf8_to_vbcs(dest=%p, src=\"%s\", maxout=%d, "
893 "encoding=%d)", dest
, src
, maxout
, encoding
));
896 * Find legacy charset map in cache...
899 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
901 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (no charmap)");
907 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
910 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
912 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (Unable to convert to UTF-32)");
918 * Convert internal UCS-4 to VBCS legacy charset (and delete BOM)...
921 for (start
= dest
, workptr
= work
; *workptr
&& maxout
> 0; maxout
--)
923 unichar
= *workptr
++;
926 * Convert ASCII verbatim (optimization)...
931 *dest
++ = (cups_sbcs_t
)unichar
;
933 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X", (unsigned)unichar
,
940 * Convert unknown character to visible replacement...
943 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
946 vrow
+= (int)(unichar
& 0xff);
949 legchar
= (cups_vbcs_t
)'?';
951 legchar
= (cups_vbcs_t
)*vrow
;
954 * Save n-byte legacy character...
957 if (legchar
> 0xffffff)
961 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (out of space)");
966 *dest
++ = (cups_sbcs_t
)(legchar
>> 24);
967 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
968 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
969 *dest
++ = (cups_sbcs_t
)legchar
;
973 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X %02X %02X",
974 (unsigned)unichar
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
976 else if (legchar
> 0xffff)
980 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (out of space)");
985 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
986 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
987 *dest
++ = (cups_sbcs_t
)legchar
;
991 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X %02X",
992 (unsigned)unichar
, dest
[-3], dest
[-2], dest
[-1]));
994 else if (legchar
> 0xff)
996 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
997 *dest
++ = (cups_sbcs_t
)legchar
;
1001 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X",
1002 (unsigned)unichar
, dest
[-2], dest
[-1]));
1006 *dest
++ = (cups_sbcs_t
)legchar
;
1008 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X",
1009 (unsigned)unichar
, dest
[-1]));
1017 DEBUG_printf(("8conv_utf8_to_vbcs: Returning %d characters",
1018 (int)(dest
- start
)));
1020 return ((int)(dest
- start
));
1025 * 'conv_vbcs_to_utf8()' - Convert legacy DBCS/VBCS to UTF-8.
1028 static int /* O - Count or -1 on error */
1030 cups_utf8_t
*dest
, /* O - Target string */
1031 const cups_sbcs_t
*src
, /* I - Source string */
1032 int maxout
, /* I - Max output */
1033 const cups_encoding_t encoding
) /* I - Encoding */
1035 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
1036 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1037 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
1038 cups_sbcs_t leadchar
; /* Lead char of n-byte legacy char */
1039 cups_vbcs_t legchar
; /* Legacy character value */
1040 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
1041 *workptr
; /* Pointer into string */
1045 * Find legacy charset map in cache...
1048 DEBUG_printf(("7conv_vbcs_to_utf8(dest=%p, src=%p, maxout=%d, encoding=%d)",
1049 dest
, src
, maxout
, encoding
));
1051 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
1053 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (NULL vmap)");
1059 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
1063 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
1066 leadchar
= (cups_sbcs_t
)legchar
;
1069 * Convert ASCII verbatim (optimization)...
1074 *workptr
++ = (cups_utf32_t
)legchar
;
1076 DEBUG_printf(("9conv_vbcs_to_utf8: %02X => %08X", src
[-1],
1077 (unsigned)legchar
));
1082 * Convert 2-byte legacy character...
1085 if (vmap
->lead2char
[(int)leadchar
] == leadchar
)
1089 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string)");
1094 legchar
= (legchar
<< 8) | *src
++;
1097 * Convert unknown character to Replacement Character...
1100 crow
= vmap
->char2uni
[(int)((legchar
>> 8) & 0xff)];
1102 crow
+= (int) (legchar
& 0xff);
1104 if (!crow
|| !*crow
)
1105 *workptr
++ = 0xfffd;
1107 *workptr
++ = (cups_utf32_t
)*crow
;
1109 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X => %08X",
1110 src
[-2], src
[-1], (unsigned)workptr
[-1]));
1115 * Fetch 3-byte or 4-byte legacy character...
1118 if (vmap
->lead3char
[(int)leadchar
] == leadchar
)
1120 if (!*src
|| !src
[1])
1122 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string 2)");
1127 legchar
= (legchar
<< 8) | *src
++;
1128 legchar
= (legchar
<< 8) | *src
++;
1130 else if (vmap
->lead4char
[(int)leadchar
] == leadchar
)
1132 if (!*src
|| !src
[1] || !src
[2])
1134 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string 3)");
1139 legchar
= (legchar
<< 8) | *src
++;
1140 legchar
= (legchar
<< 8) | *src
++;
1141 legchar
= (legchar
<< 8) | *src
++;
1145 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (bad character)");
1151 * Find 3-byte or 4-byte legacy character...
1154 wide2uni
= (_cups_wide2uni_t
*)bsearch(&legchar
,
1157 sizeof(_cups_wide2uni_t
),
1161 * Convert unknown character to Replacement Character...
1164 if (!wide2uni
|| !wide2uni
->unichar
)
1165 *workptr
++ = 0xfffd;
1167 *workptr
++ = wide2uni
->unichar
;
1169 if (vmap
->lead3char
[(int)leadchar
] == leadchar
)
1170 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X %02X => %08X",
1171 src
[-3], src
[-2], src
[-1], (unsigned)workptr
[-1]));
1173 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X %02X %02X => %08X",
1174 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)workptr
[-1]));
1181 DEBUG_printf(("9conv_vbcs_to_utf8: Converting %d UTF-32 characters to UTF-8",
1182 (int)(workptr
- work
)));
1185 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
1188 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
1193 * 'free_sbcs_charmap()' - Free memory used by a single byte character set.
1197 free_sbcs_charmap(_cups_cmap_t
*cmap
) /* I - Character set */
1199 int i
; /* Looping variable */
1202 for (i
= 0; i
< 256; i
++)
1203 if (cmap
->uni2char
[i
])
1204 free(cmap
->uni2char
[i
]);
1211 * 'free_vbcs_charmap()' - Free memory used by a variable byte character set.
1215 free_vbcs_charmap(_cups_vmap_t
*vmap
) /* I - Character set */
1217 int i
; /* Looping variable */
1220 for (i
= 0; i
< 256; i
++)
1221 if (vmap
->char2uni
[i
])
1222 free(vmap
->char2uni
[i
]);
1224 for (i
= 0; i
< 256; i
++)
1225 if (vmap
->uni2char
[i
])
1226 free(vmap
->uni2char
[i
]);
1229 free(vmap
->wide2uni
);
1236 * 'get_charmap()' - Lookup or get a character set map (private).
1238 * This code handles single-byte (SBCS), double-byte (DBCS), and
1239 * variable-byte (VBCS) character sets _without_ charset escapes...
1240 * This code does not handle multiple-byte character sets (MBCS)
1241 * (such as ISO-2022-JP) with charset switching via escapes...
1245 static void * /* O - Charset map pointer */
1247 const cups_encoding_t encoding
) /* I - Encoding */
1249 char filename
[1024]; /* Filename for charset map file */
1250 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1253 DEBUG_printf(("7get_charmap(encoding=%d)", encoding
));
1256 * Get the data directory and charset map name...
1259 snprintf(filename
, sizeof(filename
), "%s/charmaps/%s.txt",
1260 cg
->cups_datadir
, _cupsEncodingName(encoding
));
1262 DEBUG_printf(("9get_charmap: filename=\"%s\"", filename
));
1265 * Read charset map input file into cache...
1268 if (encoding
< CUPS_ENCODING_SBCS_END
)
1269 return (get_sbcs_charmap(encoding
, filename
));
1270 else if (encoding
< CUPS_ENCODING_VBCS_END
)
1271 return (get_vbcs_charmap(encoding
, filename
));
1278 * 'get_charmap_count()' - Count lines in a charmap file.
1281 static int /* O - Count or -1 on error */
1282 get_charmap_count(cups_file_t
*fp
) /* I - File to read from */
1284 int count
; /* Number of lines */
1285 char line
[256]; /* Line from input map file */
1289 * Count lines in map input file...
1294 while (cupsFileGets(fp
, line
, sizeof(line
)))
1299 * Return the number of lines...
1310 * 'get_sbcs_charmap()' - Get SBCS Charmap.
1313 static _cups_cmap_t
* /* O - Charmap or 0 on error */
1315 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1316 const char *filename
) /* I - Charmap Filename */
1318 unsigned long legchar
; /* Legacy character value */
1319 cups_utf32_t unichar
; /* Unicode character value */
1320 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
1321 cups_file_t
*fp
; /* Charset map file pointer */
1322 char *s
; /* Line parsing pointer */
1323 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1324 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
1325 char line
[256]; /* Line from charset map file */
1329 * See if we already have this SBCS charset map loaded...
1332 DEBUG_printf(("7get_sbcs_charmap(encoding=%d, filename=\"%s\")", encoding
,
1335 for (cmap
= cmap_cache
; cmap
; cmap
= cmap
->next
)
1337 if (cmap
->encoding
== encoding
)
1340 DEBUG_printf(("8get_sbcs_charmap: Returning existing cmap=%p", cmap
));
1342 return ((void *)cmap
);
1347 * Open SBCS charset map input file...
1350 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1352 DEBUG_printf(("8get_sbcs_charmap: Returning NULL (%s)", strerror(errno
)));
1358 * Allocate memory for SBCS charset map...
1361 if ((cmap
= (_cups_cmap_t
*)calloc(1, sizeof(_cups_cmap_t
))) == NULL
)
1364 DEBUG_puts("8get_sbcs_charmap: Returning NULL (Unable to allocate memory)");
1370 cmap
->encoding
= encoding
;
1373 * Save SBCS charset map into memory for transcoding...
1376 while (cupsFileGets(fp
, line
, sizeof(line
)))
1381 legchar
= strtol(line
, &s
, 16);
1382 if (legchar
< 0 || legchar
> 0xff)
1385 unichar
= strtol(s
, NULL
, 16);
1386 if (unichar
< 0 || unichar
> 0x10ffff)
1390 * Save legacy to Unicode mapping in direct lookup table...
1393 crow
= cmap
->char2uni
+ legchar
;
1394 *crow
= (cups_ucs2_t
)(unichar
& 0xffff);
1397 * Save Unicode to legacy mapping in indirect lookup table...
1400 srow
= cmap
->uni2char
[(unichar
>> 8) & 0xff];
1403 srow
= (cups_sbcs_t
*)calloc(256, sizeof(cups_sbcs_t
));
1407 cmap
->uni2char
[(unichar
>> 8) & 0xff] = srow
;
1410 srow
+= unichar
& 0xff;
1413 * Convert Replacement Character to visible replacement...
1416 if (unichar
== 0xfffd)
1417 legchar
= (unsigned long)'?';
1420 * First (oldest) legacy character uses Unicode mapping cell...
1424 *srow
= (cups_sbcs_t
)legchar
;
1430 * Add it to the cache and return...
1433 cmap
->next
= cmap_cache
;
1436 DEBUG_printf(("8get_sbcs_charmap: Returning new cmap=%p", cmap
));
1441 * If we get here, there was an error in the cmap file...
1446 free_sbcs_charmap(cmap
);
1450 DEBUG_puts("8get_sbcs_charmap: Returning NULL (Read/format error)");
1457 * 'get_vbcs_charmap()' - Get DBCS/VBCS Charmap.
1460 static _cups_vmap_t
* /* O - Charmap or 0 on error */
1462 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1463 const char *filename
) /* I - Charmap Filename */
1465 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
1466 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1467 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
1468 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
1469 cups_sbcs_t leadchar
; /* Lead char of 2-byte legacy char */
1470 unsigned long legchar
; /* Legacy character value */
1471 cups_utf32_t unichar
; /* Unicode character value */
1472 int mapcount
; /* Count of lines in charmap file */
1473 cups_file_t
*fp
; /* Charset map file pointer */
1474 char *s
; /* Line parsing pointer */
1475 char line
[256]; /* Line from charset map file */
1476 int i
; /* Loop variable */
1477 int legacy
; /* 32-bit legacy char */
1480 DEBUG_printf(("7get_vbcs_charmap(encoding=%d, filename=\"%s\")\n",
1481 encoding
, filename
));
1484 * See if we already have this DBCS/VBCS charset map loaded...
1487 for (vmap
= vmap_cache
; vmap
; vmap
= vmap
->next
)
1489 if (vmap
->encoding
== encoding
)
1492 DEBUG_printf(("8get_vbcs_charmap: Returning existing vmap=%p", vmap
));
1494 return ((void *)vmap
);
1499 * Open VBCS charset map input file...
1502 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1504 DEBUG_printf(("8get_vbcs_charmap: Returning NULL (%s)", strerror(errno
)));
1510 * Count lines in charmap file...
1513 if ((mapcount
= get_charmap_count(fp
)) <= 0)
1515 DEBUG_puts("8get_vbcs_charmap: Unable to get charmap count!");
1522 DEBUG_printf(("8get_vbcs_charmap: mapcount=%d", mapcount
));
1525 * Allocate memory for DBCS/VBCS charset map...
1528 if ((vmap
= (_cups_vmap_t
*)calloc(1, sizeof(_cups_vmap_t
))) == NULL
)
1530 DEBUG_puts("8get_vbcs_charmap: Unable to allocate memory!");
1538 vmap
->encoding
= encoding
;
1541 * Save DBCS/VBCS charset map into memory for transcoding...
1551 while (cupsFileGets(fp
, line
, sizeof(line
)))
1556 legchar
= strtoul(line
, &s
, 16);
1557 if (legchar
== ULONG_MAX
)
1560 unichar
= strtol(s
, NULL
, 16);
1561 if (unichar
< 0 || unichar
> 0x10ffff)
1566 DEBUG_printf(("9get_vbcs_charmap: i=%d, legchar=0x%08lx, unichar=0x%04x", i
,
1567 legchar
, (unsigned)unichar
));
1570 * Save lead char of 2/3/4-byte legacy char...
1573 if (legchar
> 0xffffff)
1575 leadchar
= (cups_sbcs_t
)(legchar
>> 24);
1576 vmap
->lead4char
[leadchar
] = leadchar
;
1578 else if (legchar
> 0xffff)
1580 leadchar
= (cups_sbcs_t
)(legchar
>> 16);
1581 vmap
->lead3char
[leadchar
] = leadchar
;
1585 leadchar
= (cups_sbcs_t
)(legchar
>> 8);
1586 vmap
->lead2char
[leadchar
] = leadchar
;
1590 * Save Legacy to Unicode mapping...
1593 if (legchar
<= 0xffff)
1596 * Save DBCS 16-bit to Unicode mapping in indirect lookup table...
1599 crow
= vmap
->char2uni
[(int)leadchar
];
1602 crow
= (cups_ucs2_t
*)calloc(256, sizeof(cups_ucs2_t
));
1606 vmap
->char2uni
[(int)leadchar
] = crow
;
1609 crow
[(int)(legchar
& 0xff)] = (cups_ucs2_t
)unichar
;
1614 * Save VBCS 32-bit to Unicode mapping in sorted list table...
1620 vmap
->widecount
= (mapcount
- i
+ 1);
1621 wide2uni
= (_cups_wide2uni_t
*)calloc(vmap
->widecount
,
1622 sizeof(_cups_wide2uni_t
));
1626 vmap
->wide2uni
= wide2uni
;
1629 wide2uni
->widechar
= (cups_vbcs_t
)legchar
;
1630 wide2uni
->unichar
= (cups_ucs2_t
)unichar
;
1635 * Save Unicode to legacy mapping in indirect lookup table...
1638 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
1641 vrow
= (cups_vbcs_t
*)calloc(256, sizeof(cups_vbcs_t
));
1645 vmap
->uni2char
[(int) ((unichar
>> 8) & 0xff)] = vrow
;
1648 vrow
+= (int)(unichar
& 0xff);
1651 * Convert Replacement Character to visible replacement...
1654 if (unichar
== 0xfffd)
1655 legchar
= (unsigned long)'?';
1658 * First (oldest) legacy character uses Unicode mapping cell...
1662 *vrow
= (cups_vbcs_t
)legchar
;
1665 vmap
->charcount
= (i
- vmap
->widecount
);
1670 * Add it to the cache and return...
1673 vmap
->next
= vmap_cache
;
1676 DEBUG_printf(("8get_vbcs_charmap: Returning new vmap=%p", vmap
));
1681 * If we get here, the file contains errors...
1686 free_vbcs_charmap(vmap
);
1690 DEBUG_puts("8get_vbcs_charmap: Returning NULL (Read/format error)");
1697 * End of "$Id: transcode.c 7560 2008-05-13 06:34:04Z mike $"