1b6cee71dSXin LI /* 2b6cee71dSXin LI * Copyright (c) Christos Zoulas 2003. 3b6cee71dSXin LI * All Rights Reserved. 4b6cee71dSXin LI * 5b6cee71dSXin LI * Redistribution and use in source and binary forms, with or without 6b6cee71dSXin LI * modification, are permitted provided that the following conditions 7b6cee71dSXin LI * are met: 8b6cee71dSXin LI * 1. Redistributions of source code must retain the above copyright 9b6cee71dSXin LI * notice immediately at the beginning of the file, without modification, 10b6cee71dSXin LI * this list of conditions, and the following disclaimer. 11b6cee71dSXin LI * 2. Redistributions in binary form must reproduce the above copyright 12b6cee71dSXin LI * notice, this list of conditions and the following disclaimer in the 13b6cee71dSXin LI * documentation and/or other materials provided with the distribution. 14b6cee71dSXin LI * 15b6cee71dSXin LI * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16b6cee71dSXin LI * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17b6cee71dSXin LI * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18b6cee71dSXin LI * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19b6cee71dSXin LI * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20b6cee71dSXin LI * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21b6cee71dSXin LI * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22b6cee71dSXin LI * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23b6cee71dSXin LI * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24b6cee71dSXin LI * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25b6cee71dSXin LI * SUCH DAMAGE. 26b6cee71dSXin LI */ 27b6cee71dSXin LI 28b6cee71dSXin LI #ifdef WIN32 29b6cee71dSXin LI #include <windows.h> 30b6cee71dSXin LI #include <shlwapi.h> 31b6cee71dSXin LI #endif 32b6cee71dSXin LI 33b6cee71dSXin LI #include "file.h" 34b6cee71dSXin LI 35b6cee71dSXin LI #ifndef lint 36*4460e5b0SXin LI FILE_RCSID("@(#)$File: magic.c,v 1.91 2014/12/16 23:18:40 christos Exp $") 37b6cee71dSXin LI #endif /* lint */ 38b6cee71dSXin LI 39b6cee71dSXin LI #include "magic.h" 40b6cee71dSXin LI 41b6cee71dSXin LI #include <stdlib.h> 42b6cee71dSXin LI #include <unistd.h> 43b6cee71dSXin LI #include <string.h> 44b6cee71dSXin LI #ifdef QUICK 45b6cee71dSXin LI #include <sys/mman.h> 46b6cee71dSXin LI #endif 47b6cee71dSXin LI #ifdef HAVE_LIMITS_H 48b6cee71dSXin LI #include <limits.h> /* for PIPE_BUF */ 49b6cee71dSXin LI #endif 50b6cee71dSXin LI 51b6cee71dSXin LI #if defined(HAVE_UTIMES) 52b6cee71dSXin LI # include <sys/time.h> 53b6cee71dSXin LI #elif defined(HAVE_UTIME) 54b6cee71dSXin LI # if defined(HAVE_SYS_UTIME_H) 55b6cee71dSXin LI # include <sys/utime.h> 56b6cee71dSXin LI # elif defined(HAVE_UTIME_H) 57b6cee71dSXin LI # include <utime.h> 58b6cee71dSXin LI # endif 59b6cee71dSXin LI #endif 60b6cee71dSXin LI 61b6cee71dSXin LI #ifdef HAVE_UNISTD_H 62b6cee71dSXin LI #include <unistd.h> /* for read() */ 63b6cee71dSXin LI #endif 64b6cee71dSXin LI 65b6cee71dSXin LI #ifndef PIPE_BUF 66b6cee71dSXin LI /* Get the PIPE_BUF from pathconf */ 67b6cee71dSXin LI #ifdef _PC_PIPE_BUF 68b6cee71dSXin LI #define PIPE_BUF pathconf(".", _PC_PIPE_BUF) 69b6cee71dSXin LI #else 70b6cee71dSXin LI #define PIPE_BUF 512 71b6cee71dSXin LI #endif 72b6cee71dSXin LI #endif 73b6cee71dSXin LI 74b6cee71dSXin LI private void close_and_restore(const struct magic_set *, const char *, int, 75b6cee71dSXin LI const struct stat *); 76b6cee71dSXin LI private int unreadable_info(struct magic_set *, mode_t, const char *); 77b6cee71dSXin LI private const char* get_default_magic(void); 78b6cee71dSXin LI #ifndef COMPILE_ONLY 79b6cee71dSXin LI private const char *file_or_fd(struct magic_set *, const char *, int); 80b6cee71dSXin LI #endif 81b6cee71dSXin LI 82b6cee71dSXin LI #ifndef STDIN_FILENO 83b6cee71dSXin LI #define STDIN_FILENO 0 84b6cee71dSXin LI #endif 85b6cee71dSXin LI 86b6cee71dSXin LI private const char * 87b6cee71dSXin LI get_default_magic(void) 88b6cee71dSXin LI { 89b6cee71dSXin LI static const char hmagic[] = "/.magic/magic.mgc"; 90b6cee71dSXin LI static char *default_magic; 91b6cee71dSXin LI char *home, *hmagicpath; 92b6cee71dSXin LI 93b6cee71dSXin LI #ifndef WIN32 94b6cee71dSXin LI struct stat st; 95b6cee71dSXin LI 96b6cee71dSXin LI if (default_magic) { 97b6cee71dSXin LI free(default_magic); 98b6cee71dSXin LI default_magic = NULL; 99b6cee71dSXin LI } 100b6cee71dSXin LI if ((home = getenv("HOME")) == NULL) 101b6cee71dSXin LI return MAGIC; 102b6cee71dSXin LI 103b6cee71dSXin LI if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0) 104b6cee71dSXin LI return MAGIC; 105b6cee71dSXin LI if (stat(hmagicpath, &st) == -1) { 106b6cee71dSXin LI free(hmagicpath); 107b6cee71dSXin LI if (asprintf(&hmagicpath, "%s/.magic", home) < 0) 108b6cee71dSXin LI return MAGIC; 109b6cee71dSXin LI if (stat(hmagicpath, &st) == -1) 110b6cee71dSXin LI goto out; 111b6cee71dSXin LI if (S_ISDIR(st.st_mode)) { 112b6cee71dSXin LI free(hmagicpath); 113b6cee71dSXin LI if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0) 114b6cee71dSXin LI return MAGIC; 115b6cee71dSXin LI if (access(hmagicpath, R_OK) == -1) 116b6cee71dSXin LI goto out; 117b6cee71dSXin LI } 118b6cee71dSXin LI } 119b6cee71dSXin LI 120b6cee71dSXin LI if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0) 121b6cee71dSXin LI goto out; 122b6cee71dSXin LI free(hmagicpath); 123b6cee71dSXin LI return default_magic; 124b6cee71dSXin LI out: 125b6cee71dSXin LI default_magic = NULL; 126b6cee71dSXin LI free(hmagicpath); 127b6cee71dSXin LI return MAGIC; 128b6cee71dSXin LI #else 129b6cee71dSXin LI char *hmagicp; 130b6cee71dSXin LI char *tmppath = NULL; 131c2931133SXin LI LPTSTR dllpath; 132b6cee71dSXin LI hmagicpath = NULL; 133b6cee71dSXin LI 134b6cee71dSXin LI #define APPENDPATH() \ 135b6cee71dSXin LI do { \ 136b6cee71dSXin LI if (tmppath && access(tmppath, R_OK) != -1) { \ 137b6cee71dSXin LI if (hmagicpath == NULL) \ 138b6cee71dSXin LI hmagicpath = tmppath; \ 139b6cee71dSXin LI else { \ 140b6cee71dSXin LI if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \ 141b6cee71dSXin LI PATHSEP, tmppath) >= 0) { \ 142b6cee71dSXin LI free(hmagicpath); \ 143b6cee71dSXin LI hmagicpath = hmagicp; \ 144b6cee71dSXin LI } \ 145b6cee71dSXin LI free(tmppath); \ 146b6cee71dSXin LI } \ 147b6cee71dSXin LI tmppath = NULL; \ 148b6cee71dSXin LI } \ 149b6cee71dSXin LI } while (/*CONSTCOND*/0) 150b6cee71dSXin LI 151b6cee71dSXin LI if (default_magic) { 152b6cee71dSXin LI free(default_magic); 153b6cee71dSXin LI default_magic = NULL; 154b6cee71dSXin LI } 155b6cee71dSXin LI 156b6cee71dSXin LI /* First, try to get user-specific magic file */ 157b6cee71dSXin LI if ((home = getenv("LOCALAPPDATA")) == NULL) { 158b6cee71dSXin LI if ((home = getenv("USERPROFILE")) != NULL) 159b6cee71dSXin LI if (asprintf(&tmppath, 160b6cee71dSXin LI "%s/Local Settings/Application Data%s", home, 161b6cee71dSXin LI hmagic) < 0) 162b6cee71dSXin LI tmppath = NULL; 163b6cee71dSXin LI } else { 164b6cee71dSXin LI if (asprintf(&tmppath, "%s%s", home, hmagic) < 0) 165b6cee71dSXin LI tmppath = NULL; 166b6cee71dSXin LI } 167b6cee71dSXin LI 168b6cee71dSXin LI APPENDPATH(); 169b6cee71dSXin LI 170b6cee71dSXin LI /* Second, try to get a magic file from Common Files */ 171b6cee71dSXin LI if ((home = getenv("COMMONPROGRAMFILES")) != NULL) { 172b6cee71dSXin LI if (asprintf(&tmppath, "%s%s", home, hmagic) >= 0) 173b6cee71dSXin LI APPENDPATH(); 174b6cee71dSXin LI } 175b6cee71dSXin LI 176b6cee71dSXin LI /* Third, try to get magic file relative to dll location */ 177c2931133SXin LI dllpath = malloc(sizeof(*dllpath) * (MAX_PATH + 1)); 178b6cee71dSXin LI dllpath[MAX_PATH] = 0; /* just in case long path gets truncated and not null terminated */ 179b6cee71dSXin LI if (GetModuleFileNameA(NULL, dllpath, MAX_PATH)){ 180b6cee71dSXin LI PathRemoveFileSpecA(dllpath); 181b6cee71dSXin LI if (strlen(dllpath) > 3 && 182b6cee71dSXin LI stricmp(&dllpath[strlen(dllpath) - 3], "bin") == 0) { 183b6cee71dSXin LI if (asprintf(&tmppath, 184b6cee71dSXin LI "%s/../share/misc/magic.mgc", dllpath) >= 0) 185b6cee71dSXin LI APPENDPATH(); 186b6cee71dSXin LI } else { 187b6cee71dSXin LI if (asprintf(&tmppath, 188b6cee71dSXin LI "%s/share/misc/magic.mgc", dllpath) >= 0) 189b6cee71dSXin LI APPENDPATH(); 190b6cee71dSXin LI else if (asprintf(&tmppath, 191b6cee71dSXin LI "%s/magic.mgc", dllpath) >= 0) 192b6cee71dSXin LI APPENDPATH(); 193b6cee71dSXin LI } 194b6cee71dSXin LI } 195b6cee71dSXin LI 196b6cee71dSXin LI /* Don't put MAGIC constant - it likely points to a file within MSys 197b6cee71dSXin LI tree */ 198b6cee71dSXin LI default_magic = hmagicpath; 199b6cee71dSXin LI return default_magic; 200b6cee71dSXin LI #endif 201b6cee71dSXin LI } 202b6cee71dSXin LI 203b6cee71dSXin LI public const char * 204b6cee71dSXin LI magic_getpath(const char *magicfile, int action) 205b6cee71dSXin LI { 206b6cee71dSXin LI if (magicfile != NULL) 207b6cee71dSXin LI return magicfile; 208b6cee71dSXin LI 209b6cee71dSXin LI magicfile = getenv("MAGIC"); 210b6cee71dSXin LI if (magicfile != NULL) 211b6cee71dSXin LI return magicfile; 212b6cee71dSXin LI 213b6cee71dSXin LI return action == FILE_LOAD ? get_default_magic() : MAGIC; 214b6cee71dSXin LI } 215b6cee71dSXin LI 216b6cee71dSXin LI public struct magic_set * 217b6cee71dSXin LI magic_open(int flags) 218b6cee71dSXin LI { 219b6cee71dSXin LI return file_ms_alloc(flags); 220b6cee71dSXin LI } 221b6cee71dSXin LI 222b6cee71dSXin LI private int 223b6cee71dSXin LI unreadable_info(struct magic_set *ms, mode_t md, const char *file) 224b6cee71dSXin LI { 225b6cee71dSXin LI if (file) { 226b6cee71dSXin LI /* We cannot open it, but we were able to stat it. */ 227b6cee71dSXin LI if (access(file, W_OK) == 0) 228b6cee71dSXin LI if (file_printf(ms, "writable, ") == -1) 229b6cee71dSXin LI return -1; 230b6cee71dSXin LI if (access(file, X_OK) == 0) 231b6cee71dSXin LI if (file_printf(ms, "executable, ") == -1) 232b6cee71dSXin LI return -1; 233b6cee71dSXin LI } 234b6cee71dSXin LI if (S_ISREG(md)) 235b6cee71dSXin LI if (file_printf(ms, "regular file, ") == -1) 236b6cee71dSXin LI return -1; 237b6cee71dSXin LI if (file_printf(ms, "no read permission") == -1) 238b6cee71dSXin LI return -1; 239b6cee71dSXin LI return 0; 240b6cee71dSXin LI } 241b6cee71dSXin LI 242b6cee71dSXin LI public void 243b6cee71dSXin LI magic_close(struct magic_set *ms) 244b6cee71dSXin LI { 245b6cee71dSXin LI if (ms == NULL) 246b6cee71dSXin LI return; 247b6cee71dSXin LI file_ms_free(ms); 248b6cee71dSXin LI } 249b6cee71dSXin LI 250b6cee71dSXin LI /* 251b6cee71dSXin LI * load a magic file 252b6cee71dSXin LI */ 253b6cee71dSXin LI public int 254b6cee71dSXin LI magic_load(struct magic_set *ms, const char *magicfile) 255b6cee71dSXin LI { 256b6cee71dSXin LI if (ms == NULL) 257b6cee71dSXin LI return -1; 258b6cee71dSXin LI return file_apprentice(ms, magicfile, FILE_LOAD); 259b6cee71dSXin LI } 260b6cee71dSXin LI 261c2931133SXin LI #ifndef COMPILE_ONLY 262c2931133SXin LI /* 263c2931133SXin LI * Install a set of compiled magic buffers. 264c2931133SXin LI */ 265c2931133SXin LI public int 266c2931133SXin LI magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes, 267c2931133SXin LI size_t nbufs) 268c2931133SXin LI { 269c2931133SXin LI if (ms == NULL) 270c2931133SXin LI return -1; 271c2931133SXin LI return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs); 272c2931133SXin LI } 273c2931133SXin LI #endif 274c2931133SXin LI 275b6cee71dSXin LI public int 276b6cee71dSXin LI magic_compile(struct magic_set *ms, const char *magicfile) 277b6cee71dSXin LI { 278b6cee71dSXin LI if (ms == NULL) 279b6cee71dSXin LI return -1; 280b6cee71dSXin LI return file_apprentice(ms, magicfile, FILE_COMPILE); 281b6cee71dSXin LI } 282b6cee71dSXin LI 283b6cee71dSXin LI public int 284b6cee71dSXin LI magic_check(struct magic_set *ms, const char *magicfile) 285b6cee71dSXin LI { 286b6cee71dSXin LI if (ms == NULL) 287b6cee71dSXin LI return -1; 288b6cee71dSXin LI return file_apprentice(ms, magicfile, FILE_CHECK); 289b6cee71dSXin LI } 290b6cee71dSXin LI 291b6cee71dSXin LI public int 292b6cee71dSXin LI magic_list(struct magic_set *ms, const char *magicfile) 293b6cee71dSXin LI { 294b6cee71dSXin LI if (ms == NULL) 295b6cee71dSXin LI return -1; 296b6cee71dSXin LI return file_apprentice(ms, magicfile, FILE_LIST); 297b6cee71dSXin LI } 298b6cee71dSXin LI 299b6cee71dSXin LI private void 300b6cee71dSXin LI close_and_restore(const struct magic_set *ms, const char *name, int fd, 301b6cee71dSXin LI const struct stat *sb) 302b6cee71dSXin LI { 303b6cee71dSXin LI if (fd == STDIN_FILENO || name == NULL) 304b6cee71dSXin LI return; 305b6cee71dSXin LI (void) close(fd); 306b6cee71dSXin LI 307b6cee71dSXin LI if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) { 308b6cee71dSXin LI /* 309b6cee71dSXin LI * Try to restore access, modification times if read it. 310b6cee71dSXin LI * This is really *bad* because it will modify the status 311b6cee71dSXin LI * time of the file... And of course this will affect 312b6cee71dSXin LI * backup programs 313b6cee71dSXin LI */ 314b6cee71dSXin LI #ifdef HAVE_UTIMES 315b6cee71dSXin LI struct timeval utsbuf[2]; 316b6cee71dSXin LI (void)memset(utsbuf, 0, sizeof(utsbuf)); 317b6cee71dSXin LI utsbuf[0].tv_sec = sb->st_atime; 318b6cee71dSXin LI utsbuf[1].tv_sec = sb->st_mtime; 319b6cee71dSXin LI 320b6cee71dSXin LI (void) utimes(name, utsbuf); /* don't care if loses */ 321b6cee71dSXin LI #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H) 322b6cee71dSXin LI struct utimbuf utbuf; 323b6cee71dSXin LI 324b6cee71dSXin LI (void)memset(&utbuf, 0, sizeof(utbuf)); 325b6cee71dSXin LI utbuf.actime = sb->st_atime; 326b6cee71dSXin LI utbuf.modtime = sb->st_mtime; 327b6cee71dSXin LI (void) utime(name, &utbuf); /* don't care if loses */ 328b6cee71dSXin LI #endif 329b6cee71dSXin LI } 330b6cee71dSXin LI } 331b6cee71dSXin LI 332b6cee71dSXin LI #ifndef COMPILE_ONLY 333b6cee71dSXin LI 334b6cee71dSXin LI /* 335b6cee71dSXin LI * find type of descriptor 336b6cee71dSXin LI */ 337b6cee71dSXin LI public const char * 338b6cee71dSXin LI magic_descriptor(struct magic_set *ms, int fd) 339b6cee71dSXin LI { 340b6cee71dSXin LI if (ms == NULL) 341b6cee71dSXin LI return NULL; 342b6cee71dSXin LI return file_or_fd(ms, NULL, fd); 343b6cee71dSXin LI } 344b6cee71dSXin LI 345b6cee71dSXin LI /* 346b6cee71dSXin LI * find type of named file 347b6cee71dSXin LI */ 348b6cee71dSXin LI public const char * 349b6cee71dSXin LI magic_file(struct magic_set *ms, const char *inname) 350b6cee71dSXin LI { 351b6cee71dSXin LI if (ms == NULL) 352b6cee71dSXin LI return NULL; 353b6cee71dSXin LI return file_or_fd(ms, inname, STDIN_FILENO); 354b6cee71dSXin LI } 355b6cee71dSXin LI 356b6cee71dSXin LI private const char * 357b6cee71dSXin LI file_or_fd(struct magic_set *ms, const char *inname, int fd) 358b6cee71dSXin LI { 359b6cee71dSXin LI int rv = -1; 360b6cee71dSXin LI unsigned char *buf; 361b6cee71dSXin LI struct stat sb; 362b6cee71dSXin LI ssize_t nbytes = 0; /* number of bytes read from a datafile */ 363b6cee71dSXin LI int ispipe = 0; 364b6cee71dSXin LI off_t pos = (off_t)-1; 365b6cee71dSXin LI 366b6cee71dSXin LI if (file_reset(ms) == -1) 367b6cee71dSXin LI goto out; 368b6cee71dSXin LI 369b6cee71dSXin LI /* 370b6cee71dSXin LI * one extra for terminating '\0', and 371b6cee71dSXin LI * some overlapping space for matches near EOF 372b6cee71dSXin LI */ 373b6cee71dSXin LI #define SLOP (1 + sizeof(union VALUETYPE)) 374b6cee71dSXin LI if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL) 375b6cee71dSXin LI return NULL; 376b6cee71dSXin LI 377b6cee71dSXin LI switch (file_fsmagic(ms, inname, &sb)) { 378b6cee71dSXin LI case -1: /* error */ 379b6cee71dSXin LI goto done; 380b6cee71dSXin LI case 0: /* nothing found */ 381b6cee71dSXin LI break; 382b6cee71dSXin LI default: /* matched it and printed type */ 383b6cee71dSXin LI rv = 0; 384b6cee71dSXin LI goto done; 385b6cee71dSXin LI } 386b6cee71dSXin LI 387b6cee71dSXin LI #ifdef WIN32 388b6cee71dSXin LI /* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */ 389b6cee71dSXin LI if (fd == STDIN_FILENO) 390b6cee71dSXin LI _setmode(STDIN_FILENO, O_BINARY); 391b6cee71dSXin LI #endif 392b6cee71dSXin LI 393b6cee71dSXin LI if (inname == NULL) { 394b6cee71dSXin LI if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode)) 395b6cee71dSXin LI ispipe = 1; 396b6cee71dSXin LI else 397b6cee71dSXin LI pos = lseek(fd, (off_t)0, SEEK_CUR); 398b6cee71dSXin LI } else { 399b6cee71dSXin LI int flags = O_RDONLY|O_BINARY; 400b6cee71dSXin LI int okstat = stat(inname, &sb) == 0; 401b6cee71dSXin LI 402b6cee71dSXin LI if (okstat && S_ISFIFO(sb.st_mode)) { 403b6cee71dSXin LI #ifdef O_NONBLOCK 404b6cee71dSXin LI flags |= O_NONBLOCK; 405b6cee71dSXin LI #endif 406b6cee71dSXin LI ispipe = 1; 407b6cee71dSXin LI } 408b6cee71dSXin LI 409b6cee71dSXin LI errno = 0; 410b6cee71dSXin LI if ((fd = open(inname, flags)) < 0) { 411b6cee71dSXin LI #ifdef WIN32 412b6cee71dSXin LI /* 413b6cee71dSXin LI * Can't stat, can't open. It may have been opened in 414b6cee71dSXin LI * fsmagic, so if the user doesn't have read permission, 415b6cee71dSXin LI * allow it to say so; otherwise an error was probably 416b6cee71dSXin LI * displayed in fsmagic. 417b6cee71dSXin LI */ 418b6cee71dSXin LI if (!okstat && errno == EACCES) { 419b6cee71dSXin LI sb.st_mode = S_IFBLK; 420b6cee71dSXin LI okstat = 1; 421b6cee71dSXin LI } 422b6cee71dSXin LI #endif 423b6cee71dSXin LI if (okstat && 424b6cee71dSXin LI unreadable_info(ms, sb.st_mode, inname) == -1) 425b6cee71dSXin LI goto done; 426b6cee71dSXin LI rv = 0; 427b6cee71dSXin LI goto done; 428b6cee71dSXin LI } 429b6cee71dSXin LI #ifdef O_NONBLOCK 430b6cee71dSXin LI if ((flags = fcntl(fd, F_GETFL)) != -1) { 431b6cee71dSXin LI flags &= ~O_NONBLOCK; 432b6cee71dSXin LI (void)fcntl(fd, F_SETFL, flags); 433b6cee71dSXin LI } 434b6cee71dSXin LI #endif 435b6cee71dSXin LI } 436b6cee71dSXin LI 437b6cee71dSXin LI /* 438b6cee71dSXin LI * try looking at the first HOWMANY bytes 439b6cee71dSXin LI */ 440b6cee71dSXin LI if (ispipe) { 441b6cee71dSXin LI ssize_t r = 0; 442b6cee71dSXin LI 443b6cee71dSXin LI while ((r = sread(fd, (void *)&buf[nbytes], 444b6cee71dSXin LI (size_t)(HOWMANY - nbytes), 1)) > 0) { 445b6cee71dSXin LI nbytes += r; 446b6cee71dSXin LI if (r < PIPE_BUF) break; 447b6cee71dSXin LI } 448b6cee71dSXin LI 449b6cee71dSXin LI if (nbytes == 0) { 450b6cee71dSXin LI /* We can not read it, but we were able to stat it. */ 451b6cee71dSXin LI if (unreadable_info(ms, sb.st_mode, inname) == -1) 452b6cee71dSXin LI goto done; 453b6cee71dSXin LI rv = 0; 454b6cee71dSXin LI goto done; 455b6cee71dSXin LI } 456b6cee71dSXin LI 457b6cee71dSXin LI } else { 458b6cee71dSXin LI /* Windows refuses to read from a big console buffer. */ 459b6cee71dSXin LI size_t howmany = 460b6cee71dSXin LI #if defined(WIN32) && HOWMANY > 8 * 1024 461b6cee71dSXin LI _isatty(fd) ? 8 * 1024 : 462b6cee71dSXin LI #endif 463b6cee71dSXin LI HOWMANY; 464b6cee71dSXin LI if ((nbytes = read(fd, (char *)buf, howmany)) == -1) { 465b6cee71dSXin LI if (inname == NULL && fd != STDIN_FILENO) 466b6cee71dSXin LI file_error(ms, errno, "cannot read fd %d", fd); 467b6cee71dSXin LI else 468b6cee71dSXin LI file_error(ms, errno, "cannot read `%s'", 469b6cee71dSXin LI inname == NULL ? "/dev/stdin" : inname); 470b6cee71dSXin LI goto done; 471b6cee71dSXin LI } 472b6cee71dSXin LI } 473b6cee71dSXin LI 474b6cee71dSXin LI (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */ 475b6cee71dSXin LI if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1) 476b6cee71dSXin LI goto done; 477b6cee71dSXin LI rv = 0; 478b6cee71dSXin LI done: 479b6cee71dSXin LI free(buf); 480b6cee71dSXin LI if (pos != (off_t)-1) 481b6cee71dSXin LI (void)lseek(fd, pos, SEEK_SET); 482b6cee71dSXin LI close_and_restore(ms, inname, fd, &sb); 483b6cee71dSXin LI out: 484b6cee71dSXin LI return rv == 0 ? file_getbuffer(ms) : NULL; 485b6cee71dSXin LI } 486b6cee71dSXin LI 487b6cee71dSXin LI 488b6cee71dSXin LI public const char * 489b6cee71dSXin LI magic_buffer(struct magic_set *ms, const void *buf, size_t nb) 490b6cee71dSXin LI { 491b6cee71dSXin LI if (ms == NULL) 492b6cee71dSXin LI return NULL; 493b6cee71dSXin LI if (file_reset(ms) == -1) 494b6cee71dSXin LI return NULL; 495b6cee71dSXin LI /* 496b6cee71dSXin LI * The main work is done here! 497b6cee71dSXin LI * We have the file name and/or the data buffer to be identified. 498b6cee71dSXin LI */ 499b6cee71dSXin LI if (file_buffer(ms, -1, NULL, buf, nb) == -1) { 500b6cee71dSXin LI return NULL; 501b6cee71dSXin LI } 502b6cee71dSXin LI return file_getbuffer(ms); 503b6cee71dSXin LI } 504b6cee71dSXin LI #endif 505b6cee71dSXin LI 506b6cee71dSXin LI public const char * 507b6cee71dSXin LI magic_error(struct magic_set *ms) 508b6cee71dSXin LI { 509b6cee71dSXin LI if (ms == NULL) 510b6cee71dSXin LI return "Magic database is not open"; 511b6cee71dSXin LI return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL; 512b6cee71dSXin LI } 513b6cee71dSXin LI 514b6cee71dSXin LI public int 515b6cee71dSXin LI magic_errno(struct magic_set *ms) 516b6cee71dSXin LI { 517b6cee71dSXin LI if (ms == NULL) 518b6cee71dSXin LI return EINVAL; 519b6cee71dSXin LI return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0; 520b6cee71dSXin LI } 521b6cee71dSXin LI 522b6cee71dSXin LI public int 523b6cee71dSXin LI magic_setflags(struct magic_set *ms, int flags) 524b6cee71dSXin LI { 525b6cee71dSXin LI if (ms == NULL) 526b6cee71dSXin LI return -1; 527b6cee71dSXin LI #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES) 528b6cee71dSXin LI if (flags & MAGIC_PRESERVE_ATIME) 529b6cee71dSXin LI return -1; 530b6cee71dSXin LI #endif 531b6cee71dSXin LI ms->flags = flags; 532b6cee71dSXin LI return 0; 533b6cee71dSXin LI } 534b6cee71dSXin LI 535b6cee71dSXin LI public int 536b6cee71dSXin LI magic_version(void) 537b6cee71dSXin LI { 538b6cee71dSXin LI return MAGIC_VERSION; 539b6cee71dSXin LI } 540c2931133SXin LI 541c2931133SXin LI public int 542c2931133SXin LI magic_setparam(struct magic_set *ms, int param, const void *val) 543c2931133SXin LI { 544c2931133SXin LI switch (param) { 545c2931133SXin LI case MAGIC_PARAM_INDIR_MAX: 546c2931133SXin LI ms->indir_max = *(const size_t *)val; 547c2931133SXin LI return 0; 548c2931133SXin LI case MAGIC_PARAM_NAME_MAX: 549c2931133SXin LI ms->name_max = *(const size_t *)val; 550c2931133SXin LI return 0; 551c2931133SXin LI case MAGIC_PARAM_ELF_PHNUM_MAX: 552c2931133SXin LI ms->elf_phnum_max = *(const size_t *)val; 553c2931133SXin LI return 0; 554c2931133SXin LI case MAGIC_PARAM_ELF_SHNUM_MAX: 555c2931133SXin LI ms->elf_shnum_max = *(const size_t *)val; 556c2931133SXin LI return 0; 557*4460e5b0SXin LI case MAGIC_PARAM_ELF_NOTES_MAX: 558*4460e5b0SXin LI ms->elf_notes_max = *(const size_t *)val; 559*4460e5b0SXin LI return 0; 560c2931133SXin LI default: 561c2931133SXin LI errno = EINVAL; 562c2931133SXin LI return -1; 563c2931133SXin LI } 564c2931133SXin LI } 565c2931133SXin LI 566c2931133SXin LI public int 567c2931133SXin LI magic_getparam(struct magic_set *ms, int param, void *val) 568c2931133SXin LI { 569c2931133SXin LI switch (param) { 570c2931133SXin LI case MAGIC_PARAM_INDIR_MAX: 571c2931133SXin LI *(size_t *)val = ms->indir_max; 572c2931133SXin LI return 0; 573c2931133SXin LI case MAGIC_PARAM_NAME_MAX: 574c2931133SXin LI *(size_t *)val = ms->name_max; 575c2931133SXin LI return 0; 576c2931133SXin LI case MAGIC_PARAM_ELF_PHNUM_MAX: 577c2931133SXin LI *(size_t *)val = ms->elf_phnum_max; 578c2931133SXin LI return 0; 579c2931133SXin LI case MAGIC_PARAM_ELF_SHNUM_MAX: 580c2931133SXin LI *(size_t *)val = ms->elf_shnum_max; 581c2931133SXin LI return 0; 582*4460e5b0SXin LI case MAGIC_PARAM_ELF_NOTES_MAX: 583*4460e5b0SXin LI *(size_t *)val = ms->elf_notes_max; 584*4460e5b0SXin LI return 0; 585c2931133SXin LI default: 586c2931133SXin LI errno = EINVAL; 587c2931133SXin LI return -1; 588c2931133SXin LI } 589c2931133SXin LI } 590