debextractor.c File Reference

#include "platform.h"
#include "extractor.h"
#include <zlib.h>

Go to the source code of this file.

Data Structures

struct  Matches
struct  TarHeader
struct  USTarHeader
struct  ObjectHeader

Defines

#define MAX_CONTROL_SIZE   (1024 * 1024)

Functions

static EXTRACTOR_KeywordListaddKeyword (EXTRACTOR_KeywordType type, char *keyword, EXTRACTOR_KeywordList *next)
static char * stndup (const char *str, size_t n)
static struct EXTRACTOR_KeywordsprocessControl (const char *data, const size_t size, struct EXTRACTOR_Keywords *prev)
static struct EXTRACTOR_KeywordsprocessControlTar (const char *data, const size_t size, struct EXTRACTOR_Keywords *prev)
static voidpf Emalloc (voidpf opaque, uInt items, uInt size)
static void Efree (voidpf opaque, voidpf ptr)
static struct EXTRACTOR_KeywordsprocessControlTGZ (const unsigned char *data, size_t size, struct EXTRACTOR_Keywords *prev)
EXTRACTOR_Keywordslibextractor_deb_extract (const char *filename, const char *data, const size_t size, struct EXTRACTOR_Keywords *prev)

Variables

static Matches tmap []


Define Documentation

#define MAX_CONTROL_SIZE   (1024 * 1024)

Definition at line 224 of file debextractor.c.

Referenced by processControlTGZ().


Function Documentation

static EXTRACTOR_KeywordList* addKeyword ( EXTRACTOR_KeywordType  type,
char *  keyword,
EXTRACTOR_KeywordList next 
) [static]

Definition at line 38 of file debextractor.c.

References EXTRACTOR_Keywords::keyword, EXTRACTOR_Keywords::keywordType, malloc, EXTRACTOR_Keywords::next, and NULL.

00040 {
00041   EXTRACTOR_KeywordList *result;
00042 
00043   if (keyword == NULL)
00044     return next;
00045   result = malloc (sizeof (EXTRACTOR_KeywordList));
00046   result->next = next;
00047   result->keyword = keyword;
00048   result->keywordType = type;
00049   return result;
00050 }

static void Efree ( voidpf  opaque,
voidpf  ptr 
) [static]

Definition at line 233 of file debextractor.c.

References free.

Referenced by processControlTGZ().

00234 {
00235   free (ptr);
00236 }

static voidpf Emalloc ( voidpf  opaque,
uInt  items,
uInt  size 
) [static]

Definition at line 227 of file debextractor.c.

References malloc.

Referenced by processControlTGZ().

00228 {
00229   return malloc (size * items);
00230 }

struct EXTRACTOR_Keywords* libextractor_deb_extract ( const char *  filename,
const char *  data,
const size_t  size,
struct EXTRACTOR_Keywords prev 
)

Definition at line 300 of file debextractor.c.

References addKeyword(), done, EXTRACTOR_MIMETYPE, ObjectHeader::filesize, ObjectHeader::name, processControlTGZ(), and ObjectHeader::trailer.

00303 {
00304   size_t pos;
00305   int done = 0;
00306 
00307   if (size < 128)
00308     return prev;
00309   if (0 != strncmp ("!<arch>\n", data, strlen ("!<arch>\n")))
00310     return prev;
00311   pos = strlen ("!<arch>\n");
00312   while (pos + sizeof (ObjectHeader) < size)
00313     {
00314       ObjectHeader *hdr;
00315       unsigned long long fsize;
00316       char buf[11];
00317 
00318       hdr = (ObjectHeader *) & data[pos];
00319       if (0 != strncmp (&hdr->trailer[0], "`\n", 2))
00320         return prev;
00321 
00322       memcpy (buf, &hdr->filesize[0], 10);
00323       buf[10] = '\0';
00324       if (1 != sscanf (buf, "%10llu", &fsize))
00325         return prev;
00326       pos += sizeof (ObjectHeader);
00327       if ((pos + fsize > size) || (fsize > size) || (pos + fsize < pos))
00328         return prev;
00329       if (0 == strncmp (&hdr->name[0],
00330                         "control.tar.gz", strlen ("control.tar.gz")))
00331         {
00332           prev = processControlTGZ ((const unsigned char *) &data[pos],
00333                                     fsize, prev);
00334           done++;
00335         }
00336       if (0 == strncmp (&hdr->name[0],
00337                         "debian-binary", strlen ("debian-binary")))
00338         {
00339           prev = addKeyword (EXTRACTOR_MIMETYPE,
00340                              strdup ("application/x-debian-package"), prev);
00341           done++;
00342         }
00343       pos += fsize;
00344       if (done == 2)
00345         break;                  /* no need to process the rest of the archive */
00346     }
00347   return prev;
00348 }

