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