diff sidlib.c @ 126:c1462b7880e8

Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust API/ABI accordingly.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 27 Oct 2016 11:52:27 +0300
parents 09ca9d9a8710
children 2636185649c6
line wrap: on
line diff
--- 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)