static struct EXTRACTOR_Keywords* processControl ( const char *  data,
const size_t  size,
struct EXTRACTOR_Keywords prev 
) [static]

Process the control file.

Definition at line 97 of file debextractor.c.

References addKeyword(), free, NULL, stndup(), tmap, and type.

Referenced by processControlTar().

00099 {
00100   size_t pos;
00101   char *key;
00102 
00103   pos = 0;
00104   while (pos < size)
00105     {
00106       size_t colon;
00107       size_t eol;
00108       int i;
00109 
00110       colon = pos;
00111       while (data[colon] != ':')
00112         {
00113           if ((colon > size) || (data[colon] == '\n'))
00114             return prev;
00115           colon++;
00116         }
00117       colon++;
00118       while ((colon < size) && (isspace (data[colon])))
00119         colon++;
00120       eol = colon;
00121       while ((eol < size) &&
00122              ((data[eol] != '\n') ||
00123               ((eol + 1 < size) && (data[eol + 1] == ' '))))
00124         eol++;
00125       if ((eol == colon) || (eol > size))
00126         return prev;
00127       key = stndup (&data[pos], colon - pos);
00128       i = 0;
00129       while (tmap[i].text != NULL)
00130         {
00131           if (0 == strcmp (key, tmap[i].text))
00132             {
00133               char *val;
00134 
00135               val = stndup (&data[colon], eol - colon);
00136               prev = addKeyword (tmap[i].type, val, prev);
00137               break;
00138             }
00139           i++;
00140         }
00141       free (key);
00142       pos = eol + 1;
00143     }
00144   return prev;
00145 }

static struct EXTRACTOR_Keywords* processControlTar ( const char *  data,
const size_t  size,
struct EXTRACTOR_Keywords prev 
) [static]

Process the control.tar file.

Definition at line 177 of file debextractor.c.

References TarHeader::filesize, USTarHeader::magic, TarHeader::name, and processControl().

Referenced by processControlTGZ().

00179 {
00180   TarHeader *tar;
00181   USTarHeader *ustar;
00182   size_t pos;
00183 
00184   pos = 0;
00185   while (pos + sizeof (TarHeader) < size)
00186     {
00187       unsigned long long fsize;
00188       char buf[13];
00189 
00190       tar = (TarHeader *) & data[pos];
00191       if (pos + sizeof (USTarHeader) < size)
00192         {
00193           ustar = (USTarHeader *) & data[pos];
00194           if (0 == strncmp ("ustar", &ustar->magic[0], strlen ("ustar")))
00195             pos += 512;         /* sizeof(USTarHeader); */
00196           else
00197             pos += 257;         /* sizeof(TarHeader); minus gcc alignment... */
00198         }
00199       else
00200         {
00201           pos += 257;           /* sizeof(TarHeader); minus gcc alignment... */
00202         }
00203 
00204       memcpy (buf, &tar->filesize[0], 12);
00205       buf[12] = '\0';
00206       if (1 != sscanf (buf, "%12llo", &fsize))  /* octal! Yuck yuck! */
00207         return prev;
00208       if ((pos + fsize > size) || (fsize > size) || (pos + fsize < pos))
00209         return prev;
00210 
00211       if (0 == strncmp (&tar->name[0], "./control", strlen ("./control")))
00212         {
00213           return processControl (&data[pos], fsize, prev);
00214         }
00215       if ((fsize & 511) != 0)
00216         fsize = (fsize | 511) + 1;      /* round up! */
00217       if (pos + fsize < pos)
00218         return prev;
00219       pos += fsize;
00220     }
00221   return prev;
00222 }

static struct EXTRACTOR_Keywords* processControlTGZ ( const unsigned char *  data,
size_t  size,
struct EXTRACTOR_Keywords prev 
) [static]

Process the control.tar.gz file.

Definition at line 242 of file debextractor.c.

References Efree(), Emalloc(), free, malloc, MAX_CONTROL_SIZE, NULL, and processControlTar().

Referenced by libextractor_deb_extract().

00244 {
00245   size_t bufSize;
00246   char *buf;
00247   z_stream strm;
00248 
00249   bufSize =
00250     data[size - 4] + 256 * data[size - 3] + 65536 * data[size - 2] +
00251     256 * 65536 * data[size - 1];
00252   if (bufSize > MAX_CONTROL_SIZE)
00253     return prev;
00254 
00255   memset (&strm, 0, sizeof (z_stream));
00256 
00257   strm.next_in = (Bytef *) data;
00258   strm.avail_in = size;
00259   strm.total_in = 0;
00260   strm.zalloc = &Emalloc;
00261   strm.zfree = &Efree;
00262   strm.opaque = NULL;
00263 
00264   if (Z_OK == inflateInit2 (&strm, 15 + 32))
00265     {
00266       buf = malloc (bufSize);
00267       if (buf == NULL)
00268         {
00269           inflateEnd (&strm);
00270           return prev;
00271         }
00272       strm.next_out = (Bytef *) buf;
00273       strm.avail_out = bufSize;
00274       inflate (&strm, Z_FINISH);
00275       if (strm.total_out > 0)
00276         {
00277           prev = processControlTar (buf, strm.total_out, prev);
00278           inflateEnd (&strm);
00279           free (buf);
00280           return prev;
00281         }
00282       free (buf);
00283       inflateEnd (&strm);
00284     }
00285   return prev;
00286 }

static char* stndup ( const char *  str,
size_t  n 
) [static]

Definition at line 53 of file debextractor.c.

References malloc.

Referenced by dateDecode(), libextractor_pdf_extract(), libextractor_real_extract(), processControl(), and processiTXt().

00054 {
00055   char *tmp;
00056   tmp = malloc (n + 1);
00057   tmp[n] = '\0';
00058   memcpy (tmp, str, n);
00059   return tmp;
00060 }


Variable Documentation

Matches tmap[] [static]

Initial value:

 {
  {"Package: ", EXTRACTOR_SOFTWARE},
  {"Version: ", EXTRACTOR_VERSIONNUMBER},
  {"Section: ", EXTRACTOR_GENRE},
  {"Priority: ", EXTRACTOR_PRIORITY},
  {"Architecture: ", EXTRACTOR_CREATED_FOR},
  {"Depends: ", EXTRACTOR_DEPENDENCY},
  {"Recommends: ", EXTRACTOR_RELATION},
  {"Suggests: ", EXTRACTOR_RELATION},
  {"Installed-Size: ", EXTRACTOR_SIZE},
  {"Maintainer: ", EXTRACTOR_PACKAGER},
  {"Description: ", EXTRACTOR_DESCRIPTION},
  {"Source: ", EXTRACTOR_SOURCE},
  {"Pre-Depends: ", EXTRACTOR_DEPENDENCY},
  {"Conflicts: ", EXTRACTOR_CONFLICTS},
  {"Replaces: ", EXTRACTOR_REPLACES},
  {"Provides: ", EXTRACTOR_PROVIDES},
  {NULL, 0},
  {"Essential: ", EXTRACTOR_UNKNOWN}
}

Definition at line 71 of file debextractor.c.

Referenced by check(), libextractor_id3v23_extract(), libextractor_id3v24_extract(), libextractor_id3v2_extract(), libextractor_oo_extract(), parseZZZ(), processControl(), and processMetadata().


Generated on Fri Jan 9 16:44:54 2009 for libextractor by  doxygen 1.5.1