168349Sobrien/*
2133359Sobrien * Copyright (c) Ian F. Darwin 1986-1995.
3133359Sobrien * Software written by Ian F. Darwin and others;
4133359Sobrien * maintained 1995-present by Christos Zoulas and others.
5191771Sobrien *
6133359Sobrien * Redistribution and use in source and binary forms, with or without
7133359Sobrien * modification, are permitted provided that the following conditions
8133359Sobrien * are met:
9133359Sobrien * 1. Redistributions of source code must retain the above copyright
10133359Sobrien *    notice immediately at the beginning of the file, without modification,
11133359Sobrien *    this list of conditions, and the following disclaimer.
12133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright
13133359Sobrien *    notice, this list of conditions and the following disclaimer in the
14133359Sobrien *    documentation and/or other materials provided with the distribution.
15191771Sobrien *
16133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19133359Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20133359Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26133359Sobrien * SUCH DAMAGE.
27133359Sobrien */
28133359Sobrien/*
29234449Sobrien * ASCII magic -- try to detect text encoding.
3068349Sobrien *
3168349Sobrien * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
3268349Sobrien * to handle character codes other than ASCII on a unified basis.
3368349Sobrien */
3468349Sobrien
3568349Sobrien#include "file.h"
36191771Sobrien
37191771Sobrien#ifndef	lint
38234449SobrienFILE_RCSID("@(#)$File: ascmagic.c,v 1.84 2011/12/08 12:38:24 rrt Exp $")
39191771Sobrien#endif	/* lint */
40191771Sobrien
41133359Sobrien#include "magic.h"
4268349Sobrien#include <string.h>
4368349Sobrien#include <memory.h>
4468349Sobrien#include <ctype.h>
4568349Sobrien#include <stdlib.h>
4668349Sobrien#ifdef HAVE_UNISTD_H
4768349Sobrien#include <unistd.h>
4868349Sobrien#endif
4968349Sobrien
5068349Sobrien#define MAXLINELEN 300	/* longest sane line length */
5168349Sobrien#define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
5268349Sobrien		  || (x) == 0x85 || (x) == '\f')
5368349Sobrien
54186691Sobrienprivate unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
55191771Sobrienprivate size_t trim_nuls(const unsigned char *, size_t);
5668349Sobrien
57191771Sobrien/*
58191771Sobrien * Undo the NUL-termination kindly provided by process()
59191771Sobrien * but leave at least one byte to look at
60191771Sobrien */
61191771Sobrienprivate size_t
62191771Sobrientrim_nuls(const unsigned char *buf, size_t nbytes)
63191771Sobrien{
64191771Sobrien	while (nbytes > 1 && buf[nbytes - 1] == '\0')
65191771Sobrien		nbytes--;
66133359Sobrien
67191771Sobrien	return nbytes;
68191771Sobrien}
69191771Sobrien
70133359Sobrienprotected int
71234449Sobrienfile_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
72234449Sobrien	int text)
7368349Sobrien{
74191771Sobrien	unichar *ubuf = NULL;
75191771Sobrien	size_t ulen;
76191771Sobrien	int rv = 1;
77191771Sobrien
78191771Sobrien	const char *code = NULL;
79191771Sobrien	const char *code_mime = NULL;
80191771Sobrien	const char *type = NULL;
81191771Sobrien
82191771Sobrien	if (ms->flags & MAGIC_APPLE)
83191771Sobrien		return 0;
84191771Sobrien
85191771Sobrien	nbytes = trim_nuls(buf, nbytes);
86191771Sobrien
87191771Sobrien	/* If file doesn't look like any sort of text, give up. */
88191771Sobrien	if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime,
89234449Sobrien	    &type) == 0)
90191771Sobrien		rv = 0;
91234449Sobrien        else
92234449Sobrien		rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
93234449Sobrien						 type, text);
94191771Sobrien
95234449Sobrien	free(ubuf);
96191771Sobrien
97191771Sobrien	return rv;
98191771Sobrien}
99191771Sobrien
100191771Sobrienprotected int
101191771Sobrienfile_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
102191771Sobrien    size_t nbytes, unichar *ubuf, size_t ulen, const char *code,
103234449Sobrien    const char *type, int text)
104191771Sobrien{
105191771Sobrien	unsigned char *utf8_buf = NULL, *utf8_end;
106191771Sobrien	size_t mlen, i;
107159764Sobrien	int rv = -1;
108175296Sobrien	int mime = ms->flags & MAGIC_MIME;
10968349Sobrien
110133359Sobrien	const char *subtype = NULL;
111133359Sobrien	const char *subtype_mime = NULL;
11268349Sobrien
11368349Sobrien	int has_escapes = 0;
11468349Sobrien	int has_backspace = 0;
115159764Sobrien	int seen_cr = 0;
11668349Sobrien
11768349Sobrien	int n_crlf = 0;
11868349Sobrien	int n_lf = 0;
11968349Sobrien	int n_cr = 0;
12068349Sobrien	int n_nel = 0;
121234449Sobrien	int executable = 0;
12268349Sobrien
123169962Sobrien	size_t last_line_end = (size_t)-1;
12468349Sobrien	int has_long_lines = 0;
12568349Sobrien
126191771Sobrien	if (ms->flags & MAGIC_APPLE)
127191771Sobrien		return 0;
12868349Sobrien
129191771Sobrien	nbytes = trim_nuls(buf, nbytes);
130133359Sobrien
131191771Sobrien	/* If we have fewer than 2 bytes, give up. */
132159764Sobrien	if (nbytes <= 1) {
133159764Sobrien		rv = 0;
134159764Sobrien		goto done;
135159764Sobrien	}
136159764Sobrien
137234449Sobrien	if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
138234449Sobrien		/* Convert ubuf to UTF-8 and try text soft magic */
139234449Sobrien		/* malloc size is a conservative overestimate; could be
140234449Sobrien		   improved, or at least realloced after conversion. */
141234449Sobrien		mlen = ulen * 6;
142234449Sobrien		if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
143234449Sobrien			file_oomem(ms, mlen);
144234449Sobrien			goto done;
14568349Sobrien		}
146234449Sobrien		if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
147234449Sobrien		    == NULL)
148234449Sobrien			goto done;
149234449Sobrien		if ((rv = file_softmagic(ms, utf8_buf,
150267829Sdelphij		    (size_t)(utf8_end - utf8_buf), 0, TEXTTEST, text)) == 0)
151234449Sobrien			rv = -1;
15268349Sobrien	}
15368349Sobrien
154186691Sobrien	/* Now try to discover other details about the file. */
15568349Sobrien	for (i = 0; i < ulen; i++) {
156159764Sobrien		if (ubuf[i] == '\n') {
157159764Sobrien			if (seen_cr)
158159764Sobrien				n_crlf++;
159159764Sobrien			else
160159764Sobrien				n_lf++;
161159764Sobrien			last_line_end = i;
162159764Sobrien		} else if (seen_cr)
163159764Sobrien			n_cr++;
164159764Sobrien
165159764Sobrien		seen_cr = (ubuf[i] == '\r');
166159764Sobrien		if (seen_cr)
167159764Sobrien			last_line_end = i;
168159764Sobrien
169159764Sobrien		if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
170159764Sobrien			n_nel++;
171159764Sobrien			last_line_end = i;
172159764Sobrien		}
173159764Sobrien
174159764Sobrien		/* If this line is _longer_ than MAXLINELEN, remember it. */
17568349Sobrien		if (i > last_line_end + MAXLINELEN)
17668349Sobrien			has_long_lines = 1;
17768349Sobrien
17868349Sobrien		if (ubuf[i] == '\033')
17968349Sobrien			has_escapes = 1;
18068349Sobrien		if (ubuf[i] == '\b')
18168349Sobrien			has_backspace = 1;
18268349Sobrien	}
18368349Sobrien
184159764Sobrien	/* Beware, if the data has been truncated, the final CR could have
185159764Sobrien	   been followed by a LF.  If we have HOWMANY bytes, it indicates
186159764Sobrien	   that the data might have been truncated, probably even before
187159764Sobrien	   this function was called. */
188159764Sobrien	if (seen_cr && nbytes < HOWMANY)
189159764Sobrien		n_cr++;
190159764Sobrien
191191771Sobrien	if (strcmp(type, "binary") == 0) {
192191771Sobrien		rv = 0;
193191771Sobrien		goto done;
194191771Sobrien	}
195175296Sobrien	if (mime) {
196234449Sobrien		if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) {
197175296Sobrien			if (subtype_mime) {
198191771Sobrien				if (file_printf(ms, "%s", subtype_mime) == -1)
199175296Sobrien					goto done;
200175296Sobrien			} else {
201175296Sobrien				if (file_printf(ms, "text/plain") == -1)
202175296Sobrien					goto done;
203175296Sobrien			}
204133359Sobrien		}
20568349Sobrien	} else {
206234449Sobrien		if (file_printedlen(ms)) {
207234449Sobrien			switch (file_replace(ms, " text$", ", ")) {
208234449Sobrien			case 0:
209234449Sobrien				switch (file_replace(ms, " text executable$",
210234449Sobrien				    ", ")) {
211234449Sobrien				case 0:
212234449Sobrien					if (file_printf(ms, ", ") == -1)
213234449Sobrien						goto done;
214234449Sobrien				case -1:
215234449Sobrien					goto done;
216234449Sobrien				default:
217234449Sobrien					executable = 1;
218234449Sobrien					break;
219234449Sobrien				}
220234449Sobrien				break;
221234449Sobrien			case -1:
222234449Sobrien				goto done;
223234449Sobrien			default:
224234449Sobrien				break;
225234449Sobrien			}
226234449Sobrien		}
227234449Sobrien
228191771Sobrien		if (file_printf(ms, "%s", code) == -1)
229159764Sobrien			goto done;
23068349Sobrien
23168349Sobrien		if (subtype) {
232191771Sobrien			if (file_printf(ms, " %s", subtype) == -1)
233159764Sobrien				goto done;
23468349Sobrien		}
23568349Sobrien
236191771Sobrien		if (file_printf(ms, " %s", type) == -1)
237159764Sobrien			goto done;
23868349Sobrien
239234449Sobrien		if (executable)
240234449Sobrien			if (file_printf(ms, " executable") == -1)
241234449Sobrien				goto done;
242234449Sobrien
24368349Sobrien		if (has_long_lines)
244133359Sobrien			if (file_printf(ms, ", with very long lines") == -1)
245159764Sobrien				goto done;
24668349Sobrien
24768349Sobrien		/*
24868349Sobrien		 * Only report line terminators if we find one other than LF,
24968349Sobrien		 * or if we find none at all.
25068349Sobrien		 */
25168349Sobrien		if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
25268349Sobrien		    (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
253133359Sobrien			if (file_printf(ms, ", with") == -1)
254159764Sobrien				goto done;
25568349Sobrien
256191771Sobrien			if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
257133359Sobrien				if (file_printf(ms, " no") == -1)
258159764Sobrien					goto done;
259133359Sobrien			} else {
26068349Sobrien				if (n_crlf) {
261133359Sobrien					if (file_printf(ms, " CRLF") == -1)
262159764Sobrien						goto done;
26368349Sobrien					if (n_cr || n_lf || n_nel)
264133359Sobrien						if (file_printf(ms, ",") == -1)
265159764Sobrien							goto done;
26668349Sobrien				}
26768349Sobrien				if (n_cr) {
268133359Sobrien					if (file_printf(ms, " CR") == -1)
269159764Sobrien						goto done;
27068349Sobrien					if (n_lf || n_nel)
271133359Sobrien						if (file_printf(ms, ",") == -1)
272159764Sobrien							goto done;
27368349Sobrien				}
27468349Sobrien				if (n_lf) {
275133359Sobrien					if (file_printf(ms, " LF") == -1)
276159764Sobrien						goto done;
27768349Sobrien					if (n_nel)
278133359Sobrien						if (file_printf(ms, ",") == -1)
279159764Sobrien							goto done;
28068349Sobrien				}
28168349Sobrien				if (n_nel)
282133359Sobrien					if (file_printf(ms, " NEL") == -1)
283159764Sobrien						goto done;
28468349Sobrien			}
28568349Sobrien
286133359Sobrien			if (file_printf(ms, " line terminators") == -1)
287159764Sobrien				goto done;
28868349Sobrien		}
28968349Sobrien
29068349Sobrien		if (has_escapes)
291133359Sobrien			if (file_printf(ms, ", with escape sequences") == -1)
292159764Sobrien				goto done;
29368349Sobrien		if (has_backspace)
294133359Sobrien			if (file_printf(ms, ", with overstriking") == -1)
295159764Sobrien				goto done;
29668349Sobrien	}
297159764Sobrien	rv = 1;
298159764Sobriendone:
299234449Sobrien	free(utf8_buf);
30068349Sobrien
301159764Sobrien	return rv;
30268349Sobrien}
30368349Sobrien
30468349Sobrien/*
305186691Sobrien * Encode Unicode string as UTF-8, returning pointer to character
306186691Sobrien * after end of string, or NULL if an invalid character is found.
307186691Sobrien */
308186691Sobrienprivate unsigned char *
309186691Sobrienencode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
31068349Sobrien{
311169962Sobrien	size_t i;
312186691Sobrien	unsigned char *end = buf + len;
313186691Sobrien
314186691Sobrien	for (i = 0; i < ulen; i++) {
315186691Sobrien		if (ubuf[i] <= 0x7f) {
316186691Sobrien			if (end - buf < 1)
317186691Sobrien				return NULL;
318186691Sobrien			*buf++ = (unsigned char)ubuf[i];
319186691Sobrien		} else if (ubuf[i] <= 0x7ff) {
320186691Sobrien			if (end - buf < 2)
321186691Sobrien				return NULL;
322186691Sobrien			*buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
323186691Sobrien			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
324186691Sobrien		} else if (ubuf[i] <= 0xffff) {
325186691Sobrien			if (end - buf < 3)
326186691Sobrien				return NULL;
327186691Sobrien			*buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
328186691Sobrien			*buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
329186691Sobrien			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
330186691Sobrien		} else if (ubuf[i] <= 0x1fffff) {
331186691Sobrien			if (end - buf < 4)
332186691Sobrien				return NULL;
333186691Sobrien			*buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
334186691Sobrien			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
335186691Sobrien			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
336186691Sobrien			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
337186691Sobrien		} else if (ubuf[i] <= 0x3ffffff) {
338186691Sobrien			if (end - buf < 5)
339186691Sobrien				return NULL;
340186691Sobrien			*buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
341186691Sobrien			*buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
342186691Sobrien			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
343186691Sobrien			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
344186691Sobrien			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
345186691Sobrien		} else if (ubuf[i] <= 0x7fffffff) {
346186691Sobrien			if (end - buf < 6)
347186691Sobrien				return NULL;
348186691Sobrien			*buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
349186691Sobrien			*buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
350186691Sobrien			*buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
351186691Sobrien			*buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
352186691Sobrien			*buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
353186691Sobrien			*buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
354186691Sobrien		} else /* Invalid character */
355186691Sobrien			return NULL;
356186691Sobrien	}
357186691Sobrien
358186691Sobrien	return buf;
359186691Sobrien}
360