Sync to Wine-20050725:
[reactos.git] / reactos / tools / unicode / wctomb.c
1 /*
2 * WideCharToMultiByte implementation
3 *
4 * Copyright 2000 Alexandre Julliard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <string.h>
22
23 #include "wine/unicode.h"
24
25 /* search for a character in the unicode_compose_table; helper for compose() */
26 static inline int binary_search( WCHAR ch, int low, int high )
27 {
28 extern const WCHAR unicode_compose_table[];
29 while (low <= high)
30 {
31 int pos = (low + high) / 2;
32 if (unicode_compose_table[2*pos] < ch)
33 {
34 low = pos + 1;
35 continue;
36 }
37 if (unicode_compose_table[2*pos] > ch)
38 {
39 high = pos - 1;
40 continue;
41 }
42 return pos;
43 }
44 return -1;
45 }
46
47 /* return the result of the composition of two Unicode chars, or 0 if none */
48 static WCHAR compose( const WCHAR *str )
49 {
50 extern const WCHAR unicode_compose_table[];
51 extern const unsigned int unicode_compose_table_size;
52
53 int idx = 1, low = 0, high = unicode_compose_table_size - 1;
54 for (;;)
55 {
56 int pos = binary_search( str[idx], low, high );
57 if (pos == -1) return 0;
58 if (!idx--) return unicode_compose_table[2*pos+1];
59 low = unicode_compose_table[2*pos+1];
60 high = unicode_compose_table[2*pos+3] - 1;
61 }
62 }
63
64
65 /****************************************************************/
66 /* sbcs support */
67
68 /* check if 'ch' is an acceptable sbcs mapping for 'wch' */
69 static inline int is_valid_sbcs_mapping( const struct sbcs_table *table, int flags,
70 WCHAR wch, unsigned char ch )
71 {
72 if (flags & WC_NO_BEST_FIT_CHARS) return (table->cp2uni[ch] == wch);
73 if (ch != (unsigned char)table->info.def_char) return 1;
74 return (wch == table->info.def_unicode_char);
75 }
76
77 /* query necessary dst length for src string */
78 static int get_length_sbcs( const struct sbcs_table *table, int flags,
79 const WCHAR *src, unsigned int srclen, int *used )
80 {
81 const unsigned char * const uni2cp_low = table->uni2cp_low;
82 const unsigned short * const uni2cp_high = table->uni2cp_high;
83 int ret, tmp;
84 WCHAR composed;
85
86 if (!used) used = &tmp; /* avoid checking on every char */
87 *used = 0;
88
89 for (ret = 0; srclen; ret++, src++, srclen--)
90 {
91 WCHAR wch = *src;
92 unsigned char ch;
93
94 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = compose(src)))
95 {
96 /* now check if we can use the composed char */
97 ch = uni2cp_low[uni2cp_high[composed >> 8] + (composed & 0xff)];
98 if (is_valid_sbcs_mapping( table, flags, composed, ch ))
99 {
100 /* we have a good mapping, use it */
101 src++;
102 srclen--;
103 continue;
104 }
105 /* no mapping for the composed char, check the other flags */
106 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
107 {
108 *used = 1;
109 src++; /* skip the non-spacing char */
110 srclen--;
111 continue;
112 }
113 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
114 {
115 src++;
116 srclen--;
117 }
118 /* WC_SEPCHARS is the default */
119 }
120 if (!*used)
121 {
122 ch = uni2cp_low[uni2cp_high[wch >> 8] + (wch & 0xff)];
123 *used = !is_valid_sbcs_mapping( table, flags, wch, ch );
124 }
125 }
126 return ret;
127 }
128
129 /* wcstombs for single-byte code page */
130 static inline int wcstombs_sbcs( const struct sbcs_table *table,
131 const WCHAR *src, unsigned int srclen,
132 char *dst, unsigned int dstlen )
133 {
134 const unsigned char * const uni2cp_low = table->uni2cp_low;
135 const unsigned short * const uni2cp_high = table->uni2cp_high;
136 int ret = srclen;
137
138 if (dstlen < srclen)
139 {
140 /* buffer too small: fill it up to dstlen and return error */
141 srclen = dstlen;
142 ret = -1;
143 }
144
145 while (srclen >= 16)
146 {
147 dst[0] = uni2cp_low[uni2cp_high[src[0] >> 8] + (src[0] & 0xff)];
148 dst[1] = uni2cp_low[uni2cp_high[src[1] >> 8] + (src[1] & 0xff)];
149 dst[2] = uni2cp_low[uni2cp_high[src[2] >> 8] + (src[2] & 0xff)];
150 dst[3] = uni2cp_low[uni2cp_high[src[3] >> 8] + (src[3] & 0xff)];
151 dst[4] = uni2cp_low[uni2cp_high[src[4] >> 8] + (src[4] & 0xff)];
152 dst[5] = uni2cp_low[uni2cp_high[src[5] >> 8] + (src[5] & 0xff)];
153 dst[6] = uni2cp_low[uni2cp_high[src[6] >> 8] + (src[6] & 0xff)];
154 dst[7] = uni2cp_low[uni2cp_high[src[7] >> 8] + (src[7] & 0xff)];
155 dst[8] = uni2cp_low[uni2cp_high[src[8] >> 8] + (src[8] & 0xff)];
156 dst[9] = uni2cp_low[uni2cp_high[src[9] >> 8] + (src[9] & 0xff)];
157 dst[10] = uni2cp_low[uni2cp_high[src[10] >> 8] + (src[10] & 0xff)];
158 dst[11] = uni2cp_low[uni2cp_high[src[11] >> 8] + (src[11] & 0xff)];
159 dst[12] = uni2cp_low[uni2cp_high[src[12] >> 8] + (src[12] & 0xff)];
160 dst[13] = uni2cp_low[uni2cp_high[src[13] >> 8] + (src[13] & 0xff)];
161 dst[14] = uni2cp_low[uni2cp_high[src[14] >> 8] + (src[14] & 0xff)];
162 dst[15] = uni2cp_low[uni2cp_high[src[15] >> 8] + (src[15] & 0xff)];
163 src += 16;
164 dst += 16;
165 srclen -= 16;
166 }
167
168 /* now handle remaining characters */
169 src += srclen;
170 dst += srclen;
171 switch(srclen)
172 {
173 case 15: dst[-15] = uni2cp_low[uni2cp_high[src[-15] >> 8] + (src[-15] & 0xff)];
174 case 14: dst[-14] = uni2cp_low[uni2cp_high[src[-14] >> 8] + (src[-14] & 0xff)];
175 case 13: dst[-13] = uni2cp_low[uni2cp_high[src[-13] >> 8] + (src[-13] & 0xff)];
176 case 12: dst[-12] = uni2cp_low[uni2cp_high[src[-12] >> 8] + (src[-12] & 0xff)];
177 case 11: dst[-11] = uni2cp_low[uni2cp_high[src[-11] >> 8] + (src[-11] & 0xff)];
178 case 10: dst[-10] = uni2cp_low[uni2cp_high[src[-10] >> 8] + (src[-10] & 0xff)];
179 case 9: dst[-9] = uni2cp_low[uni2cp_high[src[-9] >> 8] + (src[-9] & 0xff)];
180 case 8: dst[-8] = uni2cp_low[uni2cp_high[src[-8] >> 8] + (src[-8] & 0xff)];
181 case 7: dst[-7] = uni2cp_low[uni2cp_high[src[-7] >> 8] + (src[-7] & 0xff)];
182 case 6: dst[-6] = uni2cp_low[uni2cp_high[src[-6] >> 8] + (src[-6] & 0xff)];
183 case 5: dst[-5] = uni2cp_low[uni2cp_high[src[-5] >> 8] + (src[-5] & 0xff)];
184 case 4: dst[-4] = uni2cp_low[uni2cp_high[src[-4] >> 8] + (src[-4] & 0xff)];
185 case 3: dst[-3] = uni2cp_low[uni2cp_high[src[-3] >> 8] + (src[-3] & 0xff)];
186 case 2: dst[-2] = uni2cp_low[uni2cp_high[src[-2] >> 8] + (src[-2] & 0xff)];
187 case 1: dst[-1] = uni2cp_low[uni2cp_high[src[-1] >> 8] + (src[-1] & 0xff)];
188 case 0: break;
189 }
190 return ret;
191 }
192
193 /* slow version of wcstombs_sbcs that handles the various flags */
194 static int wcstombs_sbcs_slow( const struct sbcs_table *table, int flags,
195 const WCHAR *src, unsigned int srclen,
196 char *dst, unsigned int dstlen,
197 const char *defchar, int *used )
198 {
199 const unsigned char * const uni2cp_low = table->uni2cp_low;
200 const unsigned short * const uni2cp_high = table->uni2cp_high;
201 const unsigned char table_default = table->info.def_char & 0xff;
202 unsigned int len;
203 int tmp;
204 WCHAR composed;
205
206 if (!defchar) defchar = (const char*)&table_default;
207 if (!used) used = &tmp; /* avoid checking on every char */
208 *used = 0;
209
210 for (len = dstlen; srclen && len; dst++, len--, src++, srclen--)
211 {
212 WCHAR wch = *src;
213
214 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = compose(src)))
215 {
216 /* now check if we can use the composed char */
217 *dst = uni2cp_low[uni2cp_high[composed >> 8] + (composed & 0xff)];
218 if (is_valid_sbcs_mapping( table, flags, composed, *dst ))
219 {
220 /* we have a good mapping, use it */
221 src++;
222 srclen--;
223 continue;
224 }
225 /* no mapping for the composed char, check the other flags */
226 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
227 {
228 *dst = *defchar;
229 *used = 1;
230 src++; /* skip the non-spacing char */
231 srclen--;
232 continue;
233 }
234 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
235 {
236 src++;
237 srclen--;
238 }
239 /* WC_SEPCHARS is the default */
240 }
241
242 *dst = uni2cp_low[uni2cp_high[wch >> 8] + (wch & 0xff)];
243 if (!is_valid_sbcs_mapping( table, flags, wch, *dst ))
244 {
245 *dst = *defchar;
246 *used = 1;
247 }
248 }
249 if (srclen) return -1; /* overflow */
250 return dstlen - len;
251 }
252
253
254 /****************************************************************/
255 /* dbcs support */
256
257 /* check if 'ch' is an acceptable dbcs mapping for 'wch' */
258 static inline int is_valid_dbcs_mapping( const struct dbcs_table *table, int flags,
259 WCHAR wch, unsigned short ch )
260 {
261 if (ch == table->info.def_char && wch != table->info.def_unicode_char) return 0;
262 if (flags & WC_NO_BEST_FIT_CHARS)
263 {
264 /* check if char maps back to the same Unicode value */
265 if (ch & 0xff00)
266 {
267 unsigned char off = table->cp2uni_leadbytes[ch >> 8];
268 return (table->cp2uni[(off << 8) + (ch & 0xff)] == wch);
269 }
270 return (table->cp2uni[ch & 0xff] == wch);
271 }
272 return 1;
273 }
274
275 /* query necessary dst length for src string */
276 static int get_length_dbcs( const struct dbcs_table *table, int flags,
277 const WCHAR *src, unsigned int srclen,
278 const char *defchar, int *used )
279 {
280 const unsigned short * const uni2cp_low = table->uni2cp_low;
281 const unsigned short * const uni2cp_high = table->uni2cp_high;
282 WCHAR defchar_value = table->info.def_char;
283 WCHAR composed;
284 int len, tmp;
285
286 if (!defchar && !used && !(flags & WC_COMPOSITECHECK))
287 {
288 for (len = 0; srclen; srclen--, src++, len++)
289 {
290 if (uni2cp_low[uni2cp_high[*src >> 8] + (*src & 0xff)] & 0xff00) len++;
291 }
292 return len;
293 }
294
295 if (defchar) defchar_value = defchar[1] ? ((defchar[0] << 8) | defchar[1]) : defchar[0];
296 if (!used) used = &tmp; /* avoid checking on every char */
297 *used = 0;
298 for (len = 0; srclen; len++, srclen--, src++)
299 {
300 unsigned short res;
301 WCHAR wch = *src;
302
303 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = compose(src)))
304 {
305 /* now check if we can use the composed char */
306 res = uni2cp_low[uni2cp_high[composed >> 8] + (composed & 0xff)];
307
308 if (is_valid_dbcs_mapping( table, flags, composed, res ))
309 {
310 /* we have a good mapping for the composed char, use it */
311 if (res & 0xff00) len++;
312 src++;
313 srclen--;
314 continue;
315 }
316 /* no mapping for the composed char, check the other flags */
317 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
318 {
319 if (defchar_value & 0xff00) len++;
320 *used = 1;
321 src++; /* skip the non-spacing char */
322 srclen--;
323 continue;
324 }
325 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
326 {
327 src++;
328 srclen--;
329 }
330 /* WC_SEPCHARS is the default */
331 }
332
333 res = uni2cp_low[uni2cp_high[wch >> 8] + (wch & 0xff)];
334 if (!is_valid_dbcs_mapping( table, flags, wch, res ))
335 {
336 res = defchar_value;
337 *used = 1;
338 }
339 if (res & 0xff00) len++;
340 }
341 return len;
342 }
343
344 /* wcstombs for double-byte code page */
345 static inline int wcstombs_dbcs( const struct dbcs_table *table,
346 const WCHAR *src, unsigned int srclen,
347 char *dst, unsigned int dstlen )
348 {
349 const unsigned short * const uni2cp_low = table->uni2cp_low;
350 const unsigned short * const uni2cp_high = table->uni2cp_high;
351 int len;
352
353 for (len = dstlen; srclen && len; len--, srclen--, src++)
354 {
355 unsigned short res = uni2cp_low[uni2cp_high[*src >> 8] + (*src & 0xff)];
356 if (res & 0xff00)
357 {
358 if (len == 1) break; /* do not output a partial char */
359 len--;
360 *dst++ = res >> 8;
361 }
362 *dst++ = (char)res;
363 }
364 if (srclen) return -1; /* overflow */
365 return dstlen - len;
366 }
367
368 /* slow version of wcstombs_dbcs that handles the various flags */
369 static int wcstombs_dbcs_slow( const struct dbcs_table *table, int flags,
370 const WCHAR *src, unsigned int srclen,
371 char *dst, unsigned int dstlen,
372 const char *defchar, int *used )
373 {
374 const unsigned short * const uni2cp_low = table->uni2cp_low;
375 const unsigned short * const uni2cp_high = table->uni2cp_high;
376 WCHAR defchar_value = table->info.def_char;
377 WCHAR composed;
378 int len, tmp;
379
380 if (defchar) defchar_value = defchar[1] ? ((defchar[0] << 8) | defchar[1]) : defchar[0];
381 if (!used) used = &tmp; /* avoid checking on every char */
382 *used = 0;
383
384 for (len = dstlen; srclen && len; len--, srclen--, src++)
385 {
386 unsigned short res;
387 WCHAR wch = *src;
388
389 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = compose(src)))
390 {
391 /* now check if we can use the composed char */
392 res = uni2cp_low[uni2cp_high[composed >> 8] + (composed & 0xff)];
393
394 if (is_valid_dbcs_mapping( table, flags, composed, res ))
395 {
396 /* we have a good mapping for the composed char, use it */
397 src++;
398 srclen--;
399 goto output_char;
400 }
401 /* no mapping for the composed char, check the other flags */
402 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
403 {
404 res = defchar_value;
405 *used = 1;
406 src++; /* skip the non-spacing char */
407 srclen--;
408 goto output_char;
409 }
410 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
411 {
412 src++;
413 srclen--;
414 }
415 /* WC_SEPCHARS is the default */
416 }
417
418 res = uni2cp_low[uni2cp_high[wch >> 8] + (wch & 0xff)];
419 if (!is_valid_dbcs_mapping( table, flags, wch, res ))
420 {
421 res = defchar_value;
422 *used = 1;
423 }
424
425 output_char:
426 if (res & 0xff00)
427 {
428 if (len == 1) break; /* do not output a partial char */
429 len--;
430 *dst++ = res >> 8;
431 }
432 *dst++ = (char)res;
433 }
434 if (srclen) return -1; /* overflow */
435 return dstlen - len;
436 }
437
438 /* wide char to multi byte string conversion */
439 /* return -1 on dst buffer overflow */
440 int wine_cp_wcstombs( const union cptable *table, int flags,
441 const WCHAR *src, int srclen,
442 char *dst, int dstlen, const char *defchar, int *used )
443 {
444 if (table->info.char_size == 1)
445 {
446 if (flags || defchar || used)
447 {
448 if (!dstlen) return get_length_sbcs( &table->sbcs, flags, src, srclen, used );
449 return wcstombs_sbcs_slow( &table->sbcs, flags, src, srclen,
450 dst, dstlen, defchar, used );
451 }
452 if (!dstlen) return srclen;
453 return wcstombs_sbcs( &table->sbcs, src, srclen, dst, dstlen );
454 }
455 else /* mbcs */
456 {
457 if (!dstlen) return get_length_dbcs( &table->dbcs, flags, src, srclen, defchar, used );
458 if (flags || defchar || used)
459 return wcstombs_dbcs_slow( &table->dbcs, flags, src, srclen,
460 dst, dstlen, defchar, used );
461 return wcstombs_dbcs( &table->dbcs, src, srclen, dst, dstlen );
462 }
463 }
464
465 /* CP_SYMBOL implementation */
466 /* return -1 on dst buffer overflow, -2 on invalid character */
467 int wine_cpsymbol_wcstombs( const WCHAR *src, int srclen, char *dst, int dstlen)
468 {
469 int len, i;
470 if( dstlen == 0) return srclen;
471 len = dstlen > srclen ? srclen : dstlen;
472 for( i = 0; i < len; i++)
473 {
474 WCHAR w = src [ i ];
475 if( w < 0x20 )
476 dst[i] = w;
477 else if( w >= 0xf020 && w < 0xf100)
478 dst[i] = w - 0xf000;
479 else
480 return -2;
481 }
482 if( srclen > len) return -1;
483 return len;
484 }