libextractor

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

mime_extractor.c (3352B)


      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
     53 EXTRACTOR_mime_extract_method (struct EXTRACTOR_ExtractContext *ec)
     54 {
     55   void *buf;
     56   ssize_t ret;
     57   const char *mime;
     58 
     59   ret = ec->read (ec->cls,
     60                   &buf,
     61                   16 * 1024);
     62   if (-1 == ret)
     63     return;
     64   if ( ( (NULL == magic_path) &&
     65          (NULL != ec->config) ) ||
     66        ( (NULL != magic_path) &&
     67          (NULL == ec->config) ) ||
     68        ( (NULL != magic_path) &&
     69          (NULL != ec->config) &&
     70          (0 != strcmp (magic_path,
     71                        ec->config) )) )
     72   {
     73     if (NULL != magic_path)
     74       free (magic_path);
     75     magic_close (magic);
     76     magic = magic_open (MAGIC_MIME_TYPE);
     77     if (0 != magic_load (magic, ec->config))
     78     {
     79       /* FIXME: report errors? */
     80     }
     81     if (NULL != ec->config)
     82       magic_path = strdup (ec->config);
     83     else
     84       magic_path = NULL;
     85   }
     86   if (NULL == (mime = magic_buffer (magic, buf, ret)))
     87     return;
     88   ec->proc (ec->cls,
     89             "mime",
     90             EXTRACTOR_METATYPE_MIMETYPE,
     91             EXTRACTOR_METAFORMAT_UTF8,
     92             "text/plain",
     93             mime,
     94             strlen (mime) + 1);
     95 }
     96 
     97 
     98 /**
     99  * Constructor for the library.  Loads the magic file.
    100  */
    101 void __attribute__ ((constructor))
    102 mime_ltdl_init (void);
    103 
    104 void __attribute__ ((constructor))
    105 mime_ltdl_init ()
    106 {
    107   magic = magic_open (MAGIC_MIME_TYPE);
    108   if (0 != magic_load (magic, magic_path))
    109   {
    110     /* FIXME: how to deal with errors? */
    111   }
    112 }
    113 
    114 
    115 /**
    116  * Destructor for the library, cleans up.
    117  */
    118 void __attribute__ ((destructor))
    119 mime_ltdl_fini (void);
    120 
    121 void __attribute__ ((destructor))
    122 mime_ltdl_fini ()
    123 {
    124   if (NULL != magic)
    125   {
    126     magic_close (magic);
    127     magic = NULL;
    128   }
    129   if (NULL != magic_path)
    130   {
    131     free (magic_path);
    132     magic_path = NULL;
    133   }
    134 }
    135 
    136 
    137 /* end of mime_extractor.c */