jpeg_extractor.c (5418B)
1 /* 2 This file is part of libextractor. 3 Copyright (C) 2002, 2003, 2004, 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/jpeg_extractor.c 22 * @brief plugin to support JPEG files 23 * @author Christian Grothoff 24 */ 25 #include "platform.h" 26 #include "extractor.h" 27 #if WINDOWS || DARWIN 28 #if DARWIN 29 typedef int boolean; 30 #endif 31 #define HAVE_BOOLEAN 32 #endif 33 #include <jpeglib.h> 34 #include <setjmp.h> 35 36 37 /** 38 * Context for custom functions. 39 */ 40 struct Context 41 { 42 /** 43 * Environment for longjmp from within error_exit handler. 44 */ 45 jmp_buf env; 46 }; 47 48 49 /** 50 * Function used to avoid having libjpeg write error messages to the console. 51 */ 52 static void 53 no_emit (j_common_ptr cinfo, int msg_level) 54 { 55 /* do nothing */ 56 } 57 58 59 /** 60 * Function used to avoid having libjpeg write error messages to the console. 61 */ 62 static void 63 no_output (j_common_ptr cinfo) 64 { 65 /* do nothing */ 66 } 67 68 69 /** 70 * Function used to avoid having libjpeg kill our process. 71 */ 72 static void 73 no_exit (j_common_ptr cinfo) 74 { 75 struct Context *ctx = cinfo->client_data; 76 77 /* we're not allowed to return (by API definition), 78 and we don't want to abort/exit. So we longjmp 79 to our cleanup code instead. */ 80 longjmp (ctx->env, 1); 81 } 82 83 84 /** 85 * Main entry method for the 'image/jpeg' extraction plugin. 86 * 87 * @param ec extraction context provided to the plugin 88 */ 89 void 90 EXTRACTOR_jpeg_extract_method (struct EXTRACTOR_ExtractContext *ec); 91 92 void 93 EXTRACTOR_jpeg_extract_method (struct EXTRACTOR_ExtractContext *ec) 94 { 95 struct jpeg_decompress_struct jds; 96 struct jpeg_error_mgr em; 97 void *buf; 98 ssize_t size; 99 int is_jpeg; 100 unsigned int rounds; 101 char format[128]; 102 struct jpeg_marker_struct *mptr; 103 struct Context ctx; 104 105 is_jpeg = 0; 106 rounds = 0; /* used to avoid going on forever for non-jpeg files */ 107 jpeg_std_error (&em); 108 em.emit_message = &no_emit; 109 em.output_message = &no_output; 110 em.error_exit = &no_exit; 111 jds.client_data = &ctx; 112 if (1 == setjmp (ctx.env)) 113 goto EXIT; /* we get here if libjpeg calls 'no_exit' because it wants to die */ 114 jds.err = &em; 115 jpeg_create_decompress (&jds); 116 jpeg_save_markers (&jds, JPEG_COM, 1024 * 8); 117 while ( (1 == is_jpeg) || (rounds++ < 8) ) 118 { 119 if (-1 == (size = ec->read (ec->cls, 120 &buf, 121 16 * 1024))) 122 break; 123 if (0 == size) 124 break; 125 jpeg_mem_src (&jds, buf, size); 126 if (0 == is_jpeg) 127 { 128 if (JPEG_HEADER_OK == jpeg_read_header (&jds, 1)) 129 is_jpeg = 1; /* ok, really a jpeg, keep going until the end */ 130 continue; 131 } 132 jpeg_consume_input (&jds); 133 } 134 135 if (1 != is_jpeg) 136 goto EXIT; 137 if (0 != 138 ec->proc (ec->cls, 139 "jpeg", 140 EXTRACTOR_METATYPE_MIMETYPE, 141 EXTRACTOR_METAFORMAT_UTF8, 142 "text/plain", 143 "image/jpeg", 144 strlen ("image/jpeg") + 1)) 145 goto EXIT; 146 snprintf (format, 147 sizeof (format), 148 "%ux%u", 149 (unsigned int) jds.image_width, 150 (unsigned int) jds.image_height); 151 if (0 != 152 ec->proc (ec->cls, 153 "jpeg", 154 EXTRACTOR_METATYPE_IMAGE_DIMENSIONS, 155 EXTRACTOR_METAFORMAT_UTF8, 156 "text/plain", 157 format, 158 strlen (format) + 1)) 159 goto EXIT; 160 for (mptr = jds.marker_list; NULL != mptr; mptr = mptr->next) 161 { 162 size_t off; 163 size_t clen; 164 char *comment; 165 166 if (JPEG_COM != mptr->marker) 167 continue; 168 off = 0; 169 while ( (off < mptr->data_length) && 170 (isspace (((const unsigned char *) mptr->data)[mptr->data_length 171 - 1 - off])) ) 172 off++; 173 clen = mptr->data_length - off; 174 if (0 == clen) 175 continue; 176 /* libjpeg hands out the raw marker payload, which is neither 177 0-terminated nor guaranteed to be free of 0 bytes; C_STRING 178 promises "data_len == strlen (data) + 1", so terminate a copy 179 and measure that. */ 180 if (NULL == (comment = malloc (clen + 1))) 181 goto EXIT; 182 memcpy (comment, 183 mptr->data, 184 clen); 185 comment[clen] = '\0'; 186 if (0 == strlen (comment)) 187 { 188 free (comment); 189 continue; 190 } 191 if (0 != 192 ec->proc (ec->cls, 193 "jpeg", 194 EXTRACTOR_METATYPE_COMMENT, 195 EXTRACTOR_METAFORMAT_C_STRING, 196 "text/plain", 197 comment, 198 strlen (comment) + 1)) 199 { 200 free (comment); 201 goto EXIT; 202 } 203 free (comment); 204 } 205 206 EXIT: 207 jpeg_destroy_decompress (&jds); 208 } 209 210 211 /* end of jpeg_extractor.c */