From: jiyong.min Date: Thu, 21 Mar 2019 01:22:28 +0000 (+0900) Subject: Rename variables for buffer I/O in gif and png X-Git-Tag: accepted/tizen/unified/20190326.073434~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F10%2F201910%2F2;p=platform%2Fcore%2Fmultimedia%2Flibmm-utility.git Rename variables for buffer I/O in gif and png Change-Id: Ic30eb5dbd9731e7f7fd13561407741f348f36bf0 --- diff --git a/gif/include/mm_util_gif_private.h b/gif/include/mm_util_gif_private.h index 22d4ca0..fe487f2 100755 --- a/gif/include/mm_util_gif_private.h +++ b/gif/include/mm_util_gif_private.h @@ -30,17 +30,17 @@ extern "C" { #endif typedef struct { - size_t size; - size_t mem_size; - void *mem; -} gif_mem_s; + void *buf; + size_t buf_size; + size_t pos; +} gif_io_buf_s; typedef struct { GifFileType *GifFile; /**< GifFile opened */ char *filename; void **enc_buffer; /**< Encoded output data attached to callback */ size_t *enc_buffer_size; - gif_mem_s write_data_ptr; /**< Encoded output data attached to callback */ + gif_io_buf_s io_buf; } gif_file_s; #ifdef __cplusplus diff --git a/gif/mm_util_gif.c b/gif/mm_util_gif.c index 7806d27..9fbe6b8 100755 --- a/gif/mm_util_gif.c +++ b/gif/mm_util_gif.c @@ -66,15 +66,15 @@ static int __convert_gif_to_rgba(void **data, ColorMapObject *color_map, GifRowT static int __read_function(GifFileType *gft, GifByteType *data, int size) { - gif_mem_s *read_data_ptr = (gif_mem_s *) gft->UserData; + gif_io_buf_s *io_buf = (gif_io_buf_s *) gft->UserData; - mm_util_retvm_if(size <= 0, 0, "Failed to read memory due to size(%d).", size); - mm_util_retvm_if(read_data_ptr == NULL, 0, "Failed to read memory due to invalid input data."); - mm_util_retvm_if(read_data_ptr->mem == NULL, 0, "Failed to read memory due to invalid input buffer."); - mm_util_retvm_if(read_data_ptr->size + size > read_data_ptr->mem_size, 0, "Failed to read memory due to buffer overflow(%zu/%d/%zu).", read_data_ptr->size, size, read_data_ptr->mem_size); + mm_util_retvm_if(!io_buf || !io_buf->buf || io_buf->buf_size == 0, 0, "Invalid io_buf"); + mm_util_retvm_if(!data, 0, "Invalid data"); + mm_util_retvm_if(size <= 0, 0, "Invalid size [%d]", size); + mm_util_retvm_if(io_buf->pos + size > io_buf->buf_size, 0, "Invalid read due to buffer overflow [%zu/%d/%zu].", io_buf->pos, size, io_buf->buf_size); - memcpy(data, read_data_ptr->mem + read_data_ptr->size, size); - read_data_ptr->size += size; + memcpy(data, io_buf->buf + io_buf->pos, size); + io_buf->pos += size; return size; } @@ -90,7 +90,7 @@ static int __read_gif(const char *file_path, void *memory, const size_t src_size GifFileType *GifFile; unsigned int image_num = 0; ColorMapObject *ColorMap; - gif_mem_s read_data_ptr; + gif_io_buf_s io_buf = { memory, src_size, 0 }; void *_data = NULL; mm_util_retvm_if(!MMUTIL_STRING_VALID(file_path) && (memory == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid gif image"); @@ -106,10 +106,7 @@ static int __read_gif(const char *file_path, void *memory, const size_t src_size } } else { mm_util_debug("read from memory"); - read_data_ptr.mem = memory; - read_data_ptr.mem_size = src_size; - read_data_ptr.size = 0; - if ((GifFile = DGifOpen(&read_data_ptr, __read_function, NULL)) == NULL) { + if ((GifFile = DGifOpen(&io_buf, __read_function, NULL)) == NULL) { mm_util_error("could not open Gif File"); return MM_UTIL_ERROR_INVALID_OPERATION; } @@ -279,16 +276,17 @@ int mm_util_decode_from_gif_memory(void *memory, const size_t src_size, mm_util_ static int __write_function(GifFileType *gft, const GifByteType *data, int size) { - gif_mem_s *write_data_ptr = (gif_mem_s *) gft->UserData; + gif_io_buf_s *io_buf = (gif_io_buf_s *) gft->UserData; - mm_util_retvm_if(size <= 0, 0, "Failed to write memory due to size(%d).", size); - mm_util_retvm_if(write_data_ptr == NULL, 0, "Failed to write memory due to invalid output data."); + mm_util_retvm_if(!io_buf, 0, "Invalid io_buf"); + mm_util_retvm_if(!data, 0, "Invalid data"); + mm_util_retvm_if(size <= 0, 0, "Invalid size"); - write_data_ptr->mem = (void *)realloc(write_data_ptr->mem, (write_data_ptr->size + size)); - mm_util_retvm_if(write_data_ptr->mem == NULL, 0, "Failed to write memory due to allocation failure."); + io_buf->buf = (void *)realloc(io_buf->buf, sizeof(GifByteType) * (io_buf->buf_size + size)); + mm_util_retvm_if(!io_buf->buf, 0, "Memory allocation failed"); - memcpy(write_data_ptr->mem + write_data_ptr->size, data, size); - write_data_ptr->size += size; + memcpy(io_buf->buf + io_buf->buf_size, data, size); + io_buf->buf_size += size; return size; } @@ -391,10 +389,10 @@ int _gif_encode_open_mem(gif_file_s *gif_file) mm_util_fenter(); mm_util_retvm_if(gif_file == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - gif_file->write_data_ptr.mem = NULL; - gif_file->write_data_ptr.size = 0; + gif_file->io_buf.buf = NULL; + gif_file->io_buf.buf_size = 0; - if ((gif_file->GifFile = EGifOpen(&(gif_file->write_data_ptr), __write_function, NULL)) == NULL) { + if ((gif_file->GifFile = EGifOpen(&(gif_file->io_buf), __write_function, NULL)) == NULL) { mm_util_error("could not open File"); return MM_UTIL_ERROR_INVALID_OPERATION; } @@ -697,11 +695,11 @@ int mm_util_gif_encode_save(mm_gif_file_h gif_file_h) if (gif_file->filename != NULL) { ret = _gif_encode_move_to_file(gif_file->filename); mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "__move_tmp_to_origin failed"); - } else if (gif_file->write_data_ptr.mem != NULL) { - ret = _gif_encode_move_to_mem(gif_file->write_data_ptr.mem, gif_file->write_data_ptr.size, + } else if (gif_file->io_buf.buf != NULL) { + ret = _gif_encode_move_to_mem(gif_file->io_buf.buf, gif_file->io_buf.buf_size, gif_file->enc_buffer, gif_file->enc_buffer_size); - MMUTIL_SAFE_FREE(gif_file->write_data_ptr.mem); - gif_file->write_data_ptr.size = 0; + MMUTIL_SAFE_FREE(gif_file->io_buf.buf); + gif_file->io_buf.buf_size = 0; mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "_gif_encode_move_to_origin_mem failed"); } @@ -811,6 +809,6 @@ void mm_util_gif_encode_destroy(mm_gif_file_h gif_file_h) } MMUTIL_SAFE_G_FREE(gif_file->filename); - MMUTIL_SAFE_FREE(gif_file->write_data_ptr.mem); + MMUTIL_SAFE_FREE(gif_file->io_buf.buf); MMUTIL_SAFE_FREE(gif_file); } diff --git a/png/mm_util_png.c b/png/mm_util_png.c index 1ba36c2..172930d 100755 --- a/png/mm_util_png.c +++ b/png/mm_util_png.c @@ -45,10 +45,10 @@ typedef struct { typedef struct { - void *mem; - png_size_t mem_size; - png_size_t size; -} png_mem_s; + void *buf; + png_size_t buf_size; + png_size_t pos; +} png_io_buf_s; static void __user_error_fn(png_structp png_ptr, png_const_charp error_msg) { @@ -62,17 +62,15 @@ static void __user_warning_fn(png_structp png_ptr, png_const_charp warning_msg) static void __read_data_fn(png_structp png_ptr, png_bytep data, png_size_t size) { - png_mem_s *png_mem_ptr = (png_mem_s *) png_get_io_ptr(png_ptr); + png_io_buf_s *io_buf = (png_io_buf_s *) png_get_io_ptr(png_ptr); - if (png_mem_ptr == NULL || data == NULL) - return; + mm_util_retm_if(!io_buf || !io_buf->buf || io_buf->buf_size == 0, "Invalid io_buf"); + mm_util_retm_if(!data, "Invalid data"); + mm_util_retm_if(size == 0, "Invalid size [%d]", size); + mm_util_retm_if(io_buf->pos + size > io_buf->buf_size, "Invalid read due to buffer overflow [%zu/%d/%zu].", io_buf->pos, size, io_buf->buf_size); - mm_util_debug("[TEMP][RDFN] %p, %zu, %zu, %p, %zu", png_mem_ptr->mem, png_mem_ptr->mem_size, png_mem_ptr->size, data, size); - - if (png_mem_ptr->mem && size > 0 && png_mem_ptr->mem_size >= png_mem_ptr->size + size) { - memcpy(data, png_mem_ptr->mem + png_mem_ptr->size, size); - png_mem_ptr->size += size; - } + memcpy(data, io_buf->buf + io_buf->pos, size); + io_buf->pos += size; } static void __flush_data_fn(png_structp png_ptr G_GNUC_UNUSED) @@ -80,18 +78,19 @@ static void __flush_data_fn(png_structp png_ptr G_GNUC_UNUSED) /* do nothing */ } -static void __write_data_fn(png_structp png_ptr, png_bytep data, png_uint_32 length) +static void __write_data_fn(png_structp png_ptr, png_bytep data, png_uint_32 size) { - png_mem_s *png_mem_ptr = (png_mem_s *) png_get_io_ptr(png_ptr); + png_io_buf_s *io_buf = (png_io_buf_s *) png_get_io_ptr(png_ptr); - if (png_mem_ptr == NULL || data == NULL) - return; + mm_util_retm_if(!io_buf, "Invalid io_buf"); + mm_util_retm_if(!data, "Invalid data"); + mm_util_retm_if(size == 0, "Invalid size"); - png_mem_ptr->mem = (void *)realloc(png_mem_ptr->mem, sizeof(png_bytep) * (png_mem_ptr->size + length)); - if (png_mem_ptr->mem) { - memcpy(png_mem_ptr->mem + png_mem_ptr->size, data, length); - png_mem_ptr->size += length; - } + io_buf->buf = (void *)realloc(io_buf->buf, sizeof(png_bytep) * (io_buf->buf_size + size)); + mm_util_retm_if(!io_buf->buf, "Memory allocation failed"); + + memcpy(io_buf->buf + io_buf->buf_size, data, size); + io_buf->buf_size += size; } static void __get_property(png_property_s *png_prop, png_structp png_ptr, png_infop info_ptr) @@ -149,7 +148,7 @@ static int __read_png(FILE *fp, void *memory, const size_t memory_size, mm_util_ png_structp png_ptr; png_infop info_ptr; guint row_index; - png_mem_s png_mem_ptr = {memory, memory_size, 0}; + png_io_buf_s io_buf = {memory, memory_size, 0}; png_property_s png_prop; void *_data = NULL; @@ -183,7 +182,7 @@ static int __read_png(FILE *fp, void *memory, const size_t memory_size, mm_util_ if (fp) png_init_io(png_ptr, fp); else - png_set_read_fn(png_ptr, &png_mem_ptr, __read_data_fn); + png_set_read_fn(png_ptr, &io_buf, __read_data_fn); /* get properties(header) to png_prop */ __get_property(&png_prop, png_ptr, info_ptr); @@ -239,7 +238,7 @@ static int __write_png(mm_util_image_h *decoded, mm_util_png_compression compres guint row_index; static png_bytepp row_pointers = NULL; png_size_t rowbytes = 0; - png_mem_s png_mem_ptr = {NULL, 0}; + png_io_buf_s io_buf = {NULL, 0}; png_property_s png_prop; mm_util_retvm_if(!decoded, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image src"); @@ -281,7 +280,7 @@ static int __write_png(mm_util_image_h *decoded, mm_util_png_compression compres if (fp) png_init_io(png_ptr, fp); else - png_set_write_fn(png_ptr, &png_mem_ptr, (png_rw_ptr) __write_data_fn, __flush_data_fn); + png_set_write_fn(png_ptr, &io_buf, (png_rw_ptr) __write_data_fn, __flush_data_fn); /* set properties(header) from png_prop */ png_set_filter(png_ptr, 0, png_prop.filter); @@ -309,9 +308,9 @@ static int __write_png(mm_util_image_h *decoded, mm_util_png_compression compres png_destroy_write_struct(&png_ptr, &info_ptr); /* set result */ - if (!fp && png_mem_ptr.mem) { - *buffer = (void *)png_mem_ptr.mem; - *size = png_mem_ptr.size; + if (!fp && io_buf.buf) { + *buffer = (void *)io_buf.buf; + *size = io_buf.buf_size; } mm_util_fleave();