1
0

iconv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. #include <iconv.h>
  2. #include <errno.h>
  3. #include <wchar.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <limits.h>
  7. #include <stdint.h>
  8. #include "locale_impl.h"
  9. #define UTF_32BE 0300
  10. #define UTF_16LE 0301
  11. #define UTF_16BE 0302
  12. #define UTF_32LE 0303
  13. #define UCS2BE 0304
  14. #define UCS2LE 0305
  15. #define WCHAR_T 0306
  16. #define US_ASCII 0307
  17. #define UTF_8 0310
  18. #define UTF_16 0312
  19. #define UTF_32 0313
  20. #define UCS2 0314
  21. #define EUC_JP 0320
  22. #define SHIFT_JIS 0321
  23. #define ISO2022_JP 0322
  24. #define GB18030 0330
  25. #define GBK 0331
  26. #define GB2312 0332
  27. #define BIG5 0340
  28. #define EUC_KR 0350
  29. /* Definitions of charmaps. Each charmap consists of:
  30. * 1. Empty-string-terminated list of null-terminated aliases.
  31. * 2. Special type code or number of elided quads of entries.
  32. * 3. Character table (size determined by field 2), consisting
  33. * of 5 bytes for every 4 characters, interpreted as 10-bit
  34. * indices into the legacy_chars table. */
  35. static const unsigned char charmaps[] =
  36. "utf8\0char\0\0\310"
  37. "wchart\0\0\306"
  38. "ucs2be\0\0\304"
  39. "ucs2le\0\0\305"
  40. "utf16be\0\0\302"
  41. "utf16le\0\0\301"
  42. "ucs4be\0utf32be\0\0\300"
  43. "ucs4le\0utf32le\0\0\303"
  44. "ascii\0usascii\0iso646\0iso646us\0\0\307"
  45. "utf16\0\0\312"
  46. "ucs4\0utf32\0\0\313"
  47. "ucs2\0\0\314"
  48. "eucjp\0\0\320"
  49. "shiftjis\0sjis\0cp932\0\0\321"
  50. "iso2022jp\0\0\322"
  51. "gb18030\0\0\330"
  52. "gbk\0cp936\0windows936\0\0\331"
  53. "gb2312\0\0\332"
  54. "big5\0bigfive\0cp950\0big5hkscs\0\0\340"
  55. "euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
  56. #include "codepages.h"
  57. ;
  58. /* Table of characters that appear in legacy 8-bit codepages,
  59. * limited to 1024 slots (10 bit indices). The first 256 entries
  60. * are elided since those characters are obviously all included. */
  61. static const unsigned short legacy_chars[] = {
  62. #include "legacychars.h"
  63. };
  64. static const unsigned short jis0208[84][94] = {
  65. #include "jis0208.h"
  66. };
  67. static const unsigned short gb18030[126][190] = {
  68. #include "gb18030.h"
  69. };
  70. static const unsigned short big5[89][157] = {
  71. #include "big5.h"
  72. };
  73. static const unsigned short hkscs[] = {
  74. #include "hkscs.h"
  75. };
  76. static const unsigned short ksc[93][94] = {
  77. #include "ksc.h"
  78. };
  79. static const unsigned short rev_jis[] = {
  80. #include "revjis.h"
  81. };
  82. static int fuzzycmp(const unsigned char *a, const unsigned char *b)
  83. {
  84. for (; *a && *b; a++, b++) {
  85. while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
  86. if ((*a|32U) != *b) return 1;
  87. }
  88. return *a != *b;
  89. }
  90. static size_t find_charmap(const void *name)
  91. {
  92. const unsigned char *s;
  93. if (!*(char *)name) name=charmaps; /* "utf8" */
  94. for (s=charmaps; *s; ) {
  95. if (!fuzzycmp(name, s)) {
  96. for (; *s; s+=strlen((void *)s)+1);
  97. return s+1-charmaps;
  98. }
  99. s += strlen((void *)s)+1;
  100. if (!*s) {
  101. if (s[1] > 0200) s+=2;
  102. else s+=2+(64U-s[1])*5;
  103. }
  104. }
  105. return -1;
  106. }
  107. struct stateful_cd {
  108. iconv_t base_cd;
  109. unsigned state;
  110. };
  111. static iconv_t combine_to_from(size_t t, size_t f)
  112. {
  113. return (void *)(f<<16 | t<<1 | 1);
  114. }
  115. static size_t extract_from(iconv_t cd)
  116. {
  117. return (size_t)cd >> 16;
  118. }
  119. static size_t extract_to(iconv_t cd)
  120. {
  121. return (size_t)cd >> 1 & 0x7fff;
  122. }
  123. iconv_t iconv_open(const char *to, const char *from)
  124. {
  125. size_t f, t;
  126. struct stateful_cd *scd;
  127. if ((t = find_charmap(to))==-1
  128. || (f = find_charmap(from))==-1
  129. || (charmaps[t] >= 0330)) {
  130. errno = EINVAL;
  131. return (iconv_t)-1;
  132. }
  133. iconv_t cd = combine_to_from(t, f);
  134. switch (charmaps[f]) {
  135. case UTF_16:
  136. case UTF_32:
  137. case UCS2:
  138. case ISO2022_JP:
  139. scd = malloc(sizeof *scd);
  140. if (!scd) return (iconv_t)-1;
  141. scd->base_cd = cd;
  142. scd->state = 0;
  143. cd = (iconv_t)scd;
  144. }
  145. return cd;
  146. }
  147. static unsigned get_16(const unsigned char *s, int e)
  148. {
  149. e &= 1;
  150. return s[e]<<8 | s[1-e];
  151. }
  152. static void put_16(unsigned char *s, unsigned c, int e)
  153. {
  154. e &= 1;
  155. s[e] = c>>8;
  156. s[1-e] = c;
  157. }
  158. static unsigned get_32(const unsigned char *s, int e)
  159. {
  160. e &= 3;
  161. return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
  162. }
  163. static void put_32(unsigned char *s, unsigned c, int e)
  164. {
  165. e &= 3;
  166. s[e^0] = c>>24;
  167. s[e^1] = c>>16;
  168. s[e^2] = c>>8;
  169. s[e^3] = c;
  170. }
  171. /* Adapt as needed */
  172. #define mbrtowc_utf8 mbrtowc
  173. #define wctomb_utf8 wctomb
  174. static unsigned legacy_map(const unsigned char *map, unsigned c)
  175. {
  176. if (c < 4*map[-1]) return c;
  177. unsigned x = c - 4*map[-1];
  178. x = map[x*5/4]>>2*x%8 | map[x*5/4+1]<<8-2*x%8 & 1023;
  179. return x < 256 ? x : legacy_chars[x-256];
  180. }
  181. static unsigned uni_to_jis(unsigned c)
  182. {
  183. unsigned nel = sizeof rev_jis / sizeof *rev_jis;
  184. unsigned d, j, i, b = 0;
  185. for (;;) {
  186. i = nel/2;
  187. j = rev_jis[b+i];
  188. d = jis0208[j/256][j%256];
  189. if (d==c) return j + 0x2121;
  190. else if (nel == 1) return 0;
  191. else if (c < d)
  192. nel /= 2;
  193. else {
  194. b += i;
  195. nel -= nel/2;
  196. }
  197. }
  198. }
  199. size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
  200. {
  201. size_t x=0;
  202. struct stateful_cd *scd=0;
  203. if (!((size_t)cd & 1)) {
  204. scd = (void *)cd;
  205. cd = scd->base_cd;
  206. }
  207. unsigned to = extract_to(cd);
  208. unsigned from = extract_from(cd);
  209. const unsigned char *map = charmaps+from+1;
  210. const unsigned char *tomap = charmaps+to+1;
  211. mbstate_t st = {0};
  212. wchar_t wc;
  213. unsigned c, d;
  214. size_t k, l;
  215. int err;
  216. unsigned char type = map[-1];
  217. unsigned char totype = tomap[-1];
  218. locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  219. if (!in || !*in || !*inb) return 0;
  220. *ploc = UTF8_LOCALE;
  221. for (; *inb; *in+=l, *inb-=l) {
  222. c = *(unsigned char *)*in;
  223. l = 1;
  224. switch (type) {
  225. case UTF_8:
  226. if (c < 128) break;
  227. l = mbrtowc_utf8(&wc, *in, *inb, &st);
  228. if (l == (size_t)-1) goto ilseq;
  229. if (l == (size_t)-2) goto starved;
  230. c = wc;
  231. break;
  232. case US_ASCII:
  233. if (c >= 128) goto ilseq;
  234. break;
  235. case WCHAR_T:
  236. l = sizeof(wchar_t);
  237. if (*inb < l) goto starved;
  238. c = *(wchar_t *)*in;
  239. if (0) {
  240. case UTF_32BE:
  241. case UTF_32LE:
  242. l = 4;
  243. if (*inb < 4) goto starved;
  244. c = get_32((void *)*in, type);
  245. }
  246. if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
  247. break;
  248. case UCS2BE:
  249. case UCS2LE:
  250. case UTF_16BE:
  251. case UTF_16LE:
  252. l = 2;
  253. if (*inb < 2) goto starved;
  254. c = get_16((void *)*in, type);
  255. if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
  256. if ((unsigned)(c-0xd800) < 0x400) {
  257. if (type-UCS2BE < 2U) goto ilseq;
  258. l = 4;
  259. if (*inb < 4) goto starved;
  260. d = get_16((void *)(*in + 2), type);
  261. if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
  262. c = ((c-0xd7c0)<<10) + (d-0xdc00);
  263. }
  264. break;
  265. case UCS2:
  266. case UTF_16:
  267. l = 0;
  268. if (!scd->state) {
  269. if (*inb < 2) goto starved;
  270. c = get_16((void *)*in, 0);
  271. scd->state = type==UCS2
  272. ? c==0xfffe ? UCS2LE : UCS2BE
  273. : c==0xfffe ? UTF_16LE : UTF_16BE;
  274. if (c == 0xfffe || c == 0xfeff)
  275. l = 2;
  276. }
  277. type = scd->state;
  278. continue;
  279. case UTF_32:
  280. l = 0;
  281. if (!scd->state) {
  282. if (*inb < 4) goto starved;
  283. c = get_32((void *)*in, 0);
  284. scd->state = c==0xfffe0000 ? UTF_32LE : UTF_32BE;
  285. if (c == 0xfffe0000 || c == 0xfeff)
  286. l = 4;
  287. }
  288. type = scd->state;
  289. continue;
  290. case SHIFT_JIS:
  291. if (c < 128) break;
  292. if (c-0xa1 <= 0xdf-0xa1) {
  293. c += 0xff61-0xa1;
  294. break;
  295. }
  296. l = 2;
  297. if (*inb < 2) goto starved;
  298. d = *((unsigned char *)*in + 1);
  299. if (c-129 <= 159-129) c -= 129;
  300. else if (c-224 <= 239-224) c -= 193;
  301. else goto ilseq;
  302. c *= 2;
  303. if (d-64 <= 158-64) {
  304. if (d==127) goto ilseq;
  305. if (d>127) d--;
  306. d -= 64;
  307. } else if (d-159 <= 252-159) {
  308. c++;
  309. d -= 159;
  310. }
  311. c = jis0208[c][d];
  312. if (!c) goto ilseq;
  313. break;
  314. case EUC_JP:
  315. if (c < 128) break;
  316. l = 2;
  317. if (*inb < 2) goto starved;
  318. d = *((unsigned char *)*in + 1);
  319. if (c==0x8e) {
  320. c = d;
  321. if (c-0xa1 > 0xdf-0xa1) goto ilseq;
  322. c += 0xff61 - 0xa1;
  323. break;
  324. }
  325. c -= 0xa1;
  326. d -= 0xa1;
  327. if (c >= 84 || d >= 94) goto ilseq;
  328. c = jis0208[c][d];
  329. if (!c) goto ilseq;
  330. break;
  331. case ISO2022_JP:
  332. if (c >= 128) goto ilseq;
  333. if (c == '\033') {
  334. l = 3;
  335. if (*inb < 3) goto starved;
  336. c = *((unsigned char *)*in + 1);
  337. d = *((unsigned char *)*in + 2);
  338. if (c != '(' && c != '$') goto ilseq;
  339. switch (128*(c=='$') + d) {
  340. case 'B': scd->state=0; continue;
  341. case 'J': scd->state=1; continue;
  342. case 'I': scd->state=4; continue;
  343. case 128+'@': scd->state=2; continue;
  344. case 128+'B': scd->state=3; continue;
  345. }
  346. goto ilseq;
  347. }
  348. switch (scd->state) {
  349. case 1:
  350. if (c=='\\') c = 0xa5;
  351. if (c=='~') c = 0x203e;
  352. break;
  353. case 2:
  354. case 3:
  355. l = 2;
  356. if (*inb < 2) goto starved;
  357. d = *((unsigned char *)*in + 1);
  358. c -= 0x21;
  359. d -= 0x21;
  360. if (c >= 84 || d >= 94) goto ilseq;
  361. c = jis0208[c][d];
  362. if (!c) goto ilseq;
  363. break;
  364. case 4:
  365. if (c-0x60 < 0x1f) goto ilseq;
  366. if (c-0x21 < 0x5e) c += 0xff61-0x21;
  367. break;
  368. }
  369. break;
  370. case GB2312:
  371. if (c < 128) break;
  372. if (c < 0xa1) goto ilseq;
  373. case GBK:
  374. if (c == 128) {
  375. c = 0x20ac;
  376. break;
  377. }
  378. case GB18030:
  379. if (c < 128) break;
  380. c -= 0x81;
  381. if (c >= 126) goto ilseq;
  382. l = 2;
  383. if (*inb < 2) goto starved;
  384. d = *((unsigned char *)*in + 1);
  385. if (d < 0xa1 && type == GB2312) goto ilseq;
  386. if (d-0x40>=191 || d==127) {
  387. if (d-'0'>9 || type != GB18030)
  388. goto ilseq;
  389. l = 4;
  390. if (*inb < 4) goto starved;
  391. c = (10*c + d-'0') * 1260;
  392. d = *((unsigned char *)*in + 2);
  393. if (d-0x81>126) goto ilseq;
  394. c += 10*(d-0x81);
  395. d = *((unsigned char *)*in + 3);
  396. if (d-'0'>9) goto ilseq;
  397. c += d-'0';
  398. c += 128;
  399. for (d=0; d<=c; ) {
  400. k = 0;
  401. for (int i=0; i<126; i++)
  402. for (int j=0; j<190; j++)
  403. if (gb18030[i][j]-d <= c-d)
  404. k++;
  405. d = c+1;
  406. c += k;
  407. }
  408. break;
  409. }
  410. d -= 0x40;
  411. if (d>63) d--;
  412. c = gb18030[c][d];
  413. break;
  414. case BIG5:
  415. if (c < 128) break;
  416. l = 2;
  417. if (*inb < 2) goto starved;
  418. d = *((unsigned char *)*in + 1);
  419. if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
  420. d -= 0x40;
  421. if (d > 0x3e) d -= 0x22;
  422. if (c-0xa1>=0xfa-0xa1) {
  423. if (c-0x87>=0xff-0x87) goto ilseq;
  424. if (c < 0xa1) c -= 0x87;
  425. else c -= 0x87 + (0xfa-0xa1);
  426. c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17
  427. | hkscs[c*157+d];
  428. /* A few HKSCS characters map to pairs of UCS
  429. * characters. These are mapped to surrogate
  430. * range in the hkscs table then hard-coded
  431. * here. Ugly, yes. */
  432. if (c/256 == 0xdc) {
  433. union {
  434. char c[8];
  435. wchar_t wc[2];
  436. } tmp;
  437. char *ptmp = tmp.c;
  438. size_t tmpx = iconv(combine_to_from(to, find_charmap("utf8")),
  439. &(char *){"\303\212\314\204"
  440. "\303\212\314\214"
  441. "\303\252\314\204"
  442. "\303\252\314\214"
  443. +c%256}, &(size_t){4},
  444. &ptmp, &(size_t){sizeof tmp});
  445. size_t tmplen = ptmp - tmp.c;
  446. if (tmplen > *outb) goto toobig;
  447. if (tmpx) x++;
  448. memcpy(*out, &tmp, tmplen);
  449. *out += tmplen;
  450. *outb -= tmplen;
  451. continue;
  452. }
  453. if (!c) goto ilseq;
  454. break;
  455. }
  456. c -= 0xa1;
  457. c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17;
  458. if (!c) goto ilseq;
  459. break;
  460. case EUC_KR:
  461. if (c < 128) break;
  462. l = 2;
  463. if (*inb < 2) goto starved;
  464. d = *((unsigned char *)*in + 1);
  465. c -= 0xa1;
  466. d -= 0xa1;
  467. if (c >= 93 || d >= 94) {
  468. c += (0xa1-0x81);
  469. d += 0xa1;
  470. if (c >= 93 || c>=0xc6-0x81 && d>0x52)
  471. goto ilseq;
  472. if (d-'A'<26) d = d-'A';
  473. else if (d-'a'<26) d = d-'a'+26;
  474. else if (d-0x81<0xff-0x81) d = d-0x81+52;
  475. else goto ilseq;
  476. if (c < 0x20) c = 178*c + d;
  477. else c = 178*0x20 + 84*(c-0x20) + d;
  478. c += 0xac00;
  479. for (d=0xac00; d<=c; ) {
  480. k = 0;
  481. for (int i=0; i<93; i++)
  482. for (int j=0; j<94; j++)
  483. if (ksc[i][j]-d <= c-d)
  484. k++;
  485. d = c+1;
  486. c += k;
  487. }
  488. break;
  489. }
  490. c = ksc[c][d];
  491. if (!c) goto ilseq;
  492. break;
  493. default:
  494. if (!c) break;
  495. c = legacy_map(map, c);
  496. if (!c) goto ilseq;
  497. }
  498. switch (totype) {
  499. case WCHAR_T:
  500. if (*outb < sizeof(wchar_t)) goto toobig;
  501. *(wchar_t *)*out = c;
  502. *out += sizeof(wchar_t);
  503. *outb -= sizeof(wchar_t);
  504. break;
  505. case UTF_8:
  506. if (*outb < 4) {
  507. char tmp[4];
  508. k = wctomb_utf8(tmp, c);
  509. if (*outb < k) goto toobig;
  510. memcpy(*out, tmp, k);
  511. } else k = wctomb_utf8(*out, c);
  512. *out += k;
  513. *outb -= k;
  514. break;
  515. case US_ASCII:
  516. if (c > 0x7f) subst: x++, c='*';
  517. default:
  518. if (*outb < 1) goto toobig;
  519. if (c<256 && c==legacy_map(tomap, c)) {
  520. revout:
  521. if (*outb < 1) goto toobig;
  522. *(*out)++ = c;
  523. *outb -= 1;
  524. break;
  525. }
  526. d = c;
  527. for (c=4*totype; c<256; c++) {
  528. if (d == legacy_map(tomap, c)) {
  529. goto revout;
  530. }
  531. }
  532. goto subst;
  533. case SHIFT_JIS:
  534. if (c < 128) goto revout;
  535. if (c == 0xa5) {
  536. x++;
  537. c = '\\';
  538. goto revout;
  539. }
  540. if (c == 0x203e) {
  541. x++;
  542. c = '~';
  543. goto revout;
  544. }
  545. if (c-0xff61 <= 0xdf-0xa1) {
  546. c += 0xa1 - 0xff61;
  547. goto revout;
  548. }
  549. c = uni_to_jis(c);
  550. if (!c) goto subst;
  551. if (*outb < 2) goto toobig;
  552. d = c%256;
  553. c = c/256;
  554. *(*out)++ = (c+1)/2 + (c<95 ? 112 : 176);
  555. *(*out)++ = c%2 ? d + 31 + d/96 : d + 126;
  556. *outb -= 2;
  557. break;
  558. case EUC_JP:
  559. if (c < 128) goto revout;
  560. if (c-0xff61 <= 0xdf-0xa1) {
  561. c += 0x0e00 + 0x21 - 0xff61;
  562. } else {
  563. c = uni_to_jis(c);
  564. }
  565. if (!c) goto subst;
  566. if (*outb < 2) goto toobig;
  567. *(*out)++ = c/256 + 0x80;
  568. *(*out)++ = c%256 + 0x80;
  569. *outb -= 2;
  570. break;
  571. case ISO2022_JP:
  572. if (c < 128) goto revout;
  573. if (c-0xff61 <= 0xdf-0xa1 || c==0xa5 || c==0x203e) {
  574. if (*outb < 7) goto toobig;
  575. *(*out)++ = '\033';
  576. *(*out)++ = '(';
  577. if (c==0xa5) {
  578. *(*out)++ = 'J';
  579. *(*out)++ = '\\';
  580. } else if (c==0x203e) {
  581. *(*out)++ = 'J';
  582. *(*out)++ = '~';
  583. } else {
  584. *(*out)++ = 'I';
  585. *(*out)++ = c-0xff61+0x21;
  586. }
  587. *(*out)++ = '\033';
  588. *(*out)++ = '(';
  589. *(*out)++ = 'B';
  590. *outb -= 7;
  591. break;
  592. }
  593. c = uni_to_jis(c);
  594. if (!c) goto subst;
  595. if (*outb < 8) goto toobig;
  596. *(*out)++ = '\033';
  597. *(*out)++ = '$';
  598. *(*out)++ = 'B';
  599. *(*out)++ = c/256;
  600. *(*out)++ = c%256;
  601. *(*out)++ = '\033';
  602. *(*out)++ = '(';
  603. *(*out)++ = 'B';
  604. *outb -= 8;
  605. break;
  606. case UCS2:
  607. totype = UCS2BE;
  608. case UCS2BE:
  609. case UCS2LE:
  610. case UTF_16:
  611. case UTF_16BE:
  612. case UTF_16LE:
  613. if (c < 0x10000 || totype-UCS2BE < 2U) {
  614. if (c >= 0x10000) c = 0xFFFD;
  615. if (*outb < 2) goto toobig;
  616. put_16((void *)*out, c, totype);
  617. *out += 2;
  618. *outb -= 2;
  619. break;
  620. }
  621. if (*outb < 4) goto toobig;
  622. c -= 0x10000;
  623. put_16((void *)*out, (c>>10)|0xd800, totype);
  624. put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
  625. *out += 4;
  626. *outb -= 4;
  627. break;
  628. case UTF_32:
  629. totype = UTF_32BE;
  630. case UTF_32BE:
  631. case UTF_32LE:
  632. if (*outb < 4) goto toobig;
  633. put_32((void *)*out, c, totype);
  634. *out += 4;
  635. *outb -= 4;
  636. break;
  637. }
  638. }
  639. *ploc = loc;
  640. return x;
  641. ilseq:
  642. err = EILSEQ;
  643. x = -1;
  644. goto end;
  645. toobig:
  646. err = E2BIG;
  647. x = -1;
  648. goto end;
  649. starved:
  650. err = EINVAL;
  651. x = -1;
  652. end:
  653. errno = err;
  654. *ploc = loc;
  655. return x;
  656. }