1 /*
2  * Copyright (C) 2018 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 #include "hidden_api.h"
18 
19 #include <fstream>
20 #include <sstream>
21 
22 #include "dex/dex_file-inl.h"
23 
24 namespace art {
25 
GetApiMethodName(const DexFile & dex_file,uint32_t method_index)26 std::string HiddenApi::GetApiMethodName(const DexFile& dex_file, uint32_t method_index) {
27   std::stringstream ss;
28   const DexFile::MethodId& method_id = dex_file.GetMethodId(method_index);
29   ss << dex_file.StringByTypeIdx(method_id.class_idx_)
30      << "->"
31      << dex_file.GetMethodName(method_id)
32      << dex_file.GetMethodSignature(method_id).ToString();
33   return ss.str();
34 }
35 
GetApiFieldName(const DexFile & dex_file,uint32_t field_index)36 std::string HiddenApi::GetApiFieldName(const DexFile& dex_file, uint32_t field_index) {
37   std::stringstream ss;
38   const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
39   ss << dex_file.StringByTypeIdx(field_id.class_idx_)
40      << "->"
41      << dex_file.GetFieldName(field_id)
42      << ":"
43      << dex_file.GetFieldTypeDescriptor(field_id);
44   return ss.str();
45 }
46 
FillList(const char * filename,std::set<std::string> & entries)47 void HiddenApi::FillList(const char* filename, std::set<std::string>& entries) {
48   if (filename == nullptr) {
49     return;
50   }
51   std::ifstream in(filename);
52   std::string str;
53   while (std::getline(in, str)) {
54     entries.insert(str);
55     size_t pos = str.find("->");
56     if (pos != std::string::npos) {
57       // Add the class name.
58       entries.insert(str.substr(0, pos));
59       pos = str.find('(');
60       if (pos != std::string::npos) {
61         // Add the class->method name (so stripping the signature).
62         entries.insert(str.substr(0, pos));
63       }
64       pos = str.find(':');
65       if (pos != std::string::npos) {
66         // Add the class->field name (so stripping the type).
67         entries.insert(str.substr(0, pos));
68       }
69     }
70   }
71 }
72 
73 }  // namespace art
74