27
27
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28
28
OTHER DEALINGS IN THE SOFTWARE.
29
29
*/
30
+ #define _LARGEFILE64_SOURCE
31
+ #define _FILE_OFFSET_BITS 64
30
32
#include "file_for_patch.h"
31
33
#include <sys/stat.h> //stat mkdir
32
34
/*
@@ -264,8 +266,11 @@ hpatch_BOOL hpatch_setIsExecuteFile(const char* fileName){
264
266
#include <unistd.h>
265
267
static off64_t _import_lseek64 (hpatch_FileHandle file ,hpatch_StreamPos_t seekPos ,int whence ){
266
268
int fd ;
267
- if (feof (file ))
269
+ if (feof (file )){
270
+ if (whence == SEEK_CUR )
271
+ return -1 ;
268
272
rewind (file );
273
+ }
269
274
setbuf (file ,NULL );
270
275
fd = fileno (file );
271
276
if (fd < 0 ) return -1 ;
@@ -274,16 +279,15 @@ static off64_t _import_lseek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos
274
279
#endif
275
280
276
281
hpatch_inline static
277
- hpatch_BOOL _import_fileSeek64 (hpatch_FileHandle file ,hpatch_StreamPos_t seekPos ){
278
- const int whence = SEEK_SET ;
282
+ hpatch_BOOL _import_fileSeek64 (hpatch_FileHandle file ,hpatch_StreamPos_t seekPos ,int whence ){
279
283
#ifdef _MSC_VER
280
284
return _fseeki64 (file ,seekPos ,whence )== 0 ;
281
285
#else
282
286
# ifdef ANDROID
283
287
# if __ANDROID_API__ >= 24
284
288
return fseeko64 (file ,seekPos ,whence )== 0 ;
285
289
# else
286
- if (((off64_t )seekPos )== ((long )seekPos )) return fseek (file ,(long )seekPos ,whence )== 0 ;
290
+ if (((( off64_t )seekPos )== ((long )seekPos )) && ( whence == SEEK_SET )) return fseek (file ,(long )seekPos ,whence )== 0 ;
287
291
else return _import_lseek64 (file ,seekPos ,whence )>=0 ;
288
292
# endif
289
293
# else
@@ -292,6 +296,29 @@ hpatch_BOOL _import_fileSeek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos
292
296
#endif
293
297
}
294
298
299
+ hpatch_inline static
300
+ hpatch_BOOL _import_fileSeek64To (hpatch_FileHandle file ,hpatch_StreamPos_t seekPos ){
301
+ return _import_fileSeek64 (file ,seekPos ,SEEK_SET );
302
+ }
303
+
304
+ hpatch_inline static
305
+ hpatch_BOOL _import_fileTell64 (hpatch_FileHandle file ,hpatch_StreamPos_t * outPos ){
306
+ #ifdef _MSC_VER
307
+ __int64 pos = _ftelli64 (file );
308
+ #else
309
+ # ifdef ANDROID
310
+ # if __ANDROID_API__ >= 24
311
+ off64_t pos = ftello64 (file );
312
+ # else
313
+ off64_t pos = _import_lseek64 (file ,0 ,SEEK_CUR );
314
+ # endif
315
+ # else
316
+ off_t pos = ftello (file );
317
+ # endif
318
+ #endif
319
+ * outPos = (hpatch_StreamPos_t )pos ;
320
+ return (pos >=0 );
321
+ }
295
322
296
323
hpatch_inline static
297
324
hpatch_BOOL _import_fileClose (hpatch_FileHandle * pfile ){
@@ -355,17 +382,19 @@ hpatch_BOOL _import_fileTruncate(hpatch_FileHandle file,hpatch_StreamPos_t new_f
355
382
}*/
356
383
357
384
#if (_IS_USED_WIN32_UTF8_WAPI )
385
+ # define _FileModeType const wchar_t*
358
386
# define _kFileReadMode L"rb"
359
387
# define _kFileWriteMode L"wb+"
360
388
# define _kFileReadWriteMode L"rb+"
361
389
#else
390
+ # define _FileModeType const char*
362
391
# define _kFileReadMode "rb"
363
392
# define _kFileWriteMode "wb+"
364
393
# define _kFileReadWriteMode "rb+"
365
394
#endif
366
395
367
396
#if (_IS_USED_WIN32_UTF8_WAPI )
368
- static hpatch_FileHandle _import_fileOpenByMode (const char * fileName_utf8 ,const wchar_t * mode_w ){
397
+ static hpatch_FileHandle _import_fileOpen (const char * fileName_utf8 ,_FileModeType mode_w ){
369
398
wchar_t fileName_w [hpatch_kPathMaxSize ];
370
399
int wsize = _utf8FileName_to_w (fileName_utf8 ,fileName_w ,hpatch_kPathMaxSize );
371
400
if (wsize > 0 ) {
@@ -382,45 +411,62 @@ static hpatch_FileHandle _import_fileOpenByMode(const char* fileName_utf8,const
382
411
}
383
412
#else
384
413
hpatch_inline static
385
- hpatch_FileHandle _import_fileOpenByMode (const char * fileName_utf8 ,const char * mode ){
414
+ hpatch_FileHandle _import_fileOpen (const char * fileName_utf8 ,_FileModeType mode ){
386
415
return fopen (fileName_utf8 ,mode ); }
387
416
#endif
388
417
418
+ static hpatch_FileHandle _import_fileOpenWithSize (const char * fileName_utf8 ,_FileModeType mode ,hpatch_StreamPos_t * out_fileSize ){
419
+ hpatch_FileHandle file = 0 ;
420
+ file = _import_fileOpen (fileName_utf8 ,mode );
421
+ if ((out_fileSize == 0 )|| (file == 0 )) return file ;
422
+
423
+ if (_import_fileSeek64 (file ,0 ,SEEK_END )){
424
+ if (_import_fileTell64 (file ,out_fileSize )){
425
+ if (_import_fileSeek64 (file ,0 ,SEEK_SET )){
426
+ return file ;
427
+ }
428
+ }
429
+ }
430
+
431
+ //error clear
432
+ _import_fileClose (& file );
433
+ return 0 ;
434
+ }
435
+
436
+ static hpatch_inline
389
437
hpatch_BOOL _import_fileOpenRead (const char * fileName_utf8 ,hpatch_FileHandle * out_fileHandle ,
390
- hpatch_StreamPos_t * out_fileLength ){
438
+ hpatch_StreamPos_t * out_fileSize ){
391
439
hpatch_FileHandle file = 0 ;
392
440
assert (out_fileHandle != 0 );
393
441
if (out_fileHandle == 0 ) { _set_errno_new (EINVAL ); return hpatch_FALSE ; }
394
- if (out_fileLength != 0 ){
395
- if (!hpatch_getFileSize (fileName_utf8 ,out_fileLength )) return hpatch_FALSE ;
396
- }
397
- file = _import_fileOpenByMode (fileName_utf8 ,_kFileReadMode );
442
+ file = _import_fileOpenWithSize (fileName_utf8 ,_kFileReadMode ,out_fileSize );
398
443
if (file == 0 ) return hpatch_FALSE ;
399
444
* out_fileHandle = file ;
400
445
return hpatch_TRUE ;
401
446
}
402
447
448
+ static hpatch_inline
403
449
hpatch_BOOL _import_fileOpenCreateOrReWrite (const char * fileName_utf8 ,hpatch_FileHandle * out_fileHandle ){
404
450
hpatch_FileHandle file = 0 ;
405
451
assert (out_fileHandle != 0 );
406
452
if (out_fileHandle == 0 ) { _set_errno_new (EINVAL ); return hpatch_FALSE ; }
407
- file = _import_fileOpenByMode (fileName_utf8 ,_kFileWriteMode );
453
+ file = _import_fileOpen (fileName_utf8 ,_kFileWriteMode );
408
454
if (file == 0 ) return hpatch_FALSE ;
409
455
* out_fileHandle = file ;
410
456
return hpatch_TRUE ;
411
457
}
412
458
459
+ static
413
460
hpatch_BOOL _import_fileReopenWrite (const char * fileName_utf8 ,hpatch_FileHandle * out_fileHandle ,
414
461
hpatch_StreamPos_t * out_curFileWritePos ){
415
462
hpatch_FileHandle file = 0 ;
416
463
hpatch_StreamPos_t curFileSize = 0 ;
417
464
assert (out_fileHandle != 0 );
418
465
if (out_fileHandle == 0 ) { _set_errno_new (EINVAL ); return hpatch_FALSE ; }
419
- if (!hpatch_getFileSize (fileName_utf8 ,& curFileSize )) return hpatch_FALSE ;
420
- if (out_curFileWritePos != 0 ) * out_curFileWritePos = curFileSize ;
421
- file = _import_fileOpenByMode (fileName_utf8 ,_kFileReadWriteMode );
466
+ file = _import_fileOpenWithSize (fileName_utf8 ,_kFileReadWriteMode ,& curFileSize );
422
467
if (file == 0 ) return hpatch_FALSE ;
423
- if (!_import_fileSeek64 (file ,curFileSize ))
468
+ if (out_curFileWritePos != 0 ) * out_curFileWritePos = curFileSize ;
469
+ if (!_import_fileSeek64To (file ,curFileSize ))
424
470
{ _import_fileClose_No_errno (& file ); return hpatch_FALSE ; }
425
471
* out_fileHandle = file ;
426
472
return hpatch_TRUE ;
@@ -442,7 +488,7 @@ hpatch_BOOL _import_fileReopenWrite(const char* fileName_utf8,hpatch_FileHandle*
442
488
if ((readLen > self -> base .streamSize )
443
489
|| (readFromPos > self -> base .streamSize - readLen )) _ferr_returnv (EFBIG );
444
490
if (self -> m_fpos != readFromPos + self -> m_offset ){
445
- if (!_import_fileSeek64 (self -> m_file ,readFromPos + self -> m_offset )) _rw_ferr_return ();
491
+ if (!_import_fileSeek64To (self -> m_file ,readFromPos + self -> m_offset )) _rw_ferr_return ();
446
492
}
447
493
if (!_import_fileRead (self -> m_file ,out_data ,out_data + readLen )) _rw_ferr_return ();
448
494
self -> m_fpos = readFromPos + self -> m_offset + readLen ;
@@ -494,7 +540,7 @@ hpatch_BOOL hpatch_TFileStreamInput_close(hpatch_TFileStreamInput* self){
494
540
if (writeToPos != self -> m_fpos ){
495
541
if (self -> is_random_out ){
496
542
if (!_import_fileFlush (self -> m_file )) _rw_ferr_return (); //for lseek64 safe
497
- if (!_import_fileSeek64 (self -> m_file ,writeToPos )) _rw_ferr_return ();
543
+ if (!_import_fileSeek64To (self -> m_file ,writeToPos )) _rw_ferr_return ();
498
544
self -> m_fpos = writeToPos ;
499
545
}else {
500
546
_ferr_returnv (ERANGE ); //must continue write at self->m_fpos
0 commit comments