File: mu.c

package info (click to toggle)
macutils 2.0b3-17
  • links: PTS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,256 kB
  • sloc: ansic: 12,737; makefile: 661
file content (225 lines) | stat: -rwxr-xr-x 4,891 bytes parent folder | download | duplicates (7)
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
#include "hexbin.h"
#ifdef MU
#include "globals.h"
#include "readline.h"
#include "../util/masks.h"
#include "../util/util.h"
#include "../fileio/machdr.h"
#include "../fileio/wrfile.h"
#include "buffer.h"
#include "printhdr.h"

extern void exit();

static void do_mu_fork();
static int mu_comp_to_bin();
static int mu_convert();

/* mu format -- process .mu files */
void mu(macname)
char *macname;
{
    int n;

    for(n = 0; n < INFOBYTES; n++) {
	info[n] = 0;
    }

    /* set up name for output files */
    if(macname[0] == '\0') {
	n = 0;
	while(line[n] != '"') {
	    n++;
	}
	macname = line + n + 1;
	line[strlen(line) - 1] = 0;
    }
    n = strlen(macname);
    if(n > F_NAMELEN) {
	n = F_NAMELEN;
    }
    (void)strncpy(mh.m_name, macname, n);
    mh.m_name[n] = '\0';
    info[I_NAMEOFF] = n;
    (void)strncpy(info + I_NAMEOFF + 1, mh.m_name, n);

    if(listmode) {
	(void)fprintf(stderr, "This file is in \"mu\" format.\n");
    }
    transname(mh.m_name, trname, n);
    define_name(trname);
    print_header0(0);
    set_put(0);
    set_put(1);
    do_mu_fork();
    mh.m_datalen = data_size;
    if(!readline()) {
	(void)fprintf(stderr, "Premature EOF\n");
#ifdef SCAN
	do_error("hexbin: Premature EOF");
#endif /* SCAN */
	exit(1);
    }
    if(strncmp(line, "begin ", 6)) {
	(void)fprintf(stderr, "No UU header found.\n");
#ifdef SCAN
	do_error("hexbin: No UU header found");
#endif /* SCAN */
	exit(1);
    }
    if(!strncmp(line + 10, " .rsrc", 6)) {
	set_put(0);
	do_mu_fork();
	mh.m_rsrclen = rsrc_size;
	if(!readline()) {
	    (void)fprintf(stderr, "Premature EOF\n");
#ifdef SCAN
	    do_error("hexbin: Premature EOF");
#endif /* SCAN */
	    exit(1);
	}
	if(strncmp(line, "begin ", 6)) {
	    (void)fprintf(stderr, "No UU header found.\n");
#ifdef SCAN
	    do_error("hexbin: No UU header found");
#endif /* SCAN */
	    exit(1);
	}
    } else {
	mh.m_rsrclen = 0;
    }
    if(strncmp(line + 10, " .finfo", 7)) {
	(void)fprintf(stderr, "No finder info found.\n");
#ifdef SCAN
	do_error("hexbin: No finder info found");
#endif /* SCAN */
	exit(1);
    }
    if(!readline()) {
	(void)fprintf(stderr, "Premature EOF\n");
#ifdef SCAN
	do_error("hexbin: Premature EOF");
#endif /* SCAN */
	exit(1);
    }
    (void)mu_convert(line, info + I_TYPEOFF);
    if(!readline()) {
	(void)fprintf(stderr, "Premature EOF\n");
#ifdef SCAN
	do_error("hexbin: Premature EOF");
#endif /* SCAN */
	exit(1);
    }
    if(mu_convert(line, line)) {
	(void)fprintf(stderr, "Long finderinfo.\n");
#ifdef SCAN
	do_error("hexbin: Long finderinfo");
#endif /* SCAN */
	exit(1);
    }
    if(!readline()) {
	(void)fprintf(stderr, "Premature EOF\n");
#ifdef SCAN
	do_error("hexbin: Premature EOF");
#endif /* SCAN */
	exit(1);
    }
    if(strncmp(line, "end", 3)) {
	(void)fprintf(stderr, "\"end\" line missing.\n");
#ifdef SCAN
	do_error("hexbin: \"end\" line missing");
#endif /* SCAN */
	exit(1);
    }

    (void)strncpy(mh.m_type, info + I_TYPEOFF, 4);
    (void)strncpy(mh.m_author, info + I_AUTHOFF, 4);
    print_header1(0, 0);
    put4(info + I_DLENOFF, (unsigned long)mh.m_datalen);
    put4(info + I_RLENOFF, (unsigned long)mh.m_rsrclen);
    put4(info + I_CTIMOFF, (unsigned long)mh.m_createtime);
    put4(info + I_MTIMOFF, (unsigned long)mh.m_modifytime);
    print_header2(0);
    end_put();
}

static void do_mu_fork()
{
    long newbytes;

    while(readline()) {
	if(line[0] == 0) {
	    continue;
	}
	newbytes = mu_comp_to_bin();
	if(newbytes != 0) {
	    continue;
	}
	if(!readline()) {
	    (void)fprintf(stderr, "Premature EOF\n");
#ifdef SCAN
	    do_error("hexbin: Premature EOF");
#endif /* SCAN */
	    exit(1);
	}
	if(strncmp(line, "end", 3)) {
	    (void)fprintf(stderr, "\"end\" line missing.\n");
#ifdef SCAN
	    do_error("hexbin: \"end\" line missing");
#endif /* SCAN */
	    exit(1);
	}
	return;
    }
    (void)fprintf(stderr, "Premature EOF\n");
#ifdef SCAN
    do_error("hexbin: Premature EOF");
#endif /* SCAN */
    exit(1);
    /*NOTREACHED*/
}

static int mu_comp_to_bin()
{
    char obuf[BUFSIZ];
    int outcount, n;

    outcount = mu_convert(line, obuf);
    for(n = 0; n < outcount; n++) {
	put_byte(obuf[n]);
    }
    return outcount;
}

#define SIXB(c) (((c)-0x20) & 0x3f)

static int mu_convert(ibuf, obuf)
char *ibuf, *obuf;
{
    register char *ip = ibuf;
    register char *op = obuf;
    register int n, outcount;
    int numread, incount;

    numread = strlen(ip);
    outcount = SIXB(ip[0]);
    incount = ((outcount / 3) + 1) * 4;
    for(n = numread; n < incount; n++) {  /* restore lost spaces */
	ip[n] = ' ';
    }
    ip++;

    n = 0;
    while(n <= outcount) {
	*op++ = SIXB(ip[0]) << 2 | SIXB(ip[1]) >> 4;
	*op++ = SIXB(ip[1]) << 4 | SIXB(ip[2]) >> 2;
	*op++ = SIXB(ip[2]) << 6 | SIXB(ip[3]);
	ip += 4;
	n += 3;
    }
    return outcount;
}
#else /* MU */
int mu; /* keep lint and some compilers happy */
#endif /* MU */