]> arthur.barton.de Git - netatalk.git/blob - libatalk/unicode/charsets/mac_chinese_trad.c
f134ce8b5a6fcac2500857d24f226b221c2a1665
[netatalk.git] / libatalk / unicode / charsets / mac_chinese_trad.c
1 /*
2  * MacChineseTrad
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_chinese_trad.h"
31
32 static size_t mac_chinese_trad_pull(void *,char **, size_t *, char **, size_t *);
33 static size_t mac_chinese_trad_push(void *,char **, size_t *, char **, size_t *);
34
35 struct charset_functions charset_mac_chinese_trad = {
36   "MAC_CHINESE_TRAD",
37   2,
38   mac_chinese_trad_pull,
39   mac_chinese_trad_push,
40   CHARSET_ICONV | CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED | CHARSET_CLIENT,
41   "BIG-5",
42   NULL, NULL
43 };
44
45 static size_t mac_chinese_trad_char_push(u_int8_t* out, const ucs2_t* in, size_t* size)
46 {
47   ucs2_t wc = in[0];
48
49   if (wc <= 0x7f) {
50     if (wc == 0x5c && *size >= 2 && in[1] == 0xf87f) {
51       *size = 2;
52       out[0] = 0x80;
53     } else {
54       *size = 1;
55       out[0] = (u_int8_t)wc;
56     }
57     return 1;
58   } else if ((wc & 0xf000) == 0xe000) {
59     *size = 1;
60     return 0;
61   } else if (*size >= 2 && (in[1] & ~15) == 0xf870) {
62     ucs2_t comp = cjk_compose(wc, in[1], mac_chinese_trad_compose,
63                               sizeof(mac_chinese_trad_compose) / sizeof(u_int32_t));
64     if (comp) {
65       wc = comp;
66       *size = 2;
67     } else {
68       *size = 1;
69     }
70   } else {
71     *size = 1;
72   }
73   return cjk_char_push(cjk_lookup(wc, mac_chinese_trad_uni2_index,
74                                   mac_chinese_trad_uni2_charset), out);
75 }
76
77 static size_t mac_chinese_trad_push(void *cd, char **inbuf, size_t *inbytesleft,
78                                     char **outbuf, size_t *outbytesleft)
79 {
80   return cjk_generic_push(mac_chinese_trad_char_push,
81                           cd, inbuf, inbytesleft, outbuf, outbytesleft);
82 }
83
84 static size_t mac_chinese_trad_char_pull(ucs2_t* out, const u_int8_t* in, size_t* size)
85 {
86   u_int16_t c = in[0];
87
88   if (c <= 0x7f) {
89     *size = 1;
90     *out = c;
91     return 1;
92   } else if (c >= 0xa1 && c <= 0xfc) {
93     if (*size >= 2) {
94       u_int8_t c2 = in[1];
95
96       if ((c2 >= 0x40 && c2 <= 0x7e) || (c2 >= 0xa1 && c2 <= 0xfe)) {
97         *size = 2;
98         c = (c << 8) + c2;
99       } else {
100         errno = EILSEQ;
101         return (size_t)-1;
102       }
103     } else {
104       errno = EINVAL;
105       return (size_t)-1;
106     }
107   } else {
108     *size = 1;
109   }
110   return cjk_char_pull(cjk_lookup(c, mac_chinese_trad_2uni_index,
111                                   mac_chinese_trad_2uni_charset),
112                        out, mac_chinese_trad_compose);
113 }
114
115 static size_t mac_chinese_trad_pull(void *cd, char **inbuf, size_t *inbytesleft,
116                                     char **outbuf, size_t *outbytesleft)
117 {
118   return cjk_generic_pull(mac_chinese_trad_char_pull,
119                           cd, inbuf, inbytesleft, outbuf, outbytesleft);
120 }
121 #endif