1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "utils/utils.h"
17
18 #include <iostream>
19 #include <iterator>
20 #include <fstream>
21
22 namespace panda::utils {
23 // CC-OFFNXT(G.FUN.01): public API
GetAsset(const std::string & uri,uint8_t ** buff,size_t * buffSize,std::vector<uint8_t> & content,std::string & ami,bool & useSecureMem,void ** mapper,bool isRestricted)24 void GetAsset(const std::string &uri, [[maybe_unused]] uint8_t **buff, [[maybe_unused]] size_t *buffSize,
25 std::vector<uint8_t> &content, std::string &ami, bool &useSecureMem, [[maybe_unused]] void **mapper,
26 [[maybe_unused]] bool isRestricted)
27 {
28 size_t index = uri.find_last_of(".");
29 if (index == std::string::npos) {
30 std::cerr << "Invalid uri: " << uri << std::endl;
31 return;
32 }
33
34 auto path = uri.substr(0, index) + ".abc";
35 std::ifstream fs(path, std::ios::binary);
36 if (!fs) {
37 std::cerr << "Cannot read file: " << path << std::endl;
38 return;
39 }
40
41 fs.unsetf(std::ios::skipws);
42
43 std::streampos size;
44 fs.seekg(0, std::ios::end);
45 size = fs.tellg();
46 fs.seekg(0, std::ios::beg);
47
48 content.reserve(size);
49
50 content.insert(content.begin(), std::istream_iterator<uint8_t>(fs), std::istream_iterator<uint8_t>());
51
52 ami = path;
53 useSecureMem = false;
54 }
55
56 } // namespace panda::utils
57