aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/mime_extractor.c
blob: b9fb3cfa17238e3e456614b9598b2b3b664a0aca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
     This file is part of libextractor.
     Copyright (C) 2012 Vidyut Samanta and Christian Grothoff

     libextractor is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     libextractor is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
 */
/**
 * @file plugins/mime_extractor.c
 * @brief plugin to determine mime types using libmagic (from 'file')
 * @author Christian Grothoff
 */
#include "platform.h"
#include "extractor.h"
#include <magic.h>


/**
 * Global handle to MAGIC data.
 */
static magic_t magic;

/**
 * Path we used for loading magic data, NULL is used for 'default'.
 */
static char *magic_path;
 

/**
 * Main entry method for the 'application/ogg' extraction plugin.  The
 * 'config' of the context can be used to specify an alternative magic
 * path.  If config is not given, the default magic path will be
 * used.
 *
 * @param ec extraction context provided to the plugin
 */
void
EXTRACTOR_mime_extract_method (struct EXTRACTOR_ExtractContext *ec)
{
  void *buf;
  ssize_t ret;
  const char *mime;

  ret = ec->read (ec->cls,
		  &buf,
		  16 * 1024);
  if (-1 == ret)
    return;
  if ( ( (NULL == magic_path) &&
	 (NULL != ec->config) ) ||
       ( (NULL != magic_path) &&
	 (NULL == ec->config) ) ||
       ( (NULL != magic_path) &&
	 (NULL != ec->config) &&
	 (0 != strcmp (magic_path,
		       ec->config) )) )
    {
      if (NULL != magic_path)
	free (magic_path);		
      magic_close (magic);
      magic = magic_open (MAGIC_MIME_TYPE);
      if (0 != magic_load (magic, ec->config))
	{
	  /* FIXME: report errors? */
	}
      if (NULL != ec->config)
	magic_path = strdup (ec->config);
      else
	magic_path = NULL;
    }
  if (NULL == (mime = magic_buffer (magic, buf, ret)))
    return;
  ec->proc (ec->cls,
	    "mime",
	    EXTRACTOR_METATYPE_MIMETYPE,
	    EXTRACTOR_METAFORMAT_UTF8,
	    "text/plain",
	    mime,
	    strlen (mime) + 1);
}


/**
 * Constructor for the library.  Loads the magic file.
 */
void __attribute__ ((constructor)) 
mime_ltdl_init () 
{
  magic = magic_open (MAGIC_MIME_TYPE);
  if (0 != magic_load (magic, magic_path))
    {
      /* FIXME: how to deal with errors? */
    }
}


/**
 * Destructor for the library, cleans up.
 */
void __attribute__ ((destructor)) 
mime_ltdl_fini () 
{
  if (NULL != magic)
    {
      magic_close (magic);
      magic = NULL;
    }
  if (NULL != magic_path)
    {
      free (magic_path);
      magic_path = NULL;
    }
}

/* end of mime_extractor.c */