8259867: Move encoding checks into ZipCoder

Reviewed-by: redestad, lancea
This commit is contained in:
Eirik Bjorsnos 2021-01-20 12:02:39 +00:00 committed by Claes Redestad
parent 7c32ffea51
commit 05294802c4
2 changed files with 30 additions and 31 deletions

View file

@ -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);