Class: ApkResources

Inherits:
Object
  • Object
show all
Defined in:
lib/apktools/apkresources.rb

Overview

Class to parse an APK’s resources.arsc data and retrieve resource data associated with a given R.id value

Defined Under Namespace

Classes: ChunkHeader, PackageHeader, ResType, ResTypeConfig, ResTypeEntry, ResTypeSpec, StringPool

Constant Summary collapse

DEBUG =

:nodoc:

false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apk_file) ⇒ ApkResources

Create a new ApkResources instance from the specified apk_file

This opens and parses the contents of the APK’s resources.arsc file.



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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/apktools/apkresources.rb', line 128

def initialize(apk_file)
	data = nil		
	# Get resources.arsc from the APK file
	Zip::ZipFile.foreach(apk_file) do |f|
	  if f.name.match(/resources.arsc/)
		data = f.get_input_stream.read
	  end
	end
	
	# Parse the Table Chunk
	## Header
	header_type = read_short(data, HEADER_START)
	header_size = read_short(data, HEADER_START+2)
	header_chunk_size = read_word(data, HEADER_START+4)
	header_package_count = read_word(data, HEADER_START+8)
	puts "Resource Package Count = #{header_package_count}" if DEBUG
	if header_package_count > 1
		puts "ApkResources only supports single package resources."
		exit(1)
	end
	
	# Parse the StringPool Chunk
	## Header
	startoffset_pool = HEADER_START + header_size
	puts "Parse Main StringPool Chunk" if DEBUG
	@stringpool_main = parse_stringpool(data, startoffset_pool)
	puts "#{@stringpool_main.values.length} strings found" if DEBUG
	
	# Parse the Package Chunk
	## Header
	startoffset_package = startoffset_pool + @stringpool_main.header.chunk_size
	header = ChunkHeader.new( read_short(data, startoffset_package),
			read_short(data, startoffset_package+2),
			read_word(data, startoffset_package+4) )
	
	package_id = read_word(data, startoffset_package+8)
	package_name = read_string(data, startoffset_package+12, 256, "UTF-8")
	package_type_strings = read_word(data, startoffset_package+268)
	package_last_type = read_word(data, startoffset_package+272)
	package_key_strings = read_word(data, startoffset_package+276)
	package_last_key = read_word(data, startoffset_package+280)
	
	@package_header = PackageHeader.new(header, package_id, package_name, package_type_strings, package_key_strings)
	
	## typeStrings StringPool
	startoffset_typestrings = startoffset_package + package_type_strings
	puts "Parse typeStrings StringPool Chunk" if DEBUG
	@stringpool_typestrings = parse_stringpool(data, startoffset_typestrings)
	
	## keyStrings StringPool
	startoffset_keystrings = startoffset_package + package_key_strings
	puts "Parse keyStrings StringPool Chunk" if DEBUG
	@stringpool_keystrings = parse_stringpool(data, startoffset_keystrings)
	
	## typeSpec/type Chunks		
	@type_data = Array.new()
	current_spec = nil
	
	current = startoffset_keystrings + @stringpool_keystrings.header.chunk_size
	puts "Parse Type/TypeSpec Chunks" if DEBUG
	while current < data.length
		## Parse Header
		header = ChunkHeader.new( read_short(data, current),
				read_short(data, current+2),
				read_word(data, current+4) )
		## Check Type
		if header.type == CHUNKTYPE_TYPESPEC
			typespec_id = read_byte(data, current+8)
			typespec_entrycount = read_word(data, current+12)
			
			## Parse the config flags for each entry
			typespec_entries = Array.new()
			i=0
			while i < typespec_entrycount
				offset = i * 4 + (current+16)
				typespec_entries << read_word(data, offset)
				
				i += 1
			end
			
			typespec_name = @stringpool_typestrings.values[typespec_id - 1]
			current_spec = ResTypeSpec.new(header, typespec_name, typespec_entrycount, typespec_entries, nil)
			
			@type_data << current_spec
			current += header.chunk_size
		elsif header.type == CHUNKTYPE_TYPE
			type_id = read_byte(data, current+8)
			type_entrycount = read_word(data, current+12) 
			type_entryoffset = read_word(data, current+16)

			## The config flags set for this type chunk
			## TODO: Vary the size of the config structure based on size to accomodate for new flags
			config_size = read_word(data, current+20) # Number of bytes in structure
			type_config = ResTypeConfig.new( read_word(data, current+24),
					read_word(data, current+28),
					read_word(data, current+32),
					read_word(data, current+36 ),
					read_word(data, current+40),
					read_word(data, current+44),
					read_word(data, current+48),
					read_word(data, current+52) )

			## The remainder of the chunk is a list of the entry values for that type/configuration
			type_name = @stringpool_typestrings.values[type_id - 1]				
			if current_spec.types == nil
				current_spec.types = ResType.new(header, type_name, type_config, type_entrycount, Array.new())
			end

			i=0
			while i < type_entrycount
				## Ensure a hash exists for each type
				if current_spec.types.entries[i] == nil
					current_spec.types.entries[i] = Hash.new()
				end
				current_entry = current_spec.types.entries[i]

				## Get the start of the type from the offsets table
				index_offset = i * 4 + (current+56)
				start_offset = read_word(data, index_offset)
				if start_offset != OFFSET_NO_ENTRY
					## Set the index_offset to the start of the current entry
					index_offset = current + type_entryoffset + start_offset

					entry_flags = read_short(data, index_offset+2)
					entry_key = read_word(data, index_offset+4)
					entry_data_type = read_byte(data, index_offset+11)
					entry_data = read_word(data, index_offset+12)
					
					# Find the key in our strings index
					key_name = @stringpool_keystrings.values[entry_key]
					# Parse the value into a string
					data_value = nil
					case type_name
						when TYPE_STRING, TYPE_DRAWABLE
							data_value = get_resource_string(entry_data_type, entry_data)
						when TYPE_COLOR
							data_value = get_resource_color(entry_data_type, entry_data)
						when TYPE_DIMENSION
							data_value = get_resource_dimension(entry_data_type, entry_data)
						when TYPE_INTEGER
							data_value = get_resource_integer(entry_data_type, entry_data)
						when TYPE_BOOLEAN
							data_value = get_resource_bool(entry_data_type, entry_data)
						else
							puts "Complex Resources not yet supported." if DEBUG
							data_value = entry_data.to_s
					end
					current_entry[type_config] = ResTypeEntry.new(entry_flags, key_name, entry_data_type, data_value)
				end
				i += 1
			end
			
			current += header.chunk_size
		else
			puts "Unknown Chunk Found: #{header.type} #{header.size}" if DEBUG
			## End Immediately
			current = data.length
		end
	end
	
end

Instance Attribute Details

#package_headerObject (readonly)

PackageHeader containing information about all the type and key strings in the package



113
114
115
# File 'lib/apktools/apkresources.rb', line 113

def package_header
  @package_header
end

#stringpool_keystringsObject (readonly)

StringPool containing all key strings in the package



119
120
121
# File 'lib/apktools/apkresources.rb', line 119

def stringpool_keystrings
  @stringpool_keystrings
end

#stringpool_mainObject (readonly)

StringPool containing all value strings in the package



115
116
117
# File 'lib/apktools/apkresources.rb', line 115

def stringpool_main
  @stringpool_main
end

#stringpool_typestringsObject (readonly)

StringPool containing all type strings in the package



117
118
119
# File 'lib/apktools/apkresources.rb', line 117

def stringpool_typestrings
  @stringpool_typestrings
end

#type_dataObject (readonly)

Array of the ResTypeSpec chunks in the package



121
122
123
# File 'lib/apktools/apkresources.rb', line 121

def type_data
  @type_data
end

Instance Method Details

#get_all_keysObject

Return array of all the key values in the file



307
308
309
# File 'lib/apktools/apkresources.rb', line 307

def get_all_keys
	return @stringpool_keystrings.values
end

#get_all_stringsObject

Return array of all string values in the file



293
294
295
# File 'lib/apktools/apkresources.rb', line 293

def get_all_strings
	return @stringpool_main.values
end

#get_all_typesObject

Return array of all the type values in the file



300
301
302
# File 'lib/apktools/apkresources.rb', line 300

def get_all_types
	return @stringpool_typestrings.values
end

#get_default_resource_value(res_id) ⇒ Object

Obtain the default value for a given resource id

res_id: ID values of a resources as a FixNum or String representation (i.e. 0x7F060001)

Returns: The default ResTypeEntry to the given id, or nil if no default exists



358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/apktools/apkresources.rb', line 358

def get_default_resource_value(res_id)
	if res_id.is_a? String
		res_id = res_id.hex
	end
	
	entries = get_resource_value(res_id)
	if entries != nil
		default = ResTypeConfig.new(0, 0, 0, 0, 0, 0, 0, 0)
		default_entry = entries[default]
		return default_entry
	else
		return nil
	end
end

#get_resource_key(res_id, xml_format = false) ⇒ Object

Obtain the key value for a given resource id

res_id: ID value of a resource as a FixNum or String representation (i.e. 0x7F060001) xml_format: Optionally format return string for XML files.

If xml_format is true, return value will be @<type>/<key> If xml_format is false or missing, return value will be R.<type>.<key> If the resource id does not exist, return value will be nil



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/apktools/apkresources.rb', line 321

def get_resource_key(res_id, xml_format=false)
	if res_id.is_a? String
		res_id = res_id.hex
	end
	
	# R.id integers are a concatenation of package_id, type_id, and entry index
	res_package = (res_id >> 24) & 0xFF
	res_type = (res_id >> 16) & 0xFF
	res_index = res_id & 0xFFFF

	if res_package != @package_header.id
		# This is not a resource we can parse
		return nil
	end
	
	res_spec = @type_data[res_type-1]
	entry = res_spec.types.entries[res_index]
	
	if entry == nil
		# There is no entry in our table for this resource
		return nil
	end
	
	if xml_format
		return "@#{res_spec.id}/#{entry.values[0].key}"
	else
		return "R.#{res_spec.id}.#{entry.values[0].key}"
	end
end

#get_resource_value(res_id) ⇒ Object

Obtain the value(s) for a given resource id. A default resource is one defined in an unqualified directory.

res_id: ID value of a resource as a FixNum or String representation (i.e. 0x7F060001)

Returns: Hash of all entries matching this id, keyed by their matching ResTypeConfig or nil if the resource id cannot be found.



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/apktools/apkresources.rb', line 382

def get_resource_value(res_id)
	if res_id.is_a? String
		res_id = res_id.hex
	end
	
	# R.id integers are a concatenation of package_id, type_id, and entry index
	res_package = (res_id >> 24) & 0xFF
	res_type = (res_id >> 16) & 0xFF
	res_index = res_id & 0xFFFF

	if res_package != @package_header.id
		# This is not a resource we can parse
		return nil
	end
	
	res_spec = @type_data[res_type-1]
	
	entries = res_spec.types.entries[res_index]
	if entries == nil
		puts "Could not find #{type_name} ResType chunk" if DEBUG
		return nil
	end
	
	return entries
end