-
Notifications
You must be signed in to change notification settings - Fork 13
/
ipak.c
205 lines (171 loc) · 4.36 KB
/
ipak.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
/* Copyright (C) 2019, 2022 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 <unistd.h>
#include <sys/time.h>
#include "dis.h"
#define MAGIC ((word_t)(014777252031LL))
#define MASK ((word_t)(0557255727156LL)) /* Sixbit MZMZYN */
#define UMASK ((word_t)(0633126423275LL))
#define LEFT 0777777000000LL
#define RIGHT 0777777LL
/* 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 -t|-x[e] [-W<input word format>] [-X<output word format>] <file>\n", x);
usage_word_format ();
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 timestamp)
{
struct timeval tv[2];
unix_time (&tv[0], timestamp);
tv[1] = tv[0];
utimes (filename, tv);
}
static void
extract_file (char *filename, word_t *data, word_t length, word_t key)
{
FILE *f;
int i;
f = fopen(filename, "wb");
for (i = 0; i < length; i++)
{
write_word (f, *data++ ^ key);
}
flush_word (f);
fclose (f);
}
int
main (int argc, char **argv)
{
int ipak_size;
int extract;
char string[7];
word_t word;
word_t *p;
word_t key = 0;
FILE *f;
int opt;
int i;
output_file = stdout;
while ((opt = getopt (argc, argv, "etxW:X:")) != -1)
{
switch (opt)
{
case 'e':
key = MASK;
break;
case 't':
extract = 0;
break;
case 'x':
extract = 1;
break;
case 'W':
if (parse_input_word_format (optarg))
usage (argv[0]);
break;
case 'X':
if (parse_output_word_format (optarg))
usage (argv[0]);
break;
default:
usage (argv[0]);
}
}
if (optind != argc - 1)
usage (argv[0]);
f = fopen (argv[optind], "rb");
p = buffer;
while ((word = get_word (f)) != -1)
{
*p++ = word;
}
fclose (f);
ipak_size = p - buffer;
if (buffer[0] == MAGIC)
{
fprintf (stderr, "Format: 1977\n");
i = 0;
}
else if (buffer[1] == MAGIC)
{
fprintf (stderr, "Format: 1978\n");
sixbit_to_ascii (buffer[0] ^ UMASK, string);
fprintf (stderr, "User: %s\n", string);
i = 1;
}
else if (buffer[4] == MAGIC)
{
fprintf (stderr, "Format: 1980\n");
sixbit_to_ascii (buffer[1] ^ UMASK, string);
fprintf (stderr, "User: %s\n", string);
i = 4;
}
else if (buffer[5] == MAGIC)
{
sixbit_to_ascii (buffer[1] ^ UMASK, string);
fprintf (stderr, "User: %s\n", string);
i = 5;
}
else
{
fprintf (stderr, "Unknown IPAK format.\n");
exit (1);
}
fprintf (stderr, "\nFile name Words Timestamp\n");
while (i < ipak_size)
{
char filename[50];
word_t timestamp;
if (buffer[i] != MAGIC)
fprintf (stderr, "More magic?\n");
sixbit_to_ascii(buffer[i+1], filename);
fprintf (stderr, "%s ", filename);
sixbit_to_ascii(buffer[i+2], filename);
fprintf (stderr, "%s ", filename);
/* File name for extraction. */
weenixpath (filename, -1LL, buffer[i+1], buffer[i+2]);
timestamp = buffer[i+3];
word_t length = buffer[i+4];
fprintf (stderr, "%6lld ", length);
print_datime (stderr, timestamp);
fputc ('\n', stderr);
if (extract)
{
extract_file (filename, &buffer[i+5], length, key);
timestamps (filename, timestamp);
}
i += length + 5;
}
return 0;
}