]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5cea57f3 | 2 | #include "debug.h" |
7ed0958a | 3 | #include "util.h" |
e7f01d1e | 4 | #include <linux/kernel.h> |
a43783ae | 5 | #include <errno.h> |
07800601 | 6 | |
07800601 IM |
7 | /* |
8 | * Used as the default ->buf value, so that people can always assume | |
9 | * buf is non NULL and ->buf is NUL terminated even for a freshly | |
10 | * initialized strbuf. | |
11 | */ | |
12 | char strbuf_slopbuf[1]; | |
13 | ||
5cea57f3 | 14 | int strbuf_init(struct strbuf *sb, ssize_t hint) |
07800601 IM |
15 | { |
16 | sb->alloc = sb->len = 0; | |
17 | sb->buf = strbuf_slopbuf; | |
18 | if (hint) | |
5cea57f3 MH |
19 | return strbuf_grow(sb, hint); |
20 | return 0; | |
07800601 IM |
21 | } |
22 | ||
23 | void strbuf_release(struct strbuf *sb) | |
24 | { | |
25 | if (sb->alloc) { | |
74cf249d | 26 | zfree(&sb->buf); |
07800601 IM |
27 | strbuf_init(sb, 0); |
28 | } | |
29 | } | |
30 | ||
31 | char *strbuf_detach(struct strbuf *sb, size_t *sz) | |
32 | { | |
33 | char *res = sb->alloc ? sb->buf : NULL; | |
34 | if (sz) | |
35 | *sz = sb->len; | |
36 | strbuf_init(sb, 0); | |
37 | return res; | |
38 | } | |
39 | ||
5cea57f3 | 40 | int strbuf_grow(struct strbuf *sb, size_t extra) |
07800601 | 41 | { |
5cea57f3 MH |
42 | char *buf; |
43 | size_t nr = sb->len + extra + 1; | |
44 | ||
45 | if (nr < sb->alloc) | |
46 | return 0; | |
47 | ||
48 | if (nr <= sb->len) | |
49 | return -E2BIG; | |
50 | ||
51 | if (alloc_nr(sb->alloc) > nr) | |
52 | nr = alloc_nr(sb->alloc); | |
53 | ||
54 | /* | |
55 | * Note that sb->buf == strbuf_slopbuf if sb->alloc == 0, and it is | |
56 | * a static variable. Thus we have to avoid passing it to realloc. | |
57 | */ | |
58 | buf = realloc(sb->alloc ? sb->buf : NULL, nr * sizeof(*buf)); | |
59 | if (!buf) | |
60 | return -ENOMEM; | |
61 | ||
62 | sb->buf = buf; | |
63 | sb->alloc = nr; | |
64 | return 0; | |
07800601 IM |
65 | } |
66 | ||
5cea57f3 | 67 | int strbuf_addch(struct strbuf *sb, int c) |
0741208a | 68 | { |
5cea57f3 MH |
69 | int ret = strbuf_grow(sb, 1); |
70 | if (ret) | |
71 | return ret; | |
72 | ||
0741208a ACM |
73 | sb->buf[sb->len++] = c; |
74 | sb->buf[sb->len] = '\0'; | |
5cea57f3 | 75 | return 0; |
0741208a ACM |
76 | } |
77 | ||
5cea57f3 | 78 | int strbuf_add(struct strbuf *sb, const void *data, size_t len) |
07800601 | 79 | { |
5cea57f3 MH |
80 | int ret = strbuf_grow(sb, len); |
81 | if (ret) | |
82 | return ret; | |
83 | ||
07800601 | 84 | memcpy(sb->buf + sb->len, data, len); |
5cea57f3 | 85 | return strbuf_setlen(sb, sb->len + len); |
07800601 IM |
86 | } |
87 | ||
5cea57f3 | 88 | static int strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap) |
07800601 | 89 | { |
5cea57f3 | 90 | int len, ret; |
c7118369 | 91 | va_list ap_saved; |
07800601 | 92 | |
5cea57f3 MH |
93 | if (!strbuf_avail(sb)) { |
94 | ret = strbuf_grow(sb, 64); | |
95 | if (ret) | |
96 | return ret; | |
97 | } | |
c7118369 NK |
98 | |
99 | va_copy(ap_saved, ap); | |
f787d951 | 100 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); |
07800601 | 101 | if (len < 0) |
5cea57f3 | 102 | return len; |
07800601 | 103 | if (len > strbuf_avail(sb)) { |
5cea57f3 MH |
104 | ret = strbuf_grow(sb, len); |
105 | if (ret) | |
106 | return ret; | |
c7118369 NK |
107 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap_saved); |
108 | va_end(ap_saved); | |
07800601 | 109 | if (len > strbuf_avail(sb)) { |
5cea57f3 MH |
110 | pr_debug("this should not happen, your vsnprintf is broken"); |
111 | return -EINVAL; | |
07800601 IM |
112 | } |
113 | } | |
5cea57f3 | 114 | return strbuf_setlen(sb, sb->len + len); |
07800601 IM |
115 | } |
116 | ||
5cea57f3 | 117 | int strbuf_addf(struct strbuf *sb, const char *fmt, ...) |
c7118369 NK |
118 | { |
119 | va_list ap; | |
5cea57f3 | 120 | int ret; |
c7118369 NK |
121 | |
122 | va_start(ap, fmt); | |
5cea57f3 | 123 | ret = strbuf_addv(sb, fmt, ap); |
c7118369 | 124 | va_end(ap); |
5cea57f3 | 125 | return ret; |
c7118369 NK |
126 | } |
127 | ||
f37a291c | 128 | ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint) |
07800601 IM |
129 | { |
130 | size_t oldlen = sb->len; | |
131 | size_t oldalloc = sb->alloc; | |
5cea57f3 MH |
132 | int ret; |
133 | ||
134 | ret = strbuf_grow(sb, hint ? hint : 8192); | |
135 | if (ret) | |
136 | return ret; | |
07800601 | 137 | |
07800601 IM |
138 | for (;;) { |
139 | ssize_t cnt; | |
140 | ||
141 | cnt = read(fd, sb->buf + sb->len, sb->alloc - sb->len - 1); | |
142 | if (cnt < 0) { | |
143 | if (oldalloc == 0) | |
144 | strbuf_release(sb); | |
145 | else | |
146 | strbuf_setlen(sb, oldlen); | |
5cea57f3 | 147 | return cnt; |
07800601 IM |
148 | } |
149 | if (!cnt) | |
150 | break; | |
151 | sb->len += cnt; | |
5cea57f3 MH |
152 | ret = strbuf_grow(sb, 8192); |
153 | if (ret) | |
154 | return ret; | |
07800601 IM |
155 | } |
156 | ||
157 | sb->buf[sb->len] = '\0'; | |
158 | return sb->len - oldlen; | |
159 | } |