1 /*
2  * Copyright (C) 2017 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 art;
18 
19 import java.nio.ByteBuffer;
20 import java.util.Base64;
21 
22 import dalvik.system.InMemoryDexClassLoader;
23 
24 public class DexData {
getBootClassLoader()25   public static ClassLoader getBootClassLoader() {
26     ClassLoader cl = DexData.class.getClassLoader();
27     while (cl.getParent() != null) {
28       cl = cl.getParent();
29     }
30     return cl;
31   }
32 
create1()33   public static ClassLoader create1() {
34     return create1(getBootClassLoader());
35   }
create1(ClassLoader parent)36   public static ClassLoader create1(ClassLoader parent) {
37     return create(parent, DEX_DATA_B);
38   }
39 
create2()40   public static ClassLoader create2() {
41     return create2(getBootClassLoader());
42   }
create2(ClassLoader parent)43   public static ClassLoader create2(ClassLoader parent) {
44     return create(parent, DEX_DATA_AC);
45   }
46 
create12()47   public static ClassLoader create12() {
48     return create12(getBootClassLoader());
49   }
create12(ClassLoader parent)50   public static ClassLoader create12(ClassLoader parent) {
51     return create(parent, DEX_DATA_AC, DEX_DATA_B);
52   }
53 
create(ClassLoader parent, String... stringData)54   private static ClassLoader create(ClassLoader parent, String... stringData) {
55     ByteBuffer byteBuffers[] = new ByteBuffer[stringData.length];
56     for (int i = 0; i < stringData.length; i++) {
57       byteBuffers[i] = ByteBuffer.wrap(Base64.getDecoder().decode(stringData[i]));
58     }
59     return new InMemoryDexClassLoader(byteBuffers, parent);
60   }
61 
62   /*
63    * Derived from:
64    *
65    *   public class A {
66    *   }
67    *
68    *   public class C extends A {
69    *   }
70    *
71    */
72   private final static String DEX_DATA_AC =
73       "ZGV4CjAzNQD5KyH7WmGuqVEyL+2aKG1nyb27UJaCjFwQAgAAcAAAAHhWNBIAAAAAAAAAAIgBAAAH" +
74       "AAAAcAAAAAQAAACMAAAAAQAAAJwAAAAAAAAAAAAAAAMAAACoAAAAAgAAAMAAAAAQAQAAAAEAADAB" +
75       "AAA4AQAAQAEAAEgBAABNAQAAUgEAAGYBAAADAAAABAAAAAUAAAAGAAAABgAAAAMAAAAAAAAAAAAA" +
76       "AAAAAAABAAAAAAAAAAIAAAAAAAAAAAAAAAEAAAACAAAAAAAAAAEAAAAAAAAAcwEAAAAAAAABAAAA" +
77       "AQAAAAAAAAAAAAAAAgAAAAAAAAB9AQAAAAAAAAEAAQABAAAAaQEAAAQAAABwEAIAAAAOAAEAAQAB" +
78       "AAAAbgEAAAQAAABwEAAAAAAOAAY8aW5pdD4ABkEuamF2YQAGQy5qYXZhAANMQTsAA0xDOwASTGph" +
79       "dmEvbGFuZy9PYmplY3Q7AAFWABEABw4AEQAHDgAAAAEAAIGABIACAAABAAGBgASYAgALAAAAAAAA" +
80       "AAEAAAAAAAAAAQAAAAcAAABwAAAAAgAAAAQAAACMAAAAAwAAAAEAAACcAAAABQAAAAMAAACoAAAA" +
81       "BgAAAAIAAADAAAAAASAAAAIAAAAAAQAAAiAAAAcAAAAwAQAAAyAAAAIAAABpAQAAACAAAAIAAABz" +
82       "AQAAABAAAAEAAACIAQAA";
83 
84   /*
85    * Derived from:
86    *
87    *   public class B {
88    *   }
89    *
90    */
91   private final static String DEX_DATA_B =
92       "ZGV4CjAzNQBgKV6iWFG4aOm5WEy8oGtDZjqsftBgwJ2oAQAAcAAAAHhWNBIAAAAAAAAAACABAAAF" +
93       "AAAAcAAAAAMAAACEAAAAAQAAAJAAAAAAAAAAAAAAAAIAAACcAAAAAQAAAKwAAADcAAAAzAAAAOQA" +
94       "AADsAAAA9AAAAPkAAAANAQAAAgAAAAMAAAAEAAAABAAAAAIAAAAAAAAAAAAAAAAAAAABAAAAAAAA" +
95       "AAAAAAABAAAAAQAAAAAAAAABAAAAAAAAABUBAAAAAAAAAQABAAEAAAAQAQAABAAAAHAQAQAAAA4A" +
96       "Bjxpbml0PgAGQi5qYXZhAANMQjsAEkxqYXZhL2xhbmcvT2JqZWN0OwABVgARAAcOAAAAAQAAgYAE" +
97       "zAEACwAAAAAAAAABAAAAAAAAAAEAAAAFAAAAcAAAAAIAAAADAAAAhAAAAAMAAAABAAAAkAAAAAUA" +
98       "AAACAAAAnAAAAAYAAAABAAAArAAAAAEgAAABAAAAzAAAAAIgAAAFAAAA5AAAAAMgAAABAAAAEAEA" +
99       "AAAgAAABAAAAFQEAAAAQAAABAAAAIAEAAA==";
100 }
101