From 9e430cd8b996fe254e71269e7f3fc4296c51d3e8 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Sun, 10 Jul 2022 19:06:18 +0200 Subject: [PATCH] (libretro-common) Cleanups: * Work more with return values, prevent unnecessary local stack variables when possible * Reduce lines of code * Move local stack variables to proper scope --- libretro-common/dynamic/dylib.c | 3 +- libretro-common/streams/file_stream.c | 50 +++----- libretro-common/streams/rzip_stream.c | 161 ++++++++----------------- libretro-common/streams/trans_stream.c | 3 +- 4 files changed, 73 insertions(+), 144 deletions(-) diff --git a/libretro-common/dynamic/dylib.c b/libretro-common/dynamic/dylib.c index 680ce58677..fa65d0ba6a 100644 --- a/libretro-common/dynamic/dylib.c +++ b/libretro-common/dynamic/dylib.c @@ -174,7 +174,8 @@ function_t dylib_proc(dylib_t lib, const char *proc) void *ptr_sym = NULL; sym = NULL; - if (lib) { + if (lib) + { sceKernelDlsym((SceKernelModule)lib, proc, &ptr_sym); memcpy(&sym, &ptr_sym, sizeof(void*)); } diff --git a/libretro-common/streams/file_stream.c b/libretro-common/streams/file_stream.c index 2ac5dbb83f..801e0e533f 100644 --- a/libretro-common/streams/file_stream.c +++ b/libretro-common/streams/file_stream.c @@ -109,18 +109,14 @@ bool filestream_exists(const char *path) if (!path || !*path) return false; - - dummy = filestream_open( + if (!(dummy = filestream_open( path, RETRO_VFS_FILE_ACCESS_READ, - RETRO_VFS_FILE_ACCESS_HINT_NONE); - - if (!dummy) + RETRO_VFS_FILE_ACCESS_HINT_NONE))) return false; if (filestream_close(dummy) != 0) - if (dummy) - free(dummy); + free(dummy); dummy = NULL; return true; @@ -226,9 +222,9 @@ int filestream_vscanf(RFILE *stream, const char* format, va_list *args) char buf[4096]; char subfmt[64]; va_list args_copy; - const char * bufiter = buf; + const char *bufiter = buf; int ret = 0; - int64_t startpos = filestream_tell(stream); + int64_t startpos = 0; int64_t maxlen = filestream_read(stream, buf, sizeof(buf)-1); if (maxlen <= 0) @@ -337,7 +333,8 @@ int filestream_vscanf(RFILE *stream, const char* format, va_list *args) } va_end(args_copy); - filestream_seek(stream, startpos+(bufiter-buf), + startpos = filestream_tell(stream); + filestream_seek(stream, startpos + (bufiter - buf), RETRO_VFS_SEEK_POSITION_START); return ret; @@ -513,9 +510,7 @@ int filestream_printf(RFILE *stream, const char* format, ...) int filestream_error(RFILE *stream) { - if (stream && stream->error_flag) - return 1; - return 0; + return (stream && stream->error_flag); } int filestream_close(RFILE *stream) @@ -566,20 +561,17 @@ int64_t filestream_read_file(const char *path, void **buf, int64_t *len) if (content_buf_size < 0) goto error; - content_buf = malloc((size_t)(content_buf_size + 1)); - - if (!content_buf) + if (!(content_buf = malloc((size_t)(content_buf_size + 1)))) goto error; if ((int64_t)(uint64_t)(content_buf_size + 1) != (content_buf_size + 1)) goto error; - ret = filestream_read(file, content_buf, (int64_t)content_buf_size); - if (ret < 0) + if ((ret = filestream_read(file, content_buf, (int64_t)content_buf_size)) < + 0) goto error; if (filestream_close(file) != 0) - if (file) - free(file); + free(file); *buf = content_buf; @@ -593,9 +585,8 @@ int64_t filestream_read_file(const char *path, void **buf, int64_t *len) return 1; error: - if (file) - if (filestream_close(file) != 0) - free(file); + if (filestream_close(file) != 0) + free(file); if (content_buf) free(content_buf); if (len) @@ -622,16 +613,10 @@ bool filestream_write_file(const char *path, const void *data, int64_t size) RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!file) return false; - ret = filestream_write(file, data, size); if (filestream_close(file) != 0) - if (file) - free(file); - - if (ret != size) - return false; - - return true; + free(file); + return (ret == size); } /* Returned pointer must be freed by the caller. */ @@ -657,9 +642,8 @@ char* filestream_getline(RFILE *stream) if (idx == cur_size) { cur_size *= 2; - newline_tmp = (char*)realloc(newline, cur_size + 1); - if (!newline_tmp) + if (!(newline_tmp = (char*)realloc(newline, cur_size + 1))) { free(newline); return NULL; diff --git a/libretro-common/streams/rzip_stream.c b/libretro-common/streams/rzip_stream.c index 0dccdb6643..c6d7878a09 100644 --- a/libretro-common/streams/rzip_stream.c +++ b/libretro-common/streams/rzip_stream.c @@ -88,18 +88,17 @@ static bool rzipstream_read_file_header(rzipstream_t *stream) header_bytes[i] = 0; /* Attempt to read header bytes */ - length = filestream_read(stream->file, header_bytes, sizeof(header_bytes)); - if (length <= 0) + if ((length = filestream_read(stream->file, header_bytes, sizeof(header_bytes))) <= 0) return false; /* If file length is less than header size * then assume this is uncompressed data */ - if (length < RZIP_HEADER_SIZE) - goto file_uncompressed; /* Check 'magic numbers' - first 8 bytes * of header */ - if ((header_bytes[0] != 35) || /* # */ + if ( + (length < RZIP_HEADER_SIZE) || + (header_bytes[0] != 35) || /* # */ (header_bytes[1] != 82) || /* R */ (header_bytes[2] != 90) || /* Z */ (header_bytes[3] != 73) || /* I */ @@ -107,41 +106,35 @@ static bool rzipstream_read_file_header(rzipstream_t *stream) (header_bytes[5] != 118) || /* v */ (header_bytes[6] != RZIP_VERSION) || /* file format version number */ (header_bytes[7] != 35)) /* # */ - goto file_uncompressed; + { + /* Reset file to start */ + filestream_seek(stream->file, 0, SEEK_SET); + /* Get 'raw' file size */ + stream->size = filestream_get_size(stream->file); + stream->is_compressed = false; + return true; + } /* Get uncompressed chunk size - next 4 bytes */ - stream->chunk_size = ((uint32_t)header_bytes[11] << 24) | + if ((stream->chunk_size = ((uint32_t)header_bytes[11] << 24) | ((uint32_t)header_bytes[10] << 16) | ((uint32_t)header_bytes[9] << 8) | - (uint32_t)header_bytes[8]; - if (stream->chunk_size == 0) + (uint32_t)header_bytes[8]) == 0) return false; /* Get total uncompressed data size - next 8 bytes */ - stream->size = ((uint64_t)header_bytes[19] << 56) | + if ((stream->size = ((uint64_t)header_bytes[19] << 56) | ((uint64_t)header_bytes[18] << 48) | ((uint64_t)header_bytes[17] << 40) | ((uint64_t)header_bytes[16] << 32) | ((uint64_t)header_bytes[15] << 24) | ((uint64_t)header_bytes[14] << 16) | ((uint64_t)header_bytes[13] << 8) | - (uint64_t)header_bytes[12]; - if (stream->size == 0) + (uint64_t)header_bytes[12]) == 0) return false; stream->is_compressed = true; return true; - -file_uncompressed: - - /* Reset file to start */ - filestream_seek(stream->file, 0, SEEK_SET); - - /* Get 'raw' file size */ - stream->size = filestream_get_size(stream->file); - - stream->is_compressed = false; - return true; } /* Writes header information to RZIP file @@ -150,7 +143,6 @@ file_uncompressed: static bool rzipstream_write_file_header(rzipstream_t *stream) { unsigned i; - int64_t length; uint8_t header_bytes[RZIP_HEADER_SIZE]; if (!stream) @@ -190,12 +182,8 @@ static bool rzipstream_write_file_header(rzipstream_t *stream) filestream_seek(stream->file, 0, SEEK_SET); /* Write header bytes */ - length = filestream_write(stream->file, - header_bytes, sizeof(header_bytes)); - if (length != RZIP_HEADER_SIZE) - return false; - - return true; + return (filestream_write(stream->file, + header_bytes, sizeof(header_bytes)) == RZIP_HEADER_SIZE); } /* Stream Initialisation/De-initialisation */ @@ -240,9 +228,8 @@ static bool rzipstream_init_stream( file_mode = RETRO_VFS_FILE_ACCESS_READ; /* Open file */ - stream->file = filestream_open( - path, file_mode, RETRO_VFS_FILE_ACCESS_HINT_NONE); - if (!stream->file) + if (!(stream->file = filestream_open( + path, file_mode, RETRO_VFS_FILE_ACCESS_HINT_NONE))) return false; /* If file is open for writing, output header @@ -266,12 +253,10 @@ static bool rzipstream_init_stream( if (stream->is_writing) { /* Compression */ - stream->deflate_backend = trans_stream_get_zlib_deflate_backend(); - if (!stream->deflate_backend) + if (!(stream->deflate_backend = trans_stream_get_zlib_deflate_backend())) return false; - stream->deflate_stream = stream->deflate_backend->stream_new(); - if (!stream->deflate_stream) + if (!(stream->deflate_stream = stream->deflate_backend->stream_new())) return false; /* Set compression level */ @@ -292,8 +277,8 @@ static bool rzipstream_init_stream( stream->out_buf_size; /* Redundant safety check */ - if ((stream->in_buf_size == 0) || - (stream->out_buf_size == 0)) + if ( (stream->in_buf_size == 0) + || (stream->out_buf_size == 0)) return false; } /* When reading, don't need an inflate transform @@ -301,12 +286,10 @@ static bool rzipstream_init_stream( else if (stream->is_compressed) { /* Decompression */ - stream->inflate_backend = trans_stream_get_zlib_inflate_backend(); - if (!stream->inflate_backend) + if (!(stream->inflate_backend = trans_stream_get_zlib_inflate_backend())) return false; - stream->inflate_stream = stream->inflate_backend->stream_new(); - if (!stream->inflate_stream) + if (!(stream->inflate_stream = stream->inflate_backend->stream_new())) return false; /* Buffers @@ -323,23 +306,21 @@ static bool rzipstream_init_stream( stream->out_buf_size = stream->chunk_size + (stream->chunk_size >> 2); /* Redundant safety check */ - if ((stream->in_buf_size == 0) || - (stream->out_buf_size == 0)) + if ( (stream->in_buf_size == 0) + || (stream->out_buf_size == 0)) return false; } /* Allocate buffers */ if (stream->in_buf_size > 0) { - stream->in_buf = (uint8_t *)calloc(stream->in_buf_size, 1); - if (!stream->in_buf) + if (!(stream->in_buf = (uint8_t *)calloc(stream->in_buf_size, 1))) return false; } if (stream->out_buf_size > 0) { - stream->out_buf = (uint8_t *)calloc(stream->out_buf_size, 1); - if (!stream->out_buf) + if (!(stream->out_buf = (uint8_t *)calloc(stream->out_buf_size, 1))) return false; } @@ -415,8 +396,7 @@ rzipstream_t* rzipstream_open(const char *path, unsigned mode) return NULL; /* Allocate stream object */ - stream = (rzipstream_t*)malloc(sizeof(*stream)); - if (!stream) + if (!(stream = (rzipstream_t*)malloc(sizeof(*stream)))) return NULL; stream->is_compressed = false; @@ -456,7 +436,6 @@ rzipstream_t* rzipstream_open(const char *path, unsigned mode) static bool rzipstream_read_chunk(rzipstream_t *stream) { unsigned i; - int64_t length; uint8_t chunk_header_bytes[RZIP_CHUNK_HEADER_SIZE]; uint32_t compressed_chunk_size; uint32_t inflate_read; @@ -469,9 +448,9 @@ static bool rzipstream_read_chunk(rzipstream_t *stream) chunk_header_bytes[i] = 0; /* Attempt to read chunk header bytes */ - length = filestream_read( - stream->file, chunk_header_bytes, sizeof(chunk_header_bytes)); - if (length != RZIP_CHUNK_HEADER_SIZE) + if (filestream_read( + stream->file, chunk_header_bytes, sizeof(chunk_header_bytes)) != + RZIP_CHUNK_HEADER_SIZE) return false; /* Get size of next compressed chunk */ @@ -500,9 +479,9 @@ static bool rzipstream_read_chunk(rzipstream_t *stream) } /* Read compressed chunk from file */ - length = filestream_read( - stream->file, stream->in_buf, compressed_chunk_size); - if (length != compressed_chunk_size) + if (filestream_read( + stream->file, stream->in_buf, compressed_chunk_size) != + compressed_chunk_size) return false; /* Decompress chunk data */ @@ -576,8 +555,8 @@ int64_t rzipstream_read(rzipstream_t *stream, void *data, int64_t len) /* Get amount of data to 'read out' this loop * > i.e. minimum of remaining output buffer * occupancy and remaining 'read data' size */ - read_size = stream->out_buf_occupancy - stream->out_buf_ptr; - if (read_size > data_len) + if ((read_size = stream->out_buf_occupancy - stream->out_buf_ptr) > + data_len) read_size = data_len; /* Copy as much cached data as possible into @@ -675,33 +654,26 @@ bool rzipstream_read_file(const char *path, void **buf, int64_t *len) return false; /* Attempt to open file */ - stream = rzipstream_open(path, RETRO_VFS_FILE_ACCESS_READ); - - if (!stream) + if (!(stream = rzipstream_open(path, RETRO_VFS_FILE_ACCESS_READ))) { *buf = NULL; return false; } /* Get file size */ - content_buf_size = rzipstream_get_size(stream); - - if (content_buf_size < 0) + if ((content_buf_size = rzipstream_get_size(stream)) < 0) goto error; if ((int64_t)(uint64_t)(content_buf_size + 1) != (content_buf_size + 1)) goto error; /* Allocate buffer */ - content_buf = malloc((size_t)(content_buf_size + 1)); - - if (!content_buf) + if (!(content_buf = malloc((size_t)(content_buf_size + 1)))) goto error; /* Read file contents */ - bytes_read = rzipstream_read(stream, content_buf, content_buf_size); - - if (bytes_read < 0) + if ((bytes_read = rzipstream_read(stream, content_buf, content_buf_size)) < + 0) goto error; /* Close file */ @@ -722,7 +694,6 @@ bool rzipstream_read_file(const char *path, void **buf, int64_t *len) return true; error: - if (stream) rzipstream_close(stream); stream = NULL; @@ -789,16 +760,14 @@ static bool rzipstream_write_chunk(rzipstream_t *stream) chunk_header_bytes[1] = (deflate_written >> 8) & 0xFF; chunk_header_bytes[0] = deflate_written & 0xFF; - length = filestream_write( - stream->file, chunk_header_bytes, sizeof(chunk_header_bytes)); - if (length != RZIP_CHUNK_HEADER_SIZE) + if ((length = filestream_write( + stream->file, chunk_header_bytes, sizeof(chunk_header_bytes))) != + RZIP_CHUNK_HEADER_SIZE) return false; /* Write compressed data to file */ - length = filestream_write( - stream->file, stream->out_buf, deflate_written); - - if (length != deflate_written) + if ((length = filestream_write( + stream->file, stream->out_buf, deflate_written)) != deflate_written) return false; /* Reset input buffer pointer */ @@ -831,8 +800,7 @@ int64_t rzipstream_write(rzipstream_t *stream, const void *data, int64_t len) /* Get amount of data to cache during this loop * > i.e. minimum of space remaining in input buffer * and remaining 'write data' size */ - cache_size = stream->in_buf_size - stream->in_buf_ptr; - if (cache_size > data_len) + if ((cache_size = stream->in_buf_size - stream->in_buf_ptr) > data_len) cache_size = data_len; /* Copy as much data as possible into @@ -919,9 +887,7 @@ bool rzipstream_write_file(const char *path, const void *data, int64_t len) return false; /* Attempt to open file */ - stream = rzipstream_open(path, RETRO_VFS_FILE_ACCESS_WRITE); - - if (!stream) + if (!(stream = rzipstream_open(path, RETRO_VFS_FILE_ACCESS_WRITE))) return false; /* Write contents of data buffer to file */ @@ -933,10 +899,7 @@ bool rzipstream_write_file(const char *path, const void *data, int64_t len) /* Check that the correct number of bytes * were written */ - if (bytes_written != len) - return false; - - return true; + return (bytes_written == len); } /* File Control */ @@ -977,12 +940,7 @@ void rzipstream_rewind(rzipstream_t *stream) /* Reset file position to first chunk location */ filestream_seek(stream->file, RZIP_HEADER_SIZE, SEEK_SET); if (filestream_error(stream->file)) - { - fprintf( - stderr, - "rzipstream_rewind(): Failed to reset file position...\n"); return; - } /* Reset pointers */ stream->virtual_ptr = 0; @@ -1011,21 +969,11 @@ void rzipstream_rewind(rzipstream_t *stream) /* Reset file position to first chunk location */ filestream_seek(stream->file, RZIP_HEADER_SIZE, SEEK_SET); if (filestream_error(stream->file)) - { - fprintf( - stderr, - "rzipstream_rewind(): Failed to reset file position...\n"); return; - } /* Read chunk */ if (!rzipstream_read_chunk(stream)) - { - fprintf( - stderr, - "rzipstream_rewind(): Failed to read first chunk of file...\n"); return; - } /* Reset pointers */ stream->virtual_ptr = 0; @@ -1079,10 +1027,7 @@ int64_t rzipstream_tell(rzipstream_t *stream) * compressed content */ bool rzipstream_is_compressed(rzipstream_t *stream) { - if (!stream) - return false; - - return stream->is_compressed; + return stream && stream->is_compressed; } /* File Close */ diff --git a/libretro-common/streams/trans_stream.c b/libretro-common/streams/trans_stream.c index 59ebb52d86..aecb457b66 100644 --- a/libretro-common/streams/trans_stream.c +++ b/libretro-common/streams/trans_stream.c @@ -48,8 +48,7 @@ bool trans_stream_trans_full( rdata = *data; else { - rdata = backend->stream_new(); - if (!rdata) + if (!(rdata = backend->stream_new())) { if (error) *error = TRANS_STREAM_ERROR_ALLOCATION_FAILURE;