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 -- file types that we know based on keywords
30 * that can appear anywhere in the file.
32 * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
33 * to handle character codes other than ASCII on a unified basis.
39 FILE_RCSID("@(#)$File: ascmagic.c,v 1.77 2010/11/30 14:58:53 rrt Exp $")
52 #define MAXLINELEN 300 /* longest sane line length */
53 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
54 || (x) == 0x85 || (x) == '\f')
56 private int ascmatch(const unsigned char *, const unichar *, size_t);
57 private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
58 private size_t trim_nuls(const unsigned char *, size_t);
61 * Undo the NUL-termination kindly provided by process()
62 * but leave at least one byte to look at
65 trim_nuls(const unsigned char *buf, size_t nbytes)
67 while (nbytes > 1 && buf[nbytes - 1] == '\0')
74 file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
80 const char *code = NULL;
81 const char *code_mime = NULL;
82 const char *type = NULL;
84 if (ms->flags & MAGIC_APPLE)
87 nbytes = trim_nuls(buf, nbytes);
89 /* If file doesn't look like any sort of text, give up. */
90 if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime,
96 rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
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,
111 unsigned char *utf8_buf = NULL, *utf8_end;
113 const struct names *p;
115 int mime = ms->flags & MAGIC_MIME;
117 const char *subtype = NULL;
118 const char *subtype_mime = NULL;
121 int has_backspace = 0;
130 size_t last_line_end = (size_t)-1;
131 int has_long_lines = 0;
133 if (ms->flags & MAGIC_APPLE)
136 nbytes = trim_nuls(buf, nbytes);
138 /* If we have fewer than 2 bytes, give up. */
144 if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
145 /* Convert ubuf to UTF-8 and try text soft magic */
146 /* malloc size is a conservative overestimate; could be
147 improved, or at least realloced after conversion. */
149 if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
150 file_oomem(ms, mlen);
153 if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) == NULL)
155 if ((rv = file_softmagic(ms, utf8_buf, (size_t)(utf8_end - utf8_buf),
162 /* look for tokens from names.h - this is expensive! */
163 if ((ms->flags & MAGIC_NO_CHECK_TOKENS) != 0)
164 goto subtype_identified;
172 /* skip past any leading space */
173 while (i < ulen && ISSPC(ubuf[i]))
178 /* find the next whitespace */
179 for (end = i + 1; end < nbytes; end++)
180 if (ISSPC(ubuf[end]))
183 /* compare the word thus isolated against the token list */
184 for (p = names; p < names + NNAMES; p++) {
185 if (ascmatch((const unsigned char *)p->name, ubuf + i,
189 else if (curtype != p->type) {
195 subtype = types[p->type].human;
196 subtype_mime = types[p->type].mime;
197 goto subtype_identified;
207 /* Now try to discover other details about the file. */
208 for (i = 0; i < ulen; i++) {
209 if (ubuf[i] == '\n') {
218 seen_cr = (ubuf[i] == '\r');
222 if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
227 /* If this line is _longer_ than MAXLINELEN, remember it. */
228 if (i > last_line_end + MAXLINELEN)
231 if (ubuf[i] == '\033')
237 /* Beware, if the data has been truncated, the final CR could have
238 been followed by a LF. If we have HOWMANY bytes, it indicates
239 that the data might have been truncated, probably even before
240 this function was called. */
241 if (seen_cr && nbytes < HOWMANY)
244 if (strcmp(type, "binary") == 0) {
249 if ((mime & MAGIC_MIME_TYPE) != 0) {
251 if (file_printf(ms, "%s", subtype_mime) == -1)
254 if (file_printf(ms, "text/plain") == -1)
259 if (file_printf(ms, "%s", code) == -1)
263 if (file_printf(ms, " %s", subtype) == -1)
267 if (file_printf(ms, " %s", type) == -1)
271 if (file_printf(ms, ", with very long lines") == -1)
275 * Only report line terminators if we find one other than LF,
276 * or if we find none at all.
278 if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
279 (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
280 if (file_printf(ms, ", with") == -1)
283 if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
284 if (file_printf(ms, " no") == -1)
288 if (file_printf(ms, " CRLF") == -1)
290 if (n_cr || n_lf || n_nel)
291 if (file_printf(ms, ",") == -1)
295 if (file_printf(ms, " CR") == -1)
298 if (file_printf(ms, ",") == -1)
302 if (file_printf(ms, " LF") == -1)
305 if (file_printf(ms, ",") == -1)
309 if (file_printf(ms, " NEL") == -1)
313 if (file_printf(ms, " line terminators") == -1)
318 if (file_printf(ms, ", with escape sequences") == -1)
321 if (file_printf(ms, ", with overstriking") == -1)
333 ascmatch(const unsigned char *s, const unichar *us, size_t ulen)
337 for (i = 0; i < ulen; i++) {
349 * Encode Unicode string as UTF-8, returning pointer to character
350 * after end of string, or NULL if an invalid character is found.
352 private unsigned char *
353 encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
356 unsigned char *end = buf + len;
358 for (i = 0; i < ulen; i++) {
359 if (ubuf[i] <= 0x7f) {
362 *buf++ = (unsigned char)ubuf[i];
363 } else if (ubuf[i] <= 0x7ff) {
366 *buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
367 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
368 } else if (ubuf[i] <= 0xffff) {
371 *buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
372 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
373 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
374 } else if (ubuf[i] <= 0x1fffff) {
377 *buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
378 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
379 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
380 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
381 } else if (ubuf[i] <= 0x3ffffff) {
384 *buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
385 *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
386 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
387 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
388 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
389 } else if (ubuf[i] <= 0x7fffffff) {
392 *buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
393 *buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
394 *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
395 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
396 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
397 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
398 } else /* Invalid character */