--- embedaddon/php/ext/fileinfo/libmagic/funcs.c 2012/02/21 23:47:56 1.1 +++ embedaddon/php/ext/fileinfo/libmagic/funcs.c 2012/05/29 12:34:39 1.1.1.2 @@ -27,7 +27,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: funcs.c,v 1.53 2009/04/07 11:07:00 christos Exp $") +FILE_RCSID("@(#)$File: funcs.c,v 1.60 2011/12/08 12:38:24 rrt Exp $") #endif /* lint */ #include "magic.h" @@ -46,6 +46,12 @@ FILE_RCSID("@(#)$File: funcs.c,v 1.53 2009/04/07 11:07 # define SIZE_MAX ((size_t) -1) #endif +#ifndef PREG_OFFSET_CAPTURE +# define PREG_OFFSET_CAPTURE (1<<8) +#endif + +extern public void convert_libmagic_pattern(zval *pattern, int options); + /* * Like printf, only we append to a buffer. */ @@ -79,19 +85,17 @@ file_printf(struct magic_set *ms, const char *fmt, ... /*VARARGS*/ private void file_error_core(struct magic_set *ms, int error, const char *f, va_list va, - uint32_t lineno) + size_t lineno) { char *buf = NULL; /* Only the first error is ok */ - if (ms->event_flags & EVENT_HAD_ERR) { + if (ms->event_flags & EVENT_HAD_ERR) return; - } - if (lineno != 0) { efree(ms->o.buf); ms->o.buf = NULL; - file_printf(ms, "line %u: ", lineno); + file_printf(ms, "line %" SIZE_T_FORMAT "u: ", lineno); } vspprintf(&buf, 0, f, va); @@ -137,7 +141,8 @@ file_magerror(struct magic_set *ms, const char *f, ... protected void file_oomem(struct magic_set *ms, size_t len) { - file_error(ms, errno, "cannot allocate %zu bytes", len); + file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes", + len); } protected void @@ -158,7 +163,7 @@ file_buffer(struct magic_set *ms, php_stream *stream, { int m = 0, rv = 0, looks_text = 0; int mime = ms->flags & MAGIC_MIME; - const unsigned char *ubuf = buf; + const unsigned char *ubuf = CAST(const unsigned char *, buf); unichar *u8buf = NULL; size_t ulen; const char *code = NULL; @@ -205,9 +210,8 @@ file_buffer(struct magic_set *ms, php_stream *stream, if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "zmagic %d\n", m); goto done; - } + } #endif - /* Check if we have a tar file */ if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) if ((m = file_is_tar(ms, ubuf, nb)) != 0) { @@ -231,7 +235,8 @@ file_buffer(struct magic_set *ms, php_stream *stream, /* try soft magic tests */ if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) - if ((m = file_softmagic(ms, ubuf, nb, BINTEST)) != 0) { + if ((m = file_softmagic(ms, ubuf, nb, BINTEST, + looks_text)) != 0) { if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "softmagic %d\n", m); #ifdef BUILTIN_ELF @@ -255,10 +260,10 @@ file_buffer(struct magic_set *ms, php_stream *stream, goto done; } - /* try text properties (and possibly text tokens) */ + /* try text properties */ if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) { - if ((m = file_ascmagic(ms, ubuf, nb)) != 0) { + if ((m = file_ascmagic(ms, ubuf, nb, looks_text)) != 0) { if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "ascmagic %d\n", m); goto done; @@ -268,7 +273,8 @@ file_buffer(struct magic_set *ms, php_stream *stream, if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) { if (looks_text == 0) if ((m = file_ascmagic_with_encoding( ms, ubuf, - nb, u8buf, ulen, code, type)) != 0) { + nb, u8buf, ulen, code, type, looks_text)) + != 0) { if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "ascmagic/enc %d\n", m); @@ -291,8 +297,7 @@ file_buffer(struct magic_set *ms, php_stream *stream, if (file_printf(ms, "%s", code_mime) == -1) rv = -1; } - if (u8buf) - free(u8buf); + free(u8buf); if (rv) return rv; @@ -390,9 +395,9 @@ file_getbuffer(struct magic_set *ms) } #endif - for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) { + for (np = ms->o.pbuf, op = ms->o.buf; *op;) { if (isprint((unsigned char)*op)) { - *np++ = *op; + *np++ = *op++; } else { OCTALIFY(np, op); } @@ -417,3 +422,58 @@ file_check_mem(struct magic_set *ms, unsigned int leve #endif /* ENABLE_CONDITIONALS */ return 0; } + +protected size_t +file_printedlen(const struct magic_set *ms) +{ + return ms->o.buf == NULL ? 0 : strlen(ms->o.buf); +} + + +protected int +file_replace(struct magic_set *ms, const char *pat, const char *rep) +{ + zval *patt; + int opts = 0; + pcre_cache_entry *pce; + char *res; + zval *repl; + int res_len, rep_cnt; + TSRMLS_FETCH(); + + MAKE_STD_ZVAL(patt); + ZVAL_STRINGL(patt, pat, strlen(pat), 0); + opts |= PCRE_MULTILINE; + convert_libmagic_pattern(patt, opts); +#if (PHP_MAJOR_VERSION < 6) + if ((pce = pcre_get_compiled_regex_cache(Z_STRVAL_P(patt), Z_STRLEN_P(patt) TSRMLS_CC)) == NULL) { +#else + if ((pce = pcre_get_compiled_regex_cache(IS_STRING, Z_STRVAL_P(patt), Z_STRLEN_P(patt) TSRMLS_CC)) == NULL) { +#endif + zval_dtor(patt); + FREE_ZVAL(patt); + return -1; + } + + MAKE_STD_ZVAL(repl); + ZVAL_STRINGL(repl, rep, strlen(rep), 0); + + res = php_pcre_replace_impl(pce, ms->o.buf, strlen(ms->o.buf), repl, + 0, &res_len, -1, &rep_cnt TSRMLS_CC); + + FREE_ZVAL(repl); + zval_dtor(patt); + FREE_ZVAL(patt); + + if (NULL == res) { + return -1; + } + + strncpy(ms->o.buf, res, res_len); + ms->o.buf[res_len] = '\0'; + + efree(res); + + return rep_cnt; +} +