aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/gif_extractor.c
blob: 1e85f87171b633d359e28525b1173b30560f6db6 (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
/*
     This file is part of libextractor.
     (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., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
 */

#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 (NULL == (gif_file = DGifOpen (ec, &gif_read_func)))
    return; /* not a GIF */
  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 (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 */
    }
  DGifCloseFile (gif_file);
}

/* end of gif_extractor.c */