extractor_ipc.c (4310B)
1 /* 2 This file is part of libextractor. 3 Copyright (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., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, 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 #include "extractor_logging.h" 27 #include "extractor_ipc.h" 28 #include "extractor_plugins.h" 29 30 31 /** 32 * Process a reply from channel (seek request, metadata and done message) 33 * 34 * @param plugin plugin this communication is about 35 * @param buf buffer with data from IPC channel 36 * @param size number of bytes in buffer 37 * @param proc metadata callback 38 * @param proc_cls callback cls 39 * @return number of bytes processed, -1 on error 40 */ 41 ssize_t 42 EXTRACTOR_IPC_process_reply_ (struct EXTRACTOR_PluginList *plugin, 43 const void *data, 44 size_t size, 45 EXTRACTOR_ChannelMessageProcessor proc, 46 void *proc_cls) 47 { 48 const char *cdata; 49 unsigned char code; 50 struct SeekRequestMessage seek; 51 struct MetaMessage meta; 52 const char *mime_type; 53 const char *value; 54 ssize_t ret; 55 56 ret = 0; 57 while (size > 0) 58 { 59 cdata = data; 60 code = (unsigned char) cdata[0]; 61 switch (code) 62 { 63 case MESSAGE_DONE: /* Done */ 64 plugin->seek_request = -1; 65 plugin->round_finished = 1; 66 ret++; 67 size--; 68 data++; 69 continue; 70 case MESSAGE_SEEK: /* Seek */ 71 if (size < sizeof (struct SeekRequestMessage)) 72 { 73 plugin->seek_request = -1; 74 return ret; 75 } 76 memcpy (&seek, cdata, sizeof (seek)); 77 plugin->seek_request = (int64_t) seek.file_offset; 78 plugin->seek_whence = seek.whence; 79 ret += sizeof (struct SeekRequestMessage); 80 data += sizeof (struct SeekRequestMessage); 81 size -= sizeof (struct SeekRequestMessage); 82 continue; 83 case MESSAGE_META: /* Meta */ 84 if (size < sizeof (struct MetaMessage)) 85 { 86 plugin->seek_request = -1; 87 return ret; 88 } 89 memcpy (&meta, cdata, sizeof (meta)); 90 /* check hdr for sanity */ 91 if (meta.value_size > MAX_META_DATA) 92 { 93 LOG ("Meta data exceeds size limit\n"); 94 return -1; /* not allowing more than MAX_META_DATA meta data */ 95 } 96 if (size < sizeof (meta) + meta.mime_length + meta.value_size) 97 { 98 plugin->seek_request = -1; 99 return ret; 100 } 101 if (0 == meta.mime_length) 102 { 103 mime_type = NULL; 104 } 105 else 106 { 107 mime_type = &cdata[sizeof (struct MetaMessage)]; 108 if ('\0' != mime_type[meta.mime_length - 1]) 109 { 110 LOG ("Mime type not 0-terminated\n"); 111 return -1; 112 } 113 } 114 if (0 == meta.value_size) 115 value = NULL; 116 else 117 value = &cdata[sizeof (struct MetaMessage) + meta.mime_length]; 118 if (meta.meta_type >= EXTRACTOR_metatype_get_max ()) 119 meta.meta_type = EXTRACTOR_METATYPE_UNKNOWN; 120 proc (proc_cls, 121 plugin, 122 (enum EXTRACTOR_MetaType) meta.meta_type, 123 (enum EXTRACTOR_MetaFormat) meta.meta_format, 124 mime_type, value, meta.value_size); 125 ret += sizeof (struct MetaMessage) + meta.mime_length + meta.value_size; 126 size -= sizeof (struct MetaMessage) + meta.mime_length + meta.value_size; 127 data += sizeof (struct MetaMessage) + meta.mime_length + meta.value_size; 128 continue; 129 default: 130 LOG ("Invalid message type %d\n", (int) code); 131 return -1; 132 } 133 } 134 return ret; 135 } 136 137 138 /* end of extractor_ipc.c */