xref: /minix3/external/bsd/file/dist/src/ascmagic.c (revision d19d7d58aa5cd1165eefe1a0d807c8afe282db62)
1 /*	$NetBSD: ascmagic.c,v 1.1.1.2 2011/05/12 20:46:52 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.81 2011/03/15 22:16:29 christos Exp $")
42 #else
43 __RCSID("$NetBSD: ascmagic.c,v 1.1.1.2 2011/05/12 20:46:52 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 #include "names.h"
56 
57 #define MAXLINELEN 300	/* longest sane line length */
58 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
59 		  || (x) == 0x85 || (x) == '\f')
60 
61 private int ascmatch(const unsigned char *, const unichar *, size_t);
62 private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
63 private size_t trim_nuls(const unsigned char *, size_t);
64 
65 /*
66  * Undo the NUL-termination kindly provided by process()
67  * but leave at least one byte to look at
68  */
69 private size_t
70 trim_nuls(const unsigned char *buf, size_t nbytes)
71 {
72 	while (nbytes > 1 && buf[nbytes - 1] == '\0')
73 		nbytes--;
74 
75 	return nbytes;
76 }
77 
78 protected int
79 file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
80 {
81 	unichar *ubuf = NULL;
82 	size_t ulen;
83 	int rv = 1;
84 
85 	const char *code = NULL;
86 	const char *code_mime = NULL;
87 	const char *type = NULL;
88 
89 	if (ms->flags & MAGIC_APPLE)
90 		return 0;
91 
92 	nbytes = trim_nuls(buf, nbytes);
93 
94 	/* If file doesn't look like any sort of text, give up. */
95 	if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime,
96 	    &type) == 0) {
97 		rv = 0;
98 		goto done;
99 	}
100 
101 	rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
102 	    type);
103 
104  done:
105 	if (ubuf)
106 		free(ubuf);
107 
108 	return rv;
109 }
110 
111 protected int
112 file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
113     size_t nbytes, unichar *ubuf, size_t ulen, const char *code,
114     const char *type)
115 {
116 	unsigned char *utf8_buf = NULL, *utf8_end;
117 	size_t mlen, i;
118 	const struct names *p;
119 	int rv = -1;
120 	int mime = ms->flags & MAGIC_MIME;
121 
122 	const char *subtype = NULL;
123 	const char *subtype_mime = NULL;
124 
125 	int has_escapes = 0;
126 	int has_backspace = 0;
127 	int seen_cr = 0;
128 
129 	int n_crlf = 0;
130 	int n_lf = 0;
131 	int n_cr = 0;
132 	int n_nel = 0;
133 	int score, curtype, executable = 0;
134 
135 	size_t last_line_end = (size_t)-1;
136 	int has_long_lines = 0;
137 
138 	if (ms->flags & MAGIC_APPLE)
139 		return 0;
140 
141 	nbytes = trim_nuls(buf, nbytes);
142 
143 	/* If we have fewer than 2 bytes, give up. */
144 	if (nbytes <= 1) {
145 		rv = 0;
146 		goto done;
147 	}
148 
149 	if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
150 		/* Convert ubuf to UTF-8 and try text soft magic */
151 		/* malloc size is a conservative overestimate; could be
152 		   improved, or at least realloced after conversion. */
153 		mlen = ulen * 6;
154 		if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
155 			file_oomem(ms, mlen);
156 			goto done;
157 		}
158 		if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
159 		    == NULL)
160 			goto done;
161 		if ((rv = file_softmagic(ms, utf8_buf,
162 		    (size_t)(utf8_end - utf8_buf), TEXTTEST)) != 0)
163 			goto subtype_identified;
164 		else
165 			rv = -1;
166 	}
167 
168 	/* look for tokens from names.h - this is expensive! */
169 	if ((ms->flags & MAGIC_NO_CHECK_TOKENS) != 0)
170 		goto subtype_identified;
171 
172 	i = 0;
173 	score = 0;
174 	curtype = -1;
175 	while (i < ulen) {
176 		size_t end;
177 
178 		/* skip past any leading space */
179 		while (i < ulen && ISSPC(ubuf[i]))
180 			i++;
181 		if (i >= ulen)
182 			break;
183 
184 		/* find the next whitespace */
185 		for (end = i + 1; end < nbytes; end++)
186 			if (ISSPC(ubuf[end]))
187 				break;
188 
189 		/* compare the word thus isolated against the token list */
190 		for (p = names; p < names + NNAMES; p++) {
191 			if (ascmatch((const unsigned char *)p->name, ubuf + i,
192 			    end - i)) {
193 				if (curtype == -1)
194 					curtype = p->type;
195 				else if (curtype != p->type) {
196 					score = p->score;
197 					curtype = p->type;
198 				} else
199 					score += p->score;
200 				if (score > 1) {
201 					subtype = types[p->type].human;
202 					subtype_mime = types[p->type].mime;
203 					goto subtype_identified;
204 				}
205 			}
206 		}
207 
208 		i = end;
209 	}
210 
211 subtype_identified:
212 
213 	/* Now try to discover other details about the file. */
214 	for (i = 0; i < ulen; i++) {
215 		if (ubuf[i] == '\n') {
216 			if (seen_cr)
217 				n_crlf++;
218 			else
219 				n_lf++;
220 			last_line_end = i;
221 		} else if (seen_cr)
222 			n_cr++;
223 
224 		seen_cr = (ubuf[i] == '\r');
225 		if (seen_cr)
226 			last_line_end = i;
227 
228 		if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
229 			n_nel++;
230 			last_line_end = i;
231 		}
232 
233 		/* If this line is _longer_ than MAXLINELEN, remember it. */
234 		if (i > last_line_end + MAXLINELEN)
235 			has_long_lines = 1;
236 
237 		if (ubuf[i] == '\033')
238 			has_escapes = 1;
239 		if (ubuf[i] == '\b')
240 			has_backspace = 1;
241 	}
242 
243 	/* Beware, if the data has been truncated, the final CR could have
244 	   been followed by a LF.  If we have HOWMANY bytes, it indicates
245 	   that the data might have been truncated, probably even before
246 	   this function was called. */
247 	if (seen_cr && nbytes < HOWMANY)
248 		n_cr++;
249 
250 	if (strcmp(type, "binary") == 0) {
251 		rv = 0;
252 		goto done;
253 	}
254 	if (mime) {
255 		if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) {
256 			if (subtype_mime) {
257 				if (file_printf(ms, "%s", subtype_mime) == -1)
258 					goto done;
259 			} else {
260 				if (file_printf(ms, "text/plain") == -1)
261 					goto done;
262 			}
263 		}
264 	} else {
265 		if (file_printedlen(ms)) {
266 			switch (file_replace(ms, " text$", ", ")) {
267 			case 0:
268 				switch (file_replace(ms, " text executable$",
269 				    ", ")) {
270 				case 0:
271 					if (file_printf(ms, ", ") == -1)
272 						goto done;
273 				case -1:
274 					goto done;
275 				default:
276 					executable = 1;
277 					break;
278 				}
279 				break;
280 			case -1:
281 				goto done;
282 			default:
283 				break;
284 			}
285 		}
286 
287 		if (file_printf(ms, "%s", code) == -1)
288 			goto done;
289 
290 		if (subtype) {
291 			if (file_printf(ms, " %s", subtype) == -1)
292 				goto done;
293 		}
294 
295 		if (file_printf(ms, " %s", type) == -1)
296 			goto done;
297 
298 		if (executable)
299 			if (file_printf(ms, " executable") == -1)
300 				goto done;
301 
302 		if (has_long_lines)
303 			if (file_printf(ms, ", with very long lines") == -1)
304 				goto done;
305 
306 		/*
307 		 * Only report line terminators if we find one other than LF,
308 		 * or if we find none at all.
309 		 */
310 		if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
311 		    (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
312 			if (file_printf(ms, ", with") == -1)
313 				goto done;
314 
315 			if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
316 				if (file_printf(ms, " no") == -1)
317 					goto done;
318 			} else {
319 				if (n_crlf) {
320 					if (file_printf(ms, " CRLF") == -1)
321 						goto done;
322 					if (n_cr || n_lf || n_nel)
323 						if (file_printf(ms, ",") == -1)
324 							goto done;
325 				}
326 				if (n_cr) {
327 					if (file_printf(ms, " CR") == -1)
328 						goto done;
329 					if (n_lf || n_nel)
330 						if (file_printf(ms, ",") == -1)
331 							goto done;
332 				}
333 				if (n_lf) {
334 					if (file_printf(ms, " LF") == -1)
335 						goto done;
336 					if (n_nel)
337 						if (file_printf(ms, ",") == -1)
338 							goto done;
339 				}
340 				if (n_nel)
341 					if (file_printf(ms, " NEL") == -1)
342 						goto done;
343 			}
344 
345 			if (file_printf(ms, " line terminators") == -1)
346 				goto done;
347 		}
348 
349 		if (has_escapes)
350 			if (file_printf(ms, ", with escape sequences") == -1)
351 				goto done;
352 		if (has_backspace)
353 			if (file_printf(ms, ", with overstriking") == -1)
354 				goto done;
355 	}
356 	rv = 1;
357 done:
358 	if (utf8_buf)
359 		free(utf8_buf);
360 
361 	return rv;
362 }
363 
364 private int
365 ascmatch(const unsigned char *s, const unichar *us, size_t ulen)
366 {
367 	size_t i;
368 
369 	for (i = 0; i < ulen; i++) {
370 		if (s[i] != us[i])
371 			return 0;
372 	}
373 
374 	if (s[i])
375 		return 0;
376 	else
377 		return 1;
378 }
379 
380 /*
381  * Encode Unicode string as UTF-8, returning pointer to character
382  * after end of string, or NULL if an invalid character is found.
383  */
384 private unsigned char *
385 encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
386 {
387 	size_t i;
388 	unsigned char *end = buf + len;
389 
390 	for (i = 0; i < ulen; i++) {
391 		if (ubuf[i] <= 0x7f) {
392 			if (end - buf < 1)
393 				return NULL;
394 			*buf++ = (unsigned char)ubuf[i];
395 		} else if (ubuf[i] <= 0x7ff) {
396 			if (end - buf < 2)
397 				return NULL;
398 			*buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
399 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
400 		} else if (ubuf[i] <= 0xffff) {
401 			if (end - buf < 3)
402 				return NULL;
403 			*buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
404 			*buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
405 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
406 		} else if (ubuf[i] <= 0x1fffff) {
407 			if (end - buf < 4)
408 				return NULL;
409 			*buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
410 			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
411 			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
412 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
413 		} else if (ubuf[i] <= 0x3ffffff) {
414 			if (end - buf < 5)
415 				return NULL;
416 			*buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
417 			*buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
418 			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
419 			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
420 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
421 		} else if (ubuf[i] <= 0x7fffffff) {
422 			if (end - buf < 6)
423 				return NULL;
424 			*buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
425 			*buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
426 			*buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
427 			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
428 			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
429 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
430 		} else /* Invalid character */
431 			return NULL;
432 	}
433 
434 	return buf;
435 }
436