From: hj kim Date: Thu, 27 Feb 2020 04:05:12 +0000 (+0900) Subject: Unify duplicated codes regarding getting file size X-Git-Tag: accepted/tizen/unified/20200407.043020~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F17%2F226117%2F7;p=platform%2Fcore%2Fmultimedia%2Flibmm-fileinfo.git Unify duplicated codes regarding getting file size Change-Id: I357be69f82a03b9e8ac6d7ba94cc9341d40a4e0a --- diff --git a/formats/ffmpeg/mm_file_format_aac.c b/formats/ffmpeg/mm_file_format_aac.c index aabd77d..e9f6a02 100644 --- a/formats/ffmpeg/mm_file_format_aac.c +++ b/formats/ffmpeg/mm_file_format_aac.c @@ -541,10 +541,11 @@ int mmfile_aacparser_open(MMFileAACHandle *handle, const char *filenamec) /* Initialize the members of handle */ _aac_init_handle(privateData); - mmfile_seek(privateData->hFile, 0, MMFILE_SEEK_END); - privateData->streamInfo.fileSize = mmfile_tell(privateData->hFile); - - mmfile_seek(privateData->hFile, 0, MMFILE_SEEK_SET); + privateData->streamInfo.fileSize = mmfile_get_size(privateData->hFile); + if (privateData->streamInfo.fileSize <= 0) { + debug_error(RELEASE, "file source is too small\n"); + goto exception; + } /* Search the existance of ID3 tag */ ret = _search_id3tag(privateData); diff --git a/formats/ffmpeg/mm_file_format_amr.c b/formats/ffmpeg/mm_file_format_amr.c index 04d3234..7548358 100644 --- a/formats/ffmpeg/mm_file_format_amr.c +++ b/formats/ffmpeg/mm_file_format_amr.c @@ -253,10 +253,7 @@ int mmfile_amrparser_open(MMFileAMRHandle *handle, const char *filenamec) goto exception; } - mmfile_seek(privateData->hFile, 0, MMFILE_SEEK_END); - privateData->fileSize = mmfile_tell(privateData->hFile); - mmfile_seek(privateData->hFile, 0, MMFILE_SEEK_SET); - + privateData->fileSize = mmfile_get_size(privateData->hFile); if (privateData->fileSize < MMFILE_AMR_MIN_HEADER_SIZE) { debug_error(DEBUG, "Too small file to parse!!\n"); goto exception; diff --git a/formats/ffmpeg/mm_file_format_imelody.c b/formats/ffmpeg/mm_file_format_imelody.c index 41bfd64..77f50ec 100755 --- a/formats/ffmpeg/mm_file_format_imelody.c +++ b/formats/ffmpeg/mm_file_format_imelody.c @@ -346,10 +346,7 @@ static int __get_imelody_tag(const char *uriname, tMMFileImelodyTagInfo *tags) return MMFILE_FORMAT_FAIL; } - mmfile_seek(fp, 0, MMFILE_SEEK_END); - filesize = mmfile_tell(fp); - mmfile_seek(fp, 0, MMFILE_SEEK_SET); - + filesize = mmfile_get_size(fp); if (filesize < _MMFILE_IMY_HEADER_LENGTH) { debug_error(DEBUG, "header is too small.\n"); ret = MMFILE_FORMAT_FAIL; @@ -441,10 +438,7 @@ __get_load_memory(char *src, int *out_size) } /*get file size*/ - mmfile_seek(fp, 0L, MMFILE_SEEK_END); - src_size = mmfile_tell(fp); - mmfile_seek(fp, 0L, MMFILE_SEEK_SET); - + src_size = mmfile_get_size(fp); if (src_size <= 0) { debug_error(DEBUG, "failed to get file size.\n"); goto failed; diff --git a/formats/ffmpeg/mm_file_format_midi.c b/formats/ffmpeg/mm_file_format_midi.c index be9e679..d8e2284 100755 --- a/formats/ffmpeg/mm_file_format_midi.c +++ b/formats/ffmpeg/mm_file_format_midi.c @@ -514,10 +514,7 @@ __AvGetMidiDuration(char *szFileName, MIDI_INFO_SIMPLE *info) } /*get file size*/ - mmfile_seek(hFile, 0L, MMFILE_SEEK_END); - dFileSize = mmfile_tell(hFile); - mmfile_seek(hFile, 0L, MMFILE_SEEK_SET); - + dFileSize = mmfile_get_size(hFile); if (dFileSize <= 0) { debug_error(DEBUG, "failed to get file size.\n"); goto _RELEASE_RESOURCE; diff --git a/formats/ffmpeg/mm_file_format_mmf.c b/formats/ffmpeg/mm_file_format_mmf.c index 8d437b4..6571527 100755 --- a/formats/ffmpeg/mm_file_format_mmf.c +++ b/formats/ffmpeg/mm_file_format_mmf.c @@ -2624,10 +2624,7 @@ mmf_file_mmf_get_duration(char *src, int is_xmf) } /*get file size*/ - mmfile_seek(fp, 0L, MMFILE_SEEK_END); - src_size = mmfile_tell(fp); - mmfile_seek(fp, 0L, MMFILE_SEEK_SET); - + src_size = mmfile_get_size(fp); if (src_size <= 0) { debug_error(DEBUG, "failed to get file size.\n"); ret_msec = -1; diff --git a/formats/ffmpeg/mm_file_format_mp3.c b/formats/ffmpeg/mm_file_format_mp3.c index d9380d2..3e514b8 100644 --- a/formats/ffmpeg/mm_file_format_mp3.c +++ b/formats/ffmpeg/mm_file_format_mp3.c @@ -703,24 +703,6 @@ __AvGetBitrate(AvFileContentInfo *pInfo) return true; } - -static bool __AvGetFileSize(MMFileIOHandle *hFile, long long *file_size) -{ - long long fileLen = 0; - - mmfile_seek(hFile, 0L, SEEK_END); - fileLen = mmfile_tell(hFile); - if (fileLen <= 0) { - debug_error(DEBUG, "file is too small.\n"); - return false; - } - mmfile_seek(hFile, 0L, SEEK_SET); - - *file_size = fileLen; - - return true; -} - static bool __AvGetID3v1Header(unsigned char *buf, size_t buf_size, int *offset) { unsigned char TagV1ID[4] = { 0x54, 0x41, 0x47}; /* TAG */ @@ -1040,7 +1022,8 @@ static int mmf_file_mp3_get_tag_info(char *filename, AvFileContentInfo *pInfo) return MMFILE_FORMAT_FAIL; } - if (!__AvGetFileSize(hFile, &pInfo->fileLen)) { + pInfo->fileLen = mmfile_get_size(hFile); + if (pInfo->fileLen <= 0) { debug_error(DEBUG, "file is too small.\n"); goto EXCEPTION; } @@ -1170,7 +1153,8 @@ static int mmf_file_mp3_get_stream_info(char *filename, AvFileContentInfo *pInfo return MMFILE_FORMAT_FAIL; } - if (!__AvGetFileSize(hFile, &pInfo->fileLen)) { + pInfo->fileLen = mmfile_get_size(hFile); + if (pInfo->fileLen <= 0) { debug_error(DEBUG, "file is too small.\n"); goto EXCEPTION; } diff --git a/formats/ffmpeg/mm_file_format_tags.c b/formats/ffmpeg/mm_file_format_tags.c index 8ae6385..5ad77b9 100755 --- a/formats/ffmpeg/mm_file_format_tags.c +++ b/formats/ffmpeg/mm_file_format_tags.c @@ -77,17 +77,12 @@ int MMFileOpenTags(MMFileTagsHandle *tagsHandle, const char *uriName) goto exception; } - ret = mmfile_seek(privateData->fp, 0, MMFILE_SEEK_END); - if (MMFILE_UTIL_FAIL == ret) { - debug_error(DEBUG, "mmfile_seek\n"); + privateData->filesize = mmfile_get_size(privateData->fp); + if (privateData->filesize <= 0) { + debug_error(DEBUG, "mmfile_get_size: too small\n"); ret = MMFILE_TAGS_FAIL; goto exception; } - - privateData->filesize = mmfile_tell(privateData->fp); - - mmfile_seek(privateData->fp, 0, MMFILE_SEEK_SET); - privateData->startOffset = 0; privateData->endOffset = privateData->filesize; diff --git a/formats/ffmpeg/mm_file_format_wav.c b/formats/ffmpeg/mm_file_format_wav.c index 7d8e2a5..8327e2c 100755 --- a/formats/ffmpeg/mm_file_format_wav.c +++ b/formats/ffmpeg/mm_file_format_wav.c @@ -270,9 +270,7 @@ int mmfile_format_read_stream_wav(MMFileFormatContext *formatContext) /* Get file size. because sometimes waveinfo->size is wrong */ ret = mmfile_open(&fp, formatContext->uriFileName, MMFILE_RDONLY); if (fp) { - mmfile_seek(fp, 0, MMFILE_SEEK_END); - filesize = mmfile_tell(fp); - mmfile_seek(fp, 0, MMFILE_SEEK_SET); + filesize = mmfile_get_size(fp); mmfile_close(fp); } @@ -430,10 +428,7 @@ mmf_file_wave_get_header(char *src) /*get file size*/ - mmfile_seek(fp, 0L, MMFILE_SEEK_END); - src_size = mmfile_tell(fp); - mmfile_seek(fp, 0L, MMFILE_SEEK_SET); - + src_size = mmfile_get_size(fp); if (src_size < MMF_FILE_WAVE_HEADER_LEN) { debug_error(DEBUG, "header is too small.\n"); goto failed; diff --git a/utils/include/mm_file_utils.h b/utils/include/mm_file_utils.h index 5fb6f43..43dbe55 100755 --- a/utils/include/mm_file_utils.h +++ b/utils/include/mm_file_utils.h @@ -146,6 +146,7 @@ int mmfile_read(MMFileIOHandle *h, unsigned char *buf, int size); int mmfile_write(MMFileIOHandle *h, unsigned char *buf, int size); int64_t mmfile_seek(MMFileIOHandle *h, int64_t pos, int whence); long long mmfile_tell(MMFileIOHandle *h); +long long mmfile_get_size(MMFileIOHandle *h); int mmfile_close(MMFileIOHandle *h); diff --git a/utils/mm_file_util_io.c b/utils/mm_file_util_io.c index f59d6c3..6510903 100644 --- a/utils/mm_file_util_io.c +++ b/utils/mm_file_util_io.c @@ -253,6 +253,30 @@ long long mmfile_tell(MMFileIOHandle *handle) } EXPORT_API +long long mmfile_get_size(MMFileIOHandle *handle) +{ + long long filesize = 0; + long long cur_pos = 0; + + cur_pos = mmfile_tell(handle); + if (cur_pos < 0) + return MMFILE_IO_FAILED; + + if (mmfile_seek(handle, 0L, MMFILE_SEEK_SET) < 0) + return MMFILE_IO_FAILED; + + if (mmfile_seek(handle, 0L, MMFILE_SEEK_END) < 0) + return MMFILE_IO_FAILED; + + filesize = mmfile_tell(handle); + + if (mmfile_seek(handle, cur_pos, MMFILE_SEEK_SET) < 0) + debug_error(DEBUG, "failed to restore current position"); + + return filesize; +} + +EXPORT_API int mmfile_close(MMFileIOHandle *handle) { int ret = 0; diff --git a/utils/mm_file_util_validity.c b/utils/mm_file_util_validity.c index cc256e4..95e7f72 100755 --- a/utils/mm_file_util_validity.c +++ b/utils/mm_file_util_validity.c @@ -80,11 +80,7 @@ int MMFileFormatIsValidMP3(MMFileIOHandle *pFileIO, const char *mmfileuri, int f } } - mmfile_seek(fp, 0L, MMFILE_SEEK_SET); - - mmfile_seek(fp, 0L, MMFILE_SEEK_END); - filesize = mmfile_tell(fp); - mmfile_seek(fp, 0L, MMFILE_SEEK_SET); + filesize = mmfile_get_size(fp); if (filesize < _MMFILE_MP3_HEADER_LENGTH) { debug_error(DEBUG, "header is too small.\n"); @@ -198,12 +194,7 @@ int MMFileFormatIsValidAAC(MMFileIOHandle *pFileIO, const char *mmfileuri, int d } } - mmfile_seek(fp, 0L, MMFILE_SEEK_SET); - - /* Initialize the members of handle */ - mmfile_seek(fp, 0, MMFILE_SEEK_END); - filesize = mmfile_tell(fp); - mmfile_seek(fp, 0, MMFILE_SEEK_SET); + filesize = mmfile_get_size(fp); if (filesize < _MMFILE_AAC_HEADER_LENGTH) { debug_error(DEBUG, "header is too small.\n"); @@ -320,12 +311,7 @@ int MMFileFormatIsValidOGG(MMFileIOHandle *pFileIO, const char *mmfileuri, int d } } - mmfile_seek(fp, 0L, MMFILE_SEEK_SET); - - /* Initialize the members of handle */ - mmfile_seek(fp, 0, MMFILE_SEEK_END); - filesize = mmfile_tell(fp); - mmfile_seek(fp, 0, MMFILE_SEEK_SET); + filesize = mmfile_get_size(fp); if (filesize < _MMFILE_OGG_HEADER_LENGTH) { debug_error(DEBUG, "header is too small.\n"); @@ -407,12 +393,7 @@ int MMFileFormatIsValidMID(MMFileIOHandle *pFileIO, const char *mmfileuri, int d } } - mmfile_seek(fp, 0L, MMFILE_SEEK_SET); - - /* Initialize the members of handle */ - mmfile_seek(fp, 0, MMFILE_SEEK_END); - filesize = mmfile_tell(fp); - mmfile_seek(fp, 0, MMFILE_SEEK_SET); + filesize = mmfile_get_size(fp); if (filesize < _MMFILE_MIDI_HEADER_LENGTH) { debug_error(DEBUG, "header is too small.\n"); @@ -529,12 +510,7 @@ int MMFileFormatIsValidMP4(MMFileIOHandle *pFileIO, const char *mmfileuri, int d } } - mmfile_seek(fp, 0L, MMFILE_SEEK_SET); - - /* Initialize the members of handle */ - mmfile_seek(fp, 0, MMFILE_SEEK_END); - filesize = mmfile_tell(fp); - mmfile_seek(fp, 0, MMFILE_SEEK_SET); + filesize = mmfile_get_size(fp); if (filesize < _MMFILE_MP4_HEADER_LENGTH) { debug_error(DEBUG, "header is too small.\n");