wchar.c 44 KB

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