8233234: Better Zip Naming

Reviewed-by: alanb, ahgross, redestad, coffeys, mschoene, rhalade
This commit is contained in:
Lance Andersen 2020-01-31 13:52:04 -05:00
parent a5a46ff8e5
commit 8a616df8b5
4 changed files with 87 additions and 7 deletions

View File

@ -86,10 +86,14 @@ class ZipCoder {
}
}
String toStringUTF8(byte[] ba, int len) {
static String toStringUTF8(byte[] ba, int len) {
return UTF8.toString(ba, 0, len);
}
static String toStringUTF8(byte[] ba, int off, int len) {
return UTF8.toString(ba, off, len);
}
boolean isUTF8() {
return false;
}

View File

@ -790,7 +790,6 @@ public class ZipFile implements ZipConstants, Closeable {
throw new UncheckedIOException(ioe);
}
}
}
/**
@ -1296,6 +1295,44 @@ public class ZipFile implements ZipConstants, Closeable {
}
}
private static final void checkUTF8(byte[] a, int pos, int len) throws ZipException {
try {
int end = pos + len;
while (pos < end) {
// ASCII fast-path: When checking that a range of bytes is
// valid UTF-8, we can avoid some allocation by skipping
// past bytes in the 0-127 range
if (a[pos] < 0) {
ZipCoder.toStringUTF8(a, pos, end - pos);
break;
}
pos++;
}
} catch(Exception e) {
zerror("invalid CEN header (bad entry name)");
}
}
private final void checkEncoding(ZipCoder zc, byte[] a, int pos, int nlen) throws ZipException {
try {
zc.toString(a, pos, nlen);
} catch(Exception e) {
zerror("invalid CEN header (bad entry name)");
}
}
private static final int hashN(byte[] a, int off, int len) {
int h = 1;
while (len-- > 0) {
h = 31 * h + a[off++];
}
return h;
}
private static final int hash_append(int hash, byte b) {
return hash * 31 + b;
}
private static class End {
int centot; // 4 bytes
long cenlen; // 4 bytes
@ -1474,12 +1511,18 @@ public class ZipFile implements ZipConstants, Closeable {
int nlen = CENNAM(cen, pos);
int elen = CENEXT(cen, pos);
int clen = CENCOM(cen, pos);
if ((CENFLG(cen, pos) & 1) != 0)
int flag = CENFLG(cen, pos);
if ((flag & 1) != 0)
zerror("invalid CEN header (encrypted entry)");
if (method != STORED && method != DEFLATED)
zerror("invalid CEN header (bad compression method: " + method + ")");
if (entryPos + nlen > limit)
zerror("invalid CEN header (bad header size)");
if (zc.isUTF8() || (flag & USE_UTF8) != 0) {
checkUTF8(cen, pos + CENHDR, nlen);
} else {
checkEncoding(zc, cen, pos + CENHDR, nlen);
}
// Record the CEN offset and the name hash in our hash cell.
hash = zipCoderForPos(pos).normalizedHash(cen, entryPos, nlen);
hsh = (hash & 0x7fffffff) % tablelen;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -298,7 +298,7 @@ public class ZipInputStream extends InflaterInputStream implements ZipConstants
readFully(b, 0, len);
// Force to use UTF-8 if the USE_UTF8 bit is ON
ZipEntry e = createZipEntry(((flag & USE_UTF8) != 0)
? zc.toStringUTF8(b, len)
? ZipCoder.toStringUTF8(b, len)
: zc.toString(b, len));
// now get the remaining fields for the entry
if ((flag & 1) == 1) {

View File

@ -1550,7 +1550,8 @@ class ZipFileSystem extends FileSystem {
int nlen = CENNAM(cen, pos);
int elen = CENEXT(cen, pos);
int clen = CENCOM(cen, pos);
if ((CENFLG(cen, pos) & 1) != 0) {
int flag = CENFLG(cen, pos);
if ((flag & 1) != 0) {
throw new ZipException("invalid CEN header (encrypted entry)");
}
if (method != METHOD_STORED && method != METHOD_DEFLATED) {
@ -1561,7 +1562,11 @@ class ZipFileSystem extends FileSystem {
}
IndexNode inode = new IndexNode(cen, pos, nlen);
inodes.put(inode, inode);
if (zc.isUTF8() || (flag & FLAG_USE_UTF8) != 0) {
checkUTF8(inode.name);
} else {
checkEncoding(inode.name);
}
// skip ext and comment
pos += (CENHDR + nlen + elen + clen);
}
@ -1572,6 +1577,34 @@ class ZipFileSystem extends FileSystem {
return cen;
}
private final void checkUTF8(byte[] a) throws ZipException {
try {
int end = a.length;
int pos = 0;
while (pos < end) {
// ASCII fast-path: When checking that a range of bytes is
// valid UTF-8, we can avoid some allocation by skipping
// past bytes in the 0-127 range
if (a[pos] < 0) {
zc.toString(Arrays.copyOfRange(a, pos, a.length));
break;
}
pos++;
}
} catch(Exception e) {
throw new ZipException("invalid CEN header (bad entry name)");
}
}
private final void checkEncoding( byte[] a) throws ZipException {
try {
zc.toString(a);
} catch(Exception e) {
throw new ZipException("invalid CEN header (bad entry name)");
}
}
private void ensureOpen() {
if (!isOpen)
throw new ClosedFileSystemException();