aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/odf_extractor.c
blob: 6a68f7c605aba578ff1517718011d580a338f172 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
     This file is part of libextractor.
     Copyright (C) 2004, 2009, 2012 Vidyut Samanta and Christian Grothoff

     libextractor is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     libextractor is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/
/**
 * @file plugins/odf_extractor.c
 * @brief plugin to support ODF files
 * @author Christian Grothoff
 */
#include "platform.h"
#include <ctype.h>
#include "extractor.h"
#include "unzip.h"

/**
 * Maximum length of a filename allowed inside the ZIP archive.
 */
#define MAXFILENAME 256

/**
 * Name of the file with the meta-data in OO documents.
 */
#define METAFILE "meta.xml"


/**
 * Mapping from ODF meta data strings to LE types.
 */
struct Matches 
{
  /**
   * ODF description.
   */
  const char * text;

  /**
   * Corresponding LE type.
   */
  enum EXTRACTOR_MetaType type;
};


/**
 * NULL-terminated map from ODF meta data strings to LE types.
 */
static struct Matches tmap[] = {
  { "meta:generator",     EXTRACTOR_METATYPE_CREATED_BY_SOFTWARE },
  { "meta:page-count",    EXTRACTOR_METATYPE_PAGE_COUNT },
  { "meta:creation-date", EXTRACTOR_METATYPE_CREATION_DATE },
  { "dc:date",            EXTRACTOR_METATYPE_UNKNOWN_DATE },
  { "dc:creator",         EXTRACTOR_METATYPE_CREATOR },
  { "dc:language",        EXTRACTOR_METATYPE_LANGUAGE },
  { "dc:title",           EXTRACTOR_METATYPE_TITLE },
  { "dc:description",     EXTRACTOR_METATYPE_DESCRIPTION },
  { "dc:subject",         EXTRACTOR_METATYPE_SUBJECT },
  { "meta:keyword",       EXTRACTOR_METATYPE_KEYWORDS },
  { "meta:user-defined meta:name=\"Info 1\"", EXTRACTOR_METATYPE_COMMENT },
  { "meta:user-defined meta:name=\"Info 2\"", EXTRACTOR_METATYPE_COMMENT },
  { "meta:user-defined meta:name=\"Info 3\"", EXTRACTOR_METATYPE_COMMENT },
  { "meta:user-defined meta:name=\"Info 4\"", EXTRACTOR_METATYPE_COMMENT },
  { NULL, 0 }
};


/**
 * Obtain the mimetype of the archive by reading the 'mimetype'
 * file of the ZIP.
 *
 * @param uf unzip context to extract the mimetype from
 * @return NULL if no mimetype could be found, otherwise the mime type
 */
static char *
libextractor_oo_getmimetype (struct EXTRACTOR_UnzipFile * uf) 
{
  char filename_inzip[MAXFILENAME];
  struct EXTRACTOR_UnzipFileInfo file_info;
  char *buf;
  size_t buf_size;

  if (EXTRACTOR_UNZIP_OK !=
      EXTRACTOR_common_unzip_go_find_local_file (uf,
						 "mimetype",
						 2))
    return NULL;
  if (EXTRACTOR_UNZIP_OK != 
      EXTRACTOR_common_unzip_get_current_file_info (uf,
						    &file_info,
						    filename_inzip,
						    sizeof (filename_inzip),
						    NULL,
						    0,
						    NULL,
						    0))
    return NULL;
  if (EXTRACTOR_UNZIP_OK != 
      EXTRACTOR_common_unzip_open_current_file (uf))
    return NULL;
  buf_size = file_info.uncompressed_size;    
  if (buf_size > 1024) 
    {
      /* way too large! */
      EXTRACTOR_common_unzip_close_current_file (uf);  
      return NULL;
    }
  if (NULL == (buf = malloc (1 + buf_size))) 
    {
      /* memory exhausted! */
      EXTRACTOR_common_unzip_close_current_file (uf);  
      return NULL;
    } 
  if (buf_size != 
      (size_t) EXTRACTOR_common_unzip_read_current_file (uf, 
							 buf, 
							 buf_size)) 
    {
      free(buf);
      EXTRACTOR_common_unzip_close_current_file(uf);  
      return NULL;
    }
  /* found something */
  buf[buf_size] = '\0';
  while ( (0 < buf_size) &&
	  isspace( (unsigned char) buf[buf_size - 1]))
    buf[--buf_size] = '\0';
  if ('\0' == buf[0]) 
    {
      free (buf);
      buf = NULL;
    }
  EXTRACTOR_common_unzip_close_current_file (uf);
  return buf;
}


/**
 * Main entry method for the ODF extraction plugin.  
 *
 * @param ec extraction context provided to the plugin
 */
