-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappledouble.cpp
230 lines (172 loc) · 4.09 KB
/
appledouble.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* convert a file to an appledouble file.
*
*
*/
#include <system_error>
#include <string>
#include <vector>
#include <memory>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#ifdef _WIN32
#include "win.h"
#else
#include <err.h>
#include <sysexits.h>
#include <arpa/inet.h>
#endif
#include "mapped_file.h"
#include <afp/finder_info.h>
#include <afp/resource_fork.h>
#include "applefile.h"
#ifndef O_BINARY
#define O_BINARY 0
#endif
void usage() {
fputs("Usage: appledouble [-hv] [-o outfile] file ...\n", stderr);
exit(EX_USAGE);
}
void help() {
fputs(
"Usage: appledouble [-hv] [-o outfile] file ...\n"
"\n"
" -o Specify outfile name\n"
" -h Display help\n"
" -v Be verbose\n",
stdout);
exit(EX_OK);
}
bool _v = false;
int _rv = 0;
/* check if a file is apple single or apple double format (or neither). */
uint32_t classify(const mapped_file &mf) {
if (mf.size() < sizeof(ASHeader)) return 0;
const ASHeader *header = (const ASHeader *)mf.data();
if (header->magicNum == htonl(APPLESINGLE_MAGIC)) return APPLESINGLE_MAGIC;
if (header->magicNum == htonl(APPLEDOUBLE_MAGIC)) return APPLEDOUBLE_MAGIC;
return 0;
}
/*
* cases to consider
* 1. single file w/ fork/finder info
* 2. single file + appledouble file
* 3. single file w/o fork/finder info
* 4. file is already apple single / apple double format.
*/
void one_file(const std::string &infile, const std::string &outfile) {
// 1. check if apple single/apple double
mapped_file mf;
ASHeader head;
ASEntry e;
std::error_code ec;
try {
mf = mapped_file(infile);
} catch(std::exception &ex) {
warnx("%s: %s", infile.c_str(), ex.what());
_rv = 1;
return;
}
uint32_t fmt = classify(mf);
if (fmt == APPLESINGLE_MAGIC) {
warnx("%s: File is apple single format.", infile.c_str());
return;
}
if (fmt == APPLEDOUBLE_MAGIC) {
warnx("%s: File is apple double format.", infile.c_str());
return;
}
// flag for preferred data source?
// 1. check for native fork
// 2. check for apple double data.
int count = 0;
afp::finder_info fi;
afp::resource_fork rf;
size_t rfork_size = 0;
std::unique_ptr<uint8_t[]> rfork_data;
bool fi_ok = fi.open(infile, ec);
bool rf_ok = rf.open(infile, afp::resource_fork::read_only, ec);
if (rf_ok) {
rfork_size = rf.size(ec);
if (rfork_size) {
rfork_data.reset(new uint8_t[rfork_size]);
rfork_size = rf.read(rfork_data.get(), rfork_size, ec);
if (ec) {
warnx("%s resource fork: %s", infile.c_str(), ec.message().c_str());
return;
}
}
rf.close();
}
if (!rfork_size) rf_ok = false;
if (!fi_ok && !rf_ok) {
warnx("%s: File is not extended.", infile.c_str());
return;
}
if (rf_ok) count++;
if (fi_ok) count++;
int fd = open(outfile.c_str(), O_WRONLY | O_CREAT | O_BINARY, 0666);
memset(&head, 0, sizeof(head));
head.magicNum = htonl(APPLEDOUBLE_MAGIC);
head.versionNum = htonl(0x00020000);
head.numEntries = htons(count);
write(fd, &head, sizeof(head));
off_t offset = sizeof(ASHeader) + sizeof(ASEntry) * count;
// 1 - finder info
if (fi_ok) {
e.entryID = htonl(AS_FINDERINFO);
e.entryOffset = htonl(offset);
e.entryLength = htonl(32);
write(fd, &e, sizeof(e));
offset += 32;
}
// 2 - resource fork?
if (rf_ok) {
e.entryID = htonl(AS_RESOURCE);
e.entryOffset = htonl(offset);
e.entryLength = htonl(rfork_size);
write(fd, &e, sizeof(e));
offset += rfork_size;
}
// now write it..
// 1 - finder info
if (fi_ok) {
write(fd, fi.data(), 32);
}
// 2 - resource fork?
if (rf_ok) {
write(fd, rfork_data.get(), rfork_size);
}
close(fd);
}
int main(int argc, char **argv) {
std::string _o;
int c;
while ((c = getopt(argc, argv, "o:v")) != -1) {
switch(c) {
case 'v': _v = true; break;
case 'h': help(); break;
case 'o': _o = optarg; break;
case ':':
case '?':
default:
usage();
break;
}
}
argv += optind;
argc -= optind;
if (!argc) usage();
for (int i = 0; i < argc; ++i) {
std::string s(argv[i]);
if (_o.empty()) _o = "._" + s;
one_file(s, _o);
_o.clear();
}
return _rv;
}