xref: /netbsd-src/external/bsd/file/dist/src/ascmagic.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: ascmagic.c,v 1.1.1.11 2019/05/22 17:19:56 christos Exp $	*/
2 
3 /*
4  * Copyright (c) Ian F. Darwin 1986-1995.
5  * Software written by Ian F. Darwin and others;
6  * maintained 1995-present by Christos Zoulas and others.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 /*
31  * ASCII magic -- try to detect text encoding.
32  *
33  * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
34  * to handle character codes other than ASCII on a unified basis.
35  */
36 
37 #include "file.h"
38 
39 #ifndef	lint
40 #if 0
41 FILE_RCSID("@(#)$File: ascmagic.c,v 1.104 2019/05/07 02:27:11 christos Exp $")
42 #else
43 __RCSID("$NetBSD: ascmagic.c,v 1.1.1.11 2019/05/22 17:19:56 christos Exp $");
44 #endif
45 #endif	/* lint */
46 
47 #include "magic.h"
48 #include <string.h>
49 #include <memory.h>
50 #include <ctype.h>
51 #include <stdlib.h>
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif
55 
56 #define MAXLINELEN 300	/* longest sane line length */
57 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
58 		  || (x) == 0x85 || (x) == '\f')
59 
60 private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
61 private size_t trim_nuls(const unsigned char *, size_t);
62 
63 /*
64  * Undo the NUL-termination kindly provided by process()
65  * but leave at least one byte to look at
66  */
67 private size_t
68 trim_nuls(const unsigned char *buf, size_t nbytes)
69 {
70 	while (nbytes > 1 && buf[nbytes - 1] == '\0')
71 		nbytes--;
72 
73 	return nbytes;
74 }
75 
76 protected int
77 file_ascmagic(struct magic_set *ms, const struct buffer *b, int text)
78 {
79 	unichar *ubuf = NULL;
80 	size_t ulen = 0;
81 	int rv = 1;
82 	struct buffer bb;
83 
84 	const char *code = NULL;
85 	const char *code_mime = NULL;
86 	const char *type = NULL;
87 
88 	bb = *b;
89 	bb.flen = trim_nuls(CAST(const unsigned char *, b->fbuf), b->flen);
90 	/*
91 	 * Avoid trimming at an odd byte if the original buffer was evenly
92 	 * sized; this avoids losing the last character on UTF-16 LE text
93 	 */
94 	if ((bb.flen & 1) && !(b->flen & 1))
95 		bb.flen++;
96 
97 	/* If file doesn't look like any sort of text, give up. */
98 	if (file_encoding(ms, &bb, &ubuf, &ulen, &code, &code_mime,
99 	    &type) == 0)
100 		rv = 0;
101         else
102 		rv = file_ascmagic_with_encoding(ms, &bb,
103 		    ubuf, ulen, code, type, text);
104 
105 	free(ubuf);
106 
107 	return rv;
108 }
109 
110 protected int
111 file_ascmagic_with_encoding(struct magic_set *ms,
112     const struct buffer *b, unichar *ubuf, size_t ulen, const char *code,
113     const char *type, int text)
114 {
115 	struct buffer bb;
116 	const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
117 	size_t nbytes = b->flen;
118 	unsigned char *utf8_buf = NULL, *utf8_end;
119 	size_t mlen, i, len;
120 	int rv = -1;
121 	int mime = ms->flags & MAGIC_MIME;
122 	int need_separator = 0;
123 
124 	const char *subtype = NULL;
125 	const char *subtype_mime = NULL;
126 
127 	int has_escapes = 0;
128 	int has_backspace = 0;
129 	int seen_cr = 0;
130 
131 	int n_crlf = 0;
132 	int n_lf = 0;
133 	int n_cr = 0;
134 	int n_nel = 0;
135 	int executable = 0;
136 
137 	size_t last_line_end = CAST(size_t, -1);
138 	int has_long_lines = 0;
139 
140 	nbytes = trim_nuls(buf, nbytes);
141 
142 	/* If we have fewer than 2 bytes, give up. */
143 	if (nbytes <= 1) {
144 		rv = 0;
145 		goto done;
146 	}
147 
148 	if (ulen > 0 && (ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
149 		/* Convert ubuf to UTF-8 and try text soft magic */
150 		/* malloc size is a conservative overestimate; could be
151 		   improved, or at least realloced after conversion. */
152 		mlen = ulen * 6;
153 		if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
154 			file_oomem(ms, mlen);
155 			goto done;
156 		}
157 		if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
158 		    == NULL)
159 			goto done;
160 		buffer_init(&bb, b->fd, &b->st, utf8_buf,
161 		    CAST(size_t, utf8_end - utf8_buf));
162 
163 		if ((rv = file_softmagic(ms, &bb, NULL, NULL,
164 		    TEXTTEST, text)) == 0)
165 			rv = -1;
166 		else
167 			need_separator = 1;
168 		buffer_fini(&bb);
169 		if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
170 			rv = rv == -1 ? 0 : 1;
171 			goto done;
172 		}
173 	}
174 	if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)))
175 		return 0;
176 
177 	/* Now try to discover other details about the file. */
178 	for (i = 0; i < ulen; i++) {
179 		if (ubuf[i] == '\n') {
180 			if (seen_cr)
181 				n_crlf++;
182 			else
183 				n_lf++;
184 			last_line_end = i;
185 		} else if (seen_cr)
186 			n_cr++;
187 
188 		seen_cr = (ubuf[i] == '\r');
189 		if (seen_cr)
190 			last_line_end = i;
191 
192 		if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
193 			n_nel++;
194 			last_line_end = i;
195 		}
196 
197 		/* If this line is _longer_ than MAXLINELEN, remember it. */
198 		if (i > last_line_end + MAXLINELEN)
199 			has_long_lines = 1;
200 
201 		if (ubuf[i] == '\033')
202 			has_escapes = 1;
203 		if (ubuf[i] == '\b')
204 			has_backspace = 1;
205 	}
206 
207 	/* Beware, if the data has been truncated, the final CR could have
208 	   been followed by a LF.  If we have ms->bytes_max bytes, it indicates
209 	   that the data might have been truncated, probably even before
210 	   this function was called. */
211 	if (seen_cr && nbytes < ms->bytes_max)
212 		n_cr++;
213 
214 	if (strcmp(type, "binary") == 0) {
215 		rv = 0;
216 		goto done;
217 	}
218 	len = file_printedlen(ms);
219 	if (mime) {
220 		if ((mime & MAGIC_MIME_TYPE) != 0) {
221 			if (len) {
222 				/*
223 				 * Softmagic printed something, we
224 				 * are either done, or we need a separator
225 				 */
226 				if ((ms->flags & MAGIC_CONTINUE) == 0) {
227 					rv = 1;
228 					goto done;
229 				}
230 				if (need_separator && file_separator(ms) == -1)
231 					goto done;
232 			}
233 			if (subtype_mime) {
234 				if (file_printf(ms, "%s", subtype_mime) == -1)
235 					goto done;
236 			} else {
237 				if (file_printf(ms, "text/plain") == -1)
238 					goto done;
239 			}
240 		}
241 	} else {
242 		if (len) {
243 			switch (file_replace(ms, " text$", ", ")) {
244 			case 0:
245 				switch (file_replace(ms, " text executable$",
246 				    ", ")) {
247 				case 0:
248 					if (file_printf(ms, ", ") == -1)
249 						goto done;
250 					break;
251 				case -1:
252 					goto done;
253 				default:
254 					executable = 1;
255 					break;
256 				}
257 				break;
258 			case -1:
259 				goto done;
260 			default:
261 				break;
262 			}
263 		}
264 
265 		if (file_printf(ms, "%s", code) == -1)
266 			goto done;
267 
268 		if (subtype) {
269 			if (file_printf(ms, " %s", subtype) == -1)
270 				goto done;
271 		}
272 
273 		if (file_printf(ms, " %s", type) == -1)
274 			goto done;
275 
276 		if (executable)
277 			if (file_printf(ms, " executable") == -1)
278 				goto done;
279 
280 		if (has_long_lines)
281 			if (file_printf(ms, ", with very long lines") == -1)
282 				goto done;
283 
284 		/*
285 		 * Only report line terminators if we find one other than LF,
286 		 * or if we find none at all.
287 		 */
288 		if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
289 		    (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
290 			if (file_printf(ms, ", with") == -1)
291 				goto done;
292 
293 			if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
294 				if (file_printf(ms, " no") == -1)
295 					goto done;
296 			} else {
297 				if (n_crlf) {
298 					if (file_printf(ms, " CRLF") == -1)
299 						goto done;
300 					if (n_cr || n_lf || n_nel)
301 						if (file_printf(ms, ",") == -1)
302 							goto done;
303 				}
304 				if (n_cr) {
305 					if (file_printf(ms, " CR") == -1)
306 						goto done;
307 					if (n_lf || n_nel)
308 						if (file_printf(ms, ",") == -1)
309 							goto done;
310 				}
311 				if (n_lf) {
312 					if (file_printf(ms, " LF") == -1)
313 						goto done;
314 					if (n_nel)
315 						if (file_printf(ms, ",") == -1)
316 							goto done;
317 				}
318 				if (n_nel)
319 					if (file_printf(ms, " NEL") == -1)
320 						goto done;
321 			}
322 
323 			if (file_printf(ms, " line terminators") == -1)
324 				goto done;
325 		}
326 
327 		if (has_escapes)
328 			if (file_printf(ms, ", with escape sequences") == -1)
329 				goto done;
330 		if (has_backspace)
331 			if (file_printf(ms, ", with overstriking") == -1)
332 				goto done;
333 	}
334 	rv = 1;
335 done:
336 	free(utf8_buf);
337 
338 	return rv;
339 }
340 
341 /*
342  * Encode Unicode string as UTF-8, returning pointer to character
343  * after end of string, or NULL if an invalid character is found.
344  */
345 private unsigned char *
346 encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
347 {
348 	size_t i;
349 	unsigned char *end = buf + len;
350 
351 	for (i = 0; i < ulen; i++) {
352 		if (ubuf[i] <= 0x7f) {
353 			if (end - buf < 1)
354 				return NULL;
355 			*buf++ = CAST(unsigned char, ubuf[i]);
356 		} else if (ubuf[i] <= 0x7ff) {
357 			if (end - buf < 2)
358 				return NULL;
359 			*buf++ = CAST(unsigned char, (ubuf[i] >> 6) + 0xc0);
360 			*buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
361 		} else if (ubuf[i] <= 0xffff) {
362 			if (end - buf < 3)
363 				return NULL;
364 			*buf++ = CAST(unsigned char, (ubuf[i] >> 12) + 0xe0);
365 			*buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
366 			*buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
367 		} else if (ubuf[i] <= 0x1fffff) {
368 			if (end - buf < 4)
369 				return NULL;
370 			*buf++ = CAST(unsigned char, (ubuf[i] >> 18) + 0xf0);
371 			*buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
372 			*buf++ = CAST(unsigned char, ((ubuf[i] >>  6) & 0x3f) + 0x80);
373 			*buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
374 		} else if (ubuf[i] <= 0x3ffffff) {
375 			if (end - buf < 5)
376 				return NULL;
377 			*buf++ = CAST(unsigned char, (ubuf[i] >> 24) + 0xf8);
378 			*buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
379 			*buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
380 			*buf++ = CAST(unsigned char, ((ubuf[i] >>  6) & 0x3f) + 0x80);
381 			*buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
382 		} else if (ubuf[i] <= 0x7fffffff) {
383 			if (end - buf < 6)
384 				return NULL;
385 			*buf++ = CAST(unsigned char, (ubuf[i] >> 30) + 0xfc);
386 			*buf++ = CAST(unsigned char, ((ubuf[i] >> 24) & 0x3f) + 0x80);
387 			*buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
388 			*buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
389 			*buf++ = CAST(unsigned char, ((ubuf[i] >>  6) & 0x3f) + 0x80);
390 			*buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
391 		} else /* Invalid character */
392 			return NULL;
393 	}
394 
395 	return buf;
396 }
397