-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpointers.c
185 lines (138 loc) · 3.95 KB
/
pointers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "ext4.h"
void hexdump(char *buf, int size)
{
int index = 0 ;
//printf("entered %d\n", size);
for (; size ; size--)
printf("%c",*buf++);
}
void parse_ex_tree(int f, unsigned long long off)
{
struct ext4_extent_header eh ;
struct ext4_extent_idx ehid;
struct ext4_extent ex ;
struct ext4_extent *exp ;
//memcpy(&eh, addr, sizeof (eh) );
if (off)
lseek(f, off*4096L, SEEK_SET);
read(f, &eh, sizeof (eh));
#if 0
printf ("magic %x\n", eh.eh_magic);
printf ("entries %x\n", eh.eh_entries);
printf ("depth %x\n", eh.eh_depth);
printf ("max entries %x\n", eh.eh_max);
#endif
if (eh.eh_depth == 0 ) {
int j = 0;
for (; j<eh.eh_entries; j++) {
#if 0
memcpy(&ex,
(char*)addr+(sizeof(eh)+(j*sizeof(ex))) ,
sizeof(ex));
#endif
read(f, &ex, sizeof(ex));
#if 0
printf ("block %d\n", ex.ee_block);
printf ("len %d\n", ex.ee_len);
printf ("start %d\n", ex.ee_start_lo | (ex.ee_start_hi << 32));
#endif
if ( ex.ee_block != ex.ee_block+ex.ee_len-1 )
printf ("(%d-%d):%d-%d, ",ex.ee_block, ex.ee_block+ex.ee_len-1,
ex.ee_start_lo | (ex.ee_start_hi << 32),
(ex.ee_start_lo | (ex.ee_start_hi << 32)) + ex.ee_len-1);
else
printf ("(%d):%d, ",ex.ee_block,
ex.ee_start_lo | (ex.ee_start_hi << 32));
}
}else {
int j = 0;
for (; j<eh.eh_entries; j++) {
#if 0
memcpy(&ehid,
(char*)addr+(sizeof(eh)+(j*sizeof(ehid))) ,
sizeof(ehid));
#endif
read(f, &ehid, sizeof(ehid));
#if 0
printf ("block %d\n", ehid.ei_block);
printf ("leaf %d\n", ehid.ei_leaf_hi);
printf ("unused %d\n", ehid.ei_unused);
#endif
printf ("(ETB0):%d, ", ehid.ei_leaf_lo| (ehid.ei_leaf_hi <<32));
parse_ex_tree( f, ehid.ei_leaf_lo| (ehid.ei_leaf_hi <<32) );
}
}
}
int main (int argc, char *argv[])
{
int f;
int index = 0;
int itable ;
char content[4096] = {} ;
struct ext4_inode ext4_inode;
struct ext4_group_desc gd ;
struct ext4_extent_header eh ;
struct ext4_extent_idx ehid;
struct ext4_extent ex ;
struct ext4_extent *exp ;
int f_size = 0;
if (argc < 3) {
printf ("Throw me an Disk name and Inode boss !!\n");
return 1;
}
f = open(DISK_NAME, O_RDONLY);
if ( f == -1 ) {
printf ("run with root permissions\n");
return -1;
}
int inode = atoi(argv[2]) -1;
int group = inode/8192 ;
int index_1 = inode % 8192;
int offset = index_1 * 256;
int gd_count = get_gd_count(f);
//printf ("gd size %d\n", gd_count);
lseek(f, 1024+1024, SEEK_CUR);
// leave out these x86 boot sector and padding && //readout superblock
lseek(f, 32 * (sizeof(struct ext4_group_desc)), SEEK_CUR);
int size_of_gd = get_desc_size(f);
for (; index < gd_count; index++){
read(f, &gd, size_of_gd);
if (gd.bg_checksum && index == group) {
//print_gd_info(index, &gd) ;
itable = (((ext4_64_t)(gd.bg_inode_table_hi) << 32) | gd.bg_inode_table_lo);
memset(&gd, 0, sizeof (gd));
}
}
int constant = 1 ;
lseek(f, 0, SEEK_SET);
lseek(f, itable*4096L, SEEK_CUR);
lseek(f, offset, SEEK_CUR);
memset(&ext4_inode, 0, sizeof (struct ext4_inode));
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
printf ("offset is %d\n", offsetof(struct ext4_inode, i_block));
unsigned long long _off = read(f, 0, SEEK_CUR);
read(f, &ext4_inode, sizeof (struct ext4_inode));
if ( ! S_ISLNK(ext4_inode.i_mode) && ! S_ISREG(ext4_inode.i_mode)) {
printf ("Not a Regular file\n");
return -1 ;
}
if (S_ISLNK(ext4_inode.i_mode)) {
char *uma = ext4_inode.i_block ;
printf ("%s\n", uma);
}else {
#if 1
int loop_index = 0 ;
printf ("Iblock %u\n", ext4_inode.i_block);
for (index=0; index < EXT4_N_BLOCKS ; index++) {
printf (" block pointer. %d = %d\n", index, ext4_inode.i_block[index]);
}
lseek(f, -sizeof (struct ext4_inode), SEEK_CUR);
lseek(f, offsetof(struct ext4_inode, i_block), SEEK_CUR);
parse_ex_tree(f, 0);
puts("");
#endif
}
return ;
printf ("pid %d \n", getpid());
while(1);
}