aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/old/pdf_extractor.cc
blob: 42025c52e0ed49d642213e98b07dcec287f14118 (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
/*
     This file is part of libextractor.
     (C) 2002, 2003, 2009 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 2, 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., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.

     This code was inspired by pdfinfo and depends heavily
     on the xpdf code that pdfinfo is a part of. See also
     the INFO file in this directory.
 */

#include "platform.h"
#include "extractor.h"
#include "convert.h"
#include <math.h>

#include <poppler/goo/gmem.h>
#include <poppler/Object.h>
#include <poppler/Stream.h>
#include <poppler/Array.h>
#include <poppler/Dict.h>
#include <poppler/XRef.h>
#include <poppler/Catalog.h>
#include <poppler/Page.h>
#include <poppler/PDFDoc.h>
#include <poppler/Error.h>
#include <poppler/GlobalParams.h>
#include <poppler/goo/GooString.h>

#define ADD(s, type) do { if (0!=proc(proc_cls, "pdf", type, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1)) { err = 1; goto EXIT; }} while (0)

static int 
printInfoString(Dict *infoDict,
		const char *key,
		enum EXTRACTOR_MetaType type,
		EXTRACTOR_MetaDataProcessor proc,
		void *proc_cls)
{
  Object obj;
  GooString *s1;
  const char * s;
  char *ckey = strdup (key);
  int err = 0;
  char * result;
      
  if (ckey == NULL)
    return 0;
  result = NULL;
  if (infoDict->lookup(ckey, &obj)->isString()) {
    s1 = obj.getString();
    s = s1->getCString();
    if ((((unsigned char)s[0]) & 0xff) == 0xfe &&
	(((unsigned char)s[1]) & 0xff) == 0xff) {
      result = EXTRACTOR_common_convert_to_utf8(&s[2], s1->getLength() - 2, "UTF-16BE");
      if (result != NULL)
	ADD (result, type);
    } else {
      size_t len = strlen(s);
      
      while(0 < len) 
	{
	  /*
	   * Avoid outputting trailing spaces.
	   *
	   * The following expression might be rewritten as
	   * (! isspace(s[len - 1]) && 0xA0 != s[len - 1]).
	   * There seem to exist isspace() implementations
	   * which do return non-zero from NBSP (maybe locale-dependent).
	   * Remove ISO-8859 non-breaking space (NBSP, hex value 0xA0) from
	   * the expression if it looks suspicious (locale issues for instance).
	   *
	   * Squeezing out all non-printable characters might also be useful.
	   */
  	  if ( (' '  != s[len - 1]) && (((char)0xA0) != s[len - 1]) &&
               ('\r' != s[len - 1]) && ('\n' != s[len - 1]) &&
               ('\t' != s[len - 1]) && ('\v' != s[len - 1]) &&
               ('\f' != s[len - 1]) )
	    break;	  
          else
            len --;
        }

        /* there should be a check to truncate preposterously long values. */
      
      if (0 < len) {
	result = EXTRACTOR_common_convert_to_utf8(s, len,
						  "ISO-8859-1");
	if (result != NULL)
	  ADD (result, type);
      }
    }
  }
 EXIT:
  obj.free();
  if (result != NULL)
    free (result);
  free (ckey);
  return err;
}

static int 
printInfoDate(Dict *infoDict,
	      const char *key,
	      enum EXTRACTOR_MetaType type,
	      EXTRACTOR_MetaDataProcessor proc,
	      void *proc_cls)
{
  Object obj;
  const char *s;
  GooString *s1;  
  char *gkey;
  char * result;
  int err;
  
  err = 0;
  result = NULL;
  gkey = strdup (key);
  if (gkey == NULL)
    return 0;
  if (infoDict->lookup(gkey, &obj)->isString()) {
    s1 = obj.getString();
    s = s1->getCString();
    
    if ((s1->getChar(0) & 0xff) == 0xfe &&
	(s1->getChar(1) & 0xff) == 0xff) {
      /* isUnicode */
      
      result = EXTRACTOR_common_convert_to_utf8((const char*)&s[2], s1->getLength() - 2, "UTF-16BE");
      if (result != NULL)
	ADD (result, type);
    } else {
      if (s[0] == 'D' && s[1] == ':') 
	s += 2;
      
      ADD (s, type);
    }
    /* printf(fmt, s);*/
  }
 EXIT:
  obj.free();
  if (result != NULL)
    free (result);
  free (gkey);
  return err;
}

#define PIS(s,t) do { if (0 != (err = printInfoString (info.getDict(), s, t, proc, proc_cls))) goto EXIT; } while (0)

#define PID(s,t) do { if (0 != (err = printInfoDate (info.getDict(), s, t, proc, proc_cls))) goto EXIT; } while (0)

extern "C" {
 

  int 
  EXTRACTOR_pdf_extract (const char *data,
			 size_t size,
			 EXTRACTOR_MetaDataProcessor proc,
			 void *proc_cls,
			 const char *options)
  {
    PDFDoc * doc;
    Object info;
    Object obj;
    BaseStream * stream;
    int err;

    if (globalParams == NULL)
      {
	globalParams = new GlobalParams();
	globalParams->setErrQuiet (gTrue);
      }
    obj.initNull();
    err = 0;
    stream = new MemStream( (char*) data, 0, size, &obj);
    doc = new PDFDoc(stream, NULL, NULL);
    if (! doc->isOk()) {
      delete doc;
      return 0;
    }

    ADD ("application/pdf",
	 EXTRACTOR_METATYPE_MIMETYPE);
    if ( (NULL != doc->getDocInfo(&info)) &&
	 (info.isDict()) ) {
      PIS ("Title", EXTRACTOR_METATYPE_TITLE);
      PIS ("Subject", EXTRACTOR_METATYPE_SUBJECT);
      PIS ("Keywords", EXTRACTOR_METATYPE_KEYWORDS);
      PIS ("Author", EXTRACTOR_METATYPE_AUTHOR_NAME);
      /*
       * we now believe that Adobe's Creator is not a person nor an
       * organisation, but just a piece of software.
       */
      PIS ("Creator", EXTRACTOR_METATYPE_CREATED_BY_SOFTWARE);
      PIS ("Producer", EXTRACTOR_METATYPE_PRODUCED_BY_SOFTWARE);
      {
	char pcnt[20];
	sprintf(pcnt, "%d", doc->getNumPages());
	ADD (pcnt, EXTRACTOR_METATYPE_PAGE_COUNT);
      }
      {
	char pcnt[64];
#if HAVE_POPPLER_GETPDFMAJORVERSION
	sprintf(pcnt, "PDF %d.%d", 
		doc->getPDFMajorVersion(),
		doc->getPDFMinorVersion());
#else
	sprintf(pcnt, "PDF %.1f", 
		doc->getPDFVersion());
#endif
	ADD (pcnt, EXTRACTOR_METATYPE_FORMAT);
      }
      PID ("CreationDate", EXTRACTOR_METATYPE_CREATION_DATE);
      PID ("ModDate", EXTRACTOR_METATYPE_MODIFICATION_DATE);
    }
  EXIT:
    info.free();
    delete doc;

    return err;
  }
}