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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 * ASCII magic -- try to detect text encoding.
31 * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
32 * to handle character codes other than ASCII on a unified basis.
38 FILE_RCSID("@(#)$File: ascmagic.c,v 1.88 2014/02/12 23:20:53 christos Exp $")
50 #define MAXLINELEN 300 /* longest sane line length */
51 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
52 || (x) == 0x85 || (x) == '\f')
54 private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
55 private size_t trim_nuls(const unsigned char *, size_t);
58 * Undo the NUL-termination kindly provided by process()
59 * but leave at least one byte to look at
62 trim_nuls(const unsigned char *buf, size_t nbytes)
64 while (nbytes > 1 && buf[nbytes - 1] == '\0')
71 file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
78 const char *code = NULL;
79 const char *code_mime = NULL;
80 const char *type = NULL;
82 if (ms->flags & MAGIC_APPLE)
85 nbytes = trim_nuls(buf, nbytes);
87 /* If file doesn't look like any sort of text, give up. */
88 if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime,
92 rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
101 file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
102 size_t nbytes, unichar *ubuf, size_t ulen, const char *code,
103 const char *type, int text)
105 unsigned char *utf8_buf = NULL, *utf8_end;
108 int mime = ms->flags & MAGIC_MIME;
110 const char *subtype = NULL;
111 const char *subtype_mime = NULL;
114 int has_backspace = 0;
123 size_t last_line_end = (size_t)-1;
124 int has_long_lines = 0;
126 if (ms->flags & MAGIC_APPLE)
129 nbytes = trim_nuls(buf, nbytes);
131 /* If we have fewer than 2 bytes, give up. */
137 if (ulen > 0 && (ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
138 /* Convert ubuf to UTF-8 and try text soft magic */
139 /* malloc size is a conservative overestimate; could be
140 improved, or at least realloced after conversion. */
142 if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
143 file_oomem(ms, mlen);
146 if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
149 if ((rv = file_softmagic(ms, utf8_buf,
150 (size_t)(utf8_end - utf8_buf), 0, TEXTTEST, text)) == 0)
154 /* Now try to discover other details about the file. */
155 for (i = 0; i < ulen; i++) {
156 if (ubuf[i] == '\n') {
165 seen_cr = (ubuf[i] == '\r');
169 if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
174 /* If this line is _longer_ than MAXLINELEN, remember it. */
175 if (i > last_line_end + MAXLINELEN)
178 if (ubuf[i] == '\033')
184 /* Beware, if the data has been truncated, the final CR could have
185 been followed by a LF. If we have HOWMANY bytes, it indicates
186 that the data might have been truncated, probably even before
187 this function was called. */
188 if (seen_cr && nbytes < HOWMANY)
191 if (strcmp(type, "binary") == 0) {
196 if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) {
198 if (file_printf(ms, "%s", subtype_mime) == -1)
201 if (file_printf(ms, "text/plain") == -1)
206 if (file_printedlen(ms)) {
207 switch (file_replace(ms, " text$", ", ")) {
209 switch (file_replace(ms, " text executable$",
212 if (file_printf(ms, ", ") == -1)
229 if (file_printf(ms, "%s", code) == -1)
233 if (file_printf(ms, " %s", subtype) == -1)
237 if (file_printf(ms, " %s", type) == -1)
241 if (file_printf(ms, " executable") == -1)
245 if (file_printf(ms, ", with very long lines") == -1)
249 * Only report line terminators if we find one other than LF,
250 * or if we find none at all.
252 if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
253 (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
254 if (file_printf(ms, ", with") == -1)
257 if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
258 if (file_printf(ms, " no") == -1)
262 if (file_printf(ms, " CRLF") == -1)
264 if (n_cr || n_lf || n_nel)
265 if (file_printf(ms, ",") == -1)
269 if (file_printf(ms, " CR") == -1)
272 if (file_printf(ms, ",") == -1)
276 if (file_printf(ms, " LF") == -1)
279 if (file_printf(ms, ",") == -1)
283 if (file_printf(ms, " NEL") == -1)
287 if (file_printf(ms, " line terminators") == -1)
292 if (file_printf(ms, ", with escape sequences") == -1)
295 if (file_printf(ms, ", with overstriking") == -1)
306 * Encode Unicode string as UTF-8, returning pointer to character
307 * after end of string, or NULL if an invalid character is found.
309 private unsigned char *
310 encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
313 unsigned char *end = buf + len;
315 for (i = 0; i < ulen; i++) {
316 if (ubuf[i] <= 0x7f) {
319 *buf++ = (unsigned char)ubuf[i];
320 } else if (ubuf[i] <= 0x7ff) {
323 *buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
324 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
325 } else if (ubuf[i] <= 0xffff) {
328 *buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
329 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
330 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
331 } else if (ubuf[i] <= 0x1fffff) {
334 *buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
335 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
336 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
337 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
338 } else if (ubuf[i] <= 0x3ffffff) {
341 *buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
342 *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
343 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
344 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
345 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
346 } else if (ubuf[i] <= 0x7fffffff) {
349 *buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
350 *buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
351 *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
352 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
353 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
354 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
355 } else /* Invalid character */