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_TAG "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 SetInt64Value(AttributeKey key, int64_t value);
49     bool SetStringValue(AttributeKey key, const std::string &value);
50     bool SetAttributesValue(AttributeKey key, const Impl &array);
51     bool SetUint64ArrayValue(AttributeKey key, const std::vector<uint64_t> &value);
52     bool SetUint32ArrayValue(AttributeKey key, const std::vector<uint32_t> &value);
53     bool SetUint16ArrayValue(AttributeKey key, const std::vector<uint16_t> &value);
54     bool SetUint8ArrayValue(AttributeKey key, const std::vector<uint8_t> &value);
55     bool SetInt32ArrayValue(AttributeKey key, const std::vector<int32_t> &value);
56 
57     bool GetBoolValue(AttributeKey key, bool &value) const;
58     bool GetUint64Value(AttributeKey key, uint64_t &value) const;
59     bool GetUint32Value(AttributeKey key, uint32_t &value) const;
60     bool GetUint16Value(AttributeKey key, uint16_t &value) const;
61     bool GetUint8Value(AttributeKey key, uint8_t &value) const;
62     bool GetInt32Value(AttributeKey key, int32_t &value) const;
63     bool GetInt64Value(AttributeKey key, int64_t &value) const;
64     bool GetStringValue(AttributeKey key, std::string &value) const;
65     bool GetUint64ArrayValue(AttributeKey key, std::vector<uint64_t> &value) const;
66     bool GetUint32ArrayValue(AttributeKey key, std::vector<uint32_t> &value) const;
67     bool GetUint16ArrayValue(AttributeKey key, std::vector<uint16_t> &value) const;
68     bool GetUint8ArrayValue(AttributeKey key, std::vector<uint8_t> &value) const;
69     bool GetInt32ArrayValue(AttributeKey key, std::vector<int32_t> &value) const;
70     bool GetAttributesValue(AttributeKey key, Impl &array) const;
71     std::vector<uint8_t> Serialize() const;
72     std::vector<AttributeKey> GetKeys() const;
73 
74     static bool EncodeUint32Value(uint32_t src, std::vector<uint8_t> &dst);
75     static bool DecodeUint32Value(const std::vector<uint8_t> &src, uint32_t &dst);
76 
77 private:
78     static constexpr uint32_t MAX_ATTR_LENGTH = 81920;
79     static constexpr uint32_t MAX_ATTR_COUNT = 512;
80     static bool EncodeBoolValue(bool src, std::vector<uint8_t> &dst);
81     static bool EncodeUint64Value(uint64_t src, std::vector<uint8_t> &dst);
82     static bool EncodeUint16Value(uint16_t src, std::vector<uint8_t> &dst);
83     static bool EncodeUint8Value(uint8_t src, std::vector<uint8_t> &dst);
84     static bool EncodeInt32Value(int32_t src, std::vector<uint8_t> &dst);
85     static bool EncodeInt64Value(int64_t src, std::vector<uint8_t> &dst);
86     static bool EncodeStringValue(const std::string &src, std::vector<uint8_t> &dst);
87     static bool EncodeUint64ArrayValue(const std::vector<uint64_t> &src, std::vector<uint8_t> &dst);
88     static bool EncodeInt32ArrayValue(const std::vector<int32_t> &src, std::vector<uint8_t> &dst);
89     static bool EncodeUint32ArrayValue(const std::vector<uint32_t> &src, std::vector<uint8_t> &dst);
90     static bool EncodeUint16ArrayValue(const std::vector<uint16_t> &src, std::vector<uint8_t> &dst);
91     static bool EncodeUint8ArrayValue(const std::vector<uint8_t> &src, std::vector<uint8_t> &dst);
92 
93     static bool DecodeBoolValue(const std::vector<uint8_t> &src, bool &dst);
94     static bool DecodeUint64Value(const std::vector<uint8_t> &src, uint64_t &dst);
95     static bool DecodeUint16Value(const std::vector<uint8_t> &src, uint16_t &dst);
96     static bool DecodeUint8Value(const std::vector<uint8_t> &src, uint8_t &dst);
97     static bool DecodeInt32Value(const std::vector<uint8_t> &src, int32_t &dst);
98     static bool DecodeInt64Value(const std::vector<uint8_t> &src, int64_t &dst);
99     static bool DecodeStringValue(const std::vector<uint8_t> &src, std::string &dst);
100     static bool DecodeUint64ArrayValue(const std::vector<uint8_t> &src, std::vector<uint64_t> &dst);
101     static bool DecodeUint32ArrayValue(const std::vector<uint8_t> &src, std::vector<uint32_t> &dst);
102     static bool DecodeUint16ArrayValue(const std::vector<uint8_t> &src, std::vector<uint16_t> &dst);
103     static bool DecodeUint8ArrayValue(const std::vector<uint8_t> &src, std::vector<uint8_t> &dst);
104     static bool DecodeInt32ArrayValue(const std::vector<uint8_t> &src, std::vector<int32_t> &dst);
105     static bool CheckAttributeLength(const uint8_t *curr, const uint8_t *end, uint32_t length);
106     std::map<AttributeKey, std::vector<uint8_t>> map_;
107 };
108 
Impl(const std::vector<uint8_t> & raw)109 Attributes::Impl::Impl(const std::vector<uint8_t> &raw)
110 {
111     std::map<Attributes::AttributeKey, std::vector<uint8_t>> out;
112 
113     const uint8_t *curr = &raw.front();
114     const uint8_t *end = &raw.back() + sizeof(uint8_t);
115     while (curr < end) {
116         if (curr + sizeof(uint32_t) + sizeof(uint32_t) < curr) { // in case of out of range
117             IAM_LOGE("out of pointer range");
118             return;
119         }
120 
121         if (curr + sizeof(uint32_t) + sizeof(uint32_t) > end) {
122             IAM_LOGE("out of end range");
123             return;
124         }
125 
126         uint32_t type;
127         if (memcpy_s(&type, sizeof(uint32_t), curr, sizeof(uint32_t)) != EOK) {
128             IAM_LOGE("type copy error");
129             return;
130         }
131         curr += sizeof(uint32_t);
132 
133         uint32_t length;
134         if (memcpy_s(&length, sizeof(uint32_t), curr, sizeof(uint32_t)) != EOK) {
135             IAM_LOGE("length copy error");
136             return;
137         }
138         curr += sizeof(uint32_t);
139 
140         if (!CheckAttributeLength(curr, end, length)) {
141             IAM_LOGE("check attribute length error");
142             return;
143         }
144 
145         std::vector<uint8_t> value(length / sizeof(uint8_t));
146         if (length != 0 && memcpy_s(value.data(), value.size() * sizeof(uint8_t), curr, length) != EOK) {
147             IAM_LOGE("value copy error, length = %{public}u", length);
148             return;
149         }
150 
151         auto ret = out.insert_or_assign(static_cast<Attributes::AttributeKey>(type), value);
152         if (!ret.second) {
153             IAM_LOGE("insert_or_assign pair error, type is %{public}u", type);
154             return;
155         }
156 
157         if (out.size() > MAX_ATTR_COUNT) {
158             IAM_LOGE("insert_or_assign pair error, size reach max");
159             return;
160         }
161 
162         IAM_LOGD("insert_or_assign pair success, type is %{public}u", type);
163         curr += length;
164     }
165 
166     map_.swap(out);
167 }
168 
Impl(Attributes::Impl && other)169 Attributes::Impl::Impl(Attributes::Impl &&other) noexcept : map_(std::move(other.map_))
170 {
171 }
172 
operator =(Attributes::Impl && other)173 Attributes::Impl &Attributes::Impl::operator=(Attributes::Impl &&other) noexcept
174 {
175     map_ = std::move(other.map_);
176     return *this;
177 }
178 
CheckAttributeLength(const uint8_t * curr,const uint8_t * end,uint32_t length)179 bool Attributes::Impl::CheckAttributeLength(const uint8_t *curr, const uint8_t *end, uint32_t length)
180 {
181     if (length % sizeof(uint8_t) != 0 || length > MAX_ATTR_LENGTH) {
182         IAM_LOGE("length format error, length = %{public}u", length);
183         return false;
184     }
185     if (length > end - curr) {
186         IAM_LOGE("length too big, length = %{public}u", length);
187         return false;
188     }
189     return true;
190 }
191 
SetBoolValue(AttributeKey key,bool value)192 bool Attributes::Impl::SetBoolValue(AttributeKey key, bool value)
193 {
194     std::vector<uint8_t> dest;
195     if (!EncodeBoolValue(value, dest)) {
196         IAM_LOGE("EncodeBoolValue error");
197         return false;
198     }
199 
200     if (map_.size() > MAX_ATTR_COUNT) {
201         IAM_LOGE("attrs size reach max");
202         return false;
203     }
204 
205     map_.insert_or_assign(key, dest);
206     return true;
207 }
208 
SetUint64Value(AttributeKey key,uint64_t value)209 bool Attributes::Impl::SetUint64Value(AttributeKey key, uint64_t value)
210 {
211     std::vector<uint8_t> dest;
212     if (!EncodeUint64Value(value, dest)) {
213         IAM_LOGE("EncodeUint64Value error");
214         return false;
215     }
216 
217     if (map_.size() > MAX_ATTR_COUNT) {
218         IAM_LOGE("attrs size reach max");
219         return false;
220     }
221 
222     map_.insert_or_assign(key, dest);
223     return true;
224 }
225 
SetUint32Value(AttributeKey key,uint32_t value)226 bool Attributes::Impl::SetUint32Value(AttributeKey key, uint32_t value)
227 {
228     std::vector<uint8_t> dest;
229     if (!EncodeUint32Value(value, dest)) {
230         IAM_LOGE("EncodeUint32Value error");
231         return false;
232     }
233 
234     if (map_.size() > MAX_ATTR_COUNT) {
235         IAM_LOGE("attrs size reach max");
236         return false;
237     }
238 
239     map_.insert_or_assign(key, dest);
240     return true;
241 }
242 
SetUint16Value(AttributeKey key,uint16_t value)243 bool Attributes::Impl::SetUint16Value(AttributeKey key, uint16_t value)
244 {
245     std::vector<uint8_t> dest;
246     if (!EncodeUint16Value(value, dest)) {
247         IAM_LOGE("EncodeUint16Value error");
248         return false;
249     }
250 
251     if (map_.size() > MAX_ATTR_COUNT) {
252         IAM_LOGE("attrs size reach max");
253         return false;
254     }
255 
256     map_.insert_or_assign(key, dest);
257     return true;
258 }
259 
SetUint8Value(AttributeKey key,uint8_t value)260 bool Attributes::Impl::SetUint8Value(AttributeKey key, uint8_t value)
261 {
262     std::vector<uint8_t> dest;
263     if (!EncodeUint8Value(value, dest)) {
264         IAM_LOGE("EncodeUint8Value error");
265         return false;
266     }
267 
268     if (map_.size() > MAX_ATTR_COUNT) {
269         IAM_LOGE("attrs size reach max");
270         return false;
271     }
272 
273     map_[key] = dest;
274     return true;
275 }
276 
SetInt32Value(AttributeKey key,int32_t value)277 bool Attributes::Impl::SetInt32Value(AttributeKey key, int32_t value)
278 {
279     std::vector<uint8_t> dest;
280     if (!EncodeInt32Value(value, dest)) {
281         IAM_LOGE("EncodeInt32Value error");
282         return false;
283     }
284 
285     if (map_.size() > MAX_ATTR_COUNT) {
286         IAM_LOGE("attrs size reach max");
287         return false;
288     }
289 
290     map_.insert_or_assign(key, dest);
291     return true;
292 }
293 
SetInt64Value(AttributeKey key,int64_t value)294 bool Attributes::Impl::SetInt64Value(AttributeKey key, int64_t value)
295 {
296     std::vector<uint8_t> dest;
297     if (!EncodeInt64Value(value, dest)) {
298         IAM_LOGE("EncodeInt64Value error");
299         return false;
300     }
301 
302     if (map_.size() > MAX_ATTR_COUNT) {
303         IAM_LOGE("attrs size reach max");
304         return false;
305     }
306 
307     auto ret = map_.try_emplace(key, dest);
308     return ret.second;
309 }
310 
SetStringValue(AttributeKey key,const std::string & value)311 bool Attributes::Impl::SetStringValue(AttributeKey key, const std::string &value)
312 {
313     std::vector<uint8_t> dest;
314     if (!EncodeStringValue(value, dest)) {
315         IAM_LOGE("EncodeStringValue error");
316         return false;
317     }
318 
319     if (map_.size() > MAX_ATTR_COUNT) {
320         IAM_LOGE("attrs size reach max");
321         return false;
322     }
323 
324     map_.insert_or_assign(key, dest);
325     return true;
326 }
327 
SetUint64ArrayValue(AttributeKey key,const std::vector<uint64_t> & value)328 bool Attributes::Impl::SetUint64ArrayValue(AttributeKey key, const std::vector<uint64_t> &value)
329 {
330     std::vector<uint8_t> dest;
331     if (!EncodeUint64ArrayValue(value, dest)) {
332         IAM_LOGE("EncodeUint64ArrayValue error");
333         return false;
334     }
335 
336     if (map_.size() > MAX_ATTR_COUNT) {
337         IAM_LOGE("attrs size reach max");
338         return false;
339     }
340 
341     map_.insert_or_assign(key, dest);
342     return true;
343 }
344 
SetUint32ArrayValue(AttributeKey key,const std::vector<uint32_t> & value)345 bool Attributes::Impl::SetUint32ArrayValue(AttributeKey key, const std::vector<uint32_t> &value)
346 {
347     std::vector<uint8_t> dest;
348     if (!EncodeUint32ArrayValue(value, dest)) {
349         IAM_LOGE("EncodeUint32ArrayValue error");
350         return false;
351     }
352 
353     if (map_.size() > MAX_ATTR_COUNT) {
354         IAM_LOGE("attrs size reach max");
355         return false;
356     }
357 
358     map_.insert_or_assign(key, dest);
359     return true;
360 }
361 
SetInt32ArrayValue(AttributeKey key,const std::vector<int32_t> & value)362 bool Attributes::Impl::SetInt32ArrayValue(AttributeKey key, const std::vector<int32_t> &value)
363 {
364     std::vector<uint8_t> dest;
365     if (!EncodeInt32ArrayValue(value, dest)) {
366         IAM_LOGE("EncodeUint32ArrayValue error");
367         return false;
368     }
369 
370     if (map_.size() > MAX_ATTR_COUNT) {
371         IAM_LOGE("attrs size reach max");
372         return false;
373     }
374 
375     map_.insert_or_assign(key, dest);
376     return true;
377 }
378 
SetUint16ArrayValue(AttributeKey key,const std::vector<uint16_t> & value)379 bool Attributes::Impl::SetUint16ArrayValue(AttributeKey key, const std::vector<uint16_t> &value)
380 {
381     std::vector<uint8_t> dest;
382     if (!EncodeUint16ArrayValue(value, dest)) {
383         IAM_LOGE("EncodeUint16ArrayValue error");
384         return false;
385     }
386 
387     if (map_.size() > MAX_ATTR_COUNT) {
388         IAM_LOGE("attrs size reach max");
389         return false;
390     }
391 
392     map_.insert_or_assign(key, dest);
393     return true;
394 }
395 
SetUint8ArrayValue(AttributeKey key,const std::vector<uint8_t> & value)396 bool Attributes::Impl::SetUint8ArrayValue(AttributeKey key, const std::vector<uint8_t> &value)
397 {
398     std::vector<uint8_t> dest;
399     if (!EncodeUint8ArrayValue(value, dest)) {
400         IAM_LOGE("EncodeUint8ArrayValue error");
401         return false;
402     }
403 
404     if (map_.size() > MAX_ATTR_COUNT) {
405         IAM_LOGE("attrs size reach max");
406         return false;
407     }
408 
409     map_.insert_or_assign(key, dest);
410     return true;
411 }
412 
SetAttributesValue(Attributes::AttributeKey key,const Attributes::Impl & value)413 bool Attributes::Impl::SetAttributesValue(Attributes::AttributeKey key, const Attributes::Impl &value)
414 {
415     std::vector<uint8_t> dest = value.Serialize();
416     if (dest.empty()) {
417         return false;
418     }
419 
420     if (map_.size() > MAX_ATTR_COUNT) {
421         IAM_LOGE("attrs size reach max");
422         return false;
423     }
424 
425     map_.insert_or_assign(key, dest);
426     return true;
427 }
428 
GetBoolValue(AttributeKey key,bool & value) const429 bool Attributes::Impl::GetBoolValue(AttributeKey key, bool &value) const
430 {
431     auto iter = map_.find(key);
432     if (iter == map_.end()) {
433         return false;
434     }
435 
436     if (!DecodeBoolValue(iter->second, value)) {
437         IAM_LOGE("DecodeBoolValue error");
438         return false;
439     }
440 
441     return true;
442 }
443 
GetUint64Value(AttributeKey key,uint64_t & value) const444 bool Attributes::Impl::GetUint64Value(AttributeKey key, uint64_t &value) const
445 {
446     auto iter = map_.find(key);
447     if (iter == map_.end()) {
448         return false;
449     }
450 
451     if (!DecodeUint64Value(iter->second, value)) {
452         IAM_LOGE("DecodeUint64Value error");
453         return false;
454     }
455 
456     return true;
457 }
458 
GetUint32Value(AttributeKey key,uint32_t & value) const459 bool Attributes::Impl::GetUint32Value(AttributeKey key, uint32_t &value) const
460 {
461     auto iter = map_.find(key);
462     if (iter == map_.end()) {
463         return false;
464     }
465 
466     if (!DecodeUint32Value(iter->second, value)) {
467         IAM_LOGE("DecodeUint32Value error");
468         return false;
469     }
470 
471     return true;
472 }
473 
GetUint16Value(AttributeKey key,uint16_t & value) const474 bool Attributes::Impl::GetUint16Value(AttributeKey key, uint16_t &value) const
475 {
476     auto iter = map_.find(key);
477     if (iter == map_.end()) {
478         return false;
479     }
480 
481     if (!DecodeUint16Value(iter->second, value)) {
482         IAM_LOGE("DecodeUint16Value error");
483         return false;
484     }
485 
486     return true;
487 }
488 
GetUint8Value(AttributeKey key,uint8_t & value) const489 bool Attributes::Impl::GetUint8Value(AttributeKey key, uint8_t &value) const
490 {
491     auto iter = map_.find(key);
492     if (iter == map_.end()) {
493         return false;
494     }
495 
496     if (!DecodeUint8Value(iter->second, value)) {
497         IAM_LOGE("DecodeUint8Value error");
498         return false;
499     }
500 
501     return true;
502 }
503 
GetInt32Value(AttributeKey key,int32_t & value) const504 bool Attributes::Impl::GetInt32Value(AttributeKey key, int32_t &value) const
505 {
506     auto iter = map_.find(key);
507     if (iter == map_.end()) {
508         return false;
509     }
510 
511     if (!DecodeInt32Value(iter->second, value)) {
512         IAM_LOGE("DecodeInt32Value error");
513         return false;
514     }
515 
516     return true;
517 }
518 
GetInt64Value(AttributeKey key,int64_t & value) const519 bool Attributes::Impl::GetInt64Value(AttributeKey key, int64_t &value) const
520 {
521     auto iter = map_.find(key);
522     if (iter == map_.end()) {
523         return false;
524     }
525 
526     if (!DecodeInt64Value(iter->second, value)) {
527         IAM_LOGE("DecodeInt64Value error");
528         return false;
529     }
530 
531     return true;
532 }
533 
GetStringValue(AttributeKey key,std::string & value) const534 bool Attributes::Impl::GetStringValue(AttributeKey key, std::string &value) const
535 {
536     auto iter = map_.find(key);
537     if (iter == map_.end()) {
538         return false;
539     }
540 
541     if (!DecodeStringValue(iter->second, value)) {
542         IAM_LOGE("DecodeStringValue error");
543         return false;
544     }
545 
546     return true;
547 }
548 
GetUint64ArrayValue(AttributeKey key,std::vector<uint64_t> & value) const549 bool Attributes::Impl::GetUint64ArrayValue(AttributeKey key, std::vector<uint64_t> &value) const
550 {
551     auto iter = map_.find(key);
552     if (iter == map_.end()) {
553         return false;
554     }
555 
556     if (!DecodeUint64ArrayValue(iter->second, value)) {
557         IAM_LOGE("DecodeUint64ArrayValue error");
558         return false;
559     }
560 
561     return true;
562 }
563 
GetUint32ArrayValue(AttributeKey key,std::vector<uint32_t> & value) const564 bool Attributes::Impl::GetUint32ArrayValue(AttributeKey key, std::vector<uint32_t> &value) const
565 {
566     auto iter = map_.find(key);
567     if (iter == map_.end()) {
568         return false;
569     }
570 
571     if (!DecodeUint32ArrayValue(iter->second, value)) {
572         IAM_LOGE("DecodeUint32ArrayValue error");
573         return false;
574     }
575 
576     return true;
577 }
578 
GetInt32ArrayValue(AttributeKey key,std::vector<int32_t> & value) const579 bool Attributes::Impl::GetInt32ArrayValue(AttributeKey key, std::vector<int32_t> &value) const
580 {
581     auto iter = map_.find(key);
582     if (iter == map_.end()) {
583         return false;
584     }
585 
586     if (!DecodeInt32ArrayValue(iter->second, value)) {
587         IAM_LOGE("DecodeUint32ArrayValue error");
588         return false;
589     }
590 
591     return true;
592 }
593 
GetUint16ArrayValue(AttributeKey key,std::vector<uint16_t> & value) const594 bool Attributes::Impl::GetUint16ArrayValue(AttributeKey key, std::vector<uint16_t> &value) const
595 {
596     auto iter = map_.find(key);
597     if (iter == map_.end()) {
598         return false;
599     }
600 
601     if (!DecodeUint16ArrayValue(iter->second, value)) {
602         IAM_LOGE("DecodeUint16ArrayValue error");
603         return false;
604     }
605 
606     return true;
607 }
608 
GetUint8ArrayValue(AttributeKey key,std::vector<uint8_t> & value) const609 bool Attributes::Impl::GetUint8ArrayValue(AttributeKey key, std::vector<uint8_t> &value) const
610 {
611     auto iter = map_.find(key);
612     if (iter == map_.end()) {
613         return false;
614     }
615 
616     if (!DecodeUint8ArrayValue(iter->second, value)) {
617         IAM_LOGE("DecodeUint8ArrayValue error");
618         return false;
619     }
620     return true;
621 }
622 
GetAttributesValue(Attributes::AttributeKey key,Attributes::Impl & value) const623 bool Attributes::Impl::GetAttributesValue(Attributes::AttributeKey key, Attributes::Impl &value) const
624 {
625     auto iter = map_.find(key);
626     if (iter == map_.end()) {
627         return false;
628     }
629     Attributes::Impl out(iter->second);
630     value = std::move(out);
631     return true;
632 }
633 
Serialize() const634 std::vector<uint8_t> Attributes::Impl::Serialize() const
635 {
636     uint32_t size = 0;
637     for (const auto &[key, value] : map_) {
638         size += sizeof(uint32_t) / sizeof(uint8_t);
639         size += sizeof(uint32_t) / sizeof(uint8_t);
640         size += value.size();
641     }
642     std::vector<uint8_t> buffer;
643     buffer.reserve(size);
644 
645     for (const auto &[key, value] : map_) {
646         std::vector<uint8_t> type;
647         std::vector<uint8_t> length;
648         if (!EncodeUint32Value(key, type)) {
649             buffer.clear();
650             IAM_LOGE("EncodeUint32Value key error");
651             break;
652         }
653         if (!EncodeUint32Value(value.size() * sizeof(uint8_t), length)) {
654             buffer.clear();
655             IAM_LOGE("EncodeUint32Value value error");
656             break;
657         }
658         buffer.insert(buffer.end(), type.begin(), type.end());
659         buffer.insert(buffer.end(), length.begin(), length.end());
660         buffer.insert(buffer.end(), value.begin(), value.end());
661     }
662     return buffer;
663 }
664 
GetKeys() const665 std::vector<Attributes::AttributeKey> Attributes::Impl::GetKeys() const
666 {
667     std::vector<Attributes::AttributeKey> keys;
668     keys.reserve(map_.size());
669     for (auto const &item : map_) {
670         keys.push_back(item.first);
671     }
672     return keys;
673 }
674 
EncodeBoolValue(bool src,std::vector<uint8_t> & dst)675 bool Attributes::Impl::EncodeBoolValue(bool src, std::vector<uint8_t> &dst)
676 {
677     std::vector<uint8_t> out(1); // only 1
678     out[0] = src ? 1 : 0;
679 
680     dst.swap(out);
681     return true;
682 }
683 
EncodeUint64Value(uint64_t src,std::vector<uint8_t> & dst)684 bool Attributes::Impl::EncodeUint64Value(uint64_t src, std::vector<uint8_t> &dst)
685 {
686     std::vector<uint8_t> out(sizeof(uint64_t) / sizeof(uint8_t));
687     if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
688         return false;
689     }
690     dst.swap(out);
691     return true;
692 }
693 
EncodeUint32Value(uint32_t src,std::vector<uint8_t> & dst)694 bool Attributes::Impl::EncodeUint32Value(uint32_t src, std::vector<uint8_t> &dst)
695 {
696     std::vector<uint8_t> out(sizeof(uint32_t) / sizeof(uint8_t));
697     if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
698         return false;
699     }
700     dst.swap(out);
701     return true;
702 }
703 
EncodeUint16Value(uint16_t src,std::vector<uint8_t> & dst)704 bool Attributes::Impl::EncodeUint16Value(uint16_t src, std::vector<uint8_t> &dst)
705 {
706     std::vector<uint8_t> out(sizeof(uint16_t) / sizeof(uint8_t));
707     if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
708         return false;
709     }
710     dst.swap(out);
711     return true;
712 }
713 
EncodeUint8Value(uint8_t src,std::vector<uint8_t> & dst)714 bool Attributes::Impl::EncodeUint8Value(uint8_t src, std::vector<uint8_t> &dst)
715 {
716     std::vector<uint8_t> out(1);
717     out[0] = src;
718     dst.swap(out);
719     return true;
720 }
721 
EncodeInt32Value(int32_t src,std::vector<uint8_t> & dst)722 bool Attributes::Impl::EncodeInt32Value(int32_t src, std::vector<uint8_t> &dst)
723 {
724     std::vector<uint8_t> out(sizeof(int32_t) / sizeof(uint8_t));
725     if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
726         return false;
727     }
728     dst.swap(out);
729     return true;
730 }
731 
EncodeInt64Value(int64_t src,std::vector<uint8_t> & dst)732 bool Attributes::Impl::EncodeInt64Value(int64_t src, std::vector<uint8_t> &dst)
733 {
734     std::vector<uint8_t> out(sizeof(int64_t) / sizeof(uint8_t));
735     if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
736         return false;
737     }
738     dst.swap(out);
739     return true;
740 }
741 
EncodeStringValue(const std::string & src,std::vector<uint8_t> & dst)742 bool Attributes::Impl::EncodeStringValue(const std::string &src, std::vector<uint8_t> &dst)
743 {
744     if (src.size() > MAX_ATTR_LENGTH) {
745         return false;
746     }
747 
748     std::vector<uint8_t> out(src.begin(), src.end());
749     out.push_back(0);
750     dst.swap(out);
751     return true;
752 }
753 
EncodeUint64ArrayValue(const std::vector<uint64_t> & src,std::vector<uint8_t> & dst)754 bool Attributes::Impl::EncodeUint64ArrayValue(const std::vector<uint64_t> &src, std::vector<uint8_t> &dst)
755 {
756     auto size = src.size() * (sizeof(uint64_t) / sizeof(uint8_t));
757     if (size > MAX_ATTR_LENGTH) {
758         return false;
759     }
760 
761     std::vector<uint8_t> out(size);
762 
763     if (!src.empty() &&
764         memcpy_s(out.data(), out.size() * sizeof(uint8_t), src.data(), src.size() * sizeof(uint64_t)) != EOK) {
765         return false;
766     }
767 
768     dst.swap(out);
769     return true;
770 }
771 
EncodeUint32ArrayValue(const std::vector<uint32_t> & src,std::vector<uint8_t> & dst)772 bool Attributes::Impl::EncodeUint32ArrayValue(const std::vector<uint32_t> &src, std::vector<uint8_t> &dst)
773 {
774     auto size = src.size() * (sizeof(uint32_t) / sizeof(uint8_t));
775     if (size > MAX_ATTR_LENGTH) {
776         return false;
777     }
778 
779     std::vector<uint8_t> out(size);
780 
781     if (!src.empty() &&
782         memcpy_s(out.data(), out.size() * sizeof(uint8_t), src.data(), src.size() * sizeof(uint32_t)) != EOK) {
783         return false;
784     }
785     dst.swap(out);
786     return true;
787 }
788 
EncodeUint16ArrayValue(const std::vector<uint16_t> & src,std::vector<uint8_t> & dst)789 bool Attributes::Impl::EncodeUint16ArrayValue(const std::vector<uint16_t> &src, std::vector<uint8_t> &dst)
790 {
791     auto size = src.size() * (sizeof(uint16_t) / sizeof(uint8_t));
792     if (size > MAX_ATTR_LENGTH) {
793         return false;
794     }
795 
796     std::vector<uint8_t> out(size);
797 
798     if (!src.empty() &&
799         memcpy_s(out.data(), out.size() * sizeof(uint8_t), src.data(), src.size() * sizeof(uint16_t)) != EOK) {
800         return false;
801     }
802     dst.swap(out);
803     return true;
804 }
805 
EncodeInt32ArrayValue(const std::vector<int32_t> & src,std::vector<uint8_t> & dst)806 bool Attributes::Impl::EncodeInt32ArrayValue(const std::vector<int32_t> &src, std::vector<uint8_t> &dst)
807 {
808     auto size = src.size() * (sizeof(int32_t) / sizeof(uint8_t));
809     if (size > MAX_ATTR_LENGTH) {
810         return false;
811     }
812 
813     std::vector<uint8_t> out(size);
814 
815     if (!src.empty() &&
816         memcpy_s(out.data(), out.size() * sizeof(uint8_t), src.data(), src.size() * sizeof(int32_t)) != EOK) {
817         return false;
818     }
819     dst.swap(out);
820     return true;
821 }
822 
EncodeUint8ArrayValue(const std::vector<uint8_t> & src,std::vector<uint8_t> & dst)823 bool Attributes::Impl::EncodeUint8ArrayValue(const std::vector<uint8_t> &src, std::vector<uint8_t> &dst)
824 {
825     if (src.size() > MAX_ATTR_LENGTH) {
826         return false;
827     }
828 
829     std::vector<uint8_t> out(src);
830     dst.swap(out);
831     return true;
832 }
833 
DecodeBoolValue(const std::vector<uint8_t> & src,bool & dst)834 bool Attributes::Impl::DecodeBoolValue(const std::vector<uint8_t> &src, bool &dst)
835 {
836     if (src.size() != 1) {
837         return false;
838     }
839     dst = (src[0] == 1);
840     return true;
841 }
842 
DecodeUint64Value(const std::vector<uint8_t> & src,uint64_t & dst)843 bool Attributes::Impl::DecodeUint64Value(const std::vector<uint8_t> &src, uint64_t &dst)
844 {
845     if (src.size() * sizeof(uint8_t) != sizeof(uint64_t)) {
846         return false;
847     }
848 
849     if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
850         return false;
851     }
852     return true;
853 }
854 
DecodeUint32Value(const std::vector<uint8_t> & src,uint32_t & dst)855 bool Attributes::Impl::DecodeUint32Value(const std::vector<uint8_t> &src, uint32_t &dst)
856 {
857     if (src.size() * sizeof(uint8_t) != sizeof(uint32_t)) {
858         return false;
859     }
860     if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
861         return false;
862     }
863     return true;
864 }
865 
DecodeUint16Value(const std::vector<uint8_t> & src,uint16_t & dst)866 bool Attributes::Impl::DecodeUint16Value(const std::vector<uint8_t> &src, uint16_t &dst)
867 {
868     if (src.size() * sizeof(uint8_t) != sizeof(uint16_t)) {
869         return false;
870     }
871     if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
872         return false;
873     }
874     return true;
875 }
876 
DecodeUint8Value(const std::vector<uint8_t> & src,uint8_t & dst)877 bool Attributes::Impl::DecodeUint8Value(const std::vector<uint8_t> &src, uint8_t &dst)
878 {
879     if (src.size() != 1) {
880         return false;
881     }
882     dst = src[0];
883     return true;
884 }
885 
DecodeInt32Value(const std::vector<uint8_t> & src,int32_t & dst)886 bool Attributes::Impl::DecodeInt32Value(const std::vector<uint8_t> &src, int32_t &dst)
887 {
888     if (src.size() * sizeof(uint8_t) != sizeof(int32_t)) {
889         return false;
890     }
891     if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
892         return false;
893     }
894     return true;
895 }
896 
DecodeInt64Value(const std::vector<uint8_t> & src,int64_t & dst)897 bool Attributes::Impl::DecodeInt64Value(const std::vector<uint8_t> &src, int64_t &dst)
898 {
899     if (src.size() * sizeof(uint8_t) != sizeof(int64_t)) {
900         return false;
901     }
902     if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
903         return false;
904     }
905     return true;
906 }
907 
DecodeStringValue(const std::vector<uint8_t> & src,std::string & dst)908 bool Attributes::Impl::DecodeStringValue(const std::vector<uint8_t> &src, std::string &dst)
909 {
910     if (src.empty()) {
911         return false;
912     }
913 
914     if (src.back() != 0) {
915         return false;
916     }
917 
918     std::string out(static_cast<const char *>(static_cast<const void *>(src.data())));
919 
920     dst.swap(out);
921     return true;
922 }
923 
DecodeUint64ArrayValue(const std::vector<uint8_t> & src,std::vector<uint64_t> & dst)924 bool Attributes::Impl::DecodeUint64ArrayValue(const std::vector<uint8_t> &src, std::vector<uint64_t> &dst)
925 {
926     if (src.size() % (sizeof(uint64_t) / sizeof(uint8_t)) != 0) {
927         return false;
928     }
929 
930     std::vector<uint64_t> out(src.size() / (sizeof(uint64_t) / sizeof(uint8_t)));
931 
932     if (!out.empty() &&
933         memcpy_s(out.data(), out.size() * sizeof(uint64_t), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
934         return false;
935     }
936 
937     dst.swap(out);
938     return true;
939 }
940 
DecodeUint32ArrayValue(const std::vector<uint8_t> & src,std::vector<uint32_t> & dst)941 bool Attributes::Impl::DecodeUint32ArrayValue(const std::vector<uint8_t> &src, std::vector<uint32_t> &dst)
942 {
943     if (src.size() % (sizeof(uint32_t) / sizeof(uint8_t)) != 0) {
944         return false;
945     }
946 
947     std::vector<uint32_t> out(src.size() / (sizeof(uint32_t) / sizeof(uint8_t)));
948 
949     if (!out.empty() &&
950         memcpy_s(out.data(), out.size() * sizeof(uint32_t), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
951         return false;
952     }
953 
954     dst.swap(out);
955     return true;
956 }
957 
DecodeUint16ArrayValue(const std::vector<uint8_t> & src,std::vector<uint16_t> & dst)958 bool Attributes::Impl::DecodeUint16ArrayValue(const std::vector<uint8_t> &src, std::vector<uint16_t> &dst)
959 {
960     if (src.size() % (sizeof(uint32_t) / sizeof(uint16_t)) != 0) {
961         return false;
962     }
963 
964     std::vector<uint16_t> out(src.size() / (sizeof(uint16_t) / sizeof(uint8_t)));
965 
966     if (!out.empty() &&
967         memcpy_s(out.data(), out.size() * sizeof(uint16_t), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
968         return false;
969     }
970 
971     dst.swap(out);
972     return true;
973 }
974 
DecodeUint8ArrayValue(const std::vector<uint8_t> & src,std::vector<uint8_t> & dst)975 bool Attributes::Impl::DecodeUint8ArrayValue(const std::vector<uint8_t> &src, std::vector<uint8_t> &dst)
976 {
977     std::vector<uint8_t> out(src);
978     dst.swap(out);
979     return true;
980 }
981 
DecodeInt32ArrayValue(const std::vector<uint8_t> & src,std::vector<int32_t> & dst)982 bool Attributes::Impl::DecodeInt32ArrayValue(const std::vector<uint8_t> &src, std::vector<int32_t> &dst)
983 {
984     if (src.size() % (sizeof(int32_t) / sizeof(uint8_t)) != 0) {
985         return false;
986     }
987 
988     std::vector<int32_t> out(src.size() / (sizeof(int32_t) / sizeof(uint8_t)));
989 
990     if (!out.empty() &&
991         memcpy_s(out.data(), out.size() * sizeof(int32_t), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
992         return false;
993     }
994 
995     dst.swap(out);
996     return true;
997 }
998 
Attributes()999 Attributes::Attributes() : impl_(new (std::nothrow) Attributes::Impl())
1000 {
1001 }
1002 
Attributes(const std::vector<uint8_t> & raw)1003 Attributes::Attributes(const std::vector<uint8_t> &raw) : impl_(new (std::nothrow) Attributes::Impl(raw))
1004 {
1005 }
1006 
Attributes(Attributes && other)1007 Attributes::Attributes(Attributes &&other) noexcept : impl_(std::move(other.impl_))
1008 {
1009 }
1010 
operator =(Attributes && other)1011 Attributes &Attributes::operator=(Attributes &&other) noexcept
1012 {
1013     impl_ = std::move(other.impl_);
1014     return *this;
1015 }
1016 
~Attributes()1017 Attributes::~Attributes()
1018 {
1019     impl_ = nullptr;
1020 };
1021 
SetBoolValue(AttributeKey key,bool value)1022 bool Attributes::SetBoolValue(AttributeKey key, bool value)
1023 {
1024     if (!impl_) {
1025         return false;
1026     }
1027     return impl_->SetBoolValue(key, value);
1028 }
1029 
SetUint64Value(AttributeKey key,uint64_t value)1030 bool Attributes::SetUint64Value(AttributeKey key, uint64_t value)
1031 {
1032     if (!impl_) {
1033         return false;
1034     }
1035     return impl_->SetUint64Value(key, value);
1036 }
1037 
SetUint32Value(AttributeKey key,uint32_t value)1038 bool Attributes::SetUint32Value(AttributeKey key, uint32_t value)
1039 {
1040     if (!impl_) {
1041         return false;
1042     }
1043     return impl_->SetUint32Value(key, value);
1044 }
1045 
SetUint16Value(AttributeKey key,uint16_t value)1046 bool Attributes::SetUint16Value(AttributeKey key, uint16_t value)
1047 {
1048     if (!impl_) {
1049         return false;
1050     }
1051     return impl_->SetUint16Value(key, value);
1052 }
1053 
SetUint8Value(AttributeKey key,uint8_t value)1054 bool Attributes::SetUint8Value(AttributeKey key, uint8_t value)
1055 {
1056     if (!impl_) {
1057         return false;
1058     }
1059     return impl_->SetUint8Value(key, value);
1060 }
1061 
SetInt32Value(AttributeKey key,int32_t value)1062 bool Attributes::SetInt32Value(AttributeKey key, int32_t value)
1063 {
1064     if (!impl_) {
1065         return false;
1066     }
1067     return impl_->SetInt32Value(key, value);
1068 }
1069 
SetInt64Value(AttributeKey key,int64_t value)1070 bool Attributes::SetInt64Value(AttributeKey key, int64_t value)
1071 {
1072     if (!impl_) {
1073         return false;
1074     }
1075     return impl_->SetInt64Value(key, value);
1076 }
1077 
SetStringValue(AttributeKey key,const std::string & value)1078 bool Attributes::SetStringValue(AttributeKey key, const std::string &value)
1079 {
1080     if (!impl_) {
1081         return false;
1082     }
1083     return impl_->SetStringValue(key, value);
1084 }
1085 
SetUint64ArrayValue(AttributeKey key,const std::vector<uint64_t> & value)1086 bool Attributes::SetUint64ArrayValue(AttributeKey key, const std::vector<uint64_t> &value)
1087 {
1088     if (!impl_) {
1089         return false;
1090     }
1091     return impl_->SetUint64ArrayValue(key, value);
1092 }
1093 
SetUint32ArrayValue(AttributeKey key,const std::vector<uint32_t> & value)1094 bool Attributes::SetUint32ArrayValue(AttributeKey key, const std::vector<uint32_t> &value)
1095 {
1096     if (!impl_) {
1097         return false;
1098     }
1099     return impl_->SetUint32ArrayValue(key, value);
1100 }
1101 
SetUint16ArrayValue(AttributeKey key,const std::vector<uint16_t> & value)1102 bool Attributes::SetUint16ArrayValue(AttributeKey key, const std::vector<uint16_t> &value)
1103 {
1104     if (!impl_) {
1105         return false;
1106     }
1107     return impl_->SetUint16ArrayValue(key, value);
1108 }
1109 
SetUint8ArrayValue(AttributeKey key,const std::vector<uint8_t> & value)1110 bool Attributes::SetUint8ArrayValue(AttributeKey key, const std::vector<uint8_t> &value)
1111 {
1112     if (!impl_) {
1113         return false;
1114     }
1115     return impl_->SetUint8ArrayValue(key, value);
1116 }
1117 
SetInt32ArrayValue(AttributeKey key,const std::vector<int32_t> & value)1118 bool Attributes::SetInt32ArrayValue(AttributeKey key, const std::vector<int32_t> &value)
1119 {
1120     if (!impl_) {
1121         return false;
1122     }
1123     return impl_->SetInt32ArrayValue(key, value);
1124 }
1125 
SetAttributesValue(AttributeKey key,const Attributes & value)1126 bool Attributes::SetAttributesValue(AttributeKey key, const Attributes &value)
1127 {
1128     if (!impl_) {
1129         return false;
1130     }
1131     return impl_->SetAttributesValue(key, *value.impl_);
1132 }
1133 
SetAttributesArrayValue(AttributeKey key,const std::vector<Attributes> & array)1134 bool Attributes::SetAttributesArrayValue(AttributeKey key, const std::vector<Attributes> &array)
1135 {
1136     if (!impl_) {
1137         return false;
1138     }
1139 
1140     std::vector<std::vector<uint8_t>> serializedArray;
1141     for (auto &item : array) {
1142         if (!item.impl_) {
1143             return false;
1144         }
1145         serializedArray.push_back(item.Serialize());
1146     }
1147 
1148     uint32_t dataLen = 0;
1149     for (auto &array : serializedArray) {
1150         dataLen += (sizeof(uint32_t) + array.size());
1151     }
1152 
1153     std::vector<uint8_t> data;
1154     data.reserve(dataLen);
1155     for (auto &array : serializedArray) {
1156         std::vector<uint8_t> arrayLen;
1157         bool encodeRet = Attributes::Impl::EncodeUint32Value(array.size(), arrayLen);
1158         if (!encodeRet) {
1159             return false;
1160         }
1161         std::copy(arrayLen.begin(), arrayLen.end(), std::back_inserter(data));
1162         std::copy(array.begin(), array.end(), std::back_inserter(data));
1163     }
1164 
1165     return impl_->SetUint8ArrayValue(key, data);
1166 }
1167 
GetAttributesArrayValue(AttributeKey key,std::vector<Attributes> & array) const1168 bool Attributes::GetAttributesArrayValue(AttributeKey key, std::vector<Attributes> &array) const
1169 {
1170     if (!impl_) {
1171         return false;
1172     }
1173 
1174     std::vector<uint8_t> data;
1175     bool getDataRet = impl_->GetUint8ArrayValue(key, data);
1176     if (!getDataRet) {
1177         return false;
1178     }
1179 
1180     array.clear();
1181     uint32_t i = 0;
1182     while (i < data.size()) {
1183         if (data.size() - i < sizeof(uint32_t)) {
1184             return false;
1185         }
1186 
1187         std::vector<uint8_t> arrayLenData(data.begin() + i, data.begin() + i + sizeof(uint32_t));
1188         uint32_t arrayLen;
1189         bool decodeRet = Attributes::Impl::DecodeUint32Value(arrayLenData, arrayLen);
1190         if (!decodeRet) {
1191             return false;
1192         }
1193         i += sizeof(uint32_t);
1194 
1195         if (data.size() - i < arrayLen) {
1196             return false;
1197         }
1198 
1199         array.push_back(Attributes(std::vector<uint8_t>(data.begin() + i, data.begin() + i + arrayLen)));
1200         i += arrayLen;
1201     }
1202 
1203     return true;
1204 }
1205 
GetBoolValue(AttributeKey key,bool & value) const1206 bool Attributes::GetBoolValue(AttributeKey key, bool &value) const
1207 {
1208     if (!impl_) {
1209         return false;
1210     }
1211     return impl_->GetBoolValue(key, value);
1212 }
1213 
GetUint64Value(AttributeKey key,uint64_t & value) const1214 bool Attributes::GetUint64Value(AttributeKey key, uint64_t &value) const
1215 {
1216     if (!impl_) {
1217         return false;
1218     }
1219     return impl_->GetUint64Value(key, value);
1220 }
1221 
GetUint32Value(AttributeKey key,uint32_t & value) const1222 bool Attributes::GetUint32Value(AttributeKey key, uint32_t &value) const
1223 {
1224     if (!impl_) {
1225         return false;
1226     }
1227     return impl_->GetUint32Value(key, value);
1228 }
1229 
GetUint16Value(AttributeKey key,uint16_t & value) const1230 bool Attributes::GetUint16Value(AttributeKey key, uint16_t &value) const
1231 {
1232     if (!impl_) {
1233         return false;
1234     }
1235     return impl_->GetUint16Value(key, value);
1236 }
1237 
GetUint8Value(AttributeKey key,uint8_t & value) const1238 bool Attributes::GetUint8Value(AttributeKey key, uint8_t &value) const
1239 {
1240     if (!impl_) {
1241         return false;
1242     }
1243     return impl_->GetUint8Value(key, value);
1244 }
1245 
GetInt32Value(AttributeKey key,int32_t & value) const1246 bool Attributes::GetInt32Value(AttributeKey key, int32_t &value) const
1247 {
1248     if (!impl_) {
1249         return false;
1250     }
1251     return impl_->GetInt32Value(key, value);
1252 }
1253 
GetInt64Value(AttributeKey key,int64_t & value) const1254 bool Attributes::GetInt64Value(AttributeKey key, int64_t &value) const
1255 {
1256     if (!impl_) {
1257         return false;
1258     }
1259     return impl_->GetInt64Value(key, value);
1260 }
1261 
GetStringValue(AttributeKey key,std::string & value) const1262 bool Attributes::GetStringValue(AttributeKey key, std::string &value) const
1263 {
1264     if (!impl_) {
1265         return false;
1266     }
1267     return impl_->GetStringValue(key, value);
1268 }
1269 
GetUint64ArrayValue(AttributeKey key,std::vector<uint64_t> & value) const1270 bool Attributes::GetUint64ArrayValue(AttributeKey key, std::vector<uint64_t> &value) const
1271 {
1272     if (!impl_) {
1273         return false;
1274     }
1275     return impl_->GetUint64ArrayValue(key, value);
1276 }
1277 
GetUint32ArrayValue(AttributeKey key,std::vector<uint32_t> & value) const1278 bool Attributes::GetUint32ArrayValue(AttributeKey key, std::vector<uint32_t> &value) const
1279 {
1280     if (!impl_) {
1281         return false;
1282     }
1283     return impl_->GetUint32ArrayValue(key, value);
1284 }
1285 
GetUint16ArrayValue(AttributeKey key,std::vector<uint16_t> & value) const1286 bool Attributes::GetUint16ArrayValue(AttributeKey key, std::vector<uint16_t> &value) const
1287 {
1288     if (!impl_) {
1289         return false;
1290     }
1291     return impl_->GetUint16ArrayValue(key, value);
1292 }
1293 
GetUint8ArrayValue(AttributeKey key,std::vector<uint8_t> & value) const1294 bool Attributes::GetUint8ArrayValue(AttributeKey key, std::vector<uint8_t> &value) const
1295 {
1296     if (!impl_) {
1297         return false;
1298     }
1299     return impl_->GetUint8ArrayValue(key, value);
1300 }
1301 
GetInt32ArrayValue(AttributeKey key,std::vector<int32_t> & value) const1302 bool Attributes::GetInt32ArrayValue(AttributeKey key, std::vector<int32_t> &value) const
1303 {
1304     if (!impl_) {
1305         return false;
1306     }
1307     return impl_->GetInt32ArrayValue(key, value);
1308 }
1309 
GetAttributesValue(AttributeKey key,Attributes & value) const1310 bool Attributes::GetAttributesValue(AttributeKey key, Attributes &value) const
1311 {
1312     if (!impl_) {
1313         return false;
1314     }
1315     return impl_->GetAttributesValue(key, *value.impl_);
1316 }
1317 
Serialize() const1318 std::vector<uint8_t> Attributes::Serialize() const
1319 {
1320     if (!impl_) {
1321         return {};
1322     }
1323     return impl_->Serialize();
1324 }
1325 
GetKeys() const1326 std::vector<Attributes::AttributeKey> Attributes::GetKeys() const
1327 {
1328     if (!impl_) {
1329         return {};
1330     }
1331     return impl_->GetKeys();
1332 }
1333 } // namespace UserAuth
1334 } // namespace UserIam
1335 } // namespace OHOS
1336