This repository has been archived by the owner on Jul 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
keymap.rb
executable file
·65 lines (50 loc) · 1.62 KB
/
keymap.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env ruby
ArrayDataRegex = /(?<=\.array-data 0x4\n).*?(?=\s*\.end array-data)/m
ArrayLengthRegex = %r{(?<=const/16 v0, )(.+?)(?=\n.*?fill-array-data v0)}m
SmaliFile = 'smali/com/google/android/inputmethod/pinyin/KeyMap.smali'
def extract
smali = File.read(SmaliFile)
array_data = smali[ArrayDataRegex].gsub('t', '')
File.open('KeyMap', 'w') do |f|
array_data.each_line do |line|
src_low_byte, src_high_byte, dst_low_byte, dst_high_byte =
line.strip.split(/\s+/).collect {|byte| byte.to_i(16) }
src = [src_low_byte + src_high_byte * 0x100].pack('U')
dst = [dst_low_byte + dst_high_byte * 0x100].pack('U')
f.puts "#{src} => #{dst}"
end
end
puts 'Extract key map from KeyMap.smali to KeyMap successfully'
end
def update
keymap = File.read('KeyMap')
array_data = []
keymap.each_line do |line|
src, dst = line.strip.split(/\s*=>\s*/)
src = '0x%xt 0x%xt' % src.unpack('U').pack('L').unpack('C C')
dst = '0x%xt 0x%xt' % dst.unpack('U').pack('L').unpack('C C')
array_data << "#{' ' * 8}#{src} #{dst}"
end
smali = File.read(SmaliFile)
smali.gsub!(ArrayDataRegex, array_data.join("\n"))
smali.gsub!(ArrayLengthRegex, '%#x' % array_data.size)
File.open(SmaliFile, 'w') do |f|
f.puts smali
end
puts 'Update KeyMap.smali with KeyMap successfully'
end
case ARGV.first
when 'e', 'extract'
extract
when 'u', 'update'
update
else
puts <<-USAGE
Usage: #{File.basename(__FILE__)} command
e[xtract]
Extract key map from KeyMap.smali and save to KeyMap file.
u[pdate]
Update KeyMap.smali with contents of KeyMap file.
USAGE
exit 1
end