xref: /netbsd-src/external/bsd/file/dist/src/ascmagic.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: ascmagic.c,v 1.1.1.1 2009/05/08 16:35:06 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 -- file types that we know based on keywords
32  * that can appear anywhere in the file.
33  *
34  * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
35  * to handle character codes other than ASCII on a unified basis.
36  */
37 
38 #include "file.h"
39 
40 #ifndef	lint
41 #if 0
42 FILE_RCSID("@(#)$File: ascmagic.c,v 1.75 2009/02/03 20:27:51 christos Exp $")
43 #else
44 __RCSID("$NetBSD: ascmagic.c,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $");
45 #endif
46 #endif	/* lint */
47 
48 #include "magic.h"
49 #include <string.h>
50 #include <memory.h>
51 #include <ctype.h>
52 #include <stdlib.h>
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
56 #include "names.h"
57 
58 #define MAXLINELEN 300	/* longest sane line length */
59 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
60 		  || (x) == 0x85 || (x) == '\f')
61 
62 private int ascmatch(const unsigned char *, const unichar *, size_t);
63 private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
64 private size_t trim_nuls(const unsigned char *, size_t);
65 
66 /*
67  * Undo the NUL-termination kindly provided by process()
68  * but leave at least one byte to look at
69  */
70 private size_t
71 trim_nuls(const unsigned char *buf, size_t nbytes)
72 {
73 	while (nbytes > 1 && buf[nbytes - 1] == '\0')
74 		nbytes--;
75 
76 	return nbytes;
77 }
78 
79 protected int
80 file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
81 {
82 	unichar *ubuf = NULL;
83 	size_t ulen;
84 	int rv = 1;
85 
86 	const char *code = NULL;
87 	const char *code_mime = NULL;
88 	const char *type = NULL;
89 
90 	if (ms->flags & MAGIC_APPLE)
91 		return 0;
92 
93 	nbytes = trim_nuls(buf, nbytes);
94 
95 	/* If file doesn't look like any sort of text, give up. */
96 	if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime,
97 	    &type) == 0) {
98 		rv = 0;
99 		goto done;
100 	}
101 
102 	rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
103 	    type);
104 
105  done:
106 	if (ubuf)
107 		free(ubuf);
108 
109 	return rv;
110 }
111 
112 protected int
113 file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
114     size_t nbytes, unichar *ubuf, size_t ulen, const char *code,
115     const char *type)
116 {
117 	unsigned char *utf8_buf = NULL, *utf8_end;
118 	size_t mlen, i;
119 	const struct names *p;
120 	int rv = -1;
121 	int mime = ms->flags & MAGIC_MIME;
122 
123 	const char *subtype = NULL;
124 	const char *subtype_mime = NULL;
125 
126 	int has_escapes = 0;
127 	int has_backspace = 0;
128 	int seen_cr = 0;
129 
130 	int n_crlf = 0;
131 	int n_lf = 0;
132 	int n_cr = 0;
133 	int n_nel = 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 	/* 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)) == NULL)
158 		goto done;
159 	if ((rv = file_softmagic(ms, utf8_buf, (size_t)(utf8_end - utf8_buf),
160 	    TEXTTEST)) != 0)
161 		goto done;
162 	else
163 		rv = -1;
164 
165 	/* look for tokens from names.h - this is expensive! */
166 	if ((ms->flags & MAGIC_NO_CHECK_TOKENS) != 0)
167 		goto subtype_identified;
168 
169 	i = 0;
170 	while (i < ulen) {
171 		size_t end;
172 
173 		/* skip past any leading space */
174 		while (i < ulen && ISSPC(ubuf[i]))
175 			i++;
176 		if (i >= ulen)
177 			break;
178 
179 		/* find the next whitespace */
180 		for (end = i + 1; end < nbytes; end++)
181 			if (ISSPC(ubuf[end]))
182 				break;
183 
184 		/* compare the word thus isolated against the token list */
185 		for (p = names; p < names + NNAMES; p++) {
186 			if (ascmatch((const unsigned char *)p->name, ubuf + i,
187 			    end - i)) {
188 				subtype = types[p->type].human;
189 				subtype_mime = types[p->type].mime;
190 				goto subtype_identified;
191 			}
192 		}
193 
194 		i = end;
195 	}
196 
197 subtype_identified:
198 
199 	/* Now try to discover other details about the file. */
200 	for (i = 0; i < ulen; i++) {
201 		if (ubuf[i] == '\n') {
202 			if (seen_cr)
203 				n_crlf++;
204 			else
205 				n_lf++;
206 			last_line_end = i;
207 		} else if (seen_cr)
208 			n_cr++;
209 
210 		seen_cr = (ubuf[i] == '\r');
211 		if (seen_cr)
212 			last_line_end = i;
213 
214 		if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
215 			n_nel++;
216 			last_line_end = i;
217 		}
218 
219 		/* If this line is _longer_ than MAXLINELEN, remember it. */
220 		if (i > last_line_end + MAXLINELEN)
221 			has_long_lines = 1;
222 
223 		if (ubuf[i] == '\033')
224 			has_escapes = 1;
225 		if (ubuf[i] == '\b')
226 			has_backspace = 1;
227 	}
228 
229 	/* Beware, if the data has been truncated, the final CR could have
230 	   been followed by a LF.  If we have HOWMANY bytes, it indicates
231 	   that the data might have been truncated, probably even before
232 	   this function was called. */
233 	if (seen_cr && nbytes < HOWMANY)
234 		n_cr++;
235 
236 	if (strcmp(type, "binary") == 0) {
237 		rv = 0;
238 		goto done;
239 	}
240 	if (mime) {
241 		if ((mime & MAGIC_MIME_TYPE) != 0) {
242 			if (subtype_mime) {
243 				if (file_printf(ms, "%s", subtype_mime) == -1)
244 					goto done;
245 			} else {
246 				if (file_printf(ms, "text/plain") == -1)
247 					goto done;
248 			}
249 		}
250 	} else {
251 		if (file_printf(ms, "%s", code) == -1)
252 			goto done;
253 
254 		if (subtype) {
255 			if (file_printf(ms, " %s", subtype) == -1)
256 				goto done;
257 		}
258 
259 		if (file_printf(ms, " %s", type) == -1)
260 			goto done;
261 
262 		if (has_long_lines)
263 			if (file_printf(ms, ", with very long lines") == -1)
264 				goto done;
265 
266 		/*
267 		 * Only report line terminators if we find one other than LF,
268 		 * or if we find none at all.
269 		 */
270 		if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
271 		    (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
272 			if (file_printf(ms, ", with") == -1)
273 				goto done;
274 
275 			if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
276 				if (file_printf(ms, " no") == -1)
277 					goto done;
278 			} else {
279 				if (n_crlf) {
280 					if (file_printf(ms, " CRLF") == -1)
281 						goto done;
282 					if (n_cr || n_lf || n_nel)
283 						if (file_printf(ms, ",") == -1)
284 							goto done;
285 				}
286 				if (n_cr) {
287 					if (file_printf(ms, " CR") == -1)
288 						goto done;
289 					if (n_lf || n_nel)
290 						if (file_printf(ms, ",") == -1)
291 							goto done;
292 				}
293 				if (n_lf) {
294 					if (file_printf(ms, " LF") == -1)
295 						goto done;
296 					if (n_nel)
297 						if (file_printf(ms, ",") == -1)
298 							goto done;
299 				}
300 				if (n_nel)
301 					if (file_printf(ms, " NEL") == -1)
302 						goto done;
303 			}
304 
305 			if (file_printf(ms, " line terminators") == -1)
306 				goto done;
307 		}
308 
309 		if (has_escapes)
310 			if (file_printf(ms, ", with escape sequences") == -1)
311 				goto done;
312 		if (has_backspace)
313 			if (file_printf(ms, ", with overstriking") == -1)
314 				goto done;
315 	}
316 	rv = 1;
317 done:
318 	if (utf8_buf)
319 		free(utf8_buf);
320 
321 	return rv;
322 }
323 
324 private int
325 ascmatch(const unsigned char *s, const unichar *us, size_t ulen)
326 {
327 	size_t i;
328 
329 	for (i = 0; i < ulen; i++) {
330 		if (s[i] != us[i])
331 			return 0;
332 	}
333 
334 	if (s[i])
335 		return 0;
336 	else
337 		return 1;
338 }
339 
340 /*
341  * Encode Unicode string as UTF-8, returning pointer to character
342  * after end of string, or NULL if an invalid character is found.
343  */
344 private unsigned char *
345 encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
346 {
347 	size_t i;
348 	unsigned char *end = buf + len;
349 
350 	for (i = 0; i < ulen; i++) {
351 		if (ubuf[i] <= 0x7f) {
352 			if (end - buf < 1)
353 				return NULL;
354 			*buf++ = (unsigned char)ubuf[i];
355 		} else if (ubuf[i] <= 0x7ff) {
356 			if (end - buf < 2)
357 				return NULL;
358 			*buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
359 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
360 		} else if (ubuf[i] <= 0xffff) {
361 			if (end - buf < 3)
362 				return NULL;
363 			*buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
364 			*buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
365 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
366 		} else if (ubuf[i] <= 0x1fffff) {
367 			if (end - buf < 4)
368 				return NULL;
369 			*buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
370 			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
371 			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
372 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
373 		} else if (ubuf[i] <= 0x3ffffff) {
374 			if (end - buf < 5)
375 				return NULL;
376 			*buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
377 			*buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
378 			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
379 			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
380 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
381 		} else if (ubuf[i] <= 0x7fffffff) {
382 			if (end - buf < 6)
383 				return NULL;
384 			*buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
385 			*buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
386 			*buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
387 			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
388 			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
389 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
390 		} else /* Invalid character */
391 			return NULL;
392 	}
393 
394 	return buf;
395 }
396