]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
09a101d6 | 2 | * "$Id: transcode.c 6592 2007-06-21 21:04:40Z mike $" |
ef416fc2 | 3 | * |
4 | * Transcoding support for the Common UNIX Printing System (CUPS). | |
5 | * | |
b86bc4cf | 6 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 7 | * |
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 | |
13 | * Products at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
fa73b229 | 26 | * _cupsCharmapFlush() - Flush all character set maps out of cache. |
e1d6a774 | 27 | * _cupsCharmapFree() - Free a character set map. |
28 | * _cupsCharmapGet() - Get a character set map. | |
ef416fc2 | 29 | * cupsCharsetToUTF8() - Convert legacy character set to UTF-8. |
e1d6a774 | 30 | * cupsUTF8ToCharset() - Convert UTF-8 to legacy character set. |
ef416fc2 | 31 | * cupsUTF8ToUTF32() - Convert UTF-8 to UTF-32. |
32 | * cupsUTF32ToUTF8() - Convert UTF-32 to UTF-8. | |
e1d6a774 | 33 | * compare_wide() - Compare key for wide (VBCS) match. |
34 | * conv_sbcs_to_utf8() - Convert legacy SBCS to UTF-8. | |
ef416fc2 | 35 | * conv_utf8_to_sbcs() - Convert UTF-8 to legacy SBCS. |
36 | * conv_utf8_to_vbcs() - Convert UTF-8 to legacy DBCS/VBCS. | |
ef416fc2 | 37 | * conv_vbcs_to_utf8() - Convert legacy DBCS/VBCS to UTF-8. |
e1d6a774 | 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. | |
d6ae789d | 40 | * get_charmap() - Lookup or get a character set map (private). |
e1d6a774 | 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. | |
ef416fc2 | 44 | */ |
45 | ||
46 | /* | |
47 | * Include necessary headers... | |
48 | */ | |
49 | ||
50 | #include "globals.h" | |
e1d6a774 | 51 | #include "debug.h" |
e53920b9 | 52 | #include <limits.h> |
ef416fc2 | 53 | #include <stdlib.h> |
54 | #include <errno.h> | |
55 | #include <time.h> | |
56 | ||
57 | ||
d6ae789d | 58 | /* |
59 | * Local globals... | |
60 | */ | |
61 | ||
62 | #ifdef HAVE_PTHREAD_H | |
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 */ | |
70 | ||
71 | ||
ef416fc2 | 72 | /* |
e1d6a774 | 73 | * Local functions... |
ef416fc2 | 74 | */ |
75 | ||
e1d6a774 | 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, | |
79 | int maxout, | |
80 | const cups_encoding_t encoding); | |
81 | static int conv_utf8_to_sbcs(cups_sbcs_t *dest, | |
82 | const cups_utf8_t *src, | |
83 | int maxout, | |
84 | const cups_encoding_t encoding); | |
85 | static int conv_utf8_to_vbcs(cups_sbcs_t *dest, | |
86 | const cups_utf8_t *src, | |
87 | int maxout, | |
88 | const cups_encoding_t encoding); | |
89 | static int conv_vbcs_to_utf8(cups_utf8_t *dest, | |
90 | const cups_sbcs_t *src, | |
91 | int maxout, | |
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); | |
d6ae789d | 95 | static void *get_charmap(const cups_encoding_t encoding); |
e1d6a774 | 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); | |
101 | ||
ef416fc2 | 102 | |
103 | /* | |
e1d6a774 | 104 | * '_cupsCharmapFlush()' - Flush all character set maps out of cache. |
ef416fc2 | 105 | */ |
106 | ||
e1d6a774 | 107 | void |
d6ae789d | 108 | _cupsCharmapFlush(void) |
ef416fc2 | 109 | { |
e1d6a774 | 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 */ | |
ef416fc2 | 114 | |
115 | ||
d6ae789d | 116 | #ifdef HAVE_PTHREAD_H |
117 | pthread_mutex_lock(&map_mutex); | |
118 | #endif /* HAVE_PTHREAD_H */ | |
119 | ||
ef416fc2 | 120 | /* |
e1d6a774 | 121 | * Loop through SBCS charset map cache, free all memory... |
ef416fc2 | 122 | */ |
123 | ||
d6ae789d | 124 | for (cmap = cmap_cache; cmap; cmap = cnext) |
e1d6a774 | 125 | { |
126 | cnext = cmap->next; | |
ef416fc2 | 127 | |
e1d6a774 | 128 | free_sbcs_charmap(cmap); |
129 | } | |
ef416fc2 | 130 | |
d6ae789d | 131 | cmap_cache = NULL; |
ef416fc2 | 132 | |
133 | /* | |
e1d6a774 | 134 | * Loop through DBCS/VBCS charset map cache, free all memory... |
ef416fc2 | 135 | */ |
136 | ||
d6ae789d | 137 | for (vmap = vmap_cache; vmap; vmap = vnext) |
e1d6a774 | 138 | { |
139 | vnext = vmap->next; | |
140 | ||
141 | free_vbcs_charmap(vmap); | |
142 | ||
143 | free(vmap); | |
144 | } | |
145 | ||
d6ae789d | 146 | vmap_cache = NULL; |
147 | ||
148 | #ifdef HAVE_PTHREAD_H | |
149 | pthread_mutex_unlock(&map_mutex); | |
150 | #endif /* HAVE_PTHREAD_H */ | |
ef416fc2 | 151 | } |
152 | ||
e1d6a774 | 153 | |
ef416fc2 | 154 | /* |
e1d6a774 | 155 | * '_cupsCharmapFree()' - Free a character set map. |
ef416fc2 | 156 | * |
e1d6a774 | 157 | * This does not actually free; use '_cupsCharmapFlush()' for that. |
ef416fc2 | 158 | */ |
e1d6a774 | 159 | |
ef416fc2 | 160 | void |
e1d6a774 | 161 | _cupsCharmapFree( |
162 | const cups_encoding_t encoding) /* I - Encoding */ | |
ef416fc2 | 163 | { |
e1d6a774 | 164 | _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */ |
165 | _cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */ | |
e1d6a774 | 166 | |
ef416fc2 | 167 | |
168 | /* | |
169 | * See if we already have this SBCS charset map loaded... | |
170 | */ | |
e1d6a774 | 171 | |
d6ae789d | 172 | #ifdef HAVE_PTHREAD_H |
173 | pthread_mutex_lock(&map_mutex); | |
174 | #endif /* HAVE_PTHREAD_H */ | |
175 | ||
176 | for (cmap = cmap_cache; cmap; cmap = cmap->next) | |
ef416fc2 | 177 | { |
178 | if (cmap->encoding == encoding) | |
179 | { | |
180 | if (cmap->used > 0) | |
181 | cmap->used --; | |
d6ae789d | 182 | break; |
ef416fc2 | 183 | } |
184 | } | |
185 | ||
186 | /* | |
187 | * See if we already have this DBCS/VBCS charset map loaded... | |
188 | */ | |
e1d6a774 | 189 | |
d6ae789d | 190 | for (vmap = vmap_cache; vmap; vmap = vmap->next) |
ef416fc2 | 191 | { |
192 | if (vmap->encoding == encoding) | |
193 | { | |
194 | if (vmap->used > 0) | |
195 | vmap->used --; | |
d6ae789d | 196 | break; |
ef416fc2 | 197 | } |
198 | } | |
d6ae789d | 199 | |
200 | #ifdef HAVE_PTHREAD_H | |
201 | pthread_mutex_unlock(&map_mutex); | |
202 | #endif /* HAVE_PTHREAD_H */ | |
fa73b229 | 203 | } |
204 | ||
205 | ||
206 | /* | |
e1d6a774 | 207 | * '_cupsCharmapGet()' - Get a character set map. |
208 | * | |
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... | |
fa73b229 | 213 | */ |
214 | ||
e1d6a774 | 215 | void * /* O - Charset map pointer */ |
216 | _cupsCharmapGet( | |
217 | const cups_encoding_t encoding) /* I - Encoding */ | |
fa73b229 | 218 | { |
d6ae789d | 219 | void *charmap; /* Charset map pointer */ |
e1d6a774 | 220 | |
fa73b229 | 221 | |
e1d6a774 | 222 | DEBUG_printf(("_cupsCharmapGet(encoding=%d)\n", encoding)); |
ef416fc2 | 223 | |
224 | /* | |
e1d6a774 | 225 | * Check for valid arguments... |
ef416fc2 | 226 | */ |
e1d6a774 | 227 | |
228 | if (encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END) | |
ef416fc2 | 229 | { |
e1d6a774 | 230 | DEBUG_puts(" Bad encoding, returning NULL!"); |
231 | return (NULL); | |
ef416fc2 | 232 | } |
ef416fc2 | 233 | |
234 | /* | |
d6ae789d | 235 | * Lookup or get the charset map pointer and return... |
ef416fc2 | 236 | */ |
e1d6a774 | 237 | |
d6ae789d | 238 | #ifdef HAVE_PTHREAD_H |
239 | pthread_mutex_lock(&map_mutex); | |
240 | #endif /* HAVE_PTHREAD_H */ | |
e1d6a774 | 241 | |
d6ae789d | 242 | charmap = get_charmap(encoding); |
e1d6a774 | 243 | |
d6ae789d | 244 | #ifdef HAVE_PTHREAD_H |
245 | pthread_mutex_unlock(&map_mutex); | |
246 | #endif /* HAVE_PTHREAD_H */ | |
e1d6a774 | 247 | |
d6ae789d | 248 | return (charmap); |
ef416fc2 | 249 | } |
250 | ||
e1d6a774 | 251 | |
ef416fc2 | 252 | /* |
e1d6a774 | 253 | * 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8. |
ef416fc2 | 254 | * |
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... | |
259 | */ | |
e1d6a774 | 260 | |
261 | int /* O - Count or -1 on error */ | |
262 | cupsCharsetToUTF8( | |
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 */ | |
ef416fc2 | 267 | { |
d6ae789d | 268 | int bytes; /* Number of bytes converted */ |
269 | ||
270 | ||
ef416fc2 | 271 | /* |
272 | * Check for valid arguments... | |
273 | */ | |
274 | ||
e1d6a774 | 275 | DEBUG_printf(("cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)\n", |
276 | dest, src, maxout, encoding)); | |
277 | ||
278 | if (dest) | |
279 | *dest = '\0'; | |
280 | ||
ef416fc2 | 281 | if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING) |
e1d6a774 | 282 | { |
283 | DEBUG_puts(" Bad arguments, returning -1"); | |
ef416fc2 | 284 | return (-1); |
e1d6a774 | 285 | } |
ef416fc2 | 286 | |
287 | /* | |
288 | * Handle identity conversions... | |
289 | */ | |
290 | ||
291 | if (encoding == CUPS_UTF8 || | |
292 | encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END) | |
293 | { | |
e1d6a774 | 294 | strlcpy((char *)dest, src, maxout); |
b86bc4cf | 295 | return ((int)strlen((char *)dest)); |
ef416fc2 | 296 | } |
297 | ||
411affcf | 298 | /* |
299 | * Handle ISO-8859-1 to UTF-8 directly... | |
300 | */ | |
301 | ||
302 | if (encoding == CUPS_ISO8859_1) | |
303 | { | |
304 | int ch; /* Character from string */ | |
305 | cups_utf8_t *destptr, /* Pointer into UTF-8 buffer */ | |
306 | *destend; /* End of UTF-8 buffer */ | |
307 | ||
308 | ||
309 | destptr = dest; | |
310 | destend = dest + maxout - 2; | |
311 | ||
312 | while (*src && destptr < destend) | |
313 | { | |
314 | ch = *src++ & 255; | |
315 | ||
316 | if (ch & 128) | |
317 | { | |
318 | *destptr++ = 0xc0 | (ch >> 6); | |
319 | *destptr++ = 0x80 | (ch & 0x3f); | |
320 | } | |
321 | else | |
322 | *destptr++ = ch; | |
323 | } | |
324 | ||
325 | *destptr = '\0'; | |
326 | ||
b86bc4cf | 327 | return ((int)(destptr - dest)); |
411affcf | 328 | } |
329 | ||
ef416fc2 | 330 | /* |
e1d6a774 | 331 | * Convert input legacy charset to UTF-8... |
ef416fc2 | 332 | */ |
e1d6a774 | 333 | |
d6ae789d | 334 | #ifdef HAVE_PTHREAD_H |
335 | pthread_mutex_lock(&map_mutex); | |
336 | #endif /* HAVE_PTHREAD_H */ | |
337 | ||
ef416fc2 | 338 | if (encoding < CUPS_ENCODING_SBCS_END) |
d6ae789d | 339 | bytes = conv_sbcs_to_utf8(dest, (cups_sbcs_t *)src, maxout, encoding); |
ef416fc2 | 340 | else if (encoding < CUPS_ENCODING_VBCS_END) |
d6ae789d | 341 | bytes = conv_vbcs_to_utf8(dest, (cups_sbcs_t *)src, maxout, encoding); |
ef416fc2 | 342 | else |
e1d6a774 | 343 | { |
d6ae789d | 344 | DEBUG_puts(" Bad encoding, returning -1"); |
345 | bytes = -1; | |
e1d6a774 | 346 | } |
d6ae789d | 347 | |
348 | #ifdef HAVE_PTHREAD_H | |
349 | pthread_mutex_unlock(&map_mutex); | |
350 | #endif /* HAVE_PTHREAD_H */ | |
351 | ||
352 | return (bytes); | |
ef416fc2 | 353 | } |
354 | ||
e1d6a774 | 355 | |
ef416fc2 | 356 | /* |
e1d6a774 | 357 | * 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set. |
ef416fc2 | 358 | * |
359 | * This code handles single-byte (SBCS), double-byte (DBCS), and | |
360 | * variable-byte (VBCS) character sets _without_ charset escapes... | |
361 | * This code does not handle multiple-byte character sets (MBCS) | |
362 | * (such as ISO-2022-JP) with charset switching via escapes... | |
363 | */ | |
e1d6a774 | 364 | |
365 | int /* O - Count or -1 on error */ | |
366 | cupsUTF8ToCharset( | |
367 | char *dest, /* O - Target string */ | |
368 | const cups_utf8_t *src, /* I - Source string */ | |
369 | const int maxout, /* I - Max output */ | |
370 | const cups_encoding_t encoding) /* I - Encoding */ | |
ef416fc2 | 371 | { |
d6ae789d | 372 | int bytes; /* Number of bytes converted */ |
373 | ||
374 | ||
ef416fc2 | 375 | /* |
376 | * Check for valid arguments... | |
377 | */ | |
378 | ||
379 | if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING) | |
e1d6a774 | 380 | { |
381 | if (dest) | |
382 | *dest = '\0'; | |
383 | ||
ef416fc2 | 384 | return (-1); |
e1d6a774 | 385 | } |
ef416fc2 | 386 | |
387 | /* | |
388 | * Handle identity conversions... | |
389 | */ | |
390 | ||
391 | if (encoding == CUPS_UTF8 || | |
392 | encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END) | |
393 | { | |
e1d6a774 | 394 | strlcpy(dest, (char *)src, maxout); |
b86bc4cf | 395 | return ((int)strlen(dest)); |
ef416fc2 | 396 | } |
397 | ||
411affcf | 398 | /* |
399 | * Handle UTF-8 to ISO-8859-1 directly... | |
400 | */ | |
401 | ||
402 | if (encoding == CUPS_ISO8859_1) | |
403 | { | |
404 | int ch; /* Character from string */ | |
405 | char *destptr, /* Pointer into ISO-8859-1 buffer */ | |
406 | *destend; /* End of ISO-8859-1 buffer */ | |
407 | ||
408 | ||
409 | destptr = dest; | |
410 | destend = dest + maxout - 1; | |
411 | ||
412 | while (*src && destptr < destend) | |
413 | { | |
414 | ch = *src++; | |
415 | ||
416 | if ((ch & 0xe0) == 0xc0) | |
417 | { | |
418 | ch = ((ch & 0x1f) << 6) | (*src++ & 0x3f); | |
419 | ||
420 | if (ch < 256) | |
421 | *destptr++ = ch; | |
422 | else | |
423 | *destptr++ = '?'; | |
424 | } | |
425 | else if ((ch & 0xf0) == 0xe0 || | |
426 | (ch & 0xf8) == 0xf0) | |
427 | *destptr++ = '?'; | |
428 | else if (!(ch & 0x80)) | |
429 | *destptr++ = ch; | |
430 | } | |
431 | ||
432 | *destptr = '\0'; | |
433 | ||
b86bc4cf | 434 | return ((int)(destptr - dest)); |
411affcf | 435 | } |
436 | ||
ef416fc2 | 437 | /* |
e1d6a774 | 438 | * Convert input UTF-8 to legacy charset... |
ef416fc2 | 439 | */ |
e1d6a774 | 440 | |
d6ae789d | 441 | #ifdef HAVE_PTHREAD_H |
442 | pthread_mutex_lock(&map_mutex); | |
443 | #endif /* HAVE_PTHREAD_H */ | |
444 | ||
ef416fc2 | 445 | if (encoding < CUPS_ENCODING_SBCS_END) |
d6ae789d | 446 | bytes = conv_utf8_to_sbcs((cups_sbcs_t *)dest, src, maxout, encoding); |
ef416fc2 | 447 | else if (encoding < CUPS_ENCODING_VBCS_END) |
d6ae789d | 448 | bytes = conv_utf8_to_vbcs((cups_sbcs_t *)dest, src, maxout, encoding); |
ef416fc2 | 449 | else |
d6ae789d | 450 | bytes = -1; |
451 | ||
452 | #ifdef HAVE_PTHREAD_H | |
453 | pthread_mutex_unlock(&map_mutex); | |
454 | #endif /* HAVE_PTHREAD_H */ | |
455 | ||
456 | return (bytes); | |
ef416fc2 | 457 | } |
458 | ||
ef416fc2 | 459 | |
460 | /* | |
461 | * 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32. | |
462 | * | |
463 | * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows... | |
464 | * | |
465 | * UTF-32 char UTF-8 char(s) | |
466 | * -------------------------------------------------- | |
e1d6a774 | 467 | * 0 to 127 = 0xxxxxxx (US-ASCII) |
ef416fc2 | 468 | * 128 to 2047 = 110xxxxx 10yyyyyy |
469 | * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz | |
e1d6a774 | 470 | * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx |
ef416fc2 | 471 | * |
472 | * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4, | |
473 | * which would convert to five- or six-octet UTF-8 sequences... | |
ef416fc2 | 474 | */ |
e1d6a774 | 475 | |
476 | int /* O - Count or -1 on error */ | |
477 | cupsUTF8ToUTF32( | |
478 | cups_utf32_t *dest, /* O - Target string */ | |
479 | const cups_utf8_t *src, /* I - Source string */ | |
480 | const int maxout) /* I - Max output */ | |
ef416fc2 | 481 | { |
e1d6a774 | 482 | int i; /* Looping variable */ |
483 | cups_utf8_t ch; /* Character value */ | |
484 | cups_utf8_t next; /* Next character value */ | |
485 | cups_utf32_t ch32; /* UTF-32 character value */ | |
486 | ||
ef416fc2 | 487 | |
488 | /* | |
489 | * Check for valid arguments and clear output... | |
490 | */ | |
e1d6a774 | 491 | |
492 | if (dest) | |
493 | *dest = 0; | |
494 | ||
495 | if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING) | |
ef416fc2 | 496 | return (-1); |
ef416fc2 | 497 | |
498 | /* | |
499 | * Convert input UTF-8 to output UTF-32 (and insert BOM)... | |
500 | */ | |
e1d6a774 | 501 | |
502 | *dest++ = 0xfeff; | |
e1d6a774 | 503 | |
504 | for (i = maxout - 1; *src && i > 0; i --) | |
ef416fc2 | 505 | { |
e1d6a774 | 506 | ch = *src++; |
ef416fc2 | 507 | |
508 | /* | |
509 | * Convert UTF-8 character(s) to UTF-32 character... | |
510 | */ | |
e1d6a774 | 511 | |
512 | if (!(ch & 0x80)) | |
ef416fc2 | 513 | { |
514 | /* | |
515 | * One-octet UTF-8 <= 127 (US-ASCII)... | |
516 | */ | |
e1d6a774 | 517 | |
518 | *dest++ = ch; | |
2abf387c | 519 | continue; |
ef416fc2 | 520 | } |
521 | else if ((ch & 0xe0) == 0xc0) | |
522 | { | |
523 | /* | |
524 | * Two-octet UTF-8 <= 2047 (Latin-x)... | |
525 | */ | |
e1d6a774 | 526 | |
527 | next = *src++; | |
528 | if (!next) | |
ef416fc2 | 529 | return (-1); |
e1d6a774 | 530 | |
ef416fc2 | 531 | ch32 = ((ch & 0x1f) << 6) | (next & 0x3f); |
532 | ||
533 | /* | |
534 | * Check for non-shortest form (invalid UTF-8)... | |
535 | */ | |
e1d6a774 | 536 | |
537 | if (ch32 < 0x80) | |
ef416fc2 | 538 | return (-1); |
e1d6a774 | 539 | |
540 | *dest++ = ch32; | |
ef416fc2 | 541 | } |
542 | else if ((ch & 0xf0) == 0xe0) | |
543 | { | |
544 | /* | |
545 | * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)... | |
546 | */ | |
e1d6a774 | 547 | |
548 | next = *src++; | |
549 | if (!next) | |
ef416fc2 | 550 | return (-1); |
e1d6a774 | 551 | |
552 | ch32 = ((ch & 0x0f) << 6) | (next & 0x3f); | |
553 | ||
554 | next = *src++; | |
555 | if (!next) | |
ef416fc2 | 556 | return (-1); |
e1d6a774 | 557 | |
558 | ch32 = (ch32 << 6) | (next & 0x3f); | |
ef416fc2 | 559 | |
560 | /* | |
561 | * Check for non-shortest form (invalid UTF-8)... | |
562 | */ | |
e1d6a774 | 563 | |
564 | if (ch32 < 0x800) | |
ef416fc2 | 565 | return (-1); |
e1d6a774 | 566 | |
567 | *dest++ = ch32; | |
ef416fc2 | 568 | } |
569 | else if ((ch & 0xf8) == 0xf0) | |
570 | { | |
571 | /* | |
e1d6a774 | 572 | * Four-octet UTF-8... |
ef416fc2 | 573 | */ |
e1d6a774 | 574 | |
575 | next = *src++; | |
576 | if (!next) | |
ef416fc2 | 577 | return (-1); |
e1d6a774 | 578 | |
579 | ch32 = ((ch & 0x07) << 6) | (next & 0x3f); | |
580 | ||
581 | next = *src++; | |
582 | if (!next) | |
583 | return (-1); | |
584 | ||
585 | ch32 = (ch32 << 6) | (next & 0x3f); | |
586 | ||
587 | next = *src++; | |
588 | if (!next) | |
589 | return (-1); | |
590 | ||
591 | ch32 = (ch32 << 6) | (next & 0x3f); | |
592 | ||
ef416fc2 | 593 | /* |
e1d6a774 | 594 | * Check for non-shortest form (invalid UTF-8)... |
ef416fc2 | 595 | */ |
e1d6a774 | 596 | |
597 | if (ch32 < 0x10000) | |
598 | return (-1); | |
599 | ||
600 | *dest++ = ch32; | |
ef416fc2 | 601 | } |
602 | else | |
603 | { | |
604 | /* | |
e1d6a774 | 605 | * More than 4-octet (invalid UTF-8 sequence)... |
ef416fc2 | 606 | */ |
e1d6a774 | 607 | |
ef416fc2 | 608 | return (-1); |
609 | } | |
610 | ||
611 | /* | |
612 | * Check for UTF-16 surrogate (illegal UTF-8)... | |
613 | */ | |
ef416fc2 | 614 | |
2abf387c | 615 | if (ch32 >= 0xd800 && ch32 <= 0xdfff) |
ef416fc2 | 616 | return (-1); |
617 | } | |
e1d6a774 | 618 | |
ef416fc2 | 619 | *dest = 0; |
e1d6a774 | 620 | |
ef416fc2 | 621 | return (i); |
622 | } | |
623 | ||
e1d6a774 | 624 | |
ef416fc2 | 625 | /* |
626 | * 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8. | |
627 | * | |
628 | * 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows... | |
629 | * | |
630 | * UTF-32 char UTF-8 char(s) | |
631 | * -------------------------------------------------- | |
e1d6a774 | 632 | * 0 to 127 = 0xxxxxxx (US-ASCII) |
ef416fc2 | 633 | * 128 to 2047 = 110xxxxx 10yyyyyy |
634 | * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz | |
e1d6a774 | 635 | * > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx |
ef416fc2 | 636 | * |
637 | * UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4, | |
638 | * which would convert to five- or six-octet UTF-8 sequences... | |
ef416fc2 | 639 | */ |
e1d6a774 | 640 | |
641 | int /* O - Count or -1 on error */ | |
642 | cupsUTF32ToUTF8( | |
643 | cups_utf8_t *dest, /* O - Target string */ | |
644 | const cups_utf32_t *src, /* I - Source string */ | |
645 | const int maxout) /* I - Max output */ | |
ef416fc2 | 646 | { |
e1d6a774 | 647 | cups_utf8_t *start; /* Start of destination string */ |
648 | int i; /* Looping variable */ | |
649 | int swap; /* Byte-swap input to output */ | |
650 | cups_utf32_t ch; /* Character value */ | |
651 | ||
ef416fc2 | 652 | |
653 | /* | |
654 | * Check for valid arguments and clear output... | |
655 | */ | |
e1d6a774 | 656 | |
657 | if (dest) | |
658 | *dest = '\0'; | |
659 | ||
660 | if (!dest || !src || maxout < 1) | |
ef416fc2 | 661 | return (-1); |
ef416fc2 | 662 | |
663 | /* | |
664 | * Check for leading BOM in UTF-32 and inverted BOM... | |
665 | */ | |
e1d6a774 | 666 | |
667 | start = dest; | |
668 | swap = *src == 0xfffe0000; | |
669 | ||
670 | if (*src == 0xfffe0000 || *src == 0xfeff) | |
671 | src ++; | |
ef416fc2 | 672 | |
673 | /* | |
674 | * Convert input UTF-32 to output UTF-8... | |
675 | */ | |
e1d6a774 | 676 | |
677 | for (i = maxout - 1; *src && i > 0;) | |
ef416fc2 | 678 | { |
e1d6a774 | 679 | ch = *src++; |
ef416fc2 | 680 | |
681 | /* | |
682 | * Byte swap input UTF-32, if necessary... | |
e1d6a774 | 683 | * (only byte-swapping 24 of 32 bits) |
ef416fc2 | 684 | */ |
e1d6a774 | 685 | |
ef416fc2 | 686 | if (swap) |
687 | ch = ((ch >> 24) | ((ch >> 8) & 0xff00) | ((ch << 8) & 0xff0000)); | |
688 | ||
689 | /* | |
e1d6a774 | 690 | * Check for beyond Plane 16 (invalid UTF-32)... |
ef416fc2 | 691 | */ |
ef416fc2 | 692 | |
ef416fc2 | 693 | if (ch > 0x10ffff) |
694 | return (-1); | |
695 | ||
ef416fc2 | 696 | /* |
697 | * Convert UTF-32 character to UTF-8 character(s)... | |
698 | */ | |
e1d6a774 | 699 | |
700 | if (ch < 0x80) | |
ef416fc2 | 701 | { |
702 | /* | |
703 | * One-octet UTF-8 <= 127 (US-ASCII)... | |
704 | */ | |
e1d6a774 | 705 | |
706 | *dest++ = (cups_utf8_t)ch; | |
707 | i --; | |
ef416fc2 | 708 | } |
e1d6a774 | 709 | else if (ch < 0x800) |
ef416fc2 | 710 | { |
711 | /* | |
712 | * Two-octet UTF-8 <= 2047 (Latin-x)... | |
713 | */ | |
e1d6a774 | 714 | |
715 | if (i < 2) | |
716 | return (-1); | |
717 | ||
718 | *dest++ = (cups_utf8_t)(0xc0 | ((ch >> 6) & 0x1f)); | |
719 | *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f)); | |
720 | i -= 2; | |
ef416fc2 | 721 | } |
e1d6a774 | 722 | else if (ch < 0x10000) |
ef416fc2 | 723 | { |
724 | /* | |
725 | * Three-octet UTF-8 <= 65535 (Plane 0 - BMP)... | |
726 | */ | |
e1d6a774 | 727 | |
728 | if (i < 3) | |
729 | return (-1); | |
730 | ||
731 | *dest++ = (cups_utf8_t)(0xe0 | ((ch >> 12) & 0x0f)); | |
732 | *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f)); | |
733 | *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f)); | |
734 | i -= 3; | |
735 | } | |
736 | else | |
737 | { | |
738 | /* | |
739 | * Four-octet UTF-8... | |
740 | */ | |
741 | ||
742 | if (i < 4) | |
743 | return (-1); | |
744 | ||
745 | *dest++ = (cups_utf8_t)(0xf0 | ((ch >> 18) & 0x07)); | |
746 | *dest++ = (cups_utf8_t)(0x80 | ((ch >> 12) & 0x3f)); | |
747 | *dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f)); | |
748 | *dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f)); | |
749 | i -= 4; | |
ef416fc2 | 750 | } |
751 | } | |
e1d6a774 | 752 | |
ef416fc2 | 753 | *dest = '\0'; |
e1d6a774 | 754 | |
755 | return ((int)(dest - start)); | |
ef416fc2 | 756 | } |
757 | ||
e1d6a774 | 758 | |
ef416fc2 | 759 | /* |
e1d6a774 | 760 | * 'compare_wide()' - Compare key for wide (VBCS) match. |
761 | */ | |
762 | ||
763 | static int | |
764 | compare_wide(const void *k1, /* I - Key char */ | |
765 | const void *k2) /* I - Map char */ | |
766 | { | |
767 | cups_vbcs_t key; /* Legacy key character */ | |
768 | cups_vbcs_t map; /* Legacy map character */ | |
769 | ||
770 | ||
771 | key = *((cups_vbcs_t *)k1); | |
772 | map = ((_cups_wide2uni_t *)k2)->widechar; | |
773 | ||
774 | return ((int)(key - map)); | |
775 | } | |
776 | ||
777 | ||
778 | /* | |
779 | * 'conv_sbcs_to_utf8()' - Convert legacy SBCS to UTF-8. | |
ef416fc2 | 780 | */ |
e1d6a774 | 781 | |
782 | static int /* O - Count or -1 on error */ | |
783 | conv_sbcs_to_utf8( | |
784 | cups_utf8_t *dest, /* O - Target string */ | |
785 | const cups_sbcs_t *src, /* I - Source string */ | |
786 | int maxout, /* I - Max output */ | |
787 | const cups_encoding_t encoding) /* I - Encoding */ | |
ef416fc2 | 788 | { |
e1d6a774 | 789 | _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */ |
790 | cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */ | |
791 | cups_sbcs_t legchar; /* Legacy character value */ | |
792 | cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */ | |
793 | *workptr; /* Pointer into string */ | |
794 | ||
ef416fc2 | 795 | |
796 | /* | |
e1d6a774 | 797 | * Find legacy charset map in cache... |
ef416fc2 | 798 | */ |
e1d6a774 | 799 | |
d6ae789d | 800 | if ((cmap = (_cups_cmap_t *)get_charmap(encoding)) == NULL) |
ef416fc2 | 801 | return (-1); |
ef416fc2 | 802 | |
803 | /* | |
e1d6a774 | 804 | * Convert input legacy charset to internal UCS-4 (and insert BOM)... |
ef416fc2 | 805 | */ |
ef416fc2 | 806 | |
e1d6a774 | 807 | work[0] = 0xfeff; |
808 | for (workptr = work + 1; *src && workptr < (work + CUPS_MAX_USTRING - 1);) | |
ef416fc2 | 809 | { |
e1d6a774 | 810 | legchar = *src++; |
ef416fc2 | 811 | |
812 | /* | |
e1d6a774 | 813 | * Convert ASCII verbatim (optimization)... |
ef416fc2 | 814 | */ |
ef416fc2 | 815 | |
e1d6a774 | 816 | if (legchar < 0x80) |
817 | *workptr++ = (cups_utf32_t)legchar; | |
818 | else | |
ef416fc2 | 819 | { |
e1d6a774 | 820 | /* |
821 | * Convert unknown character to Replacement Character... | |
822 | */ | |
ef416fc2 | 823 | |
e1d6a774 | 824 | crow = cmap->char2uni + legchar; |
825 | ||
826 | if (!*crow) | |
827 | *workptr++ = 0xfffd; | |
828 | else | |
829 | *workptr++ = (cups_utf32_t)*crow; | |
ef416fc2 | 830 | } |
ef416fc2 | 831 | } |
e1d6a774 | 832 | |
833 | *workptr = 0; | |
834 | ||
835 | /* | |
836 | * Convert internal UCS-4 to output UTF-8 (and delete BOM)... | |
837 | */ | |
838 | ||
d6ae789d | 839 | cmap->used --; |
e1d6a774 | 840 | |
841 | return (cupsUTF32ToUTF8(dest, work, maxout)); | |
ef416fc2 | 842 | } |
843 | ||
e1d6a774 | 844 | |
ef416fc2 | 845 | /* |
e1d6a774 | 846 | * 'conv_utf8_to_sbcs()' - Convert UTF-8 to legacy SBCS. |
ef416fc2 | 847 | */ |
e1d6a774 | 848 | |
849 | static int /* O - Count or -1 on error */ | |
850 | conv_utf8_to_sbcs( | |
851 | cups_sbcs_t *dest, /* O - Target string */ | |
852 | const cups_utf8_t *src, /* I - Source string */ | |
853 | int maxout, /* I - Max output */ | |
854 | const cups_encoding_t encoding) /* I - Encoding */ | |
ef416fc2 | 855 | { |
e1d6a774 | 856 | cups_sbcs_t *start; /* Start of destination string */ |
857 | _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */ | |
858 | cups_sbcs_t *srow; /* Pointer to SBCS row in 'uni2char' */ | |
859 | cups_utf32_t unichar; /* Character value */ | |
860 | cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */ | |
861 | *workptr; /* Pointer into string */ | |
862 | ||
ef416fc2 | 863 | |
864 | /* | |
e1d6a774 | 865 | * Find legacy charset map in cache... |
ef416fc2 | 866 | */ |
e1d6a774 | 867 | |
d6ae789d | 868 | if ((cmap = (_cups_cmap_t *)get_charmap(encoding)) == NULL) |
ef416fc2 | 869 | return (-1); |
ef416fc2 | 870 | |
871 | /* | |
e1d6a774 | 872 | * Convert input UTF-8 to internal UCS-4 (and insert BOM)... |
ef416fc2 | 873 | */ |
e1d6a774 | 874 | |
875 | if (cupsUTF8ToUTF32(work, src, CUPS_MAX_USTRING) < 0) | |
876 | return (-1); | |
ef416fc2 | 877 | |
878 | /* | |
e1d6a774 | 879 | * Convert internal UCS-4 to SBCS legacy charset (and delete BOM)... |
ef416fc2 | 880 | */ |
e1d6a774 | 881 | |
882 | for (workptr = work + 1, start = dest; *workptr && maxout > 1; maxout --) | |
ef416fc2 | 883 | { |
e1d6a774 | 884 | unichar = *workptr++; |
885 | if (!unichar) | |
ef416fc2 | 886 | break; |
ef416fc2 | 887 | |
888 | /* | |
e1d6a774 | 889 | * Convert ASCII verbatim (optimization)... |
ef416fc2 | 890 | */ |
ef416fc2 | 891 | |
e1d6a774 | 892 | if (unichar < 0x80) |
893 | { | |
894 | *dest++ = (cups_sbcs_t)unichar; | |
895 | continue; | |
896 | } | |
ef416fc2 | 897 | |
898 | /* | |
e1d6a774 | 899 | * Convert unknown character to visible replacement... |
ef416fc2 | 900 | */ |
ef416fc2 | 901 | |
e1d6a774 | 902 | srow = cmap->uni2char[(int)((unichar >> 8) & 0xff)]; |
ef416fc2 | 903 | |
e1d6a774 | 904 | if (srow) |
905 | srow += (int)(unichar & 0xff); | |
ef416fc2 | 906 | |
e1d6a774 | 907 | if (!srow || !*srow) |
908 | *dest++ = '?'; | |
909 | else | |
910 | *dest++ = *srow; | |
ef416fc2 | 911 | } |
ef416fc2 | 912 | |
e1d6a774 | 913 | *dest = '\0'; |
914 | ||
d6ae789d | 915 | cmap->used --; |
e1d6a774 | 916 | |
917 | return ((int)(dest - start)); | |
ef416fc2 | 918 | } |
919 | ||
e1d6a774 | 920 | |
ef416fc2 | 921 | /* |
e1d6a774 | 922 | * 'conv_utf8_to_vbcs()' - Convert UTF-8 to legacy DBCS/VBCS. |
ef416fc2 | 923 | */ |
e1d6a774 | 924 | |
925 | static int /* O - Count or -1 on error */ | |
926 | conv_utf8_to_vbcs( | |
927 | cups_sbcs_t *dest, /* O - Target string */ | |
928 | const cups_utf8_t *src, /* I - Source string */ | |
929 | int maxout, /* I - Max output */ | |
930 | const cups_encoding_t encoding) /* I - Encoding */ | |
ef416fc2 | 931 | { |
e1d6a774 | 932 | cups_sbcs_t *start; /* Start of destination string */ |
933 | _cups_vmap_t *vmap; /* Legacy DBCS / Unicode Charset Map */ | |
934 | cups_vbcs_t *vrow; /* Pointer to VBCS row in 'uni2char' */ | |
935 | cups_utf32_t unichar; /* Character value */ | |
936 | cups_vbcs_t legchar; /* Legacy character value */ | |
937 | cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */ | |
938 | *workptr; /* Pointer into string */ | |
ef416fc2 | 939 | |
ef416fc2 | 940 | |
941 | /* | |
e1d6a774 | 942 | * Find legacy charset map in cache... |
ef416fc2 | 943 | */ |
ef416fc2 | 944 | |
d6ae789d | 945 | if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL) |
e1d6a774 | 946 | return (-1); |
ef416fc2 | 947 | |
948 | /* | |
e1d6a774 | 949 | * Convert input UTF-8 to internal UCS-4 (and insert BOM)... |
ef416fc2 | 950 | */ |
e1d6a774 | 951 | |
952 | if (cupsUTF8ToUTF32(work, src, CUPS_MAX_USTRING) < 0) | |
953 | return (-1); | |
ef416fc2 | 954 | |
955 | /* | |
e1d6a774 | 956 | * Convert internal UCS-4 to VBCS legacy charset (and delete BOM)... |
ef416fc2 | 957 | */ |
e1d6a774 | 958 | |
959 | for (start = dest, workptr = work + 1; *workptr && maxout > 1; maxout --) | |
ef416fc2 | 960 | { |
e1d6a774 | 961 | unichar = *workptr++; |
962 | if (!unichar) | |
ef416fc2 | 963 | break; |
ef416fc2 | 964 | |
965 | /* | |
e1d6a774 | 966 | * Convert ASCII verbatim (optimization)... |
ef416fc2 | 967 | */ |
e1d6a774 | 968 | |
969 | if (unichar < 0x80) | |
970 | { | |
b86bc4cf | 971 | *dest++ = (cups_sbcs_t)unichar; |
e1d6a774 | 972 | continue; |
973 | } | |
ef416fc2 | 974 | |
975 | /* | |
e1d6a774 | 976 | * Convert unknown character to visible replacement... |
ef416fc2 | 977 | */ |
e1d6a774 | 978 | |
979 | vrow = vmap->uni2char[(int)((unichar >> 8) & 0xff)]; | |
980 | ||
981 | if (vrow) | |
982 | vrow += (int)(unichar & 0xff); | |
983 | ||
984 | if (!vrow || !*vrow) | |
985 | legchar = (cups_vbcs_t)'?'; | |
986 | else | |
987 | legchar = (cups_vbcs_t)*vrow; | |
ef416fc2 | 988 | |
989 | /* | |
e1d6a774 | 990 | * Save n-byte legacy character... |
ef416fc2 | 991 | */ |
e1d6a774 | 992 | |
993 | if (legchar > 0xffffff) | |
ef416fc2 | 994 | { |
e1d6a774 | 995 | if (maxout < 5) |
996 | return (-1); | |
997 | ||
998 | *dest++ = (cups_sbcs_t)(legchar >> 24); | |
999 | *dest++ = (cups_sbcs_t)(legchar >> 16); | |
1000 | *dest++ = (cups_sbcs_t)(legchar >> 8); | |
1001 | *dest++ = (cups_sbcs_t)legchar; | |
1002 | ||
1003 | maxout -= 3; | |
ef416fc2 | 1004 | } |
e1d6a774 | 1005 | else if (legchar > 0xffff) |
1006 | { | |
1007 | if (maxout < 4) | |
1008 | return (-1); | |
ef416fc2 | 1009 | |
e1d6a774 | 1010 | *dest++ = (cups_sbcs_t)(legchar >> 16); |
1011 | *dest++ = (cups_sbcs_t)(legchar >> 8); | |
1012 | *dest++ = (cups_sbcs_t)legchar; | |
ef416fc2 | 1013 | |
e1d6a774 | 1014 | maxout -= 2; |
1015 | } | |
1016 | else if (legchar > 0xff) | |
1017 | { | |
1018 | *dest++ = (cups_sbcs_t)(legchar >> 8); | |
1019 | *dest++ = (cups_sbcs_t)legchar; | |
1020 | ||
1021 | maxout --; | |
1022 | } | |
ef416fc2 | 1023 | } |
e1d6a774 | 1024 | |
1025 | *dest = '\0'; | |
1026 | ||
d6ae789d | 1027 | vmap->used --; |
e1d6a774 | 1028 | |
1029 | return ((int)(dest - start)); | |
ef416fc2 | 1030 | } |
1031 | ||
e1d6a774 | 1032 | |
ef416fc2 | 1033 | /* |
e1d6a774 | 1034 | * 'conv_vbcs_to_utf8()' - Convert legacy DBCS/VBCS to UTF-8. |
ef416fc2 | 1035 | */ |
e1d6a774 | 1036 | |
1037 | static int /* O - Count or -1 on error */ | |
1038 | conv_vbcs_to_utf8( | |
1039 | cups_utf8_t *dest, /* O - Target string */ | |
1040 | const cups_sbcs_t *src, /* I - Source string */ | |
1041 | int maxout, /* I - Max output */ | |
1042 | const cups_encoding_t encoding) /* I - Encoding */ | |
ef416fc2 | 1043 | { |
e1d6a774 | 1044 | _cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */ |
1045 | cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */ | |
1046 | _cups_wide2uni_t *wide2uni; /* Pointer to row in 'wide2uni' */ | |
1047 | cups_sbcs_t leadchar; /* Lead char of n-byte legacy char */ | |
1048 | cups_vbcs_t legchar; /* Legacy character value */ | |
1049 | cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */ | |
1050 | *workptr; /* Pointer into string */ | |
ef416fc2 | 1051 | |
ef416fc2 | 1052 | |
1053 | /* | |
e1d6a774 | 1054 | * Find legacy charset map in cache... |
ef416fc2 | 1055 | */ |
ef416fc2 | 1056 | |
d6ae789d | 1057 | if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL) |
e1d6a774 | 1058 | return (-1); |
ef416fc2 | 1059 | |
1060 | /* | |
e1d6a774 | 1061 | * Convert input legacy charset to internal UCS-4 (and insert BOM)... |
ef416fc2 | 1062 | */ |
ef416fc2 | 1063 | |
e1d6a774 | 1064 | work[0] = 0xfeff; |
1065 | for (workptr = work + 1; *src && workptr < (work + CUPS_MAX_USTRING - 1);) | |
ef416fc2 | 1066 | { |
e1d6a774 | 1067 | legchar = *src++; |
1068 | leadchar = (cups_sbcs_t)legchar; | |
ef416fc2 | 1069 | |
1070 | /* | |
e1d6a774 | 1071 | * Convert ASCII verbatim (optimization)... |
ef416fc2 | 1072 | */ |
ef416fc2 | 1073 | |
e1d6a774 | 1074 | if (legchar < 0x80) |
ef416fc2 | 1075 | { |
e1d6a774 | 1076 | *workptr++ = (cups_utf32_t)legchar; |
1077 | continue; | |
ef416fc2 | 1078 | } |
1079 | ||
1080 | /* | |
e1d6a774 | 1081 | * Convert 2-byte legacy character... |
ef416fc2 | 1082 | */ |
e1d6a774 | 1083 | |
1084 | if (vmap->lead2char[(int)leadchar] == leadchar) | |
ef416fc2 | 1085 | { |
e1d6a774 | 1086 | if (!*src) |
1087 | return (-1); | |
1088 | ||
1089 | legchar = (legchar << 8) | *src++; | |
1090 | ||
ef416fc2 | 1091 | /* |
e1d6a774 | 1092 | * Convert unknown character to Replacement Character... |
ef416fc2 | 1093 | */ |
e1d6a774 | 1094 | |
1095 | crow = vmap->char2uni[(int)((legchar >> 8) & 0xff)]; | |
1096 | if (crow) | |
1097 | crow += (int) (legchar & 0xff); | |
1098 | ||
1099 | if (!crow || !*crow) | |
1100 | *workptr++ = 0xfffd; | |
1101 | else | |
1102 | *workptr++ = (cups_utf32_t)*crow; | |
1103 | continue; | |
ef416fc2 | 1104 | } |
1105 | ||
1106 | /* | |
e1d6a774 | 1107 | * Fetch 3-byte or 4-byte legacy character... |
ef416fc2 | 1108 | */ |
e1d6a774 | 1109 | |
1110 | if (vmap->lead3char[(int)leadchar] == leadchar) | |
ef416fc2 | 1111 | { |
e1d6a774 | 1112 | if (!*src || !src[1]) |
1113 | return (-1); | |
1114 | ||
1115 | legchar = (legchar << 8) | *src++; | |
1116 | legchar = (legchar << 8) | *src++; | |
ef416fc2 | 1117 | } |
e1d6a774 | 1118 | else if (vmap->lead4char[(int)leadchar] == leadchar) |
1119 | { | |
1120 | if (!*src || !src[1] || !src[2]) | |
1121 | return (-1); | |
1122 | ||
1123 | legchar = (legchar << 8) | *src++; | |
1124 | legchar = (legchar << 8) | *src++; | |
1125 | legchar = (legchar << 8) | *src++; | |
1126 | } | |
1127 | else | |
1128 | return (-1); | |
ef416fc2 | 1129 | |
1130 | /* | |
e1d6a774 | 1131 | * Find 3-byte or 4-byte legacy character... |
ef416fc2 | 1132 | */ |
e1d6a774 | 1133 | |
1134 | wide2uni = (_cups_wide2uni_t *)bsearch(&legchar, | |
1135 | vmap->wide2uni, | |
1136 | vmap->widecount, | |
1137 | sizeof(_cups_wide2uni_t), | |
1138 | compare_wide); | |
ef416fc2 | 1139 | |
1140 | /* | |
e1d6a774 | 1141 | * Convert unknown character to Replacement Character... |
ef416fc2 | 1142 | */ |
e1d6a774 | 1143 | |
1144 | if (!wide2uni || !wide2uni->unichar) | |
1145 | *workptr++ = 0xfffd; | |
1146 | else | |
1147 | *workptr++ = wide2uni->unichar; | |
ef416fc2 | 1148 | } |
e1d6a774 | 1149 | |
1150 | *workptr = 0; | |
1151 | ||
d6ae789d | 1152 | vmap->used --; |
e1d6a774 | 1153 | |
1154 | /* | |
1155 | * Convert internal UCS-4 to output UTF-8 (and delete BOM)... | |
1156 | */ | |
1157 | ||
1158 | return (cupsUTF32ToUTF8(dest, work, maxout)); | |
ef416fc2 | 1159 | } |
1160 | ||
e1d6a774 | 1161 | |
ef416fc2 | 1162 | /* |
e1d6a774 | 1163 | * 'free_sbcs_charmap()' - Free memory used by a single byte character set. |
ef416fc2 | 1164 | */ |
e1d6a774 | 1165 | |
1166 | static void | |
1167 | free_sbcs_charmap(_cups_cmap_t *cmap) /* I - Character set */ | |
ef416fc2 | 1168 | { |
e1d6a774 | 1169 | int i; /* Looping variable */ |
ef416fc2 | 1170 | |
ef416fc2 | 1171 | |
e1d6a774 | 1172 | for (i = 0; i < 256; i ++) |
1173 | if (cmap->uni2char[i]) | |
1174 | free(cmap->uni2char[i]); | |
1175 | ||
1176 | free(cmap); | |
1177 | } | |
1178 | ||
1179 | ||
1180 | /* | |
1181 | * 'free_vbcs_charmap()' - Free memory used by a variable byte character set. | |
1182 | */ | |
1183 | ||
1184 | static void | |
1185 | free_vbcs_charmap(_cups_vmap_t *vmap) /* I - Character set */ | |
1186 | { | |
1187 | int i; /* Looping variable */ | |
1188 | ||
1189 | ||
1190 | for (i = 0; i < 256; i ++) | |
1191 | if (vmap->char2uni[i]) | |
1192 | free(vmap->char2uni[i]); | |
1193 | ||
1194 | for (i = 0; i < 256; i ++) | |
1195 | if (vmap->uni2char[i]) | |
1196 | free(vmap->uni2char[i]); | |
1197 | ||
1198 | if (vmap->wide2uni) | |
1199 | free(vmap->wide2uni); | |
1200 | ||
1201 | free(vmap); | |
1202 | } | |
1203 | ||
1204 | ||
d6ae789d | 1205 | /* |
1206 | * 'get_charmap()' - Lookup or get a character set map (private). | |
1207 | * | |
1208 | * This code handles single-byte (SBCS), double-byte (DBCS), and | |
1209 | * variable-byte (VBCS) character sets _without_ charset escapes... | |
1210 | * This code does not handle multiple-byte character sets (MBCS) | |
1211 | * (such as ISO-2022-JP) with charset switching via escapes... | |
1212 | */ | |
1213 | ||
1214 | ||
d09495fa | 1215 | static void * /* O - Charset map pointer */ |
d6ae789d | 1216 | get_charmap( |
1217 | const cups_encoding_t encoding) /* I - Encoding */ | |
1218 | { | |
1219 | char filename[1024]; /* Filename for charset map file */ | |
1220 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
1221 | ||
1222 | ||
1223 | /* | |
1224 | * Get the data directory and charset map name... | |
1225 | */ | |
1226 | ||
1227 | snprintf(filename, sizeof(filename), "%s/charmaps/%s.txt", | |
1228 | cg->cups_datadir, _cupsEncodingName(encoding)); | |
1229 | ||
1230 | DEBUG_printf((" filename=\"%s\"\n", filename)); | |
1231 | ||
1232 | /* | |
1233 | * Read charset map input file into cache... | |
1234 | */ | |
1235 | ||
1236 | if (encoding < CUPS_ENCODING_SBCS_END) | |
1237 | return (get_sbcs_charmap(encoding, filename)); | |
1238 | else if (encoding < CUPS_ENCODING_VBCS_END) | |
1239 | return (get_vbcs_charmap(encoding, filename)); | |
1240 | else | |
1241 | return (NULL); | |
1242 | } | |
1243 | ||
1244 | ||
e1d6a774 | 1245 | /* |
1246 | * 'get_charmap_count()' - Count lines in a charmap file. | |
1247 | */ | |
1248 | ||
1249 | static int /* O - Count or -1 on error */ | |
1250 | get_charmap_count(cups_file_t *fp) /* I - File to read from */ | |
1251 | { | |
1252 | int count; /* Number of lines */ | |
1253 | char line[256]; /* Line from input map file */ | |
ef416fc2 | 1254 | |
ef416fc2 | 1255 | |
1256 | /* | |
e1d6a774 | 1257 | * Count lines in map input file... |
ef416fc2 | 1258 | */ |
ef416fc2 | 1259 | |
e1d6a774 | 1260 | count = 0; |
ef416fc2 | 1261 | |
e1d6a774 | 1262 | while (cupsFileGets(fp, line, sizeof(line))) |
1263 | if (line[0] == '0') | |
1264 | count ++; | |
ef416fc2 | 1265 | |
e1d6a774 | 1266 | /* |
1267 | * Return the number of lines... | |
1268 | */ | |
1269 | ||
1270 | if (count > 0) | |
1271 | return (count); | |
1272 | else | |
1273 | return (-1); | |
ef416fc2 | 1274 | } |
1275 | ||
e1d6a774 | 1276 | |
ef416fc2 | 1277 | /* |
e1d6a774 | 1278 | * 'get_sbcs_charmap()' - Get SBCS Charmap. |
ef416fc2 | 1279 | */ |
e1d6a774 | 1280 | |
1281 | static _cups_cmap_t * /* O - Charmap or 0 on error */ | |
1282 | get_sbcs_charmap( | |
1283 | const cups_encoding_t encoding, /* I - Charmap Encoding */ | |
1284 | const char *filename) /* I - Charmap Filename */ | |
ef416fc2 | 1285 | { |
e1d6a774 | 1286 | unsigned long legchar; /* Legacy character value */ |
1287 | cups_utf32_t unichar; /* Unicode character value */ | |
1288 | _cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */ | |
1289 | cups_file_t *fp; /* Charset map file pointer */ | |
1290 | char *s; /* Line parsing pointer */ | |
1291 | cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */ | |
1292 | cups_sbcs_t *srow; /* Pointer to SBCS row in 'uni2char' */ | |
1293 | char line[256]; /* Line from charset map file */ | |
e1d6a774 | 1294 | |
ef416fc2 | 1295 | |
1296 | /* | |
e1d6a774 | 1297 | * See if we already have this SBCS charset map loaded... |
ef416fc2 | 1298 | */ |
e1d6a774 | 1299 | |
d6ae789d | 1300 | for (cmap = cmap_cache; cmap; cmap = cmap->next) |
e1d6a774 | 1301 | { |
1302 | if (cmap->encoding == encoding) | |
1303 | { | |
1304 | cmap->used ++; | |
1305 | DEBUG_printf((" returning existing cmap=%p\n", cmap)); | |
d6ae789d | 1306 | |
e1d6a774 | 1307 | return ((void *)cmap); |
1308 | } | |
1309 | } | |
ef416fc2 | 1310 | |
1311 | /* | |
e1d6a774 | 1312 | * Open SBCS charset map input file... |
ef416fc2 | 1313 | */ |
e1d6a774 | 1314 | |
1315 | if ((fp = cupsFileOpen(filename, "r")) == NULL) | |
1316 | return (NULL); | |
ef416fc2 | 1317 | |
1318 | /* | |
e1d6a774 | 1319 | * Allocate memory for SBCS charset map... |
ef416fc2 | 1320 | */ |
e1d6a774 | 1321 | |
1322 | if ((cmap = (_cups_cmap_t *)calloc(1, sizeof(_cups_cmap_t))) == NULL) | |
1323 | { | |
1324 | cupsFileClose(fp); | |
1325 | DEBUG_puts(" Unable to allocate memory!"); | |
d6ae789d | 1326 | |
e1d6a774 | 1327 | return (NULL); |
1328 | } | |
1329 | ||
1330 | cmap->used ++; | |
1331 | cmap->encoding = encoding; | |
ef416fc2 | 1332 | |
1333 | /* | |
e1d6a774 | 1334 | * Save SBCS charset map into memory for transcoding... |
ef416fc2 | 1335 | */ |
e1d6a774 | 1336 | |
1337 | while (cupsFileGets(fp, line, sizeof(line))) | |
ef416fc2 | 1338 | { |
e1d6a774 | 1339 | if (line[0] != '0') |
1340 | continue; | |
1341 | ||
1342 | legchar = strtol(line, &s, 16); | |
1343 | if (legchar < 0 || legchar > 0xff) | |
1344 | goto sbcs_error; | |
1345 | ||
1346 | unichar = strtol(s, NULL, 16); | |
1347 | if (unichar < 0 || unichar > 0xffff) | |
1348 | goto sbcs_error; | |
ef416fc2 | 1349 | |
1350 | /* | |
e1d6a774 | 1351 | * Save legacy to Unicode mapping in direct lookup table... |
ef416fc2 | 1352 | */ |
e1d6a774 | 1353 | |
1354 | crow = cmap->char2uni + legchar; | |
1355 | *crow = (cups_ucs2_t)(unichar & 0xffff); | |
ef416fc2 | 1356 | |
1357 | /* | |
e1d6a774 | 1358 | * Save Unicode to legacy mapping in indirect lookup table... |
ef416fc2 | 1359 | */ |
e1d6a774 | 1360 | |
1361 | srow = cmap->uni2char[(unichar >> 8) & 0xff]; | |
1362 | if (!srow) | |
ef416fc2 | 1363 | { |
e1d6a774 | 1364 | srow = (cups_sbcs_t *)calloc(256, sizeof(cups_sbcs_t)); |
1365 | if (!srow) | |
1366 | goto sbcs_error; | |
1367 | ||
1368 | cmap->uni2char[(unichar >> 8) & 0xff] = srow; | |
ef416fc2 | 1369 | } |
1370 | ||
e1d6a774 | 1371 | srow += unichar & 0xff; |
1372 | ||
ef416fc2 | 1373 | /* |
e1d6a774 | 1374 | * Convert Replacement Character to visible replacement... |
ef416fc2 | 1375 | */ |
e1d6a774 | 1376 | |
1377 | if (unichar == 0xfffd) | |
1378 | legchar = (unsigned long)'?'; | |
ef416fc2 | 1379 | |
1380 | /* | |
e1d6a774 | 1381 | * First (oldest) legacy character uses Unicode mapping cell... |
ef416fc2 | 1382 | */ |
ef416fc2 | 1383 | |
e1d6a774 | 1384 | if (!*srow) |
1385 | *srow = (cups_sbcs_t)legchar; | |
1386 | } | |
ef416fc2 | 1387 | |
e1d6a774 | 1388 | cupsFileClose(fp); |
1389 | ||
ef416fc2 | 1390 | /* |
e1d6a774 | 1391 | * Add it to the cache and return... |
ef416fc2 | 1392 | */ |
e1d6a774 | 1393 | |
d6ae789d | 1394 | cmap->next = cmap_cache; |
1395 | cmap_cache = cmap; | |
e1d6a774 | 1396 | |
1397 | DEBUG_printf((" returning new cmap=%p\n", cmap)); | |
1398 | ||
1399 | return (cmap); | |
ef416fc2 | 1400 | |
1401 | /* | |
e1d6a774 | 1402 | * If we get here, there was an error in the cmap file... |
ef416fc2 | 1403 | */ |
e1d6a774 | 1404 | |
1405 | sbcs_error: | |
1406 | ||
1407 | free_sbcs_charmap(cmap); | |
1408 | ||
1409 | cupsFileClose(fp); | |
1410 | ||
1411 | DEBUG_puts(" Error, returning NULL!"); | |
1412 | ||
1413 | return (NULL); | |
1414 | } | |
1415 | ||
1416 | ||
1417 | /* | |
1418 | * 'get_vbcs_charmap()' - Get DBCS/VBCS Charmap. | |
1419 | */ | |
1420 | ||
1421 | static _cups_vmap_t * /* O - Charmap or 0 on error */ | |
1422 | get_vbcs_charmap( | |
1423 | const cups_encoding_t encoding, /* I - Charmap Encoding */ | |
1424 | const char *filename) /* I - Charmap Filename */ | |
1425 | { | |
1426 | _cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */ | |
1427 | cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */ | |
1428 | cups_vbcs_t *vrow; /* Pointer to VBCS row in 'uni2char' */ | |
1429 | _cups_wide2uni_t *wide2uni; /* Pointer to row in 'wide2uni' */ | |
1430 | cups_sbcs_t leadchar; /* Lead char of 2-byte legacy char */ | |
1431 | unsigned long legchar; /* Legacy character value */ | |
1432 | cups_utf32_t unichar; /* Unicode character value */ | |
1433 | int mapcount; /* Count of lines in charmap file */ | |
1434 | cups_file_t *fp; /* Charset map file pointer */ | |
1435 | char *s; /* Line parsing pointer */ | |
1436 | char line[256]; /* Line from charset map file */ | |
1437 | int i; /* Loop variable */ | |
09a101d6 | 1438 | int legacy; /* 32-bit legacy char */ |
e1d6a774 | 1439 | |
1440 | ||
1441 | DEBUG_printf(("get_vbcs_charmap(encoding=%d, filename=\"%s\")\n", | |
1442 | encoding, filename)); | |
ef416fc2 | 1443 | |
1444 | /* | |
e1d6a774 | 1445 | * See if we already have this DBCS/VBCS charset map loaded... |
ef416fc2 | 1446 | */ |
ef416fc2 | 1447 | |
d6ae789d | 1448 | for (vmap = vmap_cache; vmap; vmap = vmap->next) |
e1d6a774 | 1449 | { |
1450 | if (vmap->encoding == encoding) | |
ef416fc2 | 1451 | { |
e1d6a774 | 1452 | vmap->used ++; |
1453 | DEBUG_printf((" returning existing vmap=%p\n", vmap)); | |
d6ae789d | 1454 | |
e1d6a774 | 1455 | return ((void *)vmap); |
ef416fc2 | 1456 | } |
ef416fc2 | 1457 | } |
ef416fc2 | 1458 | |
1459 | /* | |
e1d6a774 | 1460 | * Open VBCS charset map input file... |
ef416fc2 | 1461 | */ |
ef416fc2 | 1462 | |
e1d6a774 | 1463 | if ((fp = cupsFileOpen(filename, "r")) == NULL) |
1464 | { | |
1465 | DEBUG_printf((" Unable to open file: %s\n", strerror(errno))); | |
d6ae789d | 1466 | |
e1d6a774 | 1467 | return (NULL); |
1468 | } | |
ef416fc2 | 1469 | |
1470 | /* | |
e1d6a774 | 1471 | * Count lines in charmap file... |
ef416fc2 | 1472 | */ |
e1d6a774 | 1473 | |
1474 | if ((mapcount = get_charmap_count(fp)) <= 0) | |
1475 | { | |
1476 | DEBUG_puts(" Unable to get charmap count!"); | |
d6ae789d | 1477 | |
e1d6a774 | 1478 | return (NULL); |
1479 | } | |
1480 | ||
1481 | DEBUG_printf((" mapcount=%d\n", mapcount)); | |
ef416fc2 | 1482 | |
1483 | /* | |
e1d6a774 | 1484 | * Allocate memory for DBCS/VBCS charset map... |
ef416fc2 | 1485 | */ |
e1d6a774 | 1486 | |
1487 | if ((vmap = (_cups_vmap_t *)calloc(1, sizeof(_cups_vmap_t))) == NULL) | |
1488 | { | |
1489 | cupsFileClose(fp); | |
1490 | DEBUG_puts(" Unable to allocate memory!"); | |
d6ae789d | 1491 | |
e1d6a774 | 1492 | return (NULL); |
1493 | } | |
1494 | ||
1495 | vmap->used ++; | |
1496 | vmap->encoding = encoding; | |
ef416fc2 | 1497 | |
1498 | /* | |
e1d6a774 | 1499 | * Save DBCS/VBCS charset map into memory for transcoding... |
ef416fc2 | 1500 | */ |
e1d6a774 | 1501 | |
1502 | leadchar = 0; | |
1503 | wide2uni = NULL; | |
1504 | ||
1505 | cupsFileRewind(fp); | |
1506 | ||
09a101d6 | 1507 | i = 0; |
1508 | legacy = 0; | |
e1d6a774 | 1509 | |
1510 | while (cupsFileGets(fp, line, sizeof(line))) | |
ef416fc2 | 1511 | { |
e1d6a774 | 1512 | if (line[0] != '0') |
1513 | continue; | |
1514 | ||
1515 | legchar = strtoul(line, &s, 16); | |
1516 | if (legchar == ULONG_MAX) | |
1517 | goto vbcs_error; | |
1518 | ||
1519 | unichar = strtol(s, NULL, 16); | |
1520 | if (unichar < 0 || unichar > 0xffff) | |
1521 | goto vbcs_error; | |
1522 | ||
1523 | i ++; | |
1524 | ||
1525 | /* DEBUG_printf((" i=%d, legchar=0x%08lx, unichar=0x%04x\n", i, | |
1526 | legchar, (unsigned)unichar)); */ | |
ef416fc2 | 1527 | |
1528 | /* | |
e1d6a774 | 1529 | * Save lead char of 2/3/4-byte legacy char... |
ef416fc2 | 1530 | */ |
e1d6a774 | 1531 | |
1532 | if (legchar > 0xff && legchar <= 0xffff) | |
ef416fc2 | 1533 | { |
e1d6a774 | 1534 | leadchar = (cups_sbcs_t)(legchar >> 8); |
1535 | vmap->lead2char[leadchar] = leadchar; | |
1536 | } | |
1537 | ||
1538 | if (legchar > 0xffff && legchar <= 0xffffff) | |
1539 | { | |
1540 | leadchar = (cups_sbcs_t)(legchar >> 16); | |
1541 | vmap->lead3char[leadchar] = leadchar; | |
1542 | } | |
1543 | ||
1544 | if (legchar > 0xffffff) | |
1545 | { | |
1546 | leadchar = (cups_sbcs_t)(legchar >> 24); | |
1547 | vmap->lead4char[leadchar] = leadchar; | |
ef416fc2 | 1548 | } |
1549 | ||
1550 | /* | |
e1d6a774 | 1551 | * Save Legacy to Unicode mapping... |
ef416fc2 | 1552 | */ |
e1d6a774 | 1553 | |
1554 | if (legchar <= 0xffff) | |
ef416fc2 | 1555 | { |
ef416fc2 | 1556 | /* |
e1d6a774 | 1557 | * Save DBCS 16-bit to Unicode mapping in indirect lookup table... |
ef416fc2 | 1558 | */ |
e1d6a774 | 1559 | |
1560 | crow = vmap->char2uni[(int)leadchar]; | |
1561 | if (!crow) | |
1562 | { | |
1563 | crow = (cups_ucs2_t *)calloc(256, sizeof(cups_ucs2_t)); | |
1564 | if (!crow) | |
1565 | goto vbcs_error; | |
1566 | ||
1567 | vmap->char2uni[(int)leadchar] = crow; | |
1568 | } | |
1569 | ||
1570 | crow[(int)(legchar & 0xff)] = (cups_ucs2_t)unichar; | |
1571 | } | |
1572 | else | |
1573 | { | |
1574 | /* | |
1575 | * Save VBCS 32-bit to Unicode mapping in sorted list table... | |
1576 | */ | |
1577 | ||
09a101d6 | 1578 | if (!legacy) |
e1d6a774 | 1579 | { |
09a101d6 | 1580 | legacy = 1; |
e1d6a774 | 1581 | vmap->widecount = (mapcount - i + 1); |
1582 | wide2uni = (_cups_wide2uni_t *)calloc(vmap->widecount, | |
1583 | sizeof(_cups_wide2uni_t)); | |
1584 | if (!wide2uni) | |
1585 | goto vbcs_error; | |
1586 | ||
1587 | vmap->wide2uni = wide2uni; | |
1588 | } | |
1589 | ||
1590 | wide2uni->widechar = (cups_vbcs_t)legchar; | |
1591 | wide2uni->unichar = (cups_ucs2_t)unichar; | |
1592 | wide2uni ++; | |
ef416fc2 | 1593 | } |
1594 | ||
1595 | /* | |
e1d6a774 | 1596 | * Save Unicode to legacy mapping in indirect lookup table... |
ef416fc2 | 1597 | */ |
e1d6a774 | 1598 | |
1599 | vrow = vmap->uni2char[(int)((unichar >> 8) & 0xff)]; | |
1600 | if (!vrow) | |
ef416fc2 | 1601 | { |
e1d6a774 | 1602 | vrow = (cups_vbcs_t *)calloc(256, sizeof(cups_vbcs_t)); |
1603 | if (!vrow) | |
1604 | goto vbcs_error; | |
1605 | ||
1606 | vmap->uni2char[(int) ((unichar >> 8) & 0xff)] = vrow; | |
ef416fc2 | 1607 | } |
e1d6a774 | 1608 | |
1609 | vrow += (int)(unichar & 0xff); | |
ef416fc2 | 1610 | |
1611 | /* | |
e1d6a774 | 1612 | * Convert Replacement Character to visible replacement... |
ef416fc2 | 1613 | */ |
e1d6a774 | 1614 | |
1615 | if (unichar == 0xfffd) | |
1616 | legchar = (unsigned long)'?'; | |
ef416fc2 | 1617 | |
1618 | /* | |
e1d6a774 | 1619 | * First (oldest) legacy character uses Unicode mapping cell... |
ef416fc2 | 1620 | */ |
e1d6a774 | 1621 | |
1622 | if (!*vrow) | |
1623 | *vrow = (cups_vbcs_t)legchar; | |
ef416fc2 | 1624 | } |
e1d6a774 | 1625 | |
1626 | vmap->charcount = (i - vmap->widecount); | |
1627 | ||
1628 | cupsFileClose(fp); | |
ef416fc2 | 1629 | |
1630 | /* | |
e1d6a774 | 1631 | * Add it to the cache and return... |
ef416fc2 | 1632 | */ |
ef416fc2 | 1633 | |
d6ae789d | 1634 | vmap->next = vmap_cache; |
1635 | vmap_cache = vmap; | |
e1d6a774 | 1636 | |
1637 | DEBUG_printf((" returning new vmap=%p\n", vmap)); | |
1638 | ||
1639 | return (vmap); | |
1640 | ||
1641 | /* | |
1642 | * If we get here, the file contains errors... | |
1643 | */ | |
1644 | ||
1645 | vbcs_error: | |
1646 | ||
1647 | free_vbcs_charmap(vmap); | |
1648 | ||
1649 | cupsFileClose(fp); | |
1650 | ||
1651 | DEBUG_puts(" Error, returning NULL!"); | |
1652 | ||
1653 | return (NULL); | |
ef416fc2 | 1654 | } |
1655 | ||
1656 | ||
1657 | /* | |
09a101d6 | 1658 | * End of "$Id: transcode.c 6592 2007-06-21 21:04:40Z mike $" |
ef416fc2 | 1659 | */ |