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