wchar.c 42 KB

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