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)", (void *)dest
, src
, maxout
, encoding
));
95 if (!dest
|| !src
|| maxout
< 1)
100 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
105 * Handle identity conversions...
108 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
109 encoding
>= CUPS_ENCODING_VBCS_END
)
111 strlcpy((char *)dest
, src
, (size_t)maxout
);
112 return ((int)strlen((char *)dest
));
116 * Handle ISO-8859-1 to UTF-8 directly...
121 if (encoding
== CUPS_ISO8859_1
)
123 int ch
; /* Character from string */
124 cups_utf8_t
*destend
; /* End of UTF-8 buffer */
127 destend
= dest
+ maxout
- 2;
129 while (*src
&& destptr
< destend
)
135 *destptr
++ = (cups_utf8_t
)(0xc0 | (ch
>> 6));
136 *destptr
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
139 *destptr
++ = (cups_utf8_t
)ch
;
144 return ((int)(destptr
- dest
));
148 * Convert input legacy charset to UTF-8...
152 _cupsMutexLock(&map_mutex
);
154 if (map_encoding
!= encoding
)
156 char toset
[1024]; /* Destination character set */
160 snprintf(toset
, sizeof(toset
), "%s//IGNORE", _cupsEncodingName(encoding
));
162 map_encoding
= encoding
;
163 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
164 map_to_utf8
= iconv_open("UTF-8", toset
);
167 if (map_to_utf8
!= (iconv_t
)-1)
169 char *altdestptr
= (char *)dest
; /* Silence bogus GCC type-punned */
171 srclen
= strlen(src
);
172 outBytesLeft
= (size_t)maxout
- 1;
174 iconv(map_to_utf8
, (char **)&src
, &srclen
, &altdestptr
, &outBytesLeft
);
177 _cupsMutexUnlock(&map_mutex
);
179 return ((int)(altdestptr
- (char *)dest
));
182 _cupsMutexUnlock(&map_mutex
);
183 #endif /* HAVE_ICONV_H */
186 * No iconv() support, so error out...
196 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
199 int /* O - Count or -1 on error */
201 char *dest
, /* O - Target string */
202 const cups_utf8_t
*src
, /* I - Source string */
203 const int maxout
, /* I - Max output */
204 const cups_encoding_t encoding
) /* I - Encoding */
206 char *destptr
; /* Pointer into destination */
208 size_t srclen
, /* Length of source string */
209 outBytesLeft
; /* Bytes remaining in output buffer */
210 #endif /* HAVE_ICONV_H */
214 * Check for valid arguments...
217 if (!dest
|| !src
|| maxout
< 1)
226 * Handle identity conversions...
229 if (encoding
== CUPS_UTF8
||
230 encoding
>= CUPS_ENCODING_VBCS_END
)
232 strlcpy(dest
, (char *)src
, (size_t)maxout
);
233 return ((int)strlen(dest
));
237 * Handle UTF-8 to ISO-8859-1 directly...
242 if (encoding
== CUPS_ISO8859_1
|| encoding
<= CUPS_US_ASCII
)
244 int ch
, /* Character from string */
245 maxch
; /* Maximum character for charset */
246 char *destend
; /* End of ISO-8859-1 buffer */
248 maxch
= encoding
== CUPS_ISO8859_1
? 256 : 128;
249 destend
= dest
+ maxout
- 1;
251 while (*src
&& destptr
< destend
)
255 if ((ch
& 0xe0) == 0xc0)
257 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
260 *destptr
++ = (char)ch
;
264 else if ((ch
& 0xf0) == 0xe0 ||
267 else if (!(ch
& 0x80))
268 *destptr
++ = (char)ch
;
273 return ((int)(destptr
- dest
));
278 * Convert input UTF-8 to legacy charset...
281 _cupsMutexLock(&map_mutex
);
283 if (map_encoding
!= encoding
)
285 char toset
[1024]; /* Destination character set */
289 snprintf(toset
, sizeof(toset
), "%s//IGNORE", _cupsEncodingName(encoding
));
291 map_encoding
= encoding
;
292 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
293 map_to_utf8
= iconv_open("UTF-8", toset
);
296 if (map_from_utf8
!= (iconv_t
)-1)
298 char *altsrc
= (char *)src
; /* Silence bogus GCC type-punned */
300 srclen
= strlen((char *)src
);
301 outBytesLeft
= (size_t)maxout
- 1;
303 iconv(map_from_utf8
, &altsrc
, &srclen
, &destptr
, &outBytesLeft
);
306 _cupsMutexUnlock(&map_mutex
);
308 return ((int)(destptr
- dest
));
311 _cupsMutexUnlock(&map_mutex
);
312 #endif /* HAVE_ICONV_H */
315 * No iconv() support, so error out...
325 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
327 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
329 * UTF-32 char UTF-8 char(s)
330 * --------------------------------------------------
331 * 0 to 127 = 0xxxxxxx (US-ASCII)
332 * 128 to 2047 = 110xxxxx 10yyyyyy
333 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
334 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
336 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
337 * which would convert to five- or six-octet UTF-8 sequences...
340 int /* O - Count or -1 on error */
342 cups_utf32_t
*dest
, /* O - Target string */
343 const cups_utf8_t
*src
, /* I - Source string */
344 const int maxout
) /* I - Max output */
346 int i
; /* Looping variable */
347 cups_utf8_t ch
; /* Character value */
348 cups_utf8_t next
; /* Next character value */
349 cups_utf32_t ch32
; /* UTF-32 character value */
353 * Check for valid arguments and clear output...
356 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", (void *)dest
, src
, maxout
));
361 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
363 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
369 * Convert input UTF-8 to output UTF-32...
372 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
377 * Convert UTF-8 character(s) to UTF-32 character...
383 * One-octet UTF-8 <= 127 (US-ASCII)...
388 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src
[-1], ch
));
391 else if ((ch
& 0xe0) == 0xc0)
394 * Two-octet UTF-8 <= 2047 (Latin-x)...
398 if ((next
& 0xc0) != 0x80)
400 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
405 ch32
= (cups_utf32_t
)((ch
& 0x1f) << 6) | (cups_utf32_t
)(next
& 0x3f);
408 * Check for non-shortest form (invalid UTF-8)...
413 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
420 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
421 src
[-2], src
[-1], (unsigned)ch32
));
423 else if ((ch
& 0xf0) == 0xe0)
426 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
430 if ((next
& 0xc0) != 0x80)
432 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
437 ch32
= (cups_utf32_t
)((ch
& 0x0f) << 6) | (cups_utf32_t
)(next
& 0x3f);
440 if ((next
& 0xc0) != 0x80)
442 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
447 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
450 * Check for non-shortest form (invalid UTF-8)...
455 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
462 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
463 src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
465 else if ((ch
& 0xf8) == 0xf0)
468 * Four-octet UTF-8...
472 if ((next
& 0xc0) != 0x80)
474 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
479 ch32
= (cups_utf32_t
)((ch
& 0x07) << 6) | (cups_utf32_t
)(next
& 0x3f);
482 if ((next
& 0xc0) != 0x80)
484 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
489 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
492 if ((next
& 0xc0) != 0x80)
494 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
499 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
502 * Check for non-shortest form (invalid UTF-8)...
507 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
514 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
515 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
520 * More than 4-octet (invalid UTF-8 sequence)...
523 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
529 * Check for UTF-16 surrogate (illegal UTF-8)...
532 if (ch32
>= 0xd800 && ch32
<= 0xdfff)
538 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout
- 1 - i
));
540 return (maxout
- 1 - i
);
545 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
547 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
549 * UTF-32 char UTF-8 char(s)
550 * --------------------------------------------------
551 * 0 to 127 = 0xxxxxxx (US-ASCII)
552 * 128 to 2047 = 110xxxxx 10yyyyyy
553 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
554 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
556 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
557 * which would convert to five- or six-octet UTF-8 sequences...
560 int /* O - Count or -1 on error */
562 cups_utf8_t
*dest
, /* O - Target string */
563 const cups_utf32_t
*src
, /* I - Source string */
564 const int maxout
) /* I - Max output */
566 cups_utf8_t
*start
; /* Start of destination string */
567 int i
; /* Looping variable */
568 int swap
; /* Byte-swap input to output */
569 cups_utf32_t ch
; /* Character value */
573 * Check for valid arguments and clear output...
576 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", (void *)dest
, (void *)src
, maxout
));
581 if (!dest
|| !src
|| maxout
< 1)
583 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
589 * Check for leading BOM in UTF-32 and inverted BOM...
593 swap
= *src
== 0xfffe0000;
595 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap
));
597 if (*src
== 0xfffe0000 || *src
== 0xfeff)
601 * Convert input UTF-32 to output UTF-8...
604 for (i
= maxout
- 1; *src
&& i
> 0;)
609 * Byte swap input UTF-32, if necessary...
610 * (only byte-swapping 24 of 32 bits)
614 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
617 * Check for beyond Plane 16 (invalid UTF-32)...
622 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
628 * Convert UTF-32 character to UTF-8 character(s)...
634 * One-octet UTF-8 <= 127 (US-ASCII)...
637 *dest
++ = (cups_utf8_t
)ch
;
640 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch
, dest
[-1]));
645 * Two-octet UTF-8 <= 2047 (Latin-x)...
650 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
655 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
656 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
659 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch
,
660 dest
[-2], dest
[-1]));
662 else if (ch
< 0x10000)
665 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
670 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
675 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
676 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
677 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
680 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch
,
681 dest
[-3], dest
[-2], dest
[-1]));
686 * Four-octet UTF-8...
691 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
696 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
697 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
698 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
699 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
702 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
703 (unsigned)ch
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
709 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest
- start
)));
711 return ((int)(dest
- start
));