changeset 0:b1991ee4451d

misc: initial import
author David Demelier <markand@malikania.fr>
date Thu, 29 Oct 2020 17:24:30 +0100
parents
children cf8cb4a0729e
files .hgignore INSTALL.md LICENSE.md Makefile README.md buf-clear.c buf-dup.c buf-erase.c buf-finish.c buf-init.c buf-int.h buf-printf.c buf-putc.c buf-puts.c buf-reserve.c buf-resize.c buf-shrink.c buf-sub.c buf-vprintf.c buf.c buf.h buf_clear.3 buf_dup.3 buf_erase.3 buf_finish.3 buf_init.3 buf_printf.3 buf_putc.3 buf_puts.3 buf_reserve.3 buf_resize.3 buf_shrink.3 buf_sub.3 buf_vprintf.3 extern/LICENSE.libgreatest.txt extern/VERSION.libgreatest.txt extern/libgreatest/greatest.h libbuf.3 test/test-clear.c test/test-dup.c test/test-erase.c test/test-finish.c test/test-init.c test/test-printf.c test/test-putc.c test/test-puts.c test/test-reserve.c test/test-resize.c test/test-shrink.c test/test-sub.c
diffstat 50 files changed, 4028 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,27 @@
+# vim/emacs specific.
+^tags$
+^tags.lock$
+^tags.temp$
+\.swp$
+\.swo$
+
+# macOS specific.
+\.DS_Store$
+
+# Objects files.
+\.o$
+\.a$
+
+# Test files.
+^test/test-clear$
+^test/test-dup$
+^test/test-erase$
+^test/test-finish$
+^test/test-init$
+^test/test-printf$
+^test/test-putc$
+^test/test-puts$
+^test/test-reserve$
+^test/test-resize$
+^test/test-shrink$
+^test/test-sub$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/INSTALL.md	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,26 @@
+libbuf INSTALL
+==============
+
+Installation instructions.
+
+Requirements
+------------
+
+- C99 compiler,
+- POSIX make, only required to build,
+- POSIX user land (ar, cc, cp, mkdir, rm), to install and test.
+
+Basic installation
+------------------
+
+Quick install.
+
+	$ tar xvzf libbuf-x.y.z-tar.xz
+	$ cd libbuf-x.y.z
+	$ make
+	# sudo make install
+
+Alternatively, you can use the following targets as well.
+
+- `make doxygen`: build doxygen documentation.
+- `make test`: run test suite.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LICENSE.md	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,16 @@
+libbuf ISC LICENSE
+==================
+
+Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,103 @@
+#
+# Makefile -- simple string buffer for C
+#
+# Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+.POSIX:
+
+.SUFFIXES:
+.SUFFIXES: .o .c
+
+CC=             cc
+AR=             ar
+CFLAGS=         -O3 -DNDEBUG
+
+PREFIX=         /usr/local
+INCDIR=         ${PREFIX}/include
+LIBDIR=         ${PREFIX}/lib
+MANDIR=         ${PREFIX}/share/man
+
+SRCS=           buf-clear.c \
+                buf-dup.c \
+                buf-erase.c \
+                buf-finish.c \
+                buf-init.c \
+                buf-printf.c \
+                buf-putc.c \
+                buf-puts.c \
+                buf-reserve.c \
+                buf-resize.c \
+                buf-shrink.c \
+                buf-sub.c \
+                buf-vprintf.c \
+                buf.c
+OBJS=           ${SRCS:.c=.o}
+
+MAN=            buf_clear.3 \
+                buf_dup.3 \
+                buf_erase.3 \
+                buf_finish.3 \
+                buf_init.3 \
+                buf_printf.3 \
+                buf_putc.3 \
+                buf_puts.3 \
+                buf_reserve.3 \
+                buf_resize.3 \
+                buf_shrink.3 \
+                buf_sub.3 \
+                buf_vprintf.3 \
+                libbuf.3
+
+TESTS=          test/test-clear.c \
+                test/test-dup.c \
+                test/test-erase.c \
+                test/test-finish.c \
+                test/test-init.c \
+                test/test-printf.c \
+                test/test-putc.c \
+                test/test-puts.c \
+                test/test-reserve.c \
+                test/test-resize.c \
+                test/test-shrink.c \
+                test/test-sub.c
+TESTS_OBJS=     ${TESTS:.c=}
+
+.c:
+	${CC} -Iextern/libgreatest -I. ${CFLAGS} $< -o $@ libbuf.a
+
+all: libbuf.a
+
+libbuf.a: ${OBJS}
+	${AR} -rc $@ ${OBJS}
+
+clean:
+	rm -f libbuf.a ${OBJS}
+	rm -f ${TESTS_OBJS}
+
+install:
+	mkdir -p ${DESTDIR}${INCDIR}
+	mkdir -p ${DESTDIR}${LIBDIR}
+	mkdir -p ${DESTDIR}${MANDIR}/man3
+	cp buf.h ${DESTDIR}${INCDIR}
+	cp libbuf.a ${DESTDIR}${LIBDIR}
+	cp ${MAN} ${DESTDIR}${MANDIR}/man3
+
+${TESTS_OBJS}: libbuf.a
+
+test: ${TESTS_OBJS}
+	for t in ${TESTS_OBJS}; do ./$$t; done
+
+.PHONY: all clean install test
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,54 @@
+libbuf
+======
+
+Minimalist dynamic string library for C99.
+
+Documentation
+-------------
+
+You can generate the documentation using `make doxygen`, otherwise you can also
+read the *libbuf.3* manual page.
+
+Portability
+-----------
+
+The code itself is compatible with any compiler that provides C99 support at
+least but should also support the POSIX `ENOMEM` constant. If not present on
+your system you should never check global `errno` value while using this
+function.
+
+It was tested on:
+
+- MinGW-w64,
+- Visual Studio 2019,
+- macOS Catalina (10.15.7),
+- Linux (musl, glibc),
+- FreeBSD,
+- OpenBSD,
+- NetBSD.
+
+FAQ
+---
+
+### Why another library?
+
+Sure there are a lot of string management libraries out there but I wanted a
+very minimal one that does not include fancy things like trim, split, tokenizer
+and such. This library only exposes 13 functions to the user which should be
+enough.
+
+At the time of writing it's also less than 350 lines of code.
+
+Also, I wanted a library easy to embed everywhere without a complicated build
+process. This library can be bundled in your project by just copying all .c
+files and .h files without any modification.
+
+### Why every function is defined in its own file?
+
+Because it improves static linking as only symbols you need are bundled into
+your final executable.
+
+Author
+------
+
+David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-clear.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,30 @@
+/*
+ * buf-clear.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+
+#include "buf.h"
+
+void
+buf_clear(struct buf *b)
+{
+	assert(b);
+
+	if (b->data)
+		b->data[b->length = 0] = 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-dup.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,41 @@
+/*
+ * buf-dup.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "buf.h"
+
+bool
+buf_dup(struct buf *b, const struct buf *src)
+{
+	assert(b);
+	assert(src);
+
+	if (!src->data)
+		return true;
+	if (!(b->data = BUF_MALLOC(src->length + 1)))
+		return false;
+
+	strcpy(b->data, src->data);
+	b->capacity = src->length;
+	b->length = src->length;
+
+	return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-erase.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,38 @@
+/*
+ * buf-erase.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <string.h>
+
+#include "buf.h"
+
+void
+buf_erase(struct buf *b, size_t pos, size_t count)
+{
+	assert(b);
+	assert(pos <= b->length);
+
+	if (count > b->length - pos) {
+		/* Optimize whole erase at pos. */
+		b->data[pos] = 0;
+		b->length = pos;
+	} else {
+		memmove(&b->data[pos], &b->data[pos + count], b->length - count);
+		b->length -= count;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-finish.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,32 @@
+/*
+ * buf-finish.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+
+#include "buf.h"
+
+void
+buf_finish(struct buf *b)
+{
+	assert(b);
+
+	BUF_FREE(b->data);
+	b->data = NULL;
+	b->capacity = b->length = 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-init.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,30 @@
+/*
+ * buf-init.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <string.h>
+
+#include "buf.h"
+
+void
+buf_init(struct buf *b)
+{
+	assert(b);
+
+	memset(b, 0, sizeof (*b));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-int.h	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,31 @@
+/*
+ * buf-int.h -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stddef.h>
+#include <stdbool.h>
+
+struct buf;
+
+__attribute__ ((visibility ("hidden"))) bool
+_buf_growdbl(struct buf *, size_t);
+
+bool
+_buf_growmin(struct buf *, size_t);
+
+bool
+_buf_grow(struct buf *, size_t);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-printf.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,37 @@
+/*
+ * buf-printf.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+
+#include "buf.h"
+
+bool
+buf_printf(struct buf *b, const char *fmt, ...)
+{
+	assert(b);
+	assert(fmt);
+
+	va_list ap;
+	bool ret;
+
+	va_start(ap, fmt);
+	ret = buf_vprintf(b, fmt, ap);
+	va_end(ap);
+
+	return ret;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-putc.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,36 @@
+/*
+ * buf-putc.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+
+#include "buf.h"
+#include "buf-int.h"
+
+bool
+buf_putc(struct buf *b, char c)
+{
+	assert(b);
+
+	if (!_buf_grow(b, 1))
+		return false;
+
+	b->data[b->length++] = c;
+	b->data[b->length] = 0;
+
+	return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-puts.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,40 @@
+/*
+ * buf-puts.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <string.h>
+
+#include "buf.h"
+#include "buf-int.h"
+
+bool
+buf_puts(struct buf *b, const char *s)
+{
+	assert(b);
+	assert(s);
+
+	const size_t len = strlen(s);
+
+	if (!_buf_grow(b, len))
+		return false;
+
+	strcpy(&b->data[b->length], s);
+	b->length += len;
+
+	return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-reserve.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,33 @@
+/*
+ * buf-reserve.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+
+#include "buf.h"
+#include "buf-int.h"
+
+bool
+buf_reserve(struct buf *b, size_t amount)
+{
+	assert(b);
+
+	if (!_buf_grow(b, amount))
+		return false;
+
+	return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-resize.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,45 @@
+/*
+ * buf-resize.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <string.h>
+
+#include "buf.h"
+#include "buf-int.h"
+
+bool
+buf_resize(struct buf *b, size_t size, char ch)
+{
+	assert(b);
+
+	/* New size is smaller than curren't length, just update it. */
+	if (size < b->length) {
+		b->data[b->length = size] = 0;
+		return true;
+	}
+
+	/* New size is bigger, data may be reallocated. */
+	if (!_buf_grow(b, size - b->length))
+		return false;
+
+	memset(&b->data[b->length], ch, size - b->length);
+	b->length = size;
+	b->data[b->length] = 0;
+
+	return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-shrink.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,45 @@
+/*
+ * buf-shrink.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+
+#include "buf.h"
+
+bool
+buf_shrink(struct buf *b)
+{
+	assert(b);
+
+	void *newptr;
+
+	if (b->length == 0) {
+		free(b->data);
+		b->data = NULL;
+		b->length = b->capacity = 0;
+		return true;
+	}
+
+	if (!(newptr = BUF_REALLOC(b->data, b->length + 1)))
+		return false;
+
+	b->data = newptr;
+	b->capacity = b->length;
+
+	return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-sub.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,45 @@
+/*
+ * buf-sub.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "buf.h"
+
+bool
+buf_sub(struct buf *b, const struct buf *src, size_t pos, size_t count)
+{
+	assert(b);
+	assert(src);
+	assert(pos <= src->length);
+
+	memset(b, 0, sizeof (*b));
+
+	if (count >= src->length)
+		count = src->length - pos;
+	if (!(b->data = BUF_MALLOC(count + 1)))
+		return false;
+
+	strncpy(b->data, &src->data[pos], count);
+	b->length = count;
+	b->capacity = count;
+	b->data[b->length] = 0;
+
+	return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf-vprintf.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,56 @@
+/*
+ * buf-vprintf.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <stdio.h>
+
+#include "buf.h"
+#include "buf-int.h"
+
+bool
+buf_vprintf(struct buf *b, const char *fmt, va_list args)
+{
+	assert(b);
+	assert(fmt);
+
+	va_list ap;
+	int amount;
+
+	/* Determine length. */
+	va_copy(ap, args);
+	amount = vsnprintf(NULL, 0, fmt, ap);
+	va_end(ap);
+
+	if (amount < 0)
+		return false;
+
+	/* Do actual copy. */
+	if (!_buf_grow(b, amount))
+		return false;
+
+	va_copy(ap, args);
+	amount = vsprintf(&b->data[b->length], fmt, ap);
+	va_end(ap);
+
+	if (amount < 0)
+		return false;
+
+	b->length += amount;
+
+	return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,114 @@
+/*
+ * buf.c -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include "buf.h"
+
+/*
+ * Try to increase the buffer length by a power of two until we have enough
+ * space to fit `desired'.
+ *
+ * Detects overflow and return false if happened or reallocation could not
+ * occur.
+ */
+bool
+_buf_growdbl(struct buf *b, size_t desired)
+{
+	size_t newcap = b->capacity;
+	void *newptr;
+
+	while (desired > newcap - b->length) {
+		const size_t r = newcap * 2;
+
+		if (r / newcap != 2) {
+#if defined(ENOMEM)
+			errno = ENOMEM;
+#endif
+			return false;
+		}
+
+		newcap = r;
+	}
+
+	/* At this step we must have enough room. */
+	assert(newcap - b->length >= desired);
+
+	if (!(newptr = BUF_REALLOC(b->data, newcap + 1)))
+		return false;
+
+	b->data = newptr;
+	b->capacity = newcap;
+
+	return true;
+}
+
+/*
+ * Try to allocate just enough space for the `desired' amount of space. This is
+ * only used when the buffer is already too large but hasn't reached SIZE_MAX
+ * yet.
+ *
+ * Returns false if allocation failed.
+ */
+bool
+_buf_growmin(struct buf *b, size_t desired)
+{
+	size_t newcap;
+	void *newptr;
+
+	if (desired >= SIZE_MAX - b->length) {
+#if defined(ENOMEM)
+		errno = ENOMEM;
+#endif
+		return false;
+	}
+
+	/* Don't forget to keep what's remaining between capacity and length. */
+	newcap = b->capacity + (desired - (b->capacity - b->length));
+
+	/* Try to reallocate. */
+	if (!(newptr = BUF_REALLOC(b->data, newcap + 1)))
+		return false;
+
+	b->data = newptr;
+	b->capacity = newcap;
+
+	return true;
+}
+
+bool
+_buf_grow(struct buf *b, size_t desired)
+{
+	const size_t avail = b->capacity - b->length;
+
+	if (avail >= desired)
+		return true;
+
+	if (!b->capacity) {
+		if (!(b->data = BUF_MALLOC(desired + 1)))
+			return false;
+
+		b->capacity = desired;
+	} else if (!_buf_growdbl(b, desired) && !_buf_growmin(b, desired))
+		return false;
+
+	return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf.h	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,273 @@
+/*
+ * buf.h -- simple string buffer for C
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef BUF_H
+#define BUF_H
+
+/**
+ * \file buf.h
+ * \brief Simple string buffer for C
+ *
+ * This module is meant to be copied directly into your source code and adapted
+ * to your needs.
+ *
+ * # Portability
+ *
+ * This module uses almost pure C99 functions without extensions but uses a few
+ * POSIX macros: ENOMEM. The errno global value won't be set if those macros
+ * are not present.
+ *
+ * It also requires the C99 features: `vsnprintf`, `va_copy`.
+ *
+ * # Allocation strategy
+ *
+ * This module will always try to allocate twice the existing buffer capacity
+ * unless it reaches an overflow. If overflow occurs, it will try to reallocate
+ * only the required portion instead. In any case, on 64-bits machine the
+ * maximum number is usually around 18.5PB which is far from what the system
+ * will allow.
+ *
+ * Functions use standard `malloc(3)`, `realloc(3)` and `free(3)` functions, if
+ * custom allocations are desired, simply edit buf.c and adapt BUF_ macros
+ * at the top of the file or set them at compile time.
+ */
+
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+
+/*
+ * User/developer options.
+ *
+ * These macros are only used at compile time and can be redefined using
+ * preprocessor directives.
+ *
+ * - BUF_MALLOC: defaults to malloc
+ * - BUF_REALLOC: defaults to realloc
+ * - BUF_FREE: defaults to free
+ */
+
+#if !defined(BUF_MALLOC)
+#	define BUF_MALLOC malloc
+#endif
+
+#if !defined(BUF_REALLOC)
+#	define BUF_REALLOC realloc
+#endif
+
+#if !defined(BUF_FREE)
+#	define BUF_FREE free
+#endif
+
+/**
+ * \brief Buffer structure.
+ *
+ * You can initialize this structure with 0 before use.
+ *
+ * You may modify the data member yourself but make sure length member is set
+ * accordingly if you change its length.
+ *
+ * The capacity member contains the available size **without** including the
+ * null terminator, this means that a capacity of 5 means you can write 5
+ * characters plus the null terminator as every function always allocate one
+ * more byte to terminate strings.
+ */
+struct buf {
+	char *data;             /*!< String data. */
+	size_t length;          /*!< String length */
+	size_t capacity;        /*!< Capacity **not** including null */
+};
+
+/**
+ * Initialize the string.
+ *
+ * This is equivalent to `memset(b, 0, sizeof (*b))`.
+ *
+ * \pre b != NULL
+ * \param b the buffer to initialize to 0
+ */
+void
+buf_init(struct buf *b);
+
+/**
+ * Reserve more storage to avoid reallocations.
+ *
+ * This function only change capacity and may reallocate the buffer, length
+ * is unchanged.
+ *
+ * \pre b != NULL
+ * \param b the buffer to grow
+ * \param desired the additional space to request
+ * \return false on error and sets errno
+ */
+bool
+buf_reserve(struct buf *b, size_t desired);
+
+/**
+ * Resize the string and sets character into the new storage area if needed.
+ *
+ * In contrast to \ref buf_reserve, this function change the string length
+ * and update the new storage with the given character. if the new size is
+ * smaller then only length is updated, otherwise new memory can be reallocated
+ * if needed.
+ *
+ * Use '\0' if you only want to change the length but still have a NUL
+ * terminated string.
+ *
+ * \pre b != NULL
+ * \param b the buffer to grow
+ * \param desired the additional space to request
+ * \param c the character to fill (use '\0' if not needed)
+ * \return false on error and sets errno
+ */
+bool
+buf_resize(struct buf *b, size_t size, char c);
+
+/**
+ * Reallocate the string to the string's length.
+ *
+ * If the function could not reallocate the memory, the old string buffer is
+ * kept so it is not strictly necessary to check the result.
+ *
+ * \pre b != NULL
+ * \param b the buffer to grow
+ * \return true if the string buffer was actually shrinked
+ */
+bool
+buf_shrink(struct buf *b);
+
+/**
+ * Erase a portion of the string.
+ *
+ * \pre b != NULL
+ * \pre pos <= b->length
+ * \param b the string buffer
+ * \param pos the beginning of the portion
+ * \param count the number of characters to remove or -1 to remove up to the end
+ */
+void
+buf_erase(struct buf *b, size_t pos, size_t count);
+
+/**
+ * Put a single character.
+ *
+ * \note This function can be slow for large amount of data.
+ * \pre b != NULL
+ * \param b the string buffer
+ * \param c the character to append
+ * \return false on error and sets errno
+ */
+bool
+buf_putc(struct buf *b, char c);
+
+/**
+ * Put a string.
+ *
+ * \pre b != NULL
+ * \pre s != NULL
+ * \param b the string buffer
+ * \param s the string to append
+ * \return false on error and sets errno
+ */
+bool
+buf_puts(struct buf *b, const char *s);
+
+/**
+ * Append to the string using printf(3) format.
+ *
+ * \pre b != NULL
+ * \pre fmt != NULL
+ * \param b the string buffer
+ * \param fmt the printf(3) format string
+ * \param ... additional arguments
+ * \return false on error and sets errno
+ */
+bool
+buf_printf(struct buf *b, const char *fmt, ...);
+
+/**
+ * Similar to \ref buf_printf but using `va_list`.
+ *
+ * \pre b != NULL
+ * \pre fmt != NULL
+ * \param b the string buffer
+ * \param fmt the printf(3) format string
+ * \param ap the variadic arguments pointer
+ * \return false on error and sets errno
+ */
+bool
+buf_vprintf(struct buf *b, const char *fmt, va_list ap);
+
+/**
+ * Cut a portion of the string pointed by src into b.
+ *
+ * The pointer b must not contain data as it will be overriden and may be let
+ * uninitialized.
+ *
+ * \pre b != NULL
+ * \pre src != NULL
+ * \pre pos <= b->length
+ * \param b the string buffer
+ * \param src the source origin
+ * \param pos the beginning of the portion
+ * \param count the number of characters to remove or -1 to split up to the end
+ * \return false on error and sets errno
+ */
+bool
+buf_sub(struct buf *b, const struct buf *src, size_t pos, size_t count);
+
+/**
+ * Duplicate a string buffer.
+ *
+ * The pointer b must not contain data as it will be overriden and may be let
+ * uninitialized.
+ *
+ * \pre b != NULL
+ * \pre src != NULL
+ * \param b the string buffer
+ * \param src the source origin
+ * \return false on error and sets errno
+ */
+bool
+buf_dup(struct buf *b, const struct buf *src);
+
+/**
+ * Clear the buffer.
+ *
+ * This function only change the string length, use \ref buf_shrink if you
+ * want to reduce memory usage.
+ *
+ * \pre b != NULL
+ * \param b the string to update
+ */
+void
+buf_clear(struct buf *b);
+
+/**
+ * Clear the buffer.
+ *
+ * This function will effectively free the data member and set all members to
+ * 0.
+ *
+ * \pre b != NULL
+ * \param b the buffer to clear
+ */
+void
+buf_finish(struct buf *b);
+
+#endif /* !BUF_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_clear.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,44 @@
+.\"
+.\" buf_clear.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_CLEAR 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_clear
+.Nd clear the buffer.
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft void
+.Fn buf_clear "struct buf *b"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+This function only change the string length, use
+.Xr buf_shrink 3
+if you want to reduce memory usage.
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3 ,
+.Xr buf_shrink 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_dup.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,64 @@
+.\"
+.\" buf_dup.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_DUP 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_dup
+.Nd duplicate a string buffer
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft bool
+.Fn buf_dup "struct buf *b" "const struct buf *src"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+The pointer
+.Fa b
+must not contain data as it will be overriden and may be let
+unchanged as-is.
+.\" RETURN VALUE
+.Sh RETURN VALUE
+If the
+.Fa src
+buffer isn't initialized (and contains a NULL data field) the destination
+buffer
+.Fa b
+is unchanged but the function returns true.
+.Pp
+Otherwise the
+.Fn buf_dup
+function returns false in case of error and
+.Va errno
+is set to indicate the error.
+.\" ERRORS
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ENOMEM
+There wasn't enough memory to duplicate the buffer.
+.El
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_erase.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,54 @@
+.\"
+.\" buf_erase.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_ERASE 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_erase
+.Nd erase a portion of the string
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft void
+.Fn buf_erase "struct buf *b" "size_t pos" "size_t count"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+Remove
+.Fa count
+number of character in the buffer specified by
+.Fa b
+starting at
+.Fa pos .
+.Pp
+It is possible to pass -1 as
+.Fa count
+parameter to remove the remaining text until the end of the string.
+.Pp
+Undefined behavior may occur if the
+.Fa pos
+argument is outside of the buffer bounds.
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_finish.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,47 @@
+.\"
+.\" buf_finish.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_FINISH 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_finish
+.Nd clear the buffer and free resources
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft void
+.Fn buf_finish "struct buf *b"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+Free the internal resources and reset the buffer state specified by
+.Fa b .
+.Pp
+The buffer may be freely reused after calling the
+.Fn buf_finish
+function, it is also safe to call the function on a finalized buffer as well.
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3 ,
+.Xr buf_clear
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_init.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,48 @@
+.\"
+.\" buf_init.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_INIT 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_init
+.Nd initialize a buffer
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft void
+.Fn buf_init "struct buf *b"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+The
+.Fn buf_init
+function is used to reset the buffer specified by
+.Fa b .
+.Pp
+It does not allocate anything and if can even be unneeded if you have zero'ed
+the object yourself.
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3 ,
+.Xr buf_finish 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_printf.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,64 @@
+.\"
+.\" buf_printf.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_PRINTF 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_printf
+.Nd write a to a buffer using printf(3) like format
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft bool
+.Fn buf_printf "struct buf *b" "const char *fmt" "..."
+.\" DESCRIPTION
+.Sh DESCRIPTION
+This function uses the
+.Xr printf 3
+format to be appended in the buffer
+.Fa b .
+See the
+.Xr printf 3
+manual page for a detailed description of
+.Fa fmt
+string.
+.\" RETURN VALUE
+.Sh RETURN VALUE
+The
+.Fn buf_printf
+function returns false in case of error and
+.Va errno
+is set to indicate the error.
+.\" ERRORS
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ENOMEM
+Couldn't allocate more memory.
+.El
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3 ,
+.Xr buf_vprintf 3 ,
+.Xr printf 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_putc.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,65 @@
+.\"
+.\" buf_putc.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_PUTC 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_putc
+.Nd append a single character
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft bool
+.Fn buf_putc "struct buf *b" "char c"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+Append the
+.Fa c
+character into the buffer specified by
+.Fa b .
+.Pp
+This function should be avoided for performance reasons unless you reserve more
+storage using
+.Fn buf_reserve
+prior to calling this function. See
+.Xr buf_reserve 3
+for more details.
+.\" RETURN VALUE
+.Sh RETURN VALUE
+The
+.Fn buf_putc
+function returns false in case of error and
+.Va errno
+is set to indicate the error.
+.\" ERRORS
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ENOMEM
+Couldn't allocate more memory.
+.El
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3 ,
+.Xr buf_reserve 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_puts.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,61 @@
+.\"
+.\" buf_puts.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_PUTS 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_puts
+.Nd append a NUL-terminated string
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft bool
+.Fn buf_puts "struct buf *b" "const char *src"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+Append the NUL-terminated string
+.Fa src
+into the buffer specified by
+.Fa b .
+.Pp
+It is undefined behavior if
+.Fa src
+argument is NULL.
+.\" RETURN VALUE
+.Sh RETURN VALUE
+The
+.Fn buf_puts
+function returns false in case of error and
+.Va errno
+is set to indicate the error.
+.\" ERRORS
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ENOMEM
+Couldn't allocate more memory.
+.El
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_reserve.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,68 @@
+.\"
+.\" buf_reserve.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_RESERVE 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_reserve
+.Nd allocate more storage for next operations
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft bool
+.Fn buf_reserve "struct buf *b" "size_t desired"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+The
+.Fn buf_reserve
+function tries to increase the buffer's capacity specified by
+.Fa b
+to the amount of
+.Fa desired .
+.Pp
+Once the new capacity is increased the next calls to any operation that would
+append data to the buffer can avoid reallocating the memory.
+This can be useful if you know in advance which storage space you need.
+.Pp
+If it was unable to acquire more memory, the buffer still contains the original
+data and capacity remains unchanged.
+In any case, the buffer length's remains unchanged and only capacity is
+increased.
+.\" RETURN VALUE
+.Sh RETURN VALUE
+The
+.Fn buf_reserve
+function returns false in case of error and
+.Va errno
+is set to indicate the error.
+.\" ERRORS
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ENOMEM
+Couldn't allocate more memory.
+.El
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_resize.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,78 @@
+.\"
+.\" buf_resize.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_RESIZE 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_resize
+.Nd change string length
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft bool
+.Fn buf_resize "struct buf *b" "size_t size" "char c"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+Change the buffer length specified by
+.Fa b
+to the new
+.Fa size
+and optionally fill new space with character
+.Fa c .
+.Pp
+This function serves two purposes, it can reduce the string length if the
+.Fa size
+argument is smaller than current
+.Fa b
+length or it can increase it and fill the new region with
+.Fa c .
+In the first case, the function never fails in the second it may need to
+reallocate data which could fail if no memory is available.
+.Pp
+Note that in constrast to
+.Fn buf_shrink
+function, the
+.Fn buf_resize
+function never reallocate data if the new size is smaller, you may want to call
+.Fn buf_shrink
+if memory should be released.
+.\" RETURN VALUE
+.Sh RETURN VALUE
+The
+.Fn buf_resize
+function returns false in case of error and
+.Va errno
+is set to indicate the error.
+.\" ERRORS
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ENOMEM
+Couldn't allocate more memory.
+.El
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3 ,
+.Xr buf_resize 3 ,
+.Xr buf_shrink 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_shrink.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,67 @@
+.\"
+.\" buf_shrink.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_SHRINK 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_shrink
+.Nd reduce the buffer to its minimal requirement
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft bool
+.Fn buf_shrink "struct buf *b"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+Reduce the buffer capacity specified by
+.Fa b
+to reduce memory usage.
+.Pp
+The
+.Fn buf_shrink
+function will reduce the buffer capacity to the exact buffer length.
+If reallocation of the new memory region failed, the current buffer is kept and
+its capacity is unchanged.
+As such, it is perfectly safe to ignore the return value unless you really want
+to reduce memory consumption
+.\" RETURN VALUE
+.Sh RETURN VALUE
+If the buffer's length is 0, the function simply frees the underlying data and
+return true.
+.Pp
+Otherwise the
+.Fn buf_shrink
+function returns false in case of error and
+.Va errno
+is set to indicate the error.
+.\" ERRORS
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ENOMEM
+Memory reallocation failed.
+.El
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_sub.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,76 @@
+.\"
+.\" buf_sub.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_SUB 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_sub
+.Nd duplicate a specific portion from the buffer
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft bool
+.Fn buf_sub "struct buf *b" "const struct buf *src" "size_t pos" "size_t count"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+Extract the portion at position
+.Fa pos
+from the buffer specified by
+.Fa src
+with
+.Fa count
+number of characters and set to
+.Fa b .
+.Pp
+This function will first initialize and allocate the buffer specified by
+.Fa b
+to fill the portion length.
+It should not contain any data because it will be overwritten.
+.Pp
+The
+.Fa count
+argument can be set to -1 to extract data until the end of the
+.Fa src
+buffer.
+.Pp
+Undefined behavior may occur if the
+.Fa pos
+argument is outside of the buffer bounds.
+.\" RETURN VALUE
+.Sh RETURN VALUE
+The
+.Fn buf_sub
+function returns false in case of error and
+.Va errno
+is set to indicate the error.
+.\" ERRORS
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ENOMEM
+Memory allocation failed.
+.El
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buf_vprintf.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,63 @@
+.\"
+.\" buf_vprintf.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt BUF_VPRINTF 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm buf_vprintf
+.Nd write a to a buffer using printf(3) like format
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft bool
+.Fn buf_vprintf "struct buf *b" "const char *fmt" "va_list ap"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+This function is the synonym of
+.Fn buf_printf
+but use a
+.Vt va_list
+argument instead.
+.Pp
+See
+.Xr buf_printf 3
+for more details.
+.\" RETURN VALUE
+.Sh RETURN VALUE
+The
+.Fn buf_vprintf
+function returns false in case of error and
+.Va errno
+is set to indicate the error.
+.\" ERRORS
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ENOMEM
+Couldn't allocate more memory.
+.El
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr libbuf 3 ,
+.Xr buf_printf 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm libbuf
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extern/LICENSE.libgreatest.txt	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,13 @@
+Copyright (c) 2011-2018 Scott Vokes <vokes.s@gmail.com>
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extern/VERSION.libgreatest.txt	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,1 @@
+1.4.2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extern/libgreatest/greatest.h	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,1225 @@
+/*
+ * Copyright (c) 2011-2019 Scott Vokes <vokes.s@gmail.com>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef GREATEST_H
+#define GREATEST_H
+
+#if defined(__cplusplus) && !defined(GREATEST_NO_EXTERN_CPLUSPLUS)
+extern "C" {
+#endif
+
+/* 1.4.2 */
+#define GREATEST_VERSION_MAJOR 1
+#define GREATEST_VERSION_MINOR 4
+#define GREATEST_VERSION_PATCH 2
+
+/* A unit testing system for C, contained in 1 file.
+ * It doesn't use dynamic allocation or depend on anything
+ * beyond ANSI C89.
+ *
+ * An up-to-date version can be found at:
+ *     https://github.com/silentbicycle/greatest/
+ */
+
+
+/*********************************************************************
+ * Minimal test runner template
+ *********************************************************************/
+#if 0
+
+#include "greatest.h"
+
+TEST foo_should_foo(void) {
+    PASS();
+}
+
+static void setup_cb(void *data) {
+    printf("setup callback for each test case\n");
+}
+
+static void teardown_cb(void *data) {
+    printf("teardown callback for each test case\n");
+}
+
+SUITE(suite) {
+    /* Optional setup/teardown callbacks which will be run before/after
+     * every test case. If using a test suite, they will be cleared when
+     * the suite finishes. */
+    SET_SETUP(setup_cb, voidp_to_callback_data);
+    SET_TEARDOWN(teardown_cb, voidp_to_callback_data);
+
+    RUN_TEST(foo_should_foo);
+}
+
+/* Add definitions that need to be in the test runner's main file. */
+GREATEST_MAIN_DEFS();
+
+/* Set up, run suite(s) of tests, report pass/fail/skip stats. */
+int run_tests(void) {
+    GREATEST_INIT();            /* init. greatest internals */
+    /* List of suites to run (if any). */
+    RUN_SUITE(suite);
+
+    /* Tests can also be run directly, without using test suites. */
+    RUN_TEST(foo_should_foo);
+
+    GREATEST_PRINT_REPORT();          /* display results */
+    return greatest_all_passed();
+}
+
+/* main(), for a standalone command-line test runner.
+ * This replaces run_tests above, and adds command line option
+ * handling and exiting with a pass/fail status. */
+int main(int argc, char **argv) {
+    GREATEST_MAIN_BEGIN();      /* init & parse command-line args */
+    RUN_SUITE(suite);
+    GREATEST_MAIN_END();        /* display results */
+}
+
+#endif
+/*********************************************************************/
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+
+/***********
+ * Options *
+ ***********/
+
+/* Default column width for non-verbose output. */
+#ifndef GREATEST_DEFAULT_WIDTH
+#define GREATEST_DEFAULT_WIDTH 72
+#endif
+
+/* FILE *, for test logging. */
+#ifndef GREATEST_STDOUT
+#define GREATEST_STDOUT stdout
+#endif
+
+/* Remove GREATEST_ prefix from most commonly used symbols? */
+#ifndef GREATEST_USE_ABBREVS
+#define GREATEST_USE_ABBREVS 1
+#endif
+
+/* Set to 0 to disable all use of setjmp/longjmp. */
+#ifndef GREATEST_USE_LONGJMP
+#define GREATEST_USE_LONGJMP 1
+#endif
+
+/* Make it possible to replace fprintf with another
+ * function with the same interface. */
+#ifndef GREATEST_FPRINTF
+#define GREATEST_FPRINTF fprintf
+#endif
+
+#if GREATEST_USE_LONGJMP
+#include <setjmp.h>
+#endif
+
+/* Set to 0 to disable all use of time.h / clock(). */
+#ifndef GREATEST_USE_TIME
+#define GREATEST_USE_TIME 1
+#endif
+
+#if GREATEST_USE_TIME
+#include <time.h>
+#endif
+
+/* Floating point type, for ASSERT_IN_RANGE. */
+#ifndef GREATEST_FLOAT
+#define GREATEST_FLOAT double
+#define GREATEST_FLOAT_FMT "%g"
+#endif
+
+/* Size of buffer for test name + optional '_' separator and suffix */
+#ifndef GREATEST_TESTNAME_BUF_SIZE
+#define GREATEST_TESTNAME_BUF_SIZE 128
+#endif
+
+
+/*********
+ * Types *
+ *********/
+
+/* Info for the current running suite. */
+typedef struct greatest_suite_info {
+    unsigned int tests_run;
+    unsigned int passed;
+    unsigned int failed;
+    unsigned int skipped;
+
+#if GREATEST_USE_TIME
+    /* timers, pre/post running suite and individual tests */
+    clock_t pre_suite;
+    clock_t post_suite;
+    clock_t pre_test;
+    clock_t post_test;
+#endif
+} greatest_suite_info;
+
+/* Type for a suite function. */
+typedef void greatest_suite_cb(void);
+
+/* Types for setup/teardown callbacks. If non-NULL, these will be run
+ * and passed the pointer to their additional data. */
+typedef void greatest_setup_cb(void *udata);
+typedef void greatest_teardown_cb(void *udata);
+
+/* Type for an equality comparison between two pointers of the same type.
+ * Should return non-0 if equal, otherwise 0.
+ * UDATA is a closure value, passed through from ASSERT_EQUAL_T[m]. */
+typedef int greatest_equal_cb(const void *expd, const void *got, void *udata);
+
+/* Type for a callback that prints a value pointed to by T.
+ * Return value has the same meaning as printf's.
+ * UDATA is a closure value, passed through from ASSERT_EQUAL_T[m]. */
+typedef int greatest_printf_cb(const void *t, void *udata);
+
+/* Callbacks for an arbitrary type; needed for type-specific
+ * comparisons via GREATEST_ASSERT_EQUAL_T[m].*/
+typedef struct greatest_type_info {
+    greatest_equal_cb *equal;
+    greatest_printf_cb *print;
+} greatest_type_info;
+
+typedef struct greatest_memory_cmp_env {
+    const unsigned char *exp;
+    const unsigned char *got;
+    size_t size;
+} greatest_memory_cmp_env;
+
+/* Callbacks for string and raw memory types. */
+extern greatest_type_info greatest_type_info_string;
+extern greatest_type_info greatest_type_info_memory;
+
+typedef enum {
+    GREATEST_FLAG_FIRST_FAIL = 0x01,
+    GREATEST_FLAG_LIST_ONLY = 0x02,
+    GREATEST_FLAG_ABORT_ON_FAIL = 0x04
+} greatest_flag_t;
+
+/* Internal state for a PRNG, used to shuffle test order. */
+struct greatest_prng {
+    unsigned char random_order; /* use random ordering? */
+    unsigned char initialized;  /* is random ordering initialized? */
+    unsigned char pad_0[6];
+    unsigned long state;        /* PRNG state */
+    unsigned long count;        /* how many tests, this pass */
+    unsigned long count_ceil;   /* total number of tests */
+    unsigned long count_run;    /* total tests run */
+    unsigned long a;            /* LCG multiplier */
+    unsigned long c;            /* LCG increment */
+    unsigned long m;            /* LCG modulus, based on count_ceil */
+};
+
+/* Struct containing all test runner state. */
+typedef struct greatest_run_info {
+    unsigned char flags;
+    unsigned char verbosity;
+    unsigned char pad_0[2];
+
+    unsigned int tests_run;     /* total test count */
+
+    /* currently running test suite */
+    greatest_suite_info suite;
+
+    /* overall pass/fail/skip counts */
+    unsigned int passed;
+    unsigned int failed;
+    unsigned int skipped;
+    unsigned int assertions;
+
+    /* info to print about the most recent failure */
+    unsigned int fail_line;
+    unsigned int pad_1;
+    const char *fail_file;
+    const char *msg;
+
+    /* current setup/teardown hooks and userdata */
+    greatest_setup_cb *setup;
+    void *setup_udata;
+    greatest_teardown_cb *teardown;
+    void *teardown_udata;
+
+    /* formatting info for ".....s...F"-style output */
+    unsigned int col;
+    unsigned int width;
+
+    /* only run a specific suite or test */
+    const char *suite_filter;
+    const char *test_filter;
+    const char *test_exclude;
+    const char *name_suffix;    /* print suffix with test name */
+    char name_buf[GREATEST_TESTNAME_BUF_SIZE];
+
+    struct greatest_prng prng[2]; /* 0: suites, 1: tests */
+
+#if GREATEST_USE_TIME
+    /* overall timers */
+    clock_t begin;
+    clock_t end;
+#endif
+
+#if GREATEST_USE_LONGJMP
+    int pad_jmp_buf;
+    unsigned char pad_2[4];
+    jmp_buf jump_dest;
+#endif
+} greatest_run_info;
+
+struct greatest_report_t {
+    /* overall pass/fail/skip counts */
+    unsigned int passed;
+    unsigned int failed;
+    unsigned int skipped;
+    unsigned int assertions;
+};
+
+/* Global var for the current testing context.
+ * Initialized by GREATEST_MAIN_DEFS(). */
+extern greatest_run_info greatest_info;
+
+/* Type for ASSERT_ENUM_EQ's ENUM_STR argument. */
+typedef const char *greatest_enum_str_fun(int value);
+
+
+/**********************
+ * Exported functions *
+ **********************/
+
+/* These are used internally by greatest macros. */
+int greatest_test_pre(const char *name);
+void greatest_test_post(int res);
+int greatest_do_assert_equal_t(const void *expd, const void *got,
+    greatest_type_info *type_info, void *udata);
+void greatest_prng_init_first_pass(int id);
+int greatest_prng_init_second_pass(int id, unsigned long seed);
+void greatest_prng_step(int id);
+
+/* These are part of the public greatest API. */
+void GREATEST_SET_SETUP_CB(greatest_setup_cb *cb, void *udata);
+void GREATEST_SET_TEARDOWN_CB(greatest_teardown_cb *cb, void *udata);
+void GREATEST_INIT(void);
+void GREATEST_PRINT_REPORT(void);
+int greatest_all_passed(void);
+void greatest_set_suite_filter(const char *filter);
+void greatest_set_test_filter(const char *filter);
+void greatest_set_test_exclude(const char *filter);
+void greatest_stop_at_first_fail(void);
+void greatest_abort_on_fail(void);
+void greatest_list_only(void);
+void greatest_get_report(struct greatest_report_t *report);
+unsigned int greatest_get_verbosity(void);
+void greatest_set_verbosity(unsigned int verbosity);
+void greatest_set_flag(greatest_flag_t flag);
+void greatest_set_test_suffix(const char *suffix);
+
+
+/********************
+* Language Support *
+********************/
+
+/* If __VA_ARGS__ (C99) is supported, allow parametric testing
+* without needing to manually manage the argument struct. */
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 19901L) ||        \
+    (defined(_MSC_VER) && _MSC_VER >= 1800)
+#define GREATEST_VA_ARGS
+#endif
+
+
+/**********
+ * Macros *
+ **********/
+
+/* Define a suite. (The duplication is intentional -- it eliminates
+ * a warning from -Wmissing-declarations.) */
+#define GREATEST_SUITE(NAME) void NAME(void); void NAME(void)
+
+/* Declare a suite, provided by another compilation unit. */
+#define GREATEST_SUITE_EXTERN(NAME) void NAME(void)
+
+/* Start defining a test function.
+ * The arguments are not included, to allow parametric testing. */
+#define GREATEST_TEST static enum greatest_test_res
+
+/* PASS/FAIL/SKIP result from a test. Used internally. */
+typedef enum greatest_test_res {
+    GREATEST_TEST_RES_PASS = 0,
+    GREATEST_TEST_RES_FAIL = -1,
+    GREATEST_TEST_RES_SKIP = 1
+} greatest_test_res;
+
+/* Run a suite. */
+#define GREATEST_RUN_SUITE(S_NAME) greatest_run_suite(S_NAME, #S_NAME)
+
+/* Run a test in the current suite. */
+#define GREATEST_RUN_TEST(TEST)                                         \
+    do {                                                                \
+        if (greatest_test_pre(#TEST) == 1) {                            \
+            enum greatest_test_res res = GREATEST_SAVE_CONTEXT();       \
+            if (res == GREATEST_TEST_RES_PASS) {                        \
+                res = TEST();                                           \
+            }                                                           \
+            greatest_test_post(res);                                    \
+        }                                                               \
+    } while (0)
+
+/* Ignore a test, don't warn about it being unused. */
+#define GREATEST_IGNORE_TEST(TEST) (void)TEST
+
+/* Run a test in the current suite with one void * argument,
+ * which can be a pointer to a struct with multiple arguments. */
+#define GREATEST_RUN_TEST1(TEST, ENV)                                   \
+    do {                                                                \
+        if (greatest_test_pre(#TEST) == 1) {                            \
+            enum greatest_test_res res = GREATEST_SAVE_CONTEXT();       \
+            if (res == GREATEST_TEST_RES_PASS) {                        \
+                res = TEST(ENV);                                        \
+            }                                                           \
+            greatest_test_post(res);                                    \
+        }                                                               \
+    } while (0)
+
+#ifdef GREATEST_VA_ARGS
+#define GREATEST_RUN_TESTp(TEST, ...)                                   \
+    do {                                                                \
+        if (greatest_test_pre(#TEST) == 1) {                            \
+            enum greatest_test_res res = GREATEST_SAVE_CONTEXT();       \
+            if (res == GREATEST_TEST_RES_PASS) {                        \
+                res = TEST(__VA_ARGS__);                                \
+            }                                                           \
+            greatest_test_post(res);                                    \
+        }                                                               \
+    } while (0)
+#endif
+
+
+/* Check if the test runner is in verbose mode. */
+#define GREATEST_IS_VERBOSE() ((greatest_info.verbosity) > 0)
+#define GREATEST_LIST_ONLY()                                            \
+    (greatest_info.flags & GREATEST_FLAG_LIST_ONLY)
+#define GREATEST_FIRST_FAIL()                                           \
+    (greatest_info.flags & GREATEST_FLAG_FIRST_FAIL)
+#define GREATEST_ABORT_ON_FAIL()                                        \
+    (greatest_info.flags & GREATEST_FLAG_ABORT_ON_FAIL)
+#define GREATEST_FAILURE_ABORT()                                        \
+    (GREATEST_FIRST_FAIL() &&                                           \
+        (greatest_info.suite.failed > 0 || greatest_info.failed > 0))
+
+/* Message-less forms of tests defined below. */
+#define GREATEST_PASS() GREATEST_PASSm(NULL)
+#define GREATEST_FAIL() GREATEST_FAILm(NULL)
+#define GREATEST_SKIP() GREATEST_SKIPm(NULL)
+#define GREATEST_ASSERT(COND)                                           \
+    GREATEST_ASSERTm(#COND, COND)
+#define GREATEST_ASSERT_OR_LONGJMP(COND)                                \
+    GREATEST_ASSERT_OR_LONGJMPm(#COND, COND)
+#define GREATEST_ASSERT_FALSE(COND)                                     \
+    GREATEST_ASSERT_FALSEm(#COND, COND)
+#define GREATEST_ASSERT_EQ(EXP, GOT)                                    \
+    GREATEST_ASSERT_EQm(#EXP " != " #GOT, EXP, GOT)
+#define GREATEST_ASSERT_EQ_FMT(EXP, GOT, FMT)                           \
+    GREATEST_ASSERT_EQ_FMTm(#EXP " != " #GOT, EXP, GOT, FMT)
+#define GREATEST_ASSERT_IN_RANGE(EXP, GOT, TOL)                         \
+    GREATEST_ASSERT_IN_RANGEm(#EXP " != " #GOT " +/- " #TOL, EXP, GOT, TOL)
+#define GREATEST_ASSERT_EQUAL_T(EXP, GOT, TYPE_INFO, UDATA)             \
+    GREATEST_ASSERT_EQUAL_Tm(#EXP " != " #GOT, EXP, GOT, TYPE_INFO, UDATA)
+#define GREATEST_ASSERT_STR_EQ(EXP, GOT)                                \
+    GREATEST_ASSERT_STR_EQm(#EXP " != " #GOT, EXP, GOT)
+#define GREATEST_ASSERT_STRN_EQ(EXP, GOT, SIZE)                         \
+    GREATEST_ASSERT_STRN_EQm(#EXP " != " #GOT, EXP, GOT, SIZE)
+#define GREATEST_ASSERT_MEM_EQ(EXP, GOT, SIZE)                          \
+    GREATEST_ASSERT_MEM_EQm(#EXP " != " #GOT, EXP, GOT, SIZE)
+#define GREATEST_ASSERT_ENUM_EQ(EXP, GOT, ENUM_STR)                     \
+    GREATEST_ASSERT_ENUM_EQm(#EXP " != " #GOT, EXP, GOT, ENUM_STR)
+
+/* The following forms take an additional message argument first,
+ * to be displayed by the test runner. */
+
+/* Fail if a condition is not true, with message. */
+#define GREATEST_ASSERTm(MSG, COND)                                     \
+    do {                                                                \
+        greatest_info.assertions++;                                     \
+        if (!(COND)) { GREATEST_FAILm(MSG); }                           \
+    } while (0)
+
+/* Fail if a condition is not true, longjmping out of test. */
+#define GREATEST_ASSERT_OR_LONGJMPm(MSG, COND)                          \
+    do {                                                                \
+        greatest_info.assertions++;                                     \
+        if (!(COND)) { GREATEST_FAIL_WITH_LONGJMPm(MSG); }              \
+    } while (0)
+
+/* Fail if a condition is not false, with message. */
+#define GREATEST_ASSERT_FALSEm(MSG, COND)                               \
+    do {                                                                \
+        greatest_info.assertions++;                                     \
+        if ((COND)) { GREATEST_FAILm(MSG); }                            \
+    } while (0)
+
+/* Fail if EXP != GOT (equality comparison by ==). */
+#define GREATEST_ASSERT_EQm(MSG, EXP, GOT)                              \
+    do {                                                                \
+        greatest_info.assertions++;                                     \
+        if ((EXP) != (GOT)) { GREATEST_FAILm(MSG); }                    \
+    } while (0)
+
+/* Fail if EXP != GOT (equality comparison by ==).
+ * Warning: FMT, EXP, and GOT will be evaluated more
+ * than once on failure. */
+#define GREATEST_ASSERT_EQ_FMTm(MSG, EXP, GOT, FMT)                     \
+    do {                                                                \
+        greatest_info.assertions++;                                     \
+        if ((EXP) != (GOT)) {                                           \
+            GREATEST_FPRINTF(GREATEST_STDOUT, "\nExpected: ");          \
+            GREATEST_FPRINTF(GREATEST_STDOUT, FMT, EXP);                \
+            GREATEST_FPRINTF(GREATEST_STDOUT, "\n     Got: ");          \
+            GREATEST_FPRINTF(GREATEST_STDOUT, FMT, GOT);                \
+            GREATEST_FPRINTF(GREATEST_STDOUT, "\n");                    \
+            GREATEST_FAILm(MSG);                                        \
+        }                                                               \
+    } while (0)
+
+/* Fail if EXP is not equal to GOT, printing enum IDs. */
+#define GREATEST_ASSERT_ENUM_EQm(MSG, EXP, GOT, ENUM_STR)               \
+    do {                                                                \
+        int greatest_EXP = (int)(EXP);                                  \
+        int greatest_GOT = (int)(GOT);                                  \
+        greatest_enum_str_fun *greatest_ENUM_STR = ENUM_STR;            \
+        if (greatest_EXP != greatest_GOT) {                             \
+            GREATEST_FPRINTF(GREATEST_STDOUT, "\nExpected: %s",         \
+                greatest_ENUM_STR(greatest_EXP));                       \
+            GREATEST_FPRINTF(GREATEST_STDOUT, "\n     Got: %s\n",       \
+                greatest_ENUM_STR(greatest_GOT));                       \
+            GREATEST_FAILm(MSG);                                        \
+        }                                                               \
+    } while (0)                                                         \
+
+/* Fail if GOT not in range of EXP +|- TOL. */
+#define GREATEST_ASSERT_IN_RANGEm(MSG, EXP, GOT, TOL)                   \
+    do {                                                                \
+        GREATEST_FLOAT greatest_EXP = (EXP);                            \
+        GREATEST_FLOAT greatest_GOT = (GOT);                            \
+        GREATEST_FLOAT greatest_TOL = (TOL);                            \
+        greatest_info.assertions++;                                     \
+        if ((greatest_EXP > greatest_GOT &&                             \
+                greatest_EXP - greatest_GOT > greatest_TOL) ||          \
+            (greatest_EXP < greatest_GOT &&                             \
+                greatest_GOT - greatest_EXP > greatest_TOL)) {          \
+            GREATEST_FPRINTF(GREATEST_STDOUT,                           \
+                "\nExpected: " GREATEST_FLOAT_FMT                       \
+                " +/- " GREATEST_FLOAT_FMT                              \
+                "\n     Got: " GREATEST_FLOAT_FMT                       \
+                "\n",                                                   \
+                greatest_EXP, greatest_TOL, greatest_GOT);              \
+            GREATEST_FAILm(MSG);                                        \
+        }                                                               \
+    } while (0)
+
+/* Fail if EXP is not equal to GOT, according to strcmp. */
+#define GREATEST_ASSERT_STR_EQm(MSG, EXP, GOT)                          \
+    do {                                                                \
+        GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT,                         \
+            &greatest_type_info_string, NULL);                          \
+    } while (0)                                                         \
+
+/* Fail if EXP is not equal to GOT, according to strncmp. */
+#define GREATEST_ASSERT_STRN_EQm(MSG, EXP, GOT, SIZE)                   \
+    do {                                                                \
+        size_t size = SIZE;                                             \
+        GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT,                         \
+            &greatest_type_info_string, &size);                         \
+    } while (0)                                                         \
+
+/* Fail if EXP is not equal to GOT, according to memcmp. */
+#define GREATEST_ASSERT_MEM_EQm(MSG, EXP, GOT, SIZE)                    \
+    do {                                                                \
+        greatest_memory_cmp_env env;                                    \
+        env.exp = (const unsigned char *)EXP;                           \
+        env.got = (const unsigned char *)GOT;                           \
+        env.size = SIZE;                                                \
+        GREATEST_ASSERT_EQUAL_Tm(MSG, env.exp, env.got,                 \
+            &greatest_type_info_memory, &env);                          \
+    } while (0)                                                         \
+
+/* Fail if EXP is not equal to GOT, according to a comparison
+ * callback in TYPE_INFO. If they are not equal, optionally use a
+ * print callback in TYPE_INFO to print them. */
+#define GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT, TYPE_INFO, UDATA)       \
+    do {                                                                \
+        greatest_type_info *type_info = (TYPE_INFO);                    \
+        greatest_info.assertions++;                                     \
+        if (!greatest_do_assert_equal_t(EXP, GOT,                       \
+                type_info, UDATA)) {                                    \
+            if (type_info == NULL || type_info->equal == NULL) {        \
+                GREATEST_FAILm("type_info->equal callback missing!");   \
+            } else {                                                    \
+                GREATEST_FAILm(MSG);                                    \
+            }                                                           \
+        }                                                               \
+    } while (0)                                                         \
+
+/* Pass. */
+#define GREATEST_PASSm(MSG)                                             \
+    do {                                                                \
+        greatest_info.msg = MSG;                                        \
+        return GREATEST_TEST_RES_PASS;                                  \
+    } while (0)
+
+/* Fail. */
+#define GREATEST_FAILm(MSG)                                             \
+    do {                                                                \
+        greatest_info.fail_file = __FILE__;                             \
+        greatest_info.fail_line = __LINE__;                             \
+        greatest_info.msg = MSG;                                        \
+        if (GREATEST_ABORT_ON_FAIL()) { abort(); }                      \
+        return GREATEST_TEST_RES_FAIL;                                  \
+    } while (0)
+
+/* Optional GREATEST_FAILm variant that longjmps. */
+#if GREATEST_USE_LONGJMP
+#define GREATEST_FAIL_WITH_LONGJMP() GREATEST_FAIL_WITH_LONGJMPm(NULL)
+#define GREATEST_FAIL_WITH_LONGJMPm(MSG)                                \
+    do {                                                                \
+        greatest_info.fail_file = __FILE__;                             \
+        greatest_info.fail_line = __LINE__;                             \
+        greatest_info.msg = MSG;                                        \
+        longjmp(greatest_info.jump_dest, GREATEST_TEST_RES_FAIL);       \
+    } while (0)
+#endif
+
+/* Skip the current test. */
+#define GREATEST_SKIPm(MSG)                                             \
+    do {                                                                \
+        greatest_info.msg = MSG;                                        \
+        return GREATEST_TEST_RES_SKIP;                                  \
+    } while (0)
+
+/* Check the result of a subfunction using ASSERT, etc. */
+#define GREATEST_CHECK_CALL(RES)                                        \
+    do {                                                                \
+        enum greatest_test_res greatest_RES = RES;                      \
+        if (greatest_RES != GREATEST_TEST_RES_PASS) {                   \
+            return greatest_RES;                                        \
+        }                                                               \
+    } while (0)                                                         \
+
+#if GREATEST_USE_TIME
+#define GREATEST_SET_TIME(NAME)                                         \
+    NAME = clock();                                                     \
+    if (NAME == (clock_t) -1) {                                         \
+        GREATEST_FPRINTF(GREATEST_STDOUT,                               \
+            "clock error: %s\n", #NAME);                                \
+        exit(EXIT_FAILURE);                                             \
+    }
+
+#define GREATEST_CLOCK_DIFF(C1, C2)                                     \
+    GREATEST_FPRINTF(GREATEST_STDOUT, " (%lu ticks, %.3f sec)",         \
+        (long unsigned int) (C2) - (long unsigned int)(C1),             \
+        (double)((C2) - (C1)) / (1.0 * (double)CLOCKS_PER_SEC))
+#else
+#define GREATEST_SET_TIME(UNUSED)
+#define GREATEST_CLOCK_DIFF(UNUSED1, UNUSED2)
+#endif
+
+#if GREATEST_USE_LONGJMP
+#define GREATEST_SAVE_CONTEXT()                                         \
+        /* setjmp returns 0 (GREATEST_TEST_RES_PASS) on first call *    \
+         * so the test runs, then RES_FAIL from FAIL_WITH_LONGJMP. */   \
+        ((enum greatest_test_res)(setjmp(greatest_info.jump_dest)))
+#else
+#define GREATEST_SAVE_CONTEXT()                                         \
+    /*a no-op, since setjmp/longjmp aren't being used */                \
+    GREATEST_TEST_RES_PASS
+#endif
+
+/* Run every suite / test function run within BODY in pseudo-random
+ * order, seeded by SEED. (The top 3 bits of the seed are ignored.)
+ *
+ * This should be called like:
+ *     GREATEST_SHUFFLE_TESTS(seed, {
+ *         GREATEST_RUN_TEST(some_test);
+ *         GREATEST_RUN_TEST(some_other_test);
+ *         GREATEST_RUN_TEST(yet_another_test);
+ *     });
+ *
+ * Note that the body of the second argument will be evaluated
+ * multiple times. */
+#define GREATEST_SHUFFLE_SUITES(SD, BODY) GREATEST_SHUFFLE(0, SD, BODY)
+#define GREATEST_SHUFFLE_TESTS(SD, BODY) GREATEST_SHUFFLE(1, SD, BODY)
+#define GREATEST_SHUFFLE(ID, SD, BODY)                                  \
+    do {                                                                \
+        struct greatest_prng *prng = &greatest_info.prng[ID];           \
+        greatest_prng_init_first_pass(ID);                              \
+        do {                                                            \
+            prng->count = 0;                                            \
+            if (prng->initialized) { greatest_prng_step(ID); }          \
+            BODY;                                                       \
+            if (!prng->initialized) {                                   \
+                if (!greatest_prng_init_second_pass(ID, SD)) { break; } \
+            } else if (prng->count_run == prng->count_ceil) {           \
+                break;                                                  \
+            }                                                           \
+        } while (!GREATEST_FAILURE_ABORT());                            \
+        prng->count_run = prng->random_order = prng->initialized = 0;   \
+    } while(0)
+
+/* Include several function definitions in the main test file. */
+#define GREATEST_MAIN_DEFS()                                            \
+                                                                        \
+/* Is FILTER a subset of NAME? */                                       \
+static int greatest_name_match(const char *name, const char *filter,    \
+        int res_if_none) {                                              \
+    size_t offset = 0;                                                  \
+    size_t filter_len = filter ? strlen(filter) : 0;                    \
+    if (filter_len == 0) { return res_if_none; } /* no filter */        \
+    while (name[offset] != '\0') {                                      \
+        if (name[offset] == filter[0]) {                                \
+            if (0 == strncmp(&name[offset], filter, filter_len)) {      \
+                return 1;                                               \
+            }                                                           \
+        }                                                               \
+        offset++;                                                       \
+    }                                                                   \
+                                                                        \
+    return 0;                                                           \
+}                                                                       \
+                                                                        \
+static void greatest_buffer_test_name(const char *name) {               \
+    struct greatest_run_info *g = &greatest_info;                       \
+    size_t len = strlen(name), size = sizeof(g->name_buf);              \
+    memset(g->name_buf, 0x00, size);                                    \
+    (void)strncat(g->name_buf, name, size - 1);                         \
+    if (g->name_suffix && (len + 1 < size)) {                           \
+        g->name_buf[len] = '_';                                         \
+        strncat(&g->name_buf[len+1], g->name_suffix, size-(len+2));     \
+    }                                                                   \
+}                                                                       \
+                                                                        \
+/* Before running a test, check the name filtering and                  \
+ * test shuffling state, if applicable, and then call setup hooks. */   \
+int greatest_test_pre(const char *name) {                               \
+    struct greatest_run_info *g = &greatest_info;                       \
+    int match;                                                          \
+    greatest_buffer_test_name(name);                                    \
+    match = greatest_name_match(g->name_buf, g->test_filter, 1) &&      \
+      !greatest_name_match(g->name_buf, g->test_exclude, 0);            \
+    if (GREATEST_LIST_ONLY()) {   /* just listing test names */         \
+        if (match) {                                                    \
+            GREATEST_FPRINTF(GREATEST_STDOUT, "  %s\n", g->name_buf);   \
+        }                                                               \
+        goto clear;                                                     \
+    }                                                                   \
+    if (match && (!GREATEST_FIRST_FAIL() || g->suite.failed == 0)) {    \
+            struct greatest_prng *p = &g->prng[1];                      \
+        if (p->random_order) {                                          \
+            p->count++;                                                 \
+            if (!p->initialized || ((p->count - 1) != p->state)) {      \
+                goto clear;       /* don't run this test yet */         \
+            }                                                           \
+        }                                                               \
+        GREATEST_SET_TIME(g->suite.pre_test);                           \
+        if (g->setup) { g->setup(g->setup_udata); }                     \
+        p->count_run++;                                                 \
+        return 1;                 /* test should be run */              \
+    } else {                                                            \
+        goto clear;               /* skipped */                         \
+    }                                                                   \
+clear:                                                                  \
+    g->name_suffix = NULL;                                              \
+    return 0;                                                           \
+}                                                                       \
+                                                                        \
+static void greatest_do_pass(void) {                                    \
+    struct greatest_run_info *g = &greatest_info;                       \
+    if (GREATEST_IS_VERBOSE()) {                                        \
+        GREATEST_FPRINTF(GREATEST_STDOUT, "PASS %s: %s",                \
+            g->name_buf, g->msg ? g->msg : "");                         \
+    } else {                                                            \
+        GREATEST_FPRINTF(GREATEST_STDOUT, ".");                         \
+    }                                                                   \
+    g->suite.passed++;                                                  \
+}                                                                       \
+                                                                        \
+static void greatest_do_fail(void) {                                    \
+    struct greatest_run_info *g = &greatest_info;                       \
+    if (GREATEST_IS_VERBOSE()) {                                        \
+        GREATEST_FPRINTF(GREATEST_STDOUT,                               \
+            "FAIL %s: %s (%s:%u)", g->name_buf,                         \
+            g->msg ? g->msg : "", g->fail_file, g->fail_line);          \
+    } else {                                                            \
+        GREATEST_FPRINTF(GREATEST_STDOUT, "F");                         \
+        g->col++;  /* add linebreak if in line of '.'s */               \
+        if (g->col != 0) {                                              \
+            GREATEST_FPRINTF(GREATEST_STDOUT, "\n");                    \
+            g->col = 0;                                                 \
+        }                                                               \
+        GREATEST_FPRINTF(GREATEST_STDOUT, "FAIL %s: %s (%s:%u)\n",      \
+            g->name_buf, g->msg ? g->msg : "",                          \
+            g->fail_file, g->fail_line);                                \
+    }                                                                   \
+    g->suite.failed++;                                                  \
+}                                                                       \
+                                                                        \
+static void greatest_do_skip(void) {                                    \
+    struct greatest_run_info *g = &greatest_info;                       \
+    if (GREATEST_IS_VERBOSE()) {                                        \
+        GREATEST_FPRINTF(GREATEST_STDOUT, "SKIP %s: %s",                \
+            g->name_buf, g->msg ? g->msg : "");                         \
+    } else {                                                            \
+        GREATEST_FPRINTF(GREATEST_STDOUT, "s");                         \
+    }                                                                   \
+    g->suite.skipped++;                                                 \
+}                                                                       \
+                                                                        \
+void greatest_test_post(int res) {                                      \
+    GREATEST_SET_TIME(greatest_info.suite.post_test);                   \
+    if (greatest_info.teardown) {                                       \
+        void *udata = greatest_info.teardown_udata;                     \
+        greatest_info.teardown(udata);                                  \
+    }                                                                   \
+                                                                        \
+    if (res <= GREATEST_TEST_RES_FAIL) {                                \
+        greatest_do_fail();                                             \
+    } else if (res >= GREATEST_TEST_RES_SKIP) {                         \
+        greatest_do_skip();                                             \
+    } else if (res == GREATEST_TEST_RES_PASS) {                         \
+        greatest_do_pass();                                             \
+    }                                                                   \
+    greatest_info.name_suffix = NULL;                                   \
+    greatest_info.suite.tests_run++;                                    \
+    greatest_info.col++;                                                \
+    if (GREATEST_IS_VERBOSE()) {                                        \
+        GREATEST_CLOCK_DIFF(greatest_info.suite.pre_test,               \
+            greatest_info.suite.post_test);                             \
+        GREATEST_FPRINTF(GREATEST_STDOUT, "\n");                        \
+    } else if (greatest_info.col % greatest_info.width == 0) {          \
+        GREATEST_FPRINTF(GREATEST_STDOUT, "\n");                        \
+        greatest_info.col = 0;                                          \
+    }                                                                   \
+    fflush(GREATEST_STDOUT);                                            \
+}                                                                       \
+                                                                        \
+static void report_suite(void) {                                        \
+    if (greatest_info.suite.tests_run > 0) {                            \
+        GREATEST_FPRINTF(GREATEST_STDOUT,                               \
+            "\n%u test%s - %u passed, %u failed, %u skipped",           \
+            greatest_info.suite.tests_run,                              \
+            greatest_info.suite.tests_run == 1 ? "" : "s",              \
+            greatest_info.suite.passed,                                 \
+            greatest_info.suite.failed,                                 \
+            greatest_info.suite.skipped);                               \
+        GREATEST_CLOCK_DIFF(greatest_info.suite.pre_suite,              \
+            greatest_info.suite.post_suite);                            \
+        GREATEST_FPRINTF(GREATEST_STDOUT, "\n");                        \
+    }                                                                   \
+}                                                                       \
+                                                                        \
+static void update_counts_and_reset_suite(void) {                       \
+    greatest_info.setup = NULL;                                         \
+    greatest_info.setup_udata = NULL;                                   \
+    greatest_info.teardown = NULL;                                      \
+    greatest_info.teardown_udata = NULL;                                \
+    greatest_info.passed += greatest_info.suite.passed;                 \
+    greatest_info.failed += greatest_info.suite.failed;                 \
+    greatest_info.skipped += greatest_info.suite.skipped;               \
+    greatest_info.tests_run += greatest_info.suite.tests_run;           \
+    memset(&greatest_info.suite, 0, sizeof(greatest_info.suite));       \
+    greatest_info.col = 0;                                              \
+}                                                                       \
+                                                                        \
+static int greatest_suite_pre(const char *suite_name) {                 \
+    struct greatest_prng *p = &greatest_info.prng[0];                   \
+    if (!greatest_name_match(suite_name, greatest_info.suite_filter, 1) \
+        || (GREATEST_FAILURE_ABORT())) { return 0; }                    \
+    if (p->random_order) {                                              \
+        p->count++;                                                     \
+        if (!p->initialized || ((p->count - 1) != p->state)) {          \
+            return 0; /* don't run this suite yet */                    \
+        }                                                               \
+    }                                                                   \
+    p->count_run++;                                                     \
+    update_counts_and_reset_suite();                                    \
+    GREATEST_FPRINTF(GREATEST_STDOUT, "\n* Suite %s:\n", suite_name);   \
+    GREATEST_SET_TIME(greatest_info.suite.pre_suite);                   \
+    return 1;                                                           \
+}                                                                       \
+                                                                        \
+static void greatest_suite_post(void) {                                 \
+    GREATEST_SET_TIME(greatest_info.suite.post_suite);                  \
+    report_suite();                                                     \
+}                                                                       \
+                                                                        \
+static void greatest_run_suite(greatest_suite_cb *suite_cb,             \
+                               const char *suite_name) {                \
+    if (greatest_suite_pre(suite_name)) {                               \
+        suite_cb();                                                     \
+        greatest_suite_post();                                          \
+    }                                                                   \
+}                                                                       \
+                                                                        \
+int greatest_do_assert_equal_t(const void *expd, const void *got,       \
+        greatest_type_info *type_info, void *udata) {                   \
+    int eq = 0;                                                         \
+    if (type_info == NULL || type_info->equal == NULL) {                \
+        return 0;                                                       \
+    }                                                                   \
+    eq = type_info->equal(expd, got, udata);                            \
+    if (!eq) {                                                          \
+        if (type_info->print != NULL) {                                 \
+            GREATEST_FPRINTF(GREATEST_STDOUT, "\nExpected: ");          \
+            (void)type_info->print(expd, udata);                        \
+            GREATEST_FPRINTF(GREATEST_STDOUT, "\n     Got: ");          \
+            (void)type_info->print(got, udata);                         \
+            GREATEST_FPRINTF(GREATEST_STDOUT, "\n");                    \
+        }                                                               \
+    }                                                                   \
+    return eq;                                                          \
+}                                                                       \
+                                                                        \
+static void greatest_usage(const char *name) {                          \
+    GREATEST_FPRINTF(GREATEST_STDOUT,                                   \
+        "Usage: %s [-hlfav] [-s SUITE] [-t TEST] [-x EXCLUDE]\n"        \
+        "  -h, --help  print this Help\n"                               \
+        "  -l          List suites and tests, then exit (dry run)\n"    \
+        "  -f          Stop runner after first failure\n"               \
+        "  -a          Abort on first failure (implies -f)\n"           \
+        "  -v          Verbose output\n"                                \
+        "  -s SUITE    only run suites containing substring SUITE\n"    \
+        "  -t TEST     only run tests containing substring TEST\n"      \
+        "  -x EXCLUDE  exclude tests containing substring EXCLUDE\n",   \
+        name);                                                          \
+}                                                                       \
+                                                                        \
+static void greatest_parse_options(int argc, char **argv) {             \
+    int i = 0;                                                          \
+    for (i = 1; i < argc; i++) {                                        \
+        if (argv[i][0] == '-') {                                        \
+            char f = argv[i][1];                                        \
+            if ((f == 's' || f == 't' || f == 'x') && argc <= i + 1) {  \
+                greatest_usage(argv[0]); exit(EXIT_FAILURE);            \
+            }                                                           \
+            switch (f) {                                                \
+            case 's': /* suite name filter */                           \
+                greatest_set_suite_filter(argv[i + 1]); i++; break;     \
+            case 't': /* test name filter */                            \
+                greatest_set_test_filter(argv[i + 1]); i++; break;      \
+            case 'x': /* test name exclusion */                         \
+                greatest_set_test_exclude(argv[i + 1]); i++; break;     \
+            case 'f': /* first fail flag */                             \
+                greatest_stop_at_first_fail(); break;                   \
+            case 'a': /* abort() on fail flag */                        \
+                greatest_abort_on_fail(); break;                        \
+            case 'l': /* list only (dry run) */                         \
+                greatest_list_only(); break;                            \
+            case 'v': /* first fail flag */                             \
+                greatest_info.verbosity++; break;                       \
+            case 'h': /* help */                                        \
+                greatest_usage(argv[0]); exit(EXIT_SUCCESS);            \
+            case '-':                                                   \
+                if (0 == strncmp("--help", argv[i], 6)) {               \
+                    greatest_usage(argv[0]); exit(EXIT_SUCCESS);        \
+                } else if (0 == strncmp("--", argv[i], 2)) {            \
+                    return; /* ignore following arguments */            \
+                }  /* fall through */                                   \
+            default:                                                    \
+                GREATEST_FPRINTF(GREATEST_STDOUT,                       \
+                    "Unknown argument '%s'\n", argv[i]);                \
+                greatest_usage(argv[0]);                                \
+                exit(EXIT_FAILURE);                                     \
+            }                                                           \
+        }                                                               \
+    }                                                                   \
+}                                                                       \
+                                                                        \
+int greatest_all_passed(void) { return (greatest_info.failed == 0); }   \
+                                                                        \
+void greatest_set_test_filter(const char *filter) {                     \
+    greatest_info.test_filter = filter;                                 \
+}                                                                       \
+                                                                        \
+void greatest_set_test_exclude(const char *filter) {                    \
+    greatest_info.test_exclude = filter;                                \
+}                                                                       \
+                                                                        \
+void greatest_set_suite_filter(const char *filter) {                    \
+    greatest_info.suite_filter = filter;                                \
+}                                                                       \
+                                                                        \
+void greatest_stop_at_first_fail(void) {                                \
+    greatest_set_flag(GREATEST_FLAG_FIRST_FAIL);                        \
+}                                                                       \
+                                                                        \
+void greatest_abort_on_fail(void) {                                     \
+    greatest_set_flag(GREATEST_FLAG_ABORT_ON_FAIL);                     \
+}                                                                       \
+                                                                        \
+void greatest_list_only(void) {                                         \
+    greatest_set_flag(GREATEST_FLAG_LIST_ONLY);                         \
+}                                                                       \
+                                                                        \
+void greatest_get_report(struct greatest_report_t *report) {            \
+    if (report) {                                                       \
+        report->passed = greatest_info.passed;                          \
+        report->failed = greatest_info.failed;                          \
+        report->skipped = greatest_info.skipped;                        \
+        report->assertions = greatest_info.assertions;                  \
+    }                                                                   \
+}                                                                       \
+                                                                        \
+unsigned int greatest_get_verbosity(void) {                             \
+    return greatest_info.verbosity;                                     \
+}                                                                       \
+                                                                        \
+void greatest_set_verbosity(unsigned int verbosity) {                   \
+    greatest_info.verbosity = (unsigned char)verbosity;                 \
+}                                                                       \
+                                                                        \
+void greatest_set_flag(greatest_flag_t flag) {                          \
+    greatest_info.flags = (unsigned char)(greatest_info.flags | flag);  \
+}                                                                       \
+                                                                        \
+void greatest_set_test_suffix(const char *suffix) {                     \
+    greatest_info.name_suffix = suffix;                                 \
+}                                                                       \
+                                                                        \
+void GREATEST_SET_SETUP_CB(greatest_setup_cb *cb, void *udata) {        \
+    greatest_info.setup = cb;                                           \
+    greatest_info.setup_udata = udata;                                  \
+}                                                                       \
+                                                                        \
+void GREATEST_SET_TEARDOWN_CB(greatest_teardown_cb *cb,                 \
+                                    void *udata) {                      \
+    greatest_info.teardown = cb;                                        \
+    greatest_info.teardown_udata = udata;                               \
+}                                                                       \
+                                                                        \
+static int greatest_string_equal_cb(const void *expd, const void *got,  \
+    void *udata) {                                                      \
+    size_t *size = (size_t *)udata;                                     \
+    return (size != NULL                                                \
+        ? (0 == strncmp((const char *)expd, (const char *)got, *size))  \
+        : (0 == strcmp((const char *)expd, (const char *)got)));        \
+}                                                                       \
+                                                                        \
+static int greatest_string_printf_cb(const void *t, void *udata) {      \
+    (void)udata; /* note: does not check \0 termination. */             \
+    return GREATEST_FPRINTF(GREATEST_STDOUT, "%s", (const char *)t);    \
+}                                                                       \
+                                                                        \
+greatest_type_info greatest_type_info_string = {                        \
+    greatest_string_equal_cb,                                           \
+    greatest_string_printf_cb,                                          \
+};                                                                      \
+                                                                        \
+static int greatest_memory_equal_cb(const void *expd, const void *got,  \
+    void *udata) {                                                      \
+    greatest_memory_cmp_env *env = (greatest_memory_cmp_env *)udata;    \
+    return (0 == memcmp(expd, got, env->size));                         \
+}                                                                       \
+                                                                        \
+/* Hexdump raw memory, with differences highlighted */                  \
+static int greatest_memory_printf_cb(const void *t, void *udata) {      \
+    greatest_memory_cmp_env *env = (greatest_memory_cmp_env *)udata;    \
+    const unsigned char *buf = (const unsigned char *)t;                \
+    unsigned char diff_mark = ' ';                                      \
+    FILE *out = GREATEST_STDOUT;                                        \
+    size_t i, line_i, line_len = 0;                                     \
+    int len = 0;   /* format hexdump with differences highlighted */    \
+    for (i = 0; i < env->size; i+= line_len) {                          \
+        diff_mark = ' ';                                                \
+        line_len = env->size - i;                                       \
+        if (line_len > 16) { line_len = 16; }                           \
+        for (line_i = i; line_i < i + line_len; line_i++) {             \
+            if (env->exp[line_i] != env->got[line_i]) diff_mark = 'X';  \
+        }                                                               \
+        len += GREATEST_FPRINTF(out, "\n%04x %c ",                      \
+            (unsigned int)i, diff_mark);                                \
+        for (line_i = i; line_i < i + line_len; line_i++) {             \
+            int m = env->exp[line_i] == env->got[line_i]; /* match? */  \
+            len += GREATEST_FPRINTF(out, "%02x%c",                      \
+                buf[line_i], m ? ' ' : '<');                            \
+        }                                                               \
+        for (line_i = 0; line_i < 16 - line_len; line_i++) {            \
+            len += GREATEST_FPRINTF(out, "   ");                        \
+        }                                                               \
+        GREATEST_FPRINTF(out, " ");                                     \
+        for (line_i = i; line_i < i + line_len; line_i++) {             \
+            unsigned char c = buf[line_i];                              \
+            len += GREATEST_FPRINTF(out, "%c", isprint(c) ? c : '.');   \
+        }                                                               \
+    }                                                                   \
+    len += GREATEST_FPRINTF(out, "\n");                                 \
+    return len;                                                         \
+}                                                                       \
+                                                                        \
+void greatest_prng_init_first_pass(int id) {                            \
+    greatest_info.prng[id].random_order = 1;                            \
+    greatest_info.prng[id].count_run = 0;                               \
+}                                                                       \
+                                                                        \
+int greatest_prng_init_second_pass(int id, unsigned long seed) {        \
+    struct greatest_prng *p = &greatest_info.prng[id];                  \
+    if (p->count == 0) { return 0; }                                    \
+    p->count_ceil = p->count;                                           \
+    for (p->m = 1; p->m < p->count; p->m <<= 1) {}                      \
+    p->state = seed & 0x1fffffff;     /* only use lower 29 bits */      \
+    p->a = 4LU * p->state;            /* to avoid overflow when */      \
+    p->a = (p->a ? p->a : 4) | 1;            /* multiplied by 4 */      \
+    p->c = 2147483647;        /* and so p->c ((2 ** 31) - 1) is */      \
+    p->initialized = 1;     /* always relatively prime to p->a. */      \
+    fprintf(stderr, "init_second_pass: a %lu, c %lu, state %lu\n",      \
+        p->a, p->c, p->state);                                          \
+    return 1;                                                           \
+}                                                                       \
+                                                                        \
+/* Step the pseudorandom number generator until its state reaches       \
+ * another test ID between 0 and the test count.                        \
+ * This use a linear congruential pseudorandom number generator,        \
+ * with the power-of-two ceiling of the test count as the modulus, the  \
+ * masked seed as the multiplier, and a prime as the increment. For     \
+ * each generated value < the test count, run the corresponding test.   \
+ * This will visit all IDs 0 <= X < mod once before repeating,          \
+ * with a starting position chosen based on the initial seed.           \
+ * For details, see: Knuth, The Art of Computer Programming             \
+ * Volume. 2, section 3.2.1. */                                         \
+void greatest_prng_step(int id) {                                       \
+    struct greatest_prng *p = &greatest_info.prng[id];                  \
+    do {                                                                \
+        p->state = ((p->a * p->state) + p->c) & (p->m - 1);             \
+    } while (p->state >= p->count_ceil);                                \
+}                                                                       \
+                                                                        \
+void GREATEST_INIT(void) {                                              \
+    /* Suppress unused function warning if features aren't used */      \
+    (void)greatest_run_suite;                                           \
+    (void)greatest_parse_options;                                       \
+    (void)greatest_prng_step;                                           \
+    (void)greatest_prng_init_first_pass;                                \
+    (void)greatest_prng_init_second_pass;                               \
+    (void)greatest_set_test_suffix;                                     \
+                                                                        \
+    memset(&greatest_info, 0, sizeof(greatest_info));                   \
+    greatest_info.width = GREATEST_DEFAULT_WIDTH;                       \
+    GREATEST_SET_TIME(greatest_info.begin);                             \
+}                                                                       \
+                                                                        \
+/* Report passes, failures, skipped tests, the number of                \
+ * assertions, and the overall run time. */                             \
+void GREATEST_PRINT_REPORT(void) {                                      \
+    if (!GREATEST_LIST_ONLY()) {                                        \
+        update_counts_and_reset_suite();                                \
+        GREATEST_SET_TIME(greatest_info.end);                           \
+        GREATEST_FPRINTF(GREATEST_STDOUT,                               \
+            "\nTotal: %u test%s",                                       \
+            greatest_info.tests_run,                                    \
+            greatest_info.tests_run == 1 ? "" : "s");                   \
+        GREATEST_CLOCK_DIFF(greatest_info.begin,                        \
+            greatest_info.end);                                         \
+        GREATEST_FPRINTF(GREATEST_STDOUT, ", %u assertion%s\n",         \
+            greatest_info.assertions,                                   \
+            greatest_info.assertions == 1 ? "" : "s");                  \
+        GREATEST_FPRINTF(GREATEST_STDOUT,                               \
+            "Pass: %u, fail: %u, skip: %u.\n",                          \
+            greatest_info.passed,                                       \
+            greatest_info.failed, greatest_info.skipped);               \
+    }                                                                   \
+}                                                                       \
+                                                                        \
+greatest_type_info greatest_type_info_memory = {                        \
+    greatest_memory_equal_cb,                                           \
+    greatest_memory_printf_cb,                                          \
+};                                                                      \
+                                                                        \
+greatest_run_info greatest_info
+
+/* Handle command-line arguments, etc. */
+#define GREATEST_MAIN_BEGIN()                                           \
+    do {                                                                \
+        GREATEST_INIT();                                                \
+        greatest_parse_options(argc, argv);                             \
+    } while (0)
+
+/* Report results, exit with exit status based on results. */
+#define GREATEST_MAIN_END()                                             \
+    do {                                                                \
+        GREATEST_PRINT_REPORT();                                        \
+        return (greatest_all_passed() ? EXIT_SUCCESS : EXIT_FAILURE);   \
+    } while (0)
+
+/* Make abbreviations without the GREATEST_ prefix for the
+ * most commonly used symbols. */
+#if GREATEST_USE_ABBREVS
+#define TEST           GREATEST_TEST
+#define SUITE          GREATEST_SUITE
+#define SUITE_EXTERN   GREATEST_SUITE_EXTERN
+#define RUN_TEST       GREATEST_RUN_TEST
+#define RUN_TEST1      GREATEST_RUN_TEST1
+#define RUN_SUITE      GREATEST_RUN_SUITE
+#define IGNORE_TEST    GREATEST_IGNORE_TEST
+#define ASSERT         GREATEST_ASSERT
+#define ASSERTm        GREATEST_ASSERTm
+#define ASSERT_FALSE   GREATEST_ASSERT_FALSE
+#define ASSERT_EQ      GREATEST_ASSERT_EQ
+#define ASSERT_EQ_FMT  GREATEST_ASSERT_EQ_FMT
+#define ASSERT_IN_RANGE GREATEST_ASSERT_IN_RANGE
+#define ASSERT_EQUAL_T GREATEST_ASSERT_EQUAL_T
+#define ASSERT_STR_EQ  GREATEST_ASSERT_STR_EQ
+#define ASSERT_STRN_EQ GREATEST_ASSERT_STRN_EQ
+#define ASSERT_MEM_EQ  GREATEST_ASSERT_MEM_EQ
+#define ASSERT_ENUM_EQ GREATEST_ASSERT_ENUM_EQ
+#define ASSERT_FALSEm  GREATEST_ASSERT_FALSEm
+#define ASSERT_EQm     GREATEST_ASSERT_EQm
+#define ASSERT_EQ_FMTm GREATEST_ASSERT_EQ_FMTm
+#define ASSERT_IN_RANGEm GREATEST_ASSERT_IN_RANGEm
+#define ASSERT_EQUAL_Tm GREATEST_ASSERT_EQUAL_Tm
+#define ASSERT_STR_EQm GREATEST_ASSERT_STR_EQm
+#define ASSERT_STRN_EQm GREATEST_ASSERT_STRN_EQm
+#define ASSERT_MEM_EQm GREATEST_ASSERT_MEM_EQm
+#define ASSERT_ENUM_EQm GREATEST_ASSERT_ENUM_EQm
+#define PASS           GREATEST_PASS
+#define FAIL           GREATEST_FAIL
+#define SKIP           GREATEST_SKIP
+#define PASSm          GREATEST_PASSm
+#define FAILm          GREATEST_FAILm
+#define SKIPm          GREATEST_SKIPm
+#define SET_SETUP      GREATEST_SET_SETUP_CB
+#define SET_TEARDOWN   GREATEST_SET_TEARDOWN_CB
+#define CHECK_CALL     GREATEST_CHECK_CALL
+#define SHUFFLE_TESTS  GREATEST_SHUFFLE_TESTS
+#define SHUFFLE_SUITES GREATEST_SHUFFLE_SUITES
+
+#ifdef GREATEST_VA_ARGS
+#define RUN_TESTp      GREATEST_RUN_TESTp
+#endif
+
+#if GREATEST_USE_LONGJMP
+#define ASSERT_OR_LONGJMP  GREATEST_ASSERT_OR_LONGJMP
+#define ASSERT_OR_LONGJMPm GREATEST_ASSERT_OR_LONGJMPm
+#define FAIL_WITH_LONGJMP  GREATEST_FAIL_WITH_LONGJMP
+#define FAIL_WITH_LONGJMPm GREATEST_FAIL_WITH_LONGJMPm
+#endif
+
+#endif /* USE_ABBREVS */
+
+#if defined(__cplusplus) && !defined(GREATEST_NO_EXTERN_CPLUSPLUS)
+}
+#endif
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libbuf.3	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,107 @@
+.\"
+.\" libbuf.3 -- simple string buffer for C
+.\"
+.\" Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd October 29, 2019-2020
+.Dt LIBBUF 3
+.Os
+.\" NAME
+.Sh NAME
+.Nm libbuf
+.Nd minimal dynamic string library
+.\" LIBRARY
+.Sh LIBRARY
+libbuf (libbuf, -lbuf)
+.\" SYNOPSIS
+.Sh SYNOPSIS
+.In buf.h
+.Ft void
+.Fn buf_clear "struct buf *b"
+.Ft bool
+.Fn buf_dup "struct buf *b" "const struct buf *src"
+.Ft void
+.Fn buf_erase "struct buf *b" "size_t pos" "size_t count"
+.Ft void
+.Fn buf_finish "struct buf *b"
+.Ft bool
+.Fn buf_init "struct buf *b"
+.Ft bool
+.Fn buf_printf "struct buf *b" "const char *fmt" "..."
+.Ft bool
+.Fn buf_putc "struct buf *b" "char c"
+.Ft bool
+.Fn buf_puts "struct buf *b" "const char *s"
+.Ft bool
+.Fn buf_reserve "struct buf *b" "size_t desired"
+.Ft bool
+.Fn buf_resize "struct buf *b" "size_t desired" "char c"
+.Ft bool
+.Fn buf_shrink "struct buf *b"
+.Ft bool
+.Fn buf_sub "struct buf *b" "const struct buf *src" "size_t pos" "size_t count"
+.Ft bool
+.Fn buf_vprintf "struct buf *b" "const char *fmt" "va_list ap"
+.\" DESCRIPTION
+.Sh DESCRIPTION
+The
+.Nm
+library is a general purpose string library for C.
+.Pp
+It automatically expands storage as required.
+It will first try to allocate twice as the current storage space until enough
+room is available or in case it would exceed maximum storage it will grow with
+minimal required storage.
+.Pp
+Every function expects as first argument a buffer to work with which can never
+be NULL.
+.Pp
+The
+.Vt "struct buf"
+is a publicly exposed structure with the following fields:
+.Bl -tag -width "capacity"
+.It Va data
+.Va (const char *)
+The current NUL-terminated C string, maybe NULL if the buffer isn't initialized.
+.It Va length
+.Va (size_t)
+The data length.
+.It Va capacity
+.Va (size_t)
+The real capacity available for writing characters (not including NUL). A
+capacity of 5 means you can write 5 characters plus the NUL terminator without
+reallocating.
+.El
+.\" SEE ALSO
+.Sh SEE ALSO
+.Xr buf_clear 3 ,
+.Xr buf_dup 3 ,
+.Xr buf_erase 3 ,
+.Xr buf_finish 3 ,
+.Xr buf_init 3 ,
+.Xr buf_printf 3 ,
+.Xr buf_putc 3 ,
+.Xr buf_puts 3 ,
+.Xr buf_reserve 3 ,
+.Xr buf_resize 3 ,
+.Xr buf_shrink 3 ,
+.Xr buf_sub 3 ,
+.Xr buf_vprintf 3
+.\" AUTHORS
+.Sh AUTHORS
+The
+.Nm
+library was written by
+.An David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-clear.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,62 @@
+/*
+ * test-clear.c -- test buf_clear function
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+clear_set(void)
+{
+	struct buf b = {0};
+
+	/* Clear should only reduce the length capacity must not change. */
+	buf_puts(&b, "hello world");
+	buf_clear(&b);
+	GREATEST_ASSERT_STR_EQ(b.data, "");
+	GREATEST_ASSERT_EQ(b.length, 0U);
+	GREATEST_ASSERT_EQ(b.capacity, 11U);
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_TEST
+clear_empty(void)
+{
+	struct buf b = {0};
+
+	buf_clear(&b);
+	GREATEST_ASSERT_EQ(b.length, 0U);
+	GREATEST_ASSERT_EQ(b.capacity, 0U);
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(clear_set);
+	GREATEST_RUN_TEST(clear_empty);
+	GREATEST_MAIN_END();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-dup.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,65 @@
+/*
+ * test-dup.c -- test buf_dup
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+dup_set(void)
+{
+	struct buf b = {0};
+	struct buf d = {0};
+
+	buf_puts(&b, "0123456789");
+	buf_dup(&d, &b);
+	GREATEST_ASSERT(b.data != d.data);
+	GREATEST_ASSERT_STR_EQ(d.data, "0123456789");
+	GREATEST_ASSERT_EQ(d.length, 10U);
+	GREATEST_ASSERT_EQ(d.capacity, 10U);
+	buf_finish(&d);
+
+	GREATEST_PASS();
+}
+
+GREATEST_TEST
+dup_empty(void)
+{
+	struct buf b = {0};
+	struct buf d = {0};
+
+	buf_dup(&d, &b);
+	GREATEST_ASSERT(!d.data);
+	GREATEST_ASSERT_EQ(d.length, 0U);
+	GREATEST_ASSERT_EQ(d.capacity, 0U);
+	buf_finish(&d);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(dup_set);
+	GREATEST_RUN_TEST(dup_empty);
+	GREATEST_MAIN_END();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-erase.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,55 @@
+/*
+ * test-erase.c -- test buf_erase
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+erase(void)
+{
+	struct buf b = {0};
+
+	buf_puts(&b, "0123456789");
+
+	/* Only remove portion from 1 to 3, capacity must not change. */
+	buf_erase(&b, 1, 3);
+	GREATEST_ASSERT_STR_EQ(b.data, "0456789");
+	GREATEST_ASSERT_EQ(b.length, 7U);
+	GREATEST_ASSERT_EQ(b.capacity, 10U);
+
+	/* Remove from 4 to the end. */
+	buf_erase(&b, 1, -1);
+	GREATEST_ASSERT_STR_EQ(b.data, "0");
+	GREATEST_ASSERT_EQ(b.length, 1U);
+	GREATEST_ASSERT_EQ(b.capacity, 10U);
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(erase);
+	GREATEST_MAIN_END();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-finish.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,47 @@
+/*
+ * test-finish.c -- test buf_finish
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+finish(void)
+{
+	struct buf b = {0};
+
+	buf_puts(&b, "abc");
+	buf_finish(&b);
+
+	GREATEST_ASSERT(!b.data);
+	GREATEST_ASSERT_EQ(b.length, 0U);
+	GREATEST_ASSERT_EQ(b.capacity, 0U);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(finish);
+	GREATEST_MAIN_END();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-init.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,46 @@
+/*
+ * test-init.c -- test buf_init
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+init(void)
+{
+	struct buf b;
+
+	buf_init(&b);
+
+	GREATEST_ASSERT(!b.data);
+	GREATEST_ASSERT_EQ(b.length, 0U);
+	GREATEST_ASSERT_EQ(b.capacity, 0U);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(init);
+	GREATEST_MAIN_END();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-printf.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,53 @@
+/*
+ * test-printf.c -- test buf_printf
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+printf_empty(void)
+{
+	struct buf b = {0};
+
+	/* This will allocate string of 6 characters. */
+	buf_printf(&b, "%d%d", 123, 456);
+	GREATEST_ASSERT_STR_EQ(b.data, "123456");
+	GREATEST_ASSERT_EQ(b.length, 6U);
+	GREATEST_ASSERT_EQ(b.capacity, 6U);
+
+	/* This will multiply the capacity to 12 and increase length to 10. */
+	buf_printf(&b, "%s", "hoho");
+	GREATEST_ASSERT_STR_EQ(b.data, "123456hoho");
+	GREATEST_ASSERT_EQ(b.length, 10U);
+	GREATEST_ASSERT_EQ(b.capacity, 12U);
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(printf_empty);
+	GREATEST_MAIN_END();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-putc.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,69 @@
+/*
+ * test-putc.c -- test buf_putc
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+putc_empty(void)
+{
+	struct buf b = {0};
+
+	/* This will allocate one byte and then increase by power of two. */
+	buf_putc(&b, 'a');
+	GREATEST_ASSERT_STR_EQ(b.data, "a");
+	GREATEST_ASSERT_EQ(b.length, 1U);
+	GREATEST_ASSERT_EQ(b.capacity, 1U);
+
+	buf_putc(&b, 'b');
+	GREATEST_ASSERT_STR_EQ(b.data, "ab");
+	GREATEST_ASSERT_EQ(b.length, 2U);
+	GREATEST_ASSERT_EQ(b.capacity, 2U);
+
+	buf_putc(&b, 'c');
+	GREATEST_ASSERT_STR_EQ(b.data, "abc");
+	GREATEST_ASSERT_EQ(b.length, 3U);
+	GREATEST_ASSERT_EQ(b.capacity, 4U);
+
+	/* No reallocation since there is still one byte left. */
+	buf_putc(&b, 'd');
+	GREATEST_ASSERT_STR_EQ(b.data, "abcd");
+	GREATEST_ASSERT_EQ(b.length, 4U);
+	GREATEST_ASSERT_EQ(b.capacity, 4U);
+
+	/* New reallocation here. */
+	buf_putc(&b, 'e');
+	GREATEST_ASSERT_STR_EQ(b.data, "abcde");
+	GREATEST_ASSERT_EQ(b.length, 5U);
+	GREATEST_ASSERT_EQ(b.capacity, 8U);
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(putc_empty);
+	GREATEST_MAIN_END();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-puts.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,53 @@
+/*
+ * test-puts.c -- test buf_puts
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+puts_empty(void)
+{
+	struct buf b = {0};
+
+	/* This will allocate an initial buffer of 10. */
+	buf_puts(&b, "abcdefghij");
+	GREATEST_ASSERT_STR_EQ(b.data, "abcdefghij");
+	GREATEST_ASSERT_EQ(b.length, 10U);
+	GREATEST_ASSERT_EQ(b.capacity, 10U);
+
+	/* This will multiply the capacity to 20 and increase length to 14. */
+	buf_puts(&b, "klmn");
+	GREATEST_ASSERT_STR_EQ(b.data, "abcdefghijklmn");
+	GREATEST_ASSERT_EQ(b.length, 14U);
+	GREATEST_ASSERT_EQ(b.capacity, 20U);
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(puts_empty);
+	GREATEST_MAIN_END();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-reserve.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,63 @@
+/*
+ * test-reserve.c -- test buf_reserve
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+reserve(void)
+{
+	struct buf b = {0};
+
+	/* Reserve allocates but does not change length. */
+	buf_reserve(&b, 100U);
+	GREATEST_ASSERT(b.data);
+	GREATEST_ASSERT_EQ(b.length, 0U);
+	GREATEST_ASSERT_EQ(b.capacity, 100U);
+
+	/*
+	 * Now we have a capacity of 100, if I request 100 again it should not
+	 * change capacity.
+	 */
+	buf_reserve(&b, 100U);
+	GREATEST_ASSERT(b.data);
+	GREATEST_ASSERT_EQ(b.length, 0U);
+	GREATEST_ASSERT_EQ(b.capacity, 100U);
+
+	/* But if I reserve more, it should. */
+	buf_reserve(&b, 200U);
+	GREATEST_ASSERT(b.data);
+	GREATEST_ASSERT_EQ(b.length, 0U);
+	GREATEST_ASSERT_EQ(b.capacity, 200U);
+
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(reserve);
+	GREATEST_MAIN_END();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-resize.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,76 @@
+/*
+ * test-resize.c -- test buf_resize
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+resize_bigger(void)
+{
+	struct buf b = {0};
+
+	/* Resize actually change the length with the specified character. */
+	buf_resize(&b, 10, 'a');
+	GREATEST_ASSERT_STR_EQ(b.data, "aaaaaaaaaa");
+	GREATEST_ASSERT_EQ(b.length, 10U);
+	GREATEST_ASSERT_EQ(b.capacity, 10U);
+
+	/* Add some more letters. */
+	buf_resize(&b, b.length + 5, 'b');
+	GREATEST_ASSERT_STR_EQ(b.data, "aaaaaaaaaabbbbb");
+	GREATEST_ASSERT_EQ(b.length, 15U);
+	GREATEST_ASSERT_EQ(b.capacity, 20U);
+
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_TEST
+resize_smaller(void)
+{
+	struct buf b = {0};
+
+	buf_resize(&b, 10, 'a');
+	GREATEST_ASSERT_STR_EQ(b.data, "aaaaaaaaaa");
+	GREATEST_ASSERT_EQ(b.length, 10U);
+	GREATEST_ASSERT_EQ(b.capacity, 10U);
+
+	/* Resize smaller, the letter 'b' should not even appear. */
+	buf_resize(&b, 5, 'b');
+	GREATEST_ASSERT_STR_EQ(b.data, "aaaaa");
+	GREATEST_ASSERT_EQ(b.length, 5U);
+	GREATEST_ASSERT_EQ(b.capacity, 10U);
+
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(resize_bigger);
+	GREATEST_RUN_TEST(resize_smaller);
+	GREATEST_MAIN_END();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-shrink.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,84 @@
+/*
+ * test-shrink.c -- test buf_shrink
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+shrink_set(void)
+{
+	struct buf b = {0};
+
+	/* Reserve 10 but write 5 character, shrink should set a capacity of 5. */
+	buf_reserve(&b, 10U);
+	buf_puts(&b, "abcde");
+	buf_shrink(&b);
+	GREATEST_ASSERT_STR_EQ(b.data, "abcde");
+	GREATEST_ASSERT_EQ(b.length, 5U);
+	GREATEST_ASSERT_EQ(b.capacity, 5U);
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_TEST
+shrink_empty(void)
+{
+	struct buf b = {0};
+
+	/*
+	 * Reserve 10, the data should be allocated but since we have a null
+	 * length it should be completely free'd.
+	 */
+	buf_reserve(&b, 10U);
+	buf_shrink(&b);
+	GREATEST_ASSERT(!b.data);
+	GREATEST_ASSERT_EQ(b.length, 0U);
+	GREATEST_ASSERT_EQ(b.capacity, 0U);
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_TEST
+shrink_null(void)
+{
+	struct buf b = {0};
+
+	buf_shrink(&b);
+	GREATEST_ASSERT(!b.data);
+	GREATEST_ASSERT_EQ(b.length, 0U);
+	GREATEST_ASSERT_EQ(b.capacity, 0U);
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(shrink_set);
+	GREATEST_RUN_TEST(shrink_empty);
+	GREATEST_RUN_TEST(shrink_null);
+	GREATEST_MAIN_END();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-sub.c	Thu Oct 29 17:24:30 2020 +0100
@@ -0,0 +1,58 @@
+/*
+ * test-sub.c -- test buf_sub
+ *
+ * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GREATEST_USE_ABBREVS 0
+#include <greatest.h>
+
+#include "buf.h"
+
+GREATEST_TEST
+sub(void)
+{
+	struct buf b = {0};
+	struct buf s1, s2;
+
+	buf_puts(&b, "0123456789");
+
+	/* Copy only the portion from 1 to 3. */
+	GREATEST_ASSERT(buf_sub(&s1, &b, 1, 3));
+	GREATEST_ASSERT_STR_EQ(s1.data, "123");
+	GREATEST_ASSERT_EQ(s1.length, 3U);
+	GREATEST_ASSERT_EQ(s1.capacity, 3U);
+	buf_finish(&s1);
+
+	/* Copy whole string starting from 4. */
+	GREATEST_ASSERT(buf_sub(&s2, &b, 4, -1));
+	GREATEST_ASSERT_STR_EQ(s2.data, "456789");
+	GREATEST_ASSERT_EQ(s2.length, 6U);
+	GREATEST_ASSERT_EQ(s2.capacity, 6U);
+	buf_finish(&s2);
+	buf_finish(&b);
+
+	GREATEST_PASS();
+}
+
+GREATEST_MAIN_DEFS();
+
+int
+main(int argc, char **argv)
+{
+	GREATEST_MAIN_BEGIN();
+	GREATEST_RUN_TEST(sub);
+	GREATEST_MAIN_END();
+}