wchar.c 45 KB

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