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