forked from google/xsecurelock
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuf_util.c
More file actions
71 lines (60 loc) · 1.42 KB
/
Copy pathbuf_util.c
File metadata and controls
71 lines (60 loc) · 1.42 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: Apache-2.0
#include "config.h"
#include "buf_util.h"
#include <errno.h>
#include <string.h>
void BufWriterInit(struct BufWriter *w, char *buf, size_t size) {
if (w == NULL) {
return;
}
w->ptr = buf;
w->remaining = size;
if (buf != NULL && size > 0) {
buf[0] = '\0';
}
}
int BufWriteBytes(struct BufWriter *w, const void *src, size_t len) {
if (w == NULL || src == NULL || w->ptr == NULL) {
errno = EINVAL;
return -1;
}
if (w->remaining == 0 || len > w->remaining - 1) {
errno = ENOSPC;
return -1;
}
if (len != 0) {
memcpy(w->ptr, src, len);
}
w->ptr[len] = '\0';
w->ptr += len;
w->remaining -= len;
return 0;
}
int BufWriteCString(struct BufWriter *w, const char *src) {
if (src == NULL) {
errno = EINVAL;
return -1;
}
return BufWriteBytes(w, src, strlen(src));
}
void BufWriteBytesTruncated(struct BufWriter *w, const void *src, size_t len) {
if (w == NULL || src == NULL || w->ptr == NULL || w->remaining == 0) {
return;
}
size_t write_len = len;
if (write_len > w->remaining - 1) {
write_len = w->remaining - 1;
}
if (write_len != 0) {
memcpy(w->ptr, src, write_len);
}
w->ptr[write_len] = '\0';
w->ptr += write_len;
w->remaining -= write_len;
}
void BufWriteCStringTruncated(struct BufWriter *w, const char *src) {
if (src == NULL) {
return;
}
BufWriteBytesTruncated(w, src, strlen(src));
}