diff --git a/file/config_file.c b/file/config_file.c index 797b84e..18259e3 100644 --- a/file/config_file.c +++ b/file/config_file.c @@ -52,10 +52,10 @@ struct config_entry_list /* If we got this from an #include, * do not allow overwrite. */ bool readonly; - char *key; - char *value; uint32_t key_hash; + char *key; + char *value; struct config_entry_list *next; }; @@ -240,10 +240,11 @@ static void add_sub_conf(config_file_t *conf, char *path) char real_path[PATH_MAX_LENGTH]; config_file_t *sub_conf = NULL; struct config_include_list *head = conf->includes; - struct config_include_list *node = (struct config_include_list*)calloc(1, sizeof(*node)); + struct config_include_list *node = (struct config_include_list*)malloc(sizeof(*node)); if (node) { + node->next = NULL; /* Add include list */ node->path = strdup(path); @@ -365,23 +366,29 @@ error: static config_file_t *config_file_new_internal( const char *path, unsigned depth) { - FILE *file = NULL; - struct config_file *conf = (struct config_file*)calloc(1, sizeof(*conf)); + FILE *file = NULL; + struct config_file *conf = (struct config_file*)malloc(sizeof(*conf)); if (!conf) return NULL; + conf->path = NULL; + conf->entries = NULL; + conf->tail = NULL; + conf->includes = NULL; + conf->include_depth = 0; + if (!path || !*path) return conf; if (path_is_directory(path)) goto error; - conf->path = strdup(path); + conf->path = strdup(path); if (!conf->path) goto error; conf->include_depth = depth; - file = fopen(path, "r"); + file = fopen(path, "r"); if (!file) { @@ -392,9 +399,8 @@ static config_file_t *config_file_new_internal( while (!feof(file)) { - struct config_entry_list *list = (struct config_entry_list*) - calloc(1, sizeof(*list)); - char *line = NULL; + char *line = NULL; + struct config_entry_list *list = (struct config_entry_list*)malloc(sizeof(*list)); if (!list) { @@ -403,7 +409,13 @@ static config_file_t *config_file_new_internal( return NULL; } - line = getaline(file); + list->readonly = false; + list->key_hash = 0; + list->key = NULL; + list->value = NULL; + list->next = NULL; + + line = getaline(file); if (!line) { @@ -500,14 +512,17 @@ config_file_t *config_file_new_from_string(const char *from_string) { size_t i; struct string_list *lines = NULL; - struct config_file *conf = (struct config_file*)calloc(1, sizeof(*conf)); + struct config_file *conf = (struct config_file*)malloc(sizeof(*conf)); if (!conf) return NULL; if (!from_string) return conf; - conf->path = NULL; + conf->path = NULL; + conf->entries = NULL; + conf->tail = NULL; + conf->includes = NULL; conf->include_depth = 0; lines = string_split(from_string, "\n"); @@ -516,9 +531,8 @@ config_file_t *config_file_new_from_string(const char *from_string) for (i = 0; i < lines->size; i++) { - struct config_entry_list *list = (struct config_entry_list*) - calloc(1, sizeof(*list)); - char* line = lines->elems[i].data; + struct config_entry_list *list = (struct config_entry_list*)malloc(sizeof(*list)); + char *line = lines->elems[i].data; if (!list) { @@ -527,6 +541,12 @@ config_file_t *config_file_new_from_string(const char *from_string) return NULL; } + list->readonly = false; + list->key_hash = 0; + list->key = NULL; + list->value = NULL; + list->next = NULL; + if (line && conf) { if (*line && parse_line(conf, list, line)) @@ -761,16 +781,21 @@ void config_set_string(config_file_t *conf, const char *key, const char *val) return; } - if (!val) return; + if (!val) + return; - entry = (struct config_entry_list*)calloc(1, sizeof(*entry)); - if (!entry) return; + entry = (struct config_entry_list*)malloc(sizeof(*entry)); + if (!entry) + return; - entry->key = strdup(key); - entry->value = strdup(val); + entry->readonly = false; + entry->key_hash = 0; + entry->key = strdup(key); + entry->value = strdup(val); + entry->next = NULL; if (last) - last->next = entry; + last->next = entry; else conf->entries = entry; } @@ -875,12 +900,17 @@ bool config_file_write(config_file_t *conf, const char *path) { FILE *file; - if (path) + if (path && !string_is_empty(path)) { file = fopen(path, "w"); if (!file) return false; +#ifdef WIIU + /* TODO: use FBF everywhere once https://i.imgur.com/muVhNeF.jpg is fixed */ + setvbuf(file, NULL, _IONBF, 0x4000); +#else setvbuf(file, NULL, _IOFBF, 0x4000); +#endif } else file = stdout; diff --git a/formats/libchdr/chd.c b/formats/libchdr/chd.c index 491873e..72169a1 100644 --- a/formats/libchdr/chd.c +++ b/formats/libchdr/chd.c @@ -1404,6 +1404,7 @@ chd_error chd_open_file(core_file *file, int mode, chd_file *parent, chd_file ** else { err = decompress_v5_map(newchd, &(newchd->header)); + (void)err; } #ifdef NEED_CACHE_HUNK @@ -1435,7 +1436,10 @@ chd_error chd_open_file(core_file *file, int mode, chd_file *parent, chd_file ** /* initialize the codec */ if (newchd->codecintf[0]->init != NULL) + { err = (*newchd->codecintf[0]->init)(&newchd->zlib_codec_data, newchd->header.hunkbytes); + (void)err; + } } else { @@ -1449,7 +1453,10 @@ chd_error chd_open_file(core_file *file, int mode, chd_file *parent, chd_file ** { newchd->codecintf[decompnum] = &codec_interfaces[i]; if (newchd->codecintf[decompnum] == NULL && newchd->header.compression[decompnum] != 0) + { err = CHDERR_UNSUPPORTED_FORMAT; + (void)err; + } /* initialize the codec */ if (newchd->codecintf[decompnum]->init != NULL) @@ -1470,7 +1477,10 @@ chd_error chd_open_file(core_file *file, int mode, chd_file *parent, chd_file ** break; } if (codec != NULL) + { err = (*newchd->codecintf[decompnum]->init)(codec, newchd->header.hunkbytes); + (void)err; + } } } @@ -2438,6 +2448,7 @@ static chd_error zlib_codec_decompress(void *codec, const uint8_t *src, uint32_t /* do it */ zerr = inflate(&data->inflater, Z_FINISH); + (void)zerr; if (data->inflater.total_out != destlen) return CHDERR_DECOMPRESSION_ERROR; diff --git a/formats/png/rpng.c b/formats/png/rpng.c index 56c506d..794f229 100644 --- a/formats/png/rpng.c +++ b/formats/png/rpng.c @@ -92,28 +92,27 @@ struct rpng_process bool inflate_initialized; bool adam7_pass_initialized; bool pass_initialized; - uint32_t *data; - uint32_t *palette; - struct png_ihdr ihdr; uint8_t *prev_scanline; uint8_t *decoded_scanline; uint8_t *inflate_buf; + struct png_ihdr ihdr; size_t restore_buf_size; size_t adam7_restore_buf_size; size_t data_restore_buf_size; size_t inflate_buf_size; + size_t avail_in; + size_t avail_out; + size_t total_out; + size_t pass_size; unsigned bpp; unsigned pitch; unsigned h; - struct - { - unsigned width; - unsigned height; - size_t size; - unsigned pos; - } pass; + unsigned pass_width; + unsigned pass_height; + unsigned pass_pos; + uint32_t *data; + uint32_t *palette; void *stream; - size_t avail_in, avail_out, total_out; const struct trans_stream_backend *stream_backend; }; @@ -518,29 +517,29 @@ static int png_reverse_filter_init(const struct png_ihdr *ihdr, if (!pngp->adam7_pass_initialized && ihdr->interlace) { - if (ihdr->width <= passes[pngp->pass.pos].x || - ihdr->height <= passes[pngp->pass.pos].y) /* Empty pass */ + if (ihdr->width <= passes[pngp->pass_pos].x || + ihdr->height <= passes[pngp->pass_pos].y) /* Empty pass */ return 1; - pngp->pass.width = (ihdr->width - - passes[pngp->pass.pos].x + passes[pngp->pass.pos].stride_x - 1) / passes[pngp->pass.pos].stride_x; - pngp->pass.height = (ihdr->height - passes[pngp->pass.pos].y + - passes[pngp->pass.pos].stride_y - 1) / passes[pngp->pass.pos].stride_y; + pngp->pass_width = (ihdr->width - + passes[pngp->pass_pos].x + passes[pngp->pass_pos].stride_x - 1) / passes[pngp->pass_pos].stride_x; + pngp->pass_height = (ihdr->height - passes[pngp->pass_pos].y + + passes[pngp->pass_pos].stride_y - 1) / passes[pngp->pass_pos].stride_y; pngp->data = (uint32_t*)malloc( - pngp->pass.width * pngp->pass.height * sizeof(uint32_t)); + pngp->pass_width * pngp->pass_height * sizeof(uint32_t)); if (!pngp->data) return -1; pngp->ihdr = *ihdr; - pngp->ihdr.width = pngp->pass.width; - pngp->ihdr.height = pngp->pass.height; + pngp->ihdr.width = pngp->pass_width; + pngp->ihdr.height = pngp->pass_height; - png_pass_geom(&pngp->ihdr, pngp->pass.width, - pngp->pass.height, NULL, NULL, &pngp->pass.size); + png_pass_geom(&pngp->ihdr, pngp->pass_width, + pngp->pass_height, NULL, NULL, &pngp->pass_size); - if (pngp->pass.size > pngp->total_out) + if (pngp->pass_size > pngp->total_out) { free(pngp->data); return -1; @@ -561,8 +560,8 @@ static int png_reverse_filter_init(const struct png_ihdr *ihdr, pngp->restore_buf_size = 0; pngp->data_restore_buf_size = 0; - pngp->prev_scanline = (uint8_t*)calloc(1, pngp->pitch); - pngp->decoded_scanline = (uint8_t*)calloc(1, pngp->pitch); + pngp->prev_scanline = (uint8_t*)calloc(1, pngp->pitch); + pngp->decoded_scanline = (uint8_t*)calloc(1, pngp->pitch); if (!pngp->prev_scanline || !pngp->decoded_scanline) goto error; @@ -686,7 +685,7 @@ static int png_reverse_filter_adam7_iterate(uint32_t **data_, struct rpng_process *pngp) { int ret = 0; - bool to_next = pngp->pass.pos < ARRAY_SIZE(passes); + bool to_next = pngp->pass_pos < ARRAY_SIZE(passes); uint32_t *data = *data_; if (!to_next) @@ -710,19 +709,19 @@ static int png_reverse_filter_adam7_iterate(uint32_t **data_, if (ret == IMAGE_PROCESS_ERROR || ret == IMAGE_PROCESS_ERROR_END) return IMAGE_PROCESS_ERROR; - pngp->inflate_buf += pngp->pass.size; - pngp->adam7_restore_buf_size += pngp->pass.size; + pngp->inflate_buf += pngp->pass_size; + pngp->adam7_restore_buf_size += pngp->pass_size; - pngp->total_out -= pngp->pass.size; + pngp->total_out -= pngp->pass_size; png_reverse_filter_adam7_deinterlace_pass(data, - ihdr, pngp->data, pngp->pass.width, pngp->pass.height, &passes[pngp->pass.pos]); + ihdr, pngp->data, pngp->pass_width, pngp->pass_height, &passes[pngp->pass_pos]); free(pngp->data); - pngp->pass.width = 0; - pngp->pass.height = 0; - pngp->pass.size = 0; + pngp->pass_width = 0; + pngp->pass_height = 0; + pngp->pass_size = 0; pngp->adam7_pass_initialized = false; return IMAGE_PROCESS_NEXT; @@ -741,7 +740,7 @@ static int png_reverse_filter_adam7(uint32_t **data_, case IMAGE_PROCESS_END: break; case IMAGE_PROCESS_NEXT: - pngp->pass.pos++; + pngp->pass_pos++; return 0; case IMAGE_PROCESS_ERROR: if (pngp->data) @@ -751,8 +750,8 @@ static int png_reverse_filter_adam7(uint32_t **data_, return -1; } - pngp->inflate_buf -= pngp->adam7_restore_buf_size; - pngp->adam7_restore_buf_size = 0; + pngp->inflate_buf -= pngp->adam7_restore_buf_size; + pngp->adam7_restore_buf_size = 0; return ret; } @@ -868,11 +867,44 @@ bool png_realloc_idat(const struct png_chunk *chunk, struct idat_buffer *buf) static struct rpng_process *rpng_process_init(rpng_t *rpng, unsigned *width, unsigned *height) { uint8_t *inflate_buf = NULL; - struct rpng_process *process = (struct rpng_process*)calloc(1, sizeof(*process)); + struct rpng_process *process = (struct rpng_process*)malloc(sizeof(*process)); if (!process) return NULL; + process->inflate_initialized = false; + process->adam7_pass_initialized = false; + process->pass_initialized = false; + process->prev_scanline = NULL; + process->decoded_scanline = NULL; + process->inflate_buf = NULL; + + process->ihdr.width = 0; + process->ihdr.height = 0; + process->ihdr.depth = 0; + process->ihdr.color_type = 0; + process->ihdr.compression = 0; + process->ihdr.filter = 0; + process->ihdr.interlace = 0; + + process->restore_buf_size = 0; + process->restore_buf_size = 0; + process->adam7_restore_buf_size = 0; + process->data_restore_buf_size = 0; + process->inflate_buf_size = 0; + process->avail_in = 0; + process->avail_out = 0; + process->total_out = 0; + process->pass_size = 0; + process->bpp = 0; + process->pitch = 0; + process->h = 0; + process->pass_width = 0; + process->pass_height = 0; + process->pass_pos = 0; + process->data = NULL; + process->palette = NULL; + process->stream = NULL; process->stream_backend = trans_stream_get_zlib_inflate_backend(); png_pass_geom(&rpng->ihdr, rpng->ihdr.width, diff --git a/include/file/archive_file.h b/include/file/archive_file.h index 8abe221..b2b5971 100644 --- a/include/file/archive_file.h +++ b/include/file/archive_file.h @@ -60,13 +60,13 @@ typedef struct file_archive_file_data file_archive_file_data_t; typedef struct file_archive_transfer { + enum file_archive_transfer_type type; + int32_t archive_size; file_archive_file_data_t *handle; void *stream; const uint8_t *footer; const uint8_t *directory; const uint8_t *data; - int32_t archive_size; - enum file_archive_transfer_type type; const struct file_archive_file_backend *backend; } file_archive_transfer_t; diff --git a/include/streams/chd_stream.h b/include/streams/chd_stream.h index 9abece7..2f51cae 100644 --- a/include/streams/chd_stream.h +++ b/include/streams/chd_stream.h @@ -32,11 +32,11 @@ RETRO_BEGIN_DECLS typedef struct chdstream chdstream_t; -// First data track +/* First data track */ #define CHDSTREAM_TRACK_FIRST_DATA (-1) -// Last track +/* Last track */ #define CHDSTREAM_TRACK_LAST (-2) -// Primary (largest) data track, used for CRC identification purposes +/* Primary (largest) data track, used for CRC identification purposes */ #define CHDSTREAM_TRACK_PRIMARY (-3) chdstream_t *chdstream_open(const char *path, int32_t track); diff --git a/include/streams/interface_stream.h b/include/streams/interface_stream.h index 7eb44c2..7eda0e7 100644 --- a/include/streams/interface_stream.h +++ b/include/streams/interface_stream.h @@ -32,8 +32,6 @@ RETRO_BEGIN_DECLS -typedef struct _chd_file chd_file; - enum intfstream_type { INTFSTREAM_FILE = 0, @@ -56,7 +54,7 @@ typedef struct intfstream_info } memory; struct { - chd_file *handle; + void *handle; int32_t track; } chd; enum intfstream_type type; diff --git a/streams/chd_stream.c b/streams/chd_stream.c index a1599c9..1c569cf 100644 --- a/streams/chd_stream.c +++ b/streams/chd_stream.c @@ -87,7 +87,8 @@ chdstream_get_meta(chd_file *chd, int idx, metadata_t *md) memset(md, 0, sizeof(*md)); err = chd_get_metadata(chd, CDROM_TRACK_METADATA2_TAG, idx, meta, - sizeof(meta), &meta_size, NULL, NULL); + sizeof(meta), &meta_size, NULL, NULL); + if (err == CHDERR_NONE) { sscanf(meta, CDROM_TRACK_METADATA2_FORMAT, @@ -100,7 +101,8 @@ chdstream_get_meta(chd_file *chd, int idx, metadata_t *md) } err = chd_get_metadata(chd, CDROM_TRACK_METADATA_TAG, idx, meta, - sizeof(meta), &meta_size, NULL, NULL); + sizeof(meta), &meta_size, NULL, NULL); + if (err == CHDERR_NONE) { sscanf(meta, CDROM_TRACK_METADATA_FORMAT, &md->track, md->type, @@ -110,7 +112,8 @@ chdstream_get_meta(chd_file *chd, int idx, metadata_t *md) } err = chd_get_metadata(chd, GDROM_TRACK_METADATA_TAG, idx, meta, - sizeof(meta), &meta_size, NULL, NULL); + sizeof(meta), &meta_size, NULL, NULL); + if (err == CHDERR_NONE) { sscanf(meta, GDROM_TRACK_METADATA_FORMAT, &md->track, md->type, @@ -132,9 +135,7 @@ chdstream_find_track_number(chd_file *fd, int32_t track, metadata_t *meta) for (i = 0; true; ++i) { if (!chdstream_get_meta(fd, i, meta)) - { return false; - } if (track == meta->track) { @@ -156,24 +157,29 @@ chdstream_find_special_track(chd_file *fd, int32_t track, metadata_t *meta) for (i = 1; true; ++i) { - if (!chdstream_find_track_number(fd, i, &iter)) { - if (track == CHDSTREAM_TRACK_LAST && i > 1) { + if (!chdstream_find_track_number(fd, i, &iter)) + { + if (track == CHDSTREAM_TRACK_LAST && i > 1) + { *meta = iter; return true; - } else if (track == CHDSTREAM_TRACK_PRIMARY && largest_track != 0) { - return chdstream_find_track_number(fd, largest_track, meta); } + else if (track == CHDSTREAM_TRACK_PRIMARY && largest_track != 0) + return chdstream_find_track_number(fd, largest_track, meta); } - switch (track) { + switch (track) + { case CHDSTREAM_TRACK_FIRST_DATA: - if (strcmp(iter.type, "AUDIO")) { + if (strcmp(iter.type, "AUDIO")) + { *meta = iter; return true; } break; case CHDSTREAM_TRACK_PRIMARY: - if (strcmp(iter.type, "AUDIO") && iter.frames > largest_size) { + if (strcmp(iter.type, "AUDIO") && iter.frames > largest_size) + { largest_size = iter.frames; largest_track = iter.track; } @@ -187,21 +193,19 @@ chdstream_find_special_track(chd_file *fd, int32_t track, metadata_t *meta) static bool chdstream_find_track(chd_file *fd, int32_t track, metadata_t *meta) { - if (track < 0) { + if (track < 0) return chdstream_find_special_track(fd, track, meta); - } else { - return chdstream_find_track_number(fd, track, meta); - } + return chdstream_find_track_number(fd, track, meta); } chdstream_t *chdstream_open(const char *path, int32_t track) { + metadata_t meta; uint32_t pregap = 0; const chd_header *hd = NULL; chdstream_t *stream = NULL; chd_file *chd = NULL; chd_error err = chd_open(path, CHD_OPEN_READ, NULL, &chd); - metadata_t meta; if (err != CHDERR_NONE) goto error; diff --git a/streams/file_stream.c b/streams/file_stream.c index 7a911bc..d022c04 100644 --- a/streams/file_stream.c +++ b/streams/file_stream.c @@ -62,6 +62,7 @@ #endif #include +#include #include #include @@ -567,7 +568,7 @@ int filestream_close(RFILE *stream) if (!stream) goto error; - if (stream->ext) + if (stream->ext && !string_is_empty(stream->ext)) free(stream->ext); #if defined(PSP) diff --git a/streams/interface_stream.c b/streams/interface_stream.c index b9cf8cd..a2fe9ac 100644 --- a/streams/interface_stream.c +++ b/streams/interface_stream.c @@ -25,7 +25,9 @@ #include #include #include +#ifdef HAVE_CHD #include +#endif struct intfstream_internal { @@ -46,11 +48,13 @@ struct intfstream_internal memstream_t *fp; bool writable; } memory; +#ifdef HAVE_CHD struct { int32_t track; chdstream_t *fp; } chd; +#endif }; bool intfstream_resize(intfstream_internal_t *intf, intfstream_info_t *info) diff --git a/streams/trans_stream_zlib.c b/streams/trans_stream_zlib.c index 6e21795..b9f6a99 100644 --- a/streams/trans_stream_zlib.c +++ b/streams/trans_stream_zlib.c @@ -29,30 +29,34 @@ struct zlib_trans_stream { - z_stream z; - int ex; /* window_bits or level */ bool inited; + int ex; /* window_bits or level */ + z_stream z; }; static void *zlib_deflate_stream_new(void) { struct zlib_trans_stream *ret = (struct zlib_trans_stream*)calloc(1, sizeof(struct zlib_trans_stream)); - if (ret) - ret->ex = 9; + if (!ret) + return NULL; + 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)); - if (ret) - ret->ex = MAX_WBITS; + if (!ret) + return NULL; + ret->ex = MAX_WBITS; return (void *) ret; } static void zlib_deflate_stream_free(void *data) { struct zlib_trans_stream *z = (struct zlib_trans_stream *) data; + if (!z) + return; if (z->inited) deflateEnd(&z->z); free(z); @@ -73,7 +77,8 @@ static bool zlib_deflate_define(void *data, const char *prop, uint32_t val) struct zlib_trans_stream *z = (struct zlib_trans_stream *) data; if (string_is_equal_fast(prop, "level", 5)) { - z->ex = (int) val; + if (z) + z->ex = (int) val; return true; } return false; @@ -84,7 +89,8 @@ static bool zlib_inflate_define(void *data, const char *prop, uint32_t val) struct zlib_trans_stream *z = (struct zlib_trans_stream *) data; if (string_is_equal_fast(prop, "window_bits", 11)) { - z->ex = (int) val; + if (z) + z->ex = (int) val; return true; } return false; @@ -139,11 +145,12 @@ static bool zlib_deflate_trans( uint32_t *rd, uint32_t *wn, enum trans_stream_error *error) { - int zret; - bool ret; - uint32_t pre_avail_in, pre_avail_out; + int zret = 0; + bool ret = false; + uint32_t pre_avail_in = 0; + uint32_t pre_avail_out = 0; struct zlib_trans_stream *zt = (struct zlib_trans_stream *) data; - z_stream *z = &zt->z; + z_stream *z = &zt->z; if (!zt->inited) { @@ -151,9 +158,9 @@ static bool zlib_deflate_trans( zt->inited = true; } - pre_avail_in = z->avail_in; + pre_avail_in = z->avail_in; pre_avail_out = z->avail_out; - zret = deflate(z, flush ? Z_FINISH : Z_NO_FLUSH); + zret = deflate(z, flush ? Z_FINISH : Z_NO_FLUSH); if (zret == Z_OK) { @@ -202,10 +209,11 @@ static bool zlib_inflate_trans( enum trans_stream_error *error) { int zret; - bool ret; - uint32_t pre_avail_in, pre_avail_out; + bool ret = false; + uint32_t pre_avail_in = 0; + uint32_t pre_avail_out = 0; struct zlib_trans_stream *zt = (struct zlib_trans_stream *) data; - z_stream *z = &zt->z; + z_stream *z = &zt->z; if (!zt->inited) {