1 /*
2  * Copyright (C) 2011 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 #ifndef ART_LIBDEXFILE_DEX_SIGNATURE_INL_H_
18 #define ART_LIBDEXFILE_DEX_SIGNATURE_INL_H_
19 
20 #include "signature.h"
21 
22 #include "dex_file-inl.h"
23 
24 namespace art {
25 
26 inline bool Signature::operator==(const Signature& rhs) const {
27   if (dex_file_ == nullptr) {
28     return rhs.dex_file_ == nullptr;
29   }
30   if (rhs.dex_file_ == nullptr) {
31     return false;
32   }
33   if (dex_file_ == rhs.dex_file_) {
34     return proto_id_ == rhs.proto_id_;
35   }
36   uint32_t lhs_shorty_len;  // For a shorty utf16 length == mutf8 length.
37   const char* lhs_shorty_data = dex_file_->StringDataAndUtf16LengthByIdx(proto_id_->shorty_idx_,
38                                                                          &lhs_shorty_len);
39   std::string_view lhs_shorty(lhs_shorty_data, lhs_shorty_len);
40   {
41     uint32_t rhs_shorty_len;
42     const char* rhs_shorty_data =
43         rhs.dex_file_->StringDataAndUtf16LengthByIdx(rhs.proto_id_->shorty_idx_,
44                                                      &rhs_shorty_len);
45     std::string_view rhs_shorty(rhs_shorty_data, rhs_shorty_len);
46     if (lhs_shorty != rhs_shorty) {
47       return false;  // Shorty mismatch.
48     }
49   }
50   if (lhs_shorty[0] == 'L') {
51     const dex::TypeId& return_type_id = dex_file_->GetTypeId(proto_id_->return_type_idx_);
52     const dex::TypeId& rhs_return_type_id =
53         rhs.dex_file_->GetTypeId(rhs.proto_id_->return_type_idx_);
54     if (!DexFile::StringEquals(dex_file_, return_type_id.descriptor_idx_,
55                                rhs.dex_file_, rhs_return_type_id.descriptor_idx_)) {
56       return false;  // Return type mismatch.
57     }
58   }
59   if (lhs_shorty.find('L', 1) != std::string_view::npos) {
60     const dex::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
61     const dex::TypeList* rhs_params = rhs.dex_file_->GetProtoParameters(*rhs.proto_id_);
62     // We found a reference parameter in the matching shorty, so both lists must be non-empty.
63     DCHECK(params != nullptr);
64     DCHECK(rhs_params != nullptr);
65     uint32_t params_size = params->Size();
66     DCHECK_EQ(params_size, rhs_params->Size());  // Parameter list size must match.
67     for (uint32_t i = 0; i < params_size; ++i) {
68       const dex::TypeId& param_id = dex_file_->GetTypeId(params->GetTypeItem(i).type_idx_);
69       const dex::TypeId& rhs_param_id =
70           rhs.dex_file_->GetTypeId(rhs_params->GetTypeItem(i).type_idx_);
71       if (!DexFile::StringEquals(dex_file_, param_id.descriptor_idx_,
72                                  rhs.dex_file_, rhs_param_id.descriptor_idx_)) {
73         return false;  // Parameter type mismatch.
74       }
75     }
76   }
77   return true;
78 }
79 
80 }  // namespace art
81 
82 #endif  // ART_LIBDEXFILE_DEX_SIGNATURE_INL_H_
83