3 exFAT file system implementation library.
5 Copyright (C) 2010-2013 Andrew Nayenko
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.
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.
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/>.
24 static char* wchar_to_utf8(char* output, wchar_t wc, size_t outsize)
30 *output++ = (char) wc;
36 *output++ = 0xc0 | (wc >> 6);
37 *output++ = 0x80 | (wc & 0x3f);
39 else if (wc <= 0xffff)
43 *output++ = 0xe0 | (wc >> 12);
44 *output++ = 0x80 | ((wc >> 6) & 0x3f);
45 *output++ = 0x80 | (wc & 0x3f);
47 else if (wc <= 0x1fffff)
51 *output++ = 0xf0 | (wc >> 18);
52 *output++ = 0x80 | ((wc >> 12) & 0x3f);
53 *output++ = 0x80 | ((wc >> 6) & 0x3f);
54 *output++ = 0x80 | (wc & 0x3f);
56 else if (wc <= 0x3ffffff)
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);
66 else if (wc <= 0x7fffffff)
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);
83 static const le16_t* utf16_to_wchar(const le16_t* input, wchar_t* wc,
86 if ((le16_to_cpu(input[0]) & 0xfc00) == 0xd800)
88 if (insize < 2 || (le16_to_cpu(input[1]) & 0xfc00) != 0xdc00)
90 *wc = ((wchar_t) (le16_to_cpu(input[0]) & 0x3ff) << 10);
91 *wc |= (le16_to_cpu(input[1]) & 0x3ff);
97 *wc = le16_to_cpu(*input);
102 int utf16_to_utf8(char* output, const le16_t* input, size_t outsize,
105 const le16_t* inp = input;
109 while (inp - input < insize && le16_to_cpu(*inp))
111 inp = utf16_to_wchar(inp, &wc, insize - (inp - input));
114 exfat_error("illegal UTF-16 sequence");
117 outp = wchar_to_utf8(outp, wc, outsize - (outp - output));
120 exfat_error("name is too long");
121 return -ENAMETOOLONG;
128 static const char* utf8_to_wchar(const char* input, wchar_t* wc,
131 if ((input[0] & 0x80) == 0 && insize >= 1)
133 *wc = (wchar_t) input[0];
136 if ((input[0] & 0xe0) == 0xc0 && insize >= 2)
138 *wc = (((wchar_t) input[0] & 0x1f) << 6) |
139 ((wchar_t) input[1] & 0x3f);
142 if ((input[0] & 0xf0) == 0xe0 && insize >= 3)
144 *wc = (((wchar_t) input[0] & 0x0f) << 12) |
145 (((wchar_t) input[1] & 0x3f) << 6) |
146 ((wchar_t) input[2] & 0x3f);
149 if ((input[0] & 0xf8) == 0xf0 && insize >= 4)
151 *wc = (((wchar_t) input[0] & 0x07) << 18) |
152 (((wchar_t) input[1] & 0x3f) << 12) |
153 (((wchar_t) input[2] & 0x3f) << 6) |
154 ((wchar_t) input[3] & 0x3f);
157 if ((input[0] & 0xfc) == 0xf8 && insize >= 5)
159 *wc = (((wchar_t) input[0] & 0x03) << 24) |
160 (((wchar_t) input[1] & 0x3f) << 18) |
161 (((wchar_t) input[2] & 0x3f) << 12) |
162 (((wchar_t) input[3] & 0x3f) << 6) |
163 ((wchar_t) input[4] & 0x3f);
166 if ((input[0] & 0xfe) == 0xfc && insize >= 6)
168 *wc = (((wchar_t) input[0] & 0x01) << 30) |
169 (((wchar_t) input[1] & 0x3f) << 24) |
170 (((wchar_t) input[2] & 0x3f) << 18) |
171 (((wchar_t) input[3] & 0x3f) << 12) |
172 (((wchar_t) input[4] & 0x3f) << 6) |
173 ((wchar_t) input[5] & 0x3f);
179 static le16_t* wchar_to_utf16(le16_t* output, wchar_t wc, size_t outsize)
181 if (wc <= 0xffff) /* if character is from BMP */
185 output[0] = cpu_to_le16(wc);
191 output[0] = cpu_to_le16(0xd800 | ((wc >> 10) & 0x3ff));
192 output[1] = cpu_to_le16(0xdc00 | (wc & 0x3ff));
196 int utf8_to_utf16(le16_t* output, const char* input, size_t outsize,
199 const char* inp = input;
200 le16_t* outp = output;
203 while (inp - input < insize && *inp)
205 inp = utf8_to_wchar(inp, &wc, insize - (inp - input));
208 exfat_error("illegal UTF-8 sequence");
211 outp = wchar_to_utf16(outp, wc, outsize - (outp - output));
214 exfat_error("name is too long");
215 return -ENAMETOOLONG;
218 *outp = cpu_to_le16(0);
222 size_t utf16_length(const le16_t* str)
226 while (le16_to_cpu(str[i]))