• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013, Google LLC
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google LLC nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 package com.android.tools.smali.dexlib2.dexbacked.raw;
32 
33 import com.android.tools.smali.dexlib2.HiddenApiRestriction;
34 import com.android.tools.smali.dexlib2.dexbacked.raw.util.DexAnnotator;
35 import com.android.tools.smali.dexlib2.iface.ClassDef;
36 import com.android.tools.smali.dexlib2.iface.Field;
37 import com.android.tools.smali.dexlib2.iface.Method;
38 import com.android.tools.smali.dexlib2.util.AnnotatedBytes;
39 import com.android.tools.smali.dexlib2.dexbacked.DexBuffer;
40 import com.android.tools.smali.dexlib2.dexbacked.DexReader;
41 
42 import javax.annotation.Nonnull;
43 import javax.annotation.Nullable;
44 
45 public class HiddenApiClassDataItem {
46     public static final int SIZE_OFFSET = 0x0;
47     public static final int OFFSETS_LIST_OFFSET = 0x4;
48 
49     public static final int OFFSET_ITEM_SIZE = 0x4;
50 
51 
52     @Nonnull
makeAnnotator(@onnull DexAnnotator annotator, @Nonnull MapItem mapItem)53     public static SectionAnnotator makeAnnotator(@Nonnull DexAnnotator annotator, @Nonnull MapItem mapItem) {
54         return new SectionAnnotator(annotator, mapItem) {
55             @Nonnull @Override public String getItemName() {
56                 return "hiddenapi_class_data_item";
57             }
58 
59             @Override
60             protected void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
61                 int startOffset = out.getCursor();
62 
63                 out.annotate(4, "size = 0x%x", dexFile.getDataBuffer().readSmallUint(out.getCursor()));
64 
65                 int index = 0;
66                 for (ClassDef classDef : dexFile.getClasses()) {
67                     out.annotate(0, "[%d] %s", index, classDef);
68                     out.indent();
69 
70                     int offset = dexFile.getDataBuffer().readSmallUint(out.getCursor());
71                     if (offset == 0) {
72                         out.annotate(4, "offset = 0x%x", offset);
73                     } else {
74                         out.annotate(4, "offset = 0x%x (absolute offset: 0x%x)", offset, startOffset + offset);
75                     }
76 
77                     int nextOffset = out.getCursor();
78                     if (offset > 0) {
79                         out.deindent();
80 
81                         out.moveTo(startOffset + offset);
82 
83                         DexReader<? extends DexBuffer> reader = dexFile.getBuffer().readerAt(out.getCursor());
84 
85                         for (Field field : classDef.getStaticFields()) {
86                             out.annotate(0, "%s:", field);
87                             out.indent();
88                             int restrictions = reader.readSmallUleb128();
89                             out.annotateTo(reader.getOffset(), "restriction = 0x%x: %s",
90                                     restrictions,
91                                     HiddenApiRestriction.formatHiddenRestrictions(restrictions));
92                             out.deindent();
93                         }
94                         for (Field field : classDef.getInstanceFields()) {
95                             out.annotate(0, "%s:", field);
96                             out.indent();
97                             int restrictions = reader.readSmallUleb128();
98                             out.annotateTo(reader.getOffset(), "restriction = 0x%x: %s",
99                                     restrictions,
100                                     HiddenApiRestriction.formatHiddenRestrictions(restrictions));
101                             out.deindent();
102                         }
103                         for (Method method : classDef.getDirectMethods()) {
104                             out.annotate(0, "%s:", method);
105                             out.indent();
106                             int restrictions = reader.readSmallUleb128();
107                             out.annotateTo(reader.getOffset(), "restriction = 0x%x: %s",
108                                     restrictions,
109                                     HiddenApiRestriction.formatHiddenRestrictions(restrictions));
110                             out.deindent();
111                         }
112                         for (Method method : classDef.getVirtualMethods()) {
113                             out.annotate(0, "%s:", method);
114                             out.indent();
115                             int restrictions = reader.readSmallUleb128();
116                             out.annotateTo(reader.getOffset(), "restriction = 0x%x: %s",
117                                     restrictions,
118                                     HiddenApiRestriction.formatHiddenRestrictions(restrictions));
119                             out.deindent();
120                         }
121 
122                         out.indent();
123                     }
124 
125                     out.moveTo(nextOffset);
126 
127                     out.deindent();
128 
129                     index++;
130                 }
131             }
132         };
133     }
134 }
135