10 _wctomb_r (struct _reent
*r
,
15 return __WCTOMB (r
, s
, _wchar
, state
);
19 __ascii_wctomb (struct _reent
*r
,
24 /* Avoids compiler warnings about comparisons that are always false
25 due to limited range when sizeof(wchar_t) is 2 but sizeof(wint_t)
26 is 4, as is the case on cygwin. */
27 wint_t wchar
= _wchar
;
32 if ((size_t)wchar
>= 0x100)
34 _REENT_ERRNO(r
) = EILSEQ
;
43 /* for some conversions, we use the __count field as a place to store a state value */
44 #define __state __count
47 __utf8_wctomb (struct _reent
*r
,
52 wint_t wchar
= _wchar
;
56 return 0; /* UTF-8 encoding is not state-dependent */
58 if (sizeof (wchar_t) == 2 && state
->__count
== -4
59 && (wchar
< 0xdc00 || wchar
> 0xdfff))
61 /* There's a leftover lone high surrogate. Write out the CESU-8 value
62 of the surrogate and proceed to convert the given character. Note
63 to return extra 3 bytes. */
65 tmp
= (state
->__value
.__wchb
[0] << 16 | state
->__value
.__wchb
[1] << 8)
66 - (0x10000 >> 10 | 0xd80d);
67 *s
++ = 0xe0 | ((tmp
& 0xf000) >> 12);
68 *s
++ = 0x80 | ((tmp
& 0xfc0) >> 6);
69 *s
++ = 0x80 | (tmp
& 0x3f);
78 if (wchar
>= 0x80 && wchar
<= 0x7ff)
80 *s
++ = 0xc0 | ((wchar
& 0x7c0) >> 6);
81 *s
= 0x80 | (wchar
& 0x3f);
84 if (wchar
>= 0x800 && wchar
<= 0xffff)
86 /* No UTF-16 surrogate handling in UCS-4 */
87 if (sizeof (wchar_t) == 2 && wchar
>= 0xd800 && wchar
<= 0xdfff)
92 /* First half of a surrogate pair. Store the state and
94 tmp
= ((wchar
& 0x3ff) << 10) + 0x10000;
95 state
->__value
.__wchb
[0] = (tmp
>> 16) & 0xff;
96 state
->__value
.__wchb
[1] = (tmp
>> 8) & 0xff;
98 *s
= (0xf0 | ((tmp
& 0x1c0000) >> 18));
101 if (state
->__count
== -4)
103 /* Second half of a surrogate pair. Reconstruct the full
104 Unicode value and return the trailing three bytes of the
106 tmp
= (state
->__value
.__wchb
[0] << 16)
107 | (state
->__value
.__wchb
[1] << 8)
110 *s
++ = 0xf0 | ((tmp
& 0x1c0000) >> 18);
111 *s
++ = 0x80 | ((tmp
& 0x3f000) >> 12);
112 *s
++ = 0x80 | ((tmp
& 0xfc0) >> 6);
113 *s
= 0x80 | (tmp
& 0x3f);
116 /* Otherwise translate into CESU-8 value. */
118 *s
++ = 0xe0 | ((wchar
& 0xf000) >> 12);
119 *s
++ = 0x80 | ((wchar
& 0xfc0) >> 6);
120 *s
= 0x80 | (wchar
& 0x3f);
123 if (wchar
>= 0x10000 && wchar
<= 0x10ffff)
125 *s
++ = 0xf0 | ((wchar
& 0x1c0000) >> 18);
126 *s
++ = 0x80 | ((wchar
& 0x3f000) >> 12);
127 *s
++ = 0x80 | ((wchar
& 0xfc0) >> 6);
128 *s
= 0x80 | (wchar
& 0x3f);
132 _REENT_ERRNO(r
) = EILSEQ
;
136 /* Cygwin defines its own doublebyte charset conversion functions
137 because the underlying OS requires wchar_t == UTF-16. */
140 __sjis_wctomb (struct _reent
*r
,
145 wint_t wchar
= _wchar
;
147 unsigned char char2
= (unsigned char)wchar
;
148 unsigned char char1
= (unsigned char)(wchar
>> 8);
151 return 0; /* not state-dependent */
155 /* first byte is non-zero..validate multi-byte char */
156 if (_issjis1(char1
) && _issjis2(char2
))
164 _REENT_ERRNO(r
) = EILSEQ
;
173 __eucjp_wctomb (struct _reent
*r
,
178 wint_t wchar
= _wchar
;
179 unsigned char char2
= (unsigned char)wchar
;
180 unsigned char char1
= (unsigned char)(wchar
>> 8);
183 return 0; /* not state-dependent */
187 /* first byte is non-zero..validate multi-byte char */
188 if (_iseucjp1 (char1
) && _iseucjp2 (char2
))
194 else if (_iseucjp2 (char1
) && _iseucjp2 (char2
| 0x80))
198 *s
= (char)(char2
| 0x80);
203 _REENT_ERRNO(r
) = EILSEQ
;
212 __jis_wctomb (struct _reent
*r
,
217 wint_t wchar
= _wchar
;
219 unsigned char char2
= (unsigned char)wchar
;
220 unsigned char char1
= (unsigned char)(wchar
>> 8);
223 return 1; /* state-dependent */
227 /* first byte is non-zero..validate multi-byte char */
228 if (_isjis (char1
) && _isjis (char2
))
230 if (state
->__state
== 0)
232 /* must switch from ASCII to JIS state */
243 _REENT_ERRNO(r
) = EILSEQ
;
246 if (state
->__state
!= 0)
248 /* must switch from JIS to ASCII state */
258 #endif /* !__CYGWIN__ */
260 #ifdef _MB_EXTENDED_CHARSETS_ISO
262 ___iso_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, int iso_idx
,
265 wint_t wchar
= _wchar
;
270 /* wchars <= 0x9f translate to all ISO charsets directly. */
277 for (mb
= 0; mb
< 0x60; ++mb
)
278 if (__iso_8859_conv
[iso_idx
][mb
] == wchar
)
280 *s
= (char) (mb
+ 0xa0);
283 _REENT_ERRNO(r
) = EILSEQ
;
288 if ((size_t)wchar
>= 0x100)
290 _REENT_ERRNO(r
) = EILSEQ
;
298 int __iso_8859_1_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
301 return ___iso_wctomb (r
, s
, _wchar
, -1, state
);
304 int __iso_8859_2_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
307 return ___iso_wctomb (r
, s
, _wchar
, 0, state
);
310 int __iso_8859_3_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
313 return ___iso_wctomb (r
, s
, _wchar
, 1, state
);
316 int __iso_8859_4_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
319 return ___iso_wctomb (r
, s
, _wchar
, 2, state
);
322 int __iso_8859_5_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
325 return ___iso_wctomb (r
, s
, _wchar
, 3, state
);
328 int __iso_8859_6_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
331 return ___iso_wctomb (r
, s
, _wchar
, 4, state
);
334 int __iso_8859_7_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
337 return ___iso_wctomb (r
, s
, _wchar
, 5, state
);
340 int __iso_8859_8_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
343 return ___iso_wctomb (r
, s
, _wchar
, 6, state
);
346 int __iso_8859_9_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
349 return ___iso_wctomb (r
, s
, _wchar
, 7, state
);
352 int __iso_8859_10_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
355 return ___iso_wctomb (r
, s
, _wchar
, 8, state
);
358 int __iso_8859_11_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
361 return ___iso_wctomb (r
, s
, _wchar
, 9, state
);
364 int __iso_8859_13_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
367 return ___iso_wctomb (r
, s
, _wchar
, 10, state
);
370 int __iso_8859_14_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
373 return ___iso_wctomb (r
, s
, _wchar
, 11, state
);
376 int __iso_8859_15_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
379 return ___iso_wctomb (r
, s
, _wchar
, 12, state
);
382 int __iso_8859_16_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
,
385 return ___iso_wctomb (r
, s
, _wchar
, 13, state
);
388 static wctomb_p __iso_8859_wctomb
[17] = {
399 __iso_8859_10_wctomb
,
400 __iso_8859_11_wctomb
,
401 NULL
, /* No ISO 8859-12 */
402 __iso_8859_13_wctomb
,
403 __iso_8859_14_wctomb
,
404 __iso_8859_15_wctomb
,
408 /* val *MUST* be valid! All checks for validity are supposed to be
409 performed before calling this function. */
411 __iso_wctomb (int val
)
413 return __iso_8859_wctomb
[val
];
415 #endif /* _MB_EXTENDED_CHARSETS_ISO */
417 #ifdef _MB_EXTENDED_CHARSETS_WINDOWS
419 ___cp_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, int cp_idx
,
422 wint_t wchar
= _wchar
;
433 for (mb
= 0; mb
< 0x80; ++mb
)
434 if (__cp_conv
[cp_idx
][mb
] == wchar
)
436 *s
= (char) (mb
+ 0x80);
439 _REENT_ERRNO(r
) = EILSEQ
;
444 if ((size_t)wchar
>= 0x100)
446 _REENT_ERRNO(r
) = EILSEQ
;
455 __cp_437_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
457 return ___cp_wctomb (r
, s
, _wchar
, 0, state
);
461 __cp_720_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
463 return ___cp_wctomb (r
, s
, _wchar
, 1, state
);
467 __cp_737_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
469 return ___cp_wctomb (r
, s
, _wchar
, 2, state
);
473 __cp_775_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
475 return ___cp_wctomb (r
, s
, _wchar
, 3, state
);
479 __cp_850_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
481 return ___cp_wctomb (r
, s
, _wchar
, 4, state
);
485 __cp_852_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
487 return ___cp_wctomb (r
, s
, _wchar
, 5, state
);
491 __cp_855_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
493 return ___cp_wctomb (r
, s
, _wchar
, 6, state
);
497 __cp_857_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
499 return ___cp_wctomb (r
, s
, _wchar
, 7, state
);
503 __cp_858_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
505 return ___cp_wctomb (r
, s
, _wchar
, 8, state
);
509 __cp_862_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
511 return ___cp_wctomb (r
, s
, _wchar
, 9, state
);
515 __cp_866_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
517 return ___cp_wctomb (r
, s
, _wchar
, 10, state
);
521 __cp_874_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
523 return ___cp_wctomb (r
, s
, _wchar
, 11, state
);
527 __cp_1125_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
529 return ___cp_wctomb (r
, s
, _wchar
, 12, state
);
533 __cp_1250_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
535 return ___cp_wctomb (r
, s
, _wchar
, 13, state
);
539 __cp_1251_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
541 return ___cp_wctomb (r
, s
, _wchar
, 14, state
);
545 __cp_1252_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
547 return ___cp_wctomb (r
, s
, _wchar
, 15, state
);
551 __cp_1253_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
553 return ___cp_wctomb (r
, s
, _wchar
, 16, state
);
557 __cp_1254_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
559 return ___cp_wctomb (r
, s
, _wchar
, 17, state
);
563 __cp_1255_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
565 return ___cp_wctomb (r
, s
, _wchar
, 18, state
);
569 __cp_1256_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
571 return ___cp_wctomb (r
, s
, _wchar
, 19, state
);
575 __cp_1257_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
577 return ___cp_wctomb (r
, s
, _wchar
, 20, state
);
581 __cp_1258_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
583 return ___cp_wctomb (r
, s
, _wchar
, 21, state
);
587 __cp_20866_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
589 return ___cp_wctomb (r
, s
, _wchar
, 22, state
);
593 __cp_21866_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
595 return ___cp_wctomb (r
, s
, _wchar
, 23, state
);
599 __cp_101_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
601 return ___cp_wctomb (r
, s
, _wchar
, 24, state
);
605 __cp_102_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
607 return ___cp_wctomb (r
, s
, _wchar
, 25, state
);
611 __cp_103_wctomb (struct _reent
*r
, char *s
, wchar_t _wchar
, mbstate_t *state
)
613 return ___cp_wctomb (r
, s
, _wchar
, 26, state
);
616 static wctomb_p __cp_xxx_wctomb
[27] = {
646 /* val *MUST* be valid! All checks for validity are supposed to be
647 performed before calling this function. */
649 __cp_wctomb (int val
)
651 return __cp_xxx_wctomb
[__cp_val_index (val
)];
653 #endif /* _MB_EXTENDED_CHARSETS_WINDOWS */
654 #endif /* _MB_CAPABLE */