1*898496eeSXin LI /*-
2*898496eeSXin LI * Copyright (c) 2023 Christos Zoulas
3*898496eeSXin LI * All rights reserved.
4*898496eeSXin LI *
5*898496eeSXin LI * Redistribution and use in source and binary forms, with or without
6*898496eeSXin LI * modification, are permitted provided that the following conditions
7*898496eeSXin LI * are met:
8*898496eeSXin LI * 1. Redistributions of source code must retain the above copyright
9*898496eeSXin LI * notice, this list of conditions and the following disclaimer.
10*898496eeSXin LI * 2. Redistributions in binary form must reproduce the above copyright
11*898496eeSXin LI * notice, this list of conditions and the following disclaimer in the
12*898496eeSXin LI * documentation and/or other materials provided with the distribution.
13*898496eeSXin LI *
14*898496eeSXin LI * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15*898496eeSXin LI * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16*898496eeSXin LI * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17*898496eeSXin LI * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18*898496eeSXin LI * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19*898496eeSXin LI * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20*898496eeSXin LI * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21*898496eeSXin LI * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22*898496eeSXin LI * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23*898496eeSXin LI * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24*898496eeSXin LI * POSSIBILITY OF SUCH DAMAGE.
25*898496eeSXin LI */
26*898496eeSXin LI
27*898496eeSXin LI /*
28*898496eeSXin LI * Parse SIM-H tape files
29*898496eeSXin LI * http://simh.trailing-edge.com/docs/simh_magtape.pdf
30*898496eeSXin LI */
31*898496eeSXin LI
32*898496eeSXin LI #ifndef TEST
33*898496eeSXin LI #include "file.h"
34*898496eeSXin LI
35*898496eeSXin LI #ifndef lint
36*898496eeSXin LI FILE_RCSID("@(#)$File: is_simh.c,v 1.10 2023/07/27 19:39:55 christos Exp $")
37*898496eeSXin LI #endif
38*898496eeSXin LI
39*898496eeSXin LI #include <string.h>
40*898496eeSXin LI #include <stddef.h>
41*898496eeSXin LI #include "magic.h"
42*898496eeSXin LI #else
43*898496eeSXin LI #include <stdint.h>
44*898496eeSXin LI #include <sys/types.h>
45*898496eeSXin LI #include <string.h>
46*898496eeSXin LI #include <stddef.h>
47*898496eeSXin LI #define CAST(a, b) (a)(b)
48*898496eeSXin LI #endif
49*898496eeSXin LI
50*898496eeSXin LI
51*898496eeSXin LI #ifdef DEBUG
52*898496eeSXin LI #include <stdio.h>
53*898496eeSXin LI #define DPRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
54*898496eeSXin LI #else
55*898496eeSXin LI #define DPRINTF(fmt, ...)
56*898496eeSXin LI #endif
57*898496eeSXin LI
58*898496eeSXin LI /*
59*898496eeSXin LI * if SIMH_TAPEMARKS == 0:
60*898496eeSXin LI * check all the records and tapemarks
61*898496eeSXin LI * otherwise:
62*898496eeSXin LI * check only up-to the number of tapemarks specified
63*898496eeSXin LI */
64*898496eeSXin LI #ifndef SIMH_TAPEMARKS
65*898496eeSXin LI #define SIMH_TAPEMARKS 10
66*898496eeSXin LI #endif
67*898496eeSXin LI
68*898496eeSXin LI typedef union {
69*898496eeSXin LI char s[4];
70*898496eeSXin LI uint32_t u;
71*898496eeSXin LI } myword;
72*898496eeSXin LI
73*898496eeSXin LI static myword simh_bo;
74*898496eeSXin LI
75*898496eeSXin LI #define NEED_SWAP (simh_bo.u == CAST(uint32_t, 0x01020304))
76*898496eeSXin LI
77*898496eeSXin LI /*
78*898496eeSXin LI * swap an int
79*898496eeSXin LI */
80*898496eeSXin LI static uint32_t
swap4(uint32_t sv)81*898496eeSXin LI swap4(uint32_t sv)
82*898496eeSXin LI {
83*898496eeSXin LI myword d, s;
84*898496eeSXin LI s.u = sv;
85*898496eeSXin LI d.s[0] = s.s[3];
86*898496eeSXin LI d.s[1] = s.s[2];
87*898496eeSXin LI d.s[2] = s.s[1];
88*898496eeSXin LI d.s[3] = s.s[0];
89*898496eeSXin LI return d.u;
90*898496eeSXin LI }
91*898496eeSXin LI
92*898496eeSXin LI
93*898496eeSXin LI static uint32_t
getlen(const unsigned char ** uc)94*898496eeSXin LI getlen(const unsigned char **uc)
95*898496eeSXin LI {
96*898496eeSXin LI uint32_t n;
97*898496eeSXin LI memcpy(&n, *uc, sizeof(n));
98*898496eeSXin LI *uc += sizeof(n);
99*898496eeSXin LI if (NEED_SWAP)
100*898496eeSXin LI n = swap4(n);
101*898496eeSXin LI if (n == 0xffffffff) /* check for End of Medium */
102*898496eeSXin LI return n;
103*898496eeSXin LI n &= 0x00ffffff; /* keep only the record len */
104*898496eeSXin LI if (n & 1)
105*898496eeSXin LI n++;
106*898496eeSXin LI return n;
107*898496eeSXin LI }
108*898496eeSXin LI
109*898496eeSXin LI static int
simh_parse(const unsigned char * uc,const unsigned char * ue)110*898496eeSXin LI simh_parse(const unsigned char *uc, const unsigned char *ue)
111*898496eeSXin LI {
112*898496eeSXin LI uint32_t nbytes, cbytes;
113*898496eeSXin LI const unsigned char *orig_uc = uc;
114*898496eeSXin LI size_t nt = 0, nr = 0;
115*898496eeSXin LI
116*898496eeSXin LI (void)memcpy(simh_bo.s, "\01\02\03\04", 4);
117*898496eeSXin LI
118*898496eeSXin LI while (ue - uc >= CAST(ptrdiff_t, sizeof(nbytes))) {
119*898496eeSXin LI nbytes = getlen(&uc);
120*898496eeSXin LI if ((nt > 0 || nr > 0) && nbytes == 0xFFFFFFFF)
121*898496eeSXin LI /* EOM after at least one record or tapemark */
122*898496eeSXin LI break;
123*898496eeSXin LI if (nbytes == 0) {
124*898496eeSXin LI nt++; /* count tapemarks */
125*898496eeSXin LI #if SIMH_TAPEMARKS
126*898496eeSXin LI if (nt == SIMH_TAPEMARKS)
127*898496eeSXin LI break;
128*898496eeSXin LI #endif
129*898496eeSXin LI continue;
130*898496eeSXin LI }
131*898496eeSXin LI /* handle a data record */
132*898496eeSXin LI uc += nbytes;
133*898496eeSXin LI if (ue - uc < CAST(ptrdiff_t, sizeof(nbytes)))
134*898496eeSXin LI break;
135*898496eeSXin LI cbytes = getlen(&uc);
136*898496eeSXin LI if (nbytes != cbytes)
137*898496eeSXin LI return 0;
138*898496eeSXin LI nr++;
139*898496eeSXin LI }
140*898496eeSXin LI if (nt * sizeof(uint32_t) == CAST(size_t, uc - orig_uc))
141*898496eeSXin LI return 0; /* All examined data was tapemarks (0) */
142*898496eeSXin LI if (nr == 0) /* No records */
143*898496eeSXin LI return 0;
144*898496eeSXin LI return 1;
145*898496eeSXin LI }
146*898496eeSXin LI
147*898496eeSXin LI #ifndef TEST
148*898496eeSXin LI int
file_is_simh(struct magic_set * ms,const struct buffer * b)149*898496eeSXin LI file_is_simh(struct magic_set *ms, const struct buffer *b)
150*898496eeSXin LI {
151*898496eeSXin LI const unsigned char *uc = CAST(const unsigned char *, b->fbuf);
152*898496eeSXin LI const unsigned char *ue = uc + b->flen;
153*898496eeSXin LI int mime = ms->flags & MAGIC_MIME;
154*898496eeSXin LI
155*898496eeSXin LI if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) != 0)
156*898496eeSXin LI return 0;
157*898496eeSXin LI
158*898496eeSXin LI if (!simh_parse(uc, ue))
159*898496eeSXin LI return 0;
160*898496eeSXin LI
161*898496eeSXin LI if (mime == MAGIC_MIME_ENCODING)
162*898496eeSXin LI return 1;
163*898496eeSXin LI
164*898496eeSXin LI if (mime) {
165*898496eeSXin LI if (file_printf(ms, "application/SIMH-tape-data") == -1)
166*898496eeSXin LI return -1;
167*898496eeSXin LI return 1;
168*898496eeSXin LI }
169*898496eeSXin LI
170*898496eeSXin LI if (file_printf(ms, "SIMH tape data") == -1)
171*898496eeSXin LI return -1;
172*898496eeSXin LI
173*898496eeSXin LI return 1;
174*898496eeSXin LI }
175*898496eeSXin LI
176*898496eeSXin LI #else
177*898496eeSXin LI
178*898496eeSXin LI #include <sys/types.h>
179*898496eeSXin LI #include <sys/stat.h>
180*898496eeSXin LI #include <stdio.h>
181*898496eeSXin LI #include <fcntl.h>
182*898496eeSXin LI #include <unistd.h>
183*898496eeSXin LI #include <stdlib.h>
184*898496eeSXin LI #include <stdint.h>
185*898496eeSXin LI #include <err.h>
186*898496eeSXin LI
187*898496eeSXin LI int
main(int argc,char * argv[])188*898496eeSXin LI main(int argc, char *argv[])
189*898496eeSXin LI {
190*898496eeSXin LI int fd;
191*898496eeSXin LI struct stat st;
192*898496eeSXin LI unsigned char *p;
193*898496eeSXin LI
194*898496eeSXin LI if ((fd = open(argv[1], O_RDONLY)) == -1)
195*898496eeSXin LI err(EXIT_FAILURE, "Can't open `%s'", argv[1]);
196*898496eeSXin LI
197*898496eeSXin LI if (fstat(fd, &st) == -1)
198*898496eeSXin LI err(EXIT_FAILURE, "Can't stat `%s'", argv[1]);
199*898496eeSXin LI
200*898496eeSXin LI if ((p = CAST(char *, malloc(st.st_size))) == NULL)
201*898496eeSXin LI err(EXIT_FAILURE, "Can't allocate %jd bytes",
202*898496eeSXin LI (intmax_t)st.st_size);
203*898496eeSXin LI if (read(fd, p, st.st_size) != st.st_size)
204*898496eeSXin LI err(EXIT_FAILURE, "Can't read %jd bytes",
205*898496eeSXin LI (intmax_t)st.st_size);
206*898496eeSXin LI printf("is simh %d\n", simh_parse(p, p + st.st_size));
207*898496eeSXin LI return 0;
208*898496eeSXin LI }
209*898496eeSXin LI #endif
210