diff --git a/libretro-common/cdrom/cdrom.c b/libretro-common/cdrom/cdrom.c index 339277b..58cb5cf 100644 --- a/libretro-common/cdrom/cdrom.c +++ b/libretro-common/cdrom/cdrom.c @@ -1346,9 +1346,9 @@ struct string_list* cdrom_get_available_drives(void) if (string_starts_with_size(dir_list->elems[i].data, "/dev/sg", STRLEN_CONST("/dev/sg"))) { + char drive_string[33]; libretro_vfs_implementation_file *stream; char drive_model[32] = {0}; - char drive_string[33] = {0}; union string_list_elem_attr attr = {0}; int dev_index = 0; RFILE *file = filestream_open( @@ -1380,9 +1380,9 @@ struct string_list* cdrom_get_available_drives(void) attr.i = dev_index; if (!string_is_empty(drive_model)) - strlcat(drive_string, drive_model, sizeof(drive_string)); + strlcpy(drive_string, drive_model, sizeof(drive_string)); else - strlcat(drive_string, "Unknown Drive", sizeof(drive_string)); + strlcpy(drive_string, "Unknown Drive", sizeof(drive_string)); string_list_append(list, drive_string, attr); } @@ -1404,7 +1404,7 @@ struct string_list* cdrom_get_available_drives(void) if (string_split_noalloc(&mods, buf, "\n")) { - for (i = 0; i < mods.size; i++) + for (i = 0; i < (int)mods.size; i++) { if (strcasestr(mods.elems[i].data, "sg ")) { @@ -1416,6 +1416,7 @@ struct string_list* cdrom_get_available_drives(void) } } string_list_deinitialize(&mods); + free(buf); #ifdef CDROM_DEBUG if (found) @@ -1445,13 +1446,13 @@ struct string_list* cdrom_get_available_drives(void) DWORD drive_mask = GetLogicalDrives(); int i; - for (i = 0; i < sizeof(DWORD) * 8; i++) + for (i = 0; i < (int)(sizeof(DWORD) * 8); i++) { - char path[] = {"a:\\"}; + char path[] = {"a:\\"}; char cdrom_path[] = {"cdrom://a:/drive-track01.bin"}; - path[0] += i; - cdrom_path[8] += i; + path[0] += i; + cdrom_path[8] += i; /* this drive letter doesn't exist */ if (!(drive_mask & (1 << i))) @@ -1459,15 +1460,14 @@ struct string_list* cdrom_get_available_drives(void) if (GetDriveType(path) != DRIVE_CDROM) continue; - else + { - char drive_model[32] = {0}; - char drive_string[33] = {0}; + char drive_string[33]; + libretro_vfs_implementation_file *stream; + bool is_cdrom = false; + char drive_model[32] = {0}; union string_list_elem_attr attr = {0}; RFILE *file = filestream_open(cdrom_path, RETRO_VFS_FILE_ACCESS_READ, 0); - libretro_vfs_implementation_file *stream; - bool is_cdrom = false; - if (!file) continue; @@ -1481,9 +1481,9 @@ struct string_list* cdrom_get_available_drives(void) attr.i = path[0]; if (!string_is_empty(drive_model)) - strlcat(drive_string, drive_model, sizeof(drive_string)); + strlcpy(drive_string, drive_model, sizeof(drive_string)); else - strlcat(drive_string, "Unknown Drive", sizeof(drive_string)); + strlcpy(drive_string, "Unknown Drive", sizeof(drive_string)); string_list_append(list, drive_string, attr); } @@ -1523,9 +1523,7 @@ bool cdrom_drive_has_media(const char drive) if (file) { libretro_vfs_implementation_file *stream = filestream_get_vfs_handle(file); - bool has_media = false; - - has_media = cdrom_is_media_inserted(stream); + bool has_media = cdrom_is_media_inserted(stream); filestream_close(file); @@ -1537,14 +1535,14 @@ bool cdrom_drive_has_media(const char drive) bool cdrom_set_read_cache(libretro_vfs_implementation_file *stream, bool enabled) { + int i; /* MMC Command: MODE SENSE (10) and MODE SELECT (10) */ unsigned char cdb_sense_changeable[] = {0x5A, 0, 0x48, 0, 0, 0, 0, 0, 0x14, 0}; - unsigned char cdb_sense[] = {0x5A, 0, 0x8, 0, 0, 0, 0, 0, 0x14, 0}; - unsigned char cdb_select[] = {0x55, 0x10, 0, 0, 0, 0, 0, 0, 0x14, 0}; - unsigned char buf[20] = {0}; - int rv, i; - - rv = cdrom_send_command(stream, DIRECTION_IN, buf, sizeof(buf), cdb_sense_changeable, sizeof(cdb_sense_changeable), 0); + unsigned char cdb_sense[] = {0x5A, 0, 0x8, 0, 0, 0, 0, 0, 0x14, 0}; + unsigned char cdb_select[] = {0x55, 0x10, 0, 0, 0, 0, 0, 0, 0x14, 0}; + unsigned char buf[20] = {0}; + int rv = cdrom_send_command(stream, DIRECTION_IN, buf, sizeof(buf), + cdb_sense_changeable, sizeof(cdb_sense_changeable), 0); #ifdef CDROM_DEBUG printf("[CDROM] mode sense changeable status code %d\n", rv); @@ -1580,9 +1578,7 @@ bool cdrom_set_read_cache(libretro_vfs_implementation_file *stream, bool enabled printf("Mode sense data for caching mode page: "); for (i = 0; i < (int)sizeof(buf); i++) - { printf("%02X ", buf[i]); - } printf("\n"); fflush(stdout); @@ -1595,7 +1591,7 @@ bool cdrom_set_read_cache(libretro_vfs_implementation_file *stream, bool enabled if (enabled) buf[10] &= ~1; else - buf[10] |= 1; + buf[10] |= 1; rv = cdrom_send_command(stream, DIRECTION_OUT, buf, sizeof(buf), cdb_select, sizeof(cdb_select), 0); @@ -1644,9 +1640,7 @@ bool cdrom_get_timeouts(libretro_vfs_implementation_file *stream, cdrom_group_ti printf("Mode sense data for timeout groups: "); for (i = 0; i < (int)sizeof(buf); i++) - { printf("%02X ", buf[i]); - } printf("\n"); @@ -1668,8 +1662,8 @@ bool cdrom_get_timeouts(libretro_vfs_implementation_file *stream, cdrom_group_ti bool cdrom_has_atip(libretro_vfs_implementation_file *stream) { /* MMC Command: READ TOC/PMA/ATIP */ - unsigned char cdb[] = {0x43, 0x2, 0x4, 0, 0, 0, 0, 0x9, 0x30, 0}; - unsigned char buf[32] = {0}; + unsigned char cdb[] = {0x43, 0x2, 0x4, 0, 0, 0, 0, 0x9, 0x30, 0}; + unsigned char buf[32] = {0}; unsigned short atip_len = 0; int rv = cdrom_send_command(stream, DIRECTION_IN, buf, sizeof(buf), cdb, sizeof(cdb), 0); @@ -1679,7 +1673,10 @@ bool cdrom_has_atip(libretro_vfs_implementation_file *stream) atip_len = buf[0] << 8 | buf[1]; #ifdef CDROM_DEBUG - printf("ATIP Length %d, Disc Type %d, Disc Sub-Type %d\n", atip_len, (buf[6] >> 6) & 0x1, ((buf[6] >> 5) & 0x1) << 2 | ((buf[6] >> 4) & 0x1) << 1 | ((buf[6] >> 3) & 0x1) << 0); + printf("ATIP Length %d, Disc Type %d, Disc Sub-Type %d\n", + atip_len, + (buf[6] >> 6) & 0x1, + ((buf[6] >> 5) & 0x1) << 2 | ((buf[6] >> 4) & 0x1) << 1 | ((buf[6] >> 3) & 0x1) << 0); #endif if (atip_len < 5) @@ -1690,30 +1687,23 @@ bool cdrom_has_atip(libretro_vfs_implementation_file *stream) void cdrom_device_fillpath(char *path, size_t len, char drive, unsigned char track, bool is_cue) { - size_t pos = 0; - if (!path || len == 0) return; - if (is_cue) { #ifdef _WIN32 - pos = strlcpy(path, "cdrom://", len); - + size_t pos = strlcpy(path, "cdrom://", len); if (len > pos) path[pos++] = drive; - pos = strlcat(path, ":/drive.cue", len); #else #ifdef __linux__ - pos = strlcpy(path, "cdrom://drive", len); - + size_t pos = strlcpy(path, "cdrom://drive", len); if (len > pos + 1) { path[pos++] = drive; - path[pos] = '\0'; + path[pos] = '\0'; } - pos = strlcat(path, ".cue", len); #endif #endif @@ -1721,22 +1711,18 @@ void cdrom_device_fillpath(char *path, size_t len, char drive, unsigned char tra else { #ifdef _WIN32 - pos = strlcpy(path, "cdrom://", len); - + size_t pos = strlcpy(path, "cdrom://", len); if (len > pos + 1) { path[pos++] = drive; - path[pos] = '\0'; + path[pos] = '\0'; } - pos += snprintf(path + pos, len - pos, ":/drive-track%02d.bin", track); #else #ifdef __linux__ - pos = strlcpy(path, "cdrom://drive", len); - + size_t pos = strlcpy(path, "cdrom://drive", len); if (len > pos) path[pos++] = drive; - pos += snprintf(path + pos, len - pos, "-track%02d.bin", track); #endif #endif diff --git a/libretro-common/compat/compat_strl.c b/libretro-common/compat/compat_strl.c index 3172310..3a6392c 100644 --- a/libretro-common/compat/compat_strl.c +++ b/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/libretro-common/compat/fopen_utf8.c b/libretro-common/compat/fopen_utf8.c index ea16961..384657b 100644 --- a/libretro-common/compat/fopen_utf8.c +++ b/libretro-common/compat/fopen_utf8.c @@ -37,22 +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 = _wfopen(filename_w, mode_w); - free(filename_w); - free(mode_w); - return ret; + 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); + return ret; + } #endif + return NULL; } #endif diff --git a/libretro-common/encodings/encoding_crc32.c b/libretro-common/encodings/encoding_crc32.c index 8b1f76e..ab8ce82 100644 --- a/libretro-common/encodings/encoding_crc32.c +++ b/libretro-common/encodings/encoding_crc32.c @@ -23,7 +23,6 @@ #include #include #include -#include #include static const uint32_t crc32_table[256] = { @@ -90,51 +89,3 @@ uint32_t encoding_crc32(uint32_t crc, const uint8_t *buf, size_t len) return crc ^ 0xffffffff; } - -#define CRC32_BUFFER_SIZE 1048576 -#define CRC32_MAX_MB 64 - -/** - * Calculate a CRC32 from the first part of the given file. - * "first part" being the first (CRC32_BUFFER_SIZE * CRC32_MAX_MB) - * bytes. - * - * Returns: the crc32, or 0 if there was an error. - */ -uint32_t file_crc32(uint32_t crc, const char *path) -{ - unsigned i; - RFILE *file = NULL; - unsigned char *buf = NULL; - if (!path) - return 0; - - file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0); - if (!file) - goto error; - - buf = (unsigned char*)malloc(CRC32_BUFFER_SIZE); - if (!buf) - goto error; - - for (i = 0; i < CRC32_MAX_MB; i++) - { - int64_t nread = filestream_read(file, buf, CRC32_BUFFER_SIZE); - if (nread < 0) - goto error; - - crc = encoding_crc32(crc, buf, (size_t)nread); - if (filestream_eof(file)) - break; - } - free(buf); - filestream_close(file); - return crc; - -error: - if (buf) - free(buf); - if (file) - filestream_close(file); - return 0; -} diff --git a/libretro-common/encodings/encoding_utf.c b/libretro-common/encodings/encoding_utf.c index 2760824..f9c594d 100644 --- a/libretro-common/encodings/encoding_utf.c +++ b/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/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index 44ceb72..c696ff0 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -24,13 +24,13 @@ #include #include #include -#include +#include #include #include #include -#include +#include #include #include