iconv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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 (iconv_t)(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. else {
  336. wchar_t wc;
  337. l = utf8dec_wchar(&wc, *in, *inb);
  338. c = wc;
  339. }
  340. if (!l) l++;
  341. else if (l == (size_t)-1) goto ilseq;
  342. else if (l == (size_t)-2) goto starved;
  343. break;
  344. case US_ASCII:
  345. if (c >= 128) goto ilseq;
  346. break;
  347. case WCHAR_T:
  348. l = sizeof(wchar_t);
  349. if (*inb < l) goto starved;
  350. c = *(wchar_t *)*in;
  351. if (0) {
  352. case UTF_32BE:
  353. case UTF_32LE:
  354. l = 4;
  355. if (*inb < 4) goto starved;
  356. c = get_32((void *)*in, type);
  357. }
  358. if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
  359. break;
  360. case UCS2BE:
  361. case UCS2LE:
  362. case UTF_16BE:
  363. case UTF_16LE:
  364. l = 2;
  365. if (*inb < 2) goto starved;
  366. c = get_16((void *)*in, type);
  367. if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
  368. if ((unsigned)(c-0xd800) < 0x400) {
  369. if (type-UCS2BE < 2U) goto ilseq;
  370. l = 4;
  371. if (*inb < 4) goto starved;
  372. d = get_16((void *)(*in + 2), type);
  373. if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
  374. c = ((c-0xd7c0)<<10) + (d-0xdc00);
  375. }
  376. break;
  377. case UCS2:
  378. case UTF_16:
  379. l = 0;
  380. if (!scd->state) {
  381. if (*inb < 2) goto starved;
  382. c = get_16((void *)*in, 0);
  383. scd->state = type==UCS2
  384. ? c==0xfffe ? UCS2LE : UCS2BE
  385. : c==0xfffe ? UTF_16LE : UTF_16BE;
  386. if (c == 0xfffe || c == 0xfeff)
  387. l = 2;
  388. }
  389. type = scd->state;
  390. continue;
  391. case UTF_32:
  392. l = 0;
  393. if (!scd->state) {
  394. if (*inb < 4) goto starved;
  395. c = get_32((void *)*in, 0);
  396. scd->state = c==0xfffe0000 ? UTF_32LE : UTF_32BE;
  397. if (c == 0xfffe0000 || c == 0xfeff)
  398. l = 4;
  399. }
  400. type = scd->state;
  401. continue;
  402. case SHIFT_JIS:
  403. if (c < 128) break;
  404. if (c-0xa1 <= 0xdf-0xa1) {
  405. c += 0xff61-0xa1;
  406. break;
  407. }
  408. l = 2;
  409. if (*inb < 2) goto starved;
  410. d = *((unsigned char *)*in + 1);
  411. if (c-129 <= 159-129) c -= 129;
  412. else if (c-224 <= 239-224) c -= 193;
  413. else goto ilseq;
  414. c *= 2;
  415. if (d-64 <= 158-64) {
  416. if (d==127) goto ilseq;
  417. if (d>127) d--;
  418. d -= 64;
  419. } else if (d-159 <= 252-159) {
  420. c++;
  421. d -= 159;
  422. }
  423. c = jis0208[c][d];
  424. if (!c) goto ilseq;
  425. break;
  426. case EUC_JP:
  427. if (c < 128) break;
  428. l = 2;
  429. if (*inb < 2) goto starved;
  430. d = *((unsigned char *)*in + 1);
  431. if (c==0x8e) {
  432. c = d;
  433. if (c-0xa1 > 0xdf-0xa1) goto ilseq;
  434. c += 0xff61 - 0xa1;
  435. break;
  436. }
  437. c -= 0xa1;
  438. d -= 0xa1;
  439. if (c >= 84 || d >= 94) goto ilseq;
  440. c = jis0208[c][d];
  441. if (!c) goto ilseq;
  442. break;
  443. case ISO2022_JP:
  444. if (c >= 128) goto ilseq;
  445. if (c == '\033') {
  446. l = 3;
  447. if (*inb < 3) goto starved;
  448. c = *((unsigned char *)*in + 1);
  449. d = *((unsigned char *)*in + 2);
  450. if (c != '(' && c != '$') goto ilseq;
  451. switch (128*(c=='$') + d) {
  452. case 'B': scd->state=0; continue;
  453. case 'J': scd->state=1; continue;
  454. case 'I': scd->state=4; continue;
  455. case 128+'@': scd->state=2; continue;
  456. case 128+'B': scd->state=3; continue;
  457. }
  458. goto ilseq;
  459. }
  460. switch (scd->state) {
  461. case 1:
  462. if (c=='\\') c = 0xa5;
  463. if (c=='~') c = 0x203e;
  464. break;
  465. case 2:
  466. case 3:
  467. l = 2;
  468. if (*inb < 2) goto starved;
  469. d = *((unsigned char *)*in + 1);
  470. c -= 0x21;
  471. d -= 0x21;
  472. if (c >= 84 || d >= 94) goto ilseq;
  473. c = jis0208[c][d];
  474. if (!c) goto ilseq;
  475. break;
  476. case 4:
  477. if (c-0x60 < 0x1f) goto ilseq;
  478. if (c-0x21 < 0x5e) c += 0xff61-0x21;
  479. break;
  480. }
  481. break;
  482. default:
  483. if (!c) break;
  484. c = legacy_map(map, c);
  485. if (!c) goto ilseq;
  486. }
  487. switch (totype) {
  488. case WCHAR_T:
  489. if (*outb < sizeof(wchar_t)) goto toobig;
  490. *(wchar_t *)*out = c;
  491. *out += sizeof(wchar_t);
  492. *outb -= sizeof(wchar_t);
  493. break;
  494. case UTF_8:
  495. if (*outb < 4) {
  496. k = utf8enc_wchar(tmp, c);
  497. if (*outb < k) goto toobig;
  498. memcpy(*out, tmp, k);
  499. } else k = utf8enc_wchar(*out, c);
  500. *out += k;
  501. *outb -= k;
  502. break;
  503. case US_ASCII:
  504. if (c > 0x7f) subst: x++, c='*';
  505. default:
  506. if (*outb < 1) goto toobig;
  507. if (c<256 && c==legacy_map(tomap, c)) {
  508. revout:
  509. *(*out)++ = c;
  510. *outb -= 1;
  511. break;
  512. }
  513. d = c;
  514. for (c=4*totype; c<256; c++) {
  515. if (d == legacy_map(tomap, c)) {
  516. goto revout;
  517. }
  518. }
  519. goto subst;
  520. case SHIFT_JIS:
  521. if (c < 128) goto revout;
  522. if (c == 0xa5) {
  523. x++;
  524. c = '\\';
  525. goto revout;
  526. }
  527. if (c == 0x203e) {
  528. x++;
  529. c = '~';
  530. goto revout;
  531. }
  532. if (c-0xff61 <= 0xdf-0xa1) {
  533. c += 0xa1 - 0xff61;
  534. goto revout;
  535. }
  536. c = uni_to_jis(c);
  537. if (!c) goto subst;
  538. if (*outb < 2) goto toobig;
  539. d = c%256;
  540. c = c/256;
  541. *(*out)++ = (c+1)/2 + (c<95 ? 112 : 176);
  542. *(*out)++ = c%2 ? d + 31 + d/96 : d + 126;
  543. *outb -= 2;
  544. break;
  545. case EUC_JP:
  546. if (c < 128) goto revout;
  547. if (c-0xff61 <= 0xdf-0xa1) {
  548. c += 0x0e00 + 0x21 - 0xff61;
  549. } else {
  550. c = uni_to_jis(c);
  551. }
  552. if (!c) goto subst;
  553. if (*outb < 2) goto toobig;
  554. *(*out)++ = c/256 + 0x80;
  555. *(*out)++ = c%256 + 0x80;
  556. *outb -= 2;
  557. break;
  558. case ISO2022_JP:
  559. if (c < 128) goto revout;
  560. if (c-0xff61 <= 0xdf-0xa1 || c==0xa5 || c==0x203e) {
  561. if (*outb < 7) goto toobig;
  562. *(*out)++ = '\033';
  563. *(*out)++ = '(';
  564. if (c==0xa5) {
  565. *(*out)++ = 'J';
  566. *(*out)++ = '\\';
  567. } else if (c==0x203e) {
  568. *(*out)++ = 'J';
  569. *(*out)++ = '~';
  570. } else {
  571. *(*out)++ = 'I';
  572. *(*out)++ = c-0xff61+0x21;
  573. }
  574. *(*out)++ = '\033';
  575. *(*out)++ = '(';
  576. *(*out)++ = 'B';
  577. *outb -= 7;
  578. break;
  579. }
  580. c = uni_to_jis(c);
  581. if (!c) goto subst;
  582. if (*outb < 8) goto toobig;
  583. *(*out)++ = '\033';
  584. *(*out)++ = '$';
  585. *(*out)++ = 'B';
  586. *(*out)++ = c/256;
  587. *(*out)++ = c%256;
  588. *(*out)++ = '\033';
  589. *(*out)++ = '(';
  590. *(*out)++ = 'B';
  591. *outb -= 8;
  592. break;
  593. case UCS2:
  594. totype = UCS2BE;
  595. case UCS2BE:
  596. case UCS2LE:
  597. case UTF_16:
  598. case UTF_16BE:
  599. case UTF_16LE:
  600. if (c < 0x10000 || totype-UCS2BE < 2U) {
  601. if (c >= 0x10000) c = 0xFFFD;
  602. if (*outb < 2) goto toobig;
  603. put_16((void *)*out, c, totype);
  604. *out += 2;
  605. *outb -= 2;
  606. break;
  607. }
  608. if (*outb < 4) goto toobig;
  609. c -= 0x10000;
  610. put_16((void *)*out, (c>>10)|0xd800, totype);
  611. put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
  612. *out += 4;
  613. *outb -= 4;
  614. break;
  615. case UTF_32BE:
  616. case UTF_32LE:
  617. if (*outb < 4) goto toobig;
  618. put_32((void *)*out, c, totype);
  619. *out += 4;
  620. *outb -= 4;
  621. break;
  622. }
  623. }
  624. return x;
  625. ilseq:
  626. err = EILSEQ;
  627. x = -1;
  628. goto end;
  629. toobig:
  630. err = E2BIG;
  631. x = -1;
  632. goto end;
  633. starved:
  634. err = EINVAL;
  635. x = -1;
  636. end:
  637. errno = err;
  638. return x;
  639. }
  640. int iconv_close(iconv_t cd)
  641. {
  642. return 0;
  643. }