diff options
Diffstat (limited to 'src/main/extractor_ipc.c')
-rw-r--r-- | src/main/extractor_ipc.c | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/src/main/extractor_ipc.c b/src/main/extractor_ipc.c new file mode 100644 index 0000000..8c3a687 --- /dev/null +++ b/src/main/extractor_ipc.c | |||
@@ -0,0 +1,137 @@ | |||
1 | /* | ||
2 | This file is part of libextractor. | ||
3 | (C) 2012 Vidyut Samanta and Christian Grothoff | ||
4 | |||
5 | libextractor is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | libextractor is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with libextractor; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file main/extractor_ipc.c | ||
22 | * @brief IPC with plugin (OS-independent parts) | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | |||
27 | |||
28 | /** | ||
29 | * Process a reply from channel (seek request, metadata and done message) | ||
30 | * | ||
31 | * @param buf buffer with data from IPC channel | ||
32 | * @param size number of bytes in buffer | ||
33 | * @param proc metadata callback | ||
34 | * @param proc_cls callback cls | ||
35 | * @return number of bytes processed, -1 on error | ||
36 | */ | ||
37 | ssize_t | ||
38 | EXTRACTOR_IPC_process_reply_ (const void *data, | ||
39 | size_t size, | ||
40 | EXTRACTOR_ChannelMessageProcessor proc, | ||
41 | void *proc_cls) | ||
42 | { | ||
43 | int read_result; | ||
44 | unsigned char code; | ||
45 | int64_t seek_position; | ||
46 | struct IpcHeader hdr; | ||
47 | char *mime_type; | ||
48 | char *data; | ||
49 | int must_read = 1; | ||
50 | |||
51 | while (must_read) | ||
52 | { | ||
53 | read_result = plugin_read (plugin, &code, 1); | ||
54 | if (read_result < 1) | ||
55 | return -1; | ||
56 | switch (code) | ||
57 | { | ||
58 | case MESSAGE_DONE: /* Done */ | ||
59 | plugin->seek_request = -1; | ||
60 | must_read = 0; | ||
61 | break; | ||
62 | case MESSAGE_SEEK: /* Seek */ | ||
63 | read_result = plugin_read (plugin, | ||
64 | &seek_position, sizeof (int64_t)); | ||
65 | if (read_result < sizeof (int64_t)) | ||
66 | return -1; | ||
67 | plugin->seek_request = seek_position; | ||
68 | must_read = 0; | ||
69 | break; | ||
70 | case MESSAGE_META: /* Meta */ | ||
71 | read_result = plugin_read (plugin, | ||
72 | &hdr, sizeof (hdr)); | ||
73 | if (read_result < sizeof (hdr)) | ||
74 | return -1; | ||
75 | /* FIXME: check hdr for sanity */ | ||
76 | if (hdr.data_len > MAX_META_DATA) | ||
77 | return -1; /* not allowing more than MAX_META_DATA meta data */ | ||
78 | if (0 == hdr.mime_len) | ||
79 | { | ||
80 | mime_type = NULL; | ||
81 | } | ||
82 | else | ||
83 | { | ||
84 | if (NULL == (mime_type = malloc (hdr.mime_len))) | ||
85 | return -1; | ||
86 | read_result = plugin_read (plugin, | ||
87 | mime_type, | ||
88 | hdr.mime_len); | ||
89 | if ( (read_result < hdr.mime_len) || | ||
90 | ('\0' != mime_type[hdr.mime_len-1]) ) | ||
91 | { | ||
92 | if (NULL != mime_type) | ||
93 | free (mime_type); | ||
94 | return -1; | ||
95 | } | ||
96 | } | ||
97 | if (0 == hdr.data_len) | ||
98 | { | ||
99 | data = NULL; | ||
100 | } | ||
101 | else | ||
102 | { | ||
103 | if (NULL == (data = malloc (hdr.data_len))) | ||
104 | { | ||
105 | if (NULL != mime_type) | ||
106 | free (mime_type); | ||
107 | return -1; | ||
108 | } | ||
109 | read_result = plugin_read (plugin, | ||
110 | data, hdr.data_len); | ||
111 | if (read_result < hdr.data_len) | ||
112 | { | ||
113 | if (NULL != mime_type) | ||
114 | free (mime_type); | ||
115 | free (data); | ||
116 | return -1; | ||
117 | } | ||
118 | } | ||
119 | read_result = proc (proc_cls, | ||
120 | plugin->short_libname, | ||
121 | hdr.meta_type, hdr.meta_format, | ||
122 | mime_type, data, hdr.data_len); | ||
123 | if (NULL != mime_type) | ||
124 | free (mime_type); | ||
125 | if (NULL != data) | ||
126 | free (data); | ||
127 | if (0 != read_result) | ||
128 | return 1; | ||
129 | break; | ||
130 | default: | ||
131 | return -1; | ||
132 | } | ||
133 | } | ||
134 | return 0; | ||
135 | } | ||
136 | |||
137 | /* end of extractor_ipc.c */ | ||