# HG changeset patch # User Matti Hamalainen # Date 1477558732 -10800 # Node ID 2636185649c60111aaaf39e97f475511450e24c6 # Parent 1786b9d77782786f2748ceed3da6c02843b505a9 Add si_fread_str() helper function. diff -r 1786b9d77782 -r 2636185649c6 sidlib.c --- a/sidlib.c Thu Oct 27 11:52:58 2016 +0300 +++ b/sidlib.c Thu Oct 27 11:58:52 2016 +0300 @@ -26,6 +26,27 @@ } PSIDLibHdr; +BOOL si_fread_str(th_ioctx *ctx, char **str, const size_t len) +{ + char *tmp = th_malloc(len + 1); + if (tmp == NULL) + goto err; + + if (!thfread_str(ctx, tmp, len)) + goto err; + + tmp[len] = 0; + + *str = tmp; + + return TRUE; + +err: + th_free(tmp); + return FALSE; +} + + int si_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid) { PSIDHeader *psid = NULL; @@ -80,9 +101,9 @@ psid->isRSID = psid->magic[0] == 'R'; - if (!thfread_str(ctx, (uint8_t *) psid->sidName, PSID_STR_LEN) || - !thfread_str(ctx, (uint8_t *) psid->sidAuthor, PSID_STR_LEN) || - !thfread_str(ctx, (uint8_t *) psid->sidCopyright, PSID_STR_LEN)) + if (!si_fread_str(ctx, &psid->sidName, PSID_STR_LEN) || + !si_fread_str(ctx, &psid->sidAuthor, PSID_STR_LEN) || + !si_fread_str(ctx, &psid->sidCopyright, PSID_STR_LEN)) { th_io_error(ctx, ctx->status, "Error reading SID file header: %s.\n", @@ -90,10 +111,6 @@ goto error; } - psid->sidName[PSID_STR_LEN] = 0; - psid->sidAuthor[PSID_STR_LEN] = 0; - psid->sidCopyright[PSID_STR_LEN] = 0; - // Check if we need to load PSIDv2NG header ... if (psid->version >= 2) { diff -r 1786b9d77782 -r 2636185649c6 sidlib.h --- a/sidlib.h Thu Oct 27 11:52:58 2016 +0300 +++ b/sidlib.h Thu Oct 27 11:58:52 2016 +0300 @@ -51,9 +51,9 @@ nSongs, // Number of subsongs startSong; // Default starting song uint32_t speed; // Speed - char sidName[PSID_STR_LEN + 1]; // Descriptive text-fields, ASCIIZ - char sidAuthor[PSID_STR_LEN + 1]; - char sidCopyright[PSID_STR_LEN + 1]; + char *sidName; // Descriptive text-fields, ASCIIZ + char *sidAuthor; + char *sidCopyright; // PSIDv2 data uint16_t flags; // Flags