ntdll: Remove wait timeout in get_thread_context().
[wine.git] / libs / wine / wctomb.c
blobc03d1ad134f86eed2b227c414f30bdc9d5029e51
1 /*
2 * WideCharToMultiByte implementation
4 * Copyright 2000 Alexandre Julliard
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <string.h>
23 #include "wine/asm.h"
25 #ifdef __ASM_OBSOLETE
27 #include "unicode.h"
29 extern WCHAR wine_compose( const WCHAR *str ) DECLSPEC_HIDDEN;
31 /****************************************************************/
32 /* sbcs support */
34 /* check if 'ch' is an acceptable sbcs mapping for 'wch' */
35 static inline int is_valid_sbcs_mapping( const struct sbcs_table *table, int flags,
36 WCHAR wch, unsigned char ch )
38 if ((flags & WC_NO_BEST_FIT_CHARS) || ch == (unsigned char)table->info.def_char)
39 return (table->cp2uni[ch] == wch);
40 return 1;
43 /* query necessary dst length for src string */
44 static int get_length_sbcs( const struct sbcs_table *table, int flags,
45 const WCHAR *src, unsigned int srclen, int *used )
47 const unsigned char * const uni2cp_low = table->uni2cp_low;
48 const unsigned short * const uni2cp_high = table->uni2cp_high;
49 int ret, tmp;
50 WCHAR composed;
52 if (!used) used = &tmp; /* avoid checking on every char */
53 *used = 0;
55 for (ret = 0; srclen; ret++, src++, srclen--)
57 WCHAR wch = *src;
58 unsigned char ch;
60 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = wine_compose(src)))
62 /* now check if we can use the composed char */
63 ch = uni2cp_low[uni2cp_high[composed >> 8] + (composed & 0xff)];
64 if (is_valid_sbcs_mapping( table, flags, composed, ch ))
66 /* we have a good mapping, use it */
67 src++;
68 srclen--;
69 continue;
71 /* no mapping for the composed char, check the other flags */
72 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
74 *used = 1;
75 src++; /* skip the non-spacing char */
76 srclen--;
77 continue;
79 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
81 src++;
82 srclen--;
84 /* WC_SEPCHARS is the default */
86 if (!*used)
88 ch = uni2cp_low[uni2cp_high[wch >> 8] + (wch & 0xff)];
89 *used = !is_valid_sbcs_mapping( table, flags, wch, ch );
92 return ret;
95 /* wcstombs for single-byte code page */
96 static inline int wcstombs_sbcs( const struct sbcs_table *table,
97 const WCHAR *src, unsigned int srclen,
98 char *dst, unsigned int dstlen )
100 const unsigned char * const uni2cp_low = table->uni2cp_low;
101 const unsigned short * const uni2cp_high = table->uni2cp_high;
102 int ret = srclen;
104 if (dstlen < srclen)
106 /* buffer too small: fill it up to dstlen and return error */
107 srclen = dstlen;
108 ret = -1;
111 while (srclen >= 16)
113 dst[0] = uni2cp_low[uni2cp_high[src[0] >> 8] + (src[0] & 0xff)];
114 dst[1] = uni2cp_low[uni2cp_high[src[1] >> 8] + (src[1] & 0xff)];
115 dst[2] = uni2cp_low[uni2cp_high[src[2] >> 8] + (src[2] & 0xff)];
116 dst[3] = uni2cp_low[uni2cp_high[src[3] >> 8] + (src[3] & 0xff)];
117 dst[4] = uni2cp_low[uni2cp_high[src[4] >> 8] + (src[4] & 0xff)];
118 dst[5] = uni2cp_low[uni2cp_high[src[5] >> 8] + (src[5] & 0xff)];
119 dst[6] = uni2cp_low[uni2cp_high[src[6] >> 8] + (src[6] & 0xff)];
120 dst[7] = uni2cp_low[uni2cp_high[src[7] >> 8] + (src[7] & 0xff)];
121 dst[8] = uni2cp_low[uni2cp_high[src[8] >> 8] + (src[8] & 0xff)];
122 dst[9] = uni2cp_low[uni2cp_high[src[9] >> 8] + (src[9] & 0xff)];
123 dst[10] = uni2cp_low[uni2cp_high[src[10] >> 8] + (src[10] & 0xff)];
124 dst[11] = uni2cp_low[uni2cp_high[src[11] >> 8] + (src[11] & 0xff)];
125 dst[12] = uni2cp_low[uni2cp_high[src[12] >> 8] + (src[12] & 0xff)];
126 dst[13] = uni2cp_low[uni2cp_high[src[13] >> 8] + (src[13] & 0xff)];
127 dst[14] = uni2cp_low[uni2cp_high[src[14] >> 8] + (src[14] & 0xff)];
128 dst[15] = uni2cp_low[uni2cp_high[src[15] >> 8] + (src[15] & 0xff)];
129 src += 16;
130 dst += 16;
131 srclen -= 16;
134 /* now handle remaining characters */
135 src += srclen;
136 dst += srclen;
137 switch(srclen)
139 case 15: dst[-15] = uni2cp_low[uni2cp_high[src[-15] >> 8] + (src[-15] & 0xff)];
140 case 14: dst[-14] = uni2cp_low[uni2cp_high[src[-14] >> 8] + (src[-14] & 0xff)];
141 case 13: dst[-13] = uni2cp_low[uni2cp_high[src[-13] >> 8] + (src[-13] & 0xff)];
142 case 12: dst[-12] = uni2cp_low[uni2cp_high[src[-12] >> 8] + (src[-12] & 0xff)];
143 case 11: dst[-11] = uni2cp_low[uni2cp_high[src[-11] >> 8] + (src[-11] & 0xff)];
144 case 10: dst[-10] = uni2cp_low[uni2cp_high[src[-10] >> 8] + (src[-10] & 0xff)];
145 case 9: dst[-9] = uni2cp_low[uni2cp_high[src[-9] >> 8] + (src[-9] & 0xff)];
146 case 8: dst[-8] = uni2cp_low[uni2cp_high[src[-8] >> 8] + (src[-8] & 0xff)];
147 case 7: dst[-7] = uni2cp_low[uni2cp_high[src[-7] >> 8] + (src[-7] & 0xff)];
148 case 6: dst[-6] = uni2cp_low[uni2cp_high[src[-6] >> 8] + (src[-6] & 0xff)];
149 case 5: dst[-5] = uni2cp_low[uni2cp_high[src[-5] >> 8] + (src[-5] & 0xff)];
150 case 4: dst[-4] = uni2cp_low[uni2cp_high[src[-4] >> 8] + (src[-4] & 0xff)];
151 case 3: dst[-3] = uni2cp_low[uni2cp_high[src[-3] >> 8] + (src[-3] & 0xff)];
152 case 2: dst[-2] = uni2cp_low[uni2cp_high[src[-2] >> 8] + (src[-2] & 0xff)];
153 case 1: dst[-1] = uni2cp_low[uni2cp_high[src[-1] >> 8] + (src[-1] & 0xff)];
154 case 0: break;
156 return ret;
159 /* slow version of wcstombs_sbcs that handles the various flags */
160 static int wcstombs_sbcs_slow( const struct sbcs_table *table, int flags,
161 const WCHAR *src, unsigned int srclen,
162 char *dst, unsigned int dstlen,
163 const char *defchar, int *used )
165 const unsigned char * const uni2cp_low = table->uni2cp_low;
166 const unsigned short * const uni2cp_high = table->uni2cp_high;
167 unsigned char def;
168 unsigned int len;
169 int tmp;
170 WCHAR composed;
172 if (!defchar)
173 def = table->info.def_char & 0xff;
174 else
175 def = *defchar;
177 if (!used) used = &tmp; /* avoid checking on every char */
178 *used = 0;
180 for (len = dstlen; srclen && len; dst++, len--, src++, srclen--)
182 WCHAR wch = *src;
184 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = wine_compose(src)))
186 /* now check if we can use the composed char */
187 *dst = uni2cp_low[uni2cp_high[composed >> 8] + (composed & 0xff)];
188 if (is_valid_sbcs_mapping( table, flags, composed, *dst ))
190 /* we have a good mapping, use it */
191 src++;
192 srclen--;
193 continue;
195 /* no mapping for the composed char, check the other flags */
196 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
198 *dst = def;
199 *used = 1;
200 src++; /* skip the non-spacing char */
201 srclen--;
202 continue;
204 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
206 src++;
207 srclen--;
209 /* WC_SEPCHARS is the default */
212 *dst = uni2cp_low[uni2cp_high[wch >> 8] + (wch & 0xff)];
213 if (!is_valid_sbcs_mapping( table, flags, wch, *dst ))
215 *dst = def;
216 *used = 1;
219 if (srclen) return -1; /* overflow */
220 return dstlen - len;
224 /****************************************************************/
225 /* dbcs support */
227 /* check if 'ch' is an acceptable dbcs mapping for 'wch' */
228 static inline int is_valid_dbcs_mapping( const struct dbcs_table *table, int flags,
229 WCHAR wch, unsigned short ch )
231 if ((flags & WC_NO_BEST_FIT_CHARS) || ch == table->info.def_char)
233 /* check if char maps back to the same Unicode value */
234 if (ch & 0xff00)
236 unsigned char off = table->cp2uni_leadbytes[ch >> 8];
237 return (table->cp2uni[(off << 8) + (ch & 0xff)] == wch);
239 return (table->cp2uni[ch & 0xff] == wch);
241 return 1;
244 /* compute the default char for the dbcs case */
245 static inline WCHAR get_defchar_dbcs( const struct dbcs_table *table, const char *defchar )
247 if (!defchar) return table->info.def_char;
248 if (!defchar[1]) return (unsigned char)defchar[0];
249 return ((unsigned char)defchar[0] << 8) | (unsigned char)defchar[1];
252 /* query necessary dst length for src string */
253 static int get_length_dbcs( const struct dbcs_table *table, int flags,
254 const WCHAR *src, unsigned int srclen,
255 const char *defchar, int *used )
257 const unsigned short * const uni2cp_low = table->uni2cp_low;
258 const unsigned short * const uni2cp_high = table->uni2cp_high;
259 WCHAR defchar_value, composed;
260 int len, tmp;
262 if (!defchar && !used && !(flags & WC_COMPOSITECHECK))
264 for (len = 0; srclen; srclen--, src++, len++)
266 if (uni2cp_low[uni2cp_high[*src >> 8] + (*src & 0xff)] & 0xff00) len++;
268 return len;
271 defchar_value = get_defchar_dbcs( table, defchar );
272 if (!used) used = &tmp; /* avoid checking on every char */
273 *used = 0;
274 for (len = 0; srclen; len++, srclen--, src++)
276 unsigned short res;
277 WCHAR wch = *src;
279 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = wine_compose(src)))
281 /* now check if we can use the composed char */
282 res = uni2cp_low[uni2cp_high[composed >> 8] + (composed & 0xff)];
284 if (is_valid_dbcs_mapping( table, flags, composed, res ))
286 /* we have a good mapping for the composed char, use it */
287 if (res & 0xff00) len++;
288 src++;
289 srclen--;
290 continue;
292 /* no mapping for the composed char, check the other flags */
293 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
295 if (defchar_value & 0xff00) len++;
296 *used = 1;
297 src++; /* skip the non-spacing char */
298 srclen--;
299 continue;
301 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
303 src++;
304 srclen--;
306 /* WC_SEPCHARS is the default */
309 res = uni2cp_low[uni2cp_high[wch >> 8] + (wch & 0xff)];
310 if (!is_valid_dbcs_mapping( table, flags, wch, res ))
312 res = defchar_value;
313 *used = 1;
315 if (res & 0xff00) len++;
317 return len;
320 /* wcstombs for double-byte code page */
321 static inline int wcstombs_dbcs( const struct dbcs_table *table,
322 const WCHAR *src, unsigned int srclen,
323 char *dst, unsigned int dstlen )
325 const unsigned short * const uni2cp_low = table->uni2cp_low;
326 const unsigned short * const uni2cp_high = table->uni2cp_high;
327 int len;
329 for (len = dstlen; srclen && len; len--, srclen--, src++)
331 unsigned short res = uni2cp_low[uni2cp_high[*src >> 8] + (*src & 0xff)];
332 if (res & 0xff00)
334 if (len == 1) break; /* do not output a partial char */
335 len--;
336 *dst++ = res >> 8;
338 *dst++ = (char)res;
340 if (srclen) return -1; /* overflow */
341 return dstlen - len;
344 /* slow version of wcstombs_dbcs that handles the various flags */
345 static int wcstombs_dbcs_slow( const struct dbcs_table *table, int flags,
346 const WCHAR *src, unsigned int srclen,
347 char *dst, unsigned int dstlen,
348 const char *defchar, int *used )
350 const unsigned short * const uni2cp_low = table->uni2cp_low;
351 const unsigned short * const uni2cp_high = table->uni2cp_high;
352 WCHAR defchar_value = get_defchar_dbcs( table, defchar );
353 WCHAR composed;
354 int len, tmp;
356 if (!used) used = &tmp; /* avoid checking on every char */
357 *used = 0;
359 for (len = dstlen; srclen && len; len--, srclen--, src++)
361 unsigned short res;
362 WCHAR wch = *src;
364 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = wine_compose(src)))
366 /* now check if we can use the composed char */
367 res = uni2cp_low[uni2cp_high[composed >> 8] + (composed & 0xff)];
369 if (is_valid_dbcs_mapping( table, flags, composed, res ))
371 /* we have a good mapping for the composed char, use it */
372 src++;
373 srclen--;
374 goto output_char;
376 /* no mapping for the composed char, check the other flags */
377 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
379 res = defchar_value;
380 *used = 1;
381 src++; /* skip the non-spacing char */
382 srclen--;
383 goto output_char;
385 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
387 src++;
388 srclen--;
390 /* WC_SEPCHARS is the default */
393 res = uni2cp_low[uni2cp_high[wch >> 8] + (wch & 0xff)];
394 if (!is_valid_dbcs_mapping( table, flags, wch, res ))
396 res = defchar_value;
397 *used = 1;
400 output_char:
401 if (res & 0xff00)
403 if (len == 1) break; /* do not output a partial char */
404 len--;
405 *dst++ = res >> 8;
407 *dst++ = (char)res;
409 if (srclen) return -1; /* overflow */
410 return dstlen - len;
413 /* wide char to multi byte string conversion */
414 /* return -1 on dst buffer overflow */
415 int wine_cp_wcstombs_obsolete( const union cptable *table, int flags,
416 const WCHAR *src, int srclen,
417 char *dst, int dstlen, const char *defchar, int *used )
419 if (table->info.char_size == 1)
421 if (flags || defchar || used)
423 if (!dstlen) return get_length_sbcs( &table->sbcs, flags, src, srclen, used );
424 return wcstombs_sbcs_slow( &table->sbcs, flags, src, srclen,
425 dst, dstlen, defchar, used );
427 if (!dstlen) return srclen;
428 return wcstombs_sbcs( &table->sbcs, src, srclen, dst, dstlen );
430 else /* mbcs */
432 if (!dstlen) return get_length_dbcs( &table->dbcs, flags, src, srclen, defchar, used );
433 if (flags || defchar || used)
434 return wcstombs_dbcs_slow( &table->dbcs, flags, src, srclen,
435 dst, dstlen, defchar, used );
436 return wcstombs_dbcs( &table->dbcs, src, srclen, dst, dstlen );
440 __ASM_OBSOLETE(wine_cp_wcstombs);
442 #endif /* __ASM_OBSOLETE */