-
Notifications
You must be signed in to change notification settings - Fork 0
/
binscii.c
316 lines (234 loc) · 5.91 KB
/
binscii.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
311
312
313
314
315
316
#pragma lint -1
#pragma optimize 79
#pragma noroot
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <gsos.h>
#include <window.h>
enum {
BS_OK = 0,
BS_USER_CANCELED,
BS_NOT_BINSCII,
BS_BAD_ALPHABET,
BS_BAD_CRC,
BS_TRUNCATED,
BS_PARTIAL,
};
static int error(int e) {
const char *messages[] = {
"", "",
"12/FiLeStArTfIlEsTaRt not found/^#0",
"12/Bad alphabet/^#0",
"12/Bad CRC/^#0",
"12/BINSCII data truncated/^#0",
"12/Multi-segment files are not supported/^#0",
};
if (e >= BS_NOT_BINSCII && e <= BS_PARTIAL)
AlertWindow(awPointer, NULL, (Ref)messages[e]);
return e;
}
extern unsigned crc_update(unsigned, void *, unsigned);
int FilePrompt(GSString255 *name, unsigned ftype, unsigned atype);
/*
FiLeStArTfIlEsTaRt
alphabet [64 bytes]
header [52 bytes]
data [64 bytes]
....
checksum [4 bytes]
alphabet =
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789()
(not hardcoded as some mailers changed paren characters)
*/
struct header {
uint8_t file_size[3];
uint8_t seg_start[3];
uint8_t access;
uint8_t file_type;
uint8_t aux_type[2];
uint8_t storage_type;
uint8_t block_count[2];
uint8_t create_date[2];
uint8_t create_time[2];
uint8_t mod_date[2];
uint8_t mod_time[2];
uint8_t seg_len[3];
uint8_t crc[2];
uint8_t reserved;
};
static uint8_t alphabet[256];
static int decode(const uint8_t *src, unsigned len, uint8_t *dest) {
unsigned i;
len >>= 2;
for (i = 0; i < len; ++i) {
unsigned c0 = alphabet[src[0]];
unsigned c1 = alphabet[src[1]];
unsigned c2 = alphabet[src[2]];
unsigned c3 = alphabet[src[3]];
// todo - if any are > 64, indicate a file error.
dest[0] = (c3 << 2) | (c2 >> 4);
dest[1] = (c2 << 4) | (c1 >> 2);
dest[2] = (c1 << 6) | (c0);
dest += 3;
src += 4;
}
return (len << 1) + len;
}
static int de_scii(const uint8_t *ptr, long size) {
// ptr points to line AFTER FiLeStArTfIlEsTaRt
static struct header header;
static GSString32 filename;
static uint8_t buffer[1536]; /* 1536 = 512 * 3 == 48 * 32 */
static RefNumRecGS closeDCB = { 1, 0 };
static IORecGS ioDCB = { 4, 0, buffer, 0, 0 };
uint32_t file_size;
uint32_t seg_start;
uint32_t seg_len;
unsigned i;
unsigned c;
unsigned crc;
unsigned refNum;
if (size < 64 + 52 + 4 + 2) {
return BS_TRUNCATED;
}
memset(alphabet, 0xff, 256);
for (i = 0; i < 64; ++i) {
c = ptr[i];
if (isspace(c)) return BS_BAD_ALPHABET;
if (alphabet[c] != 0xff) return BS_BAD_ALPHABET;
alphabet[c] = i;
}
for (i = 64; isspace(ptr[i]); ++i, --size);
ptr += i;
size -= i;
if (size < 52 + 4) {
return BS_TRUNCATED;
}
// name length
filename.length = ptr[0] & 0x1f;
memcpy(filename.text, ptr + 1, filename.length);
decode(ptr + 16, 36, (char *)&header);
for (i = 52; isspace(ptr[i]); ++i);
ptr += i;
size -= i;
crc = crc_update(0, &header, sizeof(header) - 3);
file_size = *(uint32_t *)&header.file_size & 0xffffff;
seg_start = *(uint32_t *)&header.seg_start & 0xffffff;
seg_len = *(uint32_t *)&header.seg_len & 0xffffff;
if (crc != *(uint16_t *)&header.crc) return BS_BAD_CRC;
if (seg_start != 0 || seg_len != file_size) return BS_PARTIAL; // partial file
// check for directory file type / aux type?
refNum = FilePrompt((GSString255 *)&filename, header.file_type, *(uint16_t *)&header.aux_type);
if (refNum == 0) return BS_USER_CANCELED;
ioDCB.refNum = refNum;
closeDCB.refNum = refNum;
unsigned offset = 0;
unsigned j = 0;
unsigned count = (seg_len + 47) / 48; // assume < 3MB
crc = 0;
for(j = 0; j < count; ++j) {
if (size < 64 + 4) return BS_TRUNCATED;
if (offset == 1536) {
crc = crc_update(crc, buffer, 1536);
ioDCB.requestCount = 1536;
WriteGS(&ioDCB);
offset = 0;
file_size -= 1536;
}
decode(ptr, 64, buffer + offset);
offset += 48;
for (i = 64; isspace(ptr[i]); ++i);
ptr += i;
size -= i;
}
if (offset) {
crc = crc_update(crc, buffer, offset);
ioDCB.requestCount = file_size;
WriteGS(&ioDCB);
}
CloseGS(&closeDCB);
// todo - set create/mod dates?
if (size < 4) return BS_TRUNCATED; // trailing crc.
decode(ptr, 4, header.crc);
if (crc != *(uint16_t *)&header.crc) return BS_BAD_CRC;
return BS_OK;
}
#define MAGIC "FiLeStArTfIlEsTaRt\r"
int DecodeBINSCII(const uint8_t *ptr, long size) {
/* find the sentinel... */
unsigned max;
unsigned i;
unsigned c;
unsigned prev = '\r';
//expect it within the first 32k...
if (size < 18) return error(BS_NOT_BINSCII);
if (size >> 16) max = 0x8000;
else max = size - 18;
for (i = 0; i < max; ++i) {
c = ptr[i];
if (c == 'F' && prev == '\r') {
if (i + 18 >= size) return error(BS_NOT_BINSCII);
if (!memcmp(ptr + i, MAGIC, 19)) {
i += 19;
size -= i;
ptr += i;
return error(de_scii(ptr, size));
}
}
prev = c;
}
return error(BS_NOT_BINSCII);
}
#if 0
int DecodeUUE(const uint8_t *ptr, long size) {
/* find the sentinel */
/* begin mode name .... end*/
/* begin-base64 mode name ... ==== */
int b64 = 0;
if (size < 9) return error(BS_NOT_BINSCII);
if (size >> 16) max = 0x8000;
else max = size - 9;
for (i = 0; i < max; ++i) {
c = ptr[i];
if (c == 'b' && prev == '\r') {
if (i + 9 >= size) return error(BS_NOT_BINSCII);
if (!memcmp(ptr + i, "begin", 5)) {
i += 5;
if (memcmp(ptr + i, "-base64 ", 8)) {
b64 = 1;
i += 8;
ptr += i;
size -= i;
return error(de_base(ptr + i, size));
}
else if (ptr[i] == ' ') {
b64 = 0;
i += 1;
ptr += i;
size -= i;
return error(de_uue(ptr + i, size));
} else {
prev = 'n';
continue;
}
}
}
prev = c;
}
return error(BS_NOT_BINSCII);
}
#endif
#ifdef TEST
const char file[] =
"FiLeStArTfIlEsTaRt\r"
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789()\r"
"Ax AAQAAAAAAAwwIEAA2LFAS5wKOsi9AAQAAgXx\r"
"AAAeAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r"
"AgN1\r"
;
int main(int argc, char **argv) {
return DecodeBINSCII(file, sizeof(file));
}
#endif