1 /*
2 * Copyright (c) 2021 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 #include "frameworks/bridge/codec/standard_codec_buffer_operator.h"
17
18 #include "base/log/log.h"
19
20 namespace OHOS::Ace::Framework {
21 namespace {
22
23 template<class T>
ReadDataFromByteBuffer(const ByteBufferReader & buffer,CodecData & resultData)24 inline bool ReadDataFromByteBuffer(const ByteBufferReader& buffer, CodecData& resultData)
25 {
26 T data;
27 if (buffer.ReadData(data)) {
28 resultData = CodecData(std::move(data));
29 return true;
30 }
31 return false;
32 }
33
34 }
35
ReadType(BufferDataType & type)36 bool StandardCodecBufferReader::ReadType(BufferDataType& type)
37 {
38 uint8_t uint8Type = 0;
39 if (byteBufferReader_.ReadData(uint8Type)) {
40 type = static_cast<BufferDataType>(uint8Type);
41 return true;
42 }
43 return false;
44 }
45
ReadMapSize(int32_t & data)46 bool StandardCodecBufferReader::ReadMapSize(int32_t& data)
47 {
48 BufferDataType type = BufferDataType::TYPE_NULL;
49 if (!ReadType(type)) {
50 LOGW("Read type failed");
51 return false;
52 }
53 if (type == BufferDataType::TYPE_MAP) {
54 byteBufferReader_.ReadData(data);
55 return true;
56 }
57 return false;
58 }
59
ReadDataList(std::vector<CodecData> & resultDataList)60 bool StandardCodecBufferReader::ReadDataList(std::vector<CodecData>& resultDataList)
61 {
62 resultDataList.clear();
63 uint8_t size = 0;
64 if (!byteBufferReader_.ReadData(size)) {
65 LOGW("Failed to read size of data list");
66 return false;
67 }
68
69 for (uint8_t idx = 0; idx < size; ++idx) {
70 CodecData data;
71 if (!ReadData(data)) {
72 LOGW("Failed to read data");
73 resultDataList.clear();
74 return false;
75 }
76 resultDataList.emplace_back(std::move(data));
77 }
78 return true;
79 }
80
ReadData(CodecData & resultData)81 bool StandardCodecBufferReader::ReadData(CodecData& resultData)
82 {
83 BufferDataType type = BufferDataType::TYPE_NULL;
84 if (!ReadType(type)) {
85 LOGW("Read type failed");
86 return false;
87 }
88
89 switch (type) {
90 case BufferDataType::TYPE_NULL:
91 resultData = CodecData();
92 return true;
93 case BufferDataType::TYPE_TRUE:
94 resultData = CodecData(true);
95 return true;
96 case BufferDataType::TYPE_FALSE:
97 resultData = CodecData(false);
98 return true;
99 case BufferDataType::TYPE_INT:
100 return ReadDataFromByteBuffer<int32_t>(byteBufferReader_, resultData);
101 case BufferDataType::TYPE_LONG:
102 return ReadDataFromByteBuffer<int64_t>(byteBufferReader_, resultData);
103 case BufferDataType::TYPE_DOUBLE:
104 return ReadDataFromByteBuffer<double>(byteBufferReader_, resultData);
105 case BufferDataType::TYPE_STRING:
106 return ReadDataFromByteBuffer<std::string>(byteBufferReader_, resultData);
107 case BufferDataType::TYPE_INT8_ARRAY:
108 return ReadDataFromByteBuffer<std::vector<int8_t>>(byteBufferReader_, resultData);
109 case BufferDataType::TYPE_INT16_ARRAY:
110 return ReadDataFromByteBuffer<std::vector<int16_t>>(byteBufferReader_, resultData);
111 case BufferDataType::TYPE_INT32_ARRAY:
112 return ReadDataFromByteBuffer<std::vector<int32_t>>(byteBufferReader_, resultData);
113 case BufferDataType::TYPE_MAP:
114 return ReadDataFromByteBuffer<std::map<std::string, std::string>>(byteBufferReader_, resultData);
115 case BufferDataType::TYPE_SET:
116 return ReadDataFromByteBuffer<std::set<std::string>>(byteBufferReader_, resultData);
117 case BufferDataType::TYPE_FUNCTION:
118 return ReadDataFromByteBuffer<int32_t>(byteBufferReader_, resultData);
119 case BufferDataType::TYPE_OBJECT:
120 return ReadDataFromByteBuffer<std::string>(byteBufferReader_, resultData);
121 default:
122 LOGW("Unknown type, type = %{public}u", static_cast<uint8_t>(type));
123 return false;
124 }
125 }
126
WriteType(BufferDataType type)127 void StandardCodecBufferWriter::WriteType(BufferDataType type)
128 {
129 byteBufferWriter_.WriteData(static_cast<uint8_t>(type));
130 }
131
WriteDataList(const std::vector<CodecData> & dataList)132 void StandardCodecBufferWriter::WriteDataList(const std::vector<CodecData>& dataList)
133 {
134 byteBufferWriter_.WriteData(static_cast<uint8_t>(dataList.size()));
135 for (const auto& data : dataList) {
136 WriteData(data);
137 }
138 }
139
WriteData(const CodecData & data)140 void StandardCodecBufferWriter::WriteData(const CodecData& data)
141 {
142 WriteType(data.GetType());
143
144 switch (data.GetType()) {
145 case BufferDataType::TYPE_INT:
146 byteBufferWriter_.WriteData(data.GetIntValue());
147 break;
148 case BufferDataType::TYPE_LONG:
149 byteBufferWriter_.WriteData(data.GetLongValue());
150 break;
151 case BufferDataType::TYPE_DOUBLE:
152 byteBufferWriter_.WriteData(data.GetDoubleValue());
153 break;
154 case BufferDataType::TYPE_STRING:
155 byteBufferWriter_.WriteData(data.GetStringValue());
156 break;
157 case BufferDataType::TYPE_INT8_ARRAY:
158 byteBufferWriter_.WriteData(data.GetInt8ArrayValue());
159 break;
160 case BufferDataType::TYPE_INT16_ARRAY:
161 byteBufferWriter_.WriteData(data.GetInt16ArrayValue());
162 break;
163 case BufferDataType::TYPE_INT32_ARRAY:
164 byteBufferWriter_.WriteData(data.GetInt32ArrayValue());
165 break;
166 case BufferDataType::TYPE_MAP:
167 byteBufferWriter_.WriteData(data.GetMapValue());
168 break;
169 case BufferDataType::TYPE_SET:
170 byteBufferWriter_.WriteData(data.GetSetValue());
171 break;
172 case BufferDataType::TYPE_FUNCTION:
173 byteBufferWriter_.WriteData(data.GetFunctionValue());
174 break;
175 case BufferDataType::TYPE_OBJECT:
176 byteBufferWriter_.WriteData(data.GetObjectValue());
177 break;
178 default:
179 break;
180 }
181 }
182
183 } // namespace OHOS::Ace::Framework