summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2015-06-12 19:49:50 +0200
committerChristophe Grenier <grenier@cgsecurity.org>2015-06-12 19:49:50 +0200
commit04d653114489586908a37b6f405340fdcb9a3824 (patch)
tree6ecd19bcbadcfdb1b575402ff5856991f80cbbee
parent56e27e660059e73f1315f83c660fc358c02e41d7 (diff)
PhotoRec: log the correct filename if file has been renamed
-rw-r--r--src/file_d2s.c6
-rw-r--r--src/file_dir.c6
-rw-r--r--src/file_doc.c16
-rw-r--r--src/file_exe.c24
-rw-r--r--src/file_exs.c6
-rw-r--r--src/file_ext.c12
-rw-r--r--src/file_gz.c8
-rw-r--r--src/file_lzh.c6
-rw-r--r--src/file_m2ts.c12
-rw-r--r--src/file_mft.c6
-rw-r--r--src/file_mov.c6
-rw-r--r--src/file_par2.c8
-rw-r--r--src/file_pdf.c6
-rw-r--r--src/file_pzh.c6
-rw-r--r--src/file_r3d.c10
-rw-r--r--src/file_rpm.c6
-rw-r--r--src/file_zip.c10
-rw-r--r--src/filegen.c61
-rw-r--r--src/filegen.h6
-rw-r--r--src/photorec.c2
20 files changed, 123 insertions, 100 deletions
diff --git a/src/file_d2s.c b/src/file_d2s.c
index b809638..d31b049 100644
--- a/src/file_d2s.c
+++ b/src/file_d2s.c
@@ -50,16 +50,16 @@ struct d2s_header {
char name[0];
} __attribute__ ((gcc_struct, __packed__));
-static void file_rename_d2s(const char *old_filename)
+static void file_rename_d2s(file_recovery_t *file_recovery)
{
unsigned char buffer[512];
FILE *file;
int buffer_size;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
buffer_size=fread(buffer, 1, sizeof(buffer), file);
fclose(file);
- file_rename(old_filename, buffer, buffer_size, 0x14, NULL, 1);
+ file_rename(file_recovery, buffer, buffer_size, 0x14, NULL, 1);
}
static int header_check_d2s(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)
diff --git a/src/file_dir.c b/src/file_dir.c
index 5035bf7..956e6e3 100644
--- a/src/file_dir.c
+++ b/src/file_dir.c
@@ -51,14 +51,14 @@ const file_hint_t file_hint_dir= {
.register_header_check=&register_header_check_dir
};
-static void file_rename_fatdir(const char *old_filename)
+static void file_rename_fatdir(file_recovery_t *file_recovery)
{
unsigned char buffer[512];
char buffer_cluster[32];
FILE *file;
int buffer_size;
unsigned int cluster;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
buffer_size=fread(buffer, 1, sizeof(buffer), file);
fclose(file);
@@ -66,7 +66,7 @@ static void file_rename_fatdir(const char *old_filename)
return;
cluster=fat_get_cluster_from_entry((const struct msdos_dir_entry *)&buffer[0]);
sprintf(buffer_cluster, "cluster_%u", cluster);
- file_rename(old_filename, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1);
+ file_rename(file_recovery, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1);
}
static data_check_t data_check_fatdir(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
diff --git a/src/file_doc.c b/src/file_doc.c
index 44f0068..9f62ff8 100644
--- a/src/file_doc.c
+++ b/src/file_doc.c
@@ -47,7 +47,7 @@
static void register_header_check_doc(file_stat_t *file_stat);
static void file_check_doc(file_recovery_t *file_recovery);
static int header_check_doc(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);
-static void file_rename_doc(const char *old_filename);
+static void file_rename_doc(file_recovery_t *file_recovery);
static uint32_t *OLE_load_FAT(FILE *IN, const struct OLE_HDR *header);
static uint32_t *OLE_load_MiniFAT(FILE *IN, const struct OLE_HDR *header, const uint32_t *fat, const unsigned int fat_entries);
@@ -820,7 +820,7 @@ static void OLE_parse_summary(FILE *file, const uint32_t *fat, const unsigned in
}
}
-static void file_rename_doc(const char *old_filename)
+static void file_rename_doc(file_recovery_t *file_recovery)
{
const char *ext=NULL;
char *title=NULL;
@@ -830,12 +830,12 @@ static void file_rename_doc(const char *old_filename)
const struct OLE_HDR *header=(const struct OLE_HDR*)&buffer_header;
time_t file_time=0;
unsigned int fat_entries;
- if(strstr(old_filename, ".sdd")!=NULL)
+ if(strstr(file_recovery->filename, ".sdd")!=NULL)
ext="sdd";
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
#ifdef DEBUG_OLE
- log_info("file_rename_doc(%s)\n", old_filename);
+ log_info("file_rename_doc(%s)\n", file_recovery->filename);
#endif
/*reads first sector including OLE header */
if(my_fseek(file, 0, SEEK_SET) < 0 ||
@@ -1058,14 +1058,14 @@ static void file_rename_doc(const char *old_filename)
free(fat);
fclose(file);
if(file_time!=0 && file_time!=(time_t)-1)
- set_date(old_filename, file_time, file_time);
+ set_date(file_recovery->filename, file_time, file_time);
if(title!=NULL)
{
- file_rename(old_filename, (const unsigned char*)title, strlen(title), 0, ext, 1);
+ file_rename(file_recovery, (const unsigned char*)title, strlen(title), 0, ext, 1);
free(title);
}
else
- file_rename(old_filename, NULL, 0, 0, ext, 1);
+ file_rename(file_recovery, NULL, 0, 0, ext, 1);
}
static void register_header_check_doc(file_stat_t *file_stat)
diff --git a/src/file_exe.c b/src/file_exe.c
index 05aa35b..ccade72 100644
--- a/src/file_exe.c
+++ b/src/file_exe.c
@@ -38,7 +38,7 @@
static void register_header_check_exe(file_stat_t *file_stat);
static int header_check_exe(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);
-static void file_rename_pe_exe(const char *old_filename);
+static void file_rename_pe_exe(file_recovery_t *file_recovery);
const file_hint_t file_hint_exe= {
.extension="exe",
@@ -253,7 +253,7 @@ static unsigned int ReadUnicodeStr(const char *buffer, unsigned int pos, const u
return pos;
}
-static int PEVersion_aux(const char*buffer, const unsigned int end, const char *old_filename, const char *needle, const unsigned int needle_len, const int force_ext)
+static int PEVersion_aux(file_recovery_t *file_recovery, const char*buffer, const unsigned int end, const char *needle, const unsigned int needle_len, const int force_ext)
{
unsigned int pos=0;
while(1)
@@ -306,7 +306,7 @@ static int PEVersion_aux(const char*buffer, const unsigned int end, const char *
{
if(do_rename)
{
- file_rename_unicode(old_filename, buffer, end, pt, NULL, force_ext);
+ file_rename_unicode(file_recovery, buffer, end, pt, NULL, force_ext);
return 0;
}
#ifdef DEBUG_EXE
@@ -328,7 +328,7 @@ static int PEVersion_aux(const char*buffer, const unsigned int end, const char *
}
}
-static void PEVersion(FILE *file, const unsigned int offset, const unsigned int length, const char *old_filename)
+static void PEVersion(FILE *file, const unsigned int offset, const unsigned int length, file_recovery_t *file_recovery)
{
char *buffer;
if(length==0 || length > 1024*1024)
@@ -341,16 +341,16 @@ static void PEVersion(FILE *file, const unsigned int offset, const unsigned int
free(buffer);
return ;
}
- if(PEVersion_aux(buffer, length, old_filename, OriginalFilename, sizeof(OriginalFilename), 0)==0)
+ if(PEVersion_aux(file_recovery, buffer, length, OriginalFilename, sizeof(OriginalFilename), 0)==0)
{
free(buffer);
return;
}
- PEVersion_aux(buffer, length, old_filename, InternalName, sizeof(InternalName), 1);
+ PEVersion_aux(file_recovery, buffer, length, InternalName, sizeof(InternalName), 1);
free(buffer);
}
-static void file_exe_ressource(FILE *file, const unsigned int base, const unsigned int dir_start, const unsigned int size, const unsigned int rsrcType, const unsigned int level, const struct pe_image_section_hdr *pe_sections, unsigned int nbr_sections, const char *old_filename)
+static void file_exe_ressource(FILE *file, const unsigned int base, const unsigned int dir_start, const unsigned int size, const unsigned int rsrcType, const unsigned int level, const struct pe_image_section_hdr *pe_sections, unsigned int nbr_sections, file_recovery_t *file_recovery)
{
struct rsrc_entries_s *rsrc_entries;
struct rsrc_entries_s *rsrc_entry;
@@ -401,7 +401,7 @@ static void file_exe_ressource(FILE *file, const unsigned int base, const unsign
size,
(level==0?le32(rsrc_entry->Type):rsrcType),
level + 1,
- pe_sections, nbr_sections, old_filename);
+ pe_sections, nbr_sections, file_recovery);
}
if(level==2)
{
@@ -421,7 +421,7 @@ static void file_exe_ressource(FILE *file, const unsigned int base, const unsign
if(le32(pe_section->VirtualAddress) <= off
&& off < le32(pe_section->VirtualAddress) + le32(pe_section->SizeOfRawData))
{
- PEVersion(file, off - le32(pe_section->VirtualAddress) + base, len, old_filename);
+ PEVersion(file, off - le32(pe_section->VirtualAddress) + base, len, file_recovery);
free(rsrc_entries);
return ;
}
@@ -435,14 +435,14 @@ static void file_exe_ressource(FILE *file, const unsigned int base, const unsign
free(rsrc_entries);
}
-static void file_rename_pe_exe(const char *old_filename)
+static void file_rename_pe_exe(file_recovery_t *file_recovery)
{
unsigned char buffer[4096];
FILE *file;
int buffer_size;
const struct dos_image_file_hdr *dos_hdr=(const struct dos_image_file_hdr*)buffer;
const struct pe_image_file_hdr *pe_hdr;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
buffer_size=fread(buffer, 1, sizeof(buffer), file);
if(buffer_size < (int)sizeof(struct dos_image_file_hdr))
@@ -503,7 +503,7 @@ static void file_rename_pe_exe(const char *old_filename)
le32(pe_section->SizeOfRawData),
0,
0,
- pe_sections, nbr_sections, old_filename);
+ pe_sections, nbr_sections, file_recovery);
fclose(file);
return;
}
diff --git a/src/file_exs.c b/src/file_exs.c
index 370f292..0b1cd26 100644
--- a/src/file_exs.c
+++ b/src/file_exs.c
@@ -42,16 +42,16 @@ const file_hint_t file_hint_exs= {
.register_header_check=&register_header_check_exs
};
-static void file_rename_exs(const char *old_filename)
+static void file_rename_exs(file_recovery_t *file_recovery)
{
unsigned char buffer[512];
FILE *file;
int buffer_size;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
buffer_size=fread(buffer, 1, sizeof(buffer), file);
fclose(file);
- file_rename(old_filename, buffer, buffer_size, 0x14, "exs", 0);
+ file_rename(file_recovery, buffer, buffer_size, 0x14, "exs", 0);
}
static int header_check_exs(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)
diff --git a/src/file_ext.c b/src/file_ext.c
index dfd89ce..da2a296 100644
--- a/src/file_ext.c
+++ b/src/file_ext.c
@@ -45,7 +45,7 @@ const file_hint_t file_hint_ext2_sb= {
.register_header_check=&register_header_check_ext2_sb
};
-static void file_rename_ext(const char *old_filename)
+static void file_rename_ext(file_recovery_t *file_recovery)
{
unsigned char buffer[512];
char buffer_cluster[32];
@@ -53,7 +53,7 @@ static void file_rename_ext(const char *old_filename)
const struct ext2_super_block *sb=(const struct ext2_super_block *)&buffer;
int buffer_size;
unsigned long int block_nr;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
buffer_size=fread(buffer, 1, sizeof(buffer), file);
fclose(file);
@@ -61,7 +61,7 @@ static void file_rename_ext(const char *old_filename)
return;
block_nr=(le32(sb->s_first_data_block)+le16(sb->s_block_group_nr)*le32(sb->s_blocks_per_group));
sprintf(buffer_cluster, "sb_%lu", block_nr);
- file_rename(old_filename, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1);
+ file_rename(file_recovery, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1);
}
static int header_check_ext2_sb(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)
@@ -85,21 +85,21 @@ static data_check_t data_check_extdir(const unsigned char *buffer, const unsigne
return DC_STOP;
}
-static void file_rename_extdir(const char *old_filename)
+static void file_rename_extdir(file_recovery_t *file_recovery)
{
unsigned char buffer[512];
char buffer_cluster[32];
FILE *file;
int buffer_size;
const uint32_t *inode=(const uint32_t *)&buffer[0];
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
buffer_size=fread(buffer, 1, sizeof(buffer), file);
fclose(file);
if(buffer_size!=sizeof(buffer))
return;
sprintf(buffer_cluster, "inode_%u", (unsigned int)le32(*inode));
- file_rename(old_filename, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1);
+ file_rename(file_recovery, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1);
}
static int header_check_ext2_dir(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)
diff --git a/src/file_gz.c b/src/file_gz.c
index 2dd8ac9..a3f79c8 100644
--- a/src/file_gz.c
+++ b/src/file_gz.c
@@ -36,7 +36,7 @@
static void register_header_check_gz(file_stat_t *file_stat);
static int header_check_gz(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);
-static void file_rename_gz(const char *old_filename);
+static void file_rename_gz(file_recovery_t *file_recovery);
const file_hint_t file_hint_gz= {
.extension="gz",
@@ -239,12 +239,12 @@ static int header_check_gz(const unsigned char *buffer, const unsigned int buffe
return 1;
}
-static void file_rename_gz(const char *old_filename)
+static void file_rename_gz(file_recovery_t *file_recovery)
{
unsigned char buffer[512];
FILE *file;
int buffer_size;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
buffer_size=fread(buffer, 1, sizeof(buffer), file);
fclose(file);
@@ -262,7 +262,7 @@ static void file_rename_gz(const char *old_filename)
}
if((flags&GZ_FNAME)!=0)
{
- file_rename(old_filename, buffer, buffer_size, off, NULL, 1);
+ file_rename(file_recovery, buffer, buffer_size, off, NULL, 1);
}
}
}
diff --git a/src/file_lzh.c b/src/file_lzh.c
index 7052dde..41a93c1 100644
--- a/src/file_lzh.c
+++ b/src/file_lzh.c
@@ -86,21 +86,21 @@ struct lzh_level2
uint16_t next_header_size;
} __attribute__ ((gcc_struct, __packed__));
-static void file_rename_level0(const char *old_filename)
+static void file_rename_level0(file_recovery_t *file_recovery)
{
unsigned char buffer[512];
FILE *file;
size_t buffer_size;
unsigned int i;
const struct lzh_level0 *hdr=(const struct lzh_level0 *)&buffer;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
buffer_size=fread(buffer, 1, sizeof(buffer), file);
fclose(file);
if(buffer_size<sizeof(struct lzh_level0))
return;
for(i=0; i< hdr->filename_len && hdr->filename[i]!=0 && hdr->filename[i]!='.'; i++);
- file_rename(old_filename, hdr->filename, i, 0, NULL, 1);
+ file_rename(file_recovery, hdr->filename, i, 0, NULL, 1);
}
static int header_check_lzh(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)
diff --git a/src/file_m2ts.c b/src/file_m2ts.c
index a55c51a..e1e8f47 100644
--- a/src/file_m2ts.c
+++ b/src/file_m2ts.c
@@ -77,13 +77,13 @@ static data_check_t data_check_ts_192(const unsigned char *buffer, const unsigne
return DC_CONTINUE;
}
-static void file_rename_ts_188(const char *old_filename)
+static void file_rename_ts_188(file_recovery_t *file_recovery)
{
FILE *file;
unsigned char buffer[188];
char buffer_pid[32];
unsigned int pid;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
if(my_fseek(file, 0, SEEK_SET) < 0 ||
fread(&buffer, sizeof(buffer), 1, file) != 1)
@@ -94,16 +94,16 @@ static void file_rename_ts_188(const char *old_filename)
fclose(file);
pid=((buffer[1]<<8)|buffer[2])&0x1fff;
sprintf(buffer_pid, "pid_%u", pid);
- file_rename(old_filename, (const unsigned char*)buffer_pid, strlen(buffer_pid), 0, NULL, 1);
+ file_rename(file_recovery, (const unsigned char*)buffer_pid, strlen(buffer_pid), 0, NULL, 1);
}
-static void file_rename_ts_192(const char *old_filename)
+static void file_rename_ts_192(file_recovery_t *file_recovery)
{
FILE *file;
unsigned char buffer[192];
char buffer_pid[32];
unsigned int pid;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
if(my_fseek(file, 0, SEEK_SET) < 0 ||
fread(&buffer, sizeof(buffer), 1, file) != 1)
@@ -114,7 +114,7 @@ static void file_rename_ts_192(const char *old_filename)
fclose(file);
pid=((buffer[5]<<8)|buffer[6])&0x1fff;
sprintf(buffer_pid, "pid_%u", pid);
- file_rename(old_filename, (const unsigned char*)buffer_pid, strlen(buffer_pid), 0, NULL, 1);
+ file_rename(file_recovery, (const unsigned char*)buffer_pid, strlen(buffer_pid), 0, NULL, 1);
}
static int header_check_m2ts(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)
diff --git a/src/file_mft.c b/src/file_mft.c
index 40bd1f2..493a76c 100644
--- a/src/file_mft.c
+++ b/src/file_mft.c
@@ -45,21 +45,21 @@ const file_hint_t file_hint_mft= {
.register_header_check=&register_header_check_mft
};
-static void file_rename_mft(const char *old_filename)
+static void file_rename_mft(file_recovery_t *file_recovery)
{
unsigned char buffer[512];
char buffer_cluster[32];
FILE *file;
int buffer_size;
const struct ntfs_mft_record *record=(const struct ntfs_mft_record *)&buffer;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
buffer_size=fread(buffer, 1, sizeof(buffer), file);
fclose(file);
if(buffer_size<54)
return;
sprintf(buffer_cluster, "record_%u", (unsigned int)le32(record->mft_record_number));
- file_rename(old_filename, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1);
+ file_rename(file_recovery, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1);
}
static int header_check_mft(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)
diff --git a/src/file_mov.c b/src/file_mov.c
index 3f56e2c..f5dcf86 100644
--- a/src/file_mov.c
+++ b/src/file_mov.c
@@ -79,11 +79,11 @@ struct atom64_struct
uint64_t size;
} __attribute__ ((gcc_struct, __packed__));
-static void file_rename_mov(const char *old_filename)
+static void file_rename_mov(file_recovery_t *file_recovery)
{
FILE *file;
unsigned char buffer[512];
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
if(fread(&buffer,sizeof(buffer),1,file)!=1)
{
@@ -92,7 +92,7 @@ static void file_rename_mov(const char *old_filename)
}
fclose(file);
buffer[8]='\0';
- file_rename(old_filename, buffer, sizeof(buffer), 4, NULL, 1);
+ file_rename(file_recovery, buffer, sizeof(buffer), 4, NULL, 1);
}
static int header_check_mov(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)
diff --git a/src/file_par2.c b/src/file_par2.c
index f63f0ee..017e6cc 100644
--- a/src/file_par2.c
+++ b/src/file_par2.c
@@ -70,11 +70,11 @@ static data_check_t data_check_par2(const unsigned char *buffer, const unsigned
return DC_CONTINUE;
}
-static void file_rename_par2(const char *old_filename)
+static void file_rename_par2(file_recovery_t *file_recovery)
{
FILE *file;
uint64_t offset=0;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
while(1)
{
@@ -102,10 +102,10 @@ static void file_rename_par2(const char *old_filename)
}
if(memcmp(&buffer[0x30], "PAR 2.0\0FileDesc", 16)==0)
{
- file_rename(old_filename, buffer,
+ fclose(file);
+ file_rename(file_recovery, buffer,
(length < buffer_size ? length : buffer_size),
0x78, NULL, 1);
- fclose(file);
return ;
}
offset+=length;
diff --git a/src/file_pdf.c b/src/file_pdf.c
index 818cb43..3d0afac 100644
--- a/src/file_pdf.c
+++ b/src/file_pdf.c
@@ -69,7 +69,7 @@ static int hex(int c)
return -1;
}
-static void file_rename_pdf(const char *old_filename)
+static void file_rename_pdf(file_recovery_t *file_recovery)
{
char title[512];
const unsigned char pattern[6]={ '/', 'T', 'i', 't', 'l', 'e' };
@@ -80,7 +80,7 @@ static void file_rename_pdf(const char *old_filename)
unsigned int j;
size_t bsize;
const unsigned char utf16[3]= { 0xfe, 0xff, 0x00};
- if((handle=fopen(old_filename, "rb"))==NULL)
+ if((handle=fopen(file_recovery->filename, "rb"))==NULL)
return;
if(my_fseek(handle, 0, SEEK_END)<0)
{
@@ -187,7 +187,7 @@ static void file_rename_pdf(const char *old_filename)
(memcmp(&title[j-5], ".docx", 5)==0 ||
memcmp(&title[j-5], ".xlsx", 5)==0))
j-=5;
- file_rename(old_filename, title, j, 0, NULL, 1);
+ file_rename(file_recovery, title, j, 0, NULL, 1);
free(buffer);
}
diff --git a/src/file_pzh.c b/src/file_pzh.c
index 692878e..b6ed094 100644
--- a/src/file_pzh.c
+++ b/src/file_pzh.c
@@ -49,12 +49,12 @@ static const unsigned char pzh_header[10]= {
0x05, 0x03
};
-static void file_rename_pzh(const char *old_filename)
+static void file_rename_pzh(file_recovery_t *file_recovery)
{
unsigned char buffer[512];
FILE *file;
int buffer_size;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
if(fseek(file, 0x9ce, SEEK_SET)<0)
{
@@ -64,7 +64,7 @@ static void file_rename_pzh(const char *old_filename)
buffer_size=fread(buffer, 1, sizeof(buffer), file);
fclose(file);
if(buffer_size > 0)
- file_rename(old_filename, buffer, buffer_size, 0, "pzh", 0);
+ file_rename(file_recovery, buffer, buffer_size, 0, "pzh", 0);
}
static int header_check_pzh(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)
diff --git a/src/file_r3d.c b/src/file_r3d.c
index 8477c0e..5052829 100644
--- a/src/file_r3d.c
+++ b/src/file_r3d.c
@@ -35,7 +35,7 @@
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);
-static void file_rename_r3d(const char *old_filename);
+static void file_rename_r3d(file_recovery_t *file_recovery);
const file_hint_t file_hint_r3d= {
.extension="r3d",
@@ -125,24 +125,24 @@ static int header_check_r3d_v2(const unsigned char *buffer, const unsigned int b
return 0;
}
-static void file_rename_r3d(const char *old_filename)
+static void file_rename_r3d(file_recovery_t *file_recovery)
{
unsigned char buffer[512];
FILE *file;
size_t buffer_size;
unsigned int i;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
buffer_size=fread(buffer, 1, sizeof(buffer), file);
fclose(file);
- if(buffer_size<10)
+ if(buffer_size<0x44)
return;
for(i=0x43; i< buffer_size && buffer[i]!=0 && buffer[i]!='.'; i++)
{
if(!isalnum(buffer[i]) && buffer[i]!='_')
return ;
}
- file_rename(old_filename, buffer, i, 0x43, NULL, 1);
+ file_rename(file_recovery, buffer, i, 0x43, NULL, 1);
}
static void register_header_check_r3d(file_stat_t *file_stat)
diff --git a/src/file_rpm.c b/src/file_rpm.c
index 56c407a..b86bbc2 100644
--- a/src/file_rpm.c
+++ b/src/file_rpm.c
@@ -56,11 +56,11 @@ struct rpmlead {
char reserved[16];
} __attribute__ ((gcc_struct, __packed__));
-static void file_rename_rpm(const char *old_filename)
+static void file_rename_rpm(file_recovery_t *file_recovery)
{
FILE *file;
struct rpmlead hdr;
- if((file=fopen(old_filename, "rb"))==NULL)
+ if((file=fopen(file_recovery->filename, "rb"))==NULL)
return;
if(fread(&hdr, sizeof(hdr), 1, file)!=1)
{
@@ -68,7 +68,7 @@ static void file_rename_rpm(const char *old_filename)
return ;
}
fclose(file);
- file_rename(old_filename, &hdr.name, 66, 0, "rpm", 0);
+ file_rename(file_recovery, &hdr.name, 66, 0, "rpm", 0);
}
static int header_check_rpm(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)
diff --git a/src/file_zip.c b/src/file_zip.c
index 9a568dd..7c796e2 100644
--- a/src/file_zip.c
+++ b/src/file_zip.c
@@ -53,7 +53,7 @@ static void register_header_check_zip(file_stat_t *file_stat);
static int header_check_zip(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);
static void file_check_zip(file_recovery_t *file_recovery);
static unsigned int pos_in_mem(const unsigned char *haystack, const unsigned int haystack_size, const unsigned char *needle, const unsigned int needle_size);
-static void file_rename_zip(const char *old_filename);
+static void file_rename_zip(file_recovery_t *file_recovery);
static char first_filename[256];
const file_hint_t file_hint_zip= {
@@ -668,13 +668,13 @@ static void file_check_zip(file_recovery_t *fr)
}
}
-static void file_rename_zip(const char *old_filename)
+static void file_rename_zip(file_recovery_t *file_recovery)
{
const char *ext=NULL;
unsigned int file_nbr=0;
file_recovery_t fr;
reset_file_recovery(&fr);
- if((fr.handle=fopen(old_filename, "rb"))==NULL)
+ if((fr.handle=fopen(file_recovery->filename, "rb"))==NULL)
return;
fr.file_size = 0;
fr.offset_error=0;
@@ -728,7 +728,7 @@ static void file_rename_zip(const char *old_filename)
if(ext!=NULL)
{
fclose(fr.handle);
- file_rename(old_filename, NULL, 0, 0, ext, 1);
+ file_rename(file_recovery, NULL, 0, 0, ext, 1);
return;
}
break;
@@ -763,7 +763,7 @@ static void file_rename_zip(const char *old_filename)
first_filename[len]!='/' &&
first_filename[len]!='\\';
len++);
- file_rename(old_filename, first_filename, len, 0, "zip", 0);
+ file_rename(file_recovery, first_filename, len, 0, "zip", 0);
return;
}
}
diff --git a/src/filegen.c b/src/filegen.c
index fee262d..49f56a3 100644
--- a/src/filegen.c
+++ b/src/filegen.c
@@ -363,17 +363,18 @@ 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 void *buffer, const int buffer_size, const int offset, const char *new_ext, const int force_ext)
+int file_rename(file_recovery_t *file_recovery, const void *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;
- const char *src=old_filename;
- const char *ext=src;
+ const char *src=file_recovery->filename;
+ const char *ext=NULL;
char *dst;
char *directory_sep;
- int len=strlen(old_filename)+1;
+ int len;
if(buffer_size<0)
- return ;
+ return -1;
+ len=strlen(src)+1;
if(offset < buffer_size && buffer!=NULL)
len+=buffer_size-offset+1;
if(new_ext!=NULL)
@@ -384,7 +385,10 @@ void file_rename(const char *old_filename, const void *buffer, const int buffer_
while(*src!='\0')
{
if(*src=='/')
+ {
directory_sep=dst;
+ ext=NULL;
+ }
if(*src=='.')
ext=src;
*dst++ = *src++;
@@ -450,31 +454,42 @@ void file_rename(const char *old_filename, const void *buffer, const int buffer_
}
else if(force_ext>0)
{
- while(*ext!='\0')
- *dst++ = *ext++;
+ if(ext!=NULL)
+ {
+ while(*ext!='\0')
+ *dst++ = *ext++;
+ }
}
*dst='\0';
- if(rename(old_filename, new_filename)<0)
+ if(rename(file_recovery->filename, new_filename)<0)
{
- /* Rename has failed, try without the original filename */
- if(buffer!=NULL)
- file_rename(old_filename, NULL, 0, 0, new_ext, force_ext);
+ /* Rename has failed */
+ free(new_filename);
+ if(buffer==NULL)
+ return -1;
+ /* Try without the original filename */
+ return file_rename(file_recovery, NULL, 0, 0, new_ext, force_ext);
+ }
+ if(strlen(new_filename)<sizeof(file_recovery->filename))
+ {
+ strcpy(file_recovery->filename, new_filename);
}
free(new_filename);
+ return 0;
}
/* The original filename begins at offset in buffer and is null terminated */
-void file_rename_unicode(const char *old_filename, const void *buffer, const int buffer_size, const int offset, const char *new_ext, const int force_ext)
+int file_rename_unicode(file_recovery_t *file_recovery, const void *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;
- const char *src=old_filename;
+ const char *src=file_recovery->filename;
const char *ext=src;
char *dst;
char *directory_sep;
- int len=strlen(old_filename)+1;
+ int len=strlen(src)+1;
if(buffer_size<0)
- return ;
+ return -1;
if(offset < buffer_size && buffer!=NULL)
len+=buffer_size-offset;
if(new_ext!=NULL)
@@ -551,11 +566,19 @@ void file_rename_unicode(const char *old_filename, const void *buffer, const int
*dst++ = *ext++;
}
*dst='\0';
- if(rename(old_filename, new_filename)<0)
+ if(rename(file_recovery->filename, new_filename)<0)
+ {
+ /* Rename has failed */
+ free(new_filename);
+ if(buffer==NULL)
+ return -1;
+ /* Try without the original filename */
+ return file_rename_unicode(file_recovery, NULL, 0, 0, new_ext, force_ext);
+ }
+ if(strlen(new_filename)<sizeof(file_recovery->filename))
{
- /* Rename has failed, try without the original filename */
- if(buffer!=NULL)
- file_rename_unicode(old_filename, NULL, 0, 0, new_ext, force_ext);
+ strcpy(file_recovery->filename, new_filename);
}
free(new_filename);
+ return 0;
}
diff --git a/src/filegen.h b/src/filegen.h
index c601400..d65e2f6 100644
--- a/src/filegen.h
+++ b/src/filegen.h
@@ -79,7 +79,7 @@ struct file_recovery_struct
data_check_t (*data_check)(const unsigned char*buffer, const unsigned int buffer_size, file_recovery_t *file_recovery);
/* data_check modifies file_recovery->calculated_file_size but not must alter file_recovery->file_size */
void (*file_check)(file_recovery_t *file_recovery);
- void (*file_rename)(const char *old_filename);
+ void (*file_rename)(file_recovery_t *file_recovery);
uint64_t checkpoint_offset;
int checkpoint_status; /* 0=suspend at offset_checkpoint if offset_checkpoint>0, 1=resume at offset_checkpoint */
unsigned int blocksize;
@@ -135,8 +135,8 @@ void register_header_check(const unsigned int offset, const void *value, const u
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 void *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 void *buffer, const int buffer_size, const int offset, const char *new_ext, const int force_ext);
+int file_rename(file_recovery_t *file_recovery, const void *buffer, const int buffer_size, const int offset, const char *new_ext, const int force_ext);
+int file_rename_unicode(file_recovery_t *file_recovery, const void *buffer, const int buffer_size, const int offset, const char *new_ext, const int force_ext);
#ifdef __cplusplus
} /* closing brace for extern "C" */
diff --git a/src/photorec.c b/src/photorec.c
index 7d6bc3f..3276eaf 100644
--- a/src/photorec.c
+++ b/src/photorec.c
@@ -590,7 +590,7 @@ static void file_finish_aux(file_recovery_t *file_recovery, struct ph_param *par
if(file_recovery->time!=0 && file_recovery->time!=(time_t)-1)
set_date(file_recovery->filename, file_recovery->time, file_recovery->time);
if(file_recovery->file_rename!=NULL)
- file_recovery->file_rename(file_recovery->filename);
+ file_recovery->file_rename(file_recovery);
if((++params->file_nbr)%MAX_FILES_PER_DIR==0)
{
params->dir_num=photorec_mkdir(params->recup_dir, params->dir_num+1);