diff --git a/src/file_d2s.c b/src/file_d2s.c index 28399516..97d6b09e 100644 --- a/src/file_d2s.c +++ b/src/file_d2s.c @@ -78,5 +78,5 @@ static void file_rename_d2s(const char *old_filename) return; buffer_size=fread(buffer, 1, sizeof(buffer), file); fclose(file); - file_rename(old_filename, buffer, buffer_size, 0x14); + file_rename(old_filename, buffer, buffer_size, 0x14, NULL, 1); } diff --git a/src/file_exe.c b/src/file_exe.c index 84054601..2192e567 100644 --- a/src/file_exe.c +++ b/src/file_exe.c @@ -313,7 +313,7 @@ static void PEVersion(FILE *file, const unsigned int offset, const unsigned int { if(do_rename) { - file_rename_unicode(old_filename, buffer, end, pt, 0); + file_rename_unicode(old_filename, buffer, end, pt, NULL, 0); free(buffer); return ; } diff --git a/src/file_gz.c b/src/file_gz.c index 06ba9bba..0eae2b2c 100644 --- a/src/file_gz.c +++ b/src/file_gz.c @@ -231,7 +231,7 @@ static void file_rename_gz(const char *old_filename) } if((flags&GZ_FNAME)!=0) { - file_rename(old_filename, buffer, buffer_size, off); + file_rename(old_filename, buffer, buffer_size, off, NULL, 1); } } } diff --git a/src/file_r3d.c b/src/file_r3d.c index 1ba402c7..e5b85bbf 100644 --- a/src/file_r3d.c +++ b/src/file_r3d.c @@ -30,7 +30,6 @@ #include #include "types.h" #include "filegen.h" -#include "log.h" static void register_header_check_r3d(file_stat_t *file_stat); static int header_check_r3d(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new); @@ -82,9 +81,8 @@ static void file_rename_r3d(const char *old_filename) return; for(i=0x43; i< buffer_size && buffer[i]!=0 && buffer[i]!='.'; i++) { - log_info("%c", buffer[i]); if(!isalnum(buffer[i]) && buffer[i]!='_') return ; } - file_rename(old_filename, buffer, i, 0x43); + file_rename(old_filename, buffer, i, 0x43, NULL, 1); } diff --git a/src/filegen.c b/src/filegen.c index 9fe368bc..8e939ab2 100644 --- a/src/filegen.c +++ b/src/filegen.c @@ -341,7 +341,7 @@ file_stat_t * init_file_stats(file_enable_t *files_enable) } /* The original filename begins at offset in buffer and is null terminated */ -void file_rename(const char *old_filename, const unsigned char *buffer, const int buffer_size, const int offset) +void file_rename(const char *old_filename, const unsigned char *buffer, const int buffer_size, const int offset, const char *new_ext, const int force_ext) { /* new_filename is large enough to avoid a buffer overflow */ char new_filename[2048]; @@ -350,8 +350,6 @@ void file_rename(const char *old_filename, const unsigned char *buffer, const in char *dst=&new_filename[0]; char *directory_sep=&new_filename[0]; int off=offset; - if(buffer_size <= offset) - return ; *dst='\0'; while(*src!='\0') { @@ -364,60 +362,96 @@ void file_rename(const char *old_filename, const unsigned char *buffer, const in dst=directory_sep; while(*dst!='.' && *dst!='\0') dst++; - *dst++ = '_'; /* Add original filename */ - while(off0) + { + while(*ext!='\0') + *dst++ = *ext++; + } + *dst='\0'; + rename(old_filename, new_filename); +} + +/* The original filename begins at offset in buffer and is null terminated */ +void file_rename_unicode(const char *old_filename, const unsigned char *buffer, const int buffer_size, const int offset, const char *new_ext, const int force_ext) +{ + /* new_filename is large enough to avoid a buffer overflow */ + char new_filename[2048]; + const char *src=old_filename; + const char *ext=src; + char *dst=&new_filename[0]; + char *directory_sep=&new_filename[0]; + int off=offset; + *dst='\0'; + while(*src!='\0') + { + if(*src=='/') + directory_sep=dst; + if(*src=='.') + ext=src; + *dst++ = *src++; + } + dst=directory_sep; + while(*dst!='.' && *dst!='\0') + dst++; + /* Add original filename */ + if(offset < buffer_size && buffer!=NULL) + { + *dst++ = '_'; + for(;off0) { - /* Add extension */ while(*ext!='\0') *dst++ = *ext++; } diff --git a/src/filegen.h b/src/filegen.h index 1d14b2af..812eeeb9 100644 --- a/src/filegen.h +++ b/src/filegen.h @@ -130,8 +130,8 @@ void register_header_check(const unsigned int offset, const unsigned char *value const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new), file_stat_t *file_stat); file_stat_t * init_file_stats(file_enable_t *files_enable); -void file_rename(const char *old_filename, const unsigned char *buffer, const int buffer_size, const int offset); -void file_rename_unicode(const char *old_filename, const unsigned char *buffer, const int buffer_size, const int offset, const int force_ext); +void file_rename(const char *old_filename, const unsigned char *buffer, const int buffer_size, const int offset, const char *new_ext, const int force_ext); +void file_rename_unicode(const char *old_filename, const unsigned char *buffer, const int buffer_size, const int offset, const char *new_ext, const int force_ext); #ifdef __cplusplus } /* closing brace for extern "C" */