diff --git a/libgambatte/libretro-common/compat/compat_strl.c b/libgambatte/libretro-common/compat/compat_strl.c index 3172310..3a6392c 100644 --- a/libgambatte/libretro-common/compat/compat_strl.c +++ b/libgambatte/libretro-common/compat/compat_strl.c @@ -60,10 +60,3 @@ size_t strlcat(char *dest, const char *source, size_t size) return len + strlcpy(dest, source, size); } #endif - -char *strldup(const char *s, size_t n) -{ - char *dst = (char*)malloc(sizeof(char) * (n + 1)); - strlcpy(dst, s, n); - return dst; -} diff --git a/libgambatte/libretro-common/compat/fopen_utf8.c b/libgambatte/libretro-common/compat/fopen_utf8.c index 85abb59..384657b 100644 --- a/libgambatte/libretro-common/compat/fopen_utf8.c +++ b/libgambatte/libretro-common/compat/fopen_utf8.c @@ -37,27 +37,28 @@ void *fopen_utf8(const char * filename, const char * mode) { #if defined(LEGACY_WIN32) - FILE *ret = NULL; char * filename_local = utf8_to_local_string_alloc(filename); - - if (!filename_local) - return NULL; - ret = fopen(filename_local, mode); if (filename_local) + { + FILE *ret = fopen(filename_local, mode); free(filename_local); - return ret; + return ret; + } #else - wchar_t * filename_w = utf8_to_utf16_string_alloc(filename); - wchar_t * mode_w = utf8_to_utf16_string_alloc(mode); - FILE* ret = NULL; - - if (filename_w && mode_w) - ret = _wfopen(filename_w, mode_w); + wchar_t * filename_w = utf8_to_utf16_string_alloc(filename); if (filename_w) + { + FILE *ret = NULL; + wchar_t *mode_w = utf8_to_utf16_string_alloc(mode); + if (mode_w) + { + ret = _wfopen(filename_w, mode_w); + free(mode_w); + } free(filename_w); - if (mode_w) - free(mode_w); - return ret; + return ret; + } #endif + return NULL; } #endif diff --git a/libgambatte/libretro-common/encodings/encoding_utf.c b/libgambatte/libretro-common/encodings/encoding_utf.c index 2760824..f9c594d 100644 --- a/libgambatte/libretro-common/encodings/encoding_utf.c +++ b/libgambatte/libretro-common/encodings/encoding_utf.c @@ -51,9 +51,12 @@ static unsigned leading_ones(uint8_t c) return ones; } -/* Simple implementation. Assumes the sequence is - * properly synchronized and terminated. */ - +/** + * utf8_conv_utf32: + * + * Simple implementation. Assumes the sequence is + * properly synchronized and terminated. + **/ size_t utf8_conv_utf32(uint32_t *out, size_t out_chars, const char *in, size_t in_size) { @@ -79,7 +82,7 @@ size_t utf8_conv_utf32(uint32_t *out, size_t out_chars, for (i = 0; i < extra; i++, in++, shift -= 6) c |= (*in & 0x3f) << shift; - *out++ = c; + *out++ = c; in_size -= 1 + extra; out_chars--; ret++; @@ -88,6 +91,11 @@ size_t utf8_conv_utf32(uint32_t *out, size_t out_chars, return ret; } +/** + * utf16_conv_utf8: + * + * Leaf function. + **/ bool utf16_conv_utf8(uint8_t *out, size_t *out_chars, const uint16_t *in, size_t in_size) { @@ -148,16 +156,20 @@ bool utf16_conv_utf8(uint8_t *out, size_t *out_chars, return false; } -/* Acts mostly like strlcpy. +/** + * utf8cpy: + * + * Acts mostly like strlcpy. * * Copies the given number of UTF-8 characters, - * but at most d_len bytes. + * but at most @d_len bytes. * - * Always NULL terminates. - * Does not copy half a character. + * Always NULL terminates. Does not copy half a character. + * @s is assumed valid UTF-8. + * Use only if @chars is considerably less than @d_len. * - * Returns number of bytes. 's' is assumed valid UTF-8. - * Use only if 'chars' is considerably less than 'd_len'. */ + * @return Number of bytes. + **/ size_t utf8cpy(char *d, size_t d_len, const char *s, size_t chars) { const uint8_t *sb = (const uint8_t*)s; @@ -186,6 +198,11 @@ size_t utf8cpy(char *d, size_t d_len, const char *s, size_t chars) return sb-sb_org; } +/** + * utf8skip: + * + * Leaf function + **/ const char *utf8skip(const char *str, size_t chars) { const uint8_t *strb = (const uint8_t*)str; @@ -204,6 +221,11 @@ const char *utf8skip(const char *str, size_t chars) return (const char*)strb; } +/** + * utf8len: + * + * Leaf function. + **/ size_t utf8len(const char *string) { size_t ret = 0; @@ -220,7 +242,15 @@ size_t utf8len(const char *string) return ret; } -/* Does not validate the input, returns garbage if it's not UTF-8. */ +/** + * utf8_walk: + * + * Does not validate the input. + * + * Leaf function. + * + * @return Returns garbage if it's not UTF-8. + **/ uint32_t utf8_walk(const char **string) { uint8_t first = UTF8_WALKBYTE(string); @@ -248,24 +278,23 @@ static bool utf16_to_char(uint8_t **utf_data, size_t *dest_len, const uint16_t *in) { unsigned len = 0; - while (in[len] != '\0') len++; - utf16_conv_utf8(NULL, dest_len, in, len); *dest_len += 1; - *utf_data = (uint8_t*)malloc(*dest_len); - if (*utf_data == 0) - return false; - - return utf16_conv_utf8(*utf_data, dest_len, in, len); + if ((*utf_data = (uint8_t*)malloc(*dest_len)) != 0) + return utf16_conv_utf8(*utf_data, dest_len, in, len); + return false; } +/** + * utf16_to_char_string: + **/ bool utf16_to_char_string(const uint16_t *in, char *s, size_t len) { - size_t dest_len = 0; - uint8_t *utf16_data = NULL; - bool ret = utf16_to_char(&utf16_data, &dest_len, in); + size_t dest_len = 0; + uint8_t *utf16_data = NULL; + bool ret = utf16_to_char(&utf16_data, &dest_len, in); if (ret) { @@ -274,13 +303,17 @@ bool utf16_to_char_string(const uint16_t *in, char *s, size_t len) } free(utf16_data); - utf16_data = NULL; + utf16_data = NULL; return ret; } #if defined(_WIN32) && !defined(_XBOX) && !defined(UNICODE) -/* Returned pointer MUST be freed by the caller if non-NULL. */ +/** + * mb_to_mb_string_alloc: + * + * @return Returned pointer MUST be freed by the caller if non-NULL. + **/ static char *mb_to_mb_string_alloc(const char *str, enum CodePage cp_in, enum CodePage cp_out) { @@ -300,10 +333,8 @@ static char *mb_to_mb_string_alloc(const char *str, if (!path_buf_wide_len) return strdup(str); - path_buf_wide = (wchar_t*) - calloc(path_buf_wide_len + sizeof(wchar_t), sizeof(wchar_t)); - - if (path_buf_wide) + if ((path_buf_wide = (wchar_t*) + calloc(path_buf_wide_len + sizeof(wchar_t), sizeof(wchar_t)))) { MultiByteToWideChar(cp_in, 0, str, -1, path_buf_wide, path_buf_wide_len); @@ -347,45 +378,49 @@ static char *mb_to_mb_string_alloc(const char *str, } #endif -/* Returned pointer MUST be freed by the caller if non-NULL. */ +/** + * utf8_to_local_string_alloc: + * + * @return Returned pointer MUST be freed by the caller if non-NULL. + **/ char* utf8_to_local_string_alloc(const char *str) { if (str && *str) - { #if defined(_WIN32) && !defined(_XBOX) && !defined(UNICODE) return mb_to_mb_string_alloc(str, CODEPAGE_UTF8, CODEPAGE_LOCAL); #else - /* assume string needs no modification if not on Windows */ - return strdup(str); + return strdup(str); /* Assume string needs no modification if not on Windows */ #endif - } return NULL; } -/* Returned pointer MUST be freed by the caller if non-NULL. */ -char* local_to_utf8_string_alloc(const char *str) +/** + * local_to_utf8_string_alloc: + * + * @return Returned pointer MUST be freed by the caller if non-NULL. + **/ +char *local_to_utf8_string_alloc(const char *str) { - if (str && *str) - { + if (str && *str) #if defined(_WIN32) && !defined(_XBOX) && !defined(UNICODE) - return mb_to_mb_string_alloc(str, CODEPAGE_LOCAL, CODEPAGE_UTF8); + return mb_to_mb_string_alloc(str, CODEPAGE_LOCAL, CODEPAGE_UTF8); #else - /* assume string needs no modification if not on Windows */ - return strdup(str); + return strdup(str); /* Assume string needs no modification if not on Windows */ #endif - } - return NULL; + return NULL; } -/* Returned pointer MUST be freed by the caller if non-NULL. */ +/** + * utf8_to_utf16_string_alloc: + * + * @return Returned pointer MUST be freed by the caller if non-NULL. + **/ wchar_t* utf8_to_utf16_string_alloc(const char *str) { #ifdef _WIN32 int len = 0; - int out_len = 0; #else size_t len = 0; - size_t out_len = 0; #endif wchar_t *buf = NULL; @@ -393,63 +428,55 @@ wchar_t* utf8_to_utf16_string_alloc(const char *str) return NULL; #ifdef _WIN32 - len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); - - if (len) + if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0))) { - buf = (wchar_t*)calloc(len, sizeof(wchar_t)); - - if (!buf) + if (!(buf = (wchar_t*)calloc(len, sizeof(wchar_t)))) return NULL; - out_len = MultiByteToWideChar(CP_UTF8, 0, str, -1, buf, len); + if ((MultiByteToWideChar(CP_UTF8, 0, str, -1, buf, len)) < 0) + { + free(buf); + return NULL; + } } else { - /* fallback to ANSI codepage instead */ - len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - - if (len) + /* Fallback to ANSI codepage instead */ + if ((len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0))) { - buf = (wchar_t*)calloc(len, sizeof(wchar_t)); - - if (!buf) + if (!(buf = (wchar_t*)calloc(len, sizeof(wchar_t)))) return NULL; - out_len = MultiByteToWideChar(CP_ACP, 0, str, -1, buf, len); + if ((MultiByteToWideChar(CP_ACP, 0, str, -1, buf, len)) < 0) + { + free(buf); + return NULL; + } } } - - if (out_len < 0) - { - free(buf); - return NULL; - } #else /* NOTE: For now, assume non-Windows platforms' locale is already UTF-8. */ - len = mbstowcs(NULL, str, 0) + 1; - - if (len) + if ((len = mbstowcs(NULL, str, 0) + 1)) { - buf = (wchar_t*)calloc(len, sizeof(wchar_t)); - - if (!buf) + if (!(buf = (wchar_t*)calloc(len, sizeof(wchar_t)))) return NULL; - out_len = mbstowcs(buf, str, len); - } - - if (out_len == (size_t)-1) - { - free(buf); - return NULL; + if ((mbstowcs(buf, str, len)) == (size_t)-1) + { + free(buf); + return NULL; + } } #endif return buf; } -/* Returned pointer MUST be freed by the caller if non-NULL. */ +/** + * utf16_to_utf8_string_alloc: + * + * @return Returned pointer MUST be freed by the caller if non-NULL. + **/ char* utf16_to_utf8_string_alloc(const wchar_t *str) { #ifdef _WIN32 @@ -465,20 +492,17 @@ char* utf16_to_utf8_string_alloc(const wchar_t *str) #ifdef _WIN32 { UINT code_page = CP_UTF8; - len = WideCharToMultiByte(code_page, - 0, str, -1, NULL, 0, NULL, NULL); /* fallback to ANSI codepage instead */ - if (!len) + if (!(len = WideCharToMultiByte(code_page, + 0, str, -1, NULL, 0, NULL, NULL))) { code_page = CP_ACP; len = WideCharToMultiByte(code_page, 0, str, -1, NULL, 0, NULL, NULL); } - buf = (char*)calloc(len, sizeof(char)); - - if (!buf) + if (!(buf = (char*)calloc(len, sizeof(char)))) return NULL; if (WideCharToMultiByte(code_page, @@ -491,13 +515,9 @@ char* utf16_to_utf8_string_alloc(const wchar_t *str) #else /* NOTE: For now, assume non-Windows platforms' * locale is already UTF-8. */ - len = wcstombs(NULL, str, 0) + 1; - - if (len) + if ((len = wcstombs(NULL, str, 0) + 1)) { - buf = (char*)calloc(len, sizeof(char)); - - if (!buf) + if (!(buf = (char*)calloc(len, sizeof(char)))) return NULL; if (wcstombs(buf, str, len) == (size_t)-1) diff --git a/libgambatte/libretro-common/file/file_path.c b/libgambatte/libretro-common/file/file_path.c index fc11270..c696ff0 100644 --- a/libgambatte/libretro-common/file/file_path.c +++ b/libgambatte/libretro-common/file/file_path.c @@ -24,13 +24,13 @@ #include #include #include -#include +#include #include #include #include -#include +#include #include #include