aboutsummaryrefslogtreecommitdiff
path: root/src/main/extractor_ipc.c
blob: 2d935dfc1e032570f0ac7a43d72c24cd65d6042b (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.
 */
/**
 * @file main/extractor_ipc.c
 * @brief IPC with plugin (OS-independent parts)
 * @author Christian Grothoff
 */
#include "platform.h"
#include "extractor_logging.h"
#include "extractor_ipc.h"
#include "extractor_plugins.h"


/**
 * Process a reply from channel (seek request, metadata and done message)
 *
 * @param plugin plugin this communication is about
 * @param buf buffer with data from IPC channel
 * @param size number of bytes in buffer
 * @param proc metadata callback
 * @param proc_cls callback cls
 * @return number of bytes processed, -1 on error
 */
ssize_t
EXTRACTOR_IPC_process_reply_ (struct EXTRACTOR_PluginList *plugin,
			      const void *data,
			      size_t size,
			      EXTRACTOR_ChannelMessageProcessor proc,
			      void *proc_cls)
{
  const char *cdata = data;
  unsigned char code;
  struct SeekRequestMessage seek;
  struct MetaMessage meta;
  const char *mime_type;
  const char *value;

  while (size > 0)
    {
      code = (unsigned char) cdata[0];
      switch (code)
	{
	case MESSAGE_DONE: /* Done */
	  plugin->seek_request = -1;
	  plugin->round_finished = 1;
	  return 1;
	case MESSAGE_SEEK: /* Seek */	  
	  if (size < sizeof (struct SeekRequestMessage))
	    {
	      plugin->seek_request = -1;
	      return 0;
	    }
	  memcpy (&seek, cdata, sizeof (seek));
	  plugin->seek_request = seek.file_offset;
	  return sizeof (struct SeekRequestMessage);
	case MESSAGE_META: /* Meta */
	  if (size < sizeof (struct MetaMessage))
	    {
	      plugin->seek_request = -1;
	      return 0;
	    }
	  memcpy (&meta, cdata, sizeof (meta));
	  /* check hdr for sanity */
	  if (meta.value_size > MAX_META_DATA)        
	    {
	      LOG ("Meta data exceeds size limit\n");
	      return -1; /* not allowing more than MAX_META_DATA meta data */
	    }
	  if (size < sizeof (meta) + meta.mime_length + meta.value_size)
	    {
	      plugin->seek_request = -1;
	      return 0;
	    }
	  if (0 == meta.mime_length)
	    {
	      mime_type = NULL;
	    }
	  else
	    {
	      mime_type = &cdata[sizeof (struct MetaMessage)];
	      if ('\0' != mime_type[meta.mime_length - 1])
		{
		  LOG ("Mime type not 0-terminated\n");
		  return -1;		
		}
	    }
	  if (0 == meta.value_size)
	    value = NULL;
	  else
	    value = &cdata[sizeof (struct MetaMessage) + meta.mime_length];
	  proc (proc_cls, 
		plugin, 
		(enum EXTRACTOR_MetaType) meta.meta_type,
		(enum EXTRACTOR_MetaFormat) meta.meta_format,
		meta.value_size,
		mime_type, value);
	  return sizeof (struct MetaMessage) + meta.mime_length + meta.value_size;
	default:
	  LOG ("Invalid message type %d\n", (int) code);
	  return -1;
	}
    }
  return 0;
}

/* end of extractor_ipc.c */