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 #ifndef IAM_MEM_H
17 #define IAM_MEM_H
18
19 #include <cstdint>
20 #include <vector>
21
22 namespace OHOS {
23 namespace UserIam {
24 namespace Common {
25 template <typename T>
Pack(std::vector<uint8_t> & dst,const T & data)26 inline void Pack(std::vector<uint8_t> &dst, const T &data)
27 {
28 const uint8_t *src = static_cast<const uint8_t *>(static_cast<const void *>(&data));
29 dst.insert(dst.end(), src, src + sizeof(T));
30 }
31
32 int32_t UnpackUint64(const std::vector<uint8_t> &src, size_t index, uint64_t &data);
33 int32_t UnpackInt32(const std::vector<uint8_t> &src, size_t index, int32_t &data);
34
CombineUint16ToUint32(uint16_t upper,uint16_t lower)35 inline uint32_t CombineUint16ToUint32(uint16_t upper, uint16_t lower)
36 {
37 return (static_cast<uint32_t>(upper) << 16U) | lower;
38 }
39 } // namespace Common
40 } // namespace UserIam
41 } // namespace OHOS
42
43 #endif // IAM_MEM_H