-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDecOverflowWhenComparing.c
More file actions
55 lines (43 loc) · 1.14 KB
/
DecOverflowWhenComparing.c
File metadata and controls
55 lines (43 loc) · 1.14 KB
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// from https://github.com/apple-oss-distributions/Libinfo/blob/9fce29e5c5edc15d3ecea55116ca17d3f6350603/lookup.subproj/mdns_module.c#L1033C1-L1079C2
char* _mdns_parse_domain_name(const uint8_t *data, uint32_t datalen)
{
int i = 0, j = 0;
uint32_t domainlen = 0;
char *domain = NULL;
if ((data == NULL) || (datalen == 0)) return NULL;
while (datalen-- > 0)
{
uint32_t len = data[i++];
domainlen += (len + 1);
domain = reallocf(domain, domainlen);
if (domain == NULL) return NULL;
if (len == 0) break; // DNS root (NUL)
if (j > 0)
{
domain[j++] = datalen ? '.' : '\0';
}
while ((len-- > 0) && (0 != datalen--))
{
if (data[i] == '.')
{
/* special case: escape the '.' with a '\' */
domain = reallocf(domain, ++domainlen);
if (domain == NULL) return NULL;
domain[j++] = '\\';
}
domain[j++] = data[i++];
}
}
domain[j] = '\0';
return domain;
}
int main() {
const uint16_t datalen = 128;
uint8_t data[datalen] = {};
memcpy(data, "\x04quildu\x03xyz\x00", 11);
_mdns_parse_domain_name(data, datalen);
}