]> arthur.barton.de Git - netatalk.git/blob - libatalk/unicode/charsets/mac_japanese.c
- merge branch-netatalk-afp-3x-dev, HEAD was tagged before
[netatalk.git] / libatalk / unicode / charsets / mac_japanese.c
1 /*
2  * MacJapanese
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_japanese.h"
31
32 static size_t mac_japanese_pull(void *,char **, size_t *, char **, size_t *);
33 static size_t mac_japanese_push(void *,char **, size_t *, char **, size_t *);
34
35 struct charset_functions charset_mac_japanese = {
36   "MAC_JAPANESE",
37   1,
38   mac_japanese_pull,
39   mac_japanese_push,
40   CHARSET_ICONV | CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED | CHARSET_CLIENT,
41   "SHIFT_JIS",
42   NULL, NULL
43 };
44
45 static size_t mac_japanese_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     *size = 1;
51     out[0] = (u_int8_t)(wc == 0x5c ? 0x80 : wc);
52     return 1;
53   } else if ((wc & 0xf000) == 0xe000) { /* user defined */
54     *size = 1;
55     if (wc > 0xe98b) return 0;
56     wc -= 0xe000;
57     out[0] = (u_int8_t)(wc / 188 + 0xf0);
58     out[1] = (u_int8_t)(wc % 188 + 0x40);
59     if (out[1] >= 0x7f) ++out[1];
60     return 2;
61   } else if ((wc & ~7) == 0xf860) {
62     wc = cjk_compose_seq(in, size, mac_japanese_compose,
63                          sizeof(mac_japanese_compose) / sizeof(u_int32_t));
64     if (!wc) return (size_t)-1;
65   } else if (*size >= 2 && ((in[1] & ~15) == 0xf870 || in[1] == 0x20dd)) {
66     ucs2_t comp = cjk_compose(wc, in[1], mac_japanese_compose,
67                               sizeof(mac_japanese_compose) / sizeof(u_int32_t));
68     if (comp) {
69       wc = comp;
70       *size = 2;
71     } else {
72       *size = 1;
73     }
74   } else {
75     *size = 1;
76   }
77   return cjk_char_push(cjk_lookup(wc, mac_japanese_uni2_index,
78                                   mac_japanese_uni2_charset), out);
79 }
80
81 static size_t mac_japanese_push(void *cd, char **inbuf, size_t *inbytesleft,
82                                 char **outbuf, size_t *outbytesleft)
83 {
84   return cjk_generic_push(mac_japanese_char_push,
85                           cd, inbuf, inbytesleft, outbuf, outbytesleft);
86 }
87
88 static size_t mac_japanese_char_pull(ucs2_t* out, const u_int8_t* in, size_t* size)
89 {
90   u_int16_t c = in[0];
91
92   if (c <= 0x7f) {
93     *size = 1;
94     *out = (c == 0x5c ? 0xa5 : c);
95     return 1;
96   } else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)) {
97     if (*size >= 2) {
98       u_int8_t c2 = in[1];
99
100       if ((c2 >= 0x40 && c2 <= 0x7e) || (c2 >= 0x80 && c2 <= 0xfc)) {
101         *size = 2;
102         if (c >= 0xf0) { /* user defined */
103           *out = 0xe000 + (c - 0xf0) * 188 + c2 - (c2 < 0x80 ? 0x40 : 0x41);
104           return 1;
105         }
106         c = (c << 8) + c2;
107       } else {
108         errno = EILSEQ;
109         return (size_t)-1;
110       }
111     } else {
112       errno = EINVAL;
113       return (size_t)-1;
114     }
115   } else {
116     *size = 1;
117   }
118   return cjk_char_pull(cjk_lookup(c, mac_japanese_2uni_index,
119                                   mac_japanese_2uni_charset),
120                        out, mac_japanese_compose);
121 }
122
123 static size_t mac_japanese_pull(void *cd, char **inbuf, size_t *inbytesleft,
124                                 char **outbuf, size_t *outbytesleft)
125 {
126   return cjk_generic_pull(mac_japanese_char_pull,
127                           cd, inbuf, inbytesleft, outbuf, outbytesleft);
128 }
129 #endif