Skip to content
Permalink
Browse files
8259867: Move encoding checks into ZipCoder
Reviewed-by: redestad, lancea
  • Loading branch information
eirbjo authored and cl4es committed Jan 20, 2021
1 parent 7c32ffe commit 05294802c4679b617281e707703780ccfb10330d
@@ -54,6 +54,14 @@ public static ZipCoder get(Charset charset) {
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 @@ boolean isUTF8() {
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 @@ private final int readAt(byte[] buf, int off, int len, long pos)
}
}

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 @@ private void initCEN(int knownTotal) throws IOException {
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;

1 comment on commit 0529480

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 0529480 Jan 20, 2021

Please sign in to comment.