From cd642d58b165243fbed334b065bd4b5ef2ec79cd Mon Sep 17 00:00:00 2001 From: sisong Date: Fri, 18 Aug 2023 14:11:28 +0800 Subject: [PATCH 1/2] import ftell; used ftell to get opened file's size (replaced stat) ; --- file_for_patch.c | 82 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 18 deletions(-) diff --git a/file_for_patch.c b/file_for_patch.c index a3cfd064..1b1a247d 100644 --- a/file_for_patch.c +++ b/file_for_patch.c @@ -27,6 +27,8 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#define _LARGEFILE64_SOURCE +#define _FILE_OFFSET_BITS 64 #include "file_for_patch.h" #include //stat mkdir /* @@ -264,8 +266,11 @@ hpatch_BOOL hpatch_setIsExecuteFile(const char* fileName){ #include static off64_t _import_lseek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos,int whence){ int fd; - if (feof(file)) + if (feof(file)){ + if (whence==SEEK_CUR) + return -1; rewind(file); + } setbuf(file,NULL); fd = fileno(file); if (fd<0) return -1; @@ -274,8 +279,7 @@ static off64_t _import_lseek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos #endif hpatch_inline static -hpatch_BOOL _import_fileSeek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos){ - const int whence=SEEK_SET; +hpatch_BOOL _import_fileSeek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos,int whence){ #ifdef _MSC_VER return _fseeki64(file,seekPos,whence)==0; #else @@ -283,7 +287,7 @@ hpatch_BOOL _import_fileSeek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos # if __ANDROID_API__ >= 24 return fseeko64(file,seekPos,whence)==0; # else - if (((off64_t)seekPos)==((long)seekPos)) return fseek(file,(long)seekPos,whence)==0; + if ((((off64_t)seekPos)==((long)seekPos))&&(whence==SEEK_SET)) return fseek(file,(long)seekPos,whence)==0; else return _import_lseek64(file,seekPos,whence)>=0; # endif # else @@ -292,6 +296,29 @@ hpatch_BOOL _import_fileSeek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos #endif } +hpatch_inline static +hpatch_BOOL _import_fileSeek64To(hpatch_FileHandle file,hpatch_StreamPos_t seekPos){ + return _import_fileSeek64(file,seekPos,SEEK_SET); +} + +hpatch_inline static +hpatch_BOOL _import_fileTell64(hpatch_FileHandle file,hpatch_StreamPos_t* outPos){ +#ifdef _MSC_VER + __int64 pos=_ftelli64(file); +#else +# ifdef ANDROID +# if __ANDROID_API__ >= 24 + off64_t pos=ftello64(file); +# else + off64_t pos=_import_lseek64(file,0,SEEK_CUR); +# endif +# else + off_t pos=ftello(file); +# endif +#endif + *outPos=(hpatch_StreamPos_t)pos; + return (pos>=0); +} hpatch_inline static hpatch_BOOL _import_fileClose(hpatch_FileHandle* pfile){ @@ -355,17 +382,19 @@ hpatch_BOOL _import_fileTruncate(hpatch_FileHandle file,hpatch_StreamPos_t new_f }*/ #if (_IS_USED_WIN32_UTF8_WAPI) +# define _FileModeType const wchar_t* # define _kFileReadMode L"rb" # define _kFileWriteMode L"wb+" # define _kFileReadWriteMode L"rb+" #else +# define _FileModeType const char* # define _kFileReadMode "rb" # define _kFileWriteMode "wb+" # define _kFileReadWriteMode "rb+" #endif #if (_IS_USED_WIN32_UTF8_WAPI) -static hpatch_FileHandle _import_fileOpenByMode(const char* fileName_utf8,const wchar_t* mode_w){ +static hpatch_FileHandle _import_fileOpen(const char* fileName_utf8,_FileModeType mode_w){ wchar_t fileName_w[hpatch_kPathMaxSize]; int wsize=_utf8FileName_to_w(fileName_utf8,fileName_w,hpatch_kPathMaxSize); if (wsize>0) { @@ -382,45 +411,62 @@ static hpatch_FileHandle _import_fileOpenByMode(const char* fileName_utf8,const } #else hpatch_inline static -hpatch_FileHandle _import_fileOpenByMode(const char* fileName_utf8,const char* mode){ +hpatch_FileHandle _import_fileOpen(const char* fileName_utf8,_FileModeType mode){ return fopen(fileName_utf8,mode); } #endif +static hpatch_FileHandle _import_fileOpenWithSize(const char* fileName_utf8,_FileModeType mode,hpatch_StreamPos_t* out_fileSize){ + hpatch_FileHandle file=0; + file=_import_fileOpen(fileName_utf8,mode); + if ((out_fileSize==0)||(file==0)) return file; + + if (_import_fileSeek64(file,0,SEEK_END)){ + if (_import_fileTell64(file,out_fileSize)){ + if (_import_fileSeek64(file,0,SEEK_SET)){ + return file; + } + } + } + + //error clear + _import_fileClose(&file); + return 0; +} + +static hpatch_inline hpatch_BOOL _import_fileOpenRead(const char* fileName_utf8,hpatch_FileHandle* out_fileHandle, - hpatch_StreamPos_t* out_fileLength){ + hpatch_StreamPos_t* out_fileSize){ hpatch_FileHandle file=0; assert(out_fileHandle!=0); if (out_fileHandle==0) { _set_errno_new(EINVAL); return hpatch_FALSE; } - if (out_fileLength!=0){ - if (!hpatch_getFileSize(fileName_utf8,out_fileLength)) return hpatch_FALSE; - } - file=_import_fileOpenByMode(fileName_utf8,_kFileReadMode); + file=_import_fileOpenWithSize(fileName_utf8,_kFileReadMode,out_fileSize); if (file==0) return hpatch_FALSE; *out_fileHandle=file; return hpatch_TRUE; } +static hpatch_inline hpatch_BOOL _import_fileOpenCreateOrReWrite(const char* fileName_utf8,hpatch_FileHandle* out_fileHandle){ hpatch_FileHandle file=0; assert(out_fileHandle!=0); if (out_fileHandle==0) { _set_errno_new(EINVAL); return hpatch_FALSE; } - file=_import_fileOpenByMode(fileName_utf8,_kFileWriteMode); + file=_import_fileOpen(fileName_utf8,_kFileWriteMode); if (file==0) return hpatch_FALSE; *out_fileHandle=file; return hpatch_TRUE; } +static hpatch_BOOL _import_fileReopenWrite(const char* fileName_utf8,hpatch_FileHandle* out_fileHandle, hpatch_StreamPos_t* out_curFileWritePos){ hpatch_FileHandle file=0; hpatch_StreamPos_t curFileSize=0; assert(out_fileHandle!=0); if (out_fileHandle==0) { _set_errno_new(EINVAL); return hpatch_FALSE; } - if (!hpatch_getFileSize(fileName_utf8,&curFileSize)) return hpatch_FALSE; - if (out_curFileWritePos!=0) *out_curFileWritePos=curFileSize; - file=_import_fileOpenByMode(fileName_utf8,_kFileReadWriteMode); + file=_import_fileOpenWithSize(fileName_utf8,_kFileReadWriteMode,&curFileSize); if (file==0) return hpatch_FALSE; - if (!_import_fileSeek64(file,curFileSize)) + if (out_curFileWritePos!=0) *out_curFileWritePos=curFileSize; + if (!_import_fileSeek64To(file,curFileSize)) { _import_fileClose_No_errno(&file); return hpatch_FALSE; } *out_fileHandle=file; return hpatch_TRUE; @@ -442,7 +488,7 @@ hpatch_BOOL _import_fileReopenWrite(const char* fileName_utf8,hpatch_FileHandle* if ((readLen>self->base.streamSize) ||(readFromPos>self->base.streamSize-readLen)) _ferr_returnv(EFBIG); if (self->m_fpos!=readFromPos+self->m_offset){ - if (!_import_fileSeek64(self->m_file,readFromPos+self->m_offset)) _rw_ferr_return(); + if (!_import_fileSeek64To(self->m_file,readFromPos+self->m_offset)) _rw_ferr_return(); } if (!_import_fileRead(self->m_file,out_data,out_data+readLen)) _rw_ferr_return(); self->m_fpos=readFromPos+self->m_offset+readLen; @@ -494,7 +540,7 @@ hpatch_BOOL hpatch_TFileStreamInput_close(hpatch_TFileStreamInput* self){ if (writeToPos!=self->m_fpos){ if (self->is_random_out){ if (!_import_fileFlush(self->m_file)) _rw_ferr_return(); //for lseek64 safe - if (!_import_fileSeek64(self->m_file,writeToPos)) _rw_ferr_return(); + if (!_import_fileSeek64To(self->m_file,writeToPos)) _rw_ferr_return(); self->m_fpos=writeToPos; }else{ _ferr_returnv(ERANGE); //must continue write at self->m_fpos From c251308b0e75df74708a4bd1f7bfa87ac6e1b297 Mon Sep 17 00:00:00 2001 From: sisong Date: Fri, 18 Aug 2023 14:35:25 +0800 Subject: [PATCH 2/2] update version; --- README.md | 2 +- libHDiffPatch/HPatch/patch_types.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e1b15e49..152e44f8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # [HDiffPatch](https://github.com/sisong/HDiffPatch) -[![release](https://img.shields.io/badge/release-v4.6.4-blue.svg)](https://github.com/sisong/HDiffPatch/releases) +[![release](https://img.shields.io/badge/release-v4.6.5-blue.svg)](https://github.com/sisong/HDiffPatch/releases) [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/sisong/HDiffPatch/blob/master/LICENSE) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-blue.svg)](https://github.com/sisong/HDiffPatch/pulls) [![+issue Welcome](https://img.shields.io/github/issues-raw/sisong/HDiffPatch?color=green&label=%2Bissue%20welcome)](https://github.com/sisong/HDiffPatch/issues) diff --git a/libHDiffPatch/HPatch/patch_types.h b/libHDiffPatch/HPatch/patch_types.h index ce526eb9..9314269b 100644 --- a/libHDiffPatch/HPatch/patch_types.h +++ b/libHDiffPatch/HPatch/patch_types.h @@ -38,7 +38,7 @@ extern "C" { #define HDIFFPATCH_VERSION_MAJOR 4 #define HDIFFPATCH_VERSION_MINOR 6 -#define HDIFFPATCH_VERSION_RELEASE 4 +#define HDIFFPATCH_VERSION_RELEASE 5 #define _HDIFFPATCH_VERSION HDIFFPATCH_VERSION_MAJOR.HDIFFPATCH_VERSION_MINOR.HDIFFPATCH_VERSION_RELEASE #define _HDIFFPATCH_QUOTE(str) #str