2 * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the MIT license. See COPYING for details.
11 * Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
12 * The copyrights to the contents of this file are licensed under the MIT License
13 * (https://www.opensource.org/licenses/mit-license.php)
26 #else /* !HAVE_STDARG_H */
27 #error Not enough var arg support!
28 #endif /* HAVE_STDARG_H */
32 #include "snprintf_compat.h"
33 #include "vasprintf_compat.h"
35 static int printbuf_extend(struct printbuf *p, int min_size);
37 struct printbuf *printbuf_new(void)
41 p = (struct printbuf *)calloc(1, sizeof(struct printbuf));
46 if (!(p->buf = (char *)malloc(p->size)))
56 * Extend the buffer p so it has a size of at least min_size.
58 * If the current size is large enough, nothing is changed.
60 * If extension failed, errno is set to indicate the error.
62 * Note: this does not check the available space! The caller
63 * is responsible for performing those calculations.
65 static int printbuf_extend(struct printbuf *p, int min_size)
70 if (p->size >= min_size)
72 /* Prevent signed integer overflows with large buffers. */
73 if (min_size > INT_MAX - 8)
78 if (p->size > INT_MAX / 2)
79 new_size = min_size + 8;
81 new_size = p->size * 2;
82 if (new_size < min_size + 8)
83 new_size = min_size + 8;
86 MC_DEBUG("printbuf_extend: realloc "
87 "bpos=%d min_size=%d old_size=%d new_size=%d\n",
88 p->bpos, min_size, p->size, new_size);
89 #endif /* PRINTBUF_DEBUG */
90 if (!(t = (char *)realloc(p->buf, new_size)))
97 int printbuf_memappend(struct printbuf *p, const char *buf, int size)
99 /* Prevent signed integer overflows with large buffers. */
100 if (size < 0 || size > INT_MAX - p->bpos - 1)
105 if (p->size <= p->bpos + size + 1)
107 if (printbuf_extend(p, p->bpos + size + 1) < 0)
110 memcpy(p->buf + p->bpos, buf, size);
112 p->buf[p->bpos] = '\0';
116 int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
122 /* Prevent signed integer overflows with large buffers. */
123 if (len < 0 || offset < -1 || len > INT_MAX - offset)
128 size_needed = offset + len;
129 if (pb->size < size_needed)
131 if (printbuf_extend(pb, size_needed) < 0)
135 if (pb->bpos < offset)
136 memset(pb->buf + pb->bpos, '\0', offset - pb->bpos);
137 memset(pb->buf + offset, charvalue, len);
138 if (pb->bpos < size_needed)
139 pb->bpos = size_needed;
144 int sprintbuf(struct printbuf *p, const char *msg, ...)
151 /* use stack buffer first */
153 size = vsnprintf(buf, 128, msg, ap);
155 /* if string is greater than stack buffer, then use dynamic string
156 * with vasprintf. Note: some implementations of vsnprintf return -1
157 * if output is truncated whereas some return the number of bytes that
158 * would have been written - this code handles both cases.
160 if (size < 0 || size > 127)
163 if ((size = vasprintf(&t, msg, ap)) < 0)
169 size = printbuf_memappend(p, t, size);
174 size = printbuf_memappend(p, buf, size);
179 void printbuf_reset(struct printbuf *p)
185 void printbuf_free(struct printbuf *p)