8259867: Move encoding checks into ZipCoder
Reviewed-by: redestad, lancea
This commit is contained in:
parent
7c32ffea51
commit
05294802c4
src/java.base/share/classes/java/util/zip
@ -54,6 +54,14 @@ class ZipCoder {
|
||||
return new ZipCoder(charset);
|
||||
}
|
||||
|
||||
void checkEncoding(byte[] a, int pos, int nlen) throws ZipException {
|
||||
try {
|
||||
toString(a, pos, nlen);
|
||||
} catch(Exception e) {
|
||||
throw new ZipException("invalid CEN header (bad entry name)");
|
||||
}
|
||||
}
|
||||
|
||||
String toString(byte[] ba, int off, int length) {
|
||||
try {
|
||||
return decoder().decode(ByteBuffer.wrap(ba, off, length)).toString();
|
||||
@ -203,6 +211,25 @@ class ZipCoder {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
void checkEncoding(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) {
|
||||
throw new ZipException("invalid CEN header (bad entry name)");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
String toString(byte[] ba, int off, int length) {
|
||||
return JLA.newStringUTF8NoRepl(ba, off, length);
|
||||
|
@ -1309,31 +1309,6 @@ 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 class End {
|
||||
int centot; // 4 bytes
|
||||
@ -1520,13 +1495,10 @@ public class ZipFile implements ZipConstants, Closeable {
|
||||
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);
|
||||
}
|
||||
ZipCoder zcp = zipCoderForPos(pos);
|
||||
zcp.checkEncoding(cen, pos + CENHDR, nlen);
|
||||
// Record the CEN offset and the name hash in our hash cell.
|
||||
hash = zipCoderForPos(pos).normalizedHash(cen, entryPos, nlen);
|
||||
hash = zcp.normalizedHash(cen, entryPos, nlen);
|
||||
hsh = (hash & 0x7fffffff) % tablelen;
|
||||
next = table[hsh];
|
||||
table[hsh] = idx;
|
||||
|
Loading…
x
Reference in New Issue
Block a user