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.
31 #include "system/locale.h"
35 #include "lib/util/byteorder.h"
45 cbuf* cbuf_clear(cbuf* b)
51 cbuf* cbuf_new(const void* ctx)
53 cbuf* s = talloc(ctx, cbuf);
57 s->buf = (char *)talloc_size(s, s->size);
58 if (s->size && (s->buf == NULL)) {
65 cbuf* cbuf_copy(const cbuf* b)
67 cbuf* s = talloc(talloc_parent(b), cbuf);
72 s->buf = (char *)talloc_memdup(s, b->buf, b->size); /* only up to pos? */
74 /* XXX shallow did not work, because realloc */
75 /* fails with multiple references */
76 /* s->buf = talloc_reference(s, b->buf); */
87 void cbuf_delete(cbuf* b)
92 #define SWAP(A,B,T) do { \
93 T tmp = A; A = B; B = tmp; \
97 void cbuf_swap(cbuf* b1, cbuf* b2)
102 talloc_reparent(b1, b2, b1->buf);
103 talloc_reparent(b2, b1, b2->buf);
104 SWAP(b1->buf, b2->buf, char*);
105 SWAP(b1->pos, b2->pos, size_t);
106 SWAP(b1->size, b2->size, size_t);
111 cbuf* cbuf_takeover(cbuf* b1, cbuf* b2)
113 talloc_reparent(b2, b1, b2->buf);
121 cbuf* cbuf_swapptr(cbuf* b, char** ptr, size_t len)
123 void* p = talloc_parent(*ptr);
124 SWAP(b->buf, *ptr, char*);
125 talloc_steal(b, b->buf);
126 talloc_steal(p, *ptr);
127 b->size = talloc_get_size(b->buf);
128 b->pos = (len == -1) ? strlen(b->buf) : len;
130 assert(b->pos <= b->size);
134 cbuf* cbuf_resize(cbuf* b, size_t size)
136 char* save_buf = b->buf;
137 b->buf = talloc_realloc(b, b->buf, char, size);
138 if (b->buf == NULL) {
139 talloc_free(save_buf);
144 b->pos = MIN(b->pos, b->size);
145 return b->buf ? b : NULL;
148 char* cbuf_reserve(cbuf* b, size_t len)
150 if(b->size < b->pos + len)
151 cbuf_resize(b, MAX(2*b->size, b->pos + len));
152 return b->buf ? b->buf + b->pos : NULL;
155 int cbuf_puts(cbuf* b, const char* str, size_t len)
166 dst = cbuf_reserve(b, len+1);
170 memcpy(dst, str, len);
171 dst[len] = '\0'; /* just to ease debugging */
174 assert(b->pos < b->size);
179 int cbuf_putc(cbuf* b, char c) {
185 dst = cbuf_reserve(b, 2);
191 dst[1] = '\0'; /* just to ease debugging */
194 assert(b->pos < b->size);
199 int cbuf_putdw(cbuf* b, uint32_t u) {
201 static const size_t LEN = sizeof(uint32_t);
206 dst = cbuf_reserve(b, LEN);
214 assert(b->pos <= b->size); /* no NULL termination*/
219 size_t cbuf_getpos(const cbuf* b) {
220 assert(b->pos <= b->size);
224 void cbuf_setpos(cbuf* b, size_t pos) {
225 assert(pos <= b->size);
228 b->buf[pos] = '\0'; /* just to ease debugging */
231 char* cbuf_gets(cbuf* b, size_t idx) {
232 assert(idx <= b->pos);
234 if (cbuf_reserve(b, 1) == NULL)
237 b->buf[b->pos] = '\0';
241 int cbuf_printf(cbuf* b, const char* fmt, ...)
245 char* dst = b->buf + b->pos;
246 const int avail = b->size - b->pos;
250 va_copy(args2, args);
252 len = vsnprintf(dst, avail, fmt, args);
255 dst = cbuf_reserve(b, len+1);
256 len = (dst != NULL) ? vsnprintf(dst, len+1, fmt, args2) : -1;
265 assert(b->pos <= b->size);
270 int cbuf_print_quoted_string(cbuf* ost, const char* s)
283 cbuf_putc(ost, '\\');
296 int cbuf_print_quoted(cbuf* ost, const char* s, size_t len)
300 cbuf_reserve(ost, len+2);
308 ret = cbuf_printf(ost, "\\%c", *s);
311 if (isprint(*s) && ((*s == ' ') || !isspace(*s))) {
312 ret = cbuf_putc(ost, *s);
314 ret = cbuf_printf(ost,
325 ret = cbuf_putc(ost,'"');
327 return (ret == -1) ? -1 : (n + ret);