libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

mime_extractor.c (3160B)


      1 /*
      2      This file is part of libextractor.
      3      Copyright (C) 2012 Vidyut Samanta and Christian Grothoff
      4 
      5      libextractor is free software; you can redistribute it and/or modify
      6      it under the terms of the GNU General Public License as published
      7      by the Free Software Foundation; either version 3, or (at your
      8      option) any later version.
      9 
     10      libextractor is distributed in the hope that it will be useful, but
     11      WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      General Public License for more details.
     14 
     15      You should have received a copy of the GNU General Public License
     16      along with libextractor; see the file COPYING.  If not, write to the
     17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18      Boston, MA 02110-1301, USA.
     19  */
     20 /**
     21  * @file plugins/mime_extractor.c
     22  * @brief plugin to determine mime types using libmagic (from 'file')
     23  * @author Christian Grothoff
     24  */
     25 #include "platform.h"
     26 #include "extractor.h"
     27 #include <magic.h>
     28 
     29 
     30 /**
     31  * Global handle to MAGIC data.
     32  */
     33 static magic_t magic;
     34 
     35 /**
     36  * Path we used for loading magic data, NULL is used for 'default'.
     37  */
     38 static char *magic_path;
     39 
     40 
     41 /**
     42  * Main entry method for the 'application/ogg' extraction plugin.  The
     43  * 'config' of the context can be used to specify an alternative magic
     44  * path.  If config is not given, the default magic path will be
     45  * used.
     46  *
     47  * @param ec extraction context provided to the plugin
     48  */
     49 void
     50 EXTRACTOR_mime_extract_method (struct EXTRACTOR_ExtractContext *ec)
     51 {
     52   void *buf;
     53   ssize_t ret;
     54   const char *mime;
     55 
     56   ret = ec->read (ec->cls,
     57                   &buf,
     58                   16 * 1024);
     59   if (-1 == ret)
     60     return;
     61   if ( ( (NULL == magic_path) &&
     62          (NULL != ec->config) ) ||
     63        ( (NULL != magic_path) &&
     64          (NULL == ec->config) ) ||
     65        ( (NULL != magic_path) &&
     66          (NULL != ec->config) &&
     67          (0 != strcmp (magic_path,
     68                        ec->config) )) )
     69   {
     70     if (NULL != magic_path)
     71       free (magic_path);
     72     magic_close (magic);
     73     magic = magic_open (MAGIC_MIME_TYPE);
     74     if (0 != magic_load (magic, ec->config))
     75     {
     76       /* FIXME: report errors? */
     77     }
     78     if (NULL != ec->config)
     79       magic_path = strdup (ec->config);
     80     else
     81       magic_path = NULL;
     82   }
     83   if (NULL == (mime = magic_buffer (magic, buf, ret)))
     84     return;
     85   ec->proc (ec->cls,
     86             "mime",
     87             EXTRACTOR_METATYPE_MIMETYPE,
     88             EXTRACTOR_METAFORMAT_UTF8,
     89             "text/plain",
     90             mime,
     91             strlen (mime) + 1);
     92 }
     93 
     94 
     95 /**
     96  * Constructor for the library.  Loads the magic file.
     97  */
     98 void __attribute__ ((constructor))
     99 mime_ltdl_init ()
    100 {
    101   magic = magic_open (MAGIC_MIME_TYPE);
    102   if (0 != magic_load (magic, magic_path))
    103   {
    104     /* FIXME: how to deal with errors? */
    105   }
    106 }
    107 
    108 
    109 /**
    110  * Destructor for the library, cleans up.
    111  */
    112 void __attribute__ ((destructor))
    113 mime_ltdl_fini ()
    114 {
    115   if (NULL != magic)
    116   {
    117     magic_close (magic);
    118     magic = NULL;
    119   }
    120   if (NULL != magic_path)
    121   {
    122     free (magic_path);
    123     magic_path = NULL;
    124   }
    125 }
    126 
    127 
    128 /* end of mime_extractor.c */