wchar.c 45 KB

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