blob: 532cba4a83532d190f10e67be130cbc1387f241f [file] [log] [blame]
Tao Bao45685822017-10-13 14:54:12 -07001/*
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#include "otautil/rangeset.h"
18
Tao Bao67983152017-11-04 00:08:08 -070019#include <limits.h>
Tao Bao45685822017-10-13 14:54:12 -070020#include <stddef.h>
21
Tao Bao67983152017-11-04 00:08:08 -070022#include <algorithm>
Tao Bao45685822017-10-13 14:54:12 -070023#include <string>
24#include <utility>
25#include <vector>
26
27#include <android-base/logging.h>
28#include <android-base/parseint.h>
29#include <android-base/stringprintf.h>
30#include <android-base/strings.h>
31
32RangeSet::RangeSet(std::vector<Range>&& pairs) {
Tao Bao67983152017-11-04 00:08:08 -070033 blocks_ = 0;
34 if (pairs.empty()) {
35 LOG(ERROR) << "Invalid number of tokens";
36 return;
Tao Bao45685822017-10-13 14:54:12 -070037 }
38
Tao Bao67983152017-11-04 00:08:08 -070039 for (const auto& range : pairs) {
40 if (!PushBack(range)) {
41 Clear();
42 return;
43 }
44 }
Tao Bao45685822017-10-13 14:54:12 -070045}
46
47RangeSet RangeSet::Parse(const std::string& range_text) {
48 std::vector<std::string> pieces = android::base::Split(range_text, ",");
Tao Bao67983152017-11-04 00:08:08 -070049 if (pieces.size() < 3) {
50 LOG(ERROR) << "Invalid range text: " << range_text;
51 return {};
52 }
Tao Bao45685822017-10-13 14:54:12 -070053
54 size_t num;
Tao Bao67983152017-11-04 00:08:08 -070055 if (!android::base::ParseUint(pieces[0], &num, static_cast<size_t>(INT_MAX))) {
56 LOG(ERROR) << "Failed to parse the number of tokens: " << range_text;
57 return {};
58 }
59 if (num == 0) {
60 LOG(ERROR) << "Invalid number of tokens: " << range_text;
61 return {};
62 }
63 if (num % 2 != 0) {
64 LOG(ERROR) << "Number of tokens must be even: " << range_text;
65 return {};
66 }
67 if (num != pieces.size() - 1) {
68 LOG(ERROR) << "Mismatching number of tokens: " << range_text;
69 return {};
70 }
Tao Bao45685822017-10-13 14:54:12 -070071
72 std::vector<Range> pairs;
73 for (size_t i = 0; i < num; i += 2) {
74 size_t first;
Tao Bao45685822017-10-13 14:54:12 -070075 size_t second;
Tao Bao67983152017-11-04 00:08:08 -070076 if (!android::base::ParseUint(pieces[i + 1], &first, static_cast<size_t>(INT_MAX)) ||
77 !android::base::ParseUint(pieces[i + 2], &second, static_cast<size_t>(INT_MAX))) {
78 return {};
79 }
Tao Bao45685822017-10-13 14:54:12 -070080 pairs.emplace_back(first, second);
81 }
Tao Bao45685822017-10-13 14:54:12 -070082 return RangeSet(std::move(pairs));
83}
84
Tao Bao67983152017-11-04 00:08:08 -070085bool RangeSet::PushBack(Range range) {
86 if (range.first >= range.second) {
87 LOG(ERROR) << "Empty or negative range: " << range.first << ", " << range.second;
88 return false;
89 }
90 size_t sz = range.second - range.first;
91 if (blocks_ >= SIZE_MAX - sz) {
92 LOG(ERROR) << "RangeSet size overflow";
93 return false;
94 }
95
96 ranges_.push_back(std::move(range));
97 blocks_ += sz;
98 return true;
99}
100
101void RangeSet::Clear() {
102 ranges_.clear();
103 blocks_ = 0;
104}
105
Tao Bao45685822017-10-13 14:54:12 -0700106std::string RangeSet::ToString() const {
107 if (ranges_.empty()) {
108 return "";
109 }
110 std::string result = std::to_string(ranges_.size() * 2);
111 for (const auto& r : ranges_) {
112 result += android::base::StringPrintf(",%zu,%zu", r.first, r.second);
113 }
114
115 return result;
116}
117
118// Get the block number for the i-th (starting from 0) block in the RangeSet.
119size_t RangeSet::GetBlockNumber(size_t idx) const {
120 CHECK_LT(idx, blocks_) << "Out of bound index " << idx << " (total blocks: " << blocks_ << ")";
121
122 for (const auto& range : ranges_) {
123 if (idx < range.second - range.first) {
124 return range.first + idx;
125 }
126 idx -= (range.second - range.first);
127 }
128
129 CHECK(false) << "Failed to find block number for index " << idx;
130 return 0; // Unreachable, but to make compiler happy.
131}
132
133// RangeSet has half-closed half-open bounds. For example, "3,5" contains blocks 3 and 4. So "3,5"
134// and "5,7" are not overlapped.
135bool RangeSet::Overlaps(const RangeSet& other) const {
136 for (const auto& range : ranges_) {
137 size_t start = range.first;
138 size_t end = range.second;
139 for (const auto& other_range : other.ranges_) {
140 size_t other_start = other_range.first;
141 size_t other_end = other_range.second;
142 // [start, end) vs [other_start, other_end)
143 if (!(other_start >= end || start >= other_end)) {
144 return true;
145 }
146 }
147 }
148 return false;
149}
150
Tao Bao45685822017-10-13 14:54:12 -0700151// Ranges in the the set should be mutually exclusive; and they're sorted by the start block.
152SortedRangeSet::SortedRangeSet(std::vector<Range>&& pairs) : RangeSet(std::move(pairs)) {
153 std::sort(ranges_.begin(), ranges_.end());
154}
155
156void SortedRangeSet::Insert(const Range& to_insert) {
157 SortedRangeSet rs({ to_insert });
158 Insert(rs);
159}
160
161// Insert the input SortedRangeSet; keep the ranges sorted and merge the overlap ranges.
162void SortedRangeSet::Insert(const SortedRangeSet& rs) {
163 if (rs.size() == 0) {
164 return;
165 }
166 // Merge and sort the two RangeSets.
167 std::vector<Range> temp = std::move(ranges_);
168 std::copy(rs.begin(), rs.end(), std::back_inserter(temp));
169 std::sort(temp.begin(), temp.end());
170
171 Clear();
172 // Trim overlaps and insert the result back to ranges_.
173 Range to_insert = temp.front();
174 for (auto it = temp.cbegin() + 1; it != temp.cend(); it++) {
175 if (it->first <= to_insert.second) {
176 to_insert.second = std::max(to_insert.second, it->second);
177 } else {
178 ranges_.push_back(to_insert);
179 blocks_ += (to_insert.second - to_insert.first);
180 to_insert = *it;
181 }
182 }
183 ranges_.push_back(to_insert);
184 blocks_ += (to_insert.second - to_insert.first);
185}
186
187// Compute the block range the file occupies, and insert that range.
188void SortedRangeSet::Insert(size_t start, size_t len) {
189 Range to_insert{ start / kBlockSize, (start + len - 1) / kBlockSize + 1 };
190 Insert(to_insert);
191}
192
Tao Bao45685822017-10-13 14:54:12 -0700193bool SortedRangeSet::Overlaps(size_t start, size_t len) const {
194 RangeSet rs({ { start / kBlockSize, (start + len - 1) / kBlockSize + 1 } });
195 return Overlaps(rs);
196}
197
198// Given an offset of the file, checks if the corresponding block (by considering the file as
199// 0-based continuous block ranges) is covered by the SortedRangeSet. If so, returns the offset
200// within this SortedRangeSet.
201//
202// For example, the 4106-th byte of a file is from block 1, assuming a block size of 4096-byte.
203// The mapped offset within a SortedRangeSet("1-9 15-19") is 10.
204//
205// An offset of 65546 falls into the 16-th block in a file. Block 16 is contained as the 10-th
206// item in SortedRangeSet("1-9 15-19"). So its data can be found at offset 40970 (i.e. 4096 * 10
207// + 10) in a range represented by this SortedRangeSet.
208size_t SortedRangeSet::GetOffsetInRangeSet(size_t old_offset) const {
209 size_t old_block_start = old_offset / kBlockSize;
210 size_t new_block_start = 0;
211 for (const auto& range : ranges_) {
212 // Find the index of old_block_start.
213 if (old_block_start >= range.second) {
214 new_block_start += (range.second - range.first);
215 } else if (old_block_start >= range.first) {
216 new_block_start += (old_block_start - range.first);
217 return (new_block_start * kBlockSize + old_offset % kBlockSize);
218 } else {
219 CHECK(false) << "block_start " << old_block_start
220 << " is missing between two ranges: " << this->ToString();
221 return 0;
222 }
223 }
224 CHECK(false) << "block_start " << old_block_start
225 << " exceeds the limit of current RangeSet: " << this->ToString();
226 return 0;
227}