aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/gif_extractor.c
blob: 8ee580796305f3e1555b96685dcadd527de93c90 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
     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/gif_extractor.c
 * @brief plugin to support GIF files
 * @author Christian Grothoff
 */
#include "platform.h"
#include "extractor.h"
#include <gif_lib.h>


/**
 * Callback invoked by libgif to read data.
 *
 * @param ft the file handle, including our extract context
 * @param bt where to write the data
 * @param arg number of bytes to read
 * @return -1 on error, otherwise number of bytes read
 */
static int
gif_read_func (GifFileType *ft,
	       GifByteType *bt,
	       int arg)
{
  struct EXTRACTOR_ExtractContext *ec = ft->UserData;
  void *data;
  ssize_t ret;

  ret = ec->read (ec->cls,
		  &data,
		  arg);
  if (-1 == ret)
    return -1;
  memcpy (bt, data, ret);
  return ret;
}


/**
 * Main entry method for the 'image/gif' extraction plugin.
 *
 * @param ec extraction context provided to the plugin
 */
void
EXTRACTOR_gif_extract_method (struct EXTRACTOR_ExtractContext *ec)
{
  GifFileType *gif_file;
  GifRecordType gif_type;
  GifByteType *ext;
  int et;
  char dims[128];
#if defined (GIF_LIB_VERSION) || GIFLIB_MAJOR <= 4
  if (NULL == (gif_file = DGifOpen (ec, &gif_read_func)))
    return; /* not a GIF */
#else
  int gif_error;

  gif_error = 0;
  gif_file = DGifOpen (ec, &gif_read_func, &gif_error);
  if (gif_file == NULL || gif_error != 0)
  {
    if (gif_file != NULL)
#if GIFLIB_MAJOR < 5 || GIFLIB_MINOR < 1
      EGifCloseFile (gif_file);
#else
      EGifCloseFile (gif_file, NULL);
#endif
    return; /* not a GIF */
  }
#endif
  if (0 !=
      ec->proc (ec->cls,
		"gif",
		EXTRACTOR_METATYPE_MIMETYPE,
		EXTRACTOR_METAFORMAT_UTF8,
		"text/plain",
		"image/gif",
		strlen ("image/gif") + 1))
    return;
  snprintf (dims,
	    sizeof (dims),
	    "%dx%d",
	    gif_file->SHeight,
	    gif_file->SWidth);
  if (0 !=
      ec->proc (ec->cls,
		"gif",
		EXTRACTOR_METATYPE_IMAGE_DIMENSIONS,
		EXTRACTOR_METAFORMAT_UTF8,
		"text/plain",
		dims,
		strlen (dims) + 1))
    return;
  while (1)
    {
      if (GIF_OK !=
	  DGifGetRecordType (gif_file,
			     &gif_type))
	break;
      if (UNDEFINED_RECORD_TYPE == gif_type)
	break;
      if (EXTENSION_RECORD_TYPE != gif_type)
	continue;
      if (GIF_OK !=
	  DGifGetExtension (gif_file, &et, &ext))
	continue;
      if (NULL == ext)
        continue;
      if (COMMENT_EXT_FUNC_CODE == et)
	{
	  ec->proc (ec->cls,
		    "gif",
		    EXTRACTOR_METATYPE_COMMENT,
		    EXTRACTOR_METAFORMAT_C_STRING,
		    "text/plain",
		    (char*) &ext[1],
		    (uint8_t) ext[0]);
	  break;
	}
      while ( (GIF_ERROR !=
	       DGifGetExtensionNext(gif_file, &ext)) &&
	      (NULL != ext) ) ; /* keep going */
    }
#if defined (GIF_LIB_VERSION) || GIFLIB_MAJOR < 5 || GIFLIB_MINOR < 1
  DGifCloseFile (gif_file);
#else
  DGifCloseFile (gif_file, NULL);
#endif
}

/* end of gif_extractor.c */