Class: Android::AXMLParser

Inherits:
Object
  • Object
show all
Defined in:
lib/android/axml_parser.rb

Overview

Note:

refer to Android OS framework code:

/frameworks/base/include/androidfw/ResourceTypes.h,

/frameworks/base/libs/androidfw/ResourceTypes.cpp

binary AXML parser

Direct Known Subclasses

AXMLWriter

Defined Under Namespace

Classes: ReadError

Constant Summary collapse

HEADER =
"\x03\x00\x08\x00"
TAG_START_NAMESPACE =
0x00100100
TAG_END_NAMESPACE =
0x00100101
TAG_START =
0x00100102
TAG_END =
0x00100103
TAG_TEXT =
0x00100104
TAG_CDSECT =
0x00100105
TAG_ENTITY_REF =
0x00100106
VAL_TYPE_NULL =
0
VAL_TYPE_REFERENCE =
1
VAL_TYPE_ATTRIBUTE =
2
VAL_TYPE_STRING =
3
VAL_TYPE_FLOAT =
4
VAL_TYPE_DIMENSION =
5
VAL_TYPE_FRACTION =
6
VAL_TYPE_INT_DEC =
16
VAL_TYPE_INT_HEX =
17
VAL_TYPE_INT_BOOLEAN =
18
VAL_TYPE_INT_COLOR_ARGB8 =
28
VAL_TYPE_INT_COLOR_RGB8 =
29
VAL_TYPE_INT_COLOR_ARGB4 =
30
VAL_TYPE_INT_COLOR_RGB4 =
31

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(axml) ⇒ AXMLParser

Returns a new instance of AXMLParser.

Parameters:

  • axml (String)

    binary xml data



52
53
54
55
# File 'lib/android/axml_parser.rb', line 52

def initialize(axml)
  @io = StringIO.new(axml, "rb")
  @strings = []
end

Instance Attribute Details

#metadataArray<String> (readonly)

Returns strings defined in axml.

Returns:

  • (Array<String>)

    strings defined in axml



49
50
51
# File 'lib/android/axml_parser.rb', line 49

def 
  @metadata
end

#stringsArray<String> (readonly)

Returns strings defined in axml.

Returns:

  • (Array<String>)

    strings defined in axml



49
50
51
# File 'lib/android/axml_parser.rb', line 49

def strings
  @strings
end

Class Method Details

.axml?(data) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/android/axml_parser.rb', line 18

def self.axml?(data)
  (data[0..3] == HEADER)
end

Instance Method Details

#convert_value(val_str_id, flags, val) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/android/axml_parser.rb', line 219

def convert_value(val_str_id, flags, val)
  unless val_str_id == 0xFFFFFFFF
    value = @strings[val_str_id]
  else
    type = flags >> 24
    case type
    when VAL_TYPE_NULL
      value = nil
    when VAL_TYPE_REFERENCE
      value = "@%#x" % val # refered resource id.
    when VAL_TYPE_INT_DEC
      value = val
    when VAL_TYPE_INT_HEX
      value = "%#x" % val
    when VAL_TYPE_INT_BOOLEAN
      value = ((val == 0xFFFFFFFF) || (val==1)) ? true : false
    else
      value = "[%#x, flag=%#x]" % [val, flags]
    end
  end
end

#current_nesting_levelObject



215
216
217
# File 'lib/android/axml_parser.rb', line 215

def current_nesting_level
  @parents.length
end

#get_namespace_prefix(ns_uri) ⇒ Object

find the first declared namespace prefix for a URI



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/android/axml_parser.rb', line 188

def get_namespace_prefix(ns_uri)
  # a namespace might be given as a URI or as a reference to a previously defined namespace.
  # E.g. like this:
  # <tag1 xmlns:android="http://schemas.android.com/apk/res/android">
  #   <tag2 xmlns:n0="android" />
  # </tag1>

  # Walk recursively through the namespaces to
  # transitively resolve URIs that just pointed to previous namespace prefixes
  current_uri = ns_uri
  @namespaces.reverse.each do |ns|
    if ns[:prefix] == current_uri
      # we found a previous namespace declaration that was referenced to by
      # the current_uri. Proceed with this namespace’s URI and try to see if this
      # is also just a reference to a previous namespace
      current_uri = ns[:uri]
    end
  end

  # current_uri now contains the URI of the topmost namespace declaration.
  # We’ll take the prefix of this and return it.
  @namespaces.reverse.each do |ns|
    return ns[:prefix] if ns[:uri] == current_uri
  end
  raise "Could not resolve URI #{ns_uri} to a namespace prefix"
end

#parseREXML::Document

parse binary xml

Returns:

  • (REXML::Document)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/android/axml_parser.rb', line 59

