• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013, 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 
32 package org.jf.baksmali;
33 
34 import org.jf.dexlib2.analysis.ClassPath;
35 import org.jf.dexlib2.analysis.InlineMethodResolver;
36 import org.jf.dexlib2.util.SyntheticAccessorResolver;
37 import org.xml.sax.Attributes;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.helpers.DefaultHandler;
40 
41 import javax.xml.parsers.ParserConfigurationException;
42 import javax.xml.parsers.SAXParser;
43 import javax.xml.parsers.SAXParserFactory;
44 import java.io.File;
45 import java.io.IOException;
46 import java.util.HashMap;
47 import java.util.Map;
48 
49 public class BaksmaliOptions {
50     public int apiLevel = 15;
51 
52     public boolean parameterRegisters = true;
53     public boolean localsDirective = false;
54     public boolean sequentialLabels = false;
55     public boolean debugInfo = true;
56     public boolean codeOffsets = false;
57     public boolean accessorComments = true;
58     public boolean allowOdex = false;
59     public boolean deodex = false;
60     public boolean implicitReferences = false;
61     public boolean normalizeVirtualMethods = false;
62 
63     // register info values
64     public static final int ALL = 1;
65     public static final int ALLPRE = 2;
66     public static final int ALLPOST = 4;
67     public static final int ARGS = 8;
68     public static final int DEST = 16;
69     public static final int MERGE = 32;
70     public static final int FULLMERGE = 64;
71 
72     public int registerInfo = 0;
73 
74     public Map<Integer,String> resourceIds = new HashMap<Integer,String>();
75     public InlineMethodResolver inlineResolver = null;
76     public ClassPath classPath = null;
77     public SyntheticAccessorResolver syntheticAccessorResolver = null;
78 
79     /**
80      * Load the resource ids from a set of public.xml files.
81      *
82      * @param resourceFiles A map of resource prefixes -> public.xml files
83      */
loadResourceIds(Map<String, File> resourceFiles)84     public void loadResourceIds(Map<String, File> resourceFiles) throws SAXException, IOException {
85         for (Map.Entry<String, File> entry: resourceFiles.entrySet()) {
86             try {
87                 SAXParser saxp = SAXParserFactory.newInstance().newSAXParser();
88                 final String prefix = entry.getKey();
89                 saxp.parse(entry.getValue(), new DefaultHandler() {
90                     @Override
91                     public void startElement(String uri, String localName, String qName,
92                                              Attributes attr) throws SAXException {
93                         if (qName.equals("public")) {
94                             String resourceType = attr.getValue("type");
95                             String resourceName = attr.getValue("name").replace('.', '_');
96                             Integer resourceId = Integer.decode(attr.getValue("id"));
97                             String qualifiedResourceName =
98                                     String.format("%s.%s.%s", prefix, resourceType, resourceName);
99                             resourceIds.put(resourceId, qualifiedResourceName);
100                         }
101                     }
102                 });
103             } catch (ParserConfigurationException ex) {
104                 throw new RuntimeException(ex);
105             }
106         }
107     }
108 }
109