1 /*
2 * Copyright (c) 2021 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 "resource_item.h"
17 #include <iostream>
18 #include "securec.h"
19
20 namespace OHOS {
21 namespace Global {
22 namespace Restool {
23 using namespace std;
ResourceItem()24 ResourceItem::ResourceItem() : type_(ResType::INVALID_RES_TYPE)
25 {
26 }
27
ResourceItem(const ResourceItem & other)28 ResourceItem::ResourceItem(const ResourceItem &other)
29 {
30 CopyFrom(other);
31 }
32
ResourceItem(const string & name,const vector<KeyParam> & keyparams,ResType type)33 ResourceItem::ResourceItem(const string &name, const vector<KeyParam> &keyparams, ResType type)
34 : data_(nullptr), dataLen_(0), name_(name), keyparams_(keyparams), type_(type)
35 {
36 }
37
~ResourceItem()38 ResourceItem::~ResourceItem()
39 {
40 ReleaseData();
41 }
42
SetData(const string & data)43 bool ResourceItem::SetData(const string &data)
44 {
45 return SetData(reinterpret_cast<const int8_t *>(data.c_str()), data.length() + 1);
46 }
47
SetData(const int8_t * data,uint32_t length)48 bool ResourceItem::SetData(const int8_t *data, uint32_t length)
49 {
50 if (data == nullptr || length < 0) {
51 return false;
52 }
53
54 ReleaseData();
55 if (length == 0) {
56 // the string in the element directory can be empty
57 data_ = reinterpret_cast<int8_t *>(new (nothrow) int8_t[0]);
58 dataLen_ = 0;
59 return data_ != nullptr;
60 }
61 int8_t *buffer = reinterpret_cast<int8_t *>(new (nothrow) int8_t[length]);
62 if (buffer == nullptr) {
63 return false;
64 }
65
66 bool result = (memset_s(buffer, length, 0, length) == EOK);
67 result = result && (memcpy_s(buffer, length, data, length) == EOK);
68 data_ = buffer;
69 dataLen_ = length;
70 return result;
71 }
72
SetFilePath(const string & filePath)73 void ResourceItem::SetFilePath(const string &filePath)
74 {
75 filePath_ = filePath;
76 }
77
SetLimitKey(const string & limitKey)78 void ResourceItem::SetLimitKey(const string &limitKey)
79 {
80 limitKey_ = limitKey;
81 }
82
SetName(const string & name)83 void ResourceItem::SetName(const string &name)
84 {
85 name_ = name;
86 }
87
MarkCoverable()88 void ResourceItem::MarkCoverable()
89 {
90 coverable_ = true;
91 }
92
GetData() const93 const int8_t *ResourceItem::GetData() const
94 {
95 return data_;
96 }
97
GetDataLength() const98 uint32_t ResourceItem::GetDataLength() const
99 {
100 return dataLen_;
101 }
102
GetName() const103 const string &ResourceItem::GetName() const
104 {
105 return name_;
106 }
107
GetResType() const108 const ResType &ResourceItem::GetResType() const
109 {
110 return type_;
111 }
112
GetKeyParam() const113 const vector<KeyParam> &ResourceItem::GetKeyParam() const
114 {
115 return keyparams_;
116 }
117
GetFilePath() const118 const string &ResourceItem::GetFilePath() const
119 {
120 return filePath_;
121 }
122
GetLimitKey() const123 const string &ResourceItem::GetLimitKey() const
124 {
125 return limitKey_;
126 }
127
IsCoverable() const128 bool ResourceItem::IsCoverable() const
129 {
130 return coverable_;
131 }
132
IsArray() const133 bool ResourceItem::IsArray() const
134 {
135 return type_ == ResType::STRARRAY || type_ == ResType::INTARRAY;
136 }
137
IsPair() const138 bool ResourceItem::IsPair() const
139 {
140 return type_ == ResType::THEME || type_ == ResType::PLURAL || type_ == ResType::PATTERN;
141 }
142
SplitValue() const143 const std::vector<std::string> ResourceItem::SplitValue() const
144 {
145 std::vector<std::string> ret;
146 if (!(IsArray() || IsPair())) {
147 return ret;
148 }
149 char *buffer = reinterpret_cast<char*>(data_);
150 uint32_t index = 0;
151 while (index < dataLen_) {
152 uint16_t strLen = *reinterpret_cast<uint16_t*>(buffer + index);
153 index += sizeof(uint16_t);
154 if (index + strLen >= dataLen_) {
155 return ret;
156 }
157 ret.push_back(string(buffer+index, strLen));
158 index = index + strLen + 1;
159 }
160 return ret;
161 }
162
CheckData()163 void ResourceItem::CheckData()
164 {
165 if (GetDataLength() == 0) {
166 return;
167 }
168 int8_t data[GetDataLength()];
169 for (uint32_t i = 0; i < GetDataLength(); i++) {
170 data[i] = GetData()[i];
171 }
172 if (data[GetDataLength() - 1] == '\0') {
173 SetData(data, GetDataLength() - 1);
174 }
175 }
176
operator =(const ResourceItem & other)177 ResourceItem &ResourceItem::operator=(const ResourceItem &other)
178 {
179 if (this == &other) {
180 return *this;
181 }
182 CopyFrom(other);
183 return *this;
184 }
185
186 // below private founction
ReleaseData()187 void ResourceItem::ReleaseData()
188 {
189 if (data_ != nullptr) {
190 delete[] data_;
191 data_ = nullptr;
192 dataLen_ = 0;
193 }
194 }
195
CopyFrom(const ResourceItem & other)196 void ResourceItem::CopyFrom(const ResourceItem &other)
197 {
198 name_ = other.name_;
199 keyparams_ = other.keyparams_;
200 type_ = other.type_;
201 dataLen_ = other.dataLen_;
202 filePath_ = other.filePath_;
203 limitKey_ = other.limitKey_;
204 coverable_ = other.coverable_;
205 if (!SetData(other.data_, other.dataLen_)) {
206 ReleaseData();
207 }
208 }
209 }
210 }
211 }