libextractor  1.11
About: GNU libextractor is a library used to extract meta-data from files of arbitrary type.
  Fossies Dox: libextractor-1.11.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

mime_extractor.c
Go to the documentation of this file.
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
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",
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))
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))
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 */
@ EXTRACTOR_METAFORMAT_UTF8
Definition: extractor.h:102
#define NULL
Definition: getopt1.c:60
@ EXTRACTOR_METATYPE_MIMETYPE
Definition: extractor.h:129
void EXTRACTOR_mime_extract_method(struct EXTRACTOR_ExtractContext *ec)
void mime_ltdl_fini()
static char * magic_path
void mime_ltdl_init()
static magic_t magic
plaform specifics
EXTRACTOR_MetaDataProcessor proc
Definition: extractor.h:525
ssize_t(* read)(void *cls, void **data, size_t size)
Definition: extractor.h:494