wchar.c 44 KB

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