2 * "$Id: transcode.c 6038 2006-10-14 15:53:10Z 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 int i
; /* Looping variable */
412 cups_utf8_t ch
; /* Character value */
413 cups_utf8_t next
; /* Next character value */
414 cups_utf32_t ch32
; /* UTF-32 character value */
418 * Check for valid arguments and clear output...
424 if (!dest
|| !src
|| maxout
< 1 || maxout
> CUPS_MAX_USTRING
)
428 * Convert input UTF-8 to output UTF-32 (and insert BOM)...
433 for (i
= maxout
- 1; *src
&& i
> 0; i
--)
438 * Convert UTF-8 character(s) to UTF-32 character...
444 * One-octet UTF-8 <= 127 (US-ASCII)...
450 else if ((ch
& 0xe0) == 0xc0)
453 * Two-octet UTF-8 <= 2047 (Latin-x)...
460 ch32
= ((ch
& 0x1f) << 6) | (next
& 0x3f);
463 * Check for non-shortest form (invalid UTF-8)...
471 else if ((ch
& 0xf0) == 0xe0)
474 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
481 ch32
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
487 ch32
= (ch32
<< 6) | (next
& 0x3f);
490 * Check for non-shortest form (invalid UTF-8)...
498 else if ((ch
& 0xf8) == 0xf0)
501 * Four-octet UTF-8...
508 ch32
= ((ch
& 0x07) << 6) | (next
& 0x3f);
514 ch32
= (ch32
<< 6) | (next
& 0x3f);
520 ch32
= (ch32
<< 6) | (next
& 0x3f);
523 * Check for non-shortest form (invalid UTF-8)...
534 * More than 4-octet (invalid UTF-8 sequence)...
541 * Check for UTF-16 surrogate (illegal UTF-8)...
544 if (ch32
>= 0xd800 && ch32
<= 0xdfff)
555 * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
557 * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
559 * UTF-32 char UTF-8 char(s)
560 * --------------------------------------------------
561 * 0 to 127 = 0xxxxxxx (US-ASCII)
562 * 128 to 2047 = 110xxxxx 10yyyyyy
563 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
564 * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
566 * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
567 * which would convert to five- or six-octet UTF-8 sequences...
570 int /* O - Count or -1 on error */
572 cups_utf8_t
*dest
, /* O - Target string */
573 const cups_utf32_t
*src
, /* I - Source string */
574 const int maxout
) /* I - Max output */
576 cups_utf8_t
*start
; /* Start of destination string */
577 int i
; /* Looping variable */
578 int swap
; /* Byte-swap input to output */
579 cups_utf32_t ch
; /* Character value */
583 * Check for valid arguments and clear output...
589 if (!dest
|| !src
|| maxout
< 1)
593 * Check for leading BOM in UTF-32 and inverted BOM...
597 swap
= *src
== 0xfffe0000;
599 if (*src
== 0xfffe0000 || *src
== 0xfeff)
603 * Convert input UTF-32 to output UTF-8...
606 for (i
= maxout
- 1; *src
&& i
> 0;)
611 * Byte swap input UTF-32, if necessary...
612 * (only byte-swapping 24 of 32 bits)
616 ch
= ((ch
>> 24) | ((ch
>> 8) & 0xff00) | ((ch
<< 8) & 0xff0000));
619 * Check for beyond Plane 16 (invalid UTF-32)...
626 * Convert UTF-32 character to UTF-8 character(s)...
632 * One-octet UTF-8 <= 127 (US-ASCII)...
635 *dest
++ = (cups_utf8_t
)ch
;
641 * Two-octet UTF-8 <= 2047 (Latin-x)...
647 *dest
++ = (cups_utf8_t
)(0xc0 | ((ch
>> 6) & 0x1f));
648 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
651 else if (ch
< 0x10000)
654 * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
660 *dest
++ = (cups_utf8_t
)(0xe0 | ((ch
>> 12) & 0x0f));
661 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
662 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
668 * Four-octet UTF-8...
674 *dest
++ = (cups_utf8_t
)(0xf0 | ((ch
>> 18) & 0x07));
675 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 12) & 0x3f));
676 *dest
++ = (cups_utf8_t
)(0x80 | ((ch
>> 6) & 0x3f));
677 *dest
++ = (cups_utf8_t
)(0x80 | (ch
& 0x3f));
684 return ((int)(dest
- start
));
689 * 'compare_wide()' - Compare key for wide (VBCS) match.
693 compare_wide(const void *k1
, /* I - Key char */
694 const void *k2
) /* I - Map char */
696 cups_vbcs_t key
; /* Legacy key character */
697 cups_vbcs_t map
; /* Legacy map character */
700 key
= *((cups_vbcs_t
*)k1
);
701 map
= ((_cups_wide2uni_t
*)k2
)->widechar
;
703 return ((int)(key
- map
));
708 * 'conv_sbcs_to_utf8()' - Convert legacy SBCS to UTF-8.
711 static int /* O - Count or -1 on error */
713 cups_utf8_t
*dest
, /* O - Target string */
714 const cups_sbcs_t
*src
, /* I - Source string */
715 int maxout
, /* I - Max output */
716 const cups_encoding_t encoding
) /* I - Encoding */
718 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
719 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
720 cups_sbcs_t legchar
; /* Legacy character value */
721 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
722 *workptr
; /* Pointer into string */
726 * Find legacy charset map in cache...
729 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
733 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
737 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
742 * Convert ASCII verbatim (optimization)...
746 *workptr
++ = (cups_utf32_t
)legchar
;
750 * Convert unknown character to Replacement Character...
753 crow
= cmap
->char2uni
+ legchar
;
758 *workptr
++ = (cups_utf32_t
)*crow
;
765 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
770 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
775 * 'conv_utf8_to_sbcs()' - Convert UTF-8 to legacy SBCS.
778 static int /* O - Count or -1 on error */
780 cups_sbcs_t
*dest
, /* O - Target string */
781 const cups_utf8_t
*src
, /* I - Source string */
782 int maxout
, /* I - Max output */
783 const cups_encoding_t encoding
) /* I - Encoding */
785 cups_sbcs_t
*start
; /* Start of destination string */
786 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
787 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
788 cups_utf32_t unichar
; /* Character value */
789 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
790 *workptr
; /* Pointer into string */
794 * Find legacy charset map in cache...
797 if ((cmap
= (_cups_cmap_t
*)get_charmap(encoding
)) == NULL
)
801 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
804 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
808 * Convert internal UCS-4 to SBCS legacy charset (and delete BOM)...
811 for (workptr
= work
+ 1, start
= dest
; *workptr
&& maxout
> 1; maxout
--)
813 unichar
= *workptr
++;
818 * Convert ASCII verbatim (optimization)...
823 *dest
++ = (cups_sbcs_t
)unichar
;
828 * Convert unknown character to visible replacement...
831 srow
= cmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
834 srow
+= (int)(unichar
& 0xff);
846 return ((int)(dest
- start
));
851 * 'conv_utf8_to_vbcs()' - Convert UTF-8 to legacy DBCS/VBCS.
854 static int /* O - Count or -1 on error */
856 cups_sbcs_t
*dest
, /* O - Target string */
857 const cups_utf8_t
*src
, /* I - Source string */
858 int maxout
, /* I - Max output */
859 const cups_encoding_t encoding
) /* I - Encoding */
861 cups_sbcs_t
*start
; /* Start of destination string */
862 _cups_vmap_t
*vmap
; /* Legacy DBCS / Unicode Charset Map */
863 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
864 cups_utf32_t unichar
; /* Character value */
865 cups_vbcs_t legchar
; /* Legacy character value */
866 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
867 *workptr
; /* Pointer into string */
871 * Find legacy charset map in cache...
874 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
878 * Convert input UTF-8 to internal UCS-4 (and insert BOM)...
881 if (cupsUTF8ToUTF32(work
, src
, CUPS_MAX_USTRING
) < 0)
885 * Convert internal UCS-4 to VBCS legacy charset (and delete BOM)...
888 for (start
= dest
, workptr
= work
+ 1; *workptr
&& maxout
> 1; maxout
--)
890 unichar
= *workptr
++;
895 * Convert ASCII verbatim (optimization)...
900 *dest
++ = (cups_vbcs_t
)unichar
;
905 * Convert unknown character to visible replacement...
908 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
911 vrow
+= (int)(unichar
& 0xff);
914 legchar
= (cups_vbcs_t
)'?';
916 legchar
= (cups_vbcs_t
)*vrow
;
919 * Save n-byte legacy character...
922 if (legchar
> 0xffffff)
927 *dest
++ = (cups_sbcs_t
)(legchar
>> 24);
928 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
929 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
930 *dest
++ = (cups_sbcs_t
)legchar
;
934 else if (legchar
> 0xffff)
939 *dest
++ = (cups_sbcs_t
)(legchar
>> 16);
940 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
941 *dest
++ = (cups_sbcs_t
)legchar
;
945 else if (legchar
> 0xff)
947 *dest
++ = (cups_sbcs_t
)(legchar
>> 8);
948 *dest
++ = (cups_sbcs_t
)legchar
;
958 return ((int)(dest
- start
));
963 * 'conv_vbcs_to_utf8()' - Convert legacy DBCS/VBCS to UTF-8.
966 static int /* O - Count or -1 on error */
968 cups_utf8_t
*dest
, /* O - Target string */
969 const cups_sbcs_t
*src
, /* I - Source string */
970 int maxout
, /* I - Max output */
971 const cups_encoding_t encoding
) /* I - Encoding */
973 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
974 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
975 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
976 cups_sbcs_t leadchar
; /* Lead char of n-byte legacy char */
977 cups_vbcs_t legchar
; /* Legacy character value */
978 cups_utf32_t work
[CUPS_MAX_USTRING
], /* Internal UCS-4 string */
979 *workptr
; /* Pointer into string */
983 * Find legacy charset map in cache...
986 if ((vmap
= (_cups_vmap_t
*)get_charmap(encoding
)) == NULL
)
990 * Convert input legacy charset to internal UCS-4 (and insert BOM)...
994 for (workptr
= work
+ 1; *src
&& workptr
< (work
+ CUPS_MAX_USTRING
- 1);)
997 leadchar
= (cups_sbcs_t
)legchar
;
1000 * Convert ASCII verbatim (optimization)...
1005 *workptr
++ = (cups_utf32_t
)legchar
;
1010 * Convert 2-byte legacy character...
1013 if (vmap
->lead2char
[(int)leadchar
] == leadchar
)
1018 legchar
= (legchar
<< 8) | *src
++;
1021 * Convert unknown character to Replacement Character...
1024 crow
= vmap
->char2uni
[(int)((legchar
>> 8) & 0xff)];
1026 crow
+= (int) (legchar
& 0xff);
1028 if (!crow
|| !*crow
)
1029 *workptr
++ = 0xfffd;
1031 *workptr
++ = (cups_utf32_t
)*crow
;
1036 * Fetch 3-byte or 4-byte legacy character...
1039 if (vmap
->lead3char
[(int)leadchar
] == leadchar
)
1041 if (!*src
|| !src
[1])
1044 legchar
= (legchar
<< 8) | *src
++;
1045 legchar
= (legchar
<< 8) | *src
++;
1047 else if (vmap
->lead4char
[(int)leadchar
] == leadchar
)
1049 if (!*src
|| !src
[1] || !src
[2])
1052 legchar
= (legchar
<< 8) | *src
++;
1053 legchar
= (legchar
<< 8) | *src
++;
1054 legchar
= (legchar
<< 8) | *src
++;
1060 * Find 3-byte or 4-byte legacy character...
1063 wide2uni
= (_cups_wide2uni_t
*)bsearch(&legchar
,
1066 sizeof(_cups_wide2uni_t
),
1070 * Convert unknown character to Replacement Character...
1073 if (!wide2uni
|| !wide2uni
->unichar
)
1074 *workptr
++ = 0xfffd;
1076 *workptr
++ = wide2uni
->unichar
;
1084 * Convert internal UCS-4 to output UTF-8 (and delete BOM)...
1087 return (cupsUTF32ToUTF8(dest
, work
, maxout
));
1092 * 'free_sbcs_charmap()' - Free memory used by a single byte character set.
1096 free_sbcs_charmap(_cups_cmap_t
*cmap
) /* I - Character set */
1098 int i
; /* Looping variable */
1101 for (i
= 0; i
< 256; i
++)
1102 if (cmap
->uni2char
[i
])
1103 free(cmap
->uni2char
[i
]);
1110 * 'free_vbcs_charmap()' - Free memory used by a variable byte character set.
1114 free_vbcs_charmap(_cups_vmap_t
*vmap
) /* I - Character set */
1116 int i
; /* Looping variable */
1119 for (i
= 0; i
< 256; i
++)
1120 if (vmap
->char2uni
[i
])
1121 free(vmap
->char2uni
[i
]);
1123 for (i
= 0; i
< 256; i
++)
1124 if (vmap
->uni2char
[i
])
1125 free(vmap
->uni2char
[i
]);
1128 free(vmap
->wide2uni
);
1135 * 'get_charmap()' - Lookup or get a character set map (private).
1137 * This code handles single-byte (SBCS), double-byte (DBCS), and
1138 * variable-byte (VBCS) character sets _without_ charset escapes...
1139 * This code does not handle multiple-byte character sets (MBCS)
1140 * (such as ISO-2022-JP) with charset switching via escapes...
1144 static void * /* O - Charset map pointer */
1146 const cups_encoding_t encoding
) /* I - Encoding */
1148 char filename
[1024]; /* Filename for charset map file */
1149 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1153 * Get the data directory and charset map name...
1156 snprintf(filename
, sizeof(filename
), "%s/charmaps/%s.txt",
1157 cg
->cups_datadir
, _cupsEncodingName(encoding
));
1159 DEBUG_printf((" filename=\"%s\"\n", filename
));
1162 * Read charset map input file into cache...
1165 if (encoding
< CUPS_ENCODING_SBCS_END
)
1166 return (get_sbcs_charmap(encoding
, filename
));
1167 else if (encoding
< CUPS_ENCODING_VBCS_END
)
1168 return (get_vbcs_charmap(encoding
, filename
));
1175 * 'get_charmap_count()' - Count lines in a charmap file.
1178 static int /* O - Count or -1 on error */
1179 get_charmap_count(cups_file_t
*fp
) /* I - File to read from */
1181 int count
; /* Number of lines */
1182 char line
[256]; /* Line from input map file */
1186 * Count lines in map input file...
1191 while (cupsFileGets(fp
, line
, sizeof(line
)))
1196 * Return the number of lines...
1207 * 'get_sbcs_charmap()' - Get SBCS Charmap.
1210 static _cups_cmap_t
* /* O - Charmap or 0 on error */
1212 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1213 const char *filename
) /* I - Charmap Filename */
1215 unsigned long legchar
; /* Legacy character value */
1216 cups_utf32_t unichar
; /* Unicode character value */
1217 _cups_cmap_t
*cmap
; /* Legacy SBCS / Unicode Charset Map */
1218 cups_file_t
*fp
; /* Charset map file pointer */
1219 char *s
; /* Line parsing pointer */
1220 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1221 cups_sbcs_t
*srow
; /* Pointer to SBCS row in 'uni2char' */
1222 char line
[256]; /* Line from charset map file */
1226 * See if we already have this SBCS charset map loaded...
1229 for (cmap
= cmap_cache
; cmap
; cmap
= cmap
->next
)
1231 if (cmap
->encoding
== encoding
)
1234 DEBUG_printf((" returning existing cmap=%p\n", cmap
));
1236 return ((void *)cmap
);
1241 * Open SBCS charset map input file...
1244 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1248 * Allocate memory for SBCS charset map...
1251 if ((cmap
= (_cups_cmap_t
*)calloc(1, sizeof(_cups_cmap_t
))) == NULL
)
1254 DEBUG_puts(" Unable to allocate memory!");
1260 cmap
->encoding
= encoding
;
1263 * Save SBCS charset map into memory for transcoding...
1266 while (cupsFileGets(fp
, line
, sizeof(line
)))
1271 legchar
= strtol(line
, &s
, 16);
1272 if (legchar
< 0 || legchar
> 0xff)
1275 unichar
= strtol(s
, NULL
, 16);
1276 if (unichar
< 0 || unichar
> 0xffff)
1280 * Save legacy to Unicode mapping in direct lookup table...
1283 crow
= cmap
->char2uni
+ legchar
;
1284 *crow
= (cups_ucs2_t
)(unichar
& 0xffff);
1287 * Save Unicode to legacy mapping in indirect lookup table...
1290 srow
= cmap
->uni2char
[(unichar
>> 8) & 0xff];
1293 srow
= (cups_sbcs_t
*)calloc(256, sizeof(cups_sbcs_t
));
1297 cmap
->uni2char
[(unichar
>> 8) & 0xff] = srow
;
1300 srow
+= unichar
& 0xff;
1303 * Convert Replacement Character to visible replacement...
1306 if (unichar
== 0xfffd)
1307 legchar
= (unsigned long)'?';
1310 * First (oldest) legacy character uses Unicode mapping cell...
1314 *srow
= (cups_sbcs_t
)legchar
;
1320 * Add it to the cache and return...
1323 cmap
->next
= cmap_cache
;
1326 DEBUG_printf((" returning new cmap=%p\n", cmap
));
1331 * If we get here, there was an error in the cmap file...
1336 free_sbcs_charmap(cmap
);
1340 DEBUG_puts(" Error, returning NULL!");
1347 * 'get_vbcs_charmap()' - Get DBCS/VBCS Charmap.
1350 static _cups_vmap_t
* /* O - Charmap or 0 on error */
1352 const cups_encoding_t encoding
, /* I - Charmap Encoding */
1353 const char *filename
) /* I - Charmap Filename */
1355 _cups_vmap_t
*vmap
; /* Legacy VBCS / Unicode Charset Map */
1356 cups_ucs2_t
*crow
; /* Pointer to UCS-2 row in 'char2uni' */
1357 cups_vbcs_t
*vrow
; /* Pointer to VBCS row in 'uni2char' */
1358 _cups_wide2uni_t
*wide2uni
; /* Pointer to row in 'wide2uni' */
1359 cups_sbcs_t leadchar
; /* Lead char of 2-byte legacy char */
1360 unsigned long legchar
; /* Legacy character value */
1361 cups_utf32_t unichar
; /* Unicode character value */
1362 int mapcount
; /* Count of lines in charmap file */
1363 cups_file_t
*fp
; /* Charset map file pointer */
1364 char *s
; /* Line parsing pointer */
1365 char line
[256]; /* Line from charset map file */
1366 int i
; /* Loop variable */
1367 int wide
; /* 32-bit legacy char */
1370 DEBUG_printf(("get_vbcs_charmap(encoding=%d, filename=\"%s\")\n",
1371 encoding
, filename
));
1374 * See if we already have this DBCS/VBCS charset map loaded...
1377 for (vmap
= vmap_cache
; vmap
; vmap
= vmap
->next
)
1379 if (vmap
->encoding
== encoding
)
1382 DEBUG_printf((" returning existing vmap=%p\n", vmap
));
1384 return ((void *)vmap
);
1389 * Open VBCS charset map input file...
1392 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1394 DEBUG_printf((" Unable to open file: %s\n", strerror(errno
)));
1400 * Count lines in charmap file...
1403 if ((mapcount
= get_charmap_count(fp
)) <= 0)
1405 DEBUG_puts(" Unable to get charmap count!");
1410 DEBUG_printf((" mapcount=%d\n", mapcount
));
1413 * Allocate memory for DBCS/VBCS charset map...
1416 if ((vmap
= (_cups_vmap_t
*)calloc(1, sizeof(_cups_vmap_t
))) == NULL
)
1419 DEBUG_puts(" Unable to allocate memory!");
1425 vmap
->encoding
= encoding
;
1428 * Save DBCS/VBCS charset map into memory for transcoding...
1439 while (cupsFileGets(fp
, line
, sizeof(line
)))
1444 legchar
= strtoul(line
, &s
, 16);
1445 if (legchar
== ULONG_MAX
)
1448 unichar
= strtol(s
, NULL
, 16);
1449 if (unichar
< 0 || unichar
> 0xffff)
1454 /* DEBUG_printf((" i=%d, legchar=0x%08lx, unichar=0x%04x\n", i,
1455 legchar, (unsigned)unichar)); */
1458 * Save lead char of 2/3/4-byte legacy char...
1461 if (legchar
> 0xff && legchar
<= 0xffff)
1463 leadchar
= (cups_sbcs_t
)(legchar
>> 8);
1464 vmap
->lead2char
[leadchar
] = leadchar
;
1467 if (legchar
> 0xffff && legchar
<= 0xffffff)
1469 leadchar
= (cups_sbcs_t
)(legchar
>> 16);
1470 vmap
->lead3char
[leadchar
] = leadchar
;
1473 if (legchar
> 0xffffff)
1475 leadchar
= (cups_sbcs_t
)(legchar
>> 24);
1476 vmap
->lead4char
[leadchar
] = leadchar
;
1480 * Save Legacy to Unicode mapping...
1483 if (legchar
<= 0xffff)
1486 * Save DBCS 16-bit to Unicode mapping in indirect lookup table...
1489 crow
= vmap
->char2uni
[(int)leadchar
];
1492 crow
= (cups_ucs2_t
*)calloc(256, sizeof(cups_ucs2_t
));
1496 vmap
->char2uni
[(int)leadchar
] = crow
;
1499 crow
[(int)(legchar
& 0xff)] = (cups_ucs2_t
)unichar
;
1504 * Save VBCS 32-bit to Unicode mapping in sorted list table...
1510 vmap
->widecount
= (mapcount
- i
+ 1);
1511 wide2uni
= (_cups_wide2uni_t
*)calloc(vmap
->widecount
,
1512 sizeof(_cups_wide2uni_t
));
1516 vmap
->wide2uni
= wide2uni
;
1519 wide2uni
->widechar
= (cups_vbcs_t
)legchar
;
1520 wide2uni
->unichar
= (cups_ucs2_t
)unichar
;
1525 * Save Unicode to legacy mapping in indirect lookup table...
1528 vrow
= vmap
->uni2char
[(int)((unichar
>> 8) & 0xff)];
1531 vrow
= (cups_vbcs_t
*)calloc(256, sizeof(cups_vbcs_t
));
1535 vmap
->uni2char
[(int) ((unichar
>> 8) & 0xff)] = vrow
;
1538 vrow
+= (int)(unichar
& 0xff);
1541 * Convert Replacement Character to visible replacement...
1544 if (unichar
== 0xfffd)
1545 legchar
= (unsigned long)'?';
1548 * First (oldest) legacy character uses Unicode mapping cell...
1552 *vrow
= (cups_vbcs_t
)legchar
;
1555 vmap
->charcount
= (i
- vmap
->widecount
);
1560 * Add it to the cache and return...
1563 vmap
->next
= vmap_cache
;
1566 DEBUG_printf((" returning new vmap=%p\n", vmap
));
1571 * If we get here, the file contains errors...
1576 free_vbcs_charmap(vmap
);
1580 DEBUG_puts(" Error, returning NULL!");
1587 * End of "$Id: transcode.c 6038 2006-10-14 15:53:10Z mike $"