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 SWAP(b
->buf
, *ptr
, char*);
120 talloc_steal(b
, b
->buf
);
121 b
->size
= talloc_get_size(b
->buf
);
122 b
->pos
= (len
== -1) ? strlen(b
->buf
) : len
;
124 assert(b
->pos
<= b
->size
);
128 cbuf
* cbuf_resize(cbuf
* b
, size_t size
)
130 char* save_buf
= b
->buf
;
131 b
->buf
= talloc_realloc(b
, b
->buf
, char, size
);
132 if (b
->buf
== NULL
) {
133 talloc_free(save_buf
);
138 b
->pos
= MIN(b
->pos
, b
->size
);
139 return b
->buf
? b
: NULL
;
142 char* cbuf_reserve(cbuf
* b
, size_t len
)
144 if(b
->size
< b
->pos
+ len
)
145 cbuf_resize(b
, MAX(2*b
->size
, b
->pos
+ len
));
146 return b
->buf
? b
->buf
+ b
->pos
: NULL
;
149 int cbuf_puts(cbuf
* b
, const char* str
, size_t len
)
160 dst
= cbuf_reserve(b
, len
+1);
164 memcpy(dst
, str
, len
);
165 dst
[len
] = '\0'; /* just to ease debugging */
168 assert(b
->pos
< b
->size
);
173 int cbuf_putc(cbuf
* b
, char c
) {
179 dst
= cbuf_reserve(b
, 2);
185 dst
[1] = '\0'; /* just to ease debugging */
188 assert(b
->pos
< b
->size
);
193 int cbuf_putdw(cbuf
* b
, uint32_t u
) {
195 static const size_t LEN
= sizeof(uint32_t);
200 dst
= cbuf_reserve(b
, LEN
);
208 assert(b
->pos
<= b
->size
); /* no NULL termination*/
213 size_t cbuf_getpos(const cbuf
* b
) {
214 assert(b
->pos
<= b
->size
);
218 void cbuf_setpos(cbuf
* b
, size_t pos
) {
219 assert(pos
<= b
->size
);
222 b
->buf
[pos
] = '\0'; /* just to ease debugging */
225 char* cbuf_gets(cbuf
* b
, size_t idx
) {
226 assert(idx
<= b
->pos
);
228 if (cbuf_reserve(b
, 1) == NULL
)
231 b
->buf
[b
->pos
] = '\0';
235 int cbuf_printf(cbuf
* b
, const char* fmt
, ...)
239 char* dst
= b
->buf
+ b
->pos
;
240 const int avail
= b
->size
- b
->pos
;
244 va_copy(args2
, args
);
246 len
= vsnprintf(dst
, avail
, fmt
, args
);
249 dst
= cbuf_reserve(b
, len
+1);
250 len
= (dst
!= NULL
) ? vsnprintf(dst
, len
+1, fmt
, args2
) : -1;
259 assert(b
->pos
<= b
->size
);
264 int cbuf_print_quoted_string(cbuf
* ost
, const char* s
)
277 cbuf_putc(ost
, '\\');