--- /dev/null
+/*
+ * libmm-utility
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: YoungHun Kim <yh8004.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdio.h>
+
+#include "mm_util_private.h"
+
+int mm_util_safe_fopen(const char *path, const char *mode, FILE **fp)
+{
+ char *_realpath = NULL;
+
+ /* The input path was wrong, it returned no such file. */
+ mm_util_retvm_if((!MMUTIL_STRING_VALID(path)), MM_UTIL_ERROR_NO_SUCH_FILE, "invalid path");
+ mm_util_retvm_if((!MMUTIL_STRING_VALID(mode)), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid mode");
+ mm_util_retvm_if((fp == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid FILE*");
+
+ /*
+ * If the mode has writing mode('w', 'w+', 'r+', 'a', 'a+'),
+ * the symbolic link should be checked with realpath.
+ */
+ if ((g_strrstr(mode, "w") != NULL) || (g_strrstr(mode, "r+") != NULL) || (g_strrstr(mode, "a") != NULL)) {
+ if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
+ _realpath = realpath(path, NULL);
+ if (_realpath == NULL) {
+ mm_util_sec_debug("realpath failed [%s]", path);
+ mm_util_stderror("realpath failed");
+ return MM_UTIL_ERROR_NO_SUCH_FILE;
+ }
+ if (strncmp(path, _realpath, strlen(path))) {
+ mm_util_error("file is symbolic link");
+ MMUTIL_SAFE_FREE(_realpath);
+ return MM_UTIL_ERROR_NO_SUCH_FILE;
+ }
+
+ MMUTIL_SAFE_FREE(_realpath);
+ } else {
+ mm_util_sec_debug("file(%s) will be created", path);
+ }
+ }
+
+ if ((*fp = fopen(path, mode)) == NULL) {
+ mm_util_sec_debug("fopen failed [%s]", path);
+ mm_util_stderror("file open failed");
+ return MM_UTIL_ERROR_NO_SUCH_FILE;
+ }
+
+ return MM_UTIL_ERROR_NONE;
+}
+
+void mm_util_safe_fclose(FILE *fp)
+{
+ if (fp)
+ fclose(fp);
+}
}
}
-#if 0
-static void mm_image_codec_writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename)
-{
- FILE *file = fopen(filename, "wb");
- if (!file || fwrite(jpegBuf, jpegSize, 1, file) != 1) {
- mm_util_error("ERROR: Could not write to %s", filename);
- mm_util_stderror("ERROR: Could not write");
- }
-
- if (file) fclose(file);
-}
-#endif
-
static void __mm_decode_libjpeg_turbo_decompress(tjhandle handle, unsigned char *jpegBuf, unsigned long jpegSize, int TD_BU, mm_util_jpeg_yuv_data* decoded_data, mm_util_color_format_e input_fmt)
{
int _hdrw = 0, _hdrh = 0, _hdrsubsamp = -1;
int TD_BU = 0;
size_t read_size = 0;
- void *src = fopen(pFileName, "rb");
- if (src == NULL) {
- mm_util_error("Error [%s] failed", pFileName);
- mm_util_stderror("Error file open failed");
- return MM_UTIL_ERROR_NO_SUCH_FILE;
+ iErrorCode = mm_util_safe_fopen(pFileName, "rb", &src);
+ if (iErrorCode == MM_UTIL_ERROR_NONE) {
+ mm_util_error("mm_util_safe_fopen failed (%d)", iErrorCode);
+ return iErrorCode;
}
fseek(src, 0, SEEK_END);
jpegSize = ftell(src);
rewind(src);
if ((dhandle = tjInitDecompress()) == NULL) {
- fclose(src);
+ mm_util_safe_fclose(src);
mm_util_error("dhandle=tjInitDecompress()) is NULL");
return MM_UTIL_ERROR_INVALID_PARAMETER;
}
srcBuf = (unsigned char *) calloc(1, sizeof(char) * jpegSize);
if (srcBuf == NULL) {
- fclose(src);
+ mm_util_safe_fclose(src);
tjDestroy(dhandle);
mm_util_error("srcBuf is NULL");
return MM_UTIL_ERROR_OUT_OF_MEMORY;
read_size = fread(srcBuf, 1, jpegSize, src);
if (read_size <= 0) {
- fclose(src);
+ mm_util_safe_fclose(src);
tjDestroy(dhandle);
tjFree(srcBuf);
mm_util_error("Read from src failed");
mm_util_debug("srcBuf[0]: 0x%2x, srcBuf[1]: 0x%2x, jpegSize:%d", srcBuf[0], srcBuf[1], jpegSize);
__mm_decode_libjpeg_turbo_decompress(dhandle, srcBuf, jpegSize, TD_BU, decoded_data, input_fmt);
- fclose(src);
+ mm_util_safe_fclose(src);
mm_util_debug("fclose");
if (dhandle)
}
srcBuf = (unsigned char *) calloc(1, sizeof(char) * size);
if (srcBuf == NULL) {
- fclose(src);
+ mm_util_safe_fclose(src);
tjDestroy(dhandle);
mm_util_error("srcBuf is NULL");
return MM_UTIL_ERROR_OUT_OF_MEMORY;
unsigned char *dstBuf = NULL;
unsigned long size = 0;
int TD_BU = 0;
- FILE *fout = fopen(filename, "w+");
- if (fout == NULL) {
- mm_util_error("FILE OPEN FAIL [%s] failed", filename);
- mm_util_stderror("FILE OPEN FAIL");
- return MM_UTIL_ERROR_NO_SUCH_FILE;
+ FILE *fout = NULL;
+
+ iErrorCode = mm_util_safe_fopen(filename, "w+", &fout);
+ if (iErrorCode != MM_UTIL_ERROR_NONE) {
+ mm_util_error("mm_util_safe_fopen failed (%d)", iErrorCode);
+ return iErrorCode;
}
mm_util_debug("fmt: %d", fmt);
size = TJBUFSIZE(width, height);
if ((dstBuf = (unsigned char *)tjAlloc(size)) == NULL) {
- fclose(fout);
+ mm_util_safe_fclose(fout);
mm_util_error("dstBuf is NULL");
return MM_UTIL_ERROR_OUT_OF_MEMORY;
}
if ((chandle = tjInitCompress()) == NULL) {
- fclose(fout);
+ mm_util_safe_fclose(fout);
tjFree(dstBuf);
mm_util_error("dstBuf is NULL");
return MM_UTIL_ERROR_INVALID_PARAMETER;
_mm_encode_libjpeg_turbo_compress(chandle, src, &dstBuf, &size, width, height, quality, TD_BU, fmt);
mm_util_debug("dstBuf: %p\t size: %u", dstBuf, size);
fwrite(dstBuf, 1, size, fout);
- fclose(fout);
+ mm_util_safe_fclose(fout);
if (chandle)
tjDestroy(chandle);
if (dstBuf)
static int __mm_image_encode_to_jpeg_file_with_libjpeg(const char *pFileName, void *rawdata, int width, int height, mm_util_color_format_e fmt, int quality)
{
int iErrorCode = MM_UTIL_ERROR_NONE;
- char *realPATH = NULL;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW y[16], cb[16], cr[16]; /* y[2][5] = color sample of row 2 and pixel column 5; (one plane) */
JSAMPARRAY data[3]; /* t[0][2][5] = color sample 0 of row 2 and column 5 */
- if (!pFileName) {
- mm_util_error("pFileName");
- return MM_UTIL_ERROR_NO_SUCH_FILE;
- }
+
+ mm_util_retvm_if(!MMUTIL_STRING_VALID(pFileName), MM_UTIL_ERROR_NO_SUCH_FILE, "invalid path");
data[0] = y;
data[1] = cb;
jpeg_create_compress(&cinfo);
- if (g_file_test(pFileName, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
- realPATH = realpath(pFileName, NULL);
- if (realPATH == NULL) {
- mm_util_error("realpath failed [%s]", pFileName);
- mm_util_stderror("realpath failed");
- return MM_UTIL_ERROR_NO_SUCH_FILE;
- }
- if (!strncmp(pFileName, realPATH, strlen(pFileName))) {
- if ((fpWriter = fopen(pFileName, "wb")) == NULL) {
- mm_util_error("[infile] file open [%s] failed", pFileName);
- mm_util_stderror("file open failed");
- MMUTIL_SAFE_FREE(realPATH);
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
- } else {
- mm_util_error("[infile] file [%s] is symlink", pFileName);
- MMUTIL_SAFE_FREE(realPATH);
- return MM_UTIL_ERROR_NO_SUCH_FILE;
- }
+ iErrorCode = mm_util_safe_fopen(pFileName, "wb", &fpWriter);
+ if (iErrorCode != MM_UTIL_ERROR_NONE) {
+ mm_util_error("mm_util_safe_fopen failed (%d)", iErrorCode);
+ jpeg_finish_compress(&cinfo);
+ mm_util_debug("jpeg_finish_compress");
- MMUTIL_SAFE_FREE(realPATH);
- } else {
- if ((fpWriter = fopen(pFileName, "wb")) == NULL) {
- mm_util_error("[infile] file open [%s] failed", pFileName);
- mm_util_stderror("file open failed");
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
+ jpeg_destroy_compress(&cinfo);
+ mm_util_debug("jpeg_destroy_compress");
+ return iErrorCode;
}
jpeg_stdio_dest(&cinfo, fpWriter);
memset(large_rect, 0x10, width);
} else {
MMUTIL_SAFE_FREE(small_rect);
- fclose(fpWriter);
+ mm_util_safe_fclose(fpWriter);
mm_util_error("large rectangle memory");
return MM_UTIL_ERROR_INVALID_PARAMETER;
}
memset(small_rect, 0x80, width);
} else {
MMUTIL_SAFE_FREE(large_rect);
- fclose(fpWriter);
+ mm_util_safe_fclose(fpWriter);
mm_util_error("small rectangle memory");
return MM_UTIL_ERROR_INVALID_PARAMETER;
}
mm_util_debug("jpeg_destroy_compress");
} else {
mm_util_error("We can't encode the IMAGE format");
- fclose(fpWriter);
+ mm_util_safe_fclose(fpWriter);
return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
}
fsync((int)(fpWriter->_fileno));
mm_util_debug("[fsync] FILE");
- fclose(fpWriter);
+ mm_util_safe_fclose(fpWriter);
return iErrorCode;
}
mm_util_fenter();
- if (!pFileName) {
- mm_util_error("pFileName");
- return MM_UTIL_ERROR_NO_SUCH_FILE;
- }
+ mm_util_retvm_if(!MMUTIL_STRING_VALID(pFileName), MM_UTIL_ERROR_NO_SUCH_FILE, "invalid path");
+
if (decoded_data == NULL) {
mm_util_error("decoded_data");
return MM_UTIL_ERROR_INVALID_PARAMETER;
if (decoded_data)
decoded_data->data = NULL;
- infile = fopen(pFileName, "rb");
- if (infile == NULL) {
- mm_util_error("[infile] file open [%s]", pFileName);
- mm_util_stderror("file open failed");
- return MM_UTIL_ERROR_NO_SUCH_FILE;
+ iErrorCode = mm_util_safe_fopen(pFileName, "rb", &infile);
+ if (iErrorCode != MM_UTIL_ERROR_NONE) {
+ mm_util_error("mm_util_safe_fopen failed (%d)", iErrorCode);
+ return iErrorCode;
}
+
mm_util_debug("infile");
/* allocate and initialize JPEG decompression object We set up the normal JPEG error routines, then override error_exit. */
dinfo.err = jpeg_std_error(&jerr.pub);
mm_util_debug("setjmp");
jpeg_destroy_decompress(&dinfo);
MMUTIL_SAFE_FREE(decoded_data->data);
- fclose(infile);
+ mm_util_safe_fclose(infile);
mm_util_fleave();
return MM_UTIL_ERROR_INVALID_PARAMETER;
}
jpeg_finish_decompress(&dinfo);
jpeg_destroy_decompress(&dinfo);
mm_util_error("[%d] We can't decode the IMAGE format", input_fmt);
- fclose(infile);
+ mm_util_safe_fclose(infile);
return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
}
jpeg_finish_decompress(&dinfo);
jpeg_destroy_decompress(&dinfo);
mm_util_error("decoded_data->data is NULL");
- fclose(infile);
+ mm_util_safe_fclose(infile);
return MM_UTIL_ERROR_OUT_OF_MEMORY;
}
mm_util_debug("decoded_data->data");
/* Release JPEG decompression object */
jpeg_destroy_decompress(&dinfo);
- fclose(infile);
+ mm_util_safe_fclose(infile);
mm_util_fleave();
return MM_UTIL_ERROR_INVALID_PARAMETER;
}
- FILE *fp = fopen(filename, "rb");
+ FILE *fp = NULL;
+ ret = mm_util_safe_fopen(filename, "rb", &fp);
+ if (ret != MM_UTIL_ERROR_NONE) {
+ mm_util_error("mm_util_safe_fopen failed (%d)", ret);
+ return ret;
+ }
+
unsigned char magic[2] = {0};
size_t read_size = 0;
if (fp) {
mm_util_debug("Success fread");
mm_util_debug("%x %x", magic[0], magic[1]);
- fclose(fp);
+ mm_util_safe_fclose(fp);
} else {
mm_util_error("[infile] file open [%s]", filename);
mm_util_stderror("file open failed");
return MM_UTIL_ERROR_INVALID_PARAMETER;
}
- FILE *fp = fopen(filename, "rb");
+ FILE *fp = NULL;
+ ret = mm_util_safe_fopen(filename, "rb", &fp);
+ if (ret != MM_UTIL_ERROR_NONE) {
+ mm_util_error("mm_util_safe_fopen failed (%d)", ret);
+ return ret;
+ }
+
unsigned char magic[2] = {0};
size_t read_size = 0;
if (fp) {
mm_util_debug("Success fread");
mm_util_debug("%x %x", magic[0], magic[1]);
- fclose(fp);
+ mm_util_safe_fclose(fp);
} else {
mm_util_error("[infile] file open [%s]", filename);
mm_util_stderror("file open failed");
if (png_ptr == NULL) {
mm_util_error("could not create read struct");
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
return MM_UTIL_ERROR_INVALID_OPERATION;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
mm_util_error("could not create info struct");
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return MM_UTIL_ERROR_INVALID_OPERATION;
}
mm_util_debug("free data");
}
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
return MM_UTIL_ERROR_INVALID_OPERATION;
}
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
return MM_UTIL_ERROR_NONE;
}
if (png_ptr == NULL) {
mm_util_error("could not create read struct");
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
return MM_UTIL_ERROR_INVALID_OPERATION;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
mm_util_error("could not create info struct");
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return MM_UTIL_ERROR_INVALID_OPERATION;
}
mm_util_debug("free data");
}
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
return MM_UTIL_ERROR_INVALID_OPERATION;
}
};
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
return MM_UTIL_ERROR_NONE;
}
int ret = MM_UTIL_ERROR_NONE;
FILE *fp;
- mm_util_debug("mm_util_decode_from_png");
- if ((fp = fopen(fpath, "r")) == NULL)
- return MM_UTIL_ERROR_NO_SUCH_FILE;
+ ret = mm_util_safe_fopen(fpath, "r", &fp);
+ if (ret != MM_UTIL_ERROR_NONE) {
+ mm_util_error("mm_util_safe_fopen failed (%d)", ret);
+ return ret;
+ }
if (fp) {
if (decoded->png.progressive)
if (png_ptr == NULL) {
mm_util_error("could not create write struct");
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
return MM_UTIL_ERROR_INVALID_OPERATION;
}
if (info_ptr == NULL) {
mm_util_error("could not create info struct");
png_destroy_write_struct(&png_ptr, png_infopp_NULL);
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
return MM_UTIL_ERROR_INVALID_OPERATION;
}
if (row_pointers)
png_free(png_ptr, row_pointers);
png_destroy_write_struct(&png_ptr, &info_ptr);
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
return MM_UTIL_ERROR_INVALID_OPERATION;
}
png_free(png_ptr, row_pointers);
png_destroy_write_struct(&png_ptr, &info_ptr);
- if (fp)
- fclose(fp);
+ mm_util_safe_fclose(fp);
return MM_UTIL_ERROR_NONE;
}
int mm_util_encode_to_png_file(void **data, mm_util_png_data *encoded, const char *fpath)
{
int ret = MM_UTIL_ERROR_NONE;
- char *realPATH = NULL;
FILE *fp;
mm_util_debug("mm_util_encode_to_png");
- if (fpath == NULL) {
- mm_util_error("Invalid parameter");
- return MM_UTIL_ERROR_NO_SUCH_FILE;
- }
-
- if (g_file_test(fpath, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
- realPATH = realpath(fpath, NULL);
- if (realPATH == NULL) {
- mm_util_error("realpath failed [%s]", fpath);
- mm_util_stderror("realpath failed");
- return MM_UTIL_ERROR_NO_SUCH_FILE;
- }
+ mm_util_retvm_if(!MMUTIL_STRING_VALID(fpath), MM_UTIL_ERROR_NO_SUCH_FILE, "invalid path");
- if (!strncmp(fpath, realPATH, strlen(fpath))) {
- if ((fp = fopen(fpath, "w")) == NULL) {
- mm_util_stderror("file open failed");
- MMUTIL_SAFE_FREE(realPATH);
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
- } else {
- mm_util_error("file is symbolic link");
- MMUTIL_SAFE_FREE(realPATH);
- return MM_UTIL_ERROR_NO_SUCH_FILE;
- }
- MMUTIL_SAFE_FREE(realPATH);
- } else {
- if ((fp = fopen(fpath, "w")) == NULL) {
- mm_util_error("[infile] file open [%s] failed", fpath);
- mm_util_stderror("file open failed");
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
+ ret = mm_util_safe_fopen(fpath, "w", &fp);
+ if (ret != MM_UTIL_ERROR_NONE) {
+ mm_util_error("mm_util_safe_fopen failed (%d)", ret);
+ return ret;
}
ret = write_png(data, encoded, fp);