wchar.c 44 KB

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