2 * "$Id: transcode.c 7560 2008-05-13 06:34:04Z mike $"
4 * Transcoding support for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * _cupsCharmapFlush() - Flush all character set maps out of cache.
20 * _cupsCharmapFree() - Free a character set map.
21 * _cupsCharmapGet() - Get a character set map.
22 * cupsCharsetToUTF8() - Convert legacy character set to UTF-8.
23 * cupsUTF8ToCharset() - Convert UTF-8 to legacy character set.
24 * cupsUTF8ToUTF32() - Convert UTF-8 to UTF-32.
25 * cupsUTF32ToUTF8() - Convert UTF-32 to UTF-8.
26 * compare_wide() - Compare key for wide (VBCS) match.
27 * conv_sbcs_to_utf8() - Convert legacy SBCS to UTF-8.
28 * conv_utf8_to_sbcs() - Convert UTF-8 to legacy SBCS.
29 * conv_utf8_to_vbcs() - Convert UTF-8 to legacy DBCS/VBCS.
30 * conv_vbcs_to_utf8() - Convert legacy DBCS/VBCS to UTF-8.
31 * free_sbcs_charmap() - Free memory used by a single byte character set.
32 * free_vbcs_charmap() - Free memory used by a variable byte character set.
33 * get_charmap() - Lookup or get a character set map (private).
34 * get_charmap_count() - Count lines in a charmap file.
35 * get_sbcs_charmap() - Get SBCS Charmap.
36 * get_vbcs_charmap() - Get DBCS/VBCS Charmap.
40 * Include necessary headers...
56 static pthread_mutex_t map_mutex
= PTHREAD_MUTEX_INITIALIZER
;
57 /* Mutex to control access to maps */
58 #endif /* HAVE_PTHREAD_H */
59 static _cups_cmap_t
*cmap_cache
= NULL
;
60 /* SBCS Charmap Cache */
61 static _cups_vmap_t
*vmap_cache
= NULL
;
62 /* VBCS Charmap Cache */
69 static int compare_wide(const void *k1
, const void *k2
);
70 static int conv_sbcs_to_utf8(cups_utf8_t
*dest
,
71 const cups_sbcs_t
*src
,
73 const cups_encoding_t encoding
);
74 static int conv_utf8_to_sbcs(cups_sbcs_t
*dest
,
75 const cups_utf8_t
*src
,
77 const cups_encoding_t encoding
);
78 static int conv_utf8_to_vbcs(cups_sbcs_t
*dest
,
79 const cups_utf8_t
*src
,
81 const cups_encoding_t encoding
);
82 static int conv_vbcs_to_utf8(cups_utf8_t
*dest
,
83 const cups_sbcs_t
*src
,
85 const cups_encoding_t encoding
);
86 static void free_sbcs_charmap(_cups_cmap_t
*sbcs
);
87 static void free_vbcs_charmap(_cups_vmap_t
*vbcs
);
88 static void *get_charmap(const cups_encoding_t encoding
);
89 static int get_charmap_count(cups_file_t
*fp
);
90 static _cups_cmap_t
*get_sbcs_charmap(const cups_encoding_t encoding
,
91 const char *filename
);
92 static _cups_vmap_t
*get_vbcs_charmap(const cups_encoding_t encoding
,
93 const char *filename
);
97 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
101 _cupsCharmapFlush(void)
103 _cups_cmap_t
*cmap
, /* Legacy SBCS / Unicode Charset Map */
104 *cnext
; /* Next Legacy SBCS Charset Map */
105 _cups_vmap_t
*vmap
, /* Legacy VBCS / Unicode Charset Map */
106 *vnext
; /* Next Legacy VBCS Charset Map */
109 #ifdef HAVE_PTHREAD_H
110 pthread_mutex_lock(&map_mutex
);
111 #endif /* HAVE_PTHREAD_H */
114 * Loop through SBCS charset map cache, free all memory...
117 for (cmap
= cmap_cache
; cmap
; cmap
= cnext
)
121 free_sbcs_charmap(cmap
);
127 * Loop through DBCS/VBCS charset map cache, free all memory...
130 for (vmap
= vmap_cache
; vmap
; vmap
= vnext
)
134 free_vbcs_charmap(vmap
);
139 #ifdef HAVE_PTHREAD_H
140 pthread_mutex_unlock(&map_mutex
);
141 #endif /* HAVE_PTHREAD_H */
146 * '_cupsCharmapFree()' - Free a character set map.
148 * This does not actually free; use '_cupsCharmapFlush()' for that.
153 const cups_encoding_t encoding
) /* I - Encoding */
155 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
156 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
160 * See if we already have this SBCS charset map loaded...
163 #ifdef HAVE_PTHREAD_H
164 pthread_mutex_lock(&map_mutex
);
165 #endif /* HAVE_PTHREAD_H */
167 for (cmap
= cmap_cache
; cmap
; cmap
= cmap
->next
)
169 if (cmap
->encoding
== encoding
)
178 * See if we already have this DBCS/VBCS charset map loaded...
181 for (vmap
= vmap_cache
; vmap
; vmap
= vmap
->next
)
183 if (vmap
->encoding
== encoding
)
191 #ifdef HAVE_PTHREAD_H
192 pthread_mutex_unlock(&map_mutex
);
193 #endif /* HAVE_PTHREAD_H */
198 * '_cupsCharmapGet()' - Get a character set map.
200 * This code handles single-byte (SBCS), double-byte (DBCS), and
201 * variable-byte (VBCS) character sets _without_ charset escapes...
202 * This code does not handle multiple-byte character sets (MBCS)
203 * (such as ISO-2022-JP) with charset switching via escapes...
206 void * /* O - Charset map pointer */
208 const cups_encoding_t encoding
) /* I - Encoding */
210 void *charmap
; /* Charset map pointer */
213 DEBUG_printf(("7_cupsCharmapGet(encoding=%d)", encoding
));
216 * Check for valid arguments...
219 if (encoding
< 0 || encoding
>= CUPS_ENCODING_VBCS_END
)
221 DEBUG_puts("8_cupsCharmapGet: Bad encoding, returning NULL!");
226 * Lookup or get the charset map pointer and return...
229 #ifdef HAVE_PTHREAD_H
230 pthread_mutex_lock(&map_mutex
);
231 #endif /* HAVE_PTHREAD_H */
233 charmap
= get_charmap(encoding
);
235 #ifdef HAVE_PTHREAD_H
236 pthread_mutex_unlock(&map_mutex
);
237 #endif /* HAVE_PTHREAD_H */
244 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
246 * This code handles single-byte (SBCS), double-byte (DBCS), and
247 * variable-byte (VBCS) character sets _without_ charset escapes...
248 * This code does not handle multiple-byte character sets (MBCS)
249 * (such as ISO-2022-JP) with charset switching via escapes...
252 int /* O - Count or -1 on error */
254 cups_utf8_t
*dest
, /* O - Target string */
255 const char *src
, /* I - Source string */
256 const int maxout
, /* I - Max output */
257 const cups_encoding_t encoding
) /* I - Encoding */
259 int bytes
; /* Number of bytes converted */
263 * Check for valid arguments...
266 DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)",
267 dest
, src
, maxout
, encoding
));
272 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
274 DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
279 * Handle identity conversions...
282 if (encoding
== CUPS_UTF8
||
283 encoding
< 0 || encoding
>= CUPS_ENCODING_VBCS_END
)
285 strlcpy((char *)dest
, src
, maxout
);
286 return ((int)strlen((char *)dest
));
290 * Handle ISO-8859-1 to UTF-8 directly...
293 if (encoding
== CUPS_ISO8859_1
)
295 int ch
; /* Character from string */
296 cups_utf8_t
*destptr
, /* Pointer into UTF-8 buffer */
297 *destend
; /* End of UTF-8 buffer */
301 destend
= dest
+ maxout
- 2;
303 while (*src
&& destptr
< destend
)
309 *destptr
++ = 0xc0 | (ch
>> 6);
310 *destptr
++ = 0x80 | (ch
& 0x3f);
318 return ((int)(destptr
- dest
));
322 * Convert input legacy charset to UTF-8...
325 #ifdef HAVE_PTHREAD_H
326 pthread_mutex_lock(&map_mutex
);
327 #endif /* HAVE_PTHREAD_H */
329 if (encoding
< CUPS_ENCODING_SBCS_END
)
330 bytes
= conv_sbcs_to_utf8(dest
, (cups_sbcs_t
*)src
, maxout
, encoding
);
332 bytes
= conv_vbcs_to_utf8(dest
, (cups_sbcs_t
*)src
, maxout
, encoding
);
334 #ifdef HAVE_PTHREAD_H
335 pthread_mutex_unlock(&map_mutex
);
336 #endif /* HAVE_PTHREAD_H */
343 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
345 * This code handles single-byte (SBCS), double-byte (DBCS), and
346 * variable-byte (VBCS) character sets _without_ charset escapes...
347 * This code does not handle multiple-byte character sets (MBCS)
348 * (such as ISO-2022-JP) with charset switching via escapes...
351 int /* O - Count or -1 on error */
353 char *dest
, /* O - Target string */
354 const cups_utf8_t
*src
, /* I - Source string */
355 const int maxout
, /* I - Max output */
356 const cups_encoding_t encoding
) /* I - Encoding */
358 int bytes
; /* Number of bytes converted */
362 * Check for valid arguments...
365 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
374 * Handle identity conversions...
377 if (encoding
== CUPS_UTF8
||
378 encoding
< 0 || encoding
>= CUPS_ENCODING_VBCS_END
)
380 strlcpy(dest
, (char *)src
, maxout
);
381 return ((int)strlen(dest
));
385 * Handle UTF-8 to ISO-8859-1 directly...
388 if (encoding
== CUPS_ISO8859_1
)
390 int ch
; /* Character from string */
391 char *destptr
, /* Pointer into ISO-8859-1 buffer */
392 *destend
; /* End of ISO-8859-1 buffer */
396 destend
= dest
+ maxout
- 1;
398 while (*src
&& destptr
< destend
)
402 if ((ch
& 0xe0) == 0xc0)
404 ch
= ((ch
& 0x1f) << 6) | (*src
++ & 0x3f);
411 else if ((ch
& 0xf0) == 0xe0 ||
414 else if (!(ch
& 0x80))
420 return ((int)(destptr
- dest
));
424 * Convert input UTF-8 to legacy charset...
427 #ifdef HAVE_PTHREAD_H
428 pthread_mutex_lock(&map_mutex
);
429 #endif /* HAVE_PTHREAD_H */
431 if (encoding
< CUPS_ENCODING_SBCS_END
)
432 bytes
= conv_utf8_to_sbcs((cups_sbcs_t
*)dest
, src
, maxout
, encoding
);
434 bytes
= conv_utf8_to_vbcs((cups_sbcs_t
*)dest
, src
, maxout
, encoding
);
436 #ifdef HAVE_PTHREAD_H
437 pthread_mutex_unlock(&map_mutex
);
438 #endif /* HAVE_PTHREAD_H */
445 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
447 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
449 * UTF-32 char UTF-8 char(s)
450 * --------------------------------------------------
451 * 0 to 127 = 0xxxxxxx (US-ASCII)
452 * 128 to 2047 = 110xxxxx 10yyyyyy
453 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
454 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
456 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
457 * which would convert to five- or six-octet UTF-8 sequences...
460 int /* O - Count or -1 on error */
462 cups_utf32_t
*dest
, /* O - Target string */
463 const cups_utf8_t
*src
, /* I - Source string */
464 const int maxout
) /* I - Max output */
466 int i
; /* Looping variable */
467 cups_utf8_t ch
; /* Character value */
468 cups_utf8_t next
; /* Next character value */
469 cups_utf32_t ch32
; /* UTF-32 character value */
473 * Check for valid arguments and clear output...
476 DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest
,
482 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
484 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
490 * Convert input UTF-8 to output UTF-32...
493 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
498 * Convert UTF-8 character(s) to UTF-32 character...
504 * One-octet UTF-8 <= 127 (US-ASCII)...
509 DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src
[-1], ch
));
512 else if ((ch
& 0xe0) == 0xc0)
515 * Two-octet UTF-8 <= 2047 (Latin-x)...
519 if ((next
& 0xc0) != 0x80)
521 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
526 ch32
= ((ch
& 0x1f) << 6) | (next
& 0x3f);
529 * Check for non-shortest form (invalid UTF-8)...
534 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
541 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
542 src
[-2], src
[-1], (unsigned)ch32
));
544 else if ((ch
& 0xf0) == 0xe0)
547 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
551 if ((next
& 0xc0) != 0x80)
553 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
558 ch32
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
561 if ((next
& 0xc0) != 0x80)
563 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
568 ch32
= (ch32
<< 6) | (next
& 0x3f);
571 * Check for non-shortest form (invalid UTF-8)...
576 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
583 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
584 src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
586 else if ((ch
& 0xf8) == 0xf0)
589 * Four-octet UTF-8...
593 if ((next
& 0xc0) != 0x80)
595 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
600 ch32
= ((ch
& 0x07) << 6) | (next
& 0x3f);
603 if ((next
& 0xc0) != 0x80)
605 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
610 ch32
= (ch32
<< 6) | (next
& 0x3f);
613 if ((next
& 0xc0) != 0x80)
615 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
620 ch32
= (ch32
<< 6) | (next
& 0x3f);
623 * Check for non-shortest form (invalid UTF-8)...
628 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
635 DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
636 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)ch32
));
641 * More than 4-octet (invalid UTF-8 sequence)...
644 DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
650 * Check for UTF-16 surrogate (illegal UTF-8)...
653 if (ch32
>= 0xd800 && ch32
<= 0xdfff)
659 DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout
- 1 - i
));
661 return (maxout
- 1 - i
);
666 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
668 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
670 * UTF-32 char UTF-8 char(s)
671 * --------------------------------------------------
672 * 0 to 127 = 0xxxxxxx (US-ASCII)
673 * 128 to 2047 = 110xxxxx 10yyyyyy
674 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
675 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
677 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
678 * which would convert to five- or six-octet UTF-8 sequences...
681 int /* O - Count or -1 on error */
683 cups_utf8_t
*dest
, /* O - Target string */
684 const cups_utf32_t
*src
, /* I - Source string */
685 const int maxout
) /* I - Max output */
687 cups_utf8_t
*start
; /* Start of destination string */
688 int i
; /* Looping variable */
689 int swap
; /* Byte-swap input to output */
690 cups_utf32_t ch
; /* Character value */
694 * Check for valid arguments and clear output...
697 DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest
, src
,
703 if (!dest
|| !src
|| maxout
< 1)
705 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
711 * Check for leading BOM in UTF-32 and inverted BOM...
715 swap
= *src
== 0xfffe0000;
717 DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap
));
719 if (*src
== 0xfffe0000 || *src
== 0xfeff)
723 * Convert input UTF-32 to output UTF-8...
726 for (i
= maxout
- 1; *src
&& i
> 0;)
731 * Byte swap input UTF-32, if necessary...
732 * (only byte-swapping 24 of 32 bits)
736 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
739 * Check for beyond Plane 16 (invalid UTF-32)...
744 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
750 * Convert UTF-32 character to UTF-8 character(s)...
756 * One-octet UTF-8 <= 127 (US-ASCII)...
759 *dest
++ = (cups_utf8_t
)ch
;
762 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch
, dest
[-1]));
767 * Two-octet UTF-8 <= 2047 (Latin-x)...
772 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
777 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
778 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
781 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch
,
782 dest
[-2], dest
[-1]));
784 else if (ch
< 0x10000)
787 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
792 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
797 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
798 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
799 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
802 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch
,
803 dest
[-3], dest
[-2], dest
[-1]));
808 * Four-octet UTF-8...
813 DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
818 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
819 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
820 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
821 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
824 DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
825 (unsigned)ch
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
831 DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest
- start
)));
833 return ((int)(dest
- start
));
838 * 'compare_wide()' - Compare key for wide (VBCS) match.
842 compare_wide(const void *k1
, /* I - Key char */
843 const void *k2
) /* I - Map char */
845 cups_vbcs_t key
; /* Legacy key character */
846 cups_vbcs_t map
; /* Legacy map character */
849 key
= *((cups_vbcs_t
*)k1
);
850 map
= ((_cups_wide2uni_t
*)k2
)->widechar
;
852 return ((int)(key
- map
));
857 * 'conv_sbcs_to_utf8()' - Convert legacy SBCS to UTF-8.
860 static int /* O - Count or -1 on error */
862 cups_utf8_t
*dest
, /* O - Target string */
863 const cups_sbcs_t
*src
, /* I - Source string */
864 int maxout
, /* I - Max output */
865 const cups_encoding_t encoding
) /* I - Encoding */
867 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
868 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
869 cups_sbcs_t legchar
; /* Legacy character value */
870 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
871 *workptr
; /* Pointer into string */
875 * Find legacy charset map in cache...
878 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
882 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
886 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
891 * Convert ASCII verbatim (optimization)...
895 *workptr
++ = (cups_utf32_t
)legchar
;
899 * Convert unknown character to Replacement Character...
902 crow
= cmap
->char2uni
+ legchar
;
907 *workptr
++ = (cups_utf32_t
)*crow
;
914 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
919 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
924 * 'conv_utf8_to_sbcs()' - Convert UTF-8 to legacy SBCS.
927 static int /* O - Count or -1 on error */
929 cups_sbcs_t
*dest
, /* O - Target string */
930 const cups_utf8_t
*src
, /* I - Source string */
931 int maxout
, /* I - Max output */
932 const cups_encoding_t encoding
) /* I - Encoding */
934 cups_sbcs_t
*start
; /* Start of destination string */
935 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
936 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
937 cups_utf32_t unichar
; /* Character value */
938 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
939 *workptr
; /* Pointer into string */
943 * Find legacy charset map in cache...
946 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
950 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
953 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
957 * Convert internal UCS-4 to SBCS legacy charset (and delete BOM)...
960 for (workptr
= work
, start
= dest
; *workptr
&& maxout
> 0; maxout
--)
962 unichar
= *workptr
++;
967 * Convert ASCII verbatim (optimization)...
972 *dest
++ = (cups_sbcs_t
)unichar
;
977 * Convert unknown character to visible replacement...
980 srow
= cmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
983 srow
+= (int)(unichar
& 0xff);
995 return ((int)(dest
- start
));
1000 * 'conv_utf8_to_vbcs()' - Convert UTF-8 to legacy DBCS/VBCS.
1003 static int /* O - Count or -1 on error */
1005 cups_sbcs_t
*dest
, /* O - Target string */
1006 const cups_utf8_t
*src
, /* I - Source string */
1007 int maxout
, /* I - Max output */
1008 const cups_encoding_t encoding
) /* I - Encoding */
1010 cups_sbcs_t
*start
; /* Start of destination string */
1011 _cups_vmap_t
*vmap
; /* Legacy DBCS / Unicode Charset Map */
1012 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
1013 cups_utf32_t unichar
; /* Character value */
1014 cups_vbcs_t legchar
; /* Legacy character value */
1015 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
1016 *workptr
; /* Pointer into string */
1019 DEBUG_printf(("7conv_utf8_to_vbcs(dest=%p, src=\"%s\", maxout=%d, "
1020 "encoding=%d)", dest
, src
, maxout
, encoding
));
1023 * Find legacy charset map in cache...
1026 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
1028 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (no charmap)");
1034 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
1037 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
1039 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (Unable to convert to UTF-32)");
1045 * Convert internal UCS-4 to VBCS legacy charset (and delete BOM)...
1048 for (start
= dest
, workptr
= work
; *workptr
&& maxout
> 0; maxout
--)
1050 unichar
= *workptr
++;
1053 * Convert ASCII verbatim (optimization)...
1058 *dest
++ = (cups_sbcs_t
)unichar
;
1060 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X", (unsigned)unichar
,
1067 * Convert unknown character to visible replacement...
1070 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
1073 vrow
+= (int)(unichar
& 0xff);
1075 if (!vrow
|| !*vrow
)
1076 legchar
= (cups_vbcs_t
)'?';
1078 legchar
= (cups_vbcs_t
)*vrow
;
1081 * Save n-byte legacy character...
1084 if (legchar
> 0xffffff)
1088 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (out of space)");
1093 *dest
++ = (cups_sbcs_t
)(legchar
>> 24);
1094 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
1095 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
1096 *dest
++ = (cups_sbcs_t
)legchar
;
1100 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X %02X %02X",
1101 (unsigned)unichar
, dest
[-4], dest
[-3], dest
[-2], dest
[-1]));
1103 else if (legchar
> 0xffff)
1107 DEBUG_puts("8conv_utf8_to_vbcs: Returning -1 (out of space)");
1112 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
1113 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
1114 *dest
++ = (cups_sbcs_t
)legchar
;
1118 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X %02X",
1119 (unsigned)unichar
, dest
[-3], dest
[-2], dest
[-1]));
1121 else if (legchar
> 0xff)
1123 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
1124 *dest
++ = (cups_sbcs_t
)legchar
;
1128 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X %02X",
1129 (unsigned)unichar
, dest
[-2], dest
[-1]));
1135 DEBUG_printf(("9conv_utf8_to_vbcs: %08x => %02X",
1136 (unsigned)unichar
, dest
[-1]));
1144 DEBUG_printf(("8conv_utf8_to_vbcs: Returning %d characters",
1145 (int)(dest
- start
)));
1147 return ((int)(dest
- start
));
1152 * 'conv_vbcs_to_utf8()' - Convert legacy DBCS/VBCS to UTF-8.
1155 static int /* O - Count or -1 on error */
1157 cups_utf8_t
*dest
, /* O - Target string */
1158 const cups_sbcs_t
*src
, /* I - Source string */
1159 int maxout
, /* I - Max output */
1160 const cups_encoding_t encoding
) /* I - Encoding */
1162 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
1163 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1164 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
1165 cups_sbcs_t leadchar
; /* Lead char of n-byte legacy char */
1166 cups_vbcs_t legchar
; /* Legacy character value */
1167 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
1168 *workptr
; /* Pointer into string */
1172 * Find legacy charset map in cache...
1175 DEBUG_printf(("7conv_vbcs_to_utf8(dest=%p, src=%p, maxout=%d, encoding=%d)",
1176 dest
, src
, maxout
, encoding
));
1178 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
1180 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (NULL vmap)");
1186 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
1190 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
1193 leadchar
= (cups_sbcs_t
)legchar
;
1196 * Convert ASCII verbatim (optimization)...
1201 *workptr
++ = (cups_utf32_t
)legchar
;
1203 DEBUG_printf(("9conv_vbcs_to_utf8: %02X => %08X", src
[-1],
1204 (unsigned)legchar
));
1209 * Convert 2-byte legacy character...
1212 if (vmap
->lead2char
[(int)leadchar
] == leadchar
)
1216 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string)");
1221 legchar
= (legchar
<< 8) | *src
++;
1224 * Convert unknown character to Replacement Character...
1227 crow
= vmap
->char2uni
[(int)((legchar
>> 8) & 0xff)];
1229 crow
+= (int) (legchar
& 0xff);
1231 if (!crow
|| !*crow
)
1232 *workptr
++ = 0xfffd;
1234 *workptr
++ = (cups_utf32_t
)*crow
;
1236 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X => %08X",
1237 src
[-2], src
[-1], (unsigned)workptr
[-1]));
1242 * Fetch 3-byte or 4-byte legacy character...
1245 if (vmap
->lead3char
[(int)leadchar
] == leadchar
)
1247 if (!*src
|| !src
[1])
1249 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string 2)");
1254 legchar
= (legchar
<< 8) | *src
++;
1255 legchar
= (legchar
<< 8) | *src
++;
1257 else if (vmap
->lead4char
[(int)leadchar
] == leadchar
)
1259 if (!*src
|| !src
[1] || !src
[2])
1261 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (short string 3)");
1266 legchar
= (legchar
<< 8) | *src
++;
1267 legchar
= (legchar
<< 8) | *src
++;
1268 legchar
= (legchar
<< 8) | *src
++;
1272 DEBUG_puts("8conv_vbcs_to_utf8: Returning -1 (bad character)");
1278 * Find 3-byte or 4-byte legacy character...
1281 wide2uni
= (_cups_wide2uni_t
*)bsearch(&legchar
,
1284 sizeof(_cups_wide2uni_t
),
1288 * Convert unknown character to Replacement Character...
1291 if (!wide2uni
|| !wide2uni
->unichar
)
1292 *workptr
++ = 0xfffd;
1294 *workptr
++ = wide2uni
->unichar
;
1296 if (vmap
->lead3char
[(int)leadchar
] == leadchar
)
1297 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X %02X => %08X",
1298 src
[-3], src
[-2], src
[-1], (unsigned)workptr
[-1]));
1300 DEBUG_printf(("9conv_vbcs_to_utf8: %02X %02X %02X %02X => %08X",
1301 src
[-4], src
[-3], src
[-2], src
[-1], (unsigned)workptr
[-1]));
1308 DEBUG_printf(("9conv_vbcs_to_utf8: Converting %d UTF-32 characters to UTF-8",
1309 (int)(workptr
- work
)));
1312 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
1315 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
1320 * 'free_sbcs_charmap()' - Free memory used by a single byte character set.
1324 free_sbcs_charmap(_cups_cmap_t
*cmap
) /* I - Character set */
1326 int i
; /* Looping variable */
1329 for (i
= 0; i
< 256; i
++)
1330 if (cmap
->uni2char
[i
])
1331 free(cmap
->uni2char
[i
]);
1338 * 'free_vbcs_charmap()' - Free memory used by a variable byte character set.
1342 free_vbcs_charmap(_cups_vmap_t
*vmap
) /* I - Character set */
1344 int i
; /* Looping variable */
1347 for (i
= 0; i
< 256; i
++)
1348 if (vmap
->char2uni
[i
])
1349 free(vmap
->char2uni
[i
]);
1351 for (i
= 0; i
< 256; i
++)
1352 if (vmap
->uni2char
[i
])
1353 free(vmap
->uni2char
[i
]);
1356 free(vmap
->wide2uni
);
1363 * 'get_charmap()' - Lookup or get a character set map (private).
1365 * This code handles single-byte (SBCS), double-byte (DBCS), and
1366 * variable-byte (VBCS) character sets _without_ charset escapes...
1367 * This code does not handle multiple-byte character sets (MBCS)
1368 * (such as ISO-2022-JP) with charset switching via escapes...
1372 static void * /* O - Charset map pointer */
1374 const cups_encoding_t encoding
) /* I - Encoding */
1376 char filename
[1024]; /* Filename for charset map file */
1377 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1380 DEBUG_printf(("7get_charmap(encoding=%d)", encoding
));
1383 * Get the data directory and charset map name...
1386 snprintf(filename
, sizeof(filename
), "%s/charmaps/%s.txt",
1387 cg
->cups_datadir
, _cupsEncodingName(encoding
));
1389 DEBUG_printf(("9get_charmap: filename=\"%s\"", filename
));
1392 * Read charset map input file into cache...
1395 if (encoding
< CUPS_ENCODING_SBCS_END
)
1396 return (get_sbcs_charmap(encoding
, filename
));
1397 else if (encoding
< CUPS_ENCODING_VBCS_END
)
1398 return (get_vbcs_charmap(encoding
, filename
));
1405 * 'get_charmap_count()' - Count lines in a charmap file.
1408 static int /* O - Count or -1 on error */
1409 get_charmap_count(cups_file_t
*fp
) /* I - File to read from */
1411 int count
; /* Number of lines */
1412 char line
[256]; /* Line from input map file */
1416 * Count lines in map input file...
1421 while (cupsFileGets(fp
, line
, sizeof(line
)))
1426 * Return the number of lines...
1437 * 'get_sbcs_charmap()' - Get SBCS Charmap.
1440 static _cups_cmap_t
* /* O - Charmap or 0 on error */
1442 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1443 const char *filename
) /* I - Charmap Filename */
1445 unsigned long legchar
; /* Legacy character value */
1446 cups_utf32_t unichar
; /* Unicode character value */
1447 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
1448 cups_file_t
*fp
; /* Charset map file pointer */
1449 char *s
; /* Line parsing pointer */
1450 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1451 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
1452 char line
[256]; /* Line from charset map file */
1456 * See if we already have this SBCS charset map loaded...
1459 DEBUG_printf(("7get_sbcs_charmap(encoding=%d, filename=\"%s\")", encoding
,
1462 for (cmap
= cmap_cache
; cmap
; cmap
= cmap
->next
)
1464 if (cmap
->encoding
== encoding
)
1467 DEBUG_printf(("8get_sbcs_charmap: Returning existing cmap=%p", cmap
));
1469 return ((void *)cmap
);
1474 * Open SBCS charset map input file...
1477 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1479 DEBUG_printf(("8get_sbcs_charmap: Returning NULL (%s)", strerror(errno
)));
1485 * Allocate memory for SBCS charset map...
1488 if ((cmap
= (_cups_cmap_t
*)calloc(1, sizeof(_cups_cmap_t
))) == NULL
)
1491 DEBUG_puts("8get_sbcs_charmap: Returning NULL (Unable to allocate memory)");
1497 cmap
->encoding
= encoding
;
1500 * Save SBCS charset map into memory for transcoding...
1503 while (cupsFileGets(fp
, line
, sizeof(line
)))
1508 legchar
= strtol(line
, &s
, 16);
1509 if (legchar
< 0 || legchar
> 0xff)
1512 unichar
= strtol(s
, NULL
, 16);
1513 if (unichar
< 0 || unichar
> 0x10ffff)
1517 * Save legacy to Unicode mapping in direct lookup table...
1520 crow
= cmap
->char2uni
+ legchar
;
1521 *crow
= (cups_ucs2_t
)(unichar
& 0xffff);
1524 * Save Unicode to legacy mapping in indirect lookup table...
1527 srow
= cmap
->uni2char
[(unichar
>> 8) & 0xff];
1530 srow
= (cups_sbcs_t
*)calloc(256, sizeof(cups_sbcs_t
));
1534 cmap
->uni2char
[(unichar
>> 8) & 0xff] = srow
;
1537 srow
+= unichar
& 0xff;
1540 * Convert Replacement Character to visible replacement...
1543 if (unichar
== 0xfffd)
1544 legchar
= (unsigned long)'?';
1547 * First (oldest) legacy character uses Unicode mapping cell...
1551 *srow
= (cups_sbcs_t
)legchar
;
1557 * Add it to the cache and return...
1560 cmap
->next
= cmap_cache
;
1563 DEBUG_printf(("8get_sbcs_charmap: Returning new cmap=%p", cmap
));
1568 * If we get here, there was an error in the cmap file...
1573 free_sbcs_charmap(cmap
);
1577 DEBUG_puts("8get_sbcs_charmap: Returning NULL (Read/format error)");
1584 * 'get_vbcs_charmap()' - Get DBCS/VBCS Charmap.
1587 static _cups_vmap_t
* /* O - Charmap or 0 on error */
1589 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1590 const char *filename
) /* I - Charmap Filename */
1592 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
1593 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1594 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
1595 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
1596 cups_sbcs_t leadchar
; /* Lead char of 2-byte legacy char */
1597 unsigned long legchar
; /* Legacy character value */
1598 cups_utf32_t unichar
; /* Unicode character value */
1599 int mapcount
; /* Count of lines in charmap file */
1600 cups_file_t
*fp
; /* Charset map file pointer */
1601 char *s
; /* Line parsing pointer */
1602 char line
[256]; /* Line from charset map file */
1603 int i
; /* Loop variable */
1604 int legacy
; /* 32-bit legacy char */
1607 DEBUG_printf(("7get_vbcs_charmap(encoding=%d, filename=\"%s\")\n",
1608 encoding
, filename
));
1611 * See if we already have this DBCS/VBCS charset map loaded...
1614 for (vmap
= vmap_cache
; vmap
; vmap
= vmap
->next
)
1616 if (vmap
->encoding
== encoding
)
1619 DEBUG_printf(("8get_vbcs_charmap: Returning existing vmap=%p", vmap
));
1621 return ((void *)vmap
);
1626 * Open VBCS charset map input file...
1629 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1631 DEBUG_printf(("8get_vbcs_charmap: Returning NULL (%s)", strerror(errno
)));
1637 * Count lines in charmap file...
1640 if ((mapcount
= get_charmap_count(fp
)) <= 0)
1642 DEBUG_puts("8get_vbcs_charmap: Unable to get charmap count!");
1649 DEBUG_printf(("8get_vbcs_charmap: mapcount=%d", mapcount
));
1652 * Allocate memory for DBCS/VBCS charset map...
1655 if ((vmap
= (_cups_vmap_t
*)calloc(1, sizeof(_cups_vmap_t
))) == NULL
)
1657 DEBUG_puts("8get_vbcs_charmap: Unable to allocate memory!");
1665 vmap
->encoding
= encoding
;
1668 * Save DBCS/VBCS charset map into memory for transcoding...
1678 while (cupsFileGets(fp
, line
, sizeof(line
)))
1683 legchar
= strtoul(line
, &s
, 16);
1684 if (legchar
== ULONG_MAX
)
1687 unichar
= strtol(s
, NULL
, 16);
1688 if (unichar
< 0 || unichar
> 0x10ffff)
1693 DEBUG_printf(("9get_vbcs_charmap: i=%d, legchar=0x%08lx, unichar=0x%04x", i
,
1694 legchar
, (unsigned)unichar
));
1697 * Save lead char of 2/3/4-byte legacy char...
1700 if (legchar
> 0xffffff)
1702 leadchar
= (cups_sbcs_t
)(legchar
>> 24);
1703 vmap
->lead4char
[leadchar
] = leadchar
;
1705 else if (legchar
> 0xffff)
1707 leadchar
= (cups_sbcs_t
)(legchar
>> 16);
1708 vmap
->lead3char
[leadchar
] = leadchar
;
1712 leadchar
= (cups_sbcs_t
)(legchar
>> 8);
1713 vmap
->lead2char
[leadchar
] = leadchar
;
1717 * Save Legacy to Unicode mapping...
1720 if (legchar
<= 0xffff)
1723 * Save DBCS 16-bit to Unicode mapping in indirect lookup table...
1726 crow
= vmap
->char2uni
[(int)leadchar
];
1729 crow
= (cups_ucs2_t
*)calloc(256, sizeof(cups_ucs2_t
));
1733 vmap
->char2uni
[(int)leadchar
] = crow
;
1736 crow
[(int)(legchar
& 0xff)] = (cups_ucs2_t
)unichar
;
1741 * Save VBCS 32-bit to Unicode mapping in sorted list table...
1747 vmap
->widecount
= (mapcount
- i
+ 1);
1748 wide2uni
= (_cups_wide2uni_t
*)calloc(vmap
->widecount
,
1749 sizeof(_cups_wide2uni_t
));
1753 vmap
->wide2uni
= wide2uni
;
1756 wide2uni
->widechar
= (cups_vbcs_t
)legchar
;
1757 wide2uni
->unichar
= (cups_ucs2_t
)unichar
;
1762 * Save Unicode to legacy mapping in indirect lookup table...
1765 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
1768 vrow
= (cups_vbcs_t
*)calloc(256, sizeof(cups_vbcs_t
));
1772 vmap
->uni2char
[(int) ((unichar
>> 8) & 0xff)] = vrow
;
1775 vrow
+= (int)(unichar
& 0xff);
1778 * Convert Replacement Character to visible replacement...
1781 if (unichar
== 0xfffd)
1782 legchar
= (unsigned long)'?';
1785 * First (oldest) legacy character uses Unicode mapping cell...
1789 *vrow
= (cups_vbcs_t
)legchar
;
1792 vmap
->charcount
= (i
- vmap
->widecount
);
1797 * Add it to the cache and return...
1800 vmap
->next
= vmap_cache
;
1803 DEBUG_printf(("8get_vbcs_charmap: Returning new vmap=%p", vmap
));
1808 * If we get here, the file contains errors...
1813 free_vbcs_charmap(vmap
);
1817 DEBUG_puts("8get_vbcs_charmap: Returning NULL (Read/format error)");
1824 * End of "$Id: transcode.c 7560 2008-05-13 06:34:04Z mike $"