summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2016-08-20 09:26:23 +0200
committerChristophe Grenier <grenier@cgsecurity.org>2016-08-20 09:26:23 +0200
commitd705bac84f34e4c8d587fb86af794525bc130757 (patch)
tree6a595dc12fe2ab713fdade7fab0b18d7dd77684b
parent828ec27071759b86d3c99468851db0c53e54976e (diff)
Replace td_list_entry by td_list_(first|prev|next|last)_entry to improve readability
-rw-r--r--src/fat_adv.c6
-rw-r--r--src/fat_unformat.c11
-rw-r--r--src/filegen.c2
-rw-r--r--src/list.h46
-rw-r--r--src/phbf.c8
-rw-r--r--src/phbs.c2
-rw-r--r--src/photorec.c18
-rw-r--r--src/phrecn.c2
-rw-r--r--src/psearchn.c2
9 files changed, 70 insertions, 27 deletions
diff --git a/src/fat_adv.c b/src/fat_adv.c
index 9be96ae..7654cc7 100644
--- a/src/fat_adv.c
+++ b/src/fat_adv.c
@@ -312,8 +312,8 @@ static int ask_root_directory(disk_t *disk_car, const partition_t *partition, co
static int is_root_cluster_candidat(const file_info_t *dir_list)
{
- const file_info_t *file1=td_list_entry_const(dir_list->list.next, const file_info_t, list);
- const file_info_t *file2=td_list_entry_const(file1->list.next, const file_info_t, list);
+ const file_info_t *file1=td_list_first_entry(&dir_list->list, const file_info_t, list);
+ const file_info_t *file2=td_list_next_entry(file1, list);
return (!td_list_empty(&dir_list->list) && (&file2->list==&dir_list->list || file1->st_ino!=file2->st_ino));
}
@@ -385,7 +385,7 @@ static unsigned int fat32_find_root_cluster(disk_t *disk_car,const partition_t *
dir_aff_log(NULL, &dir_list);
}
{
- const file_info_t *first_entry=td_list_entry_const(&dir_list.list, const file_info_t, list);
+ const file_info_t *first_entry=td_list_first_entry(&dir_list.list, const file_info_t, list);
file_info_t *new_file=(file_info_t *)MALLOC(sizeof(*new_file));
memcpy(new_file, first_entry, sizeof(*new_file));
new_file->name=(char*)MALLOC(32);
diff --git a/src/fat_unformat.c b/src/fat_unformat.c
index b6d871e..fe7ae2c 100644
--- a/src/fat_unformat.c
+++ b/src/fat_unformat.c
@@ -68,7 +68,7 @@ static int pfind_sectors_per_cluster(disk_t *disk, partition_t *partition, const
unsigned char *buffer_start=(unsigned char *)MALLOC(READ_SIZE);
unsigned char *buffer=buffer_start;
assert(disk->sector_size!=0);
- current_search_space=td_list_entry(list_search_space->list.next, alloc_data_t, list);
+ current_search_space=td_list_first_entry(&list_search_space->list, alloc_data_t, list);
if(current_search_space!=list_search_space)
offset=current_search_space->start;
if(verbose>0)
@@ -226,21 +226,20 @@ static pstatus_t fat_unformat_aux(struct ph_param *params, const struct ph_optio
#endif
reset_file_recovery(&file_recovery);
file_recovery.blocksize=cluster_size;
- buffer_start=(unsigned char *)MALLOC(READ_SIZE);
- buffer=buffer_start;
start_time=time(NULL);
previous_time=start_time;
- current_search_space=td_list_entry(list_search_space->list.prev, alloc_data_t, list);
+ current_search_space=td_list_last_entry(&list_search_space->list, alloc_data_t, list);
if(current_search_space==list_search_space)
{
- free(buffer_start);
return PSTATUS_OK;
}
offset_end=current_search_space->end;
- current_search_space=td_list_entry(list_search_space->list.next, alloc_data_t, list);
+ current_search_space=td_list_first_entry(&list_search_space->list, alloc_data_t, list);
offset=set_search_start(params, &current_search_space, list_search_space);
if(options->verbose>0)
info_list_search_space(list_search_space, current_search_space, disk->sector_size, 0, options->verbose);
+ buffer_start=(unsigned char *)MALLOC(READ_SIZE);
+ buffer=buffer_start;
disk->pread(disk, buffer_start, READ_SIZE, offset);
for(;offset < offset_end; offset+=cluster_size)
{
diff --git a/src/filegen.c b/src/filegen.c
index c7bfe83..e40d127 100644
--- a/src/filegen.c
+++ b/src/filegen.c
@@ -614,7 +614,7 @@ void get_prev_location_smart(alloc_data_t *list_search_space, alloc_data_t **cur
* Limit the search to 3 fragments or 200 MB */
for(nbr=0; nbr<3 && size < (uint64_t)200*1024*1024; nbr++)
{
- file_space=td_list_entry(file_space->list.prev, alloc_data_t, list);
+ file_space=td_list_prev_entry(file_space, list);
if(file_space==list_search_space)
return;
if(file_space->start <= offset_skipped_header && offset_skipped_header < file_space->end)
diff --git a/src/list.h b/src/list.h
index 307570f..29e8dad 100644
--- a/src/list.h
+++ b/src/list.h
@@ -229,10 +229,10 @@ static inline void td_list_splice_init(struct td_list_head *list,
* @member: the name of the td_list_struct within the struct.
*/
#define td_list_entry(ptr, type, member) \
- ((type *)((char *)(ptr)-(unsigned long long)(&((type *)0)->member)))
+ ((type *)((char *)(ptr)-(size_t)(&((type *)0)->member)))
#define td_list_entry_const(ptr, type, member) \
- ((type *)((const char *)(ptr)-(unsigned long long)(&((type *)0)->member)))
+ ((type *)((const char *)(ptr)-(size_t)(&((type *)0)->member)))
/**
* __td_list_for_each - iterate over a list
@@ -330,6 +330,48 @@ static inline void td_list_splice_init(struct td_list_head *list,
pos = n, n = td_list_entry(n->member.next, typeof(*n), member))
+/**
+ * td_list_first_entry - get the first element from a list
+ * @ptr: the list head to take the element from.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_head within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+
+#define td_list_first_entry(ptr, type, member) \
+ td_list_entry((ptr)->next, type, member)
+
+
+/**
+ * td_list_last_entry - get the last element from a list
+ * @ptr: the list head to take the element from.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_head within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+#define td_list_last_entry(ptr, type, member) \
+ td_list_entry((ptr)->prev, type, member)
+
+
+/**
+ * td_list_next_entry - get the next element in list
+ * @pos: the type * to cursor
+ * @member: the name of the list_head within the struct.
+ */
+#define td_list_next_entry(pos, member) \
+ td_list_entry((pos)->member.next, typeof(*(pos)), member)
+
+/**
+ * td_list_prev_entry - get the prev element in list
+ * @pos: the type * to cursor
+ * @member: the name of the list_head within the struct.
+ */
+#define td_list_prev_entry(pos, member) \
+ td_list_entry((pos)->member.prev, typeof(*(pos)), member)
+
+
static inline void td_list_add_sorted(struct td_list_head *newe, struct td_list_head *head,
int (*compar)(const struct td_list_head *a, const struct td_list_head *b))
{
diff --git a/src/phbf.c b/src/phbf.c
index 1165557..5d501a3 100644
--- a/src/phbf.c
+++ b/src/phbf.c
@@ -590,15 +590,17 @@ static bf_status_t photorec_bf_frag(struct ph_param *params, file_recovery_t *fi
extrablock_offset=0;
if(!td_list_empty(&file_recovery->location.list))
{
- const alloc_list_t *element=td_list_entry_const(file_recovery->location.list.prev, const alloc_list_t, list);
+ const alloc_list_t *element=td_list_last_entry(&file_recovery->location.list, alloc_list_t, list);
extrablock_offset=element->end/blocksize*blocksize;
}
/* Get the corresponding search_place */
- extractblock_search_space=td_list_entry(list_search_space->list.next, alloc_data_t, list);
+ extractblock_search_space=td_list_first_entry(&list_search_space->list, alloc_data_t, list);
while(extractblock_search_space != list_search_space &&
!(extractblock_search_space->start <= extrablock_offset &&
extrablock_offset <= extractblock_search_space->end))
- extractblock_search_space=td_list_entry(extractblock_search_space->list.next, alloc_data_t, list);
+ {
+ extractblock_search_space=td_list_next_entry(extractblock_search_space, list);
+ }
/* Update extractblock_search_space & extrablock_offset */
get_next_sector(list_search_space, &extractblock_search_space, &extrablock_offset, blocksize);
/* */
diff --git a/src/phbs.c b/src/phbs.c
index 945ae7e..b1135d8 100644
--- a/src/phbs.c
+++ b/src/phbs.c
@@ -88,7 +88,7 @@ pstatus_t photorec_find_blocksize(struct ph_param *params, const struct ph_optio
start_time=time(NULL);
previous_time=start_time;
memset(buffer_olddata, 0, blocksize);
- current_search_space=td_list_entry(list_search_space->list.next, alloc_data_t, list);
+ current_search_space=td_list_first_entry(&list_search_space->list, alloc_data_t, list);
if(current_search_space!=list_search_space)
offset=current_search_space->start;
if(options->verbose>0)
diff --git a/src/photorec.c b/src/photorec.c
index 4304a55..ff520d4 100644
--- a/src/photorec.c
+++ b/src/photorec.c
@@ -127,7 +127,7 @@ static void update_search_space_aux(alloc_data_t *list_search_space, const uint6
if(offset!=NULL && new_current_search_space!=NULL &&
current_search_space->start<=*offset && *offset<=current_search_space->end)
{
- *new_current_search_space=td_list_entry(current_search_space->list.next, alloc_data_t, list);
+ *new_current_search_space=td_list_next_entry(current_search_space, list);
*offset=(*new_current_search_space)->start;
}
td_list_del(search_walker);
@@ -146,7 +146,7 @@ static void update_search_space_aux(alloc_data_t *list_search_space, const uint6
if(offset!=NULL && new_current_search_space!=NULL &&
start<=*offset && *offset<=current_search_space->end)
{
- *new_current_search_space=td_list_entry(current_search_space->list.next, alloc_data_t, list);
+ *new_current_search_space=td_list_next_entry(current_search_space, list);
*offset=(*new_current_search_space)->start;
}
current_search_space->end=start-1;
@@ -156,7 +156,7 @@ static void update_search_space_aux(alloc_data_t *list_search_space, const uint6
if(offset!=NULL && new_current_search_space!=NULL &&
current_search_space->start<=*offset && *offset<=current_search_space->end)
{
- *new_current_search_space=td_list_entry(current_search_space->list.next, alloc_data_t, list);
+ *new_current_search_space=td_list_next_entry(current_search_space, list);
*offset=(*new_current_search_space)->start;
}
td_list_del(search_walker);
@@ -284,7 +284,7 @@ void get_prev_location(alloc_data_t *list_search_space, alloc_data_t **current_s
* Limit the search to 10 fragments or 1GB */
for(nbr=0; nbr<3 && size < (uint64_t)200*1024*1024; nbr++)
{
- file_space=td_list_entry(file_space->list.prev, alloc_data_t, list);
+ file_space=td_list_prev_entry(file_space, list);
if(file_space==list_search_space)
return;
size+=file_space->end - file_space->start + 1;
@@ -304,7 +304,7 @@ int get_prev_file_header(alloc_data_t *list_search_space, alloc_data_t **current
* Limit the search to 10 fragments or 1GB */
for(nbr=0; nbr<3 && size < (uint64_t)200*1024*1024; nbr++)
{
- file_space=td_list_entry(file_space->list.prev, alloc_data_t, list);
+ file_space=td_list_prev_entry(file_space, list);
if(file_space==list_search_space)
return -1;
size+=file_space->end - file_space->start + 1;
@@ -445,7 +445,7 @@ unsigned int find_blocksize(alloc_data_t *list_search_space, const unsigned int
*offset=0;
if(td_list_empty(&list_search_space->list))
return default_blocksize;
- *offset=(td_list_entry(list_search_space->list.next, alloc_data_t, list))->start % blocksize;
+ *offset=(td_list_first_entry(&list_search_space->list, alloc_data_t, list))->start % blocksize;
do
{
run_again=0;
@@ -890,7 +890,7 @@ static inline void file_block_remove_from_sp_aux(alloc_data_t *tmp, alloc_data_t
tmp->file_stat=NULL;
if(tmp->start <= tmp->end)
return ;
- *new_current_search_space=td_list_entry(tmp->list.next, alloc_data_t, list);
+ *new_current_search_space=td_list_next_entry(tmp, list);
*offset=(*new_current_search_space)->start;
td_list_del(&tmp->list);
free(tmp);
@@ -899,7 +899,7 @@ static inline void file_block_remove_from_sp_aux(alloc_data_t *tmp, alloc_data_t
if(*offset + blocksize == tmp->end + 1)
{
tmp->end-=blocksize;
- *new_current_search_space=td_list_entry(tmp->list.next, alloc_data_t, list);
+ *new_current_search_space=td_list_next_entry(tmp, list);
*offset=(*new_current_search_space)->start;
return ;
}
@@ -943,7 +943,7 @@ static inline void file_block_add_to_file(alloc_list_t *list, const uint64_t off
{
if(!td_list_empty(&list->list))
{
- alloc_list_t *prev=td_list_entry(list->list.prev, alloc_list_t, list);
+ alloc_list_t *prev=td_list_last_entry(&list->list, alloc_list_t, list);
if(prev->end+1==offset && prev->data==data)
{
prev->end=offset+blocksize-1;
diff --git a/src/phrecn.c b/src/phrecn.c
index 616202f..9057632 100644
--- a/src/phrecn.c
+++ b/src/phrecn.c
@@ -333,7 +333,7 @@ int photorec(struct ph_param *params, const struct ph_options *options, alloc_da
{
ind_stop=PSTATUS_OK;
if(!td_list_empty(&list_search_space->list))
- start_offset=(td_list_entry(list_search_space->list.next, alloc_data_t, list))->start % params->blocksize;
+ start_offset=(td_list_first_entry(&list_search_space->list, alloc_data_t, list))->start % params->blocksize;
}
else
{
diff --git a/src/psearchn.c b/src/psearchn.c
index dc6bcc3..dfafe3f 100644
--- a/src/psearchn.c
+++ b/src/psearchn.c
@@ -238,7 +238,7 @@ pstatus_t photorec_aux(struct ph_param *params, const struct ph_options *options
previous_time=start_time;
next_checkpoint=start_time+5*60;
memset(buffer_olddata,0,blocksize);
- current_search_space=td_list_entry(list_search_space->list.next, alloc_data_t, list);
+ current_search_space=td_list_first_entry(&list_search_space->list, alloc_data_t, list);
offset=set_search_start(params, &current_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);