1b6cee71dSXin LI /* 2b6cee71dSXin LI * Copyright (c) Ian F. Darwin 1986-1995. 3b6cee71dSXin LI * Software written by Ian F. Darwin and others; 4b6cee71dSXin LI * maintained 1995-present by Christos Zoulas and others. 5b6cee71dSXin LI * 6b6cee71dSXin LI * Redistribution and use in source and binary forms, with or without 7b6cee71dSXin LI * modification, are permitted provided that the following conditions 8b6cee71dSXin LI * are met: 9b6cee71dSXin LI * 1. Redistributions of source code must retain the above copyright 10b6cee71dSXin LI * notice immediately at the beginning of the file, without modification, 11b6cee71dSXin LI * this list of conditions, and the following disclaimer. 12b6cee71dSXin LI * 2. Redistributions in binary form must reproduce the above copyright 13b6cee71dSXin LI * notice, this list of conditions and the following disclaimer in the 14b6cee71dSXin LI * documentation and/or other materials provided with the distribution. 15b6cee71dSXin LI * 16b6cee71dSXin LI * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17b6cee71dSXin LI * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18b6cee71dSXin LI * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19b6cee71dSXin LI * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20b6cee71dSXin LI * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21b6cee71dSXin LI * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22b6cee71dSXin LI * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23b6cee71dSXin LI * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24b6cee71dSXin LI * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25b6cee71dSXin LI * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26b6cee71dSXin LI * SUCH DAMAGE. 27b6cee71dSXin LI */ 28b6cee71dSXin LI /* 29b6cee71dSXin LI * apprentice - make one pass through /etc/magic, learning its secrets. 30b6cee71dSXin LI */ 31b6cee71dSXin LI 32b6cee71dSXin LI #include "file.h" 33b6cee71dSXin LI 34b6cee71dSXin LI #ifndef lint 35*a5d223e6SXin LI FILE_RCSID("@(#)$File: apprentice.c,v 1.255 2016/10/24 18:02:17 christos Exp $") 36b6cee71dSXin LI #endif /* lint */ 37b6cee71dSXin LI 38b6cee71dSXin LI #include "magic.h" 39b6cee71dSXin LI #include <stdlib.h> 40b6cee71dSXin LI #ifdef HAVE_UNISTD_H 41b6cee71dSXin LI #include <unistd.h> 42b6cee71dSXin LI #endif 43b6cee71dSXin LI #ifdef HAVE_STDDEF_H 44b6cee71dSXin LI #include <stddef.h> 45b6cee71dSXin LI #endif 46b6cee71dSXin LI #include <string.h> 47b6cee71dSXin LI #include <assert.h> 48b6cee71dSXin LI #include <ctype.h> 49b6cee71dSXin LI #include <fcntl.h> 50b6cee71dSXin LI #ifdef QUICK 51b6cee71dSXin LI #include <sys/mman.h> 52b6cee71dSXin LI #endif 53b6cee71dSXin LI #include <dirent.h> 54b6cee71dSXin LI #if defined(HAVE_LIMITS_H) 55b6cee71dSXin LI #include <limits.h> 56b6cee71dSXin LI #endif 57b6cee71dSXin LI 58b6cee71dSXin LI #ifndef SSIZE_MAX 59b6cee71dSXin LI #define MAXMAGIC_SIZE ((ssize_t)0x7fffffff) 60b6cee71dSXin LI #else 61b6cee71dSXin LI #define MAXMAGIC_SIZE SSIZE_MAX 62b6cee71dSXin LI #endif 63b6cee71dSXin LI 64b6cee71dSXin LI #define EATAB {while (isascii((unsigned char) *l) && \ 65b6cee71dSXin LI isspace((unsigned char) *l)) ++l;} 66b6cee71dSXin LI #define LOWCASE(l) (isupper((unsigned char) (l)) ? \ 67b6cee71dSXin LI tolower((unsigned char) (l)) : (l)) 68b6cee71dSXin LI /* 69b6cee71dSXin LI * Work around a bug in headers on Digital Unix. 70b6cee71dSXin LI * At least confirmed for: OSF1 V4.0 878 71b6cee71dSXin LI */ 72b6cee71dSXin LI #if defined(__osf__) && defined(__DECC) 73b6cee71dSXin LI #ifdef MAP_FAILED 74b6cee71dSXin LI #undef MAP_FAILED 75b6cee71dSXin LI #endif 76b6cee71dSXin LI #endif 77b6cee71dSXin LI 78b6cee71dSXin LI #ifndef MAP_FAILED 79b6cee71dSXin LI #define MAP_FAILED (void *) -1 80b6cee71dSXin LI #endif 81b6cee71dSXin LI 82b6cee71dSXin LI #ifndef MAP_FILE 83b6cee71dSXin LI #define MAP_FILE 0 84b6cee71dSXin LI #endif 85b6cee71dSXin LI 86b6cee71dSXin LI #define ALLOC_CHUNK (size_t)10 87b6cee71dSXin LI #define ALLOC_INCR (size_t)200 88b6cee71dSXin LI 893e41d09dSXin LI #define MAP_TYPE_USER 0 90c2931133SXin LI #define MAP_TYPE_MALLOC 1 913e41d09dSXin LI #define MAP_TYPE_MMAP 2 92c2931133SXin LI 93b6cee71dSXin LI struct magic_entry { 94b6cee71dSXin LI struct magic *mp; 95b6cee71dSXin LI uint32_t cont_count; 96b6cee71dSXin LI uint32_t max_count; 97b6cee71dSXin LI }; 98b6cee71dSXin LI 99b6cee71dSXin LI struct magic_entry_set { 100b6cee71dSXin LI struct magic_entry *me; 101b6cee71dSXin LI uint32_t count; 102b6cee71dSXin LI uint32_t max; 103b6cee71dSXin LI }; 104b6cee71dSXin LI 105b6cee71dSXin LI struct magic_map { 106b6cee71dSXin LI void *p; 107b6cee71dSXin LI size_t len; 108c2931133SXin LI int type; 109b6cee71dSXin LI struct magic *magic[MAGIC_SETS]; 110b6cee71dSXin LI uint32_t nmagic[MAGIC_SETS]; 111b6cee71dSXin LI }; 112b6cee71dSXin LI 113b6cee71dSXin LI int file_formats[FILE_NAMES_SIZE]; 114b6cee71dSXin LI const size_t file_nformats = FILE_NAMES_SIZE; 115b6cee71dSXin LI const char *file_names[FILE_NAMES_SIZE]; 116b6cee71dSXin LI const size_t file_nnames = FILE_NAMES_SIZE; 117b6cee71dSXin LI 118b6cee71dSXin LI private int getvalue(struct magic_set *ms, struct magic *, const char **, int); 119b6cee71dSXin LI private int hextoint(int); 120b6cee71dSXin LI private const char *getstr(struct magic_set *, struct magic *, const char *, 121b6cee71dSXin LI int); 122b6cee71dSXin LI private int parse(struct magic_set *, struct magic_entry *, const char *, 123b6cee71dSXin LI size_t, int); 124b6cee71dSXin LI private void eatsize(const char **); 125b6cee71dSXin LI private int apprentice_1(struct magic_set *, const char *, int); 126b6cee71dSXin LI private size_t apprentice_magic_strength(const struct magic *); 127b6cee71dSXin LI private int apprentice_sort(const void *, const void *); 128b6cee71dSXin LI private void apprentice_list(struct mlist *, int ); 129b6cee71dSXin LI private struct magic_map *apprentice_load(struct magic_set *, 130b6cee71dSXin LI const char *, int); 131b6cee71dSXin LI private struct mlist *mlist_alloc(void); 132b6cee71dSXin LI private void mlist_free(struct mlist *); 133b6cee71dSXin LI private void byteswap(struct magic *, uint32_t); 134b6cee71dSXin LI private void bs1(struct magic *); 135b6cee71dSXin LI private uint16_t swap2(uint16_t); 136b6cee71dSXin LI private uint32_t swap4(uint32_t); 137b6cee71dSXin LI private uint64_t swap8(uint64_t); 138b6cee71dSXin LI private char *mkdbname(struct magic_set *, const char *, int); 139c2931133SXin LI private struct magic_map *apprentice_buf(struct magic_set *, struct magic *, 140c2931133SXin LI size_t); 141b6cee71dSXin LI private struct magic_map *apprentice_map(struct magic_set *, const char *); 142c2931133SXin LI private int check_buffer(struct magic_set *, struct magic_map *, const char *); 143b6cee71dSXin LI private void apprentice_unmap(struct magic_map *); 144b6cee71dSXin LI private int apprentice_compile(struct magic_set *, struct magic_map *, 145b6cee71dSXin LI const char *); 1463e41d09dSXin LI private int check_format_type(const char *, int, const char **); 147b6cee71dSXin LI private int check_format(struct magic_set *, struct magic *); 148b6cee71dSXin LI private int get_op(char); 149b6cee71dSXin LI private int parse_mime(struct magic_set *, struct magic_entry *, const char *); 150b6cee71dSXin LI private int parse_strength(struct magic_set *, struct magic_entry *, const char *); 151b6cee71dSXin LI private int parse_apple(struct magic_set *, struct magic_entry *, const char *); 1525f0216bdSXin LI private int parse_ext(struct magic_set *, struct magic_entry *, const char *); 153b6cee71dSXin LI 154b6cee71dSXin LI 155b6cee71dSXin LI private size_t magicsize = sizeof(struct magic); 156b6cee71dSXin LI 157b6cee71dSXin LI private const char usg_hdr[] = "cont\toffset\ttype\topcode\tmask\tvalue\tdesc"; 158b6cee71dSXin LI 159b6cee71dSXin LI private struct { 160b6cee71dSXin LI const char *name; 161b6cee71dSXin LI size_t len; 162b6cee71dSXin LI int (*fun)(struct magic_set *, struct magic_entry *, const char *); 163b6cee71dSXin LI } bang[] = { 164b6cee71dSXin LI #define DECLARE_FIELD(name) { # name, sizeof(# name) - 1, parse_ ## name } 165b6cee71dSXin LI DECLARE_FIELD(mime), 166b6cee71dSXin LI DECLARE_FIELD(apple), 1675f0216bdSXin LI DECLARE_FIELD(ext), 168b6cee71dSXin LI DECLARE_FIELD(strength), 169b6cee71dSXin LI #undef DECLARE_FIELD 170b6cee71dSXin LI { NULL, 0, NULL } 171b6cee71dSXin LI }; 172b6cee71dSXin LI 173b6cee71dSXin LI #ifdef COMPILE_ONLY 174b6cee71dSXin LI 175b6cee71dSXin LI int main(int, char *[]); 176b6cee71dSXin LI 177b6cee71dSXin LI int 178b6cee71dSXin LI main(int argc, char *argv[]) 179b6cee71dSXin LI { 180b6cee71dSXin LI int ret; 181b6cee71dSXin LI struct magic_set *ms; 182b6cee71dSXin LI char *progname; 183b6cee71dSXin LI 184b6cee71dSXin LI if ((progname = strrchr(argv[0], '/')) != NULL) 185b6cee71dSXin LI progname++; 186b6cee71dSXin LI else 187b6cee71dSXin LI progname = argv[0]; 188b6cee71dSXin LI 189b6cee71dSXin LI if (argc != 2) { 190b6cee71dSXin LI (void)fprintf(stderr, "Usage: %s file\n", progname); 191b6cee71dSXin LI return 1; 192b6cee71dSXin LI } 193b6cee71dSXin LI 194b6cee71dSXin LI if ((ms = magic_open(MAGIC_CHECK)) == NULL) { 195b6cee71dSXin LI (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno)); 196b6cee71dSXin LI return 1; 197b6cee71dSXin LI } 198b6cee71dSXin LI ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0; 199b6cee71dSXin LI if (ret == 1) 200b6cee71dSXin LI (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms)); 201b6cee71dSXin LI magic_close(ms); 202b6cee71dSXin LI return ret; 203b6cee71dSXin LI } 204b6cee71dSXin LI #endif /* COMPILE_ONLY */ 205b6cee71dSXin LI 206b6cee71dSXin LI struct type_tbl_s { 207b6cee71dSXin LI const char name[16]; 208b6cee71dSXin LI const size_t len; 209b6cee71dSXin LI const int type; 210b6cee71dSXin LI const int format; 211b6cee71dSXin LI }; 212b6cee71dSXin LI 213b6cee71dSXin LI /* 214b6cee71dSXin LI * XXX - the actual Single UNIX Specification says that "long" means "long", 215b6cee71dSXin LI * as in the C data type, but we treat it as meaning "4-byte integer". 216b6cee71dSXin LI * Given that the OS X version of file 5.04 did the same, I guess that passes 217b6cee71dSXin LI * the actual test; having "long" be dependent on how big a "long" is on 218b6cee71dSXin LI * the machine running "file" is silly. 219b6cee71dSXin LI */ 220b6cee71dSXin LI static const struct type_tbl_s type_tbl[] = { 221b6cee71dSXin LI # define XX(s) s, (sizeof(s) - 1) 222b6cee71dSXin LI # define XX_NULL "", 0 223b6cee71dSXin LI { XX("invalid"), FILE_INVALID, FILE_FMT_NONE }, 224b6cee71dSXin LI { XX("byte"), FILE_BYTE, FILE_FMT_NUM }, 225b6cee71dSXin LI { XX("short"), FILE_SHORT, FILE_FMT_NUM }, 226b6cee71dSXin LI { XX("default"), FILE_DEFAULT, FILE_FMT_NONE }, 227b6cee71dSXin LI { XX("long"), FILE_LONG, FILE_FMT_NUM }, 228b6cee71dSXin LI { XX("string"), FILE_STRING, FILE_FMT_STR }, 229b6cee71dSXin LI { XX("date"), FILE_DATE, FILE_FMT_STR }, 230b6cee71dSXin LI { XX("beshort"), FILE_BESHORT, FILE_FMT_NUM }, 231b6cee71dSXin LI { XX("belong"), FILE_BELONG, FILE_FMT_NUM }, 232b6cee71dSXin LI { XX("bedate"), FILE_BEDATE, FILE_FMT_STR }, 233b6cee71dSXin LI { XX("leshort"), FILE_LESHORT, FILE_FMT_NUM }, 234b6cee71dSXin LI { XX("lelong"), FILE_LELONG, FILE_FMT_NUM }, 235b6cee71dSXin LI { XX("ledate"), FILE_LEDATE, FILE_FMT_STR }, 236b6cee71dSXin LI { XX("pstring"), FILE_PSTRING, FILE_FMT_STR }, 237b6cee71dSXin LI { XX("ldate"), FILE_LDATE, FILE_FMT_STR }, 238b6cee71dSXin LI { XX("beldate"), FILE_BELDATE, FILE_FMT_STR }, 239b6cee71dSXin LI { XX("leldate"), FILE_LELDATE, FILE_FMT_STR }, 240b6cee71dSXin LI { XX("regex"), FILE_REGEX, FILE_FMT_STR }, 241b6cee71dSXin LI { XX("bestring16"), FILE_BESTRING16, FILE_FMT_STR }, 242b6cee71dSXin LI { XX("lestring16"), FILE_LESTRING16, FILE_FMT_STR }, 243b6cee71dSXin LI { XX("search"), FILE_SEARCH, FILE_FMT_STR }, 244b6cee71dSXin LI { XX("medate"), FILE_MEDATE, FILE_FMT_STR }, 245b6cee71dSXin LI { XX("meldate"), FILE_MELDATE, FILE_FMT_STR }, 246b6cee71dSXin LI { XX("melong"), FILE_MELONG, FILE_FMT_NUM }, 247b6cee71dSXin LI { XX("quad"), FILE_QUAD, FILE_FMT_QUAD }, 248b6cee71dSXin LI { XX("lequad"), FILE_LEQUAD, FILE_FMT_QUAD }, 249b6cee71dSXin LI { XX("bequad"), FILE_BEQUAD, FILE_FMT_QUAD }, 250b6cee71dSXin LI { XX("qdate"), FILE_QDATE, FILE_FMT_STR }, 251b6cee71dSXin LI { XX("leqdate"), FILE_LEQDATE, FILE_FMT_STR }, 252b6cee71dSXin LI { XX("beqdate"), FILE_BEQDATE, FILE_FMT_STR }, 253b6cee71dSXin LI { XX("qldate"), FILE_QLDATE, FILE_FMT_STR }, 254b6cee71dSXin LI { XX("leqldate"), FILE_LEQLDATE, FILE_FMT_STR }, 255b6cee71dSXin LI { XX("beqldate"), FILE_BEQLDATE, FILE_FMT_STR }, 256b6cee71dSXin LI { XX("float"), FILE_FLOAT, FILE_FMT_FLOAT }, 257b6cee71dSXin LI { XX("befloat"), FILE_BEFLOAT, FILE_FMT_FLOAT }, 258b6cee71dSXin LI { XX("lefloat"), FILE_LEFLOAT, FILE_FMT_FLOAT }, 259b6cee71dSXin LI { XX("double"), FILE_DOUBLE, FILE_FMT_DOUBLE }, 260b6cee71dSXin LI { XX("bedouble"), FILE_BEDOUBLE, FILE_FMT_DOUBLE }, 261b6cee71dSXin LI { XX("ledouble"), FILE_LEDOUBLE, FILE_FMT_DOUBLE }, 262b6cee71dSXin LI { XX("leid3"), FILE_LEID3, FILE_FMT_NUM }, 263b6cee71dSXin LI { XX("beid3"), FILE_BEID3, FILE_FMT_NUM }, 264b6cee71dSXin LI { XX("indirect"), FILE_INDIRECT, FILE_FMT_NUM }, 265b6cee71dSXin LI { XX("qwdate"), FILE_QWDATE, FILE_FMT_STR }, 266b6cee71dSXin LI { XX("leqwdate"), FILE_LEQWDATE, FILE_FMT_STR }, 267b6cee71dSXin LI { XX("beqwdate"), FILE_BEQWDATE, FILE_FMT_STR }, 268b6cee71dSXin LI { XX("name"), FILE_NAME, FILE_FMT_NONE }, 269b6cee71dSXin LI { XX("use"), FILE_USE, FILE_FMT_NONE }, 270b6cee71dSXin LI { XX("clear"), FILE_CLEAR, FILE_FMT_NONE }, 2713e41d09dSXin LI { XX("der"), FILE_DER, FILE_FMT_STR }, 272b6cee71dSXin LI { XX_NULL, FILE_INVALID, FILE_FMT_NONE }, 273b6cee71dSXin LI }; 274b6cee71dSXin LI 275b6cee71dSXin LI /* 276b6cee71dSXin LI * These are not types, and cannot be preceded by "u" to make them 277b6cee71dSXin LI * unsigned. 278b6cee71dSXin LI */ 279b6cee71dSXin LI static const struct type_tbl_s special_tbl[] = { 2803e41d09dSXin LI { XX("der"), FILE_DER, FILE_FMT_STR }, 281b6cee71dSXin LI { XX("name"), FILE_NAME, FILE_FMT_STR }, 282b6cee71dSXin LI { XX("use"), FILE_USE, FILE_FMT_STR }, 283b6cee71dSXin LI { XX_NULL, FILE_INVALID, FILE_FMT_NONE }, 284b6cee71dSXin LI }; 285b6cee71dSXin LI # undef XX 286b6cee71dSXin LI # undef XX_NULL 287b6cee71dSXin LI 288b6cee71dSXin LI private int 289b6cee71dSXin LI get_type(const struct type_tbl_s *tbl, const char *l, const char **t) 290b6cee71dSXin LI { 291b6cee71dSXin LI const struct type_tbl_s *p; 292b6cee71dSXin LI 293b6cee71dSXin LI for (p = tbl; p->len; p++) { 294b6cee71dSXin LI if (strncmp(l, p->name, p->len) == 0) { 295b6cee71dSXin LI if (t) 296b6cee71dSXin LI *t = l + p->len; 297b6cee71dSXin LI break; 298b6cee71dSXin LI } 299b6cee71dSXin LI } 300b6cee71dSXin LI return p->type; 301b6cee71dSXin LI } 302b6cee71dSXin LI 303b6cee71dSXin LI private int 304b6cee71dSXin LI get_standard_integer_type(const char *l, const char **t) 305b6cee71dSXin LI { 306b6cee71dSXin LI int type; 307b6cee71dSXin LI 308b6cee71dSXin LI if (isalpha((unsigned char)l[1])) { 309b6cee71dSXin LI switch (l[1]) { 310b6cee71dSXin LI case 'C': 311b6cee71dSXin LI /* "dC" and "uC" */ 312b6cee71dSXin LI type = FILE_BYTE; 313b6cee71dSXin LI break; 314b6cee71dSXin LI case 'S': 315b6cee71dSXin LI /* "dS" and "uS" */ 316b6cee71dSXin LI type = FILE_SHORT; 317b6cee71dSXin LI break; 318b6cee71dSXin LI case 'I': 319b6cee71dSXin LI case 'L': 320b6cee71dSXin LI /* 321b6cee71dSXin LI * "dI", "dL", "uI", and "uL". 322b6cee71dSXin LI * 323b6cee71dSXin LI * XXX - the actual Single UNIX Specification says 324b6cee71dSXin LI * that "L" means "long", as in the C data type, 325b6cee71dSXin LI * but we treat it as meaning "4-byte integer". 326b6cee71dSXin LI * Given that the OS X version of file 5.04 did 327b6cee71dSXin LI * the same, I guess that passes the actual SUS 328b6cee71dSXin LI * validation suite; having "dL" be dependent on 329b6cee71dSXin LI * how big a "long" is on the machine running 330b6cee71dSXin LI * "file" is silly. 331b6cee71dSXin LI */ 332b6cee71dSXin LI type = FILE_LONG; 333b6cee71dSXin LI break; 334b6cee71dSXin LI case 'Q': 335b6cee71dSXin LI /* "dQ" and "uQ" */ 336b6cee71dSXin LI type = FILE_QUAD; 337b6cee71dSXin LI break; 338b6cee71dSXin LI default: 339b6cee71dSXin LI /* "d{anything else}", "u{anything else}" */ 340b6cee71dSXin LI return FILE_INVALID; 341b6cee71dSXin LI } 342b6cee71dSXin LI l += 2; 343b6cee71dSXin LI } else if (isdigit((unsigned char)l[1])) { 344b6cee71dSXin LI /* 345b6cee71dSXin LI * "d{num}" and "u{num}"; we only support {num} values 346b6cee71dSXin LI * of 1, 2, 4, and 8 - the Single UNIX Specification 347b6cee71dSXin LI * doesn't say anything about whether arbitrary 348b6cee71dSXin LI * values should be supported, but both the Solaris 10 349b6cee71dSXin LI * and OS X Mountain Lion versions of file passed the 350b6cee71dSXin LI * Single UNIX Specification validation suite, and 351b6cee71dSXin LI * neither of them support values bigger than 8 or 352b6cee71dSXin LI * non-power-of-2 values. 353b6cee71dSXin LI */ 354b6cee71dSXin LI if (isdigit((unsigned char)l[2])) { 355b6cee71dSXin LI /* Multi-digit, so > 9 */ 356b6cee71dSXin LI return FILE_INVALID; 357b6cee71dSXin LI } 358b6cee71dSXin LI switch (l[1]) { 359b6cee71dSXin LI case '1': 360b6cee71dSXin LI type = FILE_BYTE; 361b6cee71dSXin LI break; 362b6cee71dSXin LI case '2': 363b6cee71dSXin LI type = FILE_SHORT; 364b6cee71dSXin LI break; 365b6cee71dSXin LI case '4': 366b6cee71dSXin LI type = FILE_LONG; 367b6cee71dSXin LI break; 368b6cee71dSXin LI case '8': 369b6cee71dSXin LI type = FILE_QUAD; 370b6cee71dSXin LI break; 371b6cee71dSXin LI default: 372b6cee71dSXin LI /* XXX - what about 3, 5, 6, or 7? */ 373b6cee71dSXin LI return FILE_INVALID; 374b6cee71dSXin LI } 375b6cee71dSXin LI l += 2; 376b6cee71dSXin LI } else { 377b6cee71dSXin LI /* 378b6cee71dSXin LI * "d" or "u" by itself. 379b6cee71dSXin LI */ 380b6cee71dSXin LI type = FILE_LONG; 381b6cee71dSXin LI ++l; 382b6cee71dSXin LI } 383b6cee71dSXin LI if (t) 384b6cee71dSXin LI *t = l; 385b6cee71dSXin LI return type; 386b6cee71dSXin LI } 387b6cee71dSXin LI 388b6cee71dSXin LI private void 389b6cee71dSXin LI init_file_tables(void) 390b6cee71dSXin LI { 391b6cee71dSXin LI static int done = 0; 392b6cee71dSXin LI const struct type_tbl_s *p; 393b6cee71dSXin LI 394b6cee71dSXin LI if (done) 395b6cee71dSXin LI return; 396b6cee71dSXin LI done++; 397b6cee71dSXin LI 398b6cee71dSXin LI for (p = type_tbl; p->len; p++) { 399b6cee71dSXin LI assert(p->type < FILE_NAMES_SIZE); 400b6cee71dSXin LI file_names[p->type] = p->name; 401b6cee71dSXin LI file_formats[p->type] = p->format; 402b6cee71dSXin LI } 403b6cee71dSXin LI assert(p - type_tbl == FILE_NAMES_SIZE); 404b6cee71dSXin LI } 405b6cee71dSXin LI 406b6cee71dSXin LI private int 407b6cee71dSXin LI add_mlist(struct mlist *mlp, struct magic_map *map, size_t idx) 408b6cee71dSXin LI { 409b6cee71dSXin LI struct mlist *ml; 410b6cee71dSXin LI 411*a5d223e6SXin LI mlp->map = NULL; 412b6cee71dSXin LI if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL) 413b6cee71dSXin LI return -1; 414b6cee71dSXin LI 415*a5d223e6SXin LI ml->map = idx == 0 ? map : NULL; 416b6cee71dSXin LI ml->magic = map->magic[idx]; 417b6cee71dSXin LI ml->nmagic = map->nmagic[idx]; 418b6cee71dSXin LI 419b6cee71dSXin LI mlp->prev->next = ml; 420b6cee71dSXin LI ml->prev = mlp->prev; 421b6cee71dSXin LI ml->next = mlp; 422b6cee71dSXin LI mlp->prev = ml; 423b6cee71dSXin LI return 0; 424b6cee71dSXin LI } 425b6cee71dSXin LI 426b6cee71dSXin LI /* 427b6cee71dSXin LI * Handle one file or directory. 428b6cee71dSXin LI */ 429b6cee71dSXin LI private int 430b6cee71dSXin LI apprentice_1(struct magic_set *ms, const char *fn, int action) 431b6cee71dSXin LI { 432b6cee71dSXin LI struct magic_map *map; 433b6cee71dSXin LI #ifndef COMPILE_ONLY 434c2931133SXin LI struct mlist *ml; 435b6cee71dSXin LI size_t i; 436c2931133SXin LI #endif 437b6cee71dSXin LI 438b6cee71dSXin LI if (magicsize != FILE_MAGICSIZE) { 439b6cee71dSXin LI file_error(ms, 0, "magic element size %lu != %lu", 440b6cee71dSXin LI (unsigned long)sizeof(*map->magic[0]), 441b6cee71dSXin LI (unsigned long)FILE_MAGICSIZE); 442b6cee71dSXin LI return -1; 443b6cee71dSXin LI } 444b6cee71dSXin LI 445b6cee71dSXin LI if (action == FILE_COMPILE) { 446b6cee71dSXin LI map = apprentice_load(ms, fn, action); 447b6cee71dSXin LI if (map == NULL) 448b6cee71dSXin LI return -1; 449b6cee71dSXin LI return apprentice_compile(ms, map, fn); 450b6cee71dSXin LI } 451b6cee71dSXin LI 452b6cee71dSXin LI #ifndef COMPILE_ONLY 453b6cee71dSXin LI map = apprentice_map(ms, fn); 454*a5d223e6SXin LI if (map == (struct magic_map *)-1) 455*a5d223e6SXin LI return -1; 456b6cee71dSXin LI if (map == NULL) { 457b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 458b6cee71dSXin LI file_magwarn(ms, "using regular magic file `%s'", fn); 459b6cee71dSXin LI map = apprentice_load(ms, fn, action); 460b6cee71dSXin LI if (map == NULL) 461b6cee71dSXin LI return -1; 462b6cee71dSXin LI } 463b6cee71dSXin LI 464b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 465b6cee71dSXin LI if (add_mlist(ms->mlist[i], map, i) == -1) { 466b6cee71dSXin LI file_oomem(ms, sizeof(*ml)); 467*a5d223e6SXin LI return -1; 468b6cee71dSXin LI } 469b6cee71dSXin LI } 470b6cee71dSXin LI 471b6cee71dSXin LI if (action == FILE_LIST) { 472b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 473c2931133SXin LI printf("Set %" SIZE_T_FORMAT "u:\nBinary patterns:\n", 474c2931133SXin LI i); 475b6cee71dSXin LI apprentice_list(ms->mlist[i], BINTEST); 476b6cee71dSXin LI printf("Text patterns:\n"); 477b6cee71dSXin LI apprentice_list(ms->mlist[i], TEXTTEST); 478b6cee71dSXin LI } 479b6cee71dSXin LI } 480b6cee71dSXin LI return 0; 481c2931133SXin LI #else 482c2931133SXin LI return 0; 483c2931133SXin LI #endif /* COMPILE_ONLY */ 484b6cee71dSXin LI } 485b6cee71dSXin LI 486b6cee71dSXin LI protected void 487b6cee71dSXin LI file_ms_free(struct magic_set *ms) 488b6cee71dSXin LI { 489b6cee71dSXin LI size_t i; 490b6cee71dSXin LI if (ms == NULL) 491b6cee71dSXin LI return; 492b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) 493b6cee71dSXin LI mlist_free(ms->mlist[i]); 494b6cee71dSXin LI free(ms->o.pbuf); 495b6cee71dSXin LI free(ms->o.buf); 496b6cee71dSXin LI free(ms->c.li); 497b6cee71dSXin LI free(ms); 498b6cee71dSXin LI } 499b6cee71dSXin LI 500b6cee71dSXin LI protected struct magic_set * 501b6cee71dSXin LI file_ms_alloc(int flags) 502b6cee71dSXin LI { 503b6cee71dSXin LI struct magic_set *ms; 504b6cee71dSXin LI size_t i, len; 505b6cee71dSXin LI 506b6cee71dSXin LI if ((ms = CAST(struct magic_set *, calloc((size_t)1, 507b6cee71dSXin LI sizeof(struct magic_set)))) == NULL) 508b6cee71dSXin LI return NULL; 509b6cee71dSXin LI 510b6cee71dSXin LI if (magic_setflags(ms, flags) == -1) { 511b6cee71dSXin LI errno = EINVAL; 512b6cee71dSXin LI goto free; 513b6cee71dSXin LI } 514b6cee71dSXin LI 515b6cee71dSXin LI ms->o.buf = ms->o.pbuf = NULL; 516b6cee71dSXin LI len = (ms->c.len = 10) * sizeof(*ms->c.li); 517b6cee71dSXin LI 518b6cee71dSXin LI if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL) 519b6cee71dSXin LI goto free; 520b6cee71dSXin LI 521b6cee71dSXin LI ms->event_flags = 0; 522b6cee71dSXin LI ms->error = -1; 523b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) 524b6cee71dSXin LI ms->mlist[i] = NULL; 525b6cee71dSXin LI ms->file = "unknown"; 526b6cee71dSXin LI ms->line = 0; 527c2931133SXin LI ms->indir_max = FILE_INDIR_MAX; 528c2931133SXin LI ms->name_max = FILE_NAME_MAX; 529c2931133SXin LI ms->elf_shnum_max = FILE_ELF_SHNUM_MAX; 530c2931133SXin LI ms->elf_phnum_max = FILE_ELF_PHNUM_MAX; 5314460e5b0SXin LI ms->elf_notes_max = FILE_ELF_NOTES_MAX; 5329ce06829SXin LI ms->regex_max = FILE_REGEX_MAX; 5333e41d09dSXin LI ms->bytes_max = FILE_BYTES_MAX; 534b6cee71dSXin LI return ms; 535b6cee71dSXin LI free: 536b6cee71dSXin LI free(ms); 537b6cee71dSXin LI return NULL; 538b6cee71dSXin LI } 539b6cee71dSXin LI 540b6cee71dSXin LI private void 541b6cee71dSXin LI apprentice_unmap(struct magic_map *map) 542b6cee71dSXin LI { 5439ce06829SXin LI size_t i; 544b6cee71dSXin LI if (map == NULL) 545b6cee71dSXin LI return; 546c2931133SXin LI 547c2931133SXin LI switch (map->type) { 5483e41d09dSXin LI case MAP_TYPE_USER: 5493e41d09dSXin LI break; 5503e41d09dSXin LI case MAP_TYPE_MALLOC: 5513e41d09dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 5523e41d09dSXin LI if ((char *)map->magic[i] >= (char *)map->p && 553*a5d223e6SXin LI (char *)map->magic[i] <= (char *)map->p + map->len) 5543e41d09dSXin LI continue; 5553e41d09dSXin LI free(map->magic[i]); 5563e41d09dSXin LI } 5573e41d09dSXin LI free(map->p); 5583e41d09dSXin LI break; 559b6cee71dSXin LI #ifdef QUICK 560c2931133SXin LI case MAP_TYPE_MMAP: 5613e41d09dSXin LI if (map->p && map->p != MAP_FAILED) 562b6cee71dSXin LI (void)munmap(map->p, map->len); 563c2931133SXin LI break; 564b6cee71dSXin LI #endif 565c2931133SXin LI default: 566c2931133SXin LI abort(); 567b6cee71dSXin LI } 568b6cee71dSXin LI free(map); 569b6cee71dSXin LI } 570b6cee71dSXin LI 571b6cee71dSXin LI private struct mlist * 572b6cee71dSXin LI mlist_alloc(void) 573b6cee71dSXin LI { 574b6cee71dSXin LI struct mlist *mlist; 575b6cee71dSXin LI if ((mlist = CAST(struct mlist *, calloc(1, sizeof(*mlist)))) == NULL) { 576b6cee71dSXin LI return NULL; 577b6cee71dSXin LI } 578b6cee71dSXin LI mlist->next = mlist->prev = mlist; 579b6cee71dSXin LI return mlist; 580b6cee71dSXin LI } 581b6cee71dSXin LI 582b6cee71dSXin LI private void 583b6cee71dSXin LI mlist_free(struct mlist *mlist) 584b6cee71dSXin LI { 585c2931133SXin LI struct mlist *ml, *next; 586b6cee71dSXin LI 587b6cee71dSXin LI if (mlist == NULL) 588b6cee71dSXin LI return; 589b6cee71dSXin LI 590c2931133SXin LI ml = mlist->next; 591c2931133SXin LI for (ml = mlist->next; (next = ml->next) != NULL; ml = next) { 592b6cee71dSXin LI if (ml->map) 593*a5d223e6SXin LI apprentice_unmap(CAST(struct magic_map *, ml->map)); 594b6cee71dSXin LI free(ml); 595c2931133SXin LI if (ml == mlist) 596c2931133SXin LI break; 597b6cee71dSXin LI } 598b6cee71dSXin LI } 599b6cee71dSXin LI 600c2931133SXin LI #ifndef COMPILE_ONLY 601c2931133SXin LI /* void **bufs: an array of compiled magic files */ 602c2931133SXin LI protected int 603c2931133SXin LI buffer_apprentice(struct magic_set *ms, struct magic **bufs, 604c2931133SXin LI size_t *sizes, size_t nbufs) 605c2931133SXin LI { 606c2931133SXin LI size_t i, j; 607c2931133SXin LI struct mlist *ml; 608c2931133SXin LI struct magic_map *map; 609c2931133SXin LI 610c2931133SXin LI if (nbufs == 0) 611c2931133SXin LI return -1; 612c2931133SXin LI 613c2931133SXin LI if (ms->mlist[0] != NULL) 614c2931133SXin LI file_reset(ms); 615c2931133SXin LI 616c2931133SXin LI init_file_tables(); 617c2931133SXin LI 618c2931133SXin LI for (i = 0; i < MAGIC_SETS; i++) { 619c2931133SXin LI mlist_free(ms->mlist[i]); 620c2931133SXin LI if ((ms->mlist[i] = mlist_alloc()) == NULL) { 621c2931133SXin LI file_oomem(ms, sizeof(*ms->mlist[i])); 622c2931133SXin LI goto fail; 623c2931133SXin LI } 624c2931133SXin LI } 625c2931133SXin LI 626c2931133SXin LI for (i = 0; i < nbufs; i++) { 627c2931133SXin LI map = apprentice_buf(ms, bufs[i], sizes[i]); 628c2931133SXin LI if (map == NULL) 629c2931133SXin LI goto fail; 630c2931133SXin LI 631c2931133SXin LI for (j = 0; j < MAGIC_SETS; j++) { 632c2931133SXin LI if (add_mlist(ms->mlist[j], map, j) == -1) { 633c2931133SXin LI file_oomem(ms, sizeof(*ml)); 634c2931133SXin LI goto fail; 635c2931133SXin LI } 636c2931133SXin LI } 637c2931133SXin LI } 638c2931133SXin LI 639c2931133SXin LI return 0; 640c2931133SXin LI fail: 641c2931133SXin LI for (i = 0; i < MAGIC_SETS; i++) { 642c2931133SXin LI mlist_free(ms->mlist[i]); 643c2931133SXin LI ms->mlist[i] = NULL; 644c2931133SXin LI } 645c2931133SXin LI return -1; 646c2931133SXin LI } 647c2931133SXin LI #endif 648c2931133SXin LI 649b6cee71dSXin LI /* const char *fn: list of magic files and directories */ 650b6cee71dSXin LI protected int 651b6cee71dSXin LI file_apprentice(struct magic_set *ms, const char *fn, int action) 652b6cee71dSXin LI { 653b6cee71dSXin LI char *p, *mfn; 654b6cee71dSXin LI int file_err, errs = -1; 655b6cee71dSXin LI size_t i; 656b6cee71dSXin LI 657b6cee71dSXin LI if (ms->mlist[0] != NULL) 658b6cee71dSXin LI file_reset(ms); 659b6cee71dSXin LI 660b6cee71dSXin LI if ((fn = magic_getpath(fn, action)) == NULL) 661b6cee71dSXin LI return -1; 662b6cee71dSXin LI 663b6cee71dSXin LI init_file_tables(); 664b6cee71dSXin LI 665b6cee71dSXin LI if ((mfn = strdup(fn)) == NULL) { 666b6cee71dSXin LI file_oomem(ms, strlen(fn)); 667b6cee71dSXin LI return -1; 668b6cee71dSXin LI } 669b6cee71dSXin LI 670b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 671b6cee71dSXin LI mlist_free(ms->mlist[i]); 672b6cee71dSXin LI if ((ms->mlist[i] = mlist_alloc()) == NULL) { 673b6cee71dSXin LI file_oomem(ms, sizeof(*ms->mlist[i])); 674c2931133SXin LI while (i-- > 0) { 675b6cee71dSXin LI mlist_free(ms->mlist[i]); 676c2931133SXin LI ms->mlist[i] = NULL; 677b6cee71dSXin LI } 678b6cee71dSXin LI free(mfn); 679b6cee71dSXin LI return -1; 680b6cee71dSXin LI } 681b6cee71dSXin LI } 682b6cee71dSXin LI fn = mfn; 683b6cee71dSXin LI 684b6cee71dSXin LI while (fn) { 685b6cee71dSXin LI p = strchr(fn, PATHSEP); 686b6cee71dSXin LI if (p) 687b6cee71dSXin LI *p++ = '\0'; 688b6cee71dSXin LI if (*fn == '\0') 689b6cee71dSXin LI break; 690b6cee71dSXin LI file_err = apprentice_1(ms, fn, action); 691b6cee71dSXin LI errs = MAX(errs, file_err); 692b6cee71dSXin LI fn = p; 693b6cee71dSXin LI } 694b6cee71dSXin LI 695b6cee71dSXin LI free(mfn); 696b6cee71dSXin LI 697b6cee71dSXin LI if (errs == -1) { 698b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 699b6cee71dSXin LI mlist_free(ms->mlist[i]); 700b6cee71dSXin LI ms->mlist[i] = NULL; 701b6cee71dSXin LI } 702b6cee71dSXin LI file_error(ms, 0, "could not find any valid magic files!"); 703b6cee71dSXin LI return -1; 704b6cee71dSXin LI } 705b6cee71dSXin LI 706b6cee71dSXin LI #if 0 707b6cee71dSXin LI /* 708b6cee71dSXin LI * Always leave the database loaded 709b6cee71dSXin LI */ 710b6cee71dSXin LI if (action == FILE_LOAD) 711b6cee71dSXin LI return 0; 712b6cee71dSXin LI 713b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 714b6cee71dSXin LI mlist_free(ms->mlist[i]); 715b6cee71dSXin LI ms->mlist[i] = NULL; 716b6cee71dSXin LI } 717b6cee71dSXin LI #endif 718b6cee71dSXin LI 719b6cee71dSXin LI switch (action) { 720b6cee71dSXin LI case FILE_LOAD: 721b6cee71dSXin LI case FILE_COMPILE: 722b6cee71dSXin LI case FILE_CHECK: 723b6cee71dSXin LI case FILE_LIST: 724b6cee71dSXin LI return 0; 725b6cee71dSXin LI default: 726b6cee71dSXin LI file_error(ms, 0, "Invalid action %d", action); 727b6cee71dSXin LI return -1; 728b6cee71dSXin LI } 729b6cee71dSXin LI } 730b6cee71dSXin LI 731b6cee71dSXin LI /* 732b6cee71dSXin LI * Compute the real length of a magic expression, for the purposes 733b6cee71dSXin LI * of determining how "strong" a magic expression is (approximating 734b6cee71dSXin LI * how specific its matches are): 735b6cee71dSXin LI * - magic characters count 0 unless escaped. 736b6cee71dSXin LI * - [] expressions count 1 737b6cee71dSXin LI * - {} expressions count 0 738b6cee71dSXin LI * - regular characters or escaped magic characters count 1 739b6cee71dSXin LI * - 0 length expressions count as one 740b6cee71dSXin LI */ 741b6cee71dSXin LI private size_t 742b6cee71dSXin LI nonmagic(const char *str) 743b6cee71dSXin LI { 744b6cee71dSXin LI const char *p; 745b6cee71dSXin LI size_t rv = 0; 746b6cee71dSXin LI 747b6cee71dSXin LI for (p = str; *p; p++) 748b6cee71dSXin LI switch (*p) { 749b6cee71dSXin LI case '\\': /* Escaped anything counts 1 */ 750b6cee71dSXin LI if (!*++p) 751b6cee71dSXin LI p--; 752b6cee71dSXin LI rv++; 753b6cee71dSXin LI continue; 754b6cee71dSXin LI case '?': /* Magic characters count 0 */ 755b6cee71dSXin LI case '*': 756b6cee71dSXin LI case '.': 757b6cee71dSXin LI case '+': 758b6cee71dSXin LI case '^': 759b6cee71dSXin LI case '$': 760b6cee71dSXin LI continue; 761b6cee71dSXin LI case '[': /* Bracketed expressions count 1 the ']' */ 762b6cee71dSXin LI while (*p && *p != ']') 763b6cee71dSXin LI p++; 764b6cee71dSXin LI p--; 765b6cee71dSXin LI continue; 766b6cee71dSXin LI case '{': /* Braced expressions count 0 */ 767b6cee71dSXin LI while (*p && *p != '}') 768b6cee71dSXin LI p++; 769b6cee71dSXin LI if (!*p) 770b6cee71dSXin LI p--; 771b6cee71dSXin LI continue; 772b6cee71dSXin LI default: /* Anything else counts 1 */ 773b6cee71dSXin LI rv++; 774b6cee71dSXin LI continue; 775b6cee71dSXin LI } 776b6cee71dSXin LI 777b6cee71dSXin LI return rv == 0 ? 1 : rv; /* Return at least 1 */ 778b6cee71dSXin LI } 779b6cee71dSXin LI 780b6cee71dSXin LI /* 781b6cee71dSXin LI * Get weight of this magic entry, for sorting purposes. 782b6cee71dSXin LI */ 783b6cee71dSXin LI private size_t 784b6cee71dSXin LI apprentice_magic_strength(const struct magic *m) 785b6cee71dSXin LI { 786b6cee71dSXin LI #define MULT 10 787b6cee71dSXin LI size_t v, val = 2 * MULT; /* baseline strength */ 788b6cee71dSXin LI 789b6cee71dSXin LI switch (m->type) { 790b6cee71dSXin LI case FILE_DEFAULT: /* make sure this sorts last */ 791b6cee71dSXin LI if (m->factor_op != FILE_FACTOR_OP_NONE) 792b6cee71dSXin LI abort(); 793b6cee71dSXin LI return 0; 794b6cee71dSXin LI 795b6cee71dSXin LI case FILE_BYTE: 796b6cee71dSXin LI val += 1 * MULT; 797b6cee71dSXin LI break; 798b6cee71dSXin LI 799b6cee71dSXin LI case FILE_SHORT: 800b6cee71dSXin LI case FILE_LESHORT: 801b6cee71dSXin LI case FILE_BESHORT: 802b6cee71dSXin LI val += 2 * MULT; 803b6cee71dSXin LI break; 804b6cee71dSXin LI 805b6cee71dSXin LI case FILE_LONG: 806b6cee71dSXin LI case FILE_LELONG: 807b6cee71dSXin LI case FILE_BELONG: 808b6cee71dSXin LI case FILE_MELONG: 809b6cee71dSXin LI val += 4 * MULT; 810b6cee71dSXin LI break; 811b6cee71dSXin LI 812b6cee71dSXin LI case FILE_PSTRING: 813b6cee71dSXin LI case FILE_STRING: 814b6cee71dSXin LI val += m->vallen * MULT; 815b6cee71dSXin LI break; 816b6cee71dSXin LI 817b6cee71dSXin LI case FILE_BESTRING16: 818b6cee71dSXin LI case FILE_LESTRING16: 819b6cee71dSXin LI val += m->vallen * MULT / 2; 820b6cee71dSXin LI break; 821b6cee71dSXin LI 822b6cee71dSXin LI case FILE_SEARCH: 823b6cee71dSXin LI val += m->vallen * MAX(MULT / m->vallen, 1); 824b6cee71dSXin LI break; 825b6cee71dSXin LI 826b6cee71dSXin LI case FILE_REGEX: 827b6cee71dSXin LI v = nonmagic(m->value.s); 828b6cee71dSXin LI val += v * MAX(MULT / v, 1); 829b6cee71dSXin LI break; 830b6cee71dSXin LI 831b6cee71dSXin LI case FILE_DATE: 832b6cee71dSXin LI case FILE_LEDATE: 833b6cee71dSXin LI case FILE_BEDATE: 834b6cee71dSXin LI case FILE_MEDATE: 835b6cee71dSXin LI case FILE_LDATE: 836b6cee71dSXin LI case FILE_LELDATE: 837b6cee71dSXin LI case FILE_BELDATE: 838b6cee71dSXin LI case FILE_MELDATE: 839b6cee71dSXin LI case FILE_FLOAT: 840b6cee71dSXin LI case FILE_BEFLOAT: 841b6cee71dSXin LI case FILE_LEFLOAT: 842b6cee71dSXin LI val += 4 * MULT; 843b6cee71dSXin LI break; 844b6cee71dSXin LI 845b6cee71dSXin LI case FILE_QUAD: 846b6cee71dSXin LI case FILE_BEQUAD: 847b6cee71dSXin LI case FILE_LEQUAD: 848b6cee71dSXin LI case FILE_QDATE: 849b6cee71dSXin LI case FILE_LEQDATE: 850b6cee71dSXin LI case FILE_BEQDATE: 851b6cee71dSXin LI case FILE_QLDATE: 852b6cee71dSXin LI case FILE_LEQLDATE: 853b6cee71dSXin LI case FILE_BEQLDATE: 854b6cee71dSXin LI case FILE_QWDATE: 855b6cee71dSXin LI case FILE_LEQWDATE: 856b6cee71dSXin LI case FILE_BEQWDATE: 857b6cee71dSXin LI case FILE_DOUBLE: 858b6cee71dSXin LI case FILE_BEDOUBLE: 859b6cee71dSXin LI case FILE_LEDOUBLE: 860b6cee71dSXin LI val += 8 * MULT; 861b6cee71dSXin LI break; 862b6cee71dSXin LI 863b6cee71dSXin LI case FILE_INDIRECT: 864b6cee71dSXin LI case FILE_NAME: 865b6cee71dSXin LI case FILE_USE: 866b6cee71dSXin LI break; 867b6cee71dSXin LI 8683e41d09dSXin LI case FILE_DER: 8693e41d09dSXin LI val += MULT; 8703e41d09dSXin LI break; 8713e41d09dSXin LI 872b6cee71dSXin LI default: 873b6cee71dSXin LI (void)fprintf(stderr, "Bad type %d\n", m->type); 874b6cee71dSXin LI abort(); 875b6cee71dSXin LI } 876b6cee71dSXin LI 877b6cee71dSXin LI switch (m->reln) { 878b6cee71dSXin LI case 'x': /* matches anything penalize */ 879b6cee71dSXin LI case '!': /* matches almost anything penalize */ 880b6cee71dSXin LI val = 0; 881b6cee71dSXin LI break; 882b6cee71dSXin LI 883b6cee71dSXin LI case '=': /* Exact match, prefer */ 884b6cee71dSXin LI val += MULT; 885b6cee71dSXin LI break; 886b6cee71dSXin LI 887b6cee71dSXin LI case '>': 888b6cee71dSXin LI case '<': /* comparison match reduce strength */ 889b6cee71dSXin LI val -= 2 * MULT; 890b6cee71dSXin LI break; 891b6cee71dSXin LI 892b6cee71dSXin LI case '^': 893b6cee71dSXin LI case '&': /* masking bits, we could count them too */ 894b6cee71dSXin LI val -= MULT; 895b6cee71dSXin LI break; 896b6cee71dSXin LI 897b6cee71dSXin LI default: 898b6cee71dSXin LI (void)fprintf(stderr, "Bad relation %c\n", m->reln); 899b6cee71dSXin LI abort(); 900b6cee71dSXin LI } 901b6cee71dSXin LI 902b6cee71dSXin LI if (val == 0) /* ensure we only return 0 for FILE_DEFAULT */ 903b6cee71dSXin LI val = 1; 904b6cee71dSXin LI 905b6cee71dSXin LI switch (m->factor_op) { 906b6cee71dSXin LI case FILE_FACTOR_OP_NONE: 907b6cee71dSXin LI break; 908b6cee71dSXin LI case FILE_FACTOR_OP_PLUS: 909b6cee71dSXin LI val += m->factor; 910b6cee71dSXin LI break; 911b6cee71dSXin LI case FILE_FACTOR_OP_MINUS: 912b6cee71dSXin LI val -= m->factor; 913b6cee71dSXin LI break; 914b6cee71dSXin LI case FILE_FACTOR_OP_TIMES: 915b6cee71dSXin LI val *= m->factor; 916b6cee71dSXin LI break; 917b6cee71dSXin LI case FILE_FACTOR_OP_DIV: 918b6cee71dSXin LI val /= m->factor; 919b6cee71dSXin LI break; 920b6cee71dSXin LI default: 921b6cee71dSXin LI abort(); 922b6cee71dSXin LI } 923b6cee71dSXin LI 924b6cee71dSXin LI /* 925b6cee71dSXin LI * Magic entries with no description get a bonus because they depend 926b6cee71dSXin LI * on subsequent magic entries to print something. 927b6cee71dSXin LI */ 928b6cee71dSXin LI if (m->desc[0] == '\0') 929b6cee71dSXin LI val++; 930b6cee71dSXin LI return val; 931b6cee71dSXin LI } 932b6cee71dSXin LI 933b6cee71dSXin LI /* 934b6cee71dSXin LI * Sort callback for sorting entries by "strength" (basically length) 935b6cee71dSXin LI */ 936b6cee71dSXin LI private int 937b6cee71dSXin LI apprentice_sort(const void *a, const void *b) 938b6cee71dSXin LI { 939b6cee71dSXin LI const struct magic_entry *ma = CAST(const struct magic_entry *, a); 940b6cee71dSXin LI const struct magic_entry *mb = CAST(const struct magic_entry *, b); 941b6cee71dSXin LI size_t sa = apprentice_magic_strength(ma->mp); 942b6cee71dSXin LI size_t sb = apprentice_magic_strength(mb->mp); 943b6cee71dSXin LI if (sa == sb) 944b6cee71dSXin LI return 0; 945b6cee71dSXin LI else if (sa > sb) 946b6cee71dSXin LI return -1; 947b6cee71dSXin LI else 948b6cee71dSXin LI return 1; 949b6cee71dSXin LI } 950b6cee71dSXin LI 951b6cee71dSXin LI /* 952b6cee71dSXin LI * Shows sorted patterns list in the order which is used for the matching 953b6cee71dSXin LI */ 954b6cee71dSXin LI private void 955b6cee71dSXin LI apprentice_list(struct mlist *mlist, int mode) 956b6cee71dSXin LI { 957b6cee71dSXin LI uint32_t magindex = 0; 958b6cee71dSXin LI struct mlist *ml; 959b6cee71dSXin LI for (ml = mlist->next; ml != mlist; ml = ml->next) { 960b6cee71dSXin LI for (magindex = 0; magindex < ml->nmagic; magindex++) { 961b6cee71dSXin LI struct magic *m = &ml->magic[magindex]; 962b6cee71dSXin LI if ((m->flag & mode) != mode) { 963b6cee71dSXin LI /* Skip sub-tests */ 964b6cee71dSXin LI while (magindex + 1 < ml->nmagic && 965b6cee71dSXin LI ml->magic[magindex + 1].cont_level != 0) 966b6cee71dSXin LI ++magindex; 967b6cee71dSXin LI continue; /* Skip to next top-level test*/ 968b6cee71dSXin LI } 969b6cee71dSXin LI 970b6cee71dSXin LI /* 971b6cee71dSXin LI * Try to iterate over the tree until we find item with 972b6cee71dSXin LI * description/mimetype. 973b6cee71dSXin LI */ 974b6cee71dSXin LI while (magindex + 1 < ml->nmagic && 975b6cee71dSXin LI ml->magic[magindex + 1].cont_level != 0 && 976b6cee71dSXin LI *ml->magic[magindex].desc == '\0' && 977b6cee71dSXin LI *ml->magic[magindex].mimetype == '\0') 978b6cee71dSXin LI magindex++; 979b6cee71dSXin LI 9805f0216bdSXin LI printf("Strength = %3" SIZE_T_FORMAT "u@%u: %s [%s]\n", 981b6cee71dSXin LI apprentice_magic_strength(m), 9825f0216bdSXin LI ml->magic[magindex].lineno, 983b6cee71dSXin LI ml->magic[magindex].desc, 984b6cee71dSXin LI ml->magic[magindex].mimetype); 985b6cee71dSXin LI } 986b6cee71dSXin LI } 987b6cee71dSXin LI } 988b6cee71dSXin LI 989b6cee71dSXin LI private void 990b6cee71dSXin LI set_test_type(struct magic *mstart, struct magic *m) 991b6cee71dSXin LI { 992b6cee71dSXin LI switch (m->type) { 993b6cee71dSXin LI case FILE_BYTE: 994b6cee71dSXin LI case FILE_SHORT: 995b6cee71dSXin LI case FILE_LONG: 996b6cee71dSXin LI case FILE_DATE: 997b6cee71dSXin LI case FILE_BESHORT: 998b6cee71dSXin LI case FILE_BELONG: 999b6cee71dSXin LI case FILE_BEDATE: 1000b6cee71dSXin LI case FILE_LESHORT: 1001b6cee71dSXin LI case FILE_LELONG: 1002b6cee71dSXin LI case FILE_LEDATE: 1003b6cee71dSXin LI case FILE_LDATE: 1004b6cee71dSXin LI case FILE_BELDATE: 1005b6cee71dSXin LI case FILE_LELDATE: 1006b6cee71dSXin LI case FILE_MEDATE: 1007b6cee71dSXin LI case FILE_MELDATE: 1008b6cee71dSXin LI case FILE_MELONG: 1009b6cee71dSXin LI case FILE_QUAD: 1010b6cee71dSXin LI case FILE_LEQUAD: 1011b6cee71dSXin LI case FILE_BEQUAD: 1012b6cee71dSXin LI case FILE_QDATE: 1013b6cee71dSXin LI case FILE_LEQDATE: 1014b6cee71dSXin LI case FILE_BEQDATE: 1015b6cee71dSXin LI case FILE_QLDATE: 1016b6cee71dSXin LI case FILE_LEQLDATE: 1017b6cee71dSXin LI case FILE_BEQLDATE: 1018b6cee71dSXin LI case FILE_QWDATE: 1019b6cee71dSXin LI case FILE_LEQWDATE: 1020b6cee71dSXin LI case FILE_BEQWDATE: 1021b6cee71dSXin LI case FILE_FLOAT: 1022b6cee71dSXin LI case FILE_BEFLOAT: 1023b6cee71dSXin LI case FILE_LEFLOAT: 1024b6cee71dSXin LI case FILE_DOUBLE: 1025b6cee71dSXin LI case FILE_BEDOUBLE: 1026b6cee71dSXin LI case FILE_LEDOUBLE: 10273e41d09dSXin LI case FILE_DER: 1028b6cee71dSXin LI mstart->flag |= BINTEST; 1029b6cee71dSXin LI break; 1030b6cee71dSXin LI case FILE_STRING: 1031b6cee71dSXin LI case FILE_PSTRING: 1032b6cee71dSXin LI case FILE_BESTRING16: 1033b6cee71dSXin LI case FILE_LESTRING16: 1034b6cee71dSXin LI /* Allow text overrides */ 1035b6cee71dSXin LI if (mstart->str_flags & STRING_TEXTTEST) 1036b6cee71dSXin LI mstart->flag |= TEXTTEST; 1037b6cee71dSXin LI else 1038b6cee71dSXin LI mstart->flag |= BINTEST; 1039b6cee71dSXin LI break; 1040b6cee71dSXin LI case FILE_REGEX: 1041b6cee71dSXin LI case FILE_SEARCH: 1042b6cee71dSXin LI /* Check for override */ 1043b6cee71dSXin LI if (mstart->str_flags & STRING_BINTEST) 1044b6cee71dSXin LI mstart->flag |= BINTEST; 1045b6cee71dSXin LI if (mstart->str_flags & STRING_TEXTTEST) 1046b6cee71dSXin LI mstart->flag |= TEXTTEST; 1047b6cee71dSXin LI 1048b6cee71dSXin LI if (mstart->flag & (TEXTTEST|BINTEST)) 1049b6cee71dSXin LI break; 1050b6cee71dSXin LI 1051b6cee71dSXin LI /* binary test if pattern is not text */ 1052b6cee71dSXin LI if (file_looks_utf8(m->value.us, (size_t)m->vallen, NULL, 1053b6cee71dSXin LI NULL) <= 0) 1054b6cee71dSXin LI mstart->flag |= BINTEST; 1055b6cee71dSXin LI else 1056b6cee71dSXin LI mstart->flag |= TEXTTEST; 1057b6cee71dSXin LI break; 1058b6cee71dSXin LI case FILE_DEFAULT: 1059b6cee71dSXin LI /* can't deduce anything; we shouldn't see this at the 1060b6cee71dSXin LI top level anyway */ 1061b6cee71dSXin LI break; 1062b6cee71dSXin LI case FILE_INVALID: 1063b6cee71dSXin LI default: 1064b6cee71dSXin LI /* invalid search type, but no need to complain here */ 1065b6cee71dSXin LI break; 1066b6cee71dSXin LI } 1067b6cee71dSXin LI } 1068b6cee71dSXin LI 1069b6cee71dSXin LI private int 1070b6cee71dSXin LI addentry(struct magic_set *ms, struct magic_entry *me, 1071b6cee71dSXin LI struct magic_entry_set *mset) 1072b6cee71dSXin LI { 1073b6cee71dSXin LI size_t i = me->mp->type == FILE_NAME ? 1 : 0; 1074b6cee71dSXin LI if (mset[i].count == mset[i].max) { 1075b6cee71dSXin LI struct magic_entry *mp; 1076b6cee71dSXin LI 1077b6cee71dSXin LI mset[i].max += ALLOC_INCR; 1078b6cee71dSXin LI if ((mp = CAST(struct magic_entry *, 1079b6cee71dSXin LI realloc(mset[i].me, sizeof(*mp) * mset[i].max))) == 1080b6cee71dSXin LI NULL) { 1081b6cee71dSXin LI file_oomem(ms, sizeof(*mp) * mset[i].max); 1082b6cee71dSXin LI return -1; 1083b6cee71dSXin LI } 1084b6cee71dSXin LI (void)memset(&mp[mset[i].count], 0, sizeof(*mp) * 1085b6cee71dSXin LI ALLOC_INCR); 1086b6cee71dSXin LI mset[i].me = mp; 1087b6cee71dSXin LI } 1088b6cee71dSXin LI mset[i].me[mset[i].count++] = *me; 1089b6cee71dSXin LI memset(me, 0, sizeof(*me)); 1090b6cee71dSXin LI return 0; 1091b6cee71dSXin LI } 1092b6cee71dSXin LI 1093b6cee71dSXin LI /* 1094b6cee71dSXin LI * Load and parse one file. 1095b6cee71dSXin LI */ 1096b6cee71dSXin LI private void 1097b6cee71dSXin LI load_1(struct magic_set *ms, int action, const char *fn, int *errs, 1098b6cee71dSXin LI struct magic_entry_set *mset) 1099b6cee71dSXin LI { 1100b6cee71dSXin LI size_t lineno = 0, llen = 0; 1101b6cee71dSXin LI char *line = NULL; 1102b6cee71dSXin LI ssize_t len; 1103b6cee71dSXin LI struct magic_entry me; 1104b6cee71dSXin LI 1105b6cee71dSXin LI FILE *f = fopen(ms->file = fn, "r"); 1106b6cee71dSXin LI if (f == NULL) { 1107b6cee71dSXin LI if (errno != ENOENT) 1108b6cee71dSXin LI file_error(ms, errno, "cannot read magic file `%s'", 1109b6cee71dSXin LI fn); 1110b6cee71dSXin LI (*errs)++; 1111b6cee71dSXin LI return; 1112b6cee71dSXin LI } 1113b6cee71dSXin LI 1114b6cee71dSXin LI memset(&me, 0, sizeof(me)); 1115b6cee71dSXin LI /* read and parse this file */ 1116b6cee71dSXin LI for (ms->line = 1; (len = getline(&line, &llen, f)) != -1; 1117b6cee71dSXin LI ms->line++) { 1118b6cee71dSXin LI if (len == 0) /* null line, garbage, etc */ 1119b6cee71dSXin LI continue; 1120b6cee71dSXin LI if (line[len - 1] == '\n') { 1121b6cee71dSXin LI lineno++; 1122b6cee71dSXin LI line[len - 1] = '\0'; /* delete newline */ 1123b6cee71dSXin LI } 1124b6cee71dSXin LI switch (line[0]) { 1125b6cee71dSXin LI case '\0': /* empty, do not parse */ 1126b6cee71dSXin LI case '#': /* comment, do not parse */ 1127b6cee71dSXin LI continue; 1128b6cee71dSXin LI case '!': 1129b6cee71dSXin LI if (line[1] == ':') { 1130b6cee71dSXin LI size_t i; 1131b6cee71dSXin LI 1132b6cee71dSXin LI for (i = 0; bang[i].name != NULL; i++) { 1133b6cee71dSXin LI if ((size_t)(len - 2) > bang[i].len && 1134b6cee71dSXin LI memcmp(bang[i].name, line + 2, 1135b6cee71dSXin LI bang[i].len) == 0) 1136b6cee71dSXin LI break; 1137b6cee71dSXin LI } 1138b6cee71dSXin LI if (bang[i].name == NULL) { 1139b6cee71dSXin LI file_error(ms, 0, 1140b6cee71dSXin LI "Unknown !: entry `%s'", line); 1141b6cee71dSXin LI (*errs)++; 1142b6cee71dSXin LI continue; 1143b6cee71dSXin LI } 1144b6cee71dSXin LI if (me.mp == NULL) { 1145b6cee71dSXin LI file_error(ms, 0, 1146b6cee71dSXin LI "No current entry for :!%s type", 1147b6cee71dSXin LI bang[i].name); 1148b6cee71dSXin LI (*errs)++; 1149b6cee71dSXin LI continue; 1150b6cee71dSXin LI } 1151b6cee71dSXin LI if ((*bang[i].fun)(ms, &me, 1152b6cee71dSXin LI line + bang[i].len + 2) != 0) { 1153b6cee71dSXin LI (*errs)++; 1154b6cee71dSXin LI continue; 1155b6cee71dSXin LI } 1156b6cee71dSXin LI continue; 1157b6cee71dSXin LI } 1158b6cee71dSXin LI /*FALLTHROUGH*/ 1159b6cee71dSXin LI default: 1160b6cee71dSXin LI again: 1161b6cee71dSXin LI switch (parse(ms, &me, line, lineno, action)) { 1162b6cee71dSXin LI case 0: 1163b6cee71dSXin LI continue; 1164b6cee71dSXin LI case 1: 1165b6cee71dSXin LI (void)addentry(ms, &me, mset); 1166b6cee71dSXin LI goto again; 1167b6cee71dSXin LI default: 1168b6cee71dSXin LI (*errs)++; 1169b6cee71dSXin LI break; 1170b6cee71dSXin LI } 1171b6cee71dSXin LI } 1172b6cee71dSXin LI } 1173b6cee71dSXin LI if (me.mp) 1174b6cee71dSXin LI (void)addentry(ms, &me, mset); 1175b6cee71dSXin LI free(line); 1176b6cee71dSXin LI (void)fclose(f); 1177b6cee71dSXin LI } 1178b6cee71dSXin LI 1179b6cee71dSXin LI /* 1180b6cee71dSXin LI * parse a file or directory of files 1181b6cee71dSXin LI * const char *fn: name of magic file or directory 1182b6cee71dSXin LI */ 1183b6cee71dSXin LI private int 1184b6cee71dSXin LI cmpstrp(const void *p1, const void *p2) 1185b6cee71dSXin LI { 1186b6cee71dSXin LI return strcmp(*(char *const *)p1, *(char *const *)p2); 1187b6cee71dSXin LI } 1188b6cee71dSXin LI 1189b6cee71dSXin LI 1190b6cee71dSXin LI private uint32_t 1191b6cee71dSXin LI set_text_binary(struct magic_set *ms, struct magic_entry *me, uint32_t nme, 1192b6cee71dSXin LI uint32_t starttest) 1193b6cee71dSXin LI { 1194b6cee71dSXin LI static const char text[] = "text"; 1195b6cee71dSXin LI static const char binary[] = "binary"; 1196b6cee71dSXin LI static const size_t len = sizeof(text); 1197b6cee71dSXin LI 1198b6cee71dSXin LI uint32_t i = starttest; 1199b6cee71dSXin LI 1200b6cee71dSXin LI do { 1201b6cee71dSXin LI set_test_type(me[starttest].mp, me[i].mp); 1202b6cee71dSXin LI if ((ms->flags & MAGIC_DEBUG) == 0) 1203b6cee71dSXin LI continue; 1204b6cee71dSXin LI (void)fprintf(stderr, "%s%s%s: %s\n", 1205b6cee71dSXin LI me[i].mp->mimetype, 1206b6cee71dSXin LI me[i].mp->mimetype[0] == '\0' ? "" : "; ", 1207b6cee71dSXin LI me[i].mp->desc[0] ? me[i].mp->desc : "(no description)", 1208b6cee71dSXin LI me[i].mp->flag & BINTEST ? binary : text); 1209b6cee71dSXin LI if (me[i].mp->flag & BINTEST) { 1210b6cee71dSXin LI char *p = strstr(me[i].mp->desc, text); 1211b6cee71dSXin LI if (p && (p == me[i].mp->desc || 1212b6cee71dSXin LI isspace((unsigned char)p[-1])) && 1213b6cee71dSXin LI (p + len - me[i].mp->desc == MAXstring 1214b6cee71dSXin LI || (p[len] == '\0' || 1215b6cee71dSXin LI isspace((unsigned char)p[len])))) 1216b6cee71dSXin LI (void)fprintf(stderr, "*** Possible " 1217b6cee71dSXin LI "binary test for text type\n"); 1218b6cee71dSXin LI } 1219b6cee71dSXin LI } while (++i < nme && me[i].mp->cont_level != 0); 1220b6cee71dSXin LI return i; 1221b6cee71dSXin LI } 1222b6cee71dSXin LI 1223b6cee71dSXin LI private void 1224b6cee71dSXin LI set_last_default(struct magic_set *ms, struct magic_entry *me, uint32_t nme) 1225b6cee71dSXin LI { 1226b6cee71dSXin LI uint32_t i; 1227b6cee71dSXin LI for (i = 0; i < nme; i++) { 1228b6cee71dSXin LI if (me[i].mp->cont_level == 0 && 1229b6cee71dSXin LI me[i].mp->type == FILE_DEFAULT) { 1230b6cee71dSXin LI while (++i < nme) 1231b6cee71dSXin LI if (me[i].mp->cont_level == 0) 1232b6cee71dSXin LI break; 1233b6cee71dSXin LI if (i != nme) { 1234b6cee71dSXin LI /* XXX - Ugh! */ 1235b6cee71dSXin LI ms->line = me[i].mp->lineno; 1236b6cee71dSXin LI file_magwarn(ms, 1237b6cee71dSXin LI "level 0 \"default\" did not sort last"); 1238b6cee71dSXin LI } 1239b6cee71dSXin LI return; 1240b6cee71dSXin LI } 1241b6cee71dSXin LI } 1242b6cee71dSXin LI } 1243b6cee71dSXin LI 1244b6cee71dSXin LI private int 1245b6cee71dSXin LI coalesce_entries(struct magic_set *ms, struct magic_entry *me, uint32_t nme, 1246b6cee71dSXin LI struct magic **ma, uint32_t *nma) 1247b6cee71dSXin LI { 1248b6cee71dSXin LI uint32_t i, mentrycount = 0; 1249b6cee71dSXin LI size_t slen; 1250b6cee71dSXin LI 1251b6cee71dSXin LI for (i = 0; i < nme; i++) 1252b6cee71dSXin LI mentrycount += me[i].cont_count; 1253b6cee71dSXin LI 1254b6cee71dSXin LI slen = sizeof(**ma) * mentrycount; 1255b6cee71dSXin LI if ((*ma = CAST(struct magic *, malloc(slen))) == NULL) { 1256b6cee71dSXin LI file_oomem(ms, slen); 1257b6cee71dSXin LI return -1; 1258b6cee71dSXin LI } 1259b6cee71dSXin LI 1260b6cee71dSXin LI mentrycount = 0; 1261b6cee71dSXin LI for (i = 0; i < nme; i++) { 1262b6cee71dSXin LI (void)memcpy(*ma + mentrycount, me[i].mp, 1263b6cee71dSXin LI me[i].cont_count * sizeof(**ma)); 1264b6cee71dSXin LI mentrycount += me[i].cont_count; 1265b6cee71dSXin LI } 1266b6cee71dSXin LI *nma = mentrycount; 1267b6cee71dSXin LI return 0; 1268b6cee71dSXin LI } 1269b6cee71dSXin LI 1270b6cee71dSXin LI private void 1271b6cee71dSXin LI magic_entry_free(struct magic_entry *me, uint32_t nme) 1272b6cee71dSXin LI { 1273b6cee71dSXin LI uint32_t i; 1274b6cee71dSXin LI if (me == NULL) 1275b6cee71dSXin LI return; 1276b6cee71dSXin LI for (i = 0; i < nme; i++) 1277b6cee71dSXin LI free(me[i].mp); 1278b6cee71dSXin LI free(me); 1279b6cee71dSXin LI } 1280b6cee71dSXin LI 1281b6cee71dSXin LI private struct magic_map * 1282b6cee71dSXin LI apprentice_load(struct magic_set *ms, const char *fn, int action) 1283b6cee71dSXin LI { 1284b6cee71dSXin LI int errs = 0; 1285b6cee71dSXin LI uint32_t i, j; 1286b6cee71dSXin LI size_t files = 0, maxfiles = 0; 1287b6cee71dSXin LI char **filearr = NULL, *mfn; 1288b6cee71dSXin LI struct stat st; 1289b6cee71dSXin LI struct magic_map *map; 1290b6cee71dSXin LI struct magic_entry_set mset[MAGIC_SETS]; 1291b6cee71dSXin LI DIR *dir; 1292b6cee71dSXin LI struct dirent *d; 1293b6cee71dSXin LI 1294b6cee71dSXin LI memset(mset, 0, sizeof(mset)); 1295b6cee71dSXin LI ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */ 1296b6cee71dSXin LI 1297b6cee71dSXin LI 1298b6cee71dSXin LI if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) 1299b6cee71dSXin LI { 1300b6cee71dSXin LI file_oomem(ms, sizeof(*map)); 1301b6cee71dSXin LI return NULL; 1302b6cee71dSXin LI } 13039ce06829SXin LI map->type = MAP_TYPE_MALLOC; 1304b6cee71dSXin LI 1305b6cee71dSXin LI /* print silly verbose header for USG compat. */ 1306b6cee71dSXin LI if (action == FILE_CHECK) 1307b6cee71dSXin LI (void)fprintf(stderr, "%s\n", usg_hdr); 1308b6cee71dSXin LI 1309b6cee71dSXin LI /* load directory or file */ 1310b6cee71dSXin LI if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) { 1311b6cee71dSXin LI dir = opendir(fn); 1312b6cee71dSXin LI if (!dir) { 1313b6cee71dSXin LI errs++; 1314b6cee71dSXin LI goto out; 1315b6cee71dSXin LI } 1316b6cee71dSXin LI while ((d = readdir(dir)) != NULL) { 1317b6cee71dSXin LI if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) { 1318b6cee71dSXin LI file_oomem(ms, 1319b6cee71dSXin LI strlen(fn) + strlen(d->d_name) + 2); 1320b6cee71dSXin LI errs++; 1321b6cee71dSXin LI closedir(dir); 1322b6cee71dSXin LI goto out; 1323b6cee71dSXin LI } 1324b6cee71dSXin LI if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) { 1325b6cee71dSXin LI free(mfn); 1326b6cee71dSXin LI continue; 1327b6cee71dSXin LI } 1328b6cee71dSXin LI if (files >= maxfiles) { 1329b6cee71dSXin LI size_t mlen; 1330b6cee71dSXin LI maxfiles = (maxfiles + 1) * 2; 1331b6cee71dSXin LI mlen = maxfiles * sizeof(*filearr); 1332b6cee71dSXin LI if ((filearr = CAST(char **, 1333b6cee71dSXin LI realloc(filearr, mlen))) == NULL) { 1334b6cee71dSXin LI file_oomem(ms, mlen); 1335b6cee71dSXin LI free(mfn); 1336b6cee71dSXin LI closedir(dir); 1337b6cee71dSXin LI errs++; 1338b6cee71dSXin LI goto out; 1339b6cee71dSXin LI } 1340b6cee71dSXin LI } 1341b6cee71dSXin LI filearr[files++] = mfn; 1342b6cee71dSXin LI } 1343b6cee71dSXin LI closedir(dir); 1344b6cee71dSXin LI qsort(filearr, files, sizeof(*filearr), cmpstrp); 1345b6cee71dSXin LI for (i = 0; i < files; i++) { 1346b6cee71dSXin LI load_1(ms, action, filearr[i], &errs, mset); 1347b6cee71dSXin LI free(filearr[i]); 1348b6cee71dSXin LI } 1349b6cee71dSXin LI free(filearr); 1350b6cee71dSXin LI } else 1351b6cee71dSXin LI load_1(ms, action, fn, &errs, mset); 1352b6cee71dSXin LI if (errs) 1353b6cee71dSXin LI goto out; 1354b6cee71dSXin LI 1355b6cee71dSXin LI for (j = 0; j < MAGIC_SETS; j++) { 1356b6cee71dSXin LI /* Set types of tests */ 1357b6cee71dSXin LI for (i = 0; i < mset[j].count; ) { 1358b6cee71dSXin LI if (mset[j].me[i].mp->cont_level != 0) { 1359b6cee71dSXin LI i++; 1360b6cee71dSXin LI continue; 1361b6cee71dSXin LI } 1362b6cee71dSXin LI i = set_text_binary(ms, mset[j].me, mset[j].count, i); 1363b6cee71dSXin LI } 13649ce06829SXin LI if (mset[j].me) 1365b6cee71dSXin LI qsort(mset[j].me, mset[j].count, sizeof(*mset[j].me), 1366b6cee71dSXin LI apprentice_sort); 1367b6cee71dSXin LI 1368b6cee71dSXin LI /* 1369b6cee71dSXin LI * Make sure that any level 0 "default" line is last 1370b6cee71dSXin LI * (if one exists). 1371b6cee71dSXin LI */ 1372b6cee71dSXin LI set_last_default(ms, mset[j].me, mset[j].count); 1373b6cee71dSXin LI 1374b6cee71dSXin LI /* coalesce per file arrays into a single one */ 1375b6cee71dSXin LI if (coalesce_entries(ms, mset[j].me, mset[j].count, 1376b6cee71dSXin LI &map->magic[j], &map->nmagic[j]) == -1) { 1377b6cee71dSXin LI errs++; 1378b6cee71dSXin LI goto out; 1379b6cee71dSXin LI } 1380b6cee71dSXin LI } 1381b6cee71dSXin LI 1382b6cee71dSXin LI out: 1383b6cee71dSXin LI for (j = 0; j < MAGIC_SETS; j++) 1384b6cee71dSXin LI magic_entry_free(mset[j].me, mset[j].count); 1385b6cee71dSXin LI 1386b6cee71dSXin LI if (errs) { 1387b6cee71dSXin LI apprentice_unmap(map); 1388b6cee71dSXin LI return NULL; 1389b6cee71dSXin LI } 1390b6cee71dSXin LI return map; 1391b6cee71dSXin LI } 1392b6cee71dSXin LI 1393b6cee71dSXin LI /* 1394b6cee71dSXin LI * extend the sign bit if the comparison is to be signed 1395b6cee71dSXin LI */ 1396b6cee71dSXin LI protected uint64_t 1397b6cee71dSXin LI file_signextend(struct magic_set *ms, struct magic *m, uint64_t v) 1398b6cee71dSXin LI { 1399b6cee71dSXin LI if (!(m->flag & UNSIGNED)) { 1400b6cee71dSXin LI switch(m->type) { 1401b6cee71dSXin LI /* 1402b6cee71dSXin LI * Do not remove the casts below. They are 1403b6cee71dSXin LI * vital. When later compared with the data, 1404b6cee71dSXin LI * the sign extension must have happened. 1405b6cee71dSXin LI */ 1406b6cee71dSXin LI case FILE_BYTE: 1407c2931133SXin LI v = (signed char) v; 1408b6cee71dSXin LI break; 1409b6cee71dSXin LI case FILE_SHORT: 1410b6cee71dSXin LI case FILE_BESHORT: 1411b6cee71dSXin LI case FILE_LESHORT: 1412b6cee71dSXin LI v = (short) v; 1413b6cee71dSXin LI break; 1414b6cee71dSXin LI case FILE_DATE: 1415b6cee71dSXin LI case FILE_BEDATE: 1416b6cee71dSXin LI case FILE_LEDATE: 1417b6cee71dSXin LI case FILE_MEDATE: 1418b6cee71dSXin LI case FILE_LDATE: 1419b6cee71dSXin LI case FILE_BELDATE: 1420b6cee71dSXin LI case FILE_LELDATE: 1421b6cee71dSXin LI case FILE_MELDATE: 1422b6cee71dSXin LI case FILE_LONG: 1423b6cee71dSXin LI case FILE_BELONG: 1424b6cee71dSXin LI case FILE_LELONG: 1425b6cee71dSXin LI case FILE_MELONG: 1426b6cee71dSXin LI case FILE_FLOAT: 1427b6cee71dSXin LI case FILE_BEFLOAT: 1428b6cee71dSXin LI case FILE_LEFLOAT: 1429b6cee71dSXin LI v = (int32_t) v; 1430b6cee71dSXin LI break; 1431b6cee71dSXin LI case FILE_QUAD: 1432b6cee71dSXin LI case FILE_BEQUAD: 1433b6cee71dSXin LI case FILE_LEQUAD: 1434b6cee71dSXin LI case FILE_QDATE: 1435b6cee71dSXin LI case FILE_QLDATE: 1436b6cee71dSXin LI case FILE_QWDATE: 1437b6cee71dSXin LI case FILE_BEQDATE: 1438b6cee71dSXin LI case FILE_BEQLDATE: 1439b6cee71dSXin LI case FILE_BEQWDATE: 1440b6cee71dSXin LI case FILE_LEQDATE: 1441b6cee71dSXin LI case FILE_LEQLDATE: 1442b6cee71dSXin LI case FILE_LEQWDATE: 1443b6cee71dSXin LI case FILE_DOUBLE: 1444b6cee71dSXin LI case FILE_BEDOUBLE: 1445b6cee71dSXin LI case FILE_LEDOUBLE: 1446b6cee71dSXin LI v = (int64_t) v; 1447b6cee71dSXin LI break; 1448b6cee71dSXin LI case FILE_STRING: 1449b6cee71dSXin LI case FILE_PSTRING: 1450b6cee71dSXin LI case FILE_BESTRING16: 1451b6cee71dSXin LI case FILE_LESTRING16: 1452b6cee71dSXin LI case FILE_REGEX: 1453b6cee71dSXin LI case FILE_SEARCH: 1454b6cee71dSXin LI case FILE_DEFAULT: 1455b6cee71dSXin LI case FILE_INDIRECT: 1456b6cee71dSXin LI case FILE_NAME: 1457b6cee71dSXin LI case FILE_USE: 1458b6cee71dSXin LI case FILE_CLEAR: 14593e41d09dSXin LI case FILE_DER: 1460b6cee71dSXin LI break; 1461b6cee71dSXin LI default: 1462b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1463b6cee71dSXin LI file_magwarn(ms, "cannot happen: m->type=%d\n", 1464b6cee71dSXin LI m->type); 1465b6cee71dSXin LI return ~0U; 1466b6cee71dSXin LI } 1467b6cee71dSXin LI } 1468b6cee71dSXin LI return v; 1469b6cee71dSXin LI } 1470b6cee71dSXin LI 1471b6cee71dSXin LI private int 1472b6cee71dSXin LI string_modifier_check(struct magic_set *ms, struct magic *m) 1473b6cee71dSXin LI { 1474b6cee71dSXin LI if ((ms->flags & MAGIC_CHECK) == 0) 1475b6cee71dSXin LI return 0; 1476b6cee71dSXin LI 1477b6cee71dSXin LI if ((m->type != FILE_REGEX || (m->str_flags & REGEX_LINE_COUNT) == 0) && 1478b6cee71dSXin LI (m->type != FILE_PSTRING && (m->str_flags & PSTRING_LEN) != 0)) { 1479b6cee71dSXin LI file_magwarn(ms, 1480b6cee71dSXin LI "'/BHhLl' modifiers are only allowed for pascal strings\n"); 1481b6cee71dSXin LI return -1; 1482b6cee71dSXin LI } 1483b6cee71dSXin LI switch (m->type) { 1484b6cee71dSXin LI case FILE_BESTRING16: 1485b6cee71dSXin LI case FILE_LESTRING16: 1486b6cee71dSXin LI if (m->str_flags != 0) { 1487b6cee71dSXin LI file_magwarn(ms, 1488b6cee71dSXin LI "no modifiers allowed for 16-bit strings\n"); 1489b6cee71dSXin LI return -1; 1490b6cee71dSXin LI } 1491b6cee71dSXin LI break; 1492b6cee71dSXin LI case FILE_STRING: 1493b6cee71dSXin LI case FILE_PSTRING: 1494b6cee71dSXin LI if ((m->str_flags & REGEX_OFFSET_START) != 0) { 1495b6cee71dSXin LI file_magwarn(ms, 1496b6cee71dSXin LI "'/%c' only allowed on regex and search\n", 1497b6cee71dSXin LI CHAR_REGEX_OFFSET_START); 1498b6cee71dSXin LI return -1; 1499b6cee71dSXin LI } 1500b6cee71dSXin LI break; 1501b6cee71dSXin LI case FILE_SEARCH: 1502b6cee71dSXin LI if (m->str_range == 0) { 1503b6cee71dSXin LI file_magwarn(ms, 1504b6cee71dSXin LI "missing range; defaulting to %d\n", 1505b6cee71dSXin LI STRING_DEFAULT_RANGE); 1506b6cee71dSXin LI m->str_range = STRING_DEFAULT_RANGE; 1507b6cee71dSXin LI return -1; 1508b6cee71dSXin LI } 1509b6cee71dSXin LI break; 1510b6cee71dSXin LI case FILE_REGEX: 1511b6cee71dSXin LI if ((m->str_flags & STRING_COMPACT_WHITESPACE) != 0) { 1512b6cee71dSXin LI file_magwarn(ms, "'/%c' not allowed on regex\n", 1513b6cee71dSXin LI CHAR_COMPACT_WHITESPACE); 1514b6cee71dSXin LI return -1; 1515b6cee71dSXin LI } 1516b6cee71dSXin LI if ((m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) != 0) { 1517b6cee71dSXin LI file_magwarn(ms, "'/%c' not allowed on regex\n", 1518b6cee71dSXin LI CHAR_COMPACT_OPTIONAL_WHITESPACE); 1519b6cee71dSXin LI return -1; 1520b6cee71dSXin LI } 1521b6cee71dSXin LI break; 1522b6cee71dSXin LI default: 1523b6cee71dSXin LI file_magwarn(ms, "coding error: m->type=%d\n", 1524b6cee71dSXin LI m->type); 1525b6cee71dSXin LI return -1; 1526b6cee71dSXin LI } 1527b6cee71dSXin LI return 0; 1528b6cee71dSXin LI } 1529b6cee71dSXin LI 1530b6cee71dSXin LI private int 1531b6cee71dSXin LI get_op(char c) 1532b6cee71dSXin LI { 1533b6cee71dSXin LI switch (c) { 1534b6cee71dSXin LI case '&': 1535b6cee71dSXin LI return FILE_OPAND; 1536b6cee71dSXin LI case '|': 1537b6cee71dSXin LI return FILE_OPOR; 1538b6cee71dSXin LI case '^': 1539b6cee71dSXin LI return FILE_OPXOR; 1540b6cee71dSXin LI case '+': 1541b6cee71dSXin LI return FILE_OPADD; 1542b6cee71dSXin LI case '-': 1543b6cee71dSXin LI return FILE_OPMINUS; 1544b6cee71dSXin LI case '*': 1545b6cee71dSXin LI return FILE_OPMULTIPLY; 1546b6cee71dSXin LI case '/': 1547b6cee71dSXin LI return FILE_OPDIVIDE; 1548b6cee71dSXin LI case '%': 1549b6cee71dSXin LI return FILE_OPMODULO; 1550b6cee71dSXin LI default: 1551b6cee71dSXin LI return -1; 1552b6cee71dSXin LI } 1553b6cee71dSXin LI } 1554b6cee71dSXin LI 1555b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS 1556b6cee71dSXin LI private int 1557b6cee71dSXin LI get_cond(const char *l, const char **t) 1558b6cee71dSXin LI { 1559b6cee71dSXin LI static const struct cond_tbl_s { 1560b6cee71dSXin LI char name[8]; 1561b6cee71dSXin LI size_t len; 1562b6cee71dSXin LI int cond; 1563b6cee71dSXin LI } cond_tbl[] = { 1564b6cee71dSXin LI { "if", 2, COND_IF }, 1565b6cee71dSXin LI { "elif", 4, COND_ELIF }, 1566b6cee71dSXin LI { "else", 4, COND_ELSE }, 1567b6cee71dSXin LI { "", 0, COND_NONE }, 1568b6cee71dSXin LI }; 1569b6cee71dSXin LI const struct cond_tbl_s *p; 1570b6cee71dSXin LI 1571b6cee71dSXin LI for (p = cond_tbl; p->len; p++) { 1572b6cee71dSXin LI if (strncmp(l, p->name, p->len) == 0 && 1573b6cee71dSXin LI isspace((unsigned char)l[p->len])) { 1574b6cee71dSXin LI if (t) 1575b6cee71dSXin LI *t = l + p->len; 1576b6cee71dSXin LI break; 1577b6cee71dSXin LI } 1578b6cee71dSXin LI } 1579b6cee71dSXin LI return p->cond; 1580b6cee71dSXin LI } 1581b6cee71dSXin LI 1582b6cee71dSXin LI private int 1583b6cee71dSXin LI check_cond(struct magic_set *ms, int cond, uint32_t cont_level) 1584b6cee71dSXin LI { 1585b6cee71dSXin LI int last_cond; 1586b6cee71dSXin LI last_cond = ms->c.li[cont_level].last_cond; 1587b6cee71dSXin LI 1588b6cee71dSXin LI switch (cond) { 1589b6cee71dSXin LI case COND_IF: 1590b6cee71dSXin LI if (last_cond != COND_NONE && last_cond != COND_ELIF) { 1591b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1592b6cee71dSXin LI file_magwarn(ms, "syntax error: `if'"); 1593b6cee71dSXin LI return -1; 1594b6cee71dSXin LI } 1595b6cee71dSXin LI last_cond = COND_IF; 1596b6cee71dSXin LI break; 1597b6cee71dSXin LI 1598b6cee71dSXin LI case COND_ELIF: 1599b6cee71dSXin LI if (last_cond != COND_IF && last_cond != COND_ELIF) { 1600b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1601b6cee71dSXin LI file_magwarn(ms, "syntax error: `elif'"); 1602b6cee71dSXin LI return -1; 1603b6cee71dSXin LI } 1604b6cee71dSXin LI last_cond = COND_ELIF; 1605b6cee71dSXin LI break; 1606b6cee71dSXin LI 1607b6cee71dSXin LI case COND_ELSE: 1608b6cee71dSXin LI if (last_cond != COND_IF && last_cond != COND_ELIF) { 1609b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1610b6cee71dSXin LI file_magwarn(ms, "syntax error: `else'"); 1611b6cee71dSXin LI return -1; 1612b6cee71dSXin LI } 1613b6cee71dSXin LI last_cond = COND_NONE; 1614b6cee71dSXin LI break; 1615b6cee71dSXin LI 1616b6cee71dSXin LI case COND_NONE: 1617b6cee71dSXin LI last_cond = COND_NONE; 1618b6cee71dSXin LI break; 1619b6cee71dSXin LI } 1620b6cee71dSXin LI 1621b6cee71dSXin LI ms->c.li[cont_level].last_cond = last_cond; 1622b6cee71dSXin LI return 0; 1623b6cee71dSXin LI } 1624b6cee71dSXin LI #endif /* ENABLE_CONDITIONALS */ 1625b6cee71dSXin LI 16264460e5b0SXin LI private int 16274460e5b0SXin LI parse_indirect_modifier(struct magic_set *ms, struct magic *m, const char **lp) 16284460e5b0SXin LI { 16294460e5b0SXin LI const char *l = *lp; 16304460e5b0SXin LI 16314460e5b0SXin LI while (!isspace((unsigned char)*++l)) 16324460e5b0SXin LI switch (*l) { 16334460e5b0SXin LI case CHAR_INDIRECT_RELATIVE: 16344460e5b0SXin LI m->str_flags |= INDIRECT_RELATIVE; 16354460e5b0SXin LI break; 16364460e5b0SXin LI default: 16374460e5b0SXin LI if (ms->flags & MAGIC_CHECK) 16384460e5b0SXin LI file_magwarn(ms, "indirect modifier `%c' " 16394460e5b0SXin LI "invalid", *l); 16404460e5b0SXin LI *lp = l; 16414460e5b0SXin LI return -1; 16424460e5b0SXin LI } 16434460e5b0SXin LI *lp = l; 16444460e5b0SXin LI return 0; 16454460e5b0SXin LI } 16464460e5b0SXin LI 16474460e5b0SXin LI private void 16484460e5b0SXin LI parse_op_modifier(struct magic_set *ms, struct magic *m, const char **lp, 16494460e5b0SXin LI int op) 16504460e5b0SXin LI { 16514460e5b0SXin LI const char *l = *lp; 16524460e5b0SXin LI char *t; 16534460e5b0SXin LI uint64_t val; 16544460e5b0SXin LI 16554460e5b0SXin LI ++l; 16564460e5b0SXin LI m->mask_op |= op; 16574460e5b0SXin LI val = (uint64_t)strtoull(l, &t, 0); 16584460e5b0SXin LI l = t; 16594460e5b0SXin LI m->num_mask = file_signextend(ms, m, val); 16604460e5b0SXin LI eatsize(&l); 16614460e5b0SXin LI *lp = l; 16624460e5b0SXin LI } 16634460e5b0SXin LI 16644460e5b0SXin LI private int 16654460e5b0SXin LI parse_string_modifier(struct magic_set *ms, struct magic *m, const char **lp) 16664460e5b0SXin LI { 16674460e5b0SXin LI const char *l = *lp; 16684460e5b0SXin LI char *t; 16694460e5b0SXin LI int have_range = 0; 16704460e5b0SXin LI 16714460e5b0SXin LI while (!isspace((unsigned char)*++l)) { 16724460e5b0SXin LI switch (*l) { 16734460e5b0SXin LI case '0': case '1': case '2': 16744460e5b0SXin LI case '3': case '4': case '5': 16754460e5b0SXin LI case '6': case '7': case '8': 16764460e5b0SXin LI case '9': 16774460e5b0SXin LI if (have_range && (ms->flags & MAGIC_CHECK)) 16784460e5b0SXin LI file_magwarn(ms, "multiple ranges"); 16794460e5b0SXin LI have_range = 1; 16804460e5b0SXin LI m->str_range = CAST(uint32_t, strtoul(l, &t, 0)); 16814460e5b0SXin LI if (m->str_range == 0) 16824460e5b0SXin LI file_magwarn(ms, "zero range"); 16834460e5b0SXin LI l = t - 1; 16844460e5b0SXin LI break; 16854460e5b0SXin LI case CHAR_COMPACT_WHITESPACE: 16864460e5b0SXin LI m->str_flags |= STRING_COMPACT_WHITESPACE; 16874460e5b0SXin LI break; 16884460e5b0SXin LI case CHAR_COMPACT_OPTIONAL_WHITESPACE: 16894460e5b0SXin LI m->str_flags |= STRING_COMPACT_OPTIONAL_WHITESPACE; 16904460e5b0SXin LI break; 16914460e5b0SXin LI case CHAR_IGNORE_LOWERCASE: 16924460e5b0SXin LI m->str_flags |= STRING_IGNORE_LOWERCASE; 16934460e5b0SXin LI break; 16944460e5b0SXin LI case CHAR_IGNORE_UPPERCASE: 16954460e5b0SXin LI m->str_flags |= STRING_IGNORE_UPPERCASE; 16964460e5b0SXin LI break; 16974460e5b0SXin LI case CHAR_REGEX_OFFSET_START: 16984460e5b0SXin LI m->str_flags |= REGEX_OFFSET_START; 16994460e5b0SXin LI break; 17004460e5b0SXin LI case CHAR_BINTEST: 17014460e5b0SXin LI m->str_flags |= STRING_BINTEST; 17024460e5b0SXin LI break; 17034460e5b0SXin LI case CHAR_TEXTTEST: 17044460e5b0SXin LI m->str_flags |= STRING_TEXTTEST; 17054460e5b0SXin LI break; 17064460e5b0SXin LI case CHAR_TRIM: 17074460e5b0SXin LI m->str_flags |= STRING_TRIM; 17084460e5b0SXin LI break; 17094460e5b0SXin LI case CHAR_PSTRING_1_LE: 17104460e5b0SXin LI #define SET_LENGTH(a) m->str_flags = (m->str_flags & ~PSTRING_LEN) | (a) 17114460e5b0SXin LI if (m->type != FILE_PSTRING) 17124460e5b0SXin LI goto bad; 17134460e5b0SXin LI SET_LENGTH(PSTRING_1_LE); 17144460e5b0SXin LI break; 17154460e5b0SXin LI case CHAR_PSTRING_2_BE: 17164460e5b0SXin LI if (m->type != FILE_PSTRING) 17174460e5b0SXin LI goto bad; 17184460e5b0SXin LI SET_LENGTH(PSTRING_2_BE); 17194460e5b0SXin LI break; 17204460e5b0SXin LI case CHAR_PSTRING_2_LE: 17214460e5b0SXin LI if (m->type != FILE_PSTRING) 17224460e5b0SXin LI goto bad; 17234460e5b0SXin LI SET_LENGTH(PSTRING_2_LE); 17244460e5b0SXin LI break; 17254460e5b0SXin LI case CHAR_PSTRING_4_BE: 17264460e5b0SXin LI if (m->type != FILE_PSTRING) 17274460e5b0SXin LI goto bad; 17284460e5b0SXin LI SET_LENGTH(PSTRING_4_BE); 17294460e5b0SXin LI break; 17304460e5b0SXin LI case CHAR_PSTRING_4_LE: 17314460e5b0SXin LI switch (m->type) { 17324460e5b0SXin LI case FILE_PSTRING: 17334460e5b0SXin LI case FILE_REGEX: 17344460e5b0SXin LI break; 17354460e5b0SXin LI default: 17364460e5b0SXin LI goto bad; 17374460e5b0SXin LI } 17384460e5b0SXin LI SET_LENGTH(PSTRING_4_LE); 17394460e5b0SXin LI break; 17404460e5b0SXin LI case CHAR_PSTRING_LENGTH_INCLUDES_ITSELF: 17414460e5b0SXin LI if (m->type != FILE_PSTRING) 17424460e5b0SXin LI goto bad; 17434460e5b0SXin LI m->str_flags |= PSTRING_LENGTH_INCLUDES_ITSELF; 17444460e5b0SXin LI break; 17454460e5b0SXin LI default: 17464460e5b0SXin LI bad: 17474460e5b0SXin LI if (ms->flags & MAGIC_CHECK) 17484460e5b0SXin LI file_magwarn(ms, "string modifier `%c' " 17494460e5b0SXin LI "invalid", *l); 17504460e5b0SXin LI goto out; 17514460e5b0SXin LI } 17524460e5b0SXin LI /* allow multiple '/' for readability */ 17534460e5b0SXin LI if (l[1] == '/' && !isspace((unsigned char)l[2])) 17544460e5b0SXin LI l++; 17554460e5b0SXin LI } 17564460e5b0SXin LI if (string_modifier_check(ms, m) == -1) 17574460e5b0SXin LI goto out; 17584460e5b0SXin LI *lp = l; 17594460e5b0SXin LI return 0; 17604460e5b0SXin LI out: 17614460e5b0SXin LI *lp = l; 17624460e5b0SXin LI return -1; 17634460e5b0SXin LI } 17644460e5b0SXin LI 1765b6cee71dSXin LI /* 1766b6cee71dSXin LI * parse one line from magic file, put into magic[index++] if valid 1767b6cee71dSXin LI */ 1768b6cee71dSXin LI private int 1769b6cee71dSXin LI parse(struct magic_set *ms, struct magic_entry *me, const char *line, 1770b6cee71dSXin LI size_t lineno, int action) 1771b6cee71dSXin LI { 1772b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS 1773b6cee71dSXin LI static uint32_t last_cont_level = 0; 1774b6cee71dSXin LI #endif 1775b6cee71dSXin LI size_t i; 1776b6cee71dSXin LI struct magic *m; 1777b6cee71dSXin LI const char *l = line; 1778b6cee71dSXin LI char *t; 1779b6cee71dSXin LI int op; 1780b6cee71dSXin LI uint32_t cont_level; 1781b6cee71dSXin LI int32_t diff; 1782b6cee71dSXin LI 1783b6cee71dSXin LI cont_level = 0; 1784b6cee71dSXin LI 1785b6cee71dSXin LI /* 1786b6cee71dSXin LI * Parse the offset. 1787b6cee71dSXin LI */ 1788b6cee71dSXin LI while (*l == '>') { 1789b6cee71dSXin LI ++l; /* step over */ 1790b6cee71dSXin LI cont_level++; 1791b6cee71dSXin LI } 1792b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS 1793b6cee71dSXin LI if (cont_level == 0 || cont_level > last_cont_level) 1794b6cee71dSXin LI if (file_check_mem(ms, cont_level) == -1) 1795b6cee71dSXin LI return -1; 1796b6cee71dSXin LI last_cont_level = cont_level; 1797b6cee71dSXin LI #endif 1798b6cee71dSXin LI if (cont_level != 0) { 1799b6cee71dSXin LI if (me->mp == NULL) { 1800b6cee71dSXin LI file_magerror(ms, "No current entry for continuation"); 1801b6cee71dSXin LI return -1; 1802b6cee71dSXin LI } 1803b6cee71dSXin LI if (me->cont_count == 0) { 1804b6cee71dSXin LI file_magerror(ms, "Continuations present with 0 count"); 1805b6cee71dSXin LI return -1; 1806b6cee71dSXin LI } 1807b6cee71dSXin LI m = &me->mp[me->cont_count - 1]; 1808b6cee71dSXin LI diff = (int32_t)cont_level - (int32_t)m->cont_level; 1809b6cee71dSXin LI if (diff > 1) 1810b6cee71dSXin LI file_magwarn(ms, "New continuation level %u is more " 1811b6cee71dSXin LI "than one larger than current level %u", cont_level, 1812b6cee71dSXin LI m->cont_level); 1813b6cee71dSXin LI if (me->cont_count == me->max_count) { 1814b6cee71dSXin LI struct magic *nm; 1815b6cee71dSXin LI size_t cnt = me->max_count + ALLOC_CHUNK; 1816b6cee71dSXin LI if ((nm = CAST(struct magic *, realloc(me->mp, 1817b6cee71dSXin LI sizeof(*nm) * cnt))) == NULL) { 1818b6cee71dSXin LI file_oomem(ms, sizeof(*nm) * cnt); 1819b6cee71dSXin LI return -1; 1820b6cee71dSXin LI } 1821b6cee71dSXin LI me->mp = m = nm; 1822b6cee71dSXin LI me->max_count = CAST(uint32_t, cnt); 1823b6cee71dSXin LI } 1824b6cee71dSXin LI m = &me->mp[me->cont_count++]; 1825b6cee71dSXin LI (void)memset(m, 0, sizeof(*m)); 1826b6cee71dSXin LI m->cont_level = cont_level; 1827b6cee71dSXin LI } else { 1828b6cee71dSXin LI static const size_t len = sizeof(*m) * ALLOC_CHUNK; 1829b6cee71dSXin LI if (me->mp != NULL) 1830b6cee71dSXin LI return 1; 1831b6cee71dSXin LI if ((m = CAST(struct magic *, malloc(len))) == NULL) { 1832b6cee71dSXin LI file_oomem(ms, len); 1833b6cee71dSXin LI return -1; 1834b6cee71dSXin LI } 1835b6cee71dSXin LI me->mp = m; 1836b6cee71dSXin LI me->max_count = ALLOC_CHUNK; 1837b6cee71dSXin LI (void)memset(m, 0, sizeof(*m)); 1838b6cee71dSXin LI m->factor_op = FILE_FACTOR_OP_NONE; 1839b6cee71dSXin LI m->cont_level = 0; 1840b6cee71dSXin LI me->cont_count = 1; 1841b6cee71dSXin LI } 1842b6cee71dSXin LI m->lineno = CAST(uint32_t, lineno); 1843b6cee71dSXin LI 1844b6cee71dSXin LI if (*l == '&') { /* m->cont_level == 0 checked below. */ 1845b6cee71dSXin LI ++l; /* step over */ 1846b6cee71dSXin LI m->flag |= OFFADD; 1847b6cee71dSXin LI } 1848b6cee71dSXin LI if (*l == '(') { 1849b6cee71dSXin LI ++l; /* step over */ 1850b6cee71dSXin LI m->flag |= INDIR; 1851b6cee71dSXin LI if (m->flag & OFFADD) 1852b6cee71dSXin LI m->flag = (m->flag & ~OFFADD) | INDIROFFADD; 1853b6cee71dSXin LI 1854b6cee71dSXin LI if (*l == '&') { /* m->cont_level == 0 checked below */ 1855b6cee71dSXin LI ++l; /* step over */ 1856b6cee71dSXin LI m->flag |= OFFADD; 1857b6cee71dSXin LI } 1858b6cee71dSXin LI } 1859b6cee71dSXin LI /* Indirect offsets are not valid at level 0. */ 18605f0216bdSXin LI if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD))) { 1861b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1862b6cee71dSXin LI file_magwarn(ms, "relative offset at level 0"); 18635f0216bdSXin LI return -1; 18645f0216bdSXin LI } 1865b6cee71dSXin LI 1866b6cee71dSXin LI /* get offset, then skip over it */ 1867b6cee71dSXin LI m->offset = (uint32_t)strtoul(l, &t, 0); 18685f0216bdSXin LI if (l == t) { 1869b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1870b6cee71dSXin LI file_magwarn(ms, "offset `%s' invalid", l); 18715f0216bdSXin LI return -1; 18725f0216bdSXin LI } 1873b6cee71dSXin LI l = t; 1874b6cee71dSXin LI 1875b6cee71dSXin LI if (m->flag & INDIR) { 1876b6cee71dSXin LI m->in_type = FILE_LONG; 1877b6cee71dSXin LI m->in_offset = 0; 1878*a5d223e6SXin LI m->in_op = 0; 1879b6cee71dSXin LI /* 1880*a5d223e6SXin LI * read [.,lbs][+-]nnnnn) 1881b6cee71dSXin LI */ 1882*a5d223e6SXin LI if (*l == '.' || *l == ',') { 1883*a5d223e6SXin LI if (*l == ',') 1884*a5d223e6SXin LI m->in_op |= FILE_OPSIGNED; 1885b6cee71dSXin LI l++; 1886b6cee71dSXin LI switch (*l) { 1887b6cee71dSXin LI case 'l': 1888b6cee71dSXin LI m->in_type = FILE_LELONG; 1889b6cee71dSXin LI break; 1890b6cee71dSXin LI case 'L': 1891b6cee71dSXin LI m->in_type = FILE_BELONG; 1892b6cee71dSXin LI break; 1893b6cee71dSXin LI case 'm': 1894b6cee71dSXin LI m->in_type = FILE_MELONG; 1895b6cee71dSXin LI break; 1896b6cee71dSXin LI case 'h': 1897b6cee71dSXin LI case 's': 1898b6cee71dSXin LI m->in_type = FILE_LESHORT; 1899b6cee71dSXin LI break; 1900b6cee71dSXin LI case 'H': 1901b6cee71dSXin LI case 'S': 1902b6cee71dSXin LI m->in_type = FILE_BESHORT; 1903b6cee71dSXin LI break; 1904b6cee71dSXin LI case 'c': 1905b6cee71dSXin LI case 'b': 1906b6cee71dSXin LI case 'C': 1907b6cee71dSXin LI case 'B': 1908b6cee71dSXin LI m->in_type = FILE_BYTE; 1909b6cee71dSXin LI break; 1910b6cee71dSXin LI case 'e': 1911b6cee71dSXin LI case 'f': 1912b6cee71dSXin LI case 'g': 1913b6cee71dSXin LI m->in_type = FILE_LEDOUBLE; 1914b6cee71dSXin LI break; 1915b6cee71dSXin LI case 'E': 1916b6cee71dSXin LI case 'F': 1917b6cee71dSXin LI case 'G': 1918b6cee71dSXin LI m->in_type = FILE_BEDOUBLE; 1919b6cee71dSXin LI break; 1920b6cee71dSXin LI case 'i': 1921b6cee71dSXin LI m->in_type = FILE_LEID3; 1922b6cee71dSXin LI break; 1923b6cee71dSXin LI case 'I': 1924b6cee71dSXin LI m->in_type = FILE_BEID3; 1925b6cee71dSXin LI break; 1926b6cee71dSXin LI default: 1927b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1928b6cee71dSXin LI file_magwarn(ms, 1929b6cee71dSXin LI "indirect offset type `%c' invalid", 1930b6cee71dSXin LI *l); 19315f0216bdSXin LI return -1; 1932b6cee71dSXin LI } 1933b6cee71dSXin LI l++; 1934b6cee71dSXin LI } 1935b6cee71dSXin LI 1936b6cee71dSXin LI if (*l == '~') { 1937b6cee71dSXin LI m->in_op |= FILE_OPINVERSE; 1938b6cee71dSXin LI l++; 1939b6cee71dSXin LI } 1940b6cee71dSXin LI if ((op = get_op(*l)) != -1) { 1941b6cee71dSXin LI m->in_op |= op; 1942b6cee71dSXin LI l++; 1943b6cee71dSXin LI } 1944b6cee71dSXin LI if (*l == '(') { 1945b6cee71dSXin LI m->in_op |= FILE_OPINDIRECT; 1946b6cee71dSXin LI l++; 1947b6cee71dSXin LI } 1948b6cee71dSXin LI if (isdigit((unsigned char)*l) || *l == '-') { 1949b6cee71dSXin LI m->in_offset = (int32_t)strtol(l, &t, 0); 19505f0216bdSXin LI if (l == t) { 1951b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1952b6cee71dSXin LI file_magwarn(ms, 1953b6cee71dSXin LI "in_offset `%s' invalid", l); 19545f0216bdSXin LI return -1; 19555f0216bdSXin LI } 1956b6cee71dSXin LI l = t; 1957b6cee71dSXin LI } 1958b6cee71dSXin LI if (*l++ != ')' || 19595f0216bdSXin LI ((m->in_op & FILE_OPINDIRECT) && *l++ != ')')) { 1960b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1961b6cee71dSXin LI file_magwarn(ms, 1962b6cee71dSXin LI "missing ')' in indirect offset"); 19635f0216bdSXin LI return -1; 19645f0216bdSXin LI } 1965b6cee71dSXin LI } 1966b6cee71dSXin LI EATAB; 1967b6cee71dSXin LI 1968b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS 1969b6cee71dSXin LI m->cond = get_cond(l, &l); 1970b6cee71dSXin LI if (check_cond(ms, m->cond, cont_level) == -1) 1971b6cee71dSXin LI return -1; 1972b6cee71dSXin LI 1973b6cee71dSXin LI EATAB; 1974b6cee71dSXin LI #endif 1975b6cee71dSXin LI 1976b6cee71dSXin LI /* 1977b6cee71dSXin LI * Parse the type. 1978b6cee71dSXin LI */ 1979b6cee71dSXin LI if (*l == 'u') { 1980b6cee71dSXin LI /* 1981b6cee71dSXin LI * Try it as a keyword type prefixed by "u"; match what 1982b6cee71dSXin LI * follows the "u". If that fails, try it as an SUS 1983b6cee71dSXin LI * integer type. 1984b6cee71dSXin LI */ 1985b6cee71dSXin LI m->type = get_type(type_tbl, l + 1, &l); 1986b6cee71dSXin LI if (m->type == FILE_INVALID) { 1987b6cee71dSXin LI /* 1988b6cee71dSXin LI * Not a keyword type; parse it as an SUS type, 1989b6cee71dSXin LI * 'u' possibly followed by a number or C/S/L. 1990b6cee71dSXin LI */ 1991b6cee71dSXin LI m->type = get_standard_integer_type(l, &l); 1992b6cee71dSXin LI } 1993b6cee71dSXin LI /* It's unsigned. */ 1994b6cee71dSXin LI if (m->type != FILE_INVALID) 1995b6cee71dSXin LI m->flag |= UNSIGNED; 1996b6cee71dSXin LI } else { 1997b6cee71dSXin LI /* 1998b6cee71dSXin LI * Try it as a keyword type. If that fails, try it as 1999b6cee71dSXin LI * an SUS integer type if it begins with "d" or as an 2000b6cee71dSXin LI * SUS string type if it begins with "s". In any case, 2001b6cee71dSXin LI * it's not unsigned. 2002b6cee71dSXin LI */ 2003b6cee71dSXin LI m->type = get_type(type_tbl, l, &l); 2004b6cee71dSXin LI if (m->type == FILE_INVALID) { 2005b6cee71dSXin LI /* 2006b6cee71dSXin LI * Not a keyword type; parse it as an SUS type, 2007b6cee71dSXin LI * either 'd' possibly followed by a number or 2008b6cee71dSXin LI * C/S/L, or just 's'. 2009b6cee71dSXin LI */ 2010b6cee71dSXin LI if (*l == 'd') 2011b6cee71dSXin LI m->type = get_standard_integer_type(l, &l); 2012b6cee71dSXin LI else if (*l == 's' && !isalpha((unsigned char)l[1])) { 2013b6cee71dSXin LI m->type = FILE_STRING; 2014b6cee71dSXin LI ++l; 2015b6cee71dSXin LI } 2016b6cee71dSXin LI } 2017b6cee71dSXin LI } 2018b6cee71dSXin LI 2019b6cee71dSXin LI if (m->type == FILE_INVALID) { 2020b6cee71dSXin LI /* Not found - try it as a special keyword. */ 2021b6cee71dSXin LI m->type = get_type(special_tbl, l, &l); 2022b6cee71dSXin LI } 2023b6cee71dSXin LI 2024b6cee71dSXin LI if (m->type == FILE_INVALID) { 2025b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2026b6cee71dSXin LI file_magwarn(ms, "type `%s' invalid", l); 2027b6cee71dSXin LI return -1; 2028b6cee71dSXin LI } 2029b6cee71dSXin LI 2030b6cee71dSXin LI /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */ 2031b6cee71dSXin LI /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */ 2032b6cee71dSXin LI 2033b6cee71dSXin LI m->mask_op = 0; 2034b6cee71dSXin LI if (*l == '~') { 2035b6cee71dSXin LI if (!IS_STRING(m->type)) 2036b6cee71dSXin LI m->mask_op |= FILE_OPINVERSE; 2037b6cee71dSXin LI else if (ms->flags & MAGIC_CHECK) 2038b6cee71dSXin LI file_magwarn(ms, "'~' invalid for string types"); 2039b6cee71dSXin LI ++l; 2040b6cee71dSXin LI } 2041b6cee71dSXin LI m->str_range = 0; 2042b6cee71dSXin LI m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0; 2043b6cee71dSXin LI if ((op = get_op(*l)) != -1) { 20444460e5b0SXin LI if (IS_STRING(m->type)) { 20454460e5b0SXin LI int r; 20464460e5b0SXin LI 20474460e5b0SXin LI if (op != FILE_OPDIVIDE) { 2048b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2049b6cee71dSXin LI file_magwarn(ms, 20504460e5b0SXin LI "invalid string/indirect op: " 20514460e5b0SXin LI "`%c'", *t); 2052b6cee71dSXin LI return -1; 2053b6cee71dSXin LI } 20544460e5b0SXin LI 20554460e5b0SXin LI if (m->type == FILE_INDIRECT) 20564460e5b0SXin LI r = parse_indirect_modifier(ms, m, &l); 20574460e5b0SXin LI else 20584460e5b0SXin LI r = parse_string_modifier(ms, m, &l); 20594460e5b0SXin LI if (r == -1) 2060b6cee71dSXin LI return -1; 20614460e5b0SXin LI } else 20624460e5b0SXin LI parse_op_modifier(ms, m, &l, op); 2063b6cee71dSXin LI } 20644460e5b0SXin LI 2065b6cee71dSXin LI /* 2066b6cee71dSXin LI * We used to set mask to all 1's here, instead let's just not do 2067b6cee71dSXin LI * anything if mask = 0 (unless you have a better idea) 2068b6cee71dSXin LI */ 2069b6cee71dSXin LI EATAB; 2070b6cee71dSXin LI 2071b6cee71dSXin LI switch (*l) { 2072b6cee71dSXin LI case '>': 2073b6cee71dSXin LI case '<': 2074b6cee71dSXin LI m->reln = *l; 2075b6cee71dSXin LI ++l; 2076b6cee71dSXin LI if (*l == '=') { 2077b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) { 2078b6cee71dSXin LI file_magwarn(ms, "%c= not supported", 2079b6cee71dSXin LI m->reln); 2080b6cee71dSXin LI return -1; 2081b6cee71dSXin LI } 2082b6cee71dSXin LI ++l; 2083b6cee71dSXin LI } 2084b6cee71dSXin LI break; 2085b6cee71dSXin LI /* Old-style anding: "0 byte &0x80 dynamically linked" */ 2086b6cee71dSXin LI case '&': 2087b6cee71dSXin LI case '^': 2088b6cee71dSXin LI case '=': 2089b6cee71dSXin LI m->reln = *l; 2090b6cee71dSXin LI ++l; 2091b6cee71dSXin LI if (*l == '=') { 2092b6cee71dSXin LI /* HP compat: ignore &= etc. */ 2093b6cee71dSXin LI ++l; 2094b6cee71dSXin LI } 2095b6cee71dSXin LI break; 2096b6cee71dSXin LI case '!': 2097b6cee71dSXin LI m->reln = *l; 2098b6cee71dSXin LI ++l; 2099b6cee71dSXin LI break; 2100b6cee71dSXin LI default: 2101b6cee71dSXin LI m->reln = '='; /* the default relation */ 2102b6cee71dSXin LI if (*l == 'x' && ((isascii((unsigned char)l[1]) && 2103b6cee71dSXin LI isspace((unsigned char)l[1])) || !l[1])) { 2104b6cee71dSXin LI m->reln = *l; 2105b6cee71dSXin LI ++l; 2106b6cee71dSXin LI } 2107b6cee71dSXin LI break; 2108b6cee71dSXin LI } 2109b6cee71dSXin LI /* 2110b6cee71dSXin LI * Grab the value part, except for an 'x' reln. 2111b6cee71dSXin LI */ 2112b6cee71dSXin LI if (m->reln != 'x' && getvalue(ms, m, &l, action)) 2113b6cee71dSXin LI return -1; 2114b6cee71dSXin LI 2115b6cee71dSXin LI /* 2116b6cee71dSXin LI * TODO finish this macro and start using it! 21173e41d09dSXin LI * #define offsetcheck {if (offset > ms->bytes_max -1) 2118b6cee71dSXin LI * magwarn("offset too big"); } 2119b6cee71dSXin LI */ 2120b6cee71dSXin LI 2121b6cee71dSXin LI /* 2122b6cee71dSXin LI * Now get last part - the description 2123b6cee71dSXin LI */ 2124b6cee71dSXin LI EATAB; 2125b6cee71dSXin LI if (l[0] == '\b') { 2126b6cee71dSXin LI ++l; 2127b6cee71dSXin LI m->flag |= NOSPACE; 2128b6cee71dSXin LI } else if ((l[0] == '\\') && (l[1] == 'b')) { 2129b6cee71dSXin LI ++l; 2130b6cee71dSXin LI ++l; 2131b6cee71dSXin LI m->flag |= NOSPACE; 2132b6cee71dSXin LI } 2133b6cee71dSXin LI for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); ) 2134b6cee71dSXin LI continue; 2135b6cee71dSXin LI if (i == sizeof(m->desc)) { 2136b6cee71dSXin LI m->desc[sizeof(m->desc) - 1] = '\0'; 2137b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2138b6cee71dSXin LI file_magwarn(ms, "description `%s' truncated", m->desc); 2139b6cee71dSXin LI } 2140b6cee71dSXin LI 2141b6cee71dSXin LI /* 2142b6cee71dSXin LI * We only do this check while compiling, or if any of the magic 2143b6cee71dSXin LI * files were not compiled. 2144b6cee71dSXin LI */ 2145b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) { 2146b6cee71dSXin LI if (check_format(ms, m) == -1) 2147b6cee71dSXin LI return -1; 2148b6cee71dSXin LI } 2149b6cee71dSXin LI #ifndef COMPILE_ONLY 2150b6cee71dSXin LI if (action == FILE_CHECK) { 2151b6cee71dSXin LI file_mdump(m); 2152b6cee71dSXin LI } 2153b6cee71dSXin LI #endif 2154b6cee71dSXin LI m->mimetype[0] = '\0'; /* initialise MIME type to none */ 2155b6cee71dSXin LI return 0; 2156b6cee71dSXin LI } 2157b6cee71dSXin LI 2158b6cee71dSXin LI /* 2159b6cee71dSXin LI * parse a STRENGTH annotation line from magic file, put into magic[index - 1] 2160b6cee71dSXin LI * if valid 2161b6cee71dSXin LI */ 2162b6cee71dSXin LI private int 2163b6cee71dSXin LI parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line) 2164b6cee71dSXin LI { 2165b6cee71dSXin LI const char *l = line; 2166b6cee71dSXin LI char *el; 2167b6cee71dSXin LI unsigned long factor; 2168b6cee71dSXin LI struct magic *m = &me->mp[0]; 2169b6cee71dSXin LI 2170b6cee71dSXin LI if (m->factor_op != FILE_FACTOR_OP_NONE) { 2171b6cee71dSXin LI file_magwarn(ms, 2172b6cee71dSXin LI "Current entry already has a strength type: %c %d", 2173b6cee71dSXin LI m->factor_op, m->factor); 2174b6cee71dSXin LI return -1; 2175b6cee71dSXin LI } 2176b6cee71dSXin LI if (m->type == FILE_NAME) { 2177b6cee71dSXin LI file_magwarn(ms, "%s: Strength setting is not supported in " 2178b6cee71dSXin LI "\"name\" magic entries", m->value.s); 2179b6cee71dSXin LI return -1; 2180b6cee71dSXin LI } 2181b6cee71dSXin LI EATAB; 2182b6cee71dSXin LI switch (*l) { 2183b6cee71dSXin LI case FILE_FACTOR_OP_NONE: 2184b6cee71dSXin LI case FILE_FACTOR_OP_PLUS: 2185b6cee71dSXin LI case FILE_FACTOR_OP_MINUS: 2186b6cee71dSXin LI case FILE_FACTOR_OP_TIMES: 2187b6cee71dSXin LI case FILE_FACTOR_OP_DIV: 2188b6cee71dSXin LI m->factor_op = *l++; 2189b6cee71dSXin LI break; 2190b6cee71dSXin LI default: 2191b6cee71dSXin LI file_magwarn(ms, "Unknown factor op `%c'", *l); 2192b6cee71dSXin LI return -1; 2193b6cee71dSXin LI } 2194b6cee71dSXin LI EATAB; 2195b6cee71dSXin LI factor = strtoul(l, &el, 0); 2196b6cee71dSXin LI if (factor > 255) { 2197b6cee71dSXin LI file_magwarn(ms, "Too large factor `%lu'", factor); 2198b6cee71dSXin LI goto out; 2199b6cee71dSXin LI } 2200b6cee71dSXin LI if (*el && !isspace((unsigned char)*el)) { 2201b6cee71dSXin LI file_magwarn(ms, "Bad factor `%s'", l); 2202b6cee71dSXin LI goto out; 2203b6cee71dSXin LI } 2204b6cee71dSXin LI m->factor = (uint8_t)factor; 2205b6cee71dSXin LI if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) { 2206b6cee71dSXin LI file_magwarn(ms, "Cannot have factor op `%c' and factor %u", 2207b6cee71dSXin LI m->factor_op, m->factor); 2208b6cee71dSXin LI goto out; 2209b6cee71dSXin LI } 2210b6cee71dSXin LI return 0; 2211b6cee71dSXin LI out: 2212b6cee71dSXin LI m->factor_op = FILE_FACTOR_OP_NONE; 2213b6cee71dSXin LI m->factor = 0; 2214b6cee71dSXin LI return -1; 2215b6cee71dSXin LI } 2216b6cee71dSXin LI 2217b6cee71dSXin LI private int 2218c2931133SXin LI goodchar(unsigned char x, const char *extra) 2219c2931133SXin LI { 2220c2931133SXin LI return (isascii(x) && isalnum(x)) || strchr(extra, x); 2221c2931133SXin LI } 2222c2931133SXin LI 2223c2931133SXin LI private int 2224b6cee71dSXin LI parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line, 2225c2931133SXin LI off_t off, size_t len, const char *name, const char *extra, int nt) 2226b6cee71dSXin LI { 2227b6cee71dSXin LI size_t i; 2228b6cee71dSXin LI const char *l = line; 2229b6cee71dSXin LI struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1]; 22305f0216bdSXin LI char *buf = CAST(char *, CAST(void *, m)) + off; 2231b6cee71dSXin LI 2232b6cee71dSXin LI if (buf[0] != '\0') { 2233b6cee71dSXin LI len = nt ? strlen(buf) : len; 2234b6cee71dSXin LI file_magwarn(ms, "Current entry already has a %s type " 2235b6cee71dSXin LI "`%.*s', new type `%s'", name, (int)len, buf, l); 2236b6cee71dSXin LI return -1; 2237b6cee71dSXin LI } 2238b6cee71dSXin LI 2239b6cee71dSXin LI if (*m->desc == '\0') { 2240b6cee71dSXin LI file_magwarn(ms, "Current entry does not yet have a " 2241b6cee71dSXin LI "description for adding a %s type", name); 2242b6cee71dSXin LI return -1; 2243b6cee71dSXin LI } 2244b6cee71dSXin LI 2245b6cee71dSXin LI EATAB; 2246c2931133SXin LI for (i = 0; *l && i < len && goodchar(*l, extra); buf[i++] = *l++) 2247b6cee71dSXin LI continue; 2248b6cee71dSXin LI 2249b6cee71dSXin LI if (i == len && *l) { 2250b6cee71dSXin LI if (nt) 2251b6cee71dSXin LI buf[len - 1] = '\0'; 2252b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2253b6cee71dSXin LI file_magwarn(ms, "%s type `%s' truncated %" 2254b6cee71dSXin LI SIZE_T_FORMAT "u", name, line, i); 2255b6cee71dSXin LI } else { 2256c2931133SXin LI if (!isspace((unsigned char)*l) && !goodchar(*l, extra)) 2257c2931133SXin LI file_magwarn(ms, "%s type `%s' has bad char '%c'", 2258c2931133SXin LI name, line, *l); 2259b6cee71dSXin LI if (nt) 2260b6cee71dSXin LI buf[i] = '\0'; 2261b6cee71dSXin LI } 2262b6cee71dSXin LI 2263b6cee71dSXin LI if (i > 0) 2264b6cee71dSXin LI return 0; 2265c2931133SXin LI 2266c2931133SXin LI file_magerror(ms, "Bad magic entry '%s'", line); 2267b6cee71dSXin LI return -1; 2268b6cee71dSXin LI } 2269b6cee71dSXin LI 2270b6cee71dSXin LI /* 2271b6cee71dSXin LI * Parse an Apple CREATOR/TYPE annotation from magic file and put it into 2272b6cee71dSXin LI * magic[index - 1] 2273b6cee71dSXin LI */ 2274b6cee71dSXin LI private int 2275b6cee71dSXin LI parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line) 2276b6cee71dSXin LI { 2277b6cee71dSXin LI struct magic *m = &me->mp[0]; 2278b6cee71dSXin LI 22795f0216bdSXin LI return parse_extra(ms, me, line, 22805f0216bdSXin LI CAST(off_t, offsetof(struct magic, apple)), 22813e41d09dSXin LI sizeof(m->apple), "APPLE", "!+-./?", 0); 2282b6cee71dSXin LI } 2283b6cee71dSXin LI 2284b6cee71dSXin LI /* 22855f0216bdSXin LI * Parse a comma-separated list of extensions 22865f0216bdSXin LI */ 22875f0216bdSXin LI private int 22885f0216bdSXin LI parse_ext(struct magic_set *ms, struct magic_entry *me, const char *line) 22895f0216bdSXin LI { 22905f0216bdSXin LI struct magic *m = &me->mp[0]; 22915f0216bdSXin LI 22925f0216bdSXin LI return parse_extra(ms, me, line, 22935f0216bdSXin LI CAST(off_t, offsetof(struct magic, ext)), 22945f0216bdSXin LI sizeof(m->ext), "EXTENSION", ",!+-/", 0); 22955f0216bdSXin LI } 22965f0216bdSXin LI 22975f0216bdSXin LI /* 2298b6cee71dSXin LI * parse a MIME annotation line from magic file, put into magic[index - 1] 2299b6cee71dSXin LI * if valid 2300b6cee71dSXin LI */ 2301b6cee71dSXin LI private int 2302b6cee71dSXin LI parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line) 2303b6cee71dSXin LI { 2304b6cee71dSXin LI struct magic *m = &me->mp[0]; 2305b6cee71dSXin LI 23065f0216bdSXin LI return parse_extra(ms, me, line, 23075f0216bdSXin LI CAST(off_t, offsetof(struct magic, mimetype)), 2308c2931133SXin LI sizeof(m->mimetype), "MIME", "+-/.", 1); 2309b6cee71dSXin LI } 2310b6cee71dSXin LI 2311b6cee71dSXin LI private int 23123e41d09dSXin LI check_format_type(const char *ptr, int type, const char **estr) 2313b6cee71dSXin LI { 2314b6cee71dSXin LI int quad = 0, h; 23153e41d09dSXin LI size_t len, cnt; 2316b6cee71dSXin LI if (*ptr == '\0') { 2317b6cee71dSXin LI /* Missing format string; bad */ 23183e41d09dSXin LI *estr = "missing format spec"; 2319b6cee71dSXin LI return -1; 2320b6cee71dSXin LI } 2321b6cee71dSXin LI 2322b6cee71dSXin LI switch (file_formats[type]) { 2323b6cee71dSXin LI case FILE_FMT_QUAD: 2324b6cee71dSXin LI quad = 1; 2325b6cee71dSXin LI /*FALLTHROUGH*/ 2326b6cee71dSXin LI case FILE_FMT_NUM: 2327b6cee71dSXin LI if (quad == 0) { 2328b6cee71dSXin LI switch (type) { 2329b6cee71dSXin LI case FILE_BYTE: 2330b6cee71dSXin LI h = 2; 2331b6cee71dSXin LI break; 2332b6cee71dSXin LI case FILE_SHORT: 2333b6cee71dSXin LI case FILE_BESHORT: 2334b6cee71dSXin LI case FILE_LESHORT: 2335b6cee71dSXin LI h = 1; 2336b6cee71dSXin LI break; 2337b6cee71dSXin LI case FILE_LONG: 2338b6cee71dSXin LI case FILE_BELONG: 2339b6cee71dSXin LI case FILE_LELONG: 2340b6cee71dSXin LI case FILE_MELONG: 2341b6cee71dSXin LI case FILE_LEID3: 2342b6cee71dSXin LI case FILE_BEID3: 2343b6cee71dSXin LI case FILE_INDIRECT: 2344b6cee71dSXin LI h = 0; 2345b6cee71dSXin LI break; 2346b6cee71dSXin LI default: 2347b6cee71dSXin LI abort(); 2348b6cee71dSXin LI } 2349b6cee71dSXin LI } else 2350b6cee71dSXin LI h = 0; 2351b6cee71dSXin LI if (*ptr == '-') 2352b6cee71dSXin LI ptr++; 2353b6cee71dSXin LI if (*ptr == '.') 2354b6cee71dSXin LI ptr++; 23553e41d09dSXin LI #define CHECKLEN() do { \ 23563e41d09dSXin LI for (len = cnt = 0; isdigit((unsigned char)*ptr); ptr++, cnt++) \ 23573e41d09dSXin LI len = len * 10 + (*ptr - '0'); \ 23583e41d09dSXin LI if (cnt > 5 || len > 1024) \ 23593e41d09dSXin LI goto toolong; \ 23603e41d09dSXin LI } while (/*CONSTCOND*/0) 23613e41d09dSXin LI 23623e41d09dSXin LI CHECKLEN(); 2363b6cee71dSXin LI if (*ptr == '.') 2364b6cee71dSXin LI ptr++; 23653e41d09dSXin LI CHECKLEN(); 2366b6cee71dSXin LI if (quad) { 2367b6cee71dSXin LI if (*ptr++ != 'l') 23683e41d09dSXin LI goto invalid; 2369b6cee71dSXin LI if (*ptr++ != 'l') 23703e41d09dSXin LI goto invalid; 2371b6cee71dSXin LI } 2372b6cee71dSXin LI 2373b6cee71dSXin LI switch (*ptr++) { 2374b6cee71dSXin LI #ifdef STRICT_FORMAT /* "long" formats are int formats for us */ 2375b6cee71dSXin LI /* so don't accept the 'l' modifier */ 2376b6cee71dSXin LI case 'l': 2377b6cee71dSXin LI switch (*ptr++) { 2378b6cee71dSXin LI case 'i': 2379b6cee71dSXin LI case 'd': 2380b6cee71dSXin LI case 'u': 2381b6cee71dSXin LI case 'o': 2382b6cee71dSXin LI case 'x': 2383b6cee71dSXin LI case 'X': 23843e41d09dSXin LI if (h == 0) 23853e41d09dSXin LI return 0; 23863e41d09dSXin LI /*FALLTHROUGH*/ 2387b6cee71dSXin LI default: 23883e41d09dSXin LI goto invalid; 2389b6cee71dSXin LI } 2390b6cee71dSXin LI 2391b6cee71dSXin LI /* 2392b6cee71dSXin LI * Don't accept h and hh modifiers. They make writing 2393b6cee71dSXin LI * magic entries more complicated, for very little benefit 2394b6cee71dSXin LI */ 2395b6cee71dSXin LI case 'h': 2396b6cee71dSXin LI if (h-- <= 0) 23973e41d09dSXin LI goto invalid; 2398b6cee71dSXin LI switch (*ptr++) { 2399b6cee71dSXin LI case 'h': 2400b6cee71dSXin LI if (h-- <= 0) 24013e41d09dSXin LI goto invalid; 2402b6cee71dSXin LI switch (*ptr++) { 2403b6cee71dSXin LI case 'i': 2404b6cee71dSXin LI case 'd': 2405b6cee71dSXin LI case 'u': 2406b6cee71dSXin LI case 'o': 2407b6cee71dSXin LI case 'x': 2408b6cee71dSXin LI case 'X': 2409b6cee71dSXin LI return 0; 2410b6cee71dSXin LI default: 24113e41d09dSXin LI goto invalid; 2412b6cee71dSXin LI } 2413b6cee71dSXin LI case 'i': 2414b6cee71dSXin LI case 'd': 2415b6cee71dSXin LI case 'u': 2416b6cee71dSXin LI case 'o': 2417b6cee71dSXin LI case 'x': 2418b6cee71dSXin LI case 'X': 24193e41d09dSXin LI if (h == 0) 24203e41d09dSXin LI return 0; 24213e41d09dSXin LI /*FALLTHROUGH*/ 2422b6cee71dSXin LI default: 24233e41d09dSXin LI goto invalid; 2424b6cee71dSXin LI } 2425b6cee71dSXin LI #endif 2426b6cee71dSXin LI case 'c': 24273e41d09dSXin LI if (h == 2) 24283e41d09dSXin LI return 0; 24293e41d09dSXin LI goto invalid; 2430b6cee71dSXin LI case 'i': 2431b6cee71dSXin LI case 'd': 2432b6cee71dSXin LI case 'u': 2433b6cee71dSXin LI case 'o': 2434b6cee71dSXin LI case 'x': 2435b6cee71dSXin LI case 'X': 2436b6cee71dSXin LI #ifdef STRICT_FORMAT 24373e41d09dSXin LI if (h == 0) 24383e41d09dSXin LI return 0; 24393e41d09dSXin LI /*FALLTHROUGH*/ 2440b6cee71dSXin LI #else 2441b6cee71dSXin LI return 0; 2442b6cee71dSXin LI #endif 2443b6cee71dSXin LI default: 24443e41d09dSXin LI goto invalid; 2445b6cee71dSXin LI } 2446b6cee71dSXin LI 2447b6cee71dSXin LI case FILE_FMT_FLOAT: 2448b6cee71dSXin LI case FILE_FMT_DOUBLE: 2449b6cee71dSXin LI if (*ptr == '-') 2450b6cee71dSXin LI ptr++; 2451b6cee71dSXin LI if (*ptr == '.') 2452b6cee71dSXin LI ptr++; 24533e41d09dSXin LI CHECKLEN(); 2454b6cee71dSXin LI if (*ptr == '.') 2455b6cee71dSXin LI ptr++; 24563e41d09dSXin LI CHECKLEN(); 2457b6cee71dSXin LI switch (*ptr++) { 2458b6cee71dSXin LI case 'e': 2459b6cee71dSXin LI case 'E': 2460b6cee71dSXin LI case 'f': 2461b6cee71dSXin LI case 'F': 2462b6cee71dSXin LI case 'g': 2463b6cee71dSXin LI case 'G': 2464b6cee71dSXin LI return 0; 2465b6cee71dSXin LI 2466b6cee71dSXin LI default: 24673e41d09dSXin LI goto invalid; 2468b6cee71dSXin LI } 2469b6cee71dSXin LI 2470b6cee71dSXin LI 2471b6cee71dSXin LI case FILE_FMT_STR: 2472b6cee71dSXin LI if (*ptr == '-') 2473b6cee71dSXin LI ptr++; 2474b6cee71dSXin LI while (isdigit((unsigned char )*ptr)) 2475b6cee71dSXin LI ptr++; 2476b6cee71dSXin LI if (*ptr == '.') { 2477b6cee71dSXin LI ptr++; 2478b6cee71dSXin LI while (isdigit((unsigned char )*ptr)) 2479b6cee71dSXin LI ptr++; 2480b6cee71dSXin LI } 2481b6cee71dSXin LI 2482b6cee71dSXin LI switch (*ptr++) { 2483b6cee71dSXin LI case 's': 2484b6cee71dSXin LI return 0; 2485b6cee71dSXin LI default: 24863e41d09dSXin LI goto invalid; 2487b6cee71dSXin LI } 2488b6cee71dSXin LI 2489b6cee71dSXin LI default: 2490b6cee71dSXin LI /* internal error */ 2491b6cee71dSXin LI abort(); 2492b6cee71dSXin LI } 24933e41d09dSXin LI invalid: 24943e41d09dSXin LI *estr = "not valid"; 24953e41d09dSXin LI toolong: 24963e41d09dSXin LI *estr = "too long"; 2497b6cee71dSXin LI return -1; 2498b6cee71dSXin LI } 2499b6cee71dSXin LI 2500b6cee71dSXin LI /* 2501b6cee71dSXin LI * Check that the optional printf format in description matches 2502b6cee71dSXin LI * the type of the magic. 2503b6cee71dSXin LI */ 2504b6cee71dSXin LI private int 2505b6cee71dSXin LI check_format(struct magic_set *ms, struct magic *m) 2506b6cee71dSXin LI { 2507b6cee71dSXin LI char *ptr; 25083e41d09dSXin LI const char *estr; 2509b6cee71dSXin LI 2510b6cee71dSXin LI for (ptr = m->desc; *ptr; ptr++) 2511b6cee71dSXin LI if (*ptr == '%') 2512b6cee71dSXin LI break; 2513b6cee71dSXin LI if (*ptr == '\0') { 2514b6cee71dSXin LI /* No format string; ok */ 2515b6cee71dSXin LI return 1; 2516b6cee71dSXin LI } 2517b6cee71dSXin LI 2518b6cee71dSXin LI assert(file_nformats == file_nnames); 2519b6cee71dSXin LI 2520b6cee71dSXin LI if (m->type >= file_nformats) { 2521b6cee71dSXin LI file_magwarn(ms, "Internal error inconsistency between " 2522b6cee71dSXin LI "m->type and format strings"); 2523b6cee71dSXin LI return -1; 2524b6cee71dSXin LI } 2525b6cee71dSXin LI if (file_formats[m->type] == FILE_FMT_NONE) { 2526b6cee71dSXin LI file_magwarn(ms, "No format string for `%s' with description " 2527b6cee71dSXin LI "`%s'", m->desc, file_names[m->type]); 2528b6cee71dSXin LI return -1; 2529b6cee71dSXin LI } 2530b6cee71dSXin LI 2531b6cee71dSXin LI ptr++; 25323e41d09dSXin LI if (check_format_type(ptr, m->type, &estr) == -1) { 2533b6cee71dSXin LI /* 2534b6cee71dSXin LI * TODO: this error message is unhelpful if the format 2535b6cee71dSXin LI * string is not one character long 2536b6cee71dSXin LI */ 25373e41d09dSXin LI file_magwarn(ms, "Printf format is %s for type " 25383e41d09dSXin LI "`%s' in description `%s'", estr, 2539b6cee71dSXin LI file_names[m->type], m->desc); 2540b6cee71dSXin LI return -1; 2541b6cee71dSXin LI } 2542b6cee71dSXin LI 2543b6cee71dSXin LI for (; *ptr; ptr++) { 2544b6cee71dSXin LI if (*ptr == '%') { 2545b6cee71dSXin LI file_magwarn(ms, 2546b6cee71dSXin LI "Too many format strings (should have at most one) " 2547b6cee71dSXin LI "for `%s' with description `%s'", 2548b6cee71dSXin LI file_names[m->type], m->desc); 2549b6cee71dSXin LI return -1; 2550b6cee71dSXin LI } 2551b6cee71dSXin LI } 2552b6cee71dSXin LI return 0; 2553b6cee71dSXin LI } 2554b6cee71dSXin LI 2555b6cee71dSXin LI /* 2556b6cee71dSXin LI * Read a numeric value from a pointer, into the value union of a magic 2557b6cee71dSXin LI * pointer, according to the magic type. Update the string pointer to point 2558b6cee71dSXin LI * just after the number read. Return 0 for success, non-zero for failure. 2559b6cee71dSXin LI */ 2560b6cee71dSXin LI private int 2561b6cee71dSXin LI getvalue(struct magic_set *ms, struct magic *m, const char **p, int action) 2562b6cee71dSXin LI { 2563b6cee71dSXin LI switch (m->type) { 2564b6cee71dSXin LI case FILE_BESTRING16: 2565b6cee71dSXin LI case FILE_LESTRING16: 2566b6cee71dSXin LI case FILE_STRING: 2567b6cee71dSXin LI case FILE_PSTRING: 2568b6cee71dSXin LI case FILE_REGEX: 2569b6cee71dSXin LI case FILE_SEARCH: 2570b6cee71dSXin LI case FILE_NAME: 2571b6cee71dSXin LI case FILE_USE: 25723e41d09dSXin LI case FILE_DER: 2573b6cee71dSXin LI *p = getstr(ms, m, *p, action == FILE_COMPILE); 2574b6cee71dSXin LI if (*p == NULL) { 2575b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2576b6cee71dSXin LI file_magwarn(ms, "cannot get string from `%s'", 2577b6cee71dSXin LI m->value.s); 2578b6cee71dSXin LI return -1; 2579b6cee71dSXin LI } 2580b6cee71dSXin LI if (m->type == FILE_REGEX) { 2581b6cee71dSXin LI file_regex_t rx; 2582b6cee71dSXin LI int rc = file_regcomp(&rx, m->value.s, REG_EXTENDED); 2583b6cee71dSXin LI if (rc) { 2584b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2585b6cee71dSXin LI file_regerror(&rx, rc, ms); 2586b6cee71dSXin LI } 2587b6cee71dSXin LI file_regfree(&rx); 2588b6cee71dSXin LI return rc ? -1 : 0; 2589b6cee71dSXin LI } 2590b6cee71dSXin LI return 0; 2591b6cee71dSXin LI case FILE_FLOAT: 2592b6cee71dSXin LI case FILE_BEFLOAT: 2593b6cee71dSXin LI case FILE_LEFLOAT: 2594b6cee71dSXin LI if (m->reln != 'x') { 2595b6cee71dSXin LI char *ep; 25969ce06829SXin LI errno = 0; 2597b6cee71dSXin LI #ifdef HAVE_STRTOF 2598b6cee71dSXin LI m->value.f = strtof(*p, &ep); 2599b6cee71dSXin LI #else 2600b6cee71dSXin LI m->value.f = (float)strtod(*p, &ep); 2601b6cee71dSXin LI #endif 26029ce06829SXin LI if (errno == 0) 2603b6cee71dSXin LI *p = ep; 2604b6cee71dSXin LI } 2605b6cee71dSXin LI return 0; 2606b6cee71dSXin LI case FILE_DOUBLE: 2607b6cee71dSXin LI case FILE_BEDOUBLE: 2608b6cee71dSXin LI case FILE_LEDOUBLE: 2609b6cee71dSXin LI if (m->reln != 'x') { 2610b6cee71dSXin LI char *ep; 26119ce06829SXin LI errno = 0; 2612b6cee71dSXin LI m->value.d = strtod(*p, &ep); 26139ce06829SXin LI if (errno == 0) 2614b6cee71dSXin LI *p = ep; 2615b6cee71dSXin LI } 2616b6cee71dSXin LI return 0; 2617b6cee71dSXin LI default: 2618b6cee71dSXin LI if (m->reln != 'x') { 2619b6cee71dSXin LI char *ep; 26209ce06829SXin LI errno = 0; 2621b6cee71dSXin LI m->value.q = file_signextend(ms, m, 2622b6cee71dSXin LI (uint64_t)strtoull(*p, &ep, 0)); 26239ce06829SXin LI if (errno == 0) { 2624b6cee71dSXin LI *p = ep; 2625b6cee71dSXin LI eatsize(p); 2626b6cee71dSXin LI } 26279ce06829SXin LI } 2628b6cee71dSXin LI return 0; 2629b6cee71dSXin LI } 2630b6cee71dSXin LI } 2631b6cee71dSXin LI 2632b6cee71dSXin LI /* 2633b6cee71dSXin LI * Convert a string containing C character escapes. Stop at an unescaped 2634b6cee71dSXin LI * space or tab. 2635b6cee71dSXin LI * Copy the converted version to "m->value.s", and the length in m->vallen. 2636b6cee71dSXin LI * Return updated scan pointer as function result. Warn if set. 2637b6cee71dSXin LI */ 2638b6cee71dSXin LI private const char * 2639b6cee71dSXin LI getstr(struct magic_set *ms, struct magic *m, const char *s, int warn) 2640b6cee71dSXin LI { 2641b6cee71dSXin LI const char *origs = s; 2642b6cee71dSXin LI char *p = m->value.s; 2643b6cee71dSXin LI size_t plen = sizeof(m->value.s); 2644b6cee71dSXin LI char *origp = p; 2645b6cee71dSXin LI char *pmax = p + plen - 1; 2646b6cee71dSXin LI int c; 2647b6cee71dSXin LI int val; 2648b6cee71dSXin LI 2649b6cee71dSXin LI while ((c = *s++) != '\0') { 2650b6cee71dSXin LI if (isspace((unsigned char) c)) 2651b6cee71dSXin LI break; 2652b6cee71dSXin LI if (p >= pmax) { 2653b6cee71dSXin LI file_error(ms, 0, "string too long: `%s'", origs); 2654b6cee71dSXin LI return NULL; 2655b6cee71dSXin LI } 2656b6cee71dSXin LI if (c == '\\') { 2657b6cee71dSXin LI switch(c = *s++) { 2658b6cee71dSXin LI 2659b6cee71dSXin LI case '\0': 2660b6cee71dSXin LI if (warn) 2661b6cee71dSXin LI file_magwarn(ms, "incomplete escape"); 26629ce06829SXin LI s--; 2663b6cee71dSXin LI goto out; 2664b6cee71dSXin LI 2665b6cee71dSXin LI case '\t': 2666b6cee71dSXin LI if (warn) { 2667b6cee71dSXin LI file_magwarn(ms, 2668b6cee71dSXin LI "escaped tab found, use \\t instead"); 2669b6cee71dSXin LI warn = 0; /* already did */ 2670b6cee71dSXin LI } 2671b6cee71dSXin LI /*FALLTHROUGH*/ 2672b6cee71dSXin LI default: 2673b6cee71dSXin LI if (warn) { 2674b6cee71dSXin LI if (isprint((unsigned char)c)) { 2675b6cee71dSXin LI /* Allow escaping of 2676b6cee71dSXin LI * ``relations'' */ 2677b6cee71dSXin LI if (strchr("<>&^=!", c) == NULL 2678b6cee71dSXin LI && (m->type != FILE_REGEX || 2679b6cee71dSXin LI strchr("[]().*?^$|{}", c) 2680b6cee71dSXin LI == NULL)) { 2681b6cee71dSXin LI file_magwarn(ms, "no " 2682b6cee71dSXin LI "need to escape " 2683b6cee71dSXin LI "`%c'", c); 2684b6cee71dSXin LI } 2685b6cee71dSXin LI } else { 2686b6cee71dSXin LI file_magwarn(ms, 2687b6cee71dSXin LI "unknown escape sequence: " 2688b6cee71dSXin LI "\\%03o", c); 2689b6cee71dSXin LI } 2690b6cee71dSXin LI } 2691b6cee71dSXin LI /*FALLTHROUGH*/ 2692b6cee71dSXin LI /* space, perhaps force people to use \040? */ 2693b6cee71dSXin LI case ' ': 2694b6cee71dSXin LI #if 0 2695b6cee71dSXin LI /* 2696b6cee71dSXin LI * Other things people escape, but shouldn't need to, 2697b6cee71dSXin LI * so we disallow them 2698b6cee71dSXin LI */ 2699b6cee71dSXin LI case '\'': 2700b6cee71dSXin LI case '"': 2701b6cee71dSXin LI case '?': 2702b6cee71dSXin LI #endif 2703b6cee71dSXin LI /* Relations */ 2704b6cee71dSXin LI case '>': 2705b6cee71dSXin LI case '<': 2706b6cee71dSXin LI case '&': 2707b6cee71dSXin LI case '^': 2708b6cee71dSXin LI case '=': 2709b6cee71dSXin LI case '!': 2710b6cee71dSXin LI /* and baskslash itself */ 2711b6cee71dSXin LI case '\\': 2712b6cee71dSXin LI *p++ = (char) c; 2713b6cee71dSXin LI break; 2714b6cee71dSXin LI 2715b6cee71dSXin LI case 'a': 2716b6cee71dSXin LI *p++ = '\a'; 2717b6cee71dSXin LI break; 2718b6cee71dSXin LI 2719b6cee71dSXin LI case 'b': 2720b6cee71dSXin LI *p++ = '\b'; 2721b6cee71dSXin LI break; 2722b6cee71dSXin LI 2723b6cee71dSXin LI case 'f': 2724b6cee71dSXin LI *p++ = '\f'; 2725b6cee71dSXin LI break; 2726b6cee71dSXin LI 2727b6cee71dSXin LI case 'n': 2728b6cee71dSXin LI *p++ = '\n'; 2729b6cee71dSXin LI break; 2730b6cee71dSXin LI 2731b6cee71dSXin LI case 'r': 2732b6cee71dSXin LI *p++ = '\r'; 2733b6cee71dSXin LI break; 2734b6cee71dSXin LI 2735b6cee71dSXin LI case 't': 2736b6cee71dSXin LI *p++ = '\t'; 2737b6cee71dSXin LI break; 2738b6cee71dSXin LI 2739b6cee71dSXin LI case 'v': 2740b6cee71dSXin LI *p++ = '\v'; 2741b6cee71dSXin LI break; 2742b6cee71dSXin LI 2743b6cee71dSXin LI /* \ and up to 3 octal digits */ 2744b6cee71dSXin LI case '0': 2745b6cee71dSXin LI case '1': 2746b6cee71dSXin LI case '2': 2747b6cee71dSXin LI case '3': 2748b6cee71dSXin LI case '4': 2749b6cee71dSXin LI case '5': 2750b6cee71dSXin LI case '6': 2751b6cee71dSXin LI case '7': 2752b6cee71dSXin LI val = c - '0'; 2753b6cee71dSXin LI c = *s++; /* try for 2 */ 2754b6cee71dSXin LI if (c >= '0' && c <= '7') { 2755b6cee71dSXin LI val = (val << 3) | (c - '0'); 2756b6cee71dSXin LI c = *s++; /* try for 3 */ 2757b6cee71dSXin LI if (c >= '0' && c <= '7') 2758b6cee71dSXin LI val = (val << 3) | (c-'0'); 2759b6cee71dSXin LI else 2760b6cee71dSXin LI --s; 2761b6cee71dSXin LI } 2762b6cee71dSXin LI else 2763b6cee71dSXin LI --s; 2764b6cee71dSXin LI *p++ = (char)val; 2765b6cee71dSXin LI break; 2766b6cee71dSXin LI 2767b6cee71dSXin LI /* \x and up to 2 hex digits */ 2768b6cee71dSXin LI case 'x': 2769b6cee71dSXin LI val = 'x'; /* Default if no digits */ 2770b6cee71dSXin LI c = hextoint(*s++); /* Get next char */ 2771b6cee71dSXin LI if (c >= 0) { 2772b6cee71dSXin LI val = c; 2773b6cee71dSXin LI c = hextoint(*s++); 2774b6cee71dSXin LI if (c >= 0) 2775b6cee71dSXin LI val = (val << 4) + c; 2776b6cee71dSXin LI else 2777b6cee71dSXin LI --s; 2778b6cee71dSXin LI } else 2779b6cee71dSXin LI --s; 2780b6cee71dSXin LI *p++ = (char)val; 2781b6cee71dSXin LI break; 2782b6cee71dSXin LI } 2783b6cee71dSXin LI } else 2784b6cee71dSXin LI *p++ = (char)c; 2785b6cee71dSXin LI } 27869ce06829SXin LI --s; 2787b6cee71dSXin LI out: 2788b6cee71dSXin LI *p = '\0'; 2789b6cee71dSXin LI m->vallen = CAST(unsigned char, (p - origp)); 2790b6cee71dSXin LI if (m->type == FILE_PSTRING) 2791b6cee71dSXin LI m->vallen += (unsigned char)file_pstring_length_size(m); 2792b6cee71dSXin LI return s; 2793b6cee71dSXin LI } 2794b6cee71dSXin LI 2795b6cee71dSXin LI 2796b6cee71dSXin LI /* Single hex char to int; -1 if not a hex char. */ 2797b6cee71dSXin LI private int 2798b6cee71dSXin LI hextoint(int c) 2799b6cee71dSXin LI { 2800b6cee71dSXin LI if (!isascii((unsigned char) c)) 2801b6cee71dSXin LI return -1; 2802b6cee71dSXin LI if (isdigit((unsigned char) c)) 2803b6cee71dSXin LI return c - '0'; 2804b6cee71dSXin LI if ((c >= 'a') && (c <= 'f')) 2805b6cee71dSXin LI return c + 10 - 'a'; 2806b6cee71dSXin LI if (( c>= 'A') && (c <= 'F')) 2807b6cee71dSXin LI return c + 10 - 'A'; 2808b6cee71dSXin LI return -1; 2809b6cee71dSXin LI } 2810b6cee71dSXin LI 2811b6cee71dSXin LI 2812b6cee71dSXin LI /* 2813b6cee71dSXin LI * Print a string containing C character escapes. 2814b6cee71dSXin LI */ 2815b6cee71dSXin LI protected void 2816b6cee71dSXin LI file_showstr(FILE *fp, const char *s, size_t len) 2817b6cee71dSXin LI { 2818b6cee71dSXin LI char c; 2819b6cee71dSXin LI 2820b6cee71dSXin LI for (;;) { 2821b6cee71dSXin LI if (len == ~0U) { 2822b6cee71dSXin LI c = *s++; 2823b6cee71dSXin LI if (c == '\0') 2824b6cee71dSXin LI break; 2825b6cee71dSXin LI } 2826b6cee71dSXin LI else { 2827b6cee71dSXin LI if (len-- == 0) 2828b6cee71dSXin LI break; 2829b6cee71dSXin LI c = *s++; 2830b6cee71dSXin LI } 2831b6cee71dSXin LI if (c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */ 2832b6cee71dSXin LI (void) fputc(c, fp); 2833b6cee71dSXin LI else { 2834b6cee71dSXin LI (void) fputc('\\', fp); 2835b6cee71dSXin LI switch (c) { 2836b6cee71dSXin LI case '\a': 2837b6cee71dSXin LI (void) fputc('a', fp); 2838b6cee71dSXin LI break; 2839b6cee71dSXin LI 2840b6cee71dSXin LI case '\b': 2841b6cee71dSXin LI (void) fputc('b', fp); 2842b6cee71dSXin LI break; 2843b6cee71dSXin LI 2844b6cee71dSXin LI case '\f': 2845b6cee71dSXin LI (void) fputc('f', fp); 2846b6cee71dSXin LI break; 2847b6cee71dSXin LI 2848b6cee71dSXin LI case '\n': 2849b6cee71dSXin LI (void) fputc('n', fp); 2850b6cee71dSXin LI break; 2851b6cee71dSXin LI 2852b6cee71dSXin LI case '\r': 2853b6cee71dSXin LI (void) fputc('r', fp); 2854b6cee71dSXin LI break; 2855b6cee71dSXin LI 2856b6cee71dSXin LI case '\t': 2857b6cee71dSXin LI (void) fputc('t', fp); 2858b6cee71dSXin LI break; 2859b6cee71dSXin LI 2860b6cee71dSXin LI case '\v': 2861b6cee71dSXin LI (void) fputc('v', fp); 2862b6cee71dSXin LI break; 2863b6cee71dSXin LI 2864b6cee71dSXin LI default: 2865b6cee71dSXin LI (void) fprintf(fp, "%.3o", c & 0377); 2866b6cee71dSXin LI break; 2867b6cee71dSXin LI } 2868b6cee71dSXin LI } 2869b6cee71dSXin LI } 2870b6cee71dSXin LI } 2871b6cee71dSXin LI 2872b6cee71dSXin LI /* 2873b6cee71dSXin LI * eatsize(): Eat the size spec from a number [eg. 10UL] 2874b6cee71dSXin LI */ 2875b6cee71dSXin LI private void 2876b6cee71dSXin LI eatsize(const char **p) 2877b6cee71dSXin LI { 2878b6cee71dSXin LI const char *l = *p; 2879b6cee71dSXin LI 2880b6cee71dSXin LI if (LOWCASE(*l) == 'u') 2881b6cee71dSXin LI l++; 2882b6cee71dSXin LI 2883b6cee71dSXin LI switch (LOWCASE(*l)) { 2884b6cee71dSXin LI case 'l': /* long */ 2885b6cee71dSXin LI case 's': /* short */ 2886b6cee71dSXin LI case 'h': /* short */ 2887b6cee71dSXin LI case 'b': /* char/byte */ 2888b6cee71dSXin LI case 'c': /* char/byte */ 2889b6cee71dSXin LI l++; 2890b6cee71dSXin LI /*FALLTHROUGH*/ 2891b6cee71dSXin LI default: 2892b6cee71dSXin LI break; 2893b6cee71dSXin LI } 2894b6cee71dSXin LI 2895b6cee71dSXin LI *p = l; 2896b6cee71dSXin LI } 2897b6cee71dSXin LI 2898b6cee71dSXin LI /* 2899c2931133SXin LI * handle a buffer containing a compiled file. 2900c2931133SXin LI */ 2901c2931133SXin LI private struct magic_map * 2902c2931133SXin LI apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len) 2903c2931133SXin LI { 2904c2931133SXin LI struct magic_map *map; 2905c2931133SXin LI 2906c2931133SXin LI if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) { 2907c2931133SXin LI file_oomem(ms, sizeof(*map)); 2908c2931133SXin LI return NULL; 2909c2931133SXin LI } 2910c2931133SXin LI map->len = len; 2911c2931133SXin LI map->p = buf; 2912c2931133SXin LI map->type = MAP_TYPE_USER; 2913c2931133SXin LI if (check_buffer(ms, map, "buffer") != 0) { 2914c2931133SXin LI apprentice_unmap(map); 2915c2931133SXin LI return NULL; 2916c2931133SXin LI } 2917c2931133SXin LI return map; 2918c2931133SXin LI } 2919c2931133SXin LI 2920c2931133SXin LI /* 2921b6cee71dSXin LI * handle a compiled file. 2922b6cee71dSXin LI */ 2923b6cee71dSXin LI 2924b6cee71dSXin LI private struct magic_map * 2925b6cee71dSXin LI apprentice_map(struct magic_set *ms, const char *fn) 2926b6cee71dSXin LI { 2927b6cee71dSXin LI int fd; 2928b6cee71dSXin LI struct stat st; 2929b6cee71dSXin LI char *dbname = NULL; 2930b6cee71dSXin LI struct magic_map *map; 2931*a5d223e6SXin LI struct magic_map *rv = NULL; 2932b6cee71dSXin LI 2933b6cee71dSXin LI fd = -1; 2934b6cee71dSXin LI if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) { 2935b6cee71dSXin LI file_oomem(ms, sizeof(*map)); 2936b6cee71dSXin LI goto error; 2937b6cee71dSXin LI } 29383e41d09dSXin LI map->type = MAP_TYPE_USER; /* unspecified */ 2939b6cee71dSXin LI 2940b6cee71dSXin LI dbname = mkdbname(ms, fn, 0); 2941b6cee71dSXin LI if (dbname == NULL) 2942b6cee71dSXin LI goto error; 2943b6cee71dSXin LI 2944b6cee71dSXin LI if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1) 2945b6cee71dSXin LI goto error; 2946b6cee71dSXin LI 2947b6cee71dSXin LI if (fstat(fd, &st) == -1) { 2948b6cee71dSXin LI file_error(ms, errno, "cannot stat `%s'", dbname); 2949b6cee71dSXin LI goto error; 2950b6cee71dSXin LI } 2951b6cee71dSXin LI if (st.st_size < 8 || st.st_size > MAXMAGIC_SIZE) { 2952b6cee71dSXin LI file_error(ms, 0, "file `%s' is too %s", dbname, 2953b6cee71dSXin LI st.st_size < 8 ? "small" : "large"); 2954b6cee71dSXin LI goto error; 2955b6cee71dSXin LI } 2956b6cee71dSXin LI 2957b6cee71dSXin LI map->len = (size_t)st.st_size; 2958b6cee71dSXin LI #ifdef QUICK 29593e41d09dSXin LI map->type = MAP_TYPE_MMAP; 2960b6cee71dSXin LI if ((map->p = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE, 2961b6cee71dSXin LI MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) { 2962b6cee71dSXin LI file_error(ms, errno, "cannot map `%s'", dbname); 2963b6cee71dSXin LI goto error; 2964b6cee71dSXin LI } 2965b6cee71dSXin LI #else 29663e41d09dSXin LI map->type = MAP_TYPE_MALLOC; 2967b6cee71dSXin LI if ((map->p = CAST(void *, malloc(map->len))) == NULL) { 2968b6cee71dSXin LI file_oomem(ms, map->len); 2969b6cee71dSXin LI goto error; 2970b6cee71dSXin LI } 2971b6cee71dSXin LI if (read(fd, map->p, map->len) != (ssize_t)map->len) { 2972b6cee71dSXin LI file_badread(ms); 2973b6cee71dSXin LI goto error; 2974b6cee71dSXin LI } 2975b6cee71dSXin LI #define RET 1 2976b6cee71dSXin LI #endif 2977b6cee71dSXin LI (void)close(fd); 2978b6cee71dSXin LI fd = -1; 2979c2931133SXin LI 2980*a5d223e6SXin LI if (check_buffer(ms, map, dbname) != 0) { 2981*a5d223e6SXin LI rv = (struct magic_map *)-1; 2982c2931133SXin LI goto error; 2983*a5d223e6SXin LI } 29843e41d09dSXin LI #ifdef QUICK 29853e41d09dSXin LI if (mprotect(map->p, (size_t)st.st_size, PROT_READ) == -1) { 29863e41d09dSXin LI file_error(ms, errno, "cannot mprotect `%s'", dbname); 29873e41d09dSXin LI goto error; 29883e41d09dSXin LI } 29893e41d09dSXin LI #endif 2990c2931133SXin LI 2991c2931133SXin LI free(dbname); 2992c2931133SXin LI return map; 2993c2931133SXin LI 2994c2931133SXin LI error: 2995c2931133SXin LI if (fd != -1) 2996c2931133SXin LI (void)close(fd); 2997c2931133SXin LI apprentice_unmap(map); 2998c2931133SXin LI free(dbname); 2999*a5d223e6SXin LI return rv; 3000c2931133SXin LI } 3001c2931133SXin LI 3002c2931133SXin LI private int 3003c2931133SXin LI check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname) 3004c2931133SXin LI { 3005c2931133SXin LI uint32_t *ptr; 3006c2931133SXin LI uint32_t entries, nentries; 3007c2931133SXin LI uint32_t version; 3008c2931133SXin LI int i, needsbyteswap; 3009c2931133SXin LI 3010b6cee71dSXin LI ptr = CAST(uint32_t *, map->p); 3011b6cee71dSXin LI if (*ptr != MAGICNO) { 3012b6cee71dSXin LI if (swap4(*ptr) != MAGICNO) { 3013b6cee71dSXin LI file_error(ms, 0, "bad magic in `%s'", dbname); 3014c2931133SXin LI return -1; 3015b6cee71dSXin LI } 3016b6cee71dSXin LI needsbyteswap = 1; 3017b6cee71dSXin LI } else 3018b6cee71dSXin LI needsbyteswap = 0; 3019b6cee71dSXin LI if (needsbyteswap) 3020b6cee71dSXin LI version = swap4(ptr[1]); 3021b6cee71dSXin LI else 3022b6cee71dSXin LI version = ptr[1]; 3023b6cee71dSXin LI if (version != VERSIONNO) { 3024b6cee71dSXin LI file_error(ms, 0, "File %s supports only version %d magic " 3025b6cee71dSXin LI "files. `%s' is version %d", VERSION, 3026b6cee71dSXin LI VERSIONNO, dbname, version); 3027c2931133SXin LI return -1; 3028b6cee71dSXin LI } 3029c2931133SXin LI entries = (uint32_t)(map->len / sizeof(struct magic)); 3030c2931133SXin LI if ((entries * sizeof(struct magic)) != map->len) { 3031c2931133SXin LI file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not " 3032b6cee71dSXin LI "a multiple of %" SIZE_T_FORMAT "u", 3033c2931133SXin LI dbname, map->len, sizeof(struct magic)); 3034c2931133SXin LI return -1; 3035b6cee71dSXin LI } 3036b6cee71dSXin LI map->magic[0] = CAST(struct magic *, map->p) + 1; 3037b6cee71dSXin LI nentries = 0; 3038b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 3039b6cee71dSXin LI if (needsbyteswap) 3040b6cee71dSXin LI map->nmagic[i] = swap4(ptr[i + 2]); 3041b6cee71dSXin LI else 3042b6cee71dSXin LI map->nmagic[i] = ptr[i + 2]; 3043b6cee71dSXin LI if (i != MAGIC_SETS - 1) 3044b6cee71dSXin LI map->magic[i + 1] = map->magic[i] + map->nmagic[i]; 3045b6cee71dSXin LI nentries += map->nmagic[i]; 3046b6cee71dSXin LI } 3047b6cee71dSXin LI if (entries != nentries + 1) { 3048b6cee71dSXin LI file_error(ms, 0, "Inconsistent entries in `%s' %u != %u", 3049b6cee71dSXin LI dbname, entries, nentries + 1); 3050c2931133SXin LI return -1; 3051b6cee71dSXin LI } 3052b6cee71dSXin LI if (needsbyteswap) 3053b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) 3054b6cee71dSXin LI byteswap(map->magic[i], map->nmagic[i]); 3055c2931133SXin LI return 0; 3056b6cee71dSXin LI } 3057b6cee71dSXin LI 3058b6cee71dSXin LI /* 3059b6cee71dSXin LI * handle an mmaped file. 3060b6cee71dSXin LI */ 3061b6cee71dSXin LI private int 3062b6cee71dSXin LI apprentice_compile(struct magic_set *ms, struct magic_map *map, const char *fn) 3063b6cee71dSXin LI { 3064b6cee71dSXin LI static const size_t nm = sizeof(*map->nmagic) * MAGIC_SETS; 3065b6cee71dSXin LI static const size_t m = sizeof(**map->magic); 3066b6cee71dSXin LI int fd = -1; 3067b6cee71dSXin LI size_t len; 3068b6cee71dSXin LI char *dbname; 3069b6cee71dSXin LI int rv = -1; 3070b6cee71dSXin LI uint32_t i; 3071b6cee71dSXin LI union { 3072b6cee71dSXin LI struct magic m; 3073b6cee71dSXin LI uint32_t h[2 + MAGIC_SETS]; 3074b6cee71dSXin LI } hdr; 3075b6cee71dSXin LI 3076b6cee71dSXin LI dbname = mkdbname(ms, fn, 1); 3077b6cee71dSXin LI 3078b6cee71dSXin LI if (dbname == NULL) 3079b6cee71dSXin LI goto out; 3080b6cee71dSXin LI 3081b6cee71dSXin LI if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) 3082b6cee71dSXin LI { 3083b6cee71dSXin LI file_error(ms, errno, "cannot open `%s'", dbname); 3084b6cee71dSXin LI goto out; 3085b6cee71dSXin LI } 3086b6cee71dSXin LI memset(&hdr, 0, sizeof(hdr)); 3087b6cee71dSXin LI hdr.h[0] = MAGICNO; 3088b6cee71dSXin LI hdr.h[1] = VERSIONNO; 3089b6cee71dSXin LI memcpy(hdr.h + 2, map->nmagic, nm); 3090b6cee71dSXin LI 3091b6cee71dSXin LI if (write(fd, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) { 3092b6cee71dSXin LI file_error(ms, errno, "error writing `%s'", dbname); 3093b6cee71dSXin LI goto out; 3094b6cee71dSXin LI } 3095b6cee71dSXin LI 3096b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 3097b6cee71dSXin LI len = m * map->nmagic[i]; 3098b6cee71dSXin LI if (write(fd, map->magic[i], len) != (ssize_t)len) { 3099b6cee71dSXin LI file_error(ms, errno, "error writing `%s'", dbname); 3100b6cee71dSXin LI goto out; 3101b6cee71dSXin LI } 3102b6cee71dSXin LI } 3103b6cee71dSXin LI 3104b6cee71dSXin LI if (fd != -1) 3105b6cee71dSXin LI (void)close(fd); 3106b6cee71dSXin LI rv = 0; 3107b6cee71dSXin LI out: 3108282e23f0SXin LI apprentice_unmap(map); 3109b6cee71dSXin LI free(dbname); 3110b6cee71dSXin LI return rv; 3111b6cee71dSXin LI } 3112b6cee71dSXin LI 3113b6cee71dSXin LI private const char ext[] = ".mgc"; 3114b6cee71dSXin LI /* 3115b6cee71dSXin LI * make a dbname 3116b6cee71dSXin LI */ 3117b6cee71dSXin LI private char * 3118b6cee71dSXin LI mkdbname(struct magic_set *ms, const char *fn, int strip) 3119b6cee71dSXin LI { 3120b6cee71dSXin LI const char *p, *q; 3121b6cee71dSXin LI char *buf; 3122b6cee71dSXin LI 3123b6cee71dSXin LI if (strip) { 3124b6cee71dSXin LI if ((p = strrchr(fn, '/')) != NULL) 3125b6cee71dSXin LI fn = ++p; 3126b6cee71dSXin LI } 3127b6cee71dSXin LI 3128b6cee71dSXin LI for (q = fn; *q; q++) 3129b6cee71dSXin LI continue; 3130b6cee71dSXin LI /* Look for .mgc */ 3131b6cee71dSXin LI for (p = ext + sizeof(ext) - 1; p >= ext && q >= fn; p--, q--) 3132b6cee71dSXin LI if (*p != *q) 3133b6cee71dSXin LI break; 3134b6cee71dSXin LI 3135b6cee71dSXin LI /* Did not find .mgc, restore q */ 3136b6cee71dSXin LI if (p >= ext) 3137b6cee71dSXin LI while (*q) 3138b6cee71dSXin LI q++; 3139b6cee71dSXin LI 3140b6cee71dSXin LI q++; 3141b6cee71dSXin LI /* Compatibility with old code that looked in .mime */ 3142b6cee71dSXin LI if (ms->flags & MAGIC_MIME) { 3143b6cee71dSXin LI if (asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext) < 0) 3144b6cee71dSXin LI return NULL; 3145b6cee71dSXin LI if (access(buf, R_OK) != -1) { 3146b6cee71dSXin LI ms->flags &= MAGIC_MIME_TYPE; 3147b6cee71dSXin LI return buf; 3148b6cee71dSXin LI } 3149b6cee71dSXin LI free(buf); 3150b6cee71dSXin LI } 3151b6cee71dSXin LI if (asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext) < 0) 3152b6cee71dSXin LI return NULL; 3153b6cee71dSXin LI 3154b6cee71dSXin LI /* Compatibility with old code that looked in .mime */ 3155*a5d223e6SXin LI if (strstr(fn, ".mime") != NULL) 3156b6cee71dSXin LI ms->flags &= MAGIC_MIME_TYPE; 3157b6cee71dSXin LI return buf; 3158b6cee71dSXin LI } 3159b6cee71dSXin LI 3160b6cee71dSXin LI /* 3161b6cee71dSXin LI * Byteswap an mmap'ed file if needed 3162b6cee71dSXin LI */ 3163b6cee71dSXin LI private void 3164b6cee71dSXin LI byteswap(struct magic *magic, uint32_t nmagic) 3165b6cee71dSXin LI { 3166b6cee71dSXin LI uint32_t i; 3167b6cee71dSXin LI for (i = 0; i < nmagic; i++) 3168b6cee71dSXin LI bs1(&magic[i]); 3169b6cee71dSXin LI } 3170b6cee71dSXin LI 3171b6cee71dSXin LI /* 3172b6cee71dSXin LI * swap a short 3173b6cee71dSXin LI */ 3174b6cee71dSXin LI private uint16_t 3175b6cee71dSXin LI swap2(uint16_t sv) 3176b6cee71dSXin LI { 3177b6cee71dSXin LI uint16_t rv; 3178b6cee71dSXin LI uint8_t *s = (uint8_t *)(void *)&sv; 3179b6cee71dSXin LI uint8_t *d = (uint8_t *)(void *)&rv; 3180b6cee71dSXin LI d[0] = s[1]; 3181b6cee71dSXin LI d[1] = s[0]; 3182b6cee71dSXin LI return rv; 3183b6cee71dSXin LI } 3184b6cee71dSXin LI 3185b6cee71dSXin LI /* 3186b6cee71dSXin LI * swap an int 3187b6cee71dSXin LI */ 3188b6cee71dSXin LI private uint32_t 3189b6cee71dSXin LI swap4(uint32_t sv) 3190b6cee71dSXin LI { 3191b6cee71dSXin LI uint32_t rv; 3192b6cee71dSXin LI uint8_t *s = (uint8_t *)(void *)&sv; 3193b6cee71dSXin LI uint8_t *d = (uint8_t *)(void *)&rv; 3194b6cee71dSXin LI d[0] = s[3]; 3195b6cee71dSXin LI d[1] = s[2]; 3196b6cee71dSXin LI d[2] = s[1]; 3197b6cee71dSXin LI d[3] = s[0]; 3198b6cee71dSXin LI return rv; 3199b6cee71dSXin LI } 3200b6cee71dSXin LI 3201b6cee71dSXin LI /* 3202b6cee71dSXin LI * swap a quad 3203b6cee71dSXin LI */ 3204b6cee71dSXin LI private uint64_t 3205b6cee71dSXin LI swap8(uint64_t sv) 3206b6cee71dSXin LI { 3207b6cee71dSXin LI uint64_t rv; 3208b6cee71dSXin LI uint8_t *s = (uint8_t *)(void *)&sv; 3209b6cee71dSXin LI uint8_t *d = (uint8_t *)(void *)&rv; 3210b6cee71dSXin LI #if 0 3211b6cee71dSXin LI d[0] = s[3]; 3212b6cee71dSXin LI d[1] = s[2]; 3213b6cee71dSXin LI d[2] = s[1]; 3214b6cee71dSXin LI d[3] = s[0]; 3215b6cee71dSXin LI d[4] = s[7]; 3216b6cee71dSXin LI d[5] = s[6]; 3217b6cee71dSXin LI d[6] = s[5]; 3218b6cee71dSXin LI d[7] = s[4]; 3219b6cee71dSXin LI #else 3220b6cee71dSXin LI d[0] = s[7]; 3221b6cee71dSXin LI d[1] = s[6]; 3222b6cee71dSXin LI d[2] = s[5]; 3223b6cee71dSXin LI d[3] = s[4]; 3224b6cee71dSXin LI d[4] = s[3]; 3225b6cee71dSXin LI d[5] = s[2]; 3226b6cee71dSXin LI d[6] = s[1]; 3227b6cee71dSXin LI d[7] = s[0]; 3228b6cee71dSXin LI #endif 3229b6cee71dSXin LI return rv; 3230b6cee71dSXin LI } 3231b6cee71dSXin LI 3232b6cee71dSXin LI /* 3233b6cee71dSXin LI * byteswap a single magic entry 3234b6cee71dSXin LI */ 3235b6cee71dSXin LI private void 3236b6cee71dSXin LI bs1(struct magic *m) 3237b6cee71dSXin LI { 3238b6cee71dSXin LI m->cont_level = swap2(m->cont_level); 3239b6cee71dSXin LI m->offset = swap4((uint32_t)m->offset); 3240b6cee71dSXin LI m->in_offset = swap4((uint32_t)m->in_offset); 3241b6cee71dSXin LI m->lineno = swap4((uint32_t)m->lineno); 3242b6cee71dSXin LI if (IS_STRING(m->type)) { 3243b6cee71dSXin LI m->str_range = swap4(m->str_range); 3244b6cee71dSXin LI m->str_flags = swap4(m->str_flags); 3245b6cee71dSXin LI } 3246b6cee71dSXin LI else { 3247b6cee71dSXin LI m->value.q = swap8(m->value.q); 3248b6cee71dSXin LI m->num_mask = swap8(m->num_mask); 3249b6cee71dSXin LI } 3250b6cee71dSXin LI } 3251b6cee71dSXin LI 3252b6cee71dSXin LI protected size_t 3253b6cee71dSXin LI file_pstring_length_size(const struct magic *m) 3254b6cee71dSXin LI { 3255b6cee71dSXin LI switch (m->str_flags & PSTRING_LEN) { 3256b6cee71dSXin LI case PSTRING_1_LE: 3257b6cee71dSXin LI return 1; 3258b6cee71dSXin LI case PSTRING_2_LE: 3259b6cee71dSXin LI case PSTRING_2_BE: 3260b6cee71dSXin LI return 2; 3261b6cee71dSXin LI case PSTRING_4_LE: 3262b6cee71dSXin LI case PSTRING_4_BE: 3263b6cee71dSXin LI return 4; 3264b6cee71dSXin LI default: 3265b6cee71dSXin LI abort(); /* Impossible */ 3266b6cee71dSXin LI return 1; 3267b6cee71dSXin LI } 3268b6cee71dSXin LI } 3269b6cee71dSXin LI protected size_t 32709ce06829SXin LI file_pstring_get_length(const struct magic *m, const char *ss) 3271b6cee71dSXin LI { 3272b6cee71dSXin LI size_t len = 0; 32739ce06829SXin LI const unsigned char *s = (const unsigned char *)ss; 3274b6cee71dSXin LI 3275b6cee71dSXin LI switch (m->str_flags & PSTRING_LEN) { 3276b6cee71dSXin LI case PSTRING_1_LE: 3277b6cee71dSXin LI len = *s; 3278b6cee71dSXin LI break; 3279b6cee71dSXin LI case PSTRING_2_LE: 3280b6cee71dSXin LI len = (s[1] << 8) | s[0]; 3281b6cee71dSXin LI break; 3282b6cee71dSXin LI case PSTRING_2_BE: 3283b6cee71dSXin LI len = (s[0] << 8) | s[1]; 3284b6cee71dSXin LI break; 3285b6cee71dSXin LI case PSTRING_4_LE: 3286b6cee71dSXin LI len = (s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0]; 3287b6cee71dSXin LI break; 3288b6cee71dSXin LI case PSTRING_4_BE: 3289b6cee71dSXin LI len = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; 3290b6cee71dSXin LI break; 3291b6cee71dSXin LI default: 3292b6cee71dSXin LI abort(); /* Impossible */ 3293b6cee71dSXin LI } 3294b6cee71dSXin LI 3295b6cee71dSXin LI if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF) 3296b6cee71dSXin LI len -= file_pstring_length_size(m); 3297b6cee71dSXin LI 3298b6cee71dSXin LI return len; 3299b6cee71dSXin LI } 3300b6cee71dSXin LI 3301b6cee71dSXin LI protected int 3302b6cee71dSXin LI file_magicfind(struct magic_set *ms, const char *name, struct mlist *v) 3303b6cee71dSXin LI { 3304b6cee71dSXin LI uint32_t i, j; 3305b6cee71dSXin LI struct mlist *mlist, *ml; 3306b6cee71dSXin LI 3307b6cee71dSXin LI mlist = ms->mlist[1]; 3308b6cee71dSXin LI 3309b6cee71dSXin LI for (ml = mlist->next; ml != mlist; ml = ml->next) { 3310b6cee71dSXin LI struct magic *ma = ml->magic; 3311b6cee71dSXin LI uint32_t nma = ml->nmagic; 3312b6cee71dSXin LI for (i = 0; i < nma; i++) { 3313b6cee71dSXin LI if (ma[i].type != FILE_NAME) 3314b6cee71dSXin LI continue; 3315b6cee71dSXin LI if (strcmp(ma[i].value.s, name) == 0) { 3316b6cee71dSXin LI v->magic = &ma[i]; 3317b6cee71dSXin LI for (j = i + 1; j < nma; j++) 3318b6cee71dSXin LI if (ma[j].cont_level == 0) 3319b6cee71dSXin LI break; 3320b6cee71dSXin LI v->nmagic = j - i; 3321b6cee71dSXin LI return 0; 3322b6cee71dSXin LI } 3323b6cee71dSXin LI } 3324b6cee71dSXin LI } 3325b6cee71dSXin LI return -1; 3326b6cee71dSXin LI } 3327