2 * "$Id: transcode.c 9820 2011-06-10 22:06:26Z 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 char *altdestptr = (char *)dest; /* Silence bogus GCC type-punned */
176 srclen = strlen(src);
177 outBytesLeft = maxout - 1;
179 iconv(map_to_utf8, (char **)&src, &srclen, &altdestptr, &outBytesLeft);
182 _cupsMutexUnlock(&map_mutex);
184 return ((int)(altdestptr - (char *)dest));
187 _cupsMutexUnlock(&map_mutex);
188 #endif /* HAVE_ICONV_H */
191 * No iconv() support, so error out...
201 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
204 int /* O - Count or -1 on error */
206 char *dest, /* O - Target string */
207 const cups_utf8_t *src, /* I - Source string */
208 const int maxout, /* I - Max output */
209 const cups_encoding_t encoding) /* I - Encoding */
211 char *destptr; /* Pointer into destination */
213 size_t srclen, /* Length of source string */
214 outBytesLeft; /* Bytes remaining in output buffer */
215 #endif /* HAVE_ICONV_H */
219 * Check for valid arguments...
222 if (!dest || !src || maxout < 1)
231 * Handle identity conversions...
234 if (encoding == CUPS_UTF8 ||
235 encoding >= CUPS_ENCODING_VBCS_END)
237 strlcpy(dest, (char *)src, maxout);
238 return ((int)strlen(dest));
242 * Handle UTF-8 to ISO-8859-1 directly...
247 if (encoding == CUPS_ISO8859_1 || encoding <= CUPS_US_ASCII)
249 int ch, /* Character from string */
250 maxch; /* Maximum character for charset */
251 char *destend; /* End of ISO-8859-1 buffer */
253 maxch = encoding == CUPS_ISO8859_1 ? 256 : 128;
254 destend = dest + maxout - 1;
256 while (*src && destptr < destend)
260 if ((ch & 0xe0) == 0xc0)
262 ch = ((ch & 0x1f) << 6) | (*src++ & 0x3f);
269 else if ((ch & 0xf0) == 0xe0 ||
272 else if (!(ch & 0x80))
278 return ((int)(destptr - dest));
283 * Convert input UTF-8 to legacy charset...
286 _cupsMutexLock(&map_mutex);
288 if (map_encoding != encoding)
292 map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8");
293 map_to_utf8 = iconv_open("UTF-8", _cupsEncodingName(encoding));
294 map_encoding = encoding;
297 if (map_from_utf8 != (iconv_t)-1)
299 char *altsrc = (char *)src; /* Silence bogus GCC type-punned */
301 srclen = strlen((char *)src);
302 outBytesLeft = maxout - 1;
304 iconv(map_from_utf8, &altsrc, &srclen, &destptr, &outBytesLeft);
307 _cupsMutexUnlock(&map_mutex);
309 return ((int)(destptr - dest));
312 _cupsMutexUnlock(&map_mutex);
313 #endif /* HAVE_ICONV_H */
316 * No iconv() support, so error out...
326 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
328 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
330 * UTF-32 char UTF-8 char(s)
331 * --------------------------------------------------
332 * 0 to 127 = 0xxxxxxx (US-ASCII)
333 * 128 to 2047 = 110xxxxx 10yyyyyy
334 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
335 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
337 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
338 * which would convert to five- or six-octet UTF-8 sequences...
341 int /* O - Count or -1 on error */
343 cups_utf32_t *dest, /* O - Target string */
344 const cups_utf8_t *src, /* I - Source string */
345 const int maxout) /* I - Max output */
347 int i; /* Looping variable */
348 cups_utf8_t ch; /* Character value */
349 cups_utf8_t next; /* Next character value */
350 cups_utf32_t ch32; /* UTF-32 character value */
354 * Check for valid arguments and clear output...
357 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest,
363 if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
365 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
371 * Convert input UTF-8 to output UTF-32...
374 for (i = maxout - 1; *src && i > 0; i --)
379 * Convert UTF-8 character(s) to UTF-32 character...
385 * One-octet UTF-8 <= 127 (US-ASCII)...
390 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src[-1], ch));
393 else if ((ch & 0xe0) == 0xc0)
396 * Two-octet UTF-8 <= 2047 (Latin-x)...
400 if ((next & 0xc0) != 0x80)
402 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
407 ch32 = ((ch & 0x1f) << 6) | (next & 0x3f);
410 * Check for non-shortest form (invalid UTF-8)...
415 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
422 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
423 src[-2], src[-1], (unsigned)ch32));
425 else if ((ch & 0xf0) == 0xe0)
428 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
432 if ((next & 0xc0) != 0x80)
434 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
439 ch32 = ((ch & 0x0f) << 6) | (next & 0x3f);
442 if ((next & 0xc0) != 0x80)
444 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
449 ch32 = (ch32 << 6) | (next & 0x3f);
452 * Check for non-shortest form (invalid UTF-8)...
457 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
464 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
465 src[-3], src[-2], src[-1], (unsigned)ch32));
467 else if ((ch & 0xf8) == 0xf0)
470 * Four-octet UTF-8...
474 if ((next & 0xc0) != 0x80)
476 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
481 ch32 = ((ch & 0x07) << 6) | (next & 0x3f);
484 if ((next & 0xc0) != 0x80)
486 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
491 ch32 = (ch32 << 6) | (next & 0x3f);
494 if ((next & 0xc0) != 0x80)
496 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
501 ch32 = (ch32 << 6) | (next & 0x3f);
504 * Check for non-shortest form (invalid UTF-8)...
509 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
516 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
517 src[-4], src[-3], src[-2], src[-1], (unsigned)ch32));
522 * More than 4-octet (invalid UTF-8 sequence)...
525 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
531 * Check for UTF-16 surrogate (illegal UTF-8)...
534 if (ch32 >= 0xd800 && ch32 <= 0xdfff)
540 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout - 1 - i));
542 return (maxout - 1 - i);
547 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
549 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
551 * UTF-32 char UTF-8 char(s)
552 * --------------------------------------------------
553 * 0 to 127 = 0xxxxxxx (US-ASCII)
554 * 128 to 2047 = 110xxxxx 10yyyyyy
555 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
556 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
558 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
559 * which would convert to five- or six-octet UTF-8 sequences...
562 int /* O - Count or -1 on error */
564 cups_utf8_t *dest, /* O - Target string */
565 const cups_utf32_t *src, /* I - Source string */
566 const int maxout) /* I - Max output */
568 cups_utf8_t *start; /* Start of destination string */
569 int i; /* Looping variable */
570 int swap; /* Byte-swap input to output */
571 cups_utf32_t ch; /* Character value */
575 * Check for valid arguments and clear output...
578 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest, src,
584 if (!dest || !src || maxout < 1)
586 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
592 * Check for leading BOM in UTF-32 and inverted BOM...
596 swap = *src == 0xfffe0000;
598 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap));
600 if (*src == 0xfffe0000 || *src == 0xfeff)
604 * Convert input UTF-32 to output UTF-8...
607 for (i = maxout - 1; *src && i > 0;)
612 * Byte swap input UTF-32, if necessary...
613 * (only byte-swapping 24 of 32 bits)
617 ch = ((ch >> 24) | ((ch >> 8) & 0xff00) | ((ch << 8) & 0xff0000));
620 * Check for beyond Plane 16 (invalid UTF-32)...
625 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
631 * Convert UTF-32 character to UTF-8 character(s)...
637 * One-octet UTF-8 <= 127 (US-ASCII)...
640 *dest++ = (cups_utf8_t)ch;
643 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch, dest[-1]));
648 * Two-octet UTF-8 <= 2047 (Latin-x)...
653 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
658 *dest++ = (cups_utf8_t)(0xc0 | ((ch >> 6) & 0x1f));
659 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
662 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch,
663 dest[-2], dest[-1]));
665 else if (ch < 0x10000)
668 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
673 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
678 *dest++ = (cups_utf8_t)(0xe0 | ((ch >> 12) & 0x0f));
679 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
680 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
683 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch,
684 dest[-3], dest[-2], dest[-1]));
689 * Four-octet UTF-8...
694 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
699 *dest++ = (cups_utf8_t)(0xf0 | ((ch >> 18) & 0x07));
700 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 12) & 0x3f));
701 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
702 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
705 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
706 (unsigned)ch, dest[-4], dest[-3], dest[-2], dest[-1]));
712 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest - start)));
714 return ((int)(dest - start));
719 * End of "$Id: transcode.c 9820 2011-06-10 22:06:26Z mike $"