Imported Upstream version 5.45
[platform/upstream/python3-magic.git] / src / magic.c
index 03a456b..052f997 100644 (file)
@@ -33,7 +33,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: magic.c,v 1.115 2021/09/20 17:45:41 christos Exp $")
+FILE_RCSID("@(#)$File: magic.c,v 1.121 2023/02/09 17:45:19 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -69,12 +69,12 @@ FILE_RCSID("@(#)$File: magic.c,v 1.115 2021/09/20 17:45:41 christos Exp $")
 #endif
 #endif
 
-private void close_and_restore(const struct magic_set *, const char *, int,
+file_private void close_and_restore(const struct magic_set *, const char *, int,
     const struct stat *);
-private int unreadable_info(struct magic_set *, mode_t, const char *);
-private const char* get_default_magic(void);
+file_private int unreadable_info(struct magic_set *, mode_t, const char *);
+file_private const char* get_default_magic(void);
 #ifndef COMPILE_ONLY
-private const char *file_or_fd(struct magic_set *, const char *, int);
+file_private const char *file_or_fd(struct magic_set *, const char *, int);
 #endif
 
 #ifndef        STDIN_FILENO
@@ -171,7 +171,7 @@ DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
 #endif
 #endif
 
-private const char *
+file_private const char *
 get_default_magic(void)
 {
        static const char hmagic[] = "/.magic/magic.mgc";
@@ -221,6 +221,10 @@ out:
                default_magic = NULL;
        }
 
+       /* Before anything else, try to get a magic file from user HOME */
+       if ((home = getenv("HOME")) != NULL)
+               _w32_append_path(&hmagicpath, "%s%s", home, hmagic);
+
        /* First, try to get a magic file from user-application data */
        if ((home = getenv("LOCALAPPDATA")) != NULL)
                _w32_append_path(&hmagicpath, "%s%s", home, hmagic);
@@ -246,7 +250,7 @@ out:
 #endif
 }
 
-public const char *
+file_public const char *
 magic_getpath(const char *magicfile, int action)
 {
        if (magicfile != NULL)
@@ -259,13 +263,13 @@ magic_getpath(const char *magicfile, int action)
        return action == FILE_LOAD ? get_default_magic() : MAGIC;
 }
 
-public struct magic_set *
+file_public struct magic_set *
 magic_open(int flags)
 {
        return file_ms_alloc(flags);
 }
 
-private int
+file_private int
 unreadable_info(struct magic_set *ms, mode_t md, const char *file)
 {
        if (file) {
@@ -273,9 +277,22 @@ unreadable_info(struct magic_set *ms, mode_t md, const char *file)
                if (access(file, W_OK) == 0)
                        if (file_printf(ms, "writable, ") == -1)
                                return -1;
+#ifndef WIN32
                if (access(file, X_OK) == 0)
                        if (file_printf(ms, "executable, ") == -1)
                                return -1;
+#else
+               /* X_OK doesn't work well on MS-Windows */
+               {
+                       const char *p = strrchr(file, '.');
+                       if (p && (stricmp(p, ".exe")
+                                 || stricmp(p, ".dll")
+                                 || stricmp(p, ".bat")
+                                 || stricmp(p, ".cmd")))
+                               if (file_printf(ms, "writable, ") == -1)
+                                       return -1;
+               }
+#endif
        }
        if (S_ISREG(md))
                if (file_printf(ms, "regular file, ") == -1)
@@ -285,7 +302,7 @@ unreadable_info(struct magic_set *ms, mode_t md, const char *file)
        return 0;
 }
 
-public void
+file_public void
 magic_close(struct magic_set *ms)
 {
        if (ms == NULL)
@@ -296,7 +313,7 @@ magic_close(struct magic_set *ms)
 /*
  * load a magic file
  */
-public int
+file_public int
 magic_load(struct magic_set *ms, const char *magicfile)
 {
        if (ms == NULL)
@@ -308,7 +325,7 @@ magic_load(struct magic_set *ms, const char *magicfile)
 /*
  * Install a set of compiled magic buffers.
  */
-public int
+file_public int
 magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
     size_t nbufs)
 {
@@ -319,7 +336,7 @@ magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
 }
 #endif
 
-public int
+file_public int
 magic_compile(struct magic_set *ms, const char *magicfile)
 {
        if (ms == NULL)
@@ -327,7 +344,7 @@ magic_compile(struct magic_set *ms, const char *magicfile)
        return file_apprentice(ms, magicfile, FILE_COMPILE);
 }
 
-public int
+file_public int
 magic_check(struct magic_set *ms, const char *magicfile)
 {
        if (ms == NULL)
@@ -335,7 +352,7 @@ magic_check(struct magic_set *ms, const char *magicfile)
        return file_apprentice(ms, magicfile, FILE_CHECK);
 }
 
-public int
+file_public int
 magic_list(struct magic_set *ms, const char *magicfile)
 {
        if (ms == NULL)
@@ -343,7 +360,7 @@ magic_list(struct magic_set *ms, const char *magicfile)
        return file_apprentice(ms, magicfile, FILE_LIST);
 }
 
-private void
+file_private void
 close_and_restore(const struct magic_set *ms, const char *name, int fd,
     const struct stat *sb)
 {
@@ -381,7 +398,7 @@ close_and_restore(const struct magic_set *ms, const char *name, int fd,
 /*
  * find type of descriptor
  */
-public const char *
+file_public const char *
 magic_descriptor(struct magic_set *ms, int fd)
 {
        if (ms == NULL)
@@ -392,7 +409,7 @@ magic_descriptor(struct magic_set *ms, int fd)
 /*
  * find type of named file
  */
-public const char *
+file_public const char *
 magic_file(struct magic_set *ms, const char *inname)
 {
        if (ms == NULL)
@@ -400,7 +417,7 @@ magic_file(struct magic_set *ms, const char *inname)
        return file_or_fd(ms, inname, STDIN_FILENO);
 }
 
-private const char *
+file_private const char *
 file_or_fd(struct magic_set *ms, const char *inname, int fd)
 {
        int     rv = -1;
@@ -442,8 +459,6 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd)
                errno = 0;
                if ((fd = open(inname, flags)) < 0) {
                        okstat = stat(inname, &sb) == 0;
-                       if (okstat && S_ISFIFO(sb.st_mode))
-                               ispipe = 1;
 #ifdef WIN32
                        /*
                         * Can't stat, can't open.  It may have been opened in
@@ -462,7 +477,7 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd)
                        rv = 0;
                        goto done;
                }
-#if O_CLOEXEC == 0
+#if O_CLOEXEC == 0 && defined(F_SETFD)
                (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
 #endif
        }
@@ -500,7 +515,7 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd)
        } else if (fd != -1) {
                /* Windows refuses to read from a big console buffer. */
                size_t howmany =
-#if defined(WIN32)
+#ifdef WIN32
                    _isatty(fd) ? 8 * 1024 :
 #endif
                    ms->bytes_max;
@@ -530,7 +545,7 @@ out:
 }
 
 
-public const char *
+file_public const char *
 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
 {
        if (ms == NULL)
@@ -548,7 +563,7 @@ magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
 }
 #endif
 
-public const char *
+file_public const char *
 magic_error(struct magic_set *ms)
 {
        if (ms == NULL)
@@ -556,7 +571,7 @@ magic_error(struct magic_set *ms)
        return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
 }
 
-public int
+file_public int
 magic_errno(struct magic_set *ms)
 {
        if (ms == NULL)
@@ -564,7 +579,7 @@ magic_errno(struct magic_set *ms)
        return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
 }
 
-public int
+file_public int
 magic_getflags(struct magic_set *ms)
 {
        if (ms == NULL)
@@ -573,7 +588,7 @@ magic_getflags(struct magic_set *ms)
        return ms->flags;
 }
 
-public int
+file_public int
 magic_setflags(struct magic_set *ms, int flags)
 {
        if (ms == NULL)
@@ -586,13 +601,13 @@ magic_setflags(struct magic_set *ms, int flags)
        return 0;
 }
 
-public int
+file_public int
 magic_version(void)
 {
        return MAGIC_VERSION;
 }
 
-public int
+file_public int
 magic_setparam(struct magic_set *ms, int param, const void *val)
 {
        if (ms == NULL)
@@ -610,6 +625,9 @@ magic_setparam(struct magic_set *ms, int param, const void *val)
        case MAGIC_PARAM_ELF_SHNUM_MAX:
                ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val));
                return 0;
+       case MAGIC_PARAM_ELF_SHSIZE_MAX:
+               ms->elf_shsize_max = *CAST(const size_t *, val);
+               return 0;
        case MAGIC_PARAM_ELF_NOTES_MAX:
                ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val));
                return 0;
@@ -628,7 +646,7 @@ magic_setparam(struct magic_set *ms, int param, const void *val)
        }
 }
 
-public int
+file_public int
 magic_getparam(struct magic_set *ms, int param, void *val)
 {
        if (ms == NULL)
@@ -646,6 +664,9 @@ magic_getparam(struct magic_set *ms, int param, void *val)
        case MAGIC_PARAM_ELF_SHNUM_MAX:
                *CAST(size_t *, val) = ms->elf_shnum_max;
                return 0;
+       case MAGIC_PARAM_ELF_SHSIZE_MAX:
+               *CAST(size_t *, val) = ms->elf_shsize_max;
+               return 0;
        case MAGIC_PARAM_ELF_NOTES_MAX:
                *CAST(size_t *, val) = ms->elf_notes_max;
                return 0;