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