diff --git a/libretro-common/cdrom/cdrom.c b/libretro-common/cdrom/cdrom.c index 55fa6736..49099d44 100644 --- a/libretro-common/cdrom/cdrom.c +++ b/libretro-common/cdrom/cdrom.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2019 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (cdrom.c). @@ -96,6 +96,7 @@ void increment_msf(unsigned char *min, unsigned char *sec, unsigned char *frame) *frame = (*frame < 74) ? (*frame + 1) : 0; } +#ifdef CDROM_DEBUG static void cdrom_print_sense_data(const unsigned char *sense, size_t len) { unsigned i; @@ -175,7 +176,7 @@ static void cdrom_print_sense_data(const unsigned char *sense, size_t len) break; } - printf("[CDROM] Sense Key: %02X (%s)\n", key, sense_key_text); + printf("[CDROM] Sense Key: %02X (%s)\n", key, sense_key_text ? sense_key_text : "null"); printf("[CDROM] ASC: %02X\n", asc); printf("[CDROM] ASCQ: %02X\n", ascq); @@ -252,6 +253,7 @@ static void cdrom_print_sense_data(const unsigned char *sense, size_t len) fflush(stdout); } +#endif #if defined(_WIN32) && !defined(_XBOX) static int cdrom_send_command_win32(const libretro_vfs_implementation_file *stream, CDROM_CMD_Direction dir, void *buf, size_t len, unsigned char *cmd, size_t cmd_len, unsigned char *sense, size_t sense_len) @@ -519,7 +521,9 @@ retry: } else { +#ifdef CDROM_DEBUG cdrom_print_sense_data(sense, sizeof(sense)); +#endif /* INQUIRY/TEST/SENSE should never fail, don't retry. */ /* READ ATIP seems to fail outright on some drives with pressed discs, skip retries. */ @@ -672,7 +676,9 @@ int cdrom_get_sense(libretro_vfs_implementation_file *stream, unsigned char *sen if (rv) return 1; +#ifdef CDROM_DEBUG cdrom_print_sense_data(buf, sizeof(buf)); +#endif return 0; } @@ -931,23 +937,23 @@ int cdrom_read_subq(libretro_vfs_implementation_file *stream, unsigned char *buf if (/*(control == 4 || control == 6) && */adr == 1 && tno == 0 && point >= 1 && point <= 99) { printf("[CDROM] - Session#: %d TNO %d POINT %d ", session_num, tno, point); - printf("[CDROM] Track start time: (MSF %02u:%02u:%02u) ", (unsigned)pmin, (unsigned)psec, (unsigned)pframe); + printf("Track start time: (aMSF %02u:%02u:%02u) ", (unsigned)pmin, (unsigned)psec, (unsigned)pframe); } else if (/*(control == 4 || control == 6) && */adr == 1 && tno == 0 && point == 0xA0) { printf("[CDROM] - Session#: %d TNO %d POINT %d ", session_num, tno, point); - printf("[CDROM] First Track Number: %d ", pmin); - printf("[CDROM] Disc Type: %d ", psec); + printf("First Track Number: %d ", pmin); + printf("Disc Type: %d ", psec); } else if (/*(control == 4 || control == 6) && */adr == 1 && tno == 0 && point == 0xA1) { printf("[CDROM] - Session#: %d TNO %d POINT %d ", session_num, tno, point); - printf("[CDROM] Last Track Number: %d ", pmin); + printf("Last Track Number: %d ", pmin); } else if (/*(control == 4 || control == 6) && */adr == 1 && tno == 0 && point == 0xA2) { printf("[CDROM] - Session#: %d TNO %d POINT %d ", session_num, tno, point); - printf("[CDROM] Lead-out runtime: (MSF %02u:%02u:%02u) ", (unsigned)pmin, (unsigned)psec, (unsigned)pframe); + printf("Lead-out start time: (aMSF %02u:%02u:%02u) ", (unsigned)pmin, (unsigned)psec, (unsigned)pframe); } printf("\n"); @@ -992,10 +998,10 @@ static int cdrom_read_track_info(libretro_vfs_implementation_file *stream, unsig #ifdef CDROM_DEBUG printf("[CDROM] Track %d Info: ", track); - printf("[CDROM] Copy: %d ", (buf[5] & 0x10) > 0); - printf("[CDROM] Data Mode: %d ", toc->track[track - 1].mode); - printf("[CDROM] LBA Start: %d (%d) ", lba, toc->track[track - 1].lba); - printf("[CDROM] Track Size: %d\n", track_size); + printf("Copy: %d ", (buf[5] & 0x10) > 0); + printf("Data Mode: %d ", toc->track[track - 1].mode); + printf("LBA Start: %d (%d) ", lba, toc->track[track - 1].lba); + printf("Track Size: %d\n", track_size); fflush(stdout); #endif @@ -1005,7 +1011,7 @@ static int cdrom_read_track_info(libretro_vfs_implementation_file *stream, unsig int cdrom_set_read_speed(libretro_vfs_implementation_file *stream, unsigned speed) { /* MMC Command: SET CD SPEED */ - unsigned char cmd[] = {0xBB, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + unsigned char cmd[] = {0xBB, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; cmd[2] = (speed >> 24) & 0xFF; cmd[3] = (speed >> 16) & 0xFF; @@ -1329,24 +1335,35 @@ struct string_list* cdrom_get_available_drives(void) #if defined(__linux__) && !defined(ANDROID) struct string_list *dir_list = dir_list_new("/dev", NULL, false, false, false, false); int i; + bool found = false; if (!dir_list) return list; for (i = 0; i < (int)dir_list->size; i++) { - if (strstr(dir_list->elems[i].data, "/dev/sg")) + if (string_starts_with_size(dir_list->elems[i].data, "/dev/sg", + STRLEN_CONST("/dev/sg"))) { - 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(dir_list->elems[i].data, RETRO_VFS_FILE_ACCESS_READ, 0); libretro_vfs_implementation_file *stream; - bool is_cdrom = false; + 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( + dir_list->elems[i].data, RETRO_VFS_FILE_ACCESS_READ, 0); + bool is_cdrom = false; + + found = true; if (!file) + { +#ifdef CDROM_DEBUG + printf("[CDROM] Could not open %s, please check permissions.\n", dir_list->elems[i].data); + fflush(stdout); +#endif continue; + } stream = filestream_get_vfs_handle(file); cdrom_get_inquiry(stream, drive_model, sizeof(drive_model), &is_cdrom); @@ -1355,10 +1372,11 @@ struct string_list* cdrom_get_available_drives(void) if (!is_cdrom) continue; - sscanf(dir_list->elems[i].data + strlen("/dev/sg"), "%d", &dev_index); + sscanf(dir_list->elems[i].data + STRLEN_CONST("/dev/sg"), + "%d", &dev_index); dev_index = '0' + dev_index; - attr.i = dev_index; + attr.i = dev_index; if (!string_is_empty(drive_model)) strlcat(drive_string, drive_model, sizeof(drive_string)); @@ -1369,6 +1387,56 @@ struct string_list* cdrom_get_available_drives(void) } } + if (!found) + { + char *buf = NULL; + int64_t len = 0; + + if (filestream_read_file("/proc/modules", (void**)&buf, &len)) + { +#ifdef CDROM_DEBUG + bool found = false; +#endif + struct string_list *mods = string_split(buf, "\n"); + + if (mods) + { + for (i = 0; i < mods->size; i++) + { + if (strcasestr(mods->elems[i].data, "sg ")) + { +#ifdef CDROM_DEBUG + found = true; +#endif + break; + } + } + + string_list_free(mods); + } + +#ifdef CDROM_DEBUG + if (found) + { + printf("[CDROM] No sg devices found but kernel module is loaded.\n"); + fflush(stdout); + } + else + { + printf("[CDROM] No sg devices found and sg kernel module is not loaded.\n"); + fflush(stdout); + } +#endif + } +#ifdef CDROM_DEBUG + else + { + printf("[CDROM] No sg devices found, could not check if sg kernel module is loaded.\n"); + fflush(stdout); + } +#endif + } + string_list_free(dir_list); #endif #if defined(_WIN32) && !defined(_XBOX) @@ -1444,9 +1512,7 @@ bool cdrom_is_media_inserted(libretro_vfs_implementation_file *stream) bool cdrom_drive_has_media(const char drive) { RFILE *file; - char cdrom_path_bin[256]; - - cdrom_path_bin[0] = '\0'; + char cdrom_path_bin[256] = {0}; cdrom_device_fillpath(cdrom_path_bin, sizeof(cdrom_path_bin), drive, 1, false); @@ -1640,8 +1706,11 @@ void cdrom_device_fillpath(char *path, size_t len, char drive, unsigned char tra #ifdef __linux__ pos = strlcpy(path, "cdrom://drive", len); - if (len > pos) + if (len > pos + 1) + { path[pos++] = drive; + path[pos] = '\0'; + } pos = strlcat(path, ".cue", len); #endif @@ -1652,8 +1721,11 @@ void cdrom_device_fillpath(char *path, size_t len, char drive, unsigned char tra #ifdef _WIN32 pos = strlcpy(path, "cdrom://", len); - if (len > pos) + if (len > pos + 1) + { path[pos++] = drive; + path[pos] = '\0'; + } pos += snprintf(path + pos, len - pos, ":/drive-track%02d.bin", track); #else diff --git a/libretro-common/compat/compat_posix_string.c b/libretro-common/compat/compat_posix_string.c index 33a30e57..6a2f07ee 100644 --- a/libretro-common/compat/compat_posix_string.c +++ b/libretro-common/compat/compat_posix_string.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (compat_posix_string.c). diff --git a/libretro-common/compat/compat_snprintf.c b/libretro-common/compat/compat_snprintf.c index b69ad047..5b0a34e0 100644 --- a/libretro-common/compat/compat_snprintf.c +++ b/libretro-common/compat/compat_snprintf.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (compat_snprintf.c). diff --git a/libretro-common/compat/compat_strcasestr.c b/libretro-common/compat/compat_strcasestr.c index 54c93a48..4129dab2 100644 --- a/libretro-common/compat/compat_strcasestr.c +++ b/libretro-common/compat/compat_strcasestr.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (compat_strcasestr.c). diff --git a/libretro-common/compat/compat_strl.c b/libretro-common/compat/compat_strl.c index 94cb39b6..31723107 100644 --- a/libretro-common/compat/compat_strl.c +++ b/libretro-common/compat/compat_strl.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (compat_strl.c). diff --git a/libretro-common/compat/fopen_utf8.c b/libretro-common/compat/fopen_utf8.c index 52b481e7..ea169615 100644 --- a/libretro-common/compat/fopen_utf8.c +++ b/libretro-common/compat/fopen_utf8.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (fopen_utf8.c). @@ -36,9 +36,7 @@ void *fopen_utf8(const char * filename, const char * mode) { -#if defined(_XBOX) - return fopen(filename, mode); -#elif defined(LEGACY_WIN32) +#if defined(LEGACY_WIN32) FILE *ret = NULL; char * filename_local = utf8_to_local_string_alloc(filename); diff --git a/libretro-common/encodings/encoding_utf.c b/libretro-common/encodings/encoding_utf.c index b6ad2f96..190264a1 100644 --- a/libretro-common/encodings/encoding_utf.c +++ b/libretro-common/encodings/encoding_utf.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (encoding_utf.c). @@ -37,6 +37,8 @@ #include #endif +#define UTF8_WALKBYTE(string) (*((*(string))++)) + static unsigned leading_ones(uint8_t c) { unsigned ones = 0; @@ -89,13 +91,14 @@ size_t utf8_conv_utf32(uint32_t *out, size_t out_chars, bool utf16_conv_utf8(uint8_t *out, size_t *out_chars, const uint16_t *in, size_t in_size) { - static uint8_t kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; - size_t out_pos = 0; - size_t in_pos = 0; + size_t out_pos = 0; + size_t in_pos = 0; + static const + uint8_t utf8_limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; for (;;) { - unsigned numAdds; + unsigned num_adds; uint32_t value; if (in_pos == in_size) @@ -124,21 +127,21 @@ bool utf16_conv_utf8(uint8_t *out, size_t *out_chars, value = (((value - 0xD800) << 10) | (c2 - 0xDC00)) + 0x10000; } - for (numAdds = 1; numAdds < 5; numAdds++) - if (value < (((uint32_t)1) << (numAdds * 5 + 6))) + for (num_adds = 1; num_adds < 5; num_adds++) + if (value < (((uint32_t)1) << (num_adds * 5 + 6))) break; if (out) - out[out_pos] = (char)(kUtf8Limits[numAdds - 1] - + (value >> (6 * numAdds))); + out[out_pos] = (char)(utf8_limits[num_adds - 1] + + (value >> (6 * num_adds))); out_pos++; do { - numAdds--; + num_adds--; if (out) out[out_pos] = (char)(0x80 - + ((value >> (6 * numAdds)) & 0x3F)); + + ((value >> (6 * num_adds)) & 0x3F)); out_pos++; - }while (numAdds != 0); + }while (num_adds != 0); } *out_chars = out_pos; @@ -166,13 +169,15 @@ size_t utf8cpy(char *d, size_t d_len, const char *s, size_t chars) while (*sb && chars-- > 0) { sb++; - while ((*sb & 0xC0) == 0x80) sb++; + while ((*sb & 0xC0) == 0x80) + sb++; } if ((size_t)(sb - sb_org) > d_len-1 /* NUL */) { sb = sb_org + d_len-1; - while ((*sb & 0xC0) == 0x80) sb--; + while ((*sb & 0xC0) == 0x80) + sb--; } memcpy(d, sb_org, sb-sb_org); @@ -184,14 +189,18 @@ size_t utf8cpy(char *d, size_t d_len, const char *s, size_t chars) const char *utf8skip(const char *str, size_t chars) { const uint8_t *strb = (const uint8_t*)str; + if (!chars) return str; + do { strb++; - while ((*strb & 0xC0)==0x80) strb++; + while ((*strb & 0xC0)==0x80) + strb++; chars--; - } while(chars); + }while (chars); + return (const char*)strb; } @@ -211,24 +220,22 @@ size_t utf8len(const char *string) return ret; } -#define utf8_walkbyte(string) (*((*(string))++)) - /* Does not validate the input, returns garbage if it's not UTF-8. */ uint32_t utf8_walk(const char **string) { - uint8_t first = utf8_walkbyte(string); + uint8_t first = UTF8_WALKBYTE(string); uint32_t ret = 0; if (first < 128) return first; - ret = (ret << 6) | (utf8_walkbyte(string) & 0x3F); + ret = (ret << 6) | (UTF8_WALKBYTE(string) & 0x3F); if (first >= 0xE0) { - ret = (ret << 6) | (utf8_walkbyte(string) & 0x3F); + ret = (ret << 6) | (UTF8_WALKBYTE(string) & 0x3F); if (first >= 0xF0) { - ret = (ret << 6) | (utf8_walkbyte(string) & 0x3F); + ret = (ret << 6) | (UTF8_WALKBYTE(string) & 0x3F); return ret | (first & 7) << 18; } return ret | (first & 15) << 12; @@ -379,13 +386,13 @@ char* local_to_utf8_string_alloc(const char *str) wchar_t* utf8_to_utf16_string_alloc(const char *str) { #ifdef _WIN32 - int len = 0; - int out_len = 0; + int len = 0; + int out_len = 0; #else - size_t len = 0; + size_t len = 0; size_t out_len = 0; #endif - wchar_t *buf = NULL; + wchar_t *buf = NULL; if (!str || !*str) return NULL; diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index 9b85d768..2f78be5f 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2019 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (file_path.c). @@ -32,8 +32,7 @@ #include #include #include -#define VFS_FRONTEND -#include +#include