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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 * ASCII magic -- file types that we know based on keywords
32 * that can appear anywhere in the file.
34 * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
35 * to handle character codes other than ASCII on a unified basis.
42 FILE_RCSID("@(#)$File: ascmagic.c,v 1.75 2009/02/03 20:27:51 christos Exp $")
58 #define MAXLINELEN 300 /* longest sane line length */
59 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
60 || (x) == 0x85 || (x) == '\f')
62 private int ascmatch(const unsigned char *, const unichar
*, size_t);
63 private unsigned char *encode_utf8(unsigned char *, size_t, unichar
*, size_t);
64 private size_t trim_nuls(const unsigned char *, size_t);
67 * Undo the NUL-termination kindly provided by process()
68 * but leave at least one byte to look at
71 trim_nuls(const unsigned char *buf
, size_t nbytes
)
73 while (nbytes
> 1 && buf
[nbytes
- 1] == '\0')
80 file_ascmagic(struct magic_set
*ms
, const unsigned char *buf
, size_t nbytes
)
86 const char *code
= NULL
;
87 const char *code_mime
= NULL
;
88 const char *type
= NULL
;
90 if (ms
->flags
& MAGIC_APPLE
)
93 nbytes
= trim_nuls(buf
, nbytes
);
95 /* If file doesn't look like any sort of text, give up. */
96 if (file_encoding(ms
, buf
, nbytes
, &ubuf
, &ulen
, &code
, &code_mime
,
102 rv
= file_ascmagic_with_encoding(ms
, buf
, nbytes
, ubuf
, ulen
, code
,
113 file_ascmagic_with_encoding(struct magic_set
*ms
, const unsigned char *buf
,
114 size_t nbytes
, unichar
*ubuf
, size_t ulen
, const char *code
,
117 unsigned char *utf8_buf
= NULL
, *utf8_end
;
119 const struct names
*p
;
121 int mime
= ms
->flags
& MAGIC_MIME
;
123 const char *subtype
= NULL
;
124 const char *subtype_mime
= NULL
;
127 int has_backspace
= 0;
135 size_t last_line_end
= (size_t)-1;
136 int has_long_lines
= 0;
138 if (ms
->flags
& MAGIC_APPLE
)
141 nbytes
= trim_nuls(buf
, nbytes
);
143 /* If we have fewer than 2 bytes, give up. */
149 /* Convert ubuf to UTF-8 and try text soft magic */
150 /* malloc size is a conservative overestimate; could be
151 improved, or at least realloced after conversion. */
153 if ((utf8_buf
= CAST(unsigned char *, malloc(mlen
))) == NULL
) {
154 file_oomem(ms
, mlen
);
157 if ((utf8_end
= encode_utf8(utf8_buf
, mlen
, ubuf
, ulen
)) == NULL
)
159 if ((rv
= file_softmagic(ms
, utf8_buf
, (size_t)(utf8_end
- utf8_buf
),
165 /* look for tokens from names.h - this is expensive! */
166 if ((ms
->flags
& MAGIC_NO_CHECK_TOKENS
) != 0)
167 goto subtype_identified
;
173 /* skip past any leading space */
174 while (i
< ulen
&& ISSPC(ubuf
[i
]))
179 /* find the next whitespace */
180 for (end
= i
+ 1; end
< nbytes
; end
++)
181 if (ISSPC(ubuf
[end
]))
184 /* compare the word thus isolated against the token list */
185 for (p
= names
; p
< names
+ NNAMES
; p
++) {
186 if (ascmatch((const unsigned char *)p
->name
, ubuf
+ i
,
188 subtype
= types
[p
->type
].human
;
189 subtype_mime
= types
[p
->type
].mime
;
190 goto subtype_identified
;
199 /* Now try to discover other details about the file. */
200 for (i
= 0; i
< ulen
; i
++) {
201 if (ubuf
[i
] == '\n') {
210 seen_cr
= (ubuf
[i
] == '\r');
214 if (ubuf
[i
] == 0x85) { /* X3.64/ECMA-43 "next line" character */
219 /* If this line is _longer_ than MAXLINELEN, remember it. */
220 if (i
> last_line_end
+ MAXLINELEN
)
223 if (ubuf
[i
] == '\033')
229 /* Beware, if the data has been truncated, the final CR could have
230 been followed by a LF. If we have HOWMANY bytes, it indicates
231 that the data might have been truncated, probably even before
232 this function was called. */
233 if (seen_cr
&& nbytes
< HOWMANY
)
236 if (strcmp(type
, "binary") == 0) {
241 if ((mime
& MAGIC_MIME_TYPE
) != 0) {
243 if (file_printf(ms
, "%s", subtype_mime
) == -1)
246 if (file_printf(ms
, "text/plain") == -1)
251 if (file_printf(ms
, "%s", code
) == -1)
255 if (file_printf(ms
, " %s", subtype
) == -1)
259 if (file_printf(ms
, " %s", type
) == -1)
263 if (file_printf(ms
, ", with very long lines") == -1)
267 * Only report line terminators if we find one other than LF,
268 * or if we find none at all.
270 if ((n_crlf
== 0 && n_cr
== 0 && n_nel
== 0 && n_lf
== 0) ||
271 (n_crlf
!= 0 || n_cr
!= 0 || n_nel
!= 0)) {
272 if (file_printf(ms
, ", with") == -1)
275 if (n_crlf
== 0 && n_cr
== 0 && n_nel
== 0 && n_lf
== 0) {
276 if (file_printf(ms
, " no") == -1)
280 if (file_printf(ms
, " CRLF") == -1)
282 if (n_cr
|| n_lf
|| n_nel
)
283 if (file_printf(ms
, ",") == -1)
287 if (file_printf(ms
, " CR") == -1)
290 if (file_printf(ms
, ",") == -1)
294 if (file_printf(ms
, " LF") == -1)
297 if (file_printf(ms
, ",") == -1)
301 if (file_printf(ms
, " NEL") == -1)
305 if (file_printf(ms
, " line terminators") == -1)
310 if (file_printf(ms
, ", with escape sequences") == -1)
313 if (file_printf(ms
, ", with overstriking") == -1)
325 ascmatch(const unsigned char *s
, const unichar
*us
, size_t ulen
)
329 for (i
= 0; i
< ulen
; i
++) {
341 * Encode Unicode string as UTF-8, returning pointer to character
342 * after end of string, or NULL if an invalid character is found.
344 private unsigned char *
345 encode_utf8(unsigned char *buf
, size_t len
, unichar
*ubuf
, size_t ulen
)
348 unsigned char *end
= buf
+ len
;
350 for (i
= 0; i
< ulen
; i
++) {
351 if (ubuf
[i
] <= 0x7f) {
354 *buf
++ = (unsigned char)ubuf
[i
];
355 } else if (ubuf
[i
] <= 0x7ff) {
358 *buf
++ = (unsigned char)((ubuf
[i
] >> 6) + 0xc0);
359 *buf
++ = (unsigned char)((ubuf
[i
] & 0x3f) + 0x80);
360 } else if (ubuf
[i
] <= 0xffff) {
363 *buf
++ = (unsigned char)((ubuf
[i
] >> 12) + 0xe0);
364 *buf
++ = (unsigned char)(((ubuf
[i
] >> 6) & 0x3f) + 0x80);
365 *buf
++ = (unsigned char)((ubuf
[i
] & 0x3f) + 0x80);
366 } else if (ubuf
[i
] <= 0x1fffff) {
369 *buf
++ = (unsigned char)((ubuf
[i
] >> 18) + 0xf0);
370 *buf
++ = (unsigned char)(((ubuf
[i
] >> 12) & 0x3f) + 0x80);
371 *buf
++ = (unsigned char)(((ubuf
[i
] >> 6) & 0x3f) + 0x80);
372 *buf
++ = (unsigned char)((ubuf
[i
] & 0x3f) + 0x80);
373 } else if (ubuf
[i
] <= 0x3ffffff) {
376 *buf
++ = (unsigned char)((ubuf
[i
] >> 24) + 0xf8);
377 *buf
++ = (unsigned char)(((ubuf
[i
] >> 18) & 0x3f) + 0x80);
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
] <= 0x7fffffff) {
384 *buf
++ = (unsigned char)((ubuf
[i
] >> 30) + 0xfc);
385 *buf
++ = (unsigned char)(((ubuf
[i
] >> 24) & 0x3f) + 0x80);
386 *buf
++ = (unsigned char)(((ubuf
[i
] >> 18) & 0x3f) + 0x80);
387 *buf
++ = (unsigned char)(((ubuf
[i
] >> 12) & 0x3f) + 0x80);
388 *buf
++ = (unsigned char)(((ubuf
[i
] >> 6) & 0x3f) + 0x80);
389 *buf
++ = (unsigned char)((ubuf
[i
] & 0x3f) + 0x80);
390 } else /* Invalid character */