Lines Matching refs:buf

50 static int ensure_space(struct k5buf *buf, size_t len)
55 if (buf->buftype == BUFTYPE_ERROR)
57 if (buf->space - 1 - buf->len >= len) /* Enough room already. */
59 if (buf->buftype == BUFTYPE_FIXED) /* Can't resize a fixed buffer. */
61 assert(buf->buftype == BUFTYPE_DYNAMIC);
62 new_space = buf->space * 2;
63 while (new_space <= SPACE_MAX && new_space - buf->len - 1 < len)
67 new_data = realloc(buf->data, new_space);
70 buf->data = new_data;
71 buf->space = new_space;
75 if (buf->buftype == BUFTYPE_DYNAMIC) {
76 free(buf->data);
77 buf->data = NULL;
79 buf->buftype = BUFTYPE_ERROR;
83 void krb5int_buf_init_fixed(struct k5buf *buf, char *data, size_t space)
86 buf->buftype = BUFTYPE_FIXED;
87 buf->data = data;
88 buf->space = space;
89 buf->len = 0;
90 buf->data[0] = '\0';
93 void krb5int_buf_init_dynamic(struct k5buf *buf)
95 buf->buftype = BUFTYPE_DYNAMIC;
96 buf->space = DYNAMIC_INITIAL_SIZE;
97 buf->data = malloc(buf->space);
98 if (buf->data == NULL) {
99 buf->buftype = BUFTYPE_ERROR;
102 buf->len = 0;
103 buf->data[0] = '\0';
106 void krb5int_buf_add(struct k5buf *buf, const char *data)
108 krb5int_buf_add_len(buf, data, strlen(data));
111 void krb5int_buf_add_len(struct k5buf *buf, const char *data, size_t len)
113 if (!ensure_space(buf, len))
115 memcpy(buf->data + buf->len, data, len);
116 buf->len += len;
117 buf->data[buf->len] = '\0';
120 void krb5int_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
127 if (buf->buftype == BUFTYPE_ERROR)
129 remaining = buf->space - buf->len;
131 if (buf->buftype == BUFTYPE_FIXED) {
134 r = vsnprintf(buf->data + buf->len, remaining, fmt, ap);
137 buf->buftype = BUFTYPE_ERROR;
139 buf->len += (unsigned int) r;
144 assert(buf->buftype == BUFTYPE_DYNAMIC);
146 r = vsnprintf(buf->data + buf->len, remaining, fmt, ap);
149 buf->len += (unsigned int) r;
155 if (!ensure_space(buf, r))
157 remaining = buf->space - buf->len;
159 r = vsnprintf(buf->data + buf->len, remaining, fmt, ap);
162 buf->buftype = BUFTYPE_ERROR;
164 buf->len += (unsigned int) r;
174 buf->buftype = BUFTYPE_ERROR;
177 if (ensure_space(buf, r)) {
178 /* Copy the temporary string into buf, including terminator. */
179 memcpy(buf->data + buf->len, tmp, r + 1);
180 buf->len += r;
185 void krb5int_buf_truncate(struct k5buf *buf, size_t len)
187 if (buf->buftype == BUFTYPE_ERROR)
189 assert(len <= buf->len);
190 buf->len = len;
191 buf->data[buf->len] = '\0';
195 char *krb5int_buf_data(struct k5buf *buf)
197 return (buf->buftype == BUFTYPE_ERROR) ? NULL : buf->data;
200 ssize_t krb5int_buf_len(struct k5buf *buf)
202 return (buf->buftype == BUFTYPE_ERROR) ? -1 : (ssize_t) buf->len;
205 void krb5int_free_buf(struct k5buf *buf)
207 if (buf->buftype == BUFTYPE_ERROR)
209 assert(buf->buftype == BUFTYPE_DYNAMIC);
210 free(buf->data);
211 buf->data = NULL;
212 buf->buftype = BUFTYPE_ERROR;