void
EXTRACTOR_odf_extract_method (struct EXTRACTOR_ExtractContext *ec)
{
  char filename_inzip[MAXFILENAME];
  struct EXTRACTOR_UnzipFile *uf;
  struct EXTRACTOR_UnzipFileInfo file_info;
  char *buf;
  char *pbuf;
  size_t buf_size;
  unsigned int i;
  char *mimetype;

  if (NULL == (uf = EXTRACTOR_common_unzip_open (ec)))
    return;
  if (NULL != (mimetype = libextractor_oo_getmimetype (uf)))
    {
      if (0 != ec->proc (ec->cls, 
			 "odf",
			 EXTRACTOR_METATYPE_MIMETYPE,
			 EXTRACTOR_METAFORMAT_UTF8,
			 "text/plain",
			 mimetype,
			 strlen (mimetype) + 1))
	{
	  EXTRACTOR_common_unzip_close (uf);
	  free (mimetype);
	  return;
	}
      free (mimetype);
    }
  if (EXTRACTOR_UNZIP_OK !=
      EXTRACTOR_common_unzip_go_find_local_file (uf,
						 METAFILE,
						 2)) 
    {
      /* metafile not found */
      EXTRACTOR_common_unzip_close (uf);
      return; 
    }
  if (EXTRACTOR_UNZIP_OK != 
      EXTRACTOR_common_unzip_get_current_file_info (uf,
						    &file_info,
						    filename_inzip,
						    sizeof (filename_inzip),
						    NULL, 0, NULL, 0)) 
    {
      /* problems accessing metafile */
      EXTRACTOR_common_unzip_close (uf);
      return;
    }
  if (EXTRACTOR_UNZIP_OK != 
      EXTRACTOR_common_unzip_open_current_file (uf)) 
    {
      /* problems with unzip */
      EXTRACTOR_common_unzip_close (uf);
      return; 
    }

  buf_size = file_info.uncompressed_size;
  if (buf_size > 128 * 1024) 
    {
      /* too big to be meta-data! */
      EXTRACTOR_common_unzip_close_current_file (uf);
      EXTRACTOR_common_unzip_close (uf);
      return; 
    }
  if (NULL == (buf = malloc (buf_size+1)))
    {
      /* out of memory */
      EXTRACTOR_common_unzip_close_current_file (uf);
      EXTRACTOR_common_unzip_close (uf);
      return;
    }
  if (buf_size != EXTRACTOR_common_unzip_read_current_file (uf, buf, buf_size)) 
    {
      EXTRACTOR_common_unzip_close_current_file (uf);
      goto CLEANUP;
    }
  EXTRACTOR_common_unzip_close_current_file (uf);
  /* we don't do "proper" parsing of the meta-data but rather use some heuristics
     to get values out that we understand */
  buf[buf_size] = '\0';
  /* printf("%s\n", buf); */
  /* try to find some of the typical OO xml headers */
  if ( (strstr (buf, "xmlns:meta=\"http://openoffice.org/2000/meta\"") != NULL) ||
       (strstr (buf, "xmlns:dc=\"http://purl.org/dc/elements/1.1/\"") != NULL) ||
       (strstr (buf, "xmlns:xlink=\"http://www.w3.org/1999/xlink\"") != NULL) ) 
    {
      /* accept as meta-data */
      for (i = 0; NULL  != tmap[i].text; i++)
	{
	  char * spos;
	  char * epos;
	  char needle[256];
	  int oc;
	  
	  pbuf = buf;
	  
	  while (1) 
	    {
	      strcpy(needle, "<");
	      strcat(needle, tmap[i].text);
	      strcat(needle, ">");
	      spos = strstr(pbuf, needle);
	      if (NULL == spos) 
		{
		  strcpy(needle, tmap[i].text);
		  strcat(needle, "=\"");
		  spos = strstr(pbuf, needle);
		  if (spos == NULL)
		    break;
		  spos += strlen(needle);
		  epos = spos;
		  while ( (epos[0] != '\0') &&
			  (epos[0] != '"') )
		    epos++;
		} 
	      else
		{
		  oc = 0;
		  spos += strlen(needle);
		  while ( (spos[0] != '\0') &&
			  ( (spos[0] == '<') ||
			    (oc > 0) ) ) 
		    {
		      if (spos[0] == '<')
			oc++;
		      if (spos[0] == '>')
			oc--;
		      spos++;
		    }
		  epos = spos;
		  while ( (epos[0] != '\0') &&
			  (epos[0] != '<') &&
			  (epos[0] != '>') ) 
		    {
		      epos++;
		    }
		}
	      if (spos != epos) 
		{
		  char key[epos - spos + 1];
		  
		  memcpy(key, spos, epos-spos);
		  key[epos-spos] = '\0';
		  if (0 != ec->proc (ec->cls, 
				     "odf",
				     tmap[i].type,
				     EXTRACTOR_METAFORMAT_UTF8,
				     "text/plain",
				     key,
				     epos - spos + 1))
		    goto CLEANUP;		    	  
		  pbuf = epos;
		} 
	      else
		break;
	    }
	}
    }
 CLEANUP:
  free (buf);
  EXTRACTOR_common_unzip_close (uf);
}

/* end of odf_extractor.c */