2 * Transcoding support for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
15 #include "debug-internal.h"
20 #endif /* HAVE_ICONV_H */
28 static _cups_mutex_t map_mutex
= _CUPS_MUTEX_INITIALIZER
;
29 /* Mutex to control access to maps */
30 static iconv_t map_from_utf8
= (iconv_t
)-1;
31 /* Convert from UTF-8 to charset */
32 static iconv_t map_to_utf8
= (iconv_t
)-1;
33 /* Convert from charset to UTF-8 */
34 static cups_encoding_t map_encoding
= CUPS_AUTO_ENCODING
;
35 /* Which charset is cached */
36 #endif /* HAVE_ICONV_H */
40 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
44 _cupsCharmapFlush(void)
47 if (map_from_utf8
!= (iconv_t
)-1)
49 iconv_close(map_from_utf8
);
50 map_from_utf8
= (iconv_t
)-1;
53 if (map_to_utf8
!= (iconv_t
)-1)
55 iconv_close(map_to_utf8
);
56 map_to_utf8
= (iconv_t
)-1;
59 map_encoding
= CUPS_AUTO_ENCODING
;
60 #endif /* HAVE_ICONV_H */
65 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
68 int /* O - Count or -1 on error */
70 cups_utf8_t
*dest
, /* O - Target string */
71 const char *src
, /* I - Source string */
72 const int maxout
, /* I - Max output */
73 const cups_encoding_t encoding
) /* I - Encoding */
75 cups_utf8_t
*destptr
; /* Pointer into UTF-8 buffer */
77 size_t srclen
, /* Length of source string */
78 outBytesLeft
; /* Bytes remaining in output buffer */
79 #endif /* HAVE_ICONV_H */
83 * Check for valid arguments...
86 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)", (void *)dest
, src
, maxout
, encoding
));
88 if (!dest
|| !src
|| maxout
< 1)
93 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
98 * Handle identity conversions...
101 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
102 encoding
>= CUPS_ENCODING_VBCS_END
)
104 strlcpy((char *)dest
, src
, (size_t)maxout
);
105 return ((int)strlen((char *)dest
));
109 * Handle ISO-8859-1 to UTF-8 directly...
114 if (encoding
== CUPS_ISO8859_1
)
116 int ch
; /* Character from string */
117 cups_utf8_t
*destend
; /* End of UTF-8 buffer */
120 destend
= dest
+ maxout
- 2;
122 while (*src
&& destptr
< destend
)
128 *destptr
++ = (cups_utf8_t
)(0xc0 | (ch
>> 6));
129 *destptr
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
132 *destptr
++ = (cups_utf8_t
)ch
;
137 return ((int)(destptr
- dest
));
141 * Convert input legacy charset to UTF-8...
145 _cupsMutexLock(&map_mutex
);
147 if (map_encoding
!= encoding
)
149 char toset
[1024]; /* Destination character set */
153 snprintf(toset
, sizeof(toset
), "%s//IGNORE", _cupsEncodingName(encoding
));
155 map_encoding
= encoding
;
156 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
157 map_to_utf8
= iconv_open("UTF-8", toset
);
160 if (map_to_utf8
!= (iconv_t
)-1)
162 char *altdestptr
= (char *)dest
; /* Silence bogus GCC type-punned */
164 srclen
= strlen(src
);
165 outBytesLeft
= (size_t)maxout
- 1;
167 iconv(map_to_utf8
, (char **)&src
, &srclen
, &altdestptr
, &outBytesLeft
);
170 _cupsMutexUnlock(&map_mutex
);
172 return ((int)(altdestptr
- (char *)dest
));
175 _cupsMutexUnlock(&map_mutex
);
176 #endif /* HAVE_ICONV_H */
179 * No iconv() support, so error out...
189 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
192 int /* O - Count or -1 on error */
194 char *dest
, /* O - Target string */
195 const cups_utf8_t
*src
, /* I - Source string */
196 const int maxout
, /* I - Max output */
197 const cups_encoding_t encoding
) /* I - Encoding */
199 char *destptr
; /* Pointer into destination */
201 size_t srclen
, /* Length of source string */
202 outBytesLeft
; /* Bytes remaining in output buffer */
203 #endif /* HAVE_ICONV_H */
207 * Check for valid arguments...
210 if (!dest
|| !src
|| maxout
< 1)
219 * Handle identity conversions...
222 if (encoding
== CUPS_UTF8
||
223 encoding
>= CUPS_ENCODING_VBCS_END
)
225 strlcpy(dest
, (char *)src
, (size_t)maxout
);
226 return ((int)strlen(dest
));
230 * Handle UTF-8 to ISO-8859-1 directly...
235 if (encoding
== CUPS_ISO8859_1
|| encoding
<= CUPS_US_ASCII
)
237 int ch
, /* Character from string */
238 maxch
; /* Maximum character for charset */
239 char *destend
; /* End of ISO-8859-1 buffer */
241 maxch
= encoding
== CUPS_ISO8859_1
? 256 : 128;
242 destend
= dest
+ maxout
- 1;
244 while (*src
&& destptr
< destend
)
248 if ((ch
& 0xe0) == 0xc0)
250 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
253 *destptr
++ = (char)ch
;
257 else if ((ch
& 0xf0) == 0xe0 ||
260 else if (!(ch
& 0x80))
261 *destptr
++ = (char)ch
;
266 return ((int)(destptr
- dest
));
271 * Convert input UTF-8 to legacy charset...
274 _cupsMutexLock(&map_mutex
);
276 if (map_encoding
!= encoding
)
278 char toset
[1024]; /* Destination character set */
282 snprintf(toset
, sizeof(toset
), "%s//IGNORE", _cupsEncodingName(encoding
));
284 map_encoding
= encoding
;
285 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
286 map_to_utf8
= iconv_open("UTF-8", toset
);
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)", (void *)dest
, src
, maxout
));
354 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
356 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
362 * Convert input UTF-8 to output UTF-32...
365 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
370 * Convert UTF-8 character(s) to UTF-32 character...
376 * One-octet UTF-8 <= 127 (US-ASCII)...
381 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src
[-1], ch
));
384 else if ((ch
& 0xe0) == 0xc0)
387 * Two-octet UTF-8 <= 2047 (Latin-x)...
391 if ((next
& 0xc0) != 0x80)
393 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
398 ch32
= (cups_utf32_t
)((ch
& 0x1f) << 6) | (cups_utf32_t
)(next
& 0x3f);
401 * Check for non-shortest form (invalid UTF-8)...
406 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
413 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
414 src
[-2], src
[-1], (unsigned)ch32
));
416 else if ((ch
& 0xf0) == 0xe0)
419 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
423 if ((next
& 0xc0) != 0x80)
425 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
430 ch32
= (cups_utf32_t
)((ch
& 0x0f) << 6) | (cups_utf32_t
)(next
& 0x3f);
433 if ((next
& 0xc0) != 0x80)
435 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
440 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
443 * Check for non-shortest form (invalid UTF-8)...
448 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
455 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
456 src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
458 else if ((ch
& 0xf8) == 0xf0)
461 * Four-octet UTF-8...
465 if ((next
& 0xc0) != 0x80)
467 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
472 ch32
= (cups_utf32_t
)((ch
& 0x07) << 6) | (cups_utf32_t
)(next
& 0x3f);
475 if ((next
& 0xc0) != 0x80)
477 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
482 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
485 if ((next
& 0xc0) != 0x80)
487 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
492 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
495 * Check for non-shortest form (invalid UTF-8)...
500 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
507 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
508 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
513 * More than 4-octet (invalid UTF-8 sequence)...
516 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
522 * Check for UTF-16 surrogate (illegal UTF-8)...
525 if (ch32
>= 0xd800 && ch32
<= 0xdfff)
531 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout
- 1 - i
));
533 return (maxout
- 1 - i
);
538 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
540 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
542 * UTF-32 char UTF-8 char(s)
543 * --------------------------------------------------
544 * 0 to 127 = 0xxxxxxx (US-ASCII)
545 * 128 to 2047 = 110xxxxx 10yyyyyy
546 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
547 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
549 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
550 * which would convert to five- or six-octet UTF-8 sequences...
553 int /* O - Count or -1 on error */
555 cups_utf8_t
*dest
, /* O - Target string */
556 const cups_utf32_t
*src
, /* I - Source string */
557 const int maxout
) /* I - Max output */
559 cups_utf8_t
*start
; /* Start of destination string */
560 int i
; /* Looping variable */
561 int swap
; /* Byte-swap input to output */
562 cups_utf32_t ch
; /* Character value */
566 * Check for valid arguments and clear output...
569 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", (void *)dest
, (void *)src
, maxout
));
574 if (!dest
|| !src
|| maxout
< 1)
576 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
582 * Check for leading BOM in UTF-32 and inverted BOM...
586 swap
= *src
== 0xfffe0000;
588 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap
));
590 if (*src
== 0xfffe0000 || *src
== 0xfeff)
594 * Convert input UTF-32 to output UTF-8...
597 for (i
= maxout
- 1; *src
&& i
> 0;)
602 * Byte swap input UTF-32, if necessary...
603 * (only byte-swapping 24 of 32 bits)
607 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
610 * Check for beyond Plane 16 (invalid UTF-32)...
615 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
621 * Convert UTF-32 character to UTF-8 character(s)...
627 * One-octet UTF-8 <= 127 (US-ASCII)...
630 *dest
++ = (cups_utf8_t
)ch
;
633 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch
, dest
[-1]));
638 * Two-octet UTF-8 <= 2047 (Latin-x)...
643 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
648 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
649 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
652 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch
,
653 dest
[-2], dest
[-1]));
655 else if (ch
< 0x10000)
658 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
663 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
668 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
669 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
670 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
673 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch
,
674 dest
[-3], dest
[-2], dest
[-1]));
679 * Four-octet UTF-8...
684 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
689 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
690 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
691 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
692 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
695 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
696 (unsigned)ch
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
702 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest
- start
)));
704 return ((int)(dest
- start
));