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