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