]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/transcode.c
Sync up some other changes.
[thirdparty/cups.git] / cups / transcode.c
CommitLineData
ef416fc2 1/*
31db8ded 2 * "$Id: transcode.c 9306 2010-09-16 21:43:57Z mike $"
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 */
7cf5915e 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 {
174 srclen = strlen(src);
175 outBytesLeft = maxout - 1;
4220952d
MS
176
177 iconv(map_to_utf8, (char **)&src, &srclen, (char **)&destptr,
178 &outBytesLeft);
179 *destptr = '\0';
cc754834
MS
180
181 _cupsMutexUnlock(&map_mutex);
182
183 return ((int)(destptr - dest));
184 }
d6ae789d 185
6d2f911b 186 _cupsMutexUnlock(&map_mutex);
cc754834 187#endif /* HAVE_ICONV_H */
d6ae789d 188
cc754834
MS
189 /*
190 * No iconv() support, so error out...
191 */
192
193 *destptr = '\0';
194
195 return (-1);
ef416fc2 196}
197
e1d6a774 198
ef416fc2 199/*
e1d6a774 200 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
ef416fc2 201 */
e1d6a774 202
203int /* O - Count or -1 on error */
204cupsUTF8ToCharset(
205 char *dest, /* O - Target string */
206 const cups_utf8_t *src, /* I - Source string */
207 const int maxout, /* I - Max output */
208 const cups_encoding_t encoding) /* I - Encoding */
ef416fc2 209{
cc754834 210 char *destptr; /* Pointer into destination */
7cf5915e 211#ifdef HAVE_ICONV_H
cc754834
MS
212 size_t srclen, /* Length of source string */
213 outBytesLeft; /* Bytes remaining in output buffer */
7cf5915e 214#endif /* HAVE_ICONV_H */
d6ae789d 215
216
ef416fc2 217 /*
218 * Check for valid arguments...
219 */
220
cc754834 221 if (!dest || !src || maxout < 1)
e1d6a774 222 {
223 if (dest)
224 *dest = '\0';
225
ef416fc2 226 return (-1);
e1d6a774 227 }
ef416fc2 228
229 /*
230 * Handle identity conversions...
231 */
232
cc754834
MS
233 if (encoding == CUPS_UTF8 || encoding <= CUPS_US_ASCII ||
234 encoding >= CUPS_ENCODING_VBCS_END)
ef416fc2 235 {
e1d6a774 236 strlcpy(dest, (char *)src, maxout);
b86bc4cf 237 return ((int)strlen(dest));
ef416fc2 238 }
239
411affcf 240 /*
241 * Handle UTF-8 to ISO-8859-1 directly...
242 */
243
cc754834
MS
244 destptr = dest;
245
411affcf 246 if (encoding == CUPS_ISO8859_1)
247 {
248 int ch; /* Character from string */
cc754834 249 char *destend; /* End of ISO-8859-1 buffer */
411affcf 250
251
411affcf 252 destend = dest + maxout - 1;
253
254 while (*src && destptr < destend)
255 {
256 ch = *src++;
257
258 if ((ch & 0xe0) == 0xc0)
259 {
260 ch = ((ch & 0x1f) << 6) | (*src++ & 0x3f);
261
262 if (ch < 256)
263 *destptr++ = ch;
264 else
265 *destptr++ = '?';
266 }
267 else if ((ch & 0xf0) == 0xe0 ||
268 (ch & 0xf8) == 0xf0)
269 *destptr++ = '?';
270 else if (!(ch & 0x80))
271 *destptr++ = ch;
272 }
273
274 *destptr = '\0';
275
b86bc4cf 276 return ((int)(destptr - dest));
411affcf 277 }
278
cc754834 279#ifdef HAVE_ICONV_H
ef416fc2 280 /*
e1d6a774 281 * Convert input UTF-8 to legacy charset...
ef416fc2 282 */
e1d6a774 283
6d2f911b 284 _cupsMutexLock(&map_mutex);
d6ae789d 285
cc754834
MS
286 if (map_encoding != encoding)
287 {
288 _cupsCharmapFlush();
289
290 map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8");
291 map_to_utf8 = iconv_open("UTF-8", _cupsEncodingName(encoding));
292 map_encoding = encoding;
293 }
294
295 if (map_from_utf8 != (iconv_t)-1)
296 {
297 srclen = strlen((char *)src);
298 outBytesLeft = maxout - 1;
4220952d
MS
299
300 iconv(map_from_utf8, (char **)&src, &srclen, &destptr, &outBytesLeft);
301 *destptr = '\0';
cc754834
MS
302
303 _cupsMutexUnlock(&map_mutex);
304
305 return ((int)(destptr - dest));
306 }
d6ae789d 307
6d2f911b 308 _cupsMutexUnlock(&map_mutex);
cc754834
MS
309#endif /* HAVE_ICONV_H */
310
311 /*
312 * No iconv() support, so error out...
313 */
314
315 *destptr = '\0';
d6ae789d 316
cc754834 317 return (-1);
ef416fc2 318}
319
ef416fc2 320
321/*
322 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
323 *
324 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
325 *
326 * UTF-32 char UTF-8 char(s)
327 * --------------------------------------------------
e1d6a774 328 * 0 to 127 = 0xxxxxxx (US-ASCII)
ef416fc2 329 * 128 to 2047 = 110xxxxx 10yyyyyy
330 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
e1d6a774 331 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
ef416fc2 332 *
333 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
334 * which would convert to five- or six-octet UTF-8 sequences...
ef416fc2 335 */
e1d6a774 336
337int /* O - Count or -1 on error */
338cupsUTF8ToUTF32(
339 cups_utf32_t *dest, /* O - Target string */
340 const cups_utf8_t *src, /* I - Source string */
341 const int maxout) /* I - Max output */
ef416fc2 342{
e1d6a774 343 int i; /* Looping variable */
344 cups_utf8_t ch; /* Character value */
345 cups_utf8_t next; /* Next character value */
346 cups_utf32_t ch32; /* UTF-32 character value */
347
ef416fc2 348
349 /*
350 * Check for valid arguments and clear output...
351 */
e1d6a774 352
e07d4801
MS
353 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest,
354 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
ef416fc2 403 ch32 = ((ch & 0x1f) << 6) | (next & 0x3f);
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
435 ch32 = ((ch & 0x0f) << 6) | (next & 0x3f);
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
445 ch32 = (ch32 << 6) | (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
477 ch32 = ((ch & 0x07) << 6) | (next & 0x3f);
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
487 ch32 = (ch32 << 6) | (next & 0x3f);
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
497 ch32 = (ch32 << 6) | (next & 0x3f);
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
e07d4801 574 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest, src,
c9fc04c6
MS
575 maxout));
576
e1d6a774 577 if (dest)
578 *dest = '\0';
579
580 if (!dest || !src || maxout < 1)
c9fc04c6 581 {
e07d4801 582 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
c9fc04c6 583
ef416fc2 584 return (-1);
c9fc04c6 585 }
ef416fc2 586
587 /*
588 * Check for leading BOM in UTF-32 and inverted BOM...
589 */
e1d6a774 590
591 start = dest;
592 swap = *src == 0xfffe0000;
593
e07d4801 594 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap));
c9fc04c6 595
e1d6a774 596 if (*src == 0xfffe0000 || *src == 0xfeff)
597 src ++;
ef416fc2 598
599 /*
600 * Convert input UTF-32 to output UTF-8...
601 */
e1d6a774 602
603 for (i = maxout - 1; *src && i > 0;)
ef416fc2 604 {
e1d6a774 605 ch = *src++;
ef416fc2 606
607 /*
608 * Byte swap input UTF-32, if necessary...
e1d6a774 609 * (only byte-swapping 24 of 32 bits)
ef416fc2 610 */
e1d6a774 611
ef416fc2 612 if (swap)
613 ch = ((ch >> 24) | ((ch >> 8) & 0xff00) | ((ch << 8) & 0xff0000));
614
615 /*
e1d6a774 616 * Check for beyond Plane 16 (invalid UTF-32)...
ef416fc2 617 */
ef416fc2 618
ef416fc2 619 if (ch > 0x10ffff)
c9fc04c6 620 {
e07d4801 621 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
c9fc04c6 622
ef416fc2 623 return (-1);
c9fc04c6 624 }
ef416fc2 625
ef416fc2 626 /*
627 * Convert UTF-32 character to UTF-8 character(s)...
628 */
e1d6a774 629
630 if (ch < 0x80)
ef416fc2 631 {
632 /*
633 * One-octet UTF-8 <= 127 (US-ASCII)...
634 */
e1d6a774 635
636 *dest++ = (cups_utf8_t)ch;
637 i --;
c9fc04c6 638
e07d4801 639 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch, dest[-1]));
ef416fc2 640 }
e1d6a774 641 else if (ch < 0x800)
ef416fc2 642 {
643 /*
644 * Two-octet UTF-8 <= 2047 (Latin-x)...
645 */
e1d6a774 646
647 if (i < 2)
c9fc04c6 648 {
e07d4801 649 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
c9fc04c6 650
e1d6a774 651 return (-1);
c9fc04c6 652 }
e1d6a774 653
654 *dest++ = (cups_utf8_t)(0xc0 | ((ch >> 6) & 0x1f));
655 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
656 i -= 2;
c9fc04c6 657
e07d4801 658 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch,
c9fc04c6 659 dest[-2], dest[-1]));
ef416fc2 660 }
e1d6a774 661 else if (ch < 0x10000)
ef416fc2 662 {
663 /*
664 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
665 */
e1d6a774 666
667 if (i < 3)
c9fc04c6 668 {
e07d4801 669 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
c9fc04c6 670
e1d6a774 671 return (-1);
c9fc04c6 672 }
e1d6a774 673
674 *dest++ = (cups_utf8_t)(0xe0 | ((ch >> 12) & 0x0f));
675 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
676 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
677 i -= 3;
c9fc04c6 678
e07d4801 679 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch,
c9fc04c6 680 dest[-3], dest[-2], dest[-1]));
e1d6a774 681 }
682 else
683 {
684 /*
685 * Four-octet UTF-8...
686 */
687
688 if (i < 4)
e07d4801
MS
689 {
690 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
691
e1d6a774 692 return (-1);
e07d4801 693 }
e1d6a774 694
695 *dest++ = (cups_utf8_t)(0xf0 | ((ch >> 18) & 0x07));
696 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 12) & 0x3f));
697 *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
698 *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
699 i -= 4;
c9fc04c6 700
e07d4801 701 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
c9fc04c6 702 (unsigned)ch, dest[-4], dest[-3], dest[-2], dest[-1]));
ef416fc2 703 }
704 }
e1d6a774 705
ef416fc2 706 *dest = '\0';
e1d6a774 707
e07d4801 708 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest - start)));
c9fc04c6 709
e1d6a774 710 return ((int)(dest - start));
ef416fc2 711}
712
e1d6a774 713
ef416fc2 714/*
31db8ded 715 * End of "$Id: transcode.c 9306 2010-09-16 21:43:57Z mike $"
ef416fc2 716 */