aboutsummaryrefslogtreecommitdiff
path: root/src/main/extractor_ipc.c
blob: b343bd6e4df516296860f07e187c52493ddf01fd (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
/*
     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 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;
  unsigned char code;
  struct SeekRequestMessage seek;
  struct MetaMessage meta;
  const char *mime_type;
  const char *value;
  ssize_t ret;

  ret = 0;
  while (size > 0)
  {
    cdata = data;
    code = (unsigned char) cdata[0];
    switch (code)
    {
    case MESSAGE_DONE: /* Done */
      plugin->seek_request = -1;
      plugin->round_finished = 1;
      ret++;
      size--;
      data++;
      continue;
    case MESSAGE_SEEK: /* Seek */
      if (size < sizeof (struct SeekRequestMessage))
      {
        plugin->seek_request = -1;
        return ret;
      }
      memcpy (&seek, cdata, sizeof (seek));
      plugin->seek_request = (int64_t) seek.file_offset;
      plugin->seek_whence = seek.whence;
      ret += sizeof (struct SeekRequestMessage);
      data += sizeof (struct SeekRequestMessage);
      size -= sizeof (struct SeekRequestMessage);
      continue;
    case MESSAGE_META: /* Meta */
      if (size < sizeof (struct MetaMessage))
      {
        plugin->seek_request = -1;
        return ret;
      }
      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 ret;
      }
      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];
      if (meta.meta_type >= EXTRACTOR_metatype_get_max ())
        meta.meta_type = EXTRACTOR_METATYPE_UNKNOWN;
      proc (proc_cls,
            plugin,
            (enum EXTRACTOR_MetaType) meta.meta_type,
            (enum EXTRACTOR_MetaFormat) meta.meta_format,
            mime_type, value, meta.value_size);
      ret += sizeof (struct MetaMessage) + meta.mime_length + meta.value_size;
      size -= sizeof (struct MetaMessage) + meta.mime_length + meta.value_size;
      data += sizeof (struct MetaMessage) + meta.mime_length + meta.value_size;
      continue;
    default:
      LOG ("Invalid message type %d\n", (int) code);
      return -1;
    }
  }
  return ret;
}


/* end of extractor_ipc.c */