dep: Update rcheevos to 0e9eb7c

This commit is contained in:
Connor McLaughlin
2022-03-27 01:01:32 +10:00
parent a55b5022c7
commit 4c4e62cee6
21 changed files with 2155 additions and 715 deletions

View File

@ -19,13 +19,29 @@ extern rc_hash_message_callback verbose_message_callback;
struct cdrom_t
{
void* file_handle;
int sector_size;
int sector_header_size;
int first_sector;
int64_t first_sector_offset;
void* file_handle; /* the file handle for reading the track data */
int sector_size; /* the size of each sector in the track data */
int sector_header_size; /* the offset to the raw data within a sector block */
int64_t file_track_offset;/* the offset of the track data within the file */
int track_first_sector; /* the first absolute sector associated to the track (includes pregap) */
int track_pregap_sectors; /* the number of pregap sectors */
#ifndef NDEBUG
uint32_t track_id; /* the index of the track */
#endif
};
static int cdreader_get_sector(unsigned char header[16])
{
int minutes = (header[12] >> 4) * 10 + (header[12] & 0x0F);
int seconds = (header[13] >> 4) * 10 + (header[13] & 0x0F);
int frames = (header[14] >> 4) * 10 + (header[14] & 0x0F);
/* convert the MSF value to a sector index, and subtract 150 (2 seconds) per:
* For data and mixed mode media (those conforming to ISO/IEC 10149), logical block address
* zero shall be assigned to the block at MSF address 00/02/00 */
return ((minutes * 60) + seconds) * 75 + frames - 150;
}
static void cdreader_determine_sector_size(struct cdrom_t* cdrom)
{
/* Attempt to determine the sector and header sizes. The CUE file may be lying.
@ -38,12 +54,12 @@ static void cdreader_determine_sector_size(struct cdrom_t* cdrom)
};
unsigned char header[32];
const int64_t toc_sector = 16;
const int64_t toc_sector = 16 + cdrom->track_pregap_sectors;
cdrom->sector_size = 0;
cdrom->sector_header_size = 0;
rc_file_seek(cdrom->file_handle, toc_sector * 2352 + cdrom->first_sector_offset, SEEK_SET);
rc_file_seek(cdrom->file_handle, toc_sector * 2352 + cdrom->file_track_offset, SEEK_SET);
if (rc_file_read(cdrom->file_handle, header, sizeof(header)) < sizeof(header))
return;
@ -55,10 +71,12 @@ static void cdreader_determine_sector_size(struct cdrom_t* cdrom)
cdrom->sector_header_size = 24;
else
cdrom->sector_header_size = 16;
cdrom->track_first_sector = cdreader_get_sector(header) - (int)toc_sector;
}
else
{
rc_file_seek(cdrom->file_handle, toc_sector * 2336 + cdrom->first_sector_offset, SEEK_SET);
rc_file_seek(cdrom->file_handle, toc_sector * 2336 + cdrom->file_track_offset, SEEK_SET);
rc_file_read(cdrom->file_handle, header, sizeof(header));
if (memcmp(header, sync_pattern, 12) == 0)
@ -69,10 +87,12 @@ static void cdreader_determine_sector_size(struct cdrom_t* cdrom)
cdrom->sector_header_size = 24;
else
cdrom->sector_header_size = 16;
cdrom->track_first_sector = cdreader_get_sector(header) - (int)toc_sector;
}
else
{
rc_file_seek(cdrom->file_handle, toc_sector * 2048 + cdrom->first_sector_offset, SEEK_SET);
rc_file_seek(cdrom->file_handle, toc_sector * 2048 + cdrom->file_track_offset, SEEK_SET);
rc_file_read(cdrom->file_handle, header, sizeof(header));
if (memcmp(&header[1], "CD001", 5) == 0)
@ -103,15 +123,18 @@ static void* cdreader_open_bin_track(const char* path, uint32_t track)
cdrom = (struct cdrom_t*)calloc(1, sizeof(*cdrom));
cdrom->file_handle = file_handle;
#ifndef NDEBUG
cdrom->track_id = track;
#endif
cdreader_determine_sector_size(cdrom);
if (cdrom->sector_size == 0)
{
size_t size;
int64_t size;
rc_file_seek(cdrom->file_handle, 0, SEEK_END);
size = ftell(cdrom->file_handle);
size = rc_file_tell(cdrom->file_handle);
if ((size % 2352) == 0)
{
@ -192,6 +215,11 @@ static int cdreader_open_bin(struct cdrom_t* cdrom, const char* path, const char
cdrom->sector_size = 2352;
cdrom->sector_header_size = 16;
}
else if (memcmp(mode, "AUDIO", 5) == 0)
{
cdrom->sector_size = 2352;
cdrom->sector_header_size = 0;
}
}
return (cdrom->sector_size != 0);
@ -226,7 +254,12 @@ static int64_t cdreader_get_bin_size(const char* cue_path, const char* bin_name)
char* bin_filename = cdreader_get_bin_path(cue_path, bin_name);
if (bin_filename)
{
void* file_handle = rc_file_open(bin_filename);
/* disable verbose messaging while getting file size */
rc_hash_message_callback old_verbose_message_callback = verbose_message_callback;
void* file_handle;
verbose_message_callback = NULL;
file_handle = rc_file_open(bin_filename);
if (file_handle)
{
rc_file_seek(file_handle, 0, SEEK_END);
@ -234,6 +267,7 @@ static int64_t cdreader_get_bin_size(const char* cue_path, const char* bin_name)
rc_file_close(file_handle);
}
verbose_message_callback = old_verbose_message_callback;
free(bin_filename);
}
@ -242,38 +276,40 @@ static int64_t cdreader_get_bin_size(const char* cue_path, const char* bin_name)
static void* cdreader_open_cue_track(const char* path, uint32_t track)
{
void* file_handle;
int64_t file_offset = 0;
char buffer[1024], mode[16];
char* bin_filename;
char file[256];
void* cue_handle;
int64_t cue_offset = 0;
char buffer[1024];
char* bin_filename = NULL;
char *ptr, *ptr2, *end;
int current_track = 0;
int sector_size = 0;
int track_first_sector = 0;
int previous_sector_size = 0;
int previous_index_sector_offset = 0;
int previous_track_is_data = 0;
int64_t previous_track_sector_offset = 0;
char previous_track_mode[16];
int largest_track = 0;
int largest_track_sector_count = 0;
int64_t largest_track_offset = 0;
char largest_track_mode[16];
char largest_track_file[256];
int64_t offset = 0;
int done = 0;
size_t num_read = 0;
struct cdrom_t* cdrom = NULL;
file_handle = rc_file_open(path);
if (!file_handle)
struct track_t
{
uint32_t id;
int sector_size;
int sector_count;
int first_sector;
int pregap_sectors;
int is_data;
int file_track_offset;
int file_first_sector;
char mode[16];
char filename[256];
} current_track, previous_track, largest_track;
cue_handle = rc_file_open(path);
if (!cue_handle)
return NULL;
file[0] = '\0';
memset(&current_track, 0, sizeof(current_track));
memset(&previous_track, 0, sizeof(previous_track));
memset(&largest_track, 0, sizeof(largest_track));
do
{
num_read = rc_file_read(file_handle, buffer, sizeof(buffer) - 1);
num_read = rc_file_read(cue_handle, buffer, sizeof(buffer) - 1);
if (num_read == 0)
break;
@ -305,58 +341,51 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
/* convert mm:ss:ff to sector count */
sscanf(ptr, "%d:%d:%d", &m, &s, &f);
sector_offset = ((m * 60) + s) * 75 + f;
sector_offset -= previous_index_sector_offset;
if (index == 1)
track_first_sector += sector_offset;
/* if looking for the largest data track, determine previous track size */
if (index == 1 && track == RC_HASH_CDTRACK_LARGEST && previous_track_is_data)
if (current_track.first_sector == -1)
{
if (sector_offset > largest_track_sector_count)
current_track.first_sector = sector_offset;
if (strcmp(current_track.filename, previous_track.filename) == 0)
{
largest_track_sector_count = sector_offset;
largest_track_offset = previous_track_sector_offset;
largest_track = current_track - 1;
memcpy(largest_track_mode, previous_track_mode, sizeof(largest_track_mode));
strcpy(largest_track_file, file);
previous_track.sector_count = current_track.first_sector - previous_track.first_sector;
current_track.file_track_offset += previous_track.sector_count * previous_track.sector_size;
}
/* if looking for the largest data track, determine previous track size */
if (track == RC_HASH_CDTRACK_LARGEST && previous_track.sector_count > largest_track.sector_count &&
previous_track.is_data)
{
memcpy(&largest_track, &previous_track, sizeof(largest_track));
}
}
/* calculate the true offset and update the counters for the next INDEX marker */
offset += (int64_t)sector_offset * previous_sector_size;
previous_sector_size = sector_size;
previous_index_sector_offset += sector_offset;
if (index == 1)
{
current_track.pregap_sectors = (sector_offset - current_track.first_sector);
if (verbose_message_callback)
{
char message[128];
char* scan = mode;
char* scan = current_track.mode;
while (*scan && !isspace((unsigned char)*scan))
++scan;
*scan = '\0';
/* it's undesirable to truncate offset to 32-bits, but %lld isn't defined in c89. */
snprintf(message, sizeof(message), "Found %s track %d (sector size %d, track starts at %d)",
mode, current_track, sector_size, (int)offset);
snprintf(message, sizeof(message), "Found %s track %d (first sector %d, sector size %d, %d pregap sectors)",
current_track.mode, current_track.id, current_track.first_sector, current_track.sector_size, current_track.pregap_sectors);
verbose_message_callback(message);
}
if (current_track == (int)track)
if (current_track.id == track)
{
done = 1;
break;
}
memcpy(previous_track_mode, mode, sizeof(previous_track_mode));
previous_track_is_data = (memcmp(mode, "MODE", 4) == 0);
previous_track_sector_offset = offset;
if (previous_track_is_data && track == RC_HASH_CDTRACK_FIRST_DATA)
if (track == RC_HASH_CDTRACK_FIRST_DATA && current_track.is_data)
{
track = current_track;
track = current_track.id;
done = 1;
break;
}
@ -364,49 +393,57 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
}
else if (strncasecmp(ptr, "TRACK ", 6) == 0)
{
if (current_track.sector_size)
memcpy(&previous_track, &current_track, sizeof(current_track));
ptr += 6;
current_track = atoi(ptr);
current_track.id = atoi(ptr);
current_track.pregap_sectors = -1;
current_track.first_sector = -1;
while (*ptr != ' ')
++ptr;
while (*ptr == ' ')
++ptr;
memcpy(mode, ptr, sizeof(mode));
memcpy(current_track.mode, ptr, sizeof(current_track.mode));
current_track.is_data = (memcmp(current_track.mode, "MODE", 4) == 0);
previous_sector_size = sector_size;
if (memcmp(mode, "MODE", 4) == 0)
if (current_track.is_data)
{
sector_size = atoi(ptr + 6);
current_track.sector_size = atoi(ptr + 6);
}
else
{
/* assume AUDIO */
sector_size = 2352;
current_track.sector_size = 2352;
}
}
else if (strncasecmp(ptr, "FILE ", 5) == 0)
{
if (previous_sector_size > 0)
if (current_track.sector_size)
{
/* determine previous track size */
int sector_count = (int)cdreader_get_bin_size(path, file) / previous_sector_size;
track_first_sector += sector_count;
memcpy(&previous_track, &current_track, sizeof(previous_track));
if (previous_track.sector_count == 0)
{
const uint32_t file_sector_count = (uint32_t)cdreader_get_bin_size(path, previous_track.filename) / previous_track.sector_size;
previous_track.sector_count = file_sector_count - previous_track.first_sector;
}
/* if looking for the largest data track, check to see if this one is larger */
if (track == RC_HASH_CDTRACK_LARGEST && previous_track_is_data)
if (track == RC_HASH_CDTRACK_LARGEST && previous_track.is_data &&
previous_track.sector_count > largest_track.sector_count)
{
if (sector_count > largest_track_sector_count)
{
largest_track_sector_count = sector_count;
largest_track_offset = previous_track_sector_offset;
largest_track = current_track;
memcpy(largest_track_mode, previous_track_mode, sizeof(largest_track_mode));
strcpy(largest_track_file, file);
}
memcpy(&largest_track, &previous_track, sizeof(largest_track));
}
}
memset(&current_track, 0, sizeof(current_track));
current_track.file_first_sector = previous_track.file_first_sector +
previous_track.first_sector + previous_track.sector_count;
ptr += 5;
ptr2 = ptr;
if (*ptr == '"')
@ -425,20 +462,8 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
} while (*ptr2 && *ptr2 != '\n' && *ptr2 != ' ');
}
if (ptr2 - ptr < (int)sizeof(file))
{
memcpy(file, ptr, ptr2 - ptr);
file[ptr2 - ptr] = '\0';
}
else
{
file[0] = '\0';
}
current_track = 0;
previous_sector_size = 0;
previous_index_sector_offset = 0;
offset = 0;
if (ptr2 - ptr < (int)sizeof(current_track.filename))
memcpy(current_track.filename, ptr, ptr2 - ptr);
}
while (*ptr && *ptr != '\n')
@ -448,42 +473,36 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
if (done)
break;
file_offset += (ptr - buffer);
rc_file_seek(file_handle, file_offset, SEEK_SET);
cue_offset += (ptr - buffer);
rc_file_seek(cue_handle, cue_offset, SEEK_SET);
} while (1);
rc_file_close(file_handle);
rc_file_close(cue_handle);
if (track == RC_HASH_CDTRACK_LARGEST)
{
previous_track_is_data = (memcmp(mode, "MODE", 4) == 0);
if (previous_track_is_data)
if (current_track.sector_size && current_track.is_data)
{
int sector_count = (int)cdreader_get_bin_size(path, file) / previous_sector_size;
sector_count -= previous_index_sector_offset;
const uint32_t file_sector_count = (uint32_t)cdreader_get_bin_size(path, current_track.filename) / current_track.sector_size;
current_track.sector_count = file_sector_count - current_track.first_sector;
if (sector_count > largest_track_sector_count)
{
largest_track_sector_count = sector_count;
largest_track_offset = previous_track_sector_offset;
largest_track = current_track;
memcpy(largest_track_mode, previous_track_mode, sizeof(largest_track_mode));
strcpy(largest_track_file, file);
}
if (largest_track.sector_count > current_track.sector_count)
memcpy(&current_track, &largest_track, sizeof(current_track));
}
else
{
memcpy(&current_track, &largest_track, sizeof(current_track));
}
if (largest_track > 0)
{
current_track = largest_track;
track = (uint32_t)largest_track;
offset = largest_track_offset;
memcpy(mode, largest_track_mode, sizeof(mode));
strcpy(file, largest_track_file);
}
track = current_track.id;
}
else if (track == RC_HASH_CDTRACK_LAST && !done)
{
track = current_track.id;
}
if (current_track == (int)track)
if (current_track.id == track)
{
cdrom = (struct cdrom_t*)calloc(1, sizeof(*cdrom));
if (!cdrom)
@ -493,20 +512,24 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
return NULL;
}
cdrom->first_sector_offset = offset;
cdrom->first_sector = track_first_sector;
cdrom->file_track_offset = current_track.file_track_offset;
cdrom->track_pregap_sectors = current_track.pregap_sectors;
cdrom->track_first_sector = current_track.file_first_sector + current_track.first_sector;
#ifndef NDEBUG
cdrom->track_id = current_track.id;
#endif
/* verify existance of bin file */
bin_filename = cdreader_get_bin_path(path, file);
bin_filename = cdreader_get_bin_path(path, current_track.filename);
if (bin_filename)
{
if (cdreader_open_bin(cdrom, bin_filename, mode))
if (cdreader_open_bin(cdrom, bin_filename, current_track.mode))
{
if (verbose_message_callback)
{
if (cdrom->first_sector_offset)
snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d, track starts at %d)",
track, cdrom->sector_size, (int)cdrom->first_sector_offset);
if (cdrom->track_pregap_sectors)
snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d, %d pregap sectors)",
track, cdrom->sector_size, cdrom->track_pregap_sectors);
else
snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d)", track, cdrom->sector_size);
@ -515,7 +538,16 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
}
else
{
snprintf((char*)buffer, sizeof(buffer), "Could not open %s", bin_filename);
if (cdrom->file_handle)
{
rc_file_close(cdrom->file_handle);
snprintf((char*)buffer, sizeof(buffer), "Could not determine sector size for %s track", current_track.mode);
}
else
{
snprintf((char*)buffer, sizeof(buffer), "Could not open %s", bin_filename);
}
rc_hash_error((const char*)buffer);
free(cdrom);
@ -593,7 +625,7 @@ static void* cdreader_open_gdi_track(const char* path, uint32_t track)
++ptr;
current_track = (uint32_t)atoi(ptr);
if (track && current_track != track)
if (track && current_track != track && track != RC_HASH_CDTRACK_FIRST_DATA)
continue;
while (isdigit((unsigned char)*ptr))
@ -643,11 +675,17 @@ static void* cdreader_open_gdi_track(const char* path, uint32_t track)
}
*ptr2 = '\0';
if (track == current_track || (track == RC_HASH_CDTRACK_FIRST_DATA && track_type == 4))
if (track == current_track)
{
found = 1;
break;
}
else if (track == RC_HASH_CDTRACK_FIRST_DATA && track_type == 4)
{
track = current_track;
found = 1;
break;
}
else if (track == RC_HASH_CDTRACK_LARGEST && track_type == 4)
{
track_size = cdreader_get_bin_size(path, file);
@ -700,12 +738,15 @@ static void* cdreader_open_gdi_track(const char* path, uint32_t track)
bin_path = cdreader_get_bin_path(path, file);
if (cdreader_open_bin(cdrom, bin_path, mode))
{
cdrom->first_sector_offset = 0;
cdrom->first_sector = lba;
cdrom->track_pregap_sectors = 0;
cdrom->track_first_sector = lba;
#ifndef NDEBUG
cdrom->track_id = current_track;
#endif
if (verbose_message_callback)
{
snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d)", track, cdrom->sector_size);
snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d)", current_track, cdrom->sector_size);
verbose_message_callback((const char*)buffer);
}
}
@ -747,7 +788,11 @@ static size_t cdreader_read_sector(void* track_handle, uint32_t sector, void* bu
if (!cdrom)
return 0;
sector_start = (int64_t)sector * cdrom->sector_size + cdrom->sector_header_size + cdrom->first_sector_offset;
if (sector < (uint32_t)cdrom->track_first_sector)
return 0;
sector_start = (int64_t)(sector - cdrom->track_first_sector) * cdrom->sector_size +
cdrom->sector_header_size + cdrom->file_track_offset;
while (requested_bytes > 2048)
{
@ -782,23 +827,26 @@ static void cdreader_close_track(void* track_handle)
}
}
static uint32_t cdreader_absolute_sector_to_track_sector(void* track_handle, uint32_t sector)
static uint32_t cdreader_first_track_sector(void* track_handle)
{
struct cdrom_t* cdrom = (struct cdrom_t*)track_handle;
if (cdrom)
return sector - cdrom->first_sector;
return cdrom->track_first_sector + cdrom->track_pregap_sectors;
return 0;
}
void rc_hash_get_default_cdreader(struct rc_hash_cdreader* cdreader)
{
cdreader->open_track = cdreader_open_track;
cdreader->read_sector = cdreader_read_sector;
cdreader->close_track = cdreader_close_track;
cdreader->first_track_sector = cdreader_first_track_sector;
}
void rc_hash_init_default_cdreader()
{
struct rc_hash_cdreader cdreader;
cdreader.open_track = cdreader_open_track;
cdreader.read_sector = cdreader_read_sector;
cdreader.close_track = cdreader_close_track;
cdreader.absolute_sector_to_track_sector = cdreader_absolute_sector_to_track_sector;
rc_hash_get_default_cdreader(&cdreader);
rc_hash_init_custom_cdreader(&cdreader);
}

