-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension_whitelist_data.h
94 lines (73 loc) · 2.36 KB
/
extension_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 EXTENSION_WHITELIST_DATA_H_
#define EXTENSION_WHITELIST_DATA_H_
#include <stdint.h>
#include <stdio.h>
#include <string.h>
struct ST_EXTENSION_WHITELIST_DATA {
public:
ST_EXTENSION_WHITELIST_DATA():
sExtensionID(nullptr) {
}
ST_EXTENSION_WHITELIST_DATA(const ST_EXTENSION_WHITELIST_DATA &other) {
if (nullptr == other.sExtensionID) {
return;
}
sExtensionID = new char[strlen(other.sExtensionID) + 1];
strcpy(sExtensionID, other.sExtensionID);
}
~ST_EXTENSION_WHITELIST_DATA() {
if (nullptr != sExtensionID) {
delete []sExtensionID;
}
}
uint64_t GetHash() const;
bool operator==(const ST_EXTENSION_WHITELIST_DATA &rhs) const {
int extensionLen = static_cast<int>(strlen(sExtensionID));
int rhsExtensionIDLen = static_cast<int>(strlen(rhs.sExtensionID));
if (extensionLen != rhsExtensionIDLen) {
return false;
}
return !memcmp(sExtensionID, rhs.sExtensionID, extensionLen);
}
// Nothing needs to be updated when an extension is added multiple times
void Update(const ST_EXTENSION_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(sExtensionID));
if (buffer) {
memcpy(buffer + size, sz, dataLenSize);
}
size += dataLenSize;
if (buffer) {
memcpy(buffer + size, sExtensionID, strlen(sExtensionID));
}
size += static_cast<uint32_t>(strlen(sExtensionID));
return size;
}
uint32_t Deserialize(char *buffer, uint32_t bufferSize) {
uint32_t size = 0;
if (!buffer || 0 == bufferSize) {
return size;
}
unsigned int extensionLength = 0;
sscanf(buffer, "%x", &extensionLength);
if (sExtensionID) {
delete []sExtensionID;
}
size = static_cast<uint32_t>(strlen(buffer) + 1);
sExtensionID = new char[extensionLength + 1];
if (!sExtensionID) {
return size;
}
memcpy(sExtensionID, buffer + size, extensionLength);
sExtensionID[extensionLength] = '\0';
size += extensionLength;
return size;
}
char* sExtensionID;
};
#endif // EXTENSION_WHITELIST_DATA_H_