-
Notifications
You must be signed in to change notification settings - Fork 7
/
pure_routines.h
219 lines (204 loc) · 5.81 KB
/
pure_routines.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
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
int pure_eq(
const uint8_t* buffer,
const uint64_t buffer_size, // The absolute length of the entire buffer.
const uint64_t buffer_offset, // The offset from which to start comparing.
const uint8_t* string,
const uint64_t string_size
) {
assert(buffer_offset <= buffer_size);
assert(string_size > 0);
if (buffer_offset + string_size > buffer_size) return 0;
return memcmp(buffer + buffer_offset, string, (size_t) string_size) == 0;
}
int pure_free(uint8_t** data, uint64_t* data_size) {
if (*data_size == 0) {
assert(*data == NULL);
// We never call malloc for 0.
// We always assert that `required` is greater than 0.
// We may therefore use `data_size == 0` to indicate the lack of allocation.
} else {
assert(*data != NULL);
free(*data);
*data = NULL;
*data_size = 0;
}
return 0;
}
int pure_overflow(
const uint64_t offset,
const uint64_t length,
const uint64_t available
) {
if (available < length) return 1;
if (offset > available - length) return 1;
return 0;
}
int pure_realloc(
uint8_t** data,
uint64_t* data_size,
const uint64_t required
) {
assert(required > 0); // N.B. See comment in pure_free().
assert(required <= SIZE_MAX);
if (*data_size > 0 && *data_size < required) {
pure_free(data, data_size);
assert(*data == NULL);
assert(*data_size == 0);
}
if (*data_size == 0) {
assert(*data == NULL);
uint64_t size = required;
if (size < PURE_MALLOC_MIN) size = PURE_MALLOC_MIN;
*data = (uint8_t*) malloc(size);
if (*data == NULL) return PURE_E_MALLOC;
*data_size = size;
}
assert(*data != NULL);
assert(*data_size >= required);
return 0;
}
int pure_search(
const uint8_t* buffer,
const uint64_t buffer_size,
const uint64_t search_offset,
uint64_t search_size,
const uint8_t* string,
const uint64_t string_size,
uint64_t* offset
) {
assert(*offset == 0);
assert(string_size > 0);
if (search_offset >= buffer_size) return PURE_E_STRING_NOT_FOUND;
if (search_offset + search_size > buffer_size) {
search_size = buffer_size - search_offset;
}
assert(search_offset + search_size <= buffer_size);
if (search_size < string_size) return PURE_E_STRING_NOT_FOUND;
uint64_t index = search_offset;
uint64_t length = search_offset + search_size - string_size;
assert(length + string_size <= buffer_size);
while (index < length) {
if (
// Avoid a function call most of the time:
buffer[index] == string[0] &&
// Check the string in full:
pure_eq(
buffer,
buffer_size,
index,
string,
string_size
)
) {
*offset = index;
return 0;
}
index++;
}
return PURE_E_STRING_NOT_FOUND;
}
uint16_t pure_u16(const uint8_t* buffer) {
const uint8_t a = buffer[0];
const uint8_t b = buffer[1];
return (a << 0) | (b << 8);
}
uint32_t pure_u32(const uint8_t* buffer) {
const uint8_t a = buffer[0];
const uint8_t b = buffer[1];
const uint8_t c = buffer[2];
const uint8_t d = buffer[3];
return (a << 0) | (b << 8) | (c << 16) | (d << 24);
}
uint64_t pure_u64(const uint8_t* buffer) {
const uint32_t a = pure_u32(buffer + 0);
const uint32_t b = pure_u32(buffer + 4);
return ((uint64_t) b << 32) + a;
}
int pure_zeroes(
const uint8_t* buffer,
uint64_t offset,
const uint64_t length
) {
assert(offset <= length);
// TO DO: Optimize: Perform 64-bit comparisons:
while (offset < length) {
if (buffer[offset++] != 0) return 0;
}
return 1;
}
// Find end of component delimited by slash or EOF.
uint64_t pure_path_component_index(
const uint8_t* path,
uint64_t index,
const uint64_t length
) {
assert(index <= length);
while (index < length) {
if (path[index] == PURE_BACKSLASH || path[index] == PURE_FORWARD_SLASH) {
return index;
} else {
index++;
}
}
return index;
}
int pure_path_component_overflow(const uint8_t* path, const uint64_t length) {
if (length < PURE_PATH_COMPONENT_MAX) return 0;
uint64_t start = 0;
while (start < length) {
uint64_t end = pure_path_component_index(path, start, length);
if (end - start > PURE_PATH_COMPONENT_MAX) return 1;
start = end + 1;
}
return 0;
}
int pure_path_control_characters_iconr(
const uint8_t* path,
const uint64_t length
) {
if (length < PURE_L_ICONR) return 0;
uint64_t offset = length - PURE_L_ICONR;
if (!pure_eq(path, length, offset, PURE_S_ICONR, PURE_L_ICONR)) return 0;
return (
offset == 0 ||
// Do not fall for partial path component matches:
path[offset - 1] == PURE_BACKSLASH ||
path[offset - 1] == PURE_FORWARD_SLASH
);
}
int pure_path_control_characters(const uint8_t* path, const uint64_t length) {
// We want to check for control characters, except the "\r" in "Icon\r" files:
uint64_t excluding_iconr_length = (uint64_t) length;
if (pure_path_control_characters_iconr(path, length)) {
assert(excluding_iconr_length >= PURE_L_ICONR);
excluding_iconr_length -= PURE_L_ICONR;
}
for (uint64_t index = 0; index < excluding_iconr_length; index++) {
if (PURE_CONTROL_CHARACTER[path[index]]) return 1;
}
return 0;
}
int pure_path_double_dots(const uint8_t* path, const uint64_t length) {
uint64_t start = 0;
while (start < length) {
uint64_t end = pure_path_component_index(path, start, length);
// Check two-character components for double dots (".."):
if (end - start == 2 && path[start + 0] == 46 && path[start + 1] == 46) {
return 1;
}
start = end + 1;
}
return 0;
}
int pure_path_drive(const uint8_t* path, const uint64_t length) {
return (
length >= 2 &&
path[1] == 58 && // ":"
path[0] >= 65 && // "A"
path[0] <= 122 // "z"
);
}
int pure_path_relative(const uint8_t* path, const uint64_t length) {
if (length == 0) return 0;
return path[0] == PURE_BACKSLASH || path[0] == PURE_FORWARD_SLASH;
}