apktools 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/apktools/apkxml.rb +32 -22
  2. metadata +2 -2
@@ -84,9 +84,8 @@ class ApkXml
84
84
  #
85
85
  # * +namespace+ = Namespace prefix of the attribute
86
86
  # * +name+ = Name of the attribute
87
- # * +raw+ = Original raw string value of the attribute, if one exists
88
- # * +value+ = ResTypeEntry for the typed value of the attribute, if one exists
89
- XmlAttribute = Struct.new(:namespace, :name, :raw, :value)
87
+ # * +value+ = Value of the attribute
88
+ XmlAttribute = Struct.new(:namespace, :name, :value)
90
89
 
91
90
  ##
92
91
  # Structure that houses the data for a given resource entry
@@ -106,6 +105,8 @@ class ApkXml
106
105
  attr_reader :current_apk
107
106
  # ApkResources instance used to resolve resources in this APK
108
107
  attr_reader :apk_resources
108
+ # Array of XmlElements from the last parse operation
109
+ attr_reader :xml_elements
109
110
 
110
111
  ##
111
112
  # Create a new ApkXml instance from the specified +apk_file+
@@ -126,6 +127,8 @@ class ApkXml
126
127
  #
127
128
  # This opens and parses the contents of the APK's resources.arsc file.
128
129
  def parse_xml(xml_file, pretty = false, resolve_resources = false)
130
+ # Reset variables
131
+ @xml_elements = Array.new()
129
132
  xml_output = ""
130
133
  indent = 0
131
134
  data = nil
@@ -192,6 +195,7 @@ class ApkXml
192
195
  elsif header.type == TYPE_XML_STARTELEMENT
193
196
  tree_header = parse_tree_header(header, data, current)
194
197
  body_start = current+header.size
198
+ # Parse the element/attribute data
195
199
  namespace = nil
196
200
  if read_word(data, body_start) != OFFSET_NO_ENTRY
197
201
  namespace = stringpool_main.values[read_word(data, body_start)]
@@ -217,16 +221,34 @@ class ApkXml
217
221
  attr_name = stringpool_main.values[read_word(data, index_offset+4)]
218
222
  attr_raw = nil
219
223
  if read_word(data, index_offset+8) != OFFSET_NO_ENTRY
224
+ # Attribute has a raw value, use it
220
225
  attr_raw = stringpool_main.values[read_word(data, index_offset+8)]
221
226
  end
222
227
  entry = ResTypeEntry.new(0, nil, read_byte(data, index_offset+15), read_word(data, index_offset+16))
223
228
 
224
- attributes << XmlAttribute.new(attr_namespace, attr_name, attr_raw, entry)
229
+ attr_value = nil
230
+ if attr_raw != nil # Use raw value
231
+ attr_value = attr_raw
232
+ elsif entry.data_type == 1
233
+ # Find the resource
234
+ default_res = apk_resources.get_default_resource_value(entry.data)
235
+ if resolve_resources && default_res != nil
236
+ attr_value = default_res.data
237
+ else
238
+ attr_value = apk_resources.get_resource_key(entry.data, true)
239
+ end
240
+ else # Value is a constant
241
+ attr_value = "0x#{entry.data.to_s(16)}"
242
+ end
243
+
244
+
245
+ attributes << XmlAttribute.new(attr_namespace, attr_name, attr_value)
225
246
  i += 1
226
247
  end
227
248
 
228
249
  element = XmlElement.new(tree_header, namespace, name, id_idx, class_idx, style_idx, attributes, xml_output == "")
229
250
 
251
+ # Print the element/attribute data
230
252
  puts "ELEMENT_START: #{element.namespace} #{element.name}" if DEBUG
231
253
  display_name = element.namespace == nil ? element.name : "#{element.namespace}:#{element.name}"
232
254
 
@@ -247,31 +269,19 @@ class ApkXml
247
269
  end
248
270
 
249
271
  element.attributes.each do |attr|
250
- puts "---ATTRIBUTE: #{attr.namespace} #{attr.name} #{attr.raw} => #{attr.value.data_type} #{attr.value.data.to_s(16)}" if DEBUG
272
+ puts "---ATTRIBUTE: #{attr.namespace} #{attr.name} #{attr.value}" if DEBUG
251
273
  display_name = attr.namespace == nil ? attr.name : "#{attr.namespace}:#{attr.name}"
252
- display_value = nil
253
- if attr.raw != nil # Use raw value
254
- display_value = attr.raw
255
- elsif attr.value.data_type == 1
256
- # Find the resource
257
- default_res = apk_resources.get_default_resource_value(attr.value.data)
258
- if resolve_resources && default_res != nil
259
- display_value = default_res.data
260
- else
261
- display_value = apk_resources.get_resource_key(attr.value.data, true)
262
- end
263
- else # Value is a constant
264
- display_value = "0x#{attr.value.data.to_s(16)}"
265
- end
266
-
267
274
  if pretty
268
275
  xml_output += "\n" + (" " * indent)
269
276
  end
270
- xml_output += "#{display_name}=\"#{display_value}\" "
277
+ xml_output += "#{display_name}=\"#{attr.value}\" "
271
278
  end
272
279
 
273
280
  xml_output += ">"
274
-
281
+
282
+ # Push every new element onto the array
283
+ @xml_elements << element
284
+
275
285
  current += header.chunk_size
276
286
  elsif header.type == TYPE_XML_ENDELEMENT
277
287
  tree_header = parse_tree_header(header, data, current)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apktools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-10 00:00:00.000000000 Z
12
+ date: 2012-08-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rubyzip