zip_extractor.c (4336B)
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 plugins/zip_extractor.c 22 * @brief plugin to support ZIP files 23 * @author Christian Grothoff 24 */ 25 #include "platform.h" 26 #include <ctype.h> 27 #include "extractor.h" 28 #include "unzip.h" 29 30 31 /** 32 * Main entry method for the 'application/zip' extraction plugin. 33 * 34 * @param ec extraction context provided to the plugin 35 */ 36 void 37 EXTRACTOR_zip_extract_method (struct EXTRACTOR_ExtractContext *ec); 38 39 void 40 EXTRACTOR_zip_extract_method (struct EXTRACTOR_ExtractContext *ec) 41 { 42 struct EXTRACTOR_UnzipFile *uf; 43 struct EXTRACTOR_UnzipFileInfo fi; 44 char fname[256]; 45 char fcomment[256]; 46 47 if (NULL == (uf = EXTRACTOR_common_unzip_open (ec))) 48 return; 49 if ( (EXTRACTOR_UNZIP_OK == 50 EXTRACTOR_common_unzip_go_find_local_file (uf, 51 "meta.xml", 52 2)) || 53 (EXTRACTOR_UNZIP_OK == 54 EXTRACTOR_common_unzip_go_find_local_file (uf, 55 "META-INF/MANIFEST.MF", 56 2)) ) 57 { 58 /* not a normal zip, might be odf, jar, etc. */ 59 goto CLEANUP; 60 } 61 if (EXTRACTOR_UNZIP_OK != 62 EXTRACTOR_common_unzip_go_to_first_file (uf)) 63 { 64 /* zip malformed? */ 65 goto CLEANUP; 66 } 67 if (0 != 68 ec->proc (ec->cls, 69 "zip", 70 EXTRACTOR_METATYPE_MIMETYPE, 71 EXTRACTOR_METAFORMAT_UTF8, 72 "text/plain", 73 "application/zip", 74 strlen ("application/zip") + 1)) 75 goto CLEANUP; 76 if (EXTRACTOR_UNZIP_OK == 77 EXTRACTOR_common_unzip_get_global_comment (uf, 78 fcomment, 79 sizeof (fcomment))) 80 { 81 if ( (0 != strlen (fcomment)) && 82 (0 != 83 ec->proc (ec->cls, 84 "zip", 85 EXTRACTOR_METATYPE_COMMENT, 86 EXTRACTOR_METAFORMAT_C_STRING, 87 "text/plain", 88 fcomment, 89 strlen (fcomment) + 1))) 90 goto CLEANUP; 91 } 92 do 93 { 94 if (EXTRACTOR_UNZIP_OK == 95 EXTRACTOR_common_unzip_get_current_file_info (uf, 96 &fi, 97 fname, 98 sizeof (fname), 99 NULL, 0, 100 fcomment, 101 sizeof (fcomment))) 102 { 103 if ( (0 != strlen (fname)) && 104 (0 != 105 ec->proc (ec->cls, 106 "zip", 107 EXTRACTOR_METATYPE_FILENAME, 108 EXTRACTOR_METAFORMAT_C_STRING, 109 "text/plain", 110 fname, 111 strlen (fname) + 1))) 112 goto CLEANUP; 113 if ( (0 != strlen (fcomment)) && 114 (0 != 115 ec->proc (ec->cls, 116 "zip", 117 EXTRACTOR_METATYPE_COMMENT, 118 EXTRACTOR_METAFORMAT_C_STRING, 119 "text/plain", 120 fcomment, 121 strlen (fcomment) + 1))) 122 goto CLEANUP; 123 } 124 } 125 while (EXTRACTOR_UNZIP_OK == 126 EXTRACTOR_common_unzip_go_to_next_file (uf)); 127 128 CLEANUP: 129 (void) EXTRACTOR_common_unzip_close (uf); 130 } 131 132 133 /* end of zip_extractor.c */