1 /*
2  * Copyright (C) 2014 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 dexfuzz.rawdex;
18 
19 import java.io.IOException;
20 
21 public class ClassDataItem implements RawDexObject {
22   public int staticFieldsSize;
23   public int instanceFieldsSize;
24   public int directMethodsSize;
25   public int virtualMethodsSize;
26 
27   public EncodedField[] staticFields;
28   public EncodedField[] instanceFields;
29   public EncodedMethod[] directMethods;
30   public EncodedMethod[] virtualMethods;
31 
32   public static class MetaInfo {
33     public ClassDefItem classDefItem;
34   }
35 
36   public MetaInfo meta = new MetaInfo();
37 
38   @Override
read(DexRandomAccessFile file)39   public void read(DexRandomAccessFile file) throws IOException {
40     file.getOffsetTracker().getNewOffsettable(file, this);
41     staticFieldsSize = file.readUleb128();
42     instanceFieldsSize = file.readUleb128();
43     directMethodsSize = file.readUleb128();
44     virtualMethodsSize = file.readUleb128();
45 
46     staticFields = new EncodedField[staticFieldsSize];
47     for (int i = 0; i < staticFieldsSize; i++) {
48       (staticFields[i] = new EncodedField()).read(file);
49     }
50     instanceFields = new EncodedField[instanceFieldsSize];
51     for (int i = 0; i < instanceFieldsSize; i++) {
52       (instanceFields[i] = new EncodedField()).read(file);
53     }
54     directMethods = new EncodedMethod[directMethodsSize];
55     for (int i = 0; i < directMethodsSize; i++) {
56       (directMethods[i] = new EncodedMethod()).read(file);
57     }
58     virtualMethods = new EncodedMethod[virtualMethodsSize];
59     for (int i = 0; i < virtualMethodsSize; i++) {
60       (virtualMethods[i] = new EncodedMethod()).read(file);
61     }
62   }
63 
64   @Override
write(DexRandomAccessFile file)65   public void write(DexRandomAccessFile file) throws IOException {
66     file.getOffsetTracker().updatePositionOfNextOffsettable(file);
67     file.writeUleb128(staticFieldsSize);
68     file.writeUleb128(instanceFieldsSize);
69     file.writeUleb128(directMethodsSize);
70     file.writeUleb128(virtualMethodsSize);
71     for (int i = 0; i < staticFieldsSize; i++) {
72       staticFields[i].write(file);
73     }
74     for (int i = 0; i < instanceFieldsSize; i++) {
75       instanceFields[i].write(file);
76     }
77     for (int i = 0; i < directMethodsSize; i++) {
78       directMethods[i].write(file);
79     }
80     for (int i = 0; i < virtualMethodsSize; i++) {
81       virtualMethods[i].write(file);
82     }
83   }
84 
incrementEncodedFields(int insertedIdx, EncodedField[] fields)85   private void incrementEncodedFields(int insertedIdx, EncodedField[] fields) {
86     int fieldIdx = 0;
87     for (EncodedField field : fields) {
88       fieldIdx = field.fieldIdxDiff;
89       if (fieldIdx >= insertedIdx) {
90         field.fieldIdxDiff++;
91         // Only need to increment one, as all the others are diffed from the previous.
92         break;
93       }
94     }
95   }
96 
incrementEncodedMethods(int insertedIdx, EncodedMethod[] methods)97   private void incrementEncodedMethods(int insertedIdx, EncodedMethod[] methods) {
98     int methodIdx = 0;
99     for (EncodedMethod method : methods) {
100       methodIdx = method.methodIdxDiff;
101       if (methodIdx >= insertedIdx) {
102         method.methodIdxDiff++;
103         // Only need to increment one, as all the others are diffed from the previous.
104         break;
105       }
106     }
107   }
108 
109   @Override
incrementIndex(IndexUpdateKind kind, int insertedIdx)110   public void incrementIndex(IndexUpdateKind kind, int insertedIdx) {
111     if (kind == IndexUpdateKind.FIELD_ID) {
112       incrementEncodedFields(insertedIdx, staticFields);
113       incrementEncodedFields(insertedIdx, instanceFields);
114     }
115     if (kind == IndexUpdateKind.METHOD_ID) {
116       incrementEncodedMethods(insertedIdx, directMethods);
117       incrementEncodedMethods(insertedIdx, virtualMethods);
118     }
119   }
120 
121   /**
122    * For a given field index, search this ClassDataItem for a definition of this field.
123    * @return null if the field isn't in this ClassDataItem.
124    */
getEncodedFieldWithIndex(int fieldIdx)125   public EncodedField getEncodedFieldWithIndex(int fieldIdx) {
126     int searchFieldIdx = 0;
127     for (EncodedField field : instanceFields) {
128       searchFieldIdx += field.fieldIdxDiff;
129       if (searchFieldIdx == fieldIdx) {
130         return field;
131       }
132     }
133     searchFieldIdx = 0;
134     for (EncodedField field : staticFields) {
135       searchFieldIdx += field.fieldIdxDiff;
136       if (searchFieldIdx == fieldIdx) {
137         return field;
138       }
139     }
140     return null;
141   }
142 }
143