| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 | 
|  | 2 | #include "debug.h" | 
|  | 3 | #include "util.h" | 
|  | 4 | #include <linux/kernel.h> | 
|  | 5 | #include <errno.h> | 
|  | 6 |  | 
|  | 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 |  | 
|  | 14 | int strbuf_init(struct strbuf *sb, ssize_t hint) | 
|  | 15 | { | 
|  | 16 | sb->alloc = sb->len = 0; | 
|  | 17 | sb->buf = strbuf_slopbuf; | 
|  | 18 | if (hint) | 
|  | 19 | return strbuf_grow(sb, hint); | 
|  | 20 | return 0; | 
|  | 21 | } | 
|  | 22 |  | 
|  | 23 | void strbuf_release(struct strbuf *sb) | 
|  | 24 | { | 
|  | 25 | if (sb->alloc) { | 
|  | 26 | zfree(&sb->buf); | 
|  | 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 |  | 
|  | 40 | int strbuf_grow(struct strbuf *sb, size_t extra) | 
|  | 41 | { | 
|  | 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; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | int strbuf_addch(struct strbuf *sb, int c) | 
|  | 68 | { | 
|  | 69 | int ret = strbuf_grow(sb, 1); | 
|  | 70 | if (ret) | 
|  | 71 | return ret; | 
|  | 72 |  | 
|  | 73 | sb->buf[sb->len++] = c; | 
|  | 74 | sb->buf[sb->len] = '\0'; | 
|  | 75 | return 0; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | int strbuf_add(struct strbuf *sb, const void *data, size_t len) | 
|  | 79 | { | 
|  | 80 | int ret = strbuf_grow(sb, len); | 
|  | 81 | if (ret) | 
|  | 82 | return ret; | 
|  | 83 |  | 
|  | 84 | memcpy(sb->buf + sb->len, data, len); | 
|  | 85 | return strbuf_setlen(sb, sb->len + len); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | static int strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap) | 
|  | 89 | { | 
|  | 90 | int len, ret; | 
|  | 91 | va_list ap_saved; | 
|  | 92 |  | 
|  | 93 | if (!strbuf_avail(sb)) { | 
|  | 94 | ret = strbuf_grow(sb, 64); | 
|  | 95 | if (ret) | 
|  | 96 | return ret; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | va_copy(ap_saved, ap); | 
|  | 100 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); | 
|  | 101 | if (len < 0) { | 
|  | 102 | va_end(ap_saved); | 
|  | 103 | return len; | 
|  | 104 | } | 
|  | 105 | if (len > strbuf_avail(sb)) { | 
|  | 106 | ret = strbuf_grow(sb, len); | 
|  | 107 | if (ret) { | 
|  | 108 | va_end(ap_saved); | 
|  | 109 | return ret; | 
|  | 110 | } | 
|  | 111 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap_saved); | 
|  | 112 | if (len > strbuf_avail(sb)) { | 
|  | 113 | pr_debug("this should not happen, your vsnprintf is broken"); | 
|  | 114 | va_end(ap_saved); | 
|  | 115 | return -EINVAL; | 
|  | 116 | } | 
|  | 117 | } | 
|  | 118 | va_end(ap_saved); | 
|  | 119 | return strbuf_setlen(sb, sb->len + len); | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | int strbuf_addf(struct strbuf *sb, const char *fmt, ...) | 
|  | 123 | { | 
|  | 124 | va_list ap; | 
|  | 125 | int ret; | 
|  | 126 |  | 
|  | 127 | va_start(ap, fmt); | 
|  | 128 | ret = strbuf_addv(sb, fmt, ap); | 
|  | 129 | va_end(ap); | 
|  | 130 | return ret; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint) | 
|  | 134 | { | 
|  | 135 | size_t oldlen = sb->len; | 
|  | 136 | size_t oldalloc = sb->alloc; | 
|  | 137 | int ret; | 
|  | 138 |  | 
|  | 139 | ret = strbuf_grow(sb, hint ? hint : 8192); | 
|  | 140 | if (ret) | 
|  | 141 | return ret; | 
|  | 142 |  | 
|  | 143 | for (;;) { | 
|  | 144 | ssize_t cnt; | 
|  | 145 |  | 
|  | 146 | cnt = read(fd, sb->buf + sb->len, sb->alloc - sb->len - 1); | 
|  | 147 | if (cnt < 0) { | 
|  | 148 | if (oldalloc == 0) | 
|  | 149 | strbuf_release(sb); | 
|  | 150 | else | 
|  | 151 | strbuf_setlen(sb, oldlen); | 
|  | 152 | return cnt; | 
|  | 153 | } | 
|  | 154 | if (!cnt) | 
|  | 155 | break; | 
|  | 156 | sb->len += cnt; | 
|  | 157 | ret = strbuf_grow(sb, 8192); | 
|  | 158 | if (ret) | 
|  | 159 | return ret; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | sb->buf[sb->len] = '\0'; | 
|  | 163 | return sb->len - oldlen; | 
|  | 164 | } |