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 #define LOG_TAG "ITypesUtil::KvTypesUtil"
16 #include "kv_types_util.h"
17 #include "log_print.h"
18 namespace OHOS::ITypesUtil {
19 using namespace DistributedKv;
20 template<>
Marshalling(const Blob & blob,MessageParcel & data)21 bool Marshalling(const Blob &blob, MessageParcel &data)
22 {
23 return data.WriteUInt8Vector(blob.Data());
24 }
25
26 template<>
Unmarshalling(Blob & output,MessageParcel & data)27 bool Unmarshalling(Blob &output, MessageParcel &data)
28 {
29 std::vector<uint8_t> blob;
30 bool result = data.ReadUInt8Vector(&blob);
31 output = blob;
32 return result;
33 }
34
35 template<>
Marshalling(const AppId & input,MessageParcel & data)36 bool Marshalling(const AppId &input, MessageParcel &data)
37 {
38 return ITypesUtil::Marshalling(input.appId, data);
39 }
40
41 template<>
Unmarshalling(AppId & output,MessageParcel & data)42 bool Unmarshalling(AppId &output, MessageParcel &data)
43 {
44 return ITypesUtil::Unmarshalling(output.appId, data);
45 }
46
47 template<>
Marshalling(const StoreId & input,MessageParcel & data)48 bool Marshalling(const StoreId &input, MessageParcel &data)
49 {
50 return ITypesUtil::Marshalling(input.storeId, data);
51 }
52
53 template<>
Unmarshalling(StoreId & output,MessageParcel & data)54 bool Unmarshalling(StoreId &output, MessageParcel &data)
55 {
56 return ITypesUtil::Unmarshalling(output.storeId, data);
57 }
58
59 template<>
Marshalling(const Entry & entry,MessageParcel & data)60 bool Marshalling(const Entry &entry, MessageParcel &data)
61 {
62 return ITypesUtil::Marshal(data, entry.key, entry.value);
63 }
64
65 template<>
Unmarshalling(Entry & output,MessageParcel & data)66 bool Unmarshalling(Entry &output, MessageParcel &data)
67 {
68 return ITypesUtil::Unmarshal(data, output.key, output.value);
69 }
70
71 template<>
Marshalling(const DeviceInfo & entry,MessageParcel & data)72 bool Marshalling(const DeviceInfo &entry, MessageParcel &data)
73 {
74 return ITypesUtil::Marshal(data, entry.deviceId, entry.deviceName, entry.deviceType);
75 }
76
77 template<>
Unmarshalling(DeviceInfo & output,MessageParcel & data)78 bool Unmarshalling(DeviceInfo &output, MessageParcel &data)
79 {
80 return ITypesUtil::Unmarshal(data, output.deviceId, output.deviceName, output.deviceType);
81 }
82
83 template<>
Marshalling(const ChangeNotification & notification,MessageParcel & parcel)84 bool Marshalling(const ChangeNotification ¬ification, MessageParcel &parcel)
85 {
86 return ITypesUtil::Marshal(parcel, notification.GetInsertEntries(), notification.GetUpdateEntries(),
87 notification.GetDeleteEntries(), notification.GetDeviceId(), notification.IsClear());
88 }
89
90 template<>
Unmarshalling(ChangeNotification & output,MessageParcel & parcel)91 bool Unmarshalling(ChangeNotification &output, MessageParcel &parcel)
92 {
93 std::vector<Entry> inserts;
94 std::vector<Entry> updates;
95 std::vector<Entry> deletes;
96 std::string deviceId;
97 bool isClear = false;
98 if (!ITypesUtil::Unmarshal(parcel, inserts, updates, deletes, deviceId, isClear)) {
99 return false;
100 }
101 output = ChangeNotification(std::move(inserts), std::move(updates), std::move(deletes), deviceId, isClear);
102 return true;
103 }
104
105 template<>
Marshalling(const Options & input,MessageParcel & data)106 bool Marshalling(const Options &input, MessageParcel &data)
107 {
108 if (!ITypesUtil::Marshal(data, input.schema, input.hapName, input.policies)) {
109 ZLOGE("write policies failed");
110 return false;
111 }
112
113 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(sizeof(input));
114 Options *target = reinterpret_cast<Options *>(buffer.get());
115 target->createIfMissing = input.createIfMissing;
116 target->encrypt = input.encrypt;
117 target->persistent = input.persistent;
118 target->backup = input.backup;
119 target->autoSync = input.autoSync;
120 target->syncable = input.syncable;
121 target->securityLevel = input.securityLevel;
122 target->area = input.area;
123 target->kvStoreType = input.kvStoreType;
124 return data.WriteRawData(buffer.get(), sizeof(input));
125 }
126
127 template<>
Unmarshalling(Options & output,MessageParcel & data)128 bool Unmarshalling(Options &output, MessageParcel &data)
129 {
130 if (!ITypesUtil::Unmarshal(data, output.schema, output.hapName, output.policies)) {
131 ZLOGE("read policies failed");
132 return false;
133 }
134
135 const Options *source = reinterpret_cast<const Options *>(data.ReadRawData(sizeof(output)));
136 if (source == nullptr) {
137 return false;
138 }
139 output.createIfMissing = source->createIfMissing;
140 output.encrypt = source->encrypt;
141 output.persistent = source->persistent;
142 output.backup = source->backup;
143 output.autoSync = source->autoSync;
144 output.securityLevel = source->securityLevel;
145 output.area = source->area;
146 output.kvStoreType = source->kvStoreType;
147 output.syncable = source->syncable;
148 return true;
149 }
150
151 template<>
Marshalling(const SyncPolicy & input,MessageParcel & data)152 bool Marshalling(const SyncPolicy &input, MessageParcel &data)
153 {
154 return ITypesUtil::Marshal(data, input.type, input.value);
155 }
156
157 template<>
Unmarshalling(SyncPolicy & output,MessageParcel & data)158 bool Unmarshalling(SyncPolicy &output, MessageParcel &data)
159 {
160 return ITypesUtil::Unmarshal(data, output.type, output.value);
161 }
162
163 template<>
Marshalling(const DevBrief & input,MessageParcel & data)164 bool Marshalling(const DevBrief &input, MessageParcel &data)
165 {
166 return ITypesUtil::Marshal(data, input.uuid, input.networkId);
167 }
168
169 template<>
Unmarshalling(DevBrief & output,MessageParcel & data)170 bool Unmarshalling(DevBrief &output, MessageParcel &data)
171 {
172 return ITypesUtil::Unmarshal(data, output.uuid, output.networkId);
173 }
174
GetTotalSize(const std::vector<Entry> & entries)175 int64_t GetTotalSize(const std::vector<Entry> &entries)
176 {
177 int64_t bufferSize = 1;
178 for (const auto &item : entries) {
179 if (item.key.Size() > Entry::MAX_KEY_LENGTH || item.value.Size() > Entry::MAX_VALUE_LENGTH) {
180 return -bufferSize;
181 }
182 bufferSize += item.key.RawSize() + item.value.RawSize();
183 }
184 return bufferSize - 1;
185 }
186
GetTotalSize(const std::vector<Key> & entries)187 int64_t GetTotalSize(const std::vector<Key> &entries)
188 {
189 int64_t bufferSize = 1;
190 for (const auto &item : entries) {
191 if (item.Size() > Entry::MAX_KEY_LENGTH) {
192 return -bufferSize;
193 }
194 bufferSize += item.RawSize();
195 }
196 return bufferSize - 1;
197 }
198 }