iconv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * Copyright © 2018 Waldemar Brodkorb <wbx@uclibc-ng.org>
  3. * Simplified port of iconv.c from musl C library including
  4. * parts of libiconv-tiny.
  5. */
  6. /* Copyright © 2005-2018 Rich Felker, et al.
  7. Permission is hereby granted, free of charge, to any person obtaining
  8. a copy of this software and associated documentation files (the
  9. "Software"), to deal in the Software without restriction, including
  10. without limitation the rights to use, copy, modify, merge, publish,
  11. distribute, sublicense, and/or sell copies of the Software, and to
  12. permit persons to whom the Software is furnished to do so, subject to
  13. the following conditions:
  14. The above copyright notice and this permission notice shall be
  15. included in all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <iconv.h>
  25. #include <errno.h>
  26. #include <wchar.h>
  27. #include <string.h>
  28. #include <strings.h>
  29. #include <stdlib.h>
  30. #include <limits.h>
  31. #include <dirent.h>
  32. #include <fcntl.h>
  33. #include <sys/mman.h>
  34. #include <sys/stat.h>
  35. #include <unistd.h>
  36. #include <stdint.h>
  37. #define UTF_32BE 0300
  38. #define UTF_16LE 0301
  39. #define UTF_16BE 0302
  40. #define UTF_32LE 0303
  41. #define UCS2BE 0304
  42. #define UCS2LE 0305
  43. #define WCHAR_T 0306
  44. #define US_ASCII 0307
  45. #define UTF_8 0310
  46. #define UTF_16 0312
  47. #define UTF_32 0313
  48. #define UCS2 0314
  49. #define EUC_JP 0320
  50. #define SHIFT_JIS 0321
  51. #define ISO2022_JP 0322
  52. #define GB18030 0330
  53. #define GBK 0331
  54. #define GB2312 0332
  55. #define BIG5 0340
  56. #define EUC_KR 0350
  57. /* Definitions of charmaps. Each charmap consists of:
  58. * 1. Empty-string-terminated list of null-terminated aliases.
  59. * 2. Special type code or number of elided quads of entries.
  60. * 3. Character table (size determined by field 2), consisting
  61. * of 5 bytes for every 4 characters, interpreted as 10-bit
  62. * indices into the legacy_chars table. */
  63. static const unsigned char charmaps[] =
  64. "utf8\0char\0\0\310"
  65. "wchart\0\0\306"
  66. "ucs2be\0\0\304"
  67. "ucs2le\0\0\305"
  68. "utf16be\0\0\302"
  69. "utf16le\0\0\301"
  70. "ucs4be\0utf32be\0\0\300"
  71. "ucs4le\0utf32le\0\0\303"
  72. "ascii\0usascii\0iso646\0iso646us\0\0\307"
  73. "utf16\0\0\312"
  74. "ucs4\0utf32\0\0\313"
  75. "ucs2\0\0\314"
  76. "eucjp\0\0\320"
  77. "shiftjis\0sjis\0\0\321"
  78. "iso2022jp\0\0\322"
  79. "gb18030\0\0\330"
  80. "gbk\0\0\331"
  81. "gb2312\0\0\332"
  82. "big5\0bigfive\0cp950\0big5hkscs\0\0\340"
  83. "euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
  84. #include "codepages.h"
  85. ;
  86. /* Table of characters that appear in legacy 8-bit codepages,
  87. * limited to 1024 slots (10 bit indices). The first 256 entries
  88. * are elided since those characters are obviously all included. */
  89. static const unsigned short legacy_chars[] = {
  90. #include "legacychars.h"
  91. };
  92. static const unsigned short jis0208[84][94] = {
  93. #include "jis0208.h"
  94. };
  95. static const unsigned short rev_jis[] = {
  96. #include "revjis.h"
  97. };
  98. static int fuzzycmp(const unsigned char *a, const unsigned char *b)
  99. {
  100. for (; *a && *b; a++, b++) {
  101. while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
  102. if ((*a|32U) != *b) return 1;
  103. }
  104. return *a != *b;
  105. }
  106. static size_t find_charmap(const void *name)
  107. {
  108. const unsigned char *s;
  109. if (!*(char *)name) name=charmaps; /* "utf8" */
  110. for (s=charmaps; *s; ) {
  111. if (!fuzzycmp(name, s)) {
  112. for (; *s; s+=strlen((void *)s)+1);
  113. return s+1-charmaps;
  114. }
  115. s += strlen((void *)s)+1;
  116. if (!*s) {
  117. if (s[1] > 0200) s+=2;
  118. else s+=2+(64U-s[1])*5;
  119. }
  120. }
  121. return -1;
  122. }
  123. struct stateful_cd {
  124. iconv_t base_cd;
  125. unsigned state;
  126. };
  127. static iconv_t combine_to_from(size_t t, size_t f)
  128. {
  129. return (void *)(f<<16 | t<<1 | 1);
  130. }
  131. static size_t extract_from(iconv_t cd)
  132. {
  133. return (size_t)cd >> 16;
  134. }
  135. static size_t extract_to(iconv_t cd)
  136. {
  137. return (size_t)cd >> 1 & 0x7fff;
  138. }
  139. iconv_t iconv_open(const char *to, const char *from)
  140. {
  141. size_t f, t;
  142. struct stateful_cd *scd;
  143. if ((t = find_charmap(to))==-1
  144. || (f = find_charmap(from))==-1
  145. || (charmaps[t] >= 0330)) {
  146. errno = EINVAL;
  147. return (iconv_t)-1;
  148. }
  149. iconv_t cd = combine_to_from(t, f);
  150. switch (charmaps[f]) {
  151. case UTF_16:
  152. case UTF_32:
  153. case UCS2:
  154. case ISO2022_JP:
  155. scd = malloc(sizeof *scd);
  156. if (!scd) return (iconv_t)-1;
  157. scd->base_cd = cd;
  158. scd->state = 0;
  159. cd = (iconv_t)scd;
  160. }
  161. return cd;
  162. }
  163. static unsigned get_16(const unsigned char *s, int e)
  164. {
  165. e &= 1;
  166. return s[e]<<8 | s[1-e];
  167. }
  168. static void put_16(unsigned char *s, unsigned c, int e)
  169. {
  170. e &= 1;
  171. s[e] = c>>8;
  172. s[1-e] = c;
  173. }
  174. static unsigned get_32(const unsigned char *s, int e)
  175. {
  176. e &= 3;
  177. return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
  178. }
  179. static void put_32(unsigned char *s, unsigned c, int e)
  180. {
  181. e &= 3;
  182. s[e^0] = c>>24;
  183. s[e^1] = c>>16;
  184. s[e^2] = c>>8;
  185. s[e^3] = c;
  186. }
  187. static inline int utf8enc_wchar(char *outb, wchar_t c)
  188. {
  189. if (c <= 0x7F) {
  190. *outb = c;
  191. return 1;
  192. }
  193. else if (c <= 0x7FF) {
  194. *outb++ = ((c >> 6) & 0x1F) | 0xC0;
  195. *outb++ = ( c & 0x3F) | 0x80;
  196. return 2;
  197. }
  198. else if (c <= 0xFFFF) {
  199. *outb++ = ((c >> 12) & 0x0F) | 0xE0;
  200. *outb++ = ((c >> 6) & 0x3F) | 0x80;
  201. *outb++ = ( c & 0x3F) | 0x80;
  202. return 3;
  203. }
  204. else if (c <= 0x10FFFF) {
  205. *outb++ = ((c >> 18) & 0x07) | 0xF0;
  206. *outb++ = ((c >> 12) & 0x3F) | 0x80;
  207. *outb++ = ((c >> 6) & 0x3F) | 0x80;
  208. *outb++ = ( c & 0x3F) | 0x80;
  209. return 4;
  210. }
  211. else {
  212. *outb++ = '?';
  213. return 1;
  214. }
  215. }
  216. static inline int utf8seq_is_overlong(char *s, int n)
  217. {
  218. switch (n)
  219. {
  220. case 2:
  221. /* 1100000x (10xxxxxx) */
  222. return (((*s >> 1) == 0x60) &&
  223. ((*(s+1) >> 6) == 0x02));
  224. case 3:
  225. /* 11100000 100xxxxx (10xxxxxx) */
  226. return ((*s == 0xE0) &&
  227. ((*(s+1) >> 5) == 0x04) &&
  228. ((*(s+2) >> 6) == 0x02));
  229. case 4:
  230. /* 11110000 1000xxxx (10xxxxxx 10xxxxxx) */
  231. return ((*s == 0xF0) &&
  232. ((*(s+1) >> 4) == 0x08) &&
  233. ((*(s+2) >> 6) == 0x02) &&
  234. ((*(s+3) >> 6) == 0x02));
  235. }
  236. return 0;
  237. }
  238. static inline int utf8seq_is_surrogate(char *s, int n)
  239. {
  240. return ((n == 3) && (*s == 0xED) && (*(s+1) >= 0xA0) && (*(s+1) <= 0xBF));
  241. }
  242. static inline int utf8seq_is_illegal(char *s, int n)
  243. {
  244. return ((n == 3) && (*s == 0xEF) && (*(s+1) == 0xBF) &&
  245. (*(s+2) >= 0xBE) && (*(s+2) <= 0xBF));
  246. }
  247. static inline int utf8dec_wchar(wchar_t *c, unsigned char *in, size_t inb)
  248. {
  249. int i;
  250. int n = -1;
  251. /* trivial char */
  252. if (*in <= 0x7F) {
  253. *c = *in;
  254. return 1;
  255. }
  256. /* find utf8 sequence length */
  257. if ((*in & 0xE0) == 0xC0) n = 2;
  258. else if ((*in & 0xF0) == 0xE0) n = 3;
  259. else if ((*in & 0xF8) == 0xF0) n = 4;
  260. else if ((*in & 0xFC) == 0xF8) n = 5;
  261. else if ((*in & 0xFE) == 0xFC) n = 6;
  262. /* starved? */
  263. if (n > inb)
  264. return -2;
  265. /* decode ... */
  266. if (n > 1 && n < 5) {
  267. /* reject invalid sequences */
  268. if (utf8seq_is_overlong(in, n) ||
  269. utf8seq_is_surrogate(in, n) ||
  270. utf8seq_is_illegal(in, n))
  271. return -1;
  272. /* decode ... */
  273. *c = (char)(*in++ & (0x7F >> n));
  274. for (i = 1; i < n; i++) {
  275. /* illegal continuation byte */
  276. if (*in < 0x80 || *in > 0xBF)
  277. return -1;
  278. *c = (*c << 6) | (*in++ & 0x3F);
  279. }
  280. return n;
  281. }
  282. /* unmapped sequence (> 4) */
  283. return -1;
  284. }
  285. static unsigned legacy_map(const unsigned char *map, unsigned c)
  286. {
  287. if (c < 4*map[-1]) return c;
  288. unsigned x = c - 4*map[-1];
  289. x = map[x*5/4]>>2*x%8 | map[x*5/4+1]<<8-2*x%8 & 1023;
  290. return x < 256 ? x : legacy_chars[x-256];
  291. }
  292. static unsigned uni_to_jis(unsigned c)
  293. {
  294. unsigned nel = sizeof rev_jis / sizeof *rev_jis;
  295. unsigned d, j, i, b = 0;
  296. for (;;) {
  297. i = nel/2;
  298. j = rev_jis[b+i];
  299. d = jis0208[j/256][j%256];
  300. if (d==c) return j + 0x2121;
  301. else if (nel == 1) return 0;
  302. else if (c < d)
  303. nel /= 2;
  304. else {
  305. b += i;
  306. nel -= nel/2;
  307. }
  308. }
  309. }
  310. size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
  311. {
  312. size_t x=0;
  313. struct stateful_cd *scd=0;
  314. if (!((size_t)cd & 1)) {
  315. scd = (void *)cd;
  316. cd = scd->base_cd;
  317. }
  318. unsigned to = extract_to(cd);
  319. unsigned from = extract_from(cd);
  320. const unsigned char *map = charmaps+from+1;
  321. const unsigned char *tomap = charmaps+to+1;
  322. char tmp[MB_LEN_MAX];
  323. unsigned c, d;
  324. size_t k, l;
  325. int err;
  326. unsigned char type = map[-1];
  327. unsigned char totype = tomap[-1];
  328. if (!in || !*in || !*inb) return 0;
  329. for (; *inb; *in+=l, *inb-=l) {
  330. c = *(unsigned char *)*in;
  331. l = 1;
  332. switch (type) {
  333. case UTF_8:
  334. if (c < 128) break;
  335. l = utf8dec_wchar(&c, *in, *inb);
  336. if (!l) l++;
  337. else if (l == (size_t)-1) goto ilseq;
  338. else if (l == (size_t)-2) goto starved;
  339. break;
  340. case US_ASCII:
  341. if (c >= 128) goto ilseq;
  342. break;
  343. case WCHAR_T:
  344. l = sizeof(wchar_t);
  345. if (*inb < l) goto starved;
  346. c = *(wchar_t *)*in;
  347. if (0) {
  348. case UTF_32BE:
  349. case UTF_32LE:
  350. l = 4;
  351. if (*inb < 4) goto starved;
  352. c = get_32((void *)*in, type);
  353. }
  354. if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
  355. break;
  356. case UCS2BE:
  357. case UCS2LE:
  358. case UTF_16BE:
  359. case UTF_16LE:
  360. l = 2;
  361. if (*inb < 2) goto starved;
  362. c = get_16((void *)*in, type);
  363. if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
  364. if ((unsigned)(c-0xd800) < 0x400) {
  365. if (type-UCS2BE < 2U) goto ilseq;
  366. l = 4;
  367. if (*inb < 4) goto starved;
  368. d = get_16((void *)(*in + 2), type);
  369. if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
  370. c = ((c-0xd7c0)<<10) + (d-0xdc00);
  371. }
  372. break;
  373. case UCS2:
  374. case UTF_16:
  375. l = 0;
  376. if (!scd->state) {
  377. if (*inb < 2) goto starved;
  378. c = get_16((void *)*in, 0);
  379. scd->state = type==UCS2
  380. ? c==0xfffe ? UCS2LE : UCS2BE
  381. : c==0xfffe ? UTF_16LE : UTF_16BE;
  382. if (c == 0xfffe || c == 0xfeff)
  383. l = 2;
  384. }
  385. type = scd->state;
  386. continue;
  387. case UTF_32:
  388. l = 0;
  389. if (!scd->state) {
  390. if (*inb < 4) goto starved;
  391. c = get_32((void *)*in, 0);
  392. scd->state = c==0xfffe0000 ? UTF_32LE : UTF_32BE;
  393. if (c == 0xfffe0000 || c == 0xfeff)
  394. l = 4;
  395. }
  396. type = scd->state;
  397. continue;
  398. case SHIFT_JIS:
  399. if (c < 128) break;
  400. if (c-0xa1 <= 0xdf-0xa1) {
  401. c += 0xff61-0xa1;
  402. break;
  403. }
  404. l = 2;
  405. if (*inb < 2) goto starved;
  406. d = *((unsigned char *)*in + 1);
  407. if (c-129 <= 159-129) c -= 129;
  408. else if (c-224 <= 239-224) c -= 193;
  409. else goto ilseq;
  410. c *= 2;
  411. if (d-64 <= 158-64) {
  412. if (d==127) goto ilseq;
  413. if (d>127) d--;
  414. d -= 64;
  415. } else if (d-159 <= 252-159) {
  416. c++;
  417. d -= 159;
  418. }
  419. c = jis0208[c][d];
  420. if (!c) goto ilseq;
  421. break;
  422. case EUC_JP:
  423. if (c < 128) break;
  424. l = 2;
  425. if (*inb < 2) goto starved;
  426. d = *((unsigned char *)*in + 1);
  427. if (c==0x8e) {
  428. c = d;
  429. if (c-0xa1 > 0xdf-0xa1) goto ilseq;
  430. c += 0xff61 - 0xa1;
  431. break;
  432. }
  433. c -= 0xa1;
  434. d -= 0xa1;
  435. if (c >= 84 || d >= 94) goto ilseq;
  436. c = jis0208[c][d];
  437. if (!c) goto ilseq;
  438. break;
  439. case ISO2022_JP:
  440. if (c >= 128) goto ilseq;
  441. if (c == '\033') {
  442. l = 3;
  443. if (*inb < 3) goto starved;
  444. c = *((unsigned char *)*in + 1);
  445. d = *((unsigned char *)*in + 2);
  446. if (c != '(' && c != '$') goto ilseq;
  447. switch (128*(c=='$') + d) {
  448. case 'B': scd->state=0; continue;
  449. case 'J': scd->state=1; continue;
  450. case 'I': scd->state=4; continue;
  451. case 128+'@': scd->state=2; continue;
  452. case 128+'B': scd->state=3; continue;
  453. }
  454. goto ilseq;
  455. }
  456. switch (scd->state) {
  457. case 1:
  458. if (c=='\\') c = 0xa5;
  459. if (c=='~') c = 0x203e;
  460. break;
  461. case 2:
  462. case 3:
  463. l = 2;
  464. if (*inb < 2) goto starved;
  465. d = *((unsigned char *)*in + 1);
  466. c -= 0x21;
  467. d -= 0x21;
  468. if (c >= 84 || d >= 94) goto ilseq;
  469. c = jis0208[c][d];
  470. if (!c) goto ilseq;
  471. break;
  472. case 4:
  473. if (c-0x60 < 0x1f) goto ilseq;
  474. if (c-0x21 < 0x5e) c += 0xff61-0x21;
  475. break;
  476. }
  477. break;
  478. default:
  479. if (!c) break;
  480. c = legacy_map(map, c);
  481. if (!c) goto ilseq;
  482. }
  483. switch (totype) {
  484. case WCHAR_T:
  485. if (*outb < sizeof(wchar_t)) goto toobig;
  486. *(wchar_t *)*out = c;
  487. *out += sizeof(wchar_t);
  488. *outb -= sizeof(wchar_t);
  489. break;
  490. case UTF_8:
  491. if (*outb < 4) {
  492. k = utf8enc_wchar(tmp, c);
  493. if (*outb < k) goto toobig;
  494. memcpy(*out, tmp, k);
  495. } else k = utf8enc_wchar(*out, c);
  496. *out += k;
  497. *outb -= k;
  498. break;
  499. case US_ASCII:
  500. if (c > 0x7f) subst: x++, c='*';
  501. default:
  502. if (*outb < 1) goto toobig;
  503. if (c<256 && c==legacy_map(tomap, c)) {
  504. revout:
  505. *(*out)++ = c;
  506. *outb -= 1;
  507. break;
  508. }
  509. d = c;
  510. for (c=4*totype; c<256; c++) {
  511. if (d == legacy_map(tomap, c)) {
  512. goto revout;
  513. }
  514. }
  515. goto subst;
  516. case SHIFT_JIS:
  517. if (c < 128) goto revout;
  518. if (c == 0xa5) {
  519. x++;
  520. c = '\\';
  521. goto revout;
  522. }
  523. if (c == 0x203e) {
  524. x++;
  525. c = '~';
  526. goto revout;
  527. }
  528. if (c-0xff61 <= 0xdf-0xa1) {
  529. c += 0xa1 - 0xff61;
  530. goto revout;
  531. }
  532. c = uni_to_jis(c);
  533. if (!c) goto subst;
  534. if (*outb < 2) goto toobig;
  535. d = c%256;
  536. c = c/256;
  537. *(*out)++ = (c+1)/2 + (c<95 ? 112 : 176);
  538. *(*out)++ = c%2 ? d + 31 + d/96 : d + 126;
  539. *outb -= 2;
  540. break;
  541. case EUC_JP:
  542. if (c < 128) goto revout;
  543. if (c-0xff61 <= 0xdf-0xa1) {
  544. c += 0x0e00 + 0x21 - 0xff61;
  545. } else {
  546. c = uni_to_jis(c);
  547. }
  548. if (!c) goto subst;
  549. if (*outb < 2) goto toobig;
  550. *(*out)++ = c/256 + 0x80;
  551. *(*out)++ = c%256 + 0x80;
  552. *outb -= 2;
  553. break;
  554. case ISO2022_JP:
  555. if (c < 128) goto revout;
  556. if (c-0xff61 <= 0xdf-0xa1 || c==0xa5 || c==0x203e) {
  557. if (*outb < 7) goto toobig;
  558. *(*out)++ = '\033';
  559. *(*out)++ = '(';
  560. if (c==0xa5) {
  561. *(*out)++ = 'J';
  562. *(*out)++ = '\\';
  563. } else if (c==0x203e) {
  564. *(*out)++ = 'J';
  565. *(*out)++ = '~';
  566. } else {
  567. *(*out)++ = 'I';
  568. *(*out)++ = c-0xff61+0x21;
  569. }
  570. *(*out)++ = '\033';
  571. *(*out)++ = '(';
  572. *(*out)++ = 'B';
  573. *outb -= 7;
  574. break;
  575. }
  576. c = uni_to_jis(c);
  577. if (!c) goto subst;
  578. if (*outb < 8) goto toobig;
  579. *(*out)++ = '\033';
  580. *(*out)++ = '$';
  581. *(*out)++ = 'B';
  582. *(*out)++ = c/256;
  583. *(*out)++ = c%256;
  584. *(*out)++ = '\033';
  585. *(*out)++ = '(';
  586. *(*out)++ = 'B';
  587. *outb -= 8;
  588. break;
  589. case UCS2:
  590. totype = UCS2BE;
  591. case UCS2BE:
  592. case UCS2LE:
  593. case UTF_16:
  594. case UTF_16BE:
  595. case UTF_16LE:
  596. if (c < 0x10000 || totype-UCS2BE < 2U) {
  597. if (c >= 0x10000) c = 0xFFFD;
  598. if (*outb < 2) goto toobig;
  599. put_16((void *)*out, c, totype);
  600. *out += 2;
  601. *outb -= 2;
  602. break;
  603. }
  604. if (*outb < 4) goto toobig;
  605. c -= 0x10000;
  606. put_16((void *)*out, (c>>10)|0xd800, totype);
  607. put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
  608. *out += 4;
  609. *outb -= 4;
  610. break;
  611. case UTF_32BE:
  612. case UTF_32LE:
  613. if (*outb < 4) goto toobig;
  614. put_32((void *)*out, c, totype);
  615. *out += 4;
  616. *outb -= 4;
  617. break;
  618. }
  619. }
  620. return x;
  621. ilseq:
  622. err = EILSEQ;
  623. x = -1;
  624. goto end;
  625. toobig:
  626. err = E2BIG;
  627. x = -1;
  628. goto end;
  629. starved:
  630. err = EINVAL;
  631. x = -1;
  632. end:
  633. errno = err;
  634. return x;
  635. }
  636. int iconv_close(iconv_t cd)
  637. {
  638. return 0;
  639. }