View File

@ -83,6 +83,12 @@ static void filereader_close(void* file_handle)
fclose((FILE*)file_handle);
}
/* for unit tests - normally would call rc_hash_init_custom_filereader(NULL) */
void rc_hash_reset_filereader()
{
filereader = NULL;
}
void rc_hash_init_custom_filereader(struct rc_hash_filereader* reader)
{
/* initialize with defaults first */
@ -193,13 +199,13 @@ static size_t rc_cd_read_sector(void* track_handle, uint32_t sector, void* buffe
return 0;
}
static uint32_t rc_cd_absolute_sector_to_track_sector(void* track_handle, uint32_t sector)
static uint32_t rc_cd_first_track_sector(void* track_handle)
{
if (cdreader && cdreader->absolute_sector_to_track_sector)
return cdreader->absolute_sector_to_track_sector(track_handle, sector);
if (cdreader && cdreader->first_track_sector)
return cdreader->first_track_sector(track_handle);
rc_hash_error("no hook registered for cdreader_absolute_sector_to_track_sector");
return sector;
rc_hash_error("no hook registered for cdreader_first_track_sector");
return 0;
}
static void rc_cd_close_track(void* track_handle)
@ -242,7 +248,7 @@ static uint32_t rc_cd_find_file_sector(void* track_handle, const char* path, uns
else
{
/* find the cd information */
if (!rc_cd_read_sector(track_handle, 16, buffer, 256))
if (!rc_cd_read_sector(track_handle, rc_cd_first_track_sector(track_handle) + 16, buffer, 256))
return 0;
/* the directory_record starts at 156, the sector containing the table of contents is 2 bytes into that.
@ -252,7 +258,6 @@ static uint32_t rc_cd_find_file_sector(void* track_handle, const char* path, uns
}
/* fetch and process the directory record */
sector = rc_cd_absolute_sector_to_track_sector(track_handle, sector);
if (!rc_cd_read_sector(track_handle, sector, buffer, sizeof(buffer)))
return 0;
@ -405,7 +410,7 @@ static int rc_hash_cd_file(md5_state_t* md5, void* track_handle, uint32_t sector
if (name)
snprintf(message, sizeof(message), "Hashing %s title (%u bytes) and contents (%u bytes) ", name, (unsigned)strlen(name), size);
else
snprintf(message, sizeof(message), "Hashing %s contents (%u bytes)", description, size);
snprintf(message, sizeof(message), "Hashing %s contents (%u bytes @ sector %u)", description, size, sector);
verbose_message_callback(message);
}
@ -648,6 +653,37 @@ static int rc_hash_arcade(char hash[33], const char* path)
return rc_hash_buffer(hash, (uint8_t*)filename, filename_length);
}
static int rc_hash_text(char hash[33], const uint8_t* buffer, size_t buffer_size)
{
md5_state_t md5;
const uint8_t* scan = buffer;
const uint8_t* stop = buffer + buffer_size;
md5_init(&md5);
do {
/* find end of line */
while (scan < stop && *scan != '\r' && *scan != '\n')
++scan;
md5_append(&md5, buffer, (int)(scan - buffer));
/* include a normalized line ending */
/* NOTE: this causes a line ending to be hashed at the end of the file, even if one was not present */
md5_append(&md5, (const uint8_t*)"\n", 1);
/* skip newline */
if (scan < stop && *scan == '\r')
++scan;
if (scan < stop && *scan == '\n')
++scan;
buffer = scan;
} while (scan < stop);
return rc_hash_finalize(&md5, hash);
}
static int rc_hash_lynx(char hash[33], const uint8_t* buffer, size_t buffer_size)
{
/* if the file contains a header, ignore it */
@ -711,86 +747,7 @@ static void rc_hash_n64_to_z64(uint8_t* buffer, const uint8_t* stop)
}
}
static int rc_hash_n64(char hash[33], const uint8_t* buffer, size_t buffer_size)
{
uint8_t* swapbuffer;
uint8_t* stop;
const size_t swapbuffer_size = 65536;
md5_state_t md5;
size_t remaining;
int is_v64;
if (buffer[0] == 0x80) /* z64 format (big endian [native]) */
{
return rc_hash_buffer(hash, buffer, buffer_size);
}
else if (buffer[0] == 0x37) /* v64 format (byteswapped) */
{
rc_hash_verbose("converting v64 to z64");
is_v64 = 1;
}
else if (buffer[0] == 0x40) /* n64 format (little endian) */
{
rc_hash_verbose("converting n64 to z64");
is_v64 = 0;
}
else
{
rc_hash_verbose("Not a Nintendo 64 ROM");
return 0;
}
swapbuffer = (uint8_t*)malloc(swapbuffer_size);
if (!swapbuffer)
return rc_hash_error("Could not allocate temporary buffer");
stop = swapbuffer + swapbuffer_size;
md5_init(&md5);
if (buffer_size > MAX_BUFFER_SIZE)
remaining = MAX_BUFFER_SIZE;
else
remaining = (size_t)buffer_size;
if (verbose_message_callback)
{
char message[64];
snprintf(message, sizeof(message), "Hashing %u bytes", (unsigned)remaining);
verbose_message_callback(message);
}
while (remaining >= swapbuffer_size)
{
memcpy(swapbuffer, buffer, swapbuffer_size);
if (is_v64)
rc_hash_v64_to_z64(swapbuffer, stop);
else
rc_hash_n64_to_z64(swapbuffer, stop);
md5_append(&md5, swapbuffer, (int)swapbuffer_size);
buffer += swapbuffer_size;
remaining -= swapbuffer_size;
}
if (remaining > 0)
{
memcpy(swapbuffer, buffer, remaining);
stop = swapbuffer + remaining;
if (is_v64)
rc_hash_v64_to_z64(swapbuffer, stop);
else
rc_hash_n64_to_z64(swapbuffer, stop);
md5_append(&md5, swapbuffer, (int)remaining);
}
free(swapbuffer);
return rc_hash_finalize(&md5, hash);
}
static int rc_hash_n64_file(char hash[33], const char* path)
static int rc_hash_n64(char hash[33], const char* path)
{
uint8_t* buffer;
uint8_t* stop;
@ -1026,7 +983,7 @@ static int rc_hash_pce_track(char hash[33], void* track_handle)
* the string "PC Engine CD-ROM SYSTEM" should exist at 32 bytes into the sector
* http://shu.sheldows.com/shu/download/pcedocs/pce_cdrom.html
*/
if (rc_cd_read_sector(track_handle, 1, buffer, 128) < 128)
if (rc_cd_read_sector(track_handle, rc_cd_first_track_sector(track_handle) + 1, buffer, 128) < 128)
{
return rc_hash_error("Not a PC Engine CD");
}
@ -1059,6 +1016,7 @@ static int rc_hash_pce_track(char hash[33], void* track_handle)
verbose_message_callback(message);
}
sector += rc_cd_first_track_sector(track_handle);
while (num_sectors > 0)
{
rc_cd_read_sector(track_handle, sector, buffer, sizeof(buffer));
@ -1122,7 +1080,8 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
return rc_hash_error("Could not open track");
/* PC-FX CD will have a header marker in sector 0 */
rc_cd_read_sector(track_handle, 0, buffer, 32);
sector = rc_cd_first_track_sector(track_handle);
rc_cd_read_sector(track_handle, sector, buffer, 32);
if (memcmp("PC-FX:Hu_CD-ROM", &buffer[0], 15) != 0)
{
rc_cd_close_track(track_handle);
@ -1132,7 +1091,8 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
if (!track_handle)
return rc_hash_error("Could not open track");
rc_cd_read_sector(track_handle, 0, buffer, 32);
sector = rc_cd_first_track_sector(track_handle);
rc_cd_read_sector(track_handle, sector, buffer, 32);
}
if (memcmp("PC-FX:Hu_CD-ROM", &buffer[0], 15) == 0)
@ -1140,7 +1100,7 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
/* PC-FX boot header fills the first two sectors of the disc
* https://bitbucket.org/trap15/pcfxtools/src/master/pcfx-cdlink.c
* the important stuff is the first 128 bytes of the second sector (title being the first 32) */
rc_cd_read_sector(track_handle, 1, buffer, 128);
rc_cd_read_sector(track_handle, sector + 1, buffer, 128);
md5_init(&md5);
md5_append(&md5, buffer, 128);
@ -1166,6 +1126,7 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
verbose_message_callback(message);
}
sector += rc_cd_first_track_sector(track_handle);
while (num_sectors > 0)
{
rc_cd_read_sector(track_handle, sector, buffer, sizeof(buffer));
@ -1178,7 +1139,7 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
else
{
int result = 0;
rc_cd_read_sector(track_handle, 1, buffer, 128);
rc_cd_read_sector(track_handle, sector + 1, buffer, 128);
/* some PC-FX CDs still identify as PCE CDs */
if (memcmp("PC Engine CD-ROM SYSTEM", &buffer[32], 23) == 0)
@ -1198,30 +1159,41 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
static int rc_hash_dreamcast(char hash[33], const char* path)
{
uint8_t buffer[256];
uint8_t buffer[256] = "";
void* track_handle;
void* last_track_handle;
char exe_file[32] = "";
unsigned size;
uint32_t sector;
uint32_t track_sector;
int result = 0;
md5_state_t md5;
int i = 0;
/* track 03 is the data track that contains the TOC and IP.BIN */
track_handle = rc_cd_open_track(path, 3);
if (!track_handle)
return rc_hash_error("Could not open track");
/* first 256 bytes from first sector should have IP.BIN structure that stores game meta information
* https://mc.pp.se/dc/ip.bin.html */
rc_cd_read_sector(track_handle, 0, buffer, sizeof(buffer));
if (track_handle)
{
/* first 256 bytes from first sector should have IP.BIN structure that stores game meta information
* https://mc.pp.se/dc/ip.bin.html */
rc_cd_read_sector(track_handle, rc_cd_first_track_sector(track_handle), buffer, sizeof(buffer));
}
if (memcmp(&buffer[0], "SEGA SEGAKATANA ", 16) != 0)
{
rc_cd_close_track(track_handle);
return rc_hash_error("Not a Dreamcast CD");
if (track_handle)
rc_cd_close_track(track_handle);
/* not a gd-rom dreamcast file. check for mil-cd by looking for the marker in the first data track */
track_handle = rc_cd_open_track(path, RC_HASH_CDTRACK_FIRST_DATA);
if (!track_handle)
return rc_hash_error("Could not open track");
rc_cd_read_sector(track_handle, rc_cd_first_track_sector(track_handle), buffer, sizeof(buffer));
if (memcmp(&buffer[0], "SEGA SEGAKATANA ", 16) != 0)
{
/* did not find marker on track 3 or first data track */
rc_cd_close_track(track_handle);
return rc_hash_error("Not a Dreamcast CD");
}
}
/* start the hash with the game meta information */
@ -1258,30 +1230,26 @@ static int rc_hash_dreamcast(char hash[33], const char* path)
exe_file[i] = '\0';
sector = rc_cd_find_file_sector(track_handle, exe_file, &size);
rc_cd_close_track(track_handle);
if (sector == 0)
return rc_hash_error("Could not locate boot executable");
/* last track contains the boot executable */
last_track_handle = rc_cd_open_track(path, RC_HASH_CDTRACK_LAST);
track_sector = rc_cd_absolute_sector_to_track_sector(last_track_handle, sector);
if ((int32_t)track_sector < 0)
{
/* boot executable is not in the last track; try the primary data track.
* There's only a handful of games that do this: Q*bert was the first identified. */
rc_cd_close_track(last_track_handle);
rc_hash_verbose("Boot executable not found in last track, trying primary track");
last_track_handle = rc_cd_open_track(path, 3);
track_sector = rc_cd_absolute_sector_to_track_sector(last_track_handle, sector);
rc_cd_close_track(track_handle);
return rc_hash_error("Could not locate boot executable");
}
result = rc_hash_cd_file(&md5, last_track_handle, track_sector, NULL, size, "boot executable");
if (rc_cd_read_sector(track_handle, sector, buffer, 1))
{
/* the boot executable is in the primary data track */
}
else
{
rc_cd_close_track(track_handle);
rc_cd_close_track(last_track_handle);
/* the boot executable is normally in the last track */
track_handle = rc_cd_open_track(path, RC_HASH_CDTRACK_LAST);
}
result = rc_hash_cd_file(&md5, track_handle, sector, NULL, size, "boot executable");
rc_cd_close_track(track_handle);
rc_hash_finalize(&md5, hash);
return result;
@ -1547,6 +1515,84 @@ static int rc_hash_snes(char hash[33], const uint8_t* buffer, size_t buffer_size
return rc_hash_buffer(hash, buffer, buffer_size);
}
struct rc_buffered_file
{
const uint8_t* read_ptr;
const uint8_t* data;
size_t data_size;
};
static struct rc_buffered_file rc_buffered_file;
static void* rc_file_open_buffered_file(const char* path)
{
struct rc_buffered_file* handle = (struct rc_buffered_file*)malloc(sizeof(struct rc_buffered_file));
memcpy(handle, &rc_buffered_file, sizeof(rc_buffered_file));
return handle;
}
void rc_file_seek_buffered_file(void* file_handle, int64_t offset, int origin)
{
struct rc_buffered_file* buffered_file = (struct rc_buffered_file*)file_handle;
switch (origin)
{
case SEEK_SET: buffered_file->read_ptr = buffered_file->data + offset; break;
case SEEK_CUR: buffered_file->read_ptr += offset; break;
case SEEK_END: buffered_file->read_ptr = buffered_file->data + buffered_file->data_size - offset; break;
}
if (buffered_file->read_ptr < buffered_file->data)
buffered_file->read_ptr = buffered_file->data;
else if (buffered_file->read_ptr > buffered_file->data + buffered_file->data_size)
buffered_file->read_ptr = buffered_file->data + buffered_file->data_size;
}
int64_t rc_file_tell_buffered_file(void* file_handle)
{
struct rc_buffered_file* buffered_file = (struct rc_buffered_file*)file_handle;
return (buffered_file->read_ptr - buffered_file->data);
}
size_t rc_file_read_buffered_file(void* file_handle, void* buffer, size_t requested_bytes)
{
struct rc_buffered_file* buffered_file = (struct rc_buffered_file*)file_handle;
const int64_t remaining = buffered_file->data_size - (buffered_file->read_ptr - buffered_file->data);
if ((int)requested_bytes > remaining)
requested_bytes = (int)remaining;
memcpy(buffer, buffered_file->read_ptr, requested_bytes);
buffered_file->read_ptr += requested_bytes;
return requested_bytes;
}
void rc_file_close_buffered_file(void* file_handle)
{
free(file_handle);
}
static int rc_hash_file_from_buffer(char hash[33], int console_id, const uint8_t* buffer, size_t buffer_size)
{
struct rc_hash_filereader buffered_filereader_funcs;
struct rc_hash_filereader* old_filereader = filereader;
int result;
memset(&buffered_filereader_funcs, 0, sizeof(buffered_filereader_funcs));
buffered_filereader_funcs.open = rc_file_open_buffered_file;
buffered_filereader_funcs.close = rc_file_close_buffered_file;
buffered_filereader_funcs.read = rc_file_read_buffered_file;
buffered_filereader_funcs.seek = rc_file_seek_buffered_file;
buffered_filereader_funcs.tell = rc_file_tell_buffered_file;
filereader = &buffered_filereader_funcs;
rc_buffered_file.data = rc_buffered_file.read_ptr = buffer;
rc_buffered_file.data_size = buffer_size;
result = rc_hash_generate_from_file(hash, console_id, "[buffered file]");
filereader = old_filereader;
return result;
}
int rc_hash_generate_from_buffer(char hash[33], int console_id, const uint8_t* buffer, size_t buffer_size)
{
switch (console_id)
@ -1558,6 +1604,7 @@ int rc_hash_generate_from_buffer(char hash[33], int console_id, const uint8_t* b
return rc_hash_error(message);
}
case RC_CONSOLE_AMSTRAD_PC:
case RC_CONSOLE_APPLE_II:
case RC_CONSOLE_ATARI_2600:
case RC_CONSOLE_ATARI_JAGUAR:
@ -1570,6 +1617,7 @@ int rc_hash_generate_from_buffer(char hash[33], int console_id, const uint8_t* b
case RC_CONSOLE_MAGNAVOX_ODYSSEY2:
case RC_CONSOLE_MASTER_SYSTEM:
case RC_CONSOLE_MEGA_DRIVE:
case RC_CONSOLE_MEGADUCK:
case RC_CONSOLE_MSX:
case RC_CONSOLE_NEOGEO_POCKET:
case RC_CONSOLE_ORIC:
@ -1584,6 +1632,10 @@ int rc_hash_generate_from_buffer(char hash[33], int console_id, const uint8_t* b
case RC_CONSOLE_WONDERSWAN:
return rc_hash_buffer(hash, buffer, buffer_size);
case RC_CONSOLE_ARDUBOY:
/* https://en.wikipedia.org/wiki/Intel_HEX */
return rc_hash_text(hash, buffer, buffer_size);
case RC_CONSOLE_ATARI_7800:
return rc_hash_7800(hash, buffer, buffer_size);
@ -1593,14 +1645,15 @@ int rc_hash_generate_from_buffer(char hash[33], int console_id, const uint8_t* b
case RC_CONSOLE_NINTENDO:
return rc_hash_nes(hash, buffer, buffer_size);
case RC_CONSOLE_NINTENDO_64:
return rc_hash_n64(hash, buffer, buffer_size);
case RC_CONSOLE_PC_ENGINE: /* NOTE: does not support PCEngine CD */
return rc_hash_pce(hash, buffer, buffer_size);
case RC_CONSOLE_SUPER_NINTENDO:
return rc_hash_snes(hash, buffer, buffer_size);
case RC_CONSOLE_NINTENDO_64:
case RC_CONSOLE_NINTENDO_DS:
return rc_hash_file_from_buffer(hash, console_id, buffer, buffer_size);
}
}
@ -1842,7 +1895,6 @@ int rc_hash_generate_from_file(char hash[33], int console_id, const char* path)
return rc_hash_error(buffer);
}
case RC_CONSOLE_APPLE_II:
case RC_CONSOLE_ATARI_2600:
case RC_CONSOLE_ATARI_JAGUAR:
case RC_CONSOLE_COLECOVISION:
@ -1854,6 +1906,7 @@ int rc_hash_generate_from_file(char hash[33], int console_id, const char* path)
case RC_CONSOLE_MAGNAVOX_ODYSSEY2:
case RC_CONSOLE_MASTER_SYSTEM:
case RC_CONSOLE_MEGA_DRIVE:
case RC_CONSOLE_MEGADUCK:
case RC_CONSOLE_NEOGEO_POCKET:
case RC_CONSOLE_ORIC:
case RC_CONSOLE_POKEMON_MINI:
@ -1867,6 +1920,8 @@ int rc_hash_generate_from_file(char hash[33], int console_id, const char* path)
/* generic whole-file hash - don't buffer */
return rc_hash_whole_file(hash, path);
case RC_CONSOLE_AMSTRAD_PC:
case RC_CONSOLE_APPLE_II:
case RC_CONSOLE_MSX:
case RC_CONSOLE_PC8800:
/* generic whole-file hash with m3u support - don't buffer */
@ -1875,6 +1930,7 @@ int rc_hash_generate_from_file(char hash[33], int console_id, const char* path)
return rc_hash_whole_file(hash, path);
case RC_CONSOLE_ARDUBOY:
case RC_CONSOLE_ATARI_7800:
case RC_CONSOLE_ATARI_LYNX:
case RC_CONSOLE_NINTENDO:
@ -1892,7 +1948,7 @@ int rc_hash_generate_from_file(char hash[33], int console_id, const char* path)
return rc_hash_arcade(hash, path);
case RC_CONSOLE_NINTENDO_64:
return rc_hash_n64_file(hash, path);
return rc_hash_n64(hash, path);
case RC_CONSOLE_NINTENDO_DS:
return rc_hash_nintendo_ds(hash, path);
@ -1986,6 +2042,9 @@ static void rc_hash_initialize_dsk_iterator(struct rc_hash_iterator* iterator, c
{
/* FAT-12 5.25" DD (512 byte sectors, 9 sectors per track, 40 tracks per side */
iterator->consoles[0] = RC_CONSOLE_MSX;
/* AMSDOS 3" - 40 tracks */
iterator->consoles[1] = RC_CONSOLE_AMSTRAD_PC;
}
else if (size == 256 * 16 * 35) /* 140KB */
{
@ -2002,6 +2061,7 @@ static void rc_hash_initialize_dsk_iterator(struct rc_hash_iterator* iterator, c
/* check MSX first, as Apple II isn't supported by RetroArch, and RAppleWin won't use the iterator */
rc_hash_iterator_append_console(iterator, RC_CONSOLE_MSX);
rc_hash_iterator_append_console(iterator, RC_CONSOLE_AMSTRAD_PC);
rc_hash_iterator_append_console(iterator, RC_CONSOLE_APPLE_II);
}
@ -2072,7 +2132,7 @@ void rc_hash_initialize_iterator(struct rc_hash_iterator* iterator, const char*
}
}
/* bin is associated with MegaDrive, Sega32X, Atari 2600, and Watara Supervision.
/* bin is associated with MegaDrive, Sega32X, Atari 2600, Watara Supervision, and MegaDuck.
* Since they all use the same hashing algorithm, only specify one of them */
iterator->consoles[0] = RC_CONSOLE_MEGA_DRIVE;
}
@ -2087,10 +2147,11 @@ void rc_hash_initialize_iterator(struct rc_hash_iterator* iterator, const char*
{
iterator->consoles[0] = RC_CONSOLE_PLAYSTATION;
iterator->consoles[1] = RC_CONSOLE_PLAYSTATION_2;
iterator->consoles[2] = RC_CONSOLE_PC_ENGINE;
iterator->consoles[3] = RC_CONSOLE_3DO;
iterator->consoles[4] = RC_CONSOLE_PCFX;
iterator->consoles[5] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */
iterator->consoles[2] = RC_CONSOLE_DREAMCAST;
iterator->consoles[3] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */
iterator->consoles[4] = RC_CONSOLE_PC_ENGINE;
iterator->consoles[5] = RC_CONSOLE_3DO;
iterator->consoles[6] = RC_CONSOLE_PCFX;
need_path = 1;
}
else if (rc_path_compare_extension(ext, "chd"))
@ -2098,10 +2159,10 @@ void rc_hash_initialize_iterator(struct rc_hash_iterator* iterator, const char*
iterator->consoles[0] = RC_CONSOLE_PLAYSTATION;
iterator->consoles[1] = RC_CONSOLE_PLAYSTATION_2;
iterator->consoles[2] = RC_CONSOLE_DREAMCAST;
iterator->consoles[3] = RC_CONSOLE_PC_ENGINE;
iterator->consoles[4] = RC_CONSOLE_3DO;
iterator->consoles[5] = RC_CONSOLE_PCFX;
iterator->consoles[6] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */
iterator->consoles[3] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */
iterator->consoles[4] = RC_CONSOLE_PC_ENGINE;
iterator->consoles[5] = RC_CONSOLE_3DO;
iterator->consoles[6] = RC_CONSOLE_PCFX;
need_path = 1;
}
else if (rc_path_compare_extension(ext, "col"))
@ -2164,6 +2225,13 @@ void rc_hash_initialize_iterator(struct rc_hash_iterator* iterator, const char*
}
break;
case 'h':
if (rc_path_compare_extension(ext, "hex"))
{
iterator->consoles[0] = RC_CONSOLE_ARDUBOY;
}
break;
case 'i':
if (rc_path_compare_extension(ext, "iso"))
{