-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoasis_utils.c
254 lines (226 loc) · 8.08 KB
/
oasis_utils.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
/*
* OASIS Utility Functions
*
* www.github.com/hharte/oasis
*
* Copyright (c) 2021-2022, Howard M. Harte
*
* Reference: http://bitsavers.org/pdf/phaseOneSystems/oasis/
*
*/
#if defined(_WIN32)
# define _CRT_SECURE_NO_DEPRECATE
# include <windows.h>
#else /* if defined(_WIN32) */
# include <sys/types.h>
# include <utime.h>
#endif /* if defined(_WIN32) */
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "./oasis.h"
#include "./oasis_utils.h"
const char kPathSeparator =
#ifdef _WIN32
'\\';
#else /* ifdef _WIN32 */
'/';
#endif /* ifdef _WIN32 */
/* Packed timestamp:
* +-------------------------------------------------+
* | BYTE | 0 | 1 | 2 |
* | FIELD | MMMM | DDDD | DYYY | YHHH | HHMM | MMMM |
* | BIT | 7 0 | 7 0 | 7 0 |
* +-------------------------------------------------+
*/
void oasis_convert_timestamp_to_tm(oasis_tm_t *timestamp, struct tm *tmout) {
tmout->tm_mon = ((timestamp->raw[0] & 0xF0) >> 4) - 1;
tmout->tm_mday =
((timestamp->raw[0] & 0x0F) << 1) | ((timestamp->raw[1] & 0x80) >> 7);
/* 0 = 1977, highest valid year is 1992. */
tmout->tm_year = ((timestamp->raw[1] & 0x78) >> 3) + 77;
tmout->tm_hour =
((timestamp->raw[1] & 0x07) << 2) | ((timestamp->raw[2] & 0xc0) >> 6);
tmout->tm_min = timestamp->raw[2] & 0x3F;
tmout->tm_sec = 0;
tmout->tm_isdst = 0;
tmout->tm_wday = 0;
tmout->tm_yday = 0;
}
void oasis_convert_tm_to_timestamp(struct tm *tmin, oasis_tm_t *timestamp) {
timestamp->raw[0] = ((tmin->tm_mon & 0xF) + 1) << 4;
timestamp->raw[0] |= ((tmin->tm_mday >> 1) & 0x0F);
timestamp->raw[1] = ((tmin->tm_mday & 1) << 7);
timestamp->raw[1] |= ((tmin->tm_year - 77) & 0x0F) << 3;
timestamp->raw[1] |= ((tmin->tm_hour >> 2) & 0x07);
timestamp->raw[2] = ((tmin->tm_hour & 0x03) << 6);
timestamp->raw[2] |= ((tmin->tm_min & 0x3F));
}
void oasis_list_dir_entry(directory_entry_block_t *dir_entry) {
char fname[FNAME_LEN + 1];
char fext[FEXT_LEN + 1];
char format[8];
char other_str[8];
char tmp_str[FNAME_LEN + 1];
struct tm tmout;
char buf[17];
if (dir_entry->file_format == 0) {
return;
}
memcpy(tmp_str, dir_entry->file_name, FNAME_LEN);
tmp_str[8] = '\0';
snprintf(fname, sizeof(fname), "%s", tmp_str);
memcpy(tmp_str, dir_entry->file_type, FEXT_LEN);
snprintf(fext, sizeof(fext), "%s", tmp_str);
snprintf(other_str, sizeof(other_str), " ");
switch (dir_entry->file_format) {
case FILE_FORMAT_DELETED:
snprintf(format, sizeof(format), "Deleted");
break;
case FILE_FORMAT_EMPTY:
snprintf(format, sizeof(format), "Empty ");
break;
case FILE_FORMAT_SYNONYM:
snprintf(format, sizeof(format), "Synonym");
break;
default:
switch (dir_entry->file_format & 0x1F) {
case FILE_FORMAT_RELOCATABLE:
snprintf(format,
sizeof(format),
"R %5d",
dir_entry->file_format_dependent1);
if (dir_entry->file_format_dependent2 > 0) {
snprintf(other_str,
sizeof(other_str),
"%5d L",
dir_entry->file_format_dependent2);
}
break;
case FILE_FORMAT_ABSOLUTE:
snprintf(format,
sizeof(format),
"A %5d",
dir_entry->file_format_dependent1);
snprintf(other_str,
sizeof(other_str),
"%5d O",
dir_entry->file_format_dependent2);
break;
case FILE_FORMAT_SEQUENTIAL:
snprintf(format,
sizeof(format),
"S %5d",
dir_entry->file_format_dependent1);
if (dir_entry->file_format_dependent2 > 0) {
snprintf(other_str,
sizeof(other_str),
"%5d E",
dir_entry->file_format_dependent2);
}
break;
case FILE_FORMAT_DIRECT:
snprintf(format,
sizeof(format),
"D %5d",
dir_entry->file_format_dependent1);
break;
case FILE_FORMAT_INDEXED:
snprintf(format,
sizeof(format),
"I%3d/%3d",
dir_entry->file_format_dependent1 & 0x01FF,
(dir_entry->file_format_dependent1 & 0xFE00) >> 9);
break;
case FILE_FORMAT_KEYED:
snprintf(format,
sizeof(format),
"K%3d/%3d",
dir_entry->file_format_dependent1 & 0x01FF,
(dir_entry->file_format_dependent1 & 0xFE00) >> 9);
break;
default:
printf("Unsupported file type %x\n", dir_entry->file_format);
break;
}
}
oasis_convert_timestamp_to_tm(&dir_entry->timestamp, &tmout);
strftime(buf, 17, "%m/%d/%y %H:%M", &tmout);
printf("%s %s %s %5d %4d %s %5d %3d %3d %s\n",
fname,
fext,
buf,
dir_entry->record_count,
dir_entry->block_count,
format,
dir_entry->start_sector,
dir_entry->owner_id,
dir_entry->shared_from_owner_id,
other_str);
}
void set_file_time(char *output_filename, struct tm *tmin) {
#if defined(_WIN32)
FILETIME file_time;
SYSTEMTIME system_time;
HANDLE hOstream;
GetSystemTime(&system_time);
system_time.wDay = tmin->tm_mday;
system_time.wMonth = tmin->tm_mon;
system_time.wYear = tmin->tm_year + 1900;
system_time.wHour = tmin->tm_hour;
system_time.wMinute = tmin->tm_min;
system_time.wSecond = 0;
SystemTimeToFileTime(&system_time, &file_time);
hOstream = CreateFileA(output_filename,
FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
SetFileTime(hOstream, &file_time, &file_time, &file_time);
#else /* POSIX */
struct utimbuf file_time;
file_time.actime = mktime(tmin);
file_time.modtime = mktime(tmin);
if (utime(output_filename, &file_time)) {
fprintf(stderr, "Error setting timestamp for %s\n", output_filename);
}
#endif /* if defined(_WIN32) */
}
void dump_hex(uint8_t *data, int len) {
uint8_t ascii[32] = { 0 };
uint8_t *pascii = ascii;
printf("\n");
if (len > 0) {
uint16_t i;
printf("\tData:");
for (i = 0; i < len; i++) {
if (i % 16 == 0) {
if (i > 0) {
*pascii++ = '\0';
printf("%s", ascii);
}
printf("\n\t%03x: ", i);
pascii = ascii;
}
printf("%02x, ", data[i]);
if (((data[i]) >= 0x20) && (data[i] < 0x7F)) {
*pascii++ = data[i];
} else {
*pascii++ = '.';
}
}
*pascii++ = '\0';
if (strnlen((char *)ascii, sizeof(ascii)) > 0) {
for (i = 0; i < 16 - strnlen((char *)ascii, sizeof(ascii)); i++) {
printf(" ");
}
printf("%s", ascii);
}
}
printf("\n");
}