4 * Transcoding support for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
22 #include "cups-private.h"
27 #endif /* HAVE_ICONV_H */
35 static _cups_mutex_t map_mutex
= _CUPS_MUTEX_INITIALIZER
;
36 /* Mutex to control access to maps */
37 static iconv_t map_from_utf8
= (iconv_t
)-1;
38 /* Convert from UTF-8 to charset */
39 static iconv_t map_to_utf8
= (iconv_t
)-1;
40 /* Convert from charset to UTF-8 */
41 static cups_encoding_t map_encoding
= CUPS_AUTO_ENCODING
;
42 /* Which charset is cached */
43 #endif /* HAVE_ICONV_H */
47 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
51 _cupsCharmapFlush(void)
54 if (map_from_utf8
!= (iconv_t
)-1)
56 iconv_close(map_from_utf8
);
57 map_from_utf8
= (iconv_t
)-1;
60 if (map_to_utf8
!= (iconv_t
)-1)
62 iconv_close(map_to_utf8
);
63 map_to_utf8
= (iconv_t
)-1;
66 map_encoding
= CUPS_AUTO_ENCODING
;
67 #endif /* HAVE_ICONV_H */
72 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
75 int /* O - Count or -1 on error */
77 cups_utf8_t
*dest
, /* O - Target string */
78 const char *src
, /* I - Source string */
79 const int maxout
, /* I - Max output */
80 const cups_encoding_t encoding
) /* I - Encoding */
82 cups_utf8_t
*destptr
; /* Pointer into UTF-8 buffer */
84 size_t srclen
, /* Length of source string */
85 outBytesLeft
; /* Bytes remaining in output buffer */
86 #endif /* HAVE_ICONV_H */
90 * Check for valid arguments...
93 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)",
94 dest
, src
, maxout
, encoding
));
96 if (!dest
|| !src
|| maxout
< 1)
101 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
106 * Handle identity conversions...
109 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
110 encoding
>= CUPS_ENCODING_VBCS_END
)
112 strlcpy((char *)dest
, src
, (size_t)maxout
);
113 return ((int)strlen((char *)dest
));
117 * Handle ISO-8859-1 to UTF-8 directly...
122 if (encoding
== CUPS_ISO8859_1
)
124 int ch
; /* Character from string */
125 cups_utf8_t
*destend
; /* End of UTF-8 buffer */
128 destend
= dest
+ maxout
- 2;
130 while (*src
&& destptr
< destend
)
136 *destptr
++ = (cups_utf8_t
)(0xc0 | (ch
>> 6));
137 *destptr
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
140 *destptr
++ = (cups_utf8_t
)ch
;
145 return ((int)(destptr
- dest
));
149 * Convert input legacy charset to UTF-8...
153 _cupsMutexLock(&map_mutex
);
155 if (map_encoding
!= encoding
)
157 char toset
[1024]; /* Destination character set */
161 snprintf(toset
, sizeof(toset
), "%s//IGNORE", _cupsEncodingName(encoding
));
163 map_encoding
= encoding
;
164 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
165 map_to_utf8
= iconv_open("UTF-8", toset
);
168 if (map_to_utf8
!= (iconv_t
)-1)
170 char *altdestptr
= (char *)dest
; /* Silence bogus GCC type-punned */
172 srclen
= strlen(src
);
173 outBytesLeft
= (size_t)maxout
- 1;
175 iconv(map_to_utf8
, (char **)&src
, &srclen
, &altdestptr
, &outBytesLeft
);
178 _cupsMutexUnlock(&map_mutex
);
180 return ((int)(altdestptr
- (char *)dest
));
183 _cupsMutexUnlock(&map_mutex
);
184 #endif /* HAVE_ICONV_H */
187 * No iconv() support, so error out...
197 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
200 int /* O - Count or -1 on error */
202 char *dest
, /* O - Target string */
203 const cups_utf8_t
*src
, /* I - Source string */
204 const int maxout
, /* I - Max output */
205 const cups_encoding_t encoding
) /* I - Encoding */
207 char *destptr
; /* Pointer into destination */
209 size_t srclen
, /* Length of source string */
210 outBytesLeft
; /* Bytes remaining in output buffer */
211 #endif /* HAVE_ICONV_H */
215 * Check for valid arguments...
218 if (!dest
|| !src
|| maxout
< 1)
227 * Handle identity conversions...
230 if (encoding
== CUPS_UTF8
||
231 encoding
>= CUPS_ENCODING_VBCS_END
)
233 strlcpy(dest
, (char *)src
, (size_t)maxout
);
234 return ((int)strlen(dest
));
238 * Handle UTF-8 to ISO-8859-1 directly...
243 if (encoding
== CUPS_ISO8859_1
|| encoding
<= CUPS_US_ASCII
)
245 int ch
, /* Character from string */
246 maxch
; /* Maximum character for charset */
247 char *destend
; /* End of ISO-8859-1 buffer */
249 maxch
= encoding
== CUPS_ISO8859_1
? 256 : 128;
250 destend
= dest
+ maxout
- 1;
252 while (*src
&& destptr
< destend
)
256 if ((ch
& 0xe0) == 0xc0)
258 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
261 *destptr
++ = (char)ch
;
265 else if ((ch
& 0xf0) == 0xe0 ||
268 else if (!(ch
& 0x80))
269 *destptr
++ = (char)ch
;
274 return ((int)(destptr
- dest
));
279 * Convert input UTF-8 to legacy charset...
282 _cupsMutexLock(&map_mutex
);
284 if (map_encoding
!= encoding
)
286 char toset
[1024]; /* Destination character set */
290 snprintf(toset
, sizeof(toset
), "%s//IGNORE", _cupsEncodingName(encoding
));
292 map_encoding
= encoding
;
293 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
294 map_to_utf8
= iconv_open("UTF-8", toset
);
297 if (map_from_utf8
!= (iconv_t
)-1)
299 char *altsrc
= (char *)src
; /* Silence bogus GCC type-punned */
301 srclen
= strlen((char *)src
);
302 outBytesLeft
= (size_t)maxout
- 1;
304 iconv(map_from_utf8
, &altsrc
, &srclen
, &destptr
, &outBytesLeft
);
307 _cupsMutexUnlock(&map_mutex
);
309 return ((int)(destptr
- dest
));
312 _cupsMutexUnlock(&map_mutex
);
313 #endif /* HAVE_ICONV_H */
316 * No iconv() support, so error out...
326 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
328 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
330 * UTF-32 char UTF-8 char(s)
331 * --------------------------------------------------
332 * 0 to 127 = 0xxxxxxx (US-ASCII)
333 * 128 to 2047 = 110xxxxx 10yyyyyy
334 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
335 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
337 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
338 * which would convert to five- or six-octet UTF-8 sequences...
341 int /* O - Count or -1 on error */
343 cups_utf32_t
*dest
, /* O - Target string */
344 const cups_utf8_t
*src
, /* I - Source string */
345 const int maxout
) /* I - Max output */
347 int i
; /* Looping variable */
348 cups_utf8_t ch
; /* Character value */
349 cups_utf8_t next
; /* Next character value */
350 cups_utf32_t ch32
; /* UTF-32 character value */
354 * Check for valid arguments and clear output...
357 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest
,
363 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
365 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
371 * Convert input UTF-8 to output UTF-32...
374 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
379 * Convert UTF-8 character(s) to UTF-32 character...
385 * One-octet UTF-8 <= 127 (US-ASCII)...
390 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src
[-1], ch
));
393 else if ((ch
& 0xe0) == 0xc0)
396 * Two-octet UTF-8 <= 2047 (Latin-x)...
400 if ((next
& 0xc0) != 0x80)
402 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
407 ch32
= (cups_utf32_t
)((ch
& 0x1f) << 6) | (cups_utf32_t
)(next
& 0x3f);
410 * Check for non-shortest form (invalid UTF-8)...
415 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
422 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
423 src
[-2], src
[-1], (unsigned)ch32
));
425 else if ((ch
& 0xf0) == 0xe0)
428 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
432 if ((next
& 0xc0) != 0x80)
434 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
439 ch32
= (cups_utf32_t
)((ch
& 0x0f) << 6) | (cups_utf32_t
)(next
& 0x3f);
442 if ((next
& 0xc0) != 0x80)
444 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
449 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
452 * Check for non-shortest form (invalid UTF-8)...
457 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
464 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
465 src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
467 else if ((ch
& 0xf8) == 0xf0)
470 * Four-octet UTF-8...
474 if ((next
& 0xc0) != 0x80)
476 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
481 ch32
= (cups_utf32_t
)((ch
& 0x07) << 6) | (cups_utf32_t
)(next
& 0x3f);
484 if ((next
& 0xc0) != 0x80)
486 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
491 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
494 if ((next
& 0xc0) != 0x80)
496 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
501 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
504 * Check for non-shortest form (invalid UTF-8)...
509 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
516 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
517 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
522 * More than 4-octet (invalid UTF-8 sequence)...
525 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
531 * Check for UTF-16 surrogate (illegal UTF-8)...
534 if (ch32
>= 0xd800 && ch32
<= 0xdfff)
540 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout
- 1 - i
));
542 return (maxout
- 1 - i
);
547 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
549 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
551 * UTF-32 char UTF-8 char(s)
552 * --------------------------------------------------
553 * 0 to 127 = 0xxxxxxx (US-ASCII)
554 * 128 to 2047 = 110xxxxx 10yyyyyy
555 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
556 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
558 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
559 * which would convert to five- or six-octet UTF-8 sequences...
562 int /* O - Count or -1 on error */
564 cups_utf8_t
*dest
, /* O - Target string */
565 const cups_utf32_t
*src
, /* I - Source string */
566 const int maxout
) /* I - Max output */
568 cups_utf8_t
*start
; /* Start of destination string */
569 int i
; /* Looping variable */
570 int swap
; /* Byte-swap input to output */
571 cups_utf32_t ch
; /* Character value */
575 * Check for valid arguments and clear output...
578 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest
, src
,
584 if (!dest
|| !src
|| maxout
< 1)
586 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
592 * Check for leading BOM in UTF-32 and inverted BOM...
596 swap
= *src
== 0xfffe0000;
598 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap
));
600 if (*src
== 0xfffe0000 || *src
== 0xfeff)
604 * Convert input UTF-32 to output UTF-8...
607 for (i
= maxout
- 1; *src
&& i
> 0;)
612 * Byte swap input UTF-32, if necessary...
613 * (only byte-swapping 24 of 32 bits)
617 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
620 * Check for beyond Plane 16 (invalid UTF-32)...
625 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
631 * Convert UTF-32 character to UTF-8 character(s)...
637 * One-octet UTF-8 <= 127 (US-ASCII)...
640 *dest
++ = (cups_utf8_t
)ch
;
643 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch
, dest
[-1]));
648 * Two-octet UTF-8 <= 2047 (Latin-x)...
653 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
658 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
659 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
662 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch
,
663 dest
[-2], dest
[-1]));
665 else if (ch
< 0x10000)
668 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
673 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
678 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
679 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
680 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
683 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch
,
684 dest
[-3], dest
[-2], dest
[-1]));
689 * Four-octet UTF-8...
694 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
699 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
700 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
701 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
702 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
705 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
706 (unsigned)ch
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
712 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest
- start
)));
714 return ((int)(dest
- start
));