1 /*
2 * Copyright (C) 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 "attributes.h"
17
18 #include <map>
19 #include <memory>
20
21 #include "iam_logger.h"
22 #include "securec.h"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
28 class Attributes::Impl {
29 public:
30 Impl() = default;
31
32 explicit Impl(const std::vector<uint8_t> &raw);
33
34 Impl(const Impl &other) = delete;
35 Impl &operator=(const Impl &other) = delete;
36
37 Impl(Impl &&other) noexcept;
38 Impl &operator=(Impl &&other) noexcept;
39
40 virtual ~Impl() = default;
41
42 bool SetBoolValue(AttributeKey key, bool value);
43 bool SetUint64Value(AttributeKey key, uint64_t value);
44 bool SetUint32Value(AttributeKey key, uint32_t value);
45 bool SetUint16Value(AttributeKey key, uint16_t value);
46 bool SetUint8Value(AttributeKey key, uint8_t value);
47 bool SetInt32Value(AttributeKey key, int32_t value);
48 bool SetStringValue(AttributeKey key, const std::string &value);
49 bool SetAttributesValue(AttributeKey key, const Impl &value);
50 bool SetUint64ArrayValue(AttributeKey key, const std::vector<uint64_t> &value);
51 bool SetUint32ArrayValue(AttributeKey key, const std::vector<uint32_t> &value);
52 bool SetUint16ArrayValue(AttributeKey key, const std::vector<uint16_t> &value);
53 bool SetUint8ArrayValue(AttributeKey key, const std::vector<uint8_t> &value);
54
55 bool GetBoolValue(AttributeKey key, bool &value) const;
56 bool GetUint64Value(AttributeKey key, uint64_t &value) const;
57 bool GetUint32Value(AttributeKey key, uint32_t &value) const;
58 bool GetUint16Value(AttributeKey key, uint16_t &value) const;
59 bool GetUint8Value(AttributeKey key, uint8_t &value) const;
60 bool GetInt32Value(AttributeKey key, int32_t &value) const;
61 bool GetStringValue(AttributeKey key, std::string &value) const;
62 bool GetUint64ArrayValue(AttributeKey key, std::vector<uint64_t> &value) const;
63 bool GetUint32ArrayValue(AttributeKey key, std::vector<uint32_t> &value) const;
64 bool GetUint16ArrayValue(AttributeKey key, std::vector<uint16_t> &value) const;
65 bool GetUint8ArrayValue(AttributeKey key, std::vector<uint8_t> &value) const;
66 bool GetAttributesValue(AttributeKey key, Impl &value) const;
67 std::vector<uint8_t> Serialize() const;
68 std::vector<AttributeKey> GetKeys() const;
69
70 private:
71 static constexpr uint32_t MAX_ATTR_LENGTH = 81920;
72 static constexpr uint32_t MAX_ATTR_COUNT = 512;
73 static bool EncodeBoolValue(bool src, std::vector<uint8_t> &dst);
74 static bool EncodeUint64Value(uint64_t src, std::vector<uint8_t> &dst);
75 static bool EncodeUint32Value(uint32_t src, std::vector<uint8_t> &dst);
76 static bool EncodeUint16Value(uint16_t src, std::vector<uint8_t> &dst);
77 static bool EncodeUint8Value(uint8_t src, std::vector<uint8_t> &dst);
78 static bool EncodeInt32Value(int32_t src, std::vector<uint8_t> &dst);
79 static bool EncodeStringValue(const std::string &src, std::vector<uint8_t> &dst);
80 static bool EncodeUint64ArrayValue(const std::vector<uint64_t> &src, std::vector<uint8_t> &dst);
81 static bool EncodeUint32ArrayValue(const std::vector<uint32_t> &src, std::vector<uint8_t> &dst);
82 static bool EncodeUint16ArrayValue(const std::vector<uint16_t> &src, std::vector<uint8_t> &dst);
83 static bool EncodeUint8ArrayValue(const std::vector<uint8_t> &src, std::vector<uint8_t> &dst);
84
85 static bool DecodeBoolValue(const std::vector<uint8_t> &src, bool &dst);
86 static bool DecodeUint64Value(const std::vector<uint8_t> &src, uint64_t &dst);
87 static bool DecodeUint32Value(const std::vector<uint8_t> &src, uint32_t &dst);
88 static bool DecodeUint16Value(const std::vector<uint8_t> &src, uint16_t &dst);
89 static bool DecodeUint8Value(const std::vector<uint8_t> &src, uint8_t &dst);
90 static bool DecodeInt32Value(const std::vector<uint8_t> &src, int32_t &dst);
91 static bool DecodeStringValue(const std::vector<uint8_t> &src, std::string &dst);
92 static bool DecodeUint64ArrayValue(const std::vector<uint8_t> &src, std::vector<uint64_t> &dst);
93 static bool DecodeUint32ArrayValue(const std::vector<uint8_t> &src, std::vector<uint32_t> &dst);
94 static bool DecodeUint16ArrayValue(const std::vector<uint8_t> &src, std::vector<uint16_t> &dst);
95 static bool DecodeUint8ArrayValue(const std::vector<uint8_t> &src, std::vector<uint8_t> &dst);
96 static bool CheckAttributeLength(const uint8_t *curr, const uint8_t *end, uint32_t length);
97 std::map<AttributeKey, const std::vector<uint8_t>> map_;
98 };
99
Impl(const std::vector<uint8_t> & raw)100 Attributes::Impl::Impl(const std::vector<uint8_t> &raw)
101 {
102 std::map<Attributes::AttributeKey, const std::vector<uint8_t>> out;
103
104 const uint8_t *curr = &raw.front();
105 const uint8_t *end = &raw.back() + sizeof(uint8_t);
106 while (curr < end) {
107 if (curr + sizeof(uint32_t) + sizeof(uint32_t) < curr) { // in case of out of range
108 IAM_LOGE("out of pointer range");
109 return;
110 }
111
112 if (curr + sizeof(uint32_t) + sizeof(uint32_t) > end) {
113 IAM_LOGE("out of end range");
114 return;
115 }
116
117 uint32_t type;
118 if (memcpy_s(&type, sizeof(uint32_t), curr, sizeof(uint32_t)) != EOK) {
119 IAM_LOGE("type copy error");
120 return;
121 }
122 curr += sizeof(uint32_t);
123
124 uint32_t length;
125 if (memcpy_s(&length, sizeof(uint32_t), curr, sizeof(uint32_t)) != EOK) {
126 IAM_LOGE("length copy error");
127 return;
128 }
129 curr += sizeof(uint32_t);
130
131 if (!CheckAttributeLength(curr, end, length)) {
132 IAM_LOGE("check attribute length error");
133 return;
134 }
135
136 std::vector<uint8_t> value(length / sizeof(uint8_t));
137 if (length != 0 && memcpy_s(value.data(), value.size() * sizeof(uint8_t), curr, length) != EOK) {
138 IAM_LOGE("value copy error, length = %{public}u", length);
139 return;
140 }
141
142 auto ret = out.try_emplace(static_cast<Attributes::AttributeKey>(type), value);
143 if (!ret.second) {
144 IAM_LOGE("emplace pair error, type is %{public}u", type);
145 return;
146 }
147
148 if (out.size() > MAX_ATTR_COUNT) {
149 IAM_LOGE("emplace pair error, size reach max");
150 return;
151 }
152
153 IAM_LOGD("emplace pair success, type is %{public}u", type);
154 curr += length;
155 }
156
157 map_.swap(out);
158 }
159
Impl(Attributes::Impl && other)160 Attributes::Impl::Impl(Attributes::Impl &&other) noexcept : map_(std::move(other.map_))
161 {
162 }
163
operator =(Attributes::Impl && other)164 Attributes::Impl &Attributes::Impl::operator=(Attributes::Impl &&other) noexcept
165 {
166 map_ = std::move(other.map_);
167 return *this;
168 }
169
CheckAttributeLength(const uint8_t * curr,const uint8_t * end,uint32_t length)170 bool Attributes::Impl::CheckAttributeLength(const uint8_t *curr, const uint8_t *end, uint32_t length)
171 {
172 if (length % sizeof(uint8_t) != 0 || length > MAX_ATTR_LENGTH) {
173 IAM_LOGE("length format error, length = %{public}u", length);
174 return false;
175 }
176 if (length > end - curr) {
177 IAM_LOGE("length too big, length = %{public}u", length);
178 return false;
179 }
180 return true;
181 }
182
SetBoolValue(AttributeKey key,bool value)183 bool Attributes::Impl::SetBoolValue(AttributeKey key, bool value)
184 {
185 std::vector<uint8_t> dest;
186 if (!EncodeBoolValue(value, dest)) {
187 IAM_LOGE("EncodeBoolValue error");
188 return false;
189 }
190
191 if (map_.size() > MAX_ATTR_COUNT) {
192 IAM_LOGE("attrs size reach max");
193 return false;
194 }
195
196 auto ret = map_.try_emplace(key, dest);
197 return ret.second;
198 }
199
SetUint64Value(AttributeKey key,uint64_t value)200 bool Attributes::Impl::SetUint64Value(AttributeKey key, uint64_t value)
201 {
202 std::vector<uint8_t> dest;
203 if (!EncodeUint64Value(value, dest)) {
204 IAM_LOGE("EncodeUint64Value error");
205 return false;
206 }
207
208 if (map_.size() > MAX_ATTR_COUNT) {
209 IAM_LOGE("attrs size reach max");
210 return false;
211 }
212
213 auto ret = map_.try_emplace(key, dest);
214 return ret.second;
215 }
216
SetUint32Value(AttributeKey key,uint32_t value)217 bool Attributes::Impl::SetUint32Value(AttributeKey key, uint32_t value)
218 {
219 std::vector<uint8_t> dest;
220 if (!EncodeUint32Value(value, dest)) {
221 IAM_LOGE("EncodeUint32Value error");
222 return false;
223 }
224
225 if (map_.size() > MAX_ATTR_COUNT) {
226 IAM_LOGE("attrs size reach max");
227 return false;
228 }
229
230 auto ret = map_.try_emplace(key, dest);
231 return ret.second;
232 }
233
SetUint16Value(AttributeKey key,uint16_t value)234 bool Attributes::Impl::SetUint16Value(AttributeKey key, uint16_t value)
235 {
236 std::vector<uint8_t> dest;
237 if (!EncodeUint16Value(value, dest)) {
238 IAM_LOGE("EncodeUint16Value error");
239 return false;
240 }
241
242 if (map_.size() > MAX_ATTR_COUNT) {
243 IAM_LOGE("attrs size reach max");
244 return false;
245 }
246
247 auto ret = map_.try_emplace(key, dest);
248 return ret.second;
249 }
250
SetUint8Value(AttributeKey key,uint8_t value)251 bool Attributes::Impl::SetUint8Value(AttributeKey key, uint8_t value)
252 {
253 std::vector<uint8_t> dest;
254 if (!EncodeUint8Value(value, dest)) {
255 IAM_LOGE("EncodeUint8Value error");
256 return false;
257 }
258
259 if (map_.size() > MAX_ATTR_COUNT) {
260 IAM_LOGE("attrs size reach max");
261 return false;
262 }
263
264 auto ret = map_.try_emplace(key, dest);
265 return ret.second;
266 }
267
SetInt32Value(AttributeKey key,int32_t value)268 bool Attributes::Impl::SetInt32Value(AttributeKey key, int32_t value)
269 {
270 std::vector<uint8_t> dest;
271 if (!EncodeInt32Value(value, dest)) {
272 IAM_LOGE("EncodeInt32Value error");
273 return false;
274 }
275
276 if (map_.size() > MAX_ATTR_COUNT) {
277 IAM_LOGE("attrs size reach max");
278 return false;
279 }
280
281 auto ret = map_.try_emplace(key, dest);
282 return ret.second;
283 }
284
SetStringValue(AttributeKey key,const std::string & value)285 bool Attributes::Impl::SetStringValue(AttributeKey key, const std::string &value)
286 {
287 std::vector<uint8_t> dest;
288 if (!EncodeStringValue(value, dest)) {
289 IAM_LOGE("EncodeStringValue error");
290 return false;
291 }
292
293 if (map_.size() > MAX_ATTR_COUNT) {
294 IAM_LOGE("attrs size reach max");
295 return false;
296 }
297
298 auto ret = map_.try_emplace(key, dest);
299 return ret.second;
300 }
301
SetUint64ArrayValue(AttributeKey key,const std::vector<uint64_t> & value)302 bool Attributes::Impl::SetUint64ArrayValue(AttributeKey key, const std::vector<uint64_t> &value)
303 {
304 std::vector<uint8_t> dest;
305 if (!EncodeUint64ArrayValue(value, dest)) {
306 IAM_LOGE("EncodeUint64ArrayValue error");
307 return false;
308 }
309
310 if (map_.size() > MAX_ATTR_COUNT) {
311 IAM_LOGE("attrs size reach max");
312 return false;
313 }
314
315 auto ret = map_.try_emplace(key, dest);
316 return ret.second;
317 }
318
SetUint32ArrayValue(AttributeKey key,const std::vector<uint32_t> & value)319 bool Attributes::Impl::SetUint32ArrayValue(AttributeKey key, const std::vector<uint32_t> &value)
320 {
321 std::vector<uint8_t> dest;
322 if (!EncodeUint32ArrayValue(value, dest)) {
323 IAM_LOGE("EncodeUint32ArrayValue error");
324 return false;
325 }
326
327 if (map_.size() > MAX_ATTR_COUNT) {
328 IAM_LOGE("attrs size reach max");
329 return false;
330 }
331
332 auto ret = map_.try_emplace(key, dest);
333 return ret.second;
334 }
335
SetUint16ArrayValue(AttributeKey key,const std::vector<uint16_t> & value)336 bool Attributes::Impl::SetUint16ArrayValue(AttributeKey key, const std::vector<uint16_t> &value)
337 {
338 std::vector<uint8_t> dest;
339 if (!EncodeUint16ArrayValue(value, dest)) {
340 IAM_LOGE("EncodeUint16ArrayValue error");
341 return false;
342 }
343
344 if (map_.size() > MAX_ATTR_COUNT) {
345 IAM_LOGE("attrs size reach max");
346 return false;
347 }
348
349 auto ret = map_.try_emplace(key, dest);
350 return ret.second;
351 }
352
SetUint8ArrayValue(AttributeKey key,const std::vector<uint8_t> & value)353 bool Attributes::Impl::SetUint8ArrayValue(AttributeKey key, const std::vector<uint8_t> &value)
354 {
355 std::vector<uint8_t> dest;
356 if (!EncodeUint8ArrayValue(value, dest)) {
357 IAM_LOGE("EncodeUint8ArrayValue error");
358 return false;
359 }
360
361 if (map_.size() > MAX_ATTR_COUNT) {
362 IAM_LOGE("attrs size reach max");
363 return false;
364 }
365
366 auto ret = map_.try_emplace(key, value);
367 return ret.second;
368 }
369
SetAttributesValue(Attributes::AttributeKey key,const Attributes::Impl & value)370 bool Attributes::Impl::SetAttributesValue(Attributes::AttributeKey key, const Attributes::Impl &value)
371 {
372 std::vector<uint8_t> dest = value.Serialize();
373 if (dest.empty()) {
374 return false;
375 }
376
377 if (map_.size() > MAX_ATTR_COUNT) {
378 IAM_LOGE("attrs size reach max");
379 return false;
380 }
381
382 auto ret = map_.try_emplace(key, dest);
383 return ret.second;
384 }
385
GetBoolValue(AttributeKey key,bool & value) const386 bool Attributes::Impl::GetBoolValue(AttributeKey key, bool &value) const
387 {
388 auto iter = map_.find(key);
389 if (iter == map_.end()) {
390 return false;
391 }
392
393 if (!DecodeBoolValue(iter->second, value)) {
394 IAM_LOGE("DecodeBoolValue error");
395 return false;
396 }
397
398 return true;
399 }
400
GetUint64Value(AttributeKey key,uint64_t & value) const401 bool Attributes::Impl::GetUint64Value(AttributeKey key, uint64_t &value) const
402 {
403 auto iter = map_.find(key);
404 if (iter == map_.end()) {
405 return false;
406 }
407
408 if (!DecodeUint64Value(iter->second, value)) {
409 IAM_LOGE("DecodeUint64Value error");
410 return false;
411 }
412
413 return true;
414 }
415
GetUint32Value(AttributeKey key,uint32_t & value) const416 bool Attributes::Impl::GetUint32Value(AttributeKey key, uint32_t &value) const
417 {
418 auto iter = map_.find(key);
419 if (iter == map_.end()) {
420 return false;
421 }
422
423 if (!DecodeUint32Value(iter->second, value)) {
424 IAM_LOGE("DecodeUint32Value error");
425 return false;
426 }
427
428 return true;
429 }
430
GetUint16Value(AttributeKey key,uint16_t & value) const431 bool Attributes::Impl::GetUint16Value(AttributeKey key, uint16_t &value) const
432 {
433 auto iter = map_.find(key);
434 if (iter == map_.end()) {
435 return false;
436 }
437
438 if (!DecodeUint16Value(iter->second, value)) {
439 IAM_LOGE("DecodeUint16Value error");
440 return false;
441 }
442
443 return true;
444 }
445
GetUint8Value(AttributeKey key,uint8_t & value) const446 bool Attributes::Impl::GetUint8Value(AttributeKey key, uint8_t &value) const
447 {
448 auto iter = map_.find(key);
449 if (iter == map_.end()) {
450 return false;
451 }
452
453 if (!DecodeUint8Value(iter->second, value)) {
454 IAM_LOGE("DecodeUint8Value error");
455 return false;
456 }
457
458 return true;
459 }
460
GetInt32Value(AttributeKey key,int32_t & value) const461 bool Attributes::Impl::GetInt32Value(AttributeKey key, int32_t &value) const
462 {
463 auto iter = map_.find(key);
464 if (iter == map_.end()) {
465 return false;
466 }
467
468 if (!DecodeInt32Value(iter->second, value)) {
469 IAM_LOGE("DecodeInt32Value error");
470 return false;
471 }
472
473 return true;
474 }
475
GetStringValue(AttributeKey key,std::string & value) const476 bool Attributes::Impl::GetStringValue(AttributeKey key, std::string &value) const
477 {
478 auto iter = map_.find(key);
479 if (iter == map_.end()) {
480 return false;
481 }
482
483 if (!DecodeStringValue(iter->second, value)) {
484 IAM_LOGE("DecodeStringValue error");
485 return false;
486 }
487
488 return true;
489 }
490
GetUint64ArrayValue(AttributeKey key,std::vector<uint64_t> & value) const491 bool Attributes::Impl::GetUint64ArrayValue(AttributeKey key, std::vector<uint64_t> &value) const
492 {
493 auto iter = map_.find(key);
494 if (iter == map_.end()) {
495 return false;
496 }
497
498 if (!DecodeUint64ArrayValue(iter->second, value)) {
499 IAM_LOGE("DecodeUint64ArrayValue error");
500 return false;
501 }
502
503 return true;
504 }
505
GetUint32ArrayValue(AttributeKey key,std::vector<uint32_t> & value) const506 bool Attributes::Impl::GetUint32ArrayValue(AttributeKey key, std::vector<uint32_t> &value) const
507 {
508 auto iter = map_.find(key);
509 if (iter == map_.end()) {
510 return false;
511 }
512
513 if (!DecodeUint32ArrayValue(iter->second, value)) {
514 IAM_LOGE("DecodeUint32ArrayValue error");
515 return false;
516 }
517
518 return true;
519 }
520
GetUint16ArrayValue(AttributeKey key,std::vector<uint16_t> & value) const521 bool Attributes::Impl::GetUint16ArrayValue(AttributeKey key, std::vector<uint16_t> &value) const
522 {
523 auto iter = map_.find(key);
524 if (iter == map_.end()) {
525 return false;
526 }
527
528 if (!DecodeUint16ArrayValue(iter->second, value)) {
529 IAM_LOGE("DecodeUint16ArrayValue error");
530 return false;
531 }
532
533 return true;
534 }
535
GetUint8ArrayValue(AttributeKey key,std::vector<uint8_t> & value) const536 bool Attributes::Impl::GetUint8ArrayValue(AttributeKey key, std::vector<uint8_t> &value) const
537 {
538 auto iter = map_.find(key);
539 if (iter == map_.end()) {
540 return false;
541 }
542
543 if (!DecodeUint8ArrayValue(iter->second, value)) {
544 IAM_LOGE("DecodeUint8ArrayValue error");
545 return false;
546 }
547 return true;
548 }
549
GetAttributesValue(Attributes::AttributeKey key,Attributes::Impl & value) const550 bool Attributes::Impl::GetAttributesValue(Attributes::AttributeKey key, Attributes::Impl &value) const
551 {
552 auto iter = map_.find(key);
553 if (iter == map_.end()) {
554 return false;
555 }
556 Attributes::Impl out(iter->second);
557 value = std::move(out);
558 return true;
559 }
560
Serialize() const561 std::vector<uint8_t> Attributes::Impl::Serialize() const
562 {
563 uint32_t size = 0;
564 for (const auto &[key, value] : map_) {
565 size += sizeof(uint32_t) / sizeof(uint8_t);
566 size += sizeof(uint32_t) / sizeof(uint8_t);
567 size += value.size();
568 }
569 std::vector<uint8_t> buffer;
570 buffer.reserve(size);
571
572 for (const auto &[key, value] : map_) {
573 std::vector<uint8_t> type;
574 std::vector<uint8_t> length;
575 if (!EncodeUint32Value(key, type)) {
576 buffer.clear();
577 IAM_LOGE("EncodeUint32Value key error");
578 break;
579 }
580 if (!EncodeUint32Value(value.size() * sizeof(uint8_t), length)) {
581 buffer.clear();
582 IAM_LOGE("EncodeUint32Value value error");
583 break;
584 }
585 buffer.insert(buffer.end(), type.begin(), type.end());
586 buffer.insert(buffer.end(), length.begin(), length.end());
587 buffer.insert(buffer.end(), value.begin(), value.end());
588 }
589 return buffer;
590 }
591
GetKeys() const592 std::vector<Attributes::AttributeKey> Attributes::Impl::GetKeys() const
593 {
594 std::vector<Attributes::AttributeKey> keys;
595 keys.reserve(map_.size());
596 for (auto const &item : map_) {
597 keys.push_back(item.first);
598 }
599 return keys;
600 }
601
EncodeBoolValue(bool src,std::vector<uint8_t> & dst)602 bool Attributes::Impl::EncodeBoolValue(bool src, std::vector<uint8_t> &dst)
603 {
604 std::vector<uint8_t> out(1); // only 1
605 out[0] = src ? 1 : 0;
606
607 dst.swap(out);
608 return true;
609 }
610
EncodeUint64Value(uint64_t src,std::vector<uint8_t> & dst)611 bool Attributes::Impl::EncodeUint64Value(uint64_t src, std::vector<uint8_t> &dst)
612 {
613 std::vector<uint8_t> out(sizeof(uint64_t) / sizeof(uint8_t));
614 if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
615 return false;
616 }
617 dst.swap(out);
618 return true;
619 }
620
EncodeUint32Value(uint32_t src,std::vector<uint8_t> & dst)621 bool Attributes::Impl::EncodeUint32Value(uint32_t src, std::vector<uint8_t> &dst)
622 {
623 std::vector<uint8_t> out(sizeof(uint32_t) / sizeof(uint8_t));
624 if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
625 return false;
626 }
627 dst.swap(out);
628 return true;
629 }
630
EncodeUint16Value(uint16_t src,std::vector<uint8_t> & dst)631 bool Attributes::Impl::EncodeUint16Value(uint16_t src, std::vector<uint8_t> &dst)
632 {
633 std::vector<uint8_t> out(sizeof(uint16_t) / sizeof(uint8_t));
634 if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
635 return false;
636 }
637 dst.swap(out);
638 return true;
639 }
640
EncodeUint8Value(uint8_t src,std::vector<uint8_t> & dst)641 bool Attributes::Impl::EncodeUint8Value(uint8_t src, std::vector<uint8_t> &dst)
642 {
643 std::vector<uint8_t> out(1);
644 out[0] = src;
645 dst.swap(out);
646 return true;
647 }
648
EncodeInt32Value(int32_t src,std::vector<uint8_t> & dst)649 bool Attributes::Impl::EncodeInt32Value(int32_t src, std::vector<uint8_t> &dst)
650 {
651 std::vector<uint8_t> out(sizeof(int32_t) / sizeof(uint8_t));
652 if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
653 return false;
654 }
655 dst.swap(out);
656 return true;
657 }
658
EncodeStringValue(const std::string & src,std::vector<uint8_t> & dst)659 bool Attributes::Impl::EncodeStringValue(const std::string &src, std::vector<uint8_t> &dst)
660 {
661 if (src.size() > MAX_ATTR_LENGTH) {
662 return false;
663 }
664
665 std::vector<uint8_t> out(src.begin(), src.end());
666 out.push_back(0);
667 dst.swap(out);
668 return true;
669 }
670
EncodeUint64ArrayValue(const std::vector<uint64_t> & src,std::vector<uint8_t> & dst)671 bool Attributes::Impl::EncodeUint64ArrayValue(const std::vector<uint64_t> &src, std::vector<uint8_t> &dst)
672 {
673 auto size = src.size() * (sizeof(uint64_t) / sizeof(uint8_t));
674 if (size > MAX_ATTR_LENGTH) {
675 return false;
676 }
677
678 std::vector<uint8_t> out(size);
679
680 if (!src.empty() &&
681 memcpy_s(out.data(), out.size() * sizeof(uint8_t), src.data(), src.size() * sizeof(uint64_t)) != EOK) {
682 return false;
683 }
684
685 dst.swap(out);
686 return true;
687 }
688
EncodeUint32ArrayValue(const std::vector<uint32_t> & src,std::vector<uint8_t> & dst)689 bool Attributes::Impl::EncodeUint32ArrayValue(const std::vector<uint32_t> &src, std::vector<uint8_t> &dst)
690 {
691 auto size = src.size() * (sizeof(uint32_t) / sizeof(uint8_t));
692 if (size > MAX_ATTR_LENGTH) {
693 return false;
694 }
695
696 std::vector<uint8_t> out(size);
697
698 if (!src.empty() &&
699 memcpy_s(out.data(), out.size() * sizeof(uint8_t), src.data(), src.size() * sizeof(uint32_t)) != EOK) {
700 return false;
701 }
702 dst.swap(out);
703 return true;
704 }
705
EncodeUint16ArrayValue(const std::vector<uint16_t> & src,std::vector<uint8_t> & dst)706 bool Attributes::Impl::EncodeUint16ArrayValue(const std::vector<uint16_t> &src, std::vector<uint8_t> &dst)
707 {
708 auto size = src.size() * (sizeof(uint16_t) / sizeof(uint8_t));
709 if (size > MAX_ATTR_LENGTH) {
710 return false;
711 }
712
713 std::vector<uint8_t> out(size);
714
715 if (!src.empty() &&
716 memcpy_s(out.data(), out.size() * sizeof(uint8_t), src.data(), src.size() * sizeof(uint16_t)) != EOK) {
717 return false;
718 }
719 dst.swap(out);
720 return true;
721 }
722
EncodeUint8ArrayValue(const std::vector<uint8_t> & src,std::vector<uint8_t> & dst)723 bool Attributes::Impl::EncodeUint8ArrayValue(const std::vector<uint8_t> &src, std::vector<uint8_t> &dst)
724 {
725 if (src.size() > MAX_ATTR_LENGTH) {
726 return false;
727 }
728
729 std::vector<uint8_t> out(src);
730 dst.swap(out);
731 return true;
732 }
733
DecodeBoolValue(const std::vector<uint8_t> & src,bool & dst)734 bool Attributes::Impl::DecodeBoolValue(const std::vector<uint8_t> &src, bool &dst)
735 {
736 if (src.size() != 1) {
737 return false;
738 }
739 dst = (src[0] == 1);
740 return true;
741 }
742
DecodeUint64Value(const std::vector<uint8_t> & src,uint64_t & dst)743 bool Attributes::Impl::DecodeUint64Value(const std::vector<uint8_t> &src, uint64_t &dst)
744 {
745 if (src.size() * sizeof(uint8_t) != sizeof(uint64_t)) {
746 return false;
747 }
748
749 if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
750 return false;
751 }
752 return true;
753 }
754
DecodeUint32Value(const std::vector<uint8_t> & src,uint32_t & dst)755 bool Attributes::Impl::DecodeUint32Value(const std::vector<uint8_t> &src, uint32_t &dst)
756 {
757 if (src.size() * sizeof(uint8_t) != sizeof(uint32_t)) {
758 return false;
759 }
760 if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
761 return false;
762 }
763 return true;
764 }
765
DecodeUint16Value(const std::vector<uint8_t> & src,uint16_t & dst)766 bool Attributes::Impl::DecodeUint16Value(const std::vector<uint8_t> &src, uint16_t &dst)
767 {
768 if (src.size() * sizeof(uint8_t) != sizeof(uint16_t)) {
769 return false;
770 }
771 if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
772 return false;
773 }
774 return true;
775 }
776
DecodeUint8Value(const std::vector<uint8_t> & src,uint8_t & dst)777 bool Attributes::Impl::DecodeUint8Value(const std::vector<uint8_t> &src, uint8_t &dst)
778 {
779 if (src.size() != 1) {
780 return false;
781 }
782 dst = src[0];
783 return true;
784 }
785
DecodeInt32Value(const std::vector<uint8_t> & src,int32_t & dst)786 bool Attributes::Impl::DecodeInt32Value(const std::vector<uint8_t> &src, int32_t &dst)
787 {
788 if (src.size() * sizeof(uint8_t) != sizeof(int32_t)) {
789 return false;
790 }
791 if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
792 return false;
793 }
794 return true;
795 }
796
DecodeStringValue(const std::vector<uint8_t> & src,std::string & dst)797 bool Attributes::Impl::DecodeStringValue(const std::vector<uint8_t> &src, std::string &dst)
798 {
799 if (src.empty()) {
800 return false;
801 }
802
803 if (src.back() != 0) {
804 return false;
805 }
806
807 std::string out(static_cast<const char *>(static_cast<const void *>(src.data())));
808
809 dst.swap(out);
810 return true;
811 }
812
DecodeUint64ArrayValue(const std::vector<uint8_t> & src,std::vector<uint64_t> & dst)813 bool Attributes::Impl::DecodeUint64ArrayValue(const std::vector<uint8_t> &src, std::vector<uint64_t> &dst)
814 {
815 if (src.size() % (sizeof(uint64_t) / sizeof(uint8_t)) != 0) {
816 return false;
817 }
818
819 std::vector<uint64_t> out(src.size() / (sizeof(uint64_t) / sizeof(uint8_t)));
820
821 if (!out.empty() &&
822 memcpy_s(out.data(), out.size() * sizeof(uint64_t), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
823 return false;
824 }
825
826 dst.swap(out);
827 return true;
828 }
829
DecodeUint32ArrayValue(const std::vector<uint8_t> & src,std::vector<uint32_t> & dst)830 bool Attributes::Impl::DecodeUint32ArrayValue(const std::vector<uint8_t> &src, std::vector<uint32_t> &dst)
831 {
832 if (src.size() % (sizeof(uint32_t) / sizeof(uint8_t)) != 0) {
833 return false;
834 }
835
836 std::vector<uint32_t> out(src.size() / (sizeof(uint32_t) / sizeof(uint8_t)));
837
838 if (!out.empty() &&
839 memcpy_s(out.data(), out.size() * sizeof(uint32_t), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
840 return false;
841 }
842
843 dst.swap(out);
844 return true;
845 }
846
DecodeUint16ArrayValue(const std::vector<uint8_t> & src,std::vector<uint16_t> & dst)847 bool Attributes::Impl::DecodeUint16ArrayValue(const std::vector<uint8_t> &src, std::vector<uint16_t> &dst)
848 {
849 if (src.size() % (sizeof(uint32_t) / sizeof(uint16_t)) != 0) {
850 return false;
851 }
852
853 std::vector<uint16_t> out(src.size() / (sizeof(uint16_t) / sizeof(uint8_t)));
854
855 if (!out.empty() &&
856 memcpy_s(out.data(), out.size() * sizeof(uint16_t), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
857 return false;
858 }
859
860 dst.swap(out);
861 return true;
862 }
863
DecodeUint8ArrayValue(const std::vector<uint8_t> & src,std::vector<uint8_t> & dst)864 bool Attributes::Impl::DecodeUint8ArrayValue(const std::vector<uint8_t> &src, std::vector<uint8_t> &dst)
865 {
866 std::vector<uint8_t> out(src);
867 dst.swap(out);
868 return true;
869 }
870
Attributes()871 Attributes::Attributes() : impl_(new (std::nothrow) Attributes::Impl())
872 {
873 }
874
Attributes(const std::vector<uint8_t> & raw)875 Attributes::Attributes(const std::vector<uint8_t> &raw) : impl_(new (std::nothrow) Attributes::Impl(raw))
876 {
877 }
878
Attributes(Attributes && other)879 Attributes::Attributes(Attributes &&other) noexcept : impl_(std::move(other.impl_))
880 {
881 }
882
operator =(Attributes && other)883 Attributes &Attributes::operator=(Attributes &&other) noexcept
884 {
885 impl_ = std::move(other.impl_);
886 return *this;
887 }
888
~Attributes()889 Attributes::~Attributes()
890 {
891 impl_ = nullptr;
892 };
893
SetBoolValue(AttributeKey key,bool value)894 bool Attributes::SetBoolValue(AttributeKey key, bool value)
895 {
896 if (!impl_) {
897 return false;
898 }
899 return impl_->SetBoolValue(key, value);
900 }
901
SetUint64Value(AttributeKey key,uint64_t value)902 bool Attributes::SetUint64Value(AttributeKey key, uint64_t value)
903 {
904 if (!impl_) {
905 return false;
906 }
907 return impl_->SetUint64Value(key, value);
908 }
909
SetUint32Value(AttributeKey key,uint32_t value)910 bool Attributes::SetUint32Value(AttributeKey key, uint32_t value)
911 {
912 if (!impl_) {
913 return false;
914 }
915 return impl_->SetUint32Value(key, value);
916 }
917
SetUint16Value(AttributeKey key,uint16_t value)918 bool Attributes::SetUint16Value(AttributeKey key, uint16_t value)
919 {
920 if (!impl_) {
921 return false;
922 }
923 return impl_->SetUint16Value(key, value);
924 }
925
SetUint8Value(AttributeKey key,uint8_t value)926 bool Attributes::SetUint8Value(AttributeKey key, uint8_t value)
927 {
928 if (!impl_) {
929 return false;
930 }
931 return impl_->SetUint8Value(key, value);
932 }
933
SetInt32Value(AttributeKey key,int32_t value)934 bool Attributes::SetInt32Value(AttributeKey key, int32_t value)
935 {
936 if (!impl_) {
937 return false;
938 }
939 return impl_->SetInt32Value(key, value);
940 }
941
SetStringValue(AttributeKey key,const std::string & value)942 bool Attributes::SetStringValue(AttributeKey key, const std::string &value)
943 {
944 if (!impl_) {
945 return false;
946 }
947 return impl_->SetStringValue(key, value);
948 }
949
SetUint64ArrayValue(AttributeKey key,const std::vector<uint64_t> & value)950 bool Attributes::SetUint64ArrayValue(AttributeKey key, const std::vector<uint64_t> &value)
951 {
952 if (!impl_) {
953 return false;
954 }
955 return impl_->SetUint64ArrayValue(key, value);
956 }
957
SetUint32ArrayValue(AttributeKey key,const std::vector<uint32_t> & value)958 bool Attributes::SetUint32ArrayValue(AttributeKey key, const std::vector<uint32_t> &value)
959 {
960 if (!impl_) {
961 return false;
962 }
963 return impl_->SetUint32ArrayValue(key, value);
964 }
965
SetUint16ArrayValue(AttributeKey key,const std::vector<uint16_t> & value)966 bool Attributes::SetUint16ArrayValue(AttributeKey key, const std::vector<uint16_t> &value)
967 {
968 if (!impl_) {
969 return false;
970 }
971 return impl_->SetUint16ArrayValue(key, value);
972 }
973
SetUint8ArrayValue(AttributeKey key,const std::vector<uint8_t> & value)974 bool Attributes::SetUint8ArrayValue(AttributeKey key, const std::vector<uint8_t> &value)
975 {
976 if (!impl_) {
977 return false;
978 }
979 return impl_->SetUint8ArrayValue(key, value);
980 }
981
SetAttributesValue(AttributeKey key,const Attributes & value)982 bool Attributes::SetAttributesValue(AttributeKey key, const Attributes &value)
983 {
984 if (!impl_) {
985 return false;
986 }
987 return impl_->SetAttributesValue(key, *value.impl_);
988 }
989
GetBoolValue(AttributeKey key,bool & value) const990 bool Attributes::GetBoolValue(AttributeKey key, bool &value) const
991 {
992 if (!impl_) {
993 return false;
994 }
995 return impl_->GetBoolValue(key, value);
996 }
997
GetUint64Value(AttributeKey key,uint64_t & value) const998 bool Attributes::GetUint64Value(AttributeKey key, uint64_t &value) const
999 {
1000 if (!impl_) {
1001 return false;
1002 }
1003 return impl_->GetUint64Value(key, value);
1004 }
1005
GetUint32Value(AttributeKey key,uint32_t & value) const1006 bool Attributes::GetUint32Value(AttributeKey key, uint32_t &value) const
1007 {
1008 if (!impl_) {
1009 return false;
1010 }
1011 return impl_->GetUint32Value(key, value);
1012 }
1013
GetUint16Value(AttributeKey key,uint16_t & value) const1014 bool Attributes::GetUint16Value(AttributeKey key, uint16_t &value) const
1015 {
1016 if (!impl_) {
1017 return false;
1018 }
1019 return impl_->GetUint16Value(key, value);
1020 }
1021
GetUint8Value(AttributeKey key,uint8_t & value) const1022 bool Attributes::GetUint8Value(AttributeKey key, uint8_t &value) const
1023 {
1024 if (!impl_) {
1025 return false;
1026 }
1027 return impl_->GetUint8Value(key, value);
1028 }
1029
GetInt32Value(AttributeKey key,int32_t & value) const1030 bool Attributes::GetInt32Value(AttributeKey key, int32_t &value) const
1031 {
1032 if (!impl_) {
1033 return false;
1034 }
1035 return impl_->GetInt32Value(key, value);
1036 }
1037
GetStringValue(AttributeKey key,std::string & value) const1038 bool Attributes::GetStringValue(AttributeKey key, std::string &value) const
1039 {
1040 if (!impl_) {
1041 return false;
1042 }
1043 return impl_->GetStringValue(key, value);
1044 }
1045
GetUint64ArrayValue(AttributeKey key,std::vector<uint64_t> & value) const1046 bool Attributes::GetUint64ArrayValue(AttributeKey key, std::vector<uint64_t> &value) const
1047 {
1048 if (!impl_) {
1049 return false;
1050 }
1051 return impl_->GetUint64ArrayValue(key, value);
1052 }
1053
GetUint32ArrayValue(AttributeKey key,std::vector<uint32_t> & value) const1054 bool Attributes::GetUint32ArrayValue(AttributeKey key, std::vector<uint32_t> &value) const
1055 {
1056 if (!impl_) {
1057 return false;
1058 }
1059 return impl_->GetUint32ArrayValue(key, value);
1060 }
1061
GetUint16ArrayValue(AttributeKey key,std::vector<uint16_t> & value) const1062 bool Attributes::GetUint16ArrayValue(AttributeKey key, std::vector<uint16_t> &value) const
1063 {
1064 if (!impl_) {
1065 return false;
1066 }
1067 return impl_->GetUint16ArrayValue(key, value);
1068 }
1069
GetUint8ArrayValue(AttributeKey key,std::vector<uint8_t> & value) const1070 bool Attributes::GetUint8ArrayValue(AttributeKey key, std::vector<uint8_t> &value) const
1071 {
1072 if (!impl_) {
1073 return false;
1074 }
1075 return impl_->GetUint8ArrayValue(key, value);
1076 }
1077
GetAttributesValue(AttributeKey key,Attributes & value) const1078 bool Attributes::GetAttributesValue(AttributeKey key, Attributes &value) const
1079 {
1080 if (!impl_) {
1081 return false;
1082 }
1083 return impl_->GetAttributesValue(key, *value.impl_);
1084 }
1085
Serialize() const1086 std::vector<uint8_t> Attributes::Serialize() const
1087 {
1088 if (!impl_) {
1089 return {};
1090 }
1091 return impl_->Serialize();
1092 }
1093
GetKeys() const1094 std::vector<Attributes::AttributeKey> Attributes::GetKeys() const
1095 {
1096 if (!impl_) {
1097 return {};
1098 }
1099 return impl_->GetKeys();
1100 }
1101 } // namespace UserAuth
1102 } // namespace UserIam
1103 } // namespace OHOS
1104