1 
2 /*
3  * Copyright (c) 2022 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "iam_mem.h"
18 
19 #include <cstdint>
20 #include <vector>
21 
22 #include "securec.h"
23 
24 namespace OHOS {
25 namespace UserIam {
26 namespace Common {
UnpackUint64(const std::vector<uint8_t> & src,size_t index,uint64_t & data)27 int32_t UnpackUint64(const std::vector<uint8_t> &src, size_t index, uint64_t &data)
28 {
29     if ((src.size() < index) || (src.size() - index < sizeof(uint64_t))) {
30         return 1;
31     }
32     if (memcpy_s(static_cast<void *>(&data), sizeof(uint64_t), &src[index], sizeof(uint64_t)) != 0) {
33         return 1;
34     }
35     return 0;
36 }
37 
UnpackInt32(const std::vector<uint8_t> & src,size_t index,int32_t & data)38 int32_t UnpackInt32(const std::vector<uint8_t> &src, size_t index, int32_t &data)
39 {
40     if ((src.size() < index) || (src.size() - index < sizeof(int32_t))) {
41         return 1;
42     }
43     if (memcpy_s(static_cast<void *>(&data), sizeof(int32_t), &src[index], sizeof(int32_t)) != 0) {
44         return 1;
45     }
46     return 0;
47 }
48 } // namespace Common
49 } // namespace UserIam
50 } // namespace OHOS