Lines Matching +full:pmax +full:- +full:pascal
2 * Copyright (c) Ian F. Darwin 1986-1995.
4 * maintained 1995-present by Christos Zoulas and others.
29 * apprentice - make one pass through /etc/magic, learning its secrets.
76 #define MAP_FAILED (void *) -1
179 #define DECLARE_FIELD(name) { # name, sizeof(# name) - 1, parse_ ## name }
213 ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0; in main()
229 * XXX - the actual Single UNIX Specification says that "long" means "long",
230 * as in the C data type, but we treat it as meaning "4-byte integer".
236 # define XX(s) s, (sizeof(s) - 1)
320 for (p = tbl; p->len; p++) { in get_type()
321 if (strncmp(l, p->name, p->len) == 0) { in get_type()
323 *t = l + p->len; in get_type()
327 return p->type; in get_type()
359 * XXX - the actual Single UNIX Specification says in get_standard_integer_type()
361 * but we treat it as meaning "4-byte integer". in get_standard_integer_type()
382 * of 1, 2, 4, and 8 - the Single UNIX Specification in get_standard_integer_type()
388 * non-power-of-2 values. in get_standard_integer_type()
391 /* Multi-digit, so > 9 */ in get_standard_integer_type()
408 /* XXX - what about 3, 5, 6, or 7? */ in get_standard_integer_type()
434 for (p = type_tbl; p->len; p++) { in init_file_tables()
435 assert(p->type < FILE_NAMES_SIZE); in init_file_tables()
436 file_names[p->type] = p->name; in init_file_tables()
437 file_formats[p->type] = p->format; in init_file_tables()
439 assert(p - type_tbl == FILE_NAMES_SIZE); in init_file_tables()
447 mlp->map = NULL; in add_mlist()
449 return -1; in add_mlist()
451 ml->map = idx == 0 ? map : NULL; in add_mlist()
452 ml->magic = map->magic[idx]; in add_mlist()
453 ml->nmagic = map->nmagic[idx]; in add_mlist()
454 if (ml->nmagic) { in add_mlist()
455 ml->magic_rxcomp = CAST(file_regex_t **, in add_mlist()
456 calloc(ml->nmagic, sizeof(*ml->magic_rxcomp))); in add_mlist()
457 if (ml->magic_rxcomp == NULL) { in add_mlist()
459 return -1; in add_mlist()
462 ml->magic_rxcomp = NULL; in add_mlist()
463 mlp->prev->next = ml; in add_mlist()
464 ml->prev = mlp->prev; in add_mlist()
465 ml->next = mlp; in add_mlist()
466 mlp->prev = ml; in add_mlist()
483 CAST(unsigned long, sizeof(*map->magic[0])), in apprentice_1()
485 return -1; in apprentice_1()
491 return -1; in apprentice_1()
498 if (ms->flags & MAGIC_CHECK) in apprentice_1()
502 return -1; in apprentice_1()
506 if (add_mlist(ms->mlist[i], map, i) == -1) { in apprentice_1()
512 file_oomem(ms, sizeof(*ms->mlist[0])); in apprentice_1()
513 return -1; in apprentice_1()
521 apprentice_list(ms->mlist[i], BINTEST); in apprentice_1()
523 apprentice_list(ms->mlist[i], TEXTTEST); in apprentice_1()
539 mlist_free(ms->mlist[i]); in file_ms_free()
540 free(ms->o.pbuf); in file_ms_free()
541 free(ms->o.buf); in file_ms_free()
542 free(ms->c.li); in file_ms_free()
544 freelocale(ms->c_lc_ctype); in file_ms_free()
559 if (magic_setflags(ms, flags) == -1) { in file_ms_alloc()
564 ms->o.buf = ms->o.pbuf = NULL; in file_ms_alloc()
565 ms->o.blen = 0; in file_ms_alloc()
566 len = (ms->c.len = 10) * sizeof(*ms->c.li); in file_ms_alloc()
568 if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL) in file_ms_alloc()
571 ms->event_flags = 0; in file_ms_alloc()
572 ms->error = -1; in file_ms_alloc()
574 ms->mlist[i] = NULL; in file_ms_alloc()
575 ms->file = "unknown"; in file_ms_alloc()
576 ms->line = 0; in file_ms_alloc()
577 ms->indir_max = FILE_INDIR_MAX; in file_ms_alloc()
578 ms->name_max = FILE_NAME_MAX; in file_ms_alloc()
579 ms->elf_shnum_max = FILE_ELF_SHNUM_MAX; in file_ms_alloc()
580 ms->elf_shsize_max = FILE_ELF_SHSIZE_MAX; in file_ms_alloc()
581 ms->elf_phnum_max = FILE_ELF_PHNUM_MAX; in file_ms_alloc()
582 ms->elf_notes_max = FILE_ELF_NOTES_MAX; in file_ms_alloc()
583 ms->regex_max = FILE_REGEX_MAX; in file_ms_alloc()
584 ms->bytes_max = FILE_BYTES_MAX; in file_ms_alloc()
585 ms->encoding_max = FILE_ENCODING_MAX; in file_ms_alloc()
587 ms->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0); in file_ms_alloc()
588 assert(ms->c_lc_ctype != NULL); in file_ms_alloc()
604 switch (map->type) { in apprentice_unmap()
608 p = CAST(char *, map->p); in apprentice_unmap()
610 char *b = RCAST(char *, map->magic[i]); in apprentice_unmap()
611 if (p != NULL && b >= p && b <= p + map->len) in apprentice_unmap()
619 if (map->p && map->p != MAP_FAILED) in apprentice_unmap()
620 (void)munmap(map->p, map->len); in apprentice_unmap()
624 fprintf(stderr, "Bad map type %d", map->type); in apprentice_unmap()
637 mlist->next = mlist->prev = mlist; in mlist_alloc()
647 mlist_free(ms->mlist[i]); in mlist_free_all()
648 ms->mlist[i] = NULL; in mlist_free_all()
657 if (ml->map) in mlist_free_one()
658 apprentice_unmap(CAST(struct magic_map *, ml->map)); in mlist_free_one()
660 for (i = 0; i < ml->nmagic; ++i) { in mlist_free_one()
661 if (ml->magic_rxcomp[i]) { in mlist_free_one()
662 file_regfree(ml->magic_rxcomp[i]); in mlist_free_one()
663 free(ml->magic_rxcomp[i]); in mlist_free_one()
664 ml->magic_rxcomp[i] = NULL; in mlist_free_one()
667 free(ml->magic_rxcomp); in mlist_free_one()
668 ml->magic_rxcomp = NULL; in mlist_free_one()
680 for (ml = mlist->next; ml != mlist;) { in mlist_free()
681 next = ml->next; in mlist_free()
698 return -1; in buffer_apprentice()
705 mlist_free(ms->mlist[i]); in buffer_apprentice()
706 if ((ms->mlist[i] = mlist_alloc()) == NULL) { in buffer_apprentice()
707 file_oomem(ms, sizeof(*ms->mlist[0])); in buffer_apprentice()
718 if (add_mlist(ms->mlist[j], map, j) == -1) { in buffer_apprentice()
719 file_oomem(ms, sizeof(*ms->mlist[0])); in buffer_apprentice()
728 return -1; in buffer_apprentice()
737 int fileerr, errs = -1; in file_apprentice()
743 return -1; in file_apprentice()
749 return -1; in file_apprentice()
753 mlist_free(ms->mlist[i]); in file_apprentice()
754 if ((ms->mlist[i] = mlist_alloc()) == NULL) { in file_apprentice()
755 file_oomem(ms, sizeof(*ms->mlist[0])); in file_apprentice()
757 mlist_free(ms->mlist[j]); in file_apprentice()
758 ms->mlist[j] = NULL; in file_apprentice()
761 return -1; in file_apprentice()
779 if (errs == -1) { in file_apprentice()
781 mlist_free(ms->mlist[i]); in file_apprentice()
782 ms->mlist[i] = NULL; in file_apprentice()
785 return -1; in file_apprentice()
796 mlist_free(ms->mlist[i]); in file_apprentice()
797 ms->mlist[i] = NULL; in file_apprentice()
809 return -1; in file_apprentice()
817 * - magic characters count 0 unless escaped.
818 * - [] expressions count 1
819 * - {} expressions count 0
820 * - regular characters or escaped magic characters count 1
821 * - 0 length expressions count as one
833 p--; in nonmagic()
846 p--; in nonmagic()
852 p--; in nonmagic()
940 switch (m->type) { in apprentice_magic_strength_1()
942 if (m->factor_op != FILE_FACTOR_OP_NONE) { in apprentice_magic_strength_1()
944 m->factor_op); in apprentice_magic_strength_1()
994 ts = typesize(m->type); in apprentice_magic_strength_1()
997 m->type); in apprentice_magic_strength_1()
1006 val += m->vallen * MULT; in apprentice_magic_strength_1()
1011 val += m->vallen * MULT / 2; in apprentice_magic_strength_1()
1015 if (m->vallen == 0) in apprentice_magic_strength_1()
1017 val += m->vallen * MAX(MULT / m->vallen, 1); in apprentice_magic_strength_1()
1021 v = nonmagic(m->value.s); in apprentice_magic_strength_1()
1036 (void)fprintf(stderr, "Bad type %d\n", m->type); in apprentice_magic_strength_1()
1040 switch (m->reln) { in apprentice_magic_strength_1()
1052 val -= 2 * MULT; in apprentice_magic_strength_1()
1057 val -= MULT; in apprentice_magic_strength_1()
1061 (void)fprintf(stderr, "Bad relation %c\n", m->reln); in apprentice_magic_strength_1()
1077 if (m->desc[0] == '\0') { in file_magic_strength()
1093 switch (m->factor_op) { in file_magic_strength()
1097 val += m->factor; in file_magic_strength()
1100 val -= m->factor; in file_magic_strength()
1103 val *= m->factor; in file_magic_strength()
1106 val /= m->factor; in file_magic_strength()
1109 (void)fprintf(stderr, "Bad factor_op %u\n", m->factor_op); in file_magic_strength()
1121 if (m->desc[0] == '\0') in file_magic_strength()
1136 size_t sa = file_magic_strength(ma->mp, ma->cont_count); in apprentice_sort()
1137 size_t sb = file_magic_strength(mb->mp, mb->cont_count); in apprentice_sort()
1141 return -1; in apprentice_sort()
1154 for (ml = mlist->next; ml != mlist; ml = ml->next) { in apprentice_list()
1155 for (magindex = 0; magindex < ml->nmagic; magindex++) { in apprentice_list()
1156 struct magic *m = &ml->magic[magindex]; in apprentice_list()
1157 if ((m->flag & mode) != mode) { in apprentice_list()
1158 /* Skip sub-tests */ in apprentice_list()
1159 while (magindex + 1 < ml->nmagic && in apprentice_list()
1160 ml->magic[magindex + 1].cont_level != 0) in apprentice_list()
1162 continue; /* Skip to next top-level test*/ in apprentice_list()
1170 for (; magindex + 1 < ml->nmagic && in apprentice_list()
1171 ml->magic[magindex + 1].cont_level != 0; in apprentice_list()
1174 if (*ml->magic[descindex].desc == '\0' in apprentice_list()
1175 && *ml->magic[mi].desc) in apprentice_list()
1177 if (*ml->magic[mimeindex].mimetype == '\0' in apprentice_list()
1178 && *ml->magic[mi].mimetype) in apprentice_list()
1183 file_magic_strength(m, ml->nmagic - magindex), in apprentice_list()
1184 ml->magic[lineindex].lineno, in apprentice_list()
1185 ml->magic[descindex].desc, in apprentice_list()
1186 ml->magic[mimeindex].mimetype); in apprentice_list()
1194 switch (m->type) { in set_test_type()
1241 mstart->flag |= BINTEST; in set_test_type()
1248 if (mstart->str_flags & STRING_TEXTTEST) in set_test_type()
1249 mstart->flag |= TEXTTEST; in set_test_type()
1251 mstart->flag |= BINTEST; in set_test_type()
1256 if (mstart->str_flags & STRING_BINTEST) in set_test_type()
1257 mstart->flag |= BINTEST; in set_test_type()
1258 if (mstart->str_flags & STRING_TEXTTEST) in set_test_type()
1259 mstart->flag |= TEXTTEST; in set_test_type()
1261 if (mstart->flag & (TEXTTEST|BINTEST)) in set_test_type()
1265 if (file_looks_utf8(m->value.us, CAST(size_t, m->vallen), NULL, in set_test_type()
1267 mstart->flag |= BINTEST; in set_test_type()
1269 mstart->flag |= TEXTTEST; in set_test_type()
1286 size_t i = me->mp->type == FILE_NAME ? 1 : 0; in addentry()
1295 return -1; in addentry()
1320 FILE *f = fopen(ms->file = fn, "r"); in load_1()
1331 for (ms->line = 1; (len = getline(&line, &llen, f)) != -1; in load_1()
1332 ms->line++) { in load_1()
1335 if (line[len - 1] == '\n') { in load_1()
1337 line[len - 1] = '\0'; /* delete newline */ in load_1()
1348 if (CAST(size_t, len - 2) > bang[i].len && in load_1()
1368 len - bang[i].len - 2) != 0) { in load_1()
1418 if ((ms->flags & MAGIC_DEBUG) == 0) in set_text_binary()
1421 me[i].mp->mimetype, in set_text_binary()
1422 me[i].mp->mimetype[0] == '\0' ? "" : "; ", in set_text_binary()
1423 me[i].mp->desc[0] ? me[i].mp->desc : "(no description)", in set_text_binary()
1424 me[i].mp->flag & BINTEST ? binary : text); in set_text_binary()
1425 if (me[i].mp->flag & BINTEST) { in set_text_binary()
1426 char *p = strstr(me[i].mp->desc, text); in set_text_binary()
1427 if (p && (p == me[i].mp->desc || in set_text_binary()
1428 isspace(CAST(unsigned char, p[-1]))) && in set_text_binary()
1429 (p + len - me[i].mp->desc == MAXstring in set_text_binary()
1435 } while (++i < nme && me[i].mp->cont_level != 0); in set_text_binary()
1444 if (me[i].mp->cont_level == 0 && in set_last_default()
1445 me[i].mp->type == FILE_DEFAULT) { in set_last_default()
1447 if (me[i].mp->cont_level == 0) in set_last_default()
1450 /* XXX - Ugh! */ in set_last_default()
1451 ms->line = me[i].mp->lineno; in set_last_default()
1479 return -1; in coalesce_entries()
1517 ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */ in apprentice_load()
1525 map->type = MAP_TYPE_MALLOC; in apprentice_load()
1539 if (d->d_name[0] == '.') in apprentice_load()
1541 if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) { in apprentice_load()
1543 strlen(fn) + strlen(d->d_name) + 2); in apprentice_load()
1548 if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) { in apprentice_load()
1587 if (mset[j].me[i].mp->cont_level != 0) { in apprentice_load()
1608 &map->magic[j], &map->nmagic[j]) == -1) { in apprentice_load()
1632 if (!(m->flag & UNSIGNED)) { in file_signextend()
1633 switch(m->type) { in file_signextend()
1706 if (ms->flags & MAGIC_CHECK) in file_signextend()
1707 file_magwarn(ms, "cannot happen: m->type=%d\n", in file_signextend()
1708 m->type); in file_signextend()
1718 if ((ms->flags & MAGIC_CHECK) == 0) in string_modifier_check()
1721 if ((m->type != FILE_REGEX || (m->str_flags & REGEX_LINE_COUNT) == 0) && in string_modifier_check()
1722 (m->type != FILE_PSTRING && (m->str_flags & PSTRING_LEN) != 0)) { in string_modifier_check()
1724 "'/BHhLl' modifiers are only allowed for pascal strings\n"); in string_modifier_check()
1725 return -1; in string_modifier_check()
1727 switch (m->type) { in string_modifier_check()
1730 if (m->str_flags != 0) { in string_modifier_check()
1732 "no modifiers allowed for 16-bit strings\n"); in string_modifier_check()
1733 return -1; in string_modifier_check()
1738 if ((m->str_flags & REGEX_OFFSET_START) != 0) { in string_modifier_check()
1742 return -1; in string_modifier_check()
1746 if (m->str_range == 0) { in string_modifier_check()
1750 m->str_range = STRING_DEFAULT_RANGE; in string_modifier_check()
1751 return -1; in string_modifier_check()
1755 if ((m->str_flags & STRING_COMPACT_WHITESPACE) != 0) { in string_modifier_check()
1758 return -1; in string_modifier_check()
1760 if ((m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) != 0) { in string_modifier_check()
1763 return -1; in string_modifier_check()
1767 file_magwarn(ms, "coding error: m->type=%d\n", in string_modifier_check()
1768 m->type); in string_modifier_check()
1769 return -1; in string_modifier_check()
1786 case '-': in get_op()
1795 return -1; in get_op()
1815 for (p = cond_tbl; p->len; p++) { in get_cond()
1816 if (strncmp(l, p->name, p->len) == 0 && in get_cond()
1817 isspace(CAST(unsigned char, l[p->len]))) { in get_cond()
1819 *t = l + p->len; in get_cond()
1823 return p->cond; in get_cond()
1830 last_cond = ms->c.li[cont_level].last_cond; in check_cond()
1835 if (ms->flags & MAGIC_CHECK) in check_cond()
1837 return -1; in check_cond()
1844 if (ms->flags & MAGIC_CHECK) in check_cond()
1846 return -1; in check_cond()
1853 if (ms->flags & MAGIC_CHECK) in check_cond()
1855 return -1; in check_cond()
1865 ms->c.li[cont_level].last_cond = last_cond; in check_cond()
1878 m->str_flags |= INDIRECT_RELATIVE; in parse_indirect_modifier()
1881 if (ms->flags & MAGIC_CHECK) in parse_indirect_modifier()
1885 return -1; in parse_indirect_modifier()
1900 m->mask_op |= op; in parse_op_modifier()
1903 m->num_mask = file_signextend(ms, m, val); in parse_op_modifier()
1921 if (have_range && (ms->flags & MAGIC_CHECK)) in parse_string_modifier()
1924 m->str_range = CAST(uint32_t, strtoul(l, &t, 0)); in parse_string_modifier()
1925 if (m->str_range == 0) in parse_string_modifier()
1927 l = t - 1; in parse_string_modifier()
1930 m->str_flags |= STRING_COMPACT_WHITESPACE; in parse_string_modifier()
1933 m->str_flags |= STRING_COMPACT_OPTIONAL_WHITESPACE; in parse_string_modifier()
1936 m->str_flags |= STRING_IGNORE_LOWERCASE; in parse_string_modifier()
1939 m->str_flags |= STRING_IGNORE_UPPERCASE; in parse_string_modifier()
1942 m->str_flags |= REGEX_OFFSET_START; in parse_string_modifier()
1945 m->str_flags |= STRING_BINTEST; in parse_string_modifier()
1948 m->str_flags |= STRING_TEXTTEST; in parse_string_modifier()
1951 m->str_flags |= STRING_TRIM; in parse_string_modifier()
1954 m->str_flags |= STRING_FULL_WORD; in parse_string_modifier()
1957 #define SET_LENGTH(a) m->str_flags = (m->str_flags & ~PSTRING_LEN) | (a) in parse_string_modifier()
1958 if (m->type != FILE_PSTRING) in parse_string_modifier()
1963 if (m->type != FILE_PSTRING) in parse_string_modifier()
1968 if (m->type != FILE_PSTRING) in parse_string_modifier()
1973 if (m->type != FILE_PSTRING) in parse_string_modifier()
1978 switch (m->type) { in parse_string_modifier()
1988 if (m->type != FILE_PSTRING) in parse_string_modifier()
1990 m->str_flags |= PSTRING_LENGTH_INCLUDES_ITSELF; in parse_string_modifier()
1994 if (ms->flags & MAGIC_CHECK) in parse_string_modifier()
2003 if (string_modifier_check(ms, m) == -1) in parse_string_modifier()
2009 return -1; in parse_string_modifier()
2041 if (file_check_mem(ms, cont_level) == -1) in parse()
2042 return -1; in parse()
2046 if (me->mp == NULL) { in parse()
2048 return -1; in parse()
2050 if (me->cont_count == 0) { in parse()
2052 return -1; in parse()
2054 m = &me->mp[me->cont_count - 1]; in parse()
2055 diff = CAST(int32_t, cont_level) - CAST(int32_t, m->cont_level); in parse()
2059 m->cont_level); in parse()
2060 if (me->cont_count == me->max_count) { in parse()
2062 size_t cnt = me->max_count + ALLOC_CHUNK; in parse()
2063 if ((nm = CAST(struct magic *, realloc(me->mp, in parse()
2066 return -1; in parse()
2068 me->mp = nm; in parse()
2069 me->max_count = CAST(uint32_t, cnt); in parse()
2071 m = &me->mp[me->cont_count++]; in parse()
2073 m->cont_level = cont_level; in parse()
2076 if (me->mp != NULL) in parse()
2080 return -1; in parse()
2082 me->mp = m; in parse()
2083 me->max_count = ALLOC_CHUNK; in parse()
2085 m->factor_op = FILE_FACTOR_OP_NONE; in parse()
2086 m->cont_level = 0; in parse()
2087 me->cont_count = 1; in parse()
2089 m->lineno = CAST(uint32_t, lineno); in parse()
2091 if (*l == '&') { /* m->cont_level == 0 checked below. */ in parse()
2093 m->flag |= OFFADD; in parse()
2097 m->flag |= INDIR; in parse()
2098 if (m->flag & OFFADD) in parse()
2099 m->flag = (m->flag & ~OFFADD) | INDIROFFADD; in parse()
2101 if (*l == '&') { /* m->cont_level == 0 checked below */ in parse()
2103 m->flag |= OFFADD; in parse()
2107 if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD))) { in parse()
2108 if (ms->flags & MAGIC_CHECK) in parse()
2110 return -1; in parse()
2114 if (*l == '-') { in parse()
2116 m->flag |= OFFNEGATIVE; in parse()
2118 m->offset = CAST(int32_t, strtol(l, &t, 0)); in parse()
2120 if (ms->flags & MAGIC_CHECK) in parse()
2122 return -1; in parse()
2127 if (m->flag & INDIR) { in parse()
2128 m->in_type = FILE_LONG; in parse()
2129 m->in_offset = 0; in parse()
2130 m->in_op = 0; in parse()
2132 * read [.,lbs][+-]nnnnn) in parse()
2136 m->in_op |= FILE_OPSIGNED; in parse()
2140 m->in_type = FILE_LELONG; in parse()
2143 m->in_type = FILE_BELONG; in parse()
2146 m->in_type = FILE_MELONG; in parse()
2150 m->in_type = FILE_LESHORT; in parse()
2154 m->in_type = FILE_BESHORT; in parse()
2160 m->in_type = FILE_BYTE; in parse()
2165 m->in_type = FILE_LEDOUBLE; in parse()
2170 m->in_type = FILE_BEDOUBLE; in parse()
2173 m->in_type = FILE_LEID3; in parse()
2176 m->in_type = FILE_BEID3; in parse()
2179 m->in_type = FILE_OCTAL; in parse()
2182 m->in_type = FILE_LEQUAD; in parse()
2185 m->in_type = FILE_BEQUAD; in parse()
2188 if (ms->flags & MAGIC_CHECK) in parse()
2192 return -1; in parse()
2198 m->in_op |= FILE_OPINVERSE; in parse()
2201 if ((op = get_op(*l)) != -1) { in parse()
2202 m->in_op |= op; in parse()
2206 m->in_op |= FILE_OPINDIRECT; in parse()
2209 if (isdigit(CAST(unsigned char, *l)) || *l == '-') { in parse()
2210 m->in_offset = CAST(int32_t, strtol(l, &t, 0)); in parse()
2212 if (ms->flags & MAGIC_CHECK) in parse()
2215 return -1; in parse()
2220 ((m->in_op & FILE_OPINDIRECT) && *l++ != ')')) { in parse()
2221 if (ms->flags & MAGIC_CHECK) in parse()
2224 return -1; in parse()
2230 m->cond = get_cond(l, &l); in parse()
2231 if (check_cond(ms, m->cond, cont_level) == -1) in parse()
2232 return -1; in parse()
2246 m->type = get_type(type_tbl, l + 1, &l); in parse()
2247 if (m->type == FILE_INVALID) { in parse()
2252 m->type = get_standard_integer_type(l, &l); in parse()
2255 if (m->type != FILE_INVALID) in parse()
2256 m->flag |= UNSIGNED; in parse()
2264 m->type = get_type(type_tbl, l, &l); in parse()
2265 if (m->type == FILE_INVALID) { in parse()
2272 m->type = get_standard_integer_type(l, &l); in parse()
2275 m->type = FILE_STRING; in parse()
2281 if (m->type == FILE_INVALID) { in parse()
2282 /* Not found - try it as a special keyword. */ in parse()
2283 m->type = get_type(special_tbl, l, &l); in parse()
2286 if (m->type == FILE_INVALID) { in parse()
2287 if (ms->flags & MAGIC_CHECK) in parse()
2289 return -1; in parse()
2292 if (m->type == FILE_NAME && cont_level != 0) { in parse()
2293 if (ms->flags & MAGIC_CHECK) in parse()
2296 return -1; in parse()
2299 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */ in parse()
2300 /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */ in parse()
2302 m->mask_op = 0; in parse()
2304 if (!IS_STRING(m->type)) in parse()
2305 m->mask_op |= FILE_OPINVERSE; in parse()
2306 else if (ms->flags & MAGIC_CHECK) in parse()
2310 m->str_range = 0; in parse()
2311 m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0; in parse()
2312 if ((op = get_op(*l)) != -1) { in parse()
2313 if (IS_STRING(m->type)) { in parse()
2317 if (ms->flags & MAGIC_CHECK) in parse()
2321 return -1; in parse()
2324 if (m->type == FILE_INDIRECT) in parse()
2328 if (r == -1) in parse()
2329 return -1; in parse()
2343 m->reln = *l; in parse()
2346 if (ms->flags & MAGIC_CHECK) { in parse()
2348 m->reln); in parse()
2349 return -1; in parse()
2354 /* Old-style anding: "0 byte &0x80 dynamically linked" */ in parse()
2358 m->reln = *l; in parse()
2366 m->reln = *l; in parse()
2370 m->reln = '='; /* the default relation */ in parse()
2373 m->reln = *l; in parse()
2381 if (m->reln != 'x' && getvalue(ms, m, &l, action)) in parse()
2382 return -1; in parse()
2386 * #define offsetcheck {if (offset > ms->bytes_max -1) in parse()
2391 * Now get last part - the description in parse()
2396 m->flag |= NOSPACE; in parse()
2400 m->flag |= NOSPACE; in parse()
2402 for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); ) in parse()
2404 if (i == sizeof(m->desc)) { in parse()
2405 m->desc[sizeof(m->desc) - 1] = '\0'; in parse()
2406 if (ms->flags & MAGIC_CHECK) in parse()
2407 file_magwarn(ms, "description `%s' truncated", m->desc); in parse()
2414 if (ms->flags & MAGIC_CHECK) { in parse()
2415 if (check_format(ms, m) == -1) in parse()
2416 return -1; in parse()
2423 m->mimetype[0] = '\0'; /* initialise MIME type to none */ in parse()
2428 * parse a STRENGTH annotation line from magic file, put into magic[index - 1]
2440 struct magic *m = &me->mp[0]; in parse_strength()
2442 if (m->factor_op != FILE_FACTOR_OP_NONE) { in parse_strength()
2445 m->factor_op, m->factor); in parse_strength()
2446 return -1; in parse_strength()
2448 if (m->type == FILE_NAME) { in parse_strength()
2451 file_printable(ms, sbuf, sizeof(sbuf), m->value.s, in parse_strength()
2452 sizeof(m->value.s))); in parse_strength()
2453 return -1; in parse_strength()
2463 m->factor_op = *l++; in parse_strength()
2467 return -1; in parse_strength()
2479 m->factor = CAST(uint8_t, factor); in parse_strength()
2480 if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) { in parse_strength()
2482 m->factor_op, m->factor); in parse_strength()
2487 m->factor_op = FILE_FACTOR_OP_NONE; in parse_strength()
2488 m->factor = 0; in parse_strength()
2489 return -1; in parse_strength()
2505 struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1]; in parse_extra()
2512 return -1; in parse_extra()
2515 if (*m->desc == '\0') { in parse_extra()
2518 return -1; in parse_extra()
2528 buf[len - 1] = '\0'; in parse_extra()
2529 if (ms->flags & MAGIC_CHECK) in parse_extra()
2544 return -1; in parse_extra()
2549 * magic[index - 1]
2557 sizeof(me->mp[0].apple), "APPLE", "!+-./?", 0); in parse_apple()
2561 * Parse a comma-separated list of extensions
2569 sizeof(me->mp[0].ext), "EXTENSION", ",!+-/@?_$&~", 0); in parse_ext()
2575 * parse a MIME annotation line from magic file, put into magic[index - 1]
2584 sizeof(me->mp[0].mimetype), "MIME", "+-/.$?:{}", 1); in parse_mime()
2595 return -1; in check_format_type()
2628 while (*ptr && strchr("-.#", *ptr) != NULL) in check_format_type()
2632 len = len * 10 + (*ptr - '0'); \ in check_format_type()
2671 if (h-- <= 0) in check_format_type()
2675 if (h-- <= 0) in check_format_type()
2724 if (*ptr == '-') in check_format_type()
2747 if (*ptr == '-') in check_format_type()
2771 return -1; in check_format_type()
2774 return -1; in check_format_type()
2787 for (ptr = m->desc; *ptr; ptr++) in check_format()
2797 if (m->type >= file_nformats) { in check_format()
2799 "m->type and format strings"); in check_format()
2800 return -1; in check_format()
2802 if (file_formats[m->type] == FILE_FMT_NONE) { in check_format()
2804 "`%s'", m->desc, file_names[m->type]); in check_format()
2805 return -1; in check_format()
2809 if (check_format_type(ptr, m->type, &estr) == -1) { in check_format()
2816 file_names[m->type], m->desc); in check_format()
2817 return -1; in check_format()
2825 file_names[m->type], m->desc); in check_format()
2826 return -1; in check_format()
2835 * just after the number read. Return 0 for success, non-zero for failure.
2844 switch (m->type) { in getvalue()
2857 if (ms->flags & MAGIC_CHECK) in getvalue()
2859 m->value.s); in getvalue()
2860 return -1; in getvalue()
2862 if (m->type == FILE_REGEX) { in getvalue()
2865 file_regcomp(ms, &rx, m->value.s, REG_EXTENDED); in getvalue()
2869 return rc ? -1 : 0; in getvalue()
2873 if (m->reln == 'x') in getvalue()
2878 switch (m->type) { in getvalue()
2884 m->value.f = strtof(*p, &ep); in getvalue()
2886 m->value.f = (float)strtod(*p, &ep); in getvalue()
2895 m->value.d = strtod(*p, &ep); in getvalue()
2900 if (file_parse_guid(*p, m->value.guid) == -1) in getvalue()
2901 return -1; in getvalue()
2902 *p += FILE_GUID_SIZE - 1; in getvalue()
2907 m->value.q = file_signextend(ms, m, ull); in getvalue()
2910 return -1; in getvalue()
2912 size_t ts = typesize(m->type); in getvalue()
2919 type_tbl[m->type].name); in getvalue()
2920 return -1; in getvalue()
2924 if (*q == '-' && ull != UINT64_MAX) in getvalue()
2925 ull = -CAST(int64_t, ull); in getvalue()
2950 type_tbl[m->type].name, ull); in getvalue()
2951 return -1; in getvalue()
2965 * Copy the converted version to "m->value.s", and the length in m->vallen.
2972 char *p = m->value.s; in getstr()
2973 size_t plen = sizeof(m->value.s); in getstr()
2975 char *pmax = p + plen - 1; in getstr() local
2983 if (p >= pmax) { in getstr()
2992 bracket_nesting--; in getstr()
3002 s--; in getstr()
3005 if (m->type == FILE_REGEX && in getstr()
3025 && (m->type != FILE_REGEX || in getstr()
3099 val = c - '0'; in getstr()
3102 val = (val << 3) | (c - '0'); in getstr()
3105 val = (val << 3) | (c-'0'); in getstr()
3107 --s; in getstr()
3110 --s; in getstr()
3124 --s; in getstr()
3126 --s; in getstr()
3131 --s; in getstr()
3134 m->vallen = CAST(unsigned char, (p - origp)); in getstr()
3135 if (m->type == FILE_PSTRING) { in getstr()
3139 m->vallen += CAST(unsigned char, l); in getstr()
3145 /* Single hex char to int; -1 if not a hex char. */
3150 return -1; in hextoint()
3152 return c - '0'; in hextoint()
3154 return c + 10 - 'a'; in hextoint()
3156 return c + 10 - 'A'; in hextoint()
3157 return -1; in hextoint()
3176 if (len-- == 0) in file_showstr()
3259 map->len = len; in apprentice_buf()
3260 map->p = buf; in apprentice_buf()
3261 map->type = MAP_TYPE_USER; in apprentice_buf()
3282 fd = -1; in apprentice_map()
3287 map->type = MAP_TYPE_USER; /* unspecified */ in apprentice_map()
3293 if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1) in apprentice_map()
3296 if (fstat(fd, &st) == -1) { in apprentice_map()
3306 map->len = CAST(size_t, st.st_size); in apprentice_map()
3308 map->type = MAP_TYPE_MMAP; in apprentice_map()
3309 if ((map->p = mmap(0, CAST(size_t, st.st_size), PROT_READ|PROT_WRITE, in apprentice_map()
3315 map->type = MAP_TYPE_MALLOC; in apprentice_map()
3316 if ((map->p = CAST(void *, malloc(map->len))) == NULL) { in apprentice_map()
3317 file_oomem(ms, map->len); in apprentice_map()
3320 if (read(fd, map->p, map->len) != (ssize_t)map->len) { in apprentice_map()
3326 fd = -1; in apprentice_map()
3332 if (mprotect(map->p, CAST(size_t, st.st_size), PROT_READ) == -1) { in apprentice_map()
3342 if (fd != -1) in apprentice_map()
3357 ptr = CAST(uint32_t *, map->p); in check_buffer()
3361 return -1; in check_buffer()
3374 return -1; in check_buffer()
3376 entries = CAST(uint32_t, map->len / sizeof(struct magic)); in check_buffer()
3377 if ((entries * sizeof(struct magic)) != map->len) { in check_buffer()
3380 dbname, map->len, sizeof(struct magic)); in check_buffer()
3381 return -1; in check_buffer()
3383 map->magic[0] = CAST(struct magic *, map->p) + 1; in check_buffer()
3387 map->nmagic[i] = swap4(ptr[i + 2]); in check_buffer()
3389 map->nmagic[i] = ptr[i + 2]; in check_buffer()
3390 if (i != MAGIC_SETS - 1) in check_buffer()
3391 map->magic[i + 1] = map->magic[i] + map->nmagic[i]; in check_buffer()
3392 nentries += map->nmagic[i]; in check_buffer()
3397 return -1; in check_buffer()
3401 byteswap(map->magic[i], map->nmagic[i]); in check_buffer()
3411 static const size_t nm = sizeof(*map->nmagic) * MAGIC_SETS; in apprentice_compile()
3412 static const size_t m = sizeof(**map->magic); in apprentice_compile()
3413 int fd = -1; in apprentice_compile()
3416 int rv = -1; in apprentice_compile()
3428 if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) in apprentice_compile()
3436 memcpy(hdr.h + 2, map->nmagic, nm); in apprentice_compile()
3444 len = m * map->nmagic[i]; in apprentice_compile()
3445 if (write(fd, map->magic[i], len) != CAST(ssize_t, len)) { in apprentice_compile()
3453 if (fd != -1) in apprentice_compile()
3479 for (p = ext + sizeof(ext) - 1; p >= ext && q >= fn; p--, q--) in mkdbname()
3490 if (ms->flags & MAGIC_MIME) { in mkdbname()
3491 if (asprintf(&buf, "%.*s.mime%s", CAST(int, q - fn), fn, ext) in mkdbname()
3494 if (access(buf, R_OK) != -1) { in mkdbname()
3495 ms->flags &= MAGIC_MIME_TYPE; in mkdbname()
3500 if (asprintf(&buf, "%.*s%s", CAST(int, q - fn), fn, ext) < 0) in mkdbname()
3505 ms->flags &= MAGIC_MIME_TYPE; in mkdbname()
3594 *l = c - us + 1; in file_varint2uintmax_t()
3595 for (; c >= us; c--) { in file_varint2uintmax_t()
3607 *l = c - us + 1; in file_varint2uintmax_t()
3619 m->cont_level = swap2(m->cont_level); in bs1()
3620 m->offset = swap4(CAST(uint32_t, m->offset)); in bs1()
3621 m->in_offset = swap4(CAST(uint32_t, m->in_offset)); in bs1()
3622 m->lineno = swap4(CAST(uint32_t, m->lineno)); in bs1()
3623 if (IS_STRING(m->type)) { in bs1()
3624 m->str_range = swap4(m->str_range); in bs1()
3625 m->str_flags = swap4(m->str_flags); in bs1()
3628 m->value.q = swap8(m->value.q); in bs1()
3629 m->num_mask = swap8(m->num_mask); in bs1()
3636 switch (m->str_flags & PSTRING_LEN) { in file_pstring_length_size()
3647 "(bad pascal string length %d)", in file_pstring_length_size()
3648 m->str_flags & PSTRING_LEN); in file_pstring_length_size()
3660 switch (m->str_flags & PSTRING_LEN) { in file_pstring_get_length()
3690 "(bad pascal string length %d)", in file_pstring_get_length()
3691 m->str_flags & PSTRING_LEN); in file_pstring_get_length()
3695 if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF) { in file_pstring_get_length()
3699 len -= l; in file_pstring_get_length()
3711 mlist = ms->mlist[1]; in file_magicfind()
3713 for (ml = mlist->next; ml != mlist; ml = ml->next) { in file_magicfind()
3714 struct magic *ma = ml->magic; in file_magicfind()
3715 for (i = 0; i < ml->nmagic; i++) { in file_magicfind()
3719 v->magic = &ma[i]; in file_magicfind()
3720 v->magic_rxcomp = &(ml->magic_rxcomp[i]); in file_magicfind()
3721 for (j = i + 1; j < ml->nmagic; j++) in file_magicfind()
3724 v->nmagic = j - i; in file_magicfind()
3729 return -1; in file_magicfind()