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"
19 #endif /* HAVE_ICONV_H */
27 static _cups_mutex_t map_mutex
= _CUPS_MUTEX_INITIALIZER
;
28 /* Mutex to control access to maps */
29 static iconv_t map_from_utf8
= (iconv_t
)-1;
30 /* Convert from UTF-8 to charset */
31 static iconv_t map_to_utf8
= (iconv_t
)-1;
32 /* Convert from charset to UTF-8 */
33 static cups_encoding_t map_encoding
= CUPS_AUTO_ENCODING
;
34 /* Which charset is cached */
35 #endif /* HAVE_ICONV_H */
39 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
43 _cupsCharmapFlush(void)
46 if (map_from_utf8
!= (iconv_t
)-1)
48 iconv_close(map_from_utf8
);
49 map_from_utf8
= (iconv_t
)-1;
52 if (map_to_utf8
!= (iconv_t
)-1)
54 iconv_close(map_to_utf8
);
55 map_to_utf8
= (iconv_t
)-1;
58 map_encoding
= CUPS_AUTO_ENCODING
;
59 #endif /* HAVE_ICONV_H */
64 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
67 int /* O - Count or -1 on error */
69 cups_utf8_t
*dest
, /* O - Target string */
70 const char *src
, /* I - Source string */
71 const int maxout
, /* I - Max output */
72 const cups_encoding_t encoding
) /* I - Encoding */
74 cups_utf8_t
*destptr
; /* Pointer into UTF-8 buffer */
76 size_t srclen
, /* Length of source string */
77 outBytesLeft
; /* Bytes remaining in output buffer */
78 #endif /* HAVE_ICONV_H */
82 * Check for valid arguments...
85 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)", (void *)dest
, src
, maxout
, encoding
));
87 if (!dest
|| !src
|| maxout
< 1)
92 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
97 * Handle identity conversions...
100 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
101 encoding
>= CUPS_ENCODING_VBCS_END
)
103 strlcpy((char *)dest
, src
, (size_t)maxout
);
104 return ((int)strlen((char *)dest
));
108 * Handle ISO-8859-1 to UTF-8 directly...
113 if (encoding
== CUPS_ISO8859_1
)
115 int ch
; /* Character from string */
116 cups_utf8_t
*destend
; /* End of UTF-8 buffer */
119 destend
= dest
+ maxout
- 2;
121 while (*src
&& destptr
< destend
)
127 *destptr
++ = (cups_utf8_t
)(0xc0 | (ch
>> 6));
128 *destptr
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
131 *destptr
++ = (cups_utf8_t
)ch
;
136 return ((int)(destptr
- dest
));
140 * Convert input legacy charset to UTF-8...
144 _cupsMutexLock(&map_mutex
);
146 if (map_encoding
!= encoding
)
148 char toset
[1024]; /* Destination character set */
152 snprintf(toset
, sizeof(toset
), "%s//IGNORE", _cupsEncodingName(encoding
));
154 map_encoding
= encoding
;
155 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
156 map_to_utf8
= iconv_open("UTF-8", toset
);
159 if (map_to_utf8
!= (iconv_t
)-1)
161 char *altdestptr
= (char *)dest
; /* Silence bogus GCC type-punned */
163 srclen
= strlen(src
);
164 outBytesLeft
= (size_t)maxout
- 1;
166 iconv(map_to_utf8
, (char **)&src
, &srclen
, &altdestptr
, &outBytesLeft
);
169 _cupsMutexUnlock(&map_mutex
);
171 return ((int)(altdestptr
- (char *)dest
));
174 _cupsMutexUnlock(&map_mutex
);
175 #endif /* HAVE_ICONV_H */
178 * No iconv() support, so error out...
188 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
191 int /* O - Count or -1 on error */
193 char *dest
, /* O - Target string */
194 const cups_utf8_t
*src
, /* I - Source string */
195 const int maxout
, /* I - Max output */
196 const cups_encoding_t encoding
) /* I - Encoding */
198 char *destptr
; /* Pointer into destination */
200 size_t srclen
, /* Length of source string */
201 outBytesLeft
; /* Bytes remaining in output buffer */
202 #endif /* HAVE_ICONV_H */
206 * Check for valid arguments...
209 if (!dest
|| !src
|| maxout
< 1)
218 * Handle identity conversions...
221 if (encoding
== CUPS_UTF8
||
222 encoding
>= CUPS_ENCODING_VBCS_END
)
224 strlcpy(dest
, (char *)src
, (size_t)maxout
);
225 return ((int)strlen(dest
));
229 * Handle UTF-8 to ISO-8859-1 directly...
234 if (encoding
== CUPS_ISO8859_1
|| encoding
<= CUPS_US_ASCII
)
236 int ch
, /* Character from string */
237 maxch
; /* Maximum character for charset */
238 char *destend
; /* End of ISO-8859-1 buffer */
240 maxch
= encoding
== CUPS_ISO8859_1
? 256 : 128;
241 destend
= dest
+ maxout
- 1;
243 while (*src
&& destptr
< destend
)
247 if ((ch
& 0xe0) == 0xc0)
249 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
252 *destptr
++ = (char)ch
;
256 else if ((ch
& 0xf0) == 0xe0 ||
259 else if (!(ch
& 0x80))
260 *destptr
++ = (char)ch
;
265 return ((int)(destptr
- dest
));
270 * Convert input UTF-8 to legacy charset...
273 _cupsMutexLock(&map_mutex
);
275 if (map_encoding
!= encoding
)
277 char toset
[1024]; /* Destination character set */
281 snprintf(toset
, sizeof(toset
), "%s//IGNORE", _cupsEncodingName(encoding
));
283 map_encoding
= encoding
;
284 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
285 map_to_utf8
= iconv_open("UTF-8", toset
);
288 if (map_from_utf8
!= (iconv_t
)-1)
290 char *altsrc
= (char *)src
; /* Silence bogus GCC type-punned */
292 srclen
= strlen((char *)src
);
293 outBytesLeft
= (size_t)maxout
- 1;
295 iconv(map_from_utf8
, &altsrc
, &srclen
, &destptr
, &outBytesLeft
);
298 _cupsMutexUnlock(&map_mutex
);
300 return ((int)(destptr
- dest
));
303 _cupsMutexUnlock(&map_mutex
);
304 #endif /* HAVE_ICONV_H */
307 * No iconv() support, so error out...
317 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
319 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
321 * UTF-32 char UTF-8 char(s)
322 * --------------------------------------------------
323 * 0 to 127 = 0xxxxxxx (US-ASCII)
324 * 128 to 2047 = 110xxxxx 10yyyyyy
325 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
326 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
328 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
329 * which would convert to five- or six-octet UTF-8 sequences...
332 int /* O - Count or -1 on error */
334 cups_utf32_t
*dest
, /* O - Target string */
335 const cups_utf8_t
*src
, /* I - Source string */
336 const int maxout
) /* I - Max output */
338 int i
; /* Looping variable */
339 cups_utf8_t ch
; /* Character value */
340 cups_utf8_t next
; /* Next character value */
341 cups_utf32_t ch32
; /* UTF-32 character value */
345 * Check for valid arguments and clear output...
348 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", (void *)dest
, src
, maxout
));
353 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
355 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
361 * Convert input UTF-8 to output UTF-32...
364 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
369 * Convert UTF-8 character(s) to UTF-32 character...
375 * One-octet UTF-8 <= 127 (US-ASCII)...
380 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src
[-1], ch
));
383 else if ((ch
& 0xe0) == 0xc0)
386 * Two-octet UTF-8 <= 2047 (Latin-x)...
390 if ((next
& 0xc0) != 0x80)
392 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
397 ch32
= (cups_utf32_t
)((ch
& 0x1f) << 6) | (cups_utf32_t
)(next
& 0x3f);
400 * Check for non-shortest form (invalid UTF-8)...
405 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
412 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
413 src
[-2], src
[-1], (unsigned)ch32
));
415 else if ((ch
& 0xf0) == 0xe0)
418 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
422 if ((next
& 0xc0) != 0x80)
424 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
429 ch32
= (cups_utf32_t
)((ch
& 0x0f) << 6) | (cups_utf32_t
)(next
& 0x3f);
432 if ((next
& 0xc0) != 0x80)
434 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
439 ch32
= (ch32
<< 6) | (cups_utf32_t
)(next
& 0x3f);
442 * Check for non-shortest form (invalid UTF-8)...
447 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
454 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
455 src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
457 else if ((ch
& 0xf8) == 0xf0)
460 * Four-octet UTF-8...
464 if ((next
& 0xc0) != 0x80)
466 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
471 ch32
= (cups_utf32_t
)((ch
& 0x07) << 6) | (cups_utf32_t
)(next
& 0x3f);
474 if ((next
& 0xc0) != 0x80)
476 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
481 ch32
= (ch32
<< 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 * Check for non-shortest form (invalid UTF-8)...
499 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
506 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
507 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
512 * More than 4-octet (invalid UTF-8 sequence)...
515 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
521 * Check for UTF-16 surrogate (illegal UTF-8)...
524 if (ch32
>= 0xd800 && ch32
<= 0xdfff)
530 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout
- 1 - i
));
532 return (maxout
- 1 - i
);
537 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
539 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
541 * UTF-32 char UTF-8 char(s)
542 * --------------------------------------------------
543 * 0 to 127 = 0xxxxxxx (US-ASCII)
544 * 128 to 2047 = 110xxxxx 10yyyyyy
545 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
546 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
548 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
549 * which would convert to five- or six-octet UTF-8 sequences...
552 int /* O - Count or -1 on error */
554 cups_utf8_t
*dest
, /* O - Target string */
555 const cups_utf32_t
*src
, /* I - Source string */
556 const int maxout
) /* I - Max output */
558 cups_utf8_t
*start
; /* Start of destination string */
559 int i
; /* Looping variable */
560 int swap
; /* Byte-swap input to output */
561 cups_utf32_t ch
; /* Character value */
565 * Check for valid arguments and clear output...
568 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", (void *)dest
, (void *)src
, maxout
));
573 if (!dest
|| !src
|| maxout
< 1)
575 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
581 * Check for leading BOM in UTF-32 and inverted BOM...
585 swap
= *src
== 0xfffe0000;
587 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap
));
589 if (*src
== 0xfffe0000 || *src
== 0xfeff)
593 * Convert input UTF-32 to output UTF-8...
596 for (i
= maxout
- 1; *src
&& i
> 0;)
601 * Byte swap input UTF-32, if necessary...
602 * (only byte-swapping 24 of 32 bits)
606 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
609 * Check for beyond Plane 16 (invalid UTF-32)...
614 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
620 * Convert UTF-32 character to UTF-8 character(s)...
626 * One-octet UTF-8 <= 127 (US-ASCII)...
629 *dest
++ = (cups_utf8_t
)ch
;
632 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch
, dest
[-1]));
637 * Two-octet UTF-8 <= 2047 (Latin-x)...
642 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
647 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
648 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
651 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch
,
652 dest
[-2], dest
[-1]));
654 else if (ch
< 0x10000)
657 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
662 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
667 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
668 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
669 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
672 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch
,
673 dest
[-3], dest
[-2], dest
[-1]));
678 * Four-octet UTF-8...
683 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
688 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
689 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
690 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
691 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
694 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
695 (unsigned)ch
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
701 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest
- start
)));
703 return ((int)(dest
- start
));