2 * Transcoding support for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
25 #endif /* HAVE_ICONV_H */
33 static _cups_mutex_t map_mutex
= _CUPS_MUTEX_INITIALIZER
;
34 /* Mutex to control access to maps */
35 static iconv_t map_from_utf8
= (iconv_t
)-1;
36 /* Convert from UTF-8 to charset */
37 static iconv_t map_to_utf8
= (iconv_t
)-1;
38 /* Convert from charset to UTF-8 */
39 static cups_encoding_t map_encoding
= CUPS_AUTO_ENCODING
;
40 /* Which charset is cached */
41 #endif /* HAVE_ICONV_H */
45 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
49 _cupsCharmapFlush(void)
52 if (map_from_utf8
!= (iconv_t
)-1)
54 iconv_close(map_from_utf8
);
55 map_from_utf8
= (iconv_t
)-1;
58 if (map_to_utf8
!= (iconv_t
)-1)
60 iconv_close(map_to_utf8
);
61 map_to_utf8
= (iconv_t
)-1;
64 map_encoding
= CUPS_AUTO_ENCODING
;
65 #endif /* HAVE_ICONV_H */
70 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
73 int /* O - Count or -1 on error */
75 cups_utf8_t
*dest
, /* O - Target string */
76 const char *src
, /* I - Source string */
77 const int maxout
, /* I - Max output */
78 const cups_encoding_t encoding
) /* I - Encoding */
80 cups_utf8_t
*destptr
; /* Pointer into UTF-8 buffer */
82 size_t srclen
, /* Length of source string */
83 outBytesLeft
; /* Bytes remaining in output buffer */
84 #endif /* HAVE_ICONV_H */
88 * Check for valid arguments...
91 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)", (void *)dest
, src
, maxout
, encoding
));
93 if (!dest
|| !src
|| maxout
< 1)
98 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
103 * Handle identity conversions...
106 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
107 encoding
>= CUPS_ENCODING_VBCS_END
)
109 strlcpy((char *)dest
, src
, (size_t)maxout
);
110 return ((int)strlen((char *)dest
));
114 * Handle ISO-8859-1 to UTF-8 directly...
119 if (encoding
== CUPS_ISO8859_1
)
121 int ch
; /* Character from string */
122 cups_utf8_t
*destend
; /* End of UTF-8 buffer */
125 destend
= dest
+ maxout
- 2;
127 while (*src
&& destptr
< destend
)
133 *destptr
++ = (cups_utf8_t
)(0xc0 | (ch
>> 6));
134 *destptr
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
137 *destptr
++ = (cups_utf8_t
)ch
;
142 return ((int)(destptr
- dest
));
146 * Convert input legacy charset to UTF-8...
150 _cupsMutexLock(&map_mutex
);
152 if (map_encoding
!= encoding
)
154 char toset
[1024]; /* Destination character set */
158 snprintf(toset
, sizeof(toset
), "%s//IGNORE", _cupsEncodingName(encoding
));
160 map_encoding
= encoding
;
161 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
162 map_to_utf8
= iconv_open("UTF-8", toset
);
165 if (map_to_utf8
!= (iconv_t
)-1)
167 char *altdestptr
= (char *)dest
; /* Silence bogus GCC type-punned */
169 srclen
= strlen(src
);
170 outBytesLeft
= (size_t)maxout
- 1;
172 iconv(map_to_utf8
, (char **)&src
, &srclen
, &altdestptr
, &outBytesLeft
);
175 _cupsMutexUnlock(&map_mutex
);
177 return ((int)(altdestptr
- (char *)dest
));
180 _cupsMutexUnlock(&map_mutex
);
181 #endif /* HAVE_ICONV_H */
184 * No iconv() support, so error out...
194 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
197 int /* O - Count or -1 on error */
199 char *dest
, /* O - Target string */
200 const cups_utf8_t
*src
, /* I - Source string */
201 const int maxout
, /* I - Max output */
202 const cups_encoding_t encoding
) /* I - Encoding */
204 char *destptr
; /* Pointer into destination */
206 size_t srclen
, /* Length of source string */
207 outBytesLeft
; /* Bytes remaining in output buffer */
208 #endif /* HAVE_ICONV_H */
212 * Check for valid arguments...
215 if (!dest
|| !src
|| maxout
< 1)
224 * Handle identity conversions...
227 if (encoding
== CUPS_UTF8
||
228 encoding
>= CUPS_ENCODING_VBCS_END
)
230 strlcpy(dest
, (char *)src
, (size_t)maxout
);
231 return ((int)strlen(dest
));
235 * Handle UTF-8 to ISO-8859-1 directly...
240 if (encoding
== CUPS_ISO8859_1
|| encoding
<= CUPS_US_ASCII
)
242 int ch
, /* Character from string */
243 maxch
; /* Maximum character for charset */
244 char *destend
; /* End of ISO-8859-1 buffer */
246 maxch
= encoding
== CUPS_ISO8859_1
? 256 : 128;
247 destend
= dest
+ maxout
- 1;
249 while (*src
&& destptr
< destend
)
253 if ((ch
& 0xe0) == 0xc0)
255 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
258 *destptr
++ = (char)ch
;
262 else if ((ch
& 0xf0) == 0xe0 ||
265 else if (!(ch
& 0x80))
266 *destptr
++ = (char)ch
;
271 return ((int)(destptr
- dest
));
276 * Convert input UTF-8 to legacy charset...
279 _cupsMutexLock(&map_mutex
);
281 if (map_encoding
!= encoding
)
283 char toset
[1024]; /* Destination character set */
287 snprintf(toset
, sizeof(toset
), "%s//IGNORE", _cupsEncodingName(encoding
));
289 map_encoding
= encoding
;
290 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
291 map_to_utf8
= iconv_open("UTF-8", toset
);
294 if (map_from_utf8
!= (iconv_t
)-1)
296 char *altsrc
= (char *)src
; /* Silence bogus GCC type-punned */
298 srclen
= strlen((char *)src
);
299 outBytesLeft
= (size_t)maxout
- 1;
301 iconv(map_from_utf8
, &altsrc
, &srclen
, &destptr
, &outBytesLeft
);
304 _cupsMutexUnlock(&map_mutex
);
306 return ((int)(destptr
- dest
));
309 _cupsMutexUnlock(&map_mutex
);
310 #endif /* HAVE_ICONV_H */
313 * No iconv() support, so error out...
323 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
325 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
327 * UTF-32 char UTF-8 char(s)
328 * --------------------------------------------------
329 * 0 to 127 = 0xxxxxxx (US-ASCII)
330 * 128 to 2047 = 110xxxxx 10yyyyyy
331 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
332 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
334 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
335 * which would convert to five- or six-octet UTF-8 sequences...
338 int /* O - Count or -1 on error */
340 cups_utf32_t
*dest
, /* O - Target string */
341 const cups_utf8_t
*src
, /* I - Source string */
342 const int maxout
) /* I - Max output */
344 int i
; /* Looping variable */
345 cups_utf8_t ch
; /* Character value */
346 cups_utf8_t next
; /* Next character value */
347 cups_utf32_t ch32
; /* UTF-32 character value */
351 * Check for valid arguments and clear output...
354 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", (void *)dest
, src
, maxout
));
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
= (cups_utf32_t
)((ch
& 0x1f) << 6) | (cups_utf32_t
)(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
= (cups_utf32_t
)((ch
& 0x0f) << 6) | (cups_utf32_t
)(next
& 0x3f);
438 if ((next
& 0xc0) != 0x80)
440 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
445 ch32
= (ch32
<< 6) | (cups_utf32_t
)(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
= (cups_utf32_t
)((ch
& 0x07) << 6) | (cups_utf32_t
)(next
& 0x3f);
480 if ((next
& 0xc0) != 0x80)
482 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
487 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
490 if ((next
& 0xc0) != 0x80)
492 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
497 ch32
= (ch32
<< 6) | (cups_utf32_t
)(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)", (void *)dest
, (void *)src
, maxout
));
579 if (!dest
|| !src
|| maxout
< 1)
581 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
587 * Check for leading BOM in UTF-32 and inverted BOM...
591 swap
= *src
== 0xfffe0000;
593 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap
));
595 if (*src
== 0xfffe0000 || *src
== 0xfeff)
599 * Convert input UTF-32 to output UTF-8...
602 for (i
= maxout
- 1; *src
&& i
> 0;)
607 * Byte swap input UTF-32, if necessary...
608 * (only byte-swapping 24 of 32 bits)
612 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
615 * Check for beyond Plane 16 (invalid UTF-32)...
620 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
626 * Convert UTF-32 character to UTF-8 character(s)...
632 * One-octet UTF-8 <= 127 (US-ASCII)...
635 *dest
++ = (cups_utf8_t
)ch
;
638 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch
, dest
[-1]));
643 * Two-octet UTF-8 <= 2047 (Latin-x)...
648 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
653 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
654 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
657 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch
,
658 dest
[-2], dest
[-1]));
660 else if (ch
< 0x10000)
663 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
668 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
673 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
674 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
675 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
678 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch
,
679 dest
[-3], dest
[-2], dest
[-1]));
684 * Four-octet UTF-8...
689 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
694 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
695 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
696 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
697 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
700 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
701 (unsigned)ch
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
707 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest
- start
)));
709 return ((int)(dest
- start
));