diff options
author | Christophe Grenier <[email protected]> | 2008-07-07 22:32:13 +0200 |
---|---|---|
committer | Christophe Grenier <[email protected]> | 2008-07-07 22:32:13 +0200 |
commit | e09fa6f39c889cbe327510eaa1523a413d5bcde2 (patch) | |
tree | 87c0f179dae28adf2d804fccb05c29044bba4c7f | |
parent | b50843560aa4f2a6afdfdf0d190db909c3449278 (diff) |
PhotoRec: add "/cmd device ext2_group,group_nr,search" to carve the corresponding ext2/ext3 group number
or "/cmd device ext2_inode,inode_nr,search" to carve the group holding the specified inode
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/ext2grp.c | 117 | ||||
-rw-r--r-- | src/ext2grp.h | 23 | ||||
-rw-r--r-- | src/list.h | 19 | ||||
-rw-r--r-- | src/phrecn.c | 68 |
5 files changed, 227 insertions, 2 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 960b340d..6660bb27 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,7 +19,7 @@ fs_H = analyse.h bfs.h bsd.h cramfs.h fat.h fatx.h ext2.h jfs_superblock.h jfs testdisk_SOURCES = testdisk.c $(base_C) $(base_H) $(fs_C) $(fs_H) testdisk.h adv.c adv.h dir.c dir.h dirpart.c dirpart.h edit.c edit.h ext2_dir.c ext2_dir.h ext2_inc.h fat_adv.c fat_adv.h fat_dir.c fat_dir.h geometry.c godmode.c godmode.h intrface.c intrface.h ntfs_adv.c ntfs_dir.c ntfs_dir.h ntfs_fix.c ntfs_inc.h rfs_dir.c rfs_dir.h $(ICON_TESTDISK) next.c next.h dimage.c dimage.h #ntfs_udl.c ntfs_udl.h -photorec_SOURCES = photorec.c photorec.h phcfg.c phcfg.h phrecn.c phrecn.h dir.c dir.h ext2p.c ext2p.h ext2_dir.c ext2_dir.h ext2_inc.h fat_dir.c fat_dir.h fatp.c fatp.h filegen.c filegen.h file_7z.c file_a.c file_ab.c file_ace.c file_aif.c file_all.c file_als.c file_amd.c file_amr.c file_arj.c file_asf.c file_asm.c file_au.c file_bkf.c file_bld.c file_bmp.c file_bz2.c file_cab.c file_cam.c file_chm.c file_cm.c file_crw.c file_ctg.c file_cwk.c file_dat.c file_dbf.c file_dim.c file_dir.c file_djv.c file_doc.c file_dpx.c file_drw.c file_ds2.c file_dsc.c file_dss.c file_dta.c file_dump.c file_dv.c file_dwg.c file_elf.c file_emf.c file_evt.c file_exe.c pe.h file_ext.c file_fcp.c file_fcs.c file_fh10.c file_fh5.c file_fits.c file_flac.c file_flv.c file_frm.c file_fs.c file_gho.c file_gif.c file_gpg.c file_gz.c file_ifo.c file_imb.c file_indd.c file_iso.c file_itu.c file_jpg.c file_jpg.h file_kdb.c file_lnk.c file_m2ts.c file_max.c file_mb.c file_mcd.c file_mdb.c file_mdf.c file_mfg.c file_mid.c file_mkv.c file_mov.c file_mp3.c file_mpg.c file_mrw.c file_mus.c file_mysql.c file_njx.c file_ogg.c file_one.c file_orf.c file_paf.c file_pap.c file_pct.c file_pcx.c file_pdf.c file_pfx.c file_png.c file_prc.c file_prt.c file_ps.c file_psd.c file_psp.c file_pst.c file_ptb.c file_qbb.c file_qdf.c file_qxd.c file_ra.c file_raf.c file_rar.c file_raw.c file_rdc.c file_reg.c file_res.c file_riff.c file_rm.c file_rns.c file_rpm.c file_sit.c file_skp.c file_spe.c file_spss.c file_sql.c file_stl.c file_stu.c file_swf.c file_tar.c file_tar.h file_tib.c file_tiff.c file_tph.c file_txt.c file_vmdk.c file_wpd.c file_wmf.c file_x3f.c file_xcf.c file_xm.c file_xsv.c file_veg.c file_wks.c file_zip.c memmem.h geometry.c list.c list.h ole.h ntfs_dir.c ntfs_dir.h ntfsp.c ntfsp.h ntfs_inc.h sessionp.c sessionp.h $(base_C) $(base_H) $(fs_C) $(fs_H) $(ICON_PHOTOREC) +photorec_SOURCES = photorec.c photorec.h phcfg.c phcfg.h phrecn.c phrecn.h dir.c dir.h ext2grp.c ext2grp.h ext2p.c ext2p.h ext2_dir.c ext2_dir.h ext2_inc.h fat_dir.c fat_dir.h fatp.c fatp.h filegen.c filegen.h file_7z.c file_a.c file_ab.c file_ace.c file_aif.c file_all.c file_als.c file_amd.c file_amr.c file_arj.c file_asf.c file_asm.c file_au.c file_bkf.c file_bld.c file_bmp.c file_bz2.c file_cab.c file_cam.c file_chm.c file_cm.c file_crw.c file_ctg.c file_cwk.c file_dat.c file_dbf.c file_dim.c file_dir.c file_djv.c file_doc.c file_dpx.c file_drw.c file_ds2.c file_dsc.c file_dss.c file_dta.c file_dump.c file_dv.c file_dwg.c file_elf.c file_emf.c file_evt.c file_exe.c pe.h file_ext.c file_fcp.c file_fcs.c file_fh10.c file_fh5.c file_fits.c file_flac.c file_flv.c file_frm.c file_fs.c file_gho.c file_gif.c file_gpg.c file_gz.c file_ifo.c file_imb.c file_indd.c file_iso.c file_itu.c file_jpg.c file_jpg.h file_kdb.c file_lnk.c file_m2ts.c file_max.c file_mb.c file_mcd.c file_mdb.c file_mdf.c file_mfg.c file_mid.c file_mkv.c file_mov.c file_mp3.c file_mpg.c file_mrw.c file_mus.c file_mysql.c file_njx.c file_ogg.c file_one.c file_orf.c file_paf.c file_pap.c file_pct.c file_pcx.c file_pdf.c file_pfx.c file_png.c file_prc.c file_prt.c file_ps.c file_psd.c file_psp.c file_pst.c file_ptb.c file_qbb.c file_qdf.c file_qxd.c file_ra.c file_raf.c file_rar.c file_raw.c file_rdc.c file_reg.c file_res.c file_riff.c file_rm.c file_rns.c file_rpm.c file_sit.c file_skp.c file_spe.c file_spss.c file_sql.c file_stl.c file_stu.c file_swf.c file_tar.c file_tar.h file_tib.c file_tiff.c file_tph.c file_txt.c file_vmdk.c file_wpd.c file_wmf.c file_x3f.c file_xcf.c file_xm.c file_xsv.c file_veg.c file_wks.c file_zip.c memmem.h geometry.c list.c list.h ole.h ntfs_dir.c ntfs_dir.h ntfsp.c ntfsp.h ntfs_inc.h sessionp.c sessionp.h $(base_C) $(base_H) $(fs_C) $(fs_H) $(ICON_PHOTOREC) #diskcp_SOURCES = diskcp.c types.h diff --git a/src/ext2grp.c b/src/ext2grp.c new file mode 100644 index 00000000..c3daa785 --- /dev/null +++ b/src/ext2grp.c @@ -0,0 +1,117 @@ +/* + + File: ext2grp.c + + Copyright (C) 2008 Christophe GRENIER <[email protected]> + + This software is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write the Free Software Foundation, Inc., 51 + Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif +#include <stdio.h> +#ifdef HAVE_STDLIB_H +#include <stdlib.h> +#endif +#ifdef HAVE_STRING_H +#include <string.h> +#endif +#include "types.h" +#include "common.h" +#include "list.h" +#include "filegen.h" +#include "intrf.h" +#include "intrfn.h" +#include "dir.h" +#include "ext2grp.h" +#include "ext2.h" +#include "log.h" +#include "phrecn.h" + +unsigned int ext2_fix_group(alloc_data_t *list_search_space, disk_t *disk, partition_t *partition) +{ + struct td_list_head *search_walker = NULL; + unsigned char *buffer; + unsigned int blocksize; + if(partition->upart_type!=UP_EXT2 && + partition->upart_type!=UP_EXT3) + { + log_error("Not a valid ext2/ext3 filesystem"); + display_message("Not a valid ext2/ext3 filesystem"); + free_search_space(list_search_space); + return 0; + } + + buffer=(unsigned char*)MALLOC(EXT2_SUPERBLOCK_SIZE); + if(disk->read(disk,EXT2_SUPERBLOCK_SIZE, buffer, partition->part_offset+0x400)!=0) + { + free(buffer); + return 0; + } + { + const struct ext2_super_block *sb=(const struct ext2_super_block *)buffer; + const unsigned int mult=(unsigned int)le32(sb->s_blocks_per_group) * (EXT2_MIN_BLOCK_SIZE<<le32(sb->s_log_block_size)); + td_list_for_each(search_walker, &list_search_space->list) + { + alloc_data_t *current_search_space; + current_search_space=td_list_entry(search_walker, alloc_data_t, list); + log_info("ext2_group: %llu\n", (long long unsigned)current_search_space->start); + current_search_space->start=current_search_space->start*mult + (sb->s_log_block_size==0?1024:0); + current_search_space->end=current_search_space->end*mult+mult-1 + (sb->s_log_block_size==0?1024:0); + } + blocksize=EXT2_MIN_BLOCK_SIZE<<le32(sb->s_log_block_size); + } + free(buffer); + return blocksize; +} + +unsigned int ext2_fix_inode(alloc_data_t *list_search_space, disk_t *disk, partition_t *partition) +{ + struct td_list_head *search_walker = NULL; + unsigned char *buffer; + unsigned int blocksize; + if(partition->upart_type!=UP_EXT2 && + partition->upart_type!=UP_EXT3) + { + log_error("Not a valid ext2/ext3 filesystem"); + display_message("Not a valid ext2/ext3 filesystem"); + free_search_space(list_search_space); + return 0; + } + + buffer=(unsigned char*)MALLOC(EXT2_SUPERBLOCK_SIZE); + if(disk->read(disk,EXT2_SUPERBLOCK_SIZE, buffer, partition->part_offset+0x400)!=0) + { + free(buffer); + return 0; + } + { + const struct ext2_super_block *sb=(const struct ext2_super_block *)buffer; + const unsigned int divd=(unsigned int)le32(sb->s_inodes_per_group); + const unsigned int mult=(unsigned int)le32(sb->s_blocks_per_group) * (EXT2_MIN_BLOCK_SIZE<<le32(sb->s_log_block_size)); + td_list_for_each(search_walker, &list_search_space->list) + { + alloc_data_t *current_search_space; + current_search_space=td_list_entry(search_walker, alloc_data_t, list); + log_info("ext2_inode: %llu\n", (long long unsigned)current_search_space->start); + current_search_space->start=current_search_space->start/divd*mult + (sb->s_log_block_size==0?1024:0); + current_search_space->end=current_search_space->end/divd*mult+mult-1 + (sb->s_log_block_size==0?1024:0); + } + blocksize=EXT2_MIN_BLOCK_SIZE<<le32(sb->s_log_block_size); + } + free(buffer); + return blocksize; +} diff --git a/src/ext2grp.h b/src/ext2grp.h new file mode 100644 index 00000000..803a08fc --- /dev/null +++ b/src/ext2grp.h @@ -0,0 +1,23 @@ +/* + + File: ext2grp.h + + Copyright (C) 2008 Christophe GRENIER <[email protected]> + + This software is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write the Free Software Foundation, Inc., 51 + Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + */ +unsigned int ext2_fix_group(alloc_data_t *list_search_space, disk_t *disk, partition_t *partition); +unsigned int ext2_fix_inode(alloc_data_t *list_search_space, disk_t *disk, partition_t *partition); @@ -347,4 +347,23 @@ static inline void td_list_add_sorted(struct td_list_head *newe, struct td_list_ } td_list_add_tail(newe, head); } + +static inline int td_list_add_sorted_uniq(struct td_list_head *newe, struct td_list_head *head, + int (*compar)(const struct td_list_head *a, const struct td_list_head *b)) +{ + struct td_list_head *pos; + td_list_for_each(pos, head) + { + const int res=compar(newe,pos); + if(res<0) + { + __td_list_add(newe, pos->prev, pos); + return 0; + } + else if(res==0) + return 1; + } + td_list_add_tail(newe, head); + return 0; +} #endif diff --git a/src/phrecn.c b/src/phrecn.c index 6ac49adb..01b4ab0f 100644 --- a/src/phrecn.c +++ b/src/phrecn.c @@ -63,6 +63,7 @@ #include "hdaccess.h" #include "file_tar.h" #include "phcfg.h" +#include "ext2grp.h" /* #define DEBUG */ /* #define DEBUG_GET_NEXT_SECTOR */ @@ -662,7 +663,7 @@ static int photorec_bf_aux(disk_t *disk_car, partition_t *partition, const int p log_critical("Brute Force : Cannot create file %s: %s\n", file_recovery->filename, strerror(errno)); return 2; } - block_buffer=(unsigned char *) malloc(sizeof(unsigned char)*blocksize); + block_buffer=(unsigned char *) MALLOC(sizeof(unsigned char)*blocksize); current_search_space=start_search_space; /* We have offset==start_search_space->start==file_recovery->location.start */ @@ -1478,6 +1479,19 @@ static int photorec(disk_t *disk_car, partition_t *partition, const int verbose, return 0; } +static int spacerange_cmp(const struct td_list_head *a, const struct td_list_head *b) +{ + const alloc_data_t *space_a=td_list_entry(a, const alloc_data_t, list); + const alloc_data_t *space_b=td_list_entry(b, const alloc_data_t, list); + if(space_a->start < space_b->start) + return -1; + if(space_a->start > space_b->start) + return 1; + return space_a->end - space_b->end; +} + +enum { INIT_SPACE_WHOLE, INIT_SPACE_PREINIT, INIT_SPACE_EXT2_GROUP, INIT_SPACE_EXT2_INODE }; + static void menu_photorec(disk_t *disk_car, const int verbose, const char *recup_dir, file_enable_t *file_enable, char **current_cmd, alloc_data_t*list_search_space) { int insert_error=0; @@ -1565,6 +1579,14 @@ static void menu_photorec(disk_t *disk_car, const int verbose, const char *recup if(res!=NULL) { partition_t *partition=current_element->part; + if(mode_init_space==INIT_SPACE_EXT2_GROUP) + { + blocksize=ext2_fix_group(list_search_space, disk_car, partition); + } + else if(mode_init_space==INIT_SPACE_EXT2_INODE) + { + blocksize=ext2_fix_inode(list_search_space, disk_car, partition); + } if(td_list_empty(&list_search_space->list)) { init_search_space(list_search_space, disk_car, partition); @@ -1618,6 +1640,50 @@ static void menu_photorec(disk_t *disk_car, const int verbose, const char *recup (*current_cmd)+=9; carve_free_space_only=1; } + else if(strncmp(*current_cmd,"ext2_group,",11)==0) + { + unsigned int groupnr; + (*current_cmd)+=11; + mode_ext2=1; + groupnr=atoi(*current_cmd); + while(*current_cmd[0]!=',' && *current_cmd[0]!='\0') + (*current_cmd)++; + if(mode_init_space==INIT_SPACE_WHOLE) + mode_init_space=INIT_SPACE_EXT2_GROUP; + if(mode_init_space==INIT_SPACE_EXT2_GROUP) + { + alloc_data_t *new_free_space; + new_free_space=(alloc_data_t*)MALLOC(sizeof(*new_free_space)); + /* Temporary storage, values need to be multiplied by group size and aligned */ + new_free_space->start=groupnr; + new_free_space->end=groupnr; + new_free_space->file_stat=NULL; + if(td_list_add_sorted_uniq(&new_free_space->list, &list_search_space->list, spacerange_cmp)) + free(new_free_space); + } + } + else if(strncmp(*current_cmd,"ext2_inode,",11)==0) + { + unsigned int inodenr; + (*current_cmd)+=11; + mode_ext2=1; + inodenr=atoi(*current_cmd); + while(*current_cmd[0]!=',' && *current_cmd[0]!='\0') + (*current_cmd)++; + if(mode_init_space==INIT_SPACE_WHOLE) + mode_init_space=INIT_SPACE_EXT2_INODE; + if(mode_init_space==INIT_SPACE_EXT2_INODE) + { + alloc_data_t *new_free_space; + new_free_space=(alloc_data_t*)MALLOC(sizeof(*new_free_space)); + /* Temporary storage, values need to be multiplied by group size and aligned */ + new_free_space->start=inodenr; + new_free_space->end=inodenr; + new_free_space->file_stat=NULL; + if(td_list_add_sorted_uniq(&new_free_space->list, &list_search_space->list, spacerange_cmp)) + free(new_free_space); + } + } else if(isdigit(*current_cmd[0])) { unsigned int order; |