This repository has been archived by the owner on Mar 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathFirstPartyHost.h
129 lines (109 loc) · 4.01 KB
/
FirstPartyHost.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
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
/* Copyright (c) 2016 Sergiy Zhukovs'kyy. Distributed under the MPL2 license.
* 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 FIRST_PARTY_HOST_H_
#define FIRST_PARTY_HOST_H_
#include <stdint.h>
#include <stdio.h>
#include <string.h>
struct ST_FIRST_PARTY_HOST
{
public:
ST_FIRST_PARTY_HOST():
sFirstPartyHost(nullptr),
sThirdPartyHosts(nullptr) {
}
ST_FIRST_PARTY_HOST(const ST_FIRST_PARTY_HOST &other) {
if (nullptr != other.sFirstPartyHost) {
sFirstPartyHost = new char[strlen(other.sFirstPartyHost) + 1];
strcpy(sFirstPartyHost, other.sFirstPartyHost);
}
if (nullptr != other.sThirdPartyHosts) {
sThirdPartyHosts = new char[strlen(other.sThirdPartyHosts) + 1];
strcpy(sThirdPartyHosts, other.sThirdPartyHosts);
}
}
~ST_FIRST_PARTY_HOST() {
if (nullptr != sFirstPartyHost) {
delete []sFirstPartyHost;
}
if (nullptr != sThirdPartyHosts) {
delete []sThirdPartyHosts;
}
}
uint64_t GetHash() const;
bool operator==(const ST_FIRST_PARTY_HOST &rhs) const {
int hostLen = static_cast<int>(strlen(sFirstPartyHost));
int rhsHostLen = static_cast<int>(strlen(rhs.sFirstPartyHost));
if (hostLen != rhsHostLen || 0 != memcmp(sFirstPartyHost, rhs.sFirstPartyHost, hostLen)) {
return false;
}
return true;
}
// Nothing needs to be updated when a host is added multiple times
void Update(const ST_FIRST_PARTY_HOST&) {}
uint32_t Serialize(char* buffer) {
uint32_t size = 0;
unsigned int pos = 0;
char sz[32];
uint32_t dataLenSize = 1 + snprintf(sz, sizeof(sz), "%x", (unsigned int)strlen(sFirstPartyHost));
if (buffer) {
memcpy(buffer + pos, sz, dataLenSize);
}
pos += dataLenSize;
if (buffer) {
memcpy(buffer + pos, sFirstPartyHost, strlen(sFirstPartyHost));
}
pos += static_cast<uint32_t>(strlen(sFirstPartyHost));
dataLenSize = 1 + snprintf(sz, sizeof(sz), "%x", (unsigned int)strlen(sThirdPartyHosts));
if (buffer) {
memcpy(buffer + pos, sz, dataLenSize);
}
pos += dataLenSize;
if (buffer) {
memcpy(buffer + pos, sThirdPartyHosts, strlen(sThirdPartyHosts));
}
size = pos + (unsigned int)strlen(sThirdPartyHosts);
return size;
}
uint32_t Deserialize(char *buffer, uint32_t bufferSize) {
uint32_t size = 0;
if (!buffer || 0 == bufferSize) {
return size;
}
// Get first party host
unsigned int firstPartyHostLength = 0;
sscanf(buffer, "%x", &firstPartyHostLength);
if (sFirstPartyHost) {
delete []sFirstPartyHost;
}
size = (unsigned int)strlen(buffer) + 1;
sFirstPartyHost = new char[firstPartyHostLength + 1];
if (!sFirstPartyHost) {
return size;
}
memcpy(sFirstPartyHost, buffer + size, firstPartyHostLength);
sFirstPartyHost[firstPartyHostLength] = '\0';
size += firstPartyHostLength;
// Get third party hosts
unsigned int thirdPartyHostLength = 0;
sscanf(buffer + size, "%x", &thirdPartyHostLength);
if (sThirdPartyHosts) {
delete []sThirdPartyHosts;
}
size += static_cast<uint32_t>(strlen(buffer + size) + 1);
sThirdPartyHosts = new char[thirdPartyHostLength + 1];
if (!sThirdPartyHosts) {
return size;
}
memcpy(sThirdPartyHosts, buffer + size, thirdPartyHostLength);
sThirdPartyHosts[thirdPartyHostLength] = '\0';
size += thirdPartyHostLength;
return size;
}
char* sFirstPartyHost;
// Third party hosts are comma separated
char* sThirdPartyHosts;
};
#endif //FIRST_PARTY_HOST_H_