2 * "$Id: transcode.c 9306 2010-09-16 21:43:57Z mike $"
4 * Transcoding support for CUPS.
6 * Copyright 2007-2010 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 * _cupsCharmapFlush() - Flush all character set maps out of cache.
20 * cupsCharsetToUTF8() - Convert legacy character set to UTF-8.
21 * cupsUTF8ToCharset() - Convert UTF-8 to legacy character set.
22 * cupsUTF8ToUTF32() - Convert UTF-8 to UTF-32.
23 * cupsUTF32ToUTF8() - Convert UTF-32 to UTF-8.
27 * Include necessary headers...
30 #include "cups-private.h"
35 #endif /* HAVE_ICONV_H */
43 static _cups_mutex_t map_mutex
= _CUPS_MUTEX_INITIALIZER
;
44 /* Mutex to control access to maps */
45 static iconv_t map_from_utf8
= (iconv_t
)-1;
46 /* Convert from UTF-8 to charset */
47 static iconv_t map_to_utf8
= (iconv_t
)-1;
48 /* Convert from charset to UTF-8 */
49 static cups_encoding_t map_encoding
= CUPS_AUTO_ENCODING
;
50 /* Which charset is cached */
51 #endif /* HAVE_ICONV_H */
55 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
59 _cupsCharmapFlush(void)
62 if (map_from_utf8
!= (iconv_t
)-1)
64 iconv_close(map_from_utf8
);
65 map_from_utf8
= (iconv_t
)-1;
68 if (map_to_utf8
!= (iconv_t
)-1)
70 iconv_close(map_to_utf8
);
71 map_to_utf8
= (iconv_t
)-1;
74 map_encoding
= CUPS_AUTO_ENCODING
;
75 #endif /* HAVE_ICONV_H */
80 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
83 int /* O - Count or -1 on error */
85 cups_utf8_t
*dest
, /* O - Target string */
86 const char *src
, /* I - Source string */
87 const int maxout
, /* I - Max output */
88 const cups_encoding_t encoding
) /* I - Encoding */
90 cups_utf8_t
*destptr
; /* Pointer into UTF-8 buffer */
92 size_t srclen
, /* Length of source string */
93 outBytesLeft
; /* Bytes remaining in output buffer */
94 #endif /* HAVE_ICONV_H */
98 * Check for valid arguments...
101 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)",
102 dest
, src
, maxout
, encoding
));
104 if (!dest
|| !src
|| maxout
< 1)
109 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
114 * Handle identity conversions...
117 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
118 encoding
>= CUPS_ENCODING_VBCS_END
)
120 strlcpy((char *)dest
, src
, maxout
);
121 return ((int)strlen((char *)dest
));
125 * Handle ISO-8859-1 to UTF-8 directly...
130 if (encoding
== CUPS_ISO8859_1
)
132 int ch
; /* Character from string */
133 cups_utf8_t
*destend
; /* End of UTF-8 buffer */
136 destend
= dest
+ maxout
- 2;
138 while (*src
&& destptr
< destend
)
144 *destptr
++ = 0xc0 | (ch
>> 6);
145 *destptr
++ = 0x80 | (ch
& 0x3f);
153 return ((int)(destptr
- dest
));
157 * Convert input legacy charset to UTF-8...
161 _cupsMutexLock(&map_mutex
);
163 if (map_encoding
!= encoding
)
167 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
168 map_to_utf8
= iconv_open("UTF-8", _cupsEncodingName(encoding
));
169 map_encoding
= encoding
;
172 if (map_to_utf8
!= (iconv_t
)-1)
174 srclen
= strlen(src
);
175 outBytesLeft
= maxout
- 1;
177 iconv(map_to_utf8
, (char **)&src
, &srclen
, (char **)&destptr
,
181 _cupsMutexUnlock(&map_mutex
);
183 return ((int)(destptr
- dest
));
186 _cupsMutexUnlock(&map_mutex
);
187 #endif /* HAVE_ICONV_H */
190 * No iconv() support, so error out...
200 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
203 int /* O - Count or -1 on error */
205 char *dest
, /* O - Target string */
206 const cups_utf8_t
*src
, /* I - Source string */
207 const int maxout
, /* I - Max output */
208 const cups_encoding_t encoding
) /* I - Encoding */
210 char *destptr
; /* Pointer into destination */
212 size_t srclen
, /* Length of source string */
213 outBytesLeft
; /* Bytes remaining in output buffer */
214 #endif /* HAVE_ICONV_H */
218 * Check for valid arguments...
221 if (!dest
|| !src
|| maxout
< 1)
230 * Handle identity conversions...
233 if (encoding
== CUPS_UTF8
|| encoding
<= CUPS_US_ASCII
||
234 encoding
>= CUPS_ENCODING_VBCS_END
)
236 strlcpy(dest
, (char *)src
, maxout
);
237 return ((int)strlen(dest
));
241 * Handle UTF-8 to ISO-8859-1 directly...
246 if (encoding
== CUPS_ISO8859_1
)
248 int ch
; /* Character from string */
249 char *destend
; /* End of ISO-8859-1 buffer */
252 destend
= dest
+ maxout
- 1;
254 while (*src
&& destptr
< destend
)
258 if ((ch
& 0xe0) == 0xc0)
260 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
267 else if ((ch
& 0xf0) == 0xe0 ||
270 else if (!(ch
& 0x80))
276 return ((int)(destptr
- dest
));
281 * Convert input UTF-8 to legacy charset...
284 _cupsMutexLock(&map_mutex
);
286 if (map_encoding
!= encoding
)
290 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
291 map_to_utf8
= iconv_open("UTF-8", _cupsEncodingName(encoding
));
292 map_encoding
= encoding
;
295 if (map_from_utf8
!= (iconv_t
)-1)
297 srclen
= strlen((char *)src
);
298 outBytesLeft
= maxout
- 1;
300 iconv(map_from_utf8
, (char **)&src
, &srclen
, &destptr
, &outBytesLeft
);
303 _cupsMutexUnlock(&map_mutex
);
305 return ((int)(destptr
- dest
));
308 _cupsMutexUnlock(&map_mutex
);
309 #endif /* HAVE_ICONV_H */
312 * No iconv() support, so error out...
322 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
324 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
326 * UTF-32 char UTF-8 char(s)
327 * --------------------------------------------------
328 * 0 to 127 = 0xxxxxxx (US-ASCII)
329 * 128 to 2047 = 110xxxxx 10yyyyyy
330 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
331 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
333 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
334 * which would convert to five- or six-octet UTF-8 sequences...
337 int /* O - Count or -1 on error */
339 cups_utf32_t
*dest
, /* O - Target string */
340 const cups_utf8_t
*src
, /* I - Source string */
341 const int maxout
) /* I - Max output */
343 int i
; /* Looping variable */
344 cups_utf8_t ch
; /* Character value */
345 cups_utf8_t next
; /* Next character value */
346 cups_utf32_t ch32
; /* UTF-32 character value */
350 * Check for valid arguments and clear output...
353 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest
,
359 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
361 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
367 * Convert input UTF-8 to output UTF-32...
370 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
375 * Convert UTF-8 character(s) to UTF-32 character...
381 * One-octet UTF-8 <= 127 (US-ASCII)...
386 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src
[-1], ch
));
389 else if ((ch
& 0xe0) == 0xc0)
392 * Two-octet UTF-8 <= 2047 (Latin-x)...
396 if ((next
& 0xc0) != 0x80)
398 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
403 ch32
= ((ch
& 0x1f) << 6) | (next
& 0x3f);
406 * Check for non-shortest form (invalid UTF-8)...
411 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
418 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
419 src
[-2], src
[-1], (unsigned)ch32
));
421 else if ((ch
& 0xf0) == 0xe0)
424 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
428 if ((next
& 0xc0) != 0x80)
430 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
435 ch32
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
438 if ((next
& 0xc0) != 0x80)
440 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
445 ch32
= (ch32
<< 6) | (next
& 0x3f);
448 * Check for non-shortest form (invalid UTF-8)...
453 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
460 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
461 src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
463 else if ((ch
& 0xf8) == 0xf0)
466 * Four-octet UTF-8...
470 if ((next
& 0xc0) != 0x80)
472 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
477 ch32
= ((ch
& 0x07) << 6) | (next
& 0x3f);
480 if ((next
& 0xc0) != 0x80)
482 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
487 ch32
= (ch32
<< 6) | (next
& 0x3f);
490 if ((next
& 0xc0) != 0x80)
492 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
497 ch32
= (ch32
<< 6) | (next
& 0x3f);
500 * Check for non-shortest form (invalid UTF-8)...
505 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
512 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
513 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
518 * More than 4-octet (invalid UTF-8 sequence)...
521 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
527 * Check for UTF-16 surrogate (illegal UTF-8)...
530 if (ch32
>= 0xd800 && ch32
<= 0xdfff)
536 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout
- 1 - i
));
538 return (maxout
- 1 - i
);
543 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
545 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
547 * UTF-32 char UTF-8 char(s)
548 * --------------------------------------------------
549 * 0 to 127 = 0xxxxxxx (US-ASCII)
550 * 128 to 2047 = 110xxxxx 10yyyyyy
551 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
552 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
554 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
555 * which would convert to five- or six-octet UTF-8 sequences...
558 int /* O - Count or -1 on error */
560 cups_utf8_t
*dest
, /* O - Target string */
561 const cups_utf32_t
*src
, /* I - Source string */
562 const int maxout
) /* I - Max output */
564 cups_utf8_t
*start
; /* Start of destination string */
565 int i
; /* Looping variable */
566 int swap
; /* Byte-swap input to output */
567 cups_utf32_t ch
; /* Character value */
571 * Check for valid arguments and clear output...
574 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest
, src
,
580 if (!dest
|| !src
|| maxout
< 1)
582 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
588 * Check for leading BOM in UTF-32 and inverted BOM...
592 swap
= *src
== 0xfffe0000;
594 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap
));
596 if (*src
== 0xfffe0000 || *src
== 0xfeff)
600 * Convert input UTF-32 to output UTF-8...
603 for (i
= maxout
- 1; *src
&& i
> 0;)
608 * Byte swap input UTF-32, if necessary...
609 * (only byte-swapping 24 of 32 bits)
613 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
616 * Check for beyond Plane 16 (invalid UTF-32)...
621 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
627 * Convert UTF-32 character to UTF-8 character(s)...
633 * One-octet UTF-8 <= 127 (US-ASCII)...
636 *dest
++ = (cups_utf8_t
)ch
;
639 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch
, dest
[-1]));
644 * Two-octet UTF-8 <= 2047 (Latin-x)...
649 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
654 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
655 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
658 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch
,
659 dest
[-2], dest
[-1]));
661 else if (ch
< 0x10000)
664 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
669 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
674 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
675 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
676 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
679 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch
,
680 dest
[-3], dest
[-2], dest
[-1]));
685 * Four-octet UTF-8...
690 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
695 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
696 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
697 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
698 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
701 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
702 (unsigned)ch
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
708 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest
- start
)));
710 return ((int)(dest
- start
));
715 * End of "$Id: transcode.c 9306 2010-09-16 21:43:57Z mike $"