forked from e-n-f/macbinary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacbinary.c
156 lines (128 loc) · 2.87 KB
/
macbinary.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
int text = 0;
int named = 0;
unsigned int read32be(unsigned char *c) {
return (c[0] << 24) |
(c[1] << 16) |
(c[2] << 8) |
(c[3] << 0);
}
int process(FILE *f, char *fname) {
unsigned char header[128];
if (fread(header, sizeof(unsigned char), 128, f) != 128) {
fprintf(stderr, "Short header read for %s\n", fname);
return EXIT_FAILURE;
}
if (header[0] != 0) {
fprintf(stderr, "Wrong byte 0 for %s\n", fname);
return EXIT_FAILURE;
}
if (header[82] != 0) {
fprintf(stderr, "Wrong byte 82 for %s\n", fname);
return EXIT_FAILURE;
}
int namelen = header[1];
if (namelen > 63) {
fprintf(stderr, "Impossible name length %d for %s\n", namelen, fname);
return EXIT_FAILURE;
}
char name[64];
memcpy(name, header + 2, namelen);
name[namelen] = 0;
char type[5] = " ";
char creator[5] = " ";
memcpy(type, header + 65, 4);
memcpy(creator, header + 69, 4);
int textual = text && (strcmp(type, "TEXT") == 0);
int len = read32be(header + 83);
int mtime = read32be(header + 91) - 2082844800;
fprintf(stderr, "converting %s (%s/%s) %d bytes\n", name, type, creator, len);
FILE *out = stdout;
if (named) {
char *cp;
for (cp = name; *cp; cp++) {
if (*cp == '/') {
*cp = ':';
}
}
out = fopen(name, "w");
if (out == NULL) {
fprintf(stderr, "Can't open %s to extract %s: %s\n",
name, fname, strerror(errno));
return EXIT_FAILURE;
}
}
int i;
for (i = 0; i < len; i++) {
int c = getc(f);
if (textual) {
if (c == '\r') {
c = '\n';
} else if (c == '\n') {
c = '\r';
}
}
putc(c, out);
}
struct timeval times[2];
times[0].tv_sec = mtime;
times[0].tv_usec = 0;
times[1].tv_sec = mtime;
times[1].tv_usec = 0;
if (out != stdout) {
fclose(out);
if (utimes(name, times) != 0) {
perror("utimes");
}
} else {
fflush(stdout);
if (futimes(1, times) != 0) {
perror("futimes");
}
}
return EXIT_SUCCESS;
}
void usage(char **argv) {
fprintf(stderr, "Usage: %s [-tO] [file ...]\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "-t: Swap \\r and \\n for TEXT type files\n");
fprintf(stderr, "-O: Write output to file named in archive, not standard output\n");
}
int main(int argc, char **argv) {
extern int optind;
extern char *optarg;
int i;
int success = EXIT_SUCCESS;
while ((i = getopt(argc, argv, "tO")) != -1) {
switch (i) {
case 't':
text = 1;
break;
case 'O':
named = 1;
break;
default:
usage(argv);
exit(EXIT_FAILURE);
}
}
if (optind < argc) {
for (i = optind; i < argc; i++) {
FILE *f = fopen(argv[i], "rb");
if (f == NULL) {
perror(argv[i]);
} else {
success = success | process(f, argv[i]);
fclose(f);
}
}
} else {
success = success | process(stdin, "standard input");
}
return success;
}