2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Gregor Beck 2010
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * @author Gregor Beck <gb@sernet.de>
25 * @brief A talloced character buffer.
41 cbuf* cbuf_clear(cbuf* b)
47 cbuf* cbuf_new(const void* ctx)
49 cbuf* s = talloc(ctx, cbuf);
53 s->buf = (char *)talloc_size(s, s->size);
54 if (s->size && (s->buf == NULL)) {
61 cbuf* cbuf_copy(const cbuf* b)
63 cbuf* s = talloc(talloc_parent(b), cbuf);
68 s->buf = (char *)talloc_memdup(s, b->buf, b->size); /* only up to pos? */
70 /* XXX shallow did not work, because realloc */
71 /* fails with multiple references */
72 /* s->buf = talloc_reference(s, b->buf); */
83 void cbuf_delete(cbuf* b)
88 #define SWAP(A,B,T) do { \
89 T tmp = A; A = B; B = tmp; \
93 void cbuf_swap(cbuf* b1, cbuf* b2)
98 talloc_reparent(b1, b2, b1->buf);
99 talloc_reparent(b2, b1, b2->buf);
100 SWAP(b1->buf, b2->buf, char*);
101 SWAP(b1->pos, b2->pos, size_t);
102 SWAP(b1->size, b2->size, size_t);
107 cbuf* cbuf_takeover(cbuf* b1, cbuf* b2)
109 talloc_reparent(b2, b1, b2->buf);
117 cbuf* cbuf_swapptr(cbuf* b, char** ptr, size_t len)
119 void* p = talloc_parent(*ptr);
120 SWAP(b->buf, *ptr, char*);
121 talloc_steal(b, b->buf);
122 talloc_steal(p, *ptr);
123 b->size = talloc_get_size(b->buf);
124 b->pos = (len == -1) ? strlen(b->buf) : len;
126 assert(b->pos <= b->size);
130 cbuf* cbuf_resize(cbuf* b, size_t size)
132 char* save_buf = b->buf;
133 b->buf = talloc_realloc(b, b->buf, char, size);
134 if (b->buf == NULL) {
135 talloc_free(save_buf);
140 b->pos = MIN(b->pos, b->size);
141 return b->buf ? b : NULL;
144 char* cbuf_reserve(cbuf* b, size_t len)
146 if(b->size < b->pos + len)
147 cbuf_resize(b, MAX(2*b->size, b->pos + len));
148 return b->buf ? b->buf + b->pos : NULL;
151 int cbuf_puts(cbuf* b, const char* str, size_t len)
162 dst = cbuf_reserve(b, len+1);
166 memcpy(dst, str, len);
167 dst[len] = '\0'; /* just to ease debugging */
170 assert(b->pos < b->size);
175 int cbuf_putc(cbuf* b, char c) {
181 dst = cbuf_reserve(b, 2);
187 dst[1] = '\0'; /* just to ease debugging */
190 assert(b->pos < b->size);
195 int cbuf_putdw(cbuf* b, uint32_t u) {
197 static const size_t LEN = sizeof(uint32_t);
202 dst = cbuf_reserve(b, LEN);
210 assert(b->pos <= b->size); /* no NULL termination*/
215 size_t cbuf_getpos(const cbuf* b) {
216 assert(b->pos <= b->size);
220 void cbuf_setpos(cbuf* b, size_t pos) {
221 assert(pos <= b->size);
224 b->buf[pos] = '\0'; /* just to ease debugging */
227 char* cbuf_gets(cbuf* b, size_t idx) {
228 assert(idx <= b->pos);
230 if (cbuf_reserve(b, 1) == NULL)
233 b->buf[b->pos] = '\0';
237 int cbuf_printf(cbuf* b, const char* fmt, ...)
241 char* dst = b->buf + b->pos;
242 const int avail = b->size - b->pos;
246 va_copy(args2, args);
248 len = vsnprintf(dst, avail, fmt, args);
251 dst = cbuf_reserve(b, len+1);
252 len = (dst != NULL) ? vsnprintf(dst, len+1, fmt, args2) : -1;
261 assert(b->pos <= b->size);
266 int cbuf_print_quoted_string(cbuf* ost, const char* s)
279 cbuf_putc(ost, '\\');
291 int cbuf_print_quoted(cbuf* ost, const char* s, size_t len)
295 cbuf_reserve(ost, len+2);
303 ret = cbuf_printf(ost, "\\%c", *s);
306 if (isprint(*s) && ((*s == ' ') || !isspace(*s))) {
307 ret = cbuf_putc(ost, *s);
309 ret = cbuf_printf(ost, "\\%02x", (uchar)*s);
318 ret = cbuf_putc(ost,'"');
320 return (ret == -1) ? -1 : (n + ret);