Mercurial > hg > sidinfo
changeset 1:25a3a142e909
Moar work.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 24 Sep 2014 22:15:55 +0300 |
parents | 8e7e08bf7b9e |
children | ffb795582a91 |
files | Makefile.gen sidinfo.c |
diffstat | 2 files changed, 86 insertions(+), 74 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile.gen Wed Sep 24 21:20:04 2014 +0300 +++ b/Makefile.gen Wed Sep 24 22:15:55 2014 +0300 @@ -10,7 +10,7 @@ # Objects # THLIBS_A=$(OBJPATH)thlibs.a -THLIBS_OBJ=th_util.o th_string.o th_args.o th_crypto.o +THLIBS_OBJ=th_util.o th_string.o th_endian.o th_args.o th_crypto.o ENDIANCHK_BIN=$(BINPATH)endianchk$(EXEEXT)
--- a/sidinfo.c Wed Sep 24 21:20:04 2014 +0300 +++ b/sidinfo.c Wed Sep 24 22:15:55 2014 +0300 @@ -2,6 +2,8 @@ #include "th_endian.h" #include "th_crypto.h" +#define PSID_BUFFER_SIZE (1024 * 16) + typedef struct { @@ -23,30 +25,42 @@ uint8_t startPage, pageLength; uint16_t reserved; - // RSID data + // Extra data BOOL isRSID; + size_t dataSize; // Total size of data - header + th_md5hash_t hash; // Songlength database hash - // Songlength database hash - th_md5hash_t hash; } PSIDHeader; -int si_read_sid_file(FILE *srcFile, PSIDHeader *psid) +int si_read_sid_file(FILE *inFile, PSIDHeader *psid) { - th_md5 + th_md5state_t state; + uint8_t tmp8, *fileData = NULL; + int index, ret = -1; + size_t read; + BOOL first; + + memset(psid, 0, sizeof(*psid)); + + if ((fileData = (uint8_t *) th_malloc(PSID_BUFFER_SIZE)) == NULL) + { + THERR("Error allocating temporary data buffer of %d bytes.\n", PSID_BUFFER_SIZE); + goto error; + } // Read PSID header in - if (!th_fread_str(srcFile, (uint8_t *) psid->magic, sizeof(psid->magic)) || - !th_fread_be16(srcFile, &psid->version) || - !th_fread_be16(srcFile, &psid->dataOffset) || - !th_fread_be16(srcFile, &psid->loadAddress) || - !th_fread_be16(srcFile, &psid->initAddress) || - !th_fread_be16(srcFile, &psid->playAddress) || - !th_fread_be16(srcFile, &psid->nSongs) || - !th_fread_be16(srcFile, &psid->startSong) || - !th_fread_be32(srcFile, &psid->speed)) + if (!th_fread_str(inFile, (uint8_t *) psid->magic, sizeof(psid->magic)) || + !th_fread_be16(inFile, &psid->version) || + !th_fread_be16(inFile, &psid->dataOffset) || + !th_fread_be16(inFile, &psid->loadAddress) || + !th_fread_be16(inFile, &psid->initAddress) || + !th_fread_be16(inFile, &psid->playAddress) || + !th_fread_be16(inFile, &psid->nSongs) || + !th_fread_be16(inFile, &psid->startSong) || + !th_fread_be32(inFile, &psid->speed)) { - THERR("Could not read PSID/RSID header from '%s'\n", filename); + THERR("Could not read PSID/RSID header.\n"); goto error; } @@ -54,70 +68,68 @@ psid->magic[1] != 'S' || psid->magic[2] != 'I' || psid->magic[3] != 'D' || psid->version < 1 || psid->version > 3) { - THERR("Not a supported PSID or RSID file '%s'\n", filename); + THERR("Not a supported PSID or RSID file.\n"); goto error; } - isRSID = psid->magic[0] == 'R'; + psid->isRSID = psid->magic[0] == 'R'; - if (!th_fread_str(srcFile, (uint8_t *)psid->sidName, sizeof(psid->sidName)) || - !th_fread_str(srcFile, (uint8_t *)psid->sidAuthor, sizeof(psid->sidAuthor)) || - !th_fread_str(srcFile, (uint8_t *)psid->sidCopyright, sizeof(psid->sidCopyright))) + if (!th_fread_str(inFile, (uint8_t *)psid->sidName, sizeof(psid->sidName)) || + !th_fread_str(inFile, (uint8_t *)psid->sidAuthor, sizeof(psid->sidAuthor)) || + !th_fread_str(inFile, (uint8_t *)psid->sidCopyright, sizeof(psid->sidCopyright))) { - THERR("Error reading SID file header from '%s'\n", filename); + THERR("Error reading SID file header.\n"); goto error; } // Check if we need to load PSIDv2NG header ... - psidH2.flags = 0; // Just silence a stupid gcc warning - if (psid->version >= 2) { // Yes, we need to - if (!th_fread_be16(srcFile, &psidH2.flags) || - !th_fread_byte(srcFile, &psidH2.startPage) || - !th_fread_byte(srcFile, &psidH2.pageLength) || - !th_fread_be16(srcFile, &psidH2.reserved)) + if (!th_fread_be16(inFile, &psid->flags) || + !th_fread_byte(inFile, &psid->startPage) || + !th_fread_byte(inFile, &psid->pageLength) || + !th_fread_be16(inFile, &psid->reserved)) { - THERR("Error reading PSID/RSID v2+ extra header data from '%s'\n", - filename); + THERR("Error reading PSID/RSID v2+ extra header data.\n"); goto error; } } - /* Allocate buffer */ - tuneSize = 0; - if ((songData = (uint8_t *) th_malloc(SI_SIDBUF_SIZE)) == NULL) - { - xs_error("Error allocating temp data buffer for file '%s'\n", filename); - goto error; - } + // Initialize MD5-hash calculation + th_md5_init(&state); - /* Read data to buffer */ - result = fread(songData, sizeof(uint8_t), TH_SIDBUF_SIZE, inFile); - xs_fclose(inFile); + // Process actual data + psid->dataSize = 0; + first = TRUE; + do { + read = fread(fileData, sizeof(uint8_t), PSID_BUFFER_SIZE, inFile); + psid->dataSize += read; + if (read < 16) + { + THERR("Error reading song data, unexpectedly small file.\n"); + goto error; + } - /* Initialize and start MD5-hash calculation */ - th_md5_init(&inState); + if (first && psid->loadAddress == 0) + { + // Strip load address (2 first bytes) + th_md5_append(&state, &fileData[2], read - 2); + first = FALSE; + } + else + { + // Append "as is" + th_md5_append(&state, fileData, read); + } + } while (read > 0 && !feof(inFile)); - if (psid->loadAddress == 0) - { - /* Strip load address (2 first bytes) */ - th_md5_append(&inState, &songData[2], result - 2); - } - else - { - /* Append "as is" */ - th_md5_append(&inState, songData, result); - } - - - /* Append header data to hash */ + // Append header data to hash #define THADDHASH(QDATAB) do { \ uint8_t ib8[2]; \ ib8[0] = (QDATAB & 0xff); \ ib8[1] = (QDATAB >> 8); \ - th_md5_append(&inState, (uint8_t *) &ib8, sizeof(ib8)); \ + th_md5_append(&state, (uint8_t *) &ib8, sizeof(ib8)); \ } while (0) THADDHASH(psid->initAddress); @@ -126,52 +138,52 @@ #undef THADDHASH // Append song speed data to hash - i8 = isRSID ? 60 : 0; + tmp8 = psid->isRSID ? 60 : 0; for (index = 0; index < psid->nSongs && index < 32; index++) { - if (isRSID) - i8 = 60; + if (psid->isRSID) + tmp8 = 60; else - i8 = (psid->speed & (1 << index)) ? 60 : 0; + tmp8 = (psid->speed & (1 << index)) ? 60 : 0; - th_md5_append(&inState, &i8, sizeof(i8)); + th_md5_append(&state, &tmp8, sizeof(tmp8)); } // Rest of songs (more than 32) for (index = 32; index < psid->nSongs; index++) - th_md5_append(&inState, &i8, sizeof(i8)); + th_md5_append(&state, &tmp8, sizeof(tmp8)); // PSIDv2NG specific if (psid->version >= 2) { // REFER TO SIDPLAY HEADERS FOR MORE INFORMATION - i8 = (psid->flags >> 2) & 3; - if (i8 == 2) - th_md5_append(&inState, &i8, sizeof(i8)); + tmp8 = (psid->flags >> 2) & 3; + if (tmp8 == 2) + th_md5_append(&state, &tmp8, sizeof(tmp8)); } // Calculate the hash - th_md5_finish(&inState, psid->hash); - + th_md5_finish(&state, psid->hash); + ret = 0; +error: // Free buffer - th_free(songData); + th_free(fileData); + return ret; } int main(int argc, char *argv[]) { - FILE *srcFile = NULL; - uint8_t i8; - int index; - // Initialize th_init("SIDInfo", "PSID/RSID information displayer", "0.1", NULL, NULL); th_verbosityLevel = 0; // Try to open the file - if ((srcFile = fopen(srcFilename, "rb")) == NULL) - goto error; +// if ((inFile = fopen(inFilename, "rb")) == NULL) +// goto error; + + return 0; }