-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
74 lines (64 loc) · 1.8 KB
/
util.c
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
#include <assert.h>
#include <inttypes.h> /* for PRIx8, etc. */
#include <stdbool.h>
#include <stddef.h> /* size_t */
#include <stdio.h> /* sscanf */
#include <stdlib.h> /* malloc */
#include <string.h> /* strlen */
#include <sys/param.h> /* for MIN, MAX */
/* If `str` is the empty string, then store NULL at `bufp` and 0 at
* `buflenp`, and return 0.
*
* If `str` consists of one or more hexadecimal octets,
* [0-9a-fA-F][0-9a-fA-F], separated by colons, then parse the octets
* into bytes, storing them in a buffer allocated on the heap. Store a
* pointer to the buffer at `bufp` and the number of bytes at `buflenp`,
* and return 0.
*
* If `str` contains any other string, or if memory on the heap cannot
* be allocated, then store NULL at `bufp` and 0 at `buflenp`, and
* return -1.
*/
int
colon_separated_octets_to_bytes(const char *str, uint8_t **bufp,
size_t *buflenp)
{
uint8_t *buf;
size_t buflen, noctets;
int i = 0, nread, rc;
*bufp = NULL;
*buflenp = 0;
noctets = (strlen(str) + 1) / 3;
if (noctets < 1)
return 0;
if ((buf = malloc(noctets)) == NULL)
return -1;
rc = sscanf(&str[i], "%02" SCNx8 "%n", &buf[0], &nread);
if (rc == EOF) {
free(buf);
return 0;
} else if (rc != 1) {
free(buf);
return -1;
}
for (buflen = 1, i = nread;
(rc = sscanf(&str[i], ":%02" SCNx8 "%n", &buf[buflen], &nread)) == 1;
i += nread)
buflen++;
if (rc != EOF || str[i] != '\0') {
free(buf);
return -1;
}
assert(buflen == noctets);
*bufp = buf;
*buflenp = buflen;
return 0;
}
/* Return twice x or SIZE_MAX, whichever is smaller. Protects
* against wraparound.
*/
size_t
twice_or_max(size_t x)
{
return x + MIN(SIZE_MAX - x, x);
}