Chameleon

Chameleon Commit Details

Date:2015-03-12 20:16:32 (5 years 8 months ago)
Author:ErmaC
Commit:2625
Parents: 2624
Message:Reformatted machOconv.c (credits to Pike R. Alpha)
Changes:
M/trunk/i386/util/machOconv.c

File differences

trunk/i386/util/machOconv.c
2121
2222
2323
24
2425
2526
2627
......
3334
3435
3536
37
38
3639
37
3840
3941
4042
43
44
4145
4246
43
44
45
46
47
48
49
50
51
52
53
54
4755
4856
49
50
57
58
59
60
5161
52
53
54
55
56
62
63
64
65
66
5767
5868
5969
70
6071
6172
62
73
6374
75
6476
6577
6678
6779
6880
69
70
81
82
83
84
7185
72
86
87
88
7389
7490
75
91
7692
77
78
79
93
94
95
96
8097
98
99
100
101
102
103
104
105
81106
82
107
83108
109
84110
85111
86112
......
89115
90116
91117
92
118
93119
94120
95121
......
100126
101127
102128
103
104
129
130
131
132
105133
106134
107135
108136
137
109138
110139
111
140
112141
113142
114143
115144
116
145
146
117147
118148
119149
120150
151
121152
122153
123
154
124155
125156
126157
127
158
128159
129160
130161
......
135166
136167
137168
138
169
139170
140171
141172
......
156187
157188
158189
159
190
191
160192
161193
162194
163
195
196
197
164198
165199
166200
167201
202
168203
169204
170205
......
173208
174209
175210
176
211
212
213
177214
178215
179216
217
180218
181219
182220
......
185223
186224
187225
226
188227
189228
190229
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
intinfile, outfile;
void *cmds;
struct mach_headermh;
void *cmds;
static boolswap_ends;
//==============================================================================
static unsigned long swap(unsigned long x)
{
if (swap_ends)
return OSSwapInt32(x);
else
return x;
if (swap_ends)
{
return OSSwapInt32(x);
}
else
{
return x;
}
}
int
main(int argc, char *argv[])
//==============================================================================
int main(int argc, char *argv[])
{
kern_return_tresult;
vm_address_tdata;
intnc, ncmds;
char *cp;
kern_return_tresult;
vm_address_tdata;
intnc, ncmds;
char *cp;
if (argc == 2)
{
infile = open(argv[1], O_RDONLY);
if (infile < 0)
{
goto usage;
goto filenotfound;
}
outfile = fileno(stdout);
}
else if (argc == 3)
{
infile = open(argv[1], O_RDONLY);
if (infile < 0){
goto usage;
if (infile < 0)
{
goto filenotfound;
}
outfile = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644);
outfile = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (outfile < 0)
{
goto usage;
goto openerror;
}
} else {
usage:
fprintf(stderr, "usage: machOconv inputfile [outputfile]\n");
}
else
{
fprintf(stderr, "Usage: machOconv inputfile [outputfile]\n");
exit(1);
filenotfound:
fprintf(stderr, "Error: File Not Found\n");
exit(1);
openerror:
fprintf(stderr, "Error: Open Failed\n");
exit(1);
}
nc = read(infile, &mh, sizeof (mh));
if (nc < 0)
{
perror("read mach header");
if (nc < (int)sizeof (mh))
{
fprintf(stderr, "read mach header: premature EOF %d\n", nc);
fprintf(stderr, "Error: read mach header: premature EOF %d\n", nc);
exit(1);
}
else if (mh.magic == MH_CIGAM)
{
swap_ends = true;
} else {
fprintf(stderr, "bad magic number %lx\n", (unsigned long)mh.magic);
}
else
{
fprintf(stderr, "Error: bad magic number %lx\n", (unsigned long)mh.magic);
exit(1);
}
cmds = calloc(swap(mh.sizeofcmds), sizeof (char));
if (cmds == 0)
{
fprintf(stderr, "alloc load commands: no memory\n");
fprintf(stderr, "Error: alloc load commands: no memory\n");
exit(1);
}
nc = read(infile, cmds, swap(mh.sizeofcmds));
if (nc < 0)
if (nc < 0)
{
perror("read load commands");
exit(1);
}
if (nc < (int)swap(mh.sizeofcmds))
{
fprintf(stderr, "read load commands: premature EOF %d\n", nc);
fprintf(stderr, "Error: read load commands: premature EOF %d\n", nc);
exit(1);
}
unsigned long vmstart = (unsigned long)-1;
unsigned long vmstart = (unsigned long) -1;
// First pass: determine actual load address
for (ncmds = swap(mh.ncmds), cp = cmds; ncmds > 0; ncmds--)
switch(swap(lcp->cmd))
{
case LC_SEGMENT:
if(vmstart > swap(scp->vmaddr))
if (vmstart > swap(scp->vmaddr))
{
vmstart = swap(scp->vmaddr);
}
switch(swap(lcp->cmd))
{
case LC_SEGMENT:
isDATA = (strcmp(scp->segname, "__DATA") == 0);
isDATA = (strncmp(scp->segname, "__DATA", sizeof("__DATA")) == 0);
if (isDATA)
{
vmsize = swap(scp->filesize);
} else {
}
else
{
vmsize = swap(scp->vmsize);
}
result = vm_allocate(mach_task_self(), &data, vmsize, true);
if (result != KERN_SUCCESS)
{
mach_error("vm_allocate segment data", result);
lseek(infile, swap(scp->fileoff), L_SET);
nc = read(infile, (void *)data, swap(scp->filesize));
if (nc < 0) {
if (nc < 0)
{
perror("read segment data");
exit(1);
}
if (nc < (int)swap(scp->filesize))
{
fprintf(stderr, "read segment data: premature EOF %d\n", nc);
lseek(outfile, swap(scp->vmaddr) - vmstart, L_SET);
nc = write(outfile, (void *)data, vmsize);
if (nc < (int)vmsize)
{
perror("write segment data");

Archive Download the corresponding diff file

Revision: 2625