wchar.c 40 KB

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