| 1 |
/* |
| 2 |
* Copyright (C) 2010 Ryszard Wiśniewski <[email protected]> |
| 3 |
* Copyright (C) 2010 Connor Tumbleson <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package brut.androlib.res.decoder; |
| 18 |
|
| 19 |
import android.util.TypedValue; |
| 20 |
import brut.androlib.AndrolibException; |
| 21 |
import brut.androlib.res.data.*; |
| 22 |
import brut.androlib.res.data.value.*; |
| 23 |
import brut.util.Duo; |
| 24 |
import brut.util.ExtDataInput; |
| 25 |
import com.google.common.io.LittleEndianDataInputStream; |
| 26 |
import org.apache.commons.io.input.CountingInputStream; |
| 27 |
|
| 28 |
import java.io.DataInput; |
| 29 |
import java.io.EOFException; |
| 30 |
import java.io.IOException; |
| 31 |
import java.io.InputStream; |
| 32 |
import java.math.BigInteger; |
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.Arrays; |
| 35 |
import java.util.HashMap; |
| 36 |
import java.util.LinkedHashMap; |
| 37 |
import java.util.List; |
| 38 |
import java.util.logging.Logger; |
| 39 |
|
| 40 |
public class ARSCDecoder { |
| 41 |
public static ARSCData decode(InputStream arscStream, boolean findFlagsOffsets, boolean keepBroken) |
| 42 |
throws AndrolibException { |
| 43 |
return decode(arscStream, findFlagsOffsets, keepBroken, new ResTable()); |
| 44 |
} |
| 45 |
|
| 46 |
public static ARSCData decode(InputStream arscStream, boolean findFlagsOffsets, boolean keepBroken, |
| 47 |
ResTable resTable) |
| 48 |
throws AndrolibException { |
| 49 |
try { |
| 50 |
ARSCDecoder decoder = new ARSCDecoder(arscStream, resTable, findFlagsOffsets, keepBroken); |
| 51 |
ResPackage[] pkgs = decoder.readTableHeader(); |
| 52 |
return new ARSCData(pkgs, decoder.mFlagsOffsets == null |
| 53 |
? null |
| 54 |
: decoder.mFlagsOffsets.toArray(new FlagsOffset[0]), resTable); |
| 55 |
} catch (IOException ex) { |
| 56 |
throw new AndrolibException("Could not decode arsc file", ex); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
private ARSCDecoder(InputStream arscStream, ResTable resTable, boolean storeFlagsOffsets, boolean keepBroken) { |
| 61 |
arscStream = mCountIn = new CountingInputStream(arscStream); |
| 62 |
if (storeFlagsOffsets) { |
| 63 |
mFlagsOffsets = new ArrayList<>(); |
| 64 |
} else { |
| 65 |
mFlagsOffsets = null; |
| 66 |
} |
| 67 |
// We need to explicitly cast to DataInput as otherwise the constructor is ambiguous. |
| 68 |
// We choose DataInput instead of InputStream as ExtDataInput wraps an InputStream in |
| 69 |
// a DataInputStream which is big-endian and ignores the little-endian behavior. |
| 70 |
mIn = new ExtDataInput((DataInput) new LittleEndianDataInputStream(arscStream)); |
| 71 |
mResTable = resTable; |
| 72 |
mKeepBroken = keepBroken; |
| 73 |
} |
| 74 |
|
| 75 |
private ResPackage[] readTableHeader() throws IOException, AndrolibException { |
| 76 |
nextChunkCheckType(Header.TYPE_TABLE); |
| 77 |
int packageCount = mIn.readInt(); |
| 78 |
|
| 79 |
mTableStrings = StringBlock.read(mIn); |
| 80 |
ResPackage[] packages = new ResPackage[packageCount]; |
| 81 |
|
| 82 |
nextChunk(); |
| 83 |
for (int i = 0; i < packageCount; i++) { |
| 84 |
mTypeIdOffset = 0; |
| 85 |
packages[i] = readTablePackage(); |
| 86 |
} |
| 87 |
return packages; |
| 88 |
} |
| 89 |
|
| 90 |
private ResPackage readTablePackage() throws IOException, AndrolibException { |
| 91 |
checkChunkType(Header.XML_TYPE_PACKAGE); |
| 92 |
int id = mIn.readInt(); |
| 93 |
|
| 94 |
if (id == 0) { |
| 95 |
// This means we are dealing with a Library Package, we should just temporarily |
| 96 |
// set the packageId to the next available id . This will be set at runtime regardless, but |
| 97 |
// for Apktool's use we need a non-zero packageId. |
| 98 |
// AOSP indicates 0x02 is next, as 0x01 is system and 0x7F is private. |
| 99 |
id = 2; |
| 100 |
if (mResTable.getPackageOriginal() == null && mResTable.getPackageRenamed() == null) { |
| 101 |
mResTable.setSharedLibrary(true); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
String name = mIn.readNullEndedString(128, true); |
| 106 |
/* typeStrings */mIn.skipInt(); |
| 107 |
/* lastPublicType */mIn.skipInt(); |
| 108 |
/* keyStrings */mIn.skipInt(); |
| 109 |
/* lastPublicKey */mIn.skipInt(); |
| 110 |
|
| 111 |
// TypeIdOffset was added platform_frameworks_base/@f90f2f8dc36e7243b85e0b6a7fd5a590893c827e |
| 112 |
// which is only in split/new applications. |
| 113 |
int splitHeaderSize = (2 + 2 + 4 + 4 + (2 * 128) + (4 * 5)); // short, short, int, int, char[128], int * 4 |
| 114 |
if (mHeader.headerSize == splitHeaderSize) { |
| 115 |
mTypeIdOffset = mIn.readInt(); |
| 116 |
} |
| 117 |
|
| 118 |
if (mTypeIdOffset > 0) { |
| 119 |
LOGGER.warning("Please report this application to Apktool for a fix: https://github.com/iBotPeaches/Apktool/issues/1728"); |
| 120 |
} |
| 121 |
|
| 122 |
mTypeNames = StringBlock.read(mIn); |
| 123 |
mSpecNames = StringBlock.read(mIn); |
| 124 |
|
| 125 |
mResId = id << 24; |
| 126 |
mPkg = new ResPackage(mResTable, id, name); |
| 127 |
|
| 128 |
nextChunk(); |
| 129 |
boolean flag = true; |
| 130 |
while (flag) { |
| 131 |
switch (mHeader.type) { |
| 132 |
case Header.XML_TYPE_SPEC_TYPE: |
| 133 |
readTableTypeSpec(); |
| 134 |
break; |
| 135 |
case Header.XML_TYPE_LIBRARY: |
| 136 |
readLibraryType(); |
| 137 |
break; |
| 138 |
case Header.XML_TYPE_OVERLAY: |
| 139 |
readOverlaySpec(); |
| 140 |
break; |
| 141 |
case Header.XML_TYPE_STAGED_ALIAS: |
| 142 |
readStagedAliasSpec(); |
| 143 |
break; |
| 144 |
default: |
| 145 |
flag = false; |
| 146 |
break; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return mPkg; |
| 151 |
} |
| 152 |
|
| 153 |
private void readLibraryType() throws AndrolibException, IOException { |
| 154 |
checkChunkType(Header.XML_TYPE_LIBRARY); |
| 155 |
int libraryCount = mIn.readInt(); |
| 156 |
|
| 157 |
int packageId; |
| 158 |
String packageName; |
| 159 |
|
| 160 |
for (int i = 0; i < libraryCount; i++) { |
| 161 |
packageId = mIn.readInt(); |
| 162 |
packageName = mIn.readNullEndedString(128, true); |
| 163 |
LOGGER.info(String.format("Decoding Shared Library (%s), pkgId: %d", packageName, packageId)); |
| 164 |
} |
| 165 |
|
| 166 |
while(nextChunk().type == Header.XML_TYPE_TYPE) { |
| 167 |
readTableTypeSpec(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private void readStagedAliasSpec() throws IOException { |
| 172 |
int count = mIn.readInt(); |
| 173 |
|
| 174 |
for (int i = 0; i < count; i++) { |
| 175 |
LOGGER.fine(String.format("Skipping staged alias stagedId (%h) finalId: %h", mIn.readInt(), mIn.readInt())); |
| 176 |
} |
| 177 |
|
| 178 |
nextChunk(); |
| 179 |
} |
| 180 |
|
| 181 |
private void readOverlaySpec() throws AndrolibException, IOException { |
| 182 |
checkChunkType(Header.XML_TYPE_OVERLAY); |
| 183 |
String name = mIn.readNullEndedString(128, true); |
| 184 |
String actor = mIn.readNullEndedString(128, true); |
| 185 |
LOGGER.fine(String.format("Overlay name: \"%s\", actor: \"%s\")", name, actor)); |
| 186 |
|
| 187 |
while(nextChunk().type == Header.XML_TYPE_OVERLAY_POLICY) { |
| 188 |
readOverlayPolicySpec(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
private void readOverlayPolicySpec() throws AndrolibException, IOException { |
| 193 |
checkChunkType(Header.XML_TYPE_OVERLAY_POLICY); |
| 194 |
/* policyFlags */mIn.skipInt(); |
| 195 |
int count = mIn.readInt(); |
| 196 |
|
| 197 |
for (int i = 0; i < count; i++) { |
| 198 |
LOGGER.fine(String.format("Skipping overlay (%h)", mIn.readInt())); |
| 199 |
} |
| 200 |
|
| 201 |
nextChunk(); |
| 202 |
} |
| 203 |
|
| 204 |
private void readTableTypeSpec() throws AndrolibException, IOException { |
| 205 |
mTypeSpec = readSingleTableTypeSpec(); |
| 206 |
addTypeSpec(mTypeSpec); |
| 207 |
|
| 208 |
int type = nextChunk().type; |
| 209 |
ResTypeSpec resTypeSpec; |
| 210 |
|
| 211 |
while (type == Header.XML_TYPE_SPEC_TYPE) { |
| 212 |
resTypeSpec = readSingleTableTypeSpec(); |
| 213 |
addTypeSpec(resTypeSpec); |
| 214 |
type = nextChunk().type; |
| 215 |
|
| 216 |
// We've detected sparse resources, lets record this so we can rebuild in that same format (sparse/not) |
| 217 |
// with aapt2. aapt1 will ignore this. |
| 218 |
if (! mResTable.getSparseResources()) { |
| 219 |
mResTable.setSparseResources(true); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
while (type == Header.XML_TYPE_TYPE) { |
| 224 |
readTableType(); |
| 225 |
|
| 226 |
// skip "TYPE 8 chunks" and/or padding data at the end of this chunk |
| 227 |
if (mCountIn.getCount() < mHeader.endPosition) { |
| 228 |
LOGGER.warning("Unknown data detected. Skipping: " + (mHeader.endPosition - mCountIn.getCount()) + " byte(s)"); |
| 229 |
mCountIn.skip(mHeader.endPosition - mCountIn.getCount()); |
| 230 |
} |
| 231 |
|
| 232 |
type = nextChunk().type; |
| 233 |
|
| 234 |
addMissingResSpecs(); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
private ResTypeSpec readSingleTableTypeSpec() throws AndrolibException, IOException { |
| 239 |
checkChunkType(Header.XML_TYPE_SPEC_TYPE); |
| 240 |
int id = mIn.readUnsignedByte(); |
| 241 |
mIn.skipBytes(3); |
| 242 |
int entryCount = mIn.readInt(); |
| 243 |
|
| 244 |
if (mFlagsOffsets != null) { |
| 245 |
mFlagsOffsets.add(new FlagsOffset(mCountIn.getCount(), entryCount)); |
| 246 |
} |
| 247 |
|
| 248 |
/* flags */mIn.skipBytes(entryCount * 4); |
| 249 |
mTypeSpec = new ResTypeSpec(mTypeNames.getString(id - 1), mResTable, mPkg, id, entryCount); |
| 250 |
mPkg.addType(mTypeSpec); |
| 251 |
return mTypeSpec; |
| 252 |
} |
| 253 |
|
| 254 |
private ResType readTableType() throws IOException, AndrolibException { |
| 255 |
checkChunkType(Header.XML_TYPE_TYPE); |
| 256 |
int typeId = mIn.readUnsignedByte() - mTypeIdOffset; |
| 257 |
if (mResTypeSpecs.containsKey(typeId)) { |
| 258 |
mResId = (0xff000000 & mResId) | mResTypeSpecs.get(typeId).getId() << 16; |
| 259 |
mTypeSpec = mResTypeSpecs.get(typeId); |
| 260 |
} |
| 261 |
|
| 262 |
int typeFlags = mIn.readByte(); |
| 263 |
/* reserved */mIn.skipBytes(2); |
| 264 |
int entryCount = mIn.readInt(); |
| 265 |
int entriesStart = mIn.readInt(); |
| 266 |
mMissingResSpecMap = new LinkedHashMap(); |
| 267 |
|
| 268 |
ResConfigFlags flags = readConfigFlags(); |
| 269 |
int position = (mHeader.startPosition + entriesStart) - (entryCount * 4); |
| 270 |
|
| 271 |
// For some APKs there is a disconnect between the reported size of Configs |
| 272 |
// If we find a mismatch skip those bytes. |
| 273 |
if (position != mCountIn.getCount()) { |
| 274 |
LOGGER.warning("Invalid data detected. Skipping: " + (position - mCountIn.getCount()) + " byte(s)"); |
| 275 |
mIn.skipBytes(position - mCountIn.getCount()); |
| 276 |
} |
| 277 |
|
| 278 |
if ((typeFlags & 0x01) != 0) { |
| 279 |
LOGGER.info("Sparse type flags detected: " + mTypeSpec.getName()); |
| 280 |
} |
| 281 |
|
| 282 |
HashMap<Integer, Integer> entryOffsetMap = new LinkedHashMap(); |
| 283 |
for (int i = 0; i < entryCount; i++) { |
| 284 |
if ((typeFlags & 0x01) != 0) { |
| 285 |
entryOffsetMap.put(mIn.readUnsignedShort(), mIn.readUnsignedShort()); |
| 286 |
} else { |
| 287 |
entryOffsetMap.put(i, mIn.readInt()); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if (flags.isInvalid) { |
| 292 |
String resName = mTypeSpec.getName() + flags.getQualifiers(); |
| 293 |
if (mKeepBroken) { |
| 294 |
LOGGER.warning("Invalid config flags detected: " + resName); |
| 295 |
} else { |
| 296 |
LOGGER.warning("Invalid config flags detected. Dropping resources: " + resName); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
mType = flags.isInvalid && !mKeepBroken ? null : mPkg.getOrCreateConfig(flags); |
| 301 |
|
| 302 |
for (int i : entryOffsetMap.keySet()) { |
| 303 |
int offset = entryOffsetMap.get(i); |
| 304 |
if (offset == -1) { |
| 305 |
continue; |
| 306 |
} |
| 307 |
mMissingResSpecMap.put(i, false); |
| 308 |
mResId = (mResId & 0xffff0000) | i; |
| 309 |
readEntry(readEntryData()); |
| 310 |
} |
| 311 |
|
| 312 |
return mType; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
private EntryData readEntryData() throws IOException, AndrolibException { |
| 317 |
short size = mIn.readShort(); |
| 318 |
if (size < 0) { |
| 319 |
throw new AndrolibException("Entry size is under 0 bytes."); |
| 320 |
} |
| 321 |
|
| 322 |
short flags = mIn.readShort(); |
| 323 |
int specNamesId = mIn.readInt(); |
| 324 |
ResValue value = (flags & ENTRY_FLAG_COMPLEX) == 0 ? readValue() : readComplexEntry(); |
| 325 |
EntryData entryData = new EntryData(); |
| 326 |
entryData.mFlags = flags; |
| 327 |
entryData.mSpecNamesId = specNamesId; |
| 328 |
entryData.mValue = value; |
| 329 |
return entryData; |
| 330 |
} |
| 331 |
|
| 332 |
private void readEntry(EntryData entryData) throws AndrolibException { |
| 333 |
int specNamesId = entryData.mSpecNamesId; |
| 334 |
ResValue value = entryData.mValue; |
| 335 |
|
| 336 |
if (mTypeSpec.isString() && value instanceof ResFileValue) { |
| 337 |
value = new ResStringValue(value.toString(), ((ResFileValue) value).getRawIntValue()); |
| 338 |
} |
| 339 |
if (mType == null) { |
| 340 |
return; |
| 341 |
} |
| 342 |
|
| 343 |
ResID resId = new ResID(mResId); |
| 344 |
ResResSpec spec; |
| 345 |
if (mPkg.hasResSpec(resId)) { |
| 346 |
spec = mPkg.getResSpec(resId); |
| 347 |
|
| 348 |
if (spec.isDummyResSpec()) { |
| 349 |
removeResSpec(spec); |
| 350 |
|
| 351 |
spec = new ResResSpec(resId, mSpecNames.getString(specNamesId), mPkg, mTypeSpec); |
| 352 |
mPkg.addResSpec(spec); |
| 353 |
mTypeSpec.addResSpec(spec); |
| 354 |
} |
| 355 |
} else { |
| 356 |
spec = new ResResSpec(resId, mSpecNames.getString(specNamesId), mPkg, mTypeSpec); |
| 357 |
mPkg.addResSpec(spec); |
| 358 |
mTypeSpec.addResSpec(spec); |
| 359 |
} |
| 360 |
ResResource res = new ResResource(mType, spec, value); |
| 361 |
|
| 362 |
try { |
| 363 |
mType.addResource(res); |
| 364 |
spec.addResource(res); |
| 365 |
} catch (AndrolibException ex) { |
| 366 |
if (mKeepBroken) { |
| 367 |
mType.addResource(res, true); |
| 368 |
spec.addResource(res, true); |
| 369 |
LOGGER.warning(String.format("Duplicate Resource Detected. Ignoring duplicate: %s", res)); |
| 370 |
} else { |
| 371 |
throw ex; |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
private ResBagValue readComplexEntry() throws IOException, AndrolibException { |
| 377 |
int parent = mIn.readInt(); |
| 378 |
int count = mIn.readInt(); |
| 379 |
|
| 380 |
ResValueFactory factory = mPkg.getValueFactory(); |
| 381 |
Duo<Integer, ResScalarValue>[] items = new Duo[count]; |
| 382 |
ResIntBasedValue resValue; |
| 383 |
int resId; |
| 384 |
|
| 385 |
for (int i = 0; i < count; i++) { |
| 386 |
resId = mIn.readInt(); |
| 387 |
resValue = readValue(); |
| 388 |
|
| 389 |
if (!(resValue instanceof ResScalarValue)) { |
| 390 |
resValue = new ResStringValue(resValue.toString(), resValue.getRawIntValue()); |
| 391 |
} |
| 392 |
items[i] = new Duo<>(resId, (ResScalarValue) resValue); |
| 393 |
} |
| 394 |
|
| 395 |
return factory.bagFactory(parent, items, mTypeSpec); |
| 396 |
} |
| 397 |
|
| 398 |
private ResIntBasedValue readValue() throws IOException, AndrolibException { |
| 399 |
/* size */mIn.skipCheckShort((short) 8); |
| 400 |
/* zero */mIn.skipCheckByte((byte) 0); |
| 401 |
byte type = mIn.readByte(); |
| 402 |
int data = mIn.readInt(); |
| 403 |
|
| 404 |
return type == TypedValue.TYPE_STRING |
| 405 |
? mPkg.getValueFactory().factory(mTableStrings.getHTML(data), data) |
| 406 |
: mPkg.getValueFactory().factory(type, data, null); |
| 407 |
} |
| 408 |
|
| 409 |
private ResConfigFlags readConfigFlags() throws IOException, AndrolibException { |
| 410 |
int size = mIn.readInt(); |
| 411 |
int read = 28; |
| 412 |
|
| 413 |
if (size < 28) { |
| 414 |
throw new AndrolibException("Config size < 28"); |
| 415 |
} |
| 416 |
|
| 417 |
boolean isInvalid = false; |
| 418 |
|
| 419 |
short mcc = mIn.readShort(); |
| 420 |
short mnc = mIn.readShort(); |
| 421 |
|
| 422 |
char[] language = this.unpackLanguageOrRegion(mIn.readByte(), mIn.readByte(), 'a'); |
| 423 |
char[] country = this.unpackLanguageOrRegion(mIn.readByte(), mIn.readByte(), '0'); |
| 424 |
|
| 425 |
byte orientation = mIn.readByte(); |
| 426 |
byte touchscreen = mIn.readByte(); |
| 427 |
|
| 428 |
int density = mIn.readUnsignedShort(); |
| 429 |
|
| 430 |
byte keyboard = mIn.readByte(); |
| 431 |
byte navigation = mIn.readByte(); |
| 432 |
byte inputFlags = mIn.readByte(); |
| 433 |
/* inputPad0 */mIn.skipBytes(1); |
| 434 |
|
| 435 |
short screenWidth = mIn.readShort(); |
| 436 |
short screenHeight = mIn.readShort(); |
| 437 |
|
| 438 |
short sdkVersion = mIn.readShort(); |
| 439 |
/* minorVersion, now must always be 0 */mIn.skipBytes(2); |
| 440 |
|
| 441 |
byte screenLayout = 0; |
| 442 |
byte uiMode = 0; |
| 443 |
short smallestScreenWidthDp = 0; |
| 444 |
if (size >= 32) { |
| 445 |
screenLayout = mIn.readByte(); |
| 446 |
uiMode = mIn.readByte(); |
| 447 |
smallestScreenWidthDp = mIn.readShort(); |
| 448 |
read = 32; |
| 449 |
} |
| 450 |
|
| 451 |
short screenWidthDp = 0; |
| 452 |
short screenHeightDp = 0; |
| 453 |
if (size >= 36) { |
| 454 |
screenWidthDp = mIn.readShort(); |
| 455 |
screenHeightDp = mIn.readShort(); |
| 456 |
read = 36; |
| 457 |
} |
| 458 |
|
| 459 |
char[] localeScript = null; |
| 460 |
char[] localeVariant = null; |
| 461 |
if (size >= 48) { |
| 462 |
localeScript = readScriptOrVariantChar(4).toCharArray(); |
| 463 |
localeVariant = readScriptOrVariantChar(8).toCharArray(); |
| 464 |
read = 48; |
| 465 |
} |
| 466 |
|
| 467 |
byte screenLayout2 = 0; |
| 468 |
byte colorMode = 0; |
| 469 |
if (size >= 52) { |
| 470 |
screenLayout2 = mIn.readByte(); |
| 471 |
colorMode = mIn.readByte(); |
| 472 |
mIn.skipBytes(2); // reserved padding |
| 473 |
read = 52; |
| 474 |
} |
| 475 |
|
| 476 |
if (size >= 56) { |
| 477 |
mIn.skipBytes(4); |
| 478 |
read = 56; |
| 479 |
} |
| 480 |
|
| 481 |
int exceedingSize = size - KNOWN_CONFIG_BYTES; |
| 482 |
if (exceedingSize > 0) { |
| 483 |
byte[] buf = new byte[exceedingSize]; |
| 484 |
read += exceedingSize; |
| 485 |
mIn.readFully(buf); |
| 486 |
BigInteger exceedingBI = new BigInteger(1, buf); |
| 487 |
|
| 488 |
if (exceedingBI.equals(BigInteger.ZERO)) { |
| 489 |
LOGGER.fine(String |
| 490 |
.format("Config flags size > %d, but exceeding bytes are all zero, so it should be ok.", |
| 491 |
KNOWN_CONFIG_BYTES)); |
| 492 |
} else { |
| 493 |
LOGGER.warning(String.format("Config flags size > %d. Size = %d. Exceeding bytes: 0x%X.", |
| 494 |
KNOWN_CONFIG_BYTES, size, exceedingBI)); |
| 495 |
isInvalid = true; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
int remainingSize = size - read; |
| 500 |
if (remainingSize > 0) { |
| 501 |
mIn.skipBytes(remainingSize); |
| 502 |
} |
| 503 |
|
| 504 |
return new ResConfigFlags(mcc, mnc, language, country, |
| 505 |
orientation, touchscreen, density, keyboard, navigation, |
| 506 |
inputFlags, screenWidth, screenHeight, sdkVersion, |
| 507 |
screenLayout, uiMode, smallestScreenWidthDp, screenWidthDp, |
| 508 |
screenHeightDp, localeScript, localeVariant, screenLayout2, |
| 509 |
colorMode, isInvalid, size); |
| 510 |
} |
| 511 |
|
| 512 |
private char[] unpackLanguageOrRegion(byte in0, byte in1, char base) { |
| 513 |
// check high bit, if so we have a packed 3 letter code |
| 514 |
if (((in0 >> 7) & 1) == 1) { |
| 515 |
int first = in1 & 0x1F; |
| 516 |
int second = ((in1 & 0xE0) >> 5) + ((in0 & 0x03) << 3); |
| 517 |
int third = (in0 & 0x7C) >> 2; |
| 518 |
|
| 519 |
// since this function handles languages & regions, we add the value(s) to the base char |
| 520 |
// which is usually 'a' or '0' depending on language or region. |
| 521 |
return new char[] { (char) (first + base), (char) (second + base), (char) (third + base) }; |
| 522 |
} |
| 523 |
return new char[] { (char) in0, (char) in1 }; |
| 524 |
} |
| 525 |
|
| 526 |
private String readScriptOrVariantChar(int length) throws IOException { |
| 527 |
StringBuilder string = new StringBuilder(16); |
| 528 |
|
| 529 |
while(length-- != 0) { |
| 530 |
short ch = mIn.readByte(); |
| 531 |
if (ch == 0) { |
| 532 |
break; |
| 533 |
} |
| 534 |
string.append((char) ch); |
| 535 |
} |
| 536 |
mIn.skipBytes(length); |
| 537 |
|
| 538 |
return string.toString(); |
| 539 |
} |
| 540 |
|
| 541 |
private void addTypeSpec(ResTypeSpec resTypeSpec) { |
| 542 |
mResTypeSpecs.put(resTypeSpec.getId(), resTypeSpec); |
| 543 |
} |
| 544 |
|
| 545 |
private void addMissingResSpecs() throws AndrolibException { |
| 546 |
int resId = mResId & 0xffff0000; |
| 547 |
|
| 548 |
for (int i : mMissingResSpecMap.keySet()) { |
| 549 |
if (mMissingResSpecMap.get(i)) continue; |
| 550 |
|
| 551 |
ResResSpec spec = new ResResSpec(new ResID(resId | i), "APKTOOL_DUMMY_" + Integer.toHexString(i), mPkg, mTypeSpec); |
| 552 |
|
| 553 |
// If we already have this resID don't add it again. |
| 554 |
if (! mPkg.hasResSpec(new ResID(resId | i))) { |
| 555 |
mPkg.addResSpec(spec); |
| 556 |
mTypeSpec.addResSpec(spec); |
| 557 |
|
| 558 |
if (mType == null) { |
| 559 |
mType = mPkg.getOrCreateConfig(new ResConfigFlags()); |
| 560 |
} |
| 561 |
|
| 562 |
// We are going to make dummy attributes a null reference (@null) now instead of a boolean false. |
| 563 |
// This is because aapt2 is much more strict when it comes to what we can put in an application. |
| 564 |
ResValue value = new ResReferenceValue(mPkg, 0, ""); |
| 565 |
|
| 566 |
ResResource res = new ResResource(mType, spec, value); |
| 567 |
mType.addResource(res); |
| 568 |
spec.addResource(res); |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
private void removeResSpec(ResResSpec spec) throws AndrolibException { |
| 574 |
if (mPkg.hasResSpec(spec.getId())) { |
| 575 |
mPkg.removeResSpec(spec); |
| 576 |
mTypeSpec.removeResSpec(spec); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
private Header nextChunk() throws IOException { |
| 581 |
return mHeader = Header.read(mIn, mCountIn); |
| 582 |
} |
| 583 |
|
| 584 |
private void checkChunkType(int expectedType) throws AndrolibException { |
| 585 |
if (mHeader.type != expectedType) { |
| 586 |
throw new AndrolibException(String.format("Invalid chunk type: expected=0x%08x, got=0x%08x", |
| 587 |
expectedType, mHeader.type)); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
private void nextChunkCheckType(int expectedType) throws IOException, AndrolibException { |
| 592 |
nextChunk(); |
| 593 |
checkChunkType(expectedType); |
| 594 |
} |
| 595 |
|
| 596 |
private final ExtDataInput mIn; |
| 597 |
private final ResTable mResTable; |
| 598 |
private final CountingInputStream mCountIn; |
| 599 |
private final List<FlagsOffset> mFlagsOffsets; |
| 600 |
private final boolean mKeepBroken; |
| 601 |
|
| 602 |
private Header mHeader; |
| 603 |
private StringBlock mTableStrings; |
| 604 |
private StringBlock mTypeNames; |
| 605 |
private StringBlock mSpecNames; |
| 606 |
private ResPackage mPkg; |
| 607 |
private ResTypeSpec mTypeSpec; |
| 608 |
private ResType mType; |
| 609 |
private int mResId; |
| 610 |
private int mTypeIdOffset = 0; |
| 611 |
private HashMap<Integer, Boolean> mMissingResSpecMap; |
| 612 |
private final HashMap<Integer, ResTypeSpec> mResTypeSpecs = new HashMap<>(); |
| 613 |
|
| 614 |
private final static short ENTRY_FLAG_COMPLEX = 0x0001; |
| 615 |
private final static short ENTRY_FLAG_PUBLIC = 0x0002; |
| 616 |
private final static short ENTRY_FLAG_WEAK = 0x0004; |
| 617 |
|
| 618 |
public static class Header { |
| 619 |
public final short type; |
| 620 |
public final int headerSize; |
| 621 |
public final int chunkSize; |
| 622 |
public final int startPosition; |
| 623 |
public final int endPosition; |
| 624 |
|
| 625 |
public Header(short type, int headerSize, int chunkSize, int headerStart) { |
| 626 |
this.type = type; |
| 627 |
this.headerSize = headerSize; |
| 628 |
this.chunkSize = chunkSize; |
| 629 |
this.startPosition = headerStart; |
| 630 |
this.endPosition = headerStart + chunkSize; |
| 631 |
} |
| 632 |
|
| 633 |
public static Header read(ExtDataInput in, CountingInputStream countIn) throws IOException { |
| 634 |
short type; |
| 635 |
int start = countIn.getCount(); |
| 636 |
try { |
| 637 |
type = in.readShort(); |
| 638 |
} catch (EOFException ex) { |
| 639 |
return new Header(TYPE_NONE, 0, 0, countIn.getCount()); |
| 640 |
} |
| 641 |
return new Header(type, in.readShort(), in.readInt(), start); |
| 642 |
} |
| 643 |
|
| 644 |
public final static short TYPE_NONE = -1; |
| 645 |
public final static short TYPE_STRING_POOL = 0x0001; |
| 646 |
public final static short TYPE_TABLE = 0x0002; |
| 647 |
public final static short TYPE_XML = 0x0003; |
| 648 |
|
| 649 |
public final static short XML_TYPE_PACKAGE = 0x0200; |
| 650 |
public final static short XML_TYPE_TYPE = 0x0201; |
| 651 |
public final static short XML_TYPE_SPEC_TYPE = 0x0202; |
| 652 |
public final static short XML_TYPE_LIBRARY = 0x0203; |
| 653 |
public final static short XML_TYPE_OVERLAY = 0x0204; |
| 654 |
public final static short XML_TYPE_OVERLAY_POLICY = 0x0205; |
| 655 |
public final static short XML_TYPE_STAGED_ALIAS = 0x0206; |
| 656 |
} |
| 657 |
|
| 658 |
public static class FlagsOffset { |
| 659 |
public final int offset; |
| 660 |
public final int count; |
| 661 |
|
| 662 |
public FlagsOffset(int offset, int count) { |
| 663 |
this.offset = offset; |
| 664 |
this.count = count; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
private class EntryData { |
| 669 |
public short mFlags; |
| 670 |
public int mSpecNamesId; |
| 671 |
public ResValue mValue; |
| 672 |
} |
| 673 |
|
| 674 |
private static final Logger LOGGER = Logger.getLogger(ARSCDecoder.class.getName()); |
| 675 |
private static final int KNOWN_CONFIG_BYTES = 56; |
| 676 |
|
| 677 |
public static class ARSCData { |
| 678 |
|
| 679 |
public ARSCData(ResPackage[] packages, FlagsOffset[] flagsOffsets, ResTable resTable) { |
| 680 |
mPackages = packages; |
| 681 |
mFlagsOffsets = flagsOffsets; |
| 682 |
mResTable = resTable; |
| 683 |
} |
| 684 |
|
| 685 |
public FlagsOffset[] getFlagsOffsets() { |
| 686 |
return mFlagsOffsets; |
| 687 |
} |
| 688 |
|
| 689 |
public ResPackage[] getPackages() { |
| 690 |
return mPackages; |
| 691 |
} |
| 692 |
|
| 693 |
public ResPackage getOnePackage() throws AndrolibException { |
| 694 |
if (mPackages.length <= 0) { |
| 695 |
throw new AndrolibException("Arsc file contains zero packages"); |
| 696 |
} else if (mPackages.length != 1) { |
| 697 |
int id = findPackageWithMostResSpecs(); |
| 698 |
LOGGER.info("Arsc file contains multiple packages. Using package " |
| 699 |
+ mPackages[id].getName() + " as default."); |
| 700 |
|
| 701 |
return mPackages[id]; |
| 702 |
} |
| 703 |
return mPackages[0]; |
| 704 |
} |
| 705 |
|
| 706 |
public int findPackageWithMostResSpecs() { |
| 707 |
int count = mPackages[0].getResSpecCount(); |
| 708 |
int id = 0; |
| 709 |
|
| 710 |
for (int i = 0; i < mPackages.length; i++) { |
| 711 |
if (mPackages[i].getResSpecCount() >= count) { |
| 712 |
count = mPackages[i].getResSpecCount(); |
| 713 |
id = i; |
| 714 |
} |
| 715 |
} |
| 716 |
return id; |
| 717 |
} |
| 718 |
|
| 719 |
public ResTable getResTable() { |
| 720 |
return mResTable; |
| 721 |
} |
| 722 |
|
| 723 |
private final ResPackage[] mPackages; |
| 724 |
private final FlagsOffset[] mFlagsOffsets; |
| 725 |
private final ResTable mResTable; |
| 726 |
} |
| 727 |
} |