Root/
Source at commit 1075 created 13 years 1 month ago. By meklort, Disk code | |
---|---|
1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | #include <stdio.h>␊ |
25 | #include <stdlib.h>␊ |
26 | #include <stdbool.h>␊ |
27 | #include <mach/mach.h>␊ |
28 | #include <mach/mach_error.h>␊ |
29 | #include <sys/file.h>␊ |
30 | #include <mach-o/loader.h>␊ |
31 | #include <libkern/OSByteOrder.h>␊ |
32 | #include <unistd.h>␊ |
33 | ␊ |
34 | int␉infile, outfile;␊ |
35 | ␊ |
36 | struct mach_header␉mh;␊ |
37 | void *␉␉cmds;␊ |
38 | ␊ |
39 | static bool␉swap_ends;␊ |
40 | ␊ |
41 | static unsigned long swap(␊ |
42 | unsigned long x␊ |
43 | )␊ |
44 | {␊ |
45 | if (swap_ends)␊ |
46 | ␉return OSSwapInt32(x);␊ |
47 | else␊ |
48 | ␉return x;␊ |
49 | }␊ |
50 | ␊ |
51 | int␊ |
52 | main(int argc, char *argv[])␊ |
53 | {␊ |
54 | kern_return_t␉result;␊ |
55 | vm_address_t␉data;␊ |
56 | int␉␉␉nc, ncmds;␊ |
57 | char *␉␉cp;␊ |
58 | ␊ |
59 | if (argc == 2) {␊ |
60 | ␉infile = open(argv[1], O_RDONLY);␊ |
61 | ␉if (infile < 0)␊ |
62 | ␉ goto usage;␊ |
63 | ␉outfile = fileno(stdout);␊ |
64 | }␊ |
65 | else if (argc == 3) {␊ |
66 | ␉infile = open(argv[1], O_RDONLY);␊ |
67 | ␉if (infile < 0)␊ |
68 | ␉ goto usage;␊ |
69 | ␉outfile = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644);␊ |
70 | ␉if (outfile < 0)␊ |
71 | ␉ goto usage;␊ |
72 | }␊ |
73 | else {␊ |
74 | usage:␊ |
75 | ␉fprintf(stderr, "usage: machOconv inputfile [outputfile]\n");␊ |
76 | ␉exit(1);␊ |
77 | }␊ |
78 | ␊ |
79 | nc = read(infile, &mh, sizeof (mh));␊ |
80 | if (nc < 0) {␊ |
81 | ␉perror("read mach header");␊ |
82 | ␉exit(1);␊ |
83 | }␊ |
84 | if (nc < (int)sizeof (mh)) {␊ |
85 | ␉fprintf(stderr, "read mach header: premature EOF %d\n", nc);␊ |
86 | ␉exit(1);␊ |
87 | }␊ |
88 | if (mh.magic == MH_MAGIC)␊ |
89 | ␉swap_ends = false;␊ |
90 | else if (mh.magic == MH_CIGAM)␊ |
91 | ␉swap_ends = true;␊ |
92 | else {␊ |
93 | ␉fprintf(stderr, "bad magic number %lx\n", (unsigned long)mh.magic);␊ |
94 | ␉exit(1);␊ |
95 | }␊ |
96 | ␊ |
97 | cmds = calloc(swap(mh.sizeofcmds), sizeof (char));␊ |
98 | if (cmds == 0) {␊ |
99 | ␉fprintf(stderr, "alloc load commands: no memory\n");␊ |
100 | ␉exit(1);␊ |
101 | }␊ |
102 | nc = read(infile, cmds, swap(mh.sizeofcmds));␊ |
103 | if (nc < 0) {␊ |
104 | ␉perror("read load commands");␊ |
105 | ␉exit(1);␊ |
106 | }␊ |
107 | if (nc < (int)swap(mh.sizeofcmds)) {␊ |
108 | ␉fprintf(stderr, "read load commands: premature EOF %d\n", nc);␊ |
109 | ␉exit(1);␊ |
110 | }␊ |
111 | ␊ |
112 | for (␉ncmds = swap(mh.ncmds), cp = cmds;␊ |
113 | ␉␉ncmds > 0; ncmds--) {␊ |
114 | ␉ bool␉isDATA;␊ |
115 | ␉ unsigned␉vmsize;␊ |
116 | ␊ |
117 | #define lcp␉((struct load_command *)cp) ␊ |
118 | ␉switch(swap(lcp->cmd)) {␊ |
119 | ␊ |
120 | ␉case LC_SEGMENT:␊ |
121 | #define scp␉((struct segment_command *)cp)␊ |
122 | ␉ isDATA = (strcmp(scp->segname, "__DATA") == 0);␊ |
123 | ␉ if (isDATA)␊ |
124 | ␉ ␉vmsize = swap(scp->filesize);␊ |
125 | ␉ else␊ |
126 | ␉ ␉vmsize = swap(scp->vmsize);␊ |
127 | ␉ result = vm_allocate(mach_task_self(), &data, vmsize, true);␊ |
128 | ␉ if (result != KERN_SUCCESS) {␊ |
129 | ␉␉mach_error("vm_allocate segment data", result);␊ |
130 | ␉␉exit(1);␊ |
131 | ␉ }␊ |
132 | ␊ |
133 | ␉ lseek(infile, swap(scp->fileoff), L_SET);␊ |
134 | ␉ nc = read(infile, (void *)data, swap(scp->filesize));␊ |
135 | ␉ if (nc < 0) {␊ |
136 | ␉␉perror("read segment data");␊ |
137 | ␉␉exit(1);␊ |
138 | ␉ }␊ |
139 | ␉ if (nc < (int)swap(scp->filesize)) {␊ |
140 | ␉␉fprintf(stderr, "read segment data: premature EOF %d\n", nc);␊ |
141 | ␉␉exit(1);␊ |
142 | ␉ }␊ |
143 | ␊ |
144 | ␉ nc = write(outfile, (void *)data, vmsize);␊ |
145 | ␉ if (nc < (int)vmsize) {␊ |
146 | ␉␉perror("write segment data");␊ |
147 | ␉␉exit(1);␊ |
148 | ␉ }␊ |
149 | ␉ ␊ |
150 | ␉ vm_deallocate(mach_task_self(), data, vmsize);␊ |
151 | ␉ break;␊ |
152 | ␉}␊ |
153 | ␊ |
154 | ␉cp += swap(lcp->cmdsize);␊ |
155 | }␊ |
156 | ␉␊ |
157 | exit(0);␊ |
158 | }␊ |
159 |