Deleted Added
full compact
35c35
< FILE_RCSID("@(#)$File: apprentice.c,v 1.211 2014/06/03 19:01:34 christos Exp $")
---
> FILE_RCSID("@(#)$File: apprentice.c,v 1.227 2014/11/28 02:46:39 christos Exp $")
88a89,92
> #define MAP_TYPE_MMAP 0
> #define MAP_TYPE_MALLOC 1
> #define MAP_TYPE_USER 2
>
103a108
> int type;
133a139,140
> private struct magic_map *apprentice_buf(struct magic_set *, struct magic *,
> size_t);
134a142
> private int check_buffer(struct magic_set *, struct magic_map *, const char *);
398a407
> mlp->map = idx == 0 ? map : NULL;
402c411
< ml->map = idx == 0 ? map : NULL;
---
> ml->map = NULL;
419,421d427
< #ifndef COMPILE_ONLY
< struct mlist *ml;
< #endif /* COMPILE_ONLY */
423a430
> struct mlist *ml;
425c432
< #endif /* COMPILE_ONLY */
---
> #endif
454,455c461
< apprentice_unmap(map);
< return -1;
---
> goto fail;
461c467,468
< printf("Set %zu:\nBinary patterns:\n", i);
---
> printf("Set %" SIZE_T_FORMAT "u:\nBinary patterns:\n",
> i);
467,468d473
< #endif /* COMPILE_ONLY */
<
469a475,483
> fail:
> for (i = 0; i < MAGIC_SETS; i++) {
> mlist_free(ms->mlist[i]);
> ms->mlist[i] = NULL;
> }
> return -1;
> #else
> return 0;
> #endif /* COMPILE_ONLY */
512a527,530
> ms->indir_max = FILE_INDIR_MAX;
> ms->name_max = FILE_NAME_MAX;
> ms->elf_shnum_max = FILE_ELF_SHNUM_MAX;
> ms->elf_phnum_max = FILE_ELF_PHNUM_MAX;
524c542,543
< if (map->p != NULL) {
---
>
> switch (map->type) {
526c545,546
< if (map->len)
---
> case MAP_TYPE_MMAP:
> if (map->p)
528c548
< else
---
> break;
529a550
> case MAP_TYPE_MALLOC:
531,534c552,556
< } else {
< uint32_t j;
< for (j = 0; j < MAGIC_SETS; j++)
< free(map->magic[j]);
---
> break;
> case MAP_TYPE_USER:
> break;
> default:
> abort();
553c575
< struct mlist *ml;
---
> struct mlist *ml, *next;
558,559c580,581
< for (ml = mlist->next; ml != mlist;) {
< struct mlist *next = ml->next;
---
> ml = mlist->next;
> for (ml = mlist->next; (next = ml->next) != NULL; ml = next) {
563c585,586
< ml = next;
---
> if (ml == mlist)
> break;
565d587
< free(ml);
567a590,638
> #ifndef COMPILE_ONLY
> /* void **bufs: an array of compiled magic files */
> protected int
> buffer_apprentice(struct magic_set *ms, struct magic **bufs,
> size_t *sizes, size_t nbufs)
> {
> size_t i, j;
> struct mlist *ml;
> struct magic_map *map;
>
> if (nbufs == 0)
> return -1;
>
> if (ms->mlist[0] != NULL)
> file_reset(ms);
>
> init_file_tables();
>
> for (i = 0; i < MAGIC_SETS; i++) {
> mlist_free(ms->mlist[i]);
> if ((ms->mlist[i] = mlist_alloc()) == NULL) {
> file_oomem(ms, sizeof(*ms->mlist[i]));
> goto fail;
> }
> }
>
> for (i = 0; i < nbufs; i++) {
> map = apprentice_buf(ms, bufs[i], sizes[i]);
> if (map == NULL)
> goto fail;
>
> for (j = 0; j < MAGIC_SETS; j++) {
> if (add_mlist(ms->mlist[j], map, j) == -1) {
> file_oomem(ms, sizeof(*ml));
> goto fail;
> }
> }
> }
>
> return 0;
> fail:
> for (i = 0; i < MAGIC_SETS; i++) {
> mlist_free(ms->mlist[i]);
> ms->mlist[i] = NULL;
> }
> return -1;
> }
> #endif
>
593,597c664,666
< if (i != 0) {
< --i;
< do
< mlist_free(ms->mlist[i]);
< while (i != 0);
---
> while (i-- > 0) {
> mlist_free(ms->mlist[i]);
> ms->mlist[i] = NULL;
1320c1389
< v = (char) v;
---
> v = (signed char) v;
2071a2141,2146
> goodchar(unsigned char x, const char *extra)
> {
> return (isascii(x) && isalnum(x)) || strchr(extra, x);
> }
>
> private int
2073c2148
< off_t off, size_t len, const char *name, int nt)
---
> off_t off, size_t len, const char *name, const char *extra, int nt)
2094,2096c2169
< for (i = 0; *l && ((isascii((unsigned char)*l) &&
< isalnum((unsigned char)*l)) || strchr("-+/.", *l)) &&
< i < len; buf[i++] = *l++)
---
> for (i = 0; *l && i < len && goodchar(*l, extra); buf[i++] = *l++)
2105a2179,2181
> if (!isspace((unsigned char)*l) && !goodchar(*l, extra))
> file_magwarn(ms, "%s type `%s' has bad char '%c'",
> name, line, *l);
2112,2113c2188,2190
< else
< return -1;
---
>
> file_magerror(ms, "Bad magic entry '%s'", line);
> return -1;
2126c2203
< sizeof(m->apple), "APPLE", 0);
---
> sizeof(m->apple), "APPLE", "!+-./", 0);
2139c2216
< sizeof(m->mimetype), "MIME", 1);
---
> sizeof(m->mimetype), "MIME", "+-/.", 1);
2699a2777,2798
> * handle a buffer containing a compiled file.
> */
> private struct magic_map *
> apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len)
> {
> struct magic_map *map;
>
> if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
> file_oomem(ms, sizeof(*map));
> return NULL;
> }
> map->len = len;
> map->p = buf;
> map->type = MAP_TYPE_USER;
> if (check_buffer(ms, map, "buffer") != 0) {
> apprentice_unmap(map);
> return NULL;
> }
> return map;
> }
>
> /*
2708,2710d2806
< uint32_t *ptr;
< uint32_t version, entries, nentries;
< int needsbyteswap;
2713d2808
< size_t i;
2744a2840
> map->type = MAP_TYPE_MMAP;
2754c2850
< map->len = 0;
---
> map->type = MAP_TYPE_MALLOC;
2758a2855,2877
>
> if (check_buffer(ms, map, dbname) != 0)
> goto error;
>
> free(dbname);
> return map;
>
> error:
> if (fd != -1)
> (void)close(fd);
> apprentice_unmap(map);
> free(dbname);
> return NULL;
> }
>
> private int
> check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname)
> {
> uint32_t *ptr;
> uint32_t entries, nentries;
> uint32_t version;
> int i, needsbyteswap;
>
2763c2882
< goto error;
---
> return -1;
2776c2895
< goto error;
---
> return -1;
2778,2780c2897,2899
< entries = (uint32_t)(st.st_size / sizeof(struct magic));
< if ((off_t)(entries * sizeof(struct magic)) != st.st_size) {
< file_error(ms, 0, "Size of `%s' %" INT64_T_FORMAT "u is not "
---
> entries = (uint32_t)(map->len / sizeof(struct magic));
> if ((entries * sizeof(struct magic)) != map->len) {
> file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not "
2782,2784c2901,2902
< dbname, (unsigned long long)st.st_size,
< sizeof(struct magic));
< goto error;
---
> dbname, map->len, sizeof(struct magic));
> return -1;
2800c2918
< goto error;
---
> return -1;
2805,2813c2923
< free(dbname);
< return map;
<
< error:
< if (fd != -1)
< (void)close(fd);
< apprentice_unmap(map);
< free(dbname);
< return NULL;
---
> return 0;