wchar.c 44 KB

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