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