| Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame^] | 17 | #ifndef ART_LIBDEXFILE_DEX_STRING_REFERENCE_H_ |
| 18 | #define ART_LIBDEXFILE_DEX_STRING_REFERENCE_H_ |
| Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 22 | #include <android-base/logging.h> |
| 23 | |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 24 | #include "dex/dex_file-inl.h" |
| 25 | #include "dex/dex_file_reference.h" |
| 26 | #include "dex/dex_file_types.h" |
| David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 27 | #include "dex/utf-inl.h" |
| Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
| Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 31 | // A string is located by its DexFile and the string_ids_ table index into that DexFile. |
| Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 32 | class StringReference : public DexFileReference { |
| 33 | public: |
| Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 34 | StringReference(const DexFile* file, dex::StringIndex index) |
| Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 35 | : DexFileReference(file, index.index_) {} |
| Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 36 | |
| Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 37 | dex::StringIndex StringIndex() const { |
| 38 | return dex::StringIndex(index); |
| Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 41 | const char* GetStringData() const { |
| 42 | return dex_file->GetStringData(dex_file->GetStringId(StringIndex())); |
| Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 43 | } |
| 44 | }; |
| 45 | |
| Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 46 | // Compare the actual referenced string values. Used for string reference deduplication. |
| 47 | struct StringReferenceValueComparator { |
| Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 48 | bool operator()(const StringReference& sr1, const StringReference& sr2) const { |
| Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 49 | // Note that we want to deduplicate identical strings even if they are referenced |
| 50 | // by different dex files, so we need some (any) total ordering of strings, rather |
| 51 | // than references. However, the references should usually be from the same dex file, |
| 52 | // so we choose the dex file string ordering so that we can simply compare indexes |
| 53 | // and avoid the costly string comparison in the most common case. |
| 54 | if (sr1.dex_file == sr2.dex_file) { |
| 55 | // Use the string order enforced by the dex file verifier. |
| 56 | DCHECK_EQ( |
| Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 57 | sr1.index < sr2.index, |
| Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 58 | CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(sr1.GetStringData(), |
| 59 | sr2.GetStringData()) < 0); |
| Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 60 | return sr1.index < sr2.index; |
| Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 61 | } else { |
| 62 | // Cannot compare indexes, so do the string comparison. |
| Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 63 | return CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(sr1.GetStringData(), |
| 64 | sr2.GetStringData()) < 0; |
| Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | } // namespace art |
| 70 | |
| David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame^] | 71 | #endif // ART_LIBDEXFILE_DEX_STRING_REFERENCE_H_ |