1133359Sobrien/* 2133359Sobrien * Copyright (c) Christos Zoulas 2003. 3133359Sobrien * All Rights Reserved. 4186690Sobrien * 5133359Sobrien * Redistribution and use in source and binary forms, with or without 6133359Sobrien * modification, are permitted provided that the following conditions 7133359Sobrien * are met: 8133359Sobrien * 1. Redistributions of source code must retain the above copyright 9133359Sobrien * notice immediately at the beginning of the file, without modification, 10133359Sobrien * this list of conditions, and the following disclaimer. 11133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright 12133359Sobrien * notice, this list of conditions and the following disclaimer in the 13133359Sobrien * documentation and/or other materials provided with the distribution. 14186690Sobrien * 15133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18133359Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19133359Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25133359Sobrien * SUCH DAMAGE. 26133359Sobrien */ 27133359Sobrien 28226048Sobrien#ifdef WIN32 29226048Sobrien#include <windows.h> 30226048Sobrien#include <shlwapi.h> 31226048Sobrien#endif 32226048Sobrien 33133359Sobrien#include "file.h" 34191736Sobrien 35191736Sobrien#ifndef lint 36362844SdelphijFILE_RCSID("@(#)$File: magic.c,v 1.112 2020/06/08 19:44:10 christos Exp $") 37191736Sobrien#endif /* lint */ 38191736Sobrien 39133359Sobrien#include "magic.h" 40133359Sobrien 41133359Sobrien#include <stdlib.h> 42133359Sobrien#include <unistd.h> 43133359Sobrien#include <string.h> 44133359Sobrien#ifdef QUICK 45133359Sobrien#include <sys/mman.h> 46133359Sobrien#endif 47169942Sobrien#include <limits.h> /* for PIPE_BUF */ 48133359Sobrien 49133359Sobrien#if defined(HAVE_UTIMES) 50133359Sobrien# include <sys/time.h> 51133359Sobrien#elif defined(HAVE_UTIME) 52133359Sobrien# if defined(HAVE_SYS_UTIME_H) 53133359Sobrien# include <sys/utime.h> 54133359Sobrien# elif defined(HAVE_UTIME_H) 55133359Sobrien# include <utime.h> 56133359Sobrien# endif 57133359Sobrien#endif 58133359Sobrien 59133359Sobrien#ifdef HAVE_UNISTD_H 60133359Sobrien#include <unistd.h> /* for read() */ 61133359Sobrien#endif 62133359Sobrien 63186690Sobrien#ifndef PIPE_BUF 64186690Sobrien/* Get the PIPE_BUF from pathconf */ 65186690Sobrien#ifdef _PC_PIPE_BUF 66186690Sobrien#define PIPE_BUF pathconf(".", _PC_PIPE_BUF) 67186690Sobrien#else 68186690Sobrien#define PIPE_BUF 512 69186690Sobrien#endif 70186690Sobrien#endif 71186690Sobrien 72133359Sobrienprivate void close_and_restore(const struct magic_set *, const char *, int, 73133359Sobrien const struct stat *); 74186690Sobrienprivate int unreadable_info(struct magic_set *, mode_t, const char *); 75226048Sobrienprivate const char* get_default_magic(void); 76175296Sobrien#ifndef COMPILE_ONLY 77175296Sobrienprivate const char *file_or_fd(struct magic_set *, const char *, int); 78175296Sobrien#endif 79133359Sobrien 80159764Sobrien#ifndef STDIN_FILENO 81159764Sobrien#define STDIN_FILENO 0 82159764Sobrien#endif 83159764Sobrien 84284237Sdelphij#ifdef WIN32 85284237Sdelphij/* HINSTANCE of this shared library. Needed for get_default_magic() */ 86284237Sdelphijstatic HINSTANCE _w32_dll_instance = NULL; 87284237Sdelphij 88284237Sdelphijstatic void 89284237Sdelphij_w32_append_path(char **hmagicpath, const char *fmt, ...) 90284237Sdelphij{ 91284237Sdelphij char *tmppath; 92284237Sdelphij char *newpath; 93284237Sdelphij va_list ap; 94284237Sdelphij 95284237Sdelphij va_start(ap, fmt); 96284237Sdelphij if (vasprintf(&tmppath, fmt, ap) < 0) { 97284237Sdelphij va_end(ap); 98284237Sdelphij return; 99284237Sdelphij } 100284237Sdelphij va_end(ap); 101284237Sdelphij 102284237Sdelphij if (access(tmppath, R_OK) == -1) 103284237Sdelphij goto out; 104284237Sdelphij 105284237Sdelphij if (*hmagicpath == NULL) { 106284237Sdelphij *hmagicpath = tmppath; 107284237Sdelphij return; 108284237Sdelphij } 109284237Sdelphij 110284237Sdelphij if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0) 111284237Sdelphij goto out; 112284237Sdelphij 113284237Sdelphij free(*hmagicpath); 114284237Sdelphij free(tmppath); 115284237Sdelphij *hmagicpath = newpath; 116284237Sdelphij return; 117284237Sdelphijout: 118284237Sdelphij free(tmppath); 119284237Sdelphij} 120284237Sdelphij 121284237Sdelphijstatic void 122284237Sdelphij_w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module) 123284237Sdelphij{ 124284237Sdelphij static const char *trypaths[] = { 125284237Sdelphij "%s/share/misc/magic.mgc", 126284237Sdelphij "%s/magic.mgc", 127284237Sdelphij }; 128284237Sdelphij LPSTR dllpath; 129284237Sdelphij size_t sp; 130284237Sdelphij 131284237Sdelphij dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath)); 132284237Sdelphij 133284237Sdelphij if (!GetModuleFileNameA(module, dllpath, MAX_PATH)) 134284237Sdelphij goto out; 135284237Sdelphij 136284237Sdelphij PathRemoveFileSpecA(dllpath); 137284237Sdelphij 138288143Sdelphij if (module) { 139288143Sdelphij char exepath[MAX_PATH]; 140288143Sdelphij GetModuleFileNameA(NULL, exepath, MAX_PATH); 141288143Sdelphij PathRemoveFileSpecA(exepath); 142288143Sdelphij if (stricmp(exepath, dllpath) == 0) 143288143Sdelphij goto out; 144288143Sdelphij } 145288143Sdelphij 146284237Sdelphij sp = strlen(dllpath); 147284237Sdelphij if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) { 148284237Sdelphij _w32_append_path(hmagicpath, 149284237Sdelphij "%s/../share/misc/magic.mgc", dllpath); 150284237Sdelphij goto out; 151284237Sdelphij } 152284237Sdelphij 153284237Sdelphij for (sp = 0; sp < __arraycount(trypaths); sp++) 154284237Sdelphij _w32_append_path(hmagicpath, trypaths[sp], dllpath); 155284237Sdelphijout: 156284237Sdelphij free(dllpath); 157284237Sdelphij} 158284237Sdelphij 159284237Sdelphij/* Placate GCC by offering a sacrificial previous prototype */ 160284237SdelphijBOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID); 161284237Sdelphij 162284237SdelphijBOOL WINAPI 163284237SdelphijDllMain(HINSTANCE hinstDLL, DWORD fdwReason, 164284237Sdelphij LPVOID lpvReserved __attribute__((__unused__))) 165284237Sdelphij{ 166284237Sdelphij if (fdwReason == DLL_PROCESS_ATTACH) 167284237Sdelphij _w32_dll_instance = hinstDLL; 168328874Seadler return 1; 169284237Sdelphij} 170284237Sdelphij#endif 171284237Sdelphij 172226048Sobrienprivate const char * 173226048Sobrienget_default_magic(void) 174226048Sobrien{ 175226048Sobrien static const char hmagic[] = "/.magic/magic.mgc"; 176226048Sobrien static char *default_magic; 177226048Sobrien char *home, *hmagicpath; 178226048Sobrien 179226048Sobrien#ifndef WIN32 180226048Sobrien struct stat st; 181226048Sobrien 182226048Sobrien if (default_magic) { 183226048Sobrien free(default_magic); 184226048Sobrien default_magic = NULL; 185226048Sobrien } 186226048Sobrien if ((home = getenv("HOME")) == NULL) 187226048Sobrien return MAGIC; 188226048Sobrien 189267843Sdelphij if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0) 190226048Sobrien return MAGIC; 191267843Sdelphij if (stat(hmagicpath, &st) == -1) { 192226048Sobrien free(hmagicpath); 193267843Sdelphij if (asprintf(&hmagicpath, "%s/.magic", home) < 0) 194226048Sobrien return MAGIC; 195267843Sdelphij if (stat(hmagicpath, &st) == -1) 196226048Sobrien goto out; 197267843Sdelphij if (S_ISDIR(st.st_mode)) { 198267843Sdelphij free(hmagicpath); 199267843Sdelphij if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0) 200267843Sdelphij return MAGIC; 201267843Sdelphij if (access(hmagicpath, R_OK) == -1) 202267843Sdelphij goto out; 203267843Sdelphij } 204226048Sobrien } 205226048Sobrien 206226048Sobrien if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0) 207226048Sobrien goto out; 208226048Sobrien free(hmagicpath); 209226048Sobrien return default_magic; 210226048Sobrienout: 211226048Sobrien default_magic = NULL; 212226048Sobrien free(hmagicpath); 213226048Sobrien return MAGIC; 214226048Sobrien#else 215267843Sdelphij hmagicpath = NULL; 216226048Sobrien 217226048Sobrien if (default_magic) { 218226048Sobrien free(default_magic); 219226048Sobrien default_magic = NULL; 220226048Sobrien } 221226048Sobrien 222284237Sdelphij /* First, try to get a magic file from user-application data */ 223284237Sdelphij if ((home = getenv("LOCALAPPDATA")) != NULL) 224284237Sdelphij _w32_append_path(&hmagicpath, "%s%s", home, hmagic); 225226048Sobrien 226284237Sdelphij /* Second, try to get a magic file from the user profile data */ 227284237Sdelphij if ((home = getenv("USERPROFILE")) != NULL) 228284237Sdelphij _w32_append_path(&hmagicpath, 229284237Sdelphij "%s/Local Settings/Application Data%s", home, hmagic); 230226048Sobrien 231284237Sdelphij /* Third, try to get a magic file from Common Files */ 232284237Sdelphij if ((home = getenv("COMMONPROGRAMFILES")) != NULL) 233284237Sdelphij _w32_append_path(&hmagicpath, "%s%s", home, hmagic); 234226048Sobrien 235284237Sdelphij /* Fourth, try to get magic file relative to exe location */ 236284237Sdelphij _w32_get_magic_relative_to(&hmagicpath, NULL); 237226048Sobrien 238284237Sdelphij /* Fifth, try to get magic file relative to dll location */ 239284237Sdelphij _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance); 240284237Sdelphij 241284237Sdelphij /* Avoid MAGIC constant - it likely points to a file within MSys tree */ 242226048Sobrien default_magic = hmagicpath; 243226048Sobrien return default_magic; 244226048Sobrien#endif 245226048Sobrien} 246226048Sobrien 247226048Sobrienpublic const char * 248226048Sobrienmagic_getpath(const char *magicfile, int action) 249226048Sobrien{ 250226048Sobrien if (magicfile != NULL) 251226048Sobrien return magicfile; 252226048Sobrien 253226048Sobrien magicfile = getenv("MAGIC"); 254226048Sobrien if (magicfile != NULL) 255226048Sobrien return magicfile; 256226048Sobrien 257226048Sobrien return action == FILE_LOAD ? get_default_magic() : MAGIC; 258226048Sobrien} 259226048Sobrien 260133359Sobrienpublic struct magic_set * 261133359Sobrienmagic_open(int flags) 262133359Sobrien{ 263267843Sdelphij return file_ms_alloc(flags); 264133359Sobrien} 265133359Sobrien 266169942Sobrienprivate int 267186690Sobrienunreadable_info(struct magic_set *ms, mode_t md, const char *file) 268169942Sobrien{ 269267843Sdelphij if (file) { 270267843Sdelphij /* We cannot open it, but we were able to stat it. */ 271267843Sdelphij if (access(file, W_OK) == 0) 272267843Sdelphij if (file_printf(ms, "writable, ") == -1) 273267843Sdelphij return -1; 274267843Sdelphij if (access(file, X_OK) == 0) 275267843Sdelphij if (file_printf(ms, "executable, ") == -1) 276267843Sdelphij return -1; 277267843Sdelphij } 278169942Sobrien if (S_ISREG(md)) 279169942Sobrien if (file_printf(ms, "regular file, ") == -1) 280169942Sobrien return -1; 281169942Sobrien if (file_printf(ms, "no read permission") == -1) 282169942Sobrien return -1; 283169942Sobrien return 0; 284169942Sobrien} 285169942Sobrien 286133359Sobrienpublic void 287169942Sobrienmagic_close(struct magic_set *ms) 288133359Sobrien{ 289267843Sdelphij if (ms == NULL) 290267843Sdelphij return; 291267843Sdelphij file_ms_free(ms); 292133359Sobrien} 293133359Sobrien 294133359Sobrien/* 295133359Sobrien * load a magic file 296133359Sobrien */ 297133359Sobrienpublic int 298133359Sobrienmagic_load(struct magic_set *ms, const char *magicfile) 299133359Sobrien{ 300267843Sdelphij if (ms == NULL) 301267843Sdelphij return -1; 302267843Sdelphij return file_apprentice(ms, magicfile, FILE_LOAD); 303133359Sobrien} 304133359Sobrien 305275698Sdelphij#ifndef COMPILE_ONLY 306275698Sdelphij/* 307275698Sdelphij * Install a set of compiled magic buffers. 308275698Sdelphij */ 309133359Sobrienpublic int 310275698Sdelphijmagic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes, 311275698Sdelphij size_t nbufs) 312275698Sdelphij{ 313275698Sdelphij if (ms == NULL) 314275698Sdelphij return -1; 315354939Sdelphij return buffer_apprentice(ms, RCAST(struct magic **, bufs), 316354939Sdelphij sizes, nbufs); 317275698Sdelphij} 318275698Sdelphij#endif 319275698Sdelphij 320275698Sdelphijpublic int 321133359Sobrienmagic_compile(struct magic_set *ms, const char *magicfile) 322133359Sobrien{ 323267843Sdelphij if (ms == NULL) 324267843Sdelphij return -1; 325267843Sdelphij return file_apprentice(ms, magicfile, FILE_COMPILE); 326133359Sobrien} 327133359Sobrien 328133359Sobrienpublic int 329133359Sobrienmagic_check(struct magic_set *ms, const char *magicfile) 330133359Sobrien{ 331267843Sdelphij if (ms == NULL) 332267843Sdelphij return -1; 333267843Sdelphij return file_apprentice(ms, magicfile, FILE_CHECK); 334133359Sobrien} 335133359Sobrien 336226048Sobrienpublic int 337226048Sobrienmagic_list(struct magic_set *ms, const char *magicfile) 338226048Sobrien{ 339267843Sdelphij if (ms == NULL) 340267843Sdelphij return -1; 341267843Sdelphij return file_apprentice(ms, magicfile, FILE_LIST); 342226048Sobrien} 343226048Sobrien 344133359Sobrienprivate void 345133359Sobrienclose_and_restore(const struct magic_set *ms, const char *name, int fd, 346133359Sobrien const struct stat *sb) 347133359Sobrien{ 348299234Sdelphij if (fd == STDIN_FILENO || name == NULL) 349159764Sobrien return; 350133359Sobrien (void) close(fd); 351159764Sobrien 352159764Sobrien if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) { 353133359Sobrien /* 354133359Sobrien * Try to restore access, modification times if read it. 355133359Sobrien * This is really *bad* because it will modify the status 356133359Sobrien * time of the file... And of course this will affect 357133359Sobrien * backup programs 358133359Sobrien */ 359133359Sobrien#ifdef HAVE_UTIMES 360133359Sobrien struct timeval utsbuf[2]; 361186690Sobrien (void)memset(utsbuf, 0, sizeof(utsbuf)); 362133359Sobrien utsbuf[0].tv_sec = sb->st_atime; 363133359Sobrien utsbuf[1].tv_sec = sb->st_mtime; 364133359Sobrien 365133359Sobrien (void) utimes(name, utsbuf); /* don't care if loses */ 366133359Sobrien#elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H) 367133359Sobrien struct utimbuf utbuf; 368133359Sobrien 369191736Sobrien (void)memset(&utbuf, 0, sizeof(utbuf)); 370133359Sobrien utbuf.actime = sb->st_atime; 371133359Sobrien utbuf.modtime = sb->st_mtime; 372133359Sobrien (void) utime(name, &utbuf); /* don't care if loses */ 373133359Sobrien#endif 374133359Sobrien } 375133359Sobrien} 376133359Sobrien 377133359Sobrien#ifndef COMPILE_ONLY 378175296Sobrien 379133359Sobrien/* 380175296Sobrien * find type of descriptor 381175296Sobrien */ 382175296Sobrienpublic const char * 383175296Sobrienmagic_descriptor(struct magic_set *ms, int fd) 384175296Sobrien{ 385267843Sdelphij if (ms == NULL) 386267843Sdelphij return NULL; 387175296Sobrien return file_or_fd(ms, NULL, fd); 388175296Sobrien} 389175296Sobrien 390175296Sobrien/* 391133359Sobrien * find type of named file 392133359Sobrien */ 393133359Sobrienpublic const char * 394133359Sobrienmagic_file(struct magic_set *ms, const char *inname) 395133359Sobrien{ 396267843Sdelphij if (ms == NULL) 397267843Sdelphij return NULL; 398175296Sobrien return file_or_fd(ms, inname, STDIN_FILENO); 399175296Sobrien} 400175296Sobrien 401175296Sobrienprivate const char * 402175296Sobrienfile_or_fd(struct magic_set *ms, const char *inname, int fd) 403175296Sobrien{ 404159764Sobrien int rv = -1; 405159764Sobrien unsigned char *buf; 406133359Sobrien struct stat sb; 407133359Sobrien ssize_t nbytes = 0; /* number of bytes read from a datafile */ 408169942Sobrien int ispipe = 0; 409354939Sdelphij int okstat = 0; 410354939Sdelphij off_t pos = CAST(off_t, -1); 411133359Sobrien 412328874Seadler if (file_reset(ms, 1) == -1) 413267843Sdelphij goto out; 414267843Sdelphij 415159764Sobrien /* 416159764Sobrien * one extra for terminating '\0', and 417159764Sobrien * some overlapping space for matches near EOF 418159764Sobrien */ 419159764Sobrien#define SLOP (1 + sizeof(union VALUETYPE)) 420298192Sdelphij if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL) 421133359Sobrien return NULL; 422133359Sobrien 423133359Sobrien switch (file_fsmagic(ms, inname, &sb)) { 424169942Sobrien case -1: /* error */ 425159764Sobrien goto done; 426169942Sobrien case 0: /* nothing found */ 427133359Sobrien break; 428169942Sobrien default: /* matched it and printed type */ 429159764Sobrien rv = 0; 430159764Sobrien goto done; 431133359Sobrien } 432133359Sobrien 433267843Sdelphij#ifdef WIN32 434267843Sdelphij /* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */ 435267843Sdelphij if (fd == STDIN_FILENO) 436267843Sdelphij _setmode(STDIN_FILENO, O_BINARY); 437267843Sdelphij#endif 438354939Sdelphij if (inname != NULL) { 439354939Sdelphij int flags = O_RDONLY|O_BINARY|O_NONBLOCK; 440169942Sobrien errno = 0; 441169942Sobrien if ((fd = open(inname, flags)) < 0) { 442354939Sdelphij okstat = stat(inname, &sb) == 0; 443354939Sdelphij if (okstat && S_ISFIFO(sb.st_mode)) 444354939Sdelphij ispipe = 1; 445267843Sdelphij#ifdef WIN32 446267843Sdelphij /* 447267843Sdelphij * Can't stat, can't open. It may have been opened in 448267843Sdelphij * fsmagic, so if the user doesn't have read permission, 449267843Sdelphij * allow it to say so; otherwise an error was probably 450267843Sdelphij * displayed in fsmagic. 451267843Sdelphij */ 452267843Sdelphij if (!okstat && errno == EACCES) { 453267843Sdelphij sb.st_mode = S_IFBLK; 454267843Sdelphij okstat = 1; 455267843Sdelphij } 456267843Sdelphij#endif 457267843Sdelphij if (okstat && 458267843Sdelphij unreadable_info(ms, sb.st_mode, inname) == -1) 459175296Sobrien goto done; 460192348Sdelphij rv = 0; 461192348Sdelphij goto done; 462169942Sobrien } 463133359Sobrien } 464133359Sobrien 465354939Sdelphij if (fd != -1) { 466362844Sdelphij okstat = fstat(fd, &sb) == 0; 467354939Sdelphij if (okstat && S_ISFIFO(sb.st_mode)) 468354939Sdelphij ispipe = 1; 469354939Sdelphij if (inname == NULL) 470354939Sdelphij pos = lseek(fd, CAST(off_t, 0), SEEK_CUR); 471354939Sdelphij } 472354939Sdelphij 473133359Sobrien /* 474298192Sdelphij * try looking at the first ms->bytes_max bytes 475133359Sobrien */ 476169942Sobrien if (ispipe) { 477354939Sdelphij if (fd != -1) { 478354939Sdelphij ssize_t r = 0; 479169942Sobrien 480354939Sdelphij while ((r = sread(fd, RCAST(void *, &buf[nbytes]), 481354939Sdelphij CAST(size_t, ms->bytes_max - nbytes), 1)) > 0) { 482354939Sdelphij nbytes += r; 483354939Sdelphij if (r < PIPE_BUF) break; 484354939Sdelphij } 485169942Sobrien } 486169942Sobrien 487309847Sdelphij if (nbytes == 0 && inname) { 488169942Sobrien /* We can not read it, but we were able to stat it. */ 489186690Sobrien if (unreadable_info(ms, sb.st_mode, inname) == -1) 490169942Sobrien goto done; 491169942Sobrien rv = 0; 492169942Sobrien goto done; 493169942Sobrien } 494169942Sobrien 495354939Sdelphij } else if (fd != -1) { 496267843Sdelphij /* Windows refuses to read from a big console buffer. */ 497267843Sdelphij size_t howmany = 498298192Sdelphij#if defined(WIN32) 499354939Sdelphij _isatty(fd) ? 8 * 1024 : 500267843Sdelphij#endif 501354939Sdelphij ms->bytes_max; 502354939Sdelphij if ((nbytes = read(fd, RCAST(void *, buf), howmany)) == -1) { 503267843Sdelphij if (inname == NULL && fd != STDIN_FILENO) 504267843Sdelphij file_error(ms, errno, "cannot read fd %d", fd); 505267843Sdelphij else 506267843Sdelphij file_error(ms, errno, "cannot read `%s'", 507267843Sdelphij inname == NULL ? "/dev/stdin" : inname); 508169942Sobrien goto done; 509169942Sobrien } 510133359Sobrien } 511133359Sobrien 512175296Sobrien (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */ 513354939Sdelphij if (file_buffer(ms, fd, okstat ? &sb : NULL, inname, buf, CAST(size_t, nbytes)) == -1) 514175296Sobrien goto done; 515159764Sobrien rv = 0; 516133359Sobriendone: 517159764Sobrien free(buf); 518299736Sdelphij if (fd != -1) { 519354939Sdelphij if (pos != CAST(off_t, -1)) 520299736Sdelphij (void)lseek(fd, pos, SEEK_SET); 521299736Sdelphij close_and_restore(ms, inname, fd, &sb); 522299736Sdelphij } 523267843Sdelphijout: 524159764Sobrien return rv == 0 ? file_getbuffer(ms) : NULL; 525133359Sobrien} 526133359Sobrien 527133359Sobrien 528133359Sobrienpublic const char * 529133359Sobrienmagic_buffer(struct magic_set *ms, const void *buf, size_t nb) 530133359Sobrien{ 531267843Sdelphij if (ms == NULL) 532267843Sdelphij return NULL; 533328874Seadler if (file_reset(ms, 1) == -1) 534133359Sobrien return NULL; 535133359Sobrien /* 536133359Sobrien * The main work is done here! 537186690Sobrien * We have the file name and/or the data buffer to be identified. 538133359Sobrien */ 539354939Sdelphij if (file_buffer(ms, -1, NULL, NULL, buf, nb) == -1) { 540133359Sobrien return NULL; 541133359Sobrien } 542133359Sobrien return file_getbuffer(ms); 543133359Sobrien} 544133359Sobrien#endif 545133359Sobrien 546133359Sobrienpublic const char * 547133359Sobrienmagic_error(struct magic_set *ms) 548133359Sobrien{ 549267843Sdelphij if (ms == NULL) 550267843Sdelphij return "Magic database is not open"; 551191736Sobrien return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL; 552133359Sobrien} 553133359Sobrien 554133359Sobrienpublic int 555133359Sobrienmagic_errno(struct magic_set *ms) 556133359Sobrien{ 557267843Sdelphij if (ms == NULL) 558267843Sdelphij return EINVAL; 559191736Sobrien return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0; 560133359Sobrien} 561133359Sobrien 562133359Sobrienpublic int 563328874Seadlermagic_getflags(struct magic_set *ms) 564328874Seadler{ 565328874Seadler if (ms == NULL) 566328874Seadler return -1; 567328874Seadler 568328874Seadler return ms->flags; 569328874Seadler} 570328874Seadler 571328874Seadlerpublic int 572133359Sobrienmagic_setflags(struct magic_set *ms, int flags) 573133359Sobrien{ 574267843Sdelphij if (ms == NULL) 575267843Sdelphij return -1; 576133359Sobrien#if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES) 577133359Sobrien if (flags & MAGIC_PRESERVE_ATIME) 578133359Sobrien return -1; 579133359Sobrien#endif 580133359Sobrien ms->flags = flags; 581133359Sobrien return 0; 582133359Sobrien} 583267843Sdelphij 584267843Sdelphijpublic int 585267843Sdelphijmagic_version(void) 586267843Sdelphij{ 587267843Sdelphij return MAGIC_VERSION; 588267843Sdelphij} 589275698Sdelphij 590275698Sdelphijpublic int 591275698Sdelphijmagic_setparam(struct magic_set *ms, int param, const void *val) 592275698Sdelphij{ 593354939Sdelphij if (ms == NULL) 594354939Sdelphij return -1; 595275698Sdelphij switch (param) { 596275698Sdelphij case MAGIC_PARAM_INDIR_MAX: 597354939Sdelphij ms->indir_max = CAST(uint16_t, *CAST(const size_t *, val)); 598275698Sdelphij return 0; 599275698Sdelphij case MAGIC_PARAM_NAME_MAX: 600354939Sdelphij ms->name_max = CAST(uint16_t, *CAST(const size_t *, val)); 601275698Sdelphij return 0; 602275698Sdelphij case MAGIC_PARAM_ELF_PHNUM_MAX: 603354939Sdelphij ms->elf_phnum_max = CAST(uint16_t, *CAST(const size_t *, val)); 604275698Sdelphij return 0; 605275698Sdelphij case MAGIC_PARAM_ELF_SHNUM_MAX: 606354939Sdelphij ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val)); 607275698Sdelphij return 0; 608276577Sdelphij case MAGIC_PARAM_ELF_NOTES_MAX: 609354939Sdelphij ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val)); 610276577Sdelphij return 0; 611288143Sdelphij case MAGIC_PARAM_REGEX_MAX: 612354939Sdelphij ms->regex_max = CAST(uint16_t, *CAST(const size_t *, val)); 613288143Sdelphij return 0; 614298192Sdelphij case MAGIC_PARAM_BYTES_MAX: 615354939Sdelphij ms->bytes_max = *CAST(const size_t *, val); 616298192Sdelphij return 0; 617275698Sdelphij default: 618275698Sdelphij errno = EINVAL; 619275698Sdelphij return -1; 620275698Sdelphij } 621275698Sdelphij} 622275698Sdelphij 623275698Sdelphijpublic int 624275698Sdelphijmagic_getparam(struct magic_set *ms, int param, void *val) 625275698Sdelphij{ 626354939Sdelphij if (ms == NULL) 627354939Sdelphij return -1; 628275698Sdelphij switch (param) { 629275698Sdelphij case MAGIC_PARAM_INDIR_MAX: 630354939Sdelphij *CAST(size_t *, val) = ms->indir_max; 631275698Sdelphij return 0; 632275698Sdelphij case MAGIC_PARAM_NAME_MAX: 633354939Sdelphij *CAST(size_t *, val) = ms->name_max; 634275698Sdelphij return 0; 635275698Sdelphij case MAGIC_PARAM_ELF_PHNUM_MAX: 636354939Sdelphij *CAST(size_t *, val) = ms->elf_phnum_max; 637275698Sdelphij return 0; 638275698Sdelphij case MAGIC_PARAM_ELF_SHNUM_MAX: 639354939Sdelphij *CAST(size_t *, val) = ms->elf_shnum_max; 640275698Sdelphij return 0; 641276577Sdelphij case MAGIC_PARAM_ELF_NOTES_MAX: 642354939Sdelphij *CAST(size_t *, val) = ms->elf_notes_max; 643276577Sdelphij return 0; 644288143Sdelphij case MAGIC_PARAM_REGEX_MAX: 645354939Sdelphij *CAST(size_t *, val) = ms->regex_max; 646288143Sdelphij return 0; 647298192Sdelphij case MAGIC_PARAM_BYTES_MAX: 648354939Sdelphij *CAST(size_t *, val) = ms->bytes_max; 649298192Sdelphij return 0; 650275698Sdelphij default: 651275698Sdelphij errno = EINVAL; 652275698Sdelphij return -1; 653275698Sdelphij } 654275698Sdelphij} 655