1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
#include "local_ads/campaign_serialization.hpp"
#include "coding/byte_stream.hpp"
#include "coding/reader.hpp"
#include "coding/varint.hpp"
#include "coding/write_to_sink.hpp"
#include "base/exception.hpp"
#include "base/logging.hpp"
#include "base/stl_add.hpp"
#include <climits>
#include <cstdint>
#include <type_traits>
#include <vector>
namespace
{
using namespace local_ads;
DECLARE_EXCEPTION(UnknownVersion, RootException);
auto const kHalfByteShift = CHAR_BIT / 2;
auto const kHalfByteMaxValue = 15;
auto const kLowerMask = 0x0F;
auto const kUpperMask = 0xF0;
auto const kMinZoomLevel = 10;
auto const kMaxZoomLevel = 17;
auto const kMaxPriority = 7;
template <typename Integral, typename Source,
typename std::enable_if<std::is_integral<Integral>::value, void *>::type = nullptr>
std::vector<Integral> ReadVarUintArray(Source & s, size_t chunksNumber)
{
std::vector<Integral> result;
for (size_t i = 0; i < chunksNumber; ++i)
result.emplace_back(static_cast<Integral>(ReadVarUint<uint64_t>(s)));
return result;
}
template <typename Integral, typename Source>
std::vector<Integral> ReadArray(Source & s, size_t chunksNumber)
{
std::vector<Integral> result;
for (size_t i = 0; i < chunksNumber; ++i)
{
result.emplace_back(ReadPrimitiveFromSource<Integral>(s));
}
return result;
}
std::vector<uint8_t> SerializeV1(std::vector<Campaign> const & campaigns)
{
std::vector<uint8_t> buff;
PushBackByteSink<decltype(buff)> dst(buff);
WriteToSink(dst, Version::V1);
WriteToSink(dst, campaigns.size());
for (auto const & c : campaigns)
WriteVarUint(dst, c.m_featureId);
for (auto const & c : campaigns)
WriteVarUint(dst, c.m_iconId);
for (auto const & c : campaigns)
WriteVarUint(dst, c.m_daysBeforeExpired);
return buff;
}
std::vector<Campaign> DeserializeV1(std::vector<uint8_t> const & bytes)
{
ReaderSource<MemReaderWithExceptions> src({bytes.data(), bytes.size()});
CHECK_EQUAL(ReadPrimitiveFromSource<Version>(src), Version::V1, ());
auto const chunksNumber = ReadPrimitiveFromSource<uint64_t>(src);
auto const featureIds = ReadVarUintArray<uint32_t>(src, chunksNumber);
auto const icons = ReadVarUintArray<uint16_t>(src, chunksNumber);
auto const expirations = ReadVarUintArray<uint8_t>(src, chunksNumber);
std::vector<Campaign> campaigns;
campaigns.reserve(chunksNumber);
for (size_t i = 0; i < chunksNumber; ++i)
{
campaigns.emplace_back(featureIds[i], icons[i], expirations[i]);
}
return campaigns;
}
uint8_t ZoomIndex(uint8_t zoomValue) { return zoomValue - kMinZoomLevel; }
uint8_t ZoomValue(uint8_t zoomIndex) { return zoomIndex + kMinZoomLevel; }
uint8_t PackZoomAndPriority(uint8_t minZoomLevel, uint8_t priority)
{
ASSERT_GREATER_OR_EQUAL(minZoomLevel, kMinZoomLevel, ("Unsupported zoom level"));
ASSERT_LESS_OR_EQUAL(minZoomLevel, kMaxZoomLevel, ("Unsupported zoom level"));
ASSERT_LESS_OR_EQUAL(priority, kMaxPriority, ("Unsupported priority value"));
auto const zoomIndex = ZoomIndex(minZoomLevel);
ASSERT_LESS_OR_EQUAL(zoomIndex, kHalfByteMaxValue, ());
ASSERT_LESS_OR_EQUAL(priority, kHalfByteMaxValue, ());
// Pack zoom and priority into single byte.
return (zoomIndex & kLowerMask) | ((priority << kHalfByteShift) & kUpperMask);
}
uint8_t UnpackZoom(uint8_t src)
{
return ZoomValue(src & kLowerMask);
}
uint8_t UnpackPriority(uint8_t src)
{
return (src >> kHalfByteShift) & kLowerMask;
}
std::vector<uint8_t> SerializeV2(std::vector<Campaign> const & campaigns)
{
std::vector<uint8_t> buff;
PushBackByteSink<decltype(buff)> dst(buff);
WriteToSink(dst, Version::V2);
WriteToSink(dst, campaigns.size());
for (auto const & c : campaigns)
WriteVarUint(dst, c.m_featureId);
for (auto const & c : campaigns)
WriteVarUint(dst, c.m_iconId);
for (auto const & c : campaigns)
WriteVarUint(dst, c.m_daysBeforeExpired);
for (auto const & c : campaigns)
WriteToSink(dst, PackZoomAndPriority(c.m_minZoomLevel, c.m_priority));
return buff;
}
std::vector<Campaign> DeserializeV2(std::vector<uint8_t> const & bytes)
{
ReaderSource<MemReaderWithExceptions> src({bytes.data(), bytes.size()});
CHECK_EQUAL(ReadPrimitiveFromSource<Version>(src), Version::V2, ());
auto const chunksNumber = ReadPrimitiveFromSource<uint64_t>(src);
auto const featureIds = ReadVarUintArray<uint32_t>(src, chunksNumber);
auto const icons = ReadVarUintArray<uint16_t>(src, chunksNumber);
auto const expirations = ReadVarUintArray<uint8_t>(src, chunksNumber);
auto const zoomAndPriority = ReadArray<uint8_t>(src, chunksNumber);
std::vector<Campaign> campaigns;
campaigns.reserve(chunksNumber);
for (size_t i = 0; i < chunksNumber; ++i)
{
campaigns.emplace_back(featureIds[i], icons[i], expirations[i],
UnpackZoom(zoomAndPriority[i]),
UnpackPriority(zoomAndPriority[i]));
ASSERT_GREATER_OR_EQUAL(campaigns.back().m_minZoomLevel, kMinZoomLevel,
("Unsupported zoom level"));
ASSERT_LESS_OR_EQUAL(campaigns.back().m_minZoomLevel, kMaxZoomLevel,
("Unsupported zoom level"));
ASSERT_LESS_OR_EQUAL(campaigns.back().m_priority, kMaxPriority, ("Unsupported priority value"));
}
return campaigns;
}
} // namespace
namespace local_ads
{
std::vector<uint8_t> Serialize(std::vector<Campaign> const & campaigns, Version const version)
{
try
{
switch (version)
{
case Version::V1: return SerializeV1(campaigns);
case Version::V2: return SerializeV2(campaigns);
default: MYTHROW(UnknownVersion, (version));
}
}
catch (RootException const & e)
{
LOG(LERROR, ("Cannot to serialize campaigns", e.what(), e.Msg()));
}
return {};
}
std::vector<uint8_t> Serialize(std::vector<Campaign> const & campaigns)
{
return Serialize(campaigns, Version::Latest);
}
std::vector<Campaign> Deserialize(std::vector<uint8_t> const & bytes)
{
try
{
ReaderSource<MemReaderWithExceptions> src({bytes.data(), bytes.size()});
auto const version = ReadPrimitiveFromSource<Version>(src);
switch (version)
{
case Version::V1: return DeserializeV1(bytes);
case Version::V2: return DeserializeV2(bytes);
default: MYTHROW(UnknownVersion, (version));
}
}
catch (RootException const & e)
{
LOG(LERROR, ("Cannot to deserialize received data", e.what(), e.Msg()));
}
catch (std::bad_alloc const & e)
{
LOG(LERROR, ("Cannot to allocate memory for local ads campaigns", e.what()));
}
return {};
}
std::string DebugPrint(local_ads::Version version)
{
using local_ads::Version;
switch (version)
{
case Version::Unknown: return "Unknown";
case Version::V1: return "Version 1";
case Version::V2: return "Version 2";
default: ASSERT(false, ("Unknown version"));
}
return {};
}
} // namespace local_ads
|