wchar.c 40 KB

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