1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.dx.command.grep;
18 
19 import com.android.dex.ClassData;
20 import com.android.dex.ClassDef;
21 import com.android.dex.Dex;
22 import com.android.dex.EncodedValueReader;
23 import com.android.dex.MethodId;
24 import com.android.dx.io.CodeReader;
25 import com.android.dx.io.instructions.DecodedInstruction;
26 import java.io.PrintWriter;
27 import java.util.HashSet;
28 import java.util.Set;
29 import java.util.regex.Pattern;
30 
31 public final class Grep {
32     private final Dex dex;
33     private final CodeReader codeReader = new CodeReader();
34     private final Set<Integer> stringIds;
35 
36     private final PrintWriter out;
37     private int count = 0;
38 
39     private ClassDef currentClass;
40     private ClassData.Method currentMethod;
41 
Grep(final Dex dex, Pattern pattern, final PrintWriter out)42     public Grep(final Dex dex, Pattern pattern, final PrintWriter out) {
43         this.dex = dex;
44         this.out = out;
45 
46         stringIds = getStringIds(dex, pattern);
47 
48         codeReader.setStringVisitor(new CodeReader.Visitor() {
49             public void visit(DecodedInstruction[] all, DecodedInstruction one) {
50                 encounterString(one.getIndex());
51             }
52         });
53     }
54 
readArray(EncodedValueReader reader)55     private void readArray(EncodedValueReader reader) {
56         for (int i = 0, size = reader.readArray(); i < size; i++) {
57             switch (reader.peek()) {
58             case EncodedValueReader.ENCODED_STRING:
59                 encounterString(reader.readString());
60                 break;
61             case EncodedValueReader.ENCODED_ARRAY:
62                 readArray(reader);
63                 break;
64             }
65         }
66     }
67 
encounterString(int index)68     private void encounterString(int index) {
69         if (stringIds.contains(index)) {
70             out.println(location() + " " + dex.strings().get(index));
71             count++;
72         }
73     }
74 
location()75     private String location() {
76         String className = dex.typeNames().get(currentClass.getTypeIndex());
77         if (currentMethod != null) {
78             MethodId methodId = dex.methodIds().get(currentMethod.getMethodIndex());
79             return className + "." + dex.strings().get(methodId.getNameIndex());
80         } else {
81             return className;
82         }
83     }
84 
85     /**
86      * Prints usages to out. Returns the number of matches found.
87      */
grep()88     public int grep() {
89         for (ClassDef classDef : dex.classDefs()) {
90             currentClass = classDef;
91             currentMethod = null;
92 
93             if (classDef.getClassDataOffset() == 0) {
94                 continue;
95             }
96 
97             ClassData classData = dex.readClassData(classDef);
98 
99             // find the strings in encoded constants
100             int staticValuesOffset = classDef.getStaticValuesOffset();
101             if (staticValuesOffset != 0) {
102                 readArray(new EncodedValueReader(dex.open(staticValuesOffset)));
103             }
104 
105             // find the strings in method bodies
106             for (ClassData.Method method : classData.allMethods()) {
107                 currentMethod = method;
108                 if (method.getCodeOffset() != 0) {
109                     codeReader.visitAll(dex.readCode(method).getInstructions());
110                 }
111             }
112         }
113 
114         currentClass = null;
115         currentMethod = null;
116         return count;
117     }
118 
getStringIds(Dex dex, Pattern pattern)119     private Set<Integer> getStringIds(Dex dex, Pattern pattern) {
120         Set<Integer> stringIds = new HashSet<Integer>();
121         int stringIndex = 0;
122         for (String s : dex.strings()) {
123             if (pattern.matcher(s).find()) {
124                 stringIds.add(stringIndex);
125             }
126             stringIndex++;
127         }
128         return stringIds;
129     }
130 }
131