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