• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // Start with fragmentId at 1 since 0 is used to indicate
54   // legacy behavior at CHRE
55   size_t fragmentId = 1;
56   size_t byteIndex = 0;
57   do {
58     // It is guaranteed that mFragmentRequests must have at least one element,
59     // which is the precondition of the getters retrieving the info from the
60     // first fragmented request.
61     if (fragmentId == 1) {
62       mFragmentRequests.emplace_back(
63           fragmentId++, transactionId, appId, appVersion, appFlags,
64           targetApiVersion, appBinary.size(),
65           getSubVector(appBinary, byteIndex, fragmentSize));
66     } else {
67       mFragmentRequests.emplace_back(
68           fragmentId++, transactionId, appId,
69           getSubVector(appBinary, byteIndex, fragmentSize));
70     }
71 
72     byteIndex += fragmentSize;
73   } while (byteIndex < appBinary.size());
74 }
75 
getNextRequest()76 const FragmentedLoadRequest &FragmentedLoadTransaction::getNextRequest() {
77   return mFragmentRequests.at(mCurrentRequestIndex++);
78 }
79 
isComplete() const80 bool FragmentedLoadTransaction::isComplete() const {
81   return (mCurrentRequestIndex >= mFragmentRequests.size());
82 }
83 
84 }  // namespace chre
85 }  // namespace android
86