Skip to content

Commit

Permalink
resolve references in XML files via the ARSC file
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenArzt committed Dec 15, 2023
1 parent b27d97c commit fea8906
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package soot.jimple.infoflow.android.axml;

import pxb.android.axml.AxmlVisitor;
import soot.jimple.infoflow.android.resources.ARSCFileParser;
import soot.jimple.infoflow.android.resources.ARSCFileParser.AbstractResource;
import soot.jimple.infoflow.android.resources.ARSCFileParser.BooleanResource;
import soot.jimple.infoflow.android.resources.ARSCFileParser.IntegerResource;
import soot.jimple.infoflow.android.resources.ARSCFileParser.StringResource;

/**
* Represents an attribute of an Android XML node.
*
* @param <T>
* determines the attribute's type. Currently {@link Integer},
* @param <T> determines the attribute's type. Currently {@link Integer},
* {@link Boolean} and {@link String} are supported.
* @author Stefan Haas, Mario Schlipf
* @author Steven Arzt
Expand Down Expand Up @@ -34,79 +38,60 @@ public class AXmlAttribute<T> extends AXmlElement {
protected int resourceId;

/**
* Creates a new {@link AXmlAttribute} object with the given
* <code>name</code>, <code>value</code> and <code>namespace</code>.<br />
* Creates a new {@link AXmlAttribute} object with the given <code>name</code>,
* <code>value</code> and <code>namespace</code>.<br />
* The <code>addded</code> flag is defaulted to true (see
* {@link AXmlElement#added}).
*
* @param name
* the attribute's name.
* @param value
* the attribute's value.
* @param ns
* the attribute's namespace.
* @param name the attribute's name.
* @param value the attribute's value.
* @param ns the attribute's namespace.
*/
public AXmlAttribute(String name, T value, String ns) {
this(name, -1, value, ns, true);
}

/**
* Creates a new {@link AXmlAttribute} object with the given
* <code>name</code>, <code>value</code> and <code>namespace</code>.<br />
* Creates a new {@link AXmlAttribute} object with the given <code>name</code>,
* <code>value</code> and <code>namespace</code>.<br />
* The <code>addded</code> flag is defaulted to true (see
* {@link AXmlElement#added}).
*
* @param name
* the attribute's name.
* @param resourceId
* the attribute's resource id.
* @param value
* the attribute's value.
* @param ns
* the attribute's namespace.
* @param name the attribute's name.
* @param resourceId the attribute's resource id.
* @param value the attribute's value.
* @param ns the attribute's namespace.
*/
public AXmlAttribute(String name, int resourceId, T value, String ns) {
this(name, resourceId, value, ns, true);
}

/**
* Creates a new {@link AXmlAttribute} object with the given
* <code>name</code>, <code>value</code> and <code>namespace</code>.
* Creates a new {@link AXmlAttribute} object with the given <code>name</code>,
* <code>value</code> and <code>namespace</code>.
*
* @param name
* the attribute's name.
* @param resourceId
* the attribute's resource id.
* @param value
* the attribute's value.
* @param ns
* the attribute's namespace.
* @param added
* wheter this attribute was part of a parsed xml file or added
* afterwards.
* @param name the attribute's name.
* @param resourceId the attribute's resource id.
* @param value the attribute's value.
* @param ns the attribute's namespace.
* @param added wheter this attribute was part of a parsed xml file or
* added afterwards.
*/
public AXmlAttribute(String name, int resourceId, T value, String ns, boolean added) {
this(name, resourceId, -1, value, ns, added);
}

/**
* Creates a new {@link AXmlAttribute} object with the given
* <code>name</code>, <code>type</code>, <code>value</code> and
* <code>namespace</code>.
* Creates a new {@link AXmlAttribute} object with the given <code>name</code>,
* <code>type</code>, <code>value</code> and <code>namespace</code>.
*
* @param name
* the attribute's name.
* @param resourceId
* the attribute's resource id.
* @param type
* the attribute's type
* @param value
* the attribute's value.
* @param ns
* the attribute's namespace.
* @param added
* wheter this attribute was part of a parsed xml file or added
* afterwards.
* @param name the attribute's name.
* @param resourceId the attribute's resource id.
* @param type the attribute's type
* @param value the attribute's value.
* @param ns the attribute's namespace.
* @param added wheter this attribute was part of a parsed xml file or
* added afterwards.
*/
public AXmlAttribute(String name, int resourceId, int type, T value, String ns, boolean added) {
super(ns, added);
Expand Down Expand Up @@ -137,8 +122,7 @@ public int getResourceId() {
/**
* Sets the value for this attribute.
*
* @param value
* the new value.
* @param value the new value.
*/
public void setValue(T value) {
this.value = value;
Expand Down Expand Up @@ -175,8 +159,8 @@ else if (this.value instanceof Boolean)
}

/**
* Returns an integer which identifies this attribute's type. This is the
* type originally passed to the constructor.
* Returns an integer which identifies this attribute's type. This is the type
* originally passed to the constructor.
*
* @return integer representing the attribute's type
* @see AxmlVisitor#TYPE_INT_HEX
Expand All @@ -192,6 +176,91 @@ public String toString() {
return this.name + "=\"" + this.value + "\"";
}

/**
* Gets this value as a Boolean. This function only looks at the immediate value
* and does not resolve references.
*
* @return This value as a Boolean.
*/
public boolean asBoolean() {
return !getValue().equals(Boolean.FALSE);
}

/**
* Gets this value as a Boolean. If this value is a reference to a resource, a
* lookup is performed.
*
* @param arscParser The ARSC parser for resource lookups
* @return This value as a Boolean.
*/
public boolean asBoolean(ARSCFileParser arscParser) {
if (type == AXmlTypes.TYPE_REFERENCE && value instanceof Integer) {
AbstractResource res = arscParser.findResource((int) value);
if (res instanceof BooleanResource)
return ((BooleanResource) res).getValue();
return false;
} else
return asBoolean();
}

/**
* Gets this value as a string. This function only looks at the immediate value
* and does not resolve references.
*
* @return This value as a string
*/
public String asString() {
return (String) value;
}

/**
* Gets this value as a string. If this value is a reference to a resource, a
* lookup is performed.
*
* @param arscParser The ARSC parser for resource lookups
* @return This value as a string
*/
public String asString(ARSCFileParser arscParser) {
if (type == AXmlTypes.TYPE_REFERENCE && value instanceof Integer) {
AbstractResource res = arscParser.findResource((int) value);
if (res instanceof StringResource)
return ((StringResource) res).getValue();
return null;
} else
return asString();
}

/**
* Gets this value as an integer value. This function only looks at the
* immediate value and does not resolve references.
*
* @return This value as an integer value
*/
public int asInteger() {
if (value instanceof Integer)
return (int) value;

// If the value has a weird data type, we stil try to parse it
return value == null ? -1 : Integer.valueOf((String) value);
}

/**
* Gets this value as an integer value. If this value is a reference to a
* resource, a lookup is performed.
*
* @param arscParser The ARSC parser for resource lookups
* @return This value as an integer value
*/
public int asInteger(ARSCFileParser arscParser) {
if (type == AXmlTypes.TYPE_REFERENCE && value instanceof Integer) {
AbstractResource res = arscParser.findResource((int) value);
if (res instanceof IntegerResource)
return ((IntegerResource) res).getValue();
return -1;
} else
return asInteger();
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Loading

0 comments on commit fea8906

Please sign in to comment.