]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - libexfat/utf.c
Merge commit 'upstream/0.9.6'
[sven/exfat-utils.git] / libexfat / utf.c
1 /*
2         utf.c (13.09.09)
3         exFAT file system implementation library.
4
5         Copyright (C) 2009, 2010  Andrew Nayenko
6
7         This program is free software: you can redistribute it and/or modify
8         it under the terms of the GNU General Public License as published by
9         the Free Software Foundation, either version 3 of the License, or
10         (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15         GNU General Public License for more details.
16
17         You should have received a copy of the GNU General Public License
18         along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "exfat.h"
22 #include <errno.h>
23
24 static char* wchar_to_utf8(char* output, wchar_t wc, size_t outsize)
25 {
26         if (wc <= 0x7f)
27         {
28                 if (outsize < 1)
29                         return NULL;
30                 *output++ = (char) wc;
31         }
32         else if (wc <= 0x7ff)
33         {
34                 if (outsize < 2)
35                         return NULL;
36                 *output++ = 0xc0 | (wc >> 6);
37                 *output++ = 0x80 | (wc & 0x3f);
38         }
39         else if (wc <= 0xffff)
40         {
41                 if (outsize < 3)
42                         return NULL;
43                 *output++ = 0xe0 | (wc >> 12);
44                 *output++ = 0x80 | ((wc >> 6) & 0x3f);
45                 *output++ = 0x80 | (wc & 0x3f);
46         }
47         else if (wc <= 0x1fffff)
48         {
49                 if (outsize < 4)
50                         return NULL;
51                 *output++ = 0xf0 | (wc >> 18);
52                 *output++ = 0x80 | ((wc >> 12) & 0x3f);
53                 *output++ = 0x80 | ((wc >> 6) & 0x3f);
54                 *output++ = 0x80 | (wc & 0x3f);
55         }
56         else if (wc <= 0x3ffffff)
57         {
58                 if (outsize < 5)
59                         return NULL;
60                 *output++ = 0xf8 | (wc >> 24);
61                 *output++ = 0x80 | ((wc >> 18) & 0x3f);
62                 *output++ = 0x80 | ((wc >> 12) & 0x3f);
63                 *output++ = 0x80 | ((wc >> 6) & 0x3f);
64                 *output++ = 0x80 | (wc & 0x3f);
65         }
66         else if (wc <= 0x7fffffff)
67         {
68                 if (outsize < 6)
69                         return NULL;
70                 *output++ = 0xfc | (wc >> 30);
71                 *output++ = 0x80 | ((wc >> 24) & 0x3f);
72                 *output++ = 0x80 | ((wc >> 18) & 0x3f);
73                 *output++ = 0x80 | ((wc >> 12) & 0x3f);
74                 *output++ = 0x80 | ((wc >> 6) & 0x3f);
75                 *output++ = 0x80 | (wc & 0x3f);
76         }
77         else
78                 return NULL;
79
80         return output;
81 }
82
83 static const le16_t* utf16_to_wchar(const le16_t* input, wchar_t* wc,
84                 size_t insize)
85 {
86         if ((le16_to_cpu(input[0]) & 0xfc00) == 0xd800)
87         {
88                 if (insize < 2 || (le16_to_cpu(input[1]) & 0xfc00) != 0xdc00)
89                         return NULL;
90                 *wc = ((wchar_t) (le16_to_cpu(input[0]) & 0x3ff) << 10);
91                 *wc |= (le16_to_cpu(input[1]) & 0x3ff);
92                 return input + 2;
93         }
94         else
95         {
96                 *wc = le16_to_cpu(*input);
97                 return input + 1;
98         }
99 }
100
101 int utf16_to_utf8(char* output, const le16_t* input, size_t outsize,
102                 size_t insize)
103 {
104         const le16_t* inp = input;
105         char* outp = output;
106         wchar_t wc;
107
108         while (inp - input < insize && le16_to_cpu(*inp))
109         {
110                 inp = utf16_to_wchar(inp, &wc, insize - (inp - input));
111                 if (inp == NULL)
112                 {
113                         exfat_error("illegal UTF-16 sequence");
114                         return -EILSEQ;
115                 }
116                 outp = wchar_to_utf8(outp, wc, outsize - (outp - output));
117                 if (outp == NULL)
118                 {
119                         exfat_error("name is too long");
120                         return -ENAMETOOLONG;
121                 }
122         }
123         *outp = '\0';
124         return 0;
125 }
126
127 static const char* utf8_to_wchar(const char* input, wchar_t* wc,
128                 size_t insize)
129 {
130         if ((input[0] & 0x80) == 0 && insize >= 1)
131         {
132                 *wc = (wchar_t) input[0];
133                 return input + 1;
134         }
135         if ((input[0] & 0xe0) == 0xc0 && insize >= 2)
136         {
137                 *wc = (((wchar_t) input[0] & 0x1f) << 6) |
138                        ((wchar_t) input[1] & 0x3f);
139                 return input + 2;
140         }
141         if ((input[0] & 0xf0) == 0xe0 && insize >= 3)
142         {
143                 *wc = (((wchar_t) input[0] & 0x0f) << 12) |
144                       (((wchar_t) input[1] & 0x3f) << 6) |
145                        ((wchar_t) input[2] & 0x3f);
146                 return input + 3;
147         }
148         if ((input[0] & 0xf8) == 0xf0 && insize >= 4)
149         {
150                 *wc = (((wchar_t) input[0] & 0x07) << 18) |
151                       (((wchar_t) input[1] & 0x3f) << 12) |
152                       (((wchar_t) input[2] & 0x3f) << 6) |
153                        ((wchar_t) input[3] & 0x3f);
154                 return input + 4;
155         }
156         if ((input[0] & 0xfc) == 0xf8 && insize >= 5)
157         {
158                 *wc = (((wchar_t) input[0] & 0x03) << 24) |
159                       (((wchar_t) input[1] & 0x3f) << 18) |
160                       (((wchar_t) input[2] & 0x3f) << 12) |
161                       (((wchar_t) input[3] & 0x3f) << 6) |
162                        ((wchar_t) input[4] & 0x3f);
163                 return input + 5;
164         }
165         if ((input[0] & 0xfe) == 0xfc && insize >= 6)
166         {
167                 *wc = (((wchar_t) input[0] & 0x01) << 30) |
168                       (((wchar_t) input[1] & 0x3f) << 24) |
169                       (((wchar_t) input[2] & 0x3f) << 18) |
170                       (((wchar_t) input[3] & 0x3f) << 12) |
171                       (((wchar_t) input[4] & 0x3f) << 6) |
172                        ((wchar_t) input[5] & 0x3f);
173                 return input + 6;
174         }
175         return NULL;
176 }
177
178 static le16_t* wchar_to_utf16(le16_t* output, wchar_t wc, size_t outsize)
179 {
180         if (wc <= 0xffff) /* if character is from BMP */
181         {
182                 if (outsize == 0)
183                         return NULL;
184                 output[0] = cpu_to_le16(wc);
185                 return output + 1;
186         }
187         if (outsize < 2)
188                 return NULL;
189         output[0] = cpu_to_le16(0xd800 | ((wc >> 10) & 0x3ff));
190         output[1] = cpu_to_le16(0xdc00 | (wc & 0x3ff));
191         return output + 2;
192 }
193
194 int utf8_to_utf16(le16_t* output, const char* input, size_t outsize,
195                 size_t insize)
196 {
197         const char* inp = input;
198         le16_t* outp = output;
199         wchar_t wc;
200
201         while (inp - input < insize && *inp)
202         {
203                 inp = utf8_to_wchar(inp, &wc, insize - (inp - input));
204                 if (inp == NULL)
205                 {
206                         exfat_error("illegal UTF-8 sequence");
207                         return -EILSEQ;
208                 }
209                 outp = wchar_to_utf16(outp, wc, outsize - (outp - output));
210                 if (outp == NULL)
211                 {
212                         exfat_error("name is too long");
213                         return -ENAMETOOLONG;
214                 }
215         }
216         *outp = cpu_to_le16(0);
217         return 0;
218 }
219
220 size_t utf16_length(const le16_t* str)
221 {
222         size_t i = 0;
223
224         while (le16_to_cpu(str[i]))
225                 i++;
226         return i;
227 }