1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (encoding_utf.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include <compat/strl.h>
30 #include <retro_inline.h>
32 #include <encodings/utf.h>
34 #if defined(_WIN32) && !defined(_XBOX)
40 #define UTF8_WALKBYTE(string) (*((*(string))++))
42 static unsigned leading_ones(uint8_t c)
54 /* Simple implementation. Assumes the sequence is
55 * properly synchronized and terminated. */
57 size_t utf8_conv_utf32(uint32_t *out, size_t out_chars,
58 const char *in, size_t in_size)
62 while (in_size && out_chars)
64 unsigned extra, shift;
66 uint8_t first = *in++;
67 unsigned ones = leading_ones(first);
69 if (ones > 6 || ones == 1) /* Invalid or desync. */
72 extra = ones ? ones - 1 : ones;
73 if (1 + extra > in_size) /* Overflow. */
76 shift = (extra - 1) * 6;
77 c = (first & ((1 << (7 - ones)) - 1)) << (6 * extra);
79 for (i = 0; i < extra; i++, in++, shift -= 6)
80 c |= (*in & 0x3f) << shift;
91 bool utf16_conv_utf8(uint8_t *out, size_t *out_chars,
92 const uint16_t *in, size_t in_size)
97 uint8_t utf8_limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
104 if (in_pos == in_size)
106 *out_chars = out_pos;
109 value = in[in_pos++];
113 out[out_pos] = (char)value;
118 if (value >= 0xD800 && value < 0xE000)
122 if (value >= 0xDC00 || in_pos == in_size)
125 if (c2 < 0xDC00 || c2 >= 0xE000)
127 value = (((value - 0xD800) << 10) | (c2 - 0xDC00)) + 0x10000;
130 for (num_adds = 1; num_adds < 5; num_adds++)
131 if (value < (((uint32_t)1) << (num_adds * 5 + 6)))
134 out[out_pos] = (char)(utf8_limits[num_adds - 1]
135 + (value >> (6 * num_adds)));
141 out[out_pos] = (char)(0x80
142 + ((value >> (6 * num_adds)) & 0x3F));
144 }while (num_adds != 0);
147 *out_chars = out_pos;
151 /* Acts mostly like strlcpy.
153 * Copies the given number of UTF-8 characters,
154 * but at most d_len bytes.
156 * Always NULL terminates.
157 * Does not copy half a character.
159 * Returns number of bytes. 's' is assumed valid UTF-8.
160 * Use only if 'chars' is considerably less than 'd_len'. */
161 size_t utf8cpy(char *d, size_t d_len, const char *s, size_t chars)
163 const uint8_t *sb = (const uint8_t*)s;
164 const uint8_t *sb_org = sb;
169 while (*sb && chars-- > 0)
172 while ((*sb & 0xC0) == 0x80)
176 if ((size_t)(sb - sb_org) > d_len-1 /* NUL */)
178 sb = sb_org + d_len-1;
179 while ((*sb & 0xC0) == 0x80)
183 memcpy(d, sb_org, sb-sb_org);
189 const char *utf8skip(const char *str, size_t chars)
191 const uint8_t *strb = (const uint8_t*)str;
199 while ((*strb & 0xC0)==0x80)
204 return (const char*)strb;
207 size_t utf8len(const char *string)
216 if ((*string & 0xC0) != 0x80)
223 /* Does not validate the input, returns garbage if it's not UTF-8. */
224 uint32_t utf8_walk(const char **string)
226 uint8_t first = UTF8_WALKBYTE(string);
232 ret = (ret << 6) | (UTF8_WALKBYTE(string) & 0x3F);
235 ret = (ret << 6) | (UTF8_WALKBYTE(string) & 0x3F);
238 ret = (ret << 6) | (UTF8_WALKBYTE(string) & 0x3F);
239 return ret | (first & 7) << 18;
241 return ret | (first & 15) << 12;
244 return ret | (first & 31) << 6;
247 static bool utf16_to_char(uint8_t **utf_data,
248 size_t *dest_len, const uint16_t *in)
252 while (in[len] != '\0')
255 utf16_conv_utf8(NULL, dest_len, in, len);
257 *utf_data = (uint8_t*)malloc(*dest_len);
261 return utf16_conv_utf8(*utf_data, dest_len, in, len);
264 bool utf16_to_char_string(const uint16_t *in, char *s, size_t len)
267 uint8_t *utf16_data = NULL;
268 bool ret = utf16_to_char(&utf16_data, &dest_len, in);
272 utf16_data[dest_len] = 0;
273 strlcpy(s, (const char*)utf16_data, len);
282 #if defined(_WIN32) && !defined(_XBOX) && !defined(UNICODE)
283 /* Returned pointer MUST be freed by the caller if non-NULL. */
284 static char *mb_to_mb_string_alloc(const char *str,
285 enum CodePage cp_in, enum CodePage cp_out)
287 wchar_t *path_buf_wide = NULL;
288 int path_buf_wide_len = MultiByteToWideChar(cp_in, 0, str, -1, NULL, 0);
290 /* Windows 95 will return 0 from these functions with
291 * a UTF8 codepage set without MSLU.
293 * From an unknown MSDN version (others omit this info):
294 * - CP_UTF8 Windows 98/Me, Windows NT 4.0 and later:
295 * Translate using UTF-8. When this is set, dwFlags must be zero.
296 * - Windows 95: Under the Microsoft Layer for Unicode,
297 * MultiByteToWideChar also supports CP_UTF7 and CP_UTF8.
300 if (!path_buf_wide_len)
303 path_buf_wide = (wchar_t*)
304 calloc(path_buf_wide_len + sizeof(wchar_t), sizeof(wchar_t));
308 MultiByteToWideChar(cp_in, 0,
309 str, -1, path_buf_wide, path_buf_wide_len);
313 int path_buf_len = WideCharToMultiByte(cp_out, 0,
314 path_buf_wide, -1, NULL, 0, NULL, NULL);
318 char *path_buf = (char*)
319 calloc(path_buf_len + sizeof(char), sizeof(char));
323 WideCharToMultiByte(cp_out, 0,
324 path_buf_wide, -1, path_buf,
325 path_buf_len, NULL, NULL);
350 /* Returned pointer MUST be freed by the caller if non-NULL. */
351 char* utf8_to_local_string_alloc(const char *str)
355 #if defined(_WIN32) && !defined(_XBOX) && !defined(UNICODE)
356 return mb_to_mb_string_alloc(str, CODEPAGE_UTF8, CODEPAGE_LOCAL);
358 /* assume string needs no modification if not on Windows */
365 /* Returned pointer MUST be freed by the caller if non-NULL. */
366 char* local_to_utf8_string_alloc(const char *str)
370 #if defined(_WIN32) && !defined(_XBOX) && !defined(UNICODE)
371 return mb_to_mb_string_alloc(str, CODEPAGE_LOCAL, CODEPAGE_UTF8);
373 /* assume string needs no modification if not on Windows */
380 /* Returned pointer MUST be freed by the caller if non-NULL. */
381 wchar_t* utf8_to_utf16_string_alloc(const char *str)
396 len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
400 buf = (wchar_t*)calloc(len, sizeof(wchar_t));
405 out_len = MultiByteToWideChar(CP_UTF8, 0, str, -1, buf, len);
409 /* fallback to ANSI codepage instead */
410 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
414 buf = (wchar_t*)calloc(len, sizeof(wchar_t));
419 out_len = MultiByteToWideChar(CP_ACP, 0, str, -1, buf, len);
429 /* NOTE: For now, assume non-Windows platforms' locale is already UTF-8. */
430 len = mbstowcs(NULL, str, 0) + 1;
434 buf = (wchar_t*)calloc(len, sizeof(wchar_t));
439 out_len = mbstowcs(buf, str, len);
442 if (out_len == (size_t)-1)
452 /* Returned pointer MUST be freed by the caller if non-NULL. */
453 char* utf16_to_utf8_string_alloc(const wchar_t *str)
467 UINT code_page = CP_UTF8;
468 len = WideCharToMultiByte(code_page,
469 0, str, -1, NULL, 0, NULL, NULL);
471 /* fallback to ANSI codepage instead */
475 len = WideCharToMultiByte(code_page,
476 0, str, -1, NULL, 0, NULL, NULL);
479 buf = (char*)calloc(len, sizeof(char));
484 if (WideCharToMultiByte(code_page,
485 0, str, -1, buf, len, NULL, NULL) < 0)
492 /* NOTE: For now, assume non-Windows platforms'
493 * locale is already UTF-8. */
494 len = wcstombs(NULL, str, 0) + 1;
498 buf = (char*)calloc(len, sizeof(char));
503 if (wcstombs(buf, str, len) == (size_t)-1)