1 /*
2  * Copyright (c) 2021-2022 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 <map>
17 
18 #include "base/log/log.h"
19 #include "base/resource/internal_resource.h"
20 
21 // binary/errorcode.json
22 // Use objcopy transform to compiled object file.
23 // The following parameters represent the beginning and end of the file.
24 extern uint8_t _binary_errorcode_json_start[];
25 extern uint8_t* _binary_errorcode_json_end;
26 
27 // binary/indexletter_bar.json
28 // Use objcopy transform to compiled object file.
29 // The following parameters represent the beginning and end of the file.
30 extern uint8_t _binary_indexletter_bar_json_start[];
31 extern uint8_t* _binary_indexletter_bar_json_end;
32 
33 // binary/entry.json
34 // Use objcopy transform to compiled object file.
35 // The following parameters represent the beginning and end of the file.
36 extern uint8_t _binary_entry_json_start[];
37 extern uint8_t* _binary_entry_json_end;
38 
39 namespace OHOS::Ace {
40 namespace {
41 
42 struct ResourceData final {
ResourceDataOHOS::Ace::__anon42b9d7bd0110::ResourceData43     ResourceData(const uint8_t* buf, size_t size) : buf(buf), size(size) {}
44     ~ResourceData() = default;
45 
46     const uint8_t* buf;
47     size_t size;
48 };
49 
50 } // namespace
51 
52 InternalResource::InternalResource() = default;
53 
54 InternalResource::~InternalResource() = default;
55 
GetResource(const ResourceId id,size_t & size) const56 const uint8_t* InternalResource::GetResource(const ResourceId id, size_t& size) const
57 {
58     static const std::map<InternalResource::ResourceId, ResourceData> RESOURCE_MAP = {
59         { InternalResource::ResourceId::ERRORINFO_JSON,
60             ResourceData(_binary_errorcode_json_start,
61                 static_cast<size_t>(_binary_errorcode_json_end - _binary_errorcode_json_start)) },
62         { InternalResource::ResourceId::INDEXLETTER_BAR_JSON,
63             ResourceData(_binary_indexletter_bar_json_start,
64                 static_cast<size_t>(_binary_indexletter_bar_json_end - _binary_indexletter_bar_json_start)) },
65         { InternalResource::ResourceId::ENTRY_JSON,
66             ResourceData(
67                 _binary_entry_json_start, static_cast<size_t>(_binary_entry_json_end - _binary_entry_json_start)) },
68     };
69     auto iter = RESOURCE_MAP.find(id);
70     if (iter != RESOURCE_MAP.end()) {
71         size = iter->second.size;
72         return iter->second.buf;
73     }
74     return nullptr;
75 }
76 
77 } // namespace OHOS::Ace
78