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 "chre_host/fragmented_load_transaction.h"
18
19 #include <algorithm>
20
21 namespace android {
22 namespace chre {
23
24 namespace {
25
26 /**
27 * Returns a vector containing a specific subarray of the original vector.
28 *
29 * If the ending index of the subarray exceeds that of the source vector,
30 * the size will be truncated to the last element of the source vector.
31 *
32 * @param source the source vector
33 * @param start the starting index of the subarray
34 * @param size the size of the subarray
35 *
36 * @return the vector containing the subarray
37 */
getSubVector(const std::vector<uint8_t> & source,size_t start,size_t size)38 inline std::vector<uint8_t> getSubVector(const std::vector<uint8_t> &source,
39 size_t start, size_t size) {
40 size_t end = std::min(source.size(), start + size);
41 return (source.size() == 0)
42 ? std::vector<uint8_t>()
43 : std::vector<uint8_t>(source.begin() + start,
44 source.begin() + end); // [start, end)
45 }
46
47 } // anonymous namespace
48
FragmentedLoadTransaction(uint32_t transactionId,uint64_t appId,uint32_t appVersion,uint32_t appFlags,uint32_t targetApiVersion,const std::vector<uint8_t> & appBinary,size_t fragmentSize)49 FragmentedLoadTransaction::FragmentedLoadTransaction(
50 uint32_t transactionId, uint64_t appId, uint32_t appVersion,
51 uint32_t appFlags, uint32_t targetApiVersion,
52 const std::vector<uint8_t> &appBinary, size_t fragmentSize) {
53 mTransactionId = transactionId;
54
55 // Start with fragmentId at 1 since 0 is used to indicate
56 // legacy behavior at CHRE
57 size_t fragmentId = 1;
58 size_t byteIndex = 0;
59 do {
60 if (fragmentId == 1) {
61 mFragmentRequests.emplace_back(
62 fragmentId++, transactionId, appId, appVersion, appFlags,
63 targetApiVersion, appBinary.size(),
64 getSubVector(appBinary, byteIndex, fragmentSize));
65 } else {
66 mFragmentRequests.emplace_back(
67 fragmentId++, transactionId,
68 getSubVector(appBinary, byteIndex, fragmentSize));
69 }
70
71 byteIndex += fragmentSize;
72 } while (byteIndex < appBinary.size());
73 }
74
getNextRequest()75 const FragmentedLoadRequest &FragmentedLoadTransaction::getNextRequest() {
76 return mFragmentRequests.at(mCurrentRequestIndex++);
77 }
78
isComplete() const79 bool FragmentedLoadTransaction::isComplete() const {
80 return (mCurrentRequestIndex >= mFragmentRequests.size());
81 }
82
83 } // namespace chre
84 } // namespace android
85