Class ARSCFileParser

java.lang.Object
soot.jimple.infoflow.android.resources.AbstractResourceParser
soot.jimple.infoflow.android.resources.ARSCFileParser

public class ARSCFileParser extends AbstractResourceParser
Parser for reading out the contents of Android's resource.arsc file. Structure declarations and comments taken from the Android source code and ported from C to Java.
Author:
Steven Arzt
  • Field Details

    • STRICT_MODE

      public static boolean STRICT_MODE
      If true any encountered resource format violation like reserved fields which should be zero but have a value will raise an RuntimeException. If false format violations will only be logged as errors.
    • logger

      protected final org.slf4j.Logger logger
    • RES_STRING_POOL_TYPE

      protected static final int RES_STRING_POOL_TYPE
      See Also:
    • RES_TABLE_TYPE

      protected static final int RES_TABLE_TYPE
      See Also:
    • RES_TABLE_PACKAGE_TYPE

      protected static final int RES_TABLE_PACKAGE_TYPE
      See Also:
    • RES_TABLE_TYPE_SPEC_TYPE

      protected static final int RES_TABLE_TYPE_SPEC_TYPE
      See Also:
    • RES_TABLE_TYPE_TYPE

      protected static final int RES_TABLE_TYPE_TYPE
      See Also:
    • SORTED_FLAG

      protected static final int SORTED_FLAG
      See Also:
    • UTF8_FLAG

      protected static final int UTF8_FLAG
      See Also:
    • SPEC_PUBLIC

      protected static final int SPEC_PUBLIC
      See Also:
    • TYPE_NULL

      protected static final int TYPE_NULL
      Contains no data
      See Also:
    • TYPE_REFERENCE

      protected static final int TYPE_REFERENCE
      The 'data' holds a ResTable_ref, a reference to another resource table entry.
      See Also:
    • TYPE_ATTRIBUTE

      protected static final int TYPE_ATTRIBUTE
      The 'data' holds an attribute resource identifier.
      See Also:
    • TYPE_STRING

      protected static final int TYPE_STRING
      The 'data' holds an index into the containing resource table's global value string pool.
      See Also:
    • TYPE_FLOAT

      protected static final int TYPE_FLOAT
      The 'data' holds a single-precision floating point number.
      See Also:
    • TYPE_DIMENSION

      protected static final int TYPE_DIMENSION
      The 'data' holds a complex number encoding a dimension value, such as "100in".
      See Also:
    • TYPE_FRACTION

      protected static final int TYPE_FRACTION
      The 'data' holds a complex number encoding a fraction of a container.
      See Also:
    • TYPE_FIRST_INT

      protected static final int TYPE_FIRST_INT
      Beginning of integer flavors...
      See Also:
    • TYPE_INT_DEC

      protected static final int TYPE_INT_DEC
      The 'data' is a raw integer value of the form n..n.
      See Also:
    • TYPE_INT_HEX

      protected static final int TYPE_INT_HEX
      The 'data' is a raw integer value of the form 0xn..n.
      See Also:
    • TYPE_INT_BOOLEAN

      protected static final int TYPE_INT_BOOLEAN
      The 'data' is either 0 or 1, for input "false" or "true" respectively.
      See Also:
    • TYPE_FIRST_COLOR_INT

      protected static final int TYPE_FIRST_COLOR_INT
      Beginning of color integer flavors...
      See Also:
    • TYPE_INT_COLOR_ARGB8

      protected static final int TYPE_INT_COLOR_ARGB8
      The 'data' is a raw integer value of the form #aarrggbb.
      See Also:
    • TYPE_INT_COLOR_RGB8

      protected static final int TYPE_INT_COLOR_RGB8
      The 'data' is a raw integer value of the form #rrggbb.
      See Also:
    • TYPE_INT_COLOR_ARGB4

      protected static final int TYPE_INT_COLOR_ARGB4
      The 'data' is a raw integer value of the form #argb.
      See Also:
    • TYPE_INT_COLOR_RGB4

      protected static final int TYPE_INT_COLOR_RGB4
      The 'data' is a raw integer value of the form #rgb.
      See Also:
    • TYPE_LAST_COLOR_INT

      protected static final int TYPE_LAST_COLOR_INT
      ...end of integer flavors.
      See Also:
    • TYPE_LAST_INT

      protected static final int TYPE_LAST_INT
      ...end of integer flavors.
      See Also:
    • ATTR_TYPE

      protected static final int ATTR_TYPE
      This entry holds the attribute's type code.
      See Also:
    • ATTR_MIN

      protected static final int ATTR_MIN
      For integral attributes, this is the minimum value it can hold.
      See Also:
    • ATTR_MAX

      protected static final int ATTR_MAX
      For integral attributes, this is the maximum value it can hold.
      See Also:
    • ATTR_L10N

      protected static final int ATTR_L10N
      Localization of this resource is can be encouraged or required with an aapt flag if this is set
      See Also:
    • ATTR_OTHER

      protected static final int ATTR_OTHER
      See Also:
    • ATTR_ZERO

      protected static final int ATTR_ZERO
      See Also:
    • ATTR_ONE

      protected static final int ATTR_ONE
      See Also:
    • ATTR_TWO

      protected static final int ATTR_TWO
      See Also:
    • ATTR_FEW

      protected static final int ATTR_FEW
      See Also:
    • ATTR_MANY

      protected static final int ATTR_MANY
      See Also:
    • NO_ENTRY

      protected static final int NO_ENTRY
      See Also:
    • COMPLEX_UNIT_SHIFT

      protected static final int COMPLEX_UNIT_SHIFT
      Where the unit type information is. This gives us 16 possible types, as defined below.
      See Also:
    • COMPLEX_UNIT_MASK

      protected static final int COMPLEX_UNIT_MASK
      See Also:
    • COMPLEX_UNIT_PX

      protected static final int COMPLEX_UNIT_PX
      TYPE_DIMENSION: Value is raw pixels.
      See Also:
    • COMPLEX_UNIT_DIP

      protected static final int COMPLEX_UNIT_DIP
      TYPE_DIMENSION: Value is Device Independent Pixels.
      See Also:
    • COMPLEX_UNIT_SP

      protected static final int COMPLEX_UNIT_SP
      TYPE_DIMENSION: Value is a Scaled device independent Pixels.
      See Also:
    • COMPLEX_UNIT_PT

      protected static final int COMPLEX_UNIT_PT
      TYPE_DIMENSION: Value is in points.
      See Also:
    • COMPLEX_UNIT_IN

      protected static final int COMPLEX_UNIT_IN
      TYPE_DIMENSION: Value is in inches.
      See Also:
    • COMPLEX_UNIT_MM

      protected static final int COMPLEX_UNIT_MM
      TYPE_DIMENSION: Value is in millimeters.
      See Also:
    • COMPLEX_UNIT_FRACTION

      protected static final int COMPLEX_UNIT_FRACTION
      TYPE_FRACTION: A basic fraction of the overall size.
      See Also:
    • COMPLEX_UNIT_FRACTION_PARENT

      protected static final int COMPLEX_UNIT_FRACTION_PARENT
      TYPE_FRACTION: A fraction of the parent size.
      See Also:
    • COMPLEX_RADIX_SHIFT

      protected static final int COMPLEX_RADIX_SHIFT
      Where the radix information is, telling where the decimal place appears in the mantissa. This give us 4 possible fixed point representations as defined below.
      See Also:
    • COMPLEX_RADIX_MASK

      protected static final int COMPLEX_RADIX_MASK
      See Also:
    • COMPLEX_RADIX_23p0

      protected static final int COMPLEX_RADIX_23p0
      The mantissa is an integral number -- i.e., 0xnnnnnn.0
      See Also:
    • COMPLEX_RADIX_16p7

      protected static final int COMPLEX_RADIX_16p7
      The mantissa magnitude is 16 bits -- i.e, 0xnnnn.nn
      See Also:
    • COMPLEX_RADIX_8p15

      protected static final int COMPLEX_RADIX_8p15
      The mantissa magnitude is 8 bits -- i.e, 0xnn.nnnn
      See Also:
    • COMPLEX_RADIX_0p23

      protected static final int COMPLEX_RADIX_0p23
      The mantissa magnitude is 0 bits -- i.e, 0x0.nnnnnn
      See Also:
    • COMPLEX_MANTISSA_SHIFT

      protected static final int COMPLEX_MANTISSA_SHIFT
      Where the actual value is. This gives us 23 bits of precision. The top bit is the sign.
      See Also:
    • COMPLEX_MANTISSA_MASK

      protected static final int COMPLEX_MANTISSA_MASK
      See Also:
    • MANTISSA_MULT

      protected static final float MANTISSA_MULT
      See Also:
    • RADIX_MULTS

      protected static final float[] RADIX_MULTS
    • FLAG_COMPLEX

      public static final int FLAG_COMPLEX
      If set, this is a complex entry, holding a set of name/value mappings. It is followed by an array of ResTable_Map structures.
      See Also:
    • FLAG_PUBLIC

      public static final int FLAG_PUBLIC
      If set, this resource has been declared public, so libraries are allowed to reference it.
      See Also:
    • FLAG_WEAK

      public static final int FLAG_WEAK
      If set, this is a weak resource and may be overridden by strong resources of the same name/type. This is only useful during linking with other resource tables.
      See Also:
    • FLAG_COMPACT

      public static final int FLAG_COMPACT
      If set, this is a compact entry with data type and value directly encoded in the entry.
      See Also:
    • FLAG_SPARSE

      public static final int FLAG_SPARSE
      If set, the entry is sparse, and encodes both the entry ID and offset into each entry, and a binary search is used to find the key. Only available on platforms >= O. Mark any types that use this with a v26 qualifier to prevent runtime issues on older platforms. ARSCFileParser.ResTable_Type.flags
      See Also:
    • FLAG_OFFSET16

      public static final int FLAG_OFFSET16
      If set, the offsets to the entries are encoded in 16-bit, real_offset = offset * 4u An 16-bit offset of 0xffffu means a NO_ENTRY ARSCFileParser.ResTable_Type.flags
      See Also:
  • Constructor Details

    • ARSCFileParser

      public ARSCFileParser()
  • Method Details

    • parse

      public void parse(File apkFile) throws IOException
      Parses the resource definition file in the given APK
      Parameters:
      apkFile - The APK file in which to parse the resource definition file
      Throws:
      IOException - Thrown if the given APK file cannot be opened
    • parse

      public void parse(InputStream stream) throws IOException
      Throws:
      IOException
    • isAttribute

      protected boolean isAttribute(ARSCFileParser.ResTable_Map map)
      Checks whether the given complex map entry is one of the well-known attributes.
      Parameters:
      map - The map entry to check
      Returns:
      True if the given entry is one of the well-known attributes, otherwise false.
    • complexToFloat

      protected static float complexToFloat(int complex)
      Taken from https://github.com/menethil/ApkTool/blob/master/src/android/util/TypedValue.java
      Parameters:
      complex -
      Returns:
    • getGlobalStringPool

      public Map<Integer,String> getGlobalStringPool()
    • getPackages

      public List<ARSCFileParser.ResPackage> getPackages()
    • getPackage

      public ARSCFileParser.ResPackage getPackage(int pkgID, String pkgName)
    • findResource

      public ARSCFileParser.AbstractResource findResource(int resourceId)
      Finds the resource with the given Android resource ID. This method is configuration-agnostic and simply returns the first match it finds.
      Parameters:
      resourceId - The Android resource ID for which to the find the resource object
      Returns:
      The resource object with the given Android resource ID if it has been found, otherwise null.
    • findAllResources

      public List<ARSCFileParser.AbstractResource> findAllResources(int resourceId)
      Finds all resources with the given Android resource ID. This method returns all matching resources, regardless of their respective configuration.
      Parameters:
      resourceId - The Android resource ID for which to the find the resource object
      Returns:
      The resource object with the given Android resource ID if it has been found, otherwise null.
    • findResourceType

      public ARSCFileParser.ResType findResourceType(int resourceId)
      Gets the resource type with the package and type specified in the given resource ID
      Parameters:
      resourceId - The resource ID
      Returns:
      The resource type with the given type and package ID, or null if no such type exists
    • parseResourceId

      public ARSCFileParser.ResourceId parseResourceId(int resourceId)
      Parses an Android resource ID into its components
      Parameters:
      resourceId - The numeric resource ID to parse
      Returns:
      The data contained in the given Android resource ID
    • findResourceByName

      public ARSCFileParser.AbstractResource findResourceByName(String type, String resourceName)
      Gets the resource with the given name and type
      Parameters:
      type - The type of the resource to retrieve, e.g., "string"
      resourceName - The name of the resource to retrieve
      Returns:
      The resource with the given name and type if such a resource exists, null otherwise
    • findStringResource

      public String findStringResource(String resourceName)
      Convenience method for loading a string resource with a given name
      Parameters:
      resourceName - The name of the resource to locate
      Returns:
      The string contents of the resource with the given name, if such a resource exists and is of type "string", null otherwise
    • findResourcesByType

      public List<ARSCFileParser.AbstractResource> findResourcesByType(String type)
      Finds all resources that have the given type
      Parameters:
      type - The resource type to look for
      Returns:
      All resources of the given type
    • getInstance

      public static ARSCFileParser getInstance(File apkFile) throws IOException
      Creates a new instance of the ARSCFileParser class and parses the Android resource database in the given APK file
      Parameters:
      apkFile - The APK file in which to parse the resource database
      Returns:
      The new ARSCFileParser instance, or null if the file could not be read
      Throws:
      IOException
    • addAll

      public void addAll(ARSCFileParser otherParser)
      Adds all resources loaded from another ARSCFileParser to this data object
      Parameters:
      otherParser - The other parser
    • raiseFormatViolationIssue

      protected void raiseFormatViolationIssue(String message, int offset)