wchar.c 44 KB

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