aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/deb_extractor.c
blob: b998b36c82044fae9b9265238affe6145035ea1c (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
     This file is part of libextractor.
     (C) 2002, 2003, 2004 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"
#include <zlib.h>

/*
 * The .deb is an ar-chive file.  It contains a tar.gz file
 * named "control.tar.gz" which then contains a file 'control'
 * that has the meta-data.  And which variant of the various
 * ar file formats is used is also not quite certain. Yuck.
 *
 * References:
 * http://www.mkssoftware.com/docs/man4/tar.4.asp
 * http://lists.debian.org/debian-policy/2003/12/msg00000.html
 * http://www.opengroup.org/onlinepubs/009695399/utilities/ar.html
 */


static char *
stndup (const char *str, size_t n)
{
  char *tmp;
  tmp = malloc (n + 1);
  tmp[n] = '\0';
  memcpy (tmp, str, n);
  return tmp;
}



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

/* see also: "man 5 deb-control" */
static Matches tmap[] = {
  {"Package: ",       EXTRACTOR_METATYPE_PACKAGE_NAME},
  {"Version: ",       EXTRACTOR_METATYPE_PACKAGE_VERSION},
  {"Section: ",       EXTRACTOR_METATYPE_SECTION},
  {"Priority: ",      EXTRACTOR_METATYPE_UPLOAD_PRIORITY},
  {"Architecture: ",  EXTRACTOR_METATYPE_TARGET_ARCHITECTURE},
  {"Depends: ",       EXTRACTOR_METATYPE_PACKAGE_DEPENDENCY},
  {"Recommends: ",    EXTRACTOR_METATYPE_PACKAGE_RECOMMENDS},
  {"Suggests: ",      EXTRACTOR_METATYPE_PACKAGE_SUGGESTS},
  {"Installed-Size: ",EXTRACTOR_METATYPE_PACKAGE_INSTALLED_SIZE},
  {"Maintainer: ",    EXTRACTOR_METATYPE_PACKAGE_MAINTAINER},
  {"Description: ",   EXTRACTOR_METATYPE_DESCRIPTION},
  {"Source: ",        EXTRACTOR_METATYPE_PACKAGE_SOURCE},
  {"Pre-Depends: ",   EXTRACTOR_METATYPE_PACKAGE_PRE_DEPENDENCY},
  {"Conflicts: ",     EXTRACTOR_METATYPE_PACKAGE_CONFLICTS},
  {"Replaces: ",      EXTRACTOR_METATYPE_PACKAGE_REPLACES},
  {"Provides: ",      EXTRACTOR_METATYPE_PACKAGE_PROVIDES},
  {"Essential: ",     EXTRACTOR_METATYPE_PACKAGE_ESSENTIAL},
  {NULL, 0}
};


/**
 * Process the control file.
 */
static int
processControl (const char *data,
                const size_t size,
		EXTRACTOR_MetaDataProcessor proc,
		void *proc_cls)
{
  size_t pos;
  char *key;
  char *val;

  pos = 0;
  while (pos < size)
    {
      size_t colon;
      size_t eol;
      int i;

      colon = pos;
      while (data[colon] != ':')
        {
          if ((colon > size) || (data[colon] == '\n'))
            return 0;
          colon++;
        }
      colon++;
      while ((colon < size) && (isspace ((unsigned char) data[colon])))
        colon++;
      eol = colon;
      while ((eol < size) &&
             ((data[eol] != '\n') ||
              ((eol + 1 < size) && (data[eol + 1] == ' '))))
        eol++;
      if ((eol == colon) || (eol > size))
        return 0;
      key = stndup (&data[pos], colon - pos);
      i = 0;
      while (tmap[i].text != NULL)
        {
          if (0 == strcmp (key, tmap[i].text))
            {
              val = stndup (&data[colon], eol - colon);
	      if (0 != proc (proc_cls, 
			     "deb",
			     tmap[i].type,
			     EXTRACTOR_METAFORMAT_UTF8,
			     "text/plain",
			     val,
			     strlen(val) + 1))
		{
		  free (val);
		  free (key);
		  return 1;
		}
	      free (val);
              break;
            }
          i++;
        }
      free (key);
      pos = eol + 1;
    }
  return 0;
}


typedef struct
{
  char name[100];
  char mode[8];
  char userId[8];
  char groupId[8];
  char filesize[12];
  char lastModTime[12];
  char chksum[8];
  char link;
  char linkName[100];
} TarHeader;

typedef struct
{
  TarHeader tar;
  char magic[6];
  char version[2];
  char uname[32];
  char gname[32];
  char devmajor[8];
  char devminor[8];
  char prefix[155];
} USTarHeader;

/**
 * Process the control.tar file.
 */
static int
processControlTar (const char *data,
                   const size_t size,
		   EXTRACTOR_MetaDataProcessor proc,
		   void *proc_cls)
{
  TarHeader *tar;
  USTarHeader *ustar;
  size_t pos;

  pos = 0;
  while (pos + sizeof (TarHeader) < size)
    {
      unsigned long long fsize;
      char buf[13];

      tar = (TarHeader *) & data[pos];
      if (pos + sizeof (USTarHeader) < size)
        {
          ustar = (USTarHeader *) & data[pos];
          if (0 == strncmp ("ustar", &ustar->magic[0], strlen ("ustar")))
            pos += 512;         /* sizeof(USTarHeader); */
          else
            pos += 257;         /* sizeof(TarHeader); minus gcc alignment... */
        }
      else
        {
          pos += 257;           /* sizeof(TarHeader); minus gcc alignment... */
        }

      memcpy (buf, &tar->filesize[0], 12);
      buf[12] = '\0';
      if (1 != sscanf (buf, "%12llo", &fsize))  /* octal! Yuck yuck! */
        return 0;
      if ((pos + fsize > size) || (fsize > size) || (pos + fsize < pos))
        return 0;

      if (0 == strncmp (&tar->name[0], "./control", strlen ("./control")))
        {
          return processControl (&data[pos], fsize, proc, proc_cls);
        }
      if ((fsize & 511) != 0)
        fsize = (fsize | 511) + 1;      /* round up! */
      if (pos + fsize < pos)
        return 0;
      pos += fsize;
    }
  return 0;
}

#define MAX_CONTROL_SIZE (1024 * 1024)
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif

static voidpf
Emalloc (voidpf opaque, uInt items, uInt size)
{
  if (SIZE_MAX / size <= items)
    return NULL;
  return malloc (size * items);
}

static void
Efree (voidpf opaque, voidpf ptr)
{
  free (ptr);
}

/**
 * Process the control.tar.gz file.
 */
static int
processControlTGZ (const unsigned char *data,
                   size_t size, 
		   EXTRACTOR_MetaDataProcessor proc,
		   void *proc_cls)
{
  uint32_t bufSize;
  char *buf;
  z_stream strm;
  int ret;

  bufSize = data[size - 4] + (data[size - 3] << 8) + (data[size - 2] << 16) + (data[size - 1] << 24);
  if (bufSize > MAX_CONTROL_SIZE)
    return 0;
  memset (&strm, 0, sizeof (z_stream));
  strm.next_in = (Bytef *) data;
  strm.avail_in = size;
  strm.total_in = 0;
  strm.zalloc = &Emalloc;
  strm.zfree = &Efree;
  strm.opaque = NULL;

  if (Z_OK == inflateInit2 (&strm, 15 + 32))
    {
      buf = malloc (bufSize);
      if (buf == NULL)
        {
          inflateEnd (&strm);
          return 0;
        }
      strm.next_out = (Bytef *) buf;
      strm.avail_out = bufSize;
      inflate (&strm, Z_FINISH);
      if (strm.total_out > 0)
        {
          ret = processControlTar (buf, strm.total_out, proc, proc_cls);
          inflateEnd (&strm);
          free (buf);
          return ret;
        }
      free (buf);
      inflateEnd (&strm);
    }
  return 0;
}

typedef struct
{
  char name[16];
  char lastModTime[12];
  char userId[6];
  char groupId[6];
  char modeInOctal[8];
  char filesize[10];
  char trailer[2];
} ObjectHeader;


int 
EXTRACTOR_deb_extract (const char *data,
		       size_t size,
		       EXTRACTOR_MetaDataProcessor proc,
		       void *proc_cls,
		       const char *options)
{
  size_t pos;
  int done = 0;
  ObjectHeader *hdr;
  unsigned long long fsize;
  char buf[11];

  if (size < 128)
    return 0;
  if (0 != strncmp ("!<arch>\n", data, strlen ("!<arch>\n")))
    return 0;
  pos = strlen ("!<arch>\n");
  while (pos + sizeof (ObjectHeader) < size)
    {
      hdr = (ObjectHeader *) & data[pos];
      if (0 != strncmp (&hdr->trailer[0], "`\n", 2))
        return 0;
      memcpy (buf, &hdr->filesize[0], 10);
      buf[10] = '\0';
      if (1 != sscanf (buf, "%10llu", &fsize))
        return 0;
      pos += sizeof (ObjectHeader);
      if ((pos + fsize > size) || (fsize > size) || (pos + fsize < pos))
        return 0;
      if (0 == strncmp (&hdr->name[0],
                        "control.tar.gz", strlen ("control.tar.gz")))
        {
          if (0 != processControlTGZ ((const unsigned char *) &data[pos],
				      fsize, proc, proc_cls))
	    return 1;
          done++;
        }
      if (0 == strncmp (&hdr->name[0],
                        "debian-binary", strlen ("debian-binary")))
        {
	  if (0 != proc (proc_cls, 
			 "deb",
			 EXTRACTOR_METATYPE_MIMETYPE,
			 EXTRACTOR_METAFORMAT_UTF8,
			 "text/plain",
			 "application/x-debian-package",
			 strlen ("application/x-debian-package")+1))
	    return 1;
          done++;
        }
      pos += fsize;
      if (done == 2)
        break;                  /* no need to process the rest of the archive */
    }
  return 0;
}