]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/transcode.c
Merge pull request #4788 from leoarnold/mailmap
[thirdparty/cups.git] / cups / transcode.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
7e86f2f6 4 * Transcoding support for CUPS.
ef416fc2 5 *
7e86f2f6
MS
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
ef416fc2 8 *
7e86f2f6
MS
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/".
ef416fc2 14 *
7e86f2f6 15 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 16 */
17
18/*
19 * Include necessary headers...
20 */
21
71e16022 22#include "cups-private.h"
e53920b9 23#include <limits.h>
ef416fc2 24#include <time.h>
cc754834
MS
25#ifdef HAVE_ICONV_H
26# include <iconv.h>
27#endif /* HAVE_ICONV_H */
ef416fc2 28
29
d6ae789d 30/*
31 * Local globals...
32 */
33
cc754834 34#ifdef HAVE_ICONV_H
6d2f911b 35static _cups_mutex_t map_mutex = _CUPS_MUTEX_INITIALIZER;
d6ae789d 36 /* Mutex to control access to maps */
cc754834
MS
37static iconv_t map_from_utf8 = (iconv_t)-1;
38 /* Convert from UTF-8 to charset */
39static iconv_t map_to_utf8 = (iconv_t)-1;
40 /* Convert from charset to UTF-8 */
41static cups_encoding_t map_encoding = CUPS_AUTO_ENCODING;
42 /* Which charset is cached */
43#endif /* HAVE_ICONV_H */
e1d6a774 44
ef416fc2 45
46/*
e1d6a774 47 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
ef416fc2 48 */
49
e1d6a774 50void
d6ae789d 51_cupsCharmapFlush(void)
ef416fc2 52{
cc754834
MS
53#ifdef HAVE_ICONV_H
54 if (map_from_utf8 != (iconv_t)-1)
ef416fc2 55 {
cc754834
MS
56 iconv_close(map_from_utf8);
57 map_from_utf8 = (iconv_t)-1;
ef416fc2 58 }
d6ae789d 59
cc754834 60 if (map_to_utf8 != (iconv_t)-1)
ef416fc2 61 {
cc754834
MS
62 iconv_close(map_to_utf8);
63 map_to_utf8 = (iconv_t)-1;
ef416fc2 64 }
ef416fc2 65
cc754834
MS
66 map_encoding = CUPS_AUTO_ENCODING;
67#endif /* HAVE_ICONV_H */
ef416fc2 68}
69
e1d6a774 70
ef416fc2 71/*
e1d6a774 72 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
ef416fc2 73 */
e1d6a774 74
75int /* O - Count or -1 on error */
76cupsCharsetToUTF8(
cc754834
MS
77 cups_utf8_t *dest, /* O - Target string */
78 const char *src, /* I - Source string */
79 const int maxout, /* I - Max output */
e1d6a774 80 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 81{
cc754834 82 cups_utf8_t *destptr; /* Pointer into UTF-8 buffer */
84315f46 83#ifdef HAVE_ICONV_H
cc754834
MS
84 size_t srclen, /* Length of source string */
85 outBytesLeft; /* Bytes remaining in output buffer */
7cf5915e 86#endif /* HAVE_ICONV_H */
d6ae789d 87
88
ef416fc2 89 /*
90 * Check for valid arguments...
91 */
92
f11a948a 93 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)",
e1d6a774 94 dest, src, maxout, encoding));
95
cc754834 96 if (!dest || !src || maxout < 1)
e1d6a774 97 {
cc754834
MS
98 if (dest)
99 *dest = '\0';
100
f11a948a 101 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
ef416fc2 102 return (-1);
e1d6a774 103 }
ef416fc2 104
105 /*
106 * Handle identity conversions...
107 */
108
cc754834
MS
109 if (encoding == CUPS_UTF8 || encoding <= CUPS_US_ASCII ||
110 encoding >= CUPS_ENCODING_VBCS_END)
ef416fc2 111 {
07623986 112 strlcpy((char *)dest, src, (size_t)maxout);
b86bc4cf 113 return ((int)strlen((char *)dest));
ef416fc2 114 }
115
411affcf 116 /*
117 * Handle ISO-8859-1 to UTF-8 directly...
118 */
119
cc754834
MS
120 destptr = dest;
121
411affcf 122 if (encoding == CUPS_ISO8859_1)
123 {
124 int ch; /* Character from string */
cc754834 125 cups_utf8_t *destend; /* End of UTF-8 buffer */
411affcf 126
127
411affcf 128 destend = dest + maxout - 2;
129
130 while (*src && destptr < destend)
131 {
132 ch = *src++ & 255;
133
134 if (ch & 128)
135 {
7e86f2f6
MS
136 *destptr++ = (cups_utf8_t)(0xc0 | (ch >> 6));
137 *destptr++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
411affcf 138 }
139 else
7e86f2f6 140 *destptr++ = (cups_utf8_t)ch;
411affcf 141 }
142
143 *destptr = '\0';
144
b86bc4cf 145 return ((int)(destptr - dest));
411affcf 146 }
147
ef416fc2 148 /*
e1d6a774 149 * Convert input legacy charset to UTF-8...
ef416fc2 150 */
e1d6a774 151
cc754834 152#ifdef HAVE_ICONV_H
6d2f911b 153 _cupsMutexLock(&map_mutex);
d6ae789d 154
cc754834
MS
155 if (map_encoding != encoding)
156 {
52958fdb
MS
157 char toset[1024]; /* Destination character set */
158
cc754834
MS
159 _cupsCharmapFlush();
160
52958fdb
MS
161 snprintf(toset, sizeof(toset), "%s//IGNORE", _cupsEncodingName(encoding));
162
163 map_encoding = encoding;
cc754834 164 map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8");
52958fdb 165 map_to_utf8 = iconv_open("UTF-8", toset);
cc754834
MS
166 }
167
168 if (map_to_utf8 != (iconv_t)-1)
169 {
f99f3698
MS
170 char *altdestptr = (char *)dest; /* Silence bogus GCC type-punned */
171
cc754834 172 srclen = strlen(src);
7e86f2f6 173 outBytesLeft = (size_t)maxout - 1;
4220952d 174
f99f3698
MS
175 iconv(map_to_utf8, (char **)&src, &srclen, &altdestptr, &outBytesLeft);
176 *altdestptr = '\0';
cc754834
MS
177
178 _cupsMutexUnlock(&map_mutex);
179
f99f3698 180 return ((int)(altdestptr - (char *)dest));
cc754834 181 }
d6ae789d 182
6d2f911b 183 _cupsMutexUnlock(&map_mutex);
cc754834 184#endif /* HAVE_ICONV_H */
d6ae789d 185
cc754834
MS
186 /*
187 * No iconv() support, so error out...
188 */
189
190 *destptr = '\0';
191
192 return (-1);
ef416fc2 193}
194
e1d6a774 195
ef416fc2 196/*
e1d6a774 197 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
ef416fc2 198 */
e1d6a774 199
200int /* O - Count or -1 on error */
201cupsUTF8ToCharset(
202 char *dest, /* O - Target string */
203 const cups_utf8_t *src, /* I - Source string */
204 const int maxout, /* I - Max output */
205 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 206{
cc754834 207 char *destptr; /* Pointer into destination */
84315f46 208#ifdef HAVE_ICONV_H
cc754834
MS
209 size_t srclen, /* Length of source string */
210 outBytesLeft; /* Bytes remaining in output buffer */
7cf5915e 211#endif /* HAVE_ICONV_H */
d6ae789d 212
213
ef416fc2 214 /*
215 * Check for valid arguments...
216 */
217
cc754834 218 if (!dest || !src || maxout < 1)
e1d6a774 219 {
220 if (dest)
221 *dest = '\0';
222
ef416fc2 223 return (-1);
e1d6a774 224 }
ef416fc2 225
226 /*
227 * Handle identity conversions...
228 */
229
22c9029b 230 if (encoding == CUPS_UTF8 ||
cc754834 231 encoding >= CUPS_ENCODING_VBCS_END)
ef416fc2 232 {
07623986 233 strlcpy(dest, (char *)src, (size_t)maxout);
b86bc4cf 234 return ((int)strlen(dest));
ef416fc2 235 }
236
411affcf 237 /*
238 * Handle UTF-8 to ISO-8859-1 directly...
239 */
240
cc754834
MS
241 destptr = dest;
242
22c9029b 243 if (encoding == CUPS_ISO8859_1 || encoding <= CUPS_US_ASCII)
411affcf 244 {
22c9029b
MS
245 int ch, /* Character from string */
246 maxch; /* Maximum character for charset */
cc754834 247 char *destend; /* End of ISO-8859-1 buffer */
411affcf 248
22c9029b 249 maxch = encoding == CUPS_ISO8859_1 ? 256 : 128;
411affcf 250 destend = dest + maxout - 1;
251
252 while (*src && destptr < destend)
253 {
254 ch = *src++;
255
256 if ((ch & 0xe0) == 0xc0)
257 {
258 ch = ((ch & 0x1f) << 6) | (*src++ & 0x3f);
259
22c9029b 260 if (ch < maxch)
7e86f2f6 261 *destptr++ = (char)ch;
411affcf 262 else
263 *destptr++ = '?';
264 }
265 else if ((ch & 0xf0) == 0xe0 ||
266 (ch & 0xf8) == 0xf0)
267 *destptr++ = '?';
268 else if (!(ch & 0x80))
7e86f2f6 269 *destptr++ = (char)ch;
411affcf 270 }
271
272 *destptr = '\0';
273
b86bc4cf 274 return ((int)(destptr - dest));
411affcf 275 }
276
cc754834 277#ifdef HAVE_ICONV_H
ef416fc2 278 /*
e1d6a774 279 * Convert input UTF-8 to legacy charset...
ef416fc2 280 */
e1d6a774 281
6d2f911b 282 _cupsMutexLock(&map_mutex);
d6ae789d 283
cc754834
MS
284 if (map_encoding != encoding)
285 {
52958fdb
MS
286 char toset[1024]; /* Destination character set */
287
cc754834
MS
288 _cupsCharmapFlush();
289
52958fdb
MS
290 snprintf(toset, sizeof(toset), "%s//IGNORE", _cupsEncodingName(encoding));
291
cc754834 292 map_encoding = encoding;
52958fdb
MS
293 map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8");
294 map_to_utf8 = iconv_open("UTF-8", toset);
cc754834
MS
295 }
296
297 if (map_from_utf8 != (iconv_t)-1)
298 {
f99f3698
MS
299 char *altsrc = (char *)src; /* Silence bogus GCC type-punned */
300
cc754834 301 srclen = strlen((char *)src);
7e86f2f6 302 outBytesLeft = (size_t)maxout - 1;
4220952d 303
f99f3698 304 iconv(map_from_utf8, &altsrc, &srclen, &destptr, &outBytesLeft);
4220952d 305 *destptr = '\0';
cc754834
MS
306
307 _cupsMutexUnlock(&map_mutex);
308
309 return ((int)(destptr - dest));
310 }
d6ae789d 311
6d2f911b 312 _cupsMutexUnlock(&map_mutex);
cc754834
MS
313#endif /* HAVE_ICONV_H */
314
315 /*
316 * No iconv() support, so error out...
317 */
318
319 *destptr = '\0';
d6ae789d 320
cc754834 321 return (-1);
ef416fc2 322}
323
ef416fc2 324
325/*
326 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
327 *
328 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
329 *
330 * UTF-32 char UTF-8 char(s)
331 * --------------------------------------------------
e1d6a774 332 * 0 to 127 = 0xxxxxxx (US-ASCII)
ef416fc2 333 * 128 to 2047 = 110xxxxx 10yyyyyy
334 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
e1d6a774 335 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
ef416fc2 336 *
337 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
338 * which would convert to five- or six-octet UTF-8 sequences...
ef416fc2 339 */
e1d6a774 340
341int /* O - Count or -1 on error */
342cupsUTF8ToUTF32(
343 cups_utf32_t *dest, /* O - Target string */
344 const cups_utf8_t *src, /* I - Source string */
345 const int maxout) /* I - Max output */
ef416fc2 346{
e1d6a774 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 */
351
ef416fc2 352
353 /*
354 * Check for valid arguments and clear output...
355 */
e1d6a774 356
e07d4801
MS
357 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest,
358 src, maxout));
c9fc04c6 359
e1d6a774 360 if (dest)
361 *dest = 0;
362
363 if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
c9fc04c6 364 {
e07d4801 365 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
c9fc04c6 366
ef416fc2 367 return (-1);
c9fc04c6 368 }
ef416fc2 369
370 /*
cda47a96 371 * Convert input UTF-8 to output UTF-32...
ef416fc2 372 */
e1d6a774 373
e1d6a774 374 for (i = maxout - 1; *src && i > 0; i --)
ef416fc2 375 {
e1d6a774 376 ch = *src++;
ef416fc2 377
378 /*
379 * Convert UTF-8 character(s) to UTF-32 character...
380 */
e1d6a774 381
382 if (!(ch & 0x80))
ef416fc2 383 {
384 /*
385 * One-octet UTF-8 <= 127 (US-ASCII)...
386 */
e1d6a774 387
388 *dest++ = ch;
c9fc04c6 389
e07d4801 390 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src[-1], ch));
2abf387c 391 continue;
ef416fc2 392 }
393 else if ((ch & 0xe0) == 0xc0)
394 {
395 /*
396 * Two-octet UTF-8 <= 2047 (Latin-x)...
397 */
e1d6a774 398
399 next = *src++;
c9fc04c6
MS
400 if ((next & 0xc0) != 0x80)
401 {
e07d4801 402 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 403
ef416fc2 404 return (-1);
c9fc04c6 405 }
e1d6a774 406
7e86f2f6 407 ch32 = (cups_utf32_t)((ch & 0x1f) << 6) | (cups_utf32_t)(next & 0x3f);
ef416fc2 408
409 /*
410 * Check for non-shortest form (invalid UTF-8)...
411 */
e1d6a774 412
413 if (ch32 < 0x80)
c9fc04c6 414 {
e07d4801 415 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 416
ef416fc2 417 return (-1);
c9fc04c6 418 }
e1d6a774 419
420 *dest++ = ch32;
c9fc04c6 421
e07d4801 422 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
c9fc04c6 423 src[-2], src[-1], (unsigned)ch32));
ef416fc2 424 }
425 else if ((ch & 0xf0) == 0xe0)
426 {
427 /*
428 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
429 */
e1d6a774 430
431 next = *src++;
c9fc04c6
MS
432 if ((next & 0xc0) != 0x80)
433 {
e07d4801 434 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 435
ef416fc2 436 return (-1);
c9fc04c6 437 }
e1d6a774 438
7e86f2f6 439 ch32 = (cups_utf32_t)((ch & 0x0f) << 6) | (cups_utf32_t)(next & 0x3f);
e1d6a774 440
441 next = *src++;
c9fc04c6
MS
442 if ((next & 0xc0) != 0x80)
443 {
e07d4801 444 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 445
ef416fc2 446 return (-1);
c9fc04c6 447 }
e1d6a774 448
7e86f2f6 449 ch32 = (ch32 << 6) | (cups_utf32_t)(next & 0x3f);
ef416fc2 450
451 /*
452 * Check for non-shortest form (invalid UTF-8)...
453 */
e1d6a774 454
455 if (ch32 < 0x800)
c9fc04c6 456 {
e07d4801 457 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 458
ef416fc2 459 return (-1);
c9fc04c6 460 }
e1d6a774 461
462 *dest++ = ch32;
c9fc04c6 463
e07d4801 464 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
c9fc04c6 465 src[-3], src[-2], src[-1], (unsigned)ch32));
ef416fc2 466 }
467 else if ((ch & 0xf8) == 0xf0)
468 {
469 /*
e1d6a774 470 * Four-octet UTF-8...
ef416fc2 471 */
e1d6a774 472
473 next = *src++;
c9fc04c6
MS
474 if ((next & 0xc0) != 0x80)
475 {
e07d4801 476 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 477
ef416fc2 478 return (-1);
c9fc04c6 479 }
e1d6a774 480
7e86f2f6 481 ch32 = (cups_utf32_t)((ch & 0x07) << 6) | (cups_utf32_t)(next & 0x3f);
e1d6a774 482
483 next = *src++;
c9fc04c6
MS
484 if ((next & 0xc0) != 0x80)
485 {
e07d4801 486 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 487
e1d6a774 488 return (-1);
c9fc04c6 489 }
e1d6a774 490
7e86f2f6 491 ch32 = (ch32 << 6) | (cups_utf32_t)(next & 0x3f);
e1d6a774 492
493 next = *src++;
c9fc04c6
MS
494 if ((next & 0xc0) != 0x80)
495 {
e07d4801 496 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 497
e1d6a774 498 return (-1);
c9fc04c6 499 }
e1d6a774 500
7e86f2f6 501 ch32 = (ch32 << 6) | (cups_utf32_t)(next & 0x3f);
e1d6a774 502
ef416fc2 503 /*
e1d6a774 504 * Check for non-shortest form (invalid UTF-8)...
ef416fc2 505 */
e1d6a774 506
507 if (ch32 < 0x10000)
c9fc04c6 508 {
e07d4801 509 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 510
e1d6a774 511 return (-1);
c9fc04c6 512 }
e1d6a774 513
514 *dest++ = ch32;
c9fc04c6 515
e07d4801 516 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
c9fc04c6 517 src[-4], src[-3], src[-2], src[-1], (unsigned)ch32));
ef416fc2 518 }
519 else
520 {
521 /*
e1d6a774 522 * More than 4-octet (invalid UTF-8 sequence)...
ef416fc2 523 */
e1d6a774 524
e07d4801 525 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
c9fc04c6 526
ef416fc2 527 return (-1);
528 }
529
530 /*
531 * Check for UTF-16 surrogate (illegal UTF-8)...
532 */
ef416fc2 533
2abf387c 534 if (ch32 >= 0xd800 && ch32 <= 0xdfff)
ef416fc2 535 return (-1);
536 }
e1d6a774 537
ef416fc2 538 *dest = 0;
e1d6a774 539
e07d4801 540 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout - 1 - i));
c9fc04c6
MS
541
542 return (maxout - 1 - i);
ef416fc2 543}
544
e1d6a774 545
ef416fc2 546/*
547 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
548 *
549 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
550 *
551 * UTF-32 char UTF-8 char(s)
552 * --------------------------------------------------
e1d6a774 553 * 0 to 127 = 0xxxxxxx (US-ASCII)
ef416fc2 554 * 128 to 2047 = 110xxxxx 10yyyyyy
555 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
e1d6a774 556 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
ef416fc2 557 *
558 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
559 * which would convert to five- or six-octet UTF-8 sequences...
ef416fc2 560 */
e1d6a774 561
562int /* O - Count or -1 on error */
563cupsUTF32ToUTF8(
564 cups_utf8_t *dest, /* O - Target string */
565 const cups_utf32_t *src, /* I - Source string */
566 const int maxout) /* I - Max output */
ef416fc2 567{
e1d6a774 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 */
572
ef416fc2 573
574 /*
575 * Check for valid arguments and clear output...
576 */
e1d6a774 577
e07d4801 578 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest, src,
c9fc04c6
MS
579 maxout));
580
e1d6a774 581 if (dest)
582 *dest = '\0';
583
584 if (!dest || !src || maxout < 1)
c9fc04c6 585 {
e07d4801 586 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
c9fc04c6 587
ef416fc2 588 return (-1);
c9fc04c6 589 }
ef416fc2 590
591 /*
592 * Check for leading BOM in UTF-32 and inverted BOM...
593 */
e1d6a774 594
595 start = dest;
596 swap = *src == 0xfffe0000;
597
e07d4801 598 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap));
c9fc04c6 599
e1d6a774 600 if (*src == 0xfffe0000 || *src == 0xfeff)
601 src ++;
ef416fc2 602
603 /*
604 * Convert input UTF-32 to output UTF-8...
605 */
e1d6a774 606
607 for (i = maxout - 1; *src && i > 0;)
ef416fc2 608 {
e1d6a774 609 ch = *src++;
ef416fc2 610
611 /*
612 * Byte swap input UTF-32, if necessary...
e1d6a774 613 * (only byte-swapping 24 of 32 bits)
ef416fc2 614 */
e1d6a774 615
ef416fc2 616 if (swap)
617 ch = ((ch >> 24) | ((ch >> 8) & 0xff00) | ((ch << 8) & 0xff0000));
618
619 /*
e1d6a774 620 * Check for beyond Plane 16 (invalid UTF-32)...
ef416fc2 621 */
ef416fc2 622
ef416fc2 623 if (ch > 0x10ffff)
c9fc04c6 624 {
e07d4801 625 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
c9fc04c6 626
ef416fc2 627 return (-1);
c9fc04c6 628 }
ef416fc2 629
ef416fc2 630 /*
631 * Convert UTF-32 character to UTF-8 character(s)...
632 */
e1d6a774 633
634 if (ch < 0x80)
ef416fc2 635 {
636 /*
637 * One-octet UTF-8 <= 127 (US-ASCII)...
638 */
e1d6a774 639
640 *dest++ = (cups_utf8_t)ch;
641 i --;
c9fc04c6 642
e07d4801 643 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch, dest[-1]));
ef416fc2 644 }
e1d6a774 645 else if (ch < 0x800)
ef416fc2 646 {
647 /*
648 * Two-octet UTF-8 <= 2047 (Latin-x)...
649 */
e1d6a774 650
651 if (i < 2)
c9fc04c6 652 {
e07d4801 653 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
c9fc04c6 654
e1d6a774 655 return (-1);
c9fc04c6 656 }
e1d6a774 657
658 *dest++ = (cups_utf8_t)(0xc0 | ((ch >> 6) & 0x1f));
659 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
660 i -= 2;
c9fc04c6 661
e07d4801 662 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch,
c9fc04c6 663 dest[-2], dest[-1]));
ef416fc2 664 }
e1d6a774 665 else if (ch < 0x10000)
ef416fc2 666 {
667 /*
668 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
669 */
e1d6a774 670
671 if (i < 3)
c9fc04c6 672 {
e07d4801 673 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
c9fc04c6 674
e1d6a774 675 return (-1);
c9fc04c6 676 }
e1d6a774 677
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));
681 i -= 3;
c9fc04c6 682
e07d4801 683 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch,
c9fc04c6 684 dest[-3], dest[-2], dest[-1]));
e1d6a774 685 }
686 else
687 {
688 /*
689 * Four-octet UTF-8...
690 */
691
692 if (i < 4)
e07d4801
MS
693 {
694 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
695
e1d6a774 696 return (-1);
e07d4801 697 }
e1d6a774 698
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));
703 i -= 4;
c9fc04c6 704
e07d4801 705 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
c9fc04c6 706 (unsigned)ch, dest[-4], dest[-3], dest[-2], dest[-1]));
ef416fc2 707 }
708 }
e1d6a774 709
ef416fc2 710 *dest = '\0';
e1d6a774 711
e07d4801 712 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest - start)));
c9fc04c6 713
e1d6a774 714 return ((int)(dest - start));
ef416fc2 715}
716
e1d6a774 717
ef416fc2 718/*
f2d18633 719 * End of "$Id$"
ef416fc2 720 */