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