aruu

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

commit f83eaf914ee40c3f19a16d4742045c9043d7e7b7
parent b18da4f6f263923defc8d624b60e63c4016a6c3a
Author: xplshn <anto@xplshn.com.ar>
Date:   Fri, 27 Nov 2026 14:01:38 -0300

libmagic: magic_prepare and magic_fd_hint read only what the tests look at, the name's type tried first

Diffstat:
Mshared/libmagic/magic.c | 214+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mshared/libmagic/magic.h | 16++++++++++++++++
Ashared/libmagic/mgindex.c | 281+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mshared/libmagic/mgint.h | 36++++++++++++++++++++++++++++++++++--
Mshared/libmagic/mgmatch.c | 37+++++++++++++++++++++++++++++++++++++
5 files changed, 582 insertions(+), 2 deletions(-)

diff --git a/shared/libmagic/magic.c b/shared/libmagic/magic.c @@ -11,6 +11,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <strings.h> #include <sys/stat.h> #include <unistd.h> @@ -361,6 +362,7 @@ magic_close(struct Magic *m) { if (!m) return; + mg_index_free(m->ix); mg_free(m->db); if (m->cloc) freelocale(m->cloc); @@ -453,3 +455,215 @@ magic_charset(const void *buf, size_t n) return "binary"; return code_mime; } + +/* what a hinted pass reads when a test of the hint's says only at run time how far it looks */ +#define HINT_REACH (64 * 1024) +/* the least a pass reads: classify calls a file of fewer bytes empty */ +#define HEAD_LEAST 512 + +/* the head of fd grown to want bytes, those already read kept */ +static int +grow_head(int fd, unsigned char **buf, size_t *have, size_t *cap, size_t want, int *eof) +{ + unsigned char *more; + ssize_t got; + + if (want > *cap) { + if (!(more = realloc(*buf, want))) + return -1; + *buf = more; + *cap = want; + } + while (*have < want && !*eof) { + if ((got = pread(fd, *buf + *have, want - *have, (off_t)*have)) < 0) { + if (errno == EINTR) + continue; + return -1; + } + if (!got) + *eof = 1; + *have += (size_t)got; + } + return 0; +} + +/* only the binary tests of the picked groups: what the hint says, or nothing */ +static int +hint_pass(struct MagicRun *r, int mime, char *dst, size_t dn) +{ + const char *code, *code_mime, *ftype; + int text, rv; + + memset(&r->out, 0, sizeof(r->out)); + r->mime = mime; + if ((text = mg_encoding(r->head.p, r->head.n, NULL, NULL, &code, &code_mime, &ftype)) < 0) + return -1; + rv = mg_softmagic(r, &r->head, MF_BINTEST, text); + if (r->out.err) + rv = -1; + if (rv > 0) + deliver(&r->out, dst, dn); + free(r->out.s); + memset(&r->out, 0, sizeof(r->out)); + return rv; +} + +static void +run_free(struct MagicRun *r) +{ + if (r->tstate == 1) + free(r->tail); + r->tail = NULL; + r->tstate = 0; +} + +/* the run over fd's head as read so far */ +static void +run_init(struct MagicRun *r, struct Magic *m, int fd, const struct stat *st, const unsigned char *buf, size_t n) +{ + memset(r, 0, sizeof(*r)); + r->m = m; + r->head.p = buf; + r->head.n = n; + r->head.file = 1; + r->fd = fd; + r->fsize = (long long)st->st_size; +} + +/* the hinted pass: 1 with the result in desc and mime when it gave hint */ +static int +try_hint(struct Magic *m, int fd, const struct stat *st, const unsigned char *buf, size_t n, const size_t *pick, + size_t npick, const char *hint, char *desc, size_t dn, char *mime, size_t mn) +{ + struct MagicRun r; + char got[MG_MIMELEN]; + locale_t old = (locale_t)0; + int rv; + + run_init(&r, m, fd, st, buf, n); + r.pick = pick; + r.npick = npick; + if (m->cloc) + old = uselocale(m->cloc); + got[0] = '\0'; + rv = hint_pass(&r, 1, got, sizeof(got)); + if (rv > 0 && !strcasecmp(got, hint)) { + if (mime && mn) + snprintf(mime, mn, "%s", got); + if (desc && dn && (rv = hint_pass(&r, 0, desc, dn)) <= 0) + desc[0] = '\0'; + rv = 1; + } else if (rv >= 0) { + rv = 0; + } + if (m->cloc) + uselocale(old); + run_free(&r); + return rv; +} + +/* the far groups over the head as read and their own bytes past it, the + * rest zeros: 1 when one of them matches */ +static int +try_far(struct Magic *m, int fd, const struct stat *st, unsigned char **buf, size_t have, size_t *cap, size_t want) +{ + struct MagicRun r; + unsigned char *more; + char got[MG_MIMELEN]; + locale_t old = (locale_t)0; + size_t *alive, nalive; + int rv; + + if (want > *cap) { + if (!(more = realloc(*buf, want))) + return -1; + *buf = more; + *cap = want; + } + memset(*buf + have, 0, want - have); + if (!(alive = malloc(m->ix->nfar * sizeof(*alive)))) + return -1; + run_init(&r, m, fd, st, *buf, want); + if ((nalive = mg_index_probe(&r, *buf, have, want, alive)) == (size_t)-1 || !nalive) { + free(alive); + run_free(&r); + memset(*buf + have, 0, want - have); + return nalive ? -1 : 0; + } + r.pick = alive; + r.npick = nalive; + if (m->cloc) + old = uselocale(m->cloc); + got[0] = '\0'; + rv = hint_pass(&r, 1, got, sizeof(got)); + if (rv == 0) + rv = hint_pass(&r, 0, got, sizeof(got)); + if (m->cloc) + uselocale(old); + run_free(&r); + free(alive); + /* what was probed is not the file: the head is read again from have */ + memset(*buf + have, 0, want - have); + return rv; +} + +int +magic_fd_hint(struct Magic *m, int fd, const char *hint, char *desc, size_t dn, char *mime, size_t mn) +{ + struct MagicRun *r; + struct stat st; + unsigned char *buf = NULL; + size_t have = 0, cap = 0, npick = 0, *pick = NULL, want; + long long reach = m->ix ? m->ix->reach : MAGIC_BYTES_MAX; + int eof = 0, rv = -1, e; + + if (desc && dn) + desc[0] = '\0'; + if (mime && mn) + mime[0] = '\0'; + if (fstat(fd, &st) < 0) + return -1; + if (!S_ISREG(st.st_mode)) + return magic_fd(m, fd, desc, dn, mime, mn); + if (m->ix && hint && *hint && (npick = mg_index_pick(m->ix, hint, &pick, &reach)) > 0) { + want = reach < 0 ? HINT_REACH : reach < HEAD_LEAST ? HEAD_LEAST : (size_t)reach; + if (grow_head(fd, &buf, &have, &cap, want, &eof) < 0) + goto out; + if ((rv = try_hint(m, fd, &st, buf, have, pick, npick, hint, desc, dn, mime, mn)) != 0) + goto out; + } + /* everything else, from what was read on: first the window the text + * tests read, which holds what nearly every test looks at; only what + * that calls data is read on, as far as the tests with a known reach + * look (disk images, mostly), never more than file(1) does */ + if (!(r = calloc(1, sizeof(*r)))) { + rv = -1; + goto out; + } + reach = m->ix ? m->ix->reach : MAGIC_BYTES_MAX; + want = reach > MAGIC_BYTES_MAX ? MAGIC_BYTES_MAX : reach < MAGIC_CHARSET_MAX ? MAGIC_CHARSET_MAX : (size_t)reach; + if (grow_head(fd, &buf, &have, &cap, m->ix ? MAGIC_CHARSET_MAX : want, &eof) < 0) { + rv = -1; + goto done; + } + run_init(r, m, fd, &st, buf, have); + rv = run(m, r, (st.st_mode & 0111) != 0, desc, dn, mime, mn); + if (rv < 0 || eof || have >= want || !m->ix || !m->ix->nfar || !mime || strcmp(mime, "application/octet-stream")) + goto done; + /* the far tests tried over just the bytes they look at; read the whole + * way only for one that matches */ + if ((rv = try_far(m, fd, &st, &buf, have, &cap, want)) <= 0) + goto done; + if ((rv = grow_head(fd, &buf, &have, &cap, want, &eof)) < 0) + goto done; + run_init(r, m, fd, &st, buf, have); + rv = run(m, r, (st.st_mode & 0111) != 0, desc, dn, mime, mn); +done: + free(r); +out: + e = errno; + free(pick); + free(buf); + errno = e; + return rv < 0 ? -1 : 0; +} diff --git a/shared/libmagic/magic.h b/shared/libmagic/magic.h @@ -84,6 +84,22 @@ int magic_buffer( int magic_fd(struct Magic *m, int fd, char *desc, size_t dn, char *mime, size_t mn); /* + * works out once what the loaded tests need of a file: how far each + * reads, and which tests give which mime type. after it the handle may + * be shared by threads classifying at the same time, as every regex is + * compiled. returns 0, or -1 with errno set when memory runs out + */ +int magic_prepare(struct Magic *m); +/* + * as magic_fd, reading no more of the file than the tests need. hint, + * a mime type the name suggests (NULL for none), is tried first: only + * the tests that can give it, over only the bytes they look at. when + * they give it, that is the answer; else every test runs, over the + * bytes already read and as many more as the tests with a known reach + * look at. without magic_prepare it reads as magic_fd does + */ +int magic_fd_hint(struct Magic *m, int fd, const char *hint, char *desc, size_t dn, char *mime, size_t mn); +/* * the charset file --mime prints for these bytes: "us-ascii", * "utf-8", "iso-8859-1" and so on, or "binary". the result is a * constant string diff --git a/shared/libmagic/mgindex.c b/shared/libmagic/mgindex.c @@ -0,0 +1,281 @@ +/* see LICENSE file for copyright and license details */ +#include "mgint.h" + +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> +#include <unistd.h> + +/* + * what the loaded tests need of a file, worked out once: for each group + * (a top level test and its continuations) how far into the file it can + * read, and which mime types it can give. a reach of -1 is not known + * before the file is read (an indirect offset, one relative to a match, + * a named test used): such a group sees what the head holds + */ + +/* the bytes a regex looks at when its line gives no range, as file(1) */ +#define REGEX_REACH MG_REGEX_MAX + +static long long +line_reach(const struct MagicLine *l, int from_end) +{ + long long off = l->offset; + + /* the end of the file is read apart from the head */ + if (from_end || (l->flag & MF_OFFNEG)) + return 0; + if (l->flag & (MF_INDIR | MF_OFFADD | MF_INDIROFFADD)) + return -1; + switch (l->type) { + case MT_DEFAULT: + case MT_CLEAR: + case MT_NAME: + case MT_OFFSET: + return 0; + case MT_INDIRECT: + case MT_USE: + case MT_DER: + return -1; + case MT_SEARCH: + return off + (long long)l->range + l->vallen; + case MT_REGEX: + if (l->sflags & SF_REGEX_LINES) + return -1; + return off + (long long)(l->range ? l->range : REGEX_REACH); + case MT_PSTRING: + return off + (long long)mg_pstrlen_size(l->sflags) + MG_STRLEN; + case MT_STRING: + case MT_BESTRING16: + case MT_LESTRING16: + case MT_OCTAL: + return off + (long long)l->vallen * (l->type == MT_STRING || l->type == MT_OCTAL ? 1 : 2); + default: + return off + (long long)mg_typesize(l->type); + } +} + +static int +by_mime(const void *a, const void *b) +{ + const struct MagicMime *x = a, *y = b; + int c = strcasecmp(x->mime, y->mime); + + if (c) + return c; + return (x->group > y->group) - (x->group < y->group); +} + +static int +add_mime(struct MagicIndex *ix, size_t *cap, const char *mime, size_t g) +{ + struct MagicMime *more; + + /* a group names a type once, however many of its lines do */ + for (size_t i = ix->nmime; i > 0 && ix->mime[i - 1].group == g; i--) + if (!strcasecmp(ix->mime[i - 1].mime, mime)) + return 0; + if (ix->nmime == *cap) { + *cap = *cap ? *cap * 2 : 1024; + if (!(more = realloc(ix->mime, *cap * sizeof(*more)))) + return -1; + ix->mime = more; + } + ix->mime[ix->nmime].mime = mime; + ix->mime[ix->nmime].group = g; + ix->nmime++; + return 0; +} + +static int +compile(struct MagicLine *l) +{ + if (l->type != MT_REGEX || l->rx || l->rxbad) + return 0; + if (!(l->rx = malloc(sizeof(*l->rx)))) + return -1; + if (regcomp(l->rx, l->value.s, REG_EXTENDED | REG_NEWLINE | (l->sflags & SF_IGNORE_CASE ? REG_ICASE : 0))) { + free(l->rx); + l->rx = NULL; + l->rxbad = 1; + } + return 0; +} + +static int +index_db(struct MagicIndex *ix, struct MagicDb *db, size_t *gcap, size_t *mcap) +{ + struct MagicLine *v = db->v[0]; + struct MagicGroup *g, *more; + size_t i, j, n = db->n[0]; + long long r; + int from_end; + + for (i = 0; i < n; i = j) { + for (j = i + 1; j < n && v[j].cont; j++) + ; + if (ix->ngroup == *gcap) { + *gcap = *gcap ? *gcap * 2 : 1024; + if (!(more = realloc(ix->group, *gcap * sizeof(*more)))) + return -1; + ix->group = more; + } + g = &ix->group[ix->ngroup]; + g->db = db; + g->first = i; + g->n = j - i; + g->reach = 0; + /* continuations of a test at the end read from the end as well */ + from_end = (v[i].flag & MF_OFFNEG) != 0; + for (size_t k = i; k < j; k++) { + /* a test from the end and what follows it read within the tail, + * as far back as the test's own offset */ + if ((v[k].flag & MF_OFFNEG) && (size_t)(uint32_t)v[k].offset + mg_typesize(v[k].type) + v[k].vallen > ix->tail) + ix->tail = (size_t)(uint32_t)v[k].offset + mg_typesize(v[k].type) + v[k].vallen; + r = line_reach(&v[k], from_end && !(v[k].flag & MF_OFFPOS)); + if (r < 0 || g->reach < 0) + g->reach = -1; + else if (r > g->reach) + g->reach = r; + if (v[k].mime[0] && add_mime(ix, mcap, v[k].mime, ix->ngroup) < 0) + return -1; + } + if (g->reach > ix->reach) + ix->reach = g->reach; + if (g->reach > MAGIC_CHARSET_MAX) { + size_t *more = realloc(ix->far, (ix->nfar + 1) * sizeof(*more)); + + if (!more) + return -1; + ix->far = more; + ix->far[ix->nfar++] = ix->ngroup; + } + ix->ngroup++; + } + return 0; +} + +int +magic_prepare(struct Magic *m) +{ + struct MagicIndex *ix; + struct MagicDb *db; + size_t gcap = 0, mcap = 0; + + if (m->ix) + return 0; + if (!(ix = calloc(1, sizeof(*ix)))) + return -1; + for (db = m->db; db; db = db->next) { + for (int s = 0; s < MG_SETS; s++) + for (size_t i = 0; i < db->n[s]; i++) + if (compile(&db->v[s][i]) < 0) + goto fail; + if (index_db(ix, db, &gcap, &mcap) < 0) + goto fail; + } + qsort(ix->mime, ix->nmime, sizeof(*ix->mime), by_mime); + m->ix = ix; + return 0; +fail: + mg_index_free(ix); + errno = ENOMEM; + return -1; +} + +void +mg_index_free(struct MagicIndex *ix) +{ + if (!ix) + return; + free(ix->group); + free(ix->mime); + free(ix->far); + free(ix); +} + +/* + * the groups that can give mime, in the order they are tried, into + * *out (to free), and how far they reach: -1 when one of them cannot + * say. 0 groups when none gives it + */ +size_t +mg_index_pick(const struct MagicIndex *ix, const char *mime, size_t **out, long long *reach) +{ + size_t lo = 0, hi = ix->nmime, n = 0, *v; + + *out = NULL; + *reach = 0; + while (lo < hi) { + size_t mid = (lo + hi) / 2; + + if (strcasecmp(ix->mime[mid].mime, mime) < 0) + lo = mid + 1; + else + hi = mid; + } + for (hi = lo; hi < ix->nmime && !strcasecmp(ix->mime[hi].mime, mime); hi++) + ; + if (hi == lo || !(v = malloc((hi - lo) * sizeof(*v)))) + return 0; + for (size_t i = lo; i < hi; i++) { + const struct MagicGroup *g = &ix->group[ix->mime[i].group]; + + v[n++] = ix->mime[i].group; + if (g->reach < 0 || *reach < 0) + *reach = -1; + else if (g->reach > *reach) + *reach = g->reach; + } + *out = v; + return n; +} + +/* the bytes of line l, at most n into the file, into buf from r's file */ +static int +read_line(struct MagicRun *r, const struct MagicLine *l, unsigned char *buf, size_t have, size_t n) +{ + long long end = line_reach(l, 0), off = l->offset; + + if (off < (long long)have) + off = (long long)have; + if (end <= 0 || off >= end || (size_t)off >= n) + return 0; + if ((size_t)end > n) + end = (long long)n; + return mg_pread(r, buf + off, (size_t)(end - off), off) < 0 ? -1 : 0; +} + +/* + * the far groups that can still match, into alive: a group whose top + * level test fails needs nothing more read. those that hold get the + * bytes their other lines look at read into buf, n long, past the have + * bytes it holds; the rest of buf is left as it is. how many, or -1 + * when the file cannot be read + */ +size_t +mg_index_probe(struct MagicRun *r, unsigned char *buf, size_t have, size_t n, size_t *alive) +{ + const struct MagicIndex *ix = r->m->ix; + struct MagicBuf b = {buf, n, 1}; + size_t k = 0; + + for (size_t i = 0; i < ix->nfar; i++) { + const struct MagicGroup *g = &ix->group[ix->far[i]]; + struct MagicLine *v = g->db->v[0] + g->first; + int holds; + + if (read_line(r, &v[0], buf, have, n) < 0) + return (size_t)-1; + if ((holds = mg_top_holds(r, &v[0], &b)) < 0) + return (size_t)-1; + if (!holds) + continue; + for (size_t l = 1; l < g->n; l++) + if (read_line(r, &v[l], buf, have, n) < 0) + return (size_t)-1; + alive[k++] = ix->far[i]; + } + return k; +} diff --git a/shared/libmagic/mgint.h b/shared/libmagic/mgint.h @@ -194,9 +194,34 @@ struct MagicDb { struct MagicDb *next; }; -struct Magic { +/* a top level test and its continuations, and how far into a file they + * read: -1 when that is known only once the file is read */ +struct MagicGroup { struct MagicDb *db; - locale_t cloc; /* regex and ctype run in the posix locale */ + size_t first, n; + long long reach; +}; +/* a mime type a group can give */ +struct MagicMime { + const char *mime; + size_t group; +}; +/* what magic_prepare works out: the groups, the types by name, and the + * farthest any group with a known reach reads */ +struct MagicIndex { + struct MagicGroup *group; + size_t ngroup; + struct MagicMime *mime; + size_t nmime; + long long reach; + size_t *far; /* the groups that read past MAGIC_CHARSET_MAX */ + size_t nfar; + size_t tail; /* the most any test reads back from the end */ +}; +struct Magic { + struct MagicDb *db; + locale_t cloc; /* regex and ctype run in the posix locale */ + struct MagicIndex *ix; /* NULL until magic_prepare */ }; /* growing output string, empty on allocation failure with err set */ @@ -237,8 +262,14 @@ struct MagicRun { int ligot[MG_LEVELS]; unsigned nindir; unsigned nnames; + const size_t *pick; /* when set, only these groups of the index are tried */ + size_t npick; }; +/* mgindex.c */ +void mg_index_free(struct MagicIndex *); +size_t mg_index_pick(const struct MagicIndex *, const char *, size_t **, long long *); +size_t mg_index_probe(struct MagicRun *, unsigned char *, size_t, size_t, size_t *); /* mgload.c */ int mg_load(struct Magic *, const char *); void mg_free(struct MagicDb *); @@ -248,6 +279,7 @@ uint64_t mg_signextend(const struct MagicLine *, uint64_t); /* mgmatch.c */ int mg_softmagic(struct MagicRun *, const struct MagicBuf *, int, int); +int mg_top_holds(struct MagicRun *, struct MagicLine *, const struct MagicBuf *); int mg_find(struct Magic *, const char *, struct MagicLine **, size_t *); size_t mg_pstrlen_size(uint32_t); diff --git a/shared/libmagic/mgmatch.c b/shared/libmagic/mgmatch.c @@ -241,6 +241,9 @@ tail_fill(struct MagicRun *r) if (r->fsize < 0) return -1; n = (unsigned long long)r->fsize < r->head.n ? (size_t)r->fsize : r->head.n; + /* no test reads further back than the index says */ + if (r->m->ix && n > r->m->ix->tail) + n = r->m->ix->tail; if ((unsigned long long)r->fsize <= r->head.n) { r->tail = (unsigned char *)r->head.p + (r->head.n - n); r->tlen = n; @@ -2161,6 +2164,30 @@ match( } /* + * whether the top level test m holds over b, its continuations not + * tried: 1, 0, or -1 when memory runs out + */ +int +mg_top_holds(struct MagicRun *r, struct MagicLine *m, const struct MagicBuf *b) +{ + struct MagicBuf bb; + struct Shared sh = {0, 0, 1}; + int ret = 0, found = 0; + + memset(&bb, 0, sizeof(bb)); + if (skip_line(m, MF_BINTEST, 0) || setoffset(r, m, &bb, b, 0, 0) < 0) + return 0; + switch (mget(r, m, b, &bb, 0, 0, MF_BINTEST, 0, 0, &sh, &ret, &found)) { + case -1: + return -1; + case 0: + return m->reln == '!'; + default: + return magiccheck(r, m); + } +} + +/* * run every loaded database against b, the binary tests when mode is * MF_BINTEST and the text tests when it is MF_TEXTTEST. text tells if * the bytes looked like text. returns 1 when something matched @@ -2177,6 +2204,16 @@ mg_softmagic(struct MagicRun *r, const struct MagicBuf *b, int mode, int text) sh.firstline = 1; r->nindir = 0; r->nnames = 0; + /* a hint narrows the tests to the groups that can give its type */ + if (r->pick) { + for (size_t i = 0; i < r->npick; i++) { + const struct MagicGroup *g = &r->m->ix->group[r->pick[i]]; + + if ((ret = match(r, g->db->v[0] + g->first, g->n, b, 0, mode, text, 0, &sh, NULL, NULL)) != 0) + return ret; + } + return 0; + } for (db = r->m->db; db; db = db->next) { if ((ret = match(r, db->v[0], db->n[0], b, 0, mode, text, 0, &sh, NULL, NULL)) != 0) return ret;