wchar.c 44 KB

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