Skip to content

Commit

Permalink
Fixed USBExtreme issue
Browse files Browse the repository at this point in the history
  • Loading branch information
AKuHAK committed Aug 28, 2021
1 parent 1cba3f9 commit 755d8f7
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions src/supportbase.c
Expand Up @@ -403,51 +403,47 @@ int sbReadList(base_game_info_t **list, const char *prefix, int *fsize, int *gam
snprintf(path, sizeof(path), "%sul.cfg", prefix);
fd = openFile(path, O_RDONLY);
if (fd >= 0) {
int ulcount;
USBExtreme_game_entry_t GameEntry;

if (count < 0)
count = 0;
size = getFileSize(fd);
*fsize = size;
ulcount = size / sizeof(USBExtreme_game_entry_t);
count += size / sizeof(USBExtreme_game_entry_t);

if (ulcount > 0) {
if ((*list = (base_game_info_t *)malloc(sizeof(base_game_info_t) * ulcount)) != NULL) {
memset(*list, 0, sizeof(base_game_info_t) * ulcount);
if (count > 0) {
if ((*list = (base_game_info_t *)malloc(sizeof(base_game_info_t) * count)) != NULL) {
memset(*list, 0, sizeof(base_game_info_t) * count);

while (size > 0) {
int ulfd = 1;
u8 part;
unsigned int name_checksum;
base_game_info_t *g = &(*list)[id++];

// populate game entry in list even if entry corrupted
read(fd, &GameEntry, sizeof(USBExtreme_game_entry_t));
size -= sizeof(USBExtreme_game_entry_t);

// populate game entry in list only if it has valid magic
if (!memcmp(GameEntry.magic, "ul.", 3)) {
int ulfd = 1;
u8 part;
unsigned int name_checksum;
base_game_info_t *g = &(*list)[id++];
count++;

// to ensure no leaks happen, we copy manually and pad the strings
memcpy(g->name, GameEntry.name, UL_GAME_NAME_MAX);
g->name[UL_GAME_NAME_MAX] = '\0';
memcpy(g->startup, GameEntry.startup, GAME_STARTUP_MAX);
g->startup[GAME_STARTUP_MAX] = '\0';
g->extension[0] = '\0';
g->parts = GameEntry.parts;
g->media = GameEntry.media;
g->format = GAME_FORMAT_USBLD;
g->sizeMB = 0;
name_checksum = USBA_crc32(g->name);

// calculate total size
for (part = 0; part < g->parts && ulfd >= 0; part++) {
snprintf(path, sizeof(path), "%sul.%08X.%s.%02x", prefix, name_checksum, g->startup, part);
ulfd = openFile(path, O_RDONLY);
if (ulfd >= 0) {
g->sizeMB += (getFileSize(ulfd) >> 20);
close(ulfd);
}
// to ensure no leaks happen, we copy manually and pad the strings
memcpy(g->name, GameEntry.name, UL_GAME_NAME_MAX);
g->name[UL_GAME_NAME_MAX] = '\0';
memcpy(g->startup, GameEntry.startup, GAME_STARTUP_MAX);
g->startup[GAME_STARTUP_MAX] = '\0';
g->extension[0] = '\0';
g->parts = GameEntry.parts;
g->media = GameEntry.media;
g->format = GAME_FORMAT_USBLD;
g->sizeMB = 0;
name_checksum = USBA_crc32(g->name);

// calculate total size for individual game
for (part = 0; part < g->parts && ulfd >= 0; part++) {
snprintf(path, sizeof(path), "%sul.%08X.%s.%02x", prefix, name_checksum, g->startup, part);
ulfd = openFile(path, O_RDONLY);
if (ulfd >= 0) {
g->sizeMB += (getFileSize(ulfd) >> 20);
close(ulfd);
}
}
}
Expand Down

0 comments on commit 755d8f7

Please sign in to comment.