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*3e41d09dSXin LI FILE_RCSID("@(#)$File: apprentice.c,v 1.248 2016/03/31 17:51:12 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 89*3e41d09dSXin LI #define MAP_TYPE_USER 0 90c2931133SXin LI #define MAP_TYPE_MALLOC 1 91*3e41d09dSXin 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 *); 146*3e41d09dSXin 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 }, 271*3e41d09dSXin 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[] = { 280*3e41d09dSXin 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 411c2931133SXin LI mlp->map = idx == 0 ? map : NULL; 412b6cee71dSXin LI if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL) 413b6cee71dSXin LI return -1; 414b6cee71dSXin LI 415c2931133SXin LI ml->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); 454b6cee71dSXin LI if (map == NULL) { 455b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 456b6cee71dSXin LI file_magwarn(ms, "using regular magic file `%s'", fn); 457b6cee71dSXin LI map = apprentice_load(ms, fn, action); 458b6cee71dSXin LI if (map == NULL) 459b6cee71dSXin LI return -1; 460b6cee71dSXin LI } 461b6cee71dSXin LI 462b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 463b6cee71dSXin LI if (add_mlist(ms->mlist[i], map, i) == -1) { 464b6cee71dSXin LI file_oomem(ms, sizeof(*ml)); 465c2931133SXin LI goto fail; 466b6cee71dSXin LI } 467b6cee71dSXin LI } 468b6cee71dSXin LI 469b6cee71dSXin LI if (action == FILE_LIST) { 470b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 471c2931133SXin LI printf("Set %" SIZE_T_FORMAT "u:\nBinary patterns:\n", 472c2931133SXin LI i); 473b6cee71dSXin LI apprentice_list(ms->mlist[i], BINTEST); 474b6cee71dSXin LI printf("Text patterns:\n"); 475b6cee71dSXin LI apprentice_list(ms->mlist[i], TEXTTEST); 476b6cee71dSXin LI } 477b6cee71dSXin LI } 478b6cee71dSXin LI return 0; 479c2931133SXin LI fail: 480c2931133SXin LI for (i = 0; i < MAGIC_SETS; i++) { 481c2931133SXin LI mlist_free(ms->mlist[i]); 482c2931133SXin LI ms->mlist[i] = NULL; 483c2931133SXin LI } 484c2931133SXin LI return -1; 485c2931133SXin LI #else 486c2931133SXin LI return 0; 487c2931133SXin LI #endif /* COMPILE_ONLY */ 488b6cee71dSXin LI } 489b6cee71dSXin LI 490b6cee71dSXin LI protected void 491b6cee71dSXin LI file_ms_free(struct magic_set *ms) 492b6cee71dSXin LI { 493b6cee71dSXin LI size_t i; 494b6cee71dSXin LI if (ms == NULL) 495b6cee71dSXin LI return; 496b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) 497b6cee71dSXin LI mlist_free(ms->mlist[i]); 498b6cee71dSXin LI free(ms->o.pbuf); 499b6cee71dSXin LI free(ms->o.buf); 500b6cee71dSXin LI free(ms->c.li); 501b6cee71dSXin LI free(ms); 502b6cee71dSXin LI } 503b6cee71dSXin LI 504b6cee71dSXin LI protected struct magic_set * 505b6cee71dSXin LI file_ms_alloc(int flags) 506b6cee71dSXin LI { 507b6cee71dSXin LI struct magic_set *ms; 508b6cee71dSXin LI size_t i, len; 509b6cee71dSXin LI 510b6cee71dSXin LI if ((ms = CAST(struct magic_set *, calloc((size_t)1, 511b6cee71dSXin LI sizeof(struct magic_set)))) == NULL) 512b6cee71dSXin LI return NULL; 513b6cee71dSXin LI 514b6cee71dSXin LI if (magic_setflags(ms, flags) == -1) { 515b6cee71dSXin LI errno = EINVAL; 516b6cee71dSXin LI goto free; 517b6cee71dSXin LI } 518b6cee71dSXin LI 519b6cee71dSXin LI ms->o.buf = ms->o.pbuf = NULL; 520b6cee71dSXin LI len = (ms->c.len = 10) * sizeof(*ms->c.li); 521b6cee71dSXin LI 522b6cee71dSXin LI if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL) 523b6cee71dSXin LI goto free; 524b6cee71dSXin LI 525b6cee71dSXin LI ms->event_flags = 0; 526b6cee71dSXin LI ms->error = -1; 527b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) 528b6cee71dSXin LI ms->mlist[i] = NULL; 529b6cee71dSXin LI ms->file = "unknown"; 530b6cee71dSXin LI ms->line = 0; 531c2931133SXin LI ms->indir_max = FILE_INDIR_MAX; 532c2931133SXin LI ms->name_max = FILE_NAME_MAX; 533c2931133SXin LI ms->elf_shnum_max = FILE_ELF_SHNUM_MAX; 534c2931133SXin LI ms->elf_phnum_max = FILE_ELF_PHNUM_MAX; 5354460e5b0SXin LI ms->elf_notes_max = FILE_ELF_NOTES_MAX; 5369ce06829SXin LI ms->regex_max = FILE_REGEX_MAX; 537*3e41d09dSXin LI ms->bytes_max = FILE_BYTES_MAX; 538b6cee71dSXin LI return ms; 539b6cee71dSXin LI free: 540b6cee71dSXin LI free(ms); 541b6cee71dSXin LI return NULL; 542b6cee71dSXin LI } 543b6cee71dSXin LI 544b6cee71dSXin LI private void 545b6cee71dSXin LI apprentice_unmap(struct magic_map *map) 546b6cee71dSXin LI { 5479ce06829SXin LI size_t i; 548b6cee71dSXin LI if (map == NULL) 549b6cee71dSXin LI return; 550c2931133SXin LI 551c2931133SXin LI switch (map->type) { 552*3e41d09dSXin LI case MAP_TYPE_USER: 553*3e41d09dSXin LI break; 554*3e41d09dSXin LI case MAP_TYPE_MALLOC: 555*3e41d09dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 556*3e41d09dSXin LI if ((char *)map->magic[i] >= (char *)map->p && 557*3e41d09dSXin LI (char *)map->magic[i] < (char *)map->p + map->len) 558*3e41d09dSXin LI continue; 559*3e41d09dSXin LI free(map->magic[i]); 560*3e41d09dSXin LI } 561*3e41d09dSXin LI free(map->p); 562*3e41d09dSXin LI break; 563b6cee71dSXin LI #ifdef QUICK 564c2931133SXin LI case MAP_TYPE_MMAP: 565*3e41d09dSXin LI if (map->p && map->p != MAP_FAILED) 566b6cee71dSXin LI (void)munmap(map->p, map->len); 567c2931133SXin LI break; 568b6cee71dSXin LI #endif 569c2931133SXin LI default: 570c2931133SXin LI abort(); 571b6cee71dSXin LI } 572b6cee71dSXin LI free(map); 573b6cee71dSXin LI } 574b6cee71dSXin LI 575b6cee71dSXin LI private struct mlist * 576b6cee71dSXin LI mlist_alloc(void) 577b6cee71dSXin LI { 578b6cee71dSXin LI struct mlist *mlist; 579b6cee71dSXin LI if ((mlist = CAST(struct mlist *, calloc(1, sizeof(*mlist)))) == NULL) { 580b6cee71dSXin LI return NULL; 581b6cee71dSXin LI } 582b6cee71dSXin LI mlist->next = mlist->prev = mlist; 583b6cee71dSXin LI return mlist; 584b6cee71dSXin LI } 585b6cee71dSXin LI 586b6cee71dSXin LI private void 587b6cee71dSXin LI mlist_free(struct mlist *mlist) 588b6cee71dSXin LI { 589c2931133SXin LI struct mlist *ml, *next; 590b6cee71dSXin LI 591b6cee71dSXin LI if (mlist == NULL) 592b6cee71dSXin LI return; 593b6cee71dSXin LI 594c2931133SXin LI ml = mlist->next; 595c2931133SXin LI for (ml = mlist->next; (next = ml->next) != NULL; ml = next) { 596b6cee71dSXin LI if (ml->map) 597b6cee71dSXin LI apprentice_unmap(ml->map); 598b6cee71dSXin LI free(ml); 599c2931133SXin LI if (ml == mlist) 600c2931133SXin LI break; 601b6cee71dSXin LI } 602b6cee71dSXin LI } 603b6cee71dSXin LI 604c2931133SXin LI #ifndef COMPILE_ONLY 605c2931133SXin LI /* void **bufs: an array of compiled magic files */ 606c2931133SXin LI protected int 607c2931133SXin LI buffer_apprentice(struct magic_set *ms, struct magic **bufs, 608c2931133SXin LI size_t *sizes, size_t nbufs) 609c2931133SXin LI { 610c2931133SXin LI size_t i, j; 611c2931133SXin LI struct mlist *ml; 612c2931133SXin LI struct magic_map *map; 613c2931133SXin LI 614c2931133SXin LI if (nbufs == 0) 615c2931133SXin LI return -1; 616c2931133SXin LI 617c2931133SXin LI if (ms->mlist[0] != NULL) 618c2931133SXin LI file_reset(ms); 619c2931133SXin LI 620c2931133SXin LI init_file_tables(); 621c2931133SXin LI 622c2931133SXin LI for (i = 0; i < MAGIC_SETS; i++) { 623c2931133SXin LI mlist_free(ms->mlist[i]); 624c2931133SXin LI if ((ms->mlist[i] = mlist_alloc()) == NULL) { 625c2931133SXin LI file_oomem(ms, sizeof(*ms->mlist[i])); 626c2931133SXin LI goto fail; 627c2931133SXin LI } 628c2931133SXin LI } 629c2931133SXin LI 630c2931133SXin LI for (i = 0; i < nbufs; i++) { 631c2931133SXin LI map = apprentice_buf(ms, bufs[i], sizes[i]); 632c2931133SXin LI if (map == NULL) 633c2931133SXin LI goto fail; 634c2931133SXin LI 635c2931133SXin LI for (j = 0; j < MAGIC_SETS; j++) { 636c2931133SXin LI if (add_mlist(ms->mlist[j], map, j) == -1) { 637c2931133SXin LI file_oomem(ms, sizeof(*ml)); 638c2931133SXin LI goto fail; 639c2931133SXin LI } 640c2931133SXin LI } 641c2931133SXin LI } 642c2931133SXin LI 643c2931133SXin LI return 0; 644c2931133SXin LI fail: 645c2931133SXin LI for (i = 0; i < MAGIC_SETS; i++) { 646c2931133SXin LI mlist_free(ms->mlist[i]); 647c2931133SXin LI ms->mlist[i] = NULL; 648c2931133SXin LI } 649c2931133SXin LI return -1; 650c2931133SXin LI } 651c2931133SXin LI #endif 652c2931133SXin LI 653b6cee71dSXin LI /* const char *fn: list of magic files and directories */ 654b6cee71dSXin LI protected int 655b6cee71dSXin LI file_apprentice(struct magic_set *ms, const char *fn, int action) 656b6cee71dSXin LI { 657b6cee71dSXin LI char *p, *mfn; 658b6cee71dSXin LI int file_err, errs = -1; 659b6cee71dSXin LI size_t i; 660b6cee71dSXin LI 661b6cee71dSXin LI if (ms->mlist[0] != NULL) 662b6cee71dSXin LI file_reset(ms); 663b6cee71dSXin LI 664b6cee71dSXin LI if ((fn = magic_getpath(fn, action)) == NULL) 665b6cee71dSXin LI return -1; 666b6cee71dSXin LI 667b6cee71dSXin LI init_file_tables(); 668b6cee71dSXin LI 669b6cee71dSXin LI if ((mfn = strdup(fn)) == NULL) { 670b6cee71dSXin LI file_oomem(ms, strlen(fn)); 671b6cee71dSXin LI return -1; 672b6cee71dSXin LI } 673b6cee71dSXin LI 674b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 675b6cee71dSXin LI mlist_free(ms->mlist[i]); 676b6cee71dSXin LI if ((ms->mlist[i] = mlist_alloc()) == NULL) { 677b6cee71dSXin LI file_oomem(ms, sizeof(*ms->mlist[i])); 678c2931133SXin LI while (i-- > 0) { 679b6cee71dSXin LI mlist_free(ms->mlist[i]); 680c2931133SXin LI ms->mlist[i] = NULL; 681b6cee71dSXin LI } 682b6cee71dSXin LI free(mfn); 683b6cee71dSXin LI return -1; 684b6cee71dSXin LI } 685b6cee71dSXin LI } 686b6cee71dSXin LI fn = mfn; 687b6cee71dSXin LI 688b6cee71dSXin LI while (fn) { 689b6cee71dSXin LI p = strchr(fn, PATHSEP); 690b6cee71dSXin LI if (p) 691b6cee71dSXin LI *p++ = '\0'; 692b6cee71dSXin LI if (*fn == '\0') 693b6cee71dSXin LI break; 694b6cee71dSXin LI file_err = apprentice_1(ms, fn, action); 695b6cee71dSXin LI errs = MAX(errs, file_err); 696b6cee71dSXin LI fn = p; 697b6cee71dSXin LI } 698b6cee71dSXin LI 699b6cee71dSXin LI free(mfn); 700b6cee71dSXin LI 701b6cee71dSXin LI if (errs == -1) { 702b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 703b6cee71dSXin LI mlist_free(ms->mlist[i]); 704b6cee71dSXin LI ms->mlist[i] = NULL; 705b6cee71dSXin LI } 706b6cee71dSXin LI file_error(ms, 0, "could not find any valid magic files!"); 707b6cee71dSXin LI return -1; 708b6cee71dSXin LI } 709b6cee71dSXin LI 710b6cee71dSXin LI #if 0 711b6cee71dSXin LI /* 712b6cee71dSXin LI * Always leave the database loaded 713b6cee71dSXin LI */ 714b6cee71dSXin LI if (action == FILE_LOAD) 715b6cee71dSXin LI return 0; 716b6cee71dSXin LI 717b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 718b6cee71dSXin LI mlist_free(ms->mlist[i]); 719b6cee71dSXin LI ms->mlist[i] = NULL; 720b6cee71dSXin LI } 721b6cee71dSXin LI #endif 722b6cee71dSXin LI 723b6cee71dSXin LI switch (action) { 724b6cee71dSXin LI case FILE_LOAD: 725b6cee71dSXin LI case FILE_COMPILE: 726b6cee71dSXin LI case FILE_CHECK: 727b6cee71dSXin LI case FILE_LIST: 728b6cee71dSXin LI return 0; 729b6cee71dSXin LI default: 730b6cee71dSXin LI file_error(ms, 0, "Invalid action %d", action); 731b6cee71dSXin LI return -1; 732b6cee71dSXin LI } 733b6cee71dSXin LI } 734b6cee71dSXin LI 735b6cee71dSXin LI /* 736b6cee71dSXin LI * Compute the real length of a magic expression, for the purposes 737b6cee71dSXin LI * of determining how "strong" a magic expression is (approximating 738b6cee71dSXin LI * how specific its matches are): 739b6cee71dSXin LI * - magic characters count 0 unless escaped. 740b6cee71dSXin LI * - [] expressions count 1 741b6cee71dSXin LI * - {} expressions count 0 742b6cee71dSXin LI * - regular characters or escaped magic characters count 1 743b6cee71dSXin LI * - 0 length expressions count as one 744b6cee71dSXin LI */ 745b6cee71dSXin LI private size_t 746b6cee71dSXin LI nonmagic(const char *str) 747b6cee71dSXin LI { 748b6cee71dSXin LI const char *p; 749b6cee71dSXin LI size_t rv = 0; 750b6cee71dSXin LI 751b6cee71dSXin LI for (p = str; *p; p++) 752b6cee71dSXin LI switch (*p) { 753b6cee71dSXin LI case '\\': /* Escaped anything counts 1 */ 754b6cee71dSXin LI if (!*++p) 755b6cee71dSXin LI p--; 756b6cee71dSXin LI rv++; 757b6cee71dSXin LI continue; 758b6cee71dSXin LI case '?': /* Magic characters count 0 */ 759b6cee71dSXin LI case '*': 760b6cee71dSXin LI case '.': 761b6cee71dSXin LI case '+': 762b6cee71dSXin LI case '^': 763b6cee71dSXin LI case '$': 764b6cee71dSXin LI continue; 765b6cee71dSXin LI case '[': /* Bracketed expressions count 1 the ']' */ 766b6cee71dSXin LI while (*p && *p != ']') 767b6cee71dSXin LI p++; 768b6cee71dSXin LI p--; 769b6cee71dSXin LI continue; 770b6cee71dSXin LI case '{': /* Braced expressions count 0 */ 771b6cee71dSXin LI while (*p && *p != '}') 772b6cee71dSXin LI p++; 773b6cee71dSXin LI if (!*p) 774b6cee71dSXin LI p--; 775b6cee71dSXin LI continue; 776b6cee71dSXin LI default: /* Anything else counts 1 */ 777b6cee71dSXin LI rv++; 778b6cee71dSXin LI continue; 779b6cee71dSXin LI } 780b6cee71dSXin LI 781b6cee71dSXin LI return rv == 0 ? 1 : rv; /* Return at least 1 */ 782b6cee71dSXin LI } 783b6cee71dSXin LI 784b6cee71dSXin LI /* 785b6cee71dSXin LI * Get weight of this magic entry, for sorting purposes. 786b6cee71dSXin LI */ 787b6cee71dSXin LI private size_t 788b6cee71dSXin LI apprentice_magic_strength(const struct magic *m) 789b6cee71dSXin LI { 790b6cee71dSXin LI #define MULT 10 791b6cee71dSXin LI size_t v, val = 2 * MULT; /* baseline strength */ 792b6cee71dSXin LI 793b6cee71dSXin LI switch (m->type) { 794b6cee71dSXin LI case FILE_DEFAULT: /* make sure this sorts last */ 795b6cee71dSXin LI if (m->factor_op != FILE_FACTOR_OP_NONE) 796b6cee71dSXin LI abort(); 797b6cee71dSXin LI return 0; 798b6cee71dSXin LI 799b6cee71dSXin LI case FILE_BYTE: 800b6cee71dSXin LI val += 1 * MULT; 801b6cee71dSXin LI break; 802b6cee71dSXin LI 803b6cee71dSXin LI case FILE_SHORT: 804b6cee71dSXin LI case FILE_LESHORT: 805b6cee71dSXin LI case FILE_BESHORT: 806b6cee71dSXin LI val += 2 * MULT; 807b6cee71dSXin LI break; 808b6cee71dSXin LI 809b6cee71dSXin LI case FILE_LONG: 810b6cee71dSXin LI case FILE_LELONG: 811b6cee71dSXin LI case FILE_BELONG: 812b6cee71dSXin LI case FILE_MELONG: 813b6cee71dSXin LI val += 4 * MULT; 814b6cee71dSXin LI break; 815b6cee71dSXin LI 816b6cee71dSXin LI case FILE_PSTRING: 817b6cee71dSXin LI case FILE_STRING: 818b6cee71dSXin LI val += m->vallen * MULT; 819b6cee71dSXin LI break; 820b6cee71dSXin LI 821b6cee71dSXin LI case FILE_BESTRING16: 822b6cee71dSXin LI case FILE_LESTRING16: 823b6cee71dSXin LI val += m->vallen * MULT / 2; 824b6cee71dSXin LI break; 825b6cee71dSXin LI 826b6cee71dSXin LI case FILE_SEARCH: 827b6cee71dSXin LI val += m->vallen * MAX(MULT / m->vallen, 1); 828b6cee71dSXin LI break; 829b6cee71dSXin LI 830b6cee71dSXin LI case FILE_REGEX: 831b6cee71dSXin LI v = nonmagic(m->value.s); 832b6cee71dSXin LI val += v * MAX(MULT / v, 1); 833b6cee71dSXin LI break; 834b6cee71dSXin LI 835b6cee71dSXin LI case FILE_DATE: 836b6cee71dSXin LI case FILE_LEDATE: 837b6cee71dSXin LI case FILE_BEDATE: 838b6cee71dSXin LI case FILE_MEDATE: 839b6cee71dSXin LI case FILE_LDATE: 840b6cee71dSXin LI case FILE_LELDATE: 841b6cee71dSXin LI case FILE_BELDATE: 842b6cee71dSXin LI case FILE_MELDATE: 843b6cee71dSXin LI case FILE_FLOAT: 844b6cee71dSXin LI case FILE_BEFLOAT: 845b6cee71dSXin LI case FILE_LEFLOAT: 846b6cee71dSXin LI val += 4 * MULT; 847b6cee71dSXin LI break; 848b6cee71dSXin LI 849b6cee71dSXin LI case FILE_QUAD: 850b6cee71dSXin LI case FILE_BEQUAD: 851b6cee71dSXin LI case FILE_LEQUAD: 852b6cee71dSXin LI case FILE_QDATE: 853b6cee71dSXin LI case FILE_LEQDATE: 854b6cee71dSXin LI case FILE_BEQDATE: 855b6cee71dSXin LI case FILE_QLDATE: 856b6cee71dSXin LI case FILE_LEQLDATE: 857b6cee71dSXin LI case FILE_BEQLDATE: 858b6cee71dSXin LI case FILE_QWDATE: 859b6cee71dSXin LI case FILE_LEQWDATE: 860b6cee71dSXin LI case FILE_BEQWDATE: 861b6cee71dSXin LI case FILE_DOUBLE: 862b6cee71dSXin LI case FILE_BEDOUBLE: 863b6cee71dSXin LI case FILE_LEDOUBLE: 864b6cee71dSXin LI val += 8 * MULT; 865b6cee71dSXin LI break; 866b6cee71dSXin LI 867b6cee71dSXin LI case FILE_INDIRECT: 868b6cee71dSXin LI case FILE_NAME: 869b6cee71dSXin LI case FILE_USE: 870b6cee71dSXin LI break; 871b6cee71dSXin LI 872*3e41d09dSXin LI case FILE_DER: 873*3e41d09dSXin LI val += MULT; 874*3e41d09dSXin LI break; 875*3e41d09dSXin LI 876b6cee71dSXin LI default: 877b6cee71dSXin LI (void)fprintf(stderr, "Bad type %d\n", m->type); 878b6cee71dSXin LI abort(); 879b6cee71dSXin LI } 880b6cee71dSXin LI 881b6cee71dSXin LI switch (m->reln) { 882b6cee71dSXin LI case 'x': /* matches anything penalize */ 883b6cee71dSXin LI case '!': /* matches almost anything penalize */ 884b6cee71dSXin LI val = 0; 885b6cee71dSXin LI break; 886b6cee71dSXin LI 887b6cee71dSXin LI case '=': /* Exact match, prefer */ 888b6cee71dSXin LI val += MULT; 889b6cee71dSXin LI break; 890b6cee71dSXin LI 891b6cee71dSXin LI case '>': 892b6cee71dSXin LI case '<': /* comparison match reduce strength */ 893b6cee71dSXin LI val -= 2 * MULT; 894b6cee71dSXin LI break; 895b6cee71dSXin LI 896b6cee71dSXin LI case '^': 897b6cee71dSXin LI case '&': /* masking bits, we could count them too */ 898b6cee71dSXin LI val -= MULT; 899b6cee71dSXin LI break; 900b6cee71dSXin LI 901b6cee71dSXin LI default: 902b6cee71dSXin LI (void)fprintf(stderr, "Bad relation %c\n", m->reln); 903b6cee71dSXin LI abort(); 904b6cee71dSXin LI } 905b6cee71dSXin LI 906b6cee71dSXin LI if (val == 0) /* ensure we only return 0 for FILE_DEFAULT */ 907b6cee71dSXin LI val = 1; 908b6cee71dSXin LI 909b6cee71dSXin LI switch (m->factor_op) { 910b6cee71dSXin LI case FILE_FACTOR_OP_NONE: 911b6cee71dSXin LI break; 912b6cee71dSXin LI case FILE_FACTOR_OP_PLUS: 913b6cee71dSXin LI val += m->factor; 914b6cee71dSXin LI break; 915b6cee71dSXin LI case FILE_FACTOR_OP_MINUS: 916b6cee71dSXin LI val -= m->factor; 917b6cee71dSXin LI break; 918b6cee71dSXin LI case FILE_FACTOR_OP_TIMES: 919b6cee71dSXin LI val *= m->factor; 920b6cee71dSXin LI break; 921b6cee71dSXin LI case FILE_FACTOR_OP_DIV: 922b6cee71dSXin LI val /= m->factor; 923b6cee71dSXin LI break; 924b6cee71dSXin LI default: 925b6cee71dSXin LI abort(); 926b6cee71dSXin LI } 927b6cee71dSXin LI 928b6cee71dSXin LI /* 929b6cee71dSXin LI * Magic entries with no description get a bonus because they depend 930b6cee71dSXin LI * on subsequent magic entries to print something. 931b6cee71dSXin LI */ 932b6cee71dSXin LI if (m->desc[0] == '\0') 933b6cee71dSXin LI val++; 934b6cee71dSXin LI return val; 935b6cee71dSXin LI } 936b6cee71dSXin LI 937b6cee71dSXin LI /* 938b6cee71dSXin LI * Sort callback for sorting entries by "strength" (basically length) 939b6cee71dSXin LI */ 940b6cee71dSXin LI private int 941b6cee71dSXin LI apprentice_sort(const void *a, const void *b) 942b6cee71dSXin LI { 943b6cee71dSXin LI const struct magic_entry *ma = CAST(const struct magic_entry *, a); 944b6cee71dSXin LI const struct magic_entry *mb = CAST(const struct magic_entry *, b); 945b6cee71dSXin LI size_t sa = apprentice_magic_strength(ma->mp); 946b6cee71dSXin LI size_t sb = apprentice_magic_strength(mb->mp); 947b6cee71dSXin LI if (sa == sb) 948b6cee71dSXin LI return 0; 949b6cee71dSXin LI else if (sa > sb) 950b6cee71dSXin LI return -1; 951b6cee71dSXin LI else 952b6cee71dSXin LI return 1; 953b6cee71dSXin LI } 954b6cee71dSXin LI 955b6cee71dSXin LI /* 956b6cee71dSXin LI * Shows sorted patterns list in the order which is used for the matching 957b6cee71dSXin LI */ 958b6cee71dSXin LI private void 959b6cee71dSXin LI apprentice_list(struct mlist *mlist, int mode) 960b6cee71dSXin LI { 961b6cee71dSXin LI uint32_t magindex = 0; 962b6cee71dSXin LI struct mlist *ml; 963b6cee71dSXin LI for (ml = mlist->next; ml != mlist; ml = ml->next) { 964b6cee71dSXin LI for (magindex = 0; magindex < ml->nmagic; magindex++) { 965b6cee71dSXin LI struct magic *m = &ml->magic[magindex]; 966b6cee71dSXin LI if ((m->flag & mode) != mode) { 967b6cee71dSXin LI /* Skip sub-tests */ 968b6cee71dSXin LI while (magindex + 1 < ml->nmagic && 969b6cee71dSXin LI ml->magic[magindex + 1].cont_level != 0) 970b6cee71dSXin LI ++magindex; 971b6cee71dSXin LI continue; /* Skip to next top-level test*/ 972b6cee71dSXin LI } 973b6cee71dSXin LI 974b6cee71dSXin LI /* 975b6cee71dSXin LI * Try to iterate over the tree until we find item with 976b6cee71dSXin LI * description/mimetype. 977b6cee71dSXin LI */ 978b6cee71dSXin LI while (magindex + 1 < ml->nmagic && 979b6cee71dSXin LI ml->magic[magindex + 1].cont_level != 0 && 980b6cee71dSXin LI *ml->magic[magindex].desc == '\0' && 981b6cee71dSXin LI *ml->magic[magindex].mimetype == '\0') 982b6cee71dSXin LI magindex++; 983b6cee71dSXin LI 9845f0216bdSXin LI printf("Strength = %3" SIZE_T_FORMAT "u@%u: %s [%s]\n", 985b6cee71dSXin LI apprentice_magic_strength(m), 9865f0216bdSXin LI ml->magic[magindex].lineno, 987b6cee71dSXin LI ml->magic[magindex].desc, 988b6cee71dSXin LI ml->magic[magindex].mimetype); 989b6cee71dSXin LI } 990b6cee71dSXin LI } 991b6cee71dSXin LI } 992b6cee71dSXin LI 993b6cee71dSXin LI private void 994b6cee71dSXin LI set_test_type(struct magic *mstart, struct magic *m) 995b6cee71dSXin LI { 996b6cee71dSXin LI switch (m->type) { 997b6cee71dSXin LI case FILE_BYTE: 998b6cee71dSXin LI case FILE_SHORT: 999b6cee71dSXin LI case FILE_LONG: 1000b6cee71dSXin LI case FILE_DATE: 1001b6cee71dSXin LI case FILE_BESHORT: 1002b6cee71dSXin LI case FILE_BELONG: 1003b6cee71dSXin LI case FILE_BEDATE: 1004b6cee71dSXin LI case FILE_LESHORT: 1005b6cee71dSXin LI case FILE_LELONG: 1006b6cee71dSXin LI case FILE_LEDATE: 1007b6cee71dSXin LI case FILE_LDATE: 1008b6cee71dSXin LI case FILE_BELDATE: 1009b6cee71dSXin LI case FILE_LELDATE: 1010b6cee71dSXin LI case FILE_MEDATE: 1011b6cee71dSXin LI case FILE_MELDATE: 1012b6cee71dSXin LI case FILE_MELONG: 1013b6cee71dSXin LI case FILE_QUAD: 1014b6cee71dSXin LI case FILE_LEQUAD: 1015b6cee71dSXin LI case FILE_BEQUAD: 1016b6cee71dSXin LI case FILE_QDATE: 1017b6cee71dSXin LI case FILE_LEQDATE: 1018b6cee71dSXin LI case FILE_BEQDATE: 1019b6cee71dSXin LI case FILE_QLDATE: 1020b6cee71dSXin LI case FILE_LEQLDATE: 1021b6cee71dSXin LI case FILE_BEQLDATE: 1022b6cee71dSXin LI case FILE_QWDATE: 1023b6cee71dSXin LI case FILE_LEQWDATE: 1024b6cee71dSXin LI case FILE_BEQWDATE: 1025b6cee71dSXin LI case FILE_FLOAT: 1026b6cee71dSXin LI case FILE_BEFLOAT: 1027b6cee71dSXin LI case FILE_LEFLOAT: 1028b6cee71dSXin LI case FILE_DOUBLE: 1029b6cee71dSXin LI case FILE_BEDOUBLE: 1030b6cee71dSXin LI case FILE_LEDOUBLE: 1031*3e41d09dSXin LI case FILE_DER: 1032b6cee71dSXin LI mstart->flag |= BINTEST; 1033b6cee71dSXin LI break; 1034b6cee71dSXin LI case FILE_STRING: 1035b6cee71dSXin LI case FILE_PSTRING: 1036b6cee71dSXin LI case FILE_BESTRING16: 1037b6cee71dSXin LI case FILE_LESTRING16: 1038b6cee71dSXin LI /* Allow text overrides */ 1039b6cee71dSXin LI if (mstart->str_flags & STRING_TEXTTEST) 1040b6cee71dSXin LI mstart->flag |= TEXTTEST; 1041b6cee71dSXin LI else 1042b6cee71dSXin LI mstart->flag |= BINTEST; 1043b6cee71dSXin LI break; 1044b6cee71dSXin LI case FILE_REGEX: 1045b6cee71dSXin LI case FILE_SEARCH: 1046b6cee71dSXin LI /* Check for override */ 1047b6cee71dSXin LI if (mstart->str_flags & STRING_BINTEST) 1048b6cee71dSXin LI mstart->flag |= BINTEST; 1049b6cee71dSXin LI if (mstart->str_flags & STRING_TEXTTEST) 1050b6cee71dSXin LI mstart->flag |= TEXTTEST; 1051b6cee71dSXin LI 1052b6cee71dSXin LI if (mstart->flag & (TEXTTEST|BINTEST)) 1053b6cee71dSXin LI break; 1054b6cee71dSXin LI 1055b6cee71dSXin LI /* binary test if pattern is not text */ 1056b6cee71dSXin LI if (file_looks_utf8(m->value.us, (size_t)m->vallen, NULL, 1057b6cee71dSXin LI NULL) <= 0) 1058b6cee71dSXin LI mstart->flag |= BINTEST; 1059b6cee71dSXin LI else 1060b6cee71dSXin LI mstart->flag |= TEXTTEST; 1061b6cee71dSXin LI break; 1062b6cee71dSXin LI case FILE_DEFAULT: 1063b6cee71dSXin LI /* can't deduce anything; we shouldn't see this at the 1064b6cee71dSXin LI top level anyway */ 1065b6cee71dSXin LI break; 1066b6cee71dSXin LI case FILE_INVALID: 1067b6cee71dSXin LI default: 1068b6cee71dSXin LI /* invalid search type, but no need to complain here */ 1069b6cee71dSXin LI break; 1070b6cee71dSXin LI } 1071b6cee71dSXin LI } 1072b6cee71dSXin LI 1073b6cee71dSXin LI private int 1074b6cee71dSXin LI addentry(struct magic_set *ms, struct magic_entry *me, 1075b6cee71dSXin LI struct magic_entry_set *mset) 1076b6cee71dSXin LI { 1077b6cee71dSXin LI size_t i = me->mp->type == FILE_NAME ? 1 : 0; 1078b6cee71dSXin LI if (mset[i].count == mset[i].max) { 1079b6cee71dSXin LI struct magic_entry *mp; 1080b6cee71dSXin LI 1081b6cee71dSXin LI mset[i].max += ALLOC_INCR; 1082b6cee71dSXin LI if ((mp = CAST(struct magic_entry *, 1083b6cee71dSXin LI realloc(mset[i].me, sizeof(*mp) * mset[i].max))) == 1084b6cee71dSXin LI NULL) { 1085b6cee71dSXin LI file_oomem(ms, sizeof(*mp) * mset[i].max); 1086b6cee71dSXin LI return -1; 1087b6cee71dSXin LI } 1088b6cee71dSXin LI (void)memset(&mp[mset[i].count], 0, sizeof(*mp) * 1089b6cee71dSXin LI ALLOC_INCR); 1090b6cee71dSXin LI mset[i].me = mp; 1091b6cee71dSXin LI } 1092b6cee71dSXin LI mset[i].me[mset[i].count++] = *me; 1093b6cee71dSXin LI memset(me, 0, sizeof(*me)); 1094b6cee71dSXin LI return 0; 1095b6cee71dSXin LI } 1096b6cee71dSXin LI 1097b6cee71dSXin LI /* 1098b6cee71dSXin LI * Load and parse one file. 1099b6cee71dSXin LI */ 1100b6cee71dSXin LI private void 1101b6cee71dSXin LI load_1(struct magic_set *ms, int action, const char *fn, int *errs, 1102b6cee71dSXin LI struct magic_entry_set *mset) 1103b6cee71dSXin LI { 1104b6cee71dSXin LI size_t lineno = 0, llen = 0; 1105b6cee71dSXin LI char *line = NULL; 1106b6cee71dSXin LI ssize_t len; 1107b6cee71dSXin LI struct magic_entry me; 1108b6cee71dSXin LI 1109b6cee71dSXin LI FILE *f = fopen(ms->file = fn, "r"); 1110b6cee71dSXin LI if (f == NULL) { 1111b6cee71dSXin LI if (errno != ENOENT) 1112b6cee71dSXin LI file_error(ms, errno, "cannot read magic file `%s'", 1113b6cee71dSXin LI fn); 1114b6cee71dSXin LI (*errs)++; 1115b6cee71dSXin LI return; 1116b6cee71dSXin LI } 1117b6cee71dSXin LI 1118b6cee71dSXin LI memset(&me, 0, sizeof(me)); 1119b6cee71dSXin LI /* read and parse this file */ 1120b6cee71dSXin LI for (ms->line = 1; (len = getline(&line, &llen, f)) != -1; 1121b6cee71dSXin LI ms->line++) { 1122b6cee71dSXin LI if (len == 0) /* null line, garbage, etc */ 1123b6cee71dSXin LI continue; 1124b6cee71dSXin LI if (line[len - 1] == '\n') { 1125b6cee71dSXin LI lineno++; 1126b6cee71dSXin LI line[len - 1] = '\0'; /* delete newline */ 1127b6cee71dSXin LI } 1128b6cee71dSXin LI switch (line[0]) { 1129b6cee71dSXin LI case '\0': /* empty, do not parse */ 1130b6cee71dSXin LI case '#': /* comment, do not parse */ 1131b6cee71dSXin LI continue; 1132b6cee71dSXin LI case '!': 1133b6cee71dSXin LI if (line[1] == ':') { 1134b6cee71dSXin LI size_t i; 1135b6cee71dSXin LI 1136b6cee71dSXin LI for (i = 0; bang[i].name != NULL; i++) { 1137b6cee71dSXin LI if ((size_t)(len - 2) > bang[i].len && 1138b6cee71dSXin LI memcmp(bang[i].name, line + 2, 1139b6cee71dSXin LI bang[i].len) == 0) 1140b6cee71dSXin LI break; 1141b6cee71dSXin LI } 1142b6cee71dSXin LI if (bang[i].name == NULL) { 1143b6cee71dSXin LI file_error(ms, 0, 1144b6cee71dSXin LI "Unknown !: entry `%s'", line); 1145b6cee71dSXin LI (*errs)++; 1146b6cee71dSXin LI continue; 1147b6cee71dSXin LI } 1148b6cee71dSXin LI if (me.mp == NULL) { 1149b6cee71dSXin LI file_error(ms, 0, 1150b6cee71dSXin LI "No current entry for :!%s type", 1151b6cee71dSXin LI bang[i].name); 1152b6cee71dSXin LI (*errs)++; 1153b6cee71dSXin LI continue; 1154b6cee71dSXin LI } 1155b6cee71dSXin LI if ((*bang[i].fun)(ms, &me, 1156b6cee71dSXin LI line + bang[i].len + 2) != 0) { 1157b6cee71dSXin LI (*errs)++; 1158b6cee71dSXin LI continue; 1159b6cee71dSXin LI } 1160b6cee71dSXin LI continue; 1161b6cee71dSXin LI } 1162b6cee71dSXin LI /*FALLTHROUGH*/ 1163b6cee71dSXin LI default: 1164b6cee71dSXin LI again: 1165b6cee71dSXin LI switch (parse(ms, &me, line, lineno, action)) { 1166b6cee71dSXin LI case 0: 1167b6cee71dSXin LI continue; 1168b6cee71dSXin LI case 1: 1169b6cee71dSXin LI (void)addentry(ms, &me, mset); 1170b6cee71dSXin LI goto again; 1171b6cee71dSXin LI default: 1172b6cee71dSXin LI (*errs)++; 1173b6cee71dSXin LI break; 1174b6cee71dSXin LI } 1175b6cee71dSXin LI } 1176b6cee71dSXin LI } 1177b6cee71dSXin LI if (me.mp) 1178b6cee71dSXin LI (void)addentry(ms, &me, mset); 1179b6cee71dSXin LI free(line); 1180b6cee71dSXin LI (void)fclose(f); 1181b6cee71dSXin LI } 1182b6cee71dSXin LI 1183b6cee71dSXin LI /* 1184b6cee71dSXin LI * parse a file or directory of files 1185b6cee71dSXin LI * const char *fn: name of magic file or directory 1186b6cee71dSXin LI */ 1187b6cee71dSXin LI private int 1188b6cee71dSXin LI cmpstrp(const void *p1, const void *p2) 1189b6cee71dSXin LI { 1190b6cee71dSXin LI return strcmp(*(char *const *)p1, *(char *const *)p2); 1191b6cee71dSXin LI } 1192b6cee71dSXin LI 1193b6cee71dSXin LI 1194b6cee71dSXin LI private uint32_t 1195b6cee71dSXin LI set_text_binary(struct magic_set *ms, struct magic_entry *me, uint32_t nme, 1196b6cee71dSXin LI uint32_t starttest) 1197b6cee71dSXin LI { 1198b6cee71dSXin LI static const char text[] = "text"; 1199b6cee71dSXin LI static const char binary[] = "binary"; 1200b6cee71dSXin LI static const size_t len = sizeof(text); 1201b6cee71dSXin LI 1202b6cee71dSXin LI uint32_t i = starttest; 1203b6cee71dSXin LI 1204b6cee71dSXin LI do { 1205b6cee71dSXin LI set_test_type(me[starttest].mp, me[i].mp); 1206b6cee71dSXin LI if ((ms->flags & MAGIC_DEBUG) == 0) 1207b6cee71dSXin LI continue; 1208b6cee71dSXin LI (void)fprintf(stderr, "%s%s%s: %s\n", 1209b6cee71dSXin LI me[i].mp->mimetype, 1210b6cee71dSXin LI me[i].mp->mimetype[0] == '\0' ? "" : "; ", 1211b6cee71dSXin LI me[i].mp->desc[0] ? me[i].mp->desc : "(no description)", 1212b6cee71dSXin LI me[i].mp->flag & BINTEST ? binary : text); 1213b6cee71dSXin LI if (me[i].mp->flag & BINTEST) { 1214b6cee71dSXin LI char *p = strstr(me[i].mp->desc, text); 1215b6cee71dSXin LI if (p && (p == me[i].mp->desc || 1216b6cee71dSXin LI isspace((unsigned char)p[-1])) && 1217b6cee71dSXin LI (p + len - me[i].mp->desc == MAXstring 1218b6cee71dSXin LI || (p[len] == '\0' || 1219b6cee71dSXin LI isspace((unsigned char)p[len])))) 1220b6cee71dSXin LI (void)fprintf(stderr, "*** Possible " 1221b6cee71dSXin LI "binary test for text type\n"); 1222b6cee71dSXin LI } 1223b6cee71dSXin LI } while (++i < nme && me[i].mp->cont_level != 0); 1224b6cee71dSXin LI return i; 1225b6cee71dSXin LI } 1226b6cee71dSXin LI 1227b6cee71dSXin LI private void 1228b6cee71dSXin LI set_last_default(struct magic_set *ms, struct magic_entry *me, uint32_t nme) 1229b6cee71dSXin LI { 1230b6cee71dSXin LI uint32_t i; 1231b6cee71dSXin LI for (i = 0; i < nme; i++) { 1232b6cee71dSXin LI if (me[i].mp->cont_level == 0 && 1233b6cee71dSXin LI me[i].mp->type == FILE_DEFAULT) { 1234b6cee71dSXin LI while (++i < nme) 1235b6cee71dSXin LI if (me[i].mp->cont_level == 0) 1236b6cee71dSXin LI break; 1237b6cee71dSXin LI if (i != nme) { 1238b6cee71dSXin LI /* XXX - Ugh! */ 1239b6cee71dSXin LI ms->line = me[i].mp->lineno; 1240b6cee71dSXin LI file_magwarn(ms, 1241b6cee71dSXin LI "level 0 \"default\" did not sort last"); 1242b6cee71dSXin LI } 1243b6cee71dSXin LI return; 1244b6cee71dSXin LI } 1245b6cee71dSXin LI } 1246b6cee71dSXin LI } 1247b6cee71dSXin LI 1248b6cee71dSXin LI private int 1249b6cee71dSXin LI coalesce_entries(struct magic_set *ms, struct magic_entry *me, uint32_t nme, 1250b6cee71dSXin LI struct magic **ma, uint32_t *nma) 1251b6cee71dSXin LI { 1252b6cee71dSXin LI uint32_t i, mentrycount = 0; 1253b6cee71dSXin LI size_t slen; 1254b6cee71dSXin LI 1255b6cee71dSXin LI for (i = 0; i < nme; i++) 1256b6cee71dSXin LI mentrycount += me[i].cont_count; 1257b6cee71dSXin LI 1258b6cee71dSXin LI slen = sizeof(**ma) * mentrycount; 1259b6cee71dSXin LI if ((*ma = CAST(struct magic *, malloc(slen))) == NULL) { 1260b6cee71dSXin LI file_oomem(ms, slen); 1261b6cee71dSXin LI return -1; 1262b6cee71dSXin LI } 1263b6cee71dSXin LI 1264b6cee71dSXin LI mentrycount = 0; 1265b6cee71dSXin LI for (i = 0; i < nme; i++) { 1266b6cee71dSXin LI (void)memcpy(*ma + mentrycount, me[i].mp, 1267b6cee71dSXin LI me[i].cont_count * sizeof(**ma)); 1268b6cee71dSXin LI mentrycount += me[i].cont_count; 1269b6cee71dSXin LI } 1270b6cee71dSXin LI *nma = mentrycount; 1271b6cee71dSXin LI return 0; 1272b6cee71dSXin LI } 1273b6cee71dSXin LI 1274b6cee71dSXin LI private void 1275b6cee71dSXin LI magic_entry_free(struct magic_entry *me, uint32_t nme) 1276b6cee71dSXin LI { 1277b6cee71dSXin LI uint32_t i; 1278b6cee71dSXin LI if (me == NULL) 1279b6cee71dSXin LI return; 1280b6cee71dSXin LI for (i = 0; i < nme; i++) 1281b6cee71dSXin LI free(me[i].mp); 1282b6cee71dSXin LI free(me); 1283b6cee71dSXin LI } 1284b6cee71dSXin LI 1285b6cee71dSXin LI private struct magic_map * 1286b6cee71dSXin LI apprentice_load(struct magic_set *ms, const char *fn, int action) 1287b6cee71dSXin LI { 1288b6cee71dSXin LI int errs = 0; 1289b6cee71dSXin LI uint32_t i, j; 1290b6cee71dSXin LI size_t files = 0, maxfiles = 0; 1291b6cee71dSXin LI char **filearr = NULL, *mfn; 1292b6cee71dSXin LI struct stat st; 1293b6cee71dSXin LI struct magic_map *map; 1294b6cee71dSXin LI struct magic_entry_set mset[MAGIC_SETS]; 1295b6cee71dSXin LI DIR *dir; 1296b6cee71dSXin LI struct dirent *d; 1297b6cee71dSXin LI 1298b6cee71dSXin LI memset(mset, 0, sizeof(mset)); 1299b6cee71dSXin LI ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */ 1300b6cee71dSXin LI 1301b6cee71dSXin LI 1302b6cee71dSXin LI if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) 1303b6cee71dSXin LI { 1304b6cee71dSXin LI file_oomem(ms, sizeof(*map)); 1305b6cee71dSXin LI return NULL; 1306b6cee71dSXin LI } 13079ce06829SXin LI map->type = MAP_TYPE_MALLOC; 1308b6cee71dSXin LI 1309b6cee71dSXin LI /* print silly verbose header for USG compat. */ 1310b6cee71dSXin LI if (action == FILE_CHECK) 1311b6cee71dSXin LI (void)fprintf(stderr, "%s\n", usg_hdr); 1312b6cee71dSXin LI 1313b6cee71dSXin LI /* load directory or file */ 1314b6cee71dSXin LI if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) { 1315b6cee71dSXin LI dir = opendir(fn); 1316b6cee71dSXin LI if (!dir) { 1317b6cee71dSXin LI errs++; 1318b6cee71dSXin LI goto out; 1319b6cee71dSXin LI } 1320b6cee71dSXin LI while ((d = readdir(dir)) != NULL) { 1321b6cee71dSXin LI if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) { 1322b6cee71dSXin LI file_oomem(ms, 1323b6cee71dSXin LI strlen(fn) + strlen(d->d_name) + 2); 1324b6cee71dSXin LI errs++; 1325b6cee71dSXin LI closedir(dir); 1326b6cee71dSXin LI goto out; 1327b6cee71dSXin LI } 1328b6cee71dSXin LI if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) { 1329b6cee71dSXin LI free(mfn); 1330b6cee71dSXin LI continue; 1331b6cee71dSXin LI } 1332b6cee71dSXin LI if (files >= maxfiles) { 1333b6cee71dSXin LI size_t mlen; 1334b6cee71dSXin LI maxfiles = (maxfiles + 1) * 2; 1335b6cee71dSXin LI mlen = maxfiles * sizeof(*filearr); 1336b6cee71dSXin LI if ((filearr = CAST(char **, 1337b6cee71dSXin LI realloc(filearr, mlen))) == NULL) { 1338b6cee71dSXin LI file_oomem(ms, mlen); 1339b6cee71dSXin LI free(mfn); 1340b6cee71dSXin LI closedir(dir); 1341b6cee71dSXin LI errs++; 1342b6cee71dSXin LI goto out; 1343b6cee71dSXin LI } 1344b6cee71dSXin LI } 1345b6cee71dSXin LI filearr[files++] = mfn; 1346b6cee71dSXin LI } 1347b6cee71dSXin LI closedir(dir); 1348b6cee71dSXin LI qsort(filearr, files, sizeof(*filearr), cmpstrp); 1349b6cee71dSXin LI for (i = 0; i < files; i++) { 1350b6cee71dSXin LI load_1(ms, action, filearr[i], &errs, mset); 1351b6cee71dSXin LI free(filearr[i]); 1352b6cee71dSXin LI } 1353b6cee71dSXin LI free(filearr); 1354b6cee71dSXin LI } else 1355b6cee71dSXin LI load_1(ms, action, fn, &errs, mset); 1356b6cee71dSXin LI if (errs) 1357b6cee71dSXin LI goto out; 1358b6cee71dSXin LI 1359b6cee71dSXin LI for (j = 0; j < MAGIC_SETS; j++) { 1360b6cee71dSXin LI /* Set types of tests */ 1361b6cee71dSXin LI for (i = 0; i < mset[j].count; ) { 1362b6cee71dSXin LI if (mset[j].me[i].mp->cont_level != 0) { 1363b6cee71dSXin LI i++; 1364b6cee71dSXin LI continue; 1365b6cee71dSXin LI } 1366b6cee71dSXin LI i = set_text_binary(ms, mset[j].me, mset[j].count, i); 1367b6cee71dSXin LI } 13689ce06829SXin LI if (mset[j].me) 1369b6cee71dSXin LI qsort(mset[j].me, mset[j].count, sizeof(*mset[j].me), 1370b6cee71dSXin LI apprentice_sort); 1371b6cee71dSXin LI 1372b6cee71dSXin LI /* 1373b6cee71dSXin LI * Make sure that any level 0 "default" line is last 1374b6cee71dSXin LI * (if one exists). 1375b6cee71dSXin LI */ 1376b6cee71dSXin LI set_last_default(ms, mset[j].me, mset[j].count); 1377b6cee71dSXin LI 1378b6cee71dSXin LI /* coalesce per file arrays into a single one */ 1379b6cee71dSXin LI if (coalesce_entries(ms, mset[j].me, mset[j].count, 1380b6cee71dSXin LI &map->magic[j], &map->nmagic[j]) == -1) { 1381b6cee71dSXin LI errs++; 1382b6cee71dSXin LI goto out; 1383b6cee71dSXin LI } 1384b6cee71dSXin LI } 1385b6cee71dSXin LI 1386b6cee71dSXin LI out: 1387b6cee71dSXin LI for (j = 0; j < MAGIC_SETS; j++) 1388b6cee71dSXin LI magic_entry_free(mset[j].me, mset[j].count); 1389b6cee71dSXin LI 1390b6cee71dSXin LI if (errs) { 1391b6cee71dSXin LI apprentice_unmap(map); 1392b6cee71dSXin LI return NULL; 1393b6cee71dSXin LI } 1394b6cee71dSXin LI return map; 1395b6cee71dSXin LI } 1396b6cee71dSXin LI 1397b6cee71dSXin LI /* 1398b6cee71dSXin LI * extend the sign bit if the comparison is to be signed 1399b6cee71dSXin LI */ 1400b6cee71dSXin LI protected uint64_t 1401b6cee71dSXin LI file_signextend(struct magic_set *ms, struct magic *m, uint64_t v) 1402b6cee71dSXin LI { 1403b6cee71dSXin LI if (!(m->flag & UNSIGNED)) { 1404b6cee71dSXin LI switch(m->type) { 1405b6cee71dSXin LI /* 1406b6cee71dSXin LI * Do not remove the casts below. They are 1407b6cee71dSXin LI * vital. When later compared with the data, 1408b6cee71dSXin LI * the sign extension must have happened. 1409b6cee71dSXin LI */ 1410b6cee71dSXin LI case FILE_BYTE: 1411c2931133SXin LI v = (signed char) v; 1412b6cee71dSXin LI break; 1413b6cee71dSXin LI case FILE_SHORT: 1414b6cee71dSXin LI case FILE_BESHORT: 1415b6cee71dSXin LI case FILE_LESHORT: 1416b6cee71dSXin LI v = (short) v; 1417b6cee71dSXin LI break; 1418b6cee71dSXin LI case FILE_DATE: 1419b6cee71dSXin LI case FILE_BEDATE: 1420b6cee71dSXin LI case FILE_LEDATE: 1421b6cee71dSXin LI case FILE_MEDATE: 1422b6cee71dSXin LI case FILE_LDATE: 1423b6cee71dSXin LI case FILE_BELDATE: 1424b6cee71dSXin LI case FILE_LELDATE: 1425b6cee71dSXin LI case FILE_MELDATE: 1426b6cee71dSXin LI case FILE_LONG: 1427b6cee71dSXin LI case FILE_BELONG: 1428b6cee71dSXin LI case FILE_LELONG: 1429b6cee71dSXin LI case FILE_MELONG: 1430b6cee71dSXin LI case FILE_FLOAT: 1431b6cee71dSXin LI case FILE_BEFLOAT: 1432b6cee71dSXin LI case FILE_LEFLOAT: 1433b6cee71dSXin LI v = (int32_t) v; 1434b6cee71dSXin LI break; 1435b6cee71dSXin LI case FILE_QUAD: 1436b6cee71dSXin LI case FILE_BEQUAD: 1437b6cee71dSXin LI case FILE_LEQUAD: 1438b6cee71dSXin LI case FILE_QDATE: 1439b6cee71dSXin LI case FILE_QLDATE: 1440b6cee71dSXin LI case FILE_QWDATE: 1441b6cee71dSXin LI case FILE_BEQDATE: 1442b6cee71dSXin LI case FILE_BEQLDATE: 1443b6cee71dSXin LI case FILE_BEQWDATE: 1444b6cee71dSXin LI case FILE_LEQDATE: 1445b6cee71dSXin LI case FILE_LEQLDATE: 1446b6cee71dSXin LI case FILE_LEQWDATE: 1447b6cee71dSXin LI case FILE_DOUBLE: 1448b6cee71dSXin LI case FILE_BEDOUBLE: 1449b6cee71dSXin LI case FILE_LEDOUBLE: 1450b6cee71dSXin LI v = (int64_t) v; 1451b6cee71dSXin LI break; 1452b6cee71dSXin LI case FILE_STRING: 1453b6cee71dSXin LI case FILE_PSTRING: 1454b6cee71dSXin LI case FILE_BESTRING16: 1455b6cee71dSXin LI case FILE_LESTRING16: 1456b6cee71dSXin LI case FILE_REGEX: 1457b6cee71dSXin LI case FILE_SEARCH: 1458b6cee71dSXin LI case FILE_DEFAULT: 1459b6cee71dSXin LI case FILE_INDIRECT: 1460b6cee71dSXin LI case FILE_NAME: 1461b6cee71dSXin LI case FILE_USE: 1462b6cee71dSXin LI case FILE_CLEAR: 1463*3e41d09dSXin LI case FILE_DER: 1464b6cee71dSXin LI break; 1465b6cee71dSXin LI default: 1466b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1467b6cee71dSXin LI file_magwarn(ms, "cannot happen: m->type=%d\n", 1468b6cee71dSXin LI m->type); 1469b6cee71dSXin LI return ~0U; 1470b6cee71dSXin LI } 1471b6cee71dSXin LI } 1472b6cee71dSXin LI return v; 1473b6cee71dSXin LI } 1474b6cee71dSXin LI 1475b6cee71dSXin LI private int 1476b6cee71dSXin LI string_modifier_check(struct magic_set *ms, struct magic *m) 1477b6cee71dSXin LI { 1478b6cee71dSXin LI if ((ms->flags & MAGIC_CHECK) == 0) 1479b6cee71dSXin LI return 0; 1480b6cee71dSXin LI 1481b6cee71dSXin LI if ((m->type != FILE_REGEX || (m->str_flags & REGEX_LINE_COUNT) == 0) && 1482b6cee71dSXin LI (m->type != FILE_PSTRING && (m->str_flags & PSTRING_LEN) != 0)) { 1483b6cee71dSXin LI file_magwarn(ms, 1484b6cee71dSXin LI "'/BHhLl' modifiers are only allowed for pascal strings\n"); 1485b6cee71dSXin LI return -1; 1486b6cee71dSXin LI } 1487b6cee71dSXin LI switch (m->type) { 1488b6cee71dSXin LI case FILE_BESTRING16: 1489b6cee71dSXin LI case FILE_LESTRING16: 1490b6cee71dSXin LI if (m->str_flags != 0) { 1491b6cee71dSXin LI file_magwarn(ms, 1492b6cee71dSXin LI "no modifiers allowed for 16-bit strings\n"); 1493b6cee71dSXin LI return -1; 1494b6cee71dSXin LI } 1495b6cee71dSXin LI break; 1496b6cee71dSXin LI case FILE_STRING: 1497b6cee71dSXin LI case FILE_PSTRING: 1498b6cee71dSXin LI if ((m->str_flags & REGEX_OFFSET_START) != 0) { 1499b6cee71dSXin LI file_magwarn(ms, 1500b6cee71dSXin LI "'/%c' only allowed on regex and search\n", 1501b6cee71dSXin LI CHAR_REGEX_OFFSET_START); 1502b6cee71dSXin LI return -1; 1503b6cee71dSXin LI } 1504b6cee71dSXin LI break; 1505b6cee71dSXin LI case FILE_SEARCH: 1506b6cee71dSXin LI if (m->str_range == 0) { 1507b6cee71dSXin LI file_magwarn(ms, 1508b6cee71dSXin LI "missing range; defaulting to %d\n", 1509b6cee71dSXin LI STRING_DEFAULT_RANGE); 1510b6cee71dSXin LI m->str_range = STRING_DEFAULT_RANGE; 1511b6cee71dSXin LI return -1; 1512b6cee71dSXin LI } 1513b6cee71dSXin LI break; 1514b6cee71dSXin LI case FILE_REGEX: 1515b6cee71dSXin LI if ((m->str_flags & STRING_COMPACT_WHITESPACE) != 0) { 1516b6cee71dSXin LI file_magwarn(ms, "'/%c' not allowed on regex\n", 1517b6cee71dSXin LI CHAR_COMPACT_WHITESPACE); 1518b6cee71dSXin LI return -1; 1519b6cee71dSXin LI } 1520b6cee71dSXin LI if ((m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) != 0) { 1521b6cee71dSXin LI file_magwarn(ms, "'/%c' not allowed on regex\n", 1522b6cee71dSXin LI CHAR_COMPACT_OPTIONAL_WHITESPACE); 1523b6cee71dSXin LI return -1; 1524b6cee71dSXin LI } 1525b6cee71dSXin LI break; 1526b6cee71dSXin LI default: 1527b6cee71dSXin LI file_magwarn(ms, "coding error: m->type=%d\n", 1528b6cee71dSXin LI m->type); 1529b6cee71dSXin LI return -1; 1530b6cee71dSXin LI } 1531b6cee71dSXin LI return 0; 1532b6cee71dSXin LI } 1533b6cee71dSXin LI 1534b6cee71dSXin LI private int 1535b6cee71dSXin LI get_op(char c) 1536b6cee71dSXin LI { 1537b6cee71dSXin LI switch (c) { 1538b6cee71dSXin LI case '&': 1539b6cee71dSXin LI return FILE_OPAND; 1540b6cee71dSXin LI case '|': 1541b6cee71dSXin LI return FILE_OPOR; 1542b6cee71dSXin LI case '^': 1543b6cee71dSXin LI return FILE_OPXOR; 1544b6cee71dSXin LI case '+': 1545b6cee71dSXin LI return FILE_OPADD; 1546b6cee71dSXin LI case '-': 1547b6cee71dSXin LI return FILE_OPMINUS; 1548b6cee71dSXin LI case '*': 1549b6cee71dSXin LI return FILE_OPMULTIPLY; 1550b6cee71dSXin LI case '/': 1551b6cee71dSXin LI return FILE_OPDIVIDE; 1552b6cee71dSXin LI case '%': 1553b6cee71dSXin LI return FILE_OPMODULO; 1554b6cee71dSXin LI default: 1555b6cee71dSXin LI return -1; 1556b6cee71dSXin LI } 1557b6cee71dSXin LI } 1558b6cee71dSXin LI 1559b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS 1560b6cee71dSXin LI private int 1561b6cee71dSXin LI get_cond(const char *l, const char **t) 1562b6cee71dSXin LI { 1563b6cee71dSXin LI static const struct cond_tbl_s { 1564b6cee71dSXin LI char name[8]; 1565b6cee71dSXin LI size_t len; 1566b6cee71dSXin LI int cond; 1567b6cee71dSXin LI } cond_tbl[] = { 1568b6cee71dSXin LI { "if", 2, COND_IF }, 1569b6cee71dSXin LI { "elif", 4, COND_ELIF }, 1570b6cee71dSXin LI { "else", 4, COND_ELSE }, 1571b6cee71dSXin LI { "", 0, COND_NONE }, 1572b6cee71dSXin LI }; 1573b6cee71dSXin LI const struct cond_tbl_s *p; 1574b6cee71dSXin LI 1575b6cee71dSXin LI for (p = cond_tbl; p->len; p++) { 1576b6cee71dSXin LI if (strncmp(l, p->name, p->len) == 0 && 1577b6cee71dSXin LI isspace((unsigned char)l[p->len])) { 1578b6cee71dSXin LI if (t) 1579b6cee71dSXin LI *t = l + p->len; 1580b6cee71dSXin LI break; 1581b6cee71dSXin LI } 1582b6cee71dSXin LI } 1583b6cee71dSXin LI return p->cond; 1584b6cee71dSXin LI } 1585b6cee71dSXin LI 1586b6cee71dSXin LI private int 1587b6cee71dSXin LI check_cond(struct magic_set *ms, int cond, uint32_t cont_level) 1588b6cee71dSXin LI { 1589b6cee71dSXin LI int last_cond; 1590b6cee71dSXin LI last_cond = ms->c.li[cont_level].last_cond; 1591b6cee71dSXin LI 1592b6cee71dSXin LI switch (cond) { 1593b6cee71dSXin LI case COND_IF: 1594b6cee71dSXin LI if (last_cond != COND_NONE && last_cond != COND_ELIF) { 1595b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1596b6cee71dSXin LI file_magwarn(ms, "syntax error: `if'"); 1597b6cee71dSXin LI return -1; 1598b6cee71dSXin LI } 1599b6cee71dSXin LI last_cond = COND_IF; 1600b6cee71dSXin LI break; 1601b6cee71dSXin LI 1602b6cee71dSXin LI case COND_ELIF: 1603b6cee71dSXin LI if (last_cond != COND_IF && last_cond != COND_ELIF) { 1604b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1605b6cee71dSXin LI file_magwarn(ms, "syntax error: `elif'"); 1606b6cee71dSXin LI return -1; 1607b6cee71dSXin LI } 1608b6cee71dSXin LI last_cond = COND_ELIF; 1609b6cee71dSXin LI break; 1610b6cee71dSXin LI 1611b6cee71dSXin LI case COND_ELSE: 1612b6cee71dSXin LI if (last_cond != COND_IF && last_cond != COND_ELIF) { 1613b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1614b6cee71dSXin LI file_magwarn(ms, "syntax error: `else'"); 1615b6cee71dSXin LI return -1; 1616b6cee71dSXin LI } 1617b6cee71dSXin LI last_cond = COND_NONE; 1618b6cee71dSXin LI break; 1619b6cee71dSXin LI 1620b6cee71dSXin LI case COND_NONE: 1621b6cee71dSXin LI last_cond = COND_NONE; 1622b6cee71dSXin LI break; 1623b6cee71dSXin LI } 1624b6cee71dSXin LI 1625b6cee71dSXin LI ms->c.li[cont_level].last_cond = last_cond; 1626b6cee71dSXin LI return 0; 1627b6cee71dSXin LI } 1628b6cee71dSXin LI #endif /* ENABLE_CONDITIONALS */ 1629b6cee71dSXin LI 16304460e5b0SXin LI private int 16314460e5b0SXin LI parse_indirect_modifier(struct magic_set *ms, struct magic *m, const char **lp) 16324460e5b0SXin LI { 16334460e5b0SXin LI const char *l = *lp; 16344460e5b0SXin LI 16354460e5b0SXin LI while (!isspace((unsigned char)*++l)) 16364460e5b0SXin LI switch (*l) { 16374460e5b0SXin LI case CHAR_INDIRECT_RELATIVE: 16384460e5b0SXin LI m->str_flags |= INDIRECT_RELATIVE; 16394460e5b0SXin LI break; 16404460e5b0SXin LI default: 16414460e5b0SXin LI if (ms->flags & MAGIC_CHECK) 16424460e5b0SXin LI file_magwarn(ms, "indirect modifier `%c' " 16434460e5b0SXin LI "invalid", *l); 16444460e5b0SXin LI *lp = l; 16454460e5b0SXin LI return -1; 16464460e5b0SXin LI } 16474460e5b0SXin LI *lp = l; 16484460e5b0SXin LI return 0; 16494460e5b0SXin LI } 16504460e5b0SXin LI 16514460e5b0SXin LI private void 16524460e5b0SXin LI parse_op_modifier(struct magic_set *ms, struct magic *m, const char **lp, 16534460e5b0SXin LI int op) 16544460e5b0SXin LI { 16554460e5b0SXin LI const char *l = *lp; 16564460e5b0SXin LI char *t; 16574460e5b0SXin LI uint64_t val; 16584460e5b0SXin LI 16594460e5b0SXin LI ++l; 16604460e5b0SXin LI m->mask_op |= op; 16614460e5b0SXin LI val = (uint64_t)strtoull(l, &t, 0); 16624460e5b0SXin LI l = t; 16634460e5b0SXin LI m->num_mask = file_signextend(ms, m, val); 16644460e5b0SXin LI eatsize(&l); 16654460e5b0SXin LI *lp = l; 16664460e5b0SXin LI } 16674460e5b0SXin LI 16684460e5b0SXin LI private int 16694460e5b0SXin LI parse_string_modifier(struct magic_set *ms, struct magic *m, const char **lp) 16704460e5b0SXin LI { 16714460e5b0SXin LI const char *l = *lp; 16724460e5b0SXin LI char *t; 16734460e5b0SXin LI int have_range = 0; 16744460e5b0SXin LI 16754460e5b0SXin LI while (!isspace((unsigned char)*++l)) { 16764460e5b0SXin LI switch (*l) { 16774460e5b0SXin LI case '0': case '1': case '2': 16784460e5b0SXin LI case '3': case '4': case '5': 16794460e5b0SXin LI case '6': case '7': case '8': 16804460e5b0SXin LI case '9': 16814460e5b0SXin LI if (have_range && (ms->flags & MAGIC_CHECK)) 16824460e5b0SXin LI file_magwarn(ms, "multiple ranges"); 16834460e5b0SXin LI have_range = 1; 16844460e5b0SXin LI m->str_range = CAST(uint32_t, strtoul(l, &t, 0)); 16854460e5b0SXin LI if (m->str_range == 0) 16864460e5b0SXin LI file_magwarn(ms, "zero range"); 16874460e5b0SXin LI l = t - 1; 16884460e5b0SXin LI break; 16894460e5b0SXin LI case CHAR_COMPACT_WHITESPACE: 16904460e5b0SXin LI m->str_flags |= STRING_COMPACT_WHITESPACE; 16914460e5b0SXin LI break; 16924460e5b0SXin LI case CHAR_COMPACT_OPTIONAL_WHITESPACE: 16934460e5b0SXin LI m->str_flags |= STRING_COMPACT_OPTIONAL_WHITESPACE; 16944460e5b0SXin LI break; 16954460e5b0SXin LI case CHAR_IGNORE_LOWERCASE: 16964460e5b0SXin LI m->str_flags |= STRING_IGNORE_LOWERCASE; 16974460e5b0SXin LI break; 16984460e5b0SXin LI case CHAR_IGNORE_UPPERCASE: 16994460e5b0SXin LI m->str_flags |= STRING_IGNORE_UPPERCASE; 17004460e5b0SXin LI break; 17014460e5b0SXin LI case CHAR_REGEX_OFFSET_START: 17024460e5b0SXin LI m->str_flags |= REGEX_OFFSET_START; 17034460e5b0SXin LI break; 17044460e5b0SXin LI case CHAR_BINTEST: 17054460e5b0SXin LI m->str_flags |= STRING_BINTEST; 17064460e5b0SXin LI break; 17074460e5b0SXin LI case CHAR_TEXTTEST: 17084460e5b0SXin LI m->str_flags |= STRING_TEXTTEST; 17094460e5b0SXin LI break; 17104460e5b0SXin LI case CHAR_TRIM: 17114460e5b0SXin LI m->str_flags |= STRING_TRIM; 17124460e5b0SXin LI break; 17134460e5b0SXin LI case CHAR_PSTRING_1_LE: 17144460e5b0SXin LI #define SET_LENGTH(a) m->str_flags = (m->str_flags & ~PSTRING_LEN) | (a) 17154460e5b0SXin LI if (m->type != FILE_PSTRING) 17164460e5b0SXin LI goto bad; 17174460e5b0SXin LI SET_LENGTH(PSTRING_1_LE); 17184460e5b0SXin LI break; 17194460e5b0SXin LI case CHAR_PSTRING_2_BE: 17204460e5b0SXin LI if (m->type != FILE_PSTRING) 17214460e5b0SXin LI goto bad; 17224460e5b0SXin LI SET_LENGTH(PSTRING_2_BE); 17234460e5b0SXin LI break; 17244460e5b0SXin LI case CHAR_PSTRING_2_LE: 17254460e5b0SXin LI if (m->type != FILE_PSTRING) 17264460e5b0SXin LI goto bad; 17274460e5b0SXin LI SET_LENGTH(PSTRING_2_LE); 17284460e5b0SXin LI break; 17294460e5b0SXin LI case CHAR_PSTRING_4_BE: 17304460e5b0SXin LI if (m->type != FILE_PSTRING) 17314460e5b0SXin LI goto bad; 17324460e5b0SXin LI SET_LENGTH(PSTRING_4_BE); 17334460e5b0SXin LI break; 17344460e5b0SXin LI case CHAR_PSTRING_4_LE: 17354460e5b0SXin LI switch (m->type) { 17364460e5b0SXin LI case FILE_PSTRING: 17374460e5b0SXin LI case FILE_REGEX: 17384460e5b0SXin LI break; 17394460e5b0SXin LI default: 17404460e5b0SXin LI goto bad; 17414460e5b0SXin LI } 17424460e5b0SXin LI SET_LENGTH(PSTRING_4_LE); 17434460e5b0SXin LI break; 17444460e5b0SXin LI case CHAR_PSTRING_LENGTH_INCLUDES_ITSELF: 17454460e5b0SXin LI if (m->type != FILE_PSTRING) 17464460e5b0SXin LI goto bad; 17474460e5b0SXin LI m->str_flags |= PSTRING_LENGTH_INCLUDES_ITSELF; 17484460e5b0SXin LI break; 17494460e5b0SXin LI default: 17504460e5b0SXin LI bad: 17514460e5b0SXin LI if (ms->flags & MAGIC_CHECK) 17524460e5b0SXin LI file_magwarn(ms, "string modifier `%c' " 17534460e5b0SXin LI "invalid", *l); 17544460e5b0SXin LI goto out; 17554460e5b0SXin LI } 17564460e5b0SXin LI /* allow multiple '/' for readability */ 17574460e5b0SXin LI if (l[1] == '/' && !isspace((unsigned char)l[2])) 17584460e5b0SXin LI l++; 17594460e5b0SXin LI } 17604460e5b0SXin LI if (string_modifier_check(ms, m) == -1) 17614460e5b0SXin LI goto out; 17624460e5b0SXin LI *lp = l; 17634460e5b0SXin LI return 0; 17644460e5b0SXin LI out: 17654460e5b0SXin LI *lp = l; 17664460e5b0SXin LI return -1; 17674460e5b0SXin LI } 17684460e5b0SXin LI 1769b6cee71dSXin LI /* 1770b6cee71dSXin LI * parse one line from magic file, put into magic[index++] if valid 1771b6cee71dSXin LI */ 1772b6cee71dSXin LI private int 1773b6cee71dSXin LI parse(struct magic_set *ms, struct magic_entry *me, const char *line, 1774b6cee71dSXin LI size_t lineno, int action) 1775b6cee71dSXin LI { 1776b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS 1777b6cee71dSXin LI static uint32_t last_cont_level = 0; 1778b6cee71dSXin LI #endif 1779b6cee71dSXin LI size_t i; 1780b6cee71dSXin LI struct magic *m; 1781b6cee71dSXin LI const char *l = line; 1782b6cee71dSXin LI char *t; 1783b6cee71dSXin LI int op; 1784b6cee71dSXin LI uint32_t cont_level; 1785b6cee71dSXin LI int32_t diff; 1786b6cee71dSXin LI 1787b6cee71dSXin LI cont_level = 0; 1788b6cee71dSXin LI 1789b6cee71dSXin LI /* 1790b6cee71dSXin LI * Parse the offset. 1791b6cee71dSXin LI */ 1792b6cee71dSXin LI while (*l == '>') { 1793b6cee71dSXin LI ++l; /* step over */ 1794b6cee71dSXin LI cont_level++; 1795b6cee71dSXin LI } 1796b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS 1797b6cee71dSXin LI if (cont_level == 0 || cont_level > last_cont_level) 1798b6cee71dSXin LI if (file_check_mem(ms, cont_level) == -1) 1799b6cee71dSXin LI return -1; 1800b6cee71dSXin LI last_cont_level = cont_level; 1801b6cee71dSXin LI #endif 1802b6cee71dSXin LI if (cont_level != 0) { 1803b6cee71dSXin LI if (me->mp == NULL) { 1804b6cee71dSXin LI file_magerror(ms, "No current entry for continuation"); 1805b6cee71dSXin LI return -1; 1806b6cee71dSXin LI } 1807b6cee71dSXin LI if (me->cont_count == 0) { 1808b6cee71dSXin LI file_magerror(ms, "Continuations present with 0 count"); 1809b6cee71dSXin LI return -1; 1810b6cee71dSXin LI } 1811b6cee71dSXin LI m = &me->mp[me->cont_count - 1]; 1812b6cee71dSXin LI diff = (int32_t)cont_level - (int32_t)m->cont_level; 1813b6cee71dSXin LI if (diff > 1) 1814b6cee71dSXin LI file_magwarn(ms, "New continuation level %u is more " 1815b6cee71dSXin LI "than one larger than current level %u", cont_level, 1816b6cee71dSXin LI m->cont_level); 1817b6cee71dSXin LI if (me->cont_count == me->max_count) { 1818b6cee71dSXin LI struct magic *nm; 1819b6cee71dSXin LI size_t cnt = me->max_count + ALLOC_CHUNK; 1820b6cee71dSXin LI if ((nm = CAST(struct magic *, realloc(me->mp, 1821b6cee71dSXin LI sizeof(*nm) * cnt))) == NULL) { 1822b6cee71dSXin LI file_oomem(ms, sizeof(*nm) * cnt); 1823b6cee71dSXin LI return -1; 1824b6cee71dSXin LI } 1825b6cee71dSXin LI me->mp = m = nm; 1826b6cee71dSXin LI me->max_count = CAST(uint32_t, cnt); 1827b6cee71dSXin LI } 1828b6cee71dSXin LI m = &me->mp[me->cont_count++]; 1829b6cee71dSXin LI (void)memset(m, 0, sizeof(*m)); 1830b6cee71dSXin LI m->cont_level = cont_level; 1831b6cee71dSXin LI } else { 1832b6cee71dSXin LI static const size_t len = sizeof(*m) * ALLOC_CHUNK; 1833b6cee71dSXin LI if (me->mp != NULL) 1834b6cee71dSXin LI return 1; 1835b6cee71dSXin LI if ((m = CAST(struct magic *, malloc(len))) == NULL) { 1836b6cee71dSXin LI file_oomem(ms, len); 1837b6cee71dSXin LI return -1; 1838b6cee71dSXin LI } 1839b6cee71dSXin LI me->mp = m; 1840b6cee71dSXin LI me->max_count = ALLOC_CHUNK; 1841b6cee71dSXin LI (void)memset(m, 0, sizeof(*m)); 1842b6cee71dSXin LI m->factor_op = FILE_FACTOR_OP_NONE; 1843b6cee71dSXin LI m->cont_level = 0; 1844b6cee71dSXin LI me->cont_count = 1; 1845b6cee71dSXin LI } 1846b6cee71dSXin LI m->lineno = CAST(uint32_t, lineno); 1847b6cee71dSXin LI 1848b6cee71dSXin LI if (*l == '&') { /* m->cont_level == 0 checked below. */ 1849b6cee71dSXin LI ++l; /* step over */ 1850b6cee71dSXin LI m->flag |= OFFADD; 1851b6cee71dSXin LI } 1852b6cee71dSXin LI if (*l == '(') { 1853b6cee71dSXin LI ++l; /* step over */ 1854b6cee71dSXin LI m->flag |= INDIR; 1855b6cee71dSXin LI if (m->flag & OFFADD) 1856b6cee71dSXin LI m->flag = (m->flag & ~OFFADD) | INDIROFFADD; 1857b6cee71dSXin LI 1858b6cee71dSXin LI if (*l == '&') { /* m->cont_level == 0 checked below */ 1859b6cee71dSXin LI ++l; /* step over */ 1860b6cee71dSXin LI m->flag |= OFFADD; 1861b6cee71dSXin LI } 1862b6cee71dSXin LI } 1863b6cee71dSXin LI /* Indirect offsets are not valid at level 0. */ 18645f0216bdSXin LI if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD))) { 1865b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1866b6cee71dSXin LI file_magwarn(ms, "relative offset at level 0"); 18675f0216bdSXin LI return -1; 18685f0216bdSXin LI } 1869b6cee71dSXin LI 1870b6cee71dSXin LI /* get offset, then skip over it */ 1871b6cee71dSXin LI m->offset = (uint32_t)strtoul(l, &t, 0); 18725f0216bdSXin LI if (l == t) { 1873b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1874b6cee71dSXin LI file_magwarn(ms, "offset `%s' invalid", l); 18755f0216bdSXin LI return -1; 18765f0216bdSXin LI } 1877b6cee71dSXin LI l = t; 1878b6cee71dSXin LI 1879b6cee71dSXin LI if (m->flag & INDIR) { 1880b6cee71dSXin LI m->in_type = FILE_LONG; 1881b6cee71dSXin LI m->in_offset = 0; 1882b6cee71dSXin LI /* 1883b6cee71dSXin LI * read [.lbs][+-]nnnnn) 1884b6cee71dSXin LI */ 1885b6cee71dSXin LI if (*l == '.') { 1886b6cee71dSXin LI l++; 1887b6cee71dSXin LI switch (*l) { 1888b6cee71dSXin LI case 'l': 1889b6cee71dSXin LI m->in_type = FILE_LELONG; 1890b6cee71dSXin LI break; 1891b6cee71dSXin LI case 'L': 1892b6cee71dSXin LI m->in_type = FILE_BELONG; 1893b6cee71dSXin LI break; 1894b6cee71dSXin LI case 'm': 1895b6cee71dSXin LI m->in_type = FILE_MELONG; 1896b6cee71dSXin LI break; 1897b6cee71dSXin LI case 'h': 1898b6cee71dSXin LI case 's': 1899b6cee71dSXin LI m->in_type = FILE_LESHORT; 1900b6cee71dSXin LI break; 1901b6cee71dSXin LI case 'H': 1902b6cee71dSXin LI case 'S': 1903b6cee71dSXin LI m->in_type = FILE_BESHORT; 1904b6cee71dSXin LI break; 1905b6cee71dSXin LI case 'c': 1906b6cee71dSXin LI case 'b': 1907b6cee71dSXin LI case 'C': 1908b6cee71dSXin LI case 'B': 1909b6cee71dSXin LI m->in_type = FILE_BYTE; 1910b6cee71dSXin LI break; 1911b6cee71dSXin LI case 'e': 1912b6cee71dSXin LI case 'f': 1913b6cee71dSXin LI case 'g': 1914b6cee71dSXin LI m->in_type = FILE_LEDOUBLE; 1915b6cee71dSXin LI break; 1916b6cee71dSXin LI case 'E': 1917b6cee71dSXin LI case 'F': 1918b6cee71dSXin LI case 'G': 1919b6cee71dSXin LI m->in_type = FILE_BEDOUBLE; 1920b6cee71dSXin LI break; 1921b6cee71dSXin LI case 'i': 1922b6cee71dSXin LI m->in_type = FILE_LEID3; 1923b6cee71dSXin LI break; 1924b6cee71dSXin LI case 'I': 1925b6cee71dSXin LI m->in_type = FILE_BEID3; 1926b6cee71dSXin LI break; 1927b6cee71dSXin LI default: 1928b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1929b6cee71dSXin LI file_magwarn(ms, 1930b6cee71dSXin LI "indirect offset type `%c' invalid", 1931b6cee71dSXin LI *l); 19325f0216bdSXin LI return -1; 1933b6cee71dSXin LI } 1934b6cee71dSXin LI l++; 1935b6cee71dSXin LI } 1936b6cee71dSXin LI 1937b6cee71dSXin LI m->in_op = 0; 1938b6cee71dSXin LI if (*l == '~') { 1939b6cee71dSXin LI m->in_op |= FILE_OPINVERSE; 1940b6cee71dSXin LI l++; 1941b6cee71dSXin LI } 1942b6cee71dSXin LI if ((op = get_op(*l)) != -1) { 1943b6cee71dSXin LI m->in_op |= op; 1944b6cee71dSXin LI l++; 1945b6cee71dSXin LI } 1946b6cee71dSXin LI if (*l == '(') { 1947b6cee71dSXin LI m->in_op |= FILE_OPINDIRECT; 1948b6cee71dSXin LI l++; 1949b6cee71dSXin LI } 1950b6cee71dSXin LI if (isdigit((unsigned char)*l) || *l == '-') { 1951b6cee71dSXin LI m->in_offset = (int32_t)strtol(l, &t, 0); 19525f0216bdSXin LI if (l == t) { 1953b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1954b6cee71dSXin LI file_magwarn(ms, 1955b6cee71dSXin LI "in_offset `%s' invalid", l); 19565f0216bdSXin LI return -1; 19575f0216bdSXin LI } 1958b6cee71dSXin LI l = t; 1959b6cee71dSXin LI } 1960b6cee71dSXin LI if (*l++ != ')' || 19615f0216bdSXin LI ((m->in_op & FILE_OPINDIRECT) && *l++ != ')')) { 1962b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 1963b6cee71dSXin LI file_magwarn(ms, 1964b6cee71dSXin LI "missing ')' in indirect offset"); 19655f0216bdSXin LI return -1; 19665f0216bdSXin LI } 1967b6cee71dSXin LI } 1968b6cee71dSXin LI EATAB; 1969b6cee71dSXin LI 1970b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS 1971b6cee71dSXin LI m->cond = get_cond(l, &l); 1972b6cee71dSXin LI if (check_cond(ms, m->cond, cont_level) == -1) 1973b6cee71dSXin LI return -1; 1974b6cee71dSXin LI 1975b6cee71dSXin LI EATAB; 1976b6cee71dSXin LI #endif 1977b6cee71dSXin LI 1978b6cee71dSXin LI /* 1979b6cee71dSXin LI * Parse the type. 1980b6cee71dSXin LI */ 1981b6cee71dSXin LI if (*l == 'u') { 1982b6cee71dSXin LI /* 1983b6cee71dSXin LI * Try it as a keyword type prefixed by "u"; match what 1984b6cee71dSXin LI * follows the "u". If that fails, try it as an SUS 1985b6cee71dSXin LI * integer type. 1986b6cee71dSXin LI */ 1987b6cee71dSXin LI m->type = get_type(type_tbl, l + 1, &l); 1988b6cee71dSXin LI if (m->type == FILE_INVALID) { 1989b6cee71dSXin LI /* 1990b6cee71dSXin LI * Not a keyword type; parse it as an SUS type, 1991b6cee71dSXin LI * 'u' possibly followed by a number or C/S/L. 1992b6cee71dSXin LI */ 1993b6cee71dSXin LI m->type = get_standard_integer_type(l, &l); 1994b6cee71dSXin LI } 1995b6cee71dSXin LI /* It's unsigned. */ 1996b6cee71dSXin LI if (m->type != FILE_INVALID) 1997b6cee71dSXin LI m->flag |= UNSIGNED; 1998b6cee71dSXin LI } else { 1999b6cee71dSXin LI /* 2000b6cee71dSXin LI * Try it as a keyword type. If that fails, try it as 2001b6cee71dSXin LI * an SUS integer type if it begins with "d" or as an 2002b6cee71dSXin LI * SUS string type if it begins with "s". In any case, 2003b6cee71dSXin LI * it's not unsigned. 2004b6cee71dSXin LI */ 2005b6cee71dSXin LI m->type = get_type(type_tbl, l, &l); 2006b6cee71dSXin LI if (m->type == FILE_INVALID) { 2007b6cee71dSXin LI /* 2008b6cee71dSXin LI * Not a keyword type; parse it as an SUS type, 2009b6cee71dSXin LI * either 'd' possibly followed by a number or 2010b6cee71dSXin LI * C/S/L, or just 's'. 2011b6cee71dSXin LI */ 2012b6cee71dSXin LI if (*l == 'd') 2013b6cee71dSXin LI m->type = get_standard_integer_type(l, &l); 2014b6cee71dSXin LI else if (*l == 's' && !isalpha((unsigned char)l[1])) { 2015b6cee71dSXin LI m->type = FILE_STRING; 2016b6cee71dSXin LI ++l; 2017b6cee71dSXin LI } 2018b6cee71dSXin LI } 2019b6cee71dSXin LI } 2020b6cee71dSXin LI 2021b6cee71dSXin LI if (m->type == FILE_INVALID) { 2022b6cee71dSXin LI /* Not found - try it as a special keyword. */ 2023b6cee71dSXin LI m->type = get_type(special_tbl, l, &l); 2024b6cee71dSXin LI } 2025b6cee71dSXin LI 2026b6cee71dSXin LI if (m->type == FILE_INVALID) { 2027b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2028b6cee71dSXin LI file_magwarn(ms, "type `%s' invalid", l); 2029b6cee71dSXin LI return -1; 2030b6cee71dSXin LI } 2031b6cee71dSXin LI 2032b6cee71dSXin LI /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */ 2033b6cee71dSXin LI /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */ 2034b6cee71dSXin LI 2035b6cee71dSXin LI m->mask_op = 0; 2036b6cee71dSXin LI if (*l == '~') { 2037b6cee71dSXin LI if (!IS_STRING(m->type)) 2038b6cee71dSXin LI m->mask_op |= FILE_OPINVERSE; 2039b6cee71dSXin LI else if (ms->flags & MAGIC_CHECK) 2040b6cee71dSXin LI file_magwarn(ms, "'~' invalid for string types"); 2041b6cee71dSXin LI ++l; 2042b6cee71dSXin LI } 2043b6cee71dSXin LI m->str_range = 0; 2044b6cee71dSXin LI m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0; 2045b6cee71dSXin LI if ((op = get_op(*l)) != -1) { 20464460e5b0SXin LI if (IS_STRING(m->type)) { 20474460e5b0SXin LI int r; 20484460e5b0SXin LI 20494460e5b0SXin LI if (op != FILE_OPDIVIDE) { 2050b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2051b6cee71dSXin LI file_magwarn(ms, 20524460e5b0SXin LI "invalid string/indirect op: " 20534460e5b0SXin LI "`%c'", *t); 2054b6cee71dSXin LI return -1; 2055b6cee71dSXin LI } 20564460e5b0SXin LI 20574460e5b0SXin LI if (m->type == FILE_INDIRECT) 20584460e5b0SXin LI r = parse_indirect_modifier(ms, m, &l); 20594460e5b0SXin LI else 20604460e5b0SXin LI r = parse_string_modifier(ms, m, &l); 20614460e5b0SXin LI if (r == -1) 2062b6cee71dSXin LI return -1; 20634460e5b0SXin LI } else 20644460e5b0SXin LI parse_op_modifier(ms, m, &l, op); 2065b6cee71dSXin LI } 20664460e5b0SXin LI 2067b6cee71dSXin LI /* 2068b6cee71dSXin LI * We used to set mask to all 1's here, instead let's just not do 2069b6cee71dSXin LI * anything if mask = 0 (unless you have a better idea) 2070b6cee71dSXin LI */ 2071b6cee71dSXin LI EATAB; 2072b6cee71dSXin LI 2073b6cee71dSXin LI switch (*l) { 2074b6cee71dSXin LI case '>': 2075b6cee71dSXin LI case '<': 2076b6cee71dSXin LI m->reln = *l; 2077b6cee71dSXin LI ++l; 2078b6cee71dSXin LI if (*l == '=') { 2079b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) { 2080b6cee71dSXin LI file_magwarn(ms, "%c= not supported", 2081b6cee71dSXin LI m->reln); 2082b6cee71dSXin LI return -1; 2083b6cee71dSXin LI } 2084b6cee71dSXin LI ++l; 2085b6cee71dSXin LI } 2086b6cee71dSXin LI break; 2087b6cee71dSXin LI /* Old-style anding: "0 byte &0x80 dynamically linked" */ 2088b6cee71dSXin LI case '&': 2089b6cee71dSXin LI case '^': 2090b6cee71dSXin LI case '=': 2091b6cee71dSXin LI m->reln = *l; 2092b6cee71dSXin LI ++l; 2093b6cee71dSXin LI if (*l == '=') { 2094b6cee71dSXin LI /* HP compat: ignore &= etc. */ 2095b6cee71dSXin LI ++l; 2096b6cee71dSXin LI } 2097b6cee71dSXin LI break; 2098b6cee71dSXin LI case '!': 2099b6cee71dSXin LI m->reln = *l; 2100b6cee71dSXin LI ++l; 2101b6cee71dSXin LI break; 2102b6cee71dSXin LI default: 2103b6cee71dSXin LI m->reln = '='; /* the default relation */ 2104b6cee71dSXin LI if (*l == 'x' && ((isascii((unsigned char)l[1]) && 2105b6cee71dSXin LI isspace((unsigned char)l[1])) || !l[1])) { 2106b6cee71dSXin LI m->reln = *l; 2107b6cee71dSXin LI ++l; 2108b6cee71dSXin LI } 2109b6cee71dSXin LI break; 2110b6cee71dSXin LI } 2111b6cee71dSXin LI /* 2112b6cee71dSXin LI * Grab the value part, except for an 'x' reln. 2113b6cee71dSXin LI */ 2114b6cee71dSXin LI if (m->reln != 'x' && getvalue(ms, m, &l, action)) 2115b6cee71dSXin LI return -1; 2116b6cee71dSXin LI 2117b6cee71dSXin LI /* 2118b6cee71dSXin LI * TODO finish this macro and start using it! 2119*3e41d09dSXin LI * #define offsetcheck {if (offset > ms->bytes_max -1) 2120b6cee71dSXin LI * magwarn("offset too big"); } 2121b6cee71dSXin LI */ 2122b6cee71dSXin LI 2123b6cee71dSXin LI /* 2124b6cee71dSXin LI * Now get last part - the description 2125b6cee71dSXin LI */ 2126b6cee71dSXin LI EATAB; 2127b6cee71dSXin LI if (l[0] == '\b') { 2128b6cee71dSXin LI ++l; 2129b6cee71dSXin LI m->flag |= NOSPACE; 2130b6cee71dSXin LI } else if ((l[0] == '\\') && (l[1] == 'b')) { 2131b6cee71dSXin LI ++l; 2132b6cee71dSXin LI ++l; 2133b6cee71dSXin LI m->flag |= NOSPACE; 2134b6cee71dSXin LI } 2135b6cee71dSXin LI for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); ) 2136b6cee71dSXin LI continue; 2137b6cee71dSXin LI if (i == sizeof(m->desc)) { 2138b6cee71dSXin LI m->desc[sizeof(m->desc) - 1] = '\0'; 2139b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2140b6cee71dSXin LI file_magwarn(ms, "description `%s' truncated", m->desc); 2141b6cee71dSXin LI } 2142b6cee71dSXin LI 2143b6cee71dSXin LI /* 2144b6cee71dSXin LI * We only do this check while compiling, or if any of the magic 2145b6cee71dSXin LI * files were not compiled. 2146b6cee71dSXin LI */ 2147b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) { 2148b6cee71dSXin LI if (check_format(ms, m) == -1) 2149b6cee71dSXin LI return -1; 2150b6cee71dSXin LI } 2151b6cee71dSXin LI #ifndef COMPILE_ONLY 2152b6cee71dSXin LI if (action == FILE_CHECK) { 2153b6cee71dSXin LI file_mdump(m); 2154b6cee71dSXin LI } 2155b6cee71dSXin LI #endif 2156b6cee71dSXin LI m->mimetype[0] = '\0'; /* initialise MIME type to none */ 2157b6cee71dSXin LI return 0; 2158b6cee71dSXin LI } 2159b6cee71dSXin LI 2160b6cee71dSXin LI /* 2161b6cee71dSXin LI * parse a STRENGTH annotation line from magic file, put into magic[index - 1] 2162b6cee71dSXin LI * if valid 2163b6cee71dSXin LI */ 2164b6cee71dSXin LI private int 2165b6cee71dSXin LI parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line) 2166b6cee71dSXin LI { 2167b6cee71dSXin LI const char *l = line; 2168b6cee71dSXin LI char *el; 2169b6cee71dSXin LI unsigned long factor; 2170b6cee71dSXin LI struct magic *m = &me->mp[0]; 2171b6cee71dSXin LI 2172b6cee71dSXin LI if (m->factor_op != FILE_FACTOR_OP_NONE) { 2173b6cee71dSXin LI file_magwarn(ms, 2174b6cee71dSXin LI "Current entry already has a strength type: %c %d", 2175b6cee71dSXin LI m->factor_op, m->factor); 2176b6cee71dSXin LI return -1; 2177b6cee71dSXin LI } 2178b6cee71dSXin LI if (m->type == FILE_NAME) { 2179b6cee71dSXin LI file_magwarn(ms, "%s: Strength setting is not supported in " 2180b6cee71dSXin LI "\"name\" magic entries", m->value.s); 2181b6cee71dSXin LI return -1; 2182b6cee71dSXin LI } 2183b6cee71dSXin LI EATAB; 2184b6cee71dSXin LI switch (*l) { 2185b6cee71dSXin LI case FILE_FACTOR_OP_NONE: 2186b6cee71dSXin LI case FILE_FACTOR_OP_PLUS: 2187b6cee71dSXin LI case FILE_FACTOR_OP_MINUS: 2188b6cee71dSXin LI case FILE_FACTOR_OP_TIMES: 2189b6cee71dSXin LI case FILE_FACTOR_OP_DIV: 2190b6cee71dSXin LI m->factor_op = *l++; 2191b6cee71dSXin LI break; 2192b6cee71dSXin LI default: 2193b6cee71dSXin LI file_magwarn(ms, "Unknown factor op `%c'", *l); 2194b6cee71dSXin LI return -1; 2195b6cee71dSXin LI } 2196b6cee71dSXin LI EATAB; 2197b6cee71dSXin LI factor = strtoul(l, &el, 0); 2198b6cee71dSXin LI if (factor > 255) { 2199b6cee71dSXin LI file_magwarn(ms, "Too large factor `%lu'", factor); 2200b6cee71dSXin LI goto out; 2201b6cee71dSXin LI } 2202b6cee71dSXin LI if (*el && !isspace((unsigned char)*el)) { 2203b6cee71dSXin LI file_magwarn(ms, "Bad factor `%s'", l); 2204b6cee71dSXin LI goto out; 2205b6cee71dSXin LI } 2206b6cee71dSXin LI m->factor = (uint8_t)factor; 2207b6cee71dSXin LI if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) { 2208b6cee71dSXin LI file_magwarn(ms, "Cannot have factor op `%c' and factor %u", 2209b6cee71dSXin LI m->factor_op, m->factor); 2210b6cee71dSXin LI goto out; 2211b6cee71dSXin LI } 2212b6cee71dSXin LI return 0; 2213b6cee71dSXin LI out: 2214b6cee71dSXin LI m->factor_op = FILE_FACTOR_OP_NONE; 2215b6cee71dSXin LI m->factor = 0; 2216b6cee71dSXin LI return -1; 2217b6cee71dSXin LI } 2218b6cee71dSXin LI 2219b6cee71dSXin LI private int 2220c2931133SXin LI goodchar(unsigned char x, const char *extra) 2221c2931133SXin LI { 2222c2931133SXin LI return (isascii(x) && isalnum(x)) || strchr(extra, x); 2223c2931133SXin LI } 2224c2931133SXin LI 2225c2931133SXin LI private int 2226b6cee71dSXin LI parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line, 2227c2931133SXin LI off_t off, size_t len, const char *name, const char *extra, int nt) 2228b6cee71dSXin LI { 2229b6cee71dSXin LI size_t i; 2230b6cee71dSXin LI const char *l = line; 2231b6cee71dSXin LI struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1]; 22325f0216bdSXin LI char *buf = CAST(char *, CAST(void *, m)) + off; 2233b6cee71dSXin LI 2234b6cee71dSXin LI if (buf[0] != '\0') { 2235b6cee71dSXin LI len = nt ? strlen(buf) : len; 2236b6cee71dSXin LI file_magwarn(ms, "Current entry already has a %s type " 2237b6cee71dSXin LI "`%.*s', new type `%s'", name, (int)len, buf, l); 2238b6cee71dSXin LI return -1; 2239b6cee71dSXin LI } 2240b6cee71dSXin LI 2241b6cee71dSXin LI if (*m->desc == '\0') { 2242b6cee71dSXin LI file_magwarn(ms, "Current entry does not yet have a " 2243b6cee71dSXin LI "description for adding a %s type", name); 2244b6cee71dSXin LI return -1; 2245b6cee71dSXin LI } 2246b6cee71dSXin LI 2247b6cee71dSXin LI EATAB; 2248c2931133SXin LI for (i = 0; *l && i < len && goodchar(*l, extra); buf[i++] = *l++) 2249b6cee71dSXin LI continue; 2250b6cee71dSXin LI 2251b6cee71dSXin LI if (i == len && *l) { 2252b6cee71dSXin LI if (nt) 2253b6cee71dSXin LI buf[len - 1] = '\0'; 2254b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2255b6cee71dSXin LI file_magwarn(ms, "%s type `%s' truncated %" 2256b6cee71dSXin LI SIZE_T_FORMAT "u", name, line, i); 2257b6cee71dSXin LI } else { 2258c2931133SXin LI if (!isspace((unsigned char)*l) && !goodchar(*l, extra)) 2259c2931133SXin LI file_magwarn(ms, "%s type `%s' has bad char '%c'", 2260c2931133SXin LI name, line, *l); 2261b6cee71dSXin LI if (nt) 2262b6cee71dSXin LI buf[i] = '\0'; 2263b6cee71dSXin LI } 2264b6cee71dSXin LI 2265b6cee71dSXin LI if (i > 0) 2266b6cee71dSXin LI return 0; 2267c2931133SXin LI 2268c2931133SXin LI file_magerror(ms, "Bad magic entry '%s'", line); 2269b6cee71dSXin LI return -1; 2270b6cee71dSXin LI } 2271b6cee71dSXin LI 2272b6cee71dSXin LI /* 2273b6cee71dSXin LI * Parse an Apple CREATOR/TYPE annotation from magic file and put it into 2274b6cee71dSXin LI * magic[index - 1] 2275b6cee71dSXin LI */ 2276b6cee71dSXin LI private int 2277b6cee71dSXin LI parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line) 2278b6cee71dSXin LI { 2279b6cee71dSXin LI struct magic *m = &me->mp[0]; 2280b6cee71dSXin LI 22815f0216bdSXin LI return parse_extra(ms, me, line, 22825f0216bdSXin LI CAST(off_t, offsetof(struct magic, apple)), 2283*3e41d09dSXin LI sizeof(m->apple), "APPLE", "!+-./?", 0); 2284b6cee71dSXin LI } 2285b6cee71dSXin LI 2286b6cee71dSXin LI /* 22875f0216bdSXin LI * Parse a comma-separated list of extensions 22885f0216bdSXin LI */ 22895f0216bdSXin LI private int 22905f0216bdSXin LI parse_ext(struct magic_set *ms, struct magic_entry *me, const char *line) 22915f0216bdSXin LI { 22925f0216bdSXin LI struct magic *m = &me->mp[0]; 22935f0216bdSXin LI 22945f0216bdSXin LI return parse_extra(ms, me, line, 22955f0216bdSXin LI CAST(off_t, offsetof(struct magic, ext)), 22965f0216bdSXin LI sizeof(m->ext), "EXTENSION", ",!+-/", 0); 22975f0216bdSXin LI } 22985f0216bdSXin LI 22995f0216bdSXin LI /* 2300b6cee71dSXin LI * parse a MIME annotation line from magic file, put into magic[index - 1] 2301b6cee71dSXin LI * if valid 2302b6cee71dSXin LI */ 2303b6cee71dSXin LI private int 2304b6cee71dSXin LI parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line) 2305b6cee71dSXin LI { 2306b6cee71dSXin LI struct magic *m = &me->mp[0]; 2307b6cee71dSXin LI 23085f0216bdSXin LI return parse_extra(ms, me, line, 23095f0216bdSXin LI CAST(off_t, offsetof(struct magic, mimetype)), 2310c2931133SXin LI sizeof(m->mimetype), "MIME", "+-/.", 1); 2311b6cee71dSXin LI } 2312b6cee71dSXin LI 2313b6cee71dSXin LI private int 2314*3e41d09dSXin LI check_format_type(const char *ptr, int type, const char **estr) 2315b6cee71dSXin LI { 2316b6cee71dSXin LI int quad = 0, h; 2317*3e41d09dSXin LI size_t len, cnt; 2318b6cee71dSXin LI if (*ptr == '\0') { 2319b6cee71dSXin LI /* Missing format string; bad */ 2320*3e41d09dSXin LI *estr = "missing format spec"; 2321b6cee71dSXin LI return -1; 2322b6cee71dSXin LI } 2323b6cee71dSXin LI 2324b6cee71dSXin LI switch (file_formats[type]) { 2325b6cee71dSXin LI case FILE_FMT_QUAD: 2326b6cee71dSXin LI quad = 1; 2327b6cee71dSXin LI /*FALLTHROUGH*/ 2328b6cee71dSXin LI case FILE_FMT_NUM: 2329b6cee71dSXin LI if (quad == 0) { 2330b6cee71dSXin LI switch (type) { 2331b6cee71dSXin LI case FILE_BYTE: 2332b6cee71dSXin LI h = 2; 2333b6cee71dSXin LI break; 2334b6cee71dSXin LI case FILE_SHORT: 2335b6cee71dSXin LI case FILE_BESHORT: 2336b6cee71dSXin LI case FILE_LESHORT: 2337b6cee71dSXin LI h = 1; 2338b6cee71dSXin LI break; 2339b6cee71dSXin LI case FILE_LONG: 2340b6cee71dSXin LI case FILE_BELONG: 2341b6cee71dSXin LI case FILE_LELONG: 2342b6cee71dSXin LI case FILE_MELONG: 2343b6cee71dSXin LI case FILE_LEID3: 2344b6cee71dSXin LI case FILE_BEID3: 2345b6cee71dSXin LI case FILE_INDIRECT: 2346b6cee71dSXin LI h = 0; 2347b6cee71dSXin LI break; 2348b6cee71dSXin LI default: 2349b6cee71dSXin LI abort(); 2350b6cee71dSXin LI } 2351b6cee71dSXin LI } else 2352b6cee71dSXin LI h = 0; 2353b6cee71dSXin LI if (*ptr == '-') 2354b6cee71dSXin LI ptr++; 2355b6cee71dSXin LI if (*ptr == '.') 2356b6cee71dSXin LI ptr++; 2357*3e41d09dSXin LI #define CHECKLEN() do { \ 2358*3e41d09dSXin LI for (len = cnt = 0; isdigit((unsigned char)*ptr); ptr++, cnt++) \ 2359*3e41d09dSXin LI len = len * 10 + (*ptr - '0'); \ 2360*3e41d09dSXin LI if (cnt > 5 || len > 1024) \ 2361*3e41d09dSXin LI goto toolong; \ 2362*3e41d09dSXin LI } while (/*CONSTCOND*/0) 2363*3e41d09dSXin LI 2364*3e41d09dSXin LI CHECKLEN(); 2365b6cee71dSXin LI if (*ptr == '.') 2366b6cee71dSXin LI ptr++; 2367*3e41d09dSXin LI CHECKLEN(); 2368b6cee71dSXin LI if (quad) { 2369b6cee71dSXin LI if (*ptr++ != 'l') 2370*3e41d09dSXin LI goto invalid; 2371b6cee71dSXin LI if (*ptr++ != 'l') 2372*3e41d09dSXin LI goto invalid; 2373b6cee71dSXin LI } 2374b6cee71dSXin LI 2375b6cee71dSXin LI switch (*ptr++) { 2376b6cee71dSXin LI #ifdef STRICT_FORMAT /* "long" formats are int formats for us */ 2377b6cee71dSXin LI /* so don't accept the 'l' modifier */ 2378b6cee71dSXin LI case 'l': 2379b6cee71dSXin LI switch (*ptr++) { 2380b6cee71dSXin LI case 'i': 2381b6cee71dSXin LI case 'd': 2382b6cee71dSXin LI case 'u': 2383b6cee71dSXin LI case 'o': 2384b6cee71dSXin LI case 'x': 2385b6cee71dSXin LI case 'X': 2386*3e41d09dSXin LI if (h == 0) 2387*3e41d09dSXin LI return 0; 2388*3e41d09dSXin LI /*FALLTHROUGH*/ 2389b6cee71dSXin LI default: 2390*3e41d09dSXin LI goto invalid; 2391b6cee71dSXin LI } 2392b6cee71dSXin LI 2393b6cee71dSXin LI /* 2394b6cee71dSXin LI * Don't accept h and hh modifiers. They make writing 2395b6cee71dSXin LI * magic entries more complicated, for very little benefit 2396b6cee71dSXin LI */ 2397b6cee71dSXin LI case 'h': 2398b6cee71dSXin LI if (h-- <= 0) 2399*3e41d09dSXin LI goto invalid; 2400b6cee71dSXin LI switch (*ptr++) { 2401b6cee71dSXin LI case 'h': 2402b6cee71dSXin LI if (h-- <= 0) 2403*3e41d09dSXin LI goto invalid; 2404b6cee71dSXin LI switch (*ptr++) { 2405b6cee71dSXin LI case 'i': 2406b6cee71dSXin LI case 'd': 2407b6cee71dSXin LI case 'u': 2408b6cee71dSXin LI case 'o': 2409b6cee71dSXin LI case 'x': 2410b6cee71dSXin LI case 'X': 2411b6cee71dSXin LI return 0; 2412b6cee71dSXin LI default: 2413*3e41d09dSXin LI goto invalid; 2414b6cee71dSXin LI } 2415b6cee71dSXin LI case 'i': 2416b6cee71dSXin LI case 'd': 2417b6cee71dSXin LI case 'u': 2418b6cee71dSXin LI case 'o': 2419b6cee71dSXin LI case 'x': 2420b6cee71dSXin LI case 'X': 2421*3e41d09dSXin LI if (h == 0) 2422*3e41d09dSXin LI return 0; 2423*3e41d09dSXin LI /*FALLTHROUGH*/ 2424b6cee71dSXin LI default: 2425*3e41d09dSXin LI goto invalid; 2426b6cee71dSXin LI } 2427b6cee71dSXin LI #endif 2428b6cee71dSXin LI case 'c': 2429*3e41d09dSXin LI if (h == 2) 2430*3e41d09dSXin LI return 0; 2431*3e41d09dSXin LI goto invalid; 2432b6cee71dSXin LI case 'i': 2433b6cee71dSXin LI case 'd': 2434b6cee71dSXin LI case 'u': 2435b6cee71dSXin LI case 'o': 2436b6cee71dSXin LI case 'x': 2437b6cee71dSXin LI case 'X': 2438b6cee71dSXin LI #ifdef STRICT_FORMAT 2439*3e41d09dSXin LI if (h == 0) 2440*3e41d09dSXin LI return 0; 2441*3e41d09dSXin LI /*FALLTHROUGH*/ 2442b6cee71dSXin LI #else 2443b6cee71dSXin LI return 0; 2444b6cee71dSXin LI #endif 2445b6cee71dSXin LI default: 2446*3e41d09dSXin LI goto invalid; 2447b6cee71dSXin LI } 2448b6cee71dSXin LI 2449b6cee71dSXin LI case FILE_FMT_FLOAT: 2450b6cee71dSXin LI case FILE_FMT_DOUBLE: 2451b6cee71dSXin LI if (*ptr == '-') 2452b6cee71dSXin LI ptr++; 2453b6cee71dSXin LI if (*ptr == '.') 2454b6cee71dSXin LI ptr++; 2455*3e41d09dSXin LI CHECKLEN(); 2456b6cee71dSXin LI if (*ptr == '.') 2457b6cee71dSXin LI ptr++; 2458*3e41d09dSXin LI CHECKLEN(); 2459b6cee71dSXin LI switch (*ptr++) { 2460b6cee71dSXin LI case 'e': 2461b6cee71dSXin LI case 'E': 2462b6cee71dSXin LI case 'f': 2463b6cee71dSXin LI case 'F': 2464b6cee71dSXin LI case 'g': 2465b6cee71dSXin LI case 'G': 2466b6cee71dSXin LI return 0; 2467b6cee71dSXin LI 2468b6cee71dSXin LI default: 2469*3e41d09dSXin LI goto invalid; 2470b6cee71dSXin LI } 2471b6cee71dSXin LI 2472b6cee71dSXin LI 2473b6cee71dSXin LI case FILE_FMT_STR: 2474b6cee71dSXin LI if (*ptr == '-') 2475b6cee71dSXin LI ptr++; 2476b6cee71dSXin LI while (isdigit((unsigned char )*ptr)) 2477b6cee71dSXin LI ptr++; 2478b6cee71dSXin LI if (*ptr == '.') { 2479b6cee71dSXin LI ptr++; 2480b6cee71dSXin LI while (isdigit((unsigned char )*ptr)) 2481b6cee71dSXin LI ptr++; 2482b6cee71dSXin LI } 2483b6cee71dSXin LI 2484b6cee71dSXin LI switch (*ptr++) { 2485b6cee71dSXin LI case 's': 2486b6cee71dSXin LI return 0; 2487b6cee71dSXin LI default: 2488*3e41d09dSXin LI goto invalid; 2489b6cee71dSXin LI } 2490b6cee71dSXin LI 2491b6cee71dSXin LI default: 2492b6cee71dSXin LI /* internal error */ 2493b6cee71dSXin LI abort(); 2494b6cee71dSXin LI } 2495*3e41d09dSXin LI invalid: 2496*3e41d09dSXin LI *estr = "not valid"; 2497*3e41d09dSXin LI toolong: 2498*3e41d09dSXin LI *estr = "too long"; 2499b6cee71dSXin LI return -1; 2500b6cee71dSXin LI } 2501b6cee71dSXin LI 2502b6cee71dSXin LI /* 2503b6cee71dSXin LI * Check that the optional printf format in description matches 2504b6cee71dSXin LI * the type of the magic. 2505b6cee71dSXin LI */ 2506b6cee71dSXin LI private int 2507b6cee71dSXin LI check_format(struct magic_set *ms, struct magic *m) 2508b6cee71dSXin LI { 2509b6cee71dSXin LI char *ptr; 2510*3e41d09dSXin LI const char *estr; 2511b6cee71dSXin LI 2512b6cee71dSXin LI for (ptr = m->desc; *ptr; ptr++) 2513b6cee71dSXin LI if (*ptr == '%') 2514b6cee71dSXin LI break; 2515b6cee71dSXin LI if (*ptr == '\0') { 2516b6cee71dSXin LI /* No format string; ok */ 2517b6cee71dSXin LI return 1; 2518b6cee71dSXin LI } 2519b6cee71dSXin LI 2520b6cee71dSXin LI assert(file_nformats == file_nnames); 2521b6cee71dSXin LI 2522b6cee71dSXin LI if (m->type >= file_nformats) { 2523b6cee71dSXin LI file_magwarn(ms, "Internal error inconsistency between " 2524b6cee71dSXin LI "m->type and format strings"); 2525b6cee71dSXin LI return -1; 2526b6cee71dSXin LI } 2527b6cee71dSXin LI if (file_formats[m->type] == FILE_FMT_NONE) { 2528b6cee71dSXin LI file_magwarn(ms, "No format string for `%s' with description " 2529b6cee71dSXin LI "`%s'", m->desc, file_names[m->type]); 2530b6cee71dSXin LI return -1; 2531b6cee71dSXin LI } 2532b6cee71dSXin LI 2533b6cee71dSXin LI ptr++; 2534*3e41d09dSXin LI if (check_format_type(ptr, m->type, &estr) == -1) { 2535b6cee71dSXin LI /* 2536b6cee71dSXin LI * TODO: this error message is unhelpful if the format 2537b6cee71dSXin LI * string is not one character long 2538b6cee71dSXin LI */ 2539*3e41d09dSXin LI file_magwarn(ms, "Printf format is %s for type " 2540*3e41d09dSXin LI "`%s' in description `%s'", estr, 2541b6cee71dSXin LI file_names[m->type], m->desc); 2542b6cee71dSXin LI return -1; 2543b6cee71dSXin LI } 2544b6cee71dSXin LI 2545b6cee71dSXin LI for (; *ptr; ptr++) { 2546b6cee71dSXin LI if (*ptr == '%') { 2547b6cee71dSXin LI file_magwarn(ms, 2548b6cee71dSXin LI "Too many format strings (should have at most one) " 2549b6cee71dSXin LI "for `%s' with description `%s'", 2550b6cee71dSXin LI file_names[m->type], m->desc); 2551b6cee71dSXin LI return -1; 2552b6cee71dSXin LI } 2553b6cee71dSXin LI } 2554b6cee71dSXin LI return 0; 2555b6cee71dSXin LI } 2556b6cee71dSXin LI 2557b6cee71dSXin LI /* 2558b6cee71dSXin LI * Read a numeric value from a pointer, into the value union of a magic 2559b6cee71dSXin LI * pointer, according to the magic type. Update the string pointer to point 2560b6cee71dSXin LI * just after the number read. Return 0 for success, non-zero for failure. 2561b6cee71dSXin LI */ 2562b6cee71dSXin LI private int 2563b6cee71dSXin LI getvalue(struct magic_set *ms, struct magic *m, const char **p, int action) 2564b6cee71dSXin LI { 2565b6cee71dSXin LI switch (m->type) { 2566b6cee71dSXin LI case FILE_BESTRING16: 2567b6cee71dSXin LI case FILE_LESTRING16: 2568b6cee71dSXin LI case FILE_STRING: 2569b6cee71dSXin LI case FILE_PSTRING: 2570b6cee71dSXin LI case FILE_REGEX: 2571b6cee71dSXin LI case FILE_SEARCH: 2572b6cee71dSXin LI case FILE_NAME: 2573b6cee71dSXin LI case FILE_USE: 2574*3e41d09dSXin LI case FILE_DER: 2575b6cee71dSXin LI *p = getstr(ms, m, *p, action == FILE_COMPILE); 2576b6cee71dSXin LI if (*p == NULL) { 2577b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2578b6cee71dSXin LI file_magwarn(ms, "cannot get string from `%s'", 2579b6cee71dSXin LI m->value.s); 2580b6cee71dSXin LI return -1; 2581b6cee71dSXin LI } 2582b6cee71dSXin LI if (m->type == FILE_REGEX) { 2583b6cee71dSXin LI file_regex_t rx; 2584b6cee71dSXin LI int rc = file_regcomp(&rx, m->value.s, REG_EXTENDED); 2585b6cee71dSXin LI if (rc) { 2586b6cee71dSXin LI if (ms->flags & MAGIC_CHECK) 2587b6cee71dSXin LI file_regerror(&rx, rc, ms); 2588b6cee71dSXin LI } 2589b6cee71dSXin LI file_regfree(&rx); 2590b6cee71dSXin LI return rc ? -1 : 0; 2591b6cee71dSXin LI } 2592b6cee71dSXin LI return 0; 2593b6cee71dSXin LI case FILE_FLOAT: 2594b6cee71dSXin LI case FILE_BEFLOAT: 2595b6cee71dSXin LI case FILE_LEFLOAT: 2596b6cee71dSXin LI if (m->reln != 'x') { 2597b6cee71dSXin LI char *ep; 25989ce06829SXin LI errno = 0; 2599b6cee71dSXin LI #ifdef HAVE_STRTOF 2600b6cee71dSXin LI m->value.f = strtof(*p, &ep); 2601b6cee71dSXin LI #else 2602b6cee71dSXin LI m->value.f = (float)strtod(*p, &ep); 2603b6cee71dSXin LI #endif 26049ce06829SXin LI if (errno == 0) 2605b6cee71dSXin LI *p = ep; 2606b6cee71dSXin LI } 2607b6cee71dSXin LI return 0; 2608b6cee71dSXin LI case FILE_DOUBLE: 2609b6cee71dSXin LI case FILE_BEDOUBLE: 2610b6cee71dSXin LI case FILE_LEDOUBLE: 2611b6cee71dSXin LI if (m->reln != 'x') { 2612b6cee71dSXin LI char *ep; 26139ce06829SXin LI errno = 0; 2614b6cee71dSXin LI m->value.d = strtod(*p, &ep); 26159ce06829SXin LI if (errno == 0) 2616b6cee71dSXin LI *p = ep; 2617b6cee71dSXin LI } 2618b6cee71dSXin LI return 0; 2619b6cee71dSXin LI default: 2620b6cee71dSXin LI if (m->reln != 'x') { 2621b6cee71dSXin LI char *ep; 26229ce06829SXin LI errno = 0; 2623b6cee71dSXin LI m->value.q = file_signextend(ms, m, 2624b6cee71dSXin LI (uint64_t)strtoull(*p, &ep, 0)); 26259ce06829SXin LI if (errno == 0) { 2626b6cee71dSXin LI *p = ep; 2627b6cee71dSXin LI eatsize(p); 2628b6cee71dSXin LI } 26299ce06829SXin LI } 2630b6cee71dSXin LI return 0; 2631b6cee71dSXin LI } 2632b6cee71dSXin LI } 2633b6cee71dSXin LI 2634b6cee71dSXin LI /* 2635b6cee71dSXin LI * Convert a string containing C character escapes. Stop at an unescaped 2636b6cee71dSXin LI * space or tab. 2637b6cee71dSXin LI * Copy the converted version to "m->value.s", and the length in m->vallen. 2638b6cee71dSXin LI * Return updated scan pointer as function result. Warn if set. 2639b6cee71dSXin LI */ 2640b6cee71dSXin LI private const char * 2641b6cee71dSXin LI getstr(struct magic_set *ms, struct magic *m, const char *s, int warn) 2642b6cee71dSXin LI { 2643b6cee71dSXin LI const char *origs = s; 2644b6cee71dSXin LI char *p = m->value.s; 2645b6cee71dSXin LI size_t plen = sizeof(m->value.s); 2646b6cee71dSXin LI char *origp = p; 2647b6cee71dSXin LI char *pmax = p + plen - 1; 2648b6cee71dSXin LI int c; 2649b6cee71dSXin LI int val; 2650b6cee71dSXin LI 2651b6cee71dSXin LI while ((c = *s++) != '\0') { 2652b6cee71dSXin LI if (isspace((unsigned char) c)) 2653b6cee71dSXin LI break; 2654b6cee71dSXin LI if (p >= pmax) { 2655b6cee71dSXin LI file_error(ms, 0, "string too long: `%s'", origs); 2656b6cee71dSXin LI return NULL; 2657b6cee71dSXin LI } 2658b6cee71dSXin LI if (c == '\\') { 2659b6cee71dSXin LI switch(c = *s++) { 2660b6cee71dSXin LI 2661b6cee71dSXin LI case '\0': 2662b6cee71dSXin LI if (warn) 2663b6cee71dSXin LI file_magwarn(ms, "incomplete escape"); 26649ce06829SXin LI s--; 2665b6cee71dSXin LI goto out; 2666b6cee71dSXin LI 2667b6cee71dSXin LI case '\t': 2668b6cee71dSXin LI if (warn) { 2669b6cee71dSXin LI file_magwarn(ms, 2670b6cee71dSXin LI "escaped tab found, use \\t instead"); 2671b6cee71dSXin LI warn = 0; /* already did */ 2672b6cee71dSXin LI } 2673b6cee71dSXin LI /*FALLTHROUGH*/ 2674b6cee71dSXin LI default: 2675b6cee71dSXin LI if (warn) { 2676b6cee71dSXin LI if (isprint((unsigned char)c)) { 2677b6cee71dSXin LI /* Allow escaping of 2678b6cee71dSXin LI * ``relations'' */ 2679b6cee71dSXin LI if (strchr("<>&^=!", c) == NULL 2680b6cee71dSXin LI && (m->type != FILE_REGEX || 2681b6cee71dSXin LI strchr("[]().*?^$|{}", c) 2682b6cee71dSXin LI == NULL)) { 2683b6cee71dSXin LI file_magwarn(ms, "no " 2684b6cee71dSXin LI "need to escape " 2685b6cee71dSXin LI "`%c'", c); 2686b6cee71dSXin LI } 2687b6cee71dSXin LI } else { 2688b6cee71dSXin LI file_magwarn(ms, 2689b6cee71dSXin LI "unknown escape sequence: " 2690b6cee71dSXin LI "\\%03o", c); 2691b6cee71dSXin LI } 2692b6cee71dSXin LI } 2693b6cee71dSXin LI /*FALLTHROUGH*/ 2694b6cee71dSXin LI /* space, perhaps force people to use \040? */ 2695b6cee71dSXin LI case ' ': 2696b6cee71dSXin LI #if 0 2697b6cee71dSXin LI /* 2698b6cee71dSXin LI * Other things people escape, but shouldn't need to, 2699b6cee71dSXin LI * so we disallow them 2700b6cee71dSXin LI */ 2701b6cee71dSXin LI case '\'': 2702b6cee71dSXin LI case '"': 2703b6cee71dSXin LI case '?': 2704b6cee71dSXin LI #endif 2705b6cee71dSXin LI /* Relations */ 2706b6cee71dSXin LI case '>': 2707b6cee71dSXin LI case '<': 2708b6cee71dSXin LI case '&': 2709b6cee71dSXin LI case '^': 2710b6cee71dSXin LI case '=': 2711b6cee71dSXin LI case '!': 2712b6cee71dSXin LI /* and baskslash itself */ 2713b6cee71dSXin LI case '\\': 2714b6cee71dSXin LI *p++ = (char) c; 2715b6cee71dSXin LI break; 2716b6cee71dSXin LI 2717b6cee71dSXin LI case 'a': 2718b6cee71dSXin LI *p++ = '\a'; 2719b6cee71dSXin LI break; 2720b6cee71dSXin LI 2721b6cee71dSXin LI case 'b': 2722b6cee71dSXin LI *p++ = '\b'; 2723b6cee71dSXin LI break; 2724b6cee71dSXin LI 2725b6cee71dSXin LI case 'f': 2726b6cee71dSXin LI *p++ = '\f'; 2727b6cee71dSXin LI break; 2728b6cee71dSXin LI 2729b6cee71dSXin LI case 'n': 2730b6cee71dSXin LI *p++ = '\n'; 2731b6cee71dSXin LI break; 2732b6cee71dSXin LI 2733b6cee71dSXin LI case 'r': 2734b6cee71dSXin LI *p++ = '\r'; 2735b6cee71dSXin LI break; 2736b6cee71dSXin LI 2737b6cee71dSXin LI case 't': 2738b6cee71dSXin LI *p++ = '\t'; 2739b6cee71dSXin LI break; 2740b6cee71dSXin LI 2741b6cee71dSXin LI case 'v': 2742b6cee71dSXin LI *p++ = '\v'; 2743b6cee71dSXin LI break; 2744b6cee71dSXin LI 2745b6cee71dSXin LI /* \ and up to 3 octal digits */ 2746b6cee71dSXin LI case '0': 2747b6cee71dSXin LI case '1': 2748b6cee71dSXin LI case '2': 2749b6cee71dSXin LI case '3': 2750b6cee71dSXin LI case '4': 2751b6cee71dSXin LI case '5': 2752b6cee71dSXin LI case '6': 2753b6cee71dSXin LI case '7': 2754b6cee71dSXin LI val = c - '0'; 2755b6cee71dSXin LI c = *s++; /* try for 2 */ 2756b6cee71dSXin LI if (c >= '0' && c <= '7') { 2757b6cee71dSXin LI val = (val << 3) | (c - '0'); 2758b6cee71dSXin LI c = *s++; /* try for 3 */ 2759b6cee71dSXin LI if (c >= '0' && c <= '7') 2760b6cee71dSXin LI val = (val << 3) | (c-'0'); 2761b6cee71dSXin LI else 2762b6cee71dSXin LI --s; 2763b6cee71dSXin LI } 2764b6cee71dSXin LI else 2765b6cee71dSXin LI --s; 2766b6cee71dSXin LI *p++ = (char)val; 2767b6cee71dSXin LI break; 2768b6cee71dSXin LI 2769b6cee71dSXin LI /* \x and up to 2 hex digits */ 2770b6cee71dSXin LI case 'x': 2771b6cee71dSXin LI val = 'x'; /* Default if no digits */ 2772b6cee71dSXin LI c = hextoint(*s++); /* Get next char */ 2773b6cee71dSXin LI if (c >= 0) { 2774b6cee71dSXin LI val = c; 2775b6cee71dSXin LI c = hextoint(*s++); 2776b6cee71dSXin LI if (c >= 0) 2777b6cee71dSXin LI val = (val << 4) + c; 2778b6cee71dSXin LI else 2779b6cee71dSXin LI --s; 2780b6cee71dSXin LI } else 2781b6cee71dSXin LI --s; 2782b6cee71dSXin LI *p++ = (char)val; 2783b6cee71dSXin LI break; 2784b6cee71dSXin LI } 2785b6cee71dSXin LI } else 2786b6cee71dSXin LI *p++ = (char)c; 2787b6cee71dSXin LI } 27889ce06829SXin LI --s; 2789b6cee71dSXin LI out: 2790b6cee71dSXin LI *p = '\0'; 2791b6cee71dSXin LI m->vallen = CAST(unsigned char, (p - origp)); 2792b6cee71dSXin LI if (m->type == FILE_PSTRING) 2793b6cee71dSXin LI m->vallen += (unsigned char)file_pstring_length_size(m); 2794b6cee71dSXin LI return s; 2795b6cee71dSXin LI } 2796b6cee71dSXin LI 2797b6cee71dSXin LI 2798b6cee71dSXin LI /* Single hex char to int; -1 if not a hex char. */ 2799b6cee71dSXin LI private int 2800b6cee71dSXin LI hextoint(int c) 2801b6cee71dSXin LI { 2802b6cee71dSXin LI if (!isascii((unsigned char) c)) 2803b6cee71dSXin LI return -1; 2804b6cee71dSXin LI if (isdigit((unsigned char) c)) 2805b6cee71dSXin LI return c - '0'; 2806b6cee71dSXin LI if ((c >= 'a') && (c <= 'f')) 2807b6cee71dSXin LI return c + 10 - 'a'; 2808b6cee71dSXin LI if (( c>= 'A') && (c <= 'F')) 2809b6cee71dSXin LI return c + 10 - 'A'; 2810b6cee71dSXin LI return -1; 2811b6cee71dSXin LI } 2812b6cee71dSXin LI 2813b6cee71dSXin LI 2814b6cee71dSXin LI /* 2815b6cee71dSXin LI * Print a string containing C character escapes. 2816b6cee71dSXin LI */ 2817b6cee71dSXin LI protected void 2818b6cee71dSXin LI file_showstr(FILE *fp, const char *s, size_t len) 2819b6cee71dSXin LI { 2820b6cee71dSXin LI char c; 2821b6cee71dSXin LI 2822b6cee71dSXin LI for (;;) { 2823b6cee71dSXin LI if (len == ~0U) { 2824b6cee71dSXin LI c = *s++; 2825b6cee71dSXin LI if (c == '\0') 2826b6cee71dSXin LI break; 2827b6cee71dSXin LI } 2828b6cee71dSXin LI else { 2829b6cee71dSXin LI if (len-- == 0) 2830b6cee71dSXin LI break; 2831b6cee71dSXin LI c = *s++; 2832b6cee71dSXin LI } 2833b6cee71dSXin LI if (c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */ 2834b6cee71dSXin LI (void) fputc(c, fp); 2835b6cee71dSXin LI else { 2836b6cee71dSXin LI (void) fputc('\\', fp); 2837b6cee71dSXin LI switch (c) { 2838b6cee71dSXin LI case '\a': 2839b6cee71dSXin LI (void) fputc('a', fp); 2840b6cee71dSXin LI break; 2841b6cee71dSXin LI 2842b6cee71dSXin LI case '\b': 2843b6cee71dSXin LI (void) fputc('b', fp); 2844b6cee71dSXin LI break; 2845b6cee71dSXin LI 2846b6cee71dSXin LI case '\f': 2847b6cee71dSXin LI (void) fputc('f', fp); 2848b6cee71dSXin LI break; 2849b6cee71dSXin LI 2850b6cee71dSXin LI case '\n': 2851b6cee71dSXin LI (void) fputc('n', fp); 2852b6cee71dSXin LI break; 2853b6cee71dSXin LI 2854b6cee71dSXin LI case '\r': 2855b6cee71dSXin LI (void) fputc('r', fp); 2856b6cee71dSXin LI break; 2857b6cee71dSXin LI 2858b6cee71dSXin LI case '\t': 2859b6cee71dSXin LI (void) fputc('t', fp); 2860b6cee71dSXin LI break; 2861b6cee71dSXin LI 2862b6cee71dSXin LI case '\v': 2863b6cee71dSXin LI (void) fputc('v', fp); 2864b6cee71dSXin LI break; 2865b6cee71dSXin LI 2866b6cee71dSXin LI default: 2867b6cee71dSXin LI (void) fprintf(fp, "%.3o", c & 0377); 2868b6cee71dSXin LI break; 2869b6cee71dSXin LI } 2870b6cee71dSXin LI } 2871b6cee71dSXin LI } 2872b6cee71dSXin LI } 2873b6cee71dSXin LI 2874b6cee71dSXin LI /* 2875b6cee71dSXin LI * eatsize(): Eat the size spec from a number [eg. 10UL] 2876b6cee71dSXin LI */ 2877b6cee71dSXin LI private void 2878b6cee71dSXin LI eatsize(const char **p) 2879b6cee71dSXin LI { 2880b6cee71dSXin LI const char *l = *p; 2881b6cee71dSXin LI 2882b6cee71dSXin LI if (LOWCASE(*l) == 'u') 2883b6cee71dSXin LI l++; 2884b6cee71dSXin LI 2885b6cee71dSXin LI switch (LOWCASE(*l)) { 2886b6cee71dSXin LI case 'l': /* long */ 2887b6cee71dSXin LI case 's': /* short */ 2888b6cee71dSXin LI case 'h': /* short */ 2889b6cee71dSXin LI case 'b': /* char/byte */ 2890b6cee71dSXin LI case 'c': /* char/byte */ 2891b6cee71dSXin LI l++; 2892b6cee71dSXin LI /*FALLTHROUGH*/ 2893b6cee71dSXin LI default: 2894b6cee71dSXin LI break; 2895b6cee71dSXin LI } 2896b6cee71dSXin LI 2897b6cee71dSXin LI *p = l; 2898b6cee71dSXin LI } 2899b6cee71dSXin LI 2900b6cee71dSXin LI /* 2901c2931133SXin LI * handle a buffer containing a compiled file. 2902c2931133SXin LI */ 2903c2931133SXin LI private struct magic_map * 2904c2931133SXin LI apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len) 2905c2931133SXin LI { 2906c2931133SXin LI struct magic_map *map; 2907c2931133SXin LI 2908c2931133SXin LI if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) { 2909c2931133SXin LI file_oomem(ms, sizeof(*map)); 2910c2931133SXin LI return NULL; 2911c2931133SXin LI } 2912c2931133SXin LI map->len = len; 2913c2931133SXin LI map->p = buf; 2914c2931133SXin LI map->type = MAP_TYPE_USER; 2915c2931133SXin LI if (check_buffer(ms, map, "buffer") != 0) { 2916c2931133SXin LI apprentice_unmap(map); 2917c2931133SXin LI return NULL; 2918c2931133SXin LI } 2919c2931133SXin LI return map; 2920c2931133SXin LI } 2921c2931133SXin LI 2922c2931133SXin LI /* 2923b6cee71dSXin LI * handle a compiled file. 2924b6cee71dSXin LI */ 2925b6cee71dSXin LI 2926b6cee71dSXin LI private struct magic_map * 2927b6cee71dSXin LI apprentice_map(struct magic_set *ms, const char *fn) 2928b6cee71dSXin LI { 2929b6cee71dSXin LI int fd; 2930b6cee71dSXin LI struct stat st; 2931b6cee71dSXin LI char *dbname = NULL; 2932b6cee71dSXin LI struct magic_map *map; 2933b6cee71dSXin LI 2934b6cee71dSXin LI fd = -1; 2935b6cee71dSXin LI if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) { 2936b6cee71dSXin LI file_oomem(ms, sizeof(*map)); 2937b6cee71dSXin LI goto error; 2938b6cee71dSXin LI } 2939*3e41d09dSXin LI map->type = MAP_TYPE_USER; /* unspecified */ 2940b6cee71dSXin LI 2941b6cee71dSXin LI dbname = mkdbname(ms, fn, 0); 2942b6cee71dSXin LI if (dbname == NULL) 2943b6cee71dSXin LI goto error; 2944b6cee71dSXin LI 2945b6cee71dSXin LI if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1) 2946b6cee71dSXin LI goto error; 2947b6cee71dSXin LI 2948b6cee71dSXin LI if (fstat(fd, &st) == -1) { 2949b6cee71dSXin LI file_error(ms, errno, "cannot stat `%s'", dbname); 2950b6cee71dSXin LI goto error; 2951b6cee71dSXin LI } 2952b6cee71dSXin LI if (st.st_size < 8 || st.st_size > MAXMAGIC_SIZE) { 2953b6cee71dSXin LI file_error(ms, 0, "file `%s' is too %s", dbname, 2954b6cee71dSXin LI st.st_size < 8 ? "small" : "large"); 2955b6cee71dSXin LI goto error; 2956b6cee71dSXin LI } 2957b6cee71dSXin LI 2958b6cee71dSXin LI map->len = (size_t)st.st_size; 2959b6cee71dSXin LI #ifdef QUICK 2960*3e41d09dSXin LI map->type = MAP_TYPE_MMAP; 2961b6cee71dSXin LI if ((map->p = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE, 2962b6cee71dSXin LI MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) { 2963b6cee71dSXin LI file_error(ms, errno, "cannot map `%s'", dbname); 2964b6cee71dSXin LI goto error; 2965b6cee71dSXin LI } 2966b6cee71dSXin LI #else 2967*3e41d09dSXin LI map->type = MAP_TYPE_MALLOC; 2968b6cee71dSXin LI if ((map->p = CAST(void *, malloc(map->len))) == NULL) { 2969b6cee71dSXin LI file_oomem(ms, map->len); 2970b6cee71dSXin LI goto error; 2971b6cee71dSXin LI } 2972b6cee71dSXin LI if (read(fd, map->p, map->len) != (ssize_t)map->len) { 2973b6cee71dSXin LI file_badread(ms); 2974b6cee71dSXin LI goto error; 2975b6cee71dSXin LI } 2976b6cee71dSXin LI #define RET 1 2977b6cee71dSXin LI #endif 2978b6cee71dSXin LI (void)close(fd); 2979b6cee71dSXin LI fd = -1; 2980c2931133SXin LI 2981c2931133SXin LI if (check_buffer(ms, map, dbname) != 0) 2982c2931133SXin LI goto error; 2983*3e41d09dSXin LI #ifdef QUICK 2984*3e41d09dSXin LI if (mprotect(map->p, (size_t)st.st_size, PROT_READ) == -1) { 2985*3e41d09dSXin LI file_error(ms, errno, "cannot mprotect `%s'", dbname); 2986*3e41d09dSXin LI goto error; 2987*3e41d09dSXin LI } 2988*3e41d09dSXin LI #endif 2989c2931133SXin LI 2990c2931133SXin LI free(dbname); 2991c2931133SXin LI return map; 2992c2931133SXin LI 2993c2931133SXin LI error: 2994c2931133SXin LI if (fd != -1) 2995c2931133SXin LI (void)close(fd); 2996c2931133SXin LI apprentice_unmap(map); 2997c2931133SXin LI free(dbname); 2998c2931133SXin LI return NULL; 2999c2931133SXin LI } 3000c2931133SXin LI 3001c2931133SXin LI private int 3002c2931133SXin LI check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname) 3003c2931133SXin LI { 3004c2931133SXin LI uint32_t *ptr; 3005c2931133SXin LI uint32_t entries, nentries; 3006c2931133SXin LI uint32_t version; 3007c2931133SXin LI int i, needsbyteswap; 3008c2931133SXin LI 3009b6cee71dSXin LI ptr = CAST(uint32_t *, map->p); 3010b6cee71dSXin LI if (*ptr != MAGICNO) { 3011b6cee71dSXin LI if (swap4(*ptr) != MAGICNO) { 3012b6cee71dSXin LI file_error(ms, 0, "bad magic in `%s'", dbname); 3013c2931133SXin LI return -1; 3014b6cee71dSXin LI } 3015b6cee71dSXin LI needsbyteswap = 1; 3016b6cee71dSXin LI } else 3017b6cee71dSXin LI needsbyteswap = 0; 3018b6cee71dSXin LI if (needsbyteswap) 3019b6cee71dSXin LI version = swap4(ptr[1]); 3020b6cee71dSXin LI else 3021b6cee71dSXin LI version = ptr[1]; 3022b6cee71dSXin LI if (version != VERSIONNO) { 3023b6cee71dSXin LI file_error(ms, 0, "File %s supports only version %d magic " 3024b6cee71dSXin LI "files. `%s' is version %d", VERSION, 3025b6cee71dSXin LI VERSIONNO, dbname, version); 3026c2931133SXin LI return -1; 3027b6cee71dSXin LI } 3028c2931133SXin LI entries = (uint32_t)(map->len / sizeof(struct magic)); 3029c2931133SXin LI if ((entries * sizeof(struct magic)) != map->len) { 3030c2931133SXin LI file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not " 3031b6cee71dSXin LI "a multiple of %" SIZE_T_FORMAT "u", 3032c2931133SXin LI dbname, map->len, sizeof(struct magic)); 3033c2931133SXin LI return -1; 3034b6cee71dSXin LI } 3035b6cee71dSXin LI map->magic[0] = CAST(struct magic *, map->p) + 1; 3036b6cee71dSXin LI nentries = 0; 3037b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 3038b6cee71dSXin LI if (needsbyteswap) 3039b6cee71dSXin LI map->nmagic[i] = swap4(ptr[i + 2]); 3040b6cee71dSXin LI else 3041b6cee71dSXin LI map->nmagic[i] = ptr[i + 2]; 3042b6cee71dSXin LI if (i != MAGIC_SETS - 1) 3043b6cee71dSXin LI map->magic[i + 1] = map->magic[i] + map->nmagic[i]; 3044b6cee71dSXin LI nentries += map->nmagic[i]; 3045b6cee71dSXin LI } 3046b6cee71dSXin LI if (entries != nentries + 1) { 3047b6cee71dSXin LI file_error(ms, 0, "Inconsistent entries in `%s' %u != %u", 3048b6cee71dSXin LI dbname, entries, nentries + 1); 3049c2931133SXin LI return -1; 3050b6cee71dSXin LI } 3051b6cee71dSXin LI if (needsbyteswap) 3052b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) 3053b6cee71dSXin LI byteswap(map->magic[i], map->nmagic[i]); 3054c2931133SXin LI return 0; 3055b6cee71dSXin LI } 3056b6cee71dSXin LI 3057b6cee71dSXin LI /* 3058b6cee71dSXin LI * handle an mmaped file. 3059b6cee71dSXin LI */ 3060b6cee71dSXin LI private int 3061b6cee71dSXin LI apprentice_compile(struct magic_set *ms, struct magic_map *map, const char *fn) 3062b6cee71dSXin LI { 3063b6cee71dSXin LI static const size_t nm = sizeof(*map->nmagic) * MAGIC_SETS; 3064b6cee71dSXin LI static const size_t m = sizeof(**map->magic); 3065b6cee71dSXin LI int fd = -1; 3066b6cee71dSXin LI size_t len; 3067b6cee71dSXin LI char *dbname; 3068b6cee71dSXin LI int rv = -1; 3069b6cee71dSXin LI uint32_t i; 3070b6cee71dSXin LI union { 3071b6cee71dSXin LI struct magic m; 3072b6cee71dSXin LI uint32_t h[2 + MAGIC_SETS]; 3073b6cee71dSXin LI } hdr; 3074b6cee71dSXin LI 3075b6cee71dSXin LI dbname = mkdbname(ms, fn, 1); 3076b6cee71dSXin LI 3077b6cee71dSXin LI if (dbname == NULL) 3078b6cee71dSXin LI goto out; 3079b6cee71dSXin LI 3080b6cee71dSXin LI if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) 3081b6cee71dSXin LI { 3082b6cee71dSXin LI file_error(ms, errno, "cannot open `%s'", dbname); 3083b6cee71dSXin LI goto out; 3084b6cee71dSXin LI } 3085b6cee71dSXin LI memset(&hdr, 0, sizeof(hdr)); 3086b6cee71dSXin LI hdr.h[0] = MAGICNO; 3087b6cee71dSXin LI hdr.h[1] = VERSIONNO; 3088b6cee71dSXin LI memcpy(hdr.h + 2, map->nmagic, nm); 3089b6cee71dSXin LI 3090b6cee71dSXin LI if (write(fd, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) { 3091b6cee71dSXin LI file_error(ms, errno, "error writing `%s'", dbname); 3092b6cee71dSXin LI goto out; 3093b6cee71dSXin LI } 3094b6cee71dSXin LI 3095b6cee71dSXin LI for (i = 0; i < MAGIC_SETS; i++) { 3096b6cee71dSXin LI len = m * map->nmagic[i]; 3097b6cee71dSXin LI if (write(fd, map->magic[i], len) != (ssize_t)len) { 3098b6cee71dSXin LI file_error(ms, errno, "error writing `%s'", dbname); 3099b6cee71dSXin LI goto out; 3100b6cee71dSXin LI } 3101b6cee71dSXin LI } 3102b6cee71dSXin LI 3103b6cee71dSXin LI if (fd != -1) 3104b6cee71dSXin LI (void)close(fd); 3105b6cee71dSXin LI rv = 0; 3106b6cee71dSXin LI out: 3107b6cee71dSXin LI free(dbname); 3108b6cee71dSXin LI return rv; 3109b6cee71dSXin LI } 3110b6cee71dSXin LI 3111b6cee71dSXin LI private const char ext[] = ".mgc"; 3112b6cee71dSXin LI /* 3113b6cee71dSXin LI * make a dbname 3114b6cee71dSXin LI */ 3115b6cee71dSXin LI private char * 3116b6cee71dSXin LI mkdbname(struct magic_set *ms, const char *fn, int strip) 3117b6cee71dSXin LI { 3118b6cee71dSXin LI const char *p, *q; 3119b6cee71dSXin LI char *buf; 3120b6cee71dSXin LI 3121b6cee71dSXin LI if (strip) { 3122b6cee71dSXin LI if ((p = strrchr(fn, '/')) != NULL) 3123b6cee71dSXin LI fn = ++p; 3124b6cee71dSXin LI } 3125b6cee71dSXin LI 3126b6cee71dSXin LI for (q = fn; *q; q++) 3127b6cee71dSXin LI continue; 3128b6cee71dSXin LI /* Look for .mgc */ 3129b6cee71dSXin LI for (p = ext + sizeof(ext) - 1; p >= ext && q >= fn; p--, q--) 3130b6cee71dSXin LI if (*p != *q) 3131b6cee71dSXin LI break; 3132b6cee71dSXin LI 3133b6cee71dSXin LI /* Did not find .mgc, restore q */ 3134b6cee71dSXin LI if (p >= ext) 3135b6cee71dSXin LI while (*q) 3136b6cee71dSXin LI q++; 3137b6cee71dSXin LI 3138b6cee71dSXin LI q++; 3139b6cee71dSXin LI /* Compatibility with old code that looked in .mime */ 3140b6cee71dSXin LI if (ms->flags & MAGIC_MIME) { 3141b6cee71dSXin LI if (asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext) < 0) 3142b6cee71dSXin LI return NULL; 3143b6cee71dSXin LI if (access(buf, R_OK) != -1) { 3144b6cee71dSXin LI ms->flags &= MAGIC_MIME_TYPE; 3145b6cee71dSXin LI return buf; 3146b6cee71dSXin LI } 3147b6cee71dSXin LI free(buf); 3148b6cee71dSXin LI } 3149b6cee71dSXin LI if (asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext) < 0) 3150b6cee71dSXin LI return NULL; 3151b6cee71dSXin LI 3152b6cee71dSXin LI /* Compatibility with old code that looked in .mime */ 3153b6cee71dSXin LI if (strstr(p, ".mime") != NULL) 3154b6cee71dSXin LI ms->flags &= MAGIC_MIME_TYPE; 3155b6cee71dSXin LI return buf; 3156b6cee71dSXin LI } 3157b6cee71dSXin LI 3158b6cee71dSXin LI /* 3159b6cee71dSXin LI * Byteswap an mmap'ed file if needed 3160b6cee71dSXin LI */ 3161b6cee71dSXin LI private void 3162b6cee71dSXin LI byteswap(struct magic *magic, uint32_t nmagic) 3163b6cee71dSXin LI { 3164b6cee71dSXin LI uint32_t i; 3165b6cee71dSXin LI for (i = 0; i < nmagic; i++) 3166b6cee71dSXin LI bs1(&magic[i]); 3167b6cee71dSXin LI } 3168b6cee71dSXin LI 3169b6cee71dSXin LI /* 3170b6cee71dSXin LI * swap a short 3171b6cee71dSXin LI */ 3172b6cee71dSXin LI private uint16_t 3173b6cee71dSXin LI swap2(uint16_t sv) 3174b6cee71dSXin LI { 3175b6cee71dSXin LI uint16_t rv; 3176b6cee71dSXin LI uint8_t *s = (uint8_t *)(void *)&sv; 3177b6cee71dSXin LI uint8_t *d = (uint8_t *)(void *)&rv; 3178b6cee71dSXin LI d[0] = s[1]; 3179b6cee71dSXin LI d[1] = s[0]; 3180b6cee71dSXin LI return rv; 3181b6cee71dSXin LI } 3182b6cee71dSXin LI 3183b6cee71dSXin LI /* 3184b6cee71dSXin LI * swap an int 3185b6cee71dSXin LI */ 3186b6cee71dSXin LI private uint32_t 3187b6cee71dSXin LI swap4(uint32_t sv) 3188b6cee71dSXin LI { 3189b6cee71dSXin LI uint32_t rv; 3190b6cee71dSXin LI uint8_t *s = (uint8_t *)(void *)&sv; 3191b6cee71dSXin LI uint8_t *d = (uint8_t *)(void *)&rv; 3192b6cee71dSXin LI d[0] = s[3]; 3193b6cee71dSXin LI d[1] = s[2]; 3194b6cee71dSXin LI d[2] = s[1]; 3195b6cee71dSXin LI d[3] = s[0]; 3196b6cee71dSXin LI return rv; 3197b6cee71dSXin LI } 3198b6cee71dSXin LI 3199b6cee71dSXin LI /* 3200b6cee71dSXin LI * swap a quad 3201b6cee71dSXin LI */ 3202b6cee71dSXin LI private uint64_t 3203b6cee71dSXin LI swap8(uint64_t sv) 3204b6cee71dSXin LI { 3205b6cee71dSXin LI uint64_t rv; 3206b6cee71dSXin LI uint8_t *s = (uint8_t *)(void *)&sv; 3207b6cee71dSXin LI uint8_t *d = (uint8_t *)(void *)&rv; 3208b6cee71dSXin LI #if 0 3209b6cee71dSXin LI d[0] = s[3]; 3210b6cee71dSXin LI d[1] = s[2]; 3211b6cee71dSXin LI d[2] = s[1]; 3212b6cee71dSXin LI d[3] = s[0]; 3213b6cee71dSXin LI d[4] = s[7]; 3214b6cee71dSXin LI d[5] = s[6]; 3215b6cee71dSXin LI d[6] = s[5]; 3216b6cee71dSXin LI d[7] = s[4]; 3217b6cee71dSXin LI #else 3218b6cee71dSXin LI d[0] = s[7]; 3219b6cee71dSXin LI d[1] = s[6]; 3220b6cee71dSXin LI d[2] = s[5]; 3221b6cee71dSXin LI d[3] = s[4]; 3222b6cee71dSXin LI d[4] = s[3]; 3223b6cee71dSXin LI d[5] = s[2]; 3224b6cee71dSXin LI d[6] = s[1]; 3225b6cee71dSXin LI d[7] = s[0]; 3226b6cee71dSXin LI #endif 3227b6cee71dSXin LI return rv; 3228b6cee71dSXin LI } 3229b6cee71dSXin LI 3230b6cee71dSXin LI /* 3231b6cee71dSXin LI * byteswap a single magic entry 3232b6cee71dSXin LI */ 3233b6cee71dSXin LI private void 3234b6cee71dSXin LI bs1(struct magic *m) 3235b6cee71dSXin LI { 3236b6cee71dSXin LI m->cont_level = swap2(m->cont_level); 3237b6cee71dSXin LI m->offset = swap4((uint32_t)m->offset); 3238b6cee71dSXin LI m->in_offset = swap4((uint32_t)m->in_offset); 3239b6cee71dSXin LI m->lineno = swap4((uint32_t)m->lineno); 3240b6cee71dSXin LI if (IS_STRING(m->type)) { 3241b6cee71dSXin LI m->str_range = swap4(m->str_range); 3242b6cee71dSXin LI m->str_flags = swap4(m->str_flags); 3243b6cee71dSXin LI } 3244b6cee71dSXin LI else { 3245b6cee71dSXin LI m->value.q = swap8(m->value.q); 3246b6cee71dSXin LI m->num_mask = swap8(m->num_mask); 3247b6cee71dSXin LI } 3248b6cee71dSXin LI } 3249b6cee71dSXin LI 3250b6cee71dSXin LI protected size_t 3251b6cee71dSXin LI file_pstring_length_size(const struct magic *m) 3252b6cee71dSXin LI { 3253b6cee71dSXin LI switch (m->str_flags & PSTRING_LEN) { 3254b6cee71dSXin LI case PSTRING_1_LE: 3255b6cee71dSXin LI return 1; 3256b6cee71dSXin LI case PSTRING_2_LE: 3257b6cee71dSXin LI case PSTRING_2_BE: 3258b6cee71dSXin LI return 2; 3259b6cee71dSXin LI case PSTRING_4_LE: 3260b6cee71dSXin LI case PSTRING_4_BE: 3261b6cee71dSXin LI return 4; 3262b6cee71dSXin LI default: 3263b6cee71dSXin LI abort(); /* Impossible */ 3264b6cee71dSXin LI return 1; 3265b6cee71dSXin LI } 3266b6cee71dSXin LI } 3267b6cee71dSXin LI protected size_t 32689ce06829SXin LI file_pstring_get_length(const struct magic *m, const char *ss) 3269b6cee71dSXin LI { 3270b6cee71dSXin LI size_t len = 0; 32719ce06829SXin LI const unsigned char *s = (const unsigned char *)ss; 3272b6cee71dSXin LI 3273b6cee71dSXin LI switch (m->str_flags & PSTRING_LEN) { 3274b6cee71dSXin LI case PSTRING_1_LE: 3275b6cee71dSXin LI len = *s; 3276b6cee71dSXin LI break; 3277b6cee71dSXin LI case PSTRING_2_LE: 3278b6cee71dSXin LI len = (s[1] << 8) | s[0]; 3279b6cee71dSXin LI break; 3280b6cee71dSXin LI case PSTRING_2_BE: 3281b6cee71dSXin LI len = (s[0] << 8) | s[1]; 3282b6cee71dSXin LI break; 3283b6cee71dSXin LI case PSTRING_4_LE: 3284b6cee71dSXin LI len = (s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0]; 3285b6cee71dSXin LI break; 3286b6cee71dSXin LI case PSTRING_4_BE: 3287b6cee71dSXin LI len = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; 3288b6cee71dSXin LI break; 3289b6cee71dSXin LI default: 3290b6cee71dSXin LI abort(); /* Impossible */ 3291b6cee71dSXin LI } 3292b6cee71dSXin LI 3293b6cee71dSXin LI if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF) 3294b6cee71dSXin LI len -= file_pstring_length_size(m); 3295b6cee71dSXin LI 3296b6cee71dSXin LI return len; 3297b6cee71dSXin LI } 3298b6cee71dSXin LI 3299b6cee71dSXin LI protected int 3300b6cee71dSXin LI file_magicfind(struct magic_set *ms, const char *name, struct mlist *v) 3301b6cee71dSXin LI { 3302b6cee71dSXin LI uint32_t i, j; 3303b6cee71dSXin LI struct mlist *mlist, *ml; 3304b6cee71dSXin LI 3305b6cee71dSXin LI mlist = ms->mlist[1]; 3306b6cee71dSXin LI 3307b6cee71dSXin LI for (ml = mlist->next; ml != mlist; ml = ml->next) { 3308b6cee71dSXin LI struct magic *ma = ml->magic; 3309b6cee71dSXin LI uint32_t nma = ml->nmagic; 3310b6cee71dSXin LI for (i = 0; i < nma; i++) { 3311b6cee71dSXin LI if (ma[i].type != FILE_NAME) 3312b6cee71dSXin LI continue; 3313b6cee71dSXin LI if (strcmp(ma[i].value.s, name) == 0) { 3314b6cee71dSXin LI v->magic = &ma[i]; 3315b6cee71dSXin LI for (j = i + 1; j < nma; j++) 3316b6cee71dSXin LI if (ma[j].cont_level == 0) 3317b6cee71dSXin LI break; 3318b6cee71dSXin LI v->nmagic = j - i; 3319b6cee71dSXin LI return 0; 3320b6cee71dSXin LI } 3321b6cee71dSXin LI } 3322b6cee71dSXin LI } 3323b6cee71dSXin LI return -1; 3324b6cee71dSXin LI } 3325