| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util.apk; |
| 18 |
|
| 19 |
import android.util.Pair; |
| 20 |
|
| 21 |
import java.io.IOException; |
| 22 |
import java.io.RandomAccessFile; |
| 23 |
import java.nio.ByteBuffer; |
| 24 |
import java.nio.ByteOrder; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
abstract class ZipUtils { |
| 33 |
private ZipUtils() {} |
| 34 |
|
| 35 |
private static final int ZIP_EOCD_REC_MIN_SIZE = 22; |
| 36 |
private static final int ZIP_EOCD_REC_SIG = 0x06054b50; |
| 37 |
private static final int ZIP_EOCD_CENTRAL_DIR_SIZE_FIELD_OFFSET = 12; |
| 38 |
private static final int ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET = 16; |
| 39 |
private static final int ZIP_EOCD_COMMENT_LENGTH_FIELD_OFFSET = 20; |
| 40 |
|
| 41 |
private static final int ZIP64_EOCD_LOCATOR_SIZE = 20; |
| 42 |
private static final int ZIP64_EOCD_LOCATOR_SIG_REVERSE_BYTE_ORDER = 0x504b0607; |
| 43 |
|
| 44 |
private static final int UINT16_MAX_VALUE = 0xffff; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static Pair<ByteBuffer, Long> findZipEndOfCentralDirectoryRecord(RandomAccessFile zip) |
| 55 |
throws IOException { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
long fileSize = zip.getChannel().size(); |
| 68 |
if (fileSize < ZIP_EOCD_REC_MIN_SIZE) { |
| 69 |
return null; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
Pair<ByteBuffer, Long> result = findZipEndOfCentralDirectoryRecord(zip, 0); |
| 76 |
if (result != null) { |
| 77 |
return result; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
return findZipEndOfCentralDirectoryRecord(zip, UINT16_MAX_VALUE); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
private static Pair<ByteBuffer, Long> findZipEndOfCentralDirectoryRecord( |
| 99 |
RandomAccessFile zip, int maxCommentSize) throws IOException { |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
if ((maxCommentSize < 0) || (maxCommentSize > UINT16_MAX_VALUE)) { |
| 111 |
throw new IllegalArgumentException("maxCommentSize: " + maxCommentSize); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
long fileSize = zip.getChannel().size(); |
| 116 |
if (fileSize < ZIP_EOCD_REC_MIN_SIZE) { |
| 117 |
|
| 118 |
return null; |
| 119 |
} |
| 120 |
|
| 121 |
maxCommentSize = (int) Math.min(maxCommentSize, fileSize - ZIP_EOCD_REC_MIN_SIZE); |
| 122 |
|
| 123 |
ByteBuffer buf = ByteBuffer.allocate(ZIP_EOCD_REC_MIN_SIZE + maxCommentSize); |
| 124 |
buf.order(ByteOrder.LITTLE_ENDIAN); |
| 125 |
long bufOffsetInFile = fileSize - buf.capacity(); |
| 126 |
zip.seek(bufOffsetInFile); |
| 127 |
zip.readFully(buf.array(), buf.arrayOffset(), buf.capacity()); |
| 128 |
int eocdOffsetInBuf = findZipEndOfCentralDirectoryRecord(buf); |
| 129 |
if (eocdOffsetInBuf == -1) { |
| 130 |
|
| 131 |
return null; |
| 132 |
} |
| 133 |
|
| 134 |
buf.position(eocdOffsetInBuf); |
| 135 |
ByteBuffer eocd = buf.slice(); |
| 136 |
eocd.order(ByteOrder.LITTLE_ENDIAN); |
| 137 |
return Pair.create(eocd, bufOffsetInFile + eocdOffsetInBuf); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
private static int findZipEndOfCentralDirectoryRecord(ByteBuffer zipContents) { |
| 147 |
assertByteOrderLittleEndian(zipContents); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
int archiveSize = zipContents.capacity(); |
| 160 |
if (archiveSize < ZIP_EOCD_REC_MIN_SIZE) { |
| 161 |
return -1; |
| 162 |
} |
| 163 |
int maxCommentLength = Math.min(archiveSize - ZIP_EOCD_REC_MIN_SIZE, UINT16_MAX_VALUE); |
| 164 |
int eocdWithEmptyCommentStartPosition = archiveSize - ZIP_EOCD_REC_MIN_SIZE; |
| 165 |
for (int expectedCommentLength = 0; expectedCommentLength <= maxCommentLength; |
| 166 |
expectedCommentLength++) { |
| 167 |
int eocdStartPos = eocdWithEmptyCommentStartPosition - expectedCommentLength; |
| 168 |
if (zipContents.getInt(eocdStartPos) == ZIP_EOCD_REC_SIG) { |
| 169 |
int actualCommentLength = |
| 170 |
getUnsignedInt16( |
| 171 |
zipContents, eocdStartPos + ZIP_EOCD_COMMENT_LENGTH_FIELD_OFFSET); |
| 172 |
if (actualCommentLength == expectedCommentLength) { |
| 173 |
return eocdStartPos; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return -1; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public static final boolean isZip64EndOfCentralDirectoryLocatorPresent( |
| 191 |
RandomAccessFile zip, long zipEndOfCentralDirectoryPosition) throws IOException { |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
long locatorPosition = zipEndOfCentralDirectoryPosition - ZIP64_EOCD_LOCATOR_SIZE; |
| 196 |
if (locatorPosition < 0) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
zip.seek(locatorPosition); |
| 201 |
|
| 202 |
|
| 203 |
return zip.readInt() == ZIP64_EOCD_LOCATOR_SIG_REVERSE_BYTE_ORDER; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public static long getZipEocdCentralDirectoryOffset(ByteBuffer zipEndOfCentralDirectory) { |
| 212 |
assertByteOrderLittleEndian(zipEndOfCentralDirectory); |
| 213 |
return getUnsignedInt32( |
| 214 |
zipEndOfCentralDirectory, |
| 215 |
zipEndOfCentralDirectory.position() + ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public static void setZipEocdCentralDirectoryOffset( |
| 224 |
ByteBuffer zipEndOfCentralDirectory, long offset) { |
| 225 |
assertByteOrderLittleEndian(zipEndOfCentralDirectory); |
| 226 |
setUnsignedInt32( |
| 227 |
zipEndOfCentralDirectory, |
| 228 |
zipEndOfCentralDirectory.position() + ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET, |
| 229 |
offset); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public static long getZipEocdCentralDirectorySizeBytes(ByteBuffer zipEndOfCentralDirectory) { |
| 238 |
assertByteOrderLittleEndian(zipEndOfCentralDirectory); |
| 239 |
return getUnsignedInt32( |
| 240 |
zipEndOfCentralDirectory, |
| 241 |
zipEndOfCentralDirectory.position() + ZIP_EOCD_CENTRAL_DIR_SIZE_FIELD_OFFSET); |
| 242 |
} |
| 243 |
|
| 244 |
private static void assertByteOrderLittleEndian(ByteBuffer buffer) { |
| 245 |
if (buffer.order() != ByteOrder.LITTLE_ENDIAN) { |
| 246 |
throw new IllegalArgumentException("ByteBuffer byte order must be little endian"); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
private static int getUnsignedInt16(ByteBuffer buffer, int offset) { |
| 251 |
return buffer.getShort(offset) & 0xffff; |
| 252 |
} |
| 253 |
|
| 254 |
private static long getUnsignedInt32(ByteBuffer buffer, int offset) { |
| 255 |
return buffer.getInt(offset) & 0xffffffffL; |
| 256 |
} |
| 257 |
|
| 258 |
private static void setUnsignedInt32(ByteBuffer buffer, int offset, long value) { |
| 259 |
if ((value < 0) || (value > 0xffffffffL)) { |
| 260 |
throw new IllegalArgumentException("uint32 value of out range: " + value); |
| 261 |
} |
| 262 |
buffer.putInt(buffer.position() + offset, (int) value); |
| 263 |
} |
| 264 |
} |