diff --git a/src/Makefile.am b/src/Makefile.am index f9365220..e54f6024 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -488,20 +488,20 @@ session_%.framac: file_%.c common.c filegen.c log.c frama-c $^ -cpp-extra-args="-DMAIN_$* -DHAVE_CONFIG_H -DSINGLE_FORMAT -DSINGLE_FORMAT_$* -D__x86_64__ -I.." $(FRAMA_C_FLAGS) -save $@ session_fidentify-%.framac: $(fidentify_C_SOURCES) - $(CC) $(CFLAGS) -DMAIN_fidentify -DHAVE_CONFIG_H -DSINGLE_FORMAT -DSINGLE_FORMAT_$* -I.. $^ -o fidentify_$* - frama-c $^ -cpp-extra-args="-DMAIN_fidentify -DHAVE_CONFIG_H -DSINGLE_FORMAT -DSINGLE_FORMAT_$* -D__x86_64__ -I.." $(FRAMA_C_FLAGS) -save $@ + $(CC) $(CFLAGS) -DDISABLED_FOR_FRAMAC -DHAVE_CONFIG_H -DSINGLE_FORMAT -DSINGLE_FORMAT_$* -I.. $^ -o fidentify_$* + frama-c $^ -cpp-extra-args="-DDISABLED_FOR_FRAMAC -DHAVE_CONFIG_H -DSINGLE_FORMAT -DSINGLE_FORMAT_$* -D__x86_64__ -I.." $(FRAMA_C_FLAGS) -save $@ session_fidentify.framac: $(fidentify_C_SOURCES) - $(CC) $(CFLAGS) -DMAIN_fidentify -DHAVE_CONFIG_H -I.. $^ -o demo_fidentify - frama-c $^ -cpp-extra-args="-DMAIN_fidentify -DHAVE_CONFIG_H -D__x86_64__ -I.." $(FRAMA_C_FLAGS) -save $@ + $(CC) $(CFLAGS) -DDISABLED_FOR_FRAMAC -DHAVE_CONFIG_H -I.. $^ -o demo_fidentify + frama-c $^ -cpp-extra-args="-DDISABLED_FOR_FRAMAC -DHAVE_CONFIG_H -D__x86_64__ -I.." $(FRAMA_C_FLAGS) -save $@ session_photorec-%.framac: $(photorec_C_SOURCES) - $(CC) $(CFLAGS) -DMAIN_photorec -DHAVE_CONFIG_H -DSINGLE_FORMAT -DSINGLE_FORMAT_$* -DSINGLE_PARTITION_TYPE -I.. $^ -o photorec_$* - frama-c $^ -cpp-extra-args="-DMAIN_photorec -DHAVE_CONFIG_H -DSINGLE_FORMAT -DSINGLE_FORMAT_$* -DSINGLE_PARTITION_TYPE -D__x86_64__ -I.." $(FRAMA_C_FLAGS) -save $@ + $(CC) $(CFLAGS) -DDISABLED_FOR_FRAMAC -DHAVE_CONFIG_H -DSINGLE_FORMAT -DSINGLE_FORMAT_$* -DSINGLE_PARTITION_TYPE -I.. $^ -o photorec_$* + frama-c $^ -cpp-extra-args="-DDISABLED_FOR_FRAMAC -DHAVE_CONFIG_H -DSINGLE_FORMAT -DSINGLE_FORMAT_$* -DSINGLE_PARTITION_TYPE -D__x86_64__ -I.." $(FRAMA_C_FLAGS) -save $@ session_photorec.framac: $(photorec_C_SOURCES) - $(CC) $(CFLAGS) -DMAIN_photorec -DHAVE_CONFIG_H -I.. $^ -o demo_photorec - frama-c $^ -cpp-extra-args="-DMAIN_photorec -DHAVE_CONFIG_H -D__x86_64__ -I.." $(FRAMA_C_FLAGS) -save $@ + $(CC) $(CFLAGS) -DDISABLED_FOR_FRAMAC -DHAVE_CONFIG_H -I.. $^ -o demo_photorec + frama-c $^ -cpp-extra-args="-DDISABLED_FOR_FRAMAC -DHAVE_CONFIG_H -D__x86_64__ -I.." $(FRAMA_C_FLAGS) -save $@ moc_qphotorec.cpp: qphotorec.h $(AM_V_GEN) QT_SELECT=$(QT_SELECT) $(MOC) $< -o $@ diff --git a/src/addpartn.c b/src/addpartn.c index 2eed0d4d..a964e0cd 100644 --- a/src/addpartn.c +++ b/src/addpartn.c @@ -24,7 +24,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/analyse.c b/src/analyse.c index 303b3788..c0ae398c 100644 --- a/src/analyse.c +++ b/src/analyse.c @@ -32,7 +32,7 @@ #include "analyse.h" #include "fat.h" #include "exfat.h" -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) #include "apfs.h" #include "bfs.h" #include "bsd.h" @@ -73,7 +73,7 @@ int search_NTFS_backup(unsigned char *buffer, disk_t *disk, partition_t *partiti return -1; { const struct ntfs_boot_sector *ntfs_header=(const struct ntfs_boot_sector*)buffer; -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) /* NTFS recovery using backup sector */ if(le16(ntfs_header->marker)==0xAA55 && recover_NTFS(disk, ntfs_header, partition, verbose, dump_ind, 1)==0) @@ -87,7 +87,7 @@ int search_HFS_backup(unsigned char *buffer, disk_t *disk, partition_t *partitio { if(disk->pread(disk, buffer, 0x400, partition->part_offset)!= 0x400) return -1; -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) { const hfs_mdb_t *hfs_mdb=(const hfs_mdb_t *)buffer; const struct hfsp_vh *vh=(const struct hfsp_vh *)buffer; @@ -142,7 +142,7 @@ int search_FAT_backup(unsigned char *buffer, disk_t *disk, partition_t *partitio int search_type_0(const unsigned char *buffer, disk_t *disk, partition_t *partition, const int verbose, const int dump_ind) { -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) /* Expect a buffer filled with 8k to handle the SWAP detection */ const pv_disk_t *pv=(const pv_disk_t *)buffer; const struct cramfs_super *cramfs=(const struct cramfs_super *)buffer; @@ -237,7 +237,7 @@ int search_type_0(const unsigned char *buffer, disk_t *disk, partition_t *partit int search_type_1(const unsigned char *buffer, const disk_t *disk, partition_t *partition, const int verbose, const int dump_ind) { -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) const struct disklabel *bsd_header=(const struct disklabel *)(buffer+0x200); const struct disk_super_block *beos_block=(const struct disk_super_block*)(buffer+0x200); const struct cramfs_super *cramfs=(const struct cramfs_super *)(buffer+0x200); @@ -278,7 +278,7 @@ int search_type_1(const unsigned char *buffer, const disk_t *disk, partition_t * int search_type_2(const unsigned char *buffer, disk_t *disk, partition_t *partition, const int verbose, const int dump_ind) { -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) const hfs_mdb_t *hfs_mdb=(const hfs_mdb_t *)(buffer+0x400); const struct hfsp_vh *vh=(const struct hfsp_vh *)(buffer+0x400); const struct ext2_super_block *sb=(const struct ext2_super_block*)(buffer+0x400); @@ -317,7 +317,7 @@ int search_type_8(unsigned char *buffer, disk_t *disk,partition_t *partition,con } if(disk->pread(disk, buffer, 4096, partition->part_offset + 4096) != 4096) return -1; -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) { /* MD 1.2 */ const struct mdp_superblock_1 *sb1=(const struct mdp_superblock_1 *)buffer; if(le32(sb1->major_version)==1 && @@ -341,7 +341,7 @@ int search_type_16(unsigned char *buffer, disk_t *disk,partition_t *partition,co /* 8k offset */ if(disk->pread(disk, buffer, 3 * DEFAULT_SECTOR_SIZE, partition->part_offset + 16 * 512) != 3 * DEFAULT_SECTOR_SIZE) return -1; -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) { const struct ufs_super_block *ufs=(const struct ufs_super_block *)buffer; const struct vdev_boot_header *zfs=(const struct vdev_boot_header*)buffer; @@ -368,7 +368,7 @@ int search_type_64(unsigned char *buffer, disk_t *disk,partition_t *partition,co /* 32k offset */ if(disk->pread(disk, buffer, 3 * DEFAULT_SECTOR_SIZE, partition->part_offset + 63 * 512) != 3 * DEFAULT_SECTOR_SIZE) return -1; -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) { const struct jfs_superblock* jfs=(const struct jfs_superblock*)(buffer+0x200); /* Test JFS */ @@ -389,7 +389,7 @@ int search_type_128(unsigned char *buffer, disk_t *disk, partition_t *partition, } if(disk->pread(disk, buffer, 11 * DEFAULT_SECTOR_SIZE, partition->part_offset + 126 * 512) != 11 * DEFAULT_SECTOR_SIZE) return -1; -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) { const unsigned char *buffer_1024=buffer+0x400; const struct reiserfs_super_block *rfs=(const struct reiserfs_super_block *)buffer_1024; @@ -428,7 +428,7 @@ int search_type_2048(unsigned char *buffer, disk_t *disk, partition_t *partition } if(disk->pread(disk, buffer, 2*DEFAULT_SECTOR_SIZE, partition->part_offset + 2048 * 512) != 2*DEFAULT_SECTOR_SIZE) return -1; -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) { const struct vmfs_volume *sb_vmfs=(const struct vmfs_volume *)buffer; if(le32(sb_vmfs->magic)==0xc001d00d && @@ -441,7 +441,7 @@ int search_type_2048(unsigned char *buffer, disk_t *disk, partition_t *partition int check_linux(disk_t *disk, partition_t *partition, const int verbose) { -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) if(check_JFS(disk, partition)==0 || check_rfs(disk, partition, verbose)==0 || check_EXT2(disk, partition, verbose)==0 || diff --git a/src/askloc.c b/src/askloc.c index 978bc322..3ecc5a08 100644 --- a/src/askloc.c +++ b/src/askloc.c @@ -23,7 +23,7 @@ #include #endif -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC #undef HAVE_GETCWD #endif diff --git a/src/askloc.h b/src/askloc.h index 7b40aaab..c831c927 100644 --- a/src/askloc.h +++ b/src/askloc.h @@ -26,7 +26,7 @@ extern "C" { #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/chgarch.c b/src/chgarch.c index 93596923..d391b76b 100644 --- a/src/chgarch.c +++ b/src/chgarch.c @@ -38,33 +38,76 @@ #include "hdaccess.h" #include "chgarch.h" -extern const arch_fnct_t arch_i386; -extern const arch_fnct_t arch_gpt; -extern const arch_fnct_t arch_humax; -extern const arch_fnct_t arch_mac; extern const arch_fnct_t arch_none; +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_GPT) +extern const arch_fnct_t arch_gpt; +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_HUMAX) +extern const arch_fnct_t arch_humax; +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_I386) +extern const arch_fnct_t arch_i386; +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_MAC) +extern const arch_fnct_t arch_mac; +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_SUN) extern const arch_fnct_t arch_sun; +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_XBOX) extern const arch_fnct_t arch_xbox; - +#endif /* return 1 if user need to give the partition table type */ int change_arch_type_cli(disk_t *disk, const int verbose, char**current_cmd) { - const arch_fnct_t *arch_list[]={&arch_i386, &arch_gpt, &arch_humax, &arch_mac, &arch_none, &arch_sun, &arch_xbox, NULL}; +#ifndef DISABLED_FOR_FRAMAC + const arch_fnct_t *arch_list[]={ +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_I386) + &arch_i386, +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_GPT) + &arch_gpt, +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_HUMAX) + &arch_humax, +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_MAC) + &arch_mac, +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_NONE) + &arch_none, +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_SUN) + &arch_sun, +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_XBOX) + &arch_xbox, +#endif + NULL}; int keep_asking; if(*current_cmd==NULL) return 1; + /*@ assert valid_read_string(*current_cmd); */ + /*@ + loop invariant valid_read_string(*current_cmd); + */ do { int i; keep_asking=0; skip_comma_in_command(current_cmd); - for(i=0;arch_list[i]!=NULL;i++) + /*@ + loop unroll 10; + @*/ + for(i=0; arch_list[i]!=NULL; i++) + { if(check_command(current_cmd, arch_list[i]->part_name_option, strlen(arch_list[i]->part_name_option))==0) { disk->arch=arch_list[i]; keep_asking=1; } + } if(check_command(current_cmd, "ask_type", 8)==0) { return 1; @@ -74,5 +117,7 @@ int change_arch_type_cli(disk_t *disk, const int verbose, char**current_cmd) hd_update_geometry(disk, verbose); log_info("%s\n",disk->description_short(disk)); log_info("Partition table type: %s\n", disk->arch->part_name); +#endif + /*@ assert valid_disk(disk); */ return 0; } diff --git a/src/chgarchn.c b/src/chgarchn.c index b5d72a0b..5e7d1183 100644 --- a/src/chgarchn.c +++ b/src/chgarchn.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif @@ -44,11 +44,11 @@ #include "autoset.h" #include "chgarchn.h" +extern const arch_fnct_t arch_none; extern const arch_fnct_t arch_i386; extern const arch_fnct_t arch_gpt; extern const arch_fnct_t arch_humax; extern const arch_fnct_t arch_mac; -extern const arch_fnct_t arch_none; extern const arch_fnct_t arch_sun; extern const arch_fnct_t arch_xbox; diff --git a/src/chgtype.c b/src/chgtype.c index 243faa34..dbe12fb4 100644 --- a/src/chgtype.c +++ b/src/chgtype.c @@ -34,14 +34,27 @@ #include "log.h" #include "log_part.h" +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_GPT) extern const arch_fnct_t arch_gpt; extern const arch_fnct_t arch_none; +#endif +/*@ + @ requires \valid(current_cmd); + @ requires valid_read_string(*current_cmd); + @ ensures valid_read_string(*current_cmd); + @*/ +// TODO assigns *current_cmd; static int get_hex_from_command(char **current_cmd) { const int tmp=strtol(*current_cmd, NULL, 16); + /*@ + @ loop invariant valid_read_string(*current_cmd); + @ loop assigns *current_cmd; + @*/ while(*current_cmd[0]!=',' && *current_cmd[0]!='\0') (*current_cmd)++; + /*@ assert valid_read_string(*current_cmd); */ return tmp; } @@ -53,6 +66,7 @@ void change_part_type_cli(const disk_t *disk_car,partition_t *partition, char ** return ; if(partition->arch==NULL) return; +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_GPT) if(partition->arch==&arch_gpt) { partition->arch=&arch_none; @@ -61,11 +75,14 @@ void change_part_type_cli(const disk_t *disk_car,partition_t *partition, char ** const int tmp_val=get_hex_from_command(current_cmd); partition->arch->set_part_type(partition,tmp_val); } +#ifndef DISABLED_FOR_FRAMAC log_info("Change partition type:\n"); log_partition(disk_car,partition); +#endif partition->arch=&arch_gpt; return; } +#endif if(partition->arch->set_part_type==NULL) return ; skip_comma_in_command(current_cmd); @@ -73,7 +90,9 @@ void change_part_type_cli(const disk_t *disk_car,partition_t *partition, char ** const int tmp_val=get_hex_from_command(current_cmd); partition->arch->set_part_type(partition,tmp_val); } +#ifndef DISABLED_FOR_FRAMAC log_info("Change partition type:\n"); log_partition(disk_car,partition); +#endif return ; } diff --git a/src/chgtypen.c b/src/chgtypen.c index 153dae0c..a6b7e094 100644 --- a/src/chgtypen.c +++ b/src/chgtypen.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/common.c b/src/common.c index 59b412f5..23572aa7 100644 --- a/src/common.c +++ b/src/common.c @@ -25,7 +25,7 @@ #include #endif -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC #undef HAVE_POSIX_MEMALIGN #undef HAVE_MEMALIGN #undef HAVE_NCURSES @@ -84,13 +84,23 @@ void *MALLOC(size_t size) } } #endif +#ifdef DISABLED_FOR_FRAMAC + if((res=calloc(1,size))==NULL) + { + exit(EXIT_FAILURE); + } +#else if((res=malloc(size))==NULL) { +#ifndef DISABLED_FOR_FRAMAC log_critical("\nCan't allocate %lu bytes of memory.\n", (long unsigned)size); log_close(); +#endif exit(EXIT_FAILURE); } memset(res,0,size); +#endif + /*@ assert \valid((char *)res + (0 .. size - 1)); */ return res; } @@ -171,7 +181,7 @@ char * strcasestr (const char *haystack, const char *needle) } #endif -#if ! defined(HAVE_LOCALTIME_R) && ! defined(__MINGW32__) && !defined(__FRAMAC__) +#if ! defined(HAVE_LOCALTIME_R) && ! defined(__MINGW32__) && !defined(DISABLED_FOR_FRAMAC) struct tm *localtime_r(const time_t *timep, struct tm *result) { return localtime(timep); @@ -179,6 +189,7 @@ struct tm *localtime_r(const time_t *timep, struct tm *result) #endif /*@ + @ decreases number; @ assigns \nothing; @*/ static unsigned int up2power_aux(const unsigned int number) @@ -312,7 +323,7 @@ time_t date_dos2unix(const unsigned short f_time, const unsigned short f_date) void set_secwest(void) { const time_t t = time(NULL); -#if defined(__MINGW32__) || defined(__FRAMAC__) +#if defined(__MINGW32__) || defined(DISABLED_FOR_FRAMAC) const struct tm *tmptr = localtime(&t); #else struct tm tmp; @@ -359,14 +370,21 @@ int check_command(char **current_cmd, const char *cmd, const size_t n) const int res=strncmp(*current_cmd, cmd, n); if(res==0) { -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC + const char *src=*current_cmd; unsigned int i; /*@ - @ loop invariant valid_read_string(*current_cmd); - @ loop assigns i, *current_cmd; + @ loop invariant valid_read_string(src); + @ loop assigns i, src; @*/ - for(i=0; i #endif @@ -344,8 +344,15 @@ struct list_part_struct list_part_t *next; int to_be_removed; }; + /*@ - predicate valid_list_part(list_part_t *list) = ((list == \null) || (\valid_read(list) && valid_list_part(list->next))); +inductive valid_list_part{L} (list_part_t *list) +{ + case list_null{L}: + valid_list_part(\null); + case list_not_null{L}: + \forall list_part_t *list; \valid_read(list) ==> valid_list_part(list->next) ==> valid_list_part(list); +} @*/ typedef struct list_disk_struct list_disk_t; @@ -441,7 +448,7 @@ struct param_disk_struct }; /*@ - predicate valid_disk(disk_t *disk) = ((disk == \null) || + predicate valid_disk(disk_t *disk) = (\valid_read(disk) && \freeable(disk) && valid_read_string(disk->device) && @@ -457,13 +464,18 @@ struct param_disk_struct (disk->wbuffer == \null || (\freeable(disk->wbuffer) && disk->wbuffer_size > 0)) && valid_arch(disk->arch) && disk->sector_size > 0 - )); + ); */ -/*@ predicate valid_list_disk(list_disk_t* root) = ((root == \null) || (\valid_read(root))); */ -/* TODO predicate valid_list_disk{L}(list_disk_t* root) = - \forall list_disk_t *node; \valid(node) && ld_reachable(root,node) ==> \valid(node->disk); - */ +/*@ +inductive valid_list_disk{L} (list_disk_t *list) +{ + case list_null{L}: + valid_list_disk(\null); + case list_not_null{L}: + \forall list_disk_t *list; \valid_read(list) ==> valid_disk(list->disk) && valid_list_disk(list->next) ==> valid_list_disk(list); +} + @*/ struct partition_struct @@ -605,7 +617,7 @@ int strncasecmp(const char * s1, const char * s2, size_t len); #ifndef HAVE_STRCASESTR char * strcasestr (const char *haystack, const char *needle); #endif -#if ! defined(HAVE_LOCALTIME_R) && ! defined(__MINGW32__) && !defined(__FRAMAC__) +#if ! defined(HAVE_LOCALTIME_R) && ! defined(__MINGW32__) && !defined(DISABLED_FOR_FRAMAC) /*@ @ requires valid_timer: \valid_read(timep); @ requires \valid(result); diff --git a/src/dfxml.c b/src/dfxml.c index 88fbef61..38035d11 100644 --- a/src/dfxml.c +++ b/src/dfxml.c @@ -24,7 +24,7 @@ #include #endif -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBEWF #undef HAVE_SYS_UTSNAME_H #undef ENABLE_DFXML @@ -206,13 +206,11 @@ void xml_add_DFXML_creator(const char *package, const char *version) #ifdef RECORD_COMPILATION_DATE xml_out2s("compilation_date", get_compilation_date()); #endif -#ifndef MAIN_photorec xml_printf("\n", td_ext2fs_version()); xml_printf("\n", td_ewf_version()); xml_printf("\n", td_jpeg_version()); xml_printf("\n", td_ntfs_version()); xml_printf("\n", td_zlib_version()); -#endif xml_pop("build_environment"); xml_push("execution_environment",""); #if defined(__CYGWIN__) || defined(__MINGW32__) @@ -247,11 +245,11 @@ void xml_add_DFXML_creator(const char *package, const char *version) #ifdef HAVE_GETEUID xml_out2i("uid", geteuid()); #endif -#if !defined(__FRAMAC__) +#if !defined(DISABLED_FOR_FRAMAC) { char outstr[200]; const time_t t = time(NULL); -#if defined(__MINGW32__) || defined(__FRAMAC__) +#if defined(__MINGW32__) || defined(DISABLED_FOR_FRAMAC) const struct tm *tmp = localtime(&t); #else struct tm tm_tmp; diff --git a/src/dimage.c b/src/dimage.c index 6e651187..b3c18c99 100644 --- a/src/dimage.c +++ b/src/dimage.c @@ -126,7 +126,7 @@ int disk_image(disk_t *disk, const partition_t *partition, const char *image_dd) free(buffer); return -1; } -#if !defined(__FRAMAC__) +#if !defined(DISABLED_FOR_FRAMAC) if(fstat(disk_dst, &stat_buf)==0) { int res=1; diff --git a/src/dir.c b/src/dir.c index 2a2e3090..b29a1209 100644 --- a/src/dir.c +++ b/src/dir.c @@ -23,7 +23,7 @@ #include #endif -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC #undef HAVE_CHMOD #endif @@ -115,6 +115,7 @@ static char ftypelet (unsigned int bits) void mode_string (const unsigned int mode, char *str) { +#ifndef DISABLED_FOR_FRAMAC str[0] = ftypelet(mode); str[1] = (mode & LINUX_S_IRUSR) ? 'r' : '-'; str[2] = (mode & LINUX_S_IWUSR) ? 'w' : '-'; @@ -156,6 +157,7 @@ void mode_string (const unsigned int mode, char *str) str[9] = 't'; } #endif +#endif } int set_datestr(char *datestr, size_t n, const time_t timev) @@ -169,7 +171,7 @@ int set_datestr(char *datestr, size_t n, const time_t timev) strncpy(datestr, " ", n); return 0; } -#if defined(__MINGW32__) || defined(__FRAMAC__) +#if defined(__MINGW32__) || defined(DISABLED_FOR_FRAMAC) tm_p=localtime(&timev); #else tm_p=localtime_r(&timev, &tmp); @@ -196,7 +198,7 @@ int dir_aff_log(const dir_data_t *dir_data, const file_info_t *dir_list) { log_info("Directory %s\n",dir_data->current_directory); } -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC td_list_for_each(file_walker, &dir_list->list) { const file_info_t *current_file=td_list_entry_const(file_walker, const file_info_t, list); @@ -230,7 +232,7 @@ int dir_aff_log(const dir_data_t *dir_data, const file_info_t *dir_list) void log_list_file(const disk_t *disk, const partition_t *partition, const dir_data_t *dir_data, const file_info_t*list) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC struct td_list_head *tmp; log_partition(disk, partition); if(dir_data!=NULL) @@ -301,6 +303,7 @@ static int is_inode_valid(const file_info_t *current_file, const unsigned int di @ requires valid_partition(partition); @ requires \valid(dir_data); @ requires \separated(disk, partition, dir_data); + @ decreases 0; @*/ static int dir_whole_partition_log_aux(disk_t *disk, const partition_t *partition, dir_data_t *dir_data, const unsigned long int inode) { @@ -353,6 +356,7 @@ int dir_whole_partition_log(disk_t *disk, const partition_t *partition, dir_data @ requires \valid(copy_ok); @ requires \valid(copy_bad); @ requires \separated(disk, partition, dir_data, copy_ok, copy_bad); + @ decreases 0; @*/ static int dir_whole_partition_copy_aux(disk_t *disk, const partition_t *partition, dir_data_t *dir_data, const unsigned long int inode, unsigned int *copy_ok, unsigned int *copy_bad) { diff --git a/src/dirn.c b/src/dirn.c index 654d14e0..1964ec58 100644 --- a/src/dirn.c +++ b/src/dirn.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/ewf.c b/src/ewf.c index e7c63912..886fa4cf 100644 --- a/src/ewf.c +++ b/src/ewf.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBEWF #endif diff --git a/src/ewf.h b/src/ewf.h index 105080a3..d90c3d93 100644 --- a/src/ewf.h +++ b/src/ewf.h @@ -25,7 +25,7 @@ extern "C" { #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBEWF #endif diff --git a/src/ext2_dir.c b/src/ext2_dir.c index efc19a30..1cca7c23 100644 --- a/src/ext2_dir.c +++ b/src/ext2_dir.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBEXT2FS #endif diff --git a/src/ext2_inc.h b/src/ext2_inc.h index d1cb0860..58d4de60 100644 --- a/src/ext2_inc.h +++ b/src/ext2_inc.h @@ -22,7 +22,7 @@ #ifndef _EXT2_INC_H #define _EXT2_INC_H -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBEXT2FS #endif diff --git a/src/ext2p.c b/src/ext2p.c index 2e57e434..eb2c8046 100644 --- a/src/ext2p.c +++ b/src/ext2p.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBEXT2FS #endif diff --git a/src/fat.c b/src/fat.c index 6199faeb..672a9d61 100644 --- a/src/fat.c +++ b/src/fat.c @@ -46,11 +46,12 @@ #include "dir.h" #include "fat_dir.h" +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_I386) extern const arch_fnct_t arch_i386; +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_MAC) extern const arch_fnct_t arch_mac; - -static int log_fat_info(const struct fat_boot_sector*fh1, const upart_type_t upart_type, const unsigned int sector_size); -static int test_OS2MB(const disk_t *disk, const struct fat_boot_sector *fat_header, const partition_t *partition, const int verbose, const int dump_ind); +#endif /*@ @ requires \valid_read(partition); @@ -77,6 +78,7 @@ static int is_fat32(const partition_t *partition); @ requires valid_partition(partition); @ requires \valid_read(fat_header); @ requires \separated(disk_car, partition, fat_header); + @ decreases 0; @*/ static int fat32_set_part_name(disk_t *disk_car, partition_t *partition, const struct fat_boot_sector*fat_header) { @@ -303,6 +305,7 @@ int check_FAT(disk_t *disk_car, partition_t *partition, const int verbose) @ requires \valid_read(partition); @ requires valid_partition(partition); @ requires \separated(disk, partition); + @ decreases 0; @*/ static unsigned int get_next_cluster_fat12(disk_t *disk, const partition_t *partition, const int offset, const unsigned int cluster) { @@ -333,6 +336,7 @@ static unsigned int get_next_cluster_fat12(disk_t *disk, const partition_t *part @ requires \valid_read(partition); @ requires valid_partition(partition); @ requires \separated(disk, partition); + @ decreases 0; @*/ static unsigned int get_next_cluster_fat16(disk_t *disk, const partition_t *partition, const int offset, const unsigned int cluster) { @@ -361,6 +365,7 @@ static unsigned int get_next_cluster_fat16(disk_t *disk, const partition_t *part @ requires \valid_read(partition); @ requires valid_partition(partition); @ requires \separated(disk, partition); + @ decreases 0; @*/ static unsigned int get_next_cluster_fat32(disk_t *disk, const partition_t *partition, const int offset, const unsigned int cluster) { @@ -862,6 +867,7 @@ unsigned long int fat32_get_next_free(const unsigned char *boot_fat32, const uns @ requires \valid_read(partition); @ requires valid_partition(partition); @ requires \separated(disk, partition); + @ decreases 0; @*/ static int fat_has_EFI_entry(disk_t *disk, const partition_t *partition, const int verbose) { @@ -977,7 +983,7 @@ static int test_OS2MB(const disk_t *disk, const struct fat_boot_sector *fat_head const char*buffer=(const char*)fat_header; if(le16(fat_header->marker)==0xAA55 && memcmp(buffer+FAT_NAME1,"FAT ",8)==0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(verbose||dump_ind) { log_info("OS2MB at %u/%u/%u\n", @@ -1043,6 +1049,7 @@ int is_part_fat(const partition_t *partition) int is_part_fat12(const partition_t *partition) { +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_I386) if(partition->arch==&arch_i386) { switch(partition->part_type_i386) @@ -1054,13 +1061,7 @@ int is_part_fat12(const partition_t *partition) break; } } - /* - else if(partition->arch==&arch_gpt) - { - if(guid_cmp(partition->part_type_gpt,GPT_ENT_TYPE_MS_BASIC_DATA)==0) - return 1; - } - */ +#endif return 0; } @@ -1071,6 +1072,7 @@ static int is_fat12(const partition_t *partition) int is_part_fat16(const partition_t *partition) { +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_I386) if(partition->arch==&arch_i386) { switch(partition->part_type_i386) @@ -1086,13 +1088,7 @@ int is_part_fat16(const partition_t *partition) break; } } - /* - else if(partition->arch==&arch_gpt) - { - if(guid_cmp(partition->part_type_gpt,GPT_ENT_TYPE_MS_BASIC_DATA)==0) - return 1; - } - */ +#endif return 0; } @@ -1103,6 +1099,7 @@ static int is_fat16(const partition_t *partition) int is_part_fat32(const partition_t *partition) { +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_I386) if(partition->arch==&arch_i386) { switch(partition->part_type_i386) @@ -1116,18 +1113,14 @@ int is_part_fat32(const partition_t *partition) break; } } - else if(partition->arch==&arch_mac) +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_MAC) + if(partition->arch==&arch_mac) { if(partition->part_type_mac==PMAC_FAT32) return 1; } - /* - else if(partition->arch==&arch_gpt) - { - if(guid_cmp(partition->part_type_gpt,GPT_ENT_TYPE_MS_BASIC_DATA)==0) - return 1; - } - */ +#endif return 0; } diff --git a/src/fat_dir.c b/src/fat_dir.c index 6c88956d..ca801ef7 100644 --- a/src/fat_dir.c +++ b/src/fat_dir.c @@ -64,6 +64,7 @@ struct fat_dir_struct @ requires \valid_read(fat_header); @ requires \valid(dir_list); @ requires \separated(disk_car, partition, dir_data, fat_header, dir_list); + @ decreases 0; @*/ static int fat1x_rootdir(disk_t *disk_car, const partition_t *partition, const dir_data_t *dir_data, const struct fat_boot_sector*fat_header, file_info_t *dir_list); @@ -109,6 +110,7 @@ int dir_fat_aux(const unsigned char*buffer, const unsigned int size, const unsig if(wctomb(NULL, 0) < 0) utf8=0; #endif +#ifndef DISABLED_FOR_FRAMAC GetNew: status=0; long_slots = 0; @@ -325,6 +327,7 @@ RecEnd: if((const void *)de<(const void *)(buffer+size-1) && de->name[0] != (int8_t) 0) goto GetNew; +#endif return 0; } @@ -351,6 +354,7 @@ static int is_EOC(const unsigned int cluster, const upart_type_t upart_type) @ requires \valid_read(dir_data); @ requires \valid(dir_list); @ requires \separated(disk_car, partition, dir_data, dir_list); + @ decreases 0; @*/ static int fat_dir(disk_t *disk_car, const partition_t *partition, dir_data_t *dir_data, const unsigned long int first_cluster, file_info_t *dir_list) { @@ -529,6 +533,7 @@ static void dir_partition_fat_close(dir_data_t *dir_data) @ requires \valid(dir_data); @ requires \valid_read(file); @ requires \separated(disk_car, partition, dir_data, file); + @ decreases 0; @*/ static int fat_copy(disk_t *disk_car, const partition_t *partition, dir_data_t *dir_data, const file_info_t *file) { diff --git a/src/fat_unformat.c b/src/fat_unformat.c index 885ecdaa..180a99c4 100644 --- a/src/fat_unformat.c +++ b/src/fat_unformat.c @@ -55,6 +55,7 @@ #include "fat_common.h" #include +#ifndef DISABLED_FOR_FRAMAC extern int need_to_stop; #define READ_SIZE 4*1024*1024 @@ -440,3 +441,4 @@ pstatus_t fat_unformat(struct ph_param *params, const struct ph_options *options /* start_data is relative to the disk */ return fat_unformat_aux(params, options, start_data, list_search_space); } +#endif diff --git a/src/fat_unformat.h b/src/fat_unformat.h index 9f37d97d..2ef8058b 100644 --- a/src/fat_unformat.h +++ b/src/fat_unformat.h @@ -25,7 +25,16 @@ extern "C" { #endif +#ifndef DISABLED_FOR_FRAMAC +/*@ + @ requires \valid(params); + @ requires \valid_read(options); + @ requires valid_list_search_space(list_search_space); + @ requires \separated(params, options, list_search_space); + @ ensures valid_list_search_space(list_search_space); + @*/ pstatus_t fat_unformat(struct ph_param *params, const struct ph_options *options, alloc_data_t *list_search_space); +#endif #ifdef __cplusplus } /* closing brace for extern "c" */ diff --git a/src/fatn.c b/src/fatn.c index 6fe96ce3..18ec067f 100644 --- a/src/fatn.c +++ b/src/fatn.c @@ -24,7 +24,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/fatx.h b/src/fatx.h index a6c46ac9..7c2718b7 100644 --- a/src/fatx.h +++ b/src/fatx.h @@ -39,6 +39,7 @@ struct disk_fatx @ requires valid_disk(disk_car); @ requires \valid(partition); @ requires \separated(disk_car, partition); + @ decreases 0; @*/ int check_FATX(disk_t *disk_car, partition_t *partition); diff --git a/src/fidentify.c b/src/fidentify.c index 1b3d4dfb..e1238183 100644 --- a/src/fidentify.c +++ b/src/fidentify.c @@ -24,7 +24,7 @@ #include #endif -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC #undef HAVE_FTELLO #endif @@ -66,6 +66,110 @@ extern file_check_list_t file_check_list; #define OPT_CHECK 1 #define OPT_TIME 2 +/*@ + @ requires \valid_function(file_recovery->data_check); + @ requires valid_data_check_param(buffer, buffer_size, file_recovery); + @ decreases 0; + @ ensures valid_file_recovery(file_recovery); + @ ensures valid_data_check_result(\result, file_recovery); + @ assigns file_recovery->calculated_file_size, file_recovery->data_check_tmp; + @ assigns file_recovery->data_check, file_recovery->file_check, file_recovery->offset_error, file_recovery->offset_ok, file_recovery->time, file_recovery->data_check_tmp; + @*/ +static data_check_t data_check_wrapper(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery) +{ + data_check_t tmp; + //@ split file_recovery->data_check; + tmp=file_recovery->data_check(buffer, buffer_size, file_recovery); + /*@ assert valid_data_check_result(tmp, file_recovery); */ + return tmp; +} + +/*@ + @ requires \valid(file_recovery); + @ requires valid_file_recovery(file_recovery); + @ requires \valid_function(file_recovery->data_check); + @ requires 0 < blocksize <= READ_SIZE; + @ requires READ_SIZE % blocksize == 0; + @ requires \valid(buffer_start + (0 .. blocksize + READ_SIZE -1)); + @ requires \separated(file_recovery, &errno, buffer_start + (..)); + @ decreases 0; + @ ensures valid_file_recovery(file_recovery); + @ assigns *file_recovery->handle, errno; + @ assigns buffer_start[0 .. blocksize + READ_SIZE -1]; + @ assigns file_recovery->file_size; + @ assigns file_recovery->calculated_file_size, file_recovery->data_check_tmp; + @ assigns file_recovery->data_check, file_recovery->file_check, file_recovery->offset_error, file_recovery->offset_ok, file_recovery->time, file_recovery->data_check_tmp; + @*/ +static data_check_t data_check_aux(file_recovery_t *file_recovery, const unsigned int blocksize, char *buffer_start) +{ + /*@ ghost const unsigned int buffer_size=blocksize + READ_SIZE; */ + /*@ + @ loop invariant valid_file_recovery(file_recovery); + @ loop invariant file_recovery == \at(file_recovery, Pre); + @ loop invariant file_recovery->calculated_file_size < PHOTOREC_MAX_FILE_SIZE; + @ loop invariant \separated(file_recovery, &errno, buffer_start + (..)); + @ loop assigns *file_recovery->handle, errno; + @ loop assigns buffer_start[0 .. blocksize + READ_SIZE -1]; + @ loop assigns file_recovery->file_size; + @ loop assigns file_recovery->calculated_file_size, file_recovery->data_check_tmp; + @ loop assigns file_recovery->data_check, file_recovery->file_check, file_recovery->offset_error, file_recovery->offset_ok, file_recovery->time, file_recovery->data_check_tmp; + @*/ + while(1) + { + char *buffer=buffer_start+blocksize; + unsigned int i; + size_t lu=0; + memset(buffer, 0, READ_SIZE); + lu=fread(buffer, 1, READ_SIZE, file_recovery->handle); + if(lu <= 0) + { + /*@ assert valid_file_recovery(file_recovery); */ + return DC_STOP; + } + /*@ assert 0 < lu <= READ_SIZE; */ + /*@ + @ loop invariant valid_file_recovery(file_recovery); + @ loop invariant file_recovery == \at(file_recovery, Pre); + @ loop invariant \valid_read(buffer_start + (0 .. blocksize + READ_SIZE - 1)); + @ loop invariant file_recovery->calculated_file_size < PHOTOREC_MAX_FILE_SIZE; + @ loop invariant \valid_function(file_recovery->data_check); + @ loop invariant \separated(file_recovery, &errno, buffer_start + (..)); + @ loop assigns i, file_recovery->file_size; + @ loop assigns file_recovery->calculated_file_size, file_recovery->data_check_tmp; + @ loop assigns file_recovery->data_check, file_recovery->file_check, file_recovery->offset_error, file_recovery->offset_ok, file_recovery->time, file_recovery->data_check_tmp; + @*/ + for(i=0; ifile_size+=blocksize; + if(res != DC_CONTINUE || file_recovery->data_check==NULL) + { + /*@ assert valid_file_recovery(file_recovery); */ + return res; + } + if( file_recovery->calculated_file_size >= PHOTOREC_MAX_FILE_SIZE || + file_recovery->file_size >= PHOTOREC_MAX_FILE_SIZE) + { + /*@ assert valid_file_recovery(file_recovery); */ + return DC_STOP; + } + /*@ assert file_recovery->calculated_file_size < PHOTOREC_MAX_FILE_SIZE; */ + } + if(lu != READ_SIZE) + { + /*@ assert valid_file_recovery(file_recovery); */ + return DC_STOP; + } + memcpy(buffer_start, &buffer_start[READ_SIZE], blocksize); + /*@ assert valid_file_recovery(file_recovery); */ + } + /*@ assert valid_file_recovery(file_recovery); */ +} + /*@ @ requires \valid(file_recovery); @ requires valid_file_recovery(file_recovery); @@ -77,70 +181,31 @@ extern file_check_list_t file_check_list; @*/ static data_check_t data_check(file_recovery_t *file_recovery, const unsigned int blocksize) { - unsigned char *buffer_start; + char *buffer_start; const unsigned int buffer_size=blocksize + READ_SIZE; + data_check_t res; if( file_recovery->calculated_file_size >= PHOTOREC_MAX_FILE_SIZE ) - return DC_STOP; - if(my_fseek(file_recovery->handle, 0, SEEK_SET) < 0) - return DC_STOP; - buffer_start=(unsigned char *)MALLOC(buffer_size); - /*@ - @ loop invariant valid_file_recovery(file_recovery); - @ loop invariant file_recovery == \at(file_recovery, Pre); - @ loop invariant file_recovery->calculated_file_size < PHOTOREC_MAX_FILE_SIZE; - @*/ - while(1) { - unsigned char *buffer=buffer_start+blocksize; - unsigned int i; - size_t lu; - memset(buffer, 0, READ_SIZE); - lu=fread(buffer, 1, READ_SIZE, file_recovery->handle); - if(lu <= 0) - { - free(buffer_start); - return DC_STOP; - } - /*@ assert 0 < lu <= READ_SIZE; */ - /*@ - @ loop invariant valid_file_recovery(file_recovery); - @ loop invariant file_recovery == \at(file_recovery, Pre); - @ loop invariant \valid_read(buffer_start + (0 .. blocksize + READ_SIZE - 1)); - @ loop invariant file_recovery->calculated_file_size < PHOTOREC_MAX_FILE_SIZE; - @ loop invariant \valid_function(file_recovery->data_check); - @*/ - for(i=0; idata_check(&buffer_start[i], 2*blocksize, file_recovery); - /*@ assert \valid_read(&buffer_start[i] + (0 .. 2*blocksize-1)); */ - file_recovery->file_size+=blocksize; - if(res != DC_CONTINUE || file_recovery->data_check==NULL) - { - free(buffer_start); - return res; - } - if( file_recovery->calculated_file_size >= PHOTOREC_MAX_FILE_SIZE || - file_recovery->file_size >= PHOTOREC_MAX_FILE_SIZE) - { - free(buffer_start); - return DC_STOP; - } - /*@ assert file_recovery->calculated_file_size < PHOTOREC_MAX_FILE_SIZE; */ - } - if(lu != READ_SIZE) - { - free(buffer_start); - return DC_STOP; - } - memcpy(buffer_start, &buffer_start[READ_SIZE], blocksize); + /*@ assert valid_file_recovery(file_recovery); */ + return DC_STOP; } + if(my_fseek(file_recovery->handle, 0, SEEK_SET) < 0) + { + /*@ assert valid_file_recovery(file_recovery); */ + return DC_STOP; + } + buffer_start=(char *)MALLOC(buffer_size); + res=data_check_aux(file_recovery, blocksize, buffer_start); + /*@ assert valid_file_recovery(file_recovery); */ + free(buffer_start); + /*@ assert valid_file_recovery(file_recovery); */ + return res; } /*@ @ requires valid_read_string(filename); @ requires \separated(filename + (..), &errno, &Frama_C_entropy_source, stdout); + @ decreases 0; @*/ static int file_identify(const char *filename, const unsigned int options) { @@ -194,8 +259,9 @@ static int file_identify(const char *filename, const unsigned int options) /*TODO assert tmp!=tmp_list; */ const file_check_t *file_check=td_list_entry_const(tmp, const file_check_t, list); /*@ assert \valid_read(file_check); */ + /* TODO assert valid_file_check_node(file_check); */ if( -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC file_check->header_check!=NULL && #endif (file_check->length==0 || memcmp(buffer + file_check->offset, file_check->value, file_check->length)==0) && @@ -213,7 +279,7 @@ static int file_identify(const char *filename, const unsigned int options) if(file_recovery_new.file_stat!=NULL && file_recovery_new.file_stat->file_hint!=NULL && (file_recovery_new.file_check!=NULL || file_recovery_new.data_check!=NULL) && -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC file_recovery_new.extension != NULL && #endif ((options&OPT_CHECK)!=0 || ((options&OPT_TIME)!=0 && file_recovery_new.time==0)) @@ -269,14 +335,14 @@ static int file_identify(const char *filename, const unsigned int options) if((options&OPT_CHECK)!=0 && (file_recovery_new.file_check!=NULL || file_recovery_new.data_check!=NULL)) printf(" file_size=%llu", (long long unsigned)file_recovery_new.file_size); if((options&OPT_TIME)!=0 && file_recovery_new.time!=0 && file_recovery_new.time!=(time_t)-1) -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC { printf(" time=%lld", (long long)file_recovery_new.time); } #else { char outstr[200]; -#if defined(__MINGW32__) || defined(__FRAMAC__) +#if defined(__MINGW32__) || defined(DISABLED_FOR_FRAMAC) const struct tm *tmp = localtime(&file_recovery_new.time); #else struct tm tm_tmp; @@ -306,7 +372,7 @@ static int file_identify(const char *filename, const unsigned int options) return 0; } -#if !defined(__AFL_COMPILER) && !defined(MAIN_fidentify) +#if !defined(__AFL_COMPILER) && !defined(DISABLED_FOR_FRAMAC) static void file_identify_dir(const char *current_dir, const unsigned int options) { DIR *dir; @@ -359,7 +425,7 @@ static void display_version(void) #ifdef RECORD_COMPILATION_DATE printf("Compilation date: %s\n", get_compilation_date()); #endif -#ifndef MAIN_fidentify +#ifndef DISABLED_FOR_FRAMAC printf("libjpeg: %s, zlib: %s\n", td_jpeg_version(), td_zlib_version()); printf("OS: %s\n" , get_os()); #endif @@ -368,7 +434,7 @@ static void display_version(void) int main(int argc, char **argv) { int i; -#ifdef MAIN_fidentify +#ifdef DISABLED_FOR_FRAMAC unsigned int options=OPT_CHECK|OPT_TIME; #else unsigned int options=0; @@ -378,7 +444,7 @@ int main(int argc, char **argv) int scan_dir=1; file_stat_t *file_stats; log_set_levels(LOG_LEVEL_DEBUG|LOG_LEVEL_TRACE|LOG_LEVEL_QUIET|LOG_LEVEL_INFO|LOG_LEVEL_VERBOSE|LOG_LEVEL_PROGRESS|LOG_LEVEL_WARNING|LOG_LEVEL_ERROR|LOG_LEVEL_PERROR|LOG_LEVEL_CRITICAL); -#ifndef MAIN_fidentify +#ifndef DISABLED_FOR_FRAMAC for(i=1; i\nhttps://www.cgsecurity.org\n", VERSION, TESTDISKDATE); log_flush(); #endif -#ifndef MAIN_fidentify +#ifndef DISABLED_FOR_FRAMAC for(i=1; ienable=1; } file_stats=init_file_stats(array_file_enable); -#ifndef MAIN_fidentify +#ifndef DISABLED_FOR_FRAMAC for(i=1; iID, "BB02", 4)!=0 || block_size<0x18) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_error("file_bac.c: invalid block at %llu\n", (long long unsigned)file_recovery->calculated_file_size); #endif diff --git a/src/file_bdm.c b/src/file_bdm.c index daf3e01d..2390f2e7 100644 --- a/src/file_bdm.c +++ b/src/file_bdm.c @@ -59,7 +59,7 @@ static int header_check_bdm(const unsigned char *buffer, const unsigned int buff static void register_header_check_bdm(file_stat_t *file_stat) { register_header_check(0, "INDX0100", 8, &header_check_bdm, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, "MOBJ0100", 8, &header_check_bdm, file_stat); #endif } diff --git a/src/file_berkeley.c b/src/file_berkeley.c index 9bb5ae4e..99944a1a 100644 --- a/src/file_berkeley.c +++ b/src/file_berkeley.c @@ -76,7 +76,7 @@ static void register_header_check_berkeley_le(file_stat_t *file_stat) static unsigned char berkeley_db_btree_8[8]={0x62, 0x31, 0x05, 0x00, 0x08, 0x00, 0x00, 0x00}; static unsigned char berkeley_db_btree_9[8]={0x62, 0x31, 0x05, 0x00, 0x09, 0x00, 0x00, 0x00}; register_header_check(0xC, berkeley_db_hash_8, 8, &header_check_berkeley_le, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0xC, berkeley_db_hash_9, 8, &header_check_berkeley_le, file_stat); register_header_check(0xC, berkeley_db_btree_8, 8, &header_check_berkeley_le, file_stat); register_header_check(0xC, berkeley_db_btree_9, 8, &header_check_berkeley_le, file_stat); diff --git a/src/file_crw.c b/src/file_crw.c index 20d337e3..fede1629 100644 --- a/src/file_crw.c +++ b/src/file_crw.c @@ -81,7 +81,7 @@ static void register_header_check_crw(file_stat_t *file_stat) static const unsigned char crw_header_be[2]= {'I','I'}; static const unsigned char crw_header_le[2]= {'M','M'}; register_header_check(0, crw_header_be, sizeof(crw_header_be), &header_check_crw, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, crw_header_le, sizeof(crw_header_le), &header_check_crw, file_stat); #endif } diff --git a/src/file_dir.c b/src/file_dir.c index fb6ac6d9..387b4e0c 100644 --- a/src/file_dir.c +++ b/src/file_dir.c @@ -20,7 +20,7 @@ */ -#if !defined(SINGLE_FORMAT) || defined(SINGLE_FORMAT_dir) || defined(MAIN_photorec) +#if !defined(SINGLE_FORMAT) || defined(SINGLE_FORMAT_dir) #ifdef HAVE_CONFIG_H #include #endif @@ -69,7 +69,7 @@ static void file_rename_fatdir(file_recovery_t *file_recovery) /*@ assert buffer_size >= 32; */ cluster=fat_get_cluster_from_entry((const struct msdos_dir_entry *)&buffer[0]); sprintf(buffer_cluster, "cluster_%u", cluster); -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) buffer_cluster[sizeof(buffer_cluster)-1]='\0'; #endif file_rename(file_recovery, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1); diff --git a/src/file_doc.c b/src/file_doc.c index 0e7cb47c..8e442a35 100644 --- a/src/file_doc.c +++ b/src/file_doc.c @@ -142,7 +142,7 @@ static uint32_t *OLE_load_FAT(FILE *IN, const struct OLE_HDR *header, const uint /*@ assert uSectorShift == le16(header->uSectorShift); */ /*@ assert num_FAT_blocks==le32(header->num_FAT_blocks); */ /*@ assert num_FAT_blocks <= 109+le32(header->num_extra_FAT_blocks)*((1<= num_FAT_blocks<> uSectorShift); -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC dataPt=(char *)MALLOC(((1024*1024+(1<> uSectorShift) << uSectorShift); #else dataPt=(char *)MALLOC(i_max << uSectorShift); @@ -712,7 +712,7 @@ static uint32_t *OLE_load_MiniFAT(FILE *IN, const struct OLE_HDR *header, const return NULL; /*@ assert 0 < csectMiniFat; */ /*@ assert 0 < csectMiniFat <= 2048; */ -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC minifat=(char *)MALLOC(2048 << 12); #else minifat=(char *)MALLOC(minifat_length); @@ -1067,7 +1067,7 @@ static void OLE_parse_title_entry(const char *buffer, const unsigned int size, c /*@ assert \valid_read((char*)(src + offset_tmp)) && \valid_read(((char*)(src+offset_tmp))+(1..count-1)); */ /*@ assert valid_read_or_empty((void const *)(src + offset_tmp), count); */ /*@ assert valid_read_or_empty((void const *)(src + offset_tmp), count); */ -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(count < 1024) { memcpy(title, &src[offset_tmp], count); @@ -1300,7 +1300,7 @@ static void OLE_parse_summary_aux(const char *dataPt, const unsigned int dirLen, { unsigned int pos; const unsigned char *udataPt=(const unsigned char *)dataPt; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC assert(dirLen >= 48 && dirLen<=1024*1024); #endif /*@ assert *ext == \null || valid_read_string(*ext); */ @@ -1367,7 +1367,7 @@ static void *OLE_read_ministream(const unsigned char *ministream, unsigned char *dataPt; unsigned int mblock=miniblock_start; unsigned int size_read; -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC const unsigned int len_aligned=(1024*1024+(1< 1024) return ; /*@ assert 0 < count <= 1024; */ -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC rsrc_entries=(struct rsrc_entries_s *)MALLOC(1024 * sizeof(struct rsrc_entries_s)); #else rsrc_entries=(struct rsrc_entries_s *)MALLOC(count * sizeof(struct rsrc_entries_s)); @@ -489,7 +489,7 @@ static void pe_resource_id(FILE *file, const unsigned int base, const unsigned i if(count==0 || count > 1024) return ; /*@ assert 0 < count <= 1024; */ -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC rsrc_entries=(struct rsrc_entries_s *)MALLOC(1024 * sizeof(struct rsrc_entries_s)); #else rsrc_entries=(struct rsrc_entries_s *)MALLOC(count * sizeof(struct rsrc_entries_s)); @@ -559,7 +559,7 @@ static void pe_resource_type(FILE *file, const unsigned int base, const unsigned if(count==0 || count > 1024) return ; /*@ assert 0 < count <= 1024; */ -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC rsrc_entries=(struct rsrc_entries_s *)MALLOC(1024 * sizeof(struct rsrc_entries_s)); #else rsrc_entries=(struct rsrc_entries_s *)MALLOC(count * sizeof(struct rsrc_entries_s)); diff --git a/src/file_ext.c b/src/file_ext.c index e73df089..3b1837d1 100644 --- a/src/file_ext.c +++ b/src/file_ext.c @@ -72,7 +72,7 @@ static void file_rename_ext(file_recovery_t *file_recovery) /*@ assert \initialized(buffer + (0 .. sizeof(buffer)-1)); */ block_nr=(unsigned long int)le32(sb->s_first_data_block) + (unsigned long int)le16(sb->s_block_group_nr)*le32(sb->s_blocks_per_group); sprintf(buffer_cluster, "sb_%lu", block_nr); -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) buffer_cluster[sizeof(buffer_cluster)-1]='\0'; #endif file_rename(file_recovery, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1); @@ -136,7 +136,7 @@ static void file_rename_extdir(file_recovery_t *file_recovery) #endif /*@ assert \initialized(buffer + (0 .. sizeof(buffer)-1)); */ sprintf(buffer_cluster, "inode_%u", (unsigned int)le32(*inode)); -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) buffer_cluster[sizeof(buffer_cluster)-1]='\0'; #endif file_rename(file_recovery, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1); diff --git a/src/file_flac.c b/src/file_flac.c index 2abef69a..0ed44d11 100644 --- a/src/file_flac.c +++ b/src/file_flac.c @@ -111,7 +111,7 @@ static void register_header_check_flac(file_stat_t *file_stat) static const unsigned char flac_header[5]= {'f', 'L', 'a', 'C', 0x00}; static const unsigned char flac_header2[5]= {'f', 'L', 'a', 'C', 0x80}; register_header_check(0, flac_header,sizeof(flac_header), &header_check_flac, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, flac_header2,sizeof(flac_header2), &header_check_flac, file_stat); #endif } diff --git a/src/file_fob.c b/src/file_fob.c index 99f2e8a6..d303e820 100644 --- a/src/file_fob.c +++ b/src/file_fob.c @@ -73,7 +73,7 @@ static int header_check_fob(const unsigned char *buffer, const unsigned int buff static void register_header_check_fob(file_stat_t *file_stat) { register_header_check(0, "Codeunit ", 9, &header_check_fob, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, "Dataport ", 9, &header_check_fob, file_stat); register_header_check(0, "Form ", 5, &header_check_fob, file_stat); register_header_check(0, "MenuSuite ", 10, &header_check_fob, file_stat); diff --git a/src/file_gif.c b/src/file_gif.c index 8ddda44d..088973a9 100644 --- a/src/file_gif.c +++ b/src/file_gif.c @@ -210,7 +210,7 @@ static void register_header_check_gif(file_stat_t *file_stat) static const unsigned char gif_header[6]= { 'G','I','F','8','7','a'}; static const unsigned char gif_header2[6]= { 'G','I','F','8','9','a'}; register_header_check(0, gif_header,sizeof(gif_header), &header_check_gif, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, gif_header2,sizeof(gif_header2), &header_check_gif, file_stat); #endif } diff --git a/src/file_gpg.c b/src/file_gpg.c index c983c2d3..f7f9087a 100644 --- a/src/file_gpg.c +++ b/src/file_gpg.c @@ -788,7 +788,7 @@ static void register_header_check_gpg(file_stat_t *file_stat) static const unsigned char gpg_header_seckey[1]= {0x95}; static const unsigned char gpg_header_pkey[1]= {0x99}; register_header_check(0, gpg_header_seckey, sizeof(gpg_header_seckey), &header_check_gpg, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, gpg_header_symkey_enc, sizeof(gpg_header_symkey_enc), &header_check_gpg, file_stat); register_header_check(0, gpg_header_pkey_enc, sizeof(gpg_header_pkey_enc), &header_check_gpg, file_stat); register_header_check(0, pgp_header, sizeof(pgp_header), &header_check_gpg, file_stat); diff --git a/src/file_gsm.c b/src/file_gsm.c index fa184f7a..a061895b 100644 --- a/src/file_gsm.c +++ b/src/file_gsm.c @@ -137,7 +137,7 @@ static void register_header_check_gsm(file_stat_t *file_stat) static const unsigned char gsm_header16[1]={ 0xdf }; register_header_check(0, gsm_header1, sizeof(gsm_header1), &header_check_gsm, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, gsm_header2, sizeof(gsm_header2), &header_check_gsm, file_stat); register_header_check(0, gsm_header3, sizeof(gsm_header3), &header_check_gsm, file_stat); register_header_check(0, gsm_header4, sizeof(gsm_header4), &header_check_gsm, file_stat); diff --git a/src/file_gz.c b/src/file_gz.c index ca51131d..e6db51de 100644 --- a/src/file_gz.c +++ b/src/file_gz.c @@ -24,7 +24,7 @@ #include #endif -#if defined(MAIN_fidentify) || defined(MAIN_photorec) || defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBZ #undef HAVE_ZLIB_H #endif diff --git a/src/file_hfsp.c b/src/file_hfsp.c index 049448b8..8514a5ec 100644 --- a/src/file_hfsp.c +++ b/src/file_hfsp.c @@ -65,7 +65,7 @@ static int header_check_hfsp(const unsigned char *buffer, const unsigned int buf static void register_header_check_hfsp(file_stat_t *file_stat) { register_header_check(0, "H+\0\4", 4, &header_check_hfsp, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, "HX\0\5", 4, &header_check_hfsp, file_stat); #endif } diff --git a/src/file_ibd.c b/src/file_ibd.c index 7bf32b03..87bb60bf 100644 --- a/src/file_ibd.c +++ b/src/file_ibd.c @@ -50,7 +50,7 @@ const file_hint_t file_hint_ibd= { #define FIL_PAGE_TYPE_FSP_HDR 8 /* File space header */ #define DICT_TF_BITS 6 /* number of flag bits */ #define DICT_TF_FORMAT_SHIFT 5 /* file format */ -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC #define DICT_TF_FORMAT_MASK 0x20 #else #define DICT_TF_FORMAT_MASK \ diff --git a/src/file_ico.c b/src/file_ico.c index 1ac1583c..6e9bc2c5 100644 --- a/src/file_ico.c +++ b/src/file_ico.c @@ -158,7 +158,7 @@ static int header_check_ico(const unsigned char *buffer, const unsigned int buff static void register_header_check_ico(file_stat_t *file_stat) { register_header_check(0, header_ico1, sizeof(header_ico1), &header_check_ico, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, header_ico2, sizeof(header_ico2), &header_check_ico, file_stat); register_header_check(0, header_ico3, sizeof(header_ico3), &header_check_ico, file_stat); register_header_check(0, header_ico4, sizeof(header_ico4), &header_check_ico, file_stat); diff --git a/src/file_ifo.c b/src/file_ifo.c index 0874895a..623d5796 100644 --- a/src/file_ifo.c +++ b/src/file_ifo.c @@ -74,7 +74,7 @@ static void register_header_check_ifo(file_stat_t *file_stat) static const unsigned char ifo_header_vmg[12]= { 'D', 'V', 'D', 'V', 'I', 'D', 'E', 'O', '-', 'V', 'M', 'G'}; static const unsigned char ifo_header_vts[12]= { 'D', 'V', 'D', 'V', 'I', 'D', 'E', 'O', '-', 'V', 'T', 'S'}; register_header_check(0, ifo_header_vmg, sizeof(ifo_header_vmg), &header_check_ifo, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, ifo_header_vts, sizeof(ifo_header_vts), &header_check_ifo, file_stat); #endif } diff --git a/src/file_jpg.c b/src/file_jpg.c index 21176051..3925a41b 100644 --- a/src/file_jpg.c +++ b/src/file_jpg.c @@ -25,7 +25,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(SINGLE_FORMAT) || defined(MAIN_fidentify) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBJPEG #undef DEBUG_JPEG #undef HAVE_JPEGLIB_H @@ -756,7 +756,7 @@ static void file_check_mpo(file_recovery_t *fr) } if(file_check_mpo_aux(fr->handle, buffer+8, offset+8, size-8) == 0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("file_check_mpo %s failed, limiting to first jpeg: %llu\n", fr->filename, (long long unsigned)jpg_fs); #endif fr->file_size=jpg_fs; @@ -925,7 +925,7 @@ static int header_check_jpg(const unsigned char *buffer, const unsigned int buff if( file_recovery->file_size <= 1024 && buffer[3]==0xec) /* APP12 */ { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("jpg %llu %llu\n", (long long unsigned)file_recovery->calculated_file_size, (long long unsigned)file_recovery->file_size); @@ -1986,7 +1986,7 @@ static void jpg_search_marker(file_recovery_t *file_recovery) ) { file_recovery->extra=tmp - offset_error; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(file_recovery->extra % file_recovery->blocksize != 0) { log_info("jpg_search_marker %s extra=%llu\n", @@ -2022,16 +2022,16 @@ static void jpg_save_thumbnail(const file_recovery_t *file_recovery, const char /*@ assert valid_read_string(&thumbname[0]); */ sep=strrchr(thumbname,'/'); if(sep!=NULL -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC && *(sep+1)=='f' #endif ) { FILE *out; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC *(sep+1)='t'; #endif -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if((out=fopen(thumbname,"wb"))!=NULL) { /*@ assert \valid_read(buffer + (0 .. nbytes - 1)); */ @@ -2047,7 +2047,7 @@ static void jpg_save_thumbnail(const file_recovery_t *file_recovery, const char /*@ assert \valid_read(thumb_char + (0 .. thumb_size - 1)); */ if(fwrite(&buffer[thumb_offset], thumb_size, 1, out) < 1) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_error("Can't write to %s: %s\n", thumbname, strerror(errno)); #endif } @@ -2057,7 +2057,7 @@ static void jpg_save_thumbnail(const file_recovery_t *file_recovery, const char } else { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_error("fopen %s failed\n", thumbname); #endif } @@ -2221,7 +2221,7 @@ static int jpg_check_app1(file_recovery_t *file_recovery, const unsigned int ext } else { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("%s thumb unknown marker 0x%02x at 0x%x\n", file_recovery->filename, buffer[j+1], j); #endif file_recovery->offset_error=j; @@ -2703,6 +2703,16 @@ static data_check_t data_check_jpg(const unsigned char *buffer, const unsigned i return DC_CONTINUE; } +/*@ + @ requires valid_register_header_check(file_stat); + @*/ +static void register_header_check_jpg(file_stat_t *file_stat) +{ + static const unsigned char jpg_header[3]= { 0xff,0xd8,0xff}; + register_header_check(0, jpg_header, sizeof(jpg_header), &header_check_jpg, file_stat); +} +#endif + const char*td_jpeg_version(void) { #if defined(HAVE_LIBJPEG) @@ -2724,17 +2734,6 @@ const char*td_jpeg_version(void) #endif } - -/*@ - @ requires valid_register_header_check(file_stat); - @*/ -static void register_header_check_jpg(file_stat_t *file_stat) -{ - static const unsigned char jpg_header[3]= { 0xff,0xd8,0xff}; - register_header_check(0, jpg_header, sizeof(jpg_header), &header_check_jpg, file_stat); -} -#endif - #if defined(MAIN_jpg) #define BLOCKSIZE 65536u int main() diff --git a/src/file_lzh.c b/src/file_lzh.c index 175b2d43..05e4f3af 100644 --- a/src/file_lzh.c +++ b/src/file_lzh.c @@ -57,7 +57,7 @@ struct lzh_level0 uint8_t level; uint8_t filename_len; /* Size should be 0, be carefull when using sizeof to decrement */ -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC uint8_t filename[0]; #endif } __attribute__ ((gcc_struct, __packed__)); @@ -73,7 +73,7 @@ struct lzh_level1 uint8_t reserved_20; uint8_t level; uint8_t filename_len; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC uint8_t filename[0]; #endif } __attribute__ ((gcc_struct, __packed__)); @@ -171,7 +171,7 @@ static int header_check_lzh(const unsigned char *buffer, const unsigned int buff static void register_header_check_lzh(file_stat_t *file_stat) { register_header_check(2, "-lh0-", 5, &header_check_lzh, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(2, "-lh1-", 5, &header_check_lzh, file_stat); register_header_check(2, "-lh2-", 5, &header_check_lzh, file_stat); register_header_check(2, "-lh3-", 5, &header_check_lzh, file_stat); diff --git a/src/file_m2ts.c b/src/file_m2ts.c index 99d1b235..dd31730a 100644 --- a/src/file_m2ts.c +++ b/src/file_m2ts.c @@ -105,7 +105,7 @@ static void file_rename_ts_188(file_recovery_t *file_recovery) #endif pid=((buffer[1]<<8)|buffer[2])&0x1fff; sprintf(buffer_pid, "pid_%u", pid); -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) buffer_pid[sizeof(buffer_pid)-1]='\0'; #endif file_rename(file_recovery, (const unsigned char*)buffer_pid, strlen(buffer_pid), 0, NULL, 1); @@ -136,7 +136,7 @@ static void file_rename_ts_192(file_recovery_t *file_recovery) #endif pid=((buffer[5]<<8)|buffer[6])&0x1fff; sprintf(buffer_pid, "pid_%u", pid); -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) buffer_pid[sizeof(buffer_pid)-1]='\0'; #endif file_rename(file_recovery, (const unsigned char*)buffer_pid, strlen(buffer_pid), 0, NULL, 1); diff --git a/src/file_mft.c b/src/file_mft.c index e4d82a25..bd82a413 100644 --- a/src/file_mft.c +++ b/src/file_mft.c @@ -67,7 +67,7 @@ static void file_rename_mft(file_recovery_t *file_recovery) #endif /*@ assert \initialized(buffer + (0 .. sizeof(buffer)-1)); */ sprintf(buffer_cluster, "record_%u", (unsigned int)le32(record->mft_record_number)); -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) buffer_cluster[sizeof(buffer_cluster)-1]='\0'; #endif file_rename(file_recovery, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1); diff --git a/src/file_mig.c b/src/file_mig.c index 1930788e..296316e3 100644 --- a/src/file_mig.c +++ b/src/file_mig.c @@ -55,7 +55,7 @@ struct MIG_HDR uint32_t unk1; uint32_t unk2; uint32_t unk3; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC unsigned char fn[0]; #endif } __attribute__ ((gcc_struct, __packed__)); diff --git a/src/file_mlv.c b/src/file_mlv.c index 3a166e5e..d891ae51 100644 --- a/src/file_mlv.c +++ b/src/file_mlv.c @@ -175,7 +175,7 @@ static void file_rename_mlv(file_recovery_t *file_recovery) } fclose(file); sprintf(ext, "M%02u", le16(hdr->fileNum)); -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) ext[sizeof(ext)-1]='\0'; #endif /*@ assert valid_read_string(ext_ptr); */ diff --git a/src/file_mov.c b/src/file_mov.c index 8568fc92..d255e547 100644 --- a/src/file_mov.c +++ b/src/file_mov.c @@ -198,7 +198,7 @@ static data_check_t data_check_mov(const unsigned char *buffer, const unsigned i } else { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(!(buffer[i+4]==0 && buffer[i+5]==0 && buffer[i+6]==0 && buffer[i+7]==0)) log_warning("file_mov.c: unknown atom 0x%02x%02x%02x%02x at %llu\n", buffer[i+4],buffer[i+5],buffer[i+6],buffer[i+7], @@ -523,7 +523,7 @@ static void register_header_check_mov_mdat(file_stat_t *file_stat) static void register_header_check_mov(file_stat_t *file_stat) { register_header_check(4, (const unsigned char*)"cmov",4, &header_check_mov, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(4, (const unsigned char*)"cmvd",4, &header_check_mov, file_stat); register_header_check(4, (const unsigned char*)"dcom",4, &header_check_mov, file_stat); register_header_check(4, (const unsigned char*)"free",4, &header_check_mov, file_stat); diff --git a/src/file_msa.c b/src/file_msa.c index 1f0c9a1c..9494ce83 100644 --- a/src/file_msa.c +++ b/src/file_msa.c @@ -85,7 +85,7 @@ static void register_header_check_msa(file_stat_t *file_stat) 0xfc, 0xff, 0xff, 0xff }; register_header_check(0, msa_header_fb, sizeof(msa_header_fb), &header_check_msa, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, msa_header_fc, sizeof(msa_header_fc), &header_check_msa, file_stat); #endif } diff --git a/src/file_mxf.c b/src/file_mxf.c index 360a6ad7..16e23fd2 100644 --- a/src/file_mxf.c +++ b/src/file_mxf.c @@ -64,7 +64,7 @@ struct partition_pack_next uint64_t body_offset; uint32_t body_SID; char op_pattern[16]; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC char essence_container[0]; #endif } __attribute__ ((gcc_struct, __packed__)); diff --git a/src/file_pcap.c b/src/file_pcap.c index 504a9c2c..ca7f4892 100644 --- a/src/file_pcap.c +++ b/src/file_pcap.c @@ -65,7 +65,7 @@ static void register_header_check_pcap(file_stat_t *file_stat) static const unsigned char pcap_le_header1[6] = {0xd4, 0xc3, 0xb2, 0xa1, 0x01, 0x00}; static const unsigned char pcap_le_header2[6] = {0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00}; register_header_check(0, pcap_le_header1, sizeof(pcap_le_header1), &header_check_pcap, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, pcap_le_header2, sizeof(pcap_le_header2), &header_check_pcap, file_stat); #endif } diff --git a/src/file_pyc.c b/src/file_pyc.c index b907c202..1f6be68a 100644 --- a/src/file_pyc.c +++ b/src/file_pyc.c @@ -88,7 +88,7 @@ static void register_header_check_pyc(file_stat_t *file_stat) static const unsigned char pyc_33_magic[4]= { 0x9e, 0x0c, '\r', '\n'}; static const unsigned char pyc_34_magic[4]= { 0xee, 0x0c, '\r', '\n'}; register_header_check(0, pyc_15_magic, sizeof(pyc_15_magic), &header_check_pyc, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, pyc_20_magic, sizeof(pyc_20_magic), &header_check_pyc, file_stat); register_header_check(0, pyc_21_magic, sizeof(pyc_21_magic), &header_check_pyc, file_stat); register_header_check(0, pyc_22_magic, sizeof(pyc_22_magic), &header_check_pyc, file_stat); diff --git a/src/file_sig.c b/src/file_sig.c index 3c8ae487..1b685b1e 100644 --- a/src/file_sig.c +++ b/src/file_sig.c @@ -65,11 +65,23 @@ struct signature_s { struct td_list_head list; const char *extension; - unsigned char *sig; + const unsigned char *sig; unsigned int sig_size; unsigned int offset; }; +/*@ + predicate valid_signature(signature_t *sig) = (\valid_read(sig) && + valid_read_string(sig->extension) && + \initialized(&sig->offset) && + \initialized(&sig->sig_size) && + sig->offset <= PHOTOREC_MAX_SIG_OFFSET && + 0 < sig->sig_size <= PHOTOREC_MAX_SIG_SIZE && + sig->offset + sig->sig_size <= PHOTOREC_MAX_SIG_OFFSET && + \valid_read((const char *)sig->sig+(0..sig->sig_size-1)) + ); + @*/ + static signature_t signatures={ .list = TD_LIST_HEAD_INIT(signatures.list) }; @@ -82,6 +94,8 @@ int signature_cmp(const struct td_list_head *a, const struct td_list_head *b) const signature_t *sig_a=td_list_entry_const(a, const signature_t, list); const signature_t *sig_b=td_list_entry_const(b, const signature_t, list); int res; + /*@ assert valid_signature(sig_a); */ + /*@ assert valid_signature(sig_b); */ if(sig_a->sig_size==0 && sig_b->sig_size!=0) return -1; if(sig_a->sig_size!=0 && sig_b->sig_size==0) @@ -105,27 +119,64 @@ int signature_cmp(const struct td_list_head *a, const struct td_list_head *b) } } -static void signature_insert(const char *extension, unsigned int offset, unsigned char *sig, unsigned int sig_size) +/*@ + @ requires offset <= PHOTOREC_MAX_SIG_OFFSET; + @ requires 0 < sig_size <= PHOTOREC_MAX_SIG_SIZE; + @ requires offset + sig_size <= PHOTOREC_MAX_SIG_OFFSET; + @ requires \valid_read((const char *)sig + (0 .. sig_size-1)); + @ requires valid_read_string(ext); + @*/ +static void signature_insert(const char *ext, unsigned int offset, const void*sig, unsigned int sig_size) { - /* FIXME: small memory leak */ - signature_t *newsig=(signature_t*)MALLOC(sizeof(*newsig)); - newsig->extension=extension; - newsig->sig=sig; + /* FIXME: memory leak for newsig */ + signature_t *newsig; + /*@ assert \valid_read((const char *)sig+(0..sig_size-1)); */ + /*@ assert valid_read_string(ext); */ + newsig=(signature_t*)MALLOC(sizeof(*newsig)); + newsig->extension=ext; + newsig->sig=(const unsigned char *)sig; newsig->sig_size=sig_size; newsig->offset=offset; + /*@ assert newsig->sig_size == sig_size; */ + + /*@ assert \valid_read(newsig); */ + /*@ assert valid_read_string(newsig->extension); */ + /*@ assert \initialized(&newsig->offset); */ + /*@ assert \initialized(&newsig->sig_size); */ + /*@ assert newsig->offset <= PHOTOREC_MAX_SIG_OFFSET; */ + /*@ assert 0 < newsig->sig_size <= PHOTOREC_MAX_SIG_SIZE; */ + /*@ assert newsig->offset + newsig->sig_size <= PHOTOREC_MAX_SIG_OFFSET; */ + /*@ assert \valid_read((const char *)sig+(0..sig_size-1)); */ + /*@ assert \valid_read((const char *)sig+(0..newsig->sig_size-1)); */ + /*@ assert (const char *)newsig->sig ==(const char *)sig; */ + /*@ assert \valid_read((const char *)newsig->sig+(0..0)); */ + /*@ assert \valid_read((const char *)newsig->sig+(0..newsig->sig_size-1)); */ + + /*@ assert valid_signature(newsig); */ td_list_add_sorted(&newsig->list, &signatures.list, signature_cmp); } +/*@ + @ requires separation: \separated(&file_hint_sig, buffer+(..), file_recovery, file_recovery_new); + @ requires valid_header_check_param(buffer, buffer_size, safe_header_only, file_recovery, file_recovery_new); + @ ensures valid_header_check_result(\result, file_recovery_new); + @ assigns *file_recovery_new; + @*/ static int header_check_sig(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) { struct td_list_head *pos; + /*@ loop assigns pos; */ td_list_for_each(pos, &signatures.list) { const signature_t *sig = td_list_entry(pos, signature_t, list); + /*@ assert sig->offset + sig->sig_size <= buffer_size; */ + /*@ assert valid_read_string(sig->extension); */ + /*@ assert valid_signature(sig); */ if(memcmp(&buffer[sig->offset], sig->sig, sig->sig_size)==0) { reset_file_recovery(file_recovery_new); file_recovery_new->extension=sig->extension; + /*@ assert valid_file_recovery(file_recovery_new); */ return 1; } } @@ -164,13 +215,27 @@ static FILE *open_signature_file(void) if (home != NULL) { FILE*handle; - char *filename=(char*)MALLOC(strlen(home)+strlen(DOT_PHOTOREC_SIG)+1); + char *filename; + size_t len_home; + const size_t len_sig=strlen(DOT_PHOTOREC_SIG); + size_t fn_size=len_sig; +#ifndef DISABLED_FOR_FRAMAC + len_home=strlen(home); + fn_size+=len_home; +#endif + filename=(char*)MALLOC(fn_size + 1); +#ifndef DISABLED_FOR_FRAMAC strcpy(filename, home); +#else + filename[0]='\0'; +#endif strcat(filename, DOT_PHOTOREC_SIG); handle=fopen(filename,"rb"); if(handle!=NULL) { +#ifndef DISABLED_FOR_FRAMAC log_info("Open signature file %s\n", filename); +#endif free(filename); return handle; } @@ -182,241 +247,385 @@ static FILE *open_signature_file(void) FILE *handle=fopen(PHOTOREC_SIG,"rb"); if(handle!=NULL) { +#ifndef DISABLED_FOR_FRAMAC log_info("Open signature file %s\n", PHOTOREC_SIG); +#endif return handle; } } return NULL; } -static char *str_uint(char *src, unsigned int *resptr) +/*@ + @ requires \valid(ptr); + @ requires valid_read_string(*ptr); + @ ensures \initialized(ptr); + @ ensures valid_read_string(*ptr); + @ assigns *ptr; + @*/ +static unsigned int str_uint_hex(char **ptr) { + char *src=*ptr; unsigned int res=0; - if(*src=='0' && (*(src+1)=='x' || *(src+1)=='X')) + /*@ + @ loop invariant valid_read_string(src); + @ loop invariant res < 0x10000000; + @ loop assigns src, res; + @*/ + for(;;src++) { - for(src+=2;;src++) + const char c=*src; + if(c>='0' && c<='9') + res=res*16+(c-'0'); + else if(c>='A' && c<='F') + res=res*16+(c-'A'+10); + else if(c>='a' && c<='f') + res=res*16+(c-'a'+10); + else { - if(*src>='0' && *src<='9') - res=res*16+(*src)-'0'; - else if(*src>='A' && *src<='F') - res=res*16+(*src)-'A'+10; - else if(*src>='a' && *src<='f') - res=res*16+(*src)-'a'+10; - else - { - *resptr=res; - return src; - } + *ptr=src; + return res; + } + if(res >= 0x10000000) + { + *ptr=src; + return res; } - } - else - { - for(;*src>='0' && *src<='9';src++) - res=res*10+(*src)-'0'; - *resptr=res; - return src; } } +/*@ + @ requires \valid(ptr); + @ requires valid_read_string(*ptr); + @ ensures \initialized(ptr); + @ ensures valid_read_string(*ptr); + @ assigns *ptr; + @*/ +static unsigned int str_uint_dec(char **ptr) +{ + char *src=*ptr; + unsigned int res=0; + /*@ + @ loop invariant valid_read_string(src); + @ loop invariant res < 0x10000000; + @ loop assigns src, res; + @*/ + for(;*src>='0' && *src<='9';src++) + { + res=res*10+(*src)-'0'; + if(res >= 0x10000000) + { + *ptr=src; + return res; + } + } + *ptr=src; + return res; +} + +/*@ + @ requires \valid(ptr); + @ requires valid_read_string(*ptr); + @ ensures \initialized(ptr); + @ ensures valid_read_string(*ptr); + @ assigns *ptr; + @*/ +static unsigned int str_uint(char **ptr) +{ + const char *src=*ptr; + if(*src=='0' && (*(src+1)=='x' || *(src+1)=='X')) + { + (*ptr)+=2; + return str_uint_hex(ptr); + } + return str_uint_dec(ptr); +} + +/*@ + @ assigns \nothing; + @ */ +static unsigned char escaped_char(const unsigned char c) +{ + switch(c) + { + case 'b': + return '\b'; + case 'n': + return '\n'; + case 't': + return '\t'; + case 'r': + return '\r'; + case '0': + return '\0'; + default: + return c; + } +} + +/*@ + @ ensures 0 <= \result <= 0x10; + @ assigns \nothing; + @*/ +static unsigned int load_hex1(const unsigned char c) +{ + if(c>='0' && c<='9') + return c-'0'; + else if(c>='A' && c<='F') + return c-'A'+10; + else if(c>='a' && c<='f') + return c-'a'+10; + return 0x10; +} + +/*@ + @ ensures 0 <= \result <= 0x100; + @ assigns \nothing; + @*/ +static unsigned int load_hex2(const unsigned char c1, const unsigned char c2) +{ + unsigned int val1=load_hex1(c1); + unsigned int val2=load_hex1(c2); + if(val1 >= 0x10 || val2 >=0x10) + return 0x100; + return (val1*16)+val2; +} + +/*@ + @ requires \valid(*ptr); + @ requires valid_string(*ptr); + @ requires \valid(tmp + (0 .. PHOTOREC_MAX_SIG_SIZE-1)); + @ ensures valid_string(*ptr); + @ ensures \initialized(tmp + (0 .. \result-1)); + @*/ +/* TODO assigns *ptr, tmp[0 .. PHOTOREC_MAX_SIG_SIZE-1]; */ +static unsigned int load_signature(char **ptr, unsigned char *tmp) +{ + unsigned int signature_size=0; + char *pos=*ptr; + /*@ + @ loop invariant \valid(*ptr); + @ loop invariant valid_string(pos); + @ loop invariant signature_size <= PHOTOREC_MAX_SIG_SIZE; + @ loop invariant \valid(tmp + (0 .. PHOTOREC_MAX_SIG_SIZE-1)); + @ loop assigns pos, signature_size, tmp[0 .. PHOTOREC_MAX_SIG_SIZE-1]; + @*/ + while(*pos!='\n' && *pos!='\0') + { + if(signature_size>=PHOTOREC_MAX_SIG_SIZE) + return 0; + /*@ assert signature_size < PHOTOREC_MAX_SIG_SIZE; */ + if(*pos ==' ' || *pos=='\t' || *pos=='\r' || *pos==',') + pos++; + else if(*pos== '\'') + { + pos++; + if(*pos=='\0') + return 0; + if(*pos=='\\') + { + pos++; + if(*pos=='\0') + return 0; + tmp[signature_size++]=escaped_char(*(unsigned char *)pos); + } + else + { + tmp[signature_size++]=*(unsigned char *)pos; + } + pos++; + if(*pos!='\'') + return 0; + pos++; + } + else if(*pos=='"') + { + pos++; + /*@ + @ loop invariant valid_string(pos); + @ loop invariant signature_size <= PHOTOREC_MAX_SIG_SIZE; + @ loop assigns pos, signature_size, tmp[0 .. PHOTOREC_MAX_SIG_SIZE-1]; + @*/ + while(*pos!='"') + { + if(*pos=='\0') + return 0; + if(signature_size>=PHOTOREC_MAX_SIG_SIZE) + return 0; + if(*pos=='\\') + { + pos++; + if(*pos=='\0') + return 0; + tmp[signature_size++]=escaped_char(*(unsigned char *)pos); + } + else + tmp[signature_size++]=*(unsigned char *)pos; + pos++; + } + /*@ assert *pos=='"'; */ + pos++; + } + else if(*pos=='0' && (*(pos+1)=='x' || *(pos+1)=='X')) + { + pos+=2; + /*@ assert valid_string(pos); */ + /*@ + @ loop invariant valid_string(pos); + @ loop invariant signature_size <= PHOTOREC_MAX_SIG_SIZE; + @ loop assigns pos, signature_size, tmp[0 .. PHOTOREC_MAX_SIG_SIZE-1]; + @*/ + while( +#ifdef DISABLED_FOR_FRAMAC + *pos!='\0' && *(pos+1)!='\0' +#else + isxdigit(*pos) && isxdigit(*(pos+1)) +#endif + ) + { + unsigned int val; + if(signature_size>=PHOTOREC_MAX_SIG_SIZE) + return 0; + /*@ assert valid_string(pos); */ + /*@ assert valid_string(pos+1); */ + val=load_hex2(*(unsigned char *)pos, *(unsigned char *)(pos+1)); + if(val >= 0x100) + break; + pos+=2; + tmp[signature_size++]=val; + } + } + else + { + return 0; + } + /*@ assert valid_string(pos); */ + } + *ptr=pos; + return signature_size; +} + +/*@ + @ requires valid_register_header_check(file_stat); + @ requires valid_file_stat(file_stat); + @ requires valid_string(pos); + @ ensures valid_string(\result); + @*/ +static char *parse_signature_line(file_stat_t *file_stat, char *pos) +{ + /* each line is composed of "extension sig_offset signature" */ + const char *sig_ext=pos; + unsigned char *sig_sig=NULL; + unsigned int sig_offset=0; + unsigned int sig_size; + /* Read the extension */ + /*@ + @ loop invariant valid_read_string(sig_ext); + @ loop invariant valid_string(pos); + @ loop assigns pos; + @*/ + while(*pos!=' ' && *pos!='\t') + { + if(*pos=='\0' || *pos=='\n' || *pos=='\r') + return pos; + pos++; + } + *pos='\0'; + pos++; + /*@ assert valid_string(pos); */ +#ifndef DISABLED_FOR_FRAMAC + log_info("register a signature for %s\n", sig_ext); +#endif + /* skip spaces */ + /*@ + @ loop invariant valid_string(pos); + @ loop assigns pos; + @*/ + while(*pos=='\t' || *pos==' ') + { + /*@ assert *pos == '\t' || *pos== ' '; */ + /*@ assert valid_string(pos); */ + pos++; + } + sig_offset=str_uint(&pos); + if(sig_offset > PHOTOREC_MAX_SIG_OFFSET) + { + /* Invalid sig_offset */ + return pos; + } + /*@ assert sig_offset <= PHOTOREC_MAX_SIG_OFFSET; */ + /* read signature */ + sig_sig=(unsigned char *)MALLOC(PHOTOREC_MAX_SIG_SIZE); + /*@ assert valid_string(pos); */ + sig_size=load_signature(&pos, sig_sig); + if(sig_size==0) + { + free(sig_sig); + return pos; + } + if(*pos=='\n') + pos++; + /*@ assert sig_offset <= PHOTOREC_MAX_SIG_OFFSET; */ + /*@ assert sig_size <= PHOTOREC_MAX_SIG_SIZE; */ + if(sig_size>0 && sig_offset + sig_size <= PHOTOREC_MAX_SIG_OFFSET ) + { + /* FIXME: memory leak for signature */ + void *signature; + /*@ assert sig_size > 0; */ + /*@ assert sig_offset + sig_size <= PHOTOREC_MAX_SIG_OFFSET; */ + signature=(void *)MALLOC(sig_size); + /*@ assert \valid((char *)signature + (0 .. sig_size - 1)); */ + memcpy(signature, sig_sig, sig_size); +#ifndef DISABLED_FOR_FRAMAC + signature_insert(sig_ext, sig_offset, signature, sig_size); + register_header_check(sig_offset, signature, sig_size, &header_check_sig, file_stat); +#endif + } + free(sig_sig); + return pos; +} + +/*@ + @ requires valid_register_header_check(file_stat); + @ requires valid_file_stat(file_stat); + @ requires valid_string(pos); + @ ensures valid_string(\result); + @*/ static char *parse_signature_file(file_stat_t *file_stat, char *pos) { - const unsigned int signatures_empty=td_list_empty(&signatures.list); +#ifndef DISABLED_FOR_FRAMAC + /*@ + @ loop invariant valid_file_stat(file_stat); + @ loop invariant valid_string(pos); + @*/ while(*pos!='\0') +#endif { /* skip comments */ + /*@ + @ loop invariant valid_string(pos); + @ loop assigns pos; + @*/ while(*pos=='#') { + /*@ + @ loop invariant valid_string(pos); + @ loop assigns pos; + @*/ while(*pos!='\0' && *pos!='\n') pos++; if(*pos=='\0') return pos; pos++; } - /* each line is composed of "extension offset signature" */ - { - char *extension; - unsigned int offset=0; - unsigned char *tmp=NULL; - unsigned int signature_max_size=512; - unsigned int signature_size=0; - { - const char *extension_start=pos; - while(*pos!='\0' && !isspace(*pos)) - pos++; - if(*pos=='\0') - return pos; - *pos='\0'; - extension=strdup(extension_start); - pos++; - } - /* skip space */ - while(isspace(*pos)) - pos++; - /* read offset */ - pos=str_uint(pos, &offset); - /* read signature */ - tmp=(unsigned char *)MALLOC(signature_max_size); - while(*pos!='\n' && *pos!='\0') - { - if(signature_size==signature_max_size) - { - unsigned char *tmp_old=tmp; - signature_max_size*=2; - tmp=(unsigned char *)realloc(tmp, signature_max_size); - if(tmp==NULL) - { - free(extension); - free(tmp_old); - return pos; - } - } - if(isspace(*pos) || *pos=='\r' || *pos==',') - pos++; - else if(*pos== '\'') - { - pos++; - if(*pos=='\0') - { - free(extension); - free(tmp); - return pos; - } - else if(*pos=='\\') - { - pos++; - if(*pos=='\0') - { - free(extension); - free(tmp); - return pos; - } - else if(*pos=='b') - tmp[signature_size++]='\b'; - else if(*pos=='n') - tmp[signature_size++]='\n'; - else if(*pos=='t') - tmp[signature_size++]='\t'; - else if(*pos=='r') - tmp[signature_size++]='\r'; - else if(*pos=='0') - tmp[signature_size++]='\0'; - else - tmp[signature_size++]=*pos; - pos++; - } - else - { - tmp[signature_size++]=*pos; - pos++; - } - if(*pos!='\'') - { - free(extension); - free(tmp); - return pos; - } - pos++; - } - else if(*pos=='"') - { - pos++; - for(; *pos!='"' && *pos!='\0'; pos++) - { - if(signature_size==signature_max_size) - { - unsigned char *tmp_old=tmp; - signature_max_size*=2; - tmp=(unsigned char *)realloc(tmp, signature_max_size); - if(tmp==NULL) - { - free(extension); - free(tmp_old); - return pos; - } - } - if(*pos=='\\') - { - pos++; - if(*pos=='\0') - { - free(extension); - free(tmp); - return pos; - } - else if(*pos=='b') - tmp[signature_size++]='\b'; - else if(*pos=='n') - tmp[signature_size++]='\n'; - else if(*pos=='r') - tmp[signature_size++]='\r'; - else if(*pos=='t') - tmp[signature_size++]='\t'; - else if(*pos=='0') - tmp[signature_size++]='\0'; - else - tmp[signature_size++]=*pos; - } - else - tmp[signature_size++]=*pos;; - } - if(*pos!='"') - { - free(extension); - free(tmp); - return pos; - } - pos++; - } - else if(*pos=='0' && (*(pos+1)=='x' || *(pos+1)=='X')) - { - pos+=2; - while(isxdigit(*pos) && isxdigit(*(pos+1))) - { - unsigned int val=(*pos); - if(*pos>='0' && *pos<='9') - val-='0'; - else if(*pos>='A' && *pos<='F') - val=val-'A'+10; - else if(*pos>='a' && *pos<='f') - val=val-'a'+10; - pos++; - val*=16; - val+=(*pos); - if(*pos>='0' && *pos<='9') - val-='0'; - else if(*pos>='A' && *pos<='F') - val=val-'A'+10; - else if(*pos>='a' && *pos<='f') - val=val-'a'+10; - pos++; - tmp[signature_size++]=val; - } - } - else - { - free(extension); - free(tmp); - return pos; - } - } - if(*pos=='\n') - pos++; - if(signature_size>0) - { - /* FIXME: Small memory leak */ - unsigned char *signature=(unsigned char *)MALLOC(signature_size); - log_info("register a signature for %s\n", extension); - memcpy(signature, tmp, signature_size); - register_header_check(offset, signature, signature_size, &header_check_sig, file_stat); - if(signatures_empty) - signature_insert(extension, offset, signature, signature_size); - } - else - { - free(extension); - } - free(tmp); - } + /* skip empty lines */ + /*@ + @ loop invariant valid_string(pos); + @ loop assigns pos; + @*/ + while(*pos=='\n' || *pos=='\r') + pos++; + pos=parse_signature_line(file_stat, pos); } return pos; } @@ -424,14 +633,17 @@ static char *parse_signature_file(file_stat_t *file_stat, char *pos) static void register_header_check_sig(file_stat_t *file_stat) { char *pos; - char *buffer; + static char *buffer=NULL; size_t buffer_size; struct stat stat_rec; FILE *handle; +// if(!td_list_empty(&signatures.list)) + if(buffer!=NULL) + return ; handle=open_signature_file(); if(!handle) return; -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC buffer_size=1024*1024; #else if(fstat(fileno(handle), &stat_rec)<0 || stat_rec.st_size>100*1024*1024) @@ -457,8 +669,10 @@ static void register_header_check_sig(file_stat_t *file_stat) pos=parse_signature_file(file_stat, pos); if(*pos!='\0') { +#ifndef DISABLED_FOR_FRAMAC log_warning("Can't parse signature: %s\n", pos); +#endif } - free(buffer); +// free(buffer); } #endif diff --git a/src/file_sp3.c b/src/file_sp3.c index ec6f062f..c35388ed 100644 --- a/src/file_sp3.c +++ b/src/file_sp3.c @@ -150,7 +150,7 @@ static void register_header_check_sp3(file_stat_t *file_stat) static const unsigned char sp31_header[8]= { 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; static const unsigned char sp32_header[8]= { 0x03, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; register_header_check(0, sp31_header, sizeof(sp31_header), &header_check_sp3, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, sp32_header, sizeof(sp32_header), &header_check_sp3, file_stat); #endif } diff --git a/src/file_spe.c b/src/file_spe.c index c875ffe3..946b237c 100644 --- a/src/file_spe.c +++ b/src/file_spe.c @@ -300,7 +300,7 @@ static int header_check_spe(const unsigned char *buffer, const unsigned int buff file_recovery_new->extension=file_hint_spe.extension; file_recovery_new->min_filesize=4100; file_recovery_new->calculated_file_size=tmp; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_debug("spe xdim=%u ydim=%u NumFrames=%u datatype=%u size=%llu\n", le16(spe->xdim), le16(spe->ydim), (unsigned int)le32(spe->NumFrames), le16(spe->datatype), (long long unsigned) file_recovery_new->calculated_file_size); diff --git a/src/file_steuer2014.c b/src/file_steuer2014.c index a116845b..abc7757d 100644 --- a/src/file_steuer2014.c +++ b/src/file_steuer2014.c @@ -33,7 +33,7 @@ #include "filegen.h" #include "common.h" -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_STRPTIME #endif diff --git a/src/file_swf.c b/src/file_swf.c index 7253fcb6..6ab0fe08 100644 --- a/src/file_swf.c +++ b/src/file_swf.c @@ -24,7 +24,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(SINGLE_FORMAT) || defined(MAIN_photorec) || defined(MAIN_fidentify) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_ZLIB_H #undef HAVE_LIBZ #endif diff --git a/src/file_tar.c b/src/file_tar.c index 35001ca4..3098e049 100644 --- a/src/file_tar.c +++ b/src/file_tar.c @@ -20,7 +20,7 @@ */ -#if !defined(SINGLE_FORMAT) || defined(SINGLE_FORMAT_tar) || defined(MAIN_photorec) +#if !defined(SINGLE_FORMAT) || defined(SINGLE_FORMAT_tar) #ifdef HAVE_CONFIG_H #include #endif @@ -134,7 +134,7 @@ static int header_check_tar(const unsigned char *buffer, const unsigned int buff static void register_header_check_tar(file_stat_t *file_stat) { register_header_check(0x101, tar_header_gnu, sizeof(tar_header_gnu), &header_check_tar, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0x101, tar_header_posix, sizeof(tar_header_posix), &header_check_tar, file_stat); #endif } diff --git a/src/file_tiff_be.c b/src/file_tiff_be.c index 3a3d9905..c6f6597f 100644 --- a/src/file_tiff_be.c +++ b/src/file_tiff_be.c @@ -217,7 +217,7 @@ static uint64_t parse_strip_be(FILE *handle, const TIFFDirEntry *entry_strip_off be16(entry_strip_bytecounts->tdir_type)!=4) return TIFF_ERROR; /*@ assert 0 < nbr <= 2048; */ -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC offsetp=(uint32_t *)MALLOC(2048*sizeof(*offsetp)); #else offsetp=(uint32_t *)MALLOC(nbr*sizeof(*offsetp)); @@ -228,7 +228,7 @@ static uint64_t parse_strip_be(FILE *handle, const TIFFDirEntry *entry_strip_off free(offsetp); return TIFF_ERROR; } -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC sizep=(uint32_t *)MALLOC(2048*sizeof(*sizep)); #else sizep=(uint32_t *)MALLOC(nbr*sizeof(*sizep)); diff --git a/src/file_tiff_le.c b/src/file_tiff_le.c index f597d46d..ee986cfe 100644 --- a/src/file_tiff_le.c +++ b/src/file_tiff_le.c @@ -221,7 +221,7 @@ static uint64_t parse_strip_le(FILE *handle, const TIFFDirEntry *entry_strip_off le16(entry_strip_bytecounts->tdir_type)!=4) return TIFF_ERROR; /*@ assert 0 < nbr <= 2048; */ -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC offsetp=(uint32_t *)MALLOC(2048*sizeof(*offsetp)); #else offsetp=(uint32_t *)MALLOC(nbr*sizeof(*offsetp)); @@ -232,7 +232,7 @@ static uint64_t parse_strip_le(FILE *handle, const TIFFDirEntry *entry_strip_off free(offsetp); return TIFF_ERROR; } -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC sizep=(uint32_t *)MALLOC(2048*sizeof(*sizep)); #else sizep=(uint32_t *)MALLOC(nbr*sizeof(*sizep)); diff --git a/src/file_txt.c b/src/file_txt.c index 8c3984ea..15e73b22 100644 --- a/src/file_txt.c +++ b/src/file_txt.c @@ -167,6 +167,7 @@ static const txt_header_t fasttxt_headers[] = { /* Unix shell */ { "#!/bin/bash", 11, "sh"}, { "#!/bin/ksh", 10, "sh"}, +#ifndef DISABLED_FOR_FRAMAC { "#!/bin/sh", 9, "sh"}, { "#! /bin/bash", 12, "sh"}, { "#! /bin/ksh", 11, "sh"}, @@ -273,6 +274,7 @@ static const txt_header_t fasttxt_headers[] = { { "[.ShellClassInfo]", 17, "Desktop.ini" }, /* Fotobook */ { "instrs); patterns = le16(hdr->patterns); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_debug("xm: %u patterns, %u instruments\n", patterns, instrs); #endif @@ -248,7 +248,7 @@ static void file_check_xm(file_recovery_t *fr) /* Parse patterns and next instruments */ if(parse_patterns(fr, patterns) < 0 || parse_instruments(fr, instrs) < 0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_debug("xm: lost sync at pos %li\n", ftell(fr->handle)); #endif fr->offset_error = fr->file_size; diff --git a/src/file_zip.c b/src/file_zip.c index f5a96fbb..93522b31 100644 --- a/src/file_zip.c +++ b/src/file_zip.c @@ -238,7 +238,7 @@ static int64_t file_get_pos(FILE *f, const void* needle, const unsigned int size { if(my_fseek(f, (off_t)count-(off_t)read_size, SEEK_CUR)<0) { -#if !defined(__FRAMAC__) +#if !defined(DISABLED_FOR_FRAMAC) log_trace("zip: file_get_pos count-read failed\n"); #endif return -1; @@ -250,7 +250,7 @@ static int64_t file_get_pos(FILE *f, const void* needle, const unsigned int size } if(feof(f) || my_fseek(f, (off_t)1-size, SEEK_CUR)<0) { -#if !defined(__FRAMAC__) +#if !defined(DISABLED_FOR_FRAMAC) log_trace("zip: file_get_pos 1-size failed\n"); #endif return -1; @@ -1387,7 +1387,7 @@ static void register_header_check_zip(file_stat_t *file_stat) { static const unsigned char zip_header2[8] = { 'P', 'K', '0', '0', 'P', 'K', 0x03, 0x04}; /* WinZIPv8-compressed files. */ register_header_check(0, zip_header,sizeof(zip_header), &header_check_zip, file_stat); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC register_header_check(0, zip_header2,sizeof(zip_header2), &header_check_winzip, file_stat); #endif } diff --git a/src/filegen.c b/src/filegen.c index fbd51c74..7354382c 100644 --- a/src/filegen.c +++ b/src/filegen.c @@ -24,7 +24,7 @@ #include #endif -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC #undef HAVE_FTELLO #undef HAVE_FSEEKO #endif @@ -63,13 +63,18 @@ file_check_list_t file_check_list={ @ requires \valid_read(b); @ assigns \nothing; @*/ -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC static #endif int file_check_cmp(const struct td_list_head *a, const struct td_list_head *b) { +#ifdef DISABLED_FOR_FRAMAC + return 1; +#else const file_check_t *fc_a=td_list_entry_const(a, const file_check_t, list); const file_check_t *fc_b=td_list_entry_const(b, const file_check_t, list); + /*@ requires valid_file_check_node(fc_a); */ + /*@ requires valid_file_check_node(fc_b); */ int res; unsigned int min_length; /*@ assert \valid_read(fc_a); */ @@ -97,12 +102,14 @@ int file_check_cmp(const struct td_list_head *a, const struct td_list_head *b) if(res!=0) return res; return (int)fc_b->length-(int)fc_a->length; +#endif } /*@ @ requires \valid(file_check_new); @ requires \valid(pos); @ requires initialization: \initialized(&file_check_new->offset) && \initialized(&file_check_new->length); + @ requires valid_file_check_node(file_check_new); @*/ static void file_check_add_tail(file_check_t *file_check_new, file_check_list_t *pos) { @@ -131,7 +138,7 @@ static void file_check_add_tail(file_check_t *file_check_new, file_check_list_t td_list_add_tail(&newe->list, &pos->list); } -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC /*@ @ requires separation: \separated(file_stat, &file_check_plist); @*/ @@ -217,6 +224,7 @@ static unsigned int index_header_check(void) void free_header_check(void) { +#ifndef DISABLED_FOR_FRAMAC struct td_list_head *tmpl; struct td_list_head *nextl; td_list_for_each_safe(tmpl, nextl, &file_check_list.list) @@ -232,6 +240,7 @@ void free_header_check(void) { struct td_list_head *tmp; struct td_list_head *next; + /* TODO loop invariant \valid(next); */ td_list_for_each_safe(tmp, next, &pos->file_checks[i].list) { #ifdef DEBUG_HEADER_CHECK @@ -260,6 +269,7 @@ void free_header_check(void) td_list_del(tmpl); free(pos); } +#endif } void file_allow_nl(file_recovery_t *file_recovery, const unsigned int nl_mode) @@ -447,7 +457,7 @@ file_stat_t * init_file_stats(file_enable_t *files_enable) } sign_nbr=index_header_check(); file_stats[enable_count-1].file_hint=NULL; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("%u first-level signatures enabled\n", sign_nbr); #endif return file_stats; @@ -462,7 +472,7 @@ file_stat_t * init_file_stats(file_enable_t *files_enable) @*/ static int file_rename_aux(file_recovery_t *file_recovery, const char *new_ext) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC char new_filename[sizeof(file_recovery->filename)]; char *dst; char *dst_dir_sep; @@ -538,7 +548,7 @@ static int _file_rename(file_recovery_t *file_recovery, const void *buffer, cons len+=buffer_size-offset+1; if(new_ext!=NULL) len+=strlen(new_ext); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC new_filename=(char*)MALLOC(len); dst=new_filename; directory_sep=new_filename; @@ -677,7 +687,7 @@ static int _file_rename_unicode(file_recovery_t *file_recovery, const void *buff len+=buffer_size-offset; if(new_ext!=NULL) len+=strlen(new_ext); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC new_filename=(char*)MALLOC(len); dst=new_filename; dst_dir_sep=dst; @@ -790,11 +800,9 @@ void header_ignored_cond_reset(uint64_t start, uint64_t end) /* 0: file_recovery_new->location.start has been taken into account, offset_skipped_header may have been updated * 1: file_recovery_new->location.start has been ignored */ -/*@ - @ requires separation: \separated(file_recovery, file_recovery->handle, file_recovery_new, &errno, &offset_skipped_header); - @*/ int header_ignored_adv(const file_recovery_t *file_recovery, const file_recovery_t *file_recovery_new) { + /*@ assert \separated(file_recovery, file_recovery->handle, file_recovery_new, &errno, &offset_skipped_header); */ file_recovery_t fr_test; off_t offset; assert(file_recovery!=NULL); diff --git a/src/fnctdsk.c b/src/fnctdsk.c index b6a7487f..ea0dcd49 100644 --- a/src/fnctdsk.c +++ b/src/fnctdsk.c @@ -59,6 +59,7 @@ uint64_t CHS2offset(const disk_t *disk_car,const CHS_t*CHS) { return (((uint64_t)CHS->cylinder * disk_car->geom.heads_per_cylinder + CHS->head) * disk_car->geom.sectors_per_head + CHS->sector - 1) * disk_car->sector_size; +// return (uint64_t)C_H_S2LBA(disk_car, CHS->cylinder, CHS->head, CHS->sector) * disk_car->sector_size; } unsigned int offset2sector(const disk_t *disk_car, const uint64_t offset) @@ -111,40 +112,72 @@ void dup_partition_t(partition_t *dst, const partition_t *src) #endif } -list_disk_t *insert_new_disk_aux(list_disk_t *list_disk, disk_t *disk, disk_t **the_disk) +/*@ + @ requires valid_list_disk(list_disk); + @ requires disk!=\null; + @ requires valid_disk(disk); + @ assigns \nothing; + @*/ +static disk_t *search_disk(const list_disk_t *list_disk, const disk_t *disk) { - list_disk_t *tmp; - list_disk_t *prev=NULL; - list_disk_t *new_disk; - if(disk==NULL) - { - if(the_disk!=NULL) - *the_disk=NULL; - return list_disk; - } - /* Add it at the end if it doesn't already exist */ + const list_disk_t *tmp; + /*@ + @ loop assigns tmp; + @*/ for(tmp=list_disk;tmp!=NULL;tmp=tmp->next) { if(tmp->disk->device!=NULL && disk->device!=NULL && strcmp(tmp->disk->device, disk->device)==0) { - disk->clean(disk); - if(the_disk!=NULL) - *the_disk=tmp->disk; - return list_disk; + return tmp->disk; } - prev=tmp; + } + return NULL; +} + +list_disk_t *insert_new_disk_aux(list_disk_t *list_disk, disk_t *disk, disk_t **the_disk) +{ + list_disk_t *prev=NULL; + list_disk_t *new_disk; + disk_t *found; + if(disk==NULL) + { + if(the_disk!=NULL) + *the_disk=NULL; + /*@ assert valid_list_disk(list_disk); */ + return list_disk; + } + found=search_disk(list_disk, disk); + /* Do not add a disk already known */ + if(found!=NULL) + { + disk->clean(disk); + if(the_disk!=NULL) + *the_disk=found; + /*@ assert valid_list_disk(list_disk); */ + return list_disk; + } + /* Add the disk at the end */ + { + list_disk_t *tmp; + /*@ + @ loop assigns tmp,prev; + @*/ + for(tmp=list_disk;tmp!=NULL;tmp=tmp->next) + prev=tmp; } new_disk=(list_disk_t *)MALLOC(sizeof(*new_disk)); new_disk->disk=disk; + new_disk->prev=prev; + new_disk->next=NULL; if(prev!=NULL) { prev->next=new_disk; } - new_disk->prev=prev; - new_disk->next=NULL; if(the_disk!=NULL) *the_disk=disk; + /*@ assert valid_list_disk(new_disk); */ + /*@ assert valid_list_disk(list_disk); */ return (list_disk!=NULL?list_disk:new_disk); } @@ -222,6 +255,10 @@ list_part_t *sort_partition_list(list_part_t *list_part) list_part_t *new_list_part=NULL; list_part_t *element; list_part_t *next; + /*@ assert valid_list_part(new_list_part); */ + /*@ + @ loop invariant valid_list_part(new_list_part); + @*/ for(element=list_part;element!=NULL;element=next) { int insert_error=0; @@ -231,6 +268,7 @@ list_part_t *sort_partition_list(list_part_t *list_part) free(element->part); free(element); } + /*@ assert valid_list_part(new_list_part); */ return new_list_part; } @@ -238,12 +276,17 @@ list_part_t *gen_sorted_partition_list(const list_part_t *list_part) { list_part_t *new_list_part=NULL; const list_part_t *element; + /*@ assert valid_list_part(new_list_part); */ + /*@ + @ loop invariant valid_list_part(new_list_part); + @*/ for(element=list_part;element!=NULL;element=element->next) { int insert_error=0; if(element->part->status!=STATUS_DELETED) new_list_part=insert_new_partition(new_list_part, element->part, 1, &insert_error); } + /*@ assert valid_list_part(new_list_part); */ return new_list_part; } @@ -252,6 +295,7 @@ void part_free_list(list_part_t *list_part) { list_part_t *element; element=list_part; + /*@ loop invariant valid_list_part(element); */ while(element!=NULL) { list_part_t *next=element->next; @@ -266,6 +310,7 @@ void part_free_list_only(list_part_t *list_part) { list_part_t *element; element=list_part; + /*@ loop invariant valid_list_part(element); */ while(element!=NULL) { list_part_t *next=element->next; @@ -283,6 +328,9 @@ int is_part_overlapping(const list_part_t *list_part) if(list_part==NULL) return 0; element=list_part; + /*@ + @ loop assigns element; + @*/ while(1) { const list_part_t *next=element->next; @@ -313,7 +361,9 @@ void partition_reset(partition_t *partition, const arch_fnct_t *arch) partition->part_type_mac=PMAC_UNK; partition->part_type_xbox=PXBOX_UNK; partition->part_type_gpt=GPT_ENT_TYPE_UNUSED; +#ifndef DISABLED_FOR_FRAMAC guid_cpy(&partition->part_uuid, &GPT_ENT_TYPE_UNUSED); +#endif partition->upart_type=UP_UNK; partition->status=STATUS_DELETED; partition->order=NO_ORDER; @@ -357,7 +407,7 @@ static unsigned int get_geometry_from_list_part_aux(const disk_t *disk_car, cons } } } -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(nbr>0) { log_info("get_geometry_from_list_part_aux head=%u nbr=%u\n", @@ -387,26 +437,29 @@ unsigned int get_geometry_from_list_part(const disk_t *disk_car, const list_part unsigned int best_score; unsigned int i; unsigned int heads_per_cylinder=disk_car->geom.heads_per_cylinder; - disk_t *new_disk_car=(disk_t *)MALLOC(sizeof(*new_disk_car)); - memcpy(new_disk_car,disk_car,sizeof(*new_disk_car)); - best_score=get_geometry_from_list_part_aux(new_disk_car, list_part, verbose); + disk_t new_disk_car; + memcpy(&new_disk_car,disk_car,sizeof(new_disk_car)); + best_score=get_geometry_from_list_part_aux(&new_disk_car, list_part, verbose); + /*@ loop assigns i, best_score, heads_per_cylinder, new_disk_car.geom.heads_per_cylinder; */ for(i=0; head_list[i]!=0; i++) { unsigned int score; - new_disk_car->geom.heads_per_cylinder=head_list[i]; - score=get_geometry_from_list_part_aux(new_disk_car, list_part, verbose); + new_disk_car.geom.heads_per_cylinder=head_list[i]; + score=get_geometry_from_list_part_aux(&new_disk_car, list_part, verbose); if(score >= best_score) { best_score=score; - heads_per_cylinder=new_disk_car->geom.heads_per_cylinder; + heads_per_cylinder=new_disk_car.geom.heads_per_cylinder; } } - free(new_disk_car); return heads_per_cylinder; } void size_to_unit(const uint64_t disk_size, char *buffer) { +#ifdef DISABLED_FOR_FRAMAC + buffer[0]='\0'; +#else if(disk_size<(uint64_t)10*1024) sprintf(buffer,"%u B", (unsigned)disk_size); else if(disk_size<(uint64_t)10*1024*1024) @@ -417,13 +470,18 @@ void size_to_unit(const uint64_t disk_size, char *buffer) sprintf(buffer,"%u GB / %u GiB", (unsigned)(disk_size/1000/1000/1000), (unsigned)(disk_size/1024/1024/1024)); else sprintf(buffer,"%u TB / %u TiB", (unsigned)(disk_size/1000/1000/1000/1000), (unsigned)(disk_size/1024/1024/1024/1024)); +#endif } void log_disk_list(list_disk_t *list_disk) { +#ifndef DISABLED_FOR_FRAMAC list_disk_t *element_disk; /* save disk parameters to rapport */ log_info("Hard disk list\n"); + /*@ + @ loop invariant valid_list_disk(list_disk); + @*/ for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next) { disk_t *disk=element_disk->disk; @@ -437,4 +495,5 @@ void log_disk_list(list_disk_t *list_disk) log_info("\n"); } log_info("\n"); +#endif } diff --git a/src/geometry.c b/src/geometry.c index 8759feb7..a9b1a6c4 100644 --- a/src/geometry.c +++ b/src/geometry.c @@ -79,7 +79,7 @@ int change_geometry_cli(disk_t *disk_car, char ** current_cmd) int geo_modified=0; if(*current_cmd==NULL) return 0; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("Current geometry\n%s sector_size=%u\n", disk_car->description(disk_car), disk_car->sector_size); #endif /*@ loop invariant valid_read_string(*current_cmd); */ @@ -96,7 +96,7 @@ int change_geometry_cli(disk_t *disk_car, char ** current_cmd) if(geo_modified==0) geo_modified=1; } -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC else log_error("Illegal cylinders value\n"); #endif @@ -112,7 +112,7 @@ int change_geometry_cli(disk_t *disk_car, char ** current_cmd) if(cyl_modified==0) set_cylinders_from_size_up(disk_car); } -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC else log_error("Illegal heads value\n"); #endif @@ -128,7 +128,7 @@ int change_geometry_cli(disk_t *disk_car, char ** current_cmd) if(cyl_modified==0) set_cylinders_from_size_up(disk_car); } -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC else log_error("Illegal sectors value\n"); #endif @@ -138,7 +138,7 @@ int change_geometry_cli(disk_t *disk_car, char ** current_cmd) tmp_val = get_int_from_command(current_cmd); if(change_sector_size(disk_car, cyl_modified, tmp_val)==0) geo_modified=2; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC else log_error("Illegal sector size\n"); #endif @@ -158,7 +158,7 @@ int change_geometry_cli(disk_t *disk_car, char ** current_cmd) /* On MacOSX if HD contains some bad sectors, the disk size may not be correctly detected */ disk_car->disk_real_size=disk_car->disk_size; #endif -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("New geometry\n%s sector_size=%u\n", disk_car->description(disk_car), disk_car->sector_size); #endif autoset_unit(disk_car); diff --git a/src/geometryn.c b/src/geometryn.c index c985fb53..06e281e7 100644 --- a/src/geometryn.c +++ b/src/geometryn.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/hdaccess.c b/src/hdaccess.c index 21a0a95b..faff4dd6 100644 --- a/src/hdaccess.c +++ b/src/hdaccess.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_FSYNC #undef HAVE_GLOB_H #undef HAVE_LIBEWF @@ -169,8 +169,20 @@ struct info_file_struct int mode; }; -static void autoset_geometry(disk_t * disk_car, const unsigned char *buffer, const int verbose); -static void file_clean(disk_t *disk); +struct dosemu_image_header { + char sig[7]; /* always set to "DOSEMU", null-terminated or to "\x0eDEXE" */ + uint32_t heads; + uint32_t sectors; + uint32_t cylinders; + uint32_t header_end; /* distance from beginning of disk to end of header + * i.e. this is the starting byte of the real disk + */ + char dummy[1]; /* someone did define the header unaligned, + * we correct that atleast for the future + */ + uint32_t dexeflags; +} __attribute__((packed)) ; + static int file_pread(disk_t *disk_car, void *buf, const unsigned int count, const uint64_t offset); static int file_pwrite(disk_t *disk_car, const void *buf, const unsigned int count, const uint64_t offset); static int file_nopwrite(disk_t *disk_car, const void *buf, const unsigned int count, const uint64_t offset); @@ -199,34 +211,36 @@ void generic_clean(disk_t *disk) } #if defined(__CYGWIN__) || defined(__MINGW32__) -list_disk_t *insert_new_disk_nodup(list_disk_t *list_disk, disk_t *disk_car, const char *device_name, const int verbose); -list_disk_t *insert_new_disk_nodup(list_disk_t *list_disk, disk_t *disk_car, const char *device_name, const int verbose) +static list_disk_t *insert_new_disk_nodup(list_disk_t *list_disk, disk_t *disk_car, const char *device_name, const int verbose) { if(disk_car==NULL) return list_disk; + if(disk_car->sector_size==512) { - int disk_same_size_present=0; list_disk_t *cur; for(cur=list_disk;cur!=NULL;cur=cur->next) { if(cur->disk->sector_size==disk_car->sector_size && ((cur->disk->model==NULL && disk_car->model==NULL && cur->disk->disk_size==disk_car->disk_size) || (cur->disk->model!=NULL && disk_car->model!=NULL && strcmp(cur->disk->model, disk_car->model)==0))) - disk_same_size_present=1; + { + if(verbose>1) + log_verbose("%s is available but reject it to avoid duplicate disk.\n", device_name); + disk_car->clean(disk_car); + return list_disk; + } } - if(disk_car->sector_size==512 && disk_same_size_present!=0) - { - if(verbose>1) - log_verbose("%s is available but reject it to avoid duplicate disk.\n", device_name); - disk_car->clean(disk_car); - return list_disk; - } - return insert_new_disk(list_disk,disk_car); } + return insert_new_disk(list_disk,disk_car); } #endif -#ifdef HAVE_GLOB_H +#if defined(HAVE_GLOB_H) +/*@ + @ requires valid_read_string(device_pattern); + @ requires \valid(list_disk); + @ ensures \valid(list_disk); + @*/ static list_disk_t *hd_glob_parse(const char *device_pattern, list_disk_t *list_disk, const int verbose, const int testdisk_mode) { glob_t globbuf; @@ -309,57 +323,93 @@ list_disk_t *hd_parse(list_disk_t *list_disk, const int verbose, const int testd char device_i2o_hd[]="/dev/i2o/hda"; char device_mmc[]="/dev/mmcblk0"; /* Disk IDE */ + /*@ + @ loop invariant valid_list_disk(list_disk); + @ loop invariant 0 <= i <= 8; + @*/ for(i=0;i<8;i++) { device_ide[strlen(device_ide)-1]='a'+i; list_disk=insert_new_disk(list_disk, file_test_availability(device_ide, verbose, testdisk_mode)); } /* Device RAID Compaq */ + /*@ + @ loop invariant valid_list_disk(list_disk); + @ loop invariant 0 <= j <= 8; + @*/ for(j=0;j<8;j++) { device_ida[strlen(device_ida)-3]='0'+j; + /*@ + @ loop invariant valid_list_disk(list_disk); + @ loop invariant 0 <= i <= 8; + @*/ for(i=0;i<8;i++) { device_ida[strlen(device_ida)-1]='0'+i; list_disk=insert_new_disk(list_disk, file_test_availability(device_ida, verbose, testdisk_mode)); } } + /*@ + @ loop invariant valid_list_disk(list_disk); + @ loop invariant 0 <= i <= 8; + @*/ for(i=0;i<8;i++) { device_cciss[strlen(device_cciss)-1]='0'+i; list_disk=insert_new_disk(list_disk, file_test_availability(device_cciss, verbose, testdisk_mode)); } /* Device RAID */ + /*@ + @ loop invariant valid_list_disk(list_disk); + @ loop invariant 0 <= i <= 10; + @*/ for(i=0;i<10;i++) { snprintf(device,sizeof(device),"/dev/rd/c0d%u",i); list_disk=insert_new_disk(list_disk, file_test_availability(device, verbose, testdisk_mode)); } /* Device RAID IDE */ + /*@ + @ loop invariant valid_list_disk(list_disk); + @ loop invariant 0 <= i <= 15; + @*/ for(i=0;i<15;i++) { snprintf(device,sizeof(device),"/dev/ataraid/d%u",i); list_disk=insert_new_disk(list_disk, file_test_availability(device, verbose, testdisk_mode)); } /* Parallel port IDE disk */ + /*@ + @ loop invariant valid_list_disk(list_disk); + @ loop invariant 0 <= i <= 4; + @*/ for(i=0;i<4;i++) { device_p_ide[strlen(device_p_ide)-1]='a'+i; list_disk=insert_new_disk(list_disk, file_test_availability(device_p_ide, verbose, testdisk_mode)); } /* I2O hard disk */ + /*@ + @ loop invariant valid_list_disk(list_disk); + @ loop invariant 0 <= i <= 26; + @*/ for(i=0;i<26;i++) { device_i2o_hd[strlen(device_i2o_hd)-1]='a'+i; list_disk=insert_new_disk(list_disk, file_test_availability(device_i2o_hd, verbose, testdisk_mode)); } /* Memory card */ + /*@ + @ loop invariant valid_list_disk(list_disk); + @ loop invariant 0 <= i <= 10; + @*/ for(i=0;i<10;i++) { device_mmc[strlen(device_mmc)-1]='0'+i; list_disk=insert_new_disk(list_disk, file_test_availability(device_mmc, verbose, testdisk_mode)); } -#ifdef HAVE_GLOB_H +#if defined(HAVE_GLOB_H) /* Disk SCSI */ list_disk=hd_glob_parse("/dev/sd[a-z]", list_disk, verbose, testdisk_mode); list_disk=hd_glob_parse("/dev/sd[a-z][a-z]", list_disk, verbose, testdisk_mode); @@ -474,10 +524,15 @@ list_disk_t *hd_parse(list_disk_t *list_disk, const int verbose, const int testd } } #endif + /*@ assert valid_list_disk(list_disk); */ return list_disk; } #ifndef DJGPP +/*@ + @ requires valid_read_string(device); + @ ensures \result > 0; + @*/ static unsigned int disk_get_sector_size(const int hd_h, const char *device, const int verbose) { unsigned int sector_size=0; @@ -580,7 +635,7 @@ static unsigned int disk_get_sector_size(const int hd_h, const char *device, con device_geometry g; int error; error = ioctl(hd_h, B_GET_GEOMETRY, &g, sizeof(g)); - if(error==0) + if(error==0 && g.bytes_per_sector > 0) { if(verbose>1) { @@ -597,12 +652,37 @@ static unsigned int disk_get_sector_size(const int hd_h, const char *device, con return DEFAULT_SECTOR_SIZE; } +/*@ + @ requires \valid_read(geom); + @ ensures \result==-1 || \result==0; + @ ensures (0 < geom->heads_per_cylinder <= 255 && 0 < geom->sectors_per_head <= 63 && 0 < geom->cylinders < 0x2000000000000) ==> \result == 0; + @ ensures !(0 < geom->heads_per_cylinder <= 255 && 0 < geom->sectors_per_head <= 63 && 0 < geom->cylinders < 0x2000000000000) ==> \result == -1; + @ assigns \nothing; + @*/ +static int check_geometry(const CHSgeometry_t *geom) +{ + if(geom->heads_per_cylinder==0 || geom->heads_per_cylinder>255) + return -1; + if(geom->sectors_per_head==0 || geom->sectors_per_head>63) + return -1; + if(geom->cylinders==0 || geom->cylinders >= 0x2000000000000) + return -1; + return 0; +} + +/*@ + @ requires \valid(geom); + @ requires valid_read_string(device); + @ requires \separated(geom, device); + @ ensures 0 < geom->heads_per_cylinder <= 255; + @ ensures 0 < geom->sectors_per_head <= 63; + @*/ static void disk_get_geometry(CHSgeometry_t *geom, const int hd_h, const char *device, const int verbose) { if(verbose>1) log_verbose("disk_get_geometry for %s\n", device); #ifdef HDIO_GETGEO_BIG - if(geom->sectors_per_head==0) + if(check_geometry(geom)<0) { struct hd_big_geometry geometry; if (ioctl(hd_h, HDIO_GETGEO_BIG, &geometry)>=0) @@ -626,7 +706,7 @@ static void disk_get_geometry(CHSgeometry_t *geom, const int hd_h, const char *d } #endif #ifdef HDIO_GETGEO - if(geom->sectors_per_head==0) + if(check_geometry(geom)<0) { struct hd_geometry geometry; if(ioctl(hd_h, HDIO_GETGEO, &geometry)>=0) @@ -643,7 +723,7 @@ static void disk_get_geometry(CHSgeometry_t *geom, const int hd_h, const char *d } #endif #ifdef DKIOCGGEOM - if(geom->sectors_per_head==0) + if(check_geometry(geom)<0) { struct dk_geom dkgeom; if (ioctl (hd_h, DKIOCGGEOM, &dkgeom)>=0) { @@ -658,7 +738,7 @@ static void disk_get_geometry(CHSgeometry_t *geom, const int hd_h, const char *d } #endif #ifdef DIOCGDINFO - if(geom->sectors_per_head==0) + if(check_geometry(geom)<0) { struct disklabel geometry; if (ioctl(hd_h, DIOCGDINFO, &geometry)==0) @@ -681,7 +761,7 @@ static void disk_get_geometry(CHSgeometry_t *geom, const int hd_h, const char *d } #endif #ifdef DIOCGFWSECTORS - if(geom->sectors_per_head==0) + if(check_geometry(geom)<0) { int error; unsigned int u; @@ -737,8 +817,12 @@ static void disk_get_geometry(CHSgeometry_t *geom, const int hd_h, const char *d } } #endif - if(geom->sectors_per_head>0 && geom->heads_per_cylinder>0) + if(check_geometry(geom)==0) + { + /*@ assert 0 < geom->heads_per_cylinder <= 255; */ + /*@ assert 0 < geom->sectors_per_head <= 63; */ return ; + } geom->cylinders=0; geom->heads_per_cylinder=1; geom->sectors_per_head=1; @@ -746,8 +830,13 @@ static void disk_get_geometry(CHSgeometry_t *geom, const int hd_h, const char *d { log_error("disk_get_geometry default geometry for %s\n", device); } + /*@ assert 0 < geom->heads_per_cylinder <= 255; */ + /*@ assert 0 < geom->sectors_per_head <= 63; */ } +/*@ + @ requires valid_read_string(device); + @*/ static uint64_t disk_get_size(const int hd_h, const char *device, const int verbose, const unsigned int sector_size) { if(verbose>1) @@ -850,7 +939,7 @@ void update_disk_car_fields(disk_t *disk_car) { if(disk_car->geom.cylinders>0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_warning("Fix disk size using CHS\n"); #endif disk_car->disk_real_size=(uint64_t)disk_car->geom.cylinders * disk_car->geom.heads_per_cylinder * @@ -865,7 +954,7 @@ void update_disk_car_fields(disk_t *disk_car) (uint64_t)disk_car->sector_size; if(cylinder_num>0 && disk_car->geom.cylinders != cylinder_num) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_debug("Fix cylinder count for %s: number of cylinders %lu != %lu (calculated)\n", disk_car->device, disk_car->geom.cylinders, cylinder_num); #endif @@ -878,27 +967,51 @@ void update_disk_car_fields(disk_t *disk_car) } #ifdef TARGET_LINUX +/*@ + @ requires valid_string(buf); + @ ensures valid_string(buf); + @*/ +static void rtrim(char *buf) +{ + unsigned int i; + /*@ loop assigns i; */ + for(i=strlen(buf); i>0 && buf[i] == ' '; i--); + /*@ assert 0 <= i < strlen(buf); */ + buf[i]='\0'; +} + /* This function reads the /sys entry named "file" for device "disk_car". */ -static char * read_device_sysfs_file (const disk_t *disk_car, const char *file) +/*@ + @ requires \valid(buf + (0..255)); + @ requires \valid_read(disk_car); + @ requires valid_read_string(file); + @ requires separation: \separated(buf, disk_car, file, &errno); + @ ensures \result==-1 || \result==0; + @ ensures \result==-1 || valid_string(buf); + @*/ +static int read_device_sysfs_file (char *buf, const disk_t *disk_car, const char *file) { FILE *f; +#ifndef DISABLED_FOR_FRAMAC char name_buf[128]; - char buf[256]; - snprintf (name_buf, 127, "/sys/block/%s/device/%s", basename (disk_car->device), file); - if ((f = fopen (name_buf, "r")) == NULL) - return NULL; - + return -1; +#else + if ((f = fopen ("/sys/block/hda/device/vendor", "r")) == NULL) + return -1; +#endif if (fgets (buf, 255, f) == NULL) { fclose (f); - return NULL; + return -1; } - + /*@ assert valid_string(buf); */ fclose (f); - return strip_dup (buf); + /*@ assert valid_string(buf); */ + rtrim(buf); + return 0; } #endif @@ -938,6 +1051,11 @@ typedef struct _scsi_inquiry_data #define INQ_CMD_LEN 6 #define INQ_REPLY_LEN sizeof(scsi_inquiry_data_t) +/*@ + @ requires \valid(vendor); + @ requires \valid(product); + @ requires \valid(fw_rev); + @*/ static int scsi_query_product_info (const int sg_fd, char **vendor, char **product, char **fw_rev) { unsigned char inqCmdBlk[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0}; @@ -993,6 +1111,11 @@ static int scsi_query_product_info (const int sg_fd, char **vendor, char **produ #endif #ifndef DJGPP +/*@ + @ requires \valid(dev); + @ requires valid_disk(dev); + @ ensures valid_disk(dev); + @*/ static void disk_get_model(const int hd_h, disk_t *dev, const unsigned int verbose) { #ifdef HDIO_GET_IDENTITY @@ -1047,17 +1170,20 @@ static void disk_get_model(const int hd_h, disk_t *dev, const unsigned int verbo return; { /* Use modern /sys interface for SCSI device */ - char *vendor; - char *product; - vendor = read_device_sysfs_file (dev, "vendor"); - product = read_device_sysfs_file (dev, "model"); - if (vendor && product) + char vendor[256]; + char product[256]; + memset(&vendor, 0, sizeof(vendor)); + memset(&product, 0, sizeof(product)); + if(read_device_sysfs_file (&vendor[0], dev, "vendor")==0) { - dev->model = (char*) MALLOC(8 + 16 + 2); - sprintf (dev->model, "%.8s %.16s", vendor, product); + /*@ assert valid_string(&vendor[0]); */ + if( read_device_sysfs_file (&product[0], dev, "model")==0) + { + /*@ assert valid_string(&product[0]); */ + dev->model = (char*) MALLOC(8 + 16 + 2); + sprintf (dev->model, "%.8s %.16s", vendor, product); + } } - free(vendor); - free(product); } #endif #if defined(__CYGWIN__) || defined(__MINGW32__) @@ -1073,11 +1199,16 @@ static void disk_get_model(const int hd_h, disk_t *dev, const unsigned int verbo file_win32_disk_get_model(handle, dev, verbose); } #endif + /*@ assert valid_disk(dev); */ } +/*@ + @ requires sector_size > 0; + @ requires valid_read_string(device); + @*/ static uint64_t compute_device_size(const int hd_h, const char *device, const int verbose, const unsigned int sector_size) { -#ifdef HAVE_PREAD +#if defined(HAVE_PREAD) /* This function can failed if there are bad sectors */ uint64_t min_offset; uint64_t max_offset; @@ -1120,10 +1251,20 @@ static uint64_t compute_device_size(const int hd_h, const char *device, const in } #endif +/*@ + @ requires \valid(disk); + @ requires \valid_read((const struct info_file_struct *)disk->data); + @ requires valid_disk(disk); + @ ensures valid_disk(disk); + @*/ +// ensures valid_read_string(\result); static const char *file_description(disk_t *disk) { const struct info_file_struct *data=(const struct info_file_struct *)disk->data; char buffer_disk_size[100]; +#ifdef DISABLED_FOR_FRAMAC + memset(&buffer_disk_size, 0, sizeof(buffer_disk_size)); +#endif size_to_unit(disk->disk_size, buffer_disk_size); if(disk->geom.heads_per_cylinder == 1 && disk->geom.sectors_per_head == 1) snprintf(disk->description_txt, sizeof(disk->description_txt), @@ -1137,13 +1278,25 @@ static const char *file_description(disk_t *disk) disk->device, buffer_disk_size, disk->geom.cylinders, disk->geom.heads_per_cylinder, disk->geom.sectors_per_head, ((data->mode&O_RDWR)==O_RDWR?"":" (RO)")); + /*@ assert valid_read_string((char *)&disk->description_txt); */ + /*@ assert valid_disk(disk); */ return disk->description_txt; } +/*@ + @ requires \valid(disk_car); + @ requires \valid_read((const struct info_file_struct *)disk_car->data); + @ requires valid_disk(disk_car); + @ ensures valid_disk(disk_car); + @*/ +// ensures valid_read_string(\result); static const char *file_description_short(disk_t *disk_car) { const struct info_file_struct *data=(const struct info_file_struct *)disk_car->data; char buffer_disk_size[100]; +#ifdef DISABLED_FOR_FRAMAC + memset(&buffer_disk_size, 0, sizeof(buffer_disk_size)); +#endif size_to_unit(disk_car->disk_size, buffer_disk_size); if(disk_car->model==NULL) snprintf(disk_car->description_short_txt, sizeof(disk_car->description_txt),"Disk %s - %s%s", @@ -1154,9 +1307,16 @@ static const char *file_description_short(disk_t *disk_car) disk_car->device, buffer_disk_size, ((data->mode&O_RDWR)==O_RDWR?"":" (RO)"), disk_car->model); + /*@ assert valid_read_string((char *)&disk_car->description_short_txt); */ + /*@ assert valid_disk(disk_car); */ return disk_car->description_short_txt; } +/*@ + @ requires \valid(disk); + @ requires \freeable(disk); + @ requires valid_disk(disk); + @*/ static void file_clean(disk_t *disk) { if(disk->data!=NULL) @@ -1184,6 +1344,11 @@ static void file_clean(disk_t *disk) generic_clean(disk); } +/*@ + @ requires \valid_read(disk); + @ requires valid_disk(disk); + @ requires \valid((char *)buf + (0 .. count - 1)); + @*/ static int file_pread_aux(const disk_t *disk, void *buf, const unsigned int count, const uint64_t offset) { long int ret; @@ -1300,11 +1465,29 @@ static int file_pread_aux(const disk_t *disk, void *buf, const unsigned int coun return ret; } +/*@ + @ requires \valid(disk_car); + @ requires valid_disk(disk_car); + @ requires disk_car->sector_size > 0; + @ requires disk_car->offset < 0x2000000000000; + @ requires 0 < count < 0x2000000000000; + @ requires offset < 0x2000000000000; + @ requires \valid((char *)buf + (0 .. count-1)); + @*/ static int file_pread(disk_t *disk_car, void *buf, const unsigned int count, const uint64_t offset) { return align_pread(&file_pread_aux, disk_car, buf, count, offset); } +/*@ + @ requires \valid(disk_car); + @ requires valid_disk(disk_car); + @ requires disk_car->sector_size > 0; + @ requires disk_car->offset < 0x2000000000000; + @ requires 0 < count < 0x2000000000000; + @ requires offset < 0x2000000000000; + @ requires \valid_read((char *)buf + (0 .. count-1)); + @*/ static int file_pwrite_aux(disk_t *disk_car, const void *buf, const unsigned int count, const uint64_t offset) { int fd=((struct info_file_struct *)disk_car->data)->handle; @@ -1343,11 +1526,28 @@ static int file_pwrite_aux(disk_t *disk_car, const void *buf, const unsigned int return ret; } +/*@ + @ requires \valid(disk_car); + @ requires disk_car->sector_size > 0; + @ requires disk_car->offset < 0x2000000000000; + @ requires 0 < count < 0x2000000000000; + @ requires offset < 0x2000000000000; + @ requires \valid_read((char *)buf + (0 .. count-1)); + @*/ static int file_pwrite(disk_t *disk_car, const void *buf, const unsigned int count, const uint64_t offset) { return align_pwrite(&file_pread_aux, &file_pwrite_aux, disk_car, buf, count, offset); } +/*@ + @ requires \valid(disk_car); + @ requires valid_disk(disk_car); + @ requires disk_car->sector_size > 0; + @ requires disk_car->offset < 0x2000000000000; + @ requires 0 < count < 0x2000000000000; + @ requires offset < 0x2000000000000; + @ requires \valid_read((char *)buf + (0 .. count-1)); + @*/ static int file_nopwrite(disk_t *disk_car, const void *buf, const unsigned int count, const uint64_t offset) { struct info_file_struct *data=(struct info_file_struct *)disk_car->data; @@ -1357,9 +1557,13 @@ static int file_nopwrite(disk_t *disk_car, const void *buf, const unsigned int c return -1; } +/*@ + @ requires \valid(disk_car); + @ requires valid_disk(disk_car); + @*/ static int file_sync(disk_t *disk_car) { -#ifdef HAVE_FSYNC +#if defined(HAVE_FSYNC) struct info_file_struct *data=(struct info_file_struct *)disk_car->data; return fsync(data->handle); #else @@ -1368,10 +1572,28 @@ static int file_sync(disk_t *disk_car) #endif } +/*@ + @ requires \valid(disk); + @ requires valid_disk(disk); + @ requires 0 < disk->geom.heads_per_cylinder <= 255; + @ requires 0 < disk->geom.sectors_per_head <= 63; + @ requires \valid_read(buffer + (0 .. DEFAULT_SECTOR_SIZE-1)); + @ requires separation: \separated(disk, buffer + (0 .. DEFAULT_SECTOR_SIZE-1)); + @ decreases 0; + @ ensures valid_disk(disk); + @*/ +// assigns disk->geom.cylinders; +// assigns disk->geom.heads_per_cylinder; +// assigns disk->geom.sectors_per_head; +// assigns disk->geom.bytes_per_sector; +// ensures 0 < disk->geom.heads_per_cylinder <= 255; +// ensures 0 < disk->geom.sectors_per_head <= 63; static void autoset_geometry(disk_t *disk, const unsigned char *buffer, const int verbose) { - if(disk->arch->get_geometry_from_mbr!=NULL) + /*@ assert 0 < disk->sector_size; */ + if((disk->arch)->get_geometry_from_mbr!=NULL) { + /*@ assert \valid_function(disk->arch->get_geometry_from_mbr); */ CHSgeometry_t geometry; geometry.cylinders=0; geometry.heads_per_cylinder=0; @@ -1379,15 +1601,25 @@ static void autoset_geometry(disk_t *disk, const unsigned char *buffer, const in geometry.bytes_per_sector=0; disk->arch->get_geometry_from_mbr(buffer, verbose, &geometry); disk->autodetect=1; - if(geometry.sectors_per_head > 0 && geometry.heads_per_cylinder > 0) + if( geometry.heads_per_cylinder > 0 && + geometry.heads_per_cylinder <= 255 && + geometry.sectors_per_head > 0 && + geometry.sectors_per_head <= 63 + ) { + /*@ assert 0 < geometry.heads_per_cylinder <= 255; */ + /*@ assert 0 < geometry.sectors_per_head <= 63; */ disk->geom.heads_per_cylinder=geometry.heads_per_cylinder; disk->geom.sectors_per_head=geometry.sectors_per_head; + /*@ assert 0 < disk->geom.heads_per_cylinder <= 255; */ + /*@ assert 0 < disk->geom.sectors_per_head <= 63; */ if(geometry.bytes_per_sector!=0) { disk->geom.bytes_per_sector=geometry.bytes_per_sector; disk->sector_size=geometry.bytes_per_sector; + /*@ assert 0 < disk->sector_size; */ } + /*@ assert 0 < disk->sector_size; */ } else { @@ -1395,14 +1627,33 @@ static void autoset_geometry(disk_t *disk, const unsigned char *buffer, const in disk->geom.sectors_per_head=63; } } + /*@ assert 0 < disk->sector_size; */ + /*@ assert 0 < disk->geom.heads_per_cylinder <= 255; */ + /*@ assert 0 < disk->geom.sectors_per_head <= 63; */ /* Round up because file is often truncated. */ disk->geom.cylinders=(disk->disk_size / disk->sector_size + (uint64_t)disk->geom.sectors_per_head * disk->geom.heads_per_cylinder - 1) / disk->geom.sectors_per_head / disk->geom.heads_per_cylinder; } +/*@ + @ requires \valid_read(hdr); + @ assigns \nothing; + @*/ +static int is_dosemu_image(const struct dosemu_image_header *hdr) +{ + if(memcmp(&hdr->sig,"DOSEMU",6)==0 && + 0 < le32(hdr->sectors) && le32(hdr->sectors) <= 63 && + 0 < le32(hdr->heads) && le32(hdr->heads) <= 255 && + 0 < le32(hdr->cylinders) && + 0 < le32(hdr->header_end)) + return 1; + return 0; +} + disk_t *file_test_availability(const char *device, const int verbose, int testdisk_mode) { + /*@ assert valid_read_string(device); */ disk_t *disk_car=NULL; struct stat stat_rec; int device_is_a_file=0; @@ -1525,10 +1776,29 @@ disk_t *file_test_availability(const char *device, const int verbose, int testdi } return NULL; } + /*@ assert 0 <= hd_h; */ +#ifdef DISABLED_FOR_FRAMAC + if(hd_h >= 1024) + return NULL; + /*@ assert 0 <= hd_h < 1024; */ +#endif disk_car=(disk_t *)MALLOC(sizeof(*disk_car)); disk_car->arch=&arch_none; init_disk(disk_car); +#ifdef DISABLED_FOR_FRAMAC + disk_car->device=(char *)MALLOC(2048); + strncpy(disk_car->device, device, 2048); + disk_car->device[2048-1]='\0'; +#else disk_car->device=strdup(device); +#endif + if(disk_car->device==NULL) + { + free(disk_car); + close(hd_h); + return NULL; + } + /*@ assert valid_read_string(disk_car->device); */ data=(struct info_file_struct *)MALLOC(sizeof(*data)); data->handle=hd_h; data->mode=mode; @@ -1539,22 +1809,27 @@ disk_t *file_test_availability(const char *device, const int verbose, int testdi disk_car->pwrite=((mode&O_RDWR)==O_RDWR?&file_pwrite:&file_nopwrite); disk_car->sync=&file_sync; disk_car->access_mode=((mode&O_RDWR)==O_RDWR?TESTDISK_O_RDWR:TESTDISK_O_RDONLY); + disk_car->model=NULL; #ifdef O_DIRECT if((mode&O_DIRECT)==O_DIRECT) disk_car->access_mode|=TESTDISK_O_DIRECT; #endif disk_car->clean=&file_clean; +#if !defined(DISABLED_FOR_FRAMAC) if(fstat(hd_h,&stat_rec)>=0 && S_ISREG(stat_rec.st_mode) && stat_rec.st_size > 0) +#endif { device_is_a_file=1; } #ifndef DJGPP if(device_is_a_file==0) { +#ifndef DISABLED_FOR_FRAMAC if(verbose>1) log_info("file_test_availability %s is a device\n", device); +#endif disk_car->sector_size=disk_get_sector_size(hd_h, device, verbose); disk_get_geometry(&disk_car->geom, hd_h, device, verbose); disk_car->disk_real_size=disk_get_size(hd_h, device, verbose, disk_car->sector_size); @@ -1576,8 +1851,11 @@ disk_t *file_test_availability(const char *device, const int verbose, int testdi unsigned char *buffer; const struct tdewf_file_header *ewf; const uint8_t evf_file_signature[8] = { 'E', 'V', 'F', 0x09, 0x0D, 0x0A, 0xFF, 0x00 }; + const struct dosemu_image_header *hdr; +#ifndef DISABLED_FOR_FRAMAC if(verbose>1) log_verbose("file_test_availability %s is a file\n", device); +#endif disk_car->sector_size=DEFAULT_SECTOR_SIZE; buffer=(unsigned char*)MALLOC(DEFAULT_SECTOR_SIZE); ewf=(const struct tdewf_file_header *)buffer; @@ -1585,14 +1863,22 @@ disk_t *file_test_availability(const char *device, const int verbose, int testdi { memset(buffer,0,DEFAULT_SECTOR_SIZE); } - if(memcmp(buffer,"DOSEMU",6)==0 && *(unsigned long*)(buffer+11)>0) +#ifdef __FRAMAC__ + Frama_C_make_unknown((char *)buffer, DEFAULT_SECTOR_SIZE); +#endif +#ifndef DISABLED_FOR_FRAMAC + hdr=(const struct dosemu_image_header *)buffer; + if(is_dosemu_image(hdr)) { log_info("%s DOSEMU\n",device); - disk_car->geom.cylinders=*(unsigned long*)(buffer+15); - disk_car->geom.heads_per_cylinder=*(unsigned long*)(buffer+7); - disk_car->geom.sectors_per_head=*(unsigned long*)(buffer+11); + disk_car->geom.cylinders=le32(hdr->cylinders); + /*@ assert 0 < disk_car->geom.cylinders < 4294967296; */ + disk_car->geom.heads_per_cylinder=le32(hdr->heads); + /*@ assert 0 < disk_car->geom.heads_per_cylinder <= 255; */ + disk_car->geom.sectors_per_head=le32(hdr->sectors); + /*@ assert 0 < disk_car->geom.sectors_per_head <= 63; */ disk_car->disk_real_size=(uint64_t)disk_car->geom.cylinders * disk_car->geom.heads_per_cylinder * disk_car->geom.sectors_per_head * disk_car->sector_size; - disk_car->offset=*(unsigned long*)(buffer+19); + disk_car->offset=le32(hdr->header_end); } else if(memcmp(buffer, evf_file_signature, 8)==0 && le16(ewf->fields_segment)==1) { @@ -1611,6 +1897,7 @@ disk_t *file_test_availability(const char *device, const int verbose, int testdi #endif } else +#endif { disk_car->geom.cylinders=0; disk_car->geom.heads_per_cylinder=255; @@ -1626,18 +1913,20 @@ disk_t *file_test_availability(const char *device, const int verbose, int testdi off_t pos; pos=lseek(hd_h,0,SEEK_END); if(pos>0 && (uint64_t)pos > disk_car->offset) + { + /*@ assert pos > disk_car->offset; */ disk_car->disk_real_size=(uint64_t)pos-disk_car->offset; + } else disk_car->disk_real_size=0; } +#ifndef DISABLED_FOR_FRAMAC autoset_geometry(disk_car,buffer,verbose); +#endif } free(buffer); } update_disk_car_fields(disk_car); -#if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE) -// posix_fadvise(hd_h,0,0,POSIX_FADV_SEQUENTIAL); -#endif if(disk_car->disk_real_size!=0) { #ifdef HDCLONE @@ -1653,14 +1942,22 @@ disk_t *file_test_availability(const char *device, const int verbose, int testdi free(new_file); } #endif + /*@ assert 0 < disk_car->geom.cylinders < 0x2000000000000; */ + /*@ assert 0 < disk_car->geom.heads_per_cylinder <= 255; */ + /*@ assert 0 < disk_car->geom.sectors_per_head <= 63; */ + /*@ assert valid_read_string(disk_car->device); */ + /*@ assert valid_disk(disk_car); */ return disk_car; } + /*@ assert disk_car->description == &file_description; */ +#ifndef DISABLED_FOR_FRAMAC if(disk_car->model==NULL) log_warning("Warning: can't get size for %s, sector size=%u\n", - disk_car->description(disk_car), disk_car->sector_size); + file_description(disk_car), disk_car->sector_size); else log_warning("Warning: can't get size for %s, sector size=%u - %s\n", - disk_car->description(disk_car), disk_car->sector_size, disk_car->model); + file_description(disk_car), disk_car->sector_size, disk_car->model); +#endif free(data); free(disk_car->device); free(disk_car->model); diff --git a/src/hdcache.c b/src/hdcache.c index af66339d..f53f0c6d 100644 --- a/src/hdcache.c +++ b/src/hdcache.c @@ -19,6 +19,7 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#if !defined(DISABLED_FOR_FRAMAC) #ifdef HAVE_CONFIG_H #include #endif @@ -193,6 +194,7 @@ static int cache_pread(disk_t *disk_car, void *buffer, const unsigned int count, @ requires valid_disk(disk_car); @ requires \valid_read((char *)buffer + (0 .. count-1)); @ requires separation: \separated(disk_car, (const char *)buffer + (0 .. count-1)); + @ decreases 0; @*/ static int cache_pwrite(disk_t *disk_car, const void *buffer, const unsigned int count, const uint64_t offset) { @@ -214,6 +216,7 @@ static int cache_pwrite(disk_t *disk_car, const void *buffer, const unsigned int /*@ @ requires \valid(disk_car); @ requires valid_disk(disk_car); + @ decreases 0; @*/ static void cache_clean(disk_t *disk_car) { @@ -241,6 +244,7 @@ static void cache_clean(disk_t *disk_car) /*@ @ requires \valid(disk_car); + @ decreases 0; @*/ static int cache_sync(disk_t *disk_car) { @@ -267,6 +271,7 @@ static void dup_geometry(CHSgeometry_t * CHS_dst, const CHSgeometry_t * CHS_sour /*@ @ requires \valid(disk_car); @ requires valid_disk(disk_car); + @ decreases 0; @ ensures valid_read_string(\result); @*/ static const char *cache_description(disk_t *disk_car) @@ -283,6 +288,7 @@ static const char *cache_description(disk_t *disk_car) /*@ @ requires \valid(disk_car); @ requires valid_disk(disk_car); + @ decreases 0; @ ensures valid_read_string(\result); @*/ static const char *cache_description_short(disk_t *disk_car) @@ -339,3 +345,4 @@ disk_t *new_diskcache(disk_t *disk_car, const unsigned int testdisk_mode) } return new_disk_car; } +#endif diff --git a/src/hdcache.h b/src/hdcache.h index d263f4bc..767bec41 100644 --- a/src/hdcache.h +++ b/src/hdcache.h @@ -24,6 +24,7 @@ #ifdef __cplusplus extern "C" { #endif +#if !defined(DISABLED_FOR_FRAMAC) /*@ @ requires \valid(disk_car); @@ -32,6 +33,7 @@ extern "C" { @*/ disk_t *new_diskcache(disk_t *disk_car, const unsigned int cache_size_min); +#endif #ifdef __cplusplus } /* closing brace for extern "C" */ #endif diff --git a/src/hidden.c b/src/hidden.c index de2940fd..ef1dcc10 100644 --- a/src/hidden.c +++ b/src/hidden.c @@ -40,14 +40,14 @@ int is_hpa_or_dco(const disk_t *disk) } else if(disk->dco > 0 && disk->user_max < disk->dco+1) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("user_max=%llu dco=%llu\n", (long long unsigned) disk->user_max, (long long unsigned) disk->dco); #endif res|=2; } -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(res>0) { if(res&1) diff --git a/src/hiddenn.c b/src/hiddenn.c index 4fba95dc..1b389513 100644 --- a/src/hiddenn.c +++ b/src/hiddenn.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/hpa_dco.c b/src/hpa_dco.c index a11c5b14..42d27001 100644 --- a/src/hpa_dco.c +++ b/src/hpa_dco.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LINUX_TYPES_H #undef HAVE_LINUX_HDREG_H #undef HAVE_SCSI_SG_H diff --git a/src/intrf.c b/src/intrf.c index fdfddb77..180b18d6 100644 --- a/src/intrf.c +++ b/src/intrf.c @@ -102,6 +102,11 @@ int screen_buffer_add(const char *_format, ...) return 0; } +/*@ + @ ensures intr_nbr_line == 0; + @ assigns intr_nbr_line; + @ assigns intr_buffer_screen[0 .. MAX_LINES-1][ 0 .. BUFFER_LINE_LENGTH]; + @*/ void screen_buffer_reset(void) { intr_nbr_line=0; @@ -145,6 +150,10 @@ const char *aff_part_aux(const unsigned int newline, const disk_t *disk_car, con return msg; } msg[sizeof(msg)-1]=0; +#ifdef DISABLED_FOR_FRAMAC + msg[0]='T'; + msg[1]='\0'; +#else if((newline&AFF_PART_ORDER)==AFF_PART_ORDER) { if(partition->status!=STATUS_EXT_IN_EXT && partition->order!=NO_ORDER) @@ -189,6 +198,7 @@ const char *aff_part_aux(const unsigned int newline, const disk_t *disk_car, con pos+=snprintf(&msg[pos],sizeof(msg)-pos-1, " [%s]",partition->partname); if(partition->fsname[0]!='\0') snprintf(&msg[pos],sizeof(msg)-pos-1, " [%s]",partition->fsname); +#endif return msg; } @@ -221,7 +231,7 @@ uint64_t ask_number_cli(char **current_cmd, const uint64_t val_cur, const uint64 /*@ assert valid_read_string(*current_cmd); */ if (val_min==val_max || (tmp_val >= val_min && tmp_val <= val_max)) return tmp_val; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC else { char res[200]; diff --git a/src/intrfn.c b/src/intrfn.c index d8ff4ddb..1c5d45bd 100644 --- a/src/intrfn.c +++ b/src/intrfn.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/intrfn.h b/src/intrfn.h index 9d928b9b..70452639 100644 --- a/src/intrfn.h +++ b/src/intrfn.h @@ -26,7 +26,7 @@ extern "C" { #endif -#if defined(HAVE_NCURSES) && !defined(MAIN_photorec) +#if defined(HAVE_NCURSES) #ifdef HAVE_NCURSES_H #include #elif defined(HAVE_NCURSESW_NCURSES_H) diff --git a/src/log.c b/src/log.c index 4c4b758e..64364e09 100644 --- a/src/log.c +++ b/src/log.c @@ -47,7 +47,7 @@ #include "common.h" #include "log.h" -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC #undef HAVE_DUP2 #endif @@ -130,7 +130,7 @@ int log_open_default(const char*default_filename, const int mode, int *errsv) #else int log_open_default(const char*default_filename, const int mode, int *errsv) { -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC return log_open(default_filename, mode, errsv); #else char*filename; diff --git a/src/md.c b/src/md.c index f042070e..9ea20306 100644 --- a/src/md.c +++ b/src/md.c @@ -36,12 +36,12 @@ #include "fnctdsk.h" #include "log.h" -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC static int test_MD(const disk_t *disk_car, const struct mdp_superblock_s *sb, const partition_t *partition, const int dump_ind) { if(le32(sb->md_magic)!=(unsigned int)MD_SB_MAGIC) return 1; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("\nRaid magic value at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset), offset2head(disk_car,partition->part_offset), @@ -67,7 +67,7 @@ static int test_MD_be(const disk_t *disk_car, const struct mdp_superblock_s *sb, { if(be32(sb->md_magic)!=(unsigned int)MD_SB_MAGIC) return 1; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("\nRaid magic value at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset), offset2head(disk_car,partition->part_offset), @@ -127,7 +127,7 @@ static void set_MD_info(const struct mdp_superblock_s *sb, partition_t *partitio (unsigned int)le32(sb1->major_version), (unsigned int)le32(sb1->level), (long unsigned)le32(sb1->dev_number)); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(le32(sb1->max_dev) <= 384) { unsigned int i,d; @@ -151,7 +151,7 @@ static void set_MD_info(const struct mdp_superblock_s *sb, partition_t *partitio } #endif } -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(verbose>0) log_info("%s %s\n", partition->fsname, partition->info); #endif @@ -195,7 +195,7 @@ static void set_MD_info_be(const struct mdp_superblock_s *sb, partition_t *parti (unsigned int)be32(sb1->major_version), (unsigned int)be32(sb1->level), (long unsigned)be32(sb1->dev_number)); -#if !defined(__FRAMAC__) +#if !defined(DISABLED_FOR_FRAMAC) if(be32(sb1->max_dev) <= 384) { unsigned int i,d; @@ -219,7 +219,7 @@ static void set_MD_info_be(const struct mdp_superblock_s *sb, partition_t *parti } #endif } -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(verbose>0) log_info("%s %s\n", partition->fsname, partition->info); #endif @@ -228,7 +228,7 @@ static void set_MD_info_be(const struct mdp_superblock_s *sb, partition_t *parti int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC unsigned char *buffer=(unsigned char*)MALLOC(MD_SB_BYTES); /* MD version 1.1 */ if(disk_car->pread(disk_car, buffer, MD_SB_BYTES, partition->part_offset) == MD_SB_BYTES) @@ -239,7 +239,7 @@ int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) le64(sb1->super_offset)==0 && test_MD(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("check_MD 1.1\n"); #endif set_MD_info((struct mdp_superblock_s*)buffer, partition, verbose); @@ -251,7 +251,7 @@ int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) be64(sb1->super_offset)==0 && test_MD_be(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("check_MD 1.1 (BigEndian)\n"); #endif set_MD_info_be((struct mdp_superblock_s*)buffer, partition, verbose); @@ -268,7 +268,7 @@ int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) le64(sb1->super_offset)==8 && test_MD(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("check_MD 1.2\n"); #endif set_MD_info((struct mdp_superblock_s*)buffer, partition, verbose); @@ -280,7 +280,7 @@ int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) be64(sb1->super_offset)==8 && test_MD_be(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("check_MD 1.2 (BigEndian)\n"); #endif set_MD_info_be((struct mdp_superblock_s*)buffer, partition, verbose); @@ -292,7 +292,7 @@ int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) { const struct mdp_superblock_s *sb=(const struct mdp_superblock_s *)buffer; const uint64_t offset=MD_NEW_SIZE_SECTORS(partition->part_size/512)*512; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(verbose>1) { log_verbose("Raid md 0.90 offset %llu\n", (long long unsigned)offset/512); @@ -304,7 +304,7 @@ int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) le32(sb->major_version)==0 && test_MD(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("check_MD 0.90\n"); #endif set_MD_info((struct mdp_superblock_s*)buffer, partition, verbose); @@ -315,7 +315,7 @@ int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) be32(sb->major_version)==0 && test_MD_be(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("check_MD 0.90 (BigEndian)\n"); #endif set_MD_info_be((struct mdp_superblock_s*)buffer, partition, verbose); @@ -328,7 +328,7 @@ int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) if(partition->part_size > 8*2*512) { const uint64_t offset=(uint64_t)(((partition->part_size/512)-8*2) & ~(4*2-1))*512; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(verbose>1) { log_verbose("Raid md 1.0 offset %llu\n", (long long unsigned)offset/512); @@ -342,7 +342,7 @@ int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) le64(sb1->super_offset)==(offset/512) && test_MD(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("check_MD 1.0\n"); #endif set_MD_info((struct mdp_superblock_s*)buffer, partition, verbose); @@ -354,7 +354,7 @@ int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) be64(sb1->super_offset)==(offset/512) && test_MD_be(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("check_MD 1.0 (BigEndian)\n"); #endif set_MD_info_be((struct mdp_superblock_s*)buffer, partition, verbose); @@ -370,7 +370,7 @@ int check_MD(disk_t *disk_car, partition_t *partition, const int verbose) int recover_MD_from_partition(disk_t *disk_car, partition_t *partition, const int verbose) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC unsigned char *buffer=(unsigned char*)MALLOC(MD_SB_BYTES); /* MD version 0.90 */ { @@ -408,7 +408,7 @@ int recover_MD_from_partition(disk_t *disk_car, partition_t *partition, const in int recover_MD(const disk_t *disk_car, const struct mdp_superblock_s *sb, partition_t *partition, const int verbose, const int dump_ind) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(test_MD(disk_car, sb, partition, dump_ind)==0) { set_MD_info(sb, partition, verbose); diff --git a/src/misc.c b/src/misc.c index 4b2470d1..0d01628f 100644 --- a/src/misc.c +++ b/src/misc.c @@ -24,7 +24,7 @@ #include #endif -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_STRPTIME #undef HAVE_SYS_UTSNAME_H #undef HAVE_UNAME @@ -233,6 +233,10 @@ https://msdn.microsoft.com/en-us/library/windows/desktop/ms724834%28v=vs.85%29.a return "Apple"; #elif defined(__OS2__) return "OS2"; +#elif defined(DISABLED_FOR_FRAMAC) + const char *res="Frama-C"; + /*@ assert valid_read_string(res); */ + return res; #else return "unknown"; #endif @@ -286,6 +290,8 @@ const char *get_compiler(void) #else return "unknown compiler"; #endif + buffer[99]='\0'; + /*@ assert valid_read_string(&buffer[0]); */ return buffer; } diff --git a/src/nodisk.c b/src/nodisk.c index 4b31ade1..b19210ff 100644 --- a/src/nodisk.c +++ b/src/nodisk.c @@ -23,7 +23,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/ntfs_dir.c b/src/ntfs_dir.c index a544f35b..9a11e1b8 100644 --- a/src/ntfs_dir.c +++ b/src/ntfs_dir.c @@ -27,7 +27,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBNTFS #undef HAVE_LIBNTFS3G #undef HAVE_SYS_PARAM_H diff --git a/src/ntfs_io.c b/src/ntfs_io.c index fa056724..7e60e2c4 100644 --- a/src/ntfs_io.c +++ b/src/ntfs_io.c @@ -26,7 +26,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBNTFS #undef HAVE_LIBNTFS3G #endif diff --git a/src/ntfs_udl.c b/src/ntfs_udl.c index 39d0ca94..ac99b4f0 100644 --- a/src/ntfs_udl.c +++ b/src/ntfs_udl.c @@ -28,7 +28,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBNTFS #undef HAVE_LIBNTFS3G #endif diff --git a/src/ntfs_utl.c b/src/ntfs_utl.c index 438ae504..adbcbd45 100644 --- a/src/ntfs_utl.c +++ b/src/ntfs_utl.c @@ -27,7 +27,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBNTFS #undef HAVE_LIBNTFS3G #endif diff --git a/src/ntfsp.c b/src/ntfsp.c index 86330c7e..e2cbd98c 100644 --- a/src/ntfsp.c +++ b/src/ntfsp.c @@ -24,7 +24,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBNTFS #undef HAVE_LIBNTFS3G #endif diff --git a/src/partauto.c b/src/partauto.c index d2b5adc0..792edae2 100644 --- a/src/partauto.c +++ b/src/partauto.c @@ -34,13 +34,25 @@ #include "partauto.h" #include "log.h" -extern const arch_fnct_t arch_gpt; -extern const arch_fnct_t arch_humax; -extern const arch_fnct_t arch_i386; -extern const arch_fnct_t arch_mac; extern const arch_fnct_t arch_none; +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_GPT) +extern const arch_fnct_t arch_gpt; +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_HUMAX) +extern const arch_fnct_t arch_humax; +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_I386) +extern const arch_fnct_t arch_i386; +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_MAC) +extern const arch_fnct_t arch_mac; +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_SUN) extern const arch_fnct_t arch_sun; +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_XBOX) extern const arch_fnct_t arch_xbox; +#endif void autodetect_arch(disk_t *disk, const arch_fnct_t *arch) { @@ -55,42 +67,61 @@ void autodetect_arch(disk_t *disk, const arch_fnct_t *arch) { disk->arch=&arch_none; list_part=arch_none.read_part(disk,verbose,0); + /*@ assert valid_list_part(list_part); */ if(list_part!=NULL && list_part->part!=NULL && list_part->part->upart_type==UP_UNK) { part_free_list(list_part); list_part=NULL; } } +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_XBOX) if(list_part==NULL) { disk->arch=&arch_xbox; list_part=arch_xbox.read_part(disk,verbose,0); + /*@ assert valid_list_part(list_part); */ } +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_GPT) if(list_part==NULL) { disk->arch=&arch_gpt; list_part=arch_gpt.read_part(disk,verbose,0); + /*@ assert valid_list_part(list_part); */ } +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_HUMAX) if(list_part==NULL) { disk->arch=&arch_humax; list_part=arch_humax.read_part(disk,verbose,0); + /*@ assert valid_list_part(list_part); */ } +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_I386) if(list_part==NULL) { disk->arch=&arch_i386; list_part=arch_i386.read_part(disk,verbose,0); + /*@ assert valid_list_part(list_part); */ } +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_SUN) if(list_part==NULL) { disk->arch=&arch_sun; list_part=arch_sun.read_part(disk,verbose,0); + /*@ assert valid_list_part(list_part); */ } +#endif +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_MAC) if(list_part==NULL) { disk->arch=&arch_mac; list_part=arch_mac.read_part(disk,verbose,0); + /*@ assert valid_list_part(list_part); */ } +#endif #ifndef DEBUG_PARTAUTO log_set_levels(old_levels); #endif @@ -108,25 +139,29 @@ void autodetect_arch(disk_t *disk, const arch_fnct_t *arch) } else { -#ifdef TARGET_SOLARIS +#ifdef DISABLED_FOR_FRAMAC + disk->arch=&arch_none; +#elif defined(TARGET_SOLARIS) && (!defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_SUN)) disk->arch=&arch_sun; -#elif defined __APPLE__ -#ifdef TESTDISK_LSB +#elif defined(__APPLE__) && defined(TESTDISK_LSB) && (!defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_GPT)) disk->arch=&arch_gpt; -#else +#elif defined(__APPLE__) && !defined(TESTDISK_LSB) && (!defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_MAC)) disk->arch=&arch_mac; -#endif #else #if defined(__CYGWIN__) || defined(__MINGW32__) if(disk->device[0]=='\\' && disk->device[1]=='\\' && disk->device[2]=='.' && disk->device[3]=='\\' && disk->device[5]==':') disk->arch=&arch_none; else #endif +#if !defined(SINGLE_PARTITION_TYPE) || (defined(SINGLE_PARTITION_I386) && defined(SINGLE_PARTITION_GPT)) /* PC/Intel partition table is limited to 2 TB, 2^32 512-bytes sectors */ if(disk->disk_size < ((uint64_t)1<<(32+9))) disk->arch=&arch_i386; else disk->arch=&arch_gpt; +#else + disk->arch=&arch_none; +#endif #endif } log_info("Partition table type defaults to %s\n", disk->arch->part_name); diff --git a/src/partgpt.c b/src/partgpt.c index e6cb461f..cd3f3278 100644 --- a/src/partgpt.c +++ b/src/partgpt.c @@ -25,7 +25,7 @@ #include #endif -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_SYS_UUID_H #undef HAVE_UUID_H #undef HAVE_UUID_UUID_H @@ -595,7 +595,7 @@ static const char *get_gpt_typename(const efi_guid_t part_type_gpt) for(i=0; gpt_sys_types[i].name!=NULL; i++) if(guid_cmp(gpt_sys_types[i].part_type, part_type_gpt)==0) return gpt_sys_types[i].name; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("%8x %04x %04x %02x %02x %02x %02x %02x %02x %02x %02x\n", part_type_gpt.time_low, part_type_gpt.time_mid, diff --git a/src/partgptn.c b/src/partgptn.c index f58de0ce..1e95902e 100644 --- a/src/partgptn.c +++ b/src/partgptn.c @@ -25,7 +25,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/parti386.c b/src/parti386.c index 1ddb5de7..2c097a16 100644 --- a/src/parti386.c +++ b/src/parti386.c @@ -21,6 +21,7 @@ */ +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_I386) #ifdef HAVE_CONFIG_H #include #endif @@ -68,7 +69,6 @@ static int is_extended(const unsigned int part_type); /*@ @ requires list_part == \null || \valid_read(list_part); - @ assigns \nothing; @*/ static int test_structure_i386(const list_part_t *list_part); @@ -104,8 +104,8 @@ static void log_dos_entry(const struct partition_dos*); @ requires geometry->cylinders==0; @ requires geometry->heads_per_cylinder==0; @ requires geometry->sectors_per_head==0; - @ assigns geometry->sectors_per_head, geometry->heads_per_cylinder, geometry->bytes_per_sector; @*/ +// assigns geometry->sectors_per_head, geometry->heads_per_cylinder, geometry->bytes_per_sector; static int get_geometry_from_i386mbr(const unsigned char *buffer, const int verbose, CHSgeometry_t *geometry); /*@ @@ -446,7 +446,7 @@ static void set_start_sect(struct partition_dos *p, unsigned int start_sect) static int get_geometry_from_i386mbr(const unsigned char *buffer, const int verbose, CHSgeometry_t *geometry) { unsigned int i; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(verbose>1) { log_trace("get_geometry_from_i386mbr\n"); @@ -479,14 +479,14 @@ static int get_geometry_from_i386mbr(const unsigned char *buffer, const int verb geometry->heads_per_cylinder==240 || geometry->heads_per_cylinder==255))) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("Geometry from i386 MBR: head=%u sector=%u\n", geometry->heads_per_cylinder, geometry->sectors_per_head); #endif } else { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(geometry->sectors_per_head>0) log_warning("Geometry from i386 MBR: head=%u sector=%u\n",geometry->heads_per_cylinder, geometry->sectors_per_head); #endif @@ -1855,3 +1855,4 @@ static const char *get_partition_typename_i386(const partition_t *partition) { return get_partition_typename_i386_aux(partition->part_type_i386); } +#endif diff --git a/src/parti386n.c b/src/parti386n.c index f6d84ada..b6d7c799 100644 --- a/src/parti386n.c +++ b/src/parti386n.c @@ -25,7 +25,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/partmacn.c b/src/partmacn.c index 61e2ce3f..9b7fed7d 100644 --- a/src/partmacn.c +++ b/src/partmacn.c @@ -25,7 +25,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/partnone.c b/src/partnone.c index 4a59825c..406b7934 100644 --- a/src/partnone.c +++ b/src/partnone.c @@ -40,7 +40,7 @@ #include "lang.h" #include "intrf.h" #include "fat_common.h" -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) #include "apfs.h" #include "bfs.h" #include "bsd.h" @@ -263,7 +263,7 @@ static list_part_t *read_part_none(disk_t *disk, const int verbose, const int sa partition=partition_new(&arch_none); buffer_disk=(unsigned char *)MALLOC(16*DEFAULT_SECTOR_SIZE); partition->part_size=disk->disk_size; -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) if(recover_MD_from_partition(disk, partition, verbose)==0) res=1; else @@ -358,10 +358,11 @@ static list_part_t *read_part_none(disk_t *disk, const int verbose, const int sa partition->status=STATUS_PRIM; screen_buffer_reset(); check_part_none(disk, verbose,partition,saveheader); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC aff_part_buffer(AFF_PART_ORDER|AFF_PART_STATUS,disk, partition); #endif list_part=insert_new_partition(NULL, partition, 0, &insert_error); + /*@ assert valid_list_part(list_part); */ if(insert_error>0) free(partition); return list_part; @@ -405,7 +406,7 @@ static void init_structure_none(const disk_t *disk_car,list_part_t *list_part, c static int check_part_none(disk_t *disk_car,const int verbose,partition_t *partition, const int saveheader) { int ret=0; -#if !defined(__FRAMAC__) && !defined(MAIN_photorec) +#if !defined(DISABLED_FOR_FRAMAC) switch(partition->upart_type) { case UP_APFS: diff --git a/src/partsun.c b/src/partsun.c index b8fbb4d3..eeed60ee 100644 --- a/src/partsun.c +++ b/src/partsun.c @@ -20,7 +20,7 @@ */ - +#if !defined(SINGLE_PARTITION_TYPE) || defined(SINGLE_PARTITION_SUN) #ifdef HAVE_CONFIG_H #include #endif @@ -176,7 +176,7 @@ static unsigned int get_part_type_sun(const partition_t *partition) static int get_geometry_from_sunmbr(const unsigned char *buffer, const int verbose, CHSgeometry_t *geometry) { const sun_disklabel *sunlabel=(const sun_disklabel*)buffer; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(verbose>1) { log_trace("get_geometry_from_sunmbr\n"); @@ -185,7 +185,7 @@ static int get_geometry_from_sunmbr(const unsigned char *buffer, const int verbo geometry->cylinders=0; geometry->heads_per_cylinder=be16(sunlabel->ntrks); geometry->sectors_per_head=be16(sunlabel->nsect); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(geometry->sectors_per_head>0) { log_info("Geometry from SUN MBR: head=%u sector=%u\n", @@ -475,3 +475,4 @@ static const char *get_partition_typename_sun(const partition_t *partition) { return get_partition_typename_sun_aux(partition->part_type_sun); } +#endif diff --git a/src/partsunn.c b/src/partsunn.c index 43f1e17e..77f20d8e 100644 --- a/src/partsunn.c +++ b/src/partsunn.c @@ -25,7 +25,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/partxboxn.c b/src/partxboxn.c index 83468e24..caa3e419 100644 --- a/src/partxboxn.c +++ b/src/partxboxn.c @@ -25,7 +25,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/pbanner.c b/src/pbanner.c index d51d4c81..02a7aaba 100644 --- a/src/pbanner.c +++ b/src/pbanner.c @@ -24,7 +24,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/pdiskseln.c b/src/pdiskseln.c index b0351f4b..7da7a7f3 100644 --- a/src/pdiskseln.c +++ b/src/pdiskseln.c @@ -28,7 +28,7 @@ #undef SUDO_BIN #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif @@ -237,7 +237,7 @@ int do_curses_photorec(struct ph_param *params, struct ph_options *options, cons .list = TD_LIST_HEAD_INIT(list_search_space.list) }; const int resume_session=(params->cmd_device!=NULL && strcmp(params->cmd_device,"resume")==0); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(params->cmd_device==NULL || resume_session!=0) { char *saved_device=NULL; diff --git a/src/phbf.c b/src/phbf.c index 0c7a9165..dd2f7c11 100644 --- a/src/phbf.c +++ b/src/phbf.c @@ -20,6 +20,7 @@ */ +#if !defined(DISABLED_FOR_FRAMAC) #ifdef HAVE_CONFIG_H #include #endif @@ -180,6 +181,7 @@ pstatus_t photorec_bf(struct ph_param *params, const struct ph_options *options, td_list_for_each(tmp, &pos->file_checks[buffer[pos->offset]].list) { const file_check_t *file_check=td_list_entry_const(tmp, const file_check_t, list); + /*@ assert valid_file_check_node(file_check); */ if((file_check->length==0 || memcmp(buffer + file_check->offset, file_check->value, file_check->length)==0) && file_check->header_check(buffer, read_size, 0, &file_recovery, &file_recovery_new)!=0) { @@ -751,3 +753,4 @@ static pstatus_t photorec_bf_aux(struct ph_param *params, file_recovery_t *file_ return PSTATUS_OK; } } +#endif diff --git a/src/phbf.h b/src/phbf.h index 765d07ee..e1ca285b 100644 --- a/src/phbf.h +++ b/src/phbf.h @@ -24,9 +24,11 @@ #ifdef __cplusplus extern "C" { #endif +#if !defined(DISABLED_FOR_FRAMAC) pstatus_t photorec_bf(struct ph_param *params, const struct ph_options *options, alloc_data_t *list_search_space); +#endif #ifdef __cplusplus } /* closing brace for extern "C" */ #endif diff --git a/src/phbs.c b/src/phbs.c index 269afed1..9364041b 100644 --- a/src/phbs.c +++ b/src/phbs.c @@ -54,7 +54,10 @@ #include "file_found.h" #define READ_SIZE 1024*512 + +#if !defined(SINGLE_FORMAT) || defined(SINGLE_FORMAT_tar) extern const file_hint_t file_hint_tar; +#endif extern file_check_list_t file_check_list; extern int need_to_stop; @@ -81,9 +84,10 @@ pstatus_t photorec_find_blocksize(struct ph_param *params, const struct ph_optio unsigned int buffer_size; const unsigned int blocksize=params->blocksize; const unsigned int read_size=(blocksize>65536?blocksize:65536); + /*@ assert read_size >= 65536; */ alloc_data_t *current_search_space; file_recovery_t file_recovery; - +#ifndef DISABLED_FOR_FRAMAC params->file_nbr=0; reset_file_recovery(&file_recovery); file_recovery.blocksize=blocksize; @@ -107,11 +111,13 @@ pstatus_t photorec_find_blocksize(struct ph_param *params, const struct ph_optio file_recovery_t file_recovery_new; file_recovery_new.blocksize=blocksize; file_recovery_new.location.start=offset; +#if !defined(SINGLE_FORMAT) || defined(SINGLE_FORMAT_tar) if(file_recovery.file_stat!=NULL && file_recovery.file_stat->file_hint==&file_hint_tar && is_valid_tar_header((const struct tar_posix_header *)(buffer-0x200))) { /* Currently saving a tar, do not check the data for know header */ } else +#endif { const struct td_list_head *tmpl; file_recovery_new.file_stat=NULL; @@ -122,6 +128,7 @@ pstatus_t photorec_find_blocksize(struct ph_param *params, const struct ph_optio td_list_for_each(tmp, &pos->file_checks[buffer[pos->offset]].list) { const file_check_t *file_check=td_list_entry_const(tmp, const file_check_t, list); + /*@ assert valid_file_check_node(file_check); */ if((file_check->length==0 || memcmp(buffer + file_check->offset, file_check->value, file_check->length)==0) && file_check->header_check(buffer, read_size, 1, &file_recovery, &file_recovery_new)!=0) { @@ -217,5 +224,6 @@ pstatus_t photorec_find_blocksize(struct ph_param *params, const struct ph_optio } } /* end while(current_search_space!=list_search_space) */ free(buffer_start); +#endif return PSTATUS_OK; } diff --git a/src/phcfg.c b/src/phcfg.c index f5f4853c..9aed55ca 100644 --- a/src/phcfg.c +++ b/src/phcfg.c @@ -81,7 +81,7 @@ static FILE *file_options_save_aux(void) } } #endif -#if !defined(DJGPP) && !defined(__FRAMAC__) +#if !defined(DJGPP) && !defined(DISABLED_FOR_FRAMAC) if(filename==NULL) { char *home; @@ -145,7 +145,7 @@ static FILE *file_options_load_aux(void) } } #endif -#if !defined(DJGPP) && !defined(__FRAMAC__) +#if !defined(DJGPP) && !defined(DISABLED_FOR_FRAMAC) { char *home; home = getenv("HOME"); diff --git a/src/phcli.c b/src/phcli.c index f05a89e7..e8830663 100644 --- a/src/phcli.c +++ b/src/phcli.c @@ -19,6 +19,8 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +extern int need_to_stop; + #ifdef HAVE_CONFIG_H #include #endif @@ -41,7 +43,6 @@ #include "poptions.h" #include "phcli.h" -extern int need_to_stop; typedef enum { INIT_SPACE_WHOLE, INIT_SPACE_PREINIT, INIT_SPACE_EXT2_GROUP, INIT_SPACE_EXT2_INODE } init_mode_t; /*@ @@ -60,10 +61,18 @@ static int spacerange_cmp(const struct td_list_head *a, const struct td_list_hea return space_a->end - space_b->end; } +#ifndef DISABLED_FOR_FRAMAC +/*@ + @ requires \valid(files_enable); + @ requires valid_read_string(*current_cmd); + @ requires \separated(files_enable, current_cmd, *current_cmd); + @ ensures valid_read_string(*current_cmd); + @*/ static int file_select_cli(file_enable_t *files_enable, char**current_cmd) { int keep_asking; log_info("\nInterface File Select\n"); + /*@ loop invariant valid_read_string(*current_cmd); */ do { file_enable_t *file_enable; @@ -131,12 +140,20 @@ static int file_select_cli(file_enable_t *files_enable, char**current_cmd) } while(keep_asking>0); return 0; } +#endif int menu_photorec_cli(list_part_t *list_part, struct ph_param *params, struct ph_options *options, alloc_data_t*list_search_space) { unsigned int user_blocksize=0; init_mode_t mode_init_space=(td_list_empty(&list_search_space->list)?INIT_SPACE_WHOLE:INIT_SPACE_PREINIT); params->partition=(list_part->next!=NULL ? list_part->next->part : list_part->part); + /*@ assert valid_partition(params->partition); */ + /*@ + @ loop invariant valid_read_string(params->cmd_run); + @ loop invariant \valid_function(params->disk->description); + @ loop invariant valid_list_search_space(list_search_space); + @ loop invariant valid_disk(params->disk); + @*/ while(1) { skip_comma_in_command(¶ms->cmd_run); @@ -146,6 +163,7 @@ int menu_photorec_cli(list_part_t *list_part, struct ph_param *params, struct ph return 0; if(check_command(¶ms->cmd_run,"search",6)==0) { +#ifndef DISABLED_FOR_FRAMAC if(mode_init_space==INIT_SPACE_EXT2_GROUP) { params->blocksize=ext2_fix_group(list_search_space, params->disk, params->partition); @@ -164,6 +182,7 @@ int menu_photorec_cli(list_part_t *list_part, struct ph_param *params, struct ph return -1; } } +#endif if(td_list_empty(&list_search_space->list)) { init_search_space(list_search_space, params->disk, params->partition); @@ -176,6 +195,7 @@ int menu_photorec_cli(list_part_t *list_part, struct ph_param *params, struct ph params->blocksize=user_blocksize; return 1; } +#ifndef DISABLED_FOR_FRAMAC else if(check_command(¶ms->cmd_run,"options",7)==0) { interface_options_photorec_cli(options, ¶ms->cmd_run); @@ -193,11 +213,13 @@ int menu_photorec_cli(list_part_t *list_part, struct ph_param *params, struct ph { change_geometry_cli(params->disk, ¶ms->cmd_run); } +#endif else if(check_command(¶ms->cmd_run,"inter",5)==0) { /* Start interactive mode */ params->cmd_run=NULL; return 0; } +#ifndef DISABLED_FOR_FRAMAC else if(check_command(¶ms->cmd_run,"wholespace",10)==0) { params->carve_free_space_only=0; @@ -254,9 +276,12 @@ int menu_photorec_cli(list_part_t *list_part, struct ph_param *params, struct ph if(element!=NULL) params->partition=element->part; } +#endif else { +#ifndef DISABLED_FOR_FRAMAC log_critical("Syntax error in command line: %s\n", params->cmd_run); +#endif return -1; } } diff --git a/src/phmain.c b/src/phmain.c index 9e622ed1..e7a68068 100644 --- a/src/phmain.c +++ b/src/phmain.c @@ -28,7 +28,7 @@ #undef SUDO_BIN #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBEWF #undef HAVE_SIGACTION #undef HAVE_NCURSES @@ -92,8 +92,10 @@ int need_to_stop=0; extern file_enable_t array_file_enable[]; +#ifndef DISABLED_FOR_FRAMAC extern uint64_t gpfh_nbr; extern uint64_t gpls_nbr; +#endif #ifdef HAVE_SIGACTION static struct sigaction action; @@ -131,6 +133,7 @@ static void display_help(void) "in the recup_dir directory.\n"); } +#ifndef DISABLED_FOR_FRAMAC static void display_version(void) { printf("\n"); @@ -143,17 +146,23 @@ static void display_version(void) td_ext2fs_version(), td_ntfs_version(), td_ewf_version(), td_jpeg_version(), td_curses_version(), td_zlib_version()); printf("OS: %s\n" , get_os()); } +#endif int main( int argc, char **argv ) { + list_disk_t *list_disk=NULL; + /*@ assert list_disk == \null; */ + /*@ assert valid_list_disk(list_disk); */ int i; +#ifdef DISABLED_FOR_FRAMAC + char *argv_framac[]={ "photorec", "/cmd", "file.dd", "search", NULL }; +#endif #ifdef SUDO_BIN - int use_sudo; + int use_sudo=0; #endif int create_log=TD_LOG_NONE; int run_setlocale=1; int testdisk_mode=TESTDISK_O_RDONLY|TESTDISK_O_READAHEAD_32K; - list_disk_t *list_disk=NULL; list_disk_t *element_disk; const char *logfile="photorec.log"; int log_opened=0; @@ -170,12 +179,16 @@ int main( int argc, char **argv ) struct ph_param params; if(argc <= 0) return 1; + /*@ assert valid_list_disk(list_disk); */ ; params.recup_dir=NULL; params.cmd_device=NULL; params.cmd_run=NULL; params.carve_free_space_only=0; - /* random (weak is ok) is need fot GPT */ - srand(time(NULL)); + params.disk=NULL; + /*@ assert valid_ph_param(¶ms); */ + /* random (weak is ok) is needed for GPT */ + srand(time(NULL)& (long)0xffffffff); + /*@ assert valid_list_disk(list_disk); */ ; #ifdef HAVE_SIGACTION /* set up the signal handler for SIGINT & SIGHUP */ sigemptyset(&action.sa_mask); @@ -200,9 +213,21 @@ int main( int argc, char **argv ) return -1; } #endif +#ifndef DISABLED_FOR_FRAMAC printf("PhotoRec %s, Data Recovery Utility, %s\nChristophe GRENIER \nhttps://www.cgsecurity.org\n",VERSION,TESTDISKDATE); +#endif +#if defined(DISABLED_FOR_FRAMAC) + argc=4; + argv=argv_framac; +#endif + /*@ assert valid_list_disk(list_disk); */ + /*@ + @ loop unroll 256; + @ loop invariant valid_list_disk(list_disk); + @*/ for(i=1;i=argc) @@ -211,7 +236,9 @@ int main( int argc, char **argv ) free(params.recup_dir); return 1; } + /*@ assert valid_read_string(argv[i+1]); */ logfile=argv[++i]; + /*@ assert valid_read_string(logfile); */ } else if((strcmp(argv[i],"/nolog")==0) ||(strcmp(argv[i],"-nolog")==0)) { @@ -236,9 +263,13 @@ int main( int argc, char **argv ) params.recup_dir=(char *)MALLOC(len + strlen(DEFAULT_RECUP_DIR) + 1); strcpy(params.recup_dir,argv[i+1]); strcat(params.recup_dir,DEFAULT_RECUP_DIR); + /*@ assert \freeable(params.recup_dir); */ } else + { params.recup_dir=strdup(argv[i+1]); + /*@ assert params.recup_dir==\null || \freeable(params.recup_dir); */ + } i++; } else if((strcmp(argv[i],"/all")==0) || (strcmp(argv[i],"-all")==0)) @@ -262,7 +293,9 @@ int main( int argc, char **argv ) } else if((strcmp(argv[i],"/nosetlocale")==0) || (strcmp(argv[i],"-nosetlocale")==0)) run_setlocale=0; - else if(strcmp(argv[i],"/cmd")==0) + else +#endif + if(strcmp(argv[i],"/cmd")==0) { if(i+2>=argc) { @@ -270,29 +303,42 @@ int main( int argc, char **argv ) free(params.recup_dir); return 1; } +#ifndef DISABLED_FOR_FRAMAC if(strcmp(argv[i+1],"resume")==0) { params.cmd_device=argv[++i]; } else +#endif { disk_t *disk_car; params.cmd_device=argv[++i]; +#ifdef DISABLED_FOR_FRAMAC + params.cmd_device="disk.dd"; +#endif params.cmd_run=argv[++i]; /* There is no log currently */ disk_car=file_test_availability(params.cmd_device, options.verbose, testdisk_mode); + /*@ assert disk_car == \null || valid_disk(disk_car); */ if(disk_car==NULL) { printf("\nUnable to open file or device %s: %s\n", params.cmd_device, strerror(errno)); free(params.recup_dir); return 1; } + /*@ assert \valid(disk_car); */ + /*@ assert 0 < disk_car->geom.cylinders < 0x2000000000000 && 0 < disk_car->geom.heads_per_cylinder <= 255 && 0 < disk_car->geom.sectors_per_head <= 63; */ + /*@ assert valid_read_string(disk_car->device); */ list_disk=insert_new_disk(list_disk,disk_car); + /*@ assert list_disk ==\null || (\valid(list_disk) && valid_disk(list_disk->disk)); */ + /*@ assert valid_list_disk(list_disk); */ } } +#ifndef DISABLED_FOR_FRAMAC else { disk_t *disk_car=file_test_availability(argv[i], options.verbose, testdisk_mode); + /*@ assert valid_disk(disk_car); */ if(disk_car==NULL) { printf("\nUnable to open file or device %s: %s\n", argv[i], strerror(errno)); @@ -300,11 +346,16 @@ int main( int argc, char **argv ) return 1; } list_disk=insert_new_disk(list_disk,disk_car); + /*@ assert list_disk ==\null || (\valid(list_disk) && valid_disk(list_disk->disk)); */ + /*@ assert valid_list_disk(list_disk); */ } +#endif } -#ifdef ENABLE_DFXML + /*@ assert valid_ph_param(¶ms); */ +#if defined(ENABLE_DFXML) xml_set_command_line(argc, argv); #endif + /*@ assert valid_read_string(logfile); */ if(create_log!=TD_LOG_NONE) log_opened=log_open(logfile, create_log, &log_errno); #ifdef HAVE_SETLOCALE @@ -361,10 +412,8 @@ int main( int argc, char **argv ) #ifdef RECORD_COMPILATION_DATE log_info("Compilation date: %s\n", get_compilation_date()); #endif -#ifndef MAIN_photorec log_info("ext2fs lib: %s, ntfs lib: %s, ewf lib: %s, libjpeg: %s, curses lib: %s\n", td_ext2fs_version(), td_ntfs_version(), td_ewf_version(), td_jpeg_version(), td_curses_version()); -#endif #if defined(HAVE_GETEUID) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(DJGPP) if(geteuid()!=0) { @@ -373,6 +422,7 @@ int main( int argc, char **argv ) #endif log_flush(); screen_buffer_reset(); +#ifndef DISABLED_FOR_FRAMAC /* Scan for available device only if no device or image has been supplied in parameter */ if(list_disk==NULL) list_disk=hd_parse(list_disk, options.verbose, testdisk_mode); @@ -383,35 +433,41 @@ int main( int argc, char **argv ) element_disk->disk=new_diskcache(element_disk->disk, testdisk_mode); } log_disk_list(list_disk); +#endif reset_array_file_enable(options.list_file_format); file_options_load(options.list_file_format); #ifdef SUDO_BIN if(list_disk==NULL && geteuid()!=0) { - use_sudo=2; + use_sudo=2; } - else - { + if(use_sudo==0) use_sudo=do_curses_photorec(¶ms, &options, list_disk); - } #else do_curses_photorec(¶ms, &options, list_disk); #endif #ifdef HAVE_NCURSES end_ncurses(); #endif - log_info("PhotoRec exited normally.\n"); +#ifndef DISABLED_FOR_FRAMAC if(options.verbose > 0) { log_info("perf: get_prev_file_header: %lu, get_prev_location_smart: %lu\n", (long unsigned)gpfh_nbr, (long unsigned)gpls_nbr); } + log_info("PhotoRec exited normally.\n"); +#endif if(log_close()!=0) { +#ifndef DISABLED_FOR_FRAMAC printf("PhotoRec: Log file corrupted!\n"); +#endif } else if(params.cmd_run!=NULL && params.cmd_run[0]!='\0') { + /*@ assert valid_read_string(params.cmd_run); */ +#ifndef DISABLED_FOR_FRAMAC printf("PhotoRec syntax error: %s\n", params.cmd_run); +#endif } #ifdef SUDO_BIN if(use_sudo>0) @@ -425,7 +481,9 @@ int main( int argc, char **argv ) run_sudo(argc, argv, create_log); } #endif +#ifndef DISABLED_FOR_FRAMAC delete_list_disk(list_disk); +#endif free(params.recup_dir); #ifdef ENABLE_DFXML xml_clear_command_line(); diff --git a/src/phnc.c b/src/phnc.c index a0ac40ec..ebd6a883 100644 --- a/src/phnc.c +++ b/src/phnc.c @@ -24,7 +24,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/photorec.c b/src/photorec.c index 0b7006a5..85bee7a6 100644 --- a/src/photorec.c +++ b/src/photorec.c @@ -24,11 +24,12 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_FTRUNCATE #undef HAVE_LIBEXT2FS #undef HAVE_LIBNTFS #undef HAVE_LIBNTFS3G +#undef ENABLE_DFXML #endif #include @@ -51,6 +52,7 @@ #if defined(__FRAMAC__) #include "__fc_builtin.h" #endif +#include #include "types.h" #include "common.h" #include "fnctdsk.h" @@ -91,7 +93,7 @@ void file_block_log(const file_recovery_t *file_recovery, const unsigned int sec struct td_list_head *tmp; if(file_recovery->filename[0]=='\0') return; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("%s\t",file_recovery->filename); td_list_for_each(tmp, &file_recovery->location.list) { @@ -114,6 +116,7 @@ void del_search_space(alloc_data_t *list_search_space, const uint64_t start, con @ requires \valid(list_search_space); @ requires new_current_search_space == \null || \valid(*new_current_search_space); @ requires offset == \null || \valid(offset); + @ decreases end-start; @*/ static void update_search_space_aux(alloc_data_t *list_search_space, const uint64_t start, const uint64_t end, alloc_data_t **new_current_search_space, uint64_t *offset) { @@ -267,43 +270,46 @@ void free_list_search_space(alloc_data_t *list_search_space) unsigned int photorec_mkdir(const char *recup_dir, const unsigned int initial_dir_num) { - int dir_ok=0; - int dir_num=initial_dir_num; + unsigned int dir_num=initial_dir_num; #ifdef DJGPP int i=0; + /*@ assert valid_read_string(recup_dir); */ #endif - /*@ loop invariant valid_read_string(recup_dir); */ - do + /*@ + @ loop invariant \separated(recup_dir, &errno); + @ loop assigns errno, dir_num; + @*/ + while(1) { char working_recup_dir[2048]; - snprintf(working_recup_dir,sizeof(working_recup_dir)-1,"%s.%d",recup_dir,dir_num); + snprintf(working_recup_dir, sizeof(working_recup_dir)-1, "%s.%u", recup_dir, dir_num); + working_recup_dir[sizeof(working_recup_dir)-1]='\0'; + /*@ assert valid_read_string(&working_recup_dir[0]); */ #ifdef HAVE_MKDIR #ifdef __MINGW32__ - if(mkdir(working_recup_dir)!=0 && errno==EEXIST) + if(mkdir(working_recup_dir)==0 || errno!=EEXIST) + return dir_num; #else - if(mkdir(working_recup_dir, 0775)!=0 && errno==EEXIST) + if(mkdir(working_recup_dir, 0775)==0 || errno!=EEXIST) + return dir_num; #endif #else #warning "You need a mkdir function!" #endif - { - dir_num++; - } - else - { - dir_ok=1; - } + if(dir_num == UINT_MAX) + dir_num=0; + else + dir_num++; #ifdef DJGPP /* Avoid endless loop in Dos version of Photorec after 999 directories if working with short name */ i++; - if(dir_ok==0 && i==1000) + if(i==1000) { dir_num=initial_dir_num; - dir_ok=1; + return dir_num; } #endif - } while(dir_ok==0); - return dir_num; + } } /*@ @@ -365,6 +371,7 @@ void forget(const alloc_data_t *list_search_space, alloc_data_t *current_search_ unsigned int remove_used_space(disk_t *disk_car, const partition_t *partition, alloc_data_t *list_search_space) { +#ifndef DISABLED_FOR_FRAMAC if( partition->upart_type==UP_FAT12 || partition->upart_type==UP_FAT16 || partition->upart_type==UP_FAT32) @@ -378,6 +385,7 @@ unsigned int remove_used_space(disk_t *disk_car, const partition_t *partition, a #if defined(HAVE_LIBEXT2FS) else if(partition->upart_type==UP_EXT2 || partition->upart_type==UP_EXT3 || partition->upart_type==UP_EXT4) return ext2_remove_used_space(disk_car, partition, list_search_space); +#endif #endif return 0; } @@ -406,10 +414,14 @@ void update_stats(file_stat_t *file_stats, alloc_data_t *list_search_space) void write_stats_log(const file_stat_t *file_stats) { +#ifndef DISABLED_FOR_FRAMAC unsigned int file_nbr=0; unsigned int i; unsigned int nbr; file_stat_t *new_file_stats; + /*@ + @ loop assigns i; + @*/ for(i=0;file_stats[i].file_hint!=NULL;i++); if(i==0) return ; @@ -437,6 +449,7 @@ void write_stats_log(const file_stat_t *file_stats) { log_info("Total: %u file found\n\n",file_nbr); } +#endif } int sorfile_stat_ts(const void *p1, const void *p2) @@ -574,10 +587,12 @@ static void file_block_free(alloc_list_t *list_allocation) @ requires \valid(file_recovery->handle); @ requires valid_file_recovery(file_recovery); @ requires \separated(file_recovery, params, file_recovery->handle); + @ decreases 0; @*/ static void file_finish_aux(file_recovery_t *file_recovery, struct ph_param *params, const int paranoid) { /*@ assert valid_file_recovery(file_recovery); */ + /*@ assert file_recovery->file_check == \null || \valid_function(file_recovery->file_check); */ if(params->status!=STATUS_EXT2_ON_SAVE_EVERYTHING && params->status!=STATUS_EXT2_OFF_SAVE_EVERYTHING && file_recovery->file_stat!=NULL && file_recovery->file_check!=NULL && paranoid>0) @@ -594,7 +609,7 @@ static void file_finish_aux(file_recovery_t *file_recovery, struct ph_param *par if(file_recovery->file_stat!=NULL && file_recovery->file_size> 0 && file_recovery->file_size < file_recovery->min_filesize) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("%s File too small ( %llu < %llu), reject it\n", file_recovery->filename, (long long unsigned) file_recovery->file_size, @@ -723,7 +738,7 @@ pfstatus_t file_finish2(file_recovery_t *file_recovery, struct ph_param *params, void info_list_search_space(const alloc_data_t *list_search_space, const alloc_data_t *current_search_space, const unsigned int sector_size, const int keep_corrupted_file, const int verbose) { -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC struct td_list_head *search_walker = NULL; unsigned long int nbr_headers=0; uint64_t sectors_with_unknown_data=0; @@ -838,16 +853,42 @@ uint64_t set_search_start(struct ph_param *params, alloc_data_t **new_current_se return offset; } -void params_reset(struct ph_param *params, const struct ph_options *options) +/*@ + @ requires valid_ph_param(params); + @ requires params->disk->sector_size > 0; + @ requires valid_read_string(params->recup_dir); + @ assigns params->file_nbr; + @ assigns params->status; + @ assigns params->real_start_time; + @ assigns params->dir_num; + @ assigns params->offset; + @ assigns params->blocksize; + @ ensures valid_ph_param(params); + @ ensures params->file_nbr == 0; + @ ensures params->status == STATUS_FIND_OFFSET; + @ ensures params->dir_num == 1; + @ ensures params->offset == PH_INVALID_OFFSET; + @ ensures params->blocksize > 0; + @ ensures valid_read_string(params->recup_dir); + @*/ +static void params_reset_aux(struct ph_param *params) { params->file_nbr=0; params->status=STATUS_FIND_OFFSET; params->real_start_time=time(NULL); params->dir_num=1; - params->file_stats=init_file_stats(options->list_file_format); params->offset=PH_INVALID_OFFSET; if(params->blocksize==0) params->blocksize=params->disk->sector_size; + /*@ assert params->blocksize > 0; */ +} + +void params_reset(struct ph_param *params, const struct ph_options *options) +{ + /*@ assert valid_ph_param(params); */ + params->file_stats=init_file_stats(options->list_file_format); + /*@ assert valid_ph_param(params); */ + params_reset_aux(params); } const char *status_to_name(const photorec_status_t status) diff --git a/src/phrecn.c b/src/phrecn.c index 0967cf6c..b3bd78c1 100644 --- a/src/phrecn.c +++ b/src/phrecn.c @@ -24,8 +24,9 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES +#undef ENABLE_DFXML #endif #include @@ -236,7 +237,7 @@ int photorec(struct ph_param *params, const struct ph_options *options, alloc_da { skip_comma_in_command(¶ms->cmd_run); /*@ assert valid_read_string(params->recup_dir); */ -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC if(check_command(¶ms->cmd_run,"status=unformat",15)==0) { params->status=STATUS_UNFORMAT; @@ -281,7 +282,7 @@ int photorec(struct ph_param *params, const struct ph_options *options, alloc_da } /*@ assert valid_read_string(params->recup_dir); */ screen_buffer_reset(); -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("\nAnalyse\n"); log_partition(params->disk, params->partition); #endif @@ -301,7 +302,7 @@ int photorec(struct ph_param *params, const struct ph_options *options, alloc_da for(params->pass=0; params->status!=STATUS_QUIT; params->pass++) { const unsigned int old_file_nbr=params->file_nbr; -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC log_info("Pass %u (blocksize=%u) ", params->pass, params->blocksize); log_info("%s\n", status_to_name(params->status)); #endif @@ -322,13 +323,13 @@ int photorec(struct ph_param *params, const struct ph_options *options, alloc_da switch(params->status) { case STATUS_UNFORMAT: -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC ind_stop=fat_unformat(params, options, list_search_space); #endif params->blocksize=blocksize_is_known; break; case STATUS_FIND_OFFSET: -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC { uint64_t start_offset=0; if(blocksize_is_known>0) @@ -354,7 +355,7 @@ int photorec(struct ph_param *params, const struct ph_options *options, alloc_da break; case STATUS_EXT2_ON_BF: case STATUS_EXT2_OFF_BF: -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC ind_stop=photorec_bf(params, options, list_search_space); #endif break; @@ -424,7 +425,7 @@ int photorec(struct ph_param *params, const struct ph_options *options, alloc_da unlink("photorec.ses"); break; } -#ifndef __FRAMAC__ +#ifndef DISABLED_FOR_FRAMAC { const time_t current_time=time(NULL); log_info("Elapsed time %uh%02um%02us\n", diff --git a/src/poptions.c b/src/poptions.c index 116ba19a..da28aec1 100644 --- a/src/poptions.c +++ b/src/poptions.c @@ -37,8 +37,16 @@ void interface_options_photorec_cli(struct ph_options *options, char **current_cmd) { if(*current_cmd==NULL) + { + /*@ assert valid_read_string(*current_cmd); */ return ; - /*@ loop invariant valid_read_string(*current_cmd); */ + } + /*@ + @ loop invariant valid_read_string(*current_cmd); + @ loop assigns *current_cmd; + @ loop assigns options->paranoid, options->keep_corrupted_file, options->mode_ext2; + @ loop assigns options->expert, options->lowmem; + @*/ while(1) { skip_comma_in_command(current_cmd); @@ -81,7 +89,10 @@ void interface_options_photorec_cli(struct ph_options *options, char **current_c } else { +#ifndef DISABLED_FOR_FRAMAC interface_options_photorec_log(options); +#endif + /*@ assert valid_read_string(*current_cmd); */ return ; } } diff --git a/src/ppartseln.c b/src/ppartseln.c index 70f422b1..4cd42240 100644 --- a/src/ppartseln.c +++ b/src/ppartseln.c @@ -24,7 +24,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif diff --git a/src/psearchn.c b/src/psearchn.c index 3a63b45f..f79b1c7f 100644 --- a/src/psearchn.c +++ b/src/psearchn.c @@ -24,7 +24,7 @@ #include #endif -#if defined(__FRAMAC__) || defined(MAIN_photorec) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_NCURSES #endif @@ -78,6 +78,8 @@ extern int need_to_stop; pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options, alloc_data_t *list_search_space) { + pstatus_t ind_stop=PSTATUS_OK; +#ifndef DISABLED_FOR_FRAMAC uint64_t offset; unsigned char *buffer_start; unsigned char *buffer_olddata; @@ -85,22 +87,34 @@ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options time_t start_time; time_t previous_time; time_t next_checkpoint; - pstatus_t ind_stop=PSTATUS_OK; - unsigned int buffer_size; const unsigned int blocksize=params->blocksize; + const unsigned int buffer_size=blocksize + READ_SIZE; + /*@ assert buffer_size==blocksize + READ_SIZE; */ const unsigned int read_size=(blocksize>65536?blocksize:65536); uint64_t offset_before_back=0; unsigned int back=0; + /*@ assert blocksize == 512; */ + /*@ assert buffer_size == blocksize + READ_SIZE ; */ +#ifdef DISABLED_FOR_FRAMAC + char buffer_start_tmp[512+READ_SIZE]; +#endif pfstatus_t file_recovered_old=PFSTATUS_BAD; alloc_data_t *current_search_space; file_recovery_t file_recovery; memset(&file_recovery, 0, sizeof(file_recovery)); reset_file_recovery(&file_recovery); file_recovery.blocksize=blocksize; - buffer_size=blocksize + READ_SIZE; + /*@ assert valid_file_recovery(&file_recovery); */ +#ifndef DISABLED_FOR_FRAMAC buffer_start=(unsigned char *)MALLOC(buffer_size); +#else + buffer_start=&buffer_start_tmp; +#endif + /*@ assert \valid((char *)buffer_start + (0 .. buffer_size-1)); */ buffer_olddata=buffer_start; buffer=buffer_olddata+blocksize; + /*@ assert \valid(buffer_start + (0 .. blocksize + READ_SIZE-1)); */ + /*@ assert \valid(buffer + (0 .. READ_SIZE-1)); */ start_time=time(NULL); previous_time=start_time; next_checkpoint=start_time+5*60; @@ -109,15 +123,22 @@ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options offset=set_search_start(params, ¤t_search_space, list_search_space); if(options->verbose > 0) info_list_search_space(list_search_space, current_search_space, params->disk->sector_size, 0, options->verbose); +#ifndef DISABLED_FOR_FRAMAC if(options->verbose > 1) { log_verbose("Reading sector %10llu/%llu\n", (unsigned long long)((offset-params->partition->part_offset)/params->disk->sector_size), (unsigned long long)((params->partition->part_size-1)/params->disk->sector_size)); } +#endif params->disk->pread(params->disk, buffer, READ_SIZE, offset); header_ignored(NULL); +#ifndef DISABLED_FOR_FRAMAC + /*@ loop invariant valid_file_recovery(&file_recovery); */ while(current_search_space!=list_search_space) +#else + if(current_search_space!=list_search_space) +#endif { pfstatus_t file_recovered=PFSTATUS_BAD; uint64_t old_offset=offset; @@ -135,7 +156,9 @@ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options exit(1); } #endif + /*@ assert valid_file_recovery(&file_recovery); */ ind_stop=photorec_check_header(&file_recovery, params, options, list_search_space, buffer, &file_recovered, offset); + /*@ assert valid_file_recovery(&file_recovery); */ if(file_recovery.file_stat!=NULL) { /* try to skip ext2/ext3 indirect block */ @@ -143,14 +166,18 @@ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options file_recovery.file_size >= 12*blocksize && ind_block(buffer,blocksize)!=0) { + /*@ assert valid_file_recovery(&file_recovery); */ file_block_append(&file_recovery, list_search_space, ¤t_search_space, &offset, blocksize, 0); + /*@ assert valid_file_recovery(&file_recovery); */ data_check_status=DC_CONTINUE; +#ifndef DISABLED_FOR_FRAMAC if(options->verbose > 1) { log_verbose("Skipping sector %10lu/%lu\n", (unsigned long)((offset-params->partition->part_offset)/params->disk->sector_size), (unsigned long)((params->partition->part_size-1)/params->disk->sector_size)); } +#endif memcpy(buffer, buffer_olddata, blocksize); } else @@ -159,7 +186,9 @@ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options { if(fwrite(buffer,blocksize,1,file_recovery.handle)<1) { - log_critical("Cannot write to file %s: %s\n", file_recovery.filename, strerror(errno)); +#ifndef DISABLED_FOR_FRAMAC + log_critical("Cannot write to file %s after %llu bytes: %s\n", file_recovery.filename, (long long unsigned)file_recovery.file_size, strerror(errno)); +#endif if(errno==EFBIG) { /* File is too big for the destination filesystem */ @@ -175,30 +204,38 @@ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options } if(ind_stop==PSTATUS_OK) { + /*@ assert valid_file_recovery(&file_recovery); */ file_block_append(&file_recovery, list_search_space, ¤t_search_space, &offset, blocksize, 1); + /*@ assert valid_file_recovery(&file_recovery); */ if(file_recovery.data_check!=NULL) data_check_status=file_recovery.data_check(buffer_olddata,2*blocksize,&file_recovery); else data_check_status=DC_CONTINUE; file_recovery.file_size+=blocksize; +#ifndef DISABLED_FOR_FRAMAC if(data_check_status==DC_STOP) { if(options->verbose > 1) log_trace("EOF found\n"); } +#endif } } if(data_check_status!=DC_STOP && data_check_status!=DC_ERROR && file_recovery.file_stat->file_hint->max_filesize>0 && file_recovery.file_size>=file_recovery.file_stat->file_hint->max_filesize) { data_check_status=DC_STOP; +#ifndef DISABLED_FOR_FRAMAC log_verbose("File should not be bigger than %llu, stopped adding data\n", (long long unsigned)file_recovery.file_stat->file_hint->max_filesize); +#endif } if(data_check_status!=DC_STOP && data_check_status!=DC_ERROR && file_recovery.file_size + blocksize >= PHOTOREC_MAX_SIZE_32 && is_fat(params->partition)) { data_check_status=DC_STOP; +#ifndef DISABLED_FOR_FRAMAC log_verbose("File should not be bigger than %llu, stopped adding data\n", (long long unsigned)file_recovery.file_stat->file_hint->max_filesize); +#endif } if(data_check_status==DC_STOP || data_check_status==DC_ERROR) { @@ -211,9 +248,15 @@ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options } if(ind_stop!=PSTATUS_OK) { +#ifndef DISABLED_FOR_FRAMAC log_info("PhotoRec has been stopped\n"); +#endif + /*@ assert valid_file_recovery(&file_recovery); */ file_recovery_aborted(&file_recovery, params, list_search_space); + /*@ assert valid_file_recovery(&file_recovery); */ +#ifndef DISABLED_FOR_FRAMAC free(buffer_start); +#endif return ind_stop; } if(file_recovered==PFSTATUS_BAD) @@ -282,12 +325,14 @@ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options memcpy(buffer_start,buffer_olddata,blocksize); buffer_olddata=buffer_start; buffer=buffer_olddata + blocksize; +#ifndef DISABLED_FOR_FRAMAC if(options->verbose > 1) { log_verbose("Reading sector %10llu/%llu\n", (unsigned long long)((offset-params->partition->part_offset)/params->disk->sector_size), (unsigned long long)((params->partition->part_size-1)/params->disk->sector_size)); } +#endif if(params->disk->pread(params->disk, buffer, READ_SIZE, offset) != READ_SIZE) { #ifdef HAVE_NCURSES @@ -309,9 +354,13 @@ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options params->offset=offset; if(need_to_stop!=0 || ind_stop!=PSTATUS_OK) { +#ifndef DISABLED_FOR_FRAMAC log_info("PhotoRec has been stopped\n"); +#endif file_recovery_aborted(&file_recovery, params, list_search_space); +#ifndef DISABLED_FOR_FRAMAC free(buffer_start); +#endif return PSTATUS_STOP; } if(current_time >= next_checkpoint) @@ -321,7 +370,10 @@ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options } file_recovered_old=file_recovered; } /* end while(current_search_space!=list_search_space) */ +#ifndef DISABLED_FOR_FRAMAC free(buffer_start); +#endif +#endif #ifdef HAVE_NCURSES photorec_info(stdscr, params->file_stats); #endif diff --git a/src/psearchn.h b/src/psearchn.h index d60b0943..ac8c630a 100644 --- a/src/psearchn.h +++ b/src/psearchn.h @@ -31,6 +31,8 @@ extern "C" { @ requires \valid_read(options); @ requires \valid(list_search_space); @ requires \separated(params, options, list_search_space); + @ decreases 0; + @ ensures valid_ph_param(params); @*/ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options, alloc_data_t *list_search_space); diff --git a/src/sessionp.c b/src/sessionp.c index 0aa5a48b..78dd9fbd 100644 --- a/src/sessionp.c +++ b/src/sessionp.c @@ -56,6 +56,34 @@ #define SESSION_MAXSIZE 40960 #define SESSION_FILENAME "photorec.ses" +static int session_save_empty(void) +{ + FILE *f_session; + f_session=fopen(SESSION_FILENAME,"wb"); + if(!f_session) + { +#ifndef DISABLED_FOR_FRAMAC + log_critical("Can't create photorec.ses file: %s\n",strerror(errno)); +#endif + return -1; + } + { /* Reserve some space */ + int res; + char *buffer; + buffer=(char *)MALLOC(SESSION_MAXSIZE); + memset(buffer,0,SESSION_MAXSIZE); + res=fwrite(buffer,1,SESSION_MAXSIZE,f_session); + free(buffer); + if(res=0) buffer_size=stat_rec.st_size; else @@ -172,15 +200,20 @@ int session_load(char **cmd_device, char **current_cmd, alloc_data_t *list_free_ int session_save(const alloc_data_t *list_free_space, const struct ph_param *params, const struct ph_options *options) { FILE *f_session; - if(params!=NULL && params->status==STATUS_QUIT) + if(params->status==STATUS_QUIT) return 0; f_session=fopen(SESSION_FILENAME,"wb"); if(!f_session) { +#ifndef DISABLED_FOR_FRAMAC log_critical("Can't create photorec.ses file: %s\n",strerror(errno)); +#endif + /*@ assert \valid_read(list_free_space); */ + /*@ assert valid_ph_param(params); */ + /*@ assert \valid_read(options); */ return -1; } - if(params!=NULL) +#ifndef DISABLED_FOR_FRAMAC { struct td_list_head *free_walker = NULL; unsigned int i; @@ -314,6 +347,7 @@ int session_save(const alloc_data_t *list_free_space, const struct ph_param *par (long long unsigned)(current_free_space->end/params->disk->sector_size)); } } +#endif { /* Reserve some space */ int res; char *buffer; @@ -324,10 +358,16 @@ int session_save(const alloc_data_t *list_free_space, const struct ph_param *par if(res #endif -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_UTIME #endif diff --git a/src/suspend_no.c b/src/suspend_no.c index 4bc493c9..3abea19e 100644 --- a/src/suspend_no.c +++ b/src/suspend_no.c @@ -2,7 +2,7 @@ #include #endif -#ifdef __FRAMAC__ +#ifdef DISABLED_FOR_FRAMAC #undef HAVE_LIBJPEG #endif diff --git a/src/testdisk.c b/src/testdisk.c index acbfb858..cd4f7ae1 100644 --- a/src/testdisk.c +++ b/src/testdisk.c @@ -27,7 +27,7 @@ #undef SUDO_BIN #endif -#if defined(__FRAMAC__) +#if defined(DISABLED_FOR_FRAMAC) #undef HAVE_LIBEWF #endif