wchar.c 41 KB

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