wchar.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  18. *
  19. * Besides uClibc, I'm using this code in my libc for elks, which is
  20. * a 16-bit environment with a fairly limited compiler. It would make
  21. * things much easier for me if this file isn't modified unnecessarily.
  22. * In particular, please put any new or replacement functions somewhere
  23. * else, and modify the makefile to use your version instead.
  24. * Thanks. Manuel
  25. *
  26. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  27. /* May 23, 2002 Initial Notes:
  28. *
  29. * I'm still tweaking this stuff, but it passes the tests I've thrown
  30. * at it, and Erik needs it for the gcc port. The glibc extension
  31. * __wcsnrtombs() hasn't been tested, as I didn't find a test for it
  32. * in the glibc source. I also need to fix the behavior of
  33. * _wchar_utf8sntowcs() if the max number of wchars to convert is 0.
  34. *
  35. * UTF-8 -> wchar -> UTF-8 conversion tests on Markus Kuhn's UTF-8-demo.txt
  36. * file on my platform (x86) show about 5-10% faster conversion speed than
  37. * glibc with mbsrtowcs()/wcsrtombs() and almost twice as fast as glibc with
  38. * individual mbrtowc()/wcrtomb() calls.
  39. *
  40. * If 'DECODER' is defined, then _wchar_utf8sntowcs() will be compiled
  41. * as a fail-safe UTF-8 decoder appropriate for a terminal, etc. which
  42. * needs to deal gracefully with whatever is sent to it. In that mode,
  43. * it passes Markus Kuhn's UTF-8-test.txt stress test. I plan to add
  44. * an arg to force that behavior, so the interface will be changing.
  45. *
  46. * I need to fix the error checking for 16-bit wide chars. This isn't
  47. * an issue for uClibc, but may be for ELKS. I'm currently not sure
  48. * if I'll use 16-bit, 32-bit, or configureable wchars in ELKS.
  49. *
  50. * Manuel
  51. */
  52. #define _GNU_SOURCE
  53. #define _ISOC99_SOURCE
  54. #include <errno.h>
  55. #include <stddef.h>
  56. #include <limits.h>
  57. #include <stdint.h>
  58. #include <inttypes.h>
  59. #include <stdlib.h>
  60. #include <stdio.h>
  61. #include <assert.h>
  62. #include <locale.h>
  63. #include <wchar.h>
  64. #define ENCODING (__global_locale.encoding)
  65. #if WCHAR_MAX > 0xffffU
  66. #define UTF_8_MAX_LEN 6
  67. #else
  68. #define UTF_8_MAX_LEN 3
  69. #endif
  70. /* #define KUHN */
  71. #warning implement __CTYPE_HAS_UTF_8_LOCALES!
  72. #define __CTYPE_HAS_UTF_8_LOCALES
  73. /* Implementation-specific work functions. */
  74. extern size_t _wchar_utf8sntowcs(wchar_t *__restrict pwc, size_t wn,
  75. const char **__restrict src, size_t n,
  76. mbstate_t *ps, int allow_continuation);
  77. extern size_t _wchar_wcsntoutf8s(char *__restrict s, size_t n,
  78. const wchar_t **__restrict src, size_t wn);
  79. /* glibc extensions. */
  80. extern size_t __mbsnrtowcs(wchar_t *__restrict dst,
  81. const char **__restrict src,
  82. size_t NMC, size_t len, mbstate_t *__restrict ps);
  83. extern size_t __wcsnrtombs(char *__restrict dst,
  84. const wchar_t **__restrict src,
  85. size_t NWC, size_t len, mbstate_t *__restrict ps);
  86. /**********************************************************************/
  87. #ifdef L_btowc
  88. wint_t btowc(int c)
  89. {
  90. wchar_t wc;
  91. unsigned char buf[1];
  92. mbstate_t mbstate;
  93. if (c != EOF) {
  94. *buf = (unsigned char) c;
  95. mbstate.mask = 0; /* Initialize the mbstate. */
  96. if (mbrtowc(&wc, buf, 1, &mbstate) == 1) {
  97. return wc;
  98. }
  99. }
  100. return WEOF;
  101. }
  102. #endif
  103. /**********************************************************************/
  104. #ifdef L_wctob
  105. /* Note: We completely ignore ps in all currently supported conversions. */
  106. int wctob(wint_t c)
  107. {
  108. unsigned char buf[MB_LEN_MAX];
  109. return (wcrtomb(buf, c, NULL) == 1) ? *buf : EOF;
  110. }
  111. #endif
  112. /**********************************************************************/
  113. #ifdef L_mbsinit
  114. int mbsinit(const mbstate_t *ps)
  115. {
  116. return !ps || !ps->mask;
  117. }
  118. #endif
  119. /**********************************************************************/
  120. #ifdef L_mbrlen
  121. size_t mbrlen(const char *__restrict s, size_t n, mbstate_t *__restrict ps)
  122. {
  123. static mbstate_t mbstate; /* Rely on bss 0-init. */
  124. return mbrtowc(NULL, s, n, (ps != NULL) ? ps : &mbstate);
  125. }
  126. #endif
  127. /**********************************************************************/
  128. #ifdef L_mbrtowc
  129. size_t mbrtowc(wchar_t *__restrict pwc, const char *__restrict s,
  130. size_t n, mbstate_t *__restrict ps)
  131. {
  132. static mbstate_t mbstate; /* Rely on bss 0-init. */
  133. wchar_t wcbuf[1];
  134. const char *p;
  135. size_t r;
  136. char empty_string[1]; /* Avoid static to be fPIC friendly. */
  137. if (!ps) {
  138. ps = &mbstate;
  139. }
  140. if (!s) {
  141. pwc = (wchar_t *) s; /* NULL */
  142. empty_string[0] = 0; /* Init the empty string when necessary. */
  143. s = empty_string;
  144. n = 1;
  145. } else if (!n) {
  146. return (ps->mask && (ps->wc == 0xffffU)) /* TODO: change error code? */
  147. ? ((size_t) -1) : ((size_t) -2);
  148. }
  149. p = s;
  150. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  151. /* Need to do this here since mbsrtowcs doesn't allow incompletes. */
  152. if (ENCODING == __ctype_encoding_utf8) {
  153. r = _wchar_utf8sntowcs(pwc, 1, &p, n, ps, 1);
  154. return (r == 1) ? (p-s) : r;
  155. }
  156. #endif
  157. r = __mbsnrtowcs(wcbuf, &p, SIZE_MAX, 1, ps);
  158. if (((ssize_t) r) >= 0) {
  159. if (pwc) {
  160. *pwc = *wcbuf;
  161. }
  162. }
  163. return (size_t) r;
  164. }
  165. #endif
  166. /**********************************************************************/
  167. #ifdef L_wcrtomb
  168. /* Note: We completely ignore ps in all currently supported conversions. */
  169. /* TODO: Check for valid state anyway? */
  170. size_t wcrtomb(register char *__restrict s, wchar_t wc,
  171. mbstate_t *__restrict ps)
  172. {
  173. wchar_t wcbuf[2];
  174. const wchar_t *pwc;
  175. size_t r;
  176. char buf[MB_LEN_MAX];
  177. if (!s) {
  178. s = buf;
  179. wc = 0;
  180. }
  181. pwc = wcbuf;
  182. wcbuf[0] = wc;
  183. wcbuf[1] = 0;
  184. r = __wcsnrtombs(s, &pwc, SIZE_MAX, MB_LEN_MAX, ps);
  185. return (r != 0) ? r : 1;
  186. }
  187. #endif
  188. /**********************************************************************/
  189. #ifdef L_mbsrtowcs
  190. size_t mbsrtowcs(wchar_t *__restrict dst, const char **__restrict src,
  191. size_t len, mbstate_t *__restrict ps)
  192. {
  193. static mbstate_t mbstate; /* Rely on bss 0-init. */
  194. return __mbsnrtowcs(dst, src, SIZE_MAX, len,
  195. ((ps != NULL) ? ps : &mbstate));
  196. }
  197. #endif
  198. /**********************************************************************/
  199. #ifdef L_wcsrtombs
  200. /* Note: We completely ignore ps in all currently supported conversions.
  201. * TODO: Check for valid state anyway? */
  202. size_t wcsrtombs(char *__restrict dst, const wchar_t **__restrict src,
  203. size_t len, mbstate_t *__restrict ps)
  204. {
  205. return __wcsnrtombs(dst, src, SIZE_MAX, len, ps);
  206. }
  207. #endif
  208. /**********************************************************************/
  209. #ifdef L__wchar_utf8sntowcs
  210. /* Define DECODER to generate a UTF-8 decoder which passes Markus Kuhn's
  211. * UTF-8-test.txt strss test.
  212. */
  213. /* #define DECODER */
  214. #ifdef DECODER
  215. #ifndef KUHN
  216. #define KUHN
  217. #endif
  218. #endif
  219. size_t _wchar_utf8sntowcs(wchar_t *__restrict pwc, size_t wn,
  220. const char **__restrict src, size_t n,
  221. mbstate_t *ps, int allow_continuation)
  222. {
  223. register const char *s;
  224. __uwchar_t mask;
  225. __uwchar_t wc;
  226. wchar_t wcbuf[1];
  227. size_t count;
  228. int incr;
  229. s = *src;
  230. assert(s != NULL);
  231. assert(ps != NULL);
  232. incr = 1;
  233. if (!pwc) {
  234. pwc = wcbuf;
  235. wn = SIZE_MAX;
  236. incr = 0;
  237. }
  238. #warning fix _wchar_utf8sntowcs to allow wn == 0!
  239. assert(wn > 0); /* TODO: fix this!! */
  240. count = wn;
  241. if ((mask = (__uwchar_t) ps->mask) != 0) { /* A continuation... */
  242. #ifdef DECODER
  243. wc = (__uwchar_t) ps->wc;
  244. if (n) {
  245. goto CONTINUE;
  246. }
  247. goto DONE;
  248. #else
  249. if ((wc = (__uwchar_t) ps->wc) != 0xffffU) {
  250. /* TODO: change error code here and below? */
  251. if (n) {
  252. goto CONTINUE;
  253. }
  254. goto DONE;
  255. }
  256. return (size_t) -1; /* We're in an error state. */
  257. #endif
  258. }
  259. do {
  260. if (!n) {
  261. goto DONE;
  262. }
  263. --n;
  264. if ((wc = ((unsigned char) *s++)) >= 0x80) { /* Not ASCII... */
  265. mask = 0x40;
  266. #warning fix range for 16 bit wides
  267. if ( ((unsigned char)(s[-1] - 0xc0)) < (0xfe - 0xc0) ) {
  268. goto START;
  269. }
  270. BAD:
  271. #ifdef DECODER
  272. wc = 0xfffd;
  273. goto COMPLETE;
  274. #else
  275. ps->mask = mask;
  276. ps->wc = 0xffffU;
  277. return (size_t) -1; /* Illegal start byte! */
  278. #endif
  279. CONTINUE:
  280. while (n) {
  281. --n;
  282. if ((*s & 0xc0) != 0x80) {
  283. goto BAD;
  284. }
  285. mask <<= 5;
  286. wc <<= 6;
  287. wc += (*s & 0x3f); /* keep seperate for bcc (smaller code) */
  288. ++s;
  289. START:
  290. wc &= ~(mask << 1);
  291. if ((wc & mask) == 0) { /* Character completed. */
  292. if ((mask >>= 5) == 0x40) {
  293. mask += mask;
  294. }
  295. /* Check for invalid sequences (longer than necessary)
  296. * and invalid chars. */
  297. if ( (wc < mask) /* Sequence not minimal length. */
  298. #ifdef KUHN
  299. #if UTF_8_MAX_LEN == 3
  300. #error broken since mask can overflow!!
  301. /* For plane 0, these are the only defined values.*/
  302. || (wc > 0xfffdU)
  303. #else
  304. /* Note that we don't need to worry about exceeding */
  305. /* 31 bits as that is the most that UTF-8 provides. */
  306. || ( ((__uwchar_t)(wc - 0xfffeU)) < 2)
  307. #endif
  308. || ( ((__uwchar_t)(wc - 0xd800U)) < (0xe000U - 0xd800U) )
  309. #endif /* KUHN */
  310. ) {
  311. goto BAD;
  312. }
  313. goto COMPLETE;
  314. }
  315. }
  316. /* Character potentially valid but incomplete. */
  317. if (!allow_continuation) {
  318. if (count != wn) {
  319. return 0;
  320. }
  321. /* NOTE: The following can fail if you allow and then disallow
  322. * continuation!!! */
  323. #if UTF_8_MAX_LEN == 3
  324. #error broken since mask can overflow!!
  325. #endif
  326. /* Need to back up... */
  327. do {
  328. --s;
  329. } while ((mask >>= 5) >= 0x40);
  330. goto DONE;
  331. }
  332. ps->mask = (wchar_t) mask;
  333. ps->wc = (wchar_t) wc;
  334. *src = s;
  335. return (size_t) -2;
  336. }
  337. COMPLETE:
  338. *pwc = wc;
  339. pwc += incr;
  340. }
  341. #ifdef DECODER
  342. while (--count);
  343. #else
  344. while (wc && --count);
  345. if (!wc) {
  346. s = NULL;
  347. }
  348. #endif
  349. DONE:
  350. /* ps->wc is irrelavent here. */
  351. ps->mask = 0;
  352. if (pwc != wcbuf) {
  353. *src = s;
  354. }
  355. return wn - count;
  356. }
  357. #endif
  358. /**********************************************************************/
  359. #ifdef L__wchar_wcstoutf8s
  360. size_t _wchar_wcsntoutf8s(char *__restrict s, size_t n,
  361. const wchar_t **__restrict src, size_t wn)
  362. {
  363. register char *p;
  364. size_t len, t;
  365. __uwchar_t wc;
  366. const __uwchar_t *swc;
  367. int store;
  368. char buf[MB_LEN_MAX];
  369. char m;
  370. store = 1;
  371. if (!s) {
  372. s = buf;
  373. n = SIZE_MAX;
  374. store = 0;
  375. }
  376. t = n;
  377. swc = (const __uwchar_t *) *src;
  378. assert(swc != NULL);
  379. while (wn && t) {
  380. wc = *swc;
  381. *s = wc;
  382. len = 1;
  383. if (wc >= 0x80) {
  384. #ifdef KUHN
  385. if (
  386. #if UTF_8_MAX_LEN == 3
  387. /* For plane 0, these are the only defined values.*/
  388. /* Note that we don't need to worry about exceeding */
  389. /* 31 bits as that is the most that UTF-8 provides. */
  390. (wc > 0xfffdU)
  391. #else
  392. /* UTF_8_MAX_LEN == 6 */
  393. (wc > 0x7fffffffUL)
  394. || ( ((__uwchar_t)(wc - 0xfffeU)) < 2)
  395. #endif
  396. || ( ((__uwchar_t)(wc - 0xd800U)) < (0xe000U - 0xd800U) )
  397. ) {
  398. return (size_t) -1;
  399. }
  400. #else /* KUHN */
  401. #if UTF_8_MAX_LEN != 3
  402. if (wc > 0x7fffffffUL) { /* Value too large. */
  403. return (size_t) -1;
  404. }
  405. #endif
  406. #endif /* KUHN */
  407. wc >>= 1;
  408. p = s;
  409. do {
  410. ++p;
  411. } while (wc >>= 5);
  412. wc = *swc;
  413. if ((len = p - s) > t) { /* Not enough space. */
  414. break;
  415. }
  416. m = 0x80;
  417. while( p>s ) {
  418. m = (m >> 1) | 0x80;
  419. *--p = (wc & 0x3f) | 0x80;
  420. wc >>= 6;
  421. }
  422. *s |= (m << 1);
  423. } else if (wc == 0) { /* End of string. */
  424. swc = NULL;
  425. break;
  426. }
  427. ++swc;
  428. --wn;
  429. t -= len;
  430. if (store) {
  431. s += len;
  432. }
  433. }
  434. *src = (const wchar_t *) swc;
  435. return n - t;
  436. }
  437. #endif
  438. /**********************************************************************/
  439. #ifdef L___mbsnrtowcs
  440. /* WARNING: We treat len as SIZE_MAX when dst is NULL! */
  441. size_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
  442. size_t NMC, size_t len, mbstate_t *__restrict ps)
  443. __attribute__ ((__weak__, __alias__("__mbsnrtowcs")));
  444. size_t __mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
  445. size_t NMC, size_t len, mbstate_t *__restrict ps)
  446. {
  447. static mbstate_t mbstate; /* Rely on bss 0-init. */
  448. wchar_t wcbuf[1];
  449. const char *s;
  450. size_t count, r;
  451. int incr;
  452. if (!ps) {
  453. ps = &mbstate;
  454. }
  455. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  456. if (ENCODING == __ctype_encoding_utf8) {
  457. return ((r = _wchar_utf8sntowcs(dst, len, src, NMC, ps, 1))
  458. != (size_t) -2) ? r : 0;
  459. }
  460. #endif
  461. incr = 1;
  462. if (!dst) {
  463. dst = wcbuf;
  464. len = SIZE_MAX;
  465. incr = 0;
  466. }
  467. /* Since all the following encodings are single-byte encodings... */
  468. if (len > NMC) {
  469. len = NMC;
  470. }
  471. count = len;
  472. s = *src;
  473. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  474. if (ENCODING == __ctype_encoding_8_bit) {
  475. wchar_t wc;
  476. while (count) {
  477. if ((wc = ((unsigned char)(*s))) >= 0x80) { /* Non-ASCII... */
  478. wc -= 0x80;
  479. wc = __global_locale.tbl8c2wc[
  480. (__global_locale.idx8c2wc[wc >> Cc2wc_IDX_SHIFT]
  481. << Cc2wc_IDX_SHIFT) + (wc & (Cc2wc_ROW_LEN - 1))];
  482. if (!wc) {
  483. goto BAD;
  484. }
  485. } else if (!wc) {
  486. s = NULL;
  487. break;
  488. }
  489. ++s;
  490. *dst = wc;
  491. dst += incr;
  492. --count;
  493. }
  494. if (dst != wcbuf) {
  495. *src = s;
  496. }
  497. return len - count;
  498. }
  499. #endif
  500. assert(ENCODING == __ctype_encoding_7_bit);
  501. while (count) {
  502. if ((*dst = (unsigned char) *s) == 0) {
  503. s = NULL;
  504. break;
  505. }
  506. if (*dst >= 0x80) {
  507. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  508. BAD:
  509. #endif
  510. __set_errno(EILSEQ);
  511. return (size_t) -1;
  512. }
  513. ++s;
  514. dst += incr;
  515. --count;
  516. }
  517. if (dst != wcbuf) {
  518. *src = s;
  519. }
  520. return len - count;
  521. }
  522. #endif
  523. /**********************************************************************/
  524. #ifdef L___wcsnrtombs
  525. /* WARNING: We treat len as SIZE_MAX when dst is NULL! */
  526. /* Note: We completely ignore ps in all currently supported conversions.
  527. * TODO: Check for valid state anyway? */
  528. size_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
  529. size_t NWC, size_t len, mbstate_t *__restrict ps)
  530. __attribute__ ((__weak__, __alias__("__wcsnrtombs")));
  531. size_t __wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
  532. size_t NWC, size_t len, mbstate_t *__restrict ps)
  533. {
  534. const __uwchar_t *s;
  535. size_t count;
  536. int incr;
  537. char buf[MB_LEN_MAX];
  538. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  539. if (ENCODING == __ctype_encoding_utf8) {
  540. return _wchar_wcsntoutf8s(dst, len, src, NWC);
  541. }
  542. #endif
  543. incr = 1;
  544. if (!dst) {
  545. dst = buf;
  546. len = SIZE_MAX;
  547. incr = 0;
  548. }
  549. /* Since all the following encodings are single-byte encodings... */
  550. if (len > NWC) {
  551. len = NWC;
  552. }
  553. count = len;
  554. s = (const __uwchar_t *) *src;
  555. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  556. if (ENCODING == __ctype_encoding_8_bit) {
  557. __uwchar_t wc;
  558. __uwchar_t u;
  559. while (count) {
  560. if ((wc = *s) <= 0x7f) {
  561. if (!(*dst = (unsigned char) wc)) {
  562. s = NULL;
  563. break;
  564. }
  565. } else {
  566. u = 0;
  567. if (wc <= Cwc2c_DOMAIN_MAX) {
  568. u = __global_locale.idx8wc2c[wc >> (Cwc2c_TI_SHIFT
  569. + Cwc2c_TT_SHIFT)];
  570. u = __global_locale.tbl8wc2c[(u << Cwc2c_TI_SHIFT)
  571. + ((wc >> Cwc2c_TT_SHIFT)
  572. & ((1 << Cwc2c_TI_SHIFT)-1))];
  573. u = __global_locale.tbl8wc2c[Cwc2c_TI_LEN
  574. + (u << Cwc2c_TT_SHIFT)
  575. + (wc & ((1 << Cwc2c_TT_SHIFT)-1))];
  576. }
  577. /* #define __WCHAR_REPLACEMENT_CHAR '?' */
  578. #ifdef __WCHAR_REPLACEMENT_CHAR
  579. *dst = (unsigned char) ( u ? u : __WCHAR_REPLACEMENT_CHAR );
  580. #else
  581. if (!u) {
  582. goto BAD;
  583. }
  584. *dst = (unsigned char) u;
  585. #endif
  586. }
  587. ++s;
  588. dst += incr;
  589. --count;
  590. }
  591. if (dst != buf) {
  592. *src = (const wchar_t *) s;
  593. }
  594. return len - count;
  595. }
  596. #endif
  597. assert(ENCODING == __ctype_encoding_7_bit);
  598. while (count) {
  599. if (*s >= 0x80) {
  600. #if defined(__CTYPE_HAS_8_BIT_LOCALES) && !defined(__WCHAR_REPLACEMENT_CHAR)
  601. BAD:
  602. #endif
  603. __set_errno(EILSEQ);
  604. return (size_t) -1;
  605. }
  606. if ((*dst = (unsigned char) *s) == 0) {
  607. s = NULL;
  608. break;
  609. }
  610. ++s;
  611. dst += incr;
  612. --count;
  613. }
  614. if (dst != buf) {
  615. *src = (const wchar_t *) s;
  616. }
  617. return len - count;
  618. }
  619. #endif
  620. /**********************************************************************/