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
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
#else
typedef enum { FALSE = 0, TRUE = 1 } bool;
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
#include "getopt.h"
#endif
#include <string.h>
#include <errno.h>
static u32 fgetu32 (FILE *f)
{
register u32 r;
r = fgetc(f) << 0;
r |= fgetc(f) << 8;
r |= fgetc(f) << 16;
r |= fgetc(f) << 24;
return r;
}
static u32 fgetu16 (FILE *f)
{
register u32 r;
r = fgetc(f) << 0;
r |= fgetc(f) << 8;
return r;
}
static void parse_zip (char *path)
{
FILE *f = fopen (path, "rb");
if (!f)
{
fprintf (stderr, "skipping '%s': %s\n", path, strerror (errno));
return;
}
while (!feof (f))
{
u32 id = fgetu32 (f);
if (id == 0x04034b50UL)
{
u16 version = fgetu16 (f);
u16 flags = fgetu16 (f);
u16 compression_method = fgetu16 (f);
u16 lastmodtime = fgetu16 (f);
u16 lastmoddate = fgetu16 (f);
u32 crc32 = fgetu32 (f);
u32 compr_size = fgetu32 (f);
u32 uncompr_size = fgetu32 (f);
u16 name_len = fgetu16 (f);
u16 extra_field_len = fgetu16 (f);
char zip_path [1024];
(void)crc32; /* suppress warning */
/* these are unused. */
(void) lastmoddate;
(void) lastmodtime;
(void) compression_method;
(void) version;
if (name_len < 1024)
{
fread (zip_path, name_len, 1, f);
zip_path [name_len] = 0;
}
else
{
fprintf (stderr, "filename too long (>1023 bytes), skipping zipfile\n");
goto out;
}
fseek (f, extra_field_len, SEEK_CUR);
printf ("found file '%s', size %ld (%ld)", zip_path, (long)uncompr_size, (long)compr_size);
if (flags & 1)
printf (", encrypted");
if (compr_size >= 23)
{
u8 file[24];
fread (file, 24, 1, f);
printf ("\n%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
file[0], file[1], file[ 2], file[ 3],
file[4], file[5], file[ 6], file[ 7],
file[8], file[9], file[10], file[11]);
printf (" : %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
file[12], file[13], file[14], file[15],
file[16], file[17], file[18], file[19],
file[20], file[21], file[22], file[23]);
compr_size -= 24;
}
printf ("\n");
fseek (f, compr_size, SEEK_CUR);
}
else if (id == 0x08074b50UL) /* extended local sig (?) */
{
fseek (f, 12, SEEK_CUR);
}
else if (id == 0x30304b50UL)
{
/* ignore */
}
else if (id == 0x02014b50UL || id == 0x06054b50UL)
{
goto out;
}
else
{
fprintf (stderr, "found id %08lx, '%s' is not a zipfile ver 2.xx, skipping\n",
(unsigned long)id, path);
goto out;
}
}
out:
fclose (f);
}
static void usage (int ec)
{
printf ("\n"
PACKAGE " version " VERSION ", zipinfo - tell me about a zip file\n"
"written by Marc Lehmann <pcg@goof.com> You can find more info on\n"
"http://www.goof.com/pcg/marc/\n"
"\n"
"USAGE: zipinfo file... the zipfiles to parse\n"
"\n"
);
exit (ec);
}
static struct option options[] = {
{ "help" , no_argument , 0, 'h' },
{ "version" , no_argument , 0, 'R' },
{ 0 , 0 , 0, 0 },
};
int main (int argc, char *argv[])
{
int option_index = 0;
int c;
while ((c = getopt_long (argc, argv, "", options, &option_index)) != -1)
switch (c)
{
case 'h':
usage (0);
break;
case 'R':
printf ("zipinfo version " VERSION "\n");
exit (0);
}
if (optind >= argc)
{
fprintf (stderr, "you have to specify one or more zip files (try --help)\n");
exit (1);
}
for (; optind < argc; optind++)
parse_zip (argv[optind]);
return 0;
}
|