Lines Matching refs:ms

67 file_clearbuf(struct magic_set *ms)  in file_clearbuf()  argument
69 free(ms->o.buf); in file_clearbuf()
70 ms->o.buf = NULL; in file_clearbuf()
71 ms->o.blen = 0; in file_clearbuf()
133 file_vprintf(struct magic_set *ms, const char *fmt, va_list ap) in file_vprintf() argument
139 if (ms->event_flags & EVENT_HAD_ERR) in file_vprintf()
143 file_clearbuf(ms); in file_vprintf()
144 file_error(ms, 0, "Bad magic format `%s' (%s)", fmt, tbuf); in file_vprintf()
149 if (len < 0 || (size_t)len > 1024 || len + ms->o.blen > 1024 * 1024) { in file_vprintf()
150 size_t blen = ms->o.blen; in file_vprintf()
152 file_clearbuf(ms); in file_vprintf()
153 file_error(ms, 0, "Output buffer space exceeded %d+%" in file_vprintf()
158 if (ms->o.buf != NULL) { in file_vprintf()
159 len = asprintf(&newstr, "%s%s", ms->o.buf, buf); in file_vprintf()
163 free(ms->o.buf); in file_vprintf()
166 ms->o.buf = buf; in file_vprintf()
167 ms->o.blen = len; in file_vprintf()
170 file_clearbuf(ms); in file_vprintf()
171 file_error(ms, errno, "vasprintf failed"); in file_vprintf()
176 file_printf(struct magic_set *ms, const char *fmt, ...) in file_printf() argument
182 rv = file_vprintf(ms, fmt, ap); in file_printf()
193 file_error_core(struct magic_set *ms, int error, const char *f, va_list va, in file_error_core() argument
197 if (ms->event_flags & EVENT_HAD_ERR) in file_error_core()
200 file_clearbuf(ms); in file_error_core()
201 (void)file_printf(ms, "line %" SIZE_T_FORMAT "u:", lineno); in file_error_core()
203 if (ms->o.buf && *ms->o.buf) in file_error_core()
204 (void)file_printf(ms, " "); in file_error_core()
205 (void)file_vprintf(ms, f, va); in file_error_core()
207 (void)file_printf(ms, " (%s)", strerror(error)); in file_error_core()
208 ms->event_flags |= EVENT_HAD_ERR; in file_error_core()
209 ms->error = error; in file_error_core()
214 file_error(struct magic_set *ms, int error, const char *f, ...) in file_error() argument
218 file_error_core(ms, error, f, va, 0); in file_error()
227 file_magerror(struct magic_set *ms, const char *f, ...) in file_magerror() argument
231 file_error_core(ms, 0, f, va, ms->line); in file_magerror()
236 file_oomem(struct magic_set *ms, size_t len) in file_oomem() argument
238 file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes", in file_oomem()
243 file_badseek(struct magic_set *ms) in file_badseek() argument
245 file_error(ms, errno, "error seeking"); in file_badseek()
249 file_badread(struct magic_set *ms) in file_badread() argument
251 file_error(ms, errno, "error reading"); in file_badread()
258 file_separator(struct magic_set *ms) in file_separator() argument
260 return file_printf(ms, FILE_SEPARATOR); in file_separator()
264 trim_separator(struct magic_set *ms) in trim_separator() argument
268 if (ms->o.buf == NULL) in trim_separator()
271 l = strlen(ms->o.buf); in trim_separator()
276 if (strcmp(ms->o.buf + l, FILE_SEPARATOR) != 0) in trim_separator()
279 ms->o.buf[l] = '\0'; in trim_separator()
283 checkdone(struct magic_set *ms, int *rv) in checkdone() argument
285 if ((ms->flags & MAGIC_CONTINUE) == 0) in checkdone()
287 if (file_separator(ms) == -1) in checkdone()
293 file_default(struct magic_set *ms, size_t nb) in file_default() argument
295 if (ms->flags & MAGIC_MIME) { in file_default()
296 if ((ms->flags & MAGIC_MIME_TYPE) && in file_default()
297 file_printf(ms, "application/%s", in file_default()
302 if (ms->flags & MAGIC_APPLE) { in file_default()
303 if (file_printf(ms, "UNKNUNKN") == -1) in file_default()
307 if (ms->flags & MAGIC_EXTENSION) { in file_default()
308 if (file_printf(ms, "???") == -1) in file_default()
323 file_buffer(struct magic_set *ms, int fd, struct stat *st, in file_buffer() argument
336 ms->mode = b.st.st_mode; in file_buffer()
346 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) { in file_buffer()
347 looks_text = file_encoding(ms, &b, NULL, 0, in file_buffer()
352 if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) { in file_buffer()
353 m = file_os2_apptype(ms, inname, &b); in file_buffer()
354 if ((ms->flags & MAGIC_DEBUG) != 0) in file_buffer()
368 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) { in file_buffer()
369 m = file_zmagic(ms, &b, inname); in file_buffer()
370 if ((ms->flags & MAGIC_DEBUG) != 0) in file_buffer()
378 if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) { in file_buffer()
379 m = file_is_tar(ms, &b); in file_buffer()
380 if ((ms->flags & MAGIC_DEBUG) != 0) in file_buffer()
383 if (checkdone(ms, &rv)) in file_buffer()
389 if ((ms->flags & MAGIC_NO_CHECK_JSON) == 0) { in file_buffer()
390 m = file_is_json(ms, &b); in file_buffer()
391 if ((ms->flags & MAGIC_DEBUG) != 0) in file_buffer()
394 if (checkdone(ms, &rv)) in file_buffer()
400 if ((ms->flags & MAGIC_NO_CHECK_CSV) == 0) { in file_buffer()
401 m = file_is_csv(ms, &b, looks_text, code); in file_buffer()
402 if ((ms->flags & MAGIC_DEBUG) != 0) in file_buffer()
405 if (checkdone(ms, &rv)) in file_buffer()
411 if ((ms->flags & MAGIC_NO_CHECK_SIMH) == 0) { in file_buffer()
412 m = file_is_simh(ms, &b); in file_buffer()
413 if ((ms->flags & MAGIC_DEBUG) != 0) in file_buffer()
416 if (checkdone(ms, &rv)) in file_buffer()
422 if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) { in file_buffer()
423 m = file_trycdf(ms, &b); in file_buffer()
424 if ((ms->flags & MAGIC_DEBUG) != 0) in file_buffer()
427 if (checkdone(ms, &rv)) in file_buffer()
432 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && nb > 5 && fd != -1) { in file_buffer()
444 if ((pb = file_push_buffer(ms)) == NULL) in file_buffer()
447 rv = file_tryelf(ms, &b); in file_buffer()
448 rbuf = file_pop_buffer(ms, pb); in file_buffer()
453 if ((ms->flags & MAGIC_DEBUG) != 0) in file_buffer()
459 if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) { in file_buffer()
460 m = file_softmagic(ms, &b, NULL, NULL, BINTEST, looks_text); in file_buffer()
461 if ((ms->flags & MAGIC_DEBUG) != 0) in file_buffer()
464 if (file_printf(ms, "%s", rbuf) == -1) in file_buffer()
468 if (checkdone(ms, &rv)) in file_buffer()
474 if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) { in file_buffer()
476 m = file_ascmagic(ms, &b, looks_text); in file_buffer()
477 if ((ms->flags & MAGIC_DEBUG) != 0) in file_buffer()
488 rv = file_default(ms, nb); in file_buffer()
490 if (file_printf(ms, "%s", def) == -1) in file_buffer()
494 trim_separator(ms); in file_buffer()
495 if ((ms->flags & MAGIC_MIME_ENCODING) != 0) { in file_buffer()
496 if (ms->flags & MAGIC_MIME_TYPE) in file_buffer()
497 if (file_printf(ms, "; charset=") == -1) in file_buffer()
499 if (file_printf(ms, "%s", code_mime) == -1) in file_buffer()
515 file_reset(struct magic_set *ms, int checkloaded) in file_reset() argument
517 if (checkloaded && ms->mlist[0] == NULL) { in file_reset()
518 file_error(ms, 0, "no magic files loaded"); in file_reset()
521 file_clearbuf(ms); in file_reset()
522 if (ms->o.pbuf) { in file_reset()
523 free(ms->o.pbuf); in file_reset()
524 ms->o.pbuf = NULL; in file_reset()
526 ms->event_flags &= ~EVENT_HAD_ERR; in file_reset()
527 ms->error = -1; in file_reset()
540 file_getbuffer(struct magic_set *ms) in file_getbuffer() argument
545 if (ms->event_flags & EVENT_HAD_ERR) in file_getbuffer()
548 if (ms->flags & MAGIC_RAW) in file_getbuffer()
549 return ms->o.buf; in file_getbuffer()
551 if (ms->o.buf == NULL) in file_getbuffer()
555 len = strlen(ms->o.buf); in file_getbuffer()
557 file_oomem(ms, len); in file_getbuffer()
561 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) { in file_getbuffer()
562 file_oomem(ms, psize); in file_getbuffer()
565 ms->o.pbuf = pbuf; in file_getbuffer()
576 np = ms->o.pbuf; in file_getbuffer()
577 op = ms->o.buf; in file_getbuffer()
602 return ms->o.pbuf; in file_getbuffer()
606 for (np = ms->o.pbuf, op = ms->o.buf; *op;) { in file_getbuffer()
614 return ms->o.pbuf; in file_getbuffer()
618 file_check_mem(struct magic_set *ms, unsigned int level) in file_check_mem() argument
622 if (level >= ms->c.len) { in file_check_mem()
623 len = (ms->c.len = 20 + level) * sizeof(*ms->c.li); in file_check_mem()
624 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ? in file_check_mem()
626 realloc(ms->c.li, len)); in file_check_mem()
627 if (ms->c.li == NULL) { in file_check_mem()
628 file_oomem(ms, len); in file_check_mem()
632 ms->c.li[level].got_match = 0; in file_check_mem()
634 ms->c.li[level].last_match = 0; in file_check_mem()
635 ms->c.li[level].last_cond = COND_NONE; in file_check_mem()
641 file_printedlen(const struct magic_set *ms) in file_printedlen() argument
643 return ms->o.blen; in file_printedlen()
647 file_replace(struct magic_set *ms, const char *pat, const char *rep) in file_replace() argument
652 rc = file_regcomp(ms, &rx, pat, REG_EXTENDED); in file_replace()
656 while (file_regexec(ms, &rx, ms->o.buf, 1, &rm, 0) == 0) { in file_replace()
657 ms->o.buf[rm.rm_so] = '\0'; in file_replace()
658 if (file_printf(ms, "%s%s", rep, in file_replace()
659 rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1) in file_replace()
671 check_regex(struct magic_set *ms, const char *pat) in check_regex() argument
683 file_magwarn(ms, in check_regex()
686 file_printable(ms, sbuf, sizeof(sbuf), pat, len)); in check_regex()
707 file_magwarn(ms, in check_regex()
709 c, file_printable(ms, sbuf, sizeof(sbuf), pat, len)); in check_regex()
714 file_magwarn(ms, "bounds too large %ld in regex `%s'", l, pat); in check_regex()
719 file_regcomp(struct magic_set *ms file_locale_used, file_regex_t *rx, in file_regcomp()
722 if (check_regex(ms, pat) == -1) in file_regcomp()
726 locale_t old = uselocale(ms->c_lc_ctype); in file_regcomp()
741 if (rc > 0 && (ms->flags & MAGIC_CHECK)) { in file_regcomp()
745 file_magerror(ms, "regex error %d for `%s', (%s)", rc, in file_regcomp()
746 file_printable(ms, buf, sizeof(buf), pat, strlen(pat)), in file_regcomp()
754 file_regexec(struct magic_set *ms file_locale_used, file_regex_t *rx, in file_regexec()
758 locale_t old = uselocale(ms->c_lc_ctype); in file_regexec()
785 file_push_buffer(struct magic_set *ms) in file_push_buffer() argument
789 if (ms->event_flags & EVENT_HAD_ERR) in file_push_buffer()
795 pb->buf = ms->o.buf; in file_push_buffer()
796 pb->blen = ms->o.blen; in file_push_buffer()
797 pb->offset = ms->offset; in file_push_buffer()
799 ms->o.buf = NULL; in file_push_buffer()
800 ms->o.blen = 0; in file_push_buffer()
801 ms->offset = 0; in file_push_buffer()
807 file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb) in file_pop_buffer() argument
811 if (ms->event_flags & EVENT_HAD_ERR) { in file_pop_buffer()
817 rbuf = ms->o.buf; in file_pop_buffer()
819 ms->o.buf = pb->buf; in file_pop_buffer()
820 ms->o.blen = pb->blen; in file_pop_buffer()
821 ms->offset = pb->offset; in file_pop_buffer()
831 file_printable(struct magic_set *ms, char *buf, size_t bufsiz, in file_printable() argument
839 if ((ms->flags & MAGIC_RAW) != 0 || isprint(*s)) { in file_printable()