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 36328875SeadlerFILE_RCSID("@(#)$File: magic.c,v 1.102 2017/08/28 13:39:18 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 47186690Sobrien#ifdef HAVE_LIMITS_H 48169942Sobrien#include <limits.h> /* for PIPE_BUF */ 49186690Sobrien#endif 50133359Sobrien 51133359Sobrien#if defined(HAVE_UTIMES) 52133359Sobrien# include <sys/time.h> 53133359Sobrien#elif defined(HAVE_UTIME) 54133359Sobrien# if defined(HAVE_SYS_UTIME_H) 55133359Sobrien# include <sys/utime.h> 56133359Sobrien# elif defined(HAVE_UTIME_H) 57133359Sobrien# include <utime.h> 58133359Sobrien# endif 59133359Sobrien#endif 60133359Sobrien 61133359Sobrien#ifdef HAVE_UNISTD_H 62133359Sobrien#include <unistd.h> /* for read() */ 63133359Sobrien#endif 64133359Sobrien 65186690Sobrien#ifndef PIPE_BUF 66186690Sobrien/* Get the PIPE_BUF from pathconf */ 67186690Sobrien#ifdef _PC_PIPE_BUF 68186690Sobrien#define PIPE_BUF pathconf(".", _PC_PIPE_BUF) 69186690Sobrien#else 70186690Sobrien#define PIPE_BUF 512 71186690Sobrien#endif 72186690Sobrien#endif 73186690Sobrien 74133359Sobrienprivate void close_and_restore(const struct magic_set *, const char *, int, 75133359Sobrien const struct stat *); 76186690Sobrienprivate int unreadable_info(struct magic_set *, mode_t, const char *); 77226048Sobrienprivate const char* get_default_magic(void); 78175296Sobrien#ifndef COMPILE_ONLY 79175296Sobrienprivate const char *file_or_fd(struct magic_set *, const char *, int); 80175296Sobrien#endif 81133359Sobrien 82159764Sobrien#ifndef STDIN_FILENO 83159764Sobrien#define STDIN_FILENO 0 84159764Sobrien#endif 85159764Sobrien 86284778Sdelphij#ifdef WIN32 87284778Sdelphij/* HINSTANCE of this shared library. Needed for get_default_magic() */ 88284778Sdelphijstatic HINSTANCE _w32_dll_instance = NULL; 89284778Sdelphij 90284778Sdelphijstatic void 91284778Sdelphij_w32_append_path(char **hmagicpath, const char *fmt, ...) 92284778Sdelphij{ 93284778Sdelphij char *tmppath; 94284778Sdelphij char *newpath; 95284778Sdelphij va_list ap; 96284778Sdelphij 97284778Sdelphij va_start(ap, fmt); 98284778Sdelphij if (vasprintf(&tmppath, fmt, ap) < 0) { 99284778Sdelphij va_end(ap); 100284778Sdelphij return; 101284778Sdelphij } 102284778Sdelphij va_end(ap); 103284778Sdelphij 104284778Sdelphij if (access(tmppath, R_OK) == -1) 105284778Sdelphij goto out; 106284778Sdelphij 107284778Sdelphij if (*hmagicpath == NULL) { 108284778Sdelphij *hmagicpath = tmppath; 109284778Sdelphij return; 110284778Sdelphij } 111284778Sdelphij 112284778Sdelphij if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0) 113284778Sdelphij goto out; 114284778Sdelphij 115284778Sdelphij free(*hmagicpath); 116284778Sdelphij free(tmppath); 117284778Sdelphij *hmagicpath = newpath; 118284778Sdelphij return; 119284778Sdelphijout: 120284778Sdelphij free(tmppath); 121284778Sdelphij} 122284778Sdelphij 123284778Sdelphijstatic void 124284778Sdelphij_w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module) 125284778Sdelphij{ 126284778Sdelphij static const char *trypaths[] = { 127284778Sdelphij "%s/share/misc/magic.mgc", 128284778Sdelphij "%s/magic.mgc", 129284778Sdelphij }; 130284778Sdelphij LPSTR dllpath; 131284778Sdelphij size_t sp; 132284778Sdelphij 133284778Sdelphij dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath)); 134284778Sdelphij 135284778Sdelphij if (!GetModuleFileNameA(module, dllpath, MAX_PATH)) 136284778Sdelphij goto out; 137284778Sdelphij 138284778Sdelphij PathRemoveFileSpecA(dllpath); 139284778Sdelphij 140290152Sdelphij if (module) { 141290152Sdelphij char exepath[MAX_PATH]; 142290152Sdelphij GetModuleFileNameA(NULL, exepath, MAX_PATH); 143290152Sdelphij PathRemoveFileSpecA(exepath); 144290152Sdelphij if (stricmp(exepath, dllpath) == 0) 145290152Sdelphij goto out; 146290152Sdelphij } 147290152Sdelphij 148284778Sdelphij sp = strlen(dllpath); 149284778Sdelphij if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) { 150284778Sdelphij _w32_append_path(hmagicpath, 151284778Sdelphij "%s/../share/misc/magic.mgc", dllpath); 152284778Sdelphij goto out; 153284778Sdelphij } 154284778Sdelphij 155284778Sdelphij for (sp = 0; sp < __arraycount(trypaths); sp++) 156284778Sdelphij _w32_append_path(hmagicpath, trypaths[sp], dllpath); 157284778Sdelphijout: 158284778Sdelphij free(dllpath); 159284778Sdelphij} 160284778Sdelphij 161284778Sdelphij/* Placate GCC by offering a sacrificial previous prototype */ 162284778SdelphijBOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID); 163284778Sdelphij 164284778SdelphijBOOL WINAPI 165284778SdelphijDllMain(HINSTANCE hinstDLL, DWORD fdwReason, 166284778Sdelphij LPVOID lpvReserved __attribute__((__unused__))) 167284778Sdelphij{ 168284778Sdelphij if (fdwReason == DLL_PROCESS_ATTACH) 169284778Sdelphij _w32_dll_instance = hinstDLL; 170328875Seadler return 1; 171284778Sdelphij} 172284778Sdelphij#endif 173284778Sdelphij 174226048Sobrienprivate const char * 175226048Sobrienget_default_magic(void) 176226048Sobrien{ 177226048Sobrien static const char hmagic[] = "/.magic/magic.mgc"; 178226048Sobrien static char *default_magic; 179226048Sobrien char *home, *hmagicpath; 180226048Sobrien 181226048Sobrien#ifndef WIN32 182226048Sobrien struct stat st; 183226048Sobrien 184226048Sobrien if (default_magic) { 185226048Sobrien free(default_magic); 186226048Sobrien default_magic = NULL; 187226048Sobrien } 188226048Sobrien if ((home = getenv("HOME")) == NULL) 189226048Sobrien return MAGIC; 190226048Sobrien 191267843Sdelphij if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0) 192226048Sobrien return MAGIC; 193267843Sdelphij if (stat(hmagicpath, &st) == -1) { 194226048Sobrien free(hmagicpath); 195267843Sdelphij if (asprintf(&hmagicpath, "%s/.magic", home) < 0) 196226048Sobrien return MAGIC; 197267843Sdelphij if (stat(hmagicpath, &st) == -1) 198226048Sobrien goto out; 199267843Sdelphij if (S_ISDIR(st.st_mode)) { 200267843Sdelphij free(hmagicpath); 201267843Sdelphij if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0) 202267843Sdelphij return MAGIC; 203267843Sdelphij if (access(hmagicpath, R_OK) == -1) 204267843Sdelphij goto out; 205267843Sdelphij } 206226048Sobrien } 207226048Sobrien 208226048Sobrien if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0) 209226048Sobrien goto out; 210226048Sobrien free(hmagicpath); 211226048Sobrien return default_magic; 212226048Sobrienout: 213226048Sobrien default_magic = NULL; 214226048Sobrien free(hmagicpath); 215226048Sobrien return MAGIC; 216226048Sobrien#else 217267843Sdelphij hmagicpath = NULL; 218226048Sobrien 219226048Sobrien if (default_magic) { 220226048Sobrien free(default_magic); 221226048Sobrien default_magic = NULL; 222226048Sobrien } 223226048Sobrien 224284778Sdelphij /* First, try to get a magic file from user-application data */ 225284778Sdelphij if ((home = getenv("LOCALAPPDATA")) != NULL) 226284778Sdelphij _w32_append_path(&hmagicpath, "%s%s", home, hmagic); 227226048Sobrien 228284778Sdelphij /* Second, try to get a magic file from the user profile data */ 229284778Sdelphij if ((home = getenv("USERPROFILE")) != NULL) 230284778Sdelphij _w32_append_path(&hmagicpath, 231284778Sdelphij "%s/Local Settings/Application Data%s", home, hmagic); 232226048Sobrien 233284778Sdelphij /* Third, try to get a magic file from Common Files */ 234284778Sdelphij if ((home = getenv("COMMONPROGRAMFILES")) != NULL) 235284778Sdelphij _w32_append_path(&hmagicpath, "%s%s", home, hmagic); 236226048Sobrien 237284778Sdelphij /* Fourth, try to get magic file relative to exe location */ 238284778Sdelphij _w32_get_magic_relative_to(&hmagicpath, NULL); 239226048Sobrien 240284778Sdelphij /* Fifth, try to get magic file relative to dll location */ 241284778Sdelphij _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance); 242284778Sdelphij 243284778Sdelphij /* Avoid MAGIC constant - it likely points to a file within MSys tree */ 244226048Sobrien default_magic = hmagicpath; 245226048Sobrien return default_magic; 246226048Sobrien#endif 247226048Sobrien} 248226048Sobrien 249226048Sobrienpublic const char * 250226048Sobrienmagic_getpath(const char *magicfile, int action) 251226048Sobrien{ 252226048Sobrien if (magicfile != NULL) 253226048Sobrien return magicfile; 254226048Sobrien 255226048Sobrien magicfile = getenv("MAGIC"); 256226048Sobrien if (magicfile != NULL) 257226048Sobrien return magicfile; 258226048Sobrien 259226048Sobrien return action == FILE_LOAD ? get_default_magic() : MAGIC; 260226048Sobrien} 261226048Sobrien 262133359Sobrienpublic struct magic_set * 263133359Sobrienmagic_open(int flags) 264133359Sobrien{ 265267843Sdelphij return file_ms_alloc(flags); 266133359Sobrien} 267133359Sobrien 268169942Sobrienprivate int 269186690Sobrienunreadable_info(struct magic_set *ms, mode_t md, const char *file) 270169942Sobrien{ 271267843Sdelphij if (file) { 272267843Sdelphij /* We cannot open it, but we were able to stat it. */ 273267843Sdelphij if (access(file, W_OK) == 0) 274267843Sdelphij if (file_printf(ms, "writable, ") == -1) 275267843Sdelphij return -1; 276267843Sdelphij if (access(file, X_OK) == 0) 277267843Sdelphij if (file_printf(ms, "executable, ") == -1) 278267843Sdelphij return -1; 279267843Sdelphij } 280169942Sobrien if (S_ISREG(md)) 281169942Sobrien if (file_printf(ms, "regular file, ") == -1) 282169942Sobrien return -1; 283169942Sobrien if (file_printf(ms, "no read permission") == -1) 284169942Sobrien return -1; 285169942Sobrien return 0; 286169942Sobrien} 287169942Sobrien 288133359Sobrienpublic void 289169942Sobrienmagic_close(struct magic_set *ms) 290133359Sobrien{ 291267843Sdelphij if (ms == NULL) 292267843Sdelphij return; 293267843Sdelphij file_ms_free(ms); 294133359Sobrien} 295133359Sobrien 296133359Sobrien/* 297133359Sobrien * load a magic file 298133359Sobrien */ 299133359Sobrienpublic int 300133359Sobrienmagic_load(struct magic_set *ms, const char *magicfile) 301133359Sobrien{ 302267843Sdelphij if (ms == NULL) 303267843Sdelphij return -1; 304267843Sdelphij return file_apprentice(ms, magicfile, FILE_LOAD); 305133359Sobrien} 306133359Sobrien 307276415Sdelphij#ifndef COMPILE_ONLY 308276415Sdelphij/* 309276415Sdelphij * Install a set of compiled magic buffers. 310276415Sdelphij */ 311133359Sobrienpublic int 312276415Sdelphijmagic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes, 313276415Sdelphij size_t nbufs) 314276415Sdelphij{ 315276415Sdelphij if (ms == NULL) 316276415Sdelphij return -1; 317276415Sdelphij return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs); 318276415Sdelphij} 319276415Sdelphij#endif 320276415Sdelphij 321276415Sdelphijpublic int 322133359Sobrienmagic_compile(struct magic_set *ms, const char *magicfile) 323133359Sobrien{ 324267843Sdelphij if (ms == NULL) 325267843Sdelphij return -1; 326267843Sdelphij return file_apprentice(ms, magicfile, FILE_COMPILE); 327133359Sobrien} 328133359Sobrien 329133359Sobrienpublic int 330133359Sobrienmagic_check(struct magic_set *ms, const char *magicfile) 331133359Sobrien{ 332267843Sdelphij if (ms == NULL) 333267843Sdelphij return -1; 334267843Sdelphij return file_apprentice(ms, magicfile, FILE_CHECK); 335133359Sobrien} 336133359Sobrien 337226048Sobrienpublic int 338226048Sobrienmagic_list(struct magic_set *ms, const char *magicfile) 339226048Sobrien{ 340267843Sdelphij if (ms == NULL) 341267843Sdelphij return -1; 342267843Sdelphij return file_apprentice(ms, magicfile, FILE_LIST); 343226048Sobrien} 344226048Sobrien 345133359Sobrienprivate void 346133359Sobrienclose_and_restore(const struct magic_set *ms, const char *name, int fd, 347133359Sobrien const struct stat *sb) 348133359Sobrien{ 349299236Sdelphij if (fd == STDIN_FILENO || name == NULL) 350159764Sobrien return; 351133359Sobrien (void) close(fd); 352159764Sobrien 353159764Sobrien if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) { 354133359Sobrien /* 355133359Sobrien * Try to restore access, modification times if read it. 356133359Sobrien * This is really *bad* because it will modify the status 357133359Sobrien * time of the file... And of course this will affect 358133359Sobrien * backup programs 359133359Sobrien */ 360133359Sobrien#ifdef HAVE_UTIMES 361133359Sobrien struct timeval utsbuf[2]; 362186690Sobrien (void)memset(utsbuf, 0, sizeof(utsbuf)); 363133359Sobrien utsbuf[0].tv_sec = sb->st_atime; 364133359Sobrien utsbuf[1].tv_sec = sb->st_mtime; 365133359Sobrien 366133359Sobrien (void) utimes(name, utsbuf); /* don't care if loses */ 367133359Sobrien#elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H) 368133359Sobrien struct utimbuf utbuf; 369133359Sobrien 370191736Sobrien (void)memset(&utbuf, 0, sizeof(utbuf)); 371133359Sobrien utbuf.actime = sb->st_atime; 372133359Sobrien utbuf.modtime = sb->st_mtime; 373133359Sobrien (void) utime(name, &utbuf); /* don't care if loses */ 374133359Sobrien#endif 375133359Sobrien } 376133359Sobrien} 377133359Sobrien 378133359Sobrien#ifndef COMPILE_ONLY 379175296Sobrien 380133359Sobrien/* 381175296Sobrien * find type of descriptor 382175296Sobrien */ 383175296Sobrienpublic const char * 384175296Sobrienmagic_descriptor(struct magic_set *ms, int fd) 385175296Sobrien{ 386267843Sdelphij if (ms == NULL) 387267843Sdelphij return NULL; 388175296Sobrien return file_or_fd(ms, NULL, fd); 389175296Sobrien} 390175296Sobrien 391175296Sobrien/* 392133359Sobrien * find type of named file 393133359Sobrien */ 394133359Sobrienpublic const char * 395133359Sobrienmagic_file(struct magic_set *ms, const char *inname) 396133359Sobrien{ 397267843Sdelphij if (ms == NULL) 398267843Sdelphij return NULL; 399175296Sobrien return file_or_fd(ms, inname, STDIN_FILENO); 400175296Sobrien} 401175296Sobrien 402175296Sobrienprivate const char * 403175296Sobrienfile_or_fd(struct magic_set *ms, const char *inname, int fd) 404175296Sobrien{ 405159764Sobrien int rv = -1; 406159764Sobrien unsigned char *buf; 407133359Sobrien struct stat sb; 408133359Sobrien ssize_t nbytes = 0; /* number of bytes read from a datafile */ 409169942Sobrien int ispipe = 0; 410267843Sdelphij off_t pos = (off_t)-1; 411133359Sobrien 412328875Seadler 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)) 420300899Sdelphij 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 438267843Sdelphij 439169942Sobrien if (inname == NULL) { 440169942Sobrien if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode)) 441169942Sobrien ispipe = 1; 442267843Sdelphij else 443267843Sdelphij pos = lseek(fd, (off_t)0, SEEK_CUR); 444169942Sobrien } else { 445169942Sobrien int flags = O_RDONLY|O_BINARY; 446267843Sdelphij int okstat = stat(inname, &sb) == 0; 447169942Sobrien 448267843Sdelphij if (okstat && S_ISFIFO(sb.st_mode)) { 449226048Sobrien#ifdef O_NONBLOCK 450169942Sobrien flags |= O_NONBLOCK; 451226048Sobrien#endif 452169942Sobrien ispipe = 1; 453169942Sobrien } 454169942Sobrien 455169942Sobrien errno = 0; 456169942Sobrien if ((fd = open(inname, flags)) < 0) { 457267843Sdelphij#ifdef WIN32 458267843Sdelphij /* 459267843Sdelphij * Can't stat, can't open. It may have been opened in 460267843Sdelphij * fsmagic, so if the user doesn't have read permission, 461267843Sdelphij * allow it to say so; otherwise an error was probably 462267843Sdelphij * displayed in fsmagic. 463267843Sdelphij */ 464267843Sdelphij if (!okstat && errno == EACCES) { 465267843Sdelphij sb.st_mode = S_IFBLK; 466267843Sdelphij okstat = 1; 467267843Sdelphij } 468267843Sdelphij#endif 469267843Sdelphij if (okstat && 470267843Sdelphij unreadable_info(ms, sb.st_mode, inname) == -1) 471175296Sobrien goto done; 472192348Sdelphij rv = 0; 473192348Sdelphij goto done; 474169942Sobrien } 475169942Sobrien#ifdef O_NONBLOCK 476169942Sobrien if ((flags = fcntl(fd, F_GETFL)) != -1) { 477169942Sobrien flags &= ~O_NONBLOCK; 478169942Sobrien (void)fcntl(fd, F_SETFL, flags); 479169942Sobrien } 480169942Sobrien#endif 481133359Sobrien } 482133359Sobrien 483133359Sobrien /* 484300899Sdelphij * try looking at the first ms->bytes_max bytes 485133359Sobrien */ 486169942Sobrien if (ispipe) { 487169942Sobrien ssize_t r = 0; 488169942Sobrien 489169942Sobrien while ((r = sread(fd, (void *)&buf[nbytes], 490300899Sdelphij (size_t)(ms->bytes_max - nbytes), 1)) > 0) { 491169942Sobrien nbytes += r; 492169942Sobrien if (r < PIPE_BUF) break; 493169942Sobrien } 494169942Sobrien 495309848Sdelphij if (nbytes == 0 && inname) { 496169942Sobrien /* We can not read it, but we were able to stat it. */ 497186690Sobrien if (unreadable_info(ms, sb.st_mode, inname) == -1) 498169942Sobrien goto done; 499169942Sobrien rv = 0; 500169942Sobrien goto done; 501169942Sobrien } 502169942Sobrien 503169942Sobrien } else { 504267843Sdelphij /* Windows refuses to read from a big console buffer. */ 505267843Sdelphij size_t howmany = 506300899Sdelphij#if defined(WIN32) 507267843Sdelphij _isatty(fd) ? 8 * 1024 : 508267843Sdelphij#endif 509300899Sdelphij ms->bytes_max; 510267843Sdelphij if ((nbytes = read(fd, (char *)buf, howmany)) == -1) { 511267843Sdelphij if (inname == NULL && fd != STDIN_FILENO) 512267843Sdelphij file_error(ms, errno, "cannot read fd %d", fd); 513267843Sdelphij else 514267843Sdelphij file_error(ms, errno, "cannot read `%s'", 515267843Sdelphij inname == NULL ? "/dev/stdin" : inname); 516169942Sobrien goto done; 517169942Sobrien } 518133359Sobrien } 519133359Sobrien 520175296Sobrien (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */ 521175296Sobrien if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1) 522175296Sobrien goto done; 523159764Sobrien rv = 0; 524133359Sobriendone: 525159764Sobrien free(buf); 526300899Sdelphij if (fd != -1) { 527300899Sdelphij if (pos != (off_t)-1) 528300899Sdelphij (void)lseek(fd, pos, SEEK_SET); 529300899Sdelphij close_and_restore(ms, inname, fd, &sb); 530300899Sdelphij } 531267843Sdelphijout: 532159764Sobrien return rv == 0 ? file_getbuffer(ms) : NULL; 533133359Sobrien} 534133359Sobrien 535133359Sobrien 536133359Sobrienpublic const char * 537133359Sobrienmagic_buffer(struct magic_set *ms, const void *buf, size_t nb) 538133359Sobrien{ 539267843Sdelphij if (ms == NULL) 540267843Sdelphij return NULL; 541328875Seadler if (file_reset(ms, 1) == -1) 542133359Sobrien return NULL; 543133359Sobrien /* 544133359Sobrien * The main work is done here! 545186690Sobrien * We have the file name and/or the data buffer to be identified. 546133359Sobrien */ 547169962Sobrien if (file_buffer(ms, -1, NULL, buf, nb) == -1) { 548133359Sobrien return NULL; 549133359Sobrien } 550133359Sobrien return file_getbuffer(ms); 551133359Sobrien} 552133359Sobrien#endif 553133359Sobrien 554133359Sobrienpublic const char * 555133359Sobrienmagic_error(struct magic_set *ms) 556133359Sobrien{ 557267843Sdelphij if (ms == NULL) 558267843Sdelphij return "Magic database is not open"; 559191736Sobrien return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL; 560133359Sobrien} 561133359Sobrien 562133359Sobrienpublic int 563133359Sobrienmagic_errno(struct magic_set *ms) 564133359Sobrien{ 565267843Sdelphij if (ms == NULL) 566267843Sdelphij return EINVAL; 567191736Sobrien return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0; 568133359Sobrien} 569133359Sobrien 570133359Sobrienpublic int 571328875Seadlermagic_getflags(struct magic_set *ms) 572328875Seadler{ 573328875Seadler if (ms == NULL) 574328875Seadler return -1; 575328875Seadler 576328875Seadler return ms->flags; 577328875Seadler} 578328875Seadler 579328875Seadlerpublic int 580133359Sobrienmagic_setflags(struct magic_set *ms, int flags) 581133359Sobrien{ 582267843Sdelphij if (ms == NULL) 583267843Sdelphij return -1; 584133359Sobrien#if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES) 585133359Sobrien if (flags & MAGIC_PRESERVE_ATIME) 586133359Sobrien return -1; 587133359Sobrien#endif 588133359Sobrien ms->flags = flags; 589133359Sobrien return 0; 590133359Sobrien} 591267843Sdelphij 592267843Sdelphijpublic int 593267843Sdelphijmagic_version(void) 594267843Sdelphij{ 595267843Sdelphij return MAGIC_VERSION; 596267843Sdelphij} 597276415Sdelphij 598276415Sdelphijpublic int 599276415Sdelphijmagic_setparam(struct magic_set *ms, int param, const void *val) 600276415Sdelphij{ 601276415Sdelphij switch (param) { 602276415Sdelphij case MAGIC_PARAM_INDIR_MAX: 603284778Sdelphij ms->indir_max = (uint16_t)*(const size_t *)val; 604276415Sdelphij return 0; 605276415Sdelphij case MAGIC_PARAM_NAME_MAX: 606284778Sdelphij ms->name_max = (uint16_t)*(const size_t *)val; 607276415Sdelphij return 0; 608276415Sdelphij case MAGIC_PARAM_ELF_PHNUM_MAX: 609284778Sdelphij ms->elf_phnum_max = (uint16_t)*(const size_t *)val; 610276415Sdelphij return 0; 611276415Sdelphij case MAGIC_PARAM_ELF_SHNUM_MAX: 612284778Sdelphij ms->elf_shnum_max = (uint16_t)*(const size_t *)val; 613276415Sdelphij return 0; 614277592Sdelphij case MAGIC_PARAM_ELF_NOTES_MAX: 615284778Sdelphij ms->elf_notes_max = (uint16_t)*(const size_t *)val; 616277592Sdelphij return 0; 617290152Sdelphij case MAGIC_PARAM_REGEX_MAX: 618290152Sdelphij ms->elf_notes_max = (uint16_t)*(const size_t *)val; 619290152Sdelphij return 0; 620300899Sdelphij case MAGIC_PARAM_BYTES_MAX: 621300899Sdelphij ms->bytes_max = *(const size_t *)val; 622300899Sdelphij return 0; 623276415Sdelphij default: 624276415Sdelphij errno = EINVAL; 625276415Sdelphij return -1; 626276415Sdelphij } 627276415Sdelphij} 628276415Sdelphij 629276415Sdelphijpublic int 630276415Sdelphijmagic_getparam(struct magic_set *ms, int param, void *val) 631276415Sdelphij{ 632276415Sdelphij switch (param) { 633276415Sdelphij case MAGIC_PARAM_INDIR_MAX: 634276415Sdelphij *(size_t *)val = ms->indir_max; 635276415Sdelphij return 0; 636276415Sdelphij case MAGIC_PARAM_NAME_MAX: 637276415Sdelphij *(size_t *)val = ms->name_max; 638276415Sdelphij return 0; 639276415Sdelphij case MAGIC_PARAM_ELF_PHNUM_MAX: 640276415Sdelphij *(size_t *)val = ms->elf_phnum_max; 641276415Sdelphij return 0; 642276415Sdelphij case MAGIC_PARAM_ELF_SHNUM_MAX: 643276415Sdelphij *(size_t *)val = ms->elf_shnum_max; 644276415Sdelphij return 0; 645277592Sdelphij case MAGIC_PARAM_ELF_NOTES_MAX: 646277592Sdelphij *(size_t *)val = ms->elf_notes_max; 647277592Sdelphij return 0; 648290152Sdelphij case MAGIC_PARAM_REGEX_MAX: 649290152Sdelphij *(size_t *)val = ms->regex_max; 650290152Sdelphij return 0; 651300899Sdelphij case MAGIC_PARAM_BYTES_MAX: 652300899Sdelphij *(size_t *)val = ms->bytes_max; 653300899Sdelphij return 0; 654276415Sdelphij default: 655276415Sdelphij errno = EINVAL; 656276415Sdelphij return -1; 657276415Sdelphij } 658276415Sdelphij} 659