-
Notifications
You must be signed in to change notification settings - Fork 1
/
unfork.cpp
192 lines (142 loc) · 4.36 KB
/
unfork.cpp
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
185
186
187
188
189
190
191
192
#include <string>
#include <stdexcept>
#include <algorithm>
#include <functional>
#include <system_error>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sysexits.h>
#include "applefile.h"
#include "mapped_file.h"
void damaged_file() {
throw std::runtime_error("File is damaged.");
}
void throw_errno() {
throw std::system_error(errno, std::generic_category());
}
void throw_errno(const std::string &what) {
throw std::system_error(errno, std::generic_category(), what);
}
class defer {
public:
typedef std::function<void()> FX;
defer() = default;
defer(FX fx) : _fx(fx) {}
defer(const defer &) = delete;
defer(defer &&) = default;
defer & operator=(const defer &) = delete;
defer & operator=(defer &&) = default;
void cancel() { _fx = nullptr; }
~defer() { if (_fx) _fx(); }
private:
FX _fx;
};
void unfork(const char *in, const char *out) {
static_assert(sizeof(ASHeader) == 26, "ASHeader size is wrong.");
static_assert(sizeof(ASEntry) == 12, "ASEntry size is wrong.");
mapped_file mf(in, mapped_file::priv);
if (mf.size() < sizeof(ASHeader)) damaged_file();
ASHeader *header = (ASHeader *)mf.data();
header->magicNum = ntohl(header->magicNum);
header->versionNum = ntohl(header->versionNum);
header->numEntries = ntohs(header->numEntries);
if (header->magicNum != 0x00051600 || header->versionNum != 0x00020000 || header->numEntries == 0)
throw std::runtime_error("Not an AppleSingle File");
if (header->numEntries * sizeof(ASEntry) + sizeof(ASHeader) > mf.size()) damaged_file();
ASEntry *begin = (ASEntry *)(mf.data() + sizeof(ASHeader));
ASEntry *end = &begin[header->numEntries];
std::for_each(begin, end, [](ASEntry &e){
e.entryID = ntohl(e.entryID);
e.entryOffset = ntohl(e.entryOffset);
e.entryLength = ntohl(e.entryLength);
});
// check for truncation....
for (auto iter = begin; iter != end; ++iter) {
const auto &e = *iter;
if (!e.entryLength) continue;
if (e.entryOffset > mf.size()) damaged_file();
if (e.entryOffset + e.entryLength > mf.size()) damaged_file();
}
std::string outname = out ? out : "";
// if no name, pull it from the name record.
if (!out) {
auto iter = std::find_if(begin, end, [](const ASEntry &e) { return e.entryID == AS_REALNAME; });
if (iter != end) {
outname.assign((const char *)mf.data() + iter->entryOffset, iter->entryLength);
}
}
if (outname.empty()) throw std::runtime_error("No filename");
int fd = open(outname.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0666);
if (fd < 1) throw_errno();
defer close_fd([fd](){ close(fd); });
for (auto iter = begin; iter != end; ++iter) {
const auto &e = *iter;
if (e.entryLength == 0) continue;
switch(e.entryID) {
case AS_DATA: {
ssize_t ok = write(fd, mf.data() + e.entryOffset, e.entryLength);
if (ok < 0) throw_errno();
//if (ok != e.entryLength) return -1;
break;
}
case AS_RESOURCE: {
int rfd = openat(fd, "com.apple.ResourceFork", O_XATTR | O_CREAT | O_TRUNC | O_WRONLY, 0666);
if (rfds < 0) throw_errno("com.apple.ResourceFork");
defer close_fd([rfd](){ close(rfd); });
ssize_t ok = write(rfd, mf.data() + e.entryOffset, e.entryLength);
if (ok < 0) throw_errno("com.apple.ResourceFork");
//if (ok != e.entryLength) return -1;
break;
}
case AS_FINDERINFO: {
if (e.entryLength != 32) {
fputs("Warning: Invalid Finder Info size.\n", stderr);
break;
}
int rfd = openat(fd, "com.apple.FinderInfo", O_XATTR | O_CREAT | O_TRUNC | O_WRONLY, 0666);
if (rfd < 0) throw_errno("com.apple.ResourceFork");
defer close_fd([rfd](){ close(rfd); });
ssize_t ok = write(rfd, mf.data() + e.entryOffset, e.entryLength);
if (ok < 0) throw_errno("com.apple.FinderInfo");
//if (ok != e.entryLength) return -1;
break;
}
}
}
}
void usage() {
fputs("Usage: unfork [-o file] file...\n", stderr);
exit(EX_USAGE);
}
int main(int argc, char **argv) {
int c;
char *_o;
while ((c = getopt(argc, argv, "o:")) != -1) {
switch(c) {
case 'o':
_o = optarg;
break;
case ':':
case '?':
default:
usage();
break;
}
}
argv += optind;
argc -= optind;
int rv = 0;
for (int i = 0; i < argc; ++i) {
try { unfork(argv[i], _o); }
catch (std::exception &ex) {
fprintf(stderr, "%s : %s\n", argv[i], ex.what());
rv = 1;
}
_o = nullptr;
}
return rv;
}