| 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.content.res.XmlResourceParser; |
| 20 |
import android.util.TypedValue; |
| 21 |
import brut.androlib.AndrolibException; |
| 22 |
import brut.androlib.res.data.ResID; |
| 23 |
import brut.androlib.res.xml.ResXmlEncoders; |
| 24 |
import brut.util.ExtDataInput; |
| 25 |
import com.google.common.io.LittleEndianDataInputStream; |
| 26 |
import org.xmlpull.v1.XmlPullParserException; |
| 27 |
import java.io.DataInput; |
| 28 |
import java.io.IOException; |
| 29 |
import java.io.InputStream; |
| 30 |
import java.io.Reader; |
| 31 |
import java.util.logging.Level; |
| 32 |
import java.util.logging.Logger; |
| 33 |
|
| 34 |
/** |
| 35 |
* Binary xml files parser. |
| 36 |
* |
| 37 |
* Parser has only two states: (1) Operational state, which parser |
| 38 |
* obtains after first successful call to next() and retains until |
| 39 |
* open(), close(), or failed call to next(). (2) Closed state, which |
| 40 |
* parser obtains after open(), close(), or failed call to next(). In |
| 41 |
* this state methods return invalid values or throw exceptions. |
| 42 |
* |
| 43 |
* TODO: * check all methods in closed state |
| 44 |
*/ |
| 45 |
public class AXmlResourceParser implements XmlResourceParser { |
| 46 |
|
| 47 |
public AXmlResourceParser() { |
| 48 |
resetEventInfo(); |
| 49 |
} |
| 50 |
|
| 51 |
public AXmlResourceParser(InputStream stream) { |
| 52 |
this(); |
| 53 |
open(stream); |
| 54 |
} |
| 55 |
|
| 56 |
public AndrolibException getFirstError() { |
| 57 |
return mFirstError; |
| 58 |
} |
| 59 |
|
| 60 |
public ResAttrDecoder getAttrDecoder() { |
| 61 |
return mAttrDecoder; |
| 62 |
} |
| 63 |
|
| 64 |
public void setAttrDecoder(ResAttrDecoder attrDecoder) { |
| 65 |
mAttrDecoder = attrDecoder; |
| 66 |
} |
| 67 |
|
| 68 |
public void open(InputStream stream) { |
| 69 |
close(); |
| 70 |
if (stream != null) { |
| 71 |
// We need to explicitly cast to DataInput as otherwise the constructor is ambiguous. |
| 72 |
// We choose DataInput instead of InputStream as ExtDataInput wraps an InputStream in |
| 73 |
// a DataInputStream which is big-endian and ignores the little-endian behavior. |
| 74 |
m_reader = new ExtDataInput((DataInput) new LittleEndianDataInputStream(stream)); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void close() { |
| 80 |
if (!m_operational) { |
| 81 |
return; |
| 82 |
} |
| 83 |
m_operational = false; |
| 84 |
m_reader = null; |
| 85 |
m_strings = null; |
| 86 |
m_resourceIDs = null; |
| 87 |
m_namespaces.reset(); |
| 88 |
resetEventInfo(); |
| 89 |
} |
| 90 |
|
| 91 |
// ///////////////////////////////// iteration |
| 92 |
@Override |
| 93 |
public int next() throws XmlPullParserException, IOException { |
| 94 |
if (m_reader == null) { |
| 95 |
throw new XmlPullParserException("Parser is not opened.", this, null); |
| 96 |
} |
| 97 |
try { |
| 98 |
doNext(); |
| 99 |
return m_event; |
| 100 |
} catch (IOException e) { |
| 101 |
close(); |
| 102 |
throw e; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public int nextToken() throws XmlPullParserException, IOException { |
| 108 |
return next(); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public int nextTag() throws XmlPullParserException, IOException { |
| 113 |
int eventType = next(); |
| 114 |
if (eventType == TEXT && isWhitespace()) { |
| 115 |
eventType = next(); |
| 116 |
} |
| 117 |
if (eventType != START_TAG && eventType != END_TAG) { |
| 118 |
throw new XmlPullParserException("Expected start or end tag.", this, null); |
| 119 |
} |
| 120 |
return eventType; |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public String nextText() throws XmlPullParserException, IOException { |
| 125 |
if (getEventType() != START_TAG) { |
| 126 |
throw new XmlPullParserException("Parser must be on START_TAG to read next text.", this, null); |
| 127 |
} |
| 128 |
int eventType = next(); |
| 129 |
if (eventType == TEXT) { |
| 130 |
String result = getText(); |
| 131 |
eventType = next(); |
| 132 |
if (eventType != END_TAG) { |
| 133 |
throw new XmlPullParserException("Event TEXT must be immediately followed by END_TAG.", this, null); |
| 134 |
} |
| 135 |
return result; |
| 136 |
} else if (eventType == END_TAG) { |
| 137 |
return ""; |
| 138 |
} else { |
| 139 |
throw new XmlPullParserException("Parser must be on START_TAG or TEXT to read text.", this, null); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public void require(int type, String namespace, String name) |
| 145 |
throws XmlPullParserException, IOException { |
| 146 |
if (type != getEventType() || (namespace != null && !namespace.equals(getNamespace())) |
| 147 |
|| (name != null && !name.equals(getName()))) { |
| 148 |
throw new XmlPullParserException(TYPES[type] + " is expected.", this, null); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public int getDepth() { |
| 154 |
return m_namespaces.getDepth() - 1; |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public int getEventType() throws XmlPullParserException { |
| 159 |
return m_event; |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public int getLineNumber() { |
| 164 |
return m_lineNumber; |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public String getName() { |
| 169 |
if (m_name == -1 || (m_event != START_TAG && m_event != END_TAG)) { |
| 170 |
return null; |
| 171 |
} |
| 172 |
return m_strings.getString(m_name); |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
public String getText() { |
| 177 |
if (m_name == -1 || m_event != TEXT) { |
| 178 |
return null; |
| 179 |
} |
| 180 |
return m_strings.getString(m_name); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public char[] getTextCharacters(int[] holderForStartAndLength) { |
| 185 |
String text = getText(); |
| 186 |
if (text == null) { |
| 187 |
return null; |
| 188 |
} |
| 189 |
holderForStartAndLength[0] = 0; |
| 190 |
holderForStartAndLength[1] = text.length(); |
| 191 |
char[] chars = new char[text.length()]; |
| 192 |
text.getChars(0, text.length(), chars, 0); |
| 193 |
return chars; |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public String getNamespace() { |
| 198 |
return m_strings.getString(m_namespaceUri); |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public String getPrefix() { |
| 203 |
int prefix = m_namespaces.findPrefix(m_namespaceUri); |
| 204 |
return m_strings.getString(prefix); |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public String getPositionDescription() { |
| 209 |
return "XML line #" + getLineNumber(); |
| 210 |
} |
| 211 |
|
| 212 |
@Override |
| 213 |
public int getNamespaceCount(int depth) { |
| 214 |
return m_namespaces.getAccumulatedCount(depth); |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public String getNamespacePrefix(int pos) { |
| 219 |
int prefix = m_namespaces.getPrefix(pos); |
| 220 |
return m_strings.getString(prefix); |
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
public String getNamespaceUri(int pos) { |
| 225 |
int uri = m_namespaces.getUri(pos); |
| 226 |
return m_strings.getString(uri); |
| 227 |
} |
| 228 |
|
| 229 |
// ///////////////////////////////// attributes |
| 230 |
@Override |
| 231 |
public String getClassAttribute() { |
| 232 |
if (m_classAttribute == -1) { |
| 233 |
return null; |
| 234 |
} |
| 235 |
int offset = getAttributeOffset(m_classAttribute); |
| 236 |
int value = m_attributes[offset + ATTRIBUTE_IX_VALUE_STRING]; |
| 237 |
return m_strings.getString(value); |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 241 |
public String getIdAttribute() { |
| 242 |
if (m_idAttribute == -1) { |
| 243 |
return null; |
| 244 |
} |
| 245 |
int offset = getAttributeOffset(m_idAttribute); |
| 246 |
int value = m_attributes[offset + ATTRIBUTE_IX_VALUE_STRING]; |
| 247 |
return m_strings.getString(value); |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public int getIdAttributeResourceValue(int defaultValue) { |
| 252 |
if (m_idAttribute == -1) { |
| 253 |
return defaultValue; |
| 254 |
} |
| 255 |
int offset = getAttributeOffset(m_idAttribute); |
| 256 |
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE]; |
| 257 |
if (valueType != TypedValue.TYPE_REFERENCE) { |
| 258 |
return defaultValue; |
| 259 |
} |
| 260 |
return m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA]; |
| 261 |
} |
| 262 |
|
| 263 |
@Override |
| 264 |
public int getStyleAttribute() { |
| 265 |
if (m_styleAttribute == -1) { |
| 266 |
return 0; |
| 267 |
} |
| 268 |
int offset = getAttributeOffset(m_styleAttribute); |
| 269 |
return m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA]; |
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public int getAttributeCount() { |
| 274 |
if (m_event != START_TAG) { |
| 275 |
return -1; |
| 276 |
} |
| 277 |
return m_attributes.length / ATTRIBUTE_LENGTH; |
| 278 |
} |
| 279 |
|
| 280 |
@Override |
| 281 |
public String getAttributeNamespace(int index) { |
| 282 |
int offset = getAttributeOffset(index); |
| 283 |
int namespace = m_attributes[offset + ATTRIBUTE_IX_NAMESPACE_URI]; |
| 284 |
if (namespace == -1) { |
| 285 |
return ""; |
| 286 |
} |
| 287 |
|
| 288 |
// Minifiers like removing the namespace, so we will default to default namespace |
| 289 |
// unless the pkgId of the resource is private. We will grab the non-standard one. |
| 290 |
String value = m_strings.getString(namespace); |
| 291 |
|
| 292 |
if (value.length() == 0) { |
| 293 |
ResID resourceId = new ResID(getAttributeNameResource(index)); |
| 294 |
if (resourceId.package_ == PRIVATE_PKG_ID) { |
| 295 |
value = getNonDefaultNamespaceUri(offset); |
| 296 |
} else { |
| 297 |
value = android_ns; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
return value; |
| 302 |
} |
| 303 |
|
| 304 |
private String getNonDefaultNamespaceUri(int offset) { |
| 305 |
String prefix = m_strings.getString(m_namespaces.getPrefix(offset)); |
| 306 |
if (prefix != null) { |
| 307 |
return m_strings.getString(m_namespaces.getUri(offset)); |
| 308 |
} |
| 309 |
|
| 310 |
// If we are here. There is some clever obfuscation going on. Our reference points to the namespace are gone. |
| 311 |
// Normally we could take the index * attributeCount to get an offset. |
| 312 |
// That would point to the URI in the StringBlock table, but that is empty. |
| 313 |
// We have the namespaces that can't be touched in the opening tag. |
| 314 |
// Though no known way to correlate them at this time. |
| 315 |
// So return the res-auto namespace. |
| 316 |
return "http://schemas.android.com/apk/res-auto"; |
| 317 |
} |
| 318 |
|
| 319 |
@Override |
| 320 |
public String getAttributePrefix(int index) { |
| 321 |
int offset = getAttributeOffset(index); |
| 322 |
int uri = m_attributes[offset + ATTRIBUTE_IX_NAMESPACE_URI]; |
| 323 |
int prefix = m_namespaces.findPrefix(uri); |
| 324 |
if (prefix == -1) { |
| 325 |
return ""; |
| 326 |
} |
| 327 |
return m_strings.getString(prefix); |
| 328 |
} |
| 329 |
|
| 330 |
@Override |
| 331 |
public String getAttributeName(int index) { |
| 332 |
int offset = getAttributeOffset(index); |
| 333 |
int name = m_attributes[offset + ATTRIBUTE_IX_NAME]; |
| 334 |
if (name == -1) { |
| 335 |
return ""; |
| 336 |
} |
| 337 |
|
| 338 |
String value = m_strings.getString(name); |
| 339 |
String namespace = getAttributeNamespace(index); |
| 340 |
|
| 341 |
// If attribute name is lacking or a private namespace emerges, |
| 342 |
// retrieve the exact attribute name by its id. |
| 343 |
if (value == null || value.length() == 0) { |
| 344 |
try { |
| 345 |
value = mAttrDecoder.decodeManifestAttr(getAttributeNameResource(index)); |
| 346 |
if (value == null) { |
| 347 |
value = ""; |
| 348 |
} |
| 349 |
} catch (AndrolibException e) { |
| 350 |
value = ""; |
| 351 |
} |
| 352 |
} else if (! namespace.equals(android_ns)) { |
| 353 |
try { |
| 354 |
String obfuscatedName = mAttrDecoder.decodeManifestAttr(getAttributeNameResource(index)); |
| 355 |
if (! (obfuscatedName == null || obfuscatedName.equals(value))) { |
| 356 |
value = obfuscatedName; |
| 357 |
} |
| 358 |
} catch (AndrolibException ignored) {} |
| 359 |
} |
| 360 |
return value; |
| 361 |
} |
| 362 |
|
| 363 |
@Override |
| 364 |
public int getAttributeNameResource(int index) { |
| 365 |
int offset = getAttributeOffset(index); |
| 366 |
int name = m_attributes[offset + ATTRIBUTE_IX_NAME]; |
| 367 |
if (m_resourceIDs == null || name < 0 || name >= m_resourceIDs.length) { |
| 368 |
return 0; |
| 369 |
} |
| 370 |
return m_resourceIDs[name]; |
| 371 |
} |
| 372 |
|
| 373 |
@Override |
| 374 |
public int getAttributeValueType(int index) { |
| 375 |
int offset = getAttributeOffset(index); |
| 376 |
return m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE]; |
| 377 |
} |
| 378 |
|
| 379 |
@Override |
| 380 |
public int getAttributeValueData(int index) { |
| 381 |
int offset = getAttributeOffset(index); |
| 382 |
return m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA]; |
| 383 |
} |
| 384 |
|
| 385 |
@Override |
| 386 |
public String getAttributeValue(int index) { |
| 387 |
int offset = getAttributeOffset(index); |
| 388 |
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE]; |
| 389 |
int valueData = m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA]; |
| 390 |
int valueRaw = m_attributes[offset + ATTRIBUTE_IX_VALUE_STRING]; |
| 391 |
|
| 392 |
if (mAttrDecoder != null) { |
| 393 |
try { |
| 394 |
String value = valueRaw == -1 ? null : ResXmlEncoders.escapeXmlChars(m_strings.getString(valueRaw)); |
| 395 |
String obfuscatedValue = mAttrDecoder.decodeManifestAttr(valueData); |
| 396 |
|
| 397 |
if (! (value == null || obfuscatedValue == null)) { |
| 398 |
int slashPos = value.lastIndexOf("/"); |
| 399 |
|
| 400 |
if (slashPos != -1) { |
| 401 |
// Handle a value with a format of "@yyy/xxx" |
| 402 |
String dir = value.substring(0, slashPos); |
| 403 |
value = dir + "/"+ obfuscatedValue; |
| 404 |
} else if (! value.equals(obfuscatedValue)) { |
| 405 |
value = obfuscatedValue; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
return mAttrDecoder.decode( |
| 410 |
valueType, |
| 411 |
valueData, |
| 412 |
value, |
| 413 |
getAttributeNameResource(index) |
| 414 |
); |
| 415 |
} catch (AndrolibException ex) { |
| 416 |
setFirstError(ex); |
| 417 |
LOGGER.log(Level.WARNING, String.format("Could not decode attr value, using undecoded value " |
| 418 |
+ "instead: ns=%s, name=%s, value=0x%08x", |
| 419 |
getAttributePrefix(index), |
| 420 |
getAttributeName(index), |
| 421 |
valueData), ex); |
| 422 |
} |
| 423 |
} |
| 424 |
return TypedValue.coerceToString(valueType, valueData); |
| 425 |
} |
| 426 |
|
| 427 |
@Override |
| 428 |
public boolean getAttributeBooleanValue(int index, boolean defaultValue) { |
| 429 |
return getAttributeIntValue(index, defaultValue ? 1 : 0) != 0; |
| 430 |
} |
| 431 |
|
| 432 |
@Override |
| 433 |
public float getAttributeFloatValue(int index, float defaultValue) { |
| 434 |
int offset = getAttributeOffset(index); |
| 435 |
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE]; |
| 436 |
if (valueType == TypedValue.TYPE_FLOAT) { |
| 437 |
int valueData = m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA]; |
| 438 |
return Float.intBitsToFloat(valueData); |
| 439 |
} |
| 440 |
return defaultValue; |
| 441 |
} |
| 442 |
|
| 443 |
@Override |
| 444 |
public int getAttributeIntValue(int index, int defaultValue) { |
| 445 |
int offset = getAttributeOffset(index); |
| 446 |
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE]; |
| 447 |
if (valueType >= TypedValue.TYPE_FIRST_INT && valueType <= TypedValue.TYPE_LAST_INT) { |
| 448 |
return m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA]; |
| 449 |
} |
| 450 |
return defaultValue; |
| 451 |
} |
| 452 |
|
| 453 |
@Override |
| 454 |
public int getAttributeUnsignedIntValue(int index, int defaultValue) { |
| 455 |
return getAttributeIntValue(index, defaultValue); |
| 456 |
} |
| 457 |
|
| 458 |
@Override |
| 459 |
public int getAttributeResourceValue(int index, int defaultValue) { |
| 460 |
int offset = getAttributeOffset(index); |
| 461 |
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE]; |
| 462 |
if (valueType == TypedValue.TYPE_REFERENCE) { |
| 463 |
return m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA]; |
| 464 |
} |
| 465 |
return defaultValue; |
| 466 |
} |
| 467 |
|
| 468 |
@Override |
| 469 |
public String getAttributeValue(String namespace, String attribute) { |
| 470 |
int index = findAttribute(namespace, attribute); |
| 471 |
if (index == -1) { |
| 472 |
return ""; |
| 473 |
} |
| 474 |
return getAttributeValue(index); |
| 475 |
} |
| 476 |
|
| 477 |
@Override |
| 478 |
public boolean getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue) { |
| 479 |
int index = findAttribute(namespace, attribute); |
| 480 |
if (index == -1) { |
| 481 |
return defaultValue; |
| 482 |
} |
| 483 |
return getAttributeBooleanValue(index, defaultValue); |
| 484 |
} |
| 485 |
|
| 486 |
@Override |
| 487 |
public float getAttributeFloatValue(String namespace, String attribute, float defaultValue) { |
| 488 |
int index = findAttribute(namespace, attribute); |
| 489 |
if (index == -1) { |
| 490 |
return defaultValue; |
| 491 |
} |
| 492 |
return getAttributeFloatValue(index, defaultValue); |
| 493 |
} |
| 494 |
|
| 495 |
@Override |
| 496 |
public int getAttributeIntValue(String namespace, String attribute, int defaultValue) { |
| 497 |
int index = findAttribute(namespace, attribute); |
| 498 |
if (index == -1) { |
| 499 |
return defaultValue; |
| 500 |
} |
| 501 |
return getAttributeIntValue(index, defaultValue); |
| 502 |
} |
| 503 |
|
| 504 |
@Override |
| 505 |
public int getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue) { |
| 506 |
int index = findAttribute(namespace, attribute); |
| 507 |
if (index == -1) { |
| 508 |
return defaultValue; |
| 509 |
} |
| 510 |
return getAttributeUnsignedIntValue(index, defaultValue); |
| 511 |
} |
| 512 |
|
| 513 |
@Override |
| 514 |
public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) { |
| 515 |
int index = findAttribute(namespace, attribute); |
| 516 |
if (index == -1) { |
| 517 |
return defaultValue; |
| 518 |
} |
| 519 |
return getAttributeResourceValue(index, defaultValue); |
| 520 |
} |
| 521 |
|
| 522 |
@Override |
| 523 |
public int getAttributeListValue(int index, String[] options, int defaultValue) { |
| 524 |
// TODO implement |
| 525 |
return 0; |
| 526 |
} |
| 527 |
|
| 528 |
@Override |
| 529 |
public int getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue) { |
| 530 |
// TODO implement |
| 531 |
return 0; |
| 532 |
} |
| 533 |
|
| 534 |
@Override |
| 535 |
public String getAttributeType(int index) { |
| 536 |
return "CDATA"; |
| 537 |
} |
| 538 |
|
| 539 |
@Override |
| 540 |
public boolean isAttributeDefault(int index) { |
| 541 |
return false; |
| 542 |
} |
| 543 |
|
| 544 |
// ///////////////////////////////// dummies |
| 545 |
@Override |
| 546 |
public void setInput(InputStream stream, String inputEncoding) { |
| 547 |
open(stream); |
| 548 |
} |
| 549 |
|
| 550 |
@Override |
| 551 |
public void setInput(Reader reader) throws XmlPullParserException { |
| 552 |
throw new XmlPullParserException(E_NOT_SUPPORTED); |
| 553 |
} |
| 554 |
|
| 555 |
@Override |
| 556 |
public String getInputEncoding() { |
| 557 |
return null; |
| 558 |
} |
| 559 |
|
| 560 |
@Override |
| 561 |
public int getColumnNumber() { |
| 562 |
return -1; |
| 563 |
} |
| 564 |
|
| 565 |
@Override |
| 566 |
public boolean isEmptyElementTag() { |
| 567 |
return false; |
| 568 |
} |
| 569 |
|
| 570 |
@Override |
| 571 |
public boolean isWhitespace() { |
| 572 |
return false; |
| 573 |
} |
| 574 |
|
| 575 |
@Override |
| 576 |
public void defineEntityReplacementText(String entityName, String replacementText) |
| 577 |
throws XmlPullParserException { |
| 578 |
throw new XmlPullParserException(E_NOT_SUPPORTED); |
| 579 |
} |
| 580 |
|
| 581 |
@Override |
| 582 |
public String getNamespace(String prefix) { |
| 583 |
throw new RuntimeException(E_NOT_SUPPORTED); |
| 584 |
} |
| 585 |
|
| 586 |
@Override |
| 587 |
public Object getProperty(String name) { |
| 588 |
return null; |
| 589 |
} |
| 590 |
|
| 591 |
@Override |
| 592 |
public void setProperty(String name, Object value) |
| 593 |
throws XmlPullParserException { |
| 594 |
throw new XmlPullParserException(E_NOT_SUPPORTED); |
| 595 |
} |
| 596 |
|
| 597 |
@Override |
| 598 |
public boolean getFeature(String feature) { |
| 599 |
return false; |
| 600 |
} |
| 601 |
|
| 602 |
@Override |
| 603 |
public void setFeature(String name, boolean value) |
| 604 |
throws XmlPullParserException { |
| 605 |
throw new XmlPullParserException(E_NOT_SUPPORTED); |
| 606 |
} |
| 607 |
|
| 608 |
// /////////////////////////////////////////// implementation |
| 609 |
/** |
| 610 |
* Namespace stack, holds prefix+uri pairs, as well as depth information. |
| 611 |
* All information is stored in one int[] array. Array consists of depth |
| 612 |
* frames: Data=DepthFrame*; DepthFrame=Count+[Prefix+Uri]*+Count; |
| 613 |
* Count='count of Prefix+Uri pairs'; Yes, count is stored twice, to enable |
| 614 |
* bottom-up traversal. increaseDepth adds depth frame, decreaseDepth |
| 615 |
* removes it. push/pop operations operate only in current depth frame. |
| 616 |
* decreaseDepth removes any remaining (not pop'ed) namespace pairs. findXXX |
| 617 |
* methods search all depth frames starting from the last namespace pair of |
| 618 |
* current depth frame. All functions that operate with int, use -1 as |
| 619 |
* 'invalid value'. |
| 620 |
* |
| 621 |
* !! functions expect 'prefix'+'uri' pairs, not 'uri'+'prefix' !! |
| 622 |
* |
| 623 |
*/ |
| 624 |
private static final class NamespaceStack { |
| 625 |
|
| 626 |
public NamespaceStack() { |
| 627 |
m_data = new int[32]; |
| 628 |
} |
| 629 |
|
| 630 |
public void reset() { |
| 631 |
m_dataLength = 0; |
| 632 |
m_depth = 0; |
| 633 |
} |
| 634 |
|
| 635 |
public int getCurrentCount() { |
| 636 |
if (m_dataLength == 0) { |
| 637 |
return 0; |
| 638 |
} |
| 639 |
int offset = m_dataLength - 1; |
| 640 |
return m_data[offset]; |
| 641 |
} |
| 642 |
|
| 643 |
public int getAccumulatedCount(int depth) { |
| 644 |
if (m_dataLength == 0 || depth < 0) { |
| 645 |
return 0; |
| 646 |
} |
| 647 |
if (depth > m_depth) { |
| 648 |
depth = m_depth; |
| 649 |
} |
| 650 |
int accumulatedCount = 0; |
| 651 |
int offset = 0; |
| 652 |
for (; depth != 0; --depth) { |
| 653 |
int count = m_data[offset]; |
| 654 |
accumulatedCount += count; |
| 655 |
offset += (2 + count * 2); |
| 656 |
} |
| 657 |
return accumulatedCount; |
| 658 |
} |
| 659 |
|
| 660 |
public void push(int prefix, int uri) { |
| 661 |
if (m_depth == 0) { |
| 662 |
increaseDepth(); |
| 663 |
} |
| 664 |
ensureDataCapacity(2); |
| 665 |
int offset = m_dataLength - 1; |
| 666 |
int count = m_data[offset]; |
| 667 |
m_data[offset - 1 - count * 2] = count + 1; |
| 668 |
m_data[offset] = prefix; |
| 669 |
m_data[offset + 1] = uri; |
| 670 |
m_data[offset + 2] = count + 1; |
| 671 |
m_dataLength += 2; |
| 672 |
} |
| 673 |
|
| 674 |
public boolean pop() { |
| 675 |
if (m_dataLength == 0) { |
| 676 |
return false; |
| 677 |
} |
| 678 |
int offset = m_dataLength - 1; |
| 679 |
int count = m_data[offset]; |
| 680 |
if (count == 0) { |
| 681 |
return false; |
| 682 |
} |
| 683 |
count -= 1; |
| 684 |
offset -= 2; |
| 685 |
m_data[offset] = count; |
| 686 |
offset -= (1 + count * 2); |
| 687 |
m_data[offset] = count; |
| 688 |
m_dataLength -= 2; |
| 689 |
return true; |
| 690 |
} |
| 691 |
|
| 692 |
public int getPrefix(int index) { |
| 693 |
return get(index, true); |
| 694 |
} |
| 695 |
|
| 696 |
public int getUri(int index) { |
| 697 |
return get(index, false); |
| 698 |
} |
| 699 |
|
| 700 |
public int findPrefix(int uri) { |
| 701 |
return find(uri, false); |
| 702 |
} |
| 703 |
|
| 704 |
public int getDepth() { |
| 705 |
return m_depth; |
| 706 |
} |
| 707 |
|
| 708 |
public void increaseDepth() { |
| 709 |
ensureDataCapacity(2); |
| 710 |
int offset = m_dataLength; |
| 711 |
m_data[offset] = 0; |
| 712 |
m_data[offset + 1] = 0; |
| 713 |
m_dataLength += 2; |
| 714 |
m_depth += 1; |
| 715 |
} |
| 716 |
|
| 717 |
public void decreaseDepth() { |
| 718 |
if (m_dataLength == 0) { |
| 719 |
return; |
| 720 |
} |
| 721 |
int offset = m_dataLength - 1; |
| 722 |
int count = m_data[offset]; |
| 723 |
if ((offset - 1 - count * 2) == 0) { |
| 724 |
return; |
| 725 |
} |
| 726 |
m_dataLength -= 2 + count * 2; |
| 727 |
m_depth -= 1; |
| 728 |
} |
| 729 |
|
| 730 |
private void ensureDataCapacity(int capacity) { |
| 731 |
int available = (m_data.length - m_dataLength); |
| 732 |
if (available > capacity) { |
| 733 |
return; |
| 734 |
} |
| 735 |
int newLength = (m_data.length + available) * 2; |
| 736 |
int[] newData = new int[newLength]; |
| 737 |
System.arraycopy(m_data, 0, newData, 0, m_dataLength); |
| 738 |
m_data = newData; |
| 739 |
} |
| 740 |
|
| 741 |
private int find(int prefixOrUri, boolean prefix) { |
| 742 |
if (m_dataLength == 0) { |
| 743 |
return -1; |
| 744 |
} |
| 745 |
int offset = m_dataLength - 1; |
| 746 |
for (int i = m_depth; i != 0; --i) { |
| 747 |
int count = m_data[offset]; |
| 748 |
offset -= 2; |
| 749 |
for (; count != 0; --count) { |
| 750 |
if (prefix) { |
| 751 |
if (m_data[offset] == prefixOrUri) { |
| 752 |
return m_data[offset + 1]; |
| 753 |
} |
| 754 |
} else { |
| 755 |
if (m_data[offset + 1] == prefixOrUri) { |
| 756 |
return m_data[offset]; |
| 757 |
} |
| 758 |
} |
| 759 |
offset -= 2; |
| 760 |
} |
| 761 |
} |
| 762 |
return -1; |
| 763 |
} |
| 764 |
|
| 765 |
private int get(int index, boolean prefix) { |
| 766 |
if (m_dataLength == 0 || index < 0) { |
| 767 |
return -1; |
| 768 |
} |
| 769 |
int offset = 0; |
| 770 |
for (int i = m_depth; i != 0; --i) { |
| 771 |
int count = m_data[offset]; |
| 772 |
if (index >= count) { |
| 773 |
index -= count; |
| 774 |
offset += (2 + count * 2); |
| 775 |
continue; |
| 776 |
} |
| 777 |
offset += (1 + index * 2); |
| 778 |
if (!prefix) { |
| 779 |
offset += 1; |
| 780 |
} |
| 781 |
return m_data[offset]; |
| 782 |
} |
| 783 |
return -1; |
| 784 |
} |
| 785 |
|
| 786 |
private int[] m_data; |
| 787 |
private int m_dataLength; |
| 788 |
private int m_depth; |
| 789 |
} |
| 790 |
|
| 791 |
private int getAttributeOffset(int index) { |
| 792 |
if (m_event != START_TAG) { |
| 793 |
throw new IndexOutOfBoundsException("Current event is not START_TAG."); |
| 794 |
} |
| 795 |
int offset = index * ATTRIBUTE_LENGTH; |
| 796 |
if (offset >= m_attributes.length) { |
| 797 |
throw new IndexOutOfBoundsException("Invalid attribute index (" + index + ")."); |
| 798 |
} |
| 799 |
return offset; |
| 800 |
} |
| 801 |
|
| 802 |
private int findAttribute(String namespace, String attribute) { |
| 803 |
if (m_strings == null || attribute == null) { |
| 804 |
return -1; |
| 805 |
} |
| 806 |
int name = m_strings.find(attribute); |
| 807 |
if (name == -1) { |
| 808 |
return -1; |
| 809 |
} |
| 810 |
int uri = (namespace != null) ? m_strings.find(namespace) : -1; |
| 811 |
for (int o = 0; o != m_attributes.length; o += ATTRIBUTE_LENGTH) { |
| 812 |
if (name == m_attributes[o + ATTRIBUTE_IX_NAME] |
| 813 |
&& (uri == -1 || uri == m_attributes[o + ATTRIBUTE_IX_NAMESPACE_URI])) { |
| 814 |
return o / ATTRIBUTE_LENGTH; |
| 815 |
} |
| 816 |
} |
| 817 |
return -1; |
| 818 |
} |
| 819 |
|
| 820 |
private void resetEventInfo() { |
| 821 |
m_event = -1; |
| 822 |
m_lineNumber = -1; |
| 823 |
m_name = -1; |
| 824 |
m_namespaceUri = -1; |
| 825 |
m_attributes = null; |
| 826 |
m_idAttribute = -1; |
| 827 |
m_classAttribute = -1; |
| 828 |
m_styleAttribute = -1; |
| 829 |
} |
| 830 |
|
| 831 |
private void doNext() throws IOException { |
| 832 |
// Delayed initialization. |
| 833 |
if (m_strings == null) { |
| 834 |
m_reader.skipCheckInt(CHUNK_AXML_FILE, CHUNK_AXML_FILE_BROKEN); |
| 835 |
|
| 836 |
/* |
| 837 |
* chunkSize |
| 838 |
*/ |
| 839 |
m_reader.skipInt(); |
| 840 |
m_strings = StringBlock.read(m_reader); |
| 841 |
m_namespaces.increaseDepth(); |
| 842 |
m_operational = true; |
| 843 |
} |
| 844 |
|
| 845 |
if (m_event == END_DOCUMENT) { |
| 846 |
return; |
| 847 |
} |
| 848 |
|
| 849 |
int event = m_event; |
| 850 |
resetEventInfo(); |
| 851 |
|
| 852 |
while (true) { |
| 853 |
if (m_decreaseDepth) { |
| 854 |
m_decreaseDepth = false; |
| 855 |
m_namespaces.decreaseDepth(); |
| 856 |
} |
| 857 |
|
| 858 |
// Fake END_DOCUMENT event. |
| 859 |
if (event == END_TAG && m_namespaces.getDepth() == 1 && m_namespaces.getCurrentCount() == 0) { |
| 860 |
m_event = END_DOCUMENT; |
| 861 |
break; |
| 862 |
} |
| 863 |
|
| 864 |
int chunkType; |
| 865 |
if (event == START_DOCUMENT) { |
| 866 |
// Fake event, see CHUNK_XML_START_TAG handler. |
| 867 |
chunkType = CHUNK_XML_START_TAG; |
| 868 |
} else { |
| 869 |
chunkType = m_reader.readInt(); |
| 870 |
} |
| 871 |
|
| 872 |
if (chunkType == CHUNK_RESOURCEIDS) { |
| 873 |
int chunkSize = m_reader.readInt(); |
| 874 |
if (chunkSize < 8 || (chunkSize % 4) != 0) { |
| 875 |
throw new IOException("Invalid resource ids size (" + chunkSize + ")."); |
| 876 |
} |
| 877 |
m_resourceIDs = m_reader.readIntArray(chunkSize / 4 - 2); |
| 878 |
continue; |
| 879 |
} |
| 880 |
|
| 881 |
if (chunkType < CHUNK_XML_FIRST || chunkType > CHUNK_XML_LAST) { |
| 882 |
throw new IOException("Invalid chunk type (" + chunkType + ")."); |
| 883 |
} |
| 884 |
|
| 885 |
// Fake START_DOCUMENT event. |
| 886 |
if (chunkType == CHUNK_XML_START_TAG && event == -1) { |
| 887 |
m_event = START_DOCUMENT; |
| 888 |
break; |
| 889 |
} |
| 890 |
|
| 891 |
// Common header. |
| 892 |
/* chunkSize */m_reader.skipInt(); |
| 893 |
int lineNumber = m_reader.readInt(); |
| 894 |
/* 0xFFFFFFFF */m_reader.skipInt(); |
| 895 |
|
| 896 |
if (chunkType == CHUNK_XML_START_NAMESPACE || chunkType == CHUNK_XML_END_NAMESPACE) { |
| 897 |
if (chunkType == CHUNK_XML_START_NAMESPACE) { |
| 898 |
int prefix = m_reader.readInt(); |
| 899 |
int uri = m_reader.readInt(); |
| 900 |
m_namespaces.push(prefix, uri); |
| 901 |
} else { |
| 902 |
/* prefix */m_reader.skipInt(); |
| 903 |
/* uri */m_reader.skipInt(); |
| 904 |
m_namespaces.pop(); |
| 905 |
} |
| 906 |
continue; |
| 907 |
} |
| 908 |
|
| 909 |
m_lineNumber = lineNumber; |
| 910 |
|
| 911 |
if (chunkType == CHUNK_XML_START_TAG) { |
| 912 |
m_namespaceUri = m_reader.readInt(); |
| 913 |
m_name = m_reader.readInt(); |
| 914 |
/* flags? */m_reader.skipInt(); |
| 915 |
int attributeCount = m_reader.readInt(); |
| 916 |
m_idAttribute = (attributeCount >>> 16) - 1; |
| 917 |
attributeCount &= 0xFFFF; |
| 918 |
m_classAttribute = m_reader.readInt(); |
| 919 |
m_styleAttribute = (m_classAttribute >>> 16) - 1; |
| 920 |
m_classAttribute = (m_classAttribute & 0xFFFF) - 1; |
| 921 |
m_attributes = m_reader.readIntArray(attributeCount * ATTRIBUTE_LENGTH); |
| 922 |
for (int i = ATTRIBUTE_IX_VALUE_TYPE; i < m_attributes.length; ) { |
| 923 |
m_attributes[i] = (m_attributes[i] >>> 24); |
| 924 |
i += ATTRIBUTE_LENGTH; |
| 925 |
} |
| 926 |
m_namespaces.increaseDepth(); |
| 927 |
m_event = START_TAG; |
| 928 |
break; |
| 929 |
} |
| 930 |
|
| 931 |
if (chunkType == CHUNK_XML_END_TAG) { |
| 932 |
m_namespaceUri = m_reader.readInt(); |
| 933 |
m_name = m_reader.readInt(); |
| 934 |
m_event = END_TAG; |
| 935 |
m_decreaseDepth = true; |
| 936 |
break; |
| 937 |
} |
| 938 |
|
| 939 |
if (chunkType == CHUNK_XML_TEXT) { |
| 940 |
m_name = m_reader.readInt(); |
| 941 |
/* ? */m_reader.skipInt(); |
| 942 |
/* ? */m_reader.skipInt(); |
| 943 |
m_event = TEXT; |
| 944 |
break; |
| 945 |
} |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
private void setFirstError(AndrolibException error) { |
| 950 |
if (mFirstError == null) { |
| 951 |
mFirstError = error; |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
// ///////////////////////////////// data |
| 956 |
/* |
| 957 |
* All values are essentially indices, e.g. m_name is an index of name in |
| 958 |
* m_strings. |
| 959 |
*/ |
| 960 |
private ExtDataInput m_reader; |
| 961 |
private ResAttrDecoder mAttrDecoder; |
| 962 |
private AndrolibException mFirstError; |
| 963 |
|
| 964 |
private boolean m_operational = false; |
| 965 |
private StringBlock m_strings; |
| 966 |
private int[] m_resourceIDs; |
| 967 |
private final NamespaceStack m_namespaces = new NamespaceStack(); |
| 968 |
private final String android_ns = "http://schemas.android.com/apk/res/android"; |
| 969 |
private boolean m_decreaseDepth; |
| 970 |
private int m_event; |
| 971 |
private int m_lineNumber; |
| 972 |
private int m_name; |
| 973 |
private int m_namespaceUri; |
| 974 |
private int[] m_attributes; |
| 975 |
private int m_idAttribute; |
| 976 |
private int m_classAttribute; |
| 977 |
private int m_styleAttribute; |
| 978 |
|
| 979 |
private final static Logger LOGGER = Logger.getLogger(AXmlResourceParser.class.getName()); |
| 980 |
private static final String E_NOT_SUPPORTED = "Method is not supported."; |
| 981 |
private static final int ATTRIBUTE_IX_NAMESPACE_URI = 0, |
| 982 |
ATTRIBUTE_IX_NAME = 1, ATTRIBUTE_IX_VALUE_STRING = 2, |
| 983 |
ATTRIBUTE_IX_VALUE_TYPE = 3, ATTRIBUTE_IX_VALUE_DATA = 4, |
| 984 |
ATTRIBUTE_LENGTH = 5; |
| 985 |
|
| 986 |
private static final int CHUNK_AXML_FILE = 0x00080003, CHUNK_AXML_FILE_BROKEN = 0x00080001, |
| 987 |
CHUNK_RESOURCEIDS = 0x00080180, CHUNK_XML_FIRST = 0x00100100, |
| 988 |
CHUNK_XML_START_NAMESPACE = 0x00100100, |
| 989 |
CHUNK_XML_END_NAMESPACE = 0x00100101, |
| 990 |
CHUNK_XML_START_TAG = 0x00100102, CHUNK_XML_END_TAG = 0x00100103, |
| 991 |
CHUNK_XML_TEXT = 0x00100104, CHUNK_XML_LAST = 0x00100104; |
| 992 |
|
| 993 |
private static final int PRIVATE_PKG_ID = 0x7F; |
| 994 |
} |