def parse
  @doc = REXML::Document.new
  @doc << REXML::XMLDecl.new

  @num_str = word(4*4)
  @xml_offset = word(3*4)

  @parents = [@doc]
  @namespaces = []
  @metadata = []
  parse_strings
  parse_tags
  @doc
end

#parse_attributeObject

parse attribute of a element



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/android/axml_parser.rb', line 175

def parse_attribute
  ns_id, name_id, val_str_id, flags, val = @io.read(4*5).unpack("V*")
  key = @strings[name_id]
  unless ns_id == 0xFFFFFFFF
    namespace_uri = @strings[ns_id]
    prefix = get_namespace_prefix(namespace_uri)
    key = "#{prefix}:#{key}"
  end
  value = convert_value(val_str_id, flags, val)
  return key, value, (flags >> 24)
end

#parse_stringsObject

relace string table parser



92
93
94
95
# File 'lib/android/axml_parser.rb', line 92

def parse_strings
  strpool = Resource::ResStringPool.new(@io.string, 8) # ugh!
  @strings = strpool.strings
end

#parse_tagsObject

parse tag



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/android/axml_parser.rb', line 98

def parse_tags
  # skip until first TAG_START_NAMESPACE
  pos = @xml_offset
  pos += 4 until (word(pos) == TAG_START_NAMESPACE)
  @io.pos -= 4

  # read tags
  #puts "start tag parse: %d(%#x)" % [@io.pos, @io.pos]
  until @io.eof?
    last_pos = @io.pos
    tag, tag1, line, tag3, ns_id, name_id = @io.read(4*6).unpack("V*")
    case tag
    when TAG_START
      tag6, num_attrs, tag8  = @io.read(4*3).unpack("V*")

      prefix = ''
      if ns_id != 0xFFFFFFFF
        namespace_uri = @strings[ns_id]
        prefix = get_namespace_prefix(namespace_uri) + ':'
      end
      elem = REXML::Element.new(prefix + @strings[name_id])

      meta_tag = if @strings[name_id] == 'meta-data'
                    @metadata << {}
                    true
                  end

      # If this element is a direct descendent of a namespace declaration
      # we add the namespace definition as an attribute.
      if @namespaces.last[:nesting_level] == current_nesting_level
        elem.add_namespace(@namespaces.last[:prefix], @namespaces.last[:uri])
      end
      #puts "start tag %d(%#x): #{@strings[name_id]} attrs:#{num_attrs}" % [last_pos, last_pos]
      @parents.last.add_element elem
      num_attrs.times do
        key, val, type = parse_attribute

        if meta_tag
          @metadata.last[key] = {
            value: val,
            position: @io.pos - 4,
            val_str_id: @io.pos - 12,
            is_string: type == VAL_TYPE_STRING
          }
        end

        if val.is_a?(String)
          # drop invalid chars that would be rejected by REXML from string
          val = val.scan(REXML::Text::VALID_XML_CHARS).join
        end
        elem.add_attribute(key, val)
      end
      @parents.push elem
    when TAG_END
      @parents.pop
    when TAG_END_NAMESPACE
      @namespaces.pop
      break if @namespaces.empty? # if the topmost namespace (usually 'android:') has been closed, we‘re done.
    when TAG_TEXT
      text = REXML::Text.new(@strings[ns_id])
      @parents.last.text = text
      dummy = @io.read(4*1).unpack("V*") # skip 4bytes
    when TAG_START_NAMESPACE
      prefix = @strings[ns_id]
      uri = @strings[name_id]
      @namespaces.push({ prefix: prefix, uri: uri, nesting_level: current_nesting_level })
    when TAG_CDSECT
      raise ReadError, "TAG_CDSECT not implemented"
    when TAG_ENTITY_REF
      raise ReadError, "TAG_ENTITY_REF not implemented"
    else
      raise ReadError, "pos=%d(%#x)[tag:%#x]" % [last_pos, last_pos, tag]
    end
  end
end

#short(offset) ⇒ Integer

read 2byte as short integer

Parameters:

  • offset (Integer)

    offset from top position. current position is used if ofset is nil

Returns:

  • (Integer)

    little endian unsign short value



86
87
88
89
# File 'lib/android/axml_parser.rb', line 86

def short(offset)
  @io.pos = offset unless offset.nil?
  @io.read(2).unpack("v")[0]
end

#word(offset = nil) ⇒ Integer

read one word(4byte) as integer

Parameters:

  • offset (Integer) (defaults to: nil)

    offset from top position. current position is used if ofset is nil

Returns:

  • (Integer)

    little endian word value



78
79
80
81
# File 'lib/android/axml_parser.rb', line 78

def word(offset=nil)
  @io.pos = offset unless offset.nil?
  @io.read(4).unpack("V")[0]
end