ÜberWall

UWfirmforce

/* $Id: cab.c,v 1.6 2011/03/31 05:14:07 khorben Exp $ */
/* Copyright (c) 2011 khorben of Uberwall */
/* This program 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 version 2 of the License.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include "plugin.h"
#define min(a, b) ((a) < (b) ? (a) : (b))
/* cab */
/* private */
/* types */
#pragma pack(1)
struct cab
{
char signature[4];
uint32_t reserved1;
uint32_t cbCabinet;
uint32_t reserved2;
uint32_t coffFiles;
uint32_t reserved3;
uint8_t versionMinor;
uint8_t versionMajor;
uint16_t cFolders;
uint16_t cFiles;
uint16_t flags;
uint16_t setID;
uint16_t iCabinet;
};
#pragma pack()
/* protected */
/* variables */
/* magic */
static unsigned char sig[] = "MSCF";
static PluginMagic cab_magic[] =
{
{ 0, 0, sig, sizeof(sig) - 1 },
{ 0, 0, NULL, 0 }
};
/* prototypes */
static int cab_callback(PluginHelper * ph, int signature, FILE * fp);
/* public */
/* plugin */
Plugin plugin =
{
PT_ARCHIVE | PT_COMPRESSION,
"CAB",
cab_magic,
cab_callback
};
/* protected */
/* functions */
/* cab_callback */
static int _callback_reserved(struct cab * hdr);
static void _callback_size(PluginHelper * ph, struct cab * hdr);
static int _callback_version(PluginHelper * ph, struct cab * hdr);
static void _callback_files(PluginHelper * ph, struct cab * hdr);
static int _callback_flags(PluginHelper * ph, struct cab * hdr);
static void _callback_set(PluginHelper * ph, struct cab * hdr);
static int _callback_extract(PluginHelper * ph, FILE * fp, struct cab * hdr);
static int cab_callback(PluginHelper * ph, int signature, FILE * fp)
{
int score = 0;
struct cab hdr;
if(fread(&hdr, sizeof(hdr), 1, fp) != 1)
return -1;
score += _callback_reserved(&hdr);
_callback_size(ph, &hdr);
score += _callback_version(ph, &hdr);
_callback_files(ph, &hdr);
score += _callback_flags(ph, &hdr);
_callback_set(ph, &hdr);
ph->printf(ph, "\n");
_callback_extract(ph, fp, &hdr);
return score / 3;
}
static int _callback_reserved(struct cab * hdr)
{
int ret = 0;
if(hdr->reserved1 == 0)
ret += 100;
if(hdr->reserved2 == 0)
ret += 100;
if(hdr->reserved3 == 0)
ret += 100;
return ret / 3;
}
static void _callback_size(PluginHelper * ph, struct cab * hdr)
{
hdr->cbCabinet = htol32(hdr->cbCabinet);
ph->printf(ph, "size %u", hdr->cbCabinet);
}
static int _callback_version(PluginHelper * ph, struct cab * hdr)
{
int ret = 0;
if(hdr->versionMajor == 1)
ret += 100;
if(hdr->versionMinor == 3)
ret += 100;
else if(hdr->versionMinor <= 4)
ret += 50;
ph->printf(ph, ", version %u.%u", hdr->versionMajor, hdr->versionMinor);
return ret / 2;
}
static void _callback_files(PluginHelper * ph, struct cab * hdr)
{
hdr->cFolders = htol16(hdr->cFolders);
hdr->cFiles = htol16(hdr->cFiles);
ph->printf(ph, ", %u files in %u folders", hdr->cFiles, hdr->cFolders);
}
static int _callback_flags(PluginHelper * ph, struct cab * hdr)
{
struct
{
unsigned int flag;
char const * string;
} flags[] =
{
{ 0x0001, "PREV_CABINET" },
{ 0x0002, "NEXT_CABINET" },
{ 0x0004, "RESERVE_PRESENT" },
{ 0x0000, NULL }
};
size_t i;
char const * sep = ", flags ";
hdr->flags = htol16(hdr->flags);
for(i = 0; flags[i].string != NULL; i++)
if(hdr->flags & flags[i].flag)
{
ph->printf(ph, "%s%s", sep, flags[i].string);
sep = "|";
}
return ((hdr->flags & 0x0007) == hdr->flags) ? 100 : 0;
}
static void _callback_set(PluginHelper * ph, struct cab * hdr)
{
hdr->setID = htol16(hdr->setID);
hdr->iCabinet = htol16(hdr->iCabinet);
ph->printf(ph, ", set %u, number %u", hdr->setID, hdr->iCabinet);
}
static int _callback_extract(PluginHelper * ph, FILE * fp, struct cab * hdr)
{
FILE * xfp;
char buf[1024];
size_t i;
size_t s;
if((xfp = ph->fopen(ph, "CAB")) == NULL)
return -1;
if(ph->fwrite(hdr, sizeof(*hdr), 1, xfp) != 1)
{
ph->fclose(xfp);
return -1;
}
for(i = sizeof(*hdr); i < hdr->cbCabinet; i += s)
{
s = min(sizeof(buf), hdr->cbCabinet - i);
if((s = fread(buf, sizeof(*buf), s, fp)) == 0)
break;
if(fwrite(buf, sizeof(*buf), s, xfp) != s)
break;
if(s < sizeof(buf))
break;
}
ph->fclose(xfp);
return (ferror(fp) || ferror(xfp)) ? -1 : 0;
}