diff sidlib.c @ 226:c32015f4969e

Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib prefix used otherwise. Add new function sidlib_read_sid_file_alloc() which allocates the PSID header struct, instead of sidlib_read_sid_file() which just reads into the given struct.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 03 Jan 2020 10:38:18 +0200
parents 4bec78f45188
children 2b3d5d49086d
line wrap: on
line diff
--- a/sidlib.c	Tue Dec 31 05:20:17 2019 +0200
+++ b/sidlib.c	Fri Jan 03 10:38:18 2020 +0200
@@ -47,7 +47,7 @@
 }
 
 
-static BOOL sidlib_read_hash_data(th_ioctx *ctx, PSIDHeader *psid,
+static BOOL sidlib_read_hash_data(th_ioctx *ctx, SIDLibPSIDHeader *psid,
     th_md5state_t *state, const BOOL newSLDB)
 {
     uint8_t *data = NULL;
@@ -105,20 +105,12 @@
 }
 
 
-BOOL sidlib_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid, const BOOL newSLDB)
+BOOL sidlib_read_sid_file(th_ioctx *ctx, SIDLibPSIDHeader *psid, const BOOL newSLDB)
 {
-    PSIDHeader *psid = NULL;
     th_md5state_t state;
     BOOL ret = FALSE;
     off_t hdrStart, hdrEnd;
 
-    if ((psid = *ppsid = th_malloc0(sizeof(PSIDHeader))) == NULL)
-    {
-        th_io_error(ctx, THERR_MALLOC,
-            "Error PSID context struct.\n");
-        goto error;
-    }
-
     hdrStart = thftell(ctx);
 
     // Read PSID header in
@@ -243,13 +235,31 @@
 }
 
 
-void sidlib_free_sid_file(PSIDHeader *psid)
+BOOL sidlib_read_sid_file_alloc(th_ioctx *ctx, SIDLibPSIDHeader **ppsid, const BOOL newSLDB)
+{
+    if ((*ppsid = th_malloc0(sizeof(SIDLibPSIDHeader))) == NULL)
+    {
+        th_io_error(ctx, THERR_MALLOC,
+            "Error allocating PSID context struct.\n");
+        return FALSE;
+    }
+    
+    (*ppsid)->allocated = TRUE;
+
+    return sidlib_read_sid_file(ctx, *ppsid, newSLDB);
+}
+
+
+void sidlib_free_sid_file(SIDLibPSIDHeader *psid)
 {
     if (psid != NULL)
     {
         th_free(psid->sidName);
         th_free(psid->sidAuthor);
         th_free(psid->sidCopyright);
+        
+        if (psid->allocated)
+            th_free(psid);
     }
 }
 
@@ -581,6 +591,7 @@
     if (hdr.nnodes == 0 || hdr.nnodes > 256*1024)
         return THERR_INVALID_DATA;
 
+    // Initialize MD5 state
     th_md5_init(&state);
     th_md5_append_ne16(&state, hdr.version);
     th_md5_append_ne32(&state, hdr.nnodes);