]> arthur.barton.de Git - netatalk.git/blob - libatalk/unicode/charsets/mac_korean.c
c0755cb9b4c9ff34dbc2ce534f4d5a1f99b7905f
[netatalk.git] / libatalk / unicode / charsets / mac_korean.c
1 /*
2  * MacKorean
3  * Copyright (C) TSUBAKIMOTO Hiroya <zorac@4000do.co.jp> 2004
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * Reference
20  * http://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif /* HAVE_CONFIG_H */
26
27 #if HAVE_USABLE_ICONV
28
29 #include "generic_cjk.h"
30 #include "mac_korean.h"
31
32 static size_t mac_korean_pull(void *,char **, size_t *, char **, size_t *);
33 static size_t mac_korean_push(void *,char **, size_t *, char **, size_t *);
34
35 struct charset_functions charset_mac_korean = {
36   "MAC_KOREAN",
37   3,
38   mac_korean_pull,
39   mac_korean_push,
40   CHARSET_ICONV | CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED | CHARSET_CLIENT,
41   "EUC-KR",
42   NULL, NULL
43 };
44
45 static size_t mac_korean_char_push(u_int8_t* out, const ucs2_t* in, size_t* size)
46 {
47   ucs2_t wc = in[0];
48
49   if ((wc & ~7) == 0xf860) {
50     wc = cjk_compose_seq(in, size, mac_korean_compose,
51                          sizeof(mac_korean_compose) / sizeof(u_int32_t));
52     if (!wc) return (size_t)-1;
53   } else if ((wc & 0xf000) == 0xe000) {
54     *size = 1;
55     return 0;
56   } else if (*size >= 2) {
57     ucs2_t comb = in[1];
58     size_t n = 1;
59
60     while ((comb & ~15) == 0xf870 ||
61            (comb >= 0x0300 && comb <= 0x036f) ||
62            (comb >= 0x20d0 && comb <= 0x20ea)) {
63       ucs2_t comp = cjk_compose(wc, comb, mac_korean_compose,
64                                 sizeof(mac_korean_compose) / sizeof(u_int32_t));
65       if (!comp) break;
66       wc = comp;
67       if (++n == *size) break;
68       comb = in[n];
69     }
70     *size = n;
71   } else {
72     *size = 1;
73   }
74   if (wc <= 0x7f) {
75     out[0] = (u_int8_t)wc;
76     return 1;
77   }
78   return cjk_char_push(cjk_lookup(wc, mac_korean_uni2_index,
79                                   mac_korean_uni2_charset), out);
80 }
81
82 static size_t mac_korean_push(void *cd, char **inbuf, size_t *inbytesleft,
83                                     char **outbuf, size_t *outbytesleft)
84 {
85   return cjk_generic_push(mac_korean_char_push,
86                           cd, inbuf, inbytesleft, outbuf, outbytesleft);
87 }
88
89 static size_t mac_korean_char_pull(ucs2_t* out, const u_int8_t* in, size_t* size)
90 {
91   u_int16_t c = in[0];
92
93   if (c <= 0x7f) {
94     *size = 1;
95     *out = c;
96     return 1;
97   } else if (c >= 0xa1 && c <= 0xfe) {
98     if (*size >= 2) {
99       u_int8_t c2 = in[1];
100       if ((c2 >= 0x41 && c2 <= 0x7d) || (c2 >= 0x81 && c2 <= 0xfe)) {
101         *size = 2;
102         c = (c << 8) + c2;
103       } else {
104         errno = EILSEQ;
105         return (size_t)-1;
106       }
107     } else {
108       errno = EINVAL;
109       return (size_t)-1;
110     }
111   } else {
112     *size = 1;
113   }
114   return cjk_char_pull(cjk_lookup(c, mac_korean_2uni_index,
115                                   mac_korean_2uni_charset),
116                        out, mac_korean_compose);
117 }
118
119 static size_t mac_korean_pull(void *cd, char **inbuf, size_t *inbytesleft,
120                                     char **outbuf, size_t *outbytesleft)
121 {
122   return cjk_generic_pull(mac_korean_char_pull,
123                           cd, inbuf, inbytesleft, outbuf, outbytesleft);
124 }
125 #endif