s4-auth: Use kerberos util functions in srv_keytab
[Samba.git] / source3 / lib / cbuf.c
blob611aa80609f375d402fa56f0654efb6718573d08
1 /*
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/>.
20 /**
21 * @file cbuf.c
22 * @author Gregor Beck <gb@sernet.de>
23 * @date Aug 2010
25 * @brief A talloced character buffer.
30 #include "includes.h"
31 #include "cbuf.h"
34 struct cbuf {
35 char* buf;
36 size_t pos;
37 size_t size;
41 cbuf* cbuf_clear(cbuf* b)
43 cbuf_setpos(b, 0);
44 return b;
47 cbuf* cbuf_new(const void* ctx)
49 cbuf* s = talloc(ctx, cbuf);
50 if (s == NULL)
51 return NULL;
52 s->size = 32;
53 s->buf = (char *)talloc_size(s, s->size);
54 if (s->size && (s->buf == NULL)) {
55 talloc_free(s);
56 return NULL;
58 return cbuf_clear(s);
61 cbuf* cbuf_copy(const cbuf* b)
63 cbuf* s = talloc(talloc_parent(b), cbuf);
64 if (s == NULL) {
65 return NULL;
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); */
74 if (s->buf == NULL) {
75 cbuf_delete(s);
76 return NULL;
78 s->size = b->size;
79 s->pos = b->pos;
80 return s;
83 void cbuf_delete(cbuf* b)
85 talloc_free(b);
88 #define SWAP(A,B,T) do { \
89 T tmp = A; A = B; B = tmp; \
90 } while(0)
93 void cbuf_swap(cbuf* b1, cbuf* b2)
95 if (b1 == b2) {
96 return;
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);
110 b1->buf = b2->buf;
111 b1->pos = b2->pos;
112 b1->size = b2->size;
113 cbuf_delete(b2);
114 return b1;
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);
127 return b;
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);
136 b->size = 0;
137 } else {
138 b->size = size;
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)
153 char* dst;
155 if (b == NULL)
156 return 0;
158 if (len == -1) {
159 len=strlen(str);
162 dst = cbuf_reserve(b, len+1);
163 if (dst == NULL)
164 return -1;
166 memcpy(dst, str, len);
167 dst[len] = '\0'; /* just to ease debugging */
169 b->pos += len;
170 assert(b->pos < b->size);
172 return len;
175 int cbuf_putc(cbuf* b, char c) {
176 char* dst;
178 if (b == NULL)
179 return 0;
181 dst = cbuf_reserve(b, 2);
182 if (dst == NULL) {
183 return -1;
186 dst[0] = c;
187 dst[1] = '\0'; /* just to ease debugging */
189 b->pos++;
190 assert(b->pos < b->size);
192 return 1;
195 int cbuf_putdw(cbuf* b, uint32_t u) {
196 char* dst;
197 static const size_t LEN = sizeof(uint32_t);
199 if (b == NULL)
200 return 0;
202 dst = cbuf_reserve(b, LEN);
203 if (dst == NULL) {
204 return -1;
207 SIVAL(dst, 0, u);
209 b->pos += LEN;
210 assert(b->pos <= b->size); /* no NULL termination*/
212 return LEN;
215 size_t cbuf_getpos(const cbuf* b) {
216 assert(b->pos <= b->size);
217 return b->pos;
220 void cbuf_setpos(cbuf* b, size_t pos) {
221 assert(pos <= b->size);
222 b->pos = pos;
223 if (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)
231 return NULL;
233 b->buf[b->pos] = '\0';
234 return b->buf + idx;
237 int cbuf_printf(cbuf* b, const char* fmt, ...)
239 va_list args, args2;
240 int len;
241 char* dst = b->buf + b->pos;
242 const int avail = b->size - b->pos;
243 assert(avail >= 0);
245 va_start(args, fmt);
246 va_copy(args2, args);
248 len = vsnprintf(dst, avail, fmt, args);
250 if (len >= avail) {
251 dst = cbuf_reserve(b, len+1);
252 len = (dst != NULL) ? vsnprintf(dst, len+1, fmt, args2) : -1;
255 if (len > 0) {
256 b->pos += len;
259 va_end(args);
260 va_end(args2);
261 assert(b->pos <= b->size);
263 return len;
266 int cbuf_print_quoted_string(cbuf* ost, const char* s)
268 int n = 1;
269 cbuf_putc(ost,'"');
271 while(true) {
272 switch (*s) {
273 case '\0':
274 cbuf_putc(ost, '"');
275 return n+1;
277 case '"':
278 case '\\':
279 cbuf_putc(ost, '\\');
280 n++;
281 /* no break */
282 default:
283 cbuf_putc(ost, *s);
284 n++;
286 s++;
291 int cbuf_print_quoted(cbuf* ost, const char* s, size_t len)
293 int n = 1;
294 int ret;
295 cbuf_reserve(ost, len+2);
297 cbuf_putc(ost,'"');
299 while(len--) {
300 switch (*s) {
301 case '"':
302 case '\\':
303 ret = cbuf_printf(ost, "\\%c", *s);
304 break;
305 default:
306 if (isprint(*s) && ((*s == ' ') || !isspace(*s))) {
307 ret = cbuf_putc(ost, *s);
308 } else {
309 ret = cbuf_printf(ost, "\\%02x", (uchar)*s);
312 s++;
313 if (ret == -1) {
314 return -1;
316 n += ret;
318 ret = cbuf_putc(ost,'"');
320 return (ret == -1) ? -1 : (n + ret);