comparison 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
comparison
equal deleted inserted replaced
225:15a51252b73a 226:c32015f4969e
45 th_free(tmp); 45 th_free(tmp);
46 return FALSE; 46 return FALSE;
47 } 47 }
48 48
49 49
50 static BOOL sidlib_read_hash_data(th_ioctx *ctx, PSIDHeader *psid, 50 static BOOL sidlib_read_hash_data(th_ioctx *ctx, SIDLibPSIDHeader *psid,
51 th_md5state_t *state, const BOOL newSLDB) 51 th_md5state_t *state, const BOOL newSLDB)
52 { 52 {
53 uint8_t *data = NULL; 53 uint8_t *data = NULL;
54 BOOL ret = FALSE, first = TRUE; 54 BOOL ret = FALSE, first = TRUE;
55 size_t read; 55 size_t read;
103 th_free(data); 103 th_free(data);
104 return ret; 104 return ret;
105 } 105 }
106 106
107 107
108 BOOL sidlib_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid, const BOOL newSLDB) 108 BOOL sidlib_read_sid_file(th_ioctx *ctx, SIDLibPSIDHeader *psid, const BOOL newSLDB)
109 { 109 {
110 PSIDHeader *psid = NULL;
111 th_md5state_t state; 110 th_md5state_t state;
112 BOOL ret = FALSE; 111 BOOL ret = FALSE;
113 off_t hdrStart, hdrEnd; 112 off_t hdrStart, hdrEnd;
114
115 if ((psid = *ppsid = th_malloc0(sizeof(PSIDHeader))) == NULL)
116 {
117 th_io_error(ctx, THERR_MALLOC,
118 "Error PSID context struct.\n");
119 goto error;
120 }
121 113
122 hdrStart = thftell(ctx); 114 hdrStart = thftell(ctx);
123 115
124 // Read PSID header in 116 // Read PSID header in
125 if (!thfread_str(ctx, (uint8_t *) psid->magic, SIDLIB_PSID_MAGIC_LEN) || 117 if (!thfread_str(ctx, (uint8_t *) psid->magic, SIDLIB_PSID_MAGIC_LEN) ||
241 // Free buffer 233 // Free buffer
242 return ret; 234 return ret;
243 } 235 }
244 236
245 237
246 void sidlib_free_sid_file(PSIDHeader *psid) 238 BOOL sidlib_read_sid_file_alloc(th_ioctx *ctx, SIDLibPSIDHeader **ppsid, const BOOL newSLDB)
239 {
240 if ((*ppsid = th_malloc0(sizeof(SIDLibPSIDHeader))) == NULL)
241 {
242 th_io_error(ctx, THERR_MALLOC,
243 "Error allocating PSID context struct.\n");
244 return FALSE;
245 }
246
247 (*ppsid)->allocated = TRUE;
248
249 return sidlib_read_sid_file(ctx, *ppsid, newSLDB);
250 }
251
252
253 void sidlib_free_sid_file(SIDLibPSIDHeader *psid)
247 { 254 {
248 if (psid != NULL) 255 if (psid != NULL)
249 { 256 {
250 th_free(psid->sidName); 257 th_free(psid->sidName);
251 th_free(psid->sidAuthor); 258 th_free(psid->sidAuthor);
252 th_free(psid->sidCopyright); 259 th_free(psid->sidCopyright);
260
261 if (psid->allocated)
262 th_free(psid);
253 } 263 }
254 } 264 }
255 265
256 266
257 const char *sidlib_get_sid_clock_str(const int flags) 267 const char *sidlib_get_sid_clock_str(const int flags)
579 589
580 // Make some reasonable assumptions about validity 590 // Make some reasonable assumptions about validity
581 if (hdr.nnodes == 0 || hdr.nnodes > 256*1024) 591 if (hdr.nnodes == 0 || hdr.nnodes > 256*1024)
582 return THERR_INVALID_DATA; 592 return THERR_INVALID_DATA;
583 593
594 // Initialize MD5 state
584 th_md5_init(&state); 595 th_md5_init(&state);
585 th_md5_append_ne16(&state, hdr.version); 596 th_md5_append_ne16(&state, hdr.version);
586 th_md5_append_ne32(&state, hdr.nnodes); 597 th_md5_append_ne32(&state, hdr.nnodes);
587 598
588 // Allocate index 599 // Allocate index