#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: funcs.c,v 1.49 2008/11/06 22:49:08 rrt Exp $")
+FILE_RCSID("@(#)$File: funcs.c,v 1.50 2008/11/07 17:27:22 christos Exp $")
#endif /* lint */
#include "magic.h"
file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf,
size_t nb)
{
- int m = 0, rv = 0, looks_text = 0;
+ int m = 0, rv = 0;
int mime = ms->flags & MAGIC_MIME;
const unsigned char *ubuf = CAST(const unsigned char *, buf);
unichar *u8buf = NULL;
size_t ulen;
- const char *code = NULL;
- const char *code_mime = NULL;
- const char *type = NULL;
if (nb == 0) {
if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
#endif
/* try compression stuff */
- if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) != 0 ||
- (m = file_zmagic(ms, fd, inname, ubuf, nb)) == 0) {
- /* Check if we have a tar file */
- if ((ms->flags & MAGIC_NO_CHECK_TAR) != 0 ||
- (m = file_is_tar(ms, ubuf, nb)) == 0) {
- /* Check if we have a CDF file */
- if ((ms->flags & MAGIC_NO_CHECK_CDF) != 0 ||
- (m = file_trycdf(ms, fd, ubuf, nb)) == 0) {
- /* try to discover text encoding */
- if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0)
- looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen, &code, &code_mime, &type);
- /* try soft magic tests */
- if ((ms->flags & MAGIC_NO_CHECK_SOFT) != 0 ||
- (m = file_softmagic(ms, ubuf, nb, BINTEST)) == 0) {
- /* try text properties (and possibly text tokens) */
- if ((ms->flags & MAGIC_NO_CHECK_TEXT) != 0 ||
- ((ms->flags & MAGIC_NO_CHECK_ENCODING) != 0 && (m = file_ascmagic(ms, ubuf, nb)) == 0) ||
- looks_text == 0 || (m = file_ascmagic_with_encoding(ms, ubuf, nb, u8buf, ulen, code, code_mime, type)) == 0) {
- /* give up */
- if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
- file_printf(ms, mime ?
- "application/octet-stream" :
- "data") == -1) {
- rv = -1;
- goto done;
- }
+ if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0)
+ if ((m = file_zmagic(ms, fd, inname, ubuf, nb)) != 0) {
+ if ((ms->flags & MAGIC_DEBUG) != 0)
+ (void)fprintf(stderr, "zmagic %d\n", m);
+ goto done;
+ }
+
+ /* Check if we have a tar file */
+ if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0)
+ if ((m = file_is_tar(ms, ubuf, nb)) != 0) {
+ if ((ms->flags & MAGIC_DEBUG) != 0)
+ (void)fprintf(stderr, "tar %d\n", m);
+ goto done;
+ }
+
+ /* Check if we have a CDF file */
+ if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0)
+ if ((m = file_trycdf(ms, fd, ubuf, nb)) != 0) {
+ if ((ms->flags & MAGIC_DEBUG) != 0)
+ (void)fprintf(stderr, "cdf %d\n", m);
+ goto done;
+ }
+
+ /* try soft magic tests */
+ if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
+ if ((m = file_softmagic(ms, ubuf, nb, BINTEST)) != 0) {
+ if ((ms->flags & MAGIC_DEBUG) != 0)
+ (void)fprintf(stderr, "softmagic %d\n", m);
+#ifdef BUILTIN_ELF
+ if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
+ nb > 5 && fd != -1) {
+ /*
+ * We matched something in the file, so this
+ * *might* be an ELF file, and the file is at
+ * least 5 bytes long, so if it's an ELF file
+ * it has at least one byte past the ELF magic
+ * number - try extracting information from the
+ * ELF headers that cannot easily * be
+ * extracted with rules in the magic file.
+ */
+ if ((m = file_tryelf(ms, fd, ubuf, nb)) != 0)
+ if ((ms->flags & MAGIC_DEBUG) != 0)
+ (void)fprintf(stderr,
+ "elf %d\n", m);
}
- m = 1;
- }
+#endif
+ goto done;
+ }
+
+ /* try text properties (and possibly text tokens) */
+ if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
+
+ if ((m = file_ascmagic(ms, ubuf, nb)) != 0) {
+ if ((ms->flags & MAGIC_DEBUG) != 0)
+ (void)fprintf(stderr, "ascmagic %d\n", m);
+ goto done;
+ }
+
+ /* try to discover text encoding */
+ if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
+ const char *code = NULL;
+ const char *code_mime = NULL;
+ const char *type = NULL;
+ int looks_text = 0;
+
+ looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen,
+ &code, &code_mime, &type);
+ if (looks_text == 0)
+ if ((m = file_ascmagic_with_encoding( ms, ubuf,
+ nb, u8buf, ulen, code, code_mime, type))
+ != 0) {
+ if ((ms->flags & MAGIC_DEBUG) != 0)
+ (void)fprintf(stderr,
+ "ascmagic/enc %d\n", m);
+ goto done;
+ }
}
- }
+ }
+
+ /* give up */
+ m = 1;
+ if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
+ file_printf(ms, mime ? "application/octet-stream" : "data") == -1) {
+ rv = -1;
}
done:
if (u8buf)
if (rv)
return rv;
-#ifdef BUILTIN_ELF
- if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
- nb > 5 && fd != -1) {
- /*
- * We matched something in the file, so this *might*
- * be an ELF file, and the file is at least 5 bytes
- * long, so if it's an ELF file it has at least one
- * byte past the ELF magic number - try extracting
- * information from the ELF headers that cannot easily
- * be extracted with rules in the magic file.
- */
- (void)file_tryelf(ms, fd, ubuf, nb);
- }
-#endif
return m;
}
#endif