Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

6821 lines (6078 sloc) 222.43 kB
/*
zipfile.c - Zip 3
Copyright (c) 1990-2008 Info-ZIP. All rights reserved.
See the accompanying file LICENSE, version 2007-Mar-4 or later
(the contents of which are also included in zip.h) for terms of use.
If, for some reason, all these files are missing, the Info-ZIP license
also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
*/
/*
* zipfile.c by Mark Adler.
*/
#define __ZIPFILE_C
#include "zip.h"
#include "revision.h"
#ifdef UNICODE_SUPPORT
# include "crc32.h"
#endif
/* for realloc 2/6/2005 EG */
#include <stdlib.h>
#include <errno.h>
/* for toupper() */
#include <ctype.h>
#ifdef VMS
# include "vms/vms.h"
# include "vms/vmsmunch.h"
# include "vms/vmsdefs.h"
#endif
#ifdef WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
/*
* XXX start of zipfile.h
*/
#ifdef THEOS
/* Macros cause stack overflow in compiler */
ush SH(uch* p) { return ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8)); }
ulg LG(uch* p) { return ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16)); }
#else /* !THEOS */
/* Macros for converting integers in little-endian to machine format */
# define SH(a) ((ush)(((ush)(uch)(a)[0]) | (((ush)(uch)(a)[1]) << 8)))
# define LG(a) ((ulg)SH(a) | ((ulg)SH((a)+2) << 16))
# ifdef ZIP64_SUPPORT /* zip64 support 08/31/2003 R.Nausedat */
# define LLG(a) ((zoff_t)LG(a) | ((zoff_t)LG((a)+4) << 32))
# endif
#endif /* ?THEOS */
/* 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))}
#ifdef ZIP64_SUPPORT /* zip64 support 08/31/2003 R.Nausedat */
# define PUTLLG(a,f) {PUTLG((a) & 0xffffffff,(f)) PUTLG((a) >> 32,(f))}
#endif
/* -- 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 */
/* local header */
#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 */
/* extended local header (data descriptor) following file data (if bit 3 set) */
/* if Zip64 then all are 8 byte and not below - 11/1/03 EG */
#define EXTCRC 0 /* uncompressed crc-32 for file */
#define EXTSIZ 4 /* compressed size in zip file */
#define EXTLEN 8 /* uncompressed size */
/* central directory header */
#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 */
/* end of central directory record */
#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 */
/* zip64 support 08/31/2003 R.Nausedat */
/* EOCDL_SIG used to detect Zip64 archive */
#define ZIP64_EOCDL_SIG 0x07064b50
/* EOCDL size is used in the empty archive check */
#define ZIP64_EOCDL_OFS_SIZE 20
#define ZIP_UWORD16_MAX 0xFFFF /* border value */
#define ZIP_UWORD32_MAX 0xFFFFFFFF /* border value */
#define ZIP_EF_HEADER_SIZE 4 /* size of pre-header of extra fields */
#ifdef ZIP64_SUPPORT
# define ZIP64_EXTCRC 0 /* uncompressed crc-32 for file */
# define ZIP64_EXTSIZ 4 /* compressed size in zip file */
# define ZIP64_EXTLEN 12 /* uncompressed size */
# define ZIP64_EOCD_SIG 0x06064b50
# define ZIP64_EOCD_OFS_SIZE 40
# define ZIP64_EOCD_OFS_CD_START 48
# define ZIP64_EOCDL_OFS_SIZE 20
# define ZIP64_EOCDL_OFS_EOCD_START 8
# define ZIP64_EOCDL_OFS_TOTALDISKS 16
# define ZIP64_MIN_VER 45 /* min version to set in the CD extra records */
# define ZIP64_CENTRAL_DIR_TAIL_SIZE (56 - 8 - 4) /* size of zip64 central dir tail, minus sig and size field bytes */
# define ZIP64_CENTRAL_DIR_TAIL_SIG 0x06064B50L /* zip64 central dir tail signature */
# define ZIP64_CENTRAL_DIR_TAIL_END_SIG 0x07064B50L /* zip64 end of cen dir locator signature */
# define ZIP64_LARGE_FILE_HEAD_SIZE 32 /* total size of zip64 extra field */
# define ZIP64_EF_TAG 0x0001 /* ID for zip64 extra field */
# define ZIP64_EFIELD_OFS_OSIZE ZIP_EF_HEADER_SIZE /* zip64 extra field: offset to original file size */
# define ZIP64_EFIELD_OFS_CSIZE (ZIP64_EFIELD_OFS_OSIZE + 8) /* zip64 extra field: offset to compressed file size */
# define ZIP64_EFIELD_OFS_OFS (ZIP64_EFIELD_OFS_CSIZE + 8) /* zip64 extra field: offset to offset in archive */
# define ZIP64_EFIELD_OFS_DISK (ZIP64_EFIELD_OFS_OFS + 8) /* zip64 extra field: offset to start disk # */
/* -------------------------------------------------------------------------------------------------------------------------- */
local int adjust_zip_local_entry OF((struct zlist far *));
local void adjust_zip_central_entry OF((struct zlist far *));
#if 0
local int remove_local_extra_field OF((struct zlist far *, ulg));
local int remove_central_extra_field OF((struct zlist far *, ulg));
#endif
local int add_central_zip64_extra_field OF((struct zlist far *));
local int add_local_zip64_extra_field OF((struct zlist far *));
#endif /* ZIP64_SUPPORT */
#ifdef UNICODE_SUPPORT
# define UTF8_PATH_EF_TAG 0x7075 /* ID for Unicode path (up) extra field */
local int add_Unicode_Path_local_extra_field OF((struct zlist far *));
local int add_Unicode_Path_cen_extra_field OF((struct zlist far *));
#endif
/* New General Purpose Bit Flag bit 11 flags when entry path and
comment are in UTF-8 */
#define UTF8_BIT (1 << 11)
/* moved out of ZIP64_SUPPORT - 2/6/2005 EG */
local void write_ushort_to_mem OF((ush, char *)); /* little endian conversions */
local void write_ulong_to_mem OF((ulg, char *));
#ifdef ZIP64_SUPPORT
local void write_int64_to_mem OF((uzoff_t, char *));
#endif /* def ZIP64_SUPPORT */
#ifdef UNICODE_SUPPORT
local void write_string_to_mem OF((char *, char *));
#endif
#if 0
local char *get_extra_field OF((ush, char *, unsigned)); /* zip64 */
#endif
#ifdef UNICODE_SUPPORT
local void read_Unicode_Path_entry OF((struct zlist far *));
local void read_Unicode_Path_local_entry OF((struct zlist far *));
#endif
/* added these self allocators - 2/6/2005 EG */
local void append_ushort_to_mem OF((ush, char **, extent *, extent *));
local void append_ulong_to_mem OF((ulg, char **, extent *, extent *));
#ifdef ZIP64_SUPPORT
local void append_int64_to_mem OF((uzoff_t, char **, extent *, extent *));
#endif /* def ZIP64_SUPPORT */
local void append_string_to_mem OF((char *, int, char**, extent *, extent *));
/* Local functions */
local int find_next_signature OF((FILE *f));
local int find_signature OF((FILE *, ZCONST char *));
local int is_signature OF((ZCONST char *, ZCONST char *));
local int at_signature OF((FILE *, ZCONST char *));
local int zqcmp OF((ZCONST zvoid *, ZCONST zvoid *));
#ifdef UNICODE_SUPPORT
local int zuqcmp OF((ZCONST zvoid *, ZCONST zvoid *));
#endif
#if 0
local int scanzipf_reg OF((FILE *f));
#endif
local int scanzipf_regnew OF((void));
#ifndef UTIL
local int rqcmp OF((ZCONST zvoid *, ZCONST zvoid *));
local int zbcmp OF((ZCONST zvoid *, ZCONST zvoid far *));
# ifdef UNICODE_SUPPORT
local int zubcmp OF((ZCONST zvoid *, ZCONST zvoid far *));
# if 0
local int zuebcmp OF((ZCONST zvoid *, ZCONST zvoid far *));
# endif
# endif /* UNICODE_SUPPORT */
local void zipoddities OF((struct zlist far *));
# if 0
local int scanzipf_fix OF((FILE *f));
# endif
local int scanzipf_fixnew OF((void));
# 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, int delim));
#endif /* !UTIL */
/*
* XXX end of zipfile.h
*/
/* Local data */
#ifdef HANDLE_AMIGA_SFX
ulg amiga_sfx_offset; /* place where size field needs updating */
#endif
local 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 */
{
char *aname = (*(struct zlist far **)a)->iname;
char *bname = (*(struct zlist far **)b)->iname;
return namecmp(aname, bname);
}
#ifdef UNICODE_SUPPORT
local int zuqcmp(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->zuname */
{
char *aname = (*(struct zlist far **)a)->iname;
char *bname = (*(struct zlist far **)b)->iname;
/* zuname could be NULL */
if ((*(struct zlist far **)a)->zuname)
aname = (*(struct zlist far **)a)->zuname;
if ((*(struct zlist far **)b)->zuname)
bname = (*(struct zlist far **)b)->zuname;
return namecmp(aname, bname);
}
#endif
#ifndef UTIL
local 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);
}
#ifdef UNICODE_SUPPORT
/* search unicode paths */
local int zubcmp(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. */
{
char *zuname = ((struct zlist far *)z)->zuname;
/* zuname is NULL if no UTF-8 name */
if (zuname == NULL)
zuname = ((struct zlist far *)z)->zname;
return namecmp((char *)n, zuname);
}
#if 0
/* search escaped unicode paths */
local int zuebcmp(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. */
{
char *zuname = ((struct zlist far *)z)->zuname;
char *zuename;
int k;
/* zuname is NULL if no UTF-8 name */
if (zuname == NULL)
zuname = ((struct zlist far *)z)->zname;
zuename = local_to_escape_string(zuname);
k = namecmp((char *)n, zuename);
free(zuename);
return k;
}
#endif
#endif
struct zlist far *zsearch(n)
ZCONST 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) {
if ((p = search(n, (ZCONST zvoid far **)zsort, zcount, zbcmp)) != NULL)
return *(struct zlist far **)p;
#ifdef UNICODE_SUPPORT
else if (unicode_mismatch != 3 && fix != 2 &&
(p = search(n, (ZCONST zvoid far **)zusort, zcount, zubcmp)) != NULL)
return *(struct zlist far **)p;
#endif
else
return NULL;
}
return NULL;
}
#endif /* !UTIL */
#ifndef VMS /* See [.VMS]VMS.C for VMS-specific ziptyp(). */
# ifndef PATHCUT
# define PATHCUT '/'
# endif
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 */
# ifdef THEOS
char *r; /* temporary pointer */
char *disk;
# endif
if ((t = malloc(strlen(s) + 5)) == NULL)
return NULL;
strcpy(t, s);
# ifdef __human68k__
_toslash(t);
# endif
# ifdef MSDOS
for (q = t; *q; INCSTR(q))
if (*q == '\\')
*q = '/';
# endif /* MSDOS */
# if defined(__RSXNT__) || defined(WIN32_CRT_OEM)
/* RSXNT/EMX C rtl uses OEM charset */
AnsiToOem(t, t);
# endif
if (adjust) return t;
# ifndef RISCOS
# ifndef QDOS
# ifdef AMIGA
if ((q = MBSRCHR(t, '/')) == NULL)
q = MBSRCHR(t, ':');
if (MBSRCHR((q ? q + 1 : t), '.') == NULL)
# else /* !AMIGA */
# ifdef THEOS
/* the argument expansion add a dot to the end of file names when
* there is no extension and at least one of a argument has wild cards.
* So check for at least one character in the extension if there is a dot
* in file name */
if ((q = MBSRCHR((q = MBSRCHR(t, PATHCUT)) == NULL ? t : q + 1, '.')) == NULL
|| q[1] == '\0') {
# else /* !THEOS */
# ifdef TANDEM
if (MBSRCHR((q = MBSRCHR(t, '.')) == NULL ? t : q + 1, ' ') == NULL)
# else /* !TANDEM */
if (MBSRCHR((q = MBSRCHR(t, PATHCUT)) == NULL ? t : q + 1, '.') == NULL)
# endif /* ?TANDEM */
# endif /* ?THEOS */
# endif /* ?AMIGA */
# ifdef CMS_MVS
if (strncmp(t,"dd:",3) != 0 && strncmp(t,"DD:",3) != 0)
# endif /* CMS_MVS */
# ifdef THEOS
/* insert .zip extension before disk name */
if ((r = MBSRCHR(t, ':')) != NULL) {
/* save disk name */
if ((disk = strdup(r)) == NULL)
return NULL;
strcpy(r[-1] == '.' ? r - 1 : r, ".zip");
strcat(t, disk);
free(disk);
} else {
if (q != NULL && *q == '.')
strcpy(q, ".zip");
else
strcat(t, ".zip");
}
}
# else /* !THEOS */
# ifdef TANDEM /* Tandem can't cope with extensions */
strcat(t, " ZIP");
# else /* !TANDEM */
strcat(t, ".zip");
# endif /* ?TANDEM */
# endif /* ?THEOS */
# else /* QDOS */
q = LastDir(t);
if(MBSRCHR(q, '_') == NULL && MBSRCHR(q, '.') == NULL)
{
strcat(t, "_zip");
}
# endif /* QDOS */
# endif /* !RISCOS */
return t;
}
#endif /* ndef VMS */
/* ---------------------------------------------------- */
/* moved out of ZIP64_SUPPORT - 2/6/2005 EG */
/* 08/31/2003 R.Nausedat */
local void write_ushort_to_mem( OFT( ush) usValue,
OFT( char *)pPtr)
#ifdef NO_PROTO
ush usValue;
char *pPtr;
#endif /* def NO_PROTO */
{
*pPtr++ = ((char)(usValue) & 0xff);
*pPtr = ((char)(usValue >> 8) & 0xff);
}
local void write_ulong_to_mem(uValue, pPtr)
ulg uValue;
char *pPtr;
{
write_ushort_to_mem((ush)(uValue & 0xffff), pPtr);
write_ushort_to_mem((ush)((uValue >> 16) & 0xffff), pPtr + 2);
}
#ifdef ZIP64_SUPPORT
local void write_int64_to_mem(l64Value,pPtr)
uzoff_t l64Value;
char *pPtr;
{
write_ulong_to_mem((ulg)(l64Value & 0xffffffff),pPtr);
write_ulong_to_mem((ulg)((l64Value >> 32) & 0xffffffff),pPtr + 4);
}
#endif /* def ZIP64_SUPPORT */
#ifdef UNICODE_SUPPORT
/* Write a string to memory */
local void write_string_to_mem(strValue, pPtr)
char *strValue;
char *pPtr;
{
if (strValue != NULL) {
int ssize = strlen(strValue);
int i;
for (i = 0; i < ssize; i++) {
*(pPtr + i) = *(strValue + i);
}
}
}
#endif /* def UNICODE_SUPPORT */
/* same as above but allocate memory as needed and keep track of current end
using offset - 2/6/05 EG */
#if 0 /* ubyte version not used */
local void append_ubyte_to_mem( OFT( unsigned char) ubValue,
OFT( char **) pPtr,
OFT( extent *) offset,
OFT( extent *) blocksize)
#ifdef NO_PROTO
unsigned char ubValue; /* byte to append */
char **pPtr; /* start of block */
extent *offset; /* next byte to write */
extent *blocksize; /* current size of block */
#endif /* def NO_PROTO */
{
if (*pPtr == NULL) {
/* malloc a 1K block */
(*blocksize) = 1024;
*pPtr = (char *) malloc(*blocksize);
if (*pPtr == NULL) {
ziperr(ZE_MEM, "append_ubyte_to_mem");
}
}
/* if (*offset) + 1 > (*blocksize) - 1 */
else if ((*offset) > (*blocksize) - (1 + 1)) {
/* realloc a bigger block in 1 K increments */
(*blocksize) += 1024;
*pPtr = realloc(*pPtr, *blocksize);
if (*pPtr == NULL) {
ziperr(ZE_MEM, "append_ubyte_to_mem");
}
}
*(*pPtr + *offset) = ubValue;
(*offset)++;
}
#endif
local void append_ushort_to_mem( OFT( ush) usValue,
OFT( char **) pPtr,
OFT( extent *) offset,
OFT( extent *) blocksize)
#ifdef NO_PROTO
ush usValue;
char **pPtr;
extent *offset;
extent *blocksize;
#endif /* def NO_PROTO */
{
if (*pPtr == NULL) {
/* malloc a 1K block */
(*blocksize) = 1024;
*pPtr = (char *) malloc(*blocksize);
if (*pPtr == NULL) {
ziperr(ZE_MEM, "append_ushort_to_mem");
}
}
/* if (*offset) + 2 > (*blocksize) - 1 */
else if ((*offset) > (*blocksize) - (1 + 2)) {
/* realloc a bigger block in 1 K increments */
(*blocksize) += 1024;
*pPtr = realloc(*pPtr, (extent)*blocksize);
if (*pPtr == NULL) {
ziperr(ZE_MEM, "append_ushort_to_mem");
}
}
write_ushort_to_mem(usValue, (*pPtr) + (*offset));
(*offset) += 2;
}
local void append_ulong_to_mem(uValue, pPtr, offset, blocksize)
ulg uValue;
char **pPtr;
extent *offset;
extent *blocksize;
{
if (*pPtr == NULL) {
/* malloc a 1K block */
(*blocksize) = 1024;
*pPtr = (char *) malloc(*blocksize);
if (*pPtr == NULL) {
ziperr(ZE_MEM, "append_ulong_to_mem");
}
}
else if ((*offset) > (*blocksize) - (1 + 4)) {
/* realloc a bigger block in 1 K increments */
(*blocksize) += 1024;
*pPtr = realloc(*pPtr, *blocksize);
if (*pPtr == NULL) {
ziperr(ZE_MEM, "append_ulong_to_mem");
}
}
write_ulong_to_mem(uValue, (*pPtr) + (*offset));
(*offset) += 4;
}
#ifdef ZIP64_SUPPORT
local void append_int64_to_mem(l64Value, pPtr, offset, blocksize)
uzoff_t l64Value;
char **pPtr;
extent *offset;
extent *blocksize;
{
if (*pPtr == NULL) {
/* malloc a 1K block */
(*blocksize) = 1024;
*pPtr = (char *) malloc(*blocksize);
if (*pPtr == NULL) {
ziperr(ZE_MEM, "append_int64_to_mem");
}
}
else if ((*offset) > (*blocksize) - (1 + 8)) {
/* realloc a bigger block in 1 K increments */
(*blocksize) += 1024;
*pPtr = realloc(*pPtr, *blocksize);
if (*pPtr == NULL) {
ziperr(ZE_MEM, "append_int64_to_mem");
}
}
write_int64_to_mem(l64Value, (*pPtr) + (*offset));
(*offset) += 8;
}
#endif /* def ZIP64_SUPPORT */
/* Append a string to the memory block. */
local void append_string_to_mem(strValue, strLength, pPtr, offset, blocksize)
char *strValue;
int strLength;
char **pPtr;
extent *offset;
extent *blocksize;
{
if (strValue != NULL) {
unsigned bsize = 1024;
unsigned ssize = strLength;
unsigned i;
if (ssize > bsize) {
bsize = ssize;
}
if (*pPtr == NULL) {
/* malloc a 1K block */
(*blocksize) = bsize;
*pPtr = (char *) malloc(*blocksize);
if (*pPtr == NULL) {
ziperr(ZE_MEM, "append_string_to_mem");
}
}
else if ((*offset) + ssize > (*blocksize) - 1) {
/* realloc a bigger block in 1 K increments */
(*blocksize) += bsize;
*pPtr = realloc(*pPtr, *blocksize);
if (*pPtr == NULL) {
ziperr(ZE_MEM, "append_string_to_mem");
}
}
for (i = 0; i < ssize; i++) {
*(*pPtr + *offset + i) = *(strValue + i);
}
(*offset) += ssize;
}
}
/* ---------------------------------------------------- */
/* zip64 support 08/31/2003 R.Nausedat */
/* moved out of zip64 support 10/22/05 */
/* Searches pExtra for extra field with specified tag.
* If it finds one it returns a pointer to it, else NULL.
* Renamed and made generic. 10/3/03
*/
char *get_extra_field( OFT( ush) tag,
OFT( char *) pExtra,
OFT( unsigned) iExtraLen)
#ifdef NO_PROTO
ush tag; /* tag to look for */
char *pExtra; /* pointer to extra field in memory */
unsigned iExtraLen; /* length of extra field */
#endif /* def NO_PROTO */
{
char *pTemp;
ush usBlockTag;
ush usBlockSize;
if( pExtra == NULL )
return NULL;
for (pTemp = pExtra; pTemp < pExtra + iExtraLen - ZIP_EF_HEADER_SIZE;)
{
usBlockTag = SH(pTemp); /* get tag */
usBlockSize = SH(pTemp + 2); /* get field data size */
if (usBlockTag == tag)
return pTemp;
pTemp += (usBlockSize + ZIP_EF_HEADER_SIZE);
}
return NULL;
}
/* copy_nondup_extra_fields
*
* Copy any extra fields in old that are not in new to new.
* Returns the new extra fields block and newLen is new length.
*/
char *copy_nondup_extra_fields(oldExtra, oldExtraLen, newExtra, newExtraLen, newLen)
char *oldExtra; /* pointer to old extra fields */
unsigned oldExtraLen; /* length of old extra fields */
char *newExtra; /* pointer to new extra fields */
unsigned newExtraLen; /* length of new extra fields */
unsigned *newLen; /* length of new extra fields after copy */
{
char *returnExtra = NULL;
ush returnExtraLen = 0;
char *tempExtra;
char *pTemp;
ush tag;
ush blocksize;
if( oldExtra == NULL ) {
/* no old extra fields so return copy of newExtra */
if (newExtra == NULL || newExtraLen == 0) {
*newLen = 0;
return NULL;
} else {
if ((returnExtra = malloc(newExtraLen)) == NULL)
ZIPERR(ZE_MEM, "extra field copy");
memcpy(returnExtra, newExtra, newExtraLen);
returnExtraLen = newExtraLen;
*newLen = returnExtraLen;
return returnExtra;
}
}
/* allocate block large enough for all extra fields */
if ((tempExtra = malloc(0xFFFF)) == NULL)
ZIPERR(ZE_MEM, "extra field copy");
/* look for each old extra field in new block */
for (pTemp = oldExtra; pTemp < oldExtra + oldExtraLen;)
{
tag = SH(pTemp); /* get tag */
blocksize = SH(pTemp + 2); /* get field data size */
if (get_extra_field(tag, newExtra, newExtraLen) == NULL) {
/* tag not in new block so add it */
memcpy(tempExtra + returnExtraLen, pTemp, blocksize + 4);
returnExtraLen += blocksize + 4;
}
pTemp += blocksize + 4;
}
/* copy all extra fields from new block */
memcpy(tempExtra + returnExtraLen, newExtra, newExtraLen);
returnExtraLen += newExtraLen;
/* copy tempExtra to returnExtra */
if ((returnExtra = malloc(returnExtraLen)) == NULL)
ZIPERR(ZE_MEM, "extra field copy");
memcpy(returnExtra, tempExtra, returnExtraLen);
free(tempExtra);
*newLen = returnExtraLen;
return returnExtra;
}
#ifdef UNICODE_SUPPORT
/* The latest format is
1 byte Version of Unicode Path Extra Field
4 bytes Name Field CRC32 Checksum
variable UTF-8 Version Of Name
*/
local void read_Unicode_Path_entry(pZipListEntry)
struct zlist far *pZipListEntry;
{
char *pTemp;
char *UPath;
char *iname;
ush ELen;
uch Version;
ush ULen;
ulg chksum = CRCVAL_INITIAL;
ulg iname_chksum;
/* check if we have a Unicode Path extra field ... */
pTemp = get_extra_field( UTF8_PATH_EF_TAG, pZipListEntry->cextra, pZipListEntry->cext );
pZipListEntry->uname = NULL;
if( pTemp == NULL ) {
return;
}
/* ... if so, update corresponding entries in struct zlist */
pTemp += 2;
/* length of this extra field */
ELen = SH(pTemp);
pTemp += 2;
/* version */
Version = (uch) *pTemp;
pTemp += 1;
if (Version > 1) {
zipwarn("Unicode Path Extra Field version > 1 - skipping", pZipListEntry->oname);
return;
}
/* iname CRC */
iname_chksum = LG(pTemp);
pTemp += 4;
/*
* Compute the CRC-32 checksum of iname
*/
/*
crc_16 = crc16f((uch *)(pZipListEntry->iname), strlen(pZipListEntry->iname));
*/
if ((iname = malloc(strlen(pZipListEntry->iname) + 1)) == NULL) {
ZIPERR(ZE_MEM, "write Unicode");
}
strcpy(iname, pZipListEntry->iname);
chksum = crc32(chksum, (uch *)(iname), strlen(iname));
free(iname);
/* chksum = adler16(ADLERVAL_INITIAL,
(uch *)(pZipListEntry->iname), strlen(pZipListEntry->iname));
*/
/* If the checksums's don't match then likely iname has been modified and
* the Unicode Path is no longer valid
*/
if (chksum != iname_chksum) {
printf("unicode_mismatch = %d\n", unicode_mismatch);
if (unicode_mismatch == 1) {
/* warn and continue */
zipwarn("Unicode does not match path - ignoring Unicode: ", pZipListEntry->oname);
} else if (unicode_mismatch == 2) {
/* ignore and continue */
} else if (unicode_mismatch == 0) {
/* error */
sprintf(errbuf, "Unicode does not match path: %s\n", pZipListEntry->oname);
strcat(errbuf,
" Likely entry name changed but Unicode not updated\n");
strcat(errbuf,
" Use -UN=i to ignore errors or n for no Unicode paths");
zipwarn(errbuf, "");
ZIPERR(ZE_FORM, "Unicode path error");
}
return;
}
ULen = ELen - 5;
/* UTF-8 Path */
if (ULen == 0) {
/* standard path is UTF-8 so use that */
ULen = pZipListEntry->nam;
if ((UPath = malloc(ULen + 1)) == NULL) {
return;
}
strcpy(UPath, pZipListEntry->name);
} else {
/* use Unicode path */
if ((UPath = malloc(ULen + 1)) == NULL) {
return;
}
strncpy(UPath, pTemp, ULen);
UPath[ULen] = '\0';
}
pZipListEntry->uname = UPath;
return;
}
local void read_Unicode_Path_local_entry(pZipListEntry)
struct zlist far *pZipListEntry;
{
char *pTemp;
char *UPath;
char *iname;
ush ELen;
uch Version;
ush ULen;
ulg chksum = CRCVAL_INITIAL;
ulg iname_chksum;
/* check if we have a Unicode Path extra field ... */
pTemp = get_extra_field( UTF8_PATH_EF_TAG, pZipListEntry->extra, pZipListEntry->ext );
pZipListEntry->uname = NULL;
if( pTemp == NULL ) {
return;
}
/* ... if so, update corresponding entries in struct zlist */
pTemp += 2;
/* length of this extra field */
ELen = SH(pTemp);
pTemp += 2;
/* version */
Version = (uch) *pTemp;
pTemp += 1;
if (Version > 1) {
zipwarn("Unicode Path Extra Field version > 1 - skipping", pZipListEntry->oname);
return;
}
/* iname CRC */
iname_chksum = LG(pTemp);
pTemp += 4;
/*
* Compute 32-bit crc of iname and AND halves to make 16-bit version
*/
/*
chksum = adler16(ADLERVAL_INITIAL,
(uch *)(pZipListEntry->iname), strlen(pZipListEntry->iname));
*/
if ((iname = malloc(strlen(pZipListEntry->iname) + 1)) == NULL) {
ZIPERR(ZE_MEM, "write Unicode");
}
strcpy(iname, pZipListEntry->iname);
chksum = crc32(chksum, (uch *)(iname), strlen(iname));
free(iname);
/* If the checksums's don't match then likely iname has been modified and
* the Unicode Path is no longer valid
*/
if (chksum != iname_chksum) {
if (unicode_mismatch == 1) {
/* warn and continue */
zipwarn("Unicode does not match path - ignoring Unicode: ", pZipListEntry->oname);
} else if (unicode_mismatch == 2) {
/* ignore and continue */
} else if (unicode_mismatch == 0) {
/* error */
sprintf(errbuf, "Unicode does not match path: %s\n", pZipListEntry->oname);
strcat(errbuf,
" Likely entry name changed but Unicode not updated\n");
strcat(errbuf,
" Use -UN=i to ignore errors or n for no Unicode paths");
zipwarn(errbuf, "");
ZIPERR(ZE_FORM, "Unicode path error");
}
return;
}
ULen = ELen - 5;
/* UTF-8 Path */
if (ULen == 0) {
/* standard path is UTF-8 so use that */
ULen = pZipListEntry->nam;
if ((UPath = malloc(ULen + 1)) == NULL) {
return;
}
strcpy(UPath, pZipListEntry->name);
} else {
/* use Unicode path */
if ((UPath = malloc(ULen + 1)) == NULL) {
return;
}
strncpy(UPath, pTemp, ULen);
UPath[ULen] = '\0';
}
pZipListEntry->uname = UPath;
return;
}
#endif /* def UNICODE_SUPPORT */
#ifdef ZIP64_SUPPORT /* zip64 support 08/31/2003 R.Nausedat */
/* searches the cextra member of zlist for a zip64 extra field. if it finds one it */
/* updates the len, siz and off members of zlist with the corresponding values of */
/* the zip64 extra field, that is if either the len, siz or off member of zlist is */
/* set to its max value we have to use the corresponding value from the zip64 extra */
/* field. as of now the dsk member of zlist is not much of interest since we should */
/* not modify multi volume archives at all. */
local void adjust_zip_central_entry(pZipListEntry)
struct zlist far *pZipListEntry;
{
char *pTemp;
/* assume not using zip64 fields */
zip64_entry = 0;
/* check if we have a "large file" Zip64 extra field ... */
pTemp = get_extra_field( ZIP64_EF_TAG, pZipListEntry->cextra, pZipListEntry->cext );
if( pTemp == NULL )
return;
/* using zip64 field */
zip64_entry = 1;
pTemp += ZIP_EF_HEADER_SIZE;
/* ... if so, update corresponding entries in struct zlist */
if (pZipListEntry->len == ZIP_UWORD32_MAX)
{
pZipListEntry->len = LLG(pTemp);
pTemp += 8;
}
if (pZipListEntry->siz == ZIP_UWORD32_MAX)
{
pZipListEntry->siz = LLG(pTemp);
pTemp += 8;
}
if (pZipListEntry->off == ZIP_UWORD32_MAX)
{
pZipListEntry->off = LLG(pTemp);
pTemp += 8;
}
if (pZipListEntry->dsk == ZIP_UWORD16_MAX)
{
pZipListEntry->dsk = LG(pTemp);
}
}
/* adjust_zip_local_entry
*
* Return 1 if there is a Zip64 extra field and 0 if not
*/
local int adjust_zip_local_entry(pZipListEntry)
struct zlist far *pZipListEntry;
{
char *pTemp;
/* assume not using zip64 fields */
zip64_entry = 0;
/* check if we have a "large file" Zip64 extra field ... */
pTemp = get_extra_field(ZIP64_EF_TAG, pZipListEntry->extra, pZipListEntry->ext );
if( pTemp == NULL )
return zip64_entry;
/* using zip64 field */
zip64_entry = 1;
pTemp += ZIP_EF_HEADER_SIZE;
/* ... if so, update corresponding entries in struct zlist */
if (pZipListEntry->len == ZIP_UWORD32_MAX)
{
pZipListEntry->len = LLG(pTemp);
pTemp += 8;
}
if (pZipListEntry->siz == ZIP_UWORD32_MAX)
{
pZipListEntry->siz = LLG(pTemp);
pTemp += 8;
}
return zip64_entry;
}
/* adds a zip64 extra field to the data the cextra member of zlist points to. If
* there is already a zip64 extra field present delete it first.
*/
local int add_central_zip64_extra_field(pZipListEntry)
struct zlist far *pZipListEntry;
{
char *pExtraFieldPtr;
char *pTemp;
ush usTemp;
ush efsize = 0;
ush esize;
ush oldefsize;
extent len;
int used_zip64 = 0;
/* get length of ef based on which fields exceed limits */
/* AppNote says:
* The order of the fields in the ZIP64 extended
* information record is fixed, but the fields will
* only appear if the corresponding Local or Central
* directory record field is set to 0xFFFF or 0xFFFFFFFF.
*/
efsize = ZIP_EF_HEADER_SIZE; /* type + size */
if (pZipListEntry->len > ZIP_UWORD32_MAX || force_zip64 == 1) {
/* compressed size */
efsize += 8;
used_zip64 = 1;
}
if (pZipListEntry->siz > ZIP_UWORD32_MAX) {
/* uncompressed size */
efsize += 8;
used_zip64 = 1;
}
if (pZipListEntry->off > ZIP_UWORD32_MAX) {
/* offset */
efsize += 8;
used_zip64 = 1;
}
if (pZipListEntry->dsk > ZIP_UWORD16_MAX) {
/* disk number */
efsize += 4;
used_zip64 = 1;
}
if (used_zip64 && force_zip64 == 0) {
zipwarn("Large entry support disabled using -fz- but needed", "");
return ZE_BIG;
}
/* malloc zip64 extra field? */
if( pZipListEntry->cextra == NULL )
{
if (efsize == ZIP_EF_HEADER_SIZE) {
return ZE_OK;
}
if ((pExtraFieldPtr = pZipListEntry->cextra = (char *) malloc(efsize)) == NULL) {
return ZE_MEM;
}
pZipListEntry->cext = efsize;
}
else
{
/* check if we have a "large file" extra field ... */
pExtraFieldPtr = get_extra_field(ZIP64_EF_TAG, pZipListEntry->cextra, pZipListEntry->cext);
if( pExtraFieldPtr == NULL )
{
/* ... we don't, so re-malloc enough memory for the old extra data plus
* the size of the zip64 extra field
*/
if ((pExtraFieldPtr = (char *) malloc(efsize + pZipListEntry->cext)) == NULL) {
return ZE_MEM;
}
/* move the old extra field */
memmove(pExtraFieldPtr, pZipListEntry->cextra, pZipListEntry->cext);
free(pZipListEntry->cextra);
pZipListEntry->cextra = pExtraFieldPtr;
pExtraFieldPtr += pZipListEntry->cext;
pZipListEntry->cext += efsize;
}
else
{
/* ... we have. sort out the existing zip64 extra field and remove it from
* pZipListEntry->cextra, re-malloc enough memory for the old extra data
* left plus the size of the zip64 extra field
*/
usTemp = SH(pExtraFieldPtr + 2);
/* if pZipListEntry->cextra == pExtraFieldPtr and pZipListEntry->cext == usTemp + efsize
* we should have only one extra field, and this is a zip64 extra field. as some
* zip tools seem to require fixed zip64 extra fields we have to check if
* usTemp + ZIP_EF_HEADER_SIZE is equal to ZIP64_LARGE_FILE_HEAD_SIZE. if it
* isn't, we free the old extra field and allocate memory for a new one
*/
if( pZipListEntry->cext == (extent)(usTemp + ZIP_EF_HEADER_SIZE) )
{
/* just Zip64 extra field in extra field */
if( pZipListEntry->cext != efsize )
{
/* wrong size */
if ((pExtraFieldPtr = (char *) malloc(efsize)) == NULL) {
return ZE_MEM;
}
free(pZipListEntry->cextra);
pZipListEntry->cextra = pExtraFieldPtr;
pZipListEntry->cext = efsize;
}
}
else
{
/* get the old Zip64 extra field out and add new */
oldefsize = usTemp + ZIP_EF_HEADER_SIZE;
if ((pTemp = (char *) malloc(pZipListEntry->cext - oldefsize + efsize)) == NULL) {
return ZE_MEM;
}
len = (extent)(pExtraFieldPtr - pZipListEntry->cextra);
memcpy(pTemp, pZipListEntry->cextra, len);
memcpy(pTemp + len, pExtraFieldPtr + oldefsize,
pZipListEntry->cext - oldefsize - len);
pZipListEntry->cext -= oldefsize;
pExtraFieldPtr = pTemp + pZipListEntry->cext;
pZipListEntry->cext += efsize;
free(pZipListEntry->cextra);
pZipListEntry->cextra = pTemp;
}
}
}
/* set zip64 extra field members */
write_ushort_to_mem(ZIP64_EF_TAG, pExtraFieldPtr);
write_ushort_to_mem((ush) (efsize - ZIP_EF_HEADER_SIZE), pExtraFieldPtr + 2);
esize = ZIP_EF_HEADER_SIZE;
if (pZipListEntry->len > ZIP_UWORD32_MAX || force_zip64 == 1) {
write_int64_to_mem(pZipListEntry->len, pExtraFieldPtr + esize);
esize += 8;
}
if (pZipListEntry->siz > ZIP_UWORD32_MAX) {
write_int64_to_mem(pZipListEntry->siz, pExtraFieldPtr + esize);
esize += 8;
}
if (pZipListEntry->off > ZIP_UWORD32_MAX) {
write_int64_to_mem(pZipListEntry->off, pExtraFieldPtr + esize);
esize += 8;
}
if (pZipListEntry->dsk > ZIP_UWORD16_MAX) {
write_ulong_to_mem(pZipListEntry->dsk, pExtraFieldPtr + esize);
}
/* un' wech */
return ZE_OK;
}
#if 0
/* Remove extra field in local extra field
* Return 1 if found, else 0
* 12/28/05
*/
local int remove_local_extra_field(pZEntry, tag)
struct zlist far *pZEntry;
ulg tag;
{
char *pExtra;
char *pOldExtra;
char *pOldTemp;
char *pTemp;
ush newEFSize;
ush usTemp;
ush blocksize;
/* check if we have the extra field ... */
pOldExtra = get_extra_field( (ush)tag, pZEntry->extra, pZEntry->ext );
if (pOldExtra)
{
/* We have. Get rid of it. */
blocksize = SH( pOldExtra + 2 );
newEFSize = pZEntry->ext - blocksize;
pExtra = (char *) malloc( newEFSize );
if( pExtra == NULL )
ziperr(ZE_MEM, "Remove Local Extra Field");
/* move all before EF */
usTemp = (extent) (pOldExtra - pZEntry->extra);
pTemp = pExtra;
memcpy( pTemp, pZEntry->extra, usTemp );
/* move all after old Zip64 EF */
pTemp = pExtra + usTemp;
pOldTemp = pOldExtra + blocksize;
usTemp = pZEntry->ext - usTemp - blocksize;
memcpy( pTemp, pOldTemp, usTemp);
/* replace extra fields */
pZEntry->ext = newEFSize;
free(pZEntry->extra);
pZEntry->extra = pExtra;
return 1;
} else {
return 0;
}
}
/* Remove extra field in central extra field
* Return 1 if found, else 0
* 12/28/05
*/
local int remove_central_extra_field(pZEntry, tag)
struct zlist far *pZEntry;
ulg tag;
{
char *pExtra;
char *pOldExtra;
char *pOldTemp;
char *pTemp;
ush newEFSize;
ush usTemp;
ush blocksize;
/* check if we have the extra field ... */
pOldExtra = get_extra_field( (ush)tag, pZEntry->cextra, pZEntry->cext );
if (pOldExtra)
{
/* We have. Get rid of it. */
blocksize = SH( pOldExtra + 2 );
newEFSize = pZEntry->cext - blocksize;
pExtra = (char *) malloc( newEFSize );
if( pExtra == NULL )
ziperr(ZE_MEM, "Remove Local Extra Field");
/* move all before EF */
usTemp = (extent) (pOldExtra - pZEntry->cextra);
pTemp = pExtra;
memcpy( pTemp, pZEntry->cextra, usTemp );
/* move all after old Zip64 EF */
pTemp = pExtra + usTemp;
pOldTemp = pOldExtra + blocksize;
usTemp = pZEntry->cext - usTemp - blocksize;
memcpy( pTemp, pOldTemp, usTemp);
/* replace extra fields */
pZEntry->cext = newEFSize;
free(pZEntry->cextra);
pZEntry->cextra = pExtra;
return 1;
} else {
return 0;
}
}
#endif
/* Add Zip64 extra field to local header
* 10/5/03 EG
*/
local int add_local_zip64_extra_field(pZEntry)
struct zlist far *pZEntry;
{
char *pZ64Extra;
char *pOldZ64Extra;
char *pOldTemp;
char *pTemp;
ush newEFSize;
ush usTemp;
ush blocksize;
ush Z64LocalLen = ZIP_EF_HEADER_SIZE + /* tag + EF Data Len */
8 + /* original uncompressed length of file */
8; /* compressed size of file */
/* malloc zip64 extra field? */
/* after the below pZ64Extra should point to start of Zip64 extra field */
if (pZEntry->ext == 0 || pZEntry->extra == NULL)
{
/* get new extra field */
pZ64Extra = pZEntry->extra = (char *) malloc(Z64LocalLen);
if (pZEntry->extra == NULL) {
ziperr( ZE_MEM, "Zip64 local extra field" );
}
pZEntry->ext = Z64LocalLen;
}
else
{
/* check if we have a Zip64 extra field ... */
pOldZ64Extra = get_extra_field( ZIP64_EF_TAG, pZEntry->extra, pZEntry->ext );
if (pOldZ64Extra == NULL)
{
/* ... we don't, so re-malloc enough memory for the old extra data plus */
/* the size of the zip64 extra field */
pZ64Extra = (char *) malloc( Z64LocalLen + pZEntry->ext );
if (pZ64Extra == NULL)
ziperr( ZE_MEM, "Zip64 Extra Field" );
/* move old extra field and update pointer and length */
memmove( pZ64Extra, pZEntry->extra, pZEntry->ext);
free( pZEntry->extra );
pZEntry->extra = pZ64Extra;
pZ64Extra += pZEntry->ext;
pZEntry->ext += Z64LocalLen;
}
else
{
/* ... we have. Sort out the existing zip64 extra field and remove it
* from pZEntry->extra, re-malloc enough memory for the old extra data
* left plus the size of the zip64 extra field */
blocksize = SH( pOldZ64Extra + 2 );
/* If the right length then go with it, else get rid of it and add a new extra field
* to existing block. */
if (blocksize == Z64LocalLen - ZIP_EF_HEADER_SIZE)
{
/* looks good */
pZ64Extra = pOldZ64Extra;
}
else
{
newEFSize = pZEntry->ext - (blocksize + ZIP_EF_HEADER_SIZE) + Z64LocalLen;
pZ64Extra = (char *) malloc( newEFSize );
if( pZ64Extra == NULL )
ziperr(ZE_MEM, "Zip64 Extra Field");
/* move all before Zip64 EF */
usTemp = (extent) (pOldZ64Extra - pZEntry->extra);
pTemp = pZ64Extra;
memcpy( pTemp, pZEntry->extra, usTemp );
/* move all after old Zip64 EF */
pTemp = pZ64Extra + usTemp;
pOldTemp = pOldZ64Extra + ZIP_EF_HEADER_SIZE + blocksize;
usTemp = pZEntry->ext - usTemp - blocksize;
memcpy( pTemp, pOldTemp, usTemp);
/* replace extra fields */
pZEntry->ext = newEFSize;
free(pZEntry->extra);
pZEntry->extra = pZ64Extra;
pZ64Extra = pTemp + usTemp;
}
}
}
/* set/update zip64 extra field members */
write_ushort_to_mem(ZIP64_EF_TAG, pZ64Extra);
write_ushort_to_mem((ush) (Z64LocalLen - ZIP_EF_HEADER_SIZE), pZ64Extra + 2);
write_int64_to_mem(pZEntry->len, pZ64Extra + 2 + 2);
write_int64_to_mem(pZEntry->siz, pZ64Extra + 2 + 2 + 8);
return ZE_OK;
}
# endif /* ZIP64_SUPPORT */
#ifdef UNICODE_SUPPORT
/* Add UTF-8 path extra field
* 10/11/05
*/
local int add_Unicode_Path_local_extra_field(pZEntry)
struct zlist far *pZEntry;
{
char *pUExtra;
char *pOldUExtra;
char *pOldTemp;
char *pTemp;
#ifdef WIN32_OEM
char *inameLocal;
#endif
ush newEFSize;
ush usTemp;
ush ULen = strlen(pZEntry->uname);
ush blocksize;
ulg chksum = CRCVAL_INITIAL;
ush ULocalLen = ZIP_EF_HEADER_SIZE + /* tag + EF Data Len */
1 + /* version */
4 + /* iname chksum */
ULen; /* UTF-8 path */
/* malloc Unicode Path extra field? */
/* after the below pUExtra should point to start of Unicode Path extra field */
if (pZEntry->ext == 0 || pZEntry->extra == NULL)
{
/* get new extra field */
pUExtra = pZEntry->extra = (char *) malloc(ULocalLen);
if (pZEntry->extra == NULL) {
ziperr( ZE_MEM, "UTF-8 Path local extra field" );
}
pZEntry->ext = ULocalLen;
}
else
{
/* check if we have a Unicode Path extra field ... */
pOldUExtra = get_extra_field( UTF8_PATH_EF_TAG, pZEntry->extra, pZEntry->ext );
if (pOldUExtra == NULL)
{
/* ... we don't, so re-malloc enough memory for the old extra data plus */
/* the size of the UTF-8 Path extra field */
pUExtra = (char *) malloc( ULocalLen + pZEntry->ext );
if (pUExtra == NULL)
ziperr( ZE_MEM, "UTF-8 Path Extra Field" );
/* move old extra field and update pointer and length */
memmove( pUExtra, pZEntry->extra, pZEntry->ext);
free( pZEntry->extra );
pZEntry->extra = pUExtra;
pUExtra += pZEntry->ext;
pZEntry->ext += ULocalLen;
}
else
{
/* ... we have. Sort out the existing UTF-8 Path extra field and remove it
* from pZEntry->extra, re-malloc enough memory for the old extra data
* left plus the size of the UTF-8 Path extra field */
blocksize = SH( pOldUExtra + 2 );
/* If the right length then go with it, else get rid of it and add a new extra field
* to existing block. */
if (blocksize == ULocalLen - ZIP_EF_HEADER_SIZE)
{
/* looks good */
pUExtra = pOldUExtra;
}
else
{
newEFSize = pZEntry->ext - (blocksize + ZIP_EF_HEADER_SIZE) + ULocalLen;
pUExtra = (char *) malloc( newEFSize );
if( pUExtra == NULL )
ziperr(ZE_MEM, "UTF-8 Path Extra Field");
/* move all before UTF-8 Path EF */
usTemp = (extent) (pOldUExtra - pZEntry->extra);
pTemp = pUExtra;
memcpy( pTemp, pZEntry->extra, usTemp );
/* move all after old UTF-8 Path EF */
pTemp = pUExtra + usTemp;
pOldTemp = pOldUExtra + ZIP_EF_HEADER_SIZE + blocksize;
usTemp = pZEntry->ext - usTemp - blocksize;
memcpy( pTemp, pOldTemp, usTemp);
/* replace extra fields */
pZEntry->ext = newEFSize;
free(pZEntry->extra);
pZEntry->extra = pUExtra;
pUExtra = pTemp + usTemp;
}
}
}
/*
* Compute the Adler-16 checksum of iname
*/
/*
chksum = adler16(ADLERVAL_INITIAL,
(uch *)(pZEntry->iname), strlen(pZEntry->iname));
*/
#ifdef WIN32_OEM
if ((inameLocal = malloc(strlen(pZEntry->iname) + 1)) == NULL) {
ZIPERR(ZE_MEM, "write Unicode");
}
/* if oem translation done convert back for checksum */
if ((pZEntry->vem & 0xff00) == 0) {
/* get original */
INTERN_TO_OEM(pZEntry->iname, inameLocal);
} else {
strcpy(inameLocal, pZEntry->iname);
}
#else
# define inameLocal (pZEntry->iname)
#endif
chksum = crc32(chksum, (uch *)(inameLocal), strlen(inameLocal));
#ifdef WIN32_OEM
free(inameLocal);
#else
# undef inameLocal
#endif
/* set/update UTF-8 Path extra field members */
/* tag header */
write_ushort_to_mem(UTF8_PATH_EF_TAG, pUExtra);
/* data size */
write_ushort_to_mem((ush) (ULocalLen - ZIP_EF_HEADER_SIZE), pUExtra + 2);
/* version */
*(pUExtra + 2 + 2) = 1;
/* iname chksum */
write_ulong_to_mem(chksum, pUExtra + 2 + 2 + 1);
/* UTF-8 path */
write_string_to_mem(pZEntry->uname, pUExtra + 2 + 2 + 1 + 4);
return ZE_OK;
}
local int add_Unicode_Path_cen_extra_field(pZEntry)
struct zlist far *pZEntry;
{
char *pUExtra;
char *pOldUExtra;
char *pOldTemp;
char *pTemp;
#ifdef WIN32_OEM
char *inameLocal;
#endif
ush newEFSize;
ush usTemp;
ush ULen = strlen(pZEntry->uname);
ush blocksize;
ulg chksum = CRCVAL_INITIAL;
ush UCenLen = ZIP_EF_HEADER_SIZE + /* tag + EF Data Len */
1 + /* version */
4 + /* checksum */
ULen; /* UTF-8 path */
/* malloc Unicode Path extra field? */
/* after the below pUExtra should point to start of Unicode Path extra field */
if (pZEntry->cext == 0 || pZEntry->cextra == NULL)
{
/* get new extra field */
pUExtra = pZEntry->cextra = (char *) malloc(UCenLen);
if (pZEntry->cextra == NULL) {
ziperr( ZE_MEM, "UTF-8 Path cen extra field" );
}
pZEntry->cext = UCenLen;
}
else
{
/* check if we have a Unicode Path extra field ... */
pOldUExtra = get_extra_field( UTF8_PATH_EF_TAG, pZEntry->cextra, pZEntry->cext );
if (pOldUExtra == NULL)
{
/* ... we don't, so re-malloc enough memory for the old extra data plus */
/* the size of the UTF-8 Path extra field */
pUExtra = (char *) malloc( UCenLen + pZEntry->cext );
if (pUExtra == NULL)
ziperr( ZE_MEM, "UTF-8 Path Extra Field" );
/* move old extra field and update pointer and length */
memmove( pUExtra, pZEntry->cextra, pZEntry->cext);
free( pZEntry->cextra );
pZEntry->cextra = pUExtra;
pUExtra += pZEntry->cext;
pZEntry->cext += UCenLen;
}
else
{
/* ... we have. Sort out the existing UTF-8 Path extra field and remove it
* from pZEntry->extra, re-malloc enough memory for the old extra data
* left plus the size of the UTF-8 Path extra field */
blocksize = SH( pOldUExtra + 2 );
/* If the right length then go with it, else get rid of it and add a new extra field
* to existing block. */
if (blocksize == UCenLen - ZIP_EF_HEADER_SIZE)
{
/* looks good */
pUExtra = pOldUExtra;
}
else
{
newEFSize = pZEntry->cext - (blocksize + ZIP_EF_HEADER_SIZE) + UCenLen;
pUExtra = (char *) malloc( newEFSize );
if( pUExtra == NULL )
ziperr(ZE_MEM, "UTF-8 Path Extra Field");
/* move all before UTF-8 Path EF */
usTemp = (extent) (pOldUExtra - pZEntry->cextra);
pTemp = pUExtra;
memcpy( pTemp, pZEntry->cextra, usTemp );
/* move all after old UTF-8 Path EF */
pTemp = pUExtra + usTemp;
pOldTemp = pOldUExtra + ZIP_EF_HEADER_SIZE + blocksize;
usTemp = pZEntry->cext - usTemp - blocksize;
memcpy( pTemp, pOldTemp, usTemp);
/* replace extra fields */
pZEntry->cext = newEFSize;
free(pZEntry->cextra);
pZEntry->cextra = pUExtra;
pUExtra = pTemp + usTemp;
}
}
}
/*
* Compute the CRC-32 checksum of iname
*/
#ifdef WIN32_OEM
if ((inameLocal = malloc(strlen(pZEntry->iname) + 1)) == NULL) {
ZIPERR(ZE_MEM, "write Unicode");
}
/* if oem translation done convert back for checksum */
if ((pZEntry->vem & 0xff00) == 0) {
/* get original */
INTERN_TO_OEM(pZEntry->iname, inameLocal);
} else {
strcpy(inameLocal, pZEntry->iname);
}
#else
# define inameLocal (pZEntry->iname)
#endif
chksum = crc32(chksum, (uch *)(inameLocal), strlen(inameLocal));
#ifdef WIN32_OEM
free(inameLocal);
#else
# undef inameLocal
#endif
/*
* Compute the Adler-16 checksum of iname
*/
/*
chksum = adler16(ADLERVAL_INITIAL,
(uch *)(pZEntry->iname), strlen(pZEntry->iname));
*/
/* set/update UTF-8 Path extra field members */
/* tag header */
write_ushort_to_mem(UTF8_PATH_EF_TAG, pUExtra);
/* data size */
write_ushort_to_mem((ush) (UCenLen - ZIP_EF_HEADER_SIZE), pUExtra + 2);
/* version */
*(pUExtra + 2 + 2) = 1;
/* iname checksum */
write_ulong_to_mem(chksum, pUExtra + 2 + 2 + 1);
/* UTF-8 path */
write_string_to_mem(pZEntry->uname, pUExtra + 2 + 2 + 1 + 4);
return ZE_OK;
}
#endif /* def UNICODE_SUPPORT */
zoff_t ffile_size OF((FILE *));
/* 2004-12-06 SMS.
* ffile_size() returns reliable file size or EOF.
* May be used to detect large files in a small-file program.
*/
zoff_t ffile_size( file)
FILE *file;
{
int sts;
size_t siz;
zoff_t ofs;
char waste[ 4];
/* Seek to actual EOF. */
sts = zfseeko( file, 0, SEEK_END);
if (sts != 0)
{
/* fseeko() failed. (Unlikely.) */
ofs = EOF;
}
else
{
/* Get apparent offset at EOF. */
ofs = zftello( file);
if (ofs < 0)
{
/* Offset negative (overflow). File too big. */
ofs = EOF;
}
else
{
/* Seek to apparent EOF offset.
Won't be at actual EOF if offset was truncated.
*/
sts = zfseeko( file, ofs, SEEK_SET);
if (sts != 0)
{
/* fseeko() failed. (Unlikely.) */
ofs = EOF;
}
else
{
/* Read a byte at apparent EOF. Should set EOF flag. */
siz = fread( waste, 1, 1, file);
if (feof( file) == 0)
{
/* Not at EOF, but should be. File too big. */
ofs = EOF;
}
}
}
}
/* Seek to BOF.
*
* 2007-05-23 SMS.
* Note that a problem in a prehistoric VAX C run-time library
* requires that rewind() be used instead of fseek(), or else
* the EOF flag is not cleared properly.
*/
/* As WIN32 has this same problem (EOF not being cleared) when
* NO_ZIP64_SUPPORT is set but LARGE_FILE_SUPPORT is set on a
* small file, seems no reason not to always use rewind().
* 8/5/07 EG
*/
#if 0
#ifdef VAXC
sts = rewind( file);
#else /* def VAXC */
sts = zfseeko( file, 0, SEEK_SET);
#endif /* def VAXC [else] */
#endif
rewind(file);
return ofs;
}
#ifndef UTIL
local 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->oname);
}
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->oname);
}
if ((fix == 2) && (z->flg != z->lflg))
/* The comparision between central and local version of the
"general purpose bit flag" cannot be used from scanzipf_regnew(),
because in the "regular" zipfile processing, the local header reads
have been postponed until the actual entry processing takes place.
They have not yet been read when "zipoddities()" is called.
This change was neccessary to support multivolume archives.
*/
{
sprintf(errbuf, "local flags = 0x%04x, central = 0x%04x: ",
z->lflg, z->flg);
zipwarn(errbuf, z->oname);
}
else if (z->flg & ~0xf && (z->flg & ~0xf0) != UTF8_BIT)
/* Only bit in high byte we support is the new UTF-8 bit */
{
sprintf(errbuf, "undefined bits used in flags = 0x%04x: ", z->flg);
zipwarn(errbuf, z->oname);
}
if (z->how > LAST_KNOWN_COMPMETHOD) {
sprintf(errbuf, "unknown compression method %u: ", z->how);
zipwarn(errbuf, z->oname);
}
if (z->dsk)
{
sprintf(errbuf, "starts on disk %lu: ", z->dsk);
zipwarn(errbuf, z->oname);
}
if (z->att!=ASCII && z->att!=BINARY && z->att!=__EBCDIC)
{
sprintf(errbuf, "unknown internal attributes = 0x%04x: ", z->att);
zipwarn(errbuf, z->oname);
}
# 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->oname);
}
# endif
/* This test is just annoying, as Zip itself does not write the same
extra fields to both the local and central headers. It's much more
complicated than this test implies. 3/17/05 */
#if 0
if (z->ext || z->cext)
{
# if 0
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);
if (noisy) fprintf(mesg, "\tzip info: %s%s\n", errbuf, z->oname);
}
# if (!defined(RISCOS) && !defined(CMS_MVS))
/* in noisy mode, extra field sizes are always reported */
else if (noisy)
# else /* RISCOS || CMS_MVS */
/* avoid warnings for zipfiles created on the same type of OS system! */
/* or, was this warning really intended (eg. OS/2)? */
/* Only give info if extra bytes were added by another system */
else if (noisy && ((z->vem >> 8) != (OS_CODE >> 8)))
# endif /* ?(RISCOS || CMS_MVS) */
# endif /* 0 */
{
fprintf(mesg, "zip info: %s has %ld bytes of %sextra data\n",
z->oname, z->ext ? (ulg)z->ext : (ulg)z->cext,
z->ext ? (z->cext ? "" : "local ") : "central ");
}
}
#endif
}
#if 0 /* scanzipf_fix() no longer used */
/*
* scanzipf_fix 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*
*
* Still much work to do so can handle more cases. 1/18/04 EG
*/
local int scanzipf_fix(f)
FILE *f; /* zip file */
/*
The name of the zip file is pointed to by the global "zipfile". The globals
zipbeg, cenbeg, 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 */
ush flg; /* general purpose bit flag */
int m; /* mismatch flag */
extent n; /* length of name */
uzoff_t p; /* current file offset */
uzoff_t 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 */
#ifndef ZIP64_SUPPORT
/* 2004-12-06 SMS.
* Check for too-big file before doing any serious work.
*/
if (ffile_size( f) == EOF)
return ZE_ZIP64;
#endif /* ndef ZIP64_SUPPORT */
/* Get any file attribute valid for this OS, to set in the central
* directory when fixing the archive:
*/
# ifndef UTIL
filetime(zipfile, &a, (zoff_t*)&s, NULL);
# endif
x = &zfiles; /* first link */
p = 0; /* starting file offset */
# ifdef HANDLE_AMIGA_SFX
amiga_sfx_offset = 0L;
# endif
/* Find start of zip structures */
for (;;) {
/* look for signature */
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++;
}
/* found a P */
b[0] = (char) m;
/* local - 11/2/03 EG */
if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG)
break;
/* why search for ENDSIG if doing only local - 11/2/03 EG
if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG || s == ENDSIG)
break;
*/
/* back up */
if (zfseeko(f, -3L, SEEK_CUR))
return ferror(f) ? ZE_READ : ZE_EOF;
/* move 1 byte forward */
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 ||
zcount + 1 < zcount)
return ZE_MEM;
if (fread(b, LOCHEAD, 1, f) != 1) {
farfree((zvoid far *)z);
break;
}
z->ver = SH(LOCVER + b);
z->vem = (ush)(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;
/* attention: this one breaks the VC optimizer (Release Build) */
/* may be fixed - 11/1/03 EG */
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;
z->oname = NULL;
#ifdef UNICODE_SUPPORT
z->uname = z->zuname = z->ouname = NULL;
#endif
/* Link into list */
*x = z;
z->nxt = NULL;
x = &z->nxt;
/* Read file name and extra field and skip data */
if (n == 0)
{
sprintf(errbuf, "%lu", (ulg)zcount + 1);
zipwarn("zero-length name for entry #", errbuf);
# ifndef DEBUG
return ZE_FORM;
# endif
}
if ((z->iname = malloc(n+1)) == NULL ||
(z->ext && (z->extra = malloc(z->ext)) == NULL))
return ZE_MEM;
if (fread(z->iname, n, 1, f) != 1 ||
(z->ext && fread(z->extra, z->ext, 1, f) != 1))
return ferror(f) ? ZE_READ : ZE_EOF;
# ifdef ZIP64_SUPPORT
/* adjust/update siz,len and off (to come: dsk) entries */
/* PKZIP does not care of the version set in a CDH: if */
/* there is a zip64 extra field assigned to a CDH PKZIP */
/* uses it, we should do so, too. */
zip64_entry = adjust_zip_local_entry(z);
/* z->siz may be updated */
s = fix > 1 ? 0L : z->siz; /* discard compressed size with -FF */
# endif
if (s && zfseeko(f, (zoff_t)s, SEEK_CUR))
return ferror(f) ? ZE_READ : ZE_EOF;
/* If there is an extended local header, s is either 0 or
* the correct compressed size.
*/
z->iname[n] = '\0'; /* terminate name */
z->zname = in2ex(z->iname); /* convert to external name */
if (z->zname == NULL)
return ZE_MEM;
z->name = z->zname;
z->cextra = z->extra;
if (noisy) fprintf(mesg, "zip: reading %s\n", z->zname);
/* Save offset, update for next header */
z->off = p;
p += 4 + LOCHEAD + n + z->ext + s;
zcount++;
/* Skip extended local header if there is one */
if ((flg & 8) != 0) {
/* Skip the compressed data if compressed size is unknown.
* For safety, we should use the central directory.
*/
if (s == 0) {
for (;;) {
while ((m = getc(f)) != EOF && m != 0x50) ; /* 0x50 == 'P' */
b[0] = (char) m;
if (fread(b+1, 15, 1, f) != 1 || LG(b) == EXTLOCSIG)
break;
if (zfseeko(f, -15L, SEEK_CUR))
return ferror(f) ? ZE_READ : ZE_EOF;
}
# ifdef ZIP64_SUPPORT
if (zip64_entry) { /* from extra field */
/* all are 8 bytes */
s = LG(4 + ZIP64_EXTSIZ + b);
} else {
s = LG(4 + EXTSIZ + b);
}
# else
s = LG(4 + EXTSIZ + b);
# endif
p += s;
if ((uzoff_t) zftello(f) != p+16L) {
zipwarn("bad extended local header for ", z->zname);
return ZE_FORM;
}
} else {
/* compressed size non-zero, assume that it is valid: */
Assert(p == zftello(f), "bad compressed size with extended header");
if (zfseeko(f, p, SEEK_SET) || fread(b, 16, 1, f) != 1)
return ferror(f) ? ZE_READ : ZE_EOF;
if (LG(b) != EXTLOCSIG) {
zipwarn("extended local header not found for ", z->zname);
return ZE_FORM;
}
}
/* overwrite the unknown values of the local header: */
/* already in host format */
# ifdef ZIP64_SUPPORT
z->crc = LG(4 + ZIP64_EXTCRC + b);
z->siz = s;
z->len = LG(4 + ZIP64_EXTLEN + b);
# else
z->crc = LG(4 + EXTCRC + b);
z->siz = s;
z->len = LG(4 + EXTLEN + b);
# endif
p += 16L;
}
else if (fix > 1) {
/* Don't trust the compressed size */
for (;;) {
while ((m = getc(f)) != EOF && m != 0x50) p++; /* 0x50 == 'P' */
b[0] = (char) m;
if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG || s == CENSIG)
break;
if (zfseeko(f, -3L, SEEK_CUR))
return ferror(f) ? ZE_READ : ZE_EOF;
p++;
}
s = p - (z->off + 4 + LOCHEAD + n + z->ext);
if (s != z->siz) {
fprintf(mesg, " compressed size %s, actual size %s for %s\n",
zip_fzofft(z->siz, NULL, "u"), zip_fzofft(s, NULL, "u"),
z->zname);
z->siz = s;
}
/* next LOCSIG already read at this point, don't read it again: */
continue;
}
/* Read next signature */
if (fread(b, 4, 1, f) != 1)
break;
}
s = p; /* save start of central */
if (LG(b) != CENSIG && noisy) {
fprintf(mesg, "zip warning: %s %s truncated.\n", zipfile,
fix > 1 ? "has been" : "would be");
if (fix == 1) {
fprintf(mesg,
"Retry with option -qF to truncate, with -FF to attempt full recovery\n");
ZIPERR(ZE_FORM, NULL);
}
}
cenbeg = s;
if (zipbeg && noisy)
fprintf(mesg, "%s: adjusting offsets for a preamble of %s bytes\n",
zipfile, zip_fzofft(zipbeg, NULL, "u"));
return ZE_OK;
} /* end of function scanzipf_fix() */
#endif /* never, scanzipf_fix() no longer used */
#endif /* !UTIL */
/*
* read_local
*
* Read the local header assumed at in_file file pointer.
* localz is the returned local header, z is the central directory entry.
*
* This is used by crypt.c.
*
* Return ZE code
*/
int readlocal(localz, z)
struct zlist far **localz;
struct zlist far *z;
{
char buf[LOCHEAD + 1];
struct zlist far *locz;
#ifndef UTIL
ulg start_disk = 0;
uzoff_t start_offset = 0;
char *split_path;
start_disk = z->dsk;
start_offset = z->off;
/* don't assume reading the right disk */
if (start_disk != current_in_disk) {
if (in_file) {
fclose(in_file);
in_file = NULL;
}
}
current_in_disk = start_disk;
/* disks are archive.z01, archive.z02, ..., archive.zip */
split_path = get_in_split_path(in_path, current_in_disk);
if (in_file == NULL) {
while ((in_file = zfopen(split_path, FOPR)) == NULL) {
/* could not open split */
/* Ask for directory with split. Updates in_path */
if (ask_for_split_read_path(start_disk) != ZE_OK) {
return ZE_ABORT;
}
free(split_path);
split_path = get_in_split_path(in_path, start_disk);
}
}
#endif
/* For utilities assume archive is on one disk for now */
if (zfseeko(in_file, z->off, SEEK_SET) != 0) {
fclose(in_file);
in_file = NULL;
zipwarn("reading archive fseek: ", strerror(errno));
return ZE_READ;
}
if (!at_signature(in_file, "PK\03\04")) {
fclose(in_file);
in_file = NULL;
zipwarn("Did not find entry for ", z->iname);
return ZE_FORM;
}
/* read local header */
if (fread(buf, LOCHEAD, 1, in_file) != 1) {
int f = ferror(in_file);
zipwarn("reading local entry: ", strerror(errno));
fclose(in_file);
return f ? ZE_READ : ZE_EOF;
}
/* Local Header
local file header signature 4 bytes (0x04034b50)
version needed to extract 2 bytes
general purpose bit flag 2 bytes
compression method 2 bytes
last mod file time 2 bytes
last mod file date 2 bytes
crc-32 4 bytes
compressed size 4 bytes
uncompressed size 4 bytes
file name length 2 bytes
extra field length 2 bytes
file name (variable size)
extra field (variable size)
*/
if ((locz = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
zipwarn("reading entry", "");
fclose(in_file);
return ZE_MEM;
}
locz->ver = SH(LOCVER + buf);
locz->lflg = SH(LOCFLG + buf);
locz->how = SH(LOCHOW + buf);
locz->tim = LG(LOCTIM + buf); /* time and date into one long */
locz->crc = LG(LOCCRC + buf);
locz->nam = SH(LOCNAM + buf);
locz->ext = SH(LOCEXT + buf);
/* Initialize all fields pointing to malloced data to NULL */
locz->zname = locz->name = locz->iname = locz->extra = NULL;
locz->oname = NULL;
#ifdef UNICODE_SUPPORT
locz->uname = NULL;
locz->zuname = NULL;
locz->ouname = NULL;
#endif
/* Read file name, extra field and comment field */
if ((locz->iname = malloc(locz->nam+1)) == NULL ||
(locz->ext && (locz->extra = malloc(locz->ext)) == NULL))
return ZE_MEM;
if (fread(locz->iname, locz->nam, 1, in_file) != 1 ||
(locz->ext && fread(locz->extra, locz->ext, 1, in_file) != 1))
return ferror(in_file) ? ZE_READ : ZE_EOF;
locz->iname[z->nam] = '\0'; /* terminate name */
#ifdef UNICODE_SUPPORT
if (unicode_mismatch != 3)
read_Unicode_Path_local_entry(locz);
#endif
#ifdef WIN32
{
/* translate archive name from OEM if came from OEM-charset environment */
unsigned hostver = (z->vem & 0xff);
Ext_ASCII_TO_Native(locz->iname, (z->vem >> 8), hostver,
((z->atx & 0xffff0000L) != 0), TRUE);
}
#endif
if ((locz->name = malloc(locz->nam+1)) == NULL)
return ZE_MEM;
strcpy(locz->name, locz->iname);
#ifdef ZIP64_SUPPORT
zip64_entry = adjust_zip_local_entry(locz);
#endif
/* Compare localz to z */
if (locz->ver != z->ver) {
sprintf(errbuf, "Local Version Needed (%d) does not match CD (%d): ", locz->ver, z->ver);
zipwarn(errbuf, z->iname);
}
if (locz->lflg != z->flg) {
zipwarn("Local Entry Flag does not match CD: ", z->iname);
}
if (locz->crc != z->crc) {
zipwarn("Local Entry CRC does not match CD: ", z->iname);
}
/* as copying get uncompressed and compressed sizes from central directory */
locz->len = z->len;
locz->siz = z->siz;
*localz = locz;
return ZE_OK;
} /* end function readlocal() */
#if 0 /* following functions are not (no longer) used. */
/*
* scanzipf_reg starts searching for the End Signature at the end of the file
* The End Signature points to the Central Directory Signature which points
* to the Local Directory Signature
* XXX probably some more consistency checks are needed
*/
local int scanzipf_reg(f)
FILE *f; /* zip file */
/*
The name of the zip file is pointed to by the global "zipfile". The globals
zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
Return an error code in the ZE_ class.
*/
{
char b[CENHEAD]; /* buffer for central headers */
extent n; /* length of name */
struct zlist far * far *x; /* pointer last entry's link */
struct zlist far *z; /* current zip entry structure */
char *t; /* temporary pointer */
char far *u; /* temporary variable */
int found;
char *buf; /* temp buffer for reading zipfile */
# ifdef ZIP64_SUPPORT
ulg u4; /* unsigned 4 byte variable */
char bf[8];
uzoff_t u8; /* unsigned 8 byte variable */
uzoff_t censiz; /* size of central directory */
uzoff_t z64eocd; /* Zip64 End Of Central Directory record byte offset */
# else
ush flg; /* general purpose bit flag */
int m; /* mismatch flag */
# endif
zoff_t deltaoff = 0;
#ifndef ZIP64_SUPPORT
/* 2004-12-06 SMS.
* Check for too-big file before doing any serious work.
*/
if (ffile_size( f) == EOF)
return ZE_ZIP64;
#endif /* ndef ZIP64_SUPPORT */
buf = malloc(4096 + 4);
if (buf == NULL)
return ZE_MEM;
#ifdef HANDLE_AMIGA_SFX
amiga_sfx_offset = (fread(buf, 1, 4, f) == 4 && LG(buf) == 0xF3030000);
/* == 1 if this file is an Amiga executable (presumably UnZipSFX) */
#endif
/* detect spanning signature */
zfseeko(f, 0, SEEK_SET);
read_split_archive = (fread(buf, 1, 4, f) == 4 && LG(buf) == 0x08074b50L);
found = 0;
t = &buf[4096];
t[1] = '\0';
t[2] = '\0';
t[3] = '\0';
/* back up as much as 4k from end */
/* zip64 support 08/31/2003 R.Nausedat */
if (zfseeko(f, -4096L, SEEK_END) == 0) {
zipbeg = (uzoff_t) (zftello(f) + 4096L);
/* back up 4k blocks and look for End Of CD signature */
while (!found && zipbeg >= 4096) {
zipbeg -= 4096L;
buf[4096] = t[1];
buf[4097] = t[2];
buf[4098] = t[3];
/*
* XXX error check ??
*/
fread(buf, 1, 4096, f);
zfseeko(f, -8192L, SEEK_CUR);
t = &buf[4095];
/*
* XXX far pointer arithmetic in DOS
*/
while (t >= buf) {
/* Check for ENDSIG ("PK\5\6" in ASCII) */
if (LG(t) == ENDSIG) {
found = 1;
/*
* XXX error check ??
* XXX far pointer arithmetic in DOS
*/
zipbeg += (uzoff_t) (t - buf);
zfseeko(f, (zoff_t) zipbeg + 4L, SEEK_SET);
break;
}
--t;
}
}
}
else
/* file less than 4k bytes */
zipbeg = 4096L;
/*
* XXX warn: garbage at the end of the file ignored
*/
if (!found && zipbeg > 0) {
size_t s;
zfseeko(f, 0L, SEEK_SET);
clearerr(f);
s = fread(buf, 1, (size_t) zipbeg, f);
/* add 0 bytes at end */
buf[s] = t[1];
buf[s + 1] = t[2];
buf[s + 2] = t[3];
t = &buf[s - 1];
/*
* XXX far pointer comparison in DOS
*/
while (t >= buf) {
/* Check for ENDSIG ("PK\5\6" in ASCII) */
if (LG(t) == ENDSIG) {
found = 1;
/*
* XXX far pointer arithmetic in DOS
*/
zipbeg = (ulg) (t - buf);
zfseeko(f, (zoff_t) zipbeg + 4L, SEEK_SET);
break;
}
--t;
}
}
free(buf);
if (!found) {
zipwarn("missing end signature--probably not a zip file (did you", "");
zipwarn("remember to use binary mode when you transferred it?)", "");
return ZE_FORM;
}
/*
* Check for a Zip64 EOCD Locator signature - 12/10/04 EG
*/
#ifndef ZIP64_SUPPORT
/* If Zip64 not enabled check if archive being read is Zip64 */
/* back up 24 bytes (size of Z64 EOCDL and ENDSIG) */
if (zfseeko(f, -24, SEEK_CUR) != 0) {
perror("fseek");
return ZE_FORM; /* XXX */
}
/* read Z64 EOCDL if there */
if (fread(b, 20, 1, f) != 1) {
return ZE_READ;
}
/* first 4 bytes are the signature if there */
if (LG(b) == ZIP64_EOCDL_SIG) {
zipwarn("found Zip64 signature - this may be a Zip64 archive", "");
zipwarn("PKZIP 4.5 or later needed - set ZIP64_SUPPORT in Zip 3", "");
return ZE_ZIP64;
}
/* now should be back at the EOCD signature */
if (fread(b, 4, 1, f) != 1) {
zipwarn("unable to read after relative seek", "");
return ZE_READ;
}
if (LG(b) != ENDSIG) {
zipwarn("unable to relative seek in archive", "");
return ZE_FORM;
}
#if 0
if (fseek(f, -4, SEEK_CUR) != 0) {
perror("fseek");
return ZE_FORM; /* XXX */
}
#endif
#endif
/* Read end header */
if (fread(b, ENDHEAD, 1, f) != 1)
return ferror(f) ? ZE_READ : ZE_EOF;
if (SH(ENDDSK + b) || SH(ENDBEG + b) ||
SH(ENDSUB + b) != SH(ENDTOT + b))
zipwarn("multiple disk information ignored", "");
zcomlen = SH(ENDCOM + b);
if (zcomlen)
{
if ((zcomment = malloc(zcomlen)) == NULL)
return ZE_MEM;
if (fread(zcomment, zcomlen, 1, f) != 1)
{
free((zvoid *)zcomment);
zcomment = NULL;
return ferror(f) ? ZE_READ : ZE_EOF;
}
#ifdef EBCDIC
if (zcomment)
memtoebc(zcomment, zcomment, zcomlen);
#endif /* EBCDIC */
}
#ifdef ZIP64_SUPPORT
/* account for Zip64 EOCD Record and Zip64 EOCD Locator */
/* Z64 EOCDL should be just before EOCD (unless this is an empty archive) */
cenbeg = zipbeg - ZIP64_EOCDL_OFS_SIZE;
/* check for empty archive */
/* changed cenbeg to uzoff_t so instead of cenbeg >= 0 use new check - 5/23/05 EG */
if (zipbeg >= ZIP64_EOCDL_OFS_SIZE) {
/* look for signature */
if (zfseeko(f, cenbeg, SEEK_SET)) {
zipwarn("end of file seeking Z64EOCDL", "");
return ZE_FORM;
}
if (fread(bf, 4, 1, f) != 1) {
ziperr(ZE_FORM, "read error");
}
u4 = LG(bf);
if (u4 == ZIP64_EOCDL_SIG) {
/* found Zip64 EOCD Locator */
/* check for disk information */
zfseeko(f, cenbeg + ZIP64_EOCDL_OFS_TOTALDISKS, SEEK_SET);
if (fread(bf, 4, 1, f) != 1) {
ziperr(ZE_FORM, "read error");
}
u4 = LG(bf);
if (u4 != 1) {
ziperr(ZE_FORM, "multiple disk archives not yet supported");
}
/* look for Zip64 EOCD Record */
zfseeko(f, cenbeg + ZIP64_EOCDL_OFS_EOCD_START, SEEK_SET);
if (fread(bf, 8, 1, f) != 1) {
ziperr(ZE_FORM, "read error");
}
z64eocd = LLG(bf);
if (zfseeko(f, z64eocd, SEEK_SET)) {
ziperr(ZE_FORM, "error searching for Z64 EOCD Record");
}
if (fread(bf, 4, 1, f) != 1) {
ziperr(ZE_FORM, "read error");
}
u4 = LG(bf);
if (u4 != ZIP64_EOCD_SIG) {
ziperr(ZE_FORM, "Z64 EOCD not found but Z64 EOCD Locator exists");
}
/* get size of CD */
zfseeko(f, z64eocd + ZIP64_EOCD_OFS_SIZE, SEEK_SET);
if (fread(bf, 8, 1, f) != 1) {
ziperr(ZE_FORM, "read error");
}
censiz = LLG(bf);
/* get start of CD */
zfseeko(f, z64eocd + ZIP64_EOCD_OFS_CD_START, SEEK_SET);
if (fread(bf, 8, 1, f) == (size_t) -1) {
ziperr(ZE_FORM, "read error");
}
cenbeg = LLG(bf);
u8 = z64eocd - cenbeg;
deltaoff = adjust ? u8 - censiz : 0L;
} else {
/* assume no Locator and no Zip64 EOCD Record */
censiz = LG(ENDSIZ + b);
cenbeg = LG(b + ENDOFF);
u8 = zipbeg - censiz;
deltaoff = adjust ? u8 - censiz : 0L;
}
}
#else /* !ZIP64_SUPPORT */
/*
* XXX assumes central header immediately precedes end header
*/
/* start of central directory */
cenbeg = zipbeg - LG(ENDSIZ + b);
/*
printf("start of central directory cenbeg %ld\n", cenbeg);
*/
/* offset to first entry of archive */
deltaoff = adjust ? cenbeg - LG(b + ENDOFF) : 0L;
#endif /* ?ZIP64_SUPPORT */
if (zipbeg < ZIP64_EOCDL_OFS_SIZE) {
/* zip file seems empty */
return ZE_OK;
}
if (zfseeko(f, cenbeg, SEEK_SET) != 0) {
perror("fseek");
return ZE_FORM; /* XXX */
}
x = &zfiles; /* first link */
if (fread(b, 4, 1, f) != 1)
return ferror(f) ? ZE_READ : ZE_EOF;
while (LG(b) == CENSIG) {
/* Read central header. The portion of the central header that should
be in common with local header is read raw, for later comparison.
(this requires that the offset of ext in the zlist structure
be greater than or equal to LOCHEAD) */
if (fread(b, CENHEAD, 1, f) != 1)
return ferror(f) ? ZE_READ : ZE_EOF;
if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL)
return ZE_MEM;
z->vem = SH(CENVEM + b);
for (u = (char far *)(&(z->ver)), n = 0; n < (CENNAM-CENVER); n++)
u[n] = b[CENVER + n];
z->nam = SH(CENNAM + b); /* used before comparing cen vs. loc */
z->cext = SH(CENEXT + b); /* may be different from z->ext */
z->com = SH(CENCOM + b);
z->dsk = SH(CENDSK + b);
z->att = SH(CENATT + b);
z->atx = LG(CENATX + b);
z->off = LG(CENOFF + b) + deltaoff;
z->dosflag = (z->vem & 0xff00) == 0;
/* Initialize all fields pointing to malloced data to NULL */
z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
z->oname = NULL;
#ifdef UNICODE_SUPPORT
z->uname = NULL; /* UTF-8 path */
z->zuname = NULL; /* Escaped local version of uname */
z->ouname = NULL; /* Display version of zuname */
#endif
/* Link into list */
*x = z;
z->nxt = NULL;
x = &z->nxt;
/* Read file name, extra field and comment field */
if (z->nam == 0)
{
sprintf(errbuf, "%lu", (ulg)zcount + 1);
zipwarn("zero-length name for entry #", errbuf);
#ifndef DEBUG
farfree((zvoid far *)z);
return ZE_FORM;
#endif
}
if ((z->iname = malloc(z->nam+1)) == NULL ||
(z->cext && (z->cextra = malloc(z->cext)) == NULL) ||
(z->com && (z->comment = malloc(z->com)) == NULL))
return ZE_MEM;
if (fread(z->iname, z->nam, 1, f) != 1 ||
(z->cext && fread(z->cextra, z->cext, 1, f) != 1) ||
(z->com && fread(z->comment, z->com, 1, f) != 1))
return ferror(f) ? ZE_READ : ZE_EOF;
z->iname[z->nam] = '\0'; /* terminate name */
#ifdef EBCDIC
if (z->com)
memtoebc(z->comment, z->comment, z->com);
#endif /* EBCDIC */
#ifdef ZIP64_SUPPORT
/* zip64 support 08/31/2003 R.Nausedat */
/* here, we have to read the len, siz etc values from the CD */
/* entry as we might have to adjust them regarding their */
/* correspronding zip64 extra fields. */
/* also, we cannot compare the values from the CD entries with */
/* the values from the LH as they might be different. */
z->len = LG(CENLEN + b);
z->siz = LG(CENSIZ + b);
z->crc = LG(CENCRC + b);
z->tim = LG(CENTIM + b); /* time and date into one long */
z->how = SH(CENHOW + b);
z->flg = SH(CENFLG + b);
z->ver = SH(CENVER + b);
/* adjust/update siz,len and off (to come: dsk) entries */
/* PKZIP does not care of the version set in a CDH: if */
/* there is a zip64 extra field assigned to a CDH PKZIP */
/* uses it, we should do so, too. */
adjust_zip_central_entry(z);
#endif /* ZIP64_SUPPORT */
/* Update zipbeg offset, prepare for next header */
if (z->off < zipbeg)
zipbeg = z->off;
zcount++;
/* Read next signature */
if (fread(b, 4, 1, f) != 1)
return ferror(f) ? ZE_READ : ZE_EOF;
}
/* Point to start of header list and read local headers */
z = zfiles;
while (z != NULL) {
/* Read next signature */
if (zfseeko(f, z->off, SEEK_SET) != 0 || fread(b, 4, 1, f) != 1)
return ferror(f) ? ZE_READ : ZE_EOF;
if (LG(b) == LOCSIG) {
if (fread(b, LOCHEAD, 1, f) != 1)
return ferror(f) ? ZE_READ : ZE_EOF;
z->lflg = SH(LOCFLG + b);
n = SH(LOCNAM + b);
z->ext = SH(LOCEXT + b);
/* Compare name and extra fields */
if (n != z->nam)
{
#ifdef EBCDIC
strtoebc(z->iname, z->iname);
#endif
zipwarn("name lengths in local and central differ for ", z->iname);
return ZE_FORM;
}
if ((t = malloc(z->nam)) == NULL)
return ZE_MEM;
if (fread(t, z->nam, 1, f) != 1)
{
free((zvoid *)t);
return ferror(f) ? ZE_READ : ZE_EOF;
}
if (memcmp(t, z->iname, z->nam))
{
free((zvoid *)t);
#ifdef EBCDIC
strtoebc(z->iname, z->iname);
#endif
zipwarn("names in local and central differ for ", z->iname);
return ZE_FORM;
}
free((zvoid *)t);
if (z->ext)
{
if ((z->extra = malloc(z->ext)) == NULL)
return ZE_MEM;
if (fread(z->extra, z->ext, 1, f) != 1)
{
free((zvoid *)(z->extra));
return ferror(f) ? ZE_READ : ZE_EOF;
}
if (z->ext == z->cext && memcmp(z->extra, z->cextra, z->ext) == 0)
{
free((zvoid *)(z->extra));
z->extra = z->cextra;
}
}
#ifdef ZIP64_SUPPORT /* zip64 support 09/02/2003 R.Nausedat */
/*
for now the below is left out if ZIP64_SUPPORT is defined as the fields
len, siz and off in struct zlist are type of int64 if ZIP64_SUPPORT
is defined. In either way, the values read from the central directory
should be valid. comments are welcome
*/
#else /* !ZIP64_SUPPORT */
/* Check extended local header if there is one */
/* bit 3 */
if ((z->lflg & 8) != 0)
{
char buf2[16];
ulg s; /* size of compressed data */
s = LG(LOCSIZ + b);
if (s == 0)
s = LG((CENSIZ-CENVER) + (char far *)(&(z->ver)));
if (zfseeko(f, (z->off + (4+LOCHEAD) + z->nam + z->ext + s), SEEK_SET)
|| (fread(buf2, 16, 1, f) != 1))
return ferror(f) ? ZE_READ : ZE_EOF;
if (LG(buf2) != EXTLOCSIG)
{
# ifdef EBCDIC
strtoebc(z->iname, z->iname);
# endif
zipwarn("extended local header not found for ", z->iname);
return ZE_FORM;
}
/* overwrite the unknown values of the local header: */
for (n = 0; n < 12; n++)
b[LOCCRC+n] = buf2[4+n];
}
/* Compare local header with that part of central header (except
for the reserved bits in the general purpose flags and except
for the already checked entry name length */
/* If I have read this right we are stepping through the z struct
here as a byte array. Need to fix this. 5/25/2005 EG */
u = (char far *)(&(z->ver));
flg = SH((CENFLG-CENVER) + u); /* Save central flags word */
u[CENFLG-CENVER+1] &= 0x1f; /* Mask reserved flag bits */
b[LOCFLG+1] &= 0x1f;
for (m = 0, n = 0; n < LOCNAM; n++) {
if (b[n] != u[n])
{
if (!m)
{
zipwarn("local and central headers differ for ", z->iname);
m = 1;
}
if (noisy)
{
sprintf(errbuf, " offset %u--local = %02x, central = %02x",
(unsigned)n, (uch)b[n], (uch)u[n]);
zipwarn(errbuf, "");
}
}
}
if (m && !adjust)
return ZE_FORM;
/* Complete the setup of the zlist entry by translating the remaining
* central header fields in memory, starting with the fields with
* highest offset. This order of the conversion commands takes into
* account potential buffer overlaps caused by structure padding.
*/
z->len = LG((CENLEN-CENVER) + u);
z->siz = LG((CENSIZ-CENVER) + u);
z->crc = LG((CENCRC-CENVER) + u);
z->tim = LG((CENTIM-CENVER) + u); /* time and date into one long */
z->how = SH((CENHOW-CENVER) + u);
z->flg = flg; /* may be different from z->lflg */
z->ver = SH((CENVER-CENVER) + u);
#endif /* ?ZIP64_SUPPORT */
/* Clear actions */
z->mark = 0;
z->trash = 0;
#ifdef UNICODE_SUPPORT
if (unicode_mismatch != 3) {
read_Unicode_Path_entry(z);
if (z->uname) {
/* match based on converted Unicode name */
z->name = utf8_to_local_string(z->uname);
# ifdef EBCDIC
/* z->zname is used for printing and must be coded in native charset */
strtoebc(z->zname, z->name);
# else
if ((z->zname = malloc(strlen(z->name) + 1)) == NULL) {
ZIPERR(ZE_MEM, "scanzipf_reg");
}
strcpy(z->zname, z->name);
# endif
z->oname = local_to_display_string(z->zname);
} else {
/* no UTF-8 path */
if ((z->name = malloc(strlen(z->iname) + 1)) == NULL) {
ZIPERR(ZE_MEM, "scanzipf_reg");
}
strcpy(z->name, z->iname);
if ((z->zname = malloc(strlen(z->iname) + 1)) == NULL) {
ZIPERR(ZE_MEM, "scanzipf_reg");
}
strcpy(z->zname, z->iname);
z->oname = local_to_display_string(z->iname);
}
}
#else /* !UNICODE_SUPPORT */
# ifdef UTIL
/* We only need z->iname in the utils */
z->name = z->iname;
# ifdef EBCDIC
/* z->zname is used for printing and must be coded in native charset */
if ((z->zname = malloc(z->nam+1)) == NULL)
return ZE_MEM;
strtoebc(z->zname, z->iname);
# else
z->zname = z->iname;
# endif
# else /* !UTIL */
z->zname = in2ex(z->iname); /* convert to external name */
if (z->zname == NULL)
return ZE_MEM;
z->name = z->zname;
# endif /* ?UTIL */
if ((z->oname = malloc(strlen(z->zname) + 1)) == NULL) {
ZIPERR(ZE_MEM, "scanzipf_reg");
}
strcpy(z->oname, z->zname);
#endif /* ?UNICODE_SUPPORT */
}
else {
#ifdef EBCDIC
strtoebc(z->iname, z->iname);
#endif
zipwarn("local header not found for ", z->iname);
return ZE_FORM;
}
#ifndef UTIL
if (verbose && fix == 0)
zipoddities(z);
#endif
z = z->nxt;
}
if (zipbeg && noisy)
fprintf(mesg, "%s: %s a preamble of %s bytes\n",
zipfile, adjust ? "adjusting offsets for" : "found",
zip_fzofft(zipbeg, NULL, "u"));
#ifdef HANDLE_AMIGA_SFX
if (zipbeg < 12 || (zipbeg & 3) != 0 /* must be longword aligned */)
amiga_sfx_offset = 0;
else if (amiga_sfx_offset) {
char buf2[16];
if (!fseek(f, zipbeg - 12, SEEK_SET) && fread(buf2, 12, 1, f) == 1) {
if (LG(buf2 + 4) == 0xF1030000 /* 1009 in Motorola byte order */)
/* could also check if LG(buf2) == 0xF2030000... no for now */
amiga_sfx_offset = zipbeg - 4;
else
amiga_sfx_offset = 0L;
}
}
#endif /* HANDLE_AMIGA_SFX */
return ZE_OK;
} /* end of function scanzipf_reg() */
#endif /* never */
/* find_next_signature
*
* Scan the file forward and look for the next PK signature.
*
* Return 1 if find one and leave file pointer pointing to next char
* after signature and set sigbuf to signature.
*
* Return 0 if not. Will be at EOF on return unless error.
*
*/
local char sigbuf[4]; /* signature found */
#if 0 /* currently unused */
/* copy signature */
char *copy_sig(copyto, copyfrom)
char *copyto;
char *copyfrom;
{
int i;
for (i = 0; i < 4; i++) {
copyto[i] = copyfrom[i];
}
return copyto;
}
#endif /* currently unused */
local int find_next_signature(f)
FILE *f;
{
int m;
/*
zoff_t here;
*/
/* look for P K ? ? signature */
m = getc(f);
/*
here = zftello(f);
*/
while (m != EOF)
{
if (m == 0x50 /*'P' except EBCDIC*/) {
/* found a P */
sigbuf[0] = (char) m;
if ((m = getc(f)) == EOF)
break;
if (m != 0x4b /*'K' except EBCDIC*/) {
/* not a signature */
ungetc(m, f);
} else {
/* found P K */
sigbuf[1] = (char) m;
if ((m = getc(f)) == EOF)
break;
if (m == 0x50 /*'P' except EBCDIC*/) {
/* not a signature but maybe start of new one */
ungetc(m, f);
continue;
} else if (m >= 16) {
/* last 2 chars expect < 16 for signature */
continue;
}
sigbuf[2] = (char) m;
if ((m = getc(f)) == EOF)
break;
if (m == 0x50 /*'P' except EBCDIC*/) {
/* not a signature but maybe start of new one */
ungetc(m, f);
continue;
} else if (m >= 16) {
/* last 2 chars expect < 16 */
continue;
}
sigbuf[3] = (char) m;
/* found possible signature */
return 1;
}
}
m = getc(f);
}
if (ferror(f)) {
return 0;
}
/* found nothing */
return 0;
}
/* find_signature
*
* Find signature.
*
* Return 1 if found and leave file pointing to next character
* after signature. Set sigbuf with signature.
*
* Return 0 if not found.
*/
local int find_signature(f, signature)
FILE *f;
ZCONST char *signature;
{
int i;
char sig[4];
/*
zoff_t here = zftello(f);
*/
for (i = 0; i < 4; i++)
sig[i] = signature[i];
/* for EBCDIC */
if (sig[0] == 'P')
sig[0] = 0x50;
if (sig[1] == 'K')
sig[1] = 0x4b;
while (!feof(f)) {
if (!find_next_signature(f)) {
return 0;
} else {
for (i = 0; i < 4; i++) {
if (sig[i] != sigbuf[i]) {
/* not a match */
break;
}
}
if (i == 4) {
/* found it */
return 1;
}
}
}
return 0;
}
/* is_signature
*
* Compare signatures
*
* Return 1 if the signatures match.
*/
local int is_signature(sig1, sig2)
ZCONST char *sig1;
ZCONST char *sig2;
{
int i;
char tsig1[4];
char tsig2[4];
for (i = 0; i < 4; i++) {
tsig1[i] = sig1[i];
tsig2[i] = sig2[i];
}
/* for EBCDIC */
if (tsig1[0] == 'P')
tsig1[0] = 0x50;
if (tsig1[1] == 'K')
tsig1[1] = 0x4b;
if (tsig2[0] == 'P')
tsig2[0] = 0x50;
if (tsig2[1] == 'K')
tsig2[1] = 0x4b;
for (i = 0; i < 4; i++) {
if (tsig1[i] != tsig2[i]) {
/* not a match */
break;
}
}
if (i == 4) {
/* found it */
return 1;
}
return 0;
}
/* at_signature
*
* Is at signature in file
*
* Return 1 if at the signature and leave file pointing to next character
* after signature.
*
* Return 0 if not.
*/
local int at_signature(f, signature)
FILE *f;
ZCONST char *signature;
{
int i;
extent m;
char sig[4];
char b[4];
for (i = 0; i < 4; i++)
sig[i] = signature[i];
/* for EBCDIC */
if (sig[0] == 'P')
sig[0] = 0x50;
if (sig[1] == 'K')
sig[1] = 0x4b;
m = fread(b, 1, 4, f);
if (m != 4) {
return 0;
} else {
for (i = 0; i < 4; i++) {
if (sig[i] != b[i]) {
/* not a match */
break;
}
}
if (i == 4) {
/* found it */
return 1;
}
}
return 0;
}
#ifndef UTIL
local int scanzipf_fixnew()
/*
Scan an assumed broke archive from the beginning, salvaging what can.
Generally scanzipf_regnew() is used for reading archives normally and
for fixing archives with a readable central directory using -F. This
scan is used by -FF and is for an archive that is unreadable by
scanzipf_regnew().
Start with the first file of the archive, either .z01 or .zip, and
look for local entries. Read local entries found and create zlist
entries for them. If we find central directory entries, read them
and update the zlist created while reading local entries.
The input path for the .zip file is in in_path. If this is a multiple disk
archive get the paths for splits from in_path as we go. If a split is not in
the same directory as the last split we ask the user where it is and update
in_path.
*/
/*
This is old:
The name of the zip file is pointed to by the global "zipfile". The globals
zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
Return an error code in the ZE_ class.
*/
{
/* This function only reads the standard End-of-CentralDir record and the
standard CentralDir-Entry records directly. To conserve stack space,
only a buffer of minimal size is declared.
*/
# if CENHEAD > ENDHEAD
# define FIXSCAN_BUFSIZE CENHEAD
# else
# define FIXSCAN_BUFSIZE ENDHEAD
# endif
char scbuf[FIXSCAN_BUFSIZE]; /* buffer big enough for headers */
char *split_path;
ulg eocdr_disk;
uzoff_t eocdr_offset;
uzoff_t current_offset = 0; /* offset before */
uzoff_t offset = 0; /* location after return from seek */
int skip_disk = 0; /* 1 if user asks to skip current disk */
int skipped_disk = 0; /* 1 if skipped start disk and start offset is useless */
int r = 0; /* zipcopy return */
uzoff_t 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 */
int plen;
char *in_path_ext;
int in_central_directory = 0; /* found a central directory record */
struct zlist far *cz;
uzoff_t cd_total_entries = 0; /* number of entries according to EOCDR */
ulg in_cd_start_disk; /* central directory start disk */
uzoff_t in_cd_start_offset; /* offset of start of cd on cd start disk */
total_disks = 1000000;
/* open the zipfile */
/* This must be .zip file, even if it doesn't exist */
/* see if zipfile name ends in .zip */
plen = strlen(in_path);
#ifdef VMS
/* On VMS, adjust plen (and in_path_ext) to avoid the file version. */
plen -= strlen(vms_file_version(in_path));
#endif /* def VMS */
in_path_ext = zipfile + plen - 4;
if (plen >= 4 &&
in_path_ext[0] == '.' &&
toupper(in_path_ext[1]) == 'Z' &&
in_path_ext[2] >= '0' && in_path_ext[2] <= '9' &&
in_path_ext[3] >= '0' && in_path_ext[3] <= '9' &&
(plen == 4 || (in_path_ext[4] >= '0' && in_path_ext[4] <= '9'))) {
/* This may be a split but not the end split */
strcpy(errbuf, "if archive to fix is split archive, need to provide\n");
strcat(errbuf, " path of the last split with .zip extension,\n");
strcat(errbuf, " even if it doesn't exist (zip will ask for splits)");
zipwarn(errbuf, "");
return ZE_FORM;
}
if ((in_file = zfopen(in_path, FOPR)) == NULL) {
zipwarn("could not open input archive: ", in_path);
}
else
{
#ifndef ZIP64_SUPPORT
/* 2004-12-06 SMS.
* Check for too-big file before doing any serious work.
*/
if (ffile_size( in_file) == EOF) {
fclose(in_file);
in_file = NULL;
zipwarn("input file requires Zip64 support: ", in_path);
return ZE_ZIP64;
}
#endif /* ndef ZIP64_SUPPORT */
/* look for End Of Central Directory Record */
/* back up 64k (the max size of the EOCDR) from end */
if (zfseeko(in_file, -0x40000L, SEEK_END) != 0) {
/* assume file is less than 64 KB so backup to beginning */
if (zfseeko(in_file, 0L, SEEK_SET) != 0) {
fclose(in_file);
in_file = NULL;
zipwarn("unable to seek in input file ", in_path);
return ZE_READ;
}
}
/* find EOCD Record signature */
if (!find_signature(in_file, "PK\05\06")) {
/* No End Of Central Directory Record */
strcpy(errbuf, "Missing end (EOCDR) signature - either this archive\n");
strcat(errbuf, " is not readable or the end is damaged");
zipwarn(errbuf, "");
}
else
{
/* at start of data after EOCDR signature */
eocdr_offset = (uzoff_t) zftello(in_file);
/* OK, it is possible this is not the last EOCDR signature (might be
EOCDR signature from a stored archive in the last 64 KB) and so not
the one we want.
The below assumes the signature does not appear in the assumed
ASCII text .ZIP file comment. Even if something like UTF-8
is stored in the comment, it's unlikely the binary \05 and \06
will be in the comment text.
*/
while (find_signature(in_file, "PK\05\06")) {
eocdr_offset = (uzoff_t) zftello(in_file);
}
/* found EOCDR */
/* format is
end of central dir signature 4 bytes (0x06054b50)
number of this disk 2 bytes
number of the disk with the
start of the central directory 2 bytes
total number of entries in the
central directory on this disk 2 bytes
total number of entries in
the central directory 2 bytes
size of the central directory 4 bytes
offset of start of central
directory with respect to
the starting disk number 4 bytes
.ZIP file comment length 2 bytes
.ZIP file comment (variable size)
*/
if (zfseeko(in_file, eocdr_offset, SEEK_SET) != 0) {
fclose(in_file);
in_file = NULL;
zipwarn("unable to seek in input file ", in_path);
return ZE_READ;
}
/* read the EOCDR */
s = fread(scbuf, 1, ENDHEAD, in_file);
/* make sure we read enough bytes */
if (s < ENDHEAD) {
sprintf(errbuf, "End record (EOCDR) only %s bytes - assume truncated",
zip_fzofft(s, NULL, "u"));
zipwarn(errbuf, "");
}
else
{
/* the first field should be number of this (the last) disk */
eocdr_disk = (ulg)SH(scbuf);
total_disks = eocdr_disk + 1;
/* assume this is this disk - if Zip64 it may not be as the
disk number may be bigger than this field can hold
*/
current_in_disk = total_disks - 1;
/* Central Directory disk, offset, and total entries */
in_cd_start_disk = (ulg)SH(scbuf + 2);
in_cd_start_offset = (uzoff_t)LG(scbuf + 12);
cd_total_entries = (uzoff_t)SH(scbuf + 6);
/* the in_cd_start_disk should always be less than the total_disks,
unless the -1 flags are being used */
if (total_disks < 0x10000 && in_cd_start_disk > total_disks) {
zipwarn("End record (EOCDR) has bad disk numbers - ignoring EOCDR", "");
total_disks = 0;
}
else
{
/* length of zipfile comment */
zcomlen = SH(scbuf + ENDCOM);
if (zcomlen)
{
if ((zcomment = malloc(zcomlen + 1)) == NULL)
return ZE_MEM;
if (fread(zcomment, zcomlen, 1, in_file) != 1)
{
free((zvoid *)zcomment);
zcomment = NULL;
zipwarn("zipfile comment truncated - ignoring", "");
} else {
zcomment[zcomlen] = '\0';
}
#ifdef EBCDIC
if (zcomment)
memtoebc(zcomment, zcomment, zcomlen);
#endif /* EBCDIC */
}
}
if (total_disks != 1)
sprintf(errbuf, " Found end record (EOCDR) - says expect %lu splits", total_disks);
else
sprintf(errbuf, " Found end record (EOCDR) - says expect single disk archive");
zipmessage(errbuf, "");
if (zcomment)
zipmessage(" Found archive comment", "");
} /* good EOCDR */
} /* found EOCDR */
/* if total disks is other than 1 then this is not start disk */
/* if the EOCDR is bad, total_disks is 0 */
/* if total_disks = 0, then guess if this is a single-disk archive
by seeing if starts with local header */
if (total_disks == 0) {
int issig;
/* seek to top */
if (zfseeko(in_file, 0, SEEK_SET) != 0) {
fclose(in_file);
in_file = NULL;
zipwarn("unable to seek in input file ", in_path);
return ZE_READ;
}
/* get next signature */
issig = find_next_signature(in_file);
if (issig) {
current_in_offset = zftello(in_file);
if (current_in_offset == 4 && is_signature(sigbuf, "PK\03\03")) {
/* could be multi-disk aborted signature at top */
/* skip */
issig = find_next_signature(in_file);
} else if (current_in_offset <= 4 && is_signature(sigbuf, "PK\03\03")) {
/* multi-disk spanning signature */
total_disks = 99999;
}
}
if (issig && total_disks == 0) {
current_in_offset = zftello(in_file);
if (current_in_offset == 8 && is_signature(sigbuf, "PK\03\04")) {
/* Local Header Record at top */
printf("Is this a single-disk archive? (y/n): ");
fflush(stdout);
if (fgets(errbuf, 100, stdin) != NULL) {
if (errbuf[0] == 'y' || errbuf[0] == 'Y') {
total_disks = 1;
zipmessage(" Assuming single-disk archive", "");
}
}
}
}
}
if (!noisy)
/* if quiet assume single-disk archive */
total_disks = 1;
if (total_disks == 1000000) {
/* still don't know, so ask */
printf("Is this a single-disk archive? (y/n): ");
fflush(stdout);
if (fgets(errbuf, 100, stdin) != NULL) {
if (errbuf[0] == 'y' || errbuf[0] == 'Y') {
total_disks = 1;
zipmessage(" Assuming single-disk archive", "");
}
}
}
if (total_disks == 1000000) {
/* assume max */
total_disks = 100000;
}
} /* .zip file exists */
/* Skip reading the Zip64 EOCDL, Zip64 EOCDR, or central directory */
/* Now read the archive starting with first disk. Find local headers,
create entry in zlist, then copy entry to new archive */
/* Multi-volume file names end in .z01, .z02, ..., .z10, .zip for 11 disk archive */
/* Unless quiet, always close the in_path disk and ask user for first disk,
unless there is an End Of Central Directory record and that says there is
only one disk.
If quiet, assume the file pointed to is a single file archive to fix. */
if (noisy && in_file) {
fclose(in_file);
in_file = NULL;
}
/* Read the archive disks - no idea how many disks there are
since we can't trust the EOCDR and other end records
*/
zipmessage("Scanning for entries...", "");
for (current_in_disk = 0; current_in_disk < total_disks; current_in_disk++) {
/* get the path for this disk */
split_path = get_in_split_path(in_path, current_in_disk);
/* if in_file is not NULL then in_file is already open */
if (in_file == NULL) {
/* open the split */
while ((in_file = zfopen(split_path, FOPR)) == NULL) {
int result;
/* could not open split */
/* Ask for directory with split. Updates global variable in_path */
result = ask_for_split_read_path(current_in_disk);
if (result == ZE_ABORT) {
zipwarn("could not find split: ", split_path);
return ZE_ABORT;
} else if (result == ZE_EOF) {
zipmessage_nl("", 1);
zipwarn("user ended reading - closing archive", "");
return ZE_EOF;
} else if (result == ZE_FORM) {
/* user asked to skip this disk */
zipmessage_nl("", 1);
sprintf(errbuf, "skipping disk %lu ...\n", current_in_disk);
zipwarn(errbuf, "");
skip_disk = 1;
break;
}
split_path = get_in_split_path(in_path, current_in_disk);
}
if (skip_disk) {
/* skip this current disk - this works because central directory entries
can't be split across splits */
skip_disk = 0;
skipped_disk = 1;
continue;
}
}
if (skipped_disk) {
/* Not much to do here as between entries. Entries are copied
in zipcopy() and that has to handle missing disks while
reading data for an entry.
*/
}
/* Main loop */
/* Look for next signature and process it */
while (find_next_signature(in_file)) {
current_in_offset = zftello(in_file);
if (is_signature(sigbuf, "PK\05\06")) {
/* End Of Central Directory Record */
sprintf(errbuf, "EOCDR found (%2lu %6s)...",
current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
zipmessage_nl(errbuf, 1);
} else if (is_signature(sigbuf, "PK\06\06")) {
/* Zip64 End Of Central Directory Record */
sprintf(errbuf, "Zip64 EOCDR found (%2lu %6s)...",
current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
zipmessage_nl(errbuf, 1);
} else if (is_signature(sigbuf, "PK\06\07")) {
/* Zip64 End Of Central Directory Locator */
sprintf(errbuf, "Zip64 EOCDL found (%2lu %6s)...",
current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
zipmessage_nl(errbuf, 1);
} else if (is_signature(sigbuf, "PK\03\04")) {
/* Local Header Record */
if (verbose) {
sprintf(errbuf, " Local (%2lu %6s):",
current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
zipmessage_nl(errbuf, 0);
}
/* Create zlist entry. Most will be filled in by zipcopy(). */
if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
zipwarn("reading central directory", "");
return ZE_MEM;
}
z->vem = 0;
z->ver = 0;
z->flg = 0;
z->how = 0;
z->tim = 0; /* time and date into one long */
z->crc = 0;
z->siz = 0;
z->len = 0;
z->nam = 0; /* used before comparing cen vs. loc */
z->cext = 0; /* may be different from z->ext */
z->com = 0;
z->dsk = 0;
z->att = 0;
z->atx = 0;
z->off = 0;
z->dosflag = 0;
/* Initialize all fields pointing to malloced data to NULL */
z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
z->oname = NULL;
#ifdef UNICODE_SUPPORT
z->uname = z->zuname = z->ouname = NULL;
#endif
/* Attempt to copy entry */
r = zipcopy(z);
if (in_central_directory) {
sprintf(errbuf, "Entry after central directory found (%2lu %6s)...",
current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
zipmessage_nl(errbuf, 1);
in_central_directory = 0;
}
if (r == ZE_EOF)
/* user said no more splits */
break;
else if (r == ZE_OK) {
zcount++;
files_total++;
bytes_total += z->siz;
/* Link into list */
if (zfiles == NULL)
/* first link */
x = &zfiles;
/* Link into list */
*x = z;
z->nxt = NULL;
x = &z->nxt;
}
} else if (is_signature(sigbuf, "PK\01\02")) {
/* Central directory header */
/* sort the zlist */
if (in_central_directory == 0) {
zipmessage("Central Directory found...", "");
/* If one or more files, sort by name */
if (zcount)
{
struct zlist far * far *x; /* pointer into zsort array */
struct zlist far *z; /* pointer into zfiles linked list */
int i = 0;
extent zl_size = zcount * sizeof(struct zlist far *);
if (zl_size / sizeof(struct zlist far *) != zcount ||
(x = zsort = (struct zlist far **)malloc(zl_size)) == NULL)
return ZE_MEM;
for (z = zfiles; z != NULL; z = z->nxt)
x[i++] = z;
qsort((char *)zsort, zcount, sizeof(struct zlist far *), zqcmp);
/* Skip Unicode searching */
}
}
if (verbose) {
sprintf(errbuf, " Cen (%2lu %6s): ",
current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
zipmessage_nl(errbuf, 0);
}
in_central_directory = 1;
/* Read central directory entry */
/* central directory signature */
/* The format of a central directory record
central file header signature 4 bytes (0x02014b50)
version made by 2 bytes
version needed to extract 2 bytes
general purpose bit flag 2 bytes
compression method 2 bytes
last mod file time 2 bytes
last mod file date 2 bytes
crc-32 4 bytes
compressed size 4 bytes
uncompressed size 4 bytes
file name length 2 bytes
extra field length 2 bytes
file comment length 2 bytes
disk number start 2 bytes
internal file attributes 2 bytes
external file attributes 4 bytes
relative offset of local header 4 bytes
file name (variable size)
extra field (variable size)
file comment (variable size)
*/
if (fread(scbuf, CENHEAD, 1, in_file) != 1) {
zipwarn("reading central directory: ", strerror(errno));
zipwarn("bad archive - error reading central directory", "");
zipwarn("skipping this entry...", "");
continue;
}
if ((cz = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
zipwarn("reading central directory", "");
return ZE_MEM;
}
cz->vem = SH(CENVEM + scbuf);
cz->ver = SH(CENVER + scbuf);
cz->flg = SH(CENFLG + scbuf);
cz->how = SH(CENHOW + scbuf);
cz->tim = LG(CENTIM + scbuf); /* time and date into one long */
cz->crc = LG(CENCRC + scbuf);
cz->siz = LG(CENSIZ + scbuf);
cz->len = LG(CENLEN + scbuf);
cz->nam = SH(CENNAM + scbuf); /* used before comparing cen vs. loc */
cz->cext = SH(CENEXT + scbuf); /* may be different from z->ext */
cz->com = SH(CENCOM + scbuf);
cz->dsk = SH(CENDSK + scbuf);
cz->att = SH(CENATT + scbuf);
cz->atx = LG(CENATX + scbuf);
cz->off = LG(CENOFF + scbuf);
cz->dosflag = (cz->vem & 0xff00) == 0;
/* Initialize all fields pointing to malloced data to NULL */
cz->zname = cz->name = cz->iname = cz->extra = cz->cextra = NULL;
cz->comment = cz->oname = NULL;
#ifdef UNICODE_SUPPORT
cz->uname = cz->zuname = cz->ouname = NULL;
#endif
/* Read file name, extra field and comment field */
if (cz->nam == 0)
{
sprintf(errbuf, "%lu", (ulg)zcount + 1);
zipwarn("zero-length name for entry #", errbuf);
zipwarn("skipping this entry...", "");
continue;
}
if ((cz->iname = malloc(cz->nam+1)) == NULL ||
(cz->cext && (cz->cextra = malloc(cz->cext + 1)) == NULL) ||
(cz->com && (cz->comment = malloc(cz->com + 1)) == NULL))
return ZE_MEM;
if (fread(cz->iname, cz->nam, 1, in_file) != 1 ||
(cz->cext && fread(cz->cextra, cz->cext, 1, in_file) != 1) ||
(cz->com && fread(cz->comment, cz->com, 1, in_file) != 1)) {
zipwarn("error reading entry: ", strerror(errno));
zipwarn("skipping this entry...", "");
continue;
}
cz->iname[cz->nam] = '\0'; /* terminate name */
/* Look up this name in zlist from local entries */
z = zsearch(cz->iname);
if (z && z->tim == cz->tim) {
/* Apparently as iname and date and time match this central
directory entry goes with this zlist entry */
if (verbose) {
/* cen dir name matches a local name */
sprintf(errbuf, "updating: %s", cz->iname);
zipmessage_nl(errbuf, 0);
}
if (z->crc != cz->crc) {
sprintf(errbuf, "local (%lu) and cen (%lu) crc mismatch", z->crc, cz->crc);
zipwarn(errbuf, "");
}
z->vem = cz->vem;
/* z->ver = cz->ver; */
/* z->flg = cz->flg; */
/* z->how = cz->how; */
/* z->tim = cz->tim; */ /* time and date into one long */
/* z->crc = cz->crc; */
/* z->siz = cz->siz; */
/* z->len = cz->len; */
/* z->nam = cz->nam; */ /* used before comparing cen vs. loc */
z->cext = cz->cext; /* may be different from z->ext */
z->com = cz->com;
z->cextra = cz->cextra;
z->comment = cz->comment;
/* z->dsk = cz->dsk; */
z->att = cz->att;
z->atx = cz->atx;
/* z->off = cz->off; */
z->dosflag = cz->dosflag;
#ifdef UNICODE_SUPPORT
if (unicode_mismatch != 3 && z->uname == NULL) {
if (z->flg & UTF8_BIT) {
/* path is UTF-8 */
if ((z->uname = malloc(strlen(z->iname) + 1)) == NULL) {
ZIPERR(ZE_MEM, "reading archive");
}
strcpy(z->uname, z->iname);
} else {
/* check for UTF-8 path extra field */
read_Unicode_Path_entry(z);
}
}
#endif
#ifdef WIN32
/* Input path may be OEM */
{
unsigned hostver = (z->vem & 0xff);
Ext_ASCII_TO_Native(z->iname, (z->vem >> 8), hostver,
((z->atx & 0xffff0000L) != 0), FALSE);
}
#endif
#ifdef EBCDIC
if (z->com)
memtoebc(z->comment, z->comment, z->com);
#endif /* EBCDIC */
#ifdef WIN32
/* Comment may be OEM */
{
unsigned hostver = (z->vem & 0xff);
Ext_ASCII_TO_Native(z->comment, (z->vem >> 8), hostver,
((z->atx & 0xffff0000L) != 0), FALSE);
}
#endif
#ifdef ZIP64_SUPPORT
/* zip64 support 08/31/2003 R.Nausedat */
/* here, we have to read the len, siz etc values from the CD */
/* entry as we might have to adjust them regarding their */
/* correspronding zip64 extra fields. */
/* also, we cannot compare the values from the CD entries with */
/* the values from the LH as they might be different. */
/* adjust/update siz,len and off (to come: dsk) entries */
/* PKZIP does not care of the version set in a CDH: if */
/* there is a zip64 extra field assigned to a CDH PKZIP */
/* uses it, we should do so, too. */
/*
adjust_zip_central_entry(z);
*/
#endif
/* Update zipbeg beginning of archive offset, prepare for next header */
/*
if (z->dsk == 0 && (!zipbegset || z->off < zipbeg)) {
zipbeg = z->off;
zipbegset = 1;
}
zcount++;
*/
#ifndef UTIL
if (verbose)
zipoddities(z);
#endif
current_offset = zftello(y);
if (zfseeko(y, z->off, SEEK_SET) != 0) {
fclose(in_file);
in_file = NULL;
zipwarn("writing archive seek: ", strerror(errno));
return ZE_WRITE;
}
if (putlocal(z, PUTLOCAL_REWRITE) != ZE_OK)
zipwarn("Error rewriting local header", "");
if (zfseeko(y, current_offset, SEEK_SET) != 0) {
fclose(in_file);
in_file = NULL;
zipwarn("write archive seek: ", strerror(errno));
return ZE_WRITE;
}
offset = zftello(y);
if (current_offset != offset) {
fclose(in_file);
in_file = NULL;
zipwarn("seek after local: ", strerror(errno));
return ZE_WRITE;
}
if (verbose)
zipmessage_nl("", 1);
} else {
/* cen dir name does not match local name */
sprintf(errbuf, "no local entry: %s", cz->iname);
zipmessage_nl(errbuf, 1);
}
} else if (zfiles == NULL && is_signature(sigbuf, "PK\07\010")) {
/* assume spanning signature at top of archive */
if (total_disks == 1) {
zipmessage(" Found spanning marker, but did not expect split (multi-disk) archive...", "");
} else if (total_disks > 1) {
zipmessage(" Found spanning marker - expected as this is split (multi-disk) archive...", "");
} else {
zipmessage(" Found spanning marker - could be split archive...", "");
}
} else {
/* this signature shouldn't be here */
int c;
char errbuftemp[40];
strcpy(errbuf, "unexpected signature ");
for (c = 0; c < 4; c++) {
sprintf(errbuftemp, "%02x ", sigbuf[c]);
strcat(errbuf, errbuftemp);
}
sprintf(errbuftemp, "on disk %lu at %s\n", current_in_disk,
zip_fzofft(current_in_offset - 4, NULL, "u"));
strcat(errbuf, errbuftemp);
zipwarn(errbuf, "");
zipwarn("skipping this signature...", "");
}
} /* while reading file */
/* close disk and do next disk */
if (in_file)
fclose(in_file);
in_file = NULL;
free(split_path);
if (r == ZE_EOF)
/* user says no more splits */
break;
} /* for each disk */
return ZE_OK;
} /* end of function scanzipf_fixnew() */
#endif /* !UTIL */
/* ---------------------- */
/* New regular scan */
/*
* scanzipf_regnew is similar to the orignal scanzipf_reg in that it
* reads the end of the archive and goes from there. Unlike that
* scan this one stops after reading the central directory and does
* not read the local headers. After the directory scan for new
* files is done in zip.c the zlist created here is used to read
* the old archive entries there. The local headers are read using
* readlocal() in zipcopy().
*
* This scan assumes the zip file is well structured. If not it may
* fail and the new scanzipf_fixnew should be used.
*
* 2006-2-4, 2007-12-10 EG
*/
local int scanzipf_regnew()
/*
The input path for the .zip file is in in_path. If a split archive,
the path for each split is created from the current disk number
and in_path. If a split is not in the same directory as the last
split we ask the user where it is and update in_path.
*/
/*
This is old but more or less still applies:
The name of the zip file is pointed to by the global "zipfile". The globals
zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
Return an error code in the ZE_ class.
*/
{
/* In this function, a local buffer is used to read in the following Zip
structures:
End-of-CentralDir record (EOCDR) (ENDHEAD)
Zip64-End-of-CentralDir-Record locator (Zip64 EOCDL) (EC64LOC)
Zip64-End-of-CentralDir record (Zip64 EOCDR) (EC64REC)
CentralDir-Entry record (CENHEAD)
To conserve valuable stack space, this buffer is sized to the largest
of these structures.
*/
# if CENHEAD > ENDHEAD
# define SCAN_BUFSIZE CENHEAD /* CENHEAD should be the larger struct */
# else
# define SCAN_BUFSIZE ENDHEAD
# endif
#ifdef ZIP64_SUPPORT
# if EC64REC > SCAN_BUFSIZE
# undef SCAN_BUFSIZE
# define SCAN_BUFSIZE EC64REC /* EC64 record should be largest struct */
# endif
# if EC64LOC > SCAN_BUFSIZE
# undef SCAN_BUFSIZE
# define SCAN_BUFSIZE EC64LOC
# endif
#endif
char scbuf[SCAN_BUFSIZE]; /* buffer just enough for all header types */
char *split_path;
ulg eocdr_disk;
uzoff_t eocdr_offset;
# ifdef ZIP64_SUPPORT
ulg z64eocdr_disk;
uzoff_t z64eocdr_offset;
uzoff_t z64eocdr_size;
ush version_made;
ush version_needed = 0;
zoff_t zip64_eocdr_start;
zoff_t z64eocdl_offset;
# endif /* def ZIP64_SUPPORT */
uzoff_t cd_total_entries; /* num of entries as read from (Zip64) EOCDR */
ulg in_cd_start_disk; /* central directory start disk */
uzoff_t in_cd_start_offset; /* offset of start of cd on cd start disk */
uzoff_t adjust_offset = 0; /* bytes before first entry (size of sfx prefix) */
uzoff_t cd_total_size = 0; /* total size of cd */
int first_CD = 1; /* looking for first CD entry */
int zipbegset = 0;
int skip_disk = 0; /* 1 if user asks to skip current disk */
int skipped_disk = 0; /* 1 if skipped start disk and start offset is useless */
uzoff_t 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 */
/* open the zipfile */
if ((in_file = zfopen(in_path, FOPR)) == NULL) {
zipwarn("could not open input archive", in_path);
return ZE_OPEN;
}
#ifndef ZIP64_SUPPORT
/* 2004-12-06 SMS.
* Check for too-big file before doing any serious work.
*/
if (ffile_size( in_file) == EOF) {
fclose(in_file);
in_file = NULL;
zipwarn("input file requires Zip64 support: ", in_path);
return ZE_ZIP64;
}
#endif /* ndef ZIP64_SUPPORT */
/* look for End Of Central Directory Record */
/* In a valid Zip archive, the EOCDR can be at most (64k-1 + ENDHEAD + 4)
bytes (=65557 bytes) from the end of the file.
We back up 128k, to allow some junk being appended to a Zip file.
*/
if ((zfseeko(in_file, -0x20000L, SEEK_END) != 0) ||
/* Some fseek() implementations (e.g. MSC 8.0 16-bit) fail to signal
an error when seeking before the beginning of the file.
As work-around, we check the position returned by zftello()
for the error value -1.
*/
(zftello(in_file) == (zoff_t)-1L)) {
/* file is less than 128 KB so back up to beginning */
if (zfseeko(in_file, 0L, SEEK_SET) != 0) {
fclose(in_file);
in_file = NULL;
zipwarn("unable to seek in input file ", in_path);
return ZE_READ;
}
}
/* find EOCD Record signature */
if (!find_signature(in_file, "PK\05\06")) {
/* No End Of Central Directory Record */
fclose(in_file);
in_file = NULL;
if (fix == 1) {
zipwarn("bad archive - missing end signature", "");
zipwarn("(If downloaded, was binary mode used? If not, the", "");
zipwarn(" archive may be scrambled and not recoverable)", "");
zipwarn("Can't use -F to fix (try -FF)", "");
} else{
zipwarn("missing end signature--probably not a zip file (did you", "");
zipwarn("remember to use binary mode when you transferred it?)", "");
zipwarn("(if you are trying to read a damaged archive try -F)", "");
}
return ZE_FORM;
}
/* at start of data after EOCDR signature */
eocdr_offset = (uzoff_t) zftello(in_file);
/* OK, it is possible this is not the last EOCDR signature (might be
EOCDR signature from a stored archive in the last 128 KB) and so not