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