aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/ps_extractor.c
blob: 8a5543f73694e2b0787139263a9f2e13e3aaefa3 (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
/*
     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.
 */

#include "platform.h"
#include "extractor.h"


static char *
readline (const char *data, size_t size, size_t pos)
{
  size_t end;
  char *res;

  while ((pos < size) &&
         ((data[pos] == (char) 0x0d) || (data[pos] == (char) 0x0a)))
    pos++;

  if (pos >= size)
    return NULL;                /* end of file */
  end = pos;
  while ((end < size) &&
         (data[end] != (char) 0x0d) && (data[end] != (char) 0x0a))
    end++;
  res = malloc (end - pos + 1);
  memcpy (res, &data[pos], end - pos);
  res[end - pos] = '\0';

  return res;
}


static int
testmeta (char *line,
          const char *match,
          enum EXTRACTOR_MetaType type, 
	  EXTRACTOR_MetaDataProcessor proc,
	  void *proc_cls)
{
  char *key;

  if ( (strncmp (line, match, strlen (match)) == 0) &&
       (strlen (line) > strlen (match)) )
    {
      if ((line[strlen (line) - 1] == ')') && (line[strlen (match)] == '('))
        {
          key = &line[strlen (match) + 1];
          key[strlen (key) - 1] = '\0'; /* remove ")" */
        }
      else
        {
          key = &line[strlen (match)];
        }
      if (0 != proc (proc_cls,
		     "ps",
		     type,
		     EXTRACTOR_METAFORMAT_UTF8,
		     "text/plain",
		     key,
		     strlen (key)+1))
	return 1;
    }
  return 0;
}

typedef struct
{
  const char *prefix;
  enum EXTRACTOR_MetaType type;
} Matches;

static Matches tests[] = {
  {"%%Title: ", EXTRACTOR_METATYPE_TITLE},
  {"%%Author: ", EXTRACTOR_METATYPE_AUTHOR_NAME},
  {"%%Version: ", EXTRACTOR_METATYPE_REVISION_NUMBER},
  {"%%Creator: ", EXTRACTOR_METATYPE_CREATED_BY_SOFTWARE},
  {"%%CreationDate: ", EXTRACTOR_METATYPE_CREATION_DATE},
  {"%%Pages: ", EXTRACTOR_METATYPE_PAGE_COUNT},
  {"%%Orientation: ", EXTRACTOR_METATYPE_PAGE_ORIENTATION},
  {"%%DocumentPaperSizes: ", EXTRACTOR_METATYPE_PAPER_SIZE},
  {"%%PageOrder: ", EXTRACTOR_METATYPE_PAGE_ORDER},
  {"%%LanguageLevel: ", EXTRACTOR_METATYPE_FORMAT_VERSION},
  {"%%Magnification: ", EXTRACTOR_METATYPE_MAGNIFICATION},

  /* Also widely used but not supported since they
     probably make no sense:
     "%%BoundingBox: ",
     "%%DocumentNeededResources: ",
     "%%DocumentSuppliedResources: ",
     "%%DocumentProcSets: ",
     "%%DocumentData: ", */

  {NULL, 0}
};

#define PS_HEADER "%!PS-Adobe"

/* mimetype = application/postscript */
int 
EXTRACTOR_ps_extract (const char *data,
		      size_t size,
		      EXTRACTOR_MetaDataProcessor proc,
		      void *proc_cls,
		      const char *options)
{
  size_t pos;
  char *line;
  int i;
  int lastLine;
  int ret;

  pos = strlen (PS_HEADER);
  if ( (size < pos) ||
       (0 != strncmp (PS_HEADER,
		      data,
		      pos)) )
    return 0;
  ret = 0;

  if (0 != proc (proc_cls,
		 "ps",
		 EXTRACTOR_METATYPE_MIMETYPE,
		 EXTRACTOR_METAFORMAT_UTF8,
		 "text/plain",
		 "application/postscript",
		 strlen ("application/postscript")+1))
    return 1;
  /* skip rest of first line */
  while ((pos < size) && (data[pos] != '\n'))
    pos++;

  lastLine = -1;
  line = NULL;
  /* while Windows-PostScript does not seem to (always?) put
     "%%EndComments", this should allow us to not read through most of
     the file for all the sane applications... For Windows-generated
     PS files, we will bail out at the end of the file. */
  while (0 != strncmp ("%%EndComments", line, strlen ("%%EndComments")))
    {
      free (line);
      line = readline (data, size, pos);
      if (line == NULL)
        break;
      i = 0;
      while (tests[i].prefix != NULL)
        {
          ret = testmeta (line, tests[i].prefix, tests[i].type, proc, proc_cls);
	  if (ret != 0)
	    break;
          i++;
        }
      if (ret != 0)
	break;

      /* %%+ continues previous meta-data type... */
      if ( (lastLine != -1) && (0 == strncmp (line, "%%+ ", strlen ("%%+ "))))
        {
          ret = testmeta (line, "%%+ ", tests[lastLine].type, proc, proc_cls);
        }
      else
        {
          /* update "previous" type */
          if (tests[i].prefix == NULL)
            lastLine = -1;
          else
            lastLine = i;
        }
      if (pos + strlen (line) + 1 <= pos)
	break; /* overflow */
      pos += strlen (line) + 1; /* skip newline, too; guarantee progress! */      
    }
  free (line);
  return ret;
}

/* end of ps_extractor.c */