Skip to content

Commit

Permalink
fixed bug #4410: respect original mp3 file offset.
Browse files Browse the repository at this point in the history
  • Loading branch information
sezero committed Jan 28, 2021
1 parent 9369412 commit ba82d21
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
16 changes: 15 additions & 1 deletion mp3utils.c
Expand Up @@ -20,6 +20,7 @@
*/

#include "SDL_stdinc.h"
#include "SDL_error.h"
#include "SDL_rwops.h"

#include "mp3utils.h"
Expand All @@ -28,6 +29,19 @@

/*********************** SDL_RW WITH BOOKKEEPING ************************/

int MP3_RWinit(struct mp3file_t *fil, SDL_RWops *src) {
fil->rw = src;
fil->start = SDL_RWtell(src);
fil->length = SDL_RWseek(src, 0, RW_SEEK_END) - fil->start;
fil->pos = 0;
if (fil->start < 0 || fil->length < 0) {
SDL_Error(SDL_EFSEEK);
return -1;
}
SDL_RWseek(src, fil->start, RW_SEEK_SET);
return 0;
}

int MP3_RWread(struct mp3file_t *fil, void *ptr, int size, int maxnum) {
int remaining = fil->length - fil->pos;
int ret;
Expand All @@ -54,7 +68,7 @@ int MP3_RWseek(struct mp3file_t *fil, int offset, int whence) {
ret = SDL_RWseek(fil->rw, fil->start + offset, RW_SEEK_SET);
if (ret < 0) return ret;
fil->pos = offset;
return (fil->pos - fil->start);
return offset;
}


Expand Down
1 change: 1 addition & 0 deletions mp3utils.h
Expand Up @@ -28,6 +28,7 @@ struct mp3file_t {
};

extern int mp3_skiptags(struct mp3file_t *fil);
extern int MP3_RWinit(struct mp3file_t *fil, SDL_RWops *src);
extern int MP3_RWread(struct mp3file_t *fil, void *ptr, int size, int maxnum);
extern int MP3_RWseek(struct mp3file_t *fil, int offset, int whence);

Expand Down
12 changes: 5 additions & 7 deletions music_mad.c
Expand Up @@ -30,14 +30,12 @@ mad_openFileRW(SDL_RWops *rw, SDL_AudioSpec *mixer, int freerw)
{
mad_data *mp3_mad;

mp3_mad = (mad_data *)SDL_malloc(sizeof(mad_data));
mp3_mad = (mad_data *)SDL_calloc(1, sizeof(mad_data));
if (mp3_mad) {
int pos = SDL_RWtell(rw);
mp3_mad->mp3file.rw = rw;
mp3_mad->mp3file.start = 0;
mp3_mad->mp3file.pos = 0;
mp3_mad->mp3file.length = SDL_RWseek(rw, 0, RW_SEEK_END);
SDL_RWseek(rw, pos, RW_SEEK_SET);
if (MP3_RWinit(&mp3_mad->mp3file, rw) < 0) {
SDL_free(mp3_mad);
return NULL;
}
if (mp3_skiptags(&mp3_mad->mp3file) < 0) {
SDL_free(mp3_mad);
Mix_SetError("music_mad: corrupt mp3 file (bad tags.)");
Expand Down
12 changes: 4 additions & 8 deletions music_mpg.c
Expand Up @@ -103,27 +103,23 @@ mpg_data*
mpg_new_rw(SDL_RWops *src, SDL_AudioSpec* mixer, int freesrc)
{
int fmt, result;
int pos;
mpg_data* m = NULL;

if (!Mix_Init(MIX_INIT_MP3)) {
goto fail;
}

m = (mpg_data*)SDL_malloc(sizeof(mpg_data));
m = (mpg_data*)SDL_calloc(1, sizeof(mpg_data));
if (!m) {
SDL_OutOfMemory();
goto fail;
}

SDL_memset(m, 0, sizeof(mpg_data));

m->mp3file.rw = src;
m->freesrc = freesrc;

pos = SDL_RWtell(src);
m->mp3file.length = SDL_RWseek(src, 0, RW_SEEK_END);
SDL_RWseek(src, pos, RW_SEEK_SET);
if (MP3_RWinit(&m->mp3file, src) < 0) {
goto fail;
}
if (mp3_skiptags(&m->mp3file) < 0) {
Mix_SetError("music_mpg: corrupt mp3 file (bad tags.)");
goto fail;
Expand Down

0 comments on commit ba82d21

Please sign in to comment.