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