# HG changeset patch # User Matti Hamalainen # Date 1477558347 -10800 # Node ID c1462b7880e8487bc3f296241882f290e50e11ed # Parent ffccc712409ad91013dbe9d73c35846d0bc55115 Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust API/ABI accordingly. diff -r ffccc712409a -r c1462b7880e8 sidinfo.c --- a/sidinfo.c Wed Oct 26 11:59:15 2016 +0300 +++ b/sidinfo.c Thu Oct 27 11:52:27 2016 +0300 @@ -511,7 +511,7 @@ BOOL argHandleFile(char *filename) { - static PSIDHeader psid; + PSIDHeader *psid = NULL; th_ioctx *inFile = NULL; FILE *outFile; int index; @@ -523,7 +523,7 @@ if ((inFile = th_io_fopen(&th_stdio_io_ops, filename, "rb")) == NULL) { THERR("Could not open file '%s'.\n", filename); - return TRUE; + goto error; } // Read PSID data @@ -535,7 +535,7 @@ // Get songlength information, if any if (sidSLDB != NULL) - psid.lengths = si_sldb_get_by_hash(sidSLDB, psid.hash); + psid->lengths = si_sldb_get_by_hash(sidSLDB, psid->hash); // Output for (index = 0; index < optFormat.nitems; index++) @@ -552,7 +552,7 @@ break; default: - siPrintPSIDInformationField(outFile, filename, &psid, &shown, item); + siPrintPSIDInformationField(outFile, filename, psid, &shown, item); break; } } @@ -565,6 +565,7 @@ // Shutdown error: + si_free_sid_file(psid); th_io_free(inFile); return TRUE; diff -r ffccc712409a -r c1462b7880e8 sidlib.c --- a/sidlib.c Wed Oct 26 11:59:15 2016 +0300 +++ b/sidlib.c Thu Oct 27 11:52:27 2016 +0300 @@ -26,16 +26,15 @@ } PSIDLibHdr; -int si_read_sid_file(th_ioctx *ctx, PSIDHeader *psid) +int si_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid) { + PSIDHeader *psid = NULL; th_md5state_t state; uint8_t tmp8, *data = NULL; int index, ret = -1; size_t read; BOOL first; - memset(psid, 0, sizeof(*psid)); - if ((data = (uint8_t *) th_malloc(PSID_BUFFER_SIZE)) == NULL) { th_io_error(ctx, THERR_MALLOC, @@ -44,6 +43,13 @@ goto error; } + if ((psid = *ppsid = th_malloc0(sizeof(PSIDHeader))) == NULL) + { + th_io_error(ctx, THERR_MALLOC, + "Error PSID context struct.\n"); + goto error; + } + // Read PSID header in if (!thfread_str(ctx, (uint8_t *) psid->magic, PSID_MAGIC_LEN) || !thfread_be16(ctx, &psid->version) || @@ -180,6 +186,17 @@ } +void si_free_sid_file(PSIDHeader *psid) +{ + if (psid != NULL) + { + th_free(psid->sidName); + th_free(psid->sidAuthor); + th_free(psid->sidCopyright); + } +} + + const char *si_get_sid_clock_str(const int flags) { switch (flags & PSF_CLOCK_MASK) diff -r ffccc712409a -r c1462b7880e8 sidlib.h --- a/sidlib.h Wed Oct 26 11:59:15 2016 +0300 +++ b/sidlib.h Thu Oct 27 11:52:27 2016 +0300 @@ -92,7 +92,8 @@ // // Functions // -int si_read_sid_file(th_ioctx *ctx, PSIDHeader *psid); +int si_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid); +void si_free_sid_file(PSIDHeader *psid); const char * si_get_sid_clock_str(const int flags); const char * si_get_sid_model_str(const int flags);