18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include "cache.h"
38c2ecf20Sopenharmony_ci#include "debug.h"
48c2ecf20Sopenharmony_ci#include "strbuf.h"
58c2ecf20Sopenharmony_ci#include <linux/kernel.h>
68c2ecf20Sopenharmony_ci#include <linux/string.h>
78c2ecf20Sopenharmony_ci#include <linux/zalloc.h>
88c2ecf20Sopenharmony_ci#include <errno.h>
98c2ecf20Sopenharmony_ci#include <stdio.h>
108c2ecf20Sopenharmony_ci#include <stdlib.h>
118c2ecf20Sopenharmony_ci#include <unistd.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/*
148c2ecf20Sopenharmony_ci * Used as the default ->buf value, so that people can always assume
158c2ecf20Sopenharmony_ci * buf is non NULL and ->buf is NUL terminated even for a freshly
168c2ecf20Sopenharmony_ci * initialized strbuf.
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_cichar strbuf_slopbuf[1];
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciint strbuf_init(struct strbuf *sb, ssize_t hint)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	sb->alloc = sb->len = 0;
238c2ecf20Sopenharmony_ci	sb->buf = strbuf_slopbuf;
248c2ecf20Sopenharmony_ci	if (hint)
258c2ecf20Sopenharmony_ci		return strbuf_grow(sb, hint);
268c2ecf20Sopenharmony_ci	return 0;
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_civoid strbuf_release(struct strbuf *sb)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	if (sb->alloc) {
328c2ecf20Sopenharmony_ci		zfree(&sb->buf);
338c2ecf20Sopenharmony_ci		strbuf_init(sb, 0);
348c2ecf20Sopenharmony_ci	}
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cichar *strbuf_detach(struct strbuf *sb, size_t *sz)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	char *res = sb->alloc ? sb->buf : NULL;
408c2ecf20Sopenharmony_ci	if (sz)
418c2ecf20Sopenharmony_ci		*sz = sb->len;
428c2ecf20Sopenharmony_ci	strbuf_init(sb, 0);
438c2ecf20Sopenharmony_ci	return res;
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciint strbuf_grow(struct strbuf *sb, size_t extra)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	char *buf;
498c2ecf20Sopenharmony_ci	size_t nr = sb->len + extra + 1;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	if (nr < sb->alloc)
528c2ecf20Sopenharmony_ci		return 0;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (nr <= sb->len)
558c2ecf20Sopenharmony_ci		return -E2BIG;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (alloc_nr(sb->alloc) > nr)
588c2ecf20Sopenharmony_ci		nr = alloc_nr(sb->alloc);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/*
618c2ecf20Sopenharmony_ci	 * Note that sb->buf == strbuf_slopbuf if sb->alloc == 0, and it is
628c2ecf20Sopenharmony_ci	 * a static variable. Thus we have to avoid passing it to realloc.
638c2ecf20Sopenharmony_ci	 */
648c2ecf20Sopenharmony_ci	buf = realloc(sb->alloc ? sb->buf : NULL, nr * sizeof(*buf));
658c2ecf20Sopenharmony_ci	if (!buf)
668c2ecf20Sopenharmony_ci		return -ENOMEM;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	sb->buf = buf;
698c2ecf20Sopenharmony_ci	sb->alloc = nr;
708c2ecf20Sopenharmony_ci	return 0;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciint strbuf_addch(struct strbuf *sb, int c)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	int ret = strbuf_grow(sb, 1);
768c2ecf20Sopenharmony_ci	if (ret)
778c2ecf20Sopenharmony_ci		return ret;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	sb->buf[sb->len++] = c;
808c2ecf20Sopenharmony_ci	sb->buf[sb->len] = '\0';
818c2ecf20Sopenharmony_ci	return 0;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciint strbuf_add(struct strbuf *sb, const void *data, size_t len)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	int ret = strbuf_grow(sb, len);
878c2ecf20Sopenharmony_ci	if (ret)
888c2ecf20Sopenharmony_ci		return ret;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	memcpy(sb->buf + sb->len, data, len);
918c2ecf20Sopenharmony_ci	return strbuf_setlen(sb, sb->len + len);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic int strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	int len, ret;
978c2ecf20Sopenharmony_ci	va_list ap_saved;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	if (!strbuf_avail(sb)) {
1008c2ecf20Sopenharmony_ci		ret = strbuf_grow(sb, 64);
1018c2ecf20Sopenharmony_ci		if (ret)
1028c2ecf20Sopenharmony_ci			return ret;
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	va_copy(ap_saved, ap);
1068c2ecf20Sopenharmony_ci	len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
1078c2ecf20Sopenharmony_ci	if (len < 0) {
1088c2ecf20Sopenharmony_ci		va_end(ap_saved);
1098c2ecf20Sopenharmony_ci		return len;
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci	if (len > strbuf_avail(sb)) {
1128c2ecf20Sopenharmony_ci		ret = strbuf_grow(sb, len);
1138c2ecf20Sopenharmony_ci		if (ret) {
1148c2ecf20Sopenharmony_ci			va_end(ap_saved);
1158c2ecf20Sopenharmony_ci			return ret;
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci		len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap_saved);
1188c2ecf20Sopenharmony_ci		if (len > strbuf_avail(sb)) {
1198c2ecf20Sopenharmony_ci			pr_debug("this should not happen, your vsnprintf is broken");
1208c2ecf20Sopenharmony_ci			va_end(ap_saved);
1218c2ecf20Sopenharmony_ci			return -EINVAL;
1228c2ecf20Sopenharmony_ci		}
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci	va_end(ap_saved);
1258c2ecf20Sopenharmony_ci	return strbuf_setlen(sb, sb->len + len);
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ciint strbuf_addf(struct strbuf *sb, const char *fmt, ...)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	va_list ap;
1318c2ecf20Sopenharmony_ci	int ret;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	va_start(ap, fmt);
1348c2ecf20Sopenharmony_ci	ret = strbuf_addv(sb, fmt, ap);
1358c2ecf20Sopenharmony_ci	va_end(ap);
1368c2ecf20Sopenharmony_ci	return ret;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cissize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	size_t oldlen = sb->len;
1428c2ecf20Sopenharmony_ci	size_t oldalloc = sb->alloc;
1438c2ecf20Sopenharmony_ci	int ret;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	ret = strbuf_grow(sb, hint ? hint : 8192);
1468c2ecf20Sopenharmony_ci	if (ret)
1478c2ecf20Sopenharmony_ci		return ret;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	for (;;) {
1508c2ecf20Sopenharmony_ci		ssize_t cnt;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci		cnt = read(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
1538c2ecf20Sopenharmony_ci		if (cnt < 0) {
1548c2ecf20Sopenharmony_ci			if (oldalloc == 0)
1558c2ecf20Sopenharmony_ci				strbuf_release(sb);
1568c2ecf20Sopenharmony_ci			else
1578c2ecf20Sopenharmony_ci				strbuf_setlen(sb, oldlen);
1588c2ecf20Sopenharmony_ci			return cnt;
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci		if (!cnt)
1618c2ecf20Sopenharmony_ci			break;
1628c2ecf20Sopenharmony_ci		sb->len += cnt;
1638c2ecf20Sopenharmony_ci		ret = strbuf_grow(sb, 8192);
1648c2ecf20Sopenharmony_ci		if (ret)
1658c2ecf20Sopenharmony_ci			return ret;
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	sb->buf[sb->len] = '\0';
1698c2ecf20Sopenharmony_ci	return sb->len - oldlen;
1708c2ecf20Sopenharmony_ci}
171