]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/transcode.c
Fix clang-reported issues (<rdar://problem/15936066>)
[thirdparty/cups.git] / cups / transcode.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
71e16022 4 * Transcoding support for CUPS.
ef416fc2 5 *
71e16022 6 * Copyright 2007-2010 by Apple Inc.
b86bc4cf 7 * Copyright 1997-2007 by Easy Software Products.
ef416fc2 8 *
bc44d920 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 *
bc44d920 15 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 16 *
17 * Contents:
18 *
fa73b229 19 * _cupsCharmapFlush() - Flush all character set maps out of cache.
ef416fc2 20 * cupsCharsetToUTF8() - Convert legacy character set to UTF-8.
e1d6a774 21 * cupsUTF8ToCharset() - Convert UTF-8 to legacy character set.
ef416fc2 22 * cupsUTF8ToUTF32() - Convert UTF-8 to UTF-32.
23 * cupsUTF32ToUTF8() - Convert UTF-32 to UTF-8.
ef416fc2 24 */
25
26/*
27 * Include necessary headers...
28 */
29
71e16022 30#include "cups-private.h"
e53920b9 31#include <limits.h>
ef416fc2 32#include <time.h>
cc754834
MS
33#ifdef HAVE_ICONV_H
34# include <iconv.h>
35#endif /* HAVE_ICONV_H */
ef416fc2 36
37
d6ae789d 38/*
39 * Local globals...
40 */
41
cc754834 42#ifdef HAVE_ICONV_H
6d2f911b 43static _cups_mutex_t map_mutex = _CUPS_MUTEX_INITIALIZER;
d6ae789d 44 /* Mutex to control access to maps */
cc754834
MS
45static iconv_t map_from_utf8 = (iconv_t)-1;
46 /* Convert from UTF-8 to charset */
47static iconv_t map_to_utf8 = (iconv_t)-1;
48 /* Convert from charset to UTF-8 */
49static cups_encoding_t map_encoding = CUPS_AUTO_ENCODING;
50 /* Which charset is cached */
51#endif /* HAVE_ICONV_H */
e1d6a774 52
ef416fc2 53
54/*
e1d6a774 55 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
ef416fc2 56 */
57
e1d6a774 58void
d6ae789d 59_cupsCharmapFlush(void)
ef416fc2 60{
cc754834
MS
61#ifdef HAVE_ICONV_H
62 if (map_from_utf8 != (iconv_t)-1)
ef416fc2 63 {
cc754834
MS
64 iconv_close(map_from_utf8);
65 map_from_utf8 = (iconv_t)-1;
ef416fc2 66 }
d6ae789d 67
cc754834 68 if (map_to_utf8 != (iconv_t)-1)
ef416fc2 69 {
cc754834
MS
70 iconv_close(map_to_utf8);
71 map_to_utf8 = (iconv_t)-1;
ef416fc2 72 }
ef416fc2 73
cc754834
MS
74 map_encoding = CUPS_AUTO_ENCODING;
75#endif /* HAVE_ICONV_H */
ef416fc2 76}
77
e1d6a774 78
ef416fc2 79/*
e1d6a774 80 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
ef416fc2 81 */
e1d6a774 82
83int /* O - Count or -1 on error */
84cupsCharsetToUTF8(
cc754834
MS
85 cups_utf8_t *dest, /* O - Target string */
86 const char *src, /* I - Source string */
87 const int maxout, /* I - Max output */
e1d6a774 88 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 89{
cc754834 90 cups_utf8_t *destptr; /* Pointer into UTF-8 buffer */
84315f46 91#ifdef HAVE_ICONV_H
cc754834
MS
92 size_t srclen, /* Length of source string */
93 outBytesLeft; /* Bytes remaining in output buffer */
7cf5915e 94#endif /* HAVE_ICONV_H */
d6ae789d 95
96
ef416fc2 97 /*
98 * Check for valid arguments...
99 */
100
f11a948a 101 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)",
e1d6a774 102 dest, src, maxout, encoding));
103
cc754834 104 if (!dest || !src || maxout < 1)
e1d6a774 105 {
cc754834
MS
106 if (dest)
107 *dest = '\0';
108
f11a948a 109 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
ef416fc2 110 return (-1);
e1d6a774 111 }
ef416fc2 112
113 /*
114 * Handle identity conversions...
115 */
116
cc754834
MS
117 if (encoding == CUPS_UTF8 || encoding <= CUPS_US_ASCII ||
118 encoding >= CUPS_ENCODING_VBCS_END)
ef416fc2 119 {
e1d6a774 120 strlcpy((char *)dest, src, maxout);
b86bc4cf 121 return ((int)strlen((char *)dest));
ef416fc2 122 }
123
411affcf 124 /*
125 * Handle ISO-8859-1 to UTF-8 directly...
126 */
127
cc754834
MS
128 destptr = dest;
129
411affcf 130 if (encoding == CUPS_ISO8859_1)
131 {
132 int ch; /* Character from string */
cc754834 133 cups_utf8_t *destend; /* End of UTF-8 buffer */
411affcf 134
135
411affcf 136 destend = dest + maxout - 2;
137
138 while (*src && destptr < destend)
139 {
140 ch = *src++ & 255;
141
142 if (ch & 128)
143 {
144 *destptr++ = 0xc0 | (ch >> 6);
145 *destptr++ = 0x80 | (ch & 0x3f);
146 }
147 else
148 *destptr++ = ch;
149 }
150
151 *destptr = '\0';
152
b86bc4cf 153 return ((int)(destptr - dest));
411affcf 154 }
155
ef416fc2 156 /*
e1d6a774 157 * Convert input legacy charset to UTF-8...
ef416fc2 158 */
e1d6a774 159
cc754834 160#ifdef HAVE_ICONV_H
6d2f911b 161 _cupsMutexLock(&map_mutex);
d6ae789d 162
cc754834
MS
163 if (map_encoding != encoding)
164 {
165 _cupsCharmapFlush();
166
167 map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8");
168 map_to_utf8 = iconv_open("UTF-8", _cupsEncodingName(encoding));
169 map_encoding = encoding;
170 }
171
172 if (map_to_utf8 != (iconv_t)-1)
173 {
f99f3698
MS
174 char *altdestptr = (char *)dest; /* Silence bogus GCC type-punned */
175
cc754834
MS
176 srclen = strlen(src);
177 outBytesLeft = maxout - 1;
4220952d 178
f99f3698
MS
179 iconv(map_to_utf8, (char **)&src, &srclen, &altdestptr, &outBytesLeft);
180 *altdestptr = '\0';
cc754834
MS
181
182 _cupsMutexUnlock(&map_mutex);
183
f99f3698 184 return ((int)(altdestptr - (char *)dest));
cc754834 185 }
d6ae789d 186
6d2f911b 187 _cupsMutexUnlock(&map_mutex);
cc754834 188#endif /* HAVE_ICONV_H */
d6ae789d 189
cc754834
MS
190 /*
191 * No iconv() support, so error out...
192 */
193
194 *destptr = '\0';
195
196 return (-1);
ef416fc2 197}
198
e1d6a774 199
ef416fc2 200/*
e1d6a774 201 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
ef416fc2 202 */
e1d6a774 203
204int /* O - Count or -1 on error */
205cupsUTF8ToCharset(
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 */
ef416fc2 210{
cc754834 211 char *destptr; /* Pointer into destination */
84315f46 212#ifdef HAVE_ICONV_H
cc754834
MS
213 size_t srclen, /* Length of source string */
214 outBytesLeft; /* Bytes remaining in output buffer */
7cf5915e 215#endif /* HAVE_ICONV_H */
d6ae789d 216
217
ef416fc2 218 /*
219 * Check for valid arguments...
220 */
221
cc754834 222 if (!dest || !src || maxout < 1)
e1d6a774 223 {
224 if (dest)
225 *dest = '\0';
226
ef416fc2 227 return (-1);
e1d6a774 228 }
ef416fc2 229
230 /*
231 * Handle identity conversions...
232 */
233
22c9029b 234 if (encoding == CUPS_UTF8 ||
cc754834 235 encoding >= CUPS_ENCODING_VBCS_END)
ef416fc2 236 {
e1d6a774 237 strlcpy(dest, (char *)src, maxout);
b86bc4cf 238 return ((int)strlen(dest));
ef416fc2 239 }
240
411affcf 241 /*
242 * Handle UTF-8 to ISO-8859-1 directly...
243 */
244
cc754834
MS
245 destptr = dest;
246
22c9029b 247 if (encoding == CUPS_ISO8859_1 || encoding <= CUPS_US_ASCII)
411affcf 248 {
22c9029b
MS
249 int ch, /* Character from string */
250 maxch; /* Maximum character for charset */
cc754834 251 char *destend; /* End of ISO-8859-1 buffer */
411affcf 252
22c9029b 253 maxch = encoding == CUPS_ISO8859_1 ? 256 : 128;
411affcf 254 destend = dest + maxout - 1;
255
256 while (*src && destptr < destend)
257 {
258 ch = *src++;
259
260 if ((ch & 0xe0) == 0xc0)
261 {
262 ch = ((ch & 0x1f) << 6) | (*src++ & 0x3f);
263
22c9029b 264 if (ch < maxch)
411affcf 265 *destptr++ = ch;
266 else
267 *destptr++ = '?';
268 }
269 else if ((ch & 0xf0) == 0xe0 ||
270 (ch & 0xf8) == 0xf0)
271 *destptr++ = '?';
272 else if (!(ch & 0x80))
273 *destptr++ = ch;
274 }
275
276 *destptr = '\0';
277
b86bc4cf 278 return ((int)(destptr - dest));
411affcf 279 }
280
cc754834 281#ifdef HAVE_ICONV_H
ef416fc2 282 /*
e1d6a774 283 * Convert input UTF-8 to legacy charset...
ef416fc2 284 */
e1d6a774 285
6d2f911b 286 _cupsMutexLock(&map_mutex);
d6ae789d 287
cc754834
MS
288 if (map_encoding != encoding)
289 {
290 _cupsCharmapFlush();
291
292 map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8");
293 map_to_utf8 = iconv_open("UTF-8", _cupsEncodingName(encoding));
294 map_encoding = encoding;
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
MS
301 srclen = strlen((char *)src);
302 outBytesLeft = 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
ef416fc2 407 ch32 = ((ch & 0x1f) << 6) | (next & 0x3f);
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
439 ch32 = ((ch & 0x0f) << 6) | (next & 0x3f);
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
449 ch32 = (ch32 << 6) | (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
481 ch32 = ((ch & 0x07) << 6) | (next & 0x3f);
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
491 ch32 = (ch32 << 6) | (next & 0x3f);
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
501 ch32 = (ch32 << 6) | (next & 0x3f);
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 */