aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

mgindex.c (8055B)


      1 /* see LICENSE file for copyright and license details */
      2 #include "mgint.h"
      3 
      4 #include <errno.h>
      5 #include <stdlib.h>
      6 #include <string.h>
      7 #include <strings.h>
      8 #include <unistd.h>
      9 
     10 /*
     11  * what the loaded tests need of a file, worked out once: for each group
     12  * (a top level test and its continuations) how far into the file it can
     13  * read, and which mime types it can give. a reach of -1 is not known
     14  * before the file is read (an indirect offset, one relative to a match,
     15  * a named test used): such a group sees what the head holds
     16  */
     17 
     18 /* the bytes a regex looks at when its line gives no range, as file(1) */
     19 #define REGEX_REACH MG_REGEX_MAX
     20 
     21 static long long
     22 line_reach(const struct MagicLine *l, int from_end)
     23 {
     24   long long off = l->offset;
     25 
     26   /* the end of the file is read apart from the head */
     27   if (from_end || (l->flag & MF_OFFNEG))
     28     return 0;
     29   if (l->flag & (MF_INDIR | MF_OFFADD | MF_INDIROFFADD))
     30     return -1;
     31   switch (l->type) {
     32     case MT_DEFAULT:
     33     case MT_CLEAR:
     34     case MT_NAME:
     35     case MT_OFFSET:
     36       return 0;
     37     case MT_INDIRECT:
     38     case MT_USE:
     39     case MT_DER:
     40       return -1;
     41     case MT_SEARCH:
     42       return off + (long long)l->range + l->vallen;
     43     case MT_REGEX:
     44       if (l->sflags & SF_REGEX_LINES)
     45         return -1;
     46       return off + (long long)(l->range ? l->range : REGEX_REACH);
     47     case MT_PSTRING:
     48       return off + (long long)mg_pstrlen_size(l->sflags) + MG_STRLEN;
     49     case MT_STRING:
     50     case MT_BESTRING16:
     51     case MT_LESTRING16:
     52     case MT_OCTAL:
     53       return off + (long long)l->vallen * (l->type == MT_STRING || l->type == MT_OCTAL ? 1 : 2);
     54     default:
     55       return off + (long long)mg_typesize(l->type);
     56   }
     57 }
     58 
     59 static int
     60 by_mime(const void *a, const void *b)
     61 {
     62   const struct MagicMime *x = a, *y = b;
     63   int                     c = strcasecmp(x->mime, y->mime);
     64 
     65   if (c)
     66     return c;
     67   return (x->group > y->group) - (x->group < y->group);
     68 }
     69 
     70 static int
     71 add_mime(struct MagicIndex *ix, size_t *cap, const char *mime, size_t g)
     72 {
     73   struct MagicMime *more;
     74 
     75   /* a group names a type once, however many of its lines do */
     76   for (size_t i = ix->nmime; i > 0 && ix->mime[i - 1].group == g; i--)
     77     if (!strcasecmp(ix->mime[i - 1].mime, mime))
     78       return 0;
     79   if (ix->nmime == *cap) {
     80     *cap = *cap ? *cap * 2 : 1024;
     81     if (!(more = realloc(ix->mime, *cap * sizeof(*more))))
     82       return -1;
     83     ix->mime = more;
     84   }
     85   ix->mime[ix->nmime].mime  = mime;
     86   ix->mime[ix->nmime].group = g;
     87   ix->nmime++;
     88   return 0;
     89 }
     90 
     91 static int
     92 compile(struct MagicLine *l)
     93 {
     94   if (l->type != MT_REGEX || l->rx || l->rxbad)
     95     return 0;
     96   if (!(l->rx = malloc(sizeof(*l->rx))))
     97     return -1;
     98   if (regcomp(
     99           l->rx,
    100           l->value.s,
    101           REG_EXTENDED | REG_NEWLINE | (l->sflags & SF_IGNORE_CASE ? REG_ICASE : 0)
    102       )) {
    103     free(l->rx);
    104     l->rx    = NULL;
    105     l->rxbad = 1;
    106   }
    107   return 0;
    108 }
    109 
    110 static int
    111 index_db(struct MagicIndex *ix, struct MagicDb *db, size_t *gcap, size_t *mcap)
    112 {
    113   struct MagicLine  *v = db->v[0];
    114   struct MagicGroup *g, *more;
    115   size_t             i, j, n = db->n[0];
    116   long long          r;
    117   int                from_end;
    118 
    119   for (i = 0; i < n; i = j) {
    120     for (j = i + 1; j < n && v[j].cont; j++)
    121       ;
    122     if (ix->ngroup == *gcap) {
    123       *gcap = *gcap ? *gcap * 2 : 1024;
    124       if (!(more = realloc(ix->group, *gcap * sizeof(*more))))
    125         return -1;
    126       ix->group = more;
    127     }
    128     g        = &ix->group[ix->ngroup];
    129     g->db    = db;
    130     g->first = i;
    131     g->n     = j - i;
    132     g->reach = 0;
    133     /* continuations of a test at the end read from the end as well */
    134     from_end = (v[i].flag & MF_OFFNEG) != 0;
    135     for (size_t k = i; k < j; k++) {
    136       /* a test from the end and what follows it read within the tail,
    137        * as far back as the test's own offset */
    138       if ((v[k].flag & MF_OFFNEG)
    139           && (size_t)(uint32_t)v[k].offset + mg_typesize(v[k].type) + v[k].vallen > ix->tail)
    140         ix->tail = (size_t)(uint32_t)v[k].offset + mg_typesize(v[k].type) + v[k].vallen;
    141       r = line_reach(&v[k], from_end && !(v[k].flag & MF_OFFPOS));
    142       if (r < 0 || g->reach < 0)
    143         g->reach = -1;
    144       else if (r > g->reach)
    145         g->reach = r;
    146       if (v[k].mime[0] && add_mime(ix, mcap, v[k].mime, ix->ngroup) < 0)
    147         return -1;
    148     }
    149     if (g->reach > ix->reach)
    150       ix->reach = g->reach;
    151     if (g->reach > MAGIC_CHARSET_MAX) {
    152       size_t *more = realloc(ix->far, (ix->nfar + 1) * sizeof(*more));
    153 
    154       if (!more)
    155         return -1;
    156       ix->far             = more;
    157       ix->far[ix->nfar++] = ix->ngroup;
    158     }
    159     ix->ngroup++;
    160   }
    161   return 0;
    162 }
    163 
    164 int
    165 magic_prepare(struct Magic *m)
    166 {
    167   struct MagicIndex *ix;
    168   struct MagicDb    *db;
    169   size_t             gcap = 0, mcap = 0;
    170 
    171   if (m->ix)
    172     return 0;
    173   if (!(ix = calloc(1, sizeof(*ix))))
    174     return -1;
    175   for (db = m->db; db; db = db->next) {
    176     for (int s = 0; s < MG_SETS; s++)
    177       for (size_t i = 0; i < db->n[s]; i++)
    178         if (compile(&db->v[s][i]) < 0)
    179           goto fail;
    180     if (index_db(ix, db, &gcap, &mcap) < 0)
    181       goto fail;
    182   }
    183   qsort(ix->mime, ix->nmime, sizeof(*ix->mime), by_mime);
    184   m->ix = ix;
    185   return 0;
    186 fail:
    187   mg_index_free(ix);
    188   errno = ENOMEM;
    189   return -1;
    190 }
    191 
    192 void
    193 mg_index_free(struct MagicIndex *ix)
    194 {
    195   if (!ix)
    196     return;
    197   free(ix->group);
    198   free(ix->mime);
    199   free(ix->far);
    200   free(ix);
    201 }
    202 
    203 /*
    204  * the groups that can give mime, in the order they are tried, into
    205  * *out (to free), and how far they reach: -1 when one of them cannot
    206  * say. 0 groups when none gives it
    207  */
    208 size_t
    209 mg_index_pick(const struct MagicIndex *ix, const char *mime, size_t **out, long long *reach)
    210 {
    211   size_t lo = 0, hi = ix->nmime, n = 0, *v;
    212 
    213   *out   = NULL;
    214   *reach = 0;
    215   while (lo < hi) {
    216     size_t mid = (lo + hi) / 2;
    217 
    218     if (strcasecmp(ix->mime[mid].mime, mime) < 0)
    219       lo = mid + 1;
    220     else
    221       hi = mid;
    222   }
    223   for (hi = lo; hi < ix->nmime && !strcasecmp(ix->mime[hi].mime, mime); hi++)
    224     ;
    225   if (hi == lo || !(v = malloc((hi - lo) * sizeof(*v))))
    226     return 0;
    227   for (size_t i = lo; i < hi; i++) {
    228     const struct MagicGroup *g = &ix->group[ix->mime[i].group];
    229 
    230     v[n++] = ix->mime[i].group;
    231     if (g->reach < 0 || *reach < 0)
    232       *reach = -1;
    233     else if (g->reach > *reach)
    234       *reach = g->reach;
    235   }
    236   *out = v;
    237   return n;
    238 }
    239 
    240 /* the bytes of line l, at most n into the file, into buf from r's file */
    241 static int
    242 read_line(struct MagicRun *r, const struct MagicLine *l, unsigned char *buf, size_t have, size_t n)
    243 {
    244   long long end = line_reach(l, 0), off = l->offset;
    245 
    246   if (off < (long long)have)
    247     off = (long long)have;
    248   if (end <= 0 || off >= end || (size_t)off >= n)
    249     return 0;
    250   if ((size_t)end > n)
    251     end = (long long)n;
    252   return mg_pread(r, buf + off, (size_t)(end - off), off) < 0 ? -1 : 0;
    253 }
    254 
    255 /*
    256  * the far groups that can still match, into alive: a group whose top
    257  * level test fails needs nothing more read. those that hold get the
    258  * bytes their other lines look at read into buf, n long, past the have
    259  * bytes it holds; the rest of buf is left as it is. how many, or -1
    260  * when the file cannot be read
    261  */
    262 size_t
    263 mg_index_probe(struct MagicRun *r, unsigned char *buf, size_t have, size_t n, size_t *alive)
    264 {
    265   const struct MagicIndex *ix = r->m->ix;
    266   struct MagicBuf          b  = {buf, n, 1};
    267   size_t                   k  = 0;
    268 
    269   for (size_t i = 0; i < ix->nfar; i++) {
    270     const struct MagicGroup *g = &ix->group[ix->far[i]];
    271     struct MagicLine        *v = g->db->v[0] + g->first;
    272     int                      holds;
    273 
    274     if (read_line(r, &v[0], buf, have, n) < 0)
    275       return (size_t)-1;
    276     if ((holds = mg_top_holds(r, &v[0], &b)) < 0)
    277       return (size_t)-1;
    278     if (!holds)
    279       continue;
    280     for (size_t l = 1; l < g->n; l++)
    281       if (read_line(r, &v[l], buf, have, n) < 0)
    282         return (size_t)-1;
    283     alive[k++] = ix->far[i];
    284   }
    285   return k;
    286 }
    287 
    288 int
    289 magic_knows(const struct Magic *m, const char *mime)
    290 {
    291   size_t   *pick, n;
    292   long long reach;
    293 
    294   if (!m->ix || !mime)
    295     return 0;
    296   n = mg_index_pick(m->ix, mime, &pick, &reach);
    297   free(pick);
    298   return n > 0;
    299 }