zip_extractor.c (4262B)
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 struct EXTRACTOR_UnzipFile *uf; 40 struct EXTRACTOR_UnzipFileInfo fi; 41 char fname[256]; 42 char fcomment[256]; 43 44 if (NULL == (uf = EXTRACTOR_common_unzip_open (ec))) 45 return; 46 if ( (EXTRACTOR_UNZIP_OK == 47 EXTRACTOR_common_unzip_go_find_local_file (uf, 48 "meta.xml", 49 2)) || 50 (EXTRACTOR_UNZIP_OK == 51 EXTRACTOR_common_unzip_go_find_local_file (uf, 52 "META-INF/MANIFEST.MF", 53 2)) ) 54 { 55 /* not a normal zip, might be odf, jar, etc. */ 56 goto CLEANUP; 57 } 58 if (EXTRACTOR_UNZIP_OK != 59 EXTRACTOR_common_unzip_go_to_first_file (uf)) 60 { 61 /* zip malformed? */ 62 goto CLEANUP; 63 } 64 if (0 != 65 ec->proc (ec->cls, 66 "zip", 67 EXTRACTOR_METATYPE_MIMETYPE, 68 EXTRACTOR_METAFORMAT_UTF8, 69 "text/plain", 70 "application/zip", 71 strlen ("application/zip") + 1)) 72 goto CLEANUP; 73 if (EXTRACTOR_UNZIP_OK == 74 EXTRACTOR_common_unzip_get_global_comment (uf, 75 fcomment, 76 sizeof (fcomment))) 77 { 78 if ( (0 != strlen (fcomment)) && 79 (0 != 80 ec->proc (ec->cls, 81 "zip", 82 EXTRACTOR_METATYPE_COMMENT, 83 EXTRACTOR_METAFORMAT_C_STRING, 84 "text/plain", 85 fcomment, 86 strlen (fcomment) + 1))) 87 goto CLEANUP; 88 } 89 do 90 { 91 if (EXTRACTOR_UNZIP_OK == 92 EXTRACTOR_common_unzip_get_current_file_info (uf, 93 &fi, 94 fname, 95 sizeof (fname), 96 NULL, 0, 97 fcomment, 98 sizeof (fcomment))) 99 { 100 if ( (0 != strlen (fname)) && 101 (0 != 102 ec->proc (ec->cls, 103 "zip", 104 EXTRACTOR_METATYPE_FILENAME, 105 EXTRACTOR_METAFORMAT_C_STRING, 106 "text/plain", 107 fname, 108 strlen (fname) + 1))) 109 goto CLEANUP; 110 if ( (0 != strlen (fcomment)) && 111 (0 != 112 ec->proc (ec->cls, 113 "zip", 114 EXTRACTOR_METATYPE_COMMENT, 115 EXTRACTOR_METAFORMAT_C_STRING, 116 "text/plain", 117 fcomment, 118 strlen (fcomment) + 1))) 119 goto CLEANUP; 120 } 121 } 122 while (EXTRACTOR_UNZIP_OK == 123 EXTRACTOR_common_unzip_go_to_next_file (uf)); 124 125 CLEANUP: 126 (void) EXTRACTOR_common_unzip_close (uf); 127 } 128 129 130 /* end of zip_extractor.c */