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 #define LOG_LABEL LABEL_IAM_COMMON
25
26 namespace OHOS {
27 namespace UserIam {
28 namespace Common {
UnpackUint64(const std::vector<uint8_t> & src,size_t index,uint64_t & data)29 int32_t UnpackUint64(const std::vector<uint8_t> &src, size_t index, uint64_t &data)
30 {
31 if ((src.size() < index) || (src.size() - index < sizeof(uint64_t))) {
32 return 1;
33 }
34 if (memcpy_s(static_cast<void *>(&data), sizeof(uint64_t), &src[index], sizeof(uint64_t)) != 0) {
35 return 1;
36 }
37 return 0;
38 }
39
UnpackInt32(const std::vector<uint8_t> & src,size_t index,int32_t & data)40 int32_t UnpackInt32(const std::vector<uint8_t> &src, size_t index, int32_t &data)
41 {
42 if ((src.size() < index) || (src.size() - index < sizeof(int32_t))) {
43 return 1;
44 }
45 if (memcpy_s(static_cast<void *>(&data), sizeof(int32_t), &src[index], sizeof(int32_t)) != 0) {
46 return 1;
47 }
48 return 0;
49 }
50 } // namespace Common
51 } // namespace UserIam
52 } // namespace OHOS