1 /*
2  * Copyright (C) 2020 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 "zip_cd_entry_map.h"
18 
ComputeHash(std::string_view name)19 static uint32_t ComputeHash(std::string_view name) {
20   return static_cast<uint32_t>(std::hash<std::string_view>{}(name));
21 }
22 
23 template <typename ZipStringOffset>
ToStringView(ZipStringOffset & entry,const uint8_t * start)24 const std::string_view ToStringView(ZipStringOffset& entry, const uint8_t *start) {
25   auto name = reinterpret_cast<const char*>(start + entry.name_offset);
26   return std::string_view{name, entry.name_length};
27 }
28 
29 // Convert a ZipEntry to a hash table index, verifying that it's in a valid range.
30 template <typename ZipStringOffset>
GetCdEntryOffset(std::string_view name,const uint8_t * start) const31 std::pair<ZipError, uint64_t> CdEntryMapZip32<ZipStringOffset>::GetCdEntryOffset(
32         std::string_view name, const uint8_t* start) const {
33   const uint32_t hash = ComputeHash(name);
34 
35   // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
36   uint32_t ent = hash & (hash_table_size_ - 1);
37   while (hash_table_[ent].name_offset != 0) {
38     if (ToStringView(hash_table_[ent], start) == name) {
39       return {kSuccess, static_cast<uint64_t>(hash_table_[ent].name_offset)};
40     }
41     ent = (ent + 1) & (hash_table_size_ - 1);
42   }
43 
44   ALOGV("Zip: Unable to find entry %.*s", static_cast<int>(name.size()), name.data());
45   return {kEntryNotFound, 0};
46 }
47 
48 template <typename ZipStringOffset>
AddToMap(std::string_view name,const uint8_t * start)49 ZipError CdEntryMapZip32<ZipStringOffset>::AddToMap(std::string_view name, const uint8_t* start) {
50   const uint64_t hash = ComputeHash(name);
51   uint32_t ent = hash & (hash_table_size_ - 1);
52 
53   /*
54    * We over-allocated the table, so we're guaranteed to find an empty slot.
55    * Further, we guarantee that the hashtable size is not 0.
56    */
57   while (hash_table_[ent].name_offset != 0) {
58     if (ToStringView(hash_table_[ent], start) == name) {
59       // We've found a duplicate entry. We don't accept duplicates.
60       ALOGW("Zip: Found duplicate entry %.*s", static_cast<int>(name.size()), name.data());
61       return kDuplicateEntry;
62     }
63     ent = (ent + 1) & (hash_table_size_ - 1);
64   }
65 
66   // `name` has already been validated before entry.
67   const char* start_char = reinterpret_cast<const char*>(start);
68   hash_table_[ent].name_offset = static_cast<uint32_t>(name.data() - start_char);
69   hash_table_[ent].name_length = static_cast<uint16_t>(name.size());
70   return kSuccess;
71 }
72 
73 template <typename ZipStringOffset>
ResetIteration()74 void CdEntryMapZip32<ZipStringOffset>::ResetIteration() {
75   current_position_ = 0;
76 }
77 
78 template <typename ZipStringOffset>
Next(const uint8_t * cd_start)79 std::pair<std::string_view, uint64_t> CdEntryMapZip32<ZipStringOffset>::Next(
80         const uint8_t* cd_start) {
81   while (current_position_ < hash_table_size_) {
82     const auto& entry = hash_table_[current_position_];
83     current_position_ += 1;
84 
85     if (entry.name_offset != 0) {
86       return {ToStringView(entry, cd_start), static_cast<uint64_t>(entry.name_offset)};
87     }
88   }
89   // We have reached the end of the hash table.
90   return {};
91 }
92 
AddToMap(std::string_view name,const uint8_t * start)93 ZipError CdEntryMapZip64::AddToMap(std::string_view name, const uint8_t* start) {
94   const auto [it, added] =
95       entry_table_.insert({name, name.data() - reinterpret_cast<const char*>(start)});
96   if (!added) {
97     ALOGW("Zip: Found duplicate entry %.*s", static_cast<int>(name.size()), name.data());
98     return kDuplicateEntry;
99   }
100   return kSuccess;
101 }
102 
GetCdEntryOffset(std::string_view name,const uint8_t *) const103 std::pair<ZipError, uint64_t> CdEntryMapZip64::GetCdEntryOffset(std::string_view name,
104                                                                 const uint8_t* /*cd_start*/) const {
105   const auto it = entry_table_.find(name);
106   if (it == entry_table_.end()) {
107     ALOGV("Zip: Could not find entry %.*s", static_cast<int>(name.size()), name.data());
108     return {kEntryNotFound, 0};
109   }
110 
111   return {kSuccess, it->second};
112 }
113 
ResetIteration()114 void CdEntryMapZip64::ResetIteration() {
115   iterator_ = entry_table_.begin();
116 }
117 
Next(const uint8_t *)118 std::pair<std::string_view, uint64_t> CdEntryMapZip64::Next(const uint8_t* /*cd_start*/) {
119   if (iterator_ == entry_table_.end()) {
120     return {};
121   }
122 
123   return *iterator_++;
124 }
125 
Create(uint64_t num_entries,size_t cd_length,uint16_t max_file_name_length)126 std::unique_ptr<CdEntryMapInterface> CdEntryMapInterface::Create(uint64_t num_entries,
127         size_t cd_length, uint16_t max_file_name_length) {
128   using T = std::unique_ptr<CdEntryMapInterface>;
129   if (num_entries > UINT16_MAX)
130     return T(new CdEntryMapZip64());
131 
132   uint16_t num_entries_ = static_cast<uint16_t>(num_entries);
133   if (cd_length > ZipStringOffset20::offset_max ||
134       max_file_name_length > ZipStringOffset20::length_max) {
135     return T(new CdEntryMapZip32<ZipStringOffset32>(num_entries_));
136   }
137   return T(new CdEntryMapZip32<ZipStringOffset20>(num_entries_));
138 }
139