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
||
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
|| encoding
<= CUPS_US_ASCII
)
248 int ch
, /* Character from string */
249 maxch
; /* Maximum character for charset */
250 char *destend
; /* End of ISO-8859-1 buffer */
252 maxch
= encoding
== CUPS_ISO8859_1
? 256 : 128;
253 destend
= dest
+ maxout
- 1;
255 while (*src
&& destptr
< destend
)
259 if ((ch
& 0xe0) == 0xc0)
261 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
268 else if ((ch
& 0xf0) == 0xe0 ||
271 else if (!(ch
& 0x80))
277 return ((int)(destptr
- dest
));
282 * Convert input UTF-8 to legacy charset...
285 _cupsMutexLock(&map_mutex
);
287 if (map_encoding
!= encoding
)
291 map_from_utf8
= iconv_open(_cupsEncodingName(encoding
), "UTF-8");
292 map_to_utf8
= iconv_open("UTF-8", _cupsEncodingName(encoding
));
293 map_encoding
= encoding
;
296 if (map_from_utf8
!= (iconv_t
)-1)
298 srclen
= strlen((char *)src
);
299 outBytesLeft
= maxout
- 1;
301 iconv(map_from_utf8
, (char **)&src
, &srclen
, &destptr
, &outBytesLeft
);
304 _cupsMutexUnlock(&map_mutex
);
306 return ((int)(destptr
- dest
));
309 _cupsMutexUnlock(&map_mutex
);
310 #endif /* HAVE_ICONV_H */
313 * No iconv() support, so error out...
323 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
325 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
327 * UTF-32 char UTF-8 char(s)
328 * --------------------------------------------------
329 * 0 to 127 = 0xxxxxxx (US-ASCII)
330 * 128 to 2047 = 110xxxxx 10yyyyyy
331 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
332 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
334 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
335 * which would convert to five- or six-octet UTF-8 sequences...
338 int /* O - Count or -1 on error */
340 cups_utf32_t
*dest
, /* O - Target string */
341 const cups_utf8_t
*src
, /* I - Source string */
342 const int maxout
) /* I - Max output */
344 int i
; /* Looping variable */
345 cups_utf8_t ch
; /* Character value */
346 cups_utf8_t next
; /* Next character value */
347 cups_utf32_t ch32
; /* UTF-32 character value */
351 * Check for valid arguments and clear output...
354 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest
,
360 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
362 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
368 * Convert input UTF-8 to output UTF-32...
371 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
376 * Convert UTF-8 character(s) to UTF-32 character...
382 * One-octet UTF-8 <= 127 (US-ASCII)...
387 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src
[-1], ch
));
390 else if ((ch
& 0xe0) == 0xc0)
393 * Two-octet UTF-8 <= 2047 (Latin-x)...
397 if ((next
& 0xc0) != 0x80)
399 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
404 ch32
= ((ch
& 0x1f) << 6) | (next
& 0x3f);
407 * Check for non-shortest form (invalid UTF-8)...
412 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
419 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
420 src
[-2], src
[-1], (unsigned)ch32
));
422 else if ((ch
& 0xf0) == 0xe0)
425 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
429 if ((next
& 0xc0) != 0x80)
431 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
436 ch32
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
439 if ((next
& 0xc0) != 0x80)
441 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
446 ch32
= (ch32
<< 6) | (next
& 0x3f);
449 * Check for non-shortest form (invalid UTF-8)...
454 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
461 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
462 src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
464 else if ((ch
& 0xf8) == 0xf0)
467 * Four-octet UTF-8...
471 if ((next
& 0xc0) != 0x80)
473 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
478 ch32
= ((ch
& 0x07) << 6) | (next
& 0x3f);
481 if ((next
& 0xc0) != 0x80)
483 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
488 ch32
= (ch32
<< 6) | (next
& 0x3f);
491 if ((next
& 0xc0) != 0x80)
493 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
498 ch32
= (ch32
<< 6) | (next
& 0x3f);
501 * Check for non-shortest form (invalid UTF-8)...
506 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
513 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
514 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
519 * More than 4-octet (invalid UTF-8 sequence)...
522 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
528 * Check for UTF-16 surrogate (illegal UTF-8)...
531 if (ch32
>= 0xd800 && ch32
<= 0xdfff)
537 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout
- 1 - i
));
539 return (maxout
- 1 - i
);
544 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
546 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
548 * UTF-32 char UTF-8 char(s)
549 * --------------------------------------------------
550 * 0 to 127 = 0xxxxxxx (US-ASCII)
551 * 128 to 2047 = 110xxxxx 10yyyyyy
552 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
553 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
555 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
556 * which would convert to five- or six-octet UTF-8 sequences...
559 int /* O - Count or -1 on error */
561 cups_utf8_t
*dest
, /* O - Target string */
562 const cups_utf32_t
*src
, /* I - Source string */
563 const int maxout
) /* I - Max output */
565 cups_utf8_t
*start
; /* Start of destination string */
566 int i
; /* Looping variable */
567 int swap
; /* Byte-swap input to output */
568 cups_utf32_t ch
; /* Character value */
572 * Check for valid arguments and clear output...
575 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest
, src
,
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
));
716 * End of "$Id: transcode.c 9306 2010-09-16 21:43:57Z mike $"