1/* $OpenBSD: ringbuf.c,v 1.10 2016/10/16 22:12:50 bluhm Exp $ */
2
3/*
4 * Copyright (c) 2004 Damien Miller
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19/*
20 * Simple ringbuffer for lines of text.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "syslogd.h"
28
29#define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
30
31/* Initialise a ring buffer */
32struct ringbuf *
33ringbuf_init(size_t len)
34{
35	struct ringbuf *ret;
36
37	if (len == 0 || (ret = malloc(sizeof(*ret))) == NULL)
38		return (NULL);
39
40	if ((ret->buf = malloc(len)) == NULL) {
41		free(ret);
42		return (NULL);
43	}
44
45	ret->len = len;
46	ret->start = ret->end = 0;
47
48	return (ret);
49}
50
51/* Free a ring buffer */
52void
53ringbuf_free(struct ringbuf *rb)
54{
55	free(rb->buf);
56	free(rb);
57}
58
59/* Clear a ring buffer */
60void
61ringbuf_clear(struct ringbuf *rb)
62{
63	rb->start = rb->end = 0;
64}
65
66/* Return the number of bytes used in a ringbuffer */
67size_t
68ringbuf_used(struct ringbuf *rb)
69{
70	return ((rb->len + rb->end - rb->start) % rb->len);
71}
72
73/*
74 * Append a line to a ring buffer, will delete lines from start
75 * of buffer as necessary
76 */
77int
78ringbuf_append_line(struct ringbuf *rb, char *line)
79{
80	size_t llen, used, copy_len;
81	int overflow = 0;
82
83	if (rb == NULL || line == NULL)
84		return (-1);
85
86	llen = strlen(line);
87	if (llen == 0)
88		return (-1);
89
90	if (line[llen - 1] != '\n')
91		llen++; /* one extra for appended '\n' */
92
93	if (llen >= rb->len)
94		return (-1);
95
96	/*
97	 * If necessary, advance start pointer to make room for appended
98	 * string. Ensure that start pointer is at the beginning of a line
99	 * once we are done (i.e move to after '\n').
100	 */
101	used = ringbuf_used(rb);
102	if (used + llen >= rb->len) {
103		rb->start = (rb->start + used + llen - rb->len) % rb->len;
104
105		/* Find next '\n' */
106		while (rb->buf[rb->start] != '\n')
107			rb->start = (rb->start + 1) % rb->len;
108		/* Skip it */
109		rb->start = (rb->start + 1) % rb->len;
110
111		overflow = 1;
112	}
113
114	/*
115	 * Now append string, starting from last pointer and wrapping if
116	 * necessary
117	 */
118	if (rb->end + llen > rb->len) {
119		copy_len = rb->len - rb->end;
120		memcpy(rb->buf + rb->end, line, copy_len);
121		memcpy(rb->buf, line + copy_len, llen - copy_len - 1);
122		rb->buf[llen - copy_len - 1] = '\n';
123	} else {
124		memcpy(rb->buf + rb->end, line, llen - 1);
125		rb->buf[rb->end + llen - 1] = '\n';
126	}
127
128	rb->end = (rb->end + llen) % rb->len;
129
130	return (overflow);
131}
132
133/*
134 * Copy and nul-terminate a ringbuffer to a string.
135 */
136ssize_t
137ringbuf_to_string(char *buf, size_t len, struct ringbuf *rb)
138{
139	size_t copy_len, n;
140
141	if (buf == NULL || rb == NULL || len == 0)
142		return (-1);
143
144	copy_len = MINIMUM(len - 1, ringbuf_used(rb));
145
146	if (copy_len == 0)
147		return (copy_len);
148
149	if (rb->start < rb->end)
150		memcpy(buf, rb->buf + rb->start, copy_len);
151	else {
152		/* If the buffer is wrapped, copy each hunk separately */
153		n = rb->len - rb->start;
154		memcpy(buf, rb->buf + rb->start, MINIMUM(n, copy_len));
155		if (copy_len > n)
156			memcpy(buf + n, rb->buf,
157			    MINIMUM(rb->end, copy_len - n));
158	}
159	buf[copy_len] = '\0';
160
161	return (ringbuf_used(rb));
162}
163