This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
autoplay_whitelist_data.h
94 lines (73 loc) · 2.35 KB
/
autoplay_whitelist_data.h
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef AUTOPLAY_WHITELIST_DATA_H_
#define AUTOPLAY_WHITELIST_DATA_H_
#include <stdint.h>
#include <stdio.h>
#include <string.h>
struct ST_AUTOPLAY_WHITELIST_DATA {
public:
ST_AUTOPLAY_WHITELIST_DATA():
sHost(nullptr) {
}
ST_AUTOPLAY_WHITELIST_DATA(const ST_AUTOPLAY_WHITELIST_DATA &other) {
if (nullptr == other.sHost) {
return;
}
sHost = new char[strlen(other.sHost) + 1];
strcpy(sHost, other.sHost);
}
~ST_AUTOPLAY_WHITELIST_DATA() {
if (nullptr != sHost) {
delete []sHost;
}
}
uint64_t GetHash() const;
bool operator==(const ST_AUTOPLAY_WHITELIST_DATA &rhs) const {
int hostLen = static_cast<int>(strlen(sHost));
int rhsHostLen = static_cast<int>(strlen(rhs.sHost));
if (hostLen != rhsHostLen) {
return false;
}
return !memcmp(sHost, rhs.sHost, hostLen);
}
// Nothing needs to be updated when a host is added multiple times
void Update(const ST_AUTOPLAY_WHITELIST_DATA &) {}
uint32_t Serialize(char* buffer) {
uint32_t size = 0;
char sz[32];
uint32_t dataLenSize = 1 + snprintf(sz, sizeof(sz), "%x", (unsigned int)strlen(sHost));
if (buffer) {
memcpy(buffer + size, sz, dataLenSize);
}
size += dataLenSize;
if (buffer) {
memcpy(buffer + size, sHost, strlen(sHost));
}
size += static_cast<uint32_t>(strlen(sHost));
return size;
}
uint32_t Deserialize(char *buffer, uint32_t bufferSize) {
uint32_t size = 0;
if (!buffer || 0 == bufferSize) {
return size;
}
unsigned int hostLength = 0;
sscanf(buffer, "%x", &hostLength);
if (sHost) {
delete []sHost;
}
size = static_cast<uint32_t>(strlen(buffer) + 1);
sHost = new char[hostLength + 1];
if (!sHost) {
return size;
}
memcpy(sHost, buffer + size, hostLength);
sHost[hostLength] = '\0';
size += hostLength;
return size;
}
char* sHost;
};
#endif // AUTOPLAY_WHITELIST_DATA_H_