-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftype.c
140 lines (119 loc) · 2.53 KB
/
ftype.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
#pragma optimize 79
#pragma noroot
#include <Types.h>
// cp should be a filename w/ .ext
int parse_extension(const char *cp, Word size, Word *ftype, LongWord *atype);
int parse_extension_c(const char *cp, Word *ftype, LongWord *atype)
{
int i;
int pd;
if (!cp || !*cp) return 0;
pd = -1;
for (i = 0; ; ++i)
{
char c;
c = cp[i];
if (c == 0) break;
if (c == '.') pd = i;
}
// pd == position of final .
// i == strlen
if (pd == -1) return 0;
if (pd + 1 >= i) return 0;
pd++; // skip past it...
return parse_extension(cp + pd, i - pd, ftype, atype);
}
// cp is just the extension
int parse_extension(const char *cp, Word size, Word *ftype, LongWord *atype)
{
Word *wp = (Word *)cp;
Word h;
if (!cp || !size) return 0;
h = ((*cp | 0x20) ^ size) & 0x0f;
switch (h)
{
case 0x00:
// shk
if (size == 3
&& (wp[0] | 0x2020) == 0x6873 // 'sh'
&& (cp[2] | 0x20) == 0x6b // 'k'
) {
*ftype = 0xe0;
*atype = 0x8002;
return 1;
}
// text
if (size == 4
&& (wp[0] | 0x2020) == 0x6574 // 'te'
&& (wp[1] | 0x2020) == 0x7478 // 'xt'
) {
*ftype = 0x04;
*atype = 0x0000;
return 1;
}
break;
case 0x01:
// bxy
if (size == 3
&& (wp[0] | 0x2020) == 0x7862 // 'bx'
&& (cp[2] | 0x20) == 0x79 // 'y'
) {
*ftype = 0xe0;
*atype = 0x8000;
return 1;
}
break;
case 0x02:
// asm
if (size == 3
&& (wp[0] | 0x2020) == 0x7361 // 'as'
&& (cp[2] | 0x20) == 0x6d // 'm'
) {
*ftype = 0xb0;
*atype = 0x0003;
return 1;
}
// c
if (size == 1
&& (cp[0] | 0x20) == 0x63 // 'c'
) {
*ftype = 0xb0;
*atype = 0x0008;
return 1;
}
break;
case 0x03:
// pas
if (size == 3
&& (wp[0] | 0x2020) == 0x6170 // 'pa'
&& (cp[2] | 0x20) == 0x73 // 's'
) {
*ftype = 0xb0;
*atype = 0x0005;
return 1;
}
break;
case 0x07:
// txt
if (size == 3
&& (wp[0] | 0x2020) == 0x7874 // 'tx'
&& (cp[2] | 0x20) == 0x74 // 't'
) {
*ftype = 0x04;
*atype = 0x0000;
return 1;
}
break;
case 0x09:
// h
if (size == 1
&& (cp[0] | 0x20) == 0x68 // 'h'
) {
*ftype = 0xb0;
*atype = 0x0008;
return 1;
}
break;
}
return 0;
}