Lines Matching refs:ms

88 -	struct magic_set *ms;
101 - if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
105 - ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
107 - (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
108 - magic_close(ms);
128 mlist_free(ms->mlist[i]);
129 - free(ms->o.pbuf);
130 - free(ms->o.buf);
131 - free(ms->c.li);
132 - free(ms);
133 + if (ms->o.pbuf) {
134 + efree(ms->o.pbuf);
136 + if (ms->o.buf) {
137 + efree(ms->o.buf);
139 + if (ms->c.li) {
140 + efree(ms->c.li);
142 + efree(ms);
147 struct magic_set *ms;
150 - if ((ms = CAST(struct magic_set *, calloc(CAST(size_t, 1u),
151 + if ((ms = CAST(struct magic_set *, ecalloc(CAST(size_t, 1u),
156 ms->o.blen = 0;
157 len = (ms->c.len = 10) * sizeof(*ms->c.li);
159 - if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
160 + if ((ms->c.li = CAST(struct level_info *, emalloc(len))) == NULL)
163 ms->event_flags = 0;
165 ms->bytes_max = FILE_BYTES_MAX;
166 return ms;
168 - free(ms);
169 + efree(ms);
242 -buffer_apprentice(struct magic_set *ms, struct magic **bufs,
252 - (void)file_reset(ms, 0);
257 - mlist_free(ms->mlist[i]);
258 - if ((ms->mlist[i] = mlist_alloc()) == NULL) {
259 - file_oomem(ms, sizeof(*ms->mlist[i]));
265 - map = apprentice_buf(ms, bufs[i], sizes[i]);
270 - if (add_mlist(ms->mlist[j], map, j) == -1) {
271 - file_oomem(ms, sizeof(*ml));
279 - mlist_free_all(ms);
286 file_apprentice(struct magic_set *ms, const char *fn, int action)
289 (void)file_reset(ms, 0);
304 + mlist_free(ms->mlist[i]);
305 + if ((ms->mlist[i] = mlist_alloc()) == NULL) {
306 + file_oomem(ms, sizeof(*ms->mlist[i]));
310 + return apprentice_1(ms, fn, action);
314 file_oomem(ms, strlen(fn));
318 mlist_free(ms->mlist[j]);
319 ms->mlist[j] = NULL;
342 file_oomem(ms, sizeof(*mp) * mset[i].max);
345 load_1(struct magic_set *ms, int action, const char *fn, int *errs,
356 - FILE *f = fopen(ms->file = fn, "r");
361 + ms->file = fn;
366 file_error(ms, errno, "cannot read magic file `%s'",
372 - for (ms->line = 1; (len = getline(&line, &llen, f)) != -1;
373 - ms->line++) {
374 + for (ms->line = 1; (line = php_stream_get_line(stream, buffer , BUFSIZ, &len)) != NULL; ms->line+…
381 (void)addentry(ms, &me, mset);
395 file_oomem(ms, slen);
426 ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
432 file_oomem(ms, sizeof(*map));
457 file_oomem(ms,
479 file_oomem(ms, mlen);
496 load_1(ms, action, filearr[i], &errs, mset);
505 load_1(ms, action, fn, &errs, mset);
521 file_oomem(ms, sizeof(*nm) * cnt);
529 file_oomem(ms, len);
539 else if (ms->flags & MAGIC_CHECK)
540 file_magwarn(ms, "'~' invalid for string types");
553 parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line,
562 parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line,
576 - if (ms->flags & MAGIC_CHECK)
577 - file_regerror(&rx, rc, ms);
603 -apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len)
608 - file_oomem(ms, sizeof(*map));
614 - if (check_buffer(ms, map, "buffer") != 0) {
626 apprentice_map(struct magic_set *ms, const char *fn)
645 file_oomem(ms, sizeof(*map));
666 dbname = mkdbname(ms, fn, 0);
680 file_error(ms, errno, "cannot stat `%s'", dbname);
686 file_error(ms, 0, "file `%s' is too %s", dbname,
697 - file_error(ms, errno, "cannot map `%s'", dbname);
703 - file_oomem(ms, map->len);
707 - file_badread(ms);
716 - if (check_buffer(ms, map, dbname) != 0) {
721 - file_error(ms, errno, "cannot mprotect `%s'", dbname);
723 + file_badread(ms);
740 -check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname)
754 file_error(ms, 0, "bad magic in `%s'", dbname);
764 - file_error(ms, 0, "File %s supports only version %d magic "
766 + file_error(ms, 0, "File %d.%d supports only version %d magic "
774 - file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not "
791 + file_error(ms, 0, "Size of `%s' %llu is not a multiple of %zu",
805 file_error(ms, 0, "Inconsistent entries in `%s' %u != %u",
846 dbname = mkdbname(ms, fn, 1);
857 file_error(ms, errno, "cannot open `%s'", dbname);
866 file_error(ms, errno, "error writing `%s'", dbname);
875 file_error(ms, errno, "error writing `%s'", dbname);
898 if (ms->flags & MAGIC_MIME) {
909 ms->flags &= MAGIC_MIME_TYPE;
944 file_ascmagic(struct magic_set *ms, const struct buffer *b, int text)
952 rv = file_ascmagic_with_encoding(ms, &bb,
962 file_ascmagic_with_encoding(struct magic_set *ms,
974 file_oomem(ms, mlen);
1384 prv = format_decompression_error(ms, i, newbuf);
1386 - prv = file_buffer(ms, -1, NULL, name, newbuf, nsz);
1387 + prv = file_buffer(ms, NULL, NULL, name, newbuf, nsz);
1395 - if (file_buffer(ms, -1, NULL, NULL, buf, nbytes) == -1) {
1396 + if (file_buffer(ms, NULL, NULL, NULL, buf, nbytes) == -1) {
1397 if (file_pop_buffer(ms, pb) != NULL)
1401 if ((rbuf = file_pop_buffer(ms, pb)) != NULL) {
1402 if (file_printf(ms, "%s", rbuf) == -1) {
1410 if (!mime && file_printf(ms, ")") == -1)
1419 ms->flags |= MAGIC_COMPRESS;
1446 file_badseek(ms);
1487 return toomany(ms, "program headers", phnum);
1489 if (dophn_core(ms, clazz, swap, fd,
1496 if (shnum > ms->elf_shnum_max)
1497 return toomany(ms, "section", shnum);
1498 if (dophn_exec(ms, clazz, swap, fd,
1505 if (shnum > ms->elf_shnum_max)
1506 return toomany(ms, "section headers", shnum);
1507 if (doshn(ms, clazz, swap, fd,
1547 -file_encoding(struct magic_set *ms, const struct buffer *b, unichar **ubuf,
1548 +file_encoding(struct magic_set *ms, const struct buffer *b, unicodechar **ubuf,
1568 file_oomem(ms, mlen);
1575 file_oomem(ms, mlen);
2007 -bad_link(struct magic_set *ms, int err, char *buf)
2009 - int mime = ms->flags & MAGIC_MIME;
2011 - file_printf(ms, "inode/symlink")
2015 - if (ms->flags & MAGIC_ERROR) {
2016 - file_error(ms, err,
2020 - if (file_printf(ms, "broken symbolic link to %s", buf) == -1)
2031 handle_mime(struct magic_set *ms, int mime, const char *str)
2036 -file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
2037 +file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb)
2040 int mime = ms->flags & MAGIC_MIME;
2041 int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION);
2057 - if ((ms->flags & MAGIC_SYMLINK) == 0)
2094 if (ms->flags & MAGIC_ERROR) {
2101 - if (handle_mime(ms, mime, "directory") == -1)
2104 - } else if (file_printf(ms, "%sdirectory", COMMA) == -1)
2114 - if ((ms->flags & MAGIC_DEVICES) != 0) {
2119 - if (handle_mime(ms, mime, "chardevice") == -1)
2133 + if ((ms->flags & MAGIC_DEVICES) != 0) {
2138 + if (handle_mime(ms, mime, "chardevice") == -1)
2143 if (file_printf(ms, "%scharacter special (%d/%d/%d)",
2147 if (file_printf(ms, "%scharacter special", COMMA) == -1)
2160 - if ((ms->flags & MAGIC_DEVICES) != 0) {
2165 - if (handle_mime(ms, mime, "blockdevice") == -1)
2171 - if (file_printf(ms, "%sblock special (%d/%d/%d)",
2176 - if (file_printf(ms, "%sblock special (%ld/%ld)",
2184 - if (file_printf(ms, "%sblock special", COMMA) == -1)
2194 if((ms->flags & MAGIC_DEVICES) != 0)
2201 if (ms->flags & MAGIC_ERROR) {
2202 - file_error(ms, errno, "unreadable symlink `%s'",
2204 + file_error(ms, errno, "unreadable symlink `%s'", fn);
2208 - if (handle_mime(ms, mime, "symlink") == -1)
2211 - } else if (file_printf(ms,
2227 - return bad_link(ms, errno, buf);
2231 - return bad_link(ms, errno, buf);
2240 - if (ms->flags & MAGIC_ERROR) {
2241 - file_error(ms, 0,
2246 - if (handle_mime(ms, mime,
2250 - } else if (file_printf(ms,
2264 - return bad_link(ms, errno, buf);
2270 - if ((ms->flags & MAGIC_SYMLINK) != 0) {
2272 - ms->flags &= MAGIC_SYMLINK;
2273 - p = magic_file(ms, buf);
2274 - ms->flags |= MAGIC_SYMLINK;
2279 - if (handle_mime(ms, mime, "symlink") == -1)
2282 - } else if (file_printf(ms, "%ssymbolic link to %s",
2310 file_clearbuf(struct magic_set *ms)
2312 - free(ms->o.buf);
2313 + efree(ms->o.buf);
2314 ms->o.buf = NULL;
2315 ms->o.blen = 0;
2325 -file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
2331 - if (ms->event_flags & EVENT_HAD_ERR)
2335 - file_clearbuf(ms);
2336 - file_error(ms, 0, "Bad magic format `%s' (%s)", fmt, tbuf);
2341 - if (len < 0 || (size_t)len > 1024 || len + ms->o.blen > 1024 * 1024) {
2342 - size_t blen = ms->o.blen;
2344 - file_clearbuf(ms);
2345 - file_error(ms, 0, "Output buffer space exceeded %d+%zu", len,
2350 - if (ms->o.buf != NULL) {
2351 - len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
2355 - free(ms->o.buf);
2358 - ms->o.buf = buf;
2359 - ms->o.blen = len;
2362 - file_clearbuf(ms);
2363 - file_error(ms, errno, "vasprintf failed");
2368 file_printf(struct magic_set *ms, const char *fmt, ...)
2375 - rv = file_vprintf(ms, fmt, ap);
2380 + if (ms->o.buf != NULL) {
2381 + spprintf(&newstr, 0, "%s%s", ms->o.buf, (buf ? buf : ""));
2385 + efree(ms->o.buf);
2386 + ms->o.buf = newstr;
2388 + ms->o.buf = buf;
2399 file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
2405 if (ms->event_flags & EVENT_HAD_ERR)
2408 - file_clearbuf(ms);
2409 - (void)file_printf(ms, "line %" SIZE_T_FORMAT "u:", lineno);
2410 + efree(ms->o.buf);
2411 + ms->o.buf = NULL;
2412 + file_printf(ms, "line %" SIZE_T_FORMAT "u:", lineno);
2419 + file_printf(ms, "%s (%s)", (*buf ? buf : ""), strerror(error));
2421 + file_printf(ms, "%s", buf);
2423 - if (ms->o.buf && *ms->o.buf)
2424 - (void)file_printf(ms, " ");
2425 - (void)file_vprintf(ms, f, va);
2427 - (void)file_printf(ms, " (%s)", strerror(error));
2433 ms->event_flags |= EVENT_HAD_ERR;
2434 ms->error = error;
2440 -file_buffer(struct magic_set *ms, int fd, struct stat *st,
2442 +file_buffer(struct magic_set *ms, php_stream *stream, zend_stat_t *st,
2466 ms->mode = b.st.st_mode;
2475 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) {
2476 m = file_zmagic(ms, &b, inname);
2478 if (file_printf(ms, "%s", code_mime) == -1)
2492 file_clearbuf(ms);
2493 if (ms->o.pbuf) {
2494 - free(ms->o.pbuf);
2495 + efree(ms->o.pbuf);
2496 ms->o.pbuf = NULL;
2498 ms->event_flags &= ~EVENT_HAD_ERR;
2503 - if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
2504 + if ((pbuf = CAST(char *, erealloc(ms->o.pbuf, psize))) == NULL) {
2505 file_oomem(ms, psize);
2509 if (level >= ms->c.len) {
2510 len = (ms->c.len = 20 + level) * sizeof(*ms->c.li);
2511 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
2513 - realloc(ms->c.li, len));
2515 + erealloc(ms->c.li, len));
2516 if (ms->c.li == NULL) {
2517 file_oomem(ms, len);
2521 file_printedlen(const struct magic_set *ms)
2523 - return ms->o.blen;
2524 + return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
2528 file_replace(struct magic_set *ms, const char *pat, const char *rep)
2535 - file_regerror(&rx, rc, ms);
2539 - while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
2540 - ms->o.buf[rm.rm_so] = '\0';
2541 - if (file_printf(ms, "%s%s", rep,
2542 - rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
2564 + res = php_pcre_replace_impl(pce, NULL, ms->o.buf, strlen(ms->o.buf), repl, -1, &rep_cnt);
2620 + strncpy(ms->o.buf, ZSTR_VAL(res), ZSTR_LEN(res));
2621 + ms->o.buf[ZSTR_LEN(res)] = '\0';
2624 -file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
2630 - file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
2638 if (ms->event_flags & EVENT_HAD_ERR)
2645 pb->buf = ms->o.buf;
2649 if (ms->event_flags & EVENT_HAD_ERR) {
2658 ms->o.blen = pb->blen;
2659 ms->offset = pb->offset;
2902 return file_apprentice(ms, magicfile, FILE_LOAD);
2910 -magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
2913 - if (ms == NULL)
2915 - return buffer_apprentice(ms, RCAST(struct magic **, bufs),
2921 magic_compile(struct magic_set *ms, const char *magicfile)
2924 return file_apprentice(ms, magicfile, FILE_LIST);
2928 -close_and_restore(const struct magic_set *ms, const char *name, int fd,
2935 - if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
2965 if (ms == NULL)
2967 - return file_or_fd(ms, NULL, fd);
2968 + return file_or_stream(ms, NULL, NULL);
2974 if (ms == NULL)
2976 - return file_or_fd(ms, inname, STDIN_FILENO);
2977 + return file_or_stream(ms, inname, NULL);
2981 +magic_stream(struct magic_set *ms, php_stream *stream)
2983 + if (ms == NULL)
2985 + return file_or_stream(ms, NULL, stream);
2989 -file_or_fd(struct magic_set *ms, const char *inname, int fd)
2990 +file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
3002 if (file_reset(ms, 1) == -1)
3008 - if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL)
3009 + if ((buf = CAST(unsigned char *, emalloc(ms->bytes_max + SLOP))) == NULL)
3012 switch (file_fsmagic(ms, inname, &sb)) {
3042 - unreadable_info(ms, sb.st_mode, inname) == -1)
3049 + if (unreadable_info(ms, sb.st_mode, inname) == -1)
3065 + if (ms->flags & MAGIC_ERROR) {
3066 + file_error(ms, errno, "cannot stat `%s'", inname);
3074 * try looking at the first ms->bytes_max bytes
3081 - CAST(size_t, ms->bytes_max - nbytes), 1)) > 0) {
3089 - if (unreadable_info(ms, sb.st_mode, inname) == -1)
3101 - ms->bytes_max;
3104 - file_error(ms, errno, "cannot read fd %d", fd);
3106 - file_error(ms, errno, "cannot read `%s'",
3110 + if ((nbytes = php_stream_read(stream, (char *)buf, ms->bytes_max - nbytes)) < 0) {
3111 + file_error(ms, errno, "cannot read `%s'", inname);
3116 - if (file_buffer(ms, fd, okstat ? &sb : NULL, inname, buf, CAST(size_t, nbytes)) == -1)
3117 + if (file_buffer(ms, stream, &sb, inname, buf, CAST(size_t, nbytes)) == -1)
3125 - close_and_restore(ms, inname, fd, &sb);
3132 return rv == 0 ? file_getbuffer(ms) : NULL;
3137 - if (file_buffer(ms, -1, NULL, NULL, buf, nb) == -1) {
3138 + if (file_buffer(ms, NULL, NULL, NULL, buf, nb) == -1) {
3141 return file_getbuffer(ms);
3174 file_magwarn(struct magic_set *ms, const char *f, ...)
3183 - if (ms->file)
3184 - (void) fprintf(stderr, "%s, %lu: ", ms->file,
3185 - CAST(unsigned long, ms->line));
3298 - if (!NOTMIME(ms) && root_storage)
3301 + if (!NOTMIME(ms) && root_storage)
3306 if (file_printf(ms, "%s%s",
3315 } else if (ms->flags & MAGIC_MIME_TYPE) {
3316 if (file_printf(ms, "application/CDFV2") == -1)
3321 m = cdf_file_property_info(ms, info, count, root_storage);
3390 check_fmt(struct magic_set *ms, const char *fmt)
3403 - file_regerror(&rx, rc, ms);
3445 varexpand(struct magic_set *ms, char *buf, size_t len, const char *str)
3451 - cp = strndup(RCAST(const char *, ms->search.s),
3452 + cp = estrndup(RCAST(const char *, ms->search.s),
3453 ms->search.rm_len);
3455 - file_oomem(ms, ms->search.rm_len);
3458 rval = file_printf(ms, F(ms, desc, "%s"),
3459 file_printable(sbuf, sizeof(sbuf), cp, ms->search.rm_len));
3468 if ((ms->flags & MAGIC_NODESC) == 0 &&
3469 - file_printf(ms, F(ms, m->desc, "%u"), offset) == -1)
3472 + file_printf(ms, F(ms, m->desc, "%u"), offset) == -1) {
3476 if (file_printf(ms, "%s", rbuf) == -1) {
3561 magiccheck(struct magic_set *ms, struct magic *m)
3574 - if (ms->search.s == NULL)
3583 - file_regerror(&rx, rc, ms);
3597 - size_t slen = ms->search.s_len;
3603 - file_error(ms, errno,
3608 - memcpy(copy, ms->search.s, slen);
3620 + haystack = zend_string_init(ms->search.s, ms->search.s_len, 0);
3646 + ms->search.s += Z_LVAL_P(offset); /* this is where the match starts */
3647 + ms->search.offset += Z_LVAL_P(offset); /* this is where the match starts as size_t */
3648 + ms->search.rm_len = Z_STRLEN_P(match) /* This is the length of the matched pattern */;
3668 - ms->search.s += CAST(int, pmatch.rm_so);
3669 - ms->search.offset += CAST(size_t, pmatch.rm_so);
3670 - ms->search.rm_len = CAST(size_t,
3680 - file_regerror(&rx, rc, ms);