wchar.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  1. /* Copyright (C) 2002, 2003, 2004 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. * July 1, 2002
  51. *
  52. * Fixed _wchar_utf8sntowcs() for the max number of wchars == 0 case.
  53. * Fixed nul-char bug in btowc(), and another in __mbsnrtowcs() for 8-bit
  54. * locales.
  55. * Enabled building of a C/POSIX-locale-only version, so full locale support
  56. * no longer needs to be enabled.
  57. *
  58. * Nov 4, 2002
  59. *
  60. * Fixed a bug in _wchar_wcsntoutf8s(). Don't store wcs position if dst is NULL.
  61. * Also, introduce an awful hack into _wchar_wcsntoutf8s() and wcsrtombs() in
  62. * order to support %ls in printf. See comments below for details.
  63. * Change behaviour of wc<->mb functions when in the C locale. Now they do
  64. * a 1-1 map for the range 0x80-UCHAR_MAX. This is for backwards compatibility
  65. * and consistency with the stds requirements that a printf format string by
  66. * a valid multibyte string beginning and ending in it's initial shift state.
  67. *
  68. * Nov 5, 2002
  69. *
  70. * Forgot to change btowc and wctob when I changed the wc<->mb functions yesterday.
  71. *
  72. * Nov 7, 2002
  73. *
  74. * Add wcwidth and wcswidth, based on Markus Kuhn's wcwidth of 2002-05-08.
  75. * Added some size/speed optimizations and integrated it into my locale
  76. * framework. Minimally tested at the moment, but the stub C-locale
  77. * version (which most people would probably be using) should be fine.
  78. *
  79. * Nov 21, 2002
  80. *
  81. * Revert the wc<->mb changes from earlier this month involving the C-locale.
  82. * Add a couple of ugly hacks to support *wprintf.
  83. * Add a mini iconv() and iconv implementation (requires locale support).
  84. *
  85. * Aug 1, 2003
  86. * Bug fix for mbrtowc.
  87. *
  88. * Aug 18, 2003
  89. * Bug fix: _wchar_utf8sntowcs and _wchar_wcsntoutf8s now set errno if EILSEQ.
  90. *
  91. * Feb 11, 2004
  92. * Bug fix: Fix size check for remaining output space in iconv().
  93. *
  94. * Manuel
  95. */
  96. #ifdef _LIBC
  97. #include <errno.h>
  98. #include <stddef.h>
  99. #include <limits.h>
  100. #include <stdint.h>
  101. #include <inttypes.h>
  102. #include <stdlib.h>
  103. #include <stdio.h>
  104. #include <assert.h>
  105. #include <locale.h>
  106. #include <wchar.h>
  107. #include <bits/uClibc_uwchar.h>
  108. /**********************************************************************/
  109. #ifdef __UCLIBC_HAS_LOCALE__
  110. #ifdef __UCLIBC_MJN3_ONLY__
  111. #ifdef L_iswspace
  112. /* generates one warning */
  113. #warning TODO: Fix Cc2wc* and Cwc2c* defines!
  114. #endif
  115. #endif /* __UCLIBC_MJN3_ONLY__ */
  116. #define ENCODING (__UCLIBC_CURLOCALE->encoding)
  117. #define Cc2wc_IDX_SHIFT __LOCALE_DATA_Cc2wc_IDX_SHIFT
  118. #define Cc2wc_ROW_LEN __LOCALE_DATA_Cc2wc_ROW_LEN
  119. #define Cwc2c_DOMAIN_MAX __LOCALE_DATA_Cwc2c_DOMAIN_MAX
  120. #define Cwc2c_TI_SHIFT __LOCALE_DATA_Cwc2c_TI_SHIFT
  121. #define Cwc2c_TT_SHIFT __LOCALE_DATA_Cwc2c_TT_SHIFT
  122. #define Cwc2c_TI_LEN __LOCALE_DATA_Cwc2c_TI_LEN
  123. #ifndef __CTYPE_HAS_UTF_8_LOCALES
  124. #warning __CTYPE_HAS_UTF_8_LOCALES not set!
  125. #endif
  126. #else /* __UCLIBC_HAS_LOCALE__ */
  127. #ifdef __UCLIBC_MJN3_ONLY__
  128. #ifdef L_btowc
  129. /* emit only once */
  130. #warning fix preprocessor logic testing locale settings
  131. #endif
  132. #endif
  133. #define ENCODING (__ctype_encoding_7_bit)
  134. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  135. #error __CTYPE_HAS_8_BIT_LOCALES is defined!
  136. #endif
  137. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  138. #error __CTYPE_HAS_UTF_8_LOCALES is defined!
  139. #endif
  140. #undef L__wchar_utf8sntowcs
  141. #undef L__wchar_wcsntoutf8s
  142. #endif /* __UCLIBC_HAS_LOCALE__ */
  143. /**********************************************************************/
  144. #if WCHAR_MAX > 0xffffUL
  145. #define UTF_8_MAX_LEN 6
  146. #else
  147. #define UTF_8_MAX_LEN 3
  148. #endif
  149. #define KUHN 1
  150. /* Implementation-specific work functions. */
  151. extern size_t _wchar_utf8sntowcs(wchar_t *__restrict pwc, size_t wn,
  152. const char **__restrict src, size_t n,
  153. mbstate_t *ps, int allow_continuation) attribute_hidden;
  154. extern size_t _wchar_wcsntoutf8s(char *__restrict s, size_t n,
  155. const wchar_t **__restrict src, size_t wn) attribute_hidden;
  156. #endif
  157. /**********************************************************************/
  158. #ifdef L_btowc
  159. wint_t btowc(int c)
  160. {
  161. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  162. wchar_t wc;
  163. unsigned char buf[1];
  164. mbstate_t mbstate;
  165. if (c != EOF) {
  166. *buf = (unsigned char) c;
  167. mbstate.__mask = 0; /* Initialize the mbstate. */
  168. if (mbrtowc(&wc, (char*) buf, 1, &mbstate) <= 1) {
  169. return wc;
  170. }
  171. }
  172. return WEOF;
  173. #else /* !__CTYPE_HAS_8_BIT_LOCALES */
  174. #ifdef __UCLIBC_HAS_LOCALE__
  175. assert((ENCODING == __ctype_encoding_7_bit)
  176. || (ENCODING == __ctype_encoding_utf8));
  177. #endif
  178. /* If we don't have 8-bit locale support, then this is trivial since
  179. * anything outside of 0-0x7f is illegal in C/POSIX and UTF-8 locales. */
  180. return (((unsigned int)c) < 0x80) ? c : WEOF;
  181. #endif /* !__CTYPE_HAS_8_BIT_LOCALES */
  182. }
  183. libc_hidden_def(btowc)
  184. #endif
  185. /**********************************************************************/
  186. #ifdef L_wctob
  187. /* Note: We completely ignore ps in all currently supported conversions. */
  188. int wctob(wint_t c)
  189. {
  190. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  191. unsigned char buf[MB_LEN_MAX];
  192. return (wcrtomb((char*) buf, c, NULL) == 1) ? *buf : EOF;
  193. #else /* __CTYPE_HAS_8_BIT_LOCALES */
  194. #ifdef __UCLIBC_HAS_LOCALE__
  195. assert((ENCODING == __ctype_encoding_7_bit)
  196. || (ENCODING == __ctype_encoding_utf8));
  197. #endif /* __UCLIBC_HAS_LOCALE__ */
  198. /* If we don't have 8-bit locale support, then this is trivial since
  199. * anything outside of 0-0x7f is illegal in C/POSIX and UTF-8 locales. */
  200. /* TODO: need unsigned version of wint_t... */
  201. /* return (((unsigned int)c) < 0x80) ? c : WEOF; */
  202. return ((c >= 0) && (c < 0x80)) ? c : EOF;
  203. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  204. }
  205. #endif
  206. /**********************************************************************/
  207. #ifdef L_mbsinit
  208. int mbsinit(const mbstate_t *ps)
  209. {
  210. return !ps || !ps->__mask;
  211. }
  212. libc_hidden_def(mbsinit)
  213. #endif
  214. /**********************************************************************/
  215. #ifdef L_mbrlen
  216. size_t mbrlen(const char *__restrict s, size_t n, mbstate_t *__restrict ps)
  217. {
  218. static mbstate_t mbstate; /* Rely on bss 0-init. */
  219. return mbrtowc(NULL, s, n, (ps != NULL) ? ps : &mbstate);
  220. }
  221. libc_hidden_def(mbrlen)
  222. #endif
  223. /**********************************************************************/
  224. #ifdef L_mbrtowc
  225. size_t mbrtowc(wchar_t *__restrict pwc, const char *__restrict s,
  226. size_t n, mbstate_t *__restrict ps)
  227. {
  228. static mbstate_t mbstate; /* Rely on bss 0-init. */
  229. wchar_t wcbuf[1];
  230. const char *p;
  231. size_t r;
  232. char empty_string[1]; /* Avoid static to be fPIC friendly. */
  233. if (!ps) {
  234. ps = &mbstate;
  235. }
  236. if (!s) {
  237. pwc = (wchar_t *) s; /* NULL */
  238. empty_string[0] = 0; /* Init the empty string when necessary. */
  239. s = empty_string;
  240. n = 1;
  241. } else if (*s == '\0') {
  242. /* According to the ISO C 89 standard this is the expected behaviour. */
  243. return 0;
  244. } else if (!n) {
  245. /* TODO: change error code? */
  246. #if 0
  247. return (ps->__mask && (ps->__wc == 0xffffU))
  248. ? ((size_t) -1) : ((size_t) -2);
  249. #else
  250. return 0;
  251. #endif
  252. }
  253. p = s;
  254. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  255. /* Need to do this here since mbsrtowcs doesn't allow incompletes. */
  256. if (ENCODING == __ctype_encoding_utf8) {
  257. if (!pwc) {
  258. pwc = wcbuf;
  259. }
  260. r = _wchar_utf8sntowcs(pwc, 1, &p, n, ps, 1);
  261. return (r == 1) ? (p-s) : r; /* Need to return 0 if nul char. */
  262. }
  263. #endif
  264. #ifdef __UCLIBC_MJN3_ONLY__
  265. #warning TODO: This adds a trailing nul!
  266. #endif /* __UCLIBC_MJN3_ONLY__ */
  267. r = mbsnrtowcs(wcbuf, &p, SIZE_MAX, 1, ps);
  268. if (((ssize_t) r) >= 0) {
  269. if (pwc) {
  270. *pwc = *wcbuf;
  271. }
  272. }
  273. return (size_t) r;
  274. }
  275. libc_hidden_def(mbrtowc)
  276. #endif
  277. /**********************************************************************/
  278. #ifdef L_wcrtomb
  279. /* Note: We completely ignore ps in all currently supported conversions. */
  280. /* TODO: Check for valid state anyway? */
  281. size_t wcrtomb(register char *__restrict s, wchar_t wc,
  282. mbstate_t *__restrict ps)
  283. {
  284. #ifdef __UCLIBC_MJN3_ONLY__
  285. #warning TODO: Should wcsnrtombs nul-terminate unconditionally? Check glibc.
  286. #endif /* __UCLIBC_MJN3_ONLY__ */
  287. wchar_t wcbuf[1];
  288. const wchar_t *pwc;
  289. size_t r;
  290. char buf[MB_LEN_MAX];
  291. if (!s) {
  292. s = buf;
  293. wc = 0;
  294. }
  295. pwc = wcbuf;
  296. wcbuf[0] = wc;
  297. r = wcsnrtombs(s, &pwc, 1, MB_LEN_MAX, ps);
  298. return (r != 0) ? r : 1;
  299. }
  300. libc_hidden_def(wcrtomb)
  301. #endif
  302. /**********************************************************************/
  303. #ifdef L_mbsrtowcs
  304. size_t mbsrtowcs(wchar_t *__restrict dst, const char **__restrict src,
  305. size_t len, mbstate_t *__restrict ps)
  306. {
  307. static mbstate_t mbstate; /* Rely on bss 0-init. */
  308. return mbsnrtowcs(dst, src, SIZE_MAX, len,
  309. ((ps != NULL) ? ps : &mbstate));
  310. }
  311. libc_hidden_def(mbsrtowcs)
  312. #endif
  313. /**********************************************************************/
  314. #ifdef L_wcsrtombs
  315. /* Note: We completely ignore ps in all currently supported conversions.
  316. * TODO: Check for valid state anyway? */
  317. size_t wcsrtombs(char *__restrict dst, const wchar_t **__restrict src,
  318. size_t len, mbstate_t *__restrict ps)
  319. {
  320. return wcsnrtombs(dst, src, SIZE_MAX, len, ps);
  321. }
  322. libc_hidden_def(wcsrtombs)
  323. #endif
  324. /**********************************************************************/
  325. #ifdef L__wchar_utf8sntowcs
  326. /* Define DECODER to generate a UTF-8 decoder which passes Markus Kuhn's
  327. * UTF-8-test.txt strss test.
  328. */
  329. /* #define DECODER */
  330. #ifdef DECODER
  331. #ifndef KUHN
  332. #define KUHN
  333. #endif
  334. #endif
  335. size_t attribute_hidden _wchar_utf8sntowcs(wchar_t *__restrict pwc, size_t wn,
  336. const char **__restrict src, size_t n,
  337. mbstate_t *ps, int allow_continuation)
  338. {
  339. register const char *s;
  340. __uwchar_t mask;
  341. __uwchar_t wc;
  342. wchar_t wcbuf[1];
  343. size_t count;
  344. int incr;
  345. s = *src;
  346. assert(s != NULL);
  347. assert(ps != NULL);
  348. incr = 1;
  349. /* NOTE: The following is an AWFUL HACK! In order to support %s in
  350. * wprintf, we need to be able to compute the number of wchars needed
  351. * for the mbs conversion, not to exceed the precision specified.
  352. * But if dst is NULL, the return value is the length assuming a
  353. * sufficiently sized buffer. So, we allow passing of (wchar_t *) ps
  354. * as pwc in order to flag that we really want the length, subject
  355. * to the restricted buffer size and no partial conversions.
  356. * See mbsnrtowcs() as well. */
  357. if (!pwc || (pwc == ((wchar_t *)ps))) {
  358. if (!pwc) {
  359. wn = SIZE_MAX;
  360. }
  361. pwc = wcbuf;
  362. incr = 0;
  363. }
  364. /* This is really here only to support the glibc extension function
  365. * __mbsnrtowcs which apparently returns 0 if wn == 0 without any
  366. * check on the validity of the mbstate. */
  367. if (!(count = wn)) {
  368. return 0;
  369. }
  370. if ((mask = (__uwchar_t) ps->__mask) != 0) { /* A continuation... */
  371. #ifdef DECODER
  372. wc = (__uwchar_t) ps->__wc;
  373. if (n) {
  374. goto CONTINUE;
  375. }
  376. goto DONE;
  377. #else
  378. if ((wc = (__uwchar_t) ps->__wc) != 0xffffU) {
  379. /* TODO: change error code here and below? */
  380. if (n) {
  381. goto CONTINUE;
  382. }
  383. goto DONE;
  384. }
  385. __set_errno(EILSEQ);
  386. return (size_t) -1; /* We're in an error state. */
  387. #endif
  388. }
  389. do {
  390. if (!n) {
  391. goto DONE;
  392. }
  393. --n;
  394. if ((wc = ((unsigned char) *s++)) >= 0x80) { /* Not ASCII... */
  395. mask = 0x40;
  396. #ifdef __UCLIBC_MJN3_ONLY__
  397. #warning TODO: Fix range for 16 bit wchar_t case.
  398. #endif
  399. if (( ((unsigned char)(s[-1] - 0xc0)) < (0xfe - 0xc0) ) &&
  400. (((unsigned char)s[-1] != 0xc0 ) && ((unsigned char)s[-1] != 0xc1 ))) {
  401. goto START;
  402. }
  403. BAD:
  404. #ifdef DECODER
  405. wc = 0xfffdU;
  406. goto COMPLETE;
  407. #else
  408. ps->__mask = mask;
  409. ps->__wc = 0xffffU;
  410. __set_errno(EILSEQ);
  411. return (size_t) -1; /* Illegal start byte! */
  412. #endif
  413. CONTINUE:
  414. while (n) {
  415. --n;
  416. if ((*s & 0xc0) != 0x80) {
  417. goto BAD;
  418. }
  419. mask <<= 5;
  420. wc <<= 6;
  421. wc += (*s & 0x3f); /* keep seperate for bcc (smaller code) */
  422. ++s;
  423. START:
  424. wc &= ~(mask << 1);
  425. if ((wc & mask) == 0) { /* Character completed. */
  426. if ((mask >>= 5) == 0x40) {
  427. mask += mask;
  428. }
  429. /* Check for invalid sequences (longer than necessary)
  430. * and invalid chars. */
  431. if ( (wc < mask) /* Sequence not minimal length. */
  432. #ifdef KUHN
  433. #if UTF_8_MAX_LEN == 3
  434. #error broken since mask can overflow!!
  435. /* For plane 0, these are the only defined values.*/
  436. || (wc > 0xfffdU)
  437. #else
  438. /* Note that we don't need to worry about exceeding */
  439. /* 31 bits as that is the most that UTF-8 provides. */
  440. || ( ((__uwchar_t)(wc - 0xfffeU)) < 2)
  441. #endif
  442. || ( ((__uwchar_t)(wc - 0xd800U)) < (0xe000U - 0xd800U) )
  443. #endif /* KUHN */
  444. ) {
  445. goto BAD;
  446. }
  447. goto COMPLETE;
  448. }
  449. }
  450. /* Character potentially valid but incomplete. */
  451. if (!allow_continuation) {
  452. if (count != wn) {
  453. return 0;
  454. }
  455. /* NOTE: The following can fail if you allow and then disallow
  456. * continuation!!! */
  457. #if UTF_8_MAX_LEN == 3
  458. #error broken since mask can overflow!!
  459. #endif
  460. /* Need to back up... */
  461. do {
  462. --s;
  463. } while ((mask >>= 5) >= 0x40);
  464. goto DONE;
  465. }
  466. ps->__mask = (wchar_t) mask;
  467. ps->__wc = (wchar_t) wc;
  468. *src = s;
  469. return (size_t) -2;
  470. }
  471. COMPLETE:
  472. *pwc = wc;
  473. pwc += incr;
  474. }
  475. #ifdef DECODER
  476. while (--count);
  477. #else
  478. while (wc && --count);
  479. if (!wc) {
  480. s = NULL;
  481. }
  482. #endif
  483. DONE:
  484. /* ps->__wc is irrelavent here. */
  485. ps->__mask = 0;
  486. if (pwc != wcbuf) {
  487. *src = s;
  488. }
  489. return wn - count;
  490. }
  491. #endif
  492. /**********************************************************************/
  493. #ifdef L__wchar_wcsntoutf8s
  494. size_t attribute_hidden _wchar_wcsntoutf8s(char *__restrict s, size_t n,
  495. const wchar_t **__restrict src, size_t wn)
  496. {
  497. register char *p;
  498. size_t len, t;
  499. __uwchar_t wc;
  500. const __uwchar_t *swc;
  501. int store;
  502. char buf[MB_LEN_MAX];
  503. char m;
  504. store = 1;
  505. /* NOTE: The following is an AWFUL HACK! In order to support %ls in
  506. * printf, we need to be able to compute the number of bytes needed
  507. * for the mbs conversion, not to exceed the precision specified.
  508. * But if dst is NULL, the return value is the length assuming a
  509. * sufficiently sized buffer. So, we allow passing of (char *) src
  510. * as dst in order to flag that we really want the length, subject
  511. * to the restricted buffer size and no partial conversions.
  512. * See wcsnrtombs() as well. */
  513. if (!s || (s == ((char *) src))) {
  514. if (!s) {
  515. n = SIZE_MAX;
  516. }
  517. s = buf;
  518. store = 0;
  519. }
  520. t = n;
  521. swc = (const __uwchar_t *) *src;
  522. assert(swc != NULL);
  523. while (wn && t) {
  524. wc = *swc;
  525. *s = wc;
  526. len = 1;
  527. if (wc >= 0x80) {
  528. #ifdef KUHN
  529. if (
  530. #if UTF_8_MAX_LEN == 3
  531. /* For plane 0, these are the only defined values.*/
  532. /* Note that we don't need to worry about exceeding */
  533. /* 31 bits as that is the most that UTF-8 provides. */
  534. (wc > 0xfffdU)
  535. #else
  536. /* UTF_8_MAX_LEN == 6 */
  537. (wc > 0x7fffffffUL)
  538. || ( ((__uwchar_t)(wc - 0xfffeU)) < 2)
  539. #endif
  540. || ( ((__uwchar_t)(wc - 0xd800U)) < (0xe000U - 0xd800U) )
  541. ) {
  542. __set_errno(EILSEQ);
  543. return (size_t) -1;
  544. }
  545. #else /* KUHN */
  546. #if UTF_8_MAX_LEN != 3
  547. if (wc > 0x7fffffffUL) { /* Value too large. */
  548. __set_errno(EILSEQ);
  549. return (size_t) -1;
  550. }
  551. #endif
  552. #endif /* KUHN */
  553. wc >>= 1;
  554. p = s;
  555. do {
  556. ++p;
  557. } while (wc >>= 5);
  558. wc = *swc;
  559. if ((len = p - s) > t) { /* Not enough space. */
  560. break;
  561. }
  562. m = 0x80;
  563. while( p>s ) {
  564. m = (m >> 1) | 0x80;
  565. *--p = (wc & 0x3f) | 0x80;
  566. wc >>= 6;
  567. }
  568. *s |= (m << 1);
  569. } else if (wc == 0) { /* End of string. */
  570. swc = NULL;
  571. break;
  572. }
  573. ++swc;
  574. --wn;
  575. t -= len;
  576. if (store) {
  577. s += len;
  578. }
  579. }
  580. if (store) {
  581. *src = (const wchar_t *) swc;
  582. }
  583. return n - t;
  584. }
  585. #endif
  586. /**********************************************************************/
  587. #ifdef L_mbsnrtowcs
  588. /* WARNING: We treat len as SIZE_MAX when dst is NULL! */
  589. size_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
  590. size_t NMC, size_t len, mbstate_t *__restrict ps)
  591. {
  592. static mbstate_t mbstate; /* Rely on bss 0-init. */
  593. wchar_t wcbuf[1];
  594. const char *s;
  595. size_t count;
  596. int incr;
  597. if (!ps) {
  598. ps = &mbstate;
  599. }
  600. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  601. if (ENCODING == __ctype_encoding_utf8) {
  602. size_t r;
  603. return ((r = _wchar_utf8sntowcs(dst, len, src, NMC, ps, 1))
  604. != (size_t) -2) ? r : 0;
  605. }
  606. #endif
  607. incr = 1;
  608. /* NOTE: The following is an AWFUL HACK! In order to support %s in
  609. * wprintf, we need to be able to compute the number of wchars needed
  610. * for the mbs conversion, not to exceed the precision specified.
  611. * But if dst is NULL, the return value is the length assuming a
  612. * sufficiently sized buffer. So, we allow passing of ((wchar_t *)ps)
  613. * as dst in order to flag that we really want the length, subject
  614. * to the restricted buffer size and no partial conversions.
  615. * See _wchar_utf8sntowcs() as well. */
  616. if (!dst || (dst == ((wchar_t *)ps))) {
  617. if (!dst) {
  618. len = SIZE_MAX;
  619. }
  620. dst = wcbuf;
  621. incr = 0;
  622. }
  623. /* Since all the following encodings are single-byte encodings... */
  624. if (len > NMC) {
  625. len = NMC;
  626. }
  627. count = len;
  628. s = *src;
  629. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  630. if (ENCODING == __ctype_encoding_8_bit) {
  631. wchar_t wc;
  632. while (count) {
  633. if ((wc = ((unsigned char)(*s))) >= 0x80) { /* Non-ASCII... */
  634. wc -= 0x80;
  635. wc = __UCLIBC_CURLOCALE->tbl8c2wc[
  636. (__UCLIBC_CURLOCALE->idx8c2wc[wc >> Cc2wc_IDX_SHIFT]
  637. << Cc2wc_IDX_SHIFT) + (wc & (Cc2wc_ROW_LEN - 1))];
  638. if (!wc) {
  639. goto BAD;
  640. }
  641. }
  642. if (!(*dst = wc)) {
  643. s = NULL;
  644. break;
  645. }
  646. dst += incr;
  647. ++s;
  648. --count;
  649. }
  650. if (dst != wcbuf) {
  651. *src = s;
  652. }
  653. return len - count;
  654. }
  655. #endif
  656. #ifdef __UCLIBC_HAS_LOCALE__
  657. assert(ENCODING == __ctype_encoding_7_bit);
  658. #endif
  659. while (count) {
  660. if ((*dst = (unsigned char) *s) == 0) {
  661. s = NULL;
  662. break;
  663. }
  664. if (*dst >= 0x80) {
  665. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  666. BAD:
  667. #endif
  668. __set_errno(EILSEQ);
  669. return (size_t) -1;
  670. }
  671. ++s;
  672. dst += incr;
  673. --count;
  674. }
  675. if (dst != wcbuf) {
  676. *src = s;
  677. }
  678. return len - count;
  679. }
  680. libc_hidden_def(mbsnrtowcs)
  681. #endif
  682. /**********************************************************************/
  683. #ifdef L_wcsnrtombs
  684. /* WARNING: We treat len as SIZE_MAX when dst is NULL! */
  685. /* Note: We completely ignore ps in all currently supported conversions.
  686. * TODO: Check for valid state anyway? */
  687. size_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
  688. size_t NWC, size_t len, mbstate_t *__restrict ps)
  689. {
  690. const __uwchar_t *s;
  691. size_t count;
  692. int incr;
  693. char buf[MB_LEN_MAX];
  694. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  695. if (ENCODING == __ctype_encoding_utf8) {
  696. return _wchar_wcsntoutf8s(dst, len, src, NWC);
  697. }
  698. #endif /* __CTYPE_HAS_UTF_8_LOCALES */
  699. incr = 1;
  700. /* NOTE: The following is an AWFUL HACK! In order to support %ls in
  701. * printf, we need to be able to compute the number of bytes needed
  702. * for the mbs conversion, not to exceed the precision specified.
  703. * But if dst is NULL, the return value is the length assuming a
  704. * sufficiently sized buffer. So, we allow passing of (char *) src
  705. * as dst in order to flag that we really want the length, subject
  706. * to the restricted buffer size and no partial conversions.
  707. * See _wchar_wcsntoutf8s() as well. */
  708. if (!dst || (dst == ((char *) src))) {
  709. if (!dst) {
  710. len = SIZE_MAX;
  711. }
  712. dst = buf;
  713. incr = 0;
  714. }
  715. /* Since all the following encodings are single-byte encodings... */
  716. if (len > NWC) {
  717. len = NWC;
  718. }
  719. count = len;
  720. s = (const __uwchar_t *) *src;
  721. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  722. if (ENCODING == __ctype_encoding_8_bit) {
  723. __uwchar_t wc;
  724. __uwchar_t u;
  725. while (count) {
  726. if ((wc = *s) <= 0x7f) {
  727. if (!(*dst = (unsigned char) wc)) {
  728. s = NULL;
  729. break;
  730. }
  731. } else {
  732. u = 0;
  733. if (wc <= Cwc2c_DOMAIN_MAX) {
  734. u = __UCLIBC_CURLOCALE->idx8wc2c[wc >> (Cwc2c_TI_SHIFT
  735. + Cwc2c_TT_SHIFT)];
  736. u = __UCLIBC_CURLOCALE->tbl8wc2c[(u << Cwc2c_TI_SHIFT)
  737. + ((wc >> Cwc2c_TT_SHIFT)
  738. & ((1 << Cwc2c_TI_SHIFT)-1))];
  739. u = __UCLIBC_CURLOCALE->tbl8wc2c[Cwc2c_TI_LEN
  740. + (u << Cwc2c_TT_SHIFT)
  741. + (wc & ((1 << Cwc2c_TT_SHIFT)-1))];
  742. }
  743. #ifdef __WCHAR_REPLACEMENT_CHAR
  744. *dst = (unsigned char) ( u ? u : __WCHAR_REPLACEMENT_CHAR );
  745. #else /* __WCHAR_REPLACEMENT_CHAR */
  746. if (!u) {
  747. goto BAD;
  748. }
  749. *dst = (unsigned char) u;
  750. #endif /* __WCHAR_REPLACEMENT_CHAR */
  751. }
  752. ++s;
  753. dst += incr;
  754. --count;
  755. }
  756. if (dst != buf) {
  757. *src = (const wchar_t *) s;
  758. }
  759. return len - count;
  760. }
  761. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  762. #ifdef __UCLIBC_HAS_LOCALE__
  763. assert(ENCODING == __ctype_encoding_7_bit);
  764. #endif
  765. while (count) {
  766. if (*s >= 0x80) {
  767. #if defined(__CTYPE_HAS_8_BIT_LOCALES) && !defined(__WCHAR_REPLACEMENT_CHAR)
  768. BAD:
  769. #endif
  770. __set_errno(EILSEQ);
  771. return (size_t) -1;
  772. }
  773. if ((*dst = (unsigned char) *s) == 0) {
  774. s = NULL;
  775. break;
  776. }
  777. ++s;
  778. dst += incr;
  779. --count;
  780. }
  781. if (dst != buf) {
  782. *src = (const wchar_t *) s;
  783. }
  784. return len - count;
  785. }
  786. libc_hidden_def(wcsnrtombs)
  787. #endif
  788. /**********************************************************************/
  789. #ifdef L_wcswidth
  790. #ifdef __UCLIBC_MJN3_ONLY__
  791. #warning REMINDER: If we start doing translit, wcwidth and wcswidth will need updating.
  792. #warning TODO: Update wcwidth to match latest by Kuhn.
  793. #endif
  794. #if defined(__UCLIBC_HAS_LOCALE__) && \
  795. ( defined(__CTYPE_HAS_8_BIT_LOCALES) || defined(__CTYPE_HAS_UTF_8_LOCALES) )
  796. static const unsigned char new_idx[] = {
  797. 0, 5, 5, 6, 10, 15, 28, 39,
  798. 48, 48, 71, 94, 113, 128, 139, 154,
  799. 175, 186, 188, 188, 188, 188, 188, 188,
  800. 203, 208, 208, 208, 208, 208, 208, 208,
  801. 208, 219, 219, 219, 222, 222, 222, 222,
  802. 222, 222, 222, 222, 222, 222, 222, 224,
  803. 224, 231, 231, 231, 231, 231, 231, 231,
  804. 231, 231, 231, 231, 231, 231, 231, 231,
  805. 231, 231, 231, 231, 231, 231, 231, 231,
  806. 231, 231, 231, 231, 231, 231, 231, 231,
  807. 231, 231, 231, 231, 231, 231, 231, 231,
  808. 231, 231, 231, 231, 231, 231, 231, 231,
  809. 231, 231, 231, 231, 231, 231, 231, 231,
  810. 231, 231, 231, 231, 231, 231, 231, 231,
  811. 231, 231, 231, 231, 231, 231, 231, 231,
  812. 231, 231, 231, 231, 231, 231, 231, 231,
  813. 231, 231, 231, 231, 231, 231, 231, 231,
  814. 231, 231, 231, 231, 231, 231, 231, 231,
  815. 231, 231, 231, 231, 231, 231, 231, 231,
  816. 231, 231, 231, 231, 231, 231, 231, 231,
  817. 231, 231, 231, 231, 231, 233, 233, 233,
  818. 233, 233, 233, 233, 234, 234, 234, 234,
  819. 234, 234, 234, 234, 234, 234, 234, 234,
  820. 234, 234, 234, 234, 234, 234, 234, 234,
  821. 234, 234, 234, 234, 234, 234, 234, 234,
  822. 234, 234, 234, 234, 234, 234, 234, 234,
  823. 234, 234, 234, 234, 234, 234, 234, 234,
  824. 236, 236, 236, 236, 236, 236, 236, 236,
  825. 236, 236, 236, 236, 236, 236, 236, 236,
  826. 236, 236, 236, 236, 236, 236, 236, 236,
  827. 236, 236, 236, 236, 236, 236, 236, 236,
  828. 236, 237, 237, 238, 241, 241, 242, 249,
  829. 255,
  830. };
  831. static const unsigned char new_tbl[] = {
  832. 0x00, 0x01, 0x20, 0x7f, 0xa0, 0x00, 0x00, 0x50,
  833. 0x60, 0x70, 0x00, 0x83, 0x87, 0x88, 0x8a, 0x00,
  834. 0x91, 0xa2, 0xa3, 0xba, 0xbb, 0xbe, 0xbf, 0xc0,
  835. 0xc1, 0xc3, 0xc4, 0xc5, 0x00, 0x4b, 0x56, 0x70,
  836. 0x71, 0xd6, 0xe5, 0xe7, 0xe9, 0xea, 0xee, 0x00,
  837. 0x0f, 0x10, 0x11, 0x12, 0x30, 0x4b, 0xa6, 0xb1,
  838. 0x00, 0x01, 0x03, 0x3c, 0x3d, 0x41, 0x49, 0x4d,
  839. 0x4e, 0x51, 0x55, 0x62, 0x64, 0x81, 0x82, 0xbc,
  840. 0xbd, 0xc1, 0xc5, 0xcd, 0xce, 0xe2, 0xe4, 0x00,
  841. 0x02, 0x03, 0x3c, 0x3d, 0x41, 0x43, 0x47, 0x49,
  842. 0x4b, 0x4e, 0x70, 0x72, 0x81, 0x83, 0xbc, 0xbd,
  843. 0xc1, 0xc6, 0xc7, 0xc9, 0xcd, 0xce, 0x00, 0x01,
  844. 0x02, 0x3c, 0x3d, 0x3f, 0x40, 0x41, 0x44, 0x4d,
  845. 0x4e, 0x56, 0x57, 0x82, 0x83, 0xc0, 0xc1, 0xcd,
  846. 0xce, 0x00, 0x3e, 0x41, 0x46, 0x49, 0x4a, 0x4e,
  847. 0x55, 0x57, 0xbf, 0xc0, 0xc6, 0xc7, 0xcc, 0xce,
  848. 0x00, 0x41, 0x44, 0x4d, 0x4e, 0xca, 0xcb, 0xd2,
  849. 0xd5, 0xd6, 0xd7, 0x00, 0x31, 0x32, 0x34, 0x3b,
  850. 0x47, 0x4f, 0xb1, 0xb2, 0xb4, 0xba, 0xbb, 0xbd,
  851. 0xc8, 0xce, 0x00, 0x18, 0x1a, 0x35, 0x36, 0x37,
  852. 0x38, 0x39, 0x3a, 0x71, 0x7f, 0x80, 0x85, 0x86,
  853. 0x88, 0x90, 0x98, 0x99, 0xbd, 0xc6, 0xc7, 0x00,
  854. 0x2d, 0x31, 0x32, 0x33, 0x36, 0x38, 0x39, 0x3a,
  855. 0x58, 0x5a, 0x00, 0x60, 0x00, 0x12, 0x15, 0x32,
  856. 0x35, 0x52, 0x54, 0x72, 0x74, 0xb7, 0xbe, 0xc6,
  857. 0xc7, 0xc9, 0xd4, 0x00, 0x0b, 0x0f, 0xa9, 0xaa,
  858. 0x00, 0x0b, 0x10, 0x2a, 0x2f, 0x60, 0x64, 0x6a,
  859. 0x70, 0xd0, 0xeb, 0x00, 0x29, 0x2b, 0x00, 0x80,
  860. 0x00, 0x2a, 0x30, 0x3f, 0x40, 0x99, 0x9b, 0x00,
  861. 0xd0, 0x00, 0x00, 0xa4, 0x00, 0x00, 0x00, 0x1e,
  862. 0x1f, 0x00, 0x00, 0x10, 0x20, 0x24, 0x30, 0x70,
  863. 0xff, 0x00, 0x61, 0xe0, 0xe7, 0xf9, 0xfc,
  864. };
  865. static const signed char new_wtbl[] = {
  866. 0, -1, 1, -1, 1, 1, 0, 1,
  867. 0, 1, 1, 0, 1, 0, 1, 1,
  868. 0, 1, 0, 1, 0, 1, 0, 1,
  869. 0, 1, 0, 1, 1, 0, 1, 0,
  870. 1, 0, 1, 0, 1, 0, 1, 1,
  871. 0, 1, 0, 1, 0, 1, 0, 1,
  872. 1, 0, 1, 0, 1, 0, 1, 0,
  873. 1, 0, 1, 0, 1, 0, 1, 0,
  874. 1, 0, 1, 0, 1, 0, 1, 1,
  875. 0, 1, 0, 1, 0, 1, 0, 1,
  876. 0, 1, 0, 1, 0, 1, 0, 1,
  877. 0, 1, 0, 1, 0, 1, 1, 0,
  878. 1, 0, 1, 0, 1, 0, 1, 0,
  879. 1, 0, 1, 0, 1, 0, 1, 0,
  880. 1, 1, 0, 1, 0, 1, 0, 1,
  881. 0, 1, 0, 1, 0, 1, 0, 1,
  882. 1, 0, 1, 0, 1, 0, 1, 0,
  883. 1, 0, 1, 1, 0, 1, 0, 1,
  884. 0, 1, 0, 1, 0, 1, 0, 1,
  885. 0, 1, 1, 0, 1, 0, 1, 0,
  886. 1, 0, 1, 0, 1, 0, 1, 0,
  887. 1, 0, 1, 0, 1, 0, 1, 1,
  888. 0, 1, 0, 1, 0, 1, 0, 1,
  889. 0, 1, 2, 0, 1, 0, 1, 0,
  890. 1, 0, 1, 0, 1, 0, 1, 0,
  891. 1, 0, 1, 1, 0, 1, 0, 1,
  892. 1, 0, 1, 0, 1, 0, 1, 0,
  893. 1, 0, 1, 1, 2, 1, 1, 2,
  894. 2, 0, 2, 1, 2, 0, 2, 2,
  895. 1, 1, 2, 1, 1, 2, 1, 0,
  896. 1, 1, 0, 1, 0, 1, 2, 1,
  897. 0, 2, 1, 2, 1, 0, 1,
  898. };
  899. int wcswidth(const wchar_t *pwcs, size_t n)
  900. {
  901. int h, l, m, count;
  902. wchar_t wc;
  903. unsigned char b;
  904. if (ENCODING == __ctype_encoding_7_bit) {
  905. size_t i;
  906. for (i = 0 ; (i < n) && pwcs[i] ; i++) {
  907. if (pwcs[i] != (pwcs[i] & 0x7f)) {
  908. return -1;
  909. }
  910. }
  911. }
  912. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  913. else if (ENCODING == __ctype_encoding_8_bit) {
  914. mbstate_t mbstate;
  915. mbstate.__mask = 0; /* Initialize the mbstate. */
  916. if (wcsnrtombs(NULL, &pwcs, n, SIZE_MAX, &mbstate) == ((size_t) - 1)) {
  917. return -1;
  918. }
  919. }
  920. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  921. #if defined(__CTYPE_HAS_UTF_8_LOCALES) && defined(KUHN)
  922. /* For stricter handling of allowed unicode values... see comments above. */
  923. else if (ENCODING == __ctype_encoding_utf8) {
  924. size_t i;
  925. for (i = 0 ; (i < n) && pwcs[i] ; i++) {
  926. if ( (((__uwchar_t)((pwcs[i]) - 0xfffeU)) < 2)
  927. || (((__uwchar_t)((pwcs[i]) - 0xd800U)) < (0xe000U - 0xd800U))
  928. ) {
  929. return -1;
  930. }
  931. }
  932. }
  933. #endif /* __CTYPE_HAS_UTF_8_LOCALES */
  934. for (count = 0 ; n && (wc = *pwcs++) ; n--) {
  935. if (wc <= 0xff) {
  936. /* If we're here, wc != 0. */
  937. if ((wc < 32) || ((wc >= 0x7f) && (wc < 0xa0))) {
  938. return -1;
  939. }
  940. ++count;
  941. continue;
  942. }
  943. if (((unsigned int) wc) <= 0xffff) {
  944. b = wc & 0xff;
  945. h = (wc >> 8);
  946. l = new_idx[h];
  947. h = new_idx[h+1];
  948. while ((m = (l+h) >> 1) != l) {
  949. if (b >= new_tbl[m]) {
  950. l = m;
  951. } else { /* wc < tbl[m] */
  952. h = m;
  953. }
  954. }
  955. count += new_wtbl[l]; /* none should be -1. */
  956. continue;
  957. }
  958. /* Redo this to minimize average number of compares?*/
  959. if (wc >= 0x1d167) {
  960. if (wc <= 0x1d1ad) {
  961. if ((wc <= 0x1d169
  962. || (wc >= 0x1d173
  963. && (wc <= 0x1d182
  964. || (wc >= 0x1d185
  965. && (wc <= 0x1d18b
  966. || (wc >= 0x1d1aa))))))
  967. ) {
  968. continue;
  969. }
  970. } else if (((wc >= 0xe0020) && (wc <= 0xe007f)) || (wc == 0xe0001)) {
  971. continue;
  972. } else if ((wc >= 0x20000) && (wc <= 0x2ffff)) {
  973. ++count; /* need 2.. add one here */
  974. }
  975. #if (WCHAR_MAX > 0x7fffffffL)
  976. else if (wc > 0x7fffffffL) {
  977. return -1;
  978. }
  979. #endif /* (WCHAR_MAX > 0x7fffffffL) */
  980. }
  981. ++count;
  982. }
  983. return count;
  984. }
  985. #else /* __UCLIBC_HAS_LOCALE__ */
  986. int wcswidth(const wchar_t *pwcs, size_t n)
  987. {
  988. int count;
  989. wchar_t wc;
  990. size_t i;
  991. for (i = 0 ; (i < n) && pwcs[i] ; i++) {
  992. if (pwcs[i] != (pwcs[i] & 0x7f)) {
  993. return -1;
  994. }
  995. }
  996. for (count = 0 ; n && (wc = *pwcs++) ; n--) {
  997. if (wc <= 0xff) {
  998. /* If we're here, wc != 0. */
  999. if ((wc < 32) || ((wc >= 0x7f) && (wc < 0xa0))) {
  1000. return -1;
  1001. }
  1002. ++count;
  1003. continue;
  1004. } else {
  1005. return -1;
  1006. }
  1007. }
  1008. return count;
  1009. }
  1010. #endif /* __UCLIBC_HAS_LOCALE__ */
  1011. libc_hidden_def(wcswidth)
  1012. #endif
  1013. /**********************************************************************/
  1014. #ifdef L_wcwidth
  1015. int wcwidth(wchar_t wc)
  1016. {
  1017. return wcswidth(&wc, 1);
  1018. }
  1019. #endif
  1020. /**********************************************************************/
  1021. typedef struct {
  1022. mbstate_t tostate;
  1023. mbstate_t fromstate;
  1024. int tocodeset;
  1025. int fromcodeset;
  1026. int frombom;
  1027. int tobom;
  1028. int fromcodeset0;
  1029. int frombom0;
  1030. int tobom0;
  1031. int skip_invalid_input; /* To support iconv -c option. */
  1032. } _UC_iconv_t;
  1033. /* For the multibyte
  1034. * bit 0 means swap endian
  1035. * bit 1 means 2 byte
  1036. * bit 2 means 4 byte
  1037. *
  1038. */
  1039. #if defined L_iconv && defined _LIBC
  1040. /* Used externally only by iconv utility */
  1041. extern const unsigned char __iconv_codesets[];
  1042. libc_hidden_proto(__iconv_codesets)
  1043. #endif
  1044. #if defined L_iconv || defined L_iconv_main
  1045. const unsigned char __iconv_codesets[] =
  1046. "\x0a\xe0""WCHAR_T\x00" /* superset of UCS-4 but platform-endian */
  1047. #if __BYTE_ORDER == __BIG_ENDIAN
  1048. "\x08\xec""UCS-4\x00" /* always BE */
  1049. "\x0a\xec""UCS-4BE\x00"
  1050. "\x0a\xed""UCS-4LE\x00"
  1051. "\x09\xe4""UTF-32\x00" /* platform endian with BOM */
  1052. "\x0b\xe4""UTF-32BE\x00"
  1053. "\x0b\xe5""UTF-32LE\x00"
  1054. "\x08\xe2""UCS-2\x00" /* always BE */
  1055. "\x0a\xe2""UCS-2BE\x00"
  1056. "\x0a\xe3""UCS-2LE\x00"
  1057. "\x09\xea""UTF-16\x00" /* platform endian with BOM */
  1058. "\x0b\xea""UTF-16BE\x00"
  1059. "\x0b\xeb""UTF-16LE\x00"
  1060. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  1061. "\x08\xed""UCS-4\x00" /* always BE */
  1062. "\x0a\xed""UCS-4BE\x00"
  1063. "\x0a\xec""UCS-4LE\x00"
  1064. "\x09\xf4""UTF-32\x00" /* platform endian with BOM */
  1065. "\x0b\xe5""UTF-32BE\x00"
  1066. "\x0b\xe4""UTF-32LE\x00"
  1067. "\x08\xe3""UCS-2\x00" /* always BE */
  1068. "\x0a\xe3""UCS-2BE\x00"
  1069. "\x0a\xe2""UCS-2LE\x00"
  1070. "\x09\xfa""UTF-16\x00" /* platform endian with BOM */
  1071. "\x0b\xeb""UTF-16BE\x00"
  1072. "\x0b\xea""UTF-16LE\x00"
  1073. #endif
  1074. "\x08\x02""UTF-8\x00"
  1075. "\x0b\x01""US-ASCII\x00"
  1076. "\x07\x01""ASCII"; /* Must be last! (special case to save a nul) */
  1077. #endif
  1078. #if defined L_iconv && defined _LIBC
  1079. libc_hidden_data_def(__iconv_codesets)
  1080. #endif
  1081. #ifdef L_iconv
  1082. #include <iconv.h>
  1083. #include <string.h>
  1084. #include <endian.h>
  1085. #include <byteswap.h>
  1086. #if (__BYTE_ORDER != __BIG_ENDIAN) && (__BYTE_ORDER != __LITTLE_ENDIAN)
  1087. #error unsupported endianness for iconv
  1088. #endif
  1089. #ifndef __CTYPE_HAS_8_BIT_LOCALES
  1090. #error currently iconv requires 8 bit locales
  1091. #endif
  1092. #ifndef __CTYPE_HAS_UTF_8_LOCALES
  1093. #error currently iconv requires UTF-8 locales
  1094. #endif
  1095. enum {
  1096. IC_WCHAR_T = 0xe0,
  1097. IC_MULTIBYTE = 0xe0,
  1098. #if __BYTE_ORDER == __BIG_ENDIAN
  1099. IC_UCS_4 = 0xec,
  1100. IC_UTF_32 = 0xe4,
  1101. IC_UCS_2 = 0xe2,
  1102. IC_UTF_16 = 0xea,
  1103. #else
  1104. IC_UCS_4 = 0xed,
  1105. IC_UTF_32 = 0xe5,
  1106. IC_UCS_2 = 0xe3,
  1107. IC_UTF_16 = 0xeb,
  1108. #endif
  1109. IC_UTF_8 = 2,
  1110. IC_ASCII = 1
  1111. };
  1112. static int find_codeset(const char *name)
  1113. {
  1114. const unsigned char *s;
  1115. int codeset;
  1116. for (s = __iconv_codesets; *s; s += *s) {
  1117. if (!strcasecmp((char*) (s + 2), name)) {
  1118. return s[1];
  1119. }
  1120. }
  1121. /* The following is ripped from find_locale in locale.c. */
  1122. /* TODO: maybe CODESET_LIST + *s ??? */
  1123. /* 7bit is 1, UTF-8 is 2, 8-bit is >= 3 */
  1124. codeset = 2;
  1125. s = (const unsigned char *) __LOCALE_DATA_CODESET_LIST;
  1126. do {
  1127. ++codeset; /* Increment codeset first. */
  1128. if (!strcasecmp(__LOCALE_DATA_CODESET_LIST+*s, name)) {
  1129. return codeset;
  1130. }
  1131. } while (*++s);
  1132. return 0; /* No matching codeset! */
  1133. }
  1134. iconv_t weak_function iconv_open(const char *tocode, const char *fromcode)
  1135. {
  1136. register _UC_iconv_t *px;
  1137. int tocodeset, fromcodeset;
  1138. if (((tocodeset = find_codeset(tocode)) != 0)
  1139. && ((fromcodeset = find_codeset(fromcode)) != 0)) {
  1140. if ((px = malloc(sizeof(_UC_iconv_t))) != NULL) {
  1141. px->tocodeset = tocodeset;
  1142. px->tobom0 = px->tobom = (tocodeset & 0x10) >> 4;
  1143. px->fromcodeset0 = px->fromcodeset = fromcodeset;
  1144. px->frombom0 = px->frombom = (fromcodeset & 0x10) >> 4;
  1145. px->skip_invalid_input = px->tostate.__mask
  1146. = px->fromstate.__mask = 0;
  1147. return (iconv_t) px;
  1148. }
  1149. } else {
  1150. __set_errno(EINVAL);
  1151. }
  1152. return (iconv_t)(-1);
  1153. }
  1154. int weak_function iconv_close(iconv_t cd)
  1155. {
  1156. free(cd);
  1157. return 0;
  1158. }
  1159. size_t weak_function iconv(iconv_t cd, char **__restrict inbuf,
  1160. size_t *__restrict inbytesleft,
  1161. char **__restrict outbuf,
  1162. size_t *__restrict outbytesleft)
  1163. {
  1164. _UC_iconv_t *px = (_UC_iconv_t *) cd;
  1165. size_t nrcount, r;
  1166. wchar_t wc, wc2;
  1167. int inci, inco;
  1168. assert(px != (_UC_iconv_t *)(-1));
  1169. assert(sizeof(wchar_t) == 4);
  1170. if (!inbuf || !*inbuf) { /* Need to reinitialze conversion state. */
  1171. /* Note: For shift-state encodings we possibly need to output the
  1172. * shift sequence to return to initial state! */
  1173. if ((px->fromcodeset & 0xf0) == 0xe0) {
  1174. }
  1175. px->tostate.__mask = px->fromstate.__mask = 0;
  1176. px->fromcodeset = px->fromcodeset0;
  1177. px->tobom = px->tobom0;
  1178. px->frombom = px->frombom0;
  1179. return 0;
  1180. }
  1181. nrcount = 0;
  1182. while (*inbytesleft) {
  1183. if (!*outbytesleft) {
  1184. TOO_BIG:
  1185. __set_errno(E2BIG);
  1186. return (size_t) -1;
  1187. }
  1188. inci = inco = 1;
  1189. if (px->fromcodeset >= IC_MULTIBYTE) {
  1190. inci = (px->fromcodeset == IC_WCHAR_T) ? 4: (px->fromcodeset & 6);
  1191. if (*inbytesleft < inci) goto INVALID;
  1192. wc = (((unsigned int)((unsigned char)((*inbuf)[0]))) << 8)
  1193. + ((unsigned char)((*inbuf)[1]));
  1194. if (inci == 4) {
  1195. wc = (((unsigned int)((unsigned char)((*inbuf)[2]))) << 8)
  1196. + ((unsigned char)((*inbuf)[3])) + (wc << 16);
  1197. if (!(px->fromcodeset & 1)) wc = bswap_32(wc);
  1198. } else {
  1199. if (!(px->fromcodeset & 1)) wc = bswap_16(wc);
  1200. if (((px->fromcodeset & IC_UTF_16) == IC_UTF_16)
  1201. && (((__uwchar_t)(wc - 0xd800U)) < (0xdc00U - 0xd800U))
  1202. ) { /* surrogate */
  1203. wc =- 0xd800U;
  1204. if (*inbytesleft < 4) goto INVALID;
  1205. wc2 = (((unsigned int)((unsigned char)((*inbuf)[2]))) << 8)
  1206. + ((unsigned char)((*inbuf)[3]));
  1207. if (!(px->fromcodeset & 1)) wc = bswap_16(wc2);
  1208. if (((__uwchar_t)(wc2 -= 0xdc00U)) < (0xe0000U - 0xdc00U)) {
  1209. goto ILLEGAL;
  1210. }
  1211. inci = 4; /* Change inci here in case skipping illegals. */
  1212. wc = 0x10000UL + (wc << 10) + wc2;
  1213. }
  1214. }
  1215. if (px->frombom) {
  1216. px->frombom = 0;
  1217. if ((wc == 0xfeffU)
  1218. || (wc == ((inci == 4)
  1219. ? (((wchar_t) 0xfffe0000UL))
  1220. : ((wchar_t)(0xfffeUL))))
  1221. ) {
  1222. if (wc != 0xfeffU) {
  1223. px->fromcodeset ^= 1; /* toggle endianness */
  1224. wc = 0xfeffU;
  1225. }
  1226. if (!px->frombom) {
  1227. goto BOM_SKIP_OUTPUT;
  1228. }
  1229. goto GOT_BOM;
  1230. }
  1231. }
  1232. if (px->fromcodeset != IC_WCHAR_T) {
  1233. if (((__uwchar_t) wc) > (((px->fromcodeset & IC_UCS_4) == IC_UCS_4)
  1234. ? 0x7fffffffUL : 0x10ffffUL)
  1235. #ifdef KUHN
  1236. || (((__uwchar_t)(wc - 0xfffeU)) < 2)
  1237. || (((__uwchar_t)(wc - 0xd800U)) < (0xe000U - 0xd800U))
  1238. #endif
  1239. ) {
  1240. goto ILLEGAL;
  1241. }
  1242. }
  1243. } else if (px->fromcodeset == IC_UTF_8) {
  1244. const char *p = *inbuf;
  1245. r = _wchar_utf8sntowcs(&wc, 1, &p, *inbytesleft, &px->fromstate, 0);
  1246. if (((ssize_t) r) <= 0) { /* either EILSEQ or incomplete or nul */
  1247. if (((ssize_t) r) < 0) { /* either EILSEQ or incomplete or nul */
  1248. assert((r == (size_t)(-1)) || (r == (size_t)(-2)));
  1249. if (r == (size_t)(-2)) {
  1250. INVALID:
  1251. __set_errno(EINVAL);
  1252. } else {
  1253. px->fromstate.__mask = 0;
  1254. inci = 1;
  1255. ILLEGAL:
  1256. if (px->skip_invalid_input) {
  1257. px->skip_invalid_input = 2; /* flag for iconv utility */
  1258. goto BOM_SKIP_OUTPUT;
  1259. }
  1260. __set_errno(EILSEQ);
  1261. }
  1262. return (size_t)(-1);
  1263. }
  1264. #ifdef __UCLIBC_MJN3_ONLY__
  1265. #warning TODO: optimize this.
  1266. #endif
  1267. if (p != NULL) { /* incomplete char case */
  1268. goto INVALID;
  1269. }
  1270. p = *inbuf + 1; /* nul */
  1271. }
  1272. inci = p - *inbuf;
  1273. } else if ((wc = ((unsigned char)(**inbuf))) >= 0x80) { /* Non-ASCII... */
  1274. if (px->fromcodeset == IC_ASCII) { /* US-ASCII codeset */
  1275. goto ILLEGAL;
  1276. } else { /* some other 8-bit ascii-extension codeset */
  1277. const __codeset_8_bit_t *c8b
  1278. = __locale_mmap->codeset_8_bit + px->fromcodeset - 3;
  1279. wc -= 0x80;
  1280. wc = __UCLIBC_CURLOCALE->tbl8c2wc[
  1281. (c8b->idx8c2wc[wc >> Cc2wc_IDX_SHIFT]
  1282. << Cc2wc_IDX_SHIFT) + (wc & (Cc2wc_ROW_LEN - 1))];
  1283. if (!wc) {
  1284. goto ILLEGAL;
  1285. }
  1286. }
  1287. }
  1288. if (px->tobom) {
  1289. inci = 0;
  1290. wc = 0xfeffU;
  1291. GOT_BOM:
  1292. px->tobom = 0;
  1293. }
  1294. if (px->tocodeset >= IC_MULTIBYTE) {
  1295. inco = (px->tocodeset == IC_WCHAR_T) ? 4: (px->tocodeset & 6);
  1296. if (*outbytesleft < inco) goto TOO_BIG;
  1297. if (px->tocodeset != IC_WCHAR_T) {
  1298. if (((__uwchar_t) wc) > (((px->tocodeset & IC_UCS_4) == IC_UCS_4)
  1299. ? 0x7fffffffUL : 0x10ffffUL)
  1300. #ifdef KUHN
  1301. || (((__uwchar_t)(wc - 0xfffeU)) < 2)
  1302. || (((__uwchar_t)(wc - 0xd800U)) < (0xe000U - 0xd800U))
  1303. #endif
  1304. ) {
  1305. REPLACE_32:
  1306. wc = 0xfffd;
  1307. ++nrcount;
  1308. }
  1309. }
  1310. if (inco == 4) {
  1311. if (px->tocodeset & 1) wc = bswap_32(wc);
  1312. } else {
  1313. if (((__uwchar_t)wc ) > 0xffffU) {
  1314. if ((px->tocodeset & IC_UTF_16) != IC_UTF_16) {
  1315. goto REPLACE_32;
  1316. }
  1317. if (*outbytesleft < (inco = 4)) goto TOO_BIG;
  1318. wc2 = 0xdc00U + (wc & 0x3ff);
  1319. wc = 0xd800U + ((wc >> 10) & 0x3ff);
  1320. if (px->tocodeset & 1) {
  1321. wc = bswap_16(wc);
  1322. wc2 = bswap_16(wc2);
  1323. }
  1324. wc += (wc2 << 16);
  1325. } else if (px->tocodeset & 1) wc = bswap_16(wc);
  1326. }
  1327. (*outbuf)[0] = (char)((unsigned char)(wc));
  1328. (*outbuf)[1] = (char)((unsigned char)(wc >> 8));
  1329. if (inco == 4) {
  1330. (*outbuf)[2] = (char)((unsigned char)(wc >> 16));
  1331. (*outbuf)[3] = (char)((unsigned char)(wc >> 24));
  1332. }
  1333. } else if (px->tocodeset == IC_UTF_8) {
  1334. const wchar_t *pw = &wc;
  1335. do {
  1336. r = _wchar_wcsntoutf8s(*outbuf, *outbytesleft, &pw, 1);
  1337. if (r != (size_t)(-1)) {
  1338. #ifdef __UCLIBC_MJN3_ONLY__
  1339. #warning TODO: What happens for a nul?
  1340. #endif
  1341. if (r == 0) {
  1342. if (wc != 0) {
  1343. goto TOO_BIG;
  1344. }
  1345. ++r;
  1346. }
  1347. break;
  1348. }
  1349. wc = 0xfffdU;
  1350. ++nrcount;
  1351. } while (1);
  1352. inco = r;
  1353. } else if (((__uwchar_t)(wc)) < 0x80) {
  1354. CHAR_GOOD:
  1355. **outbuf = wc;
  1356. } else {
  1357. if ((px->tocodeset != 0x01) && (wc <= Cwc2c_DOMAIN_MAX)) {
  1358. const __codeset_8_bit_t *c8b
  1359. = __locale_mmap->codeset_8_bit + px->tocodeset - 3;
  1360. __uwchar_t u;
  1361. u = c8b->idx8wc2c[wc >> (Cwc2c_TI_SHIFT + Cwc2c_TT_SHIFT)];
  1362. u = __UCLIBC_CURLOCALE->tbl8wc2c[(u << Cwc2c_TI_SHIFT)
  1363. + ((wc >> Cwc2c_TT_SHIFT)
  1364. & ((1 << Cwc2c_TI_SHIFT)-1))];
  1365. wc = __UCLIBC_CURLOCALE->tbl8wc2c[Cwc2c_TI_LEN
  1366. + (u << Cwc2c_TT_SHIFT)
  1367. + (wc & ((1 << Cwc2c_TT_SHIFT)-1))];
  1368. if (wc) {
  1369. goto CHAR_GOOD;
  1370. }
  1371. }
  1372. **outbuf = '?';
  1373. ++nrcount;
  1374. }
  1375. *outbuf += inco;
  1376. *outbytesleft -= inco;
  1377. BOM_SKIP_OUTPUT:
  1378. *inbuf += inci;
  1379. *inbytesleft -= inci;
  1380. }
  1381. return nrcount;
  1382. }
  1383. #endif