1 /*
2  * Copyright (c) 2024 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 PREFERENCES_VALUE_PARCEL_H
17 #define PREFERENCES_VALUE_PARCEL_H
18 
19 #include <vector>
20 #include <cstdint>
21 #include <string>
22 #include <type_traits>
23 
24 #include "preferences_value.h"
25 namespace OHOS {
26 namespace NativePreferences {
27 
28 class PreferencesValueParcel {
29 public:
30     static uint8_t GetTypeIndex(const PreferencesValue &value);
31     static uint32_t CalSize(PreferencesValue value);
32     static int MarshallingPreferenceValue(const PreferencesValue &value, std::vector<uint8_t> &data);
33     static std::pair<int, PreferencesValue> UnmarshallingPreferenceValue(const std::vector<uint8_t> &data);
34 
35 private:
36     enum ParcelTypeIndex {
37         MONO_TYPE = 0,
38         INT_TYPE = 1,
39         LONG_TYPE = 2,
40         FLOAT_TYPE = 3,
41         DOUBLE_TYPE = 4,
42         BOOL_TYPE = 5,
43         STRING_TYPE = 6,
44         STRING_ARRAY_TYPE = 7,
45         BOOL_ARRAY_TYPE = 8,
46         DOUBLE_ARRAY_TYPE = 9,
47         UINT8_ARRAY_TYPE = 10,
48         OBJECT_TYPE = 11,
49         BIG_INT_TYPE = 12
50     };
51     static int MarshallingBasicValue(const PreferencesValue &value, const uint8_t type, std::vector<uint8_t> &data);
52     static int MarshallingStringValue(const PreferencesValue &value, const uint8_t type, std::vector<uint8_t> &data);
53     static int MarshallingStringArrayValue(const PreferencesValue &value, const uint8_t type,
54         std::vector<uint8_t> &data);
55     static int MarshallingVecUInt8AfterType(const PreferencesValue &value, uint8_t *startAddr);
56     static int MarshallingVecBigIntAfterType(const PreferencesValue &value, uint8_t *startAddr);
57     static int MarshallingVecDoubleAfterType(const PreferencesValue &value, uint8_t *startAddr);
58     static int MarshallingVecBoolAfterType(const PreferencesValue &value, uint8_t *startAddr);
59     static int MarshallingBasicArrayValue(const PreferencesValue &value, const uint8_t type,
60         std::vector<uint8_t> &data);
61     static std::pair<int, PreferencesValue> UnmarshallingBasicValue(const uint8_t type,
62         const std::vector<uint8_t> &data);
63     static std::pair<int, PreferencesValue> UnmarshallingStringValue(const uint8_t type,
64         const std::vector<uint8_t> &data);
65     static std::pair<int, PreferencesValue> UnmarshallingStringArrayValue(const uint8_t type,
66         const std::vector<uint8_t> &data);
67     static std::pair<int, PreferencesValue> UnmarshallingVecUInt8(const std::vector<uint8_t> &data);
68     static std::pair<int, PreferencesValue> UnmarshallingVecDouble(const std::vector<uint8_t> &data);
69     static std::pair<int, PreferencesValue> UnmarshallingVecBool(const std::vector<uint8_t> &data);
70     static std::pair<int, PreferencesValue> UnmarshallingVecBigInt(const std::vector<uint8_t> &data);
71     static std::pair<int, PreferencesValue> UnmarshallingBasicArrayValue(const uint8_t type,
72         const std::vector<uint8_t> &data);
73     static int MarshallingBasicValueInner(const PreferencesValue &value, const uint8_t type,
74         std::vector<uint8_t> &data);
75 };
76 } // namespace NativePreferences
77 } // namespace OHOS
78 #endif