📄 mime_magic.c
字号:
/* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2007 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Hartmut Holzgraefe <hholzgra@php.net> | +----------------------------------------------------------------------+ $Id: mime_magic.c,v 1.13.2.13.2.3 2007/01/01 09:46:44 sebastian Exp $ This module contains a lot of stuff taken from Apache mod_mime_magic, so the license section is a little bit longer than usual: ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. *//* * mod_mime_magic: MIME type lookup via file magic numbers * Copyright (c) 1996-1997 Cisco Systems, Inc. * * This software was submitted by Cisco Systems to the Apache Group in July * 1997. Future revisions and derivatives of this source code must * acknowledge Cisco Systems as the original contributor of this module. * All other licensing and usage conditions are those of the Apache Group. * * Some of this code is derived from the free version of the file command * originally posted to comp.sources.unix. Copyright info for that program * is included below as required. * --------------------------------------------------------------------------- * - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin. * * This software is not subject to any license of the American Telephone and * Telegraph Company or of the Regents of the University of California. * * Permission is granted to anyone to use this software for any purpose on any * computer system, and to alter it and redistribute it freely, subject to * the following restrictions: * * 1. The author is not responsible for the consequences of use of this * software, no matter how awful, even if they arise from flaws in it. * * 2. The origin of this software must not be misrepresented, either by * explicit claim or by omission. Since few users ever read sources, credits * must appear in the documentation. * * 3. Altered versions must be plainly marked as such, and must not be * misrepresented as being the original software. Since few users ever read * sources, credits must appear in the documentation. * * 4. This notice may not be removed or altered. * ------------------------------------------------------------------------- * * For compliance with Mr Darwin's terms: this has been very significantly * modified from the free "file" command. * - all-in-one file for compilation convenience when moving from one * version of Apache to the next. * - Memory allocation is done through the Apache API's pool structure. * - All functions have had necessary Apache API request or server * structures passed to them where necessary to call other Apache API * routines. (i.e. usually for logging, files, or memory allocation in * itself or a called function.) * - struct magic has been converted from an array to a single-ended linked * list because it only grows one record at a time, it's only accessed * sequentially, and the Apache API has no equivalent of realloc(). * - Functions have been changed to get their parameters from the server * configuration instead of globals. (It should be reentrant now but has * not been tested in a threaded environment.) * - Places where it used to print results to stdout now saves them in a * list where they're used to set the MIME type in the Apache request * record. * - Command-line flags have been removed since they will never be used here. * * Ian Kluft <ikluft@cisco.com> * Engineering Information Framework * Central Engineering * Cisco Systems, Inc. * San Jose, CA, USA * * Initial installation July/August 1996 * Misc bug fixes May 1997 * Submission to Apache Group July 1997 * */#ifdef HAVE_CONFIG_H#include "config.h"#endif #include "php.h"#include "php_streams.h"#include "php_ini.h"#include "ext/standard/info.h"#include "php_mime_magic.h"#include "phpmimemagic.h"#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#ifdef PHP_WIN32#define PHP_MIME_MAGIC_FILE_PATH PHP_PREFIX "\\magic.mime"#endif#define MODNAME "mime_magic"static int apprentice(void);static int ascmagic(unsigned char *, int);static int is_tar(unsigned char *, int);static int softmagic(unsigned char *, int);static void tryit(unsigned char *, int, int);static int getvalue(struct magic *, char **);static int hextoint(int);static char *getstr(char *, char *, int, int *);static int parse(char *, int);static int match(unsigned char *, int);static int mget(union VALUETYPE *, unsigned char *, struct magic *, int);static int mcheck(union VALUETYPE *, struct magic *);static void mprint(union VALUETYPE *, struct magic *);static int mconvert(union VALUETYPE *, struct magic *);static int magic_rsl_get(char **, char **);static int magic_process(char * TSRMLS_DC);static long from_oct(int, char *);static int fsmagic(char *fn TSRMLS_DC);#if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)static int zmagic(unsigned char *, int);#endifstatic magic_req_rec *magic_set_config(void);static void magic_free_config(magic_req_rec *);ZEND_DECLARE_MODULE_GLOBALS(mime_magic)/* True global resources - no need for thread safety here */static magic_server_config_rec mime_global;/* {{{ mime_magic_functions[] */function_entry mime_magic_functions[] = { PHP_FE(mime_content_type, NULL) {NULL, NULL, NULL} };/* }}} *//* {{{ mime_magic_module_entry */zend_module_entry mime_magic_module_entry = {#if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER,#endif "mime_magic", mime_magic_functions, PHP_MINIT(mime_magic), PHP_MSHUTDOWN(mime_magic), NULL, NULL, PHP_MINFO(mime_magic),#if ZEND_MODULE_API_NO >= 20010901 "0.1", #endif STANDARD_MODULE_PROPERTIES};/* }}} */#ifdef COMPILE_DL_MIME_MAGICZEND_GET_MODULE(mime_magic)#endif/* {{{ PHP_INI */PHP_INI_BEGIN()STD_PHP_INI_ENTRY("mime_magic.magicfile", PHP_MIME_MAGIC_FILE_PATH, PHP_INI_SYSTEM, OnUpdateString, magicfile, zend_mime_magic_globals, mime_magic_globals)PHP_INI_END()/* }}} *//* {{{ php_mime_magic_init_globals */static void php_mime_magic_init_globals(zend_mime_magic_globals *mime_magic_globals){ mime_global.magicfile = NULL;}/* }}} *//* {{{ PHP_MINIT_FUNCTION */PHP_MINIT_FUNCTION(mime_magic){ ZEND_INIT_MODULE_GLOBALS(mime_magic, php_mime_magic_init_globals, NULL); REGISTER_INI_ENTRIES(); mime_global.magicfile = MIME_MAGIC_G(magicfile); if(mime_global.magicfile) { apprentice(); } return SUCCESS;}/* }}} *//* {{{ PHP_MSHUTDOWN_FUNCTION */PHP_MSHUTDOWN_FUNCTION(mime_magic){ UNREGISTER_INI_ENTRIES(); if (mime_global.magic != NULL && mime_global.magic != (struct magic *) -1) { struct magic *iter = mime_global.magic; while (iter != NULL) { struct magic *iter_next = iter->next; free(iter); iter = iter_next; } } return SUCCESS;}/* }}} *//* {{{ PHP_MINFO_FUNCTION */PHP_MINFO_FUNCTION(mime_magic){ php_info_print_table_start(); php_info_print_table_header(2, "mime_magic support", "enabled"); php_info_print_table_end(); DISPLAY_INI_ENTRIES();}/* }}} *//* {{{ proto string mime_content_type(string filename) Return content-type for file */PHP_FUNCTION(mime_content_type){ char *filename = NULL; int filename_len; magic_server_config_rec *conf = &mime_global; char *content_type=NULL, *content_encoding=NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) { return; } if (conf->magic == (struct magic *)-1) { php_error(E_ERROR, MODNAME " could not be initialized, magic file %s is not available", conf->magicfile); RETURN_FALSE; } if(!conf->magic) { php_error(E_WARNING, MODNAME " not initialized"); RETURN_FALSE; } MIME_MAGIC_G(req_dat) = magic_set_config(); if(MIME_MAGIC_OK != magic_process(filename TSRMLS_CC)) { RETVAL_FALSE; } else if(MIME_MAGIC_OK != magic_rsl_get(&content_type, &content_encoding)) { RETVAL_FALSE; } else { RETVAL_STRING(content_type, 1); } if(content_type) efree(content_type); if(content_encoding) efree(content_encoding); magic_free_config(MIME_MAGIC_G(req_dat));}/* }}} */#define EATAB {while (isspace((unsigned char) *l)) ++l;}/* * apprentice - load configuration from the magic file r * API request record */static int apprentice(void){ FILE *f; char line[BUFSIZ + 1]; int errs = 0; int lineno; char *fname; magic_server_config_rec *conf = &mime_global; TSRMLS_FETCH(); fname = conf->magicfile; /* todo cwd? */ f = fopen(fname, "rb"); if (f == NULL) { conf->magic = (struct magic *)-1; return -1; } /* set up the magic list (empty) */ conf->magic = conf->last = NULL; /* parse it */ for (lineno = 1; fgets(line, BUFSIZ, f) != NULL; lineno++) { int ws_offset; /* delete newline */ if (line[0]) { line[strlen(line) - 1] = '\0'; } /* skip leading whitespace */ ws_offset = 0; while (line[ws_offset] && isspace(line[ws_offset])) { ws_offset++; } /* skip blank lines */ if (line[ws_offset] == 0) { continue; } /* comment, do not parse */ if (line[ws_offset] == '#') continue; /* parse it */ if (parse(line + ws_offset, lineno) != 0) ++errs; } (void) fclose(f); return (errs ? -1 : 0);}/* * extend the sign bit if the comparison is to be signed */static unsigned long signextend(struct magic *m, unsigned long v){ if (!(m->flag & UNSIGNED)) switch (m->type) { /* * Do not remove the casts below. They are vital. When later * compared with the data, the sign extension must have happened. */ case BYTE: v = (char) v; break; case SHORT: case BESHORT: case LESHORT: v = (short) v; break; case DATE: case BEDATE: case LEDATE: case LONG: case BELONG: case LELONG: v = (long) v; break; case STRING: break; default: php_error(E_WARNING, MODNAME ": can't happen: m->type=%d", m->type); return -1; } return v;}/* * parse one line from magic file, put into magic[index++] if valid */static int parse(char *l, int lineno){ struct magic *m; char *t, *s; magic_server_config_rec *conf = &mime_global; /* allocate magic structure entry */ m = (struct magic *) calloc(1, sizeof(struct magic)); /* append to linked list */ m->next = NULL; if (!conf->magic || !conf->last) { conf->magic = conf->last = m; } else { conf->last->next = m; conf->last = m; } /* set values in magic structure */ m->flag = 0; m->cont_level = 0; m->lineno = lineno;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -