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
)
159 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
160 map_to_utf8
= iconv_open("UTF-8", _cupsEncodingName(encoding
));
161 map_encoding
= encoding
;
164 if (map_to_utf8
!= (iconv_t
)-1)
166 char *altdestptr
= (char *)dest
; /* Silence bogus GCC type-punned */
168 srclen
= strlen(src
);
169 outBytesLeft
= (size_t)maxout
- 1;
171 iconv(map_to_utf8
, (char **)&src
, &srclen
, &altdestptr
, &outBytesLeft
);
174 _cupsMutexUnlock(&map_mutex
);
176 return ((int)(altdestptr
- (char *)dest
));
179 _cupsMutexUnlock(&map_mutex
);
180 #endif /* HAVE_ICONV_H */
183 * No iconv() support, so error out...
193 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
196 int /* O - Count or -1 on error */
198 char *dest
, /* O - Target string */
199 const cups_utf8_t
*src
, /* I - Source string */
200 const int maxout
, /* I - Max output */
201 const cups_encoding_t encoding
) /* I - Encoding */
203 char *destptr
; /* Pointer into destination */
205 size_t srclen
, /* Length of source string */
206 outBytesLeft
; /* Bytes remaining in output buffer */
207 #endif /* HAVE_ICONV_H */
211 * Check for valid arguments...
214 if (!dest
|| !src
|| maxout
< 1)
223 * Handle identity conversions...
226 if (encoding
== CUPS_UTF8
||
227 encoding
>= CUPS_ENCODING_VBCS_END
)
229 strlcpy(dest
, (char *)src
, (size_t)maxout
);
230 return ((int)strlen(dest
));
234 * Handle UTF-8 to ISO-8859-1 directly...
239 if (encoding
== CUPS_ISO8859_1
|| encoding
<= CUPS_US_ASCII
)
241 int ch
, /* Character from string */
242 maxch
; /* Maximum character for charset */
243 char *destend
; /* End of ISO-8859-1 buffer */
245 maxch
= encoding
== CUPS_ISO8859_1
? 256 : 128;
246 destend
= dest
+ maxout
- 1;
248 while (*src
&& destptr
< destend
)
252 if ((ch
& 0xe0) == 0xc0)
254 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
257 *destptr
++ = (char)ch
;
261 else if ((ch
& 0xf0) == 0xe0 ||
264 else if (!(ch
& 0x80))
265 *destptr
++ = (char)ch
;
270 return ((int)(destptr
- dest
));
275 * Convert input UTF-8 to legacy charset...
278 _cupsMutexLock(&map_mutex
);
280 if (map_encoding
!= encoding
)
284 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
285 map_to_utf8
= iconv_open("UTF-8", _cupsEncodingName(encoding
));
286 map_encoding
= encoding
;
289 if (map_from_utf8
!= (iconv_t
)-1)
291 char *altsrc
= (char *)src
; /* Silence bogus GCC type-punned */
293 srclen
= strlen((char *)src
);
294 outBytesLeft
= (size_t)maxout
- 1;
296 iconv(map_from_utf8
, &altsrc
, &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
= (cups_utf32_t
)((ch
& 0x1f) << 6) | (cups_utf32_t
)(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
= (cups_utf32_t
)((ch
& 0x0f) << 6) | (cups_utf32_t
)(next
& 0x3f);
434 if ((next
& 0xc0) != 0x80)
436 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
441 ch32
= (ch32
<< 6) | (cups_utf32_t
)(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
= (cups_utf32_t
)((ch
& 0x07) << 6) | (cups_utf32_t
)(next
& 0x3f);
476 if ((next
& 0xc0) != 0x80)
478 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
483 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
486 if ((next
& 0xc0) != 0x80)
488 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
493 ch32
= (ch32
<< 6) | (cups_utf32_t
)(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
));