zipfile.c
来自「infozip2.2源码」· C语言 代码 · 共 1,465 行 · 第 1/3 页
C
1,465 行
/* Copyright (C) 1990-1997 Mark Adler, Richard B. Wales, Jean-loup Gailly, Kai Uwe Rommel, Onno van der Linden and Igor Mandrichenko. Permission is granted to any individual or institution to use, copy, or redistribute this software so long as all of the original files are included, that it is not sold for profit, and that this copyright notice is retained.*//* * zipfile.c by Mark Adler. */#define NOCPYRT /* this is not a main module */#include "zip.h"#include "revision.h"#ifdef VMS# include <rms.h># include <starlet.h># include "vms/vmsmunch.h"# include "vms/vmsdefs.h"#endif/* * XXX start of zipfile.h *//* Macros for converting integers in little-endian to machine format */#define SH(a) (((ush)(uch)(a)[0]) | (((ush)(uch)(a)[1]) << 8))#define LG(a) ((ulg)SH(a) | ((ulg)SH((a)+2) << 16))/* Macros for writing machine integers to little-endian format */#define PUTSH(a,f) {putc((char)((a) & 0xff),(f)); putc((char)((a) >> 8),(f));}#define PUTLG(a,f) {PUTSH((a) & 0xffff,(f)) PUTSH((a) >> 16,(f))}/* -- Structure of a ZIP file -- *//* Signatures for zip file information headers */#define LOCSIG 0x04034b50L#define CENSIG 0x02014b50L#define ENDSIG 0x06054b50L#define EXTLOCSIG 0x08074b50L/* Offsets of values in headers */#define LOCVER 0 /* version needed to extract */#define LOCFLG 2 /* encrypt, deflate flags */#define LOCHOW 4 /* compression method */#define LOCTIM 6 /* last modified file time, DOS format */#define LOCDAT 8 /* last modified file date, DOS format */#define LOCCRC 10 /* uncompressed crc-32 for file */#define LOCSIZ 14 /* compressed size in zip file */#define LOCLEN 18 /* uncompressed size */#define LOCNAM 22 /* length of filename */#define LOCEXT 24 /* length of extra field */#define EXTCRC 0 /* uncompressed crc-32 for file */#define EXTSIZ 4 /* compressed size in zip file */#define EXTLEN 8 /* uncompressed size */#define CENVEM 0 /* version made by */#define CENVER 2 /* version needed to extract */#define CENFLG 4 /* encrypt, deflate flags */#define CENHOW 6 /* compression method */#define CENTIM 8 /* last modified file time, DOS format */#define CENDAT 10 /* last modified file date, DOS format */#define CENCRC 12 /* uncompressed crc-32 for file */#define CENSIZ 16 /* compressed size in zip file */#define CENLEN 20 /* uncompressed size */#define CENNAM 24 /* length of filename */#define CENEXT 26 /* length of extra field */#define CENCOM 28 /* file comment length */#define CENDSK 30 /* disk number start */#define CENATT 32 /* internal file attributes */#define CENATX 34 /* external file attributes */#define CENOFF 38 /* relative offset of local header */#define ENDDSK 0 /* number of this disk */#define ENDBEG 2 /* number of the starting disk */#define ENDSUB 4 /* entries on this disk */#define ENDTOT 6 /* total number of entries */#define ENDSIZ 8 /* size of entire central directory */#define ENDOFF 12 /* offset of central on starting disk */#define ENDCOM 16 /* length of zip file comment *//* Local functions */local int zqcmp OF((ZCONST zvoid *, ZCONST zvoid *));#ifndef UTIL local void zipoddities OF((struct zlist far *)); local int readzipfile2 OF((void)); local int rqcmp OF((ZCONST zvoid *, ZCONST zvoid *)); local int zbcmp OF((ZCONST zvoid *, ZCONST zvoid far *));# ifdef USE_EF_UT_TIME local int ef_scan_ut_time OF((char *ef_buf, extent ef_len, int ef_is_cent, iztimes *z_utim));# endif /* USE_EF_UT_TIME */ local void cutpath OF((char *p));#endif /* !UTIL *//* * XXX end of zipfile.h *//* Local data */#ifdef HANDLE_AMIGA_SFX ulg amiga_sfx_offset; /* place where size field needs updating */#endiflocal int zqcmp(a, b)ZCONST zvoid *a, *b; /* pointers to pointers to zip entries *//* Used by qsort() to compare entries in the zfile list. * Compares the internal names z->iname */{ return namecmp((*(struct zlist far **)a)->iname, (*(struct zlist far **)b)->iname);}#ifndef UTILlocal int rqcmp(a, b)ZCONST zvoid *a, *b; /* pointers to pointers to zip entries *//* Used by qsort() to compare entries in the zfile list. * Compare the internal names z->iname, but in reverse order. */{ return namecmp((*(struct zlist far **)b)->iname, (*(struct zlist far **)a)->iname);}local int zbcmp(n, z)ZCONST zvoid *n; /* string to search for */ZCONST zvoid far *z; /* pointer to a pointer to a zip entry *//* Used by search() to compare a target to an entry in the zfile list. */{ return namecmp((char *)n, ((struct zlist far *)z)->zname);}struct zlist far *zsearch(n)char *n; /* name to find *//* Return a pointer to the entry in zfile with the name n, or NULL if not found. */{ zvoid far **p; /* result of search() */ if (zcount && (p = search(n, (zvoid far **)zsort, zcount, zbcmp)) != NULL) return *(struct zlist far **)p; else return NULL;}#endif /* !UTIL */#ifndef VMS# define PATHCUT '/'char *ziptyp(s)char *s; /* file name to force to zip *//* If the file name *s has a dot (other than the first char), or if the -A option is used (adjust self-extracting file) then return the name, otherwise append .zip to the name. Allocate the space for the name in either case. Return a pointer to the new name, or NULL if malloc() fails. */{ char *q; /* temporary pointer */ char *t; /* pointer to malloc'ed string */ if ((t = malloc(strlen(s) + 5)) == NULL) return NULL; strcpy(t, s);#ifdef __human68k__ _toslash(t);#endif#ifdef MSDOS for (q = t; *q; q++) if (*q == '\\') *q = '/';#endif /* MSDOS */ if (adjust) return t;#ifndef RISCOS# ifndef QDOS# ifdef AMIGA if ((q = strrchr(t, '/')) == NULL) q = strrchr(t, ':'); if (strrchr((q ? q + 1 : t), '.') == NULL)# else /* !AMIGA */# ifdef TANDEM if (strrchr((q = strrchr(t, '.')) == NULL ? t : q + 1, ' ') == NULL)# else /* !TANDEM */ if (strrchr((q = strrchr(t, PATHCUT)) == NULL ? t : q + 1, '.') == NULL)# endif /* TANDEM */# endif /* ?AMIGA */# ifdef CMS_MVS if (strncmp(t,"dd:",3) != 0 && strncmp(t,"DD:",3) != 0)# endif /* CMS_MVS */# ifdef TANDEM /* Tandem can't cope with extensions */ strcat(t, " ZIP");# else /* !TANDEM */ strcat(t, ".zip");# endif /* !TANDEM */# else q = LastDir(t); if(strrchr(q, '_') == NULL && strrchr(q, '.') == NULL) { strcat(t, "_zip"); }# endif /* QDOS */#endif /* !RISCOS */ return t;}#else /* VMS */# define PATHCUT ']'char *ziptyp(s)char *s;{ int status; struct FAB fab; struct NAM nam; static char zero=0; char result[NAM$C_MAXRSS+1],exp[NAM$C_MAXRSS+1]; char *p; fab = cc$rms_fab; nam = cc$rms_nam; fab.fab$l_fna = s; fab.fab$b_fns = strlen(fab.fab$l_fna); fab.fab$l_dna = "sys$disk:[].zip"; /* Default fspec */ fab.fab$b_dns = strlen(fab.fab$l_dna); fab.fab$l_nam = &nam; nam.nam$l_rsa = result; /* Put resultant name of */ nam.nam$b_rss = sizeof(result)-1; /* existing zipfile here */ nam.nam$l_esa = exp; /* For full spec of */ nam.nam$b_ess = sizeof(exp)-1; /* file to create */ status = sys$parse(&fab); if( (status & 1) == 0 ) return &zero; status = sys$search(&fab); if( status & 1 ) { /* Existing ZIP file */ int l; if( (p=malloc( (l=nam.nam$b_rsl) + 1 )) != NULL ) { result[l] = 0; strcpy(p,result); } } else { /* New ZIP file */ int l; if( (p=malloc( (l=nam.nam$b_esl) + 1 )) != NULL ) { exp[l] = 0; strcpy(p,exp); } } return p;}#endif /* VMS */#ifndef UTILlocal void zipoddities(z)struct zlist far *z;{ if ((z->vem >> 8) >= NUM_HOSTS) { sprintf(errbuf, "made by version %d.%d on system type %d: ", (ush)(z->vem & 0xff) / (ush)10, (ush)(z->vem & 0xff) % (ush)10, z->vem >> 8); zipwarn(errbuf, z->zname); } if (z->ver != 10 && z->ver != 11 && z->ver != 20) { sprintf(errbuf, "needs unzip %d.%d on system type %d: ", (ush)(z->ver & 0xff) / (ush)10, (ush)(z->ver & 0xff) % (ush)10, z->ver >> 8); zipwarn(errbuf, z->zname); } if (z->flg != z->lflg) { sprintf(errbuf, "local flags = 0x%04x, central = 0x%04x: ", z->lflg, z->flg); zipwarn(errbuf, z->zname); } else if (z->flg & ~0xf) { sprintf(errbuf, "undefined bits used in flags = 0x%04x: ", z->flg); zipwarn(errbuf, z->zname); } if (z->how > DEFLATE) { sprintf(errbuf, "unknown compression method %u: ", z->how); zipwarn(errbuf, z->zname); } if (z->dsk) { sprintf(errbuf, "starts on disk %u: ", z->dsk); zipwarn(errbuf, z->zname); } if (z->att!=ASCII && z->att!=BINARY && z->att!=__EBCDIC) { sprintf(errbuf, "unknown internal attributes = 0x%04x: ", z->att); zipwarn(errbuf, z->zname); }#if 0/* This test is ridiculous, it produces an error message for almost every *//* platform of origin other than MS-DOS, Unix, VMS, and Acorn! Perhaps *//* we could test "if (z->dosflag && z->atx & ~0xffL)", but what for? */ if (((n = z->vem >> 8) != 3) && n != 2 && n != 13 && z->atx & ~0xffL) { sprintf(errbuf, "unknown external attributes = 0x%08lx: ", z->atx); zipwarn(errbuf, z->zname); }#endif if (z->ext || z->cext) if (z->ext && z->cext && z->extra != z->cextra) { sprintf(errbuf, "local extra (%ld bytes) != central extra (%ld bytes): ", (ulg)z->ext, (ulg)z->cext); zipwarn(errbuf, z->zname); }#ifndef RISCOS else if (noisy)#else /* RISCOS *//* avoid warnings on zipfiles created on RISCOS itself! *//* probably this can be made more generic with z->vem != OS_CODE *//* or, was this warning really intended (eg. OS/2)? */ else if (noisy && ((z->vem >> 8) != 13))#endif { fprintf(stderr, "zip info: %s has %ld bytes of %sextra data\n", z->zname, z->ext ? (ulg)z->ext : (ulg)z->cext, z->ext ? (z->cext ? "" : "local ") : "central "); }}/* * readzipfile2 is called with zip -F or zip -FF * read the file from front to back and pick up the pieces * NOTE: there are still checks missing to see if the header * that was found is *VALID* */local int readzipfile2()/* The name of the zip file is pointed to by the global "zipfile". The globals zfiles, zcount, zcomlen, zcomment, and zsort are filled in. Return an error code in the ZE_ class.*/{ ulg a = 0L; /* attributes returned by filetime() */ char b[CENHEAD]; /* buffer for central headers */ FILE *f; /* zip file */ ush flg; /* general purpose bit flag */ int m; /* mismatch flag */ extent n; /* length of name */ ulg p; /* current file offset */ ulg s; /* size of data, start of central */ struct zlist far * far *x; /* pointer last entry's link */ struct zlist far *z; /* current zip entry structure */ /* Initialize zip file info */ zipbeg = 0; zfiles = NULL; /* Points to first header */ zcomlen = 0; /* zip file comment length */ /* If zip file exists, read headers and check structure */#ifdef VMS if (zipfile == NULL || !(*zipfile) || !strcmp(zipfile, "-")) return ZE_OK; { int rtype; if ((VMSmunch(zipfile, GET_RTYPE, (char *)&rtype) == RMS$_NORMAL) && (rtype == FAT$C_VARIABLE)) { fprintf(stderr, "\n Error: zipfile is in variable-length record format. Please\n\ run \"bilf b %s\" to convert the zipfile to fixed-length\n\ record format.\n\n", zipfile); return ZE_FORM; } } if ((f = fopen(zipfile, FOPR)) != NULL)#else /* !VMS */ if (zipfile != NULL && *zipfile && strcmp(zipfile, "-") && (f = fopen(zipfile, FOPR)) != NULL)#endif /* ?VMS */ { /* Get any file attribute valid for this OS, to set in the central * directory when fixing the archive: */#ifndef UTIL filetime(zipfile, &a, (long*)&s, NULL);#endif x = &zfiles; /* first link */ p = 0; /* starting file offset */ zcount = 0; /* number of files */#ifdef HANDLE_AMIGA_SFX amiga_sfx_offset = 0L;#endif /* Find start of zip structures */ for (;;) { while ((m = getc(f)) != EOF && m != 0x50) /* 0x50 == 'P' */ {#ifdef HANDLE_AMIGA_SFX if (p == 0 && m == 0) amiga_sfx_offset = 1L; else if (amiga_sfx_offset) { if ((p == 1 && m != 0) || (p == 2 && m != 3) || (p == 3 && (uch) m != 0xF3)) amiga_sfx_offset = 0L; }#endif /* HANDLE_AMIGA_SFX */ p++; } b[0] = (char) m; if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG || s == ENDSIG) break; if (fseek(f, -3L, SEEK_CUR)) return ferror(f) ? ZE_READ : ZE_EOF; p++; } zipbeg = p;#ifdef HANDLE_AMIGA_SFX if (amiga_sfx_offset && zipbeg >= 12 && (zipbeg & 3) == 0 && fseek(f, -12L, SEEK_CUR) == 0 && fread(b, 12, 1, f) == 1 && LG(b + 4) == 0xF1030000 /* 1009 in Motorola byte order */) amiga_sfx_offset = zipbeg - 4; else amiga_sfx_offset = 0L;#endif /* HANDLE_AMIGA_SFX */ /* Read local headers */ while (LG(b) == LOCSIG) { if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) return ZE_MEM; if (fread(b, LOCHEAD, 1, f) != 1) { farfree((zvoid far *)z); break; } z->ver = SH(LOCVER + b); z->vem = dosify ? 20 : OS_CODE + Z_MAJORVER * 10 + Z_MINORVER; z->dosflag = dosify; flg = z->flg = z->lflg = SH(LOCFLG + b); z->how = SH(LOCHOW + b); z->tim = LG(LOCTIM + b); /* time and date into one long */ z->crc = LG(LOCCRC + b); z->siz = LG(LOCSIZ + b); z->len = LG(LOCLEN + b); n = z->nam = SH(LOCNAM + b); z->cext = z->ext = SH(LOCEXT + b); z->com = 0; z->dsk = 0; z->att = 0; z->atx = dosify ? a & 0xff : a; /* Attributes from filetime() */ z->mark = 0; z->trash = 0; s = fix > 1 ? 0L : z->siz; /* discard compressed size with -FF */ /* Initialize all fields pointing to malloced data to NULL */ z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL; /* Link into list */ *x = z; z->nxt = NULL; x = &z->nxt; /* Read file name and extra field and skip data */ if (n == 0) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?