1/*
2 * Copyright 2016, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.jf.baksmali;
33
34import com.beust.jcommander.JCommander;
35import com.beust.jcommander.Parameter;
36import com.beust.jcommander.Parameters;
37import com.beust.jcommander.ParametersDelegate;
38import org.jf.dexlib2.analysis.ClassProto;
39import org.jf.dexlib2.iface.ClassDef;
40import org.jf.dexlib2.iface.reference.FieldReference;
41import org.jf.util.SparseArray;
42import org.jf.util.jcommander.ExtendedParameters;
43
44import javax.annotation.Nonnull;
45import java.io.IOException;
46import java.util.List;
47
48@Parameters(commandDescription = "Lists the instance field offsets for classes in a dex file.")
49@ExtendedParameters(
50        commandName = "fieldoffsets",
51        commandAliases = { "fieldoffset", "fo" })
52public class ListFieldOffsetsCommand extends DexInputCommand {
53
54    @Parameter(names = {"-h", "-?", "--help"}, help = true,
55            description = "Show usage information")
56    private boolean help;
57
58    @ParametersDelegate
59    private AnalysisArguments analysisArguments = new AnalysisArguments();
60
61    public ListFieldOffsetsCommand(@Nonnull List<JCommander> commandAncestors) {
62        super(commandAncestors);
63    }
64
65    @Override public void run() {
66        if (help || inputList == null || inputList.isEmpty()) {
67            usage();
68            return;
69        }
70
71        if (inputList.size() > 1) {
72            System.err.println("Too many files specified");
73            usage();
74            return;
75        }
76
77        String input = inputList.get(0);
78        loadDexFile(input);
79        BaksmaliOptions options = getOptions();
80
81        try {
82            for (ClassDef classDef: dexFile.getClasses()) {
83                ClassProto classProto = (ClassProto) options.classPath.getClass(classDef);
84                SparseArray<FieldReference> fields = classProto.getInstanceFields();
85                String className = "Class "  + classDef.getType() + " : " + fields.size() + " instance fields\n";
86                System.out.write(className.getBytes());
87                for (int i=0;i<fields.size();i++) {
88                    String field = fields.keyAt(i) + ":" + fields.valueAt(i).getType() + " " + fields.valueAt(i).getName() + "\n";
89                    System.out.write(field.getBytes());
90                }
91                System.out.write("\n".getBytes());
92            }
93            System.out.close();
94        } catch (IOException ex) {
95            throw new RuntimeException(ex);
96        }
97    }
98
99    @Nonnull
100    private BaksmaliOptions getOptions() {
101        if (dexFile == null) {
102            throw new IllegalStateException("You must call loadDexFile first");
103        }
104
105        final BaksmaliOptions options = new BaksmaliOptions();
106
107        options.apiLevel = apiLevel;
108
109        try {
110            options.classPath = analysisArguments.loadClassPathForDexFile(
111                    inputFile.getAbsoluteFile().getParentFile(), dexFile, false);
112        } catch (Exception ex) {
113            System.err.println("Error occurred while loading class path files.");
114            ex.printStackTrace(System.err);
115            System.exit(-1);
116        }
117
118        return options;
119    }
120}
121