-
Notifications
You must be signed in to change notification settings - Fork 13
/
itsarc.c
310 lines (250 loc) · 6.21 KB
/
itsarc.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Copyright (C) 2017 Lars Brinkhoff <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <time.h>
#include <stdio.h>
#include <sys/time.h>
#include "dis.h"
#define VERY_OLD_ARC ((word_t)(0777777777777LL))
#define OLD_ARC ((word_t)(0416243010101LL)) /* Sixbit ARC!!! */
#define NEW_ARC ((word_t)(0416243210101LL)) /* Sixbit ARC1!! */
#define LEFT 0777777000000LL
#define RIGHT 0777777LL
static int old = 0;
/* Just allocate a full moby to hold the file. */
static word_t buffer[256 * 1024];
static void usage (const char *x)
{
fprintf (stderr, "Usage: %s -x|-t <file>\n", x);
exit (1);
}
static void
unix_time (struct timeval *tv, word_t t)
{
struct tm tm;
int seconds = (t & RIGHT) / 2;
int date = (t >> 18);
tm.tm_sec = seconds % 60;
tm.tm_min = (seconds / 60) % 60;
tm.tm_hour = seconds / 3600;
tm.tm_mday = (date & 037);
tm.tm_mon = ((date & 0740) >> 5) - 1;
tm.tm_year = (date & 0777000) >> 9;
tm.tm_isdst = 0;
tv->tv_sec = mktime (&tm);
tv->tv_usec = (t & 1) * 500000L;
}
static void
timestamps (char *filename, word_t modified, word_t referenced)
{
struct timeval tv[2];
unix_time (&tv[0], referenced);
unix_time (&tv[1], modified);
utimes (filename, tv);
}
static void
extract_file (char *filename, word_t *data, word_t length)
{
FILE *f;
int i;
f = fopen(filename, "wb");
for (i = 0; i < length; i++)
{
write_word (f, *data++);
}
flush_word (f);
fclose (f);
}
static int
ildb (word_t **w, int *p)
{
word_t b = **w;
b = b >> (30 - 6*(*p));
b &= 077;
(*p)++;
if (*p == 6)
{
*p = 0;
(*w)++;
}
return (int)b;
}
static int
extract_block (FILE *f, word_t *block, int *b, int *count)
{
word_t header = *block;
int i, n;
*b = header & 017777777;
n = ((header >> 23) & 01777) + 1;
*count += n;
if (f)
{
block++;
for (i = 0; i < n; i++)
write_word (f, block[i]);
}
return (header & 0200000000000LL) == 0;
}
static int
extract_blocks (FILE *f, word_t *ufd, int undscp)
{
word_t *d;
int o, b, n, n2, n3;
int count = 0;
d = &ufd[11+undscp/6];
o = undscp % 6;
n = ildb (&d, &o);
if (n != 040)
{
fprintf (stderr, "ERROR\n");
exit (1);
}
n2 = ildb (&d, &o);
n3 = ildb (&d, &o);
b = ((n & 037) << 12) + (n2 << 6) + n3;
b = buffer[02005+b];
while (extract_block (f, &buffer[b], &b, &count))
;
return count;
}
static int
extract_old_file (char *filename, int i, int extract)
{
word_t *ufd = buffer;
int undscp = ufd[i+2] & 017777;
FILE *f = NULL;
int n;
if (extract)
f = fopen(filename, "wb");
n = extract_blocks (f, ufd, undscp);
if (extract)
{
flush_word (f);
fclose (f);
}
return n;
}
int
main (int argc, char **argv)
{
int extract;
char string[7];
word_t word;
word_t *p;
FILE *f;
input_word_format = &its_word_format;
output_word_format = &its_word_format;
output_file = stdout;
if (argc != 3)
usage (argv[0]);
if (argv[1][0] != '-')
usage (argv[0]);
switch (argv[1][1])
{
case 't':
extract = 0;
break;
case 'x':
extract = 1;
break;
default:
usage (argv[0]);
break;
}
f = fopen (argv[2], "rb");
p = buffer;
while ((word = get_word (f)) != -1)
{
*p++ = word;
}
fclose (f);
/* word_t arc_size = p - buffer; */
if (buffer[0] == NEW_ARC)
{
/* fprintf (stderr, "New ARC1!! archive.\n") */ ;
}
else if (buffer[0] == OLD_ARC)
{
fprintf (stderr, "Old ARC!!! archive.\n");
old = 1;
}
else if (buffer[0] == VERY_OLD_ARC)
{
fprintf (stderr, "Old 777777777777 archive.\n");
old = 1;
}
else
{
sixbit_to_ascii(buffer[0], string);
fprintf (stderr, "First word: %012llo \"%s\"\n", buffer[0], string);
fprintf (stderr, "Not an ARC file.\n");
exit (1);
}
word_t name_beg;
if (old)
name_beg = buffer[2];
else
name_beg = buffer[1];
/* word_t data_end = buffer[2]; */
fprintf (stderr, "Last cleanup: ");
print_datime (stderr, buffer[3]);
fputc ('\n', stderr);
if (!old)
{
fprintf (stderr, "Created: ");
print_datime (stderr, buffer[4]);
fputc ('\n', stderr);
word_t dumped = buffer[5];
fprintf (stderr, "Dumped: %llo\n", dumped);
}
fprintf (stderr, "\nFile name Words Modified Referenced Byte\n");
int i;
for (i = name_beg; i < 02000; i += 5)
{
char filename[14];
word_t modified, referenced;
sixbit_to_ascii(buffer[i], filename);
fprintf (stderr, "%s ", filename);
sixbit_to_ascii(buffer[i+1], filename + 7);
fprintf (stderr, "%s ", filename + 7);
/* File name for extraction. */
weenixpath (filename, -1LL, buffer[i], buffer[i+1]);
/* word_t flags = buffer[i+2] >> 18; */
word_t data = buffer[i+2] & RIGHT;
modified = buffer[i+3];
referenced = (buffer[i+4] & LEFT);
word_t length;
if (old) {
length = extract_old_file (filename, i, extract);
timestamps (filename, modified, referenced);
} else
length = buffer[data] - 3;
fprintf (stderr, "%6lld ", length);
print_datime (stderr, modified);
fputs (" ", stderr);
print_date (stderr, referenced);
if (!old)
{
int author = (buffer[i+4] >> 9) & 0777;
if (author != 0 && author != 0777)
fprintf (stderr, " Author %03o", author);
}
int leftovers;
fprintf (stderr, " %d\n",
byte_size (buffer[i+4] & 0777, &leftovers));
if (!old && extract)
{
extract_file (filename, &buffer[data+3], length);
timestamps (filename, modified, referenced);
}
}
return 0;
}