Module: FuncList

Defined in:
lib/codeobscure/funclist.rb

Constant Summary collapse

@@func_regex =
/\s*(\w+)\s*:\s*\(\s*\w*\s*\s*\w+\s*\*?\s*\)\s*\w+\s*/
@@func_simple_regex =
/\s*[-\+]\s*\(\s*\w+\s*\*?\)\s*(\w+)\s*;*/
@@hcls_regex =
/@interface\s+(\w+)\s*/
@@mcls_regex =
/@implementation\s+(\w+)\s*/
@@property_regex =
/\s*@property\s*\(.*?getter=(\w+).*?\)\s*\w+\s*\*?\s*\w+\s*.*;/
@@property_regex2 =
/\s*@property\s*\(.*?\)\s*\w+\s*\*?\s*(\w+)\s*.*;/
@@property_regex3 =
/\s*@property\s*\(.*?\)\s*\w+\s*<.*>\s*\*?\s*(\w+)\s*.*;/
@@value_for_key_filte_regex =

—————filter regex—————-

/\[\w*\s+setValue\s*:\s*.*\s* forKey\s*:\s*@\"(.*)\"\]/
@@class_from_str_regex =
/NSClassFromString\(\s*@"(\w+)"\s*\)/

Class Method Summary collapse

Class Method Details

.capture(content_str, type = "m", fetch_types = ["p","f","c"]) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
# File 'lib/codeobscure/funclist.rb', line 22

def self.capture(content_str,type = "m",fetch_types=["p","f","c"]) 
  results = []
  str = to_utf8 content_str
  if fetch_types.include? "f"
    str.scan @@func_regex do |curr_match|
      md = Regexp.last_match
      whole_match = md[0]
      captures = md.captures

      captures.each do |capture|
        results << "f:#{capture}"
        #p [whole_match, capture]
        p "f:[#{capture}]"
      end
    end
    #no arguments function
    str.scan @@func_simple_regex do |curr_match|
      md = Regexp.last_match
      whole_match = md[0]
      captures = md.captures

      captures.each do |capture|
        results << "f:#{capture}"
        #p [whole_match, capture]
        p "f:[#{capture}]"
      end
    end
  end
  cls_regex = ""
  if type == "h"
    cls_regex = @@hcls_regex
  else
    cls_regex = @@mcls_regex
  end
  if fetch_types.include? "c"
    str.scan cls_regex do |curr_match|
      md = Regexp.last_match
      whole_match = md[0]
      captures = md.captures

      captures.each do |capture|
        results << "c:#{capture}"
        #p [whole_match, capture]
        p "c:[#{capture}]"
      end
    end
  end
  if fetch_types.include? "p"
    str.scan @@property_regex do |curr_match|
      md = Regexp.last_match
      whole_match = md[0]
      captures = md.captures

      captures.each do |capture|
        results << "p:#{capture}"
        #p [whole_match, capture]
        p "p:[#{capture}]"
      end
    end
    str.scan @@property_regex2 do |curr_match|
      md = Regexp.last_match
      whole_match = md[0]
      captures = md.captures

      captures.each do |capture|
        results << "p:#{capture}"
        #p [whole_match, capture]
        p "p:[#{capture}]"
      end
    end
    str.scan @@property_regex3 do |curr_match|
      md = Regexp.last_match
      whole_match = md[0]
      captures = md.captures

      captures.each do |capture|
        results << "p:#{capture}"
        #p [whole_match, capture]
        p "p:[#{capture}]"
      end
    end
  end

  #---------------记录可能引起崩溃的字段----------------
  str.scan @@value_for_key_filte_regex do |curr_match|
    md = Regexp.last_match
    whole_match = md[0]
    captures = md.captures

    captures.each do |capture|
      FiltSymbols.insertValue capture
      p "过滤的[setValue forkey]数据:#{capture}"
    end
  end


  str.scan @@class_from_str_regex do |curr_match|
    md = Regexp.last_match
    whole_match = md[0]
    captures = md.captures

    captures.each do |capture|
      FiltSymbols.insertValue capture
      p "过滤的[NSClassFromString]数据:#{capture}"
    end
  end
 
  results
end

.genFuncList(path, type = "m", need_filt = true, fetch_types = ["p","f","c"]) ⇒ Object



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
# File 'lib/codeobscure/funclist.rb', line 132

def self.genFuncList(path,type = "m",need_filt=true,fetch_types=["p","f","c"])
  capture_methods = []
  p_methods = []
  funclist_path = "#{path}/func.list"  
  file = File.open(funclist_path, "w")
  file_pathes = []
  if type == "h" || type == "m"
    file_pathes = `find #{path} -name "*.#{type}" -d`.split "\n"
  elsif type == "all"
    file_pathes += `find #{path} -name "*.h" -d`.split "\n"
    file_pathes += `find #{path} -name "*.m" -d`.split "\n"
  end
  file_pathes.each do |file_path|
    content = File.read file_path
    captures = capture content , type , fetch_types
    captures.each do |capture_method| 
      method_type = capture_method.split(":").first
      method_content = capture_method.split(":").last
      if !(method_content.start_with?("init") || method_content.start_with?("set") || method_content.start_with?("_") || method_content.length < 4) || !need_filt
        if method_type == "c" || method_type == "f"
          if !capture_methods.include? capture_method 
            capture_methods << capture_method 
          end
        elsif method_type == "p"
          if !p_methods.include? capture_method 
            p_methods << capture_method 
          end
        end
      end
    end
  end
  
  p_methods.each do |capture_method|
    method_type = capture_method.split(":").first
    method_content = capture_method.split(":").last
    c_method = "c:#{method_content}"
    f_method = "f:#{method_content}"
    if capture_methods.include? c_method
      capture_methods.delete c_method
    end
    if capture_methods.include? f_method
      capture_methods.delete f_method
    end
  end

  capture_methods += p_methods

  if capture_methods.length > 0 
    file.write(capture_methods.join "\n") 
  end
  file.close
  funclist_path
end

.to_utf8(str) ⇒ Object



16
17
18
19
20
# File 'lib/codeobscure/funclist.rb', line 16

def self.to_utf8(str)
  str = str.force_encoding('UTF-8')
  return str if str.valid_encoding?
  str.encode("UTF-8", 'binary', invalid: :replace, undef: :replace, replace: '')
end