2 * "$Id: transcode.c 5373 2006-04-06 20:03:32Z mike $"
4 * Transcoding support for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are
9 * the property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the
11 * file "LICENSE.txt" which should have been included with this file.
12 * If this file is missing or damaged please contact Easy Software
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * _cupsCharmapFlush() - Flush all character set maps out of cache.
27 * _cupsCharmapFree() - Free a character set map.
28 * _cupsCharmapGet() - Get a character set map.
29 * cupsCharsetToUTF8() - Convert legacy character set to UTF-8.
30 * cupsUTF8ToCharset() - Convert UTF-8 to legacy character set.
31 * cupsUTF8ToUTF32() - Convert UTF-8 to UTF-32.
32 * cupsUTF32ToUTF8() - Convert UTF-32 to UTF-8.
33 * compare_wide() - Compare key for wide (VBCS) match.
34 * conv_sbcs_to_utf8() - Convert legacy SBCS to UTF-8.
35 * conv_utf8_to_sbcs() - Convert UTF-8 to legacy SBCS.
36 * conv_utf8_to_vbcs() - Convert UTF-8 to legacy DBCS/VBCS.
37 * conv_vbcs_to_utf8() - Convert legacy DBCS/VBCS to UTF-8.
38 * free_sbcs_charmap() - Free memory used by a single byte character set.
39 * free_vbcs_charmap() - Free memory used by a variable byte character set.
40 * get_charmap() - Lookup or get a character set map (private).
41 * get_charmap_count() - Count lines in a charmap file.
42 * get_sbcs_charmap() - Get SBCS Charmap.
43 * get_vbcs_charmap() - Get DBCS/VBCS Charmap.
47 * Include necessary headers...
63 static pthread_mutex_t map_mutex
= PTHREAD_MUTEX_INITIALIZER
;
64 /* Mutex to control access to maps */
65 #endif /* HAVE_PTHREAD_H */
66 static _cups_cmap_t
*cmap_cache
= NULL
;
67 /* SBCS Charmap Cache */
68 static _cups_vmap_t
*vmap_cache
= NULL
;
69 /* VBCS Charmap Cache */
76 static int compare_wide(const void *k1
, const void *k2
);
77 static int conv_sbcs_to_utf8(cups_utf8_t
*dest
,
78 const cups_sbcs_t
*src
,
80 const cups_encoding_t encoding
);
81 static int conv_utf8_to_sbcs(cups_sbcs_t
*dest
,
82 const cups_utf8_t
*src
,
84 const cups_encoding_t encoding
);
85 static int conv_utf8_to_vbcs(cups_sbcs_t
*dest
,
86 const cups_utf8_t
*src
,
88 const cups_encoding_t encoding
);
89 static int conv_vbcs_to_utf8(cups_utf8_t
*dest
,
90 const cups_sbcs_t
*src
,
92 const cups_encoding_t encoding
);
93 static void free_sbcs_charmap(_cups_cmap_t
*sbcs
);
94 static void free_vbcs_charmap(_cups_vmap_t
*vbcs
);
95 static void *get_charmap(const cups_encoding_t encoding
);
96 static int get_charmap_count(cups_file_t
*fp
);
97 static _cups_cmap_t
*get_sbcs_charmap(const cups_encoding_t encoding
,
98 const char *filename
);
99 static _cups_vmap_t
*get_vbcs_charmap(const cups_encoding_t encoding
,
100 const char *filename
);
104 * '_cupsCharmapFlush()' - Flush all character set maps out of cache.
108 _cupsCharmapFlush(void)
110 _cups_cmap_t
*cmap
, /* Legacy SBCS / Unicode Charset Map */
111 *cnext
; /* Next Legacy SBCS Charset Map */
112 _cups_vmap_t
*vmap
, /* Legacy VBCS / Unicode Charset Map */
113 *vnext
; /* Next Legacy VBCS Charset Map */
116 #ifdef HAVE_PTHREAD_H
117 pthread_mutex_lock(&map_mutex
);
118 #endif /* HAVE_PTHREAD_H */
121 * Loop through SBCS charset map cache, free all memory...
124 for (cmap
= cmap_cache
; cmap
; cmap
= cnext
)
128 free_sbcs_charmap(cmap
);
134 * Loop through DBCS/VBCS charset map cache, free all memory...
137 for (vmap
= vmap_cache
; vmap
; vmap
= vnext
)
141 free_vbcs_charmap(vmap
);
148 #ifdef HAVE_PTHREAD_H
149 pthread_mutex_unlock(&map_mutex
);
150 #endif /* HAVE_PTHREAD_H */
155 * '_cupsCharmapFree()' - Free a character set map.
157 * This does not actually free; use '_cupsCharmapFlush()' for that.
162 const cups_encoding_t encoding
) /* I - Encoding */
164 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
165 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
169 * See if we already have this SBCS charset map loaded...
172 #ifdef HAVE_PTHREAD_H
173 pthread_mutex_lock(&map_mutex
);
174 #endif /* HAVE_PTHREAD_H */
176 for (cmap
= cmap_cache
; cmap
; cmap
= cmap
->next
)
178 if (cmap
->encoding
== encoding
)
187 * See if we already have this DBCS/VBCS charset map loaded...
190 for (vmap
= vmap_cache
; vmap
; vmap
= vmap
->next
)
192 if (vmap
->encoding
== encoding
)
200 #ifdef HAVE_PTHREAD_H
201 pthread_mutex_unlock(&map_mutex
);
202 #endif /* HAVE_PTHREAD_H */
207 * '_cupsCharmapGet()' - Get a character set map.
209 * This code handles single-byte (SBCS), double-byte (DBCS), and
210 * variable-byte (VBCS) character sets _without_ charset escapes...
211 * This code does not handle multiple-byte character sets (MBCS)
212 * (such as ISO-2022-JP) with charset switching via escapes...
215 void * /* O - Charset map pointer */
217 const cups_encoding_t encoding
) /* I - Encoding */
219 void *charmap
; /* Charset map pointer */
222 DEBUG_printf(("_cupsCharmapGet(encoding=%d)\n", encoding
));
225 * Check for valid arguments...
228 if (encoding
< 0 || encoding
>= CUPS_ENCODING_VBCS_END
)
230 DEBUG_puts(" Bad encoding, returning NULL!");
235 * Lookup or get the charset map pointer and return...
238 #ifdef HAVE_PTHREAD_H
239 pthread_mutex_lock(&map_mutex
);
240 #endif /* HAVE_PTHREAD_H */
242 charmap
= get_charmap(encoding
);
244 #ifdef HAVE_PTHREAD_H
245 pthread_mutex_unlock(&map_mutex
);
246 #endif /* HAVE_PTHREAD_H */
253 * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
255 * This code handles single-byte (SBCS), double-byte (DBCS), and
256 * variable-byte (VBCS) character sets _without_ charset escapes...
257 * This code does not handle multiple-byte character sets (MBCS)
258 * (such as ISO-2022-JP) with charset switching via escapes...
261 int /* O - Count or -1 on error */
263 cups_utf8_t
*dest
, /* O - Target string */
264 const char *src
, /* I - Source string */
265 const int maxout
, /* I - Max output */
266 const cups_encoding_t encoding
) /* I - Encoding */
268 int bytes
; /* Number of bytes converted */
272 * Check for valid arguments...
275 DEBUG_printf(("cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)\n",
276 dest
, src
, maxout
, encoding
));
281 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
283 DEBUG_puts(" Bad arguments, returning -1");
288 * Handle identity conversions...
291 if (encoding
== CUPS_UTF8
||
292 encoding
< 0 || encoding
>= CUPS_ENCODING_VBCS_END
)
294 strlcpy((char *)dest
, src
, maxout
);
295 return (strlen((char *)dest
));
299 * Convert input legacy charset to UTF-8...
302 #ifdef HAVE_PTHREAD_H
303 pthread_mutex_lock(&map_mutex
);
304 #endif /* HAVE_PTHREAD_H */
306 if (encoding
< CUPS_ENCODING_SBCS_END
)
307 bytes
= conv_sbcs_to_utf8(dest
, (cups_sbcs_t
*)src
, maxout
, encoding
);
308 else if (encoding
< CUPS_ENCODING_VBCS_END
)
309 bytes
= conv_vbcs_to_utf8(dest
, (cups_sbcs_t
*)src
, maxout
, encoding
);
312 DEBUG_puts(" Bad encoding, returning -1");
316 #ifdef HAVE_PTHREAD_H
317 pthread_mutex_unlock(&map_mutex
);
318 #endif /* HAVE_PTHREAD_H */
325 * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
327 * This code handles single-byte (SBCS), double-byte (DBCS), and
328 * variable-byte (VBCS) character sets _without_ charset escapes...
329 * This code does not handle multiple-byte character sets (MBCS)
330 * (such as ISO-2022-JP) with charset switching via escapes...
333 int /* O - Count or -1 on error */
335 char *dest
, /* O - Target string */
336 const cups_utf8_t
*src
, /* I - Source string */
337 const int maxout
, /* I - Max output */
338 const cups_encoding_t encoding
) /* I - Encoding */
340 int bytes
; /* Number of bytes converted */
344 * Check for valid arguments...
347 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
356 * Handle identity conversions...
359 if (encoding
== CUPS_UTF8
||
360 encoding
< 0 || encoding
>= CUPS_ENCODING_VBCS_END
)
362 strlcpy(dest
, (char *)src
, maxout
);
363 return (strlen(dest
));
367 * Convert input UTF-8 to legacy charset...
370 #ifdef HAVE_PTHREAD_H
371 pthread_mutex_lock(&map_mutex
);
372 #endif /* HAVE_PTHREAD_H */
374 if (encoding
< CUPS_ENCODING_SBCS_END
)
375 bytes
= conv_utf8_to_sbcs((cups_sbcs_t
*)dest
, src
, maxout
, encoding
);
376 else if (encoding
< CUPS_ENCODING_VBCS_END
)
377 bytes
= conv_utf8_to_vbcs((cups_sbcs_t
*)dest
, src
, maxout
, encoding
);
381 #ifdef HAVE_PTHREAD_H
382 pthread_mutex_unlock(&map_mutex
);
383 #endif /* HAVE_PTHREAD_H */
390 * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
392 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
394 * UTF-32 char UTF-8 char(s)
395 * --------------------------------------------------
396 * 0 to 127 = 0xxxxxxx (US-ASCII)
397 * 128 to 2047 = 110xxxxx 10yyyyyy
398 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
399 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
401 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
402 * which would convert to five- or six-octet UTF-8 sequences...
405 int /* O - Count or -1 on error */
407 cups_utf32_t
*dest
, /* O - Target string */
408 const cups_utf8_t
*src
, /* I - Source string */
409 const int maxout
) /* I - Max output */
411 size_t srclen
; /* Source string length */
412 int i
; /* Looping variable */
413 cups_utf8_t ch
; /* Character value */
414 cups_utf8_t next
; /* Next character value */
415 cups_utf32_t ch32
; /* UTF-32 character value */
419 * Check for valid arguments and clear output...
425 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
429 * Convert input UTF-8 to output UTF-32 (and insert BOM)...
433 srclen
= strlen((char *)src
);
435 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
440 * Convert UTF-8 character(s) to UTF-32 character...
446 * One-octet UTF-8 <= 127 (US-ASCII)...
451 else if ((ch
& 0xe0) == 0xc0)
454 * Two-octet UTF-8 <= 2047 (Latin-x)...
461 ch32
= ((ch
& 0x1f) << 6) | (next
& 0x3f);
464 * Check for non-shortest form (invalid UTF-8)...
472 else if ((ch
& 0xf0) == 0xe0)
475 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
482 ch32
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
488 ch32
= (ch32
<< 6) | (next
& 0x3f);
491 * Check for non-shortest form (invalid UTF-8)...
499 else if ((ch
& 0xf8) == 0xf0)
502 * Four-octet UTF-8...
509 ch32
= ((ch
& 0x07) << 6) | (next
& 0x3f);
515 ch32
= (ch32
<< 6) | (next
& 0x3f);
521 ch32
= (ch32
<< 6) | (next
& 0x3f);
524 * Check for non-shortest form (invalid UTF-8)...
535 * More than 4-octet (invalid UTF-8 sequence)...
542 * Check for UTF-16 surrogate (illegal UTF-8)...
545 if (*dest
>= 0xd800 && *dest
<= 0xdfff)
556 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
558 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
560 * UTF-32 char UTF-8 char(s)
561 * --------------------------------------------------
562 * 0 to 127 = 0xxxxxxx (US-ASCII)
563 * 128 to 2047 = 110xxxxx 10yyyyyy
564 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
565 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
567 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
568 * which would convert to five- or six-octet UTF-8 sequences...
571 int /* O - Count or -1 on error */
573 cups_utf8_t
*dest
, /* O - Target string */
574 const cups_utf32_t
*src
, /* I - Source string */
575 const int maxout
) /* I - Max output */
577 cups_utf8_t
*start
; /* Start of destination string */
578 int i
; /* Looping variable */
579 int swap
; /* Byte-swap input to output */
580 cups_utf32_t ch
; /* Character value */
584 * Check for valid arguments and clear output...
590 if (!dest
|| !src
|| maxout
< 1)
594 * Check for leading BOM in UTF-32 and inverted BOM...
598 swap
= *src
== 0xfffe0000;
600 if (*src
== 0xfffe0000 || *src
== 0xfeff)
604 * Convert input UTF-32 to output UTF-8...
607 for (i
= maxout
- 1; *src
&& i
> 0;)
612 * Byte swap input UTF-32, if necessary...
613 * (only byte-swapping 24 of 32 bits)
617 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
620 * Check for beyond Plane 16 (invalid UTF-32)...
627 * Convert UTF-32 character to UTF-8 character(s)...
633 * One-octet UTF-8 <= 127 (US-ASCII)...
636 *dest
++ = (cups_utf8_t
)ch
;
642 * Two-octet UTF-8 <= 2047 (Latin-x)...
648 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
649 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
652 else if (ch
< 0x10000)
655 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
661 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
662 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
663 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
669 * Four-octet UTF-8...
675 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
676 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
677 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
678 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
685 return ((int)(dest
- start
));
690 * 'compare_wide()' - Compare key for wide (VBCS) match.
694 compare_wide(const void *k1
, /* I - Key char */
695 const void *k2
) /* I - Map char */
697 cups_vbcs_t key
; /* Legacy key character */
698 cups_vbcs_t map
; /* Legacy map character */
701 key
= *((cups_vbcs_t
*)k1
);
702 map
= ((_cups_wide2uni_t
*)k2
)->widechar
;
704 return ((int)(key
- map
));
709 * 'conv_sbcs_to_utf8()' - Convert legacy SBCS to UTF-8.
712 static int /* O - Count or -1 on error */
714 cups_utf8_t
*dest
, /* O - Target string */
715 const cups_sbcs_t
*src
, /* I - Source string */
716 int maxout
, /* I - Max output */
717 const cups_encoding_t encoding
) /* I - Encoding */
719 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
720 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
721 cups_sbcs_t legchar
; /* Legacy character value */
722 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
723 *workptr
; /* Pointer into string */
727 * Find legacy charset map in cache...
730 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
734 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
738 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
743 * Convert ASCII verbatim (optimization)...
747 *workptr
++ = (cups_utf32_t
)legchar
;
751 * Convert unknown character to Replacement Character...
754 crow
= cmap
->char2uni
+ legchar
;
759 *workptr
++ = (cups_utf32_t
)*crow
;
766 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
771 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
776 * 'conv_utf8_to_sbcs()' - Convert UTF-8 to legacy SBCS.
779 static int /* O - Count or -1 on error */
781 cups_sbcs_t
*dest
, /* O - Target string */
782 const cups_utf8_t
*src
, /* I - Source string */
783 int maxout
, /* I - Max output */
784 const cups_encoding_t encoding
) /* I - Encoding */
786 cups_sbcs_t
*start
; /* Start of destination string */
787 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
788 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
789 cups_utf32_t unichar
; /* Character value */
790 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
791 *workptr
; /* Pointer into string */
795 * Find legacy charset map in cache...
798 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
802 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
805 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
809 * Convert internal UCS-4 to SBCS legacy charset (and delete BOM)...
812 for (workptr
= work
+ 1, start
= dest
; *workptr
&& maxout
> 1; maxout
--)
814 unichar
= *workptr
++;
819 * Convert ASCII verbatim (optimization)...
824 *dest
++ = (cups_sbcs_t
)unichar
;
829 * Convert unknown character to visible replacement...
832 srow
= cmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
835 srow
+= (int)(unichar
& 0xff);
847 return ((int)(dest
- start
));
852 * 'conv_utf8_to_vbcs()' - Convert UTF-8 to legacy DBCS/VBCS.
855 static int /* O - Count or -1 on error */
857 cups_sbcs_t
*dest
, /* O - Target string */
858 const cups_utf8_t
*src
, /* I - Source string */
859 int maxout
, /* I - Max output */
860 const cups_encoding_t encoding
) /* I - Encoding */
862 cups_sbcs_t
*start
; /* Start of destination string */
863 _cups_vmap_t
*vmap
; /* Legacy DBCS / Unicode Charset Map */
864 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
865 cups_utf32_t unichar
; /* Character value */
866 cups_vbcs_t legchar
; /* Legacy character value */
867 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
868 *workptr
; /* Pointer into string */
872 * Find legacy charset map in cache...
875 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
879 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
882 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
886 * Convert internal UCS-4 to VBCS legacy charset (and delete BOM)...
889 for (start
= dest
, workptr
= work
+ 1; *workptr
&& maxout
> 1; maxout
--)
891 unichar
= *workptr
++;
896 * Convert ASCII verbatim (optimization)...
901 *dest
++ = (cups_vbcs_t
)unichar
;
906 * Convert unknown character to visible replacement...
909 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
912 vrow
+= (int)(unichar
& 0xff);
915 legchar
= (cups_vbcs_t
)'?';
917 legchar
= (cups_vbcs_t
)*vrow
;
920 * Save n-byte legacy character...
923 if (legchar
> 0xffffff)
928 *dest
++ = (cups_sbcs_t
)(legchar
>> 24);
929 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
930 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
931 *dest
++ = (cups_sbcs_t
)legchar
;
935 else if (legchar
> 0xffff)
940 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
941 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
942 *dest
++ = (cups_sbcs_t
)legchar
;
946 else if (legchar
> 0xff)
948 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
949 *dest
++ = (cups_sbcs_t
)legchar
;
959 return ((int)(dest
- start
));
964 * 'conv_vbcs_to_utf8()' - Convert legacy DBCS/VBCS to UTF-8.
967 static int /* O - Count or -1 on error */
969 cups_utf8_t
*dest
, /* O - Target string */
970 const cups_sbcs_t
*src
, /* I - Source string */
971 int maxout
, /* I - Max output */
972 const cups_encoding_t encoding
) /* I - Encoding */
974 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
975 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
976 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
977 cups_sbcs_t leadchar
; /* Lead char of n-byte legacy char */
978 cups_vbcs_t legchar
; /* Legacy character value */
979 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
980 *workptr
; /* Pointer into string */
984 * Find legacy charset map in cache...
987 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
991 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
995 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
998 leadchar
= (cups_sbcs_t
)legchar
;
1001 * Convert ASCII verbatim (optimization)...
1006 *workptr
++ = (cups_utf32_t
)legchar
;
1011 * Convert 2-byte legacy character...
1014 if (vmap
->lead2char
[(int)leadchar
] == leadchar
)
1019 legchar
= (legchar
<< 8) | *src
++;
1022 * Convert unknown character to Replacement Character...
1025 crow
= vmap
->char2uni
[(int)((legchar
>> 8) & 0xff)];
1027 crow
+= (int) (legchar
& 0xff);
1029 if (!crow
|| !*crow
)
1030 *workptr
++ = 0xfffd;
1032 *workptr
++ = (cups_utf32_t
)*crow
;
1037 * Fetch 3-byte or 4-byte legacy character...
1040 if (vmap
->lead3char
[(int)leadchar
] == leadchar
)
1042 if (!*src
|| !src
[1])
1045 legchar
= (legchar
<< 8) | *src
++;
1046 legchar
= (legchar
<< 8) | *src
++;
1048 else if (vmap
->lead4char
[(int)leadchar
] == leadchar
)
1050 if (!*src
|| !src
[1] || !src
[2])
1053 legchar
= (legchar
<< 8) | *src
++;
1054 legchar
= (legchar
<< 8) | *src
++;
1055 legchar
= (legchar
<< 8) | *src
++;
1061 * Find 3-byte or 4-byte legacy character...
1064 wide2uni
= (_cups_wide2uni_t
*)bsearch(&legchar
,
1067 sizeof(_cups_wide2uni_t
),
1071 * Convert unknown character to Replacement Character...
1074 if (!wide2uni
|| !wide2uni
->unichar
)
1075 *workptr
++ = 0xfffd;
1077 *workptr
++ = wide2uni
->unichar
;
1085 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
1088 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
1093 * 'free_sbcs_charmap()' - Free memory used by a single byte character set.
1097 free_sbcs_charmap(_cups_cmap_t
*cmap
) /* I - Character set */
1099 int i
; /* Looping variable */
1102 for (i
= 0; i
< 256; i
++)
1103 if (cmap
->uni2char
[i
])
1104 free(cmap
->uni2char
[i
]);
1111 * 'free_vbcs_charmap()' - Free memory used by a variable byte character set.
1115 free_vbcs_charmap(_cups_vmap_t
*vmap
) /* I - Character set */
1117 int i
; /* Looping variable */
1120 for (i
= 0; i
< 256; i
++)
1121 if (vmap
->char2uni
[i
])
1122 free(vmap
->char2uni
[i
]);
1124 for (i
= 0; i
< 256; i
++)
1125 if (vmap
->uni2char
[i
])
1126 free(vmap
->uni2char
[i
]);
1129 free(vmap
->wide2uni
);
1136 * 'get_charmap()' - Lookup or get a character set map (private).
1138 * This code handles single-byte (SBCS), double-byte (DBCS), and
1139 * variable-byte (VBCS) character sets _without_ charset escapes...
1140 * This code does not handle multiple-byte character sets (MBCS)
1141 * (such as ISO-2022-JP) with charset switching via escapes...
1145 void * /* O - Charset map pointer */
1147 const cups_encoding_t encoding
) /* I - Encoding */
1149 char filename
[1024]; /* Filename for charset map file */
1150 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1154 * Get the data directory and charset map name...
1157 snprintf(filename
, sizeof(filename
), "%s/charmaps/%s.txt",
1158 cg
->cups_datadir
, _cupsEncodingName(encoding
));
1160 DEBUG_printf((" filename=\"%s\"\n", filename
));
1163 * Read charset map input file into cache...
1166 if (encoding
< CUPS_ENCODING_SBCS_END
)
1167 return (get_sbcs_charmap(encoding
, filename
));
1168 else if (encoding
< CUPS_ENCODING_VBCS_END
)
1169 return (get_vbcs_charmap(encoding
, filename
));
1176 * 'get_charmap_count()' - Count lines in a charmap file.
1179 static int /* O - Count or -1 on error */
1180 get_charmap_count(cups_file_t
*fp
) /* I - File to read from */
1182 int count
; /* Number of lines */
1183 char line
[256]; /* Line from input map file */
1187 * Count lines in map input file...
1192 while (cupsFileGets(fp
, line
, sizeof(line
)))
1197 * Return the number of lines...
1208 * 'get_sbcs_charmap()' - Get SBCS Charmap.
1211 static _cups_cmap_t
* /* O - Charmap or 0 on error */
1213 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1214 const char *filename
) /* I - Charmap Filename */
1216 unsigned long legchar
; /* Legacy character value */
1217 cups_utf32_t unichar
; /* Unicode character value */
1218 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
1219 cups_file_t
*fp
; /* Charset map file pointer */
1220 char *s
; /* Line parsing pointer */
1221 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1222 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
1223 char line
[256]; /* Line from charset map file */
1227 * See if we already have this SBCS charset map loaded...
1230 for (cmap
= cmap_cache
; cmap
; cmap
= cmap
->next
)
1232 if (cmap
->encoding
== encoding
)
1235 DEBUG_printf((" returning existing cmap=%p\n", cmap
));
1237 return ((void *)cmap
);
1242 * Open SBCS charset map input file...
1245 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1249 * Allocate memory for SBCS charset map...
1252 if ((cmap
= (_cups_cmap_t
*)calloc(1, sizeof(_cups_cmap_t
))) == NULL
)
1255 DEBUG_puts(" Unable to allocate memory!");
1261 cmap
->encoding
= encoding
;
1264 * Save SBCS charset map into memory for transcoding...
1267 while (cupsFileGets(fp
, line
, sizeof(line
)))
1272 legchar
= strtol(line
, &s
, 16);
1273 if (legchar
< 0 || legchar
> 0xff)
1276 unichar
= strtol(s
, NULL
, 16);
1277 if (unichar
< 0 || unichar
> 0xffff)
1281 * Save legacy to Unicode mapping in direct lookup table...
1284 crow
= cmap
->char2uni
+ legchar
;
1285 *crow
= (cups_ucs2_t
)(unichar
& 0xffff);
1288 * Save Unicode to legacy mapping in indirect lookup table...
1291 srow
= cmap
->uni2char
[(unichar
>> 8) & 0xff];
1294 srow
= (cups_sbcs_t
*)calloc(256, sizeof(cups_sbcs_t
));
1298 cmap
->uni2char
[(unichar
>> 8) & 0xff] = srow
;
1301 srow
+= unichar
& 0xff;
1304 * Convert Replacement Character to visible replacement...
1307 if (unichar
== 0xfffd)
1308 legchar
= (unsigned long)'?';
1311 * First (oldest) legacy character uses Unicode mapping cell...
1315 *srow
= (cups_sbcs_t
)legchar
;
1321 * Add it to the cache and return...
1324 cmap
->next
= cmap_cache
;
1327 DEBUG_printf((" returning new cmap=%p\n", cmap
));
1332 * If we get here, there was an error in the cmap file...
1337 free_sbcs_charmap(cmap
);
1341 DEBUG_puts(" Error, returning NULL!");
1348 * 'get_vbcs_charmap()' - Get DBCS/VBCS Charmap.
1351 static _cups_vmap_t
* /* O - Charmap or 0 on error */
1353 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1354 const char *filename
) /* I - Charmap Filename */
1356 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
1357 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1358 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
1359 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
1360 cups_sbcs_t leadchar
; /* Lead char of 2-byte legacy char */
1361 unsigned long legchar
; /* Legacy character value */
1362 cups_utf32_t unichar
; /* Unicode character value */
1363 int mapcount
; /* Count of lines in charmap file */
1364 cups_file_t
*fp
; /* Charset map file pointer */
1365 char *s
; /* Line parsing pointer */
1366 char line
[256]; /* Line from charset map file */
1367 int i
; /* Loop variable */
1368 int wide
; /* 32-bit legacy char */
1371 DEBUG_printf(("get_vbcs_charmap(encoding=%d, filename=\"%s\")\n",
1372 encoding
, filename
));
1375 * See if we already have this DBCS/VBCS charset map loaded...
1378 for (vmap
= vmap_cache
; vmap
; vmap
= vmap
->next
)
1380 if (vmap
->encoding
== encoding
)
1383 DEBUG_printf((" returning existing vmap=%p\n", vmap
));
1385 return ((void *)vmap
);
1390 * Open VBCS charset map input file...
1393 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1395 DEBUG_printf((" Unable to open file: %s\n", strerror(errno
)));
1401 * Count lines in charmap file...
1404 if ((mapcount
= get_charmap_count(fp
)) <= 0)
1406 DEBUG_puts(" Unable to get charmap count!");
1411 DEBUG_printf((" mapcount=%d\n", mapcount
));
1414 * Allocate memory for DBCS/VBCS charset map...
1417 if ((vmap
= (_cups_vmap_t
*)calloc(1, sizeof(_cups_vmap_t
))) == NULL
)
1420 DEBUG_puts(" Unable to allocate memory!");
1426 vmap
->encoding
= encoding
;
1429 * Save DBCS/VBCS charset map into memory for transcoding...
1440 while (cupsFileGets(fp
, line
, sizeof(line
)))
1445 legchar
= strtoul(line
, &s
, 16);
1446 if (legchar
== ULONG_MAX
)
1449 unichar
= strtol(s
, NULL
, 16);
1450 if (unichar
< 0 || unichar
> 0xffff)
1455 /* DEBUG_printf((" i=%d, legchar=0x%08lx, unichar=0x%04x\n", i,
1456 legchar, (unsigned)unichar)); */
1459 * Save lead char of 2/3/4-byte legacy char...
1462 if (legchar
> 0xff && legchar
<= 0xffff)
1464 leadchar
= (cups_sbcs_t
)(legchar
>> 8);
1465 vmap
->lead2char
[leadchar
] = leadchar
;
1468 if (legchar
> 0xffff && legchar
<= 0xffffff)
1470 leadchar
= (cups_sbcs_t
)(legchar
>> 16);
1471 vmap
->lead3char
[leadchar
] = leadchar
;
1474 if (legchar
> 0xffffff)
1476 leadchar
= (cups_sbcs_t
)(legchar
>> 24);
1477 vmap
->lead4char
[leadchar
] = leadchar
;
1481 * Save Legacy to Unicode mapping...
1484 if (legchar
<= 0xffff)
1487 * Save DBCS 16-bit to Unicode mapping in indirect lookup table...
1490 crow
= vmap
->char2uni
[(int)leadchar
];
1493 crow
= (cups_ucs2_t
*)calloc(256, sizeof(cups_ucs2_t
));
1497 vmap
->char2uni
[(int)leadchar
] = crow
;
1500 crow
[(int)(legchar
& 0xff)] = (cups_ucs2_t
)unichar
;
1505 * Save VBCS 32-bit to Unicode mapping in sorted list table...
1511 vmap
->widecount
= (mapcount
- i
+ 1);
1512 wide2uni
= (_cups_wide2uni_t
*)calloc(vmap
->widecount
,
1513 sizeof(_cups_wide2uni_t
));
1517 vmap
->wide2uni
= wide2uni
;
1520 wide2uni
->widechar
= (cups_vbcs_t
)legchar
;
1521 wide2uni
->unichar
= (cups_ucs2_t
)unichar
;
1526 * Save Unicode to legacy mapping in indirect lookup table...
1529 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
1532 vrow
= (cups_vbcs_t
*)calloc(256, sizeof(cups_vbcs_t
));
1536 vmap
->uni2char
[(int) ((unichar
>> 8) & 0xff)] = vrow
;
1539 vrow
+= (int)(unichar
& 0xff);
1542 * Convert Replacement Character to visible replacement...
1545 if (unichar
== 0xfffd)
1546 legchar
= (unsigned long)'?';
1549 * First (oldest) legacy character uses Unicode mapping cell...
1553 *vrow
= (cups_vbcs_t
)legchar
;
1556 vmap
->charcount
= (i
- vmap
->widecount
);
1561 * Add it to the cache and return...
1564 vmap
->next
= vmap_cache
;
1567 DEBUG_printf((" returning new vmap=%p\n", vmap
));
1572 * If we get here, the file contains errors...
1577 free_vbcs_charmap(vmap
);
1581 DEBUG_puts(" Error, returning NULL!");
1588 * End of "$Id: transcode.c 5373 2006-04-06 20:03:32Z mike $"