xref: /netbsd-src/external/bsd/file/dist/src/ascmagic.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: ascmagic.c,v 1.1.1.6 2014/06/13 01:48:22 christos Exp $	*/
2 /*
3  * Copyright (c) Ian F. Darwin 1986-1995.
4  * Software written by Ian F. Darwin and others;
5  * maintained 1995-present by Christos Zoulas and others.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*
30  * ASCII magic -- try to detect text encoding.
31  *
32  * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
33  * to handle character codes other than ASCII on a unified basis.
34  */
35 
36 #include "file.h"
37 
38 #ifndef	lint
39 #if 0
40 FILE_RCSID("@(#)$File: ascmagic.c,v 1.88 2014/02/12 23:20:53 christos Exp $")
41 #else
42 __RCSID("$NetBSD: ascmagic.c,v 1.1.1.6 2014/06/13 01:48:22 christos Exp $");
43 #endif
44 #endif	/* lint */
45 
46 #include "magic.h"
47 #include <string.h>
48 #include <memory.h>
49 #include <ctype.h>
50 #include <stdlib.h>
51 #ifdef HAVE_UNISTD_H
52 #include <unistd.h>
53 #endif
54 
55 #define MAXLINELEN 300	/* longest sane line length */
56 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
57 		  || (x) == 0x85 || (x) == '\f')
58 
59 private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
60 private size_t trim_nuls(const unsigned char *, size_t);
61 
62 /*
63  * Undo the NUL-termination kindly provided by process()
64  * but leave at least one byte to look at
65  */
66 private size_t
67 trim_nuls(const unsigned char *buf, size_t nbytes)
68 {
69 	while (nbytes > 1 && buf[nbytes - 1] == '\0')
70 		nbytes--;
71 
72 	return nbytes;
73 }
74 
75 protected int
76 file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
77 	int text)
78 {
79 	unichar *ubuf = NULL;
80 	size_t ulen = 0;
81 	int rv = 1;
82 
83 	const char *code = NULL;
84 	const char *code_mime = NULL;
85 	const char *type = NULL;
86 
87 	if (ms->flags & MAGIC_APPLE)
88 		return 0;
89 
90 	nbytes = trim_nuls(buf, nbytes);
91 
92 	/* If file doesn't look like any sort of text, give up. */
93 	if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime,
94 	    &type) == 0)
95 		rv = 0;
96         else
97 		rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
98 						 type, text);
99 
100 	free(ubuf);
101 
102 	return rv;
103 }
104 
105 protected int
106 file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
107     size_t nbytes, unichar *ubuf, size_t ulen, const char *code,
108     const char *type, int text)
109 {
110 	unsigned char *utf8_buf = NULL, *utf8_end;
111 	size_t mlen, i;
112 	int rv = -1;
113 	int mime = ms->flags & MAGIC_MIME;
114 
115 	const char *subtype = NULL;
116 	const char *subtype_mime = NULL;
117 
118 	int has_escapes = 0;
119 	int has_backspace = 0;
120 	int seen_cr = 0;
121 
122 	int n_crlf = 0;
123 	int n_lf = 0;
124 	int n_cr = 0;
125 	int n_nel = 0;
126 	int executable = 0;
127 
128 	size_t last_line_end = (size_t)-1;
129 	int has_long_lines = 0;
130 
131 	if (ms->flags & MAGIC_APPLE)
132 		return 0;
133 
134 	nbytes = trim_nuls(buf, nbytes);
135 
136 	/* If we have fewer than 2 bytes, give up. */
137 	if (nbytes <= 1) {
138 		rv = 0;
139 		goto done;
140 	}
141 
142 	if (ulen > 0 && (ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
143 		/* Convert ubuf to UTF-8 and try text soft magic */
144 		/* malloc size is a conservative overestimate; could be
145 		   improved, or at least realloced after conversion. */
146 		mlen = ulen * 6;
147 		if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
148 			file_oomem(ms, mlen);
149 			goto done;
150 		}
151 		if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
152 		    == NULL)
153 			goto done;
154 		if ((rv = file_softmagic(ms, utf8_buf,
155 		    (size_t)(utf8_end - utf8_buf), 0, TEXTTEST, text)) == 0)
156 			rv = -1;
157 	}
158 
159 	/* Now try to discover other details about the file. */
160 	for (i = 0; i < ulen; i++) {
161 		if (ubuf[i] == '\n') {
162 			if (seen_cr)
163 				n_crlf++;
164 			else
165 				n_lf++;
166 			last_line_end = i;
167 		} else if (seen_cr)
168 			n_cr++;
169 
170 		seen_cr = (ubuf[i] == '\r');
171 		if (seen_cr)
172 			last_line_end = i;
173 
174 		if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
175 			n_nel++;
176 			last_line_end = i;
177 		}
178 
179 		/* If this line is _longer_ than MAXLINELEN, remember it. */
180 		if (i > last_line_end + MAXLINELEN)
181 			has_long_lines = 1;
182 
183 		if (ubuf[i] == '\033')
184 			has_escapes = 1;
185 		if (ubuf[i] == '\b')
186 			has_backspace = 1;
187 	}
188 
189 	/* Beware, if the data has been truncated, the final CR could have
190 	   been followed by a LF.  If we have HOWMANY bytes, it indicates
191 	   that the data might have been truncated, probably even before
192 	   this function was called. */
193 	if (seen_cr && nbytes < HOWMANY)
194 		n_cr++;
195 
196 	if (strcmp(type, "binary") == 0) {
197 		rv = 0;
198 		goto done;
199 	}
200 	if (mime) {
201 		if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) {
202 			if (subtype_mime) {
203 				if (file_printf(ms, "%s", subtype_mime) == -1)
204 					goto done;
205 			} else {
206 				if (file_printf(ms, "text/plain") == -1)
207 					goto done;
208 			}
209 		}
210 	} else {
211 		if (file_printedlen(ms)) {
212 			switch (file_replace(ms, " text$", ", ")) {
213 			case 0:
214 				switch (file_replace(ms, " text executable$",
215 				    ", ")) {
216 				case 0:
217 					if (file_printf(ms, ", ") == -1)
218 						goto done;
219 					break;
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