/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
int
JAR_extract(JAR *jar, char *path, char *outpath)
{ int result;
JAR_Physical *phy;
if (jar->fp == NULL && jar->filename) {
jar->fp = (FILE *)JAR_FOPEN(jar->filename, "rb");
} if (jar->fp == NULL) { /* file not found */ return JAR_ERR_FNF;
}
phy = jar_get_physical(jar, path); if (phy) { if (phy->compression == 0) {
result = jar_physical_extraction((PRFileDesc *)jar->fp, outpath, phy->offset, phy->length);
} else { /* compression methods other than 8 are unsupported, *butforhistoricalreasons,jar_physical_inflatewillbecalledfor
* unsupported compression method constants too. */
result = jar_physical_inflate((PRFileDesc *)jar->fp, outpath,
phy->offset, phy->length,
(unsignedint)phy->compression);
}
#ifdefined(XP_UNIX) if (phy->mode)
chmod(outpath, 0400 | (mode_t)phy->mode); #endif
} else { /* pathname not found in archive */
result = JAR_ERR_PNF;
} return result;
}
/* *jar_extract_manifests * *Extractthemanifestfilesandparsethem, *fromanopenarchivefilewhosecontentsareknown. *
*/ staticint
jar_extract_manifests(JAR *jar, jarArch format, JAR_FILE fp)
{ int status, signatures;
if (format != jarArchZip && format != jarArchTar) return JAR_ERR_CORRUPT;
if ((status = jar_extract_mf(jar, format, fp, "mf")) < 0) return status; if (!status) return JAR_ERR_ORDER; if ((status = jar_extract_mf(jar, format, fp, "sf")) < 0) return status; if (!status) return JAR_ERR_ORDER; if ((status = jar_extract_mf(jar, format, fp, "rsa")) < 0) return status;
signatures = status; if ((status = jar_extract_mf(jar, format, fp, "dsa")) < 0) return status; if (!(signatures += status)) return JAR_ERR_SIG; return0;
}
/* *jar_extract_mf * *Extractsmanifestfilesbasedonanextension,which *shouldbe.MF,.SF,.RSA,etc.Orderofthefilesisnowno *longerimportantwhenzippingjarfiles. *
*/ staticint
jar_extract_mf(JAR *jar, jarArch format, JAR_FILE fp, char *ext)
{
ZZLink *link;
ZZList *list = jar->phy; int ret = 0;
if (ZZ_ListEmpty(list)) return JAR_ERR_PNF;
for (link = ZZ_ListHead(list);
ret >= 0 && !ZZ_ListIterDone(list, link);
link = link->next) {
JAR_Item *it = link->thing;
if (it->type == jarTypePhy &&
!PORT_Strncmp(it->pathname, "META-INF", 8)) {
JAR_Physical *phy = (JAR_Physical *)it->data; char *fn = it->pathname + 8; char *e; char *manifest; long length; int num, status;
if (PORT_Strlen(it->pathname) < 8) continue;
if (*fn == '/' || *fn == '\\')
fn++; if (*fn == 0) { /* just a directory entry */ continue;
}
/* skip to extension */ for (e = fn; *e && *e != '.'; e++) /* yip */;
/* and skip dot */ if (*e == '.')
e++; if (PORT_Strcasecmp(ext, e)) { /* not the right extension */ continue;
} if (phy->length == 0 || phy->length > 0xFFFF) { /* manifest files cannot be zero length or too big! */ /* the 0xFFFF limit is per J2SE SDK */ return JAR_ERR_CORRUPT;
}
/* Read in the manifest and parse it */ /* Raw inflate in zlib 1.1.4 needs an extra dummy byte at the end */
manifest = (char *)PORT_ZAlloc(phy->length + 1); if (!manifest) return JAR_ERR_MEMORY;
if (JAR_FREAD(fp, filename, filename_len) != filename_len) { /* truncated archive file */
err = JAR_ERR_CORRUPT; goto loser;
}
filename[filename_len] = 0; /* Add this to our jar chain */
phy = PORT_ZNew(JAR_Physical); if (phy == NULL) {
err = JAR_ERR_MEMORY; goto loser;
}
/* We will index any file that comes our way, but when it comes
to actually extraction, compression must be 0 or 8 */
compression = x86ShortToUint32(Local->method);
phy->compression = (compression <= 255) ? compression : 222; /* XXX 222 is bad magic. */
#ifdefined(XP_UNIX) /* with unix we need to locate any bits from
the protection mask in the external attributes. */
attr = Central->external_attributes[2]; /* magic */ if (attr) { /* we have to read the filename, again */
filename_len = x86ShortToUint32(Central->filename_len); if (filename_len >= JAR_SIZE) { /* corrupt in central directory */
err = JAR_ERR_CORRUPT; goto loser;
}
if (JAR_FREAD(fp, filename, filename_len) != filename_len) { /* truncated in central directory */
err = JAR_ERR_CORRUPT; goto loser;
}
filename[filename_len] = 0;
/* look up this name again */
phy = jar_get_physical(jar, filename); if (phy) { /* always allow access by self */
phy->mode = 0400 | attr;
}
} #endif
pos += sizeof(struct ZipCentral) +
x86ShortToUint32(Central->filename_len) +
x86ShortToUint32(Central->commentfield_len) +
x86ShortToUint32(Central->extrafield_len);
} elseif (sigVal == ESIG) { if (JAR_FREAD(fp, End, sizeof *End) != sizeof *End) {
err = JAR_ERR_CORRUPT; goto loser;
} break;
} else { /* garbage in archive */
err = JAR_ERR_CORRUPT; goto loser;
}
}
loser: if (Local)
PORT_Free(Local); if (phy && it == NULL)
PORT_Free(phy); if (Central)
PORT_Free(Central); if (End)
PORT_Free(End); return err;
}
/* *jar_listtar * *ListthephysicalcontentsofaUnix *.tarfileintotheJARlinkedlist. *
*/ staticint
jar_listtar(JAR *jar, JAR_FILE fp)
{ char *s;
JAR_Physical *phy; long pos = 0L; long sz; union TarEntry tarball;
while (1) {
JAR_FSEEK(fp, pos, (PRSeekWhence)0);
if (JAR_FREAD(fp, &tarball, sizeof tarball) < sizeof tarball) break;
/* Force NUL-termination within the fixed-size filename field so
* the unbounded string scan below cannot walk past it. */
tarball.val.filename[sizeof tarball.val.filename - 1] = 0;
if (!*tarball.val.filename) break;
sz = octalToLong(tarball.val.size);
/* Tag the end of filename */
s = tarball.val.filename; while (*s && *s != ' ')
s++;
*s = 0;
/* Add to our linked list */
phy = PORT_ZNew(JAR_Physical); if (phy == NULL) return JAR_ERR_MEMORY;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.