INN commit: trunk (9 files)
INN Commit
rra at isc.org
Thu Aug 28 18:59:18 UTC 2014
Date: Thursday, August 28, 2014 @ 11:59:18
Author: iulius
Revision: 9658
Sync lib/buffer.c and include/inn/buffer.h with latest rra-c-util version
Changes are:
- Remove the bool arguments to buffer_sprintf and buffer_vsprintf and
instead introduce new buffer_append_sprintf and _vsprintf functions
to append to the buffer, which is what the functions did with a true
argument. This avoids having a bool argument to functions, the meaning
of which is often obscure in the calling code.
- Fix buffer_free to support taking NULL pointers and doing nothing
with them. Don't check whether a pointer is NULL before passing it
into free and instead assume free can handle NULL pointers properly.
This has been true for many years.
- Allocate memory with calloc and assume this sets pointers to NULL
instead of explicitly initializing them. We already had to assume this
in various places, and architectures where the all-zero bit pattern is
not the NULL pointer are exceedingly rare.
Modified:
trunk/backends/archive.c
trunk/include/inn/buffer.h
trunk/innd/cc.c
trunk/innd/site.c
trunk/lib/buffer.c
trunk/lib/nntp.c
trunk/nnrpd/auth-ext.c
trunk/storage/overview.c
trunk/tests/lib/buffer-t.c
----------------------+
backends/archive.c | 2
include/inn/buffer.h | 111 ++++++++++++++++++--------
innd/cc.c | 72 ++++++++---------
innd/site.c | 48 +++++------
lib/buffer.c | 205 +++++++++++++++++++++++++++++--------------------
lib/nntp.c | 16 +--
nnrpd/auth-ext.c | 16 +--
storage/overview.c | 2
tests/lib/buffer-t.c | 44 +++++++---
9 files changed, 310 insertions(+), 206 deletions(-)
Modified: backends/archive.c
===================================================================
--- backends/archive.c 2014-08-25 17:13:12 UTC (rev 9657)
+++ backends/archive.c 2014-08-28 18:59:18 UTC (rev 9658)
@@ -254,7 +254,7 @@
tm = localtime(&now);
year = tm->tm_year + 1900;
month = tm->tm_mon + 1;
- buffer_sprintf(path, true, "/%04d%02d", year, month);
+ buffer_append_sprintf(path, "/%04d%02d", year, month);
} else {
buffer_append(path, "/", 1);
buffer_append(path, number, strlen(number));
Modified: include/inn/buffer.h
===================================================================
--- include/inn/buffer.h 2014-08-25 17:13:12 UTC (rev 9657)
+++ include/inn/buffer.h 2014-08-28 18:59:18 UTC (rev 9658)
@@ -1,18 +1,44 @@
-/* $Id$
-**
-** Counted, reusable memory buffer.
-**
-** A buffer is an allocated bit of memory with a known size and a separate
-** data length. It's intended to store strings and can be reused repeatedly
-** to minimize the number of memory allocations. Buffers increase in
-** increments of 1K.
-**
-** A buffer contains a notion of the data that's been used and the data
-** that's been left, used when the buffer is an I/O buffer where lots of data
-** is buffered and then slowly processed out of the buffer. The total length
-** of the data is used + left. If a buffer is just used to store some data,
-** used can be set to 0 and left stores the length of the data.
-*/
+/* $Id$
+ *
+ * Counted, reusable memory buffer.
+ *
+ * A buffer is an allocated bit of memory with a known size and a separate
+ * data length. It's intended to store strings and can be reused repeatedly
+ * to minimize the number of memory allocations. Buffers increase in
+ * increments of 1K, or double for some operations.
+ *
+ * A buffer contains a notion of the data that's been used and the data
+ * that's been left, used when the buffer is an I/O buffer where lots of data
+ * is buffered and then slowly processed out of the buffer. The total length
+ * of the data is used + left. If a buffer is just used to store some data,
+ * used can be set to 0 and left stores the length of the data.
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <eagle at eyrie.org>
+ * Copyright 2011, 2012
+ * The Board of Trustees of the Leland Stanford Junior University
+ * Copyright (c) 2004, 2005, 2006
+ * by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
+ * 2002, 2003 by The Internet Software Consortium and Rich Salz
+ *
+ * This code is derived from software contributed to the Internet Software
+ * Consortium by Rich Salz.
+ *
+ * Permission to use, copy, modify, and 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 ISC DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL ISC 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 INN_BUFFER_H
#define INN_BUFFER_H 1
@@ -36,47 +62,64 @@
/* Free an allocated buffer. */
void buffer_free(struct buffer *);
-/* Resize a buffer to be at least as large as the provided size. Invalidates
- pointers into the buffer. */
+/*
+ * Resize a buffer to be at least as large as the provided size. Invalidates
+ * pointers into the buffer.
+ */
void buffer_resize(struct buffer *, size_t);
-/* Compact a buffer, removing all used data and moving unused data to the
- beginning of the buffer. Invalidates pointers into the buffer. */
+/*
+ * Compact a buffer, removing all used data and moving unused data to the
+ * beginning of the buffer. Invalidates pointers into the buffer.
+ */
void buffer_compact(struct buffer *);
/* Set the buffer contents, ignoring anything currently there. */
void buffer_set(struct buffer *, const char *data, size_t length);
+/*
+ * Set the buffer contents via a sprintf-style format string. No trailing
+ * nul is added.
+ */
+void buffer_sprintf(struct buffer *, const char *, ...)
+ __attribute__((__format__(printf, 2, 3)));
+void buffer_vsprintf(struct buffer *, const char *, va_list);
+
/* Append data to the buffer. */
void buffer_append(struct buffer *, const char *data, size_t length);
-/* Print data into the buffer, either appending or replacing the existing
- data. No trailing nul is added. */
-void buffer_sprintf(struct buffer *, bool append, const char *, ...)
- __attribute__((__format__(printf, 3, 4)));
-void buffer_vsprintf(struct buffer *, bool append, const char *, va_list);
+/* Append via an sprintf-style format string. No trailing nul is added. */
+void buffer_append_sprintf(struct buffer *, const char *, ...)
+ __attribute__((__format__(printf, 2, 3)));
+void buffer_append_vsprintf(struct buffer *, const char *, va_list);
/* Swap the contents of two buffers. */
void buffer_swap(struct buffer *, struct buffer *);
-/* Find the given string in the unconsumed data in a buffer. start is an
- offset into the unused data specifying where to start the search (to save
- time with multiple searches). Pass 0 to start the search at the beginning
- of the unused data. Returns true if the terminator is found, putting the
- offset (into the unused data space) of the beginning of the terminator into
- the fourth argument. Returns false if the terminator isn't found. */
+/*
+ * Find the given string in the unconsumed data in a buffer. start is an
+ * offset into the unused data specifying where to start the search (to save
+ * time with multiple searches). Pass 0 to start the search at the beginning
+ * of the unused data. Returns true if the terminator is found, putting the
+ * offset (into the unused data space) of the beginning of the terminator into
+ * the fourth argument. Returns false if the terminator isn't found.
+ */
bool buffer_find_string(struct buffer *, const char *, size_t start,
size_t *offset);
-/* Read from a file descriptor into a buffer, up to the available space in the
- buffer. Return the number of characters read. */
+/*
+ * Read from a file descriptor into a buffer, up to the available space in the
+ * buffer. Return the number of characters read.
+ */
ssize_t buffer_read(struct buffer *, int fd);
/* Read from a file descriptor into a buffer until end of file is reached. */
bool buffer_read_all(struct buffer *, int fd);
-/* Read the contents of a file into a buffer. This should be used instead of
- buffer_read_all when fstat can be called on the file descriptor. */
+/*
+ * Read the contents of a file into a buffer. This should be used instead of
+ * buffer_read_all when fstat can be called on the file descriptor.
+ */
bool buffer_read_file(struct buffer *, int fd);
END_DECLS
Modified: innd/cc.c
===================================================================
--- innd/cc.c 2014-08-25 17:13:12 UTC (rev 9657)
+++ innd/cc.c 2014-08-28 18:59:18 UTC (rev 9658)
@@ -498,7 +498,7 @@
if (errors == 0)
return NULL;
- buffer_sprintf(&CCreply, false, "1 Found %d error(s) -- see syslog", errors);
+ buffer_sprintf(&CCreply, "1 Found %d error(s) -- see syslog", errors);
return CCreply.data;
}
@@ -829,32 +829,32 @@
char *stats;
#endif
- buffer_sprintf(&CCreply, false, "0 Server ");
+ buffer_sprintf(&CCreply, "0 Server ");
/* Server's mode. */
switch (Mode) {
default:
- buffer_sprintf(&CCreply, true, "Unknown %d\n", Mode);
+ buffer_append_sprintf(&CCreply, "Unknown %d\n", Mode);
break;
case OMrunning:
- buffer_sprintf(&CCreply, true, "running\n");
+ buffer_append_sprintf(&CCreply, "running\n");
break;
case OMpaused:
- buffer_sprintf(&CCreply, true, "paused %s\n", ModeReason);
+ buffer_append_sprintf(&CCreply, "paused %s\n", ModeReason);
break;
case OMthrottled:
- buffer_sprintf(&CCreply, true, "throttled %s\n", ModeReason);
+ buffer_append_sprintf(&CCreply, "throttled %s\n", ModeReason);
break;
}
if (RejectReason)
- buffer_sprintf(&CCreply, true, "Rejecting %s\n", RejectReason);
+ buffer_append_sprintf(&CCreply, "Rejecting %s\n", RejectReason);
else
- buffer_sprintf(&CCreply, true, "Allowing remote connections\n");
+ buffer_append_sprintf(&CCreply, "Allowing remote connections\n");
/* Server parameters. */
for (count = 0, index = 0; CHANiter(&index, CTnntp) != NULL; )
count++;
- buffer_sprintf(&CCreply, true, "Parameters c %lu i %lu (%d) l %lu o %d"
+ buffer_append_sprintf(&CCreply, "Parameters c %lu i %lu (%d) l %lu o %d"
" t %ld H %d T %d X %ld %s %s\n",
innconf->artcutoff, innconf->maxconnections, count,
innconf->maxartsize, MaxOutgoing, (long) TimeOut.tv_sec,
@@ -864,40 +864,40 @@
/* Reservation. */
if (Reservation)
- buffer_sprintf(&CCreply, true, "Reserved %s\n", Reservation);
+ buffer_append_sprintf(&CCreply, "Reserved %s\n", Reservation);
else
- buffer_sprintf(&CCreply, true, "Not reserved\n");
+ buffer_append_sprintf(&CCreply, "Not reserved\n");
/* Newsreaders. */
- buffer_sprintf(&CCreply, true, "Readers ");
+ buffer_append_sprintf(&CCreply, "Readers ");
if (innconf->readerswhenstopped)
- buffer_sprintf(&CCreply, true, "independent ");
+ buffer_append_sprintf(&CCreply, "independent ");
else
- buffer_sprintf(&CCreply, true, "follow ");
+ buffer_append_sprintf(&CCreply, "follow ");
if (NNRPReason == NULL)
- buffer_sprintf(&CCreply, true, "enabled");
+ buffer_append_sprintf(&CCreply, "enabled");
else
- buffer_sprintf(&CCreply, true, "disabled %s", NNRPReason);
+ buffer_append_sprintf(&CCreply, "disabled %s", NNRPReason);
#ifdef DO_PERL
- buffer_sprintf(&CCreply, true, "\nPerl filtering ");
+ buffer_append_sprintf(&CCreply, "\nPerl filtering ");
if (PerlFilterActive)
- buffer_sprintf(&CCreply, true, "enabled");
+ buffer_append_sprintf(&CCreply, "enabled");
else
- buffer_sprintf(&CCreply, true, "disabled");
+ buffer_append_sprintf(&CCreply, "disabled");
stats = PLstats();
if (stats != NULL) {
- buffer_sprintf(&CCreply, true, "\nPerl filter stats: %s", stats);
+ buffer_append_sprintf(&CCreply, "\nPerl filter stats: %s", stats);
free(stats);
}
#endif
#ifdef DO_PYTHON
- buffer_sprintf(&CCreply, true, "\nPython filtering ");
+ buffer_append_sprintf(&CCreply, "\nPython filtering ");
if (PythonFilterActive)
- buffer_sprintf(&CCreply, true, "enabled");
+ buffer_append_sprintf(&CCreply, "enabled");
else
- buffer_sprintf(&CCreply, true, "disabled");
+ buffer_append_sprintf(&CCreply, "disabled");
#endif
buffer_append(&CCreply, "", 1);
@@ -930,8 +930,8 @@
if (av[0][0] != '\0') {
cp = CHANfromdescriptor(atoi(av[0]));
if (cp == NULL)
- return xstrdup(CCnochannel);
- buffer_sprintf(&CCreply, false, "0 %s", CHANname(cp));
+ return xstrdup(CCnochannel);
+ buffer_sprintf(&CCreply, "0 %s", CHANname(cp));
return CCreply.data;
}
buffer_set(&CCreply, "0 ", 2);
@@ -940,37 +940,37 @@
continue;
if (++count > 1)
buffer_append(&CCreply, "\n", 1);
- buffer_sprintf(&CCreply, true, "%s", CHANname(cp));
+ buffer_append_sprintf(&CCreply, "%s", CHANname(cp));
switch (cp->Type) {
case CTremconn:
- buffer_sprintf(&CCreply, true, ":remconn::");
+ buffer_append_sprintf(&CCreply, ":remconn::");
break;
case CTreject:
- buffer_sprintf(&CCreply, true, ":reject::");
+ buffer_append_sprintf(&CCreply, ":reject::");
break;
case CTnntp:
mode = (cp->MaxCnx > 0 && cp->ActiveCnx == 0) ? "paused" : "";
- buffer_sprintf(&CCreply, true, ":%s:%ld:%s",
+ buffer_append_sprintf(&CCreply, ":%s:%ld:%s",
cp->State == CScancel ? "cancel" : "nntp",
(long) (Now.tv_sec - cp->LastActive), mode);
break;
case CTlocalconn:
- buffer_sprintf(&CCreply, true, ":localconn::");
+ buffer_append_sprintf(&CCreply, ":localconn::");
break;
case CTcontrol:
- buffer_sprintf(&CCreply, true, ":control::");
+ buffer_append_sprintf(&CCreply, ":control::");
break;
case CTfile:
- buffer_sprintf(&CCreply, true, "::");
+ buffer_append_sprintf(&CCreply, "::");
break;
case CTexploder:
- buffer_sprintf(&CCreply, true, ":exploder::");
+ buffer_append_sprintf(&CCreply, ":exploder::");
break;
case CTprocess:
- buffer_sprintf(&CCreply, true, ":");
+ buffer_append_sprintf(&CCreply, ":");
break;
default:
- buffer_sprintf(&CCreply, true, ":unknown::");
+ buffer_append_sprintf(&CCreply, ":unknown::");
break;
}
}
@@ -1602,7 +1602,7 @@
oerrno = errno;
syslog(L_ERROR, "%s cant kill %ld %d site %s, %m", LogName,
(long) sp->pid, s, p);
- buffer_sprintf(&CCreply, false, "1 Can't signal process %ld: %s",
+ buffer_sprintf(&CCreply, "1 Can't signal process %ld: %s",
(long) sp->pid, strerror(oerrno));
return CCreply.data;
}
Modified: innd/site.c
===================================================================
--- innd/site.c 2014-08-25 17:13:12 UTC (rev 9657)
+++ innd/site.c 2014-08-28 18:59:18 UTC (rev 9658)
@@ -1145,87 +1145,87 @@
return;
}
- buffer_sprintf(bp, true, "%s%s:\t", sp->Name, sp->IsMaster ? "(*)" : "");
+ buffer_append_sprintf(bp, "%s%s:\t", sp->Name, sp->IsMaster ? "(*)" : "");
if (sp->Type == FTfunnel) {
sp = &Sites[sp->Funnel];
- buffer_sprintf(bp, true, "funnel -> %s: ", sp->Name);
+ buffer_append_sprintf(bp, "funnel -> %s: ", sp->Name);
}
switch (sp->Type) {
default:
- buffer_sprintf(bp, true, "unknown feed type %d", sp->Type);
+ buffer_append_sprintf(bp, "unknown feed type %d", sp->Type);
break;
case FTerror:
case FTfile:
- buffer_sprintf(bp, true, "file");
+ buffer_append_sprintf(bp, "file");
if (sp->Buffered)
- buffer_sprintf(bp, true, " buffered(%lu)",
+ buffer_append_sprintf(bp, " buffered(%lu)",
(unsigned long) sp->Buffer.left);
else if ((cp = sp->Channel) == NULL)
- buffer_sprintf(bp, true, " no channel?");
+ buffer_append_sprintf(bp, " no channel?");
else
- buffer_sprintf(bp, true, " open fd=%d, in mem %lu", cp->fd,
+ buffer_append_sprintf(bp, " open fd=%d, in mem %lu", cp->fd,
(unsigned long) cp->Out.left);
break;
case FTchannel:
- buffer_sprintf(bp, true, "channel");
+ buffer_append_sprintf(bp, "channel");
goto common;
case FTexploder:
- buffer_sprintf(bp, true, "exploder");
+ buffer_append_sprintf(bp, "exploder");
common:
if (sp->Process >= 0)
- buffer_sprintf(bp, true, " pid=%ld", (long) sp->pid);
+ buffer_append_sprintf(bp, " pid=%ld", (long) sp->pid);
if (sp->Spooling)
- buffer_sprintf(bp, true, " spooling");
+ buffer_append_sprintf(bp, " spooling");
cp = sp->Channel;
if (cp == NULL)
- buffer_sprintf(bp, true, " no channel?");
+ buffer_append_sprintf(bp, " no channel?");
else
- buffer_sprintf(bp, true, " fd=%d, in mem %lu", cp->fd,
+ buffer_append_sprintf(bp, " fd=%d, in mem %lu", cp->fd,
(unsigned long) cp->Out.left);
break;
case FTfunnel:
- buffer_sprintf(bp, true, "recursive funnel");
+ buffer_append_sprintf(bp, "recursive funnel");
break;
case FTlogonly:
- buffer_sprintf(bp, true, "log only");
+ buffer_append_sprintf(bp, "log only");
break;
case FTprogram:
- buffer_sprintf(bp, true, "program");
+ buffer_append_sprintf(bp, "program");
if (sp->FNLwantsnames)
- buffer_sprintf(bp, true, " with names");
+ buffer_append_sprintf(bp, " with names");
break;
}
buffer_append(bp, "\n", 1);
if (Verbose) {
sep = "\t";
if (sp->Buffered && sp->Flushpoint) {
- buffer_sprintf(bp, true, "%sFlush @ %lu", sep,
+ buffer_append_sprintf(bp, "%sFlush @ %lu", sep,
(unsigned long) sp->Flushpoint);
sep = "; ";
}
if (sp->StartWriting || sp->StopWriting) {
- buffer_sprintf(bp, true, "%sWrite [%ld..%ld]", sep,
+ buffer_append_sprintf(bp, "%sWrite [%ld..%ld]", sep,
sp->StopWriting, sp->StartWriting);
sep = "; ";
}
if (sp->StartSpooling) {
- buffer_sprintf(bp, true, "%sSpool @ %ld", sep, sp->StartSpooling);
+ buffer_append_sprintf(bp, "%sSpool @ %ld", sep, sp->StartSpooling);
sep = "; ";
}
if (sep[0] != '\t')
buffer_append(bp, "\n", 1);
if (sp->Spooling && sp->SpoolName)
- buffer_sprintf(bp, true, "\tSpooling to \"%s\"\n", sp->SpoolName);
+ buffer_append_sprintf(bp, "\tSpooling to \"%s\"\n", sp->SpoolName);
cp = sp->Channel;
if (cp != NULL) {
- buffer_sprintf(bp, true, "\tChannel created %.12s",
+ buffer_append_sprintf(bp, "\tChannel created %.12s",
ctime(&cp->Started) + 4);
- buffer_sprintf(bp, true, ", last active %.12s\n",
+ buffer_append_sprintf(bp, ", last active %.12s\n",
ctime(&cp->LastActive) + 4);
if (cp->Waketime > Now.tv_sec)
- buffer_sprintf(bp, true, "\tSleeping until %.12s\n",
+ buffer_append_sprintf(bp, "\tSleeping until %.12s\n",
ctime(&cp->Waketime) + 4);
}
Modified: lib/buffer.c
===================================================================
--- lib/buffer.c 2014-08-25 17:13:12 UTC (rev 9657)
+++ lib/buffer.c 2014-08-28 18:59:18 UTC (rev 9658)
@@ -1,61 +1,83 @@
-/* $Id$
-**
-** Counted, reusable memory buffer.
-**
-** A buffer is an allocated bit of memory with a known size and a separate
-** data length. It's intended to store strings and can be reused repeatedly
-** to minimize the number of memory allocations. Buffers increase in
-** increments of 1K.
-**
-** A buffer contains a notion of the data that's been used and the data
-** that's been left, used when the buffer is an I/O buffer where lots of data
-** is buffered and then slowly processed out of the buffer. The total length
-** of the data is used + left. If a buffer is just used to store some data,
-** used can be set to 0 and left stores the length of the data.
-*/
+/* $Id$
+ *
+ * Counted, reusable memory buffer.
+ *
+ * A buffer is an allocated bit of memory with a known size and a separate
+ * data length. It's intended to store strings and can be reused repeatedly
+ * to minimize the number of memory allocations. Buffers increase in
+ * increments of 1K, or double for some operations.
+ *
+ * A buffer contains data that's been used and the data that's been left, used
+ * when the buffer is an I/O buffer where lots of data is buffered and then
+ * slowly processed out of the buffer. The total length of the data is used +
+ * left. If a buffer is just used to store some data, used can be set to 0
+ * and left stores the length of the data.
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <eagle at eyrie.org>
+ * Copyright 2011, 2012, 2014
+ * The Board of Trustees of the Leland Stanford Junior University
+ * Copyright (c) 2004, 2005, 2006
+ * by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
+ * 2002, 2003 by The Internet Software Consortium and Rich Salz
+ *
+ * This code is derived from software contributed to the Internet Software
+ * Consortium by Rich Salz.
+ *
+ * Permission to use, copy, modify, and 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 ISC DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL ISC 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 "config.h"
#include "clibrary.h"
+
#include <errno.h>
#include <sys/stat.h>
#include "inn/buffer.h"
#include "inn/libinn.h"
+
/*
-** Allocate a new struct buffer and initialize it.
-*/
+ * Allocate a new struct buffer and initialize it.
+ */
struct buffer *
buffer_new(void)
{
- struct buffer *buffer;
-
- buffer = xmalloc(sizeof(struct buffer));
- buffer->size = 0;
- buffer->used = 0;
- buffer->left = 0;
- buffer->data = NULL;
- return buffer;
+ return xcalloc(1, sizeof(struct buffer));
}
/*
-** Free a buffer.
-*/
+ * Free a buffer.
+ */
void
buffer_free(struct buffer *buffer)
{
- if (buffer->data != NULL)
- free(buffer->data);
+ if (buffer == NULL)
+ return;
+ free(buffer->data);
free(buffer);
}
/*
-** Resize a buffer to be at least as large as the provided second argument.
-** Resize buffers to multiples of 1KB to keep the number of reallocations to
-** a minimum. Refuse to resize a buffer to make it smaller.
-*/
+ * Resize a buffer to be at least as large as the provided second argument.
+ * Resize buffers to multiples of 1KB to keep the number of reallocations to a
+ * minimum. Refuse to resize a buffer to make it smaller.
+ */
void
buffer_resize(struct buffer *buffer, size_t size)
{
@@ -67,9 +89,9 @@
/*
-** Compact a buffer by moving the data between buffer->used and buffer->left
-** to the beginning of the buffer, overwriting the already-consumed data.
-*/
+ * Compact a buffer by moving the data between buffer->used and buffer->left
+ * to the beginning of the buffer, overwriting the already-consumed data.
+ */
void
buffer_compact(struct buffer *buffer)
{
@@ -82,9 +104,9 @@
/*
-** Replace whatever data is currently in the buffer with the provided data.
-** Resize the buffer if needed.
-*/
+ * Replace whatever data is currently in the buffer with the provided data.
+ * Resize the buffer if needed.
+ */
void
buffer_set(struct buffer *buffer, const char *data, size_t length)
{
@@ -98,9 +120,9 @@
/*
-** Append data to a buffer. The new data shows up as additional unused data
-** at the end of the buffer. Resize the buffer if needed.
-*/
+ * Append data to a buffer. The new data shows up as additional unused data
+ * at the end of the buffer. Resize the buffer if needed.
+ */
void
buffer_append(struct buffer *buffer, const char *data, size_t length)
{
@@ -116,21 +138,17 @@
/*
-** Print data into a buffer from the supplied va_list, either appending to
-** the end of it or replacing the existing contents. The new data shows up
-** as unused data at the end of the buffer. The trailing nul is not added to
-** the buffer.
-*/
+ * Print data into a buffer from the supplied va_list, appending to the end.
+ * The new data shows up as unused data at the end of the buffer. The
+ * trailing nul is not added to the buffer.
+ */
void
-buffer_vsprintf(struct buffer *buffer, bool append, const char *format,
- va_list args)
+buffer_append_vsprintf(struct buffer *buffer, const char *format, va_list args)
{
size_t total, avail;
ssize_t status;
va_list args_copy;
- if (!append)
- buffer_set(buffer, NULL, 0);
total = buffer->used + buffer->left;
avail = buffer->size - total;
va_copy(args_copy, args);
@@ -152,26 +170,54 @@
/*
-** Print data into a buffer, either appending to the end of it or replacing
-** the existing contents. The new data shows up as unused data at the end of
-** the buffer. Resize the buffer if needed. The trailing nul is not added
-** to the buffer.
-*/
+ * Print data into a buffer, appending to the end. The new data shows up as
+ * unused data at the end of the buffer. Resize the buffer if needed. The
+ * trailing nul is not added to the buffer.
+ */
void
-buffer_sprintf(struct buffer *buffer, bool append, const char *format, ...)
+buffer_append_sprintf(struct buffer *buffer, const char *format, ...)
{
va_list args;
va_start(args, format);
- buffer_vsprintf(buffer, append, format, args);
+ buffer_append_vsprintf(buffer, format, args);
va_end(args);
}
/*
-** Swap the contents of two buffers.
-*/
+ * Replace the current buffer contents with data printed from the supplied
+ * va_list. The new data shows up as unused data at the end of the buffer.
+ * The trailing nul is not added to the buffer.
+ */
void
+buffer_vsprintf(struct buffer *buffer, const char *format, va_list args)
+{
+ buffer_set(buffer, NULL, 0);
+ buffer_append_vsprintf(buffer, format, args);
+}
+
+
+/*
+ * Replace the current buffer contents with data printed from the supplied
+ * format string and arguments. The new data shows up as unused data at the
+ * end of the buffer. The trailing nul is not added to the buffer.
+ */
+void
+buffer_sprintf(struct buffer *buffer, const char *format, ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ buffer_vsprintf(buffer, format, args);
+ va_end(args);
+}
+
+
+/*
+ * Swap the contents of two buffers.
+ */
+void
buffer_swap(struct buffer *one, struct buffer *two)
{
struct buffer tmp;
@@ -183,12 +229,12 @@
/*
-** Find a given string in the unconsumed data in buffer. We know that all
-** the data prior to start (an offset into the space between buffer->used and
-** buffer->left) has already been searched. Returns the offset of the string
-** (with the same meaning as start) in offset if found, and returns true if
-** the terminator is found and false otherwise.
-*/
+ * Find a given string in the unconsumed data in buffer. We know that all the
+ * data prior to start (an offset into the space between buffer->used and
+ * buffer->left) has already been searched. Returns the offset of the string
+ * (with the same meaning as start) in offset if found, and returns true if
+ * the terminator is found and false otherwise.
+ */
bool
buffer_find_string(struct buffer *buffer, const char *string, size_t start,
size_t *offset)
@@ -213,9 +259,9 @@
/*
-** Read from a file descriptor into a buffer, up to the available space in
-** the buffer, and return the number of characters read.
-*/
+ * Read from a file descriptor into a buffer, up to the available space in the
+ * buffer, and return the number of characters read.
+ */
ssize_t
buffer_read(struct buffer *buffer, int fd)
{
@@ -232,10 +278,10 @@
/*
-** Read from a file descriptor until end of file is reached, doubling the
-** buffer size as necessary to hold all of the data. Returns true on
-** success, false on failure (in which case errno will be set).
-*/
+ * Read from a file descriptor until end of file is reached, doubling the
+ * buffer size as necessary to hold all of the data. Returns true on success,
+ * false on failure (in which case errno will be set).
+ */
bool
buffer_read_all(struct buffer *buffer, int fd)
{
@@ -254,13 +300,12 @@
/*
-** Read the entire contents of a file into a buffer. This is a slight
-** optimization over buffer_read_all because it can stat the file descriptor
-** first and size the buffer appropriately. buffer_read_all will still
-** handle the case where the file size changes while it's being read.
-** Returns true on success, false on failure (in which case errno will be
-** set).
-*/
+ * Read the entire contents of a file into a buffer. This is a slight
+ * optimization over buffer_read_all because it can stat the file descriptor
+ * first and size the buffer appropriately. buffer_read_all will still handle
+ * the case where the file size changes while it's being read. Returns true
+ * on success, false on failure (in which case errno will be set).
+ */
bool
buffer_read_file(struct buffer *buffer, int fd)
{
Modified: lib/nntp.c
===================================================================
--- lib/nntp.c 2014-08-25 17:13:12 UTC (rev 9657)
+++ lib/nntp.c 2014-08-28 18:59:18 UTC (rev 9658)
@@ -329,7 +329,7 @@
va_list args;
va_start(args, format);
- buffer_vsprintf(&nntp->out, true, format, args);
+ buffer_append_vsprintf(&nntp->out, format, args);
va_end(args);
buffer_append(&nntp->out, "\r\n", 2);
return nntp_flush(nntp);
@@ -346,7 +346,7 @@
va_list args;
va_start(args, format);
- buffer_vsprintf(&nntp->out, true, format, args);
+ buffer_append_vsprintf(&nntp->out, format, args);
va_end(args);
buffer_append(&nntp->out, "\r\n", 2);
return nntp_flush(nntp);
@@ -366,11 +366,11 @@
va_list args;
if (format == NULL)
- buffer_sprintf(&nntp->out, true, "%d\r\n", code);
+ buffer_append_sprintf(&nntp->out, "%d\r\n", code);
else {
- buffer_sprintf(&nntp->out, true, "%d ", code);
+ buffer_append_sprintf(&nntp->out, "%d ", code);
va_start(args, format);
- buffer_vsprintf(&nntp->out, true, format, args);
+ buffer_append_vsprintf(&nntp->out, format, args);
va_end(args);
buffer_append(&nntp->out, "\r\n", 2);
}
@@ -389,11 +389,11 @@
va_list args;
if (format == NULL)
- buffer_sprintf(&nntp->out, true, "%d\r\n", code);
+ buffer_append_sprintf(&nntp->out, "%d\r\n", code);
else {
- buffer_sprintf(&nntp->out, true, "%d ", code);
+ buffer_append_sprintf(&nntp->out, "%d ", code);
va_start(args, format);
- buffer_vsprintf(&nntp->out, true, format, args);
+ buffer_append_vsprintf(&nntp->out, format, args);
va_end(args);
buffer_append(&nntp->out, "\r\n", 2);
}
Modified: nnrpd/auth-ext.c
===================================================================
--- nnrpd/auth-ext.c 2014-08-25 17:13:12 UTC (rev 9657)
+++ nnrpd/auth-ext.c 2014-08-28 18:59:18 UTC (rev 9658)
@@ -284,15 +284,15 @@
append_client_info(struct client *client, struct buffer *data)
{
if (*client->host)
- buffer_sprintf(data, true, "ClientHost: %s\r\n", client->host);
+ buffer_append_sprintf(data, "ClientHost: %s\r\n", client->host);
if (*client->ip)
- buffer_sprintf(data, true, "ClientIP: %s\r\n", client->ip);
+ buffer_append_sprintf(data, "ClientIP: %s\r\n", client->ip);
if (client->port != 0)
- buffer_sprintf(data, true, "ClientPort: %hu\r\n", client->port);
+ buffer_append_sprintf(data, "ClientPort: %hu\r\n", client->port);
if (*client->serverip)
- buffer_sprintf(data, true, "LocalIP: %s\r\n", client->serverip);
+ buffer_append_sprintf(data, "LocalIP: %s\r\n", client->serverip);
if (client->serverport != 0)
- buffer_sprintf(data, true, "LocalPort: %hu\r\n", client->serverport);
+ buffer_append_sprintf(data, "LocalPort: %hu\r\n", client->serverport);
}
@@ -321,10 +321,10 @@
input = buffer_new();
append_client_info(client, input);
if (username != NULL)
- buffer_sprintf(input, true, "ClientAuthname: %s\r\n", username);
+ buffer_append_sprintf(input, "ClientAuthname: %s\r\n", username);
if (password != NULL)
- buffer_sprintf(input, true, "ClientPassword: %s\r\n", password);
- buffer_sprintf(input, true, ".\r\n");
+ buffer_append_sprintf(input, "ClientPassword: %s\r\n", password);
+ buffer_append_sprintf(input, ".\r\n");
xwrite(process->write_fd, input->data, input->left);
close(process->write_fd);
buffer_free(input);
Modified: storage/overview.c
===================================================================
--- storage/overview.c 2014-08-25 17:13:12 UTC (rev 9657)
+++ storage/overview.c 2014-08-28 18:59:18 UTC (rev 9658)
@@ -166,7 +166,7 @@
overview->overdata = buffer_new();
buffer_resize(overview->overdata, data->overlen + 13);
}
- buffer_sprintf(overview->overdata, false, "%ld\t", data->number);
+ buffer_sprintf(overview->overdata, "%ld\t", data->number);
buffer_append(overview->overdata, data->overview, data->overlen);
buffer_append(overview->overdata, "\r\n", 2);
Modified: tests/lib/buffer-t.c
===================================================================
--- tests/lib/buffer-t.c 2014-08-25 17:13:12 UTC (rev 9657)
+++ tests/lib/buffer-t.c 2014-08-28 18:59:18 UTC (rev 9658)
@@ -32,14 +32,30 @@
static const char test_string2[] = " of the buffer system";
static const char test_string3[] = "This is a test\0 of the buffer system";
+/*
+ * Test buffer_vsprintf. Wrapper needed to generate the va_list.
+ */
+static void
+test_vsprintf(struct buffer *buffer, const char *format, ...)
+{
+ va_list args;
+ va_start(args, format);
+ buffer_vsprintf(buffer, format, args);
+ va_end(args);
+}
+
+
+/*
+ * Likewise for buffer_append_vsprintf.
+ */
static void
-test_vsprintf(struct buffer *buffer, bool append, const char *format, ...)
+test_append_vsprintf(struct buffer *buffer, const char *format, ...)
{
va_list args;
va_start(args, format);
- buffer_vsprintf(buffer, append, format, args);
+ buffer_append_vsprintf(buffer, format, args);
va_end(args);
}
@@ -159,10 +175,10 @@
free(data);
buffer_free(three);
- /* buffer_sprintf */
+ /* buffer_sprintf and buffer_append_sprintf */
three = buffer_new();
- buffer_sprintf(three, true, "testing %d testing", 6);
- is_int(0, three->used, "buffer_sprintf doesn't change used");
+ buffer_append_sprintf(three, "testing %d testing", 6);
+ is_int(0, three->used, "buffer_append_sprintf doesn't change used");
is_int(17, three->left, "but sets left correctly");
buffer_append(three, "", 1);
is_int(18, three->left, "appending a nul works");
@@ -170,18 +186,18 @@
three->left--;
three->used += 5;
three->left -= 5;
- buffer_sprintf(three, true, " %d", 7);
+ buffer_append_sprintf(three, " %d", 7);
is_int(14, three->left, "appending a digit works");
buffer_append(three, "", 1);
is_string("testing 6 testing 7", three->data, "and the data is correct");
- buffer_sprintf(three, false, "%d testing", 8);
+ buffer_sprintf(three, "%d testing", 8);
is_int(9, three->left, "replacing the buffer works");
is_string("8 testing", three->data, "and the results are correct");
data = xmalloc(1050);
memset(data, 'a', 1049);
data[1049] = '\0';
is_int(1024, three->size, "size before large sprintf is 1024");
- buffer_sprintf(three, false, "%s", data);
+ buffer_sprintf(three, "%s", data);
is_int(2048, three->size, "size after large sprintf is 2048");
is_int(1049, three->left, "and left is correct");
buffer_append(three, "", 1);
@@ -235,10 +251,10 @@
free(data);
buffer_free(three);
- /* buffer_vsprintf */
+ /* buffer_vsprintf and buffer_append_vsprintf */
three = buffer_new();
- test_vsprintf(three, true, "testing %d testing", 6);
- is_int(0, three->used, "buffer_vsprintf leaves used as 0");
+ test_append_vsprintf(three, "testing %d testing", 6);
+ is_int(0, three->used, "buffer_append_vsprintf leaves used as 0");
is_int(17, three->left, "and left is correct");
buffer_append(three, "", 1);
is_int(18, three->left, "and left is correct after appending a nul");
@@ -246,18 +262,18 @@
three->left--;
three->used += 5;
three->left -= 5;
- test_vsprintf(three, true, " %d", 7);
+ test_append_vsprintf(three, " %d", 7);
is_int(14, three->left, "and appending results in the correct left");
buffer_append(three, "", 1);
is_string("testing 6 testing 7", three->data, "and the right data");
- test_vsprintf(three, false, "%d testing", 8);
+ test_vsprintf(three, "%d testing", 8);
is_int(9, three->left, "replacing the buffer results in the correct size");
is_string("8 testing", three->data, "and the correct data");
data = xmalloc(1050);
memset(data, 'a', 1049);
data[1049] = '\0';
is_int(1024, three->size, "size is 1024 before large vsprintf");
- test_vsprintf(three, false, "%s", data);
+ test_vsprintf(three, "%s", data);
is_int(2048, three->size, "and 2048 afterwards");
is_int(1049, three->left, "and left is correct");
buffer_append(three, "", 1);
More information about the inn-committers
mailing list