3 * exFAT file system implementation library.
5 * Created by Andrew Nayenko on 13.09.09.
6 * This software is distributed under the GNU General Public License
7 * version 3 or any later.
13 static char* wchar_to_utf8(char* output, wchar_t wc, size_t outsize)
19 *output++ = (char) wc;
25 *output++ = 0xc0 | (wc >> 6);
26 *output++ = 0x80 | (wc & 0x3f);
28 else if (wc <= 0xffff)
32 *output++ = 0xe0 | (wc >> 12);
33 *output++ = 0x80 | ((wc >> 6) & 0x3f);
34 *output++ = 0x80 | (wc & 0x3f);
36 else if (wc <= 0x1fffff)
40 *output++ = 0xf0 | (wc >> 18);
41 *output++ = 0x80 | ((wc >> 12) & 0x3f);
42 *output++ = 0x80 | ((wc >> 6) & 0x3f);
43 *output++ = 0x80 | (wc & 0x3f);
45 else if (wc <= 0x3ffffff)
49 *output++ = 0xf8 | (wc >> 24);
50 *output++ = 0x80 | ((wc >> 18) & 0x3f);
51 *output++ = 0x80 | ((wc >> 12) & 0x3f);
52 *output++ = 0x80 | ((wc >> 6) & 0x3f);
53 *output++ = 0x80 | (wc & 0x3f);
55 else if (wc <= 0x7fffffff)
59 *output++ = 0xfc | (wc >> 30);
60 *output++ = 0x80 | ((wc >> 24) & 0x3f);
61 *output++ = 0x80 | ((wc >> 18) & 0x3f);
62 *output++ = 0x80 | ((wc >> 12) & 0x3f);
63 *output++ = 0x80 | ((wc >> 6) & 0x3f);
64 *output++ = 0x80 | (wc & 0x3f);
72 static const le16_t* utf16_to_wchar(const le16_t* input, wchar_t* wc,
75 if ((le16_to_cpu(input[0]) & 0xfc00) == 0xd800)
77 if (insize < 2 || (le16_to_cpu(input[1]) & 0xfc00) != 0xdc00)
79 *wc = ((wchar_t) (le16_to_cpu(input[0]) & 0x3ff) << 10);
80 *wc |= (le16_to_cpu(input[1]) & 0x3ff);
85 *wc = le16_to_cpu(*input);
90 int utf16_to_utf8(char* output, const le16_t* input, size_t outsize,
93 const le16_t* inp = input;
97 while (inp - input < insize && le16_to_cpu(*inp))
99 inp = utf16_to_wchar(inp, &wc, insize - (inp - input));
102 exfat_error("illegal UTF-16 sequence");
105 outp = wchar_to_utf8(outp, wc, outsize - (outp - output));
108 exfat_error("name is too long");
109 return -ENAMETOOLONG;
116 static const char* utf8_to_wchar(const char* input, wchar_t* wc,
119 if ((input[0] & 0x80) == 0 && insize >= 1)
121 *wc = (wchar_t) input[0];
124 if ((input[0] & 0xe0) == 0xc0 && insize >= 2)
126 *wc = (((wchar_t) input[0] & 0x1f) << 6) |
127 ((wchar_t) input[1] & 0x3f);
130 if ((input[0] & 0xf0) == 0xe0 && insize >= 3)
132 *wc = (((wchar_t) input[0] & 0x0f) << 12) |
133 (((wchar_t) input[1] & 0x3f) << 6) |
134 ((wchar_t) input[2] & 0x3f);
137 if ((input[0] & 0xf8) == 0xf0 && insize >= 4)
139 *wc = (((wchar_t) input[0] & 0x07) << 18) |
140 (((wchar_t) input[1] & 0x3f) << 12) |
141 (((wchar_t) input[2] & 0x3f) << 6) |
142 ((wchar_t) input[3] & 0x3f);
145 if ((input[0] & 0xfc) == 0xf8 && insize >= 5)
147 *wc = (((wchar_t) input[0] & 0x03) << 24) |
148 (((wchar_t) input[1] & 0x3f) << 18) |
149 (((wchar_t) input[2] & 0x3f) << 12) |
150 (((wchar_t) input[3] & 0x3f) << 6) |
151 ((wchar_t) input[4] & 0x3f);
154 if ((input[0] & 0xfe) == 0xfc && insize >= 6)
156 *wc = (((wchar_t) input[0] & 0x01) << 30) |
157 (((wchar_t) input[1] & 0x3f) << 24) |
158 (((wchar_t) input[2] & 0x3f) << 18) |
159 (((wchar_t) input[3] & 0x3f) << 12) |
160 (((wchar_t) input[4] & 0x3f) << 6) |
161 ((wchar_t) input[5] & 0x3f);
167 static le16_t* wchar_to_utf16(le16_t* output, wchar_t wc, size_t outsize)
169 if (wc <= 0xffff) /* if character is from BMP */
173 exfat_error("name is too long");
176 output[0] = cpu_to_le16(wc);
181 exfat_error("name is too long");
184 output[0] = cpu_to_le16(0xd800 | ((wc >> 10) & 0x3ff));
185 output[1] = cpu_to_le16(0xdc00 | (wc & 0x3ff));
189 int utf8_to_utf16(le16_t* output, const char* input, size_t outsize,
192 const char* inp = input;
193 le16_t* outp = output;
196 while (inp - input < insize && *inp)
198 inp = utf8_to_wchar(inp, &wc, insize - (inp - input));
201 exfat_error("illegal UTF-8 sequence");
204 outp = wchar_to_utf16(outp, wc, outsize - (outp - output));
207 exfat_error("name is too long");
208 return -ENAMETOOLONG;
211 *outp = cpu_to_le16(0);