diff --git a/streams/chd_stream.c b/streams/chd_stream.c index e0fa7fe..404e130 100644 --- a/streams/chd_stream.c +++ b/streams/chd_stream.c @@ -207,12 +207,12 @@ chdstream_find_track(chd_file *fd, int32_t track, metadata_t *meta) chdstream_t *chdstream_open(const char *path, int32_t track) { metadata_t meta; - uint32_t pregap = 0; - uint8_t *hunkmem = NULL; - const chd_header *hd = NULL; - chdstream_t *stream = NULL; - chd_file *chd = NULL; - chd_error err = chd_open(path, CHD_OPEN_READ, NULL, &chd); + uint32_t pregap = 0; + uint8_t *hunkmem = NULL; + const chd_header *hd = NULL; + chdstream_t *stream = NULL; + chd_file *chd = NULL; + chd_error err = chd_open(path, CHD_OPEN_READ, NULL, &chd); if (err != CHDERR_NONE) return NULL; @@ -220,38 +220,39 @@ chdstream_t *chdstream_open(const char *path, int32_t track) if (!chdstream_find_track(chd, track, &meta)) goto error; - stream = (chdstream_t*)calloc(1, sizeof(*stream)); + stream = (chdstream_t*)malloc(sizeof(*stream)); if (!stream) goto error; - hd = chd_get_header(chd); - hunkmem = (uint8_t*)malloc(hd->hunkbytes); + stream->chd = NULL; + stream->swab = false; + stream->frame_size = 0; + stream->frame_offset = 0; + stream->frames_per_hunk = 0; + stream->track_frame = 0; + stream->track_start = 0; + stream->track_end = 0; + stream->offset = 0; + stream->hunknum = -1; + + hd = chd_get_header(chd); + hunkmem = (uint8_t*)malloc(hd->hunkbytes); if (!hunkmem) goto error; - stream->hunkmem = hunkmem; + stream->hunkmem = hunkmem; if (string_is_equal(meta.type, "MODE1_RAW")) - { stream->frame_size = SECTOR_SIZE; - stream->frame_offset = 0; - } else if (string_is_equal(meta.type, "MODE2_RAW")) - { stream->frame_size = SECTOR_SIZE; - stream->frame_offset = 0; - } else if (string_is_equal(meta.type, "AUDIO")) { stream->frame_size = SECTOR_SIZE; - stream->frame_offset = 0; stream->swab = true; } else - { stream->frame_size = hd->unitbytes; - stream->frame_offset = 0; - } /* Only include pregap data if it was in the track file */ if (meta.pgtype[0] != 'V') @@ -263,8 +264,6 @@ chdstream_t *chdstream_open(const char *path, int32_t track) stream->track_start = (size_t)pregap * stream->frame_size; stream->track_end = stream->track_start + (size_t)meta.frames * stream->frame_size; - stream->offset = 0; - stream->hunknum = -1; return stream; @@ -280,14 +279,14 @@ error: void chdstream_close(chdstream_t *stream) { - if (stream) - { - if (stream->hunkmem) - free(stream->hunkmem); - if (stream->chd) - chd_close(stream->chd); - free(stream); - } + if (!stream) + return; + + if (stream->hunkmem) + free(stream->hunkmem); + if (stream->chd) + chd_close(stream->chd); + free(stream); } static bool diff --git a/streams/interface_stream.c b/streams/interface_stream.c index 65c4626..09bccc9 100644 --- a/streams/interface_stream.c +++ b/streams/interface_stream.c @@ -221,12 +221,24 @@ void *intfstream_init(intfstream_info_t *info) if (!info) goto error; - intf = (intfstream_internal_t*)calloc(1, sizeof(*intf)); + intf = (intfstream_internal_t*)malloc(sizeof(*intf)); if (!intf) goto error; - intf->type = info->type; + intf->type = info->type; + intf->file.fp = NULL; + intf->memory.buf.data = NULL; + intf->memory.buf.size = 0; + intf->memory.fp = NULL; + intf->memory.writable = false; +#ifdef HAVE_CHD + intf->chd.track = 0; + intf->chd.fp = NULL; +#endif +#ifdef HAVE_ZLIB + intf->rzip.fp = NULL; +#endif switch (intf->type) { @@ -256,7 +268,8 @@ error: return NULL; } -int64_t intfstream_seek(intfstream_internal_t *intf, int64_t offset, int whence) +int64_t intfstream_seek( + intfstream_internal_t *intf, int64_t offset, int whence) { if (!intf) return -1; diff --git a/streams/memory_stream.c b/streams/memory_stream.c index 22259d1..d425601 100644 --- a/streams/memory_stream.c +++ b/streams/memory_stream.c @@ -26,6 +26,7 @@ #include +/* TODO/FIXME - static globals */ static uint8_t* g_buffer = NULL; static uint64_t g_size = 0; static uint64_t last_file_size = 0; @@ -48,7 +49,7 @@ static void memstream_update_pos(memstream_t *stream) void memstream_set_buffer(uint8_t *buffer, uint64_t size) { g_buffer = buffer; - g_size = size; + g_size = size; } uint64_t memstream_get_last_size(void) @@ -75,11 +76,22 @@ memstream_t *memstream_open(unsigned writing) if (!g_buffer || !g_size) return NULL; - stream = (memstream_t*)calloc(1, sizeof(*stream)); + stream = (memstream_t*)malloc(sizeof(*stream)); + + if (!stream) + return NULL; + + stream->buf = NULL; + stream->size = 0; + stream->ptr = 0; + stream->max_ptr = 0; + stream->writing = 0; + memstream_init(stream, g_buffer, g_size, writing); - g_buffer = NULL; - g_size = 0; + g_buffer = NULL; + g_size = 0; + return stream; } diff --git a/streams/rzip_stream.c b/streams/rzip_stream.c index 09fd34e..07aa40f 100644 --- a/streams/rzip_stream.c +++ b/streams/rzip_stream.c @@ -407,10 +407,28 @@ rzipstream_t* rzipstream_open(const char *path, unsigned mode) return NULL; /* Allocate stream object */ - stream = (rzipstream_t*)calloc(1, sizeof(*stream)); + stream = (rzipstream_t*)malloc(sizeof(*stream)); if (!stream) return NULL; + stream->is_compressed = false; + stream->is_writing = false; + stream->size = 0; + stream->chunk_size = 0; + stream->virtual_ptr = 0; + stream->file = NULL; + stream->deflate_backend = NULL; + stream->deflate_stream = NULL; + stream->inflate_backend = NULL; + stream->inflate_stream = NULL; + stream->in_buf = NULL; + stream->in_buf_size = 0; + stream->in_buf_ptr = 0; + stream->out_buf = NULL; + stream->out_buf_size = 0; + stream->out_buf_ptr = 0; + stream->out_buf_occupancy = 0; + /* Initialise stream */ if (!rzipstream_init_stream( stream, path, diff --git a/streams/trans_stream.c b/streams/trans_stream.c index c7b3473..59ebb52 100644 --- a/streams/trans_stream.c +++ b/streams/trans_stream.c @@ -45,9 +45,7 @@ bool trans_stream_trans_full( uint32_t rd, wn; if (data && *data) - { rdata = *data; - } else { rdata = backend->stream_new(); diff --git a/streams/trans_stream_pipe.c b/streams/trans_stream_pipe.c index 808c8bc..76f8b16 100644 --- a/streams/trans_stream_pipe.c +++ b/streams/trans_stream_pipe.c @@ -34,7 +34,17 @@ struct pipe_trans_stream static void *pipe_stream_new(void) { - return (struct pipe_trans_stream*)calloc(1, sizeof(struct pipe_trans_stream)); + struct pipe_trans_stream *stream = + (struct pipe_trans_stream*)malloc(sizeof(*stream)); + if (!stream) + return NULL; + + stream->in = NULL; + stream->out = NULL; + stream->in_size = 0; + stream->out_size = 0; + + return stream; } static void pipe_stream_free(void *data) @@ -45,15 +55,23 @@ static void pipe_stream_free(void *data) static void pipe_set_in(void *data, const uint8_t *in, uint32_t in_size) { struct pipe_trans_stream *p = (struct pipe_trans_stream *) data; - p->in = in; - p->in_size = in_size; + + if (!p) + return; + + p->in = in; + p->in_size = in_size; } static void pipe_set_out(void *data, uint8_t *out, uint32_t out_size) { struct pipe_trans_stream *p = (struct pipe_trans_stream *) data; - p->out = out; - p->out_size = out_size; + + if (!p) + return; + + p->out = out; + p->out_size = out_size; } static bool pipe_trans( @@ -66,21 +84,19 @@ static bool pipe_trans( if (p->out_size < p->in_size) { memcpy(p->out, p->in, p->out_size); - *rd = *wn = p->out_size; - p->in += p->out_size; + *rd = *wn = p->out_size; + p->in += p->out_size; p->out += p->out_size; - *error = TRANS_STREAM_ERROR_BUFFER_FULL; + *error = TRANS_STREAM_ERROR_BUFFER_FULL; return false; } - else - { - memcpy(p->out, p->in, p->in_size); - *rd = *wn = p->in_size; - p->in += p->in_size; - p->out += p->in_size; - *error = TRANS_STREAM_ERROR_NONE; - return true; - } + + memcpy(p->out, p->in, p->in_size); + *rd = *wn = p->in_size; + p->in += p->in_size; + p->out += p->in_size; + *error = TRANS_STREAM_ERROR_NONE; + return true; } const struct trans_stream_backend pipe_backend = { diff --git a/streams/trans_stream_zlib.c b/streams/trans_stream_zlib.c index 9e783bd..e1ca022 100644 --- a/streams/trans_stream_zlib.c +++ b/streams/trans_stream_zlib.c @@ -36,20 +36,24 @@ struct zlib_trans_stream static void *zlib_deflate_stream_new(void) { - struct zlib_trans_stream *ret = (struct zlib_trans_stream*)calloc(1, sizeof(struct zlib_trans_stream)); + struct zlib_trans_stream *ret = (struct zlib_trans_stream*) + malloc(sizeof(*ret)); if (!ret) return NULL; - ret->ex = 9; - return (void *) ret; + ret->inited = false; + ret->ex = 9; + return (void *)ret; } static void *zlib_inflate_stream_new(void) { - struct zlib_trans_stream *ret = (struct zlib_trans_stream*)calloc(1, sizeof(struct zlib_trans_stream)); + struct zlib_trans_stream *ret = (struct zlib_trans_stream*) + malloc(sizeof(*ret)); if (!ret) return NULL; - ret->ex = MAX_WBITS; - return (void *) ret; + ret->inited = false; + ret->ex = MAX_WBITS; + return (void *)ret; } static void zlib_deflate_stream_free(void *data) @@ -222,9 +226,9 @@ static bool zlib_inflate_trans( zt->inited = true; } - pre_avail_in = z->avail_in; + pre_avail_in = z->avail_in; pre_avail_out = z->avail_out; - zret = inflate(z, flush ? Z_FINISH : Z_NO_FLUSH); + zret = inflate(z, flush ? Z_FINISH : Z_NO_FLUSH); if (zret == Z_OK) {