xref: /netbsd-src/external/bsd/file/dist/src/ascmagic.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: ascmagic.c,v 1.1.1.9 2018/04/15 19:32:48 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.98 2017/11/02 20:25:39 christos Exp $")
42 #else
43 __RCSID("$NetBSD: ascmagic.c,v 1.1.1.9 2018/04/15 19:32:48 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(b->fbuf, b->flen);
90 
91 	/* If file doesn't look like any sort of text, give up. */
92 	if (file_encoding(ms, &bb, &ubuf, &ulen, &code, &code_mime,
93 	    &type) == 0)
94 		rv = 0;
95         else
96 		rv = file_ascmagic_with_encoding(ms, &bb,
97 		    ubuf, ulen, code, type, text);
98 
99 	free(ubuf);
100 
101 	return rv;
102 }
103 
104 protected int
105 file_ascmagic_with_encoding(struct magic_set *ms,
106     const struct buffer *b, unichar *ubuf, size_t ulen, const char *code,
107     const char *type, int text)
108 {
109 	struct buffer bb;
110 	const unsigned char *buf = b->fbuf;
111 	size_t nbytes = b->flen;
112 	unsigned char *utf8_buf = NULL, *utf8_end;
113 	size_t mlen, i;
114 	int rv = -1;
115 	int mime = ms->flags & MAGIC_MIME;
116 
117 	const char *subtype = NULL;
118 	const char *subtype_mime = NULL;
119 
120 	int has_escapes = 0;
121 	int has_backspace = 0;
122 	int seen_cr = 0;
123 
124 	int n_crlf = 0;
125 	int n_lf = 0;
126 	int n_cr = 0;
127 	int n_nel = 0;
128 	int executable = 0;
129 
130 	size_t last_line_end = (size_t)-1;
131 	int has_long_lines = 0;
132 
133 	nbytes = trim_nuls(buf, nbytes);
134 
135 	/* If we have fewer than 2 bytes, give up. */
136 	if (nbytes <= 1) {
137 		rv = 0;
138 		goto done;
139 	}
140 
141 	if (ulen > 0 && (ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
142 		/* Convert ubuf to UTF-8 and try text soft magic */
143 		/* malloc size is a conservative overestimate; could be
144 		   improved, or at least realloced after conversion. */
145 		mlen = ulen * 6;
146 		if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
147 			file_oomem(ms, mlen);
148 			goto done;
149 		}
150 		if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
151 		    == NULL)
152 			goto done;
153 		buffer_init(&bb, b->fd, utf8_buf,
154 		    (size_t)(utf8_end - utf8_buf));
155 
156 		if ((rv = file_softmagic(ms, &bb, NULL, NULL,
157 		    TEXTTEST, text)) == 0)
158 			rv = -1;
159 		buffer_fini(&bb);
160 		if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
161 			rv = rv == -1 ? 0 : 1;
162 			goto done;
163 		}
164 	}
165 	if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)))
166 		return 0;
167 
168 	/* Now try to discover other details about the file. */
169 	for (i = 0; i < ulen; i++) {
170 		if (ubuf[i] == '\n') {
171 			if (seen_cr)
172 				n_crlf++;
173 			else
174 				n_lf++;
175 			last_line_end = i;
176 		} else if (seen_cr)
177 			n_cr++;
178 
179 		seen_cr = (ubuf[i] == '\r');
180 		if (seen_cr)
181 			last_line_end = i;
182 
183 		if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
184 			n_nel++;
185 			last_line_end = i;
186 		}
187 
188 		/* If this line is _longer_ than MAXLINELEN, remember it. */
189 		if (i > last_line_end + MAXLINELEN)
190 			has_long_lines = 1;
191 
192 		if (ubuf[i] == '\033')
193 			has_escapes = 1;
194 		if (ubuf[i] == '\b')
195 			has_backspace = 1;
196 	}
197 
198 	/* Beware, if the data has been truncated, the final CR could have
199 	   been followed by a LF.  If we have ms->bytes_max bytes, it indicates
200 	   that the data might have been truncated, probably even before
201 	   this function was called. */
202 	if (seen_cr && nbytes < ms->bytes_max)
203 		n_cr++;
204 
205 	if (strcmp(type, "binary") == 0) {
206 		rv = 0;
207 		goto done;
208 	}
209 	if (mime) {
210 		if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) {
211 			if (subtype_mime) {
212 				if (file_printf(ms, "%s", subtype_mime) == -1)
213 					goto done;
214 			} else {
215 				if (file_printf(ms, "text/plain") == -1)
216 					goto done;
217 			}
218 		}
219 	} else {
220 		if (file_printedlen(ms)) {
221 			switch (file_replace(ms, " text$", ", ")) {
222 			case 0:
223 				switch (file_replace(ms, " text executable$",
224 				    ", ")) {
225 				case 0:
226 					if (file_printf(ms, ", ") == -1)
227 						goto done;
228 					break;
229 				case -1:
230 					goto done;
231 				default:
232 					executable = 1;
233 					break;
234 				}
235 				break;
236 			case -1:
237 				goto done;
238 			default:
239 				break;
240 			}
241 		}
242 
243 		if (file_printf(ms, "%s", code) == -1)
244 			goto done;
245 
246 		if (subtype) {
247 			if (file_printf(ms, " %s", subtype) == -1)
248 				goto done;
249 		}
250 
251 		if (file_printf(ms, " %s", type) == -1)
252 			goto done;
253 
254 		if (executable)
255 			if (file_printf(ms, " executable") == -1)
256 				goto done;
257 
258 		if (has_long_lines)
259 			if (file_printf(ms, ", with very long lines") == -1)
260 				goto done;
261 
262 		/*
263 		 * Only report line terminators if we find one other than LF,
264 		 * or if we find none at all.
265 		 */
266 		if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
267 		    (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
268 			if (file_printf(ms, ", with") == -1)
269 				goto done;
270 
271 			if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
272 				if (file_printf(ms, " no") == -1)
273 					goto done;
274 			} else {
275 				if (n_crlf) {
276 					if (file_printf(ms, " CRLF") == -1)
277 						goto done;
278 					if (n_cr || n_lf || n_nel)
279 						if (file_printf(ms, ",") == -1)
280 							goto done;
281 				}
282 				if (n_cr) {
283 					if (file_printf(ms, " CR") == -1)
284 						goto done;
285 					if (n_lf || n_nel)
286 						if (file_printf(ms, ",") == -1)
287 							goto done;
288 				}
289 				if (n_lf) {
290 					if (file_printf(ms, " LF") == -1)
291 						goto done;
292 					if (n_nel)
293 						if (file_printf(ms, ",") == -1)
294 							goto done;
295 				}
296 				if (n_nel)
297 					if (file_printf(ms, " NEL") == -1)
298 						goto done;
299 			}
300 
301 			if (file_printf(ms, " line terminators") == -1)
302 				goto done;
303 		}
304 
305 		if (has_escapes)
306 			if (file_printf(ms, ", with escape sequences") == -1)
307 				goto done;
308 		if (has_backspace)
309 			if (file_printf(ms, ", with overstriking") == -1)
310 				goto done;
311 	}
312 	rv = 1;
313 done:
314 	free(utf8_buf);
315 
316 	return rv;
317 }
318 
319 /*
320  * Encode Unicode string as UTF-8, returning pointer to character
321  * after end of string, or NULL if an invalid character is found.
322  */
323 private unsigned char *
324 encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
325 {
326 	size_t i;
327 	unsigned char *end = buf + len;
328 
329 	for (i = 0; i < ulen; i++) {
330 		if (ubuf[i] <= 0x7f) {
331 			if (end - buf < 1)
332 				return NULL;
333 			*buf++ = (unsigned char)ubuf[i];
334 		} else if (ubuf[i] <= 0x7ff) {
335 			if (end - buf < 2)
336 				return NULL;
337 			*buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
338 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
339 		} else if (ubuf[i] <= 0xffff) {
340 			if (end - buf < 3)
341 				return NULL;
342 			*buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
343 			*buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
344 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
345 		} else if (ubuf[i] <= 0x1fffff) {
346 			if (end - buf < 4)
347 				return NULL;
348 			*buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
349 			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
350 			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
351 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
352 		} else if (ubuf[i] <= 0x3ffffff) {
353 			if (end - buf < 5)
354 				return NULL;
355 			*buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
356 			*buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
357 			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
358 			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
359 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
360 		} else if (ubuf[i] <= 0x7fffffff) {
361 			if (end - buf < 6)
362 				return NULL;
363 			*buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
364 			*buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
365 			*buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
366 			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
367 			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
368 			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
369 		} else /* Invalid character */
370 			return NULL;
371 	}
372 
373 	return buf;
374 }
375