Lines Matching refs:buf

32 sshbuf_check_sanity(const struct sshbuf *buf)
35 if (__predict_false(buf == NULL ||
36 (!buf->readonly && buf->d != buf->cd) ||
37 buf->refcount < 1 || buf->refcount > SSHBUF_REFS_MAX ||
38 buf->cd == NULL ||
39 (buf->dont_free && (buf->readonly || buf->parent != NULL)) ||
40 buf->max_size > SSHBUF_SIZE_MAX ||
41 buf->alloc > buf->max_size ||
42 buf->size > buf->alloc ||
43 buf->off > buf->size)) {
54 sshbuf_maybe_pack(struct sshbuf *buf, int force)
58 if (buf->off == 0 || buf->readonly || buf->refcount > 1)
61 (buf->off >= SSHBUF_PACK_MIN && buf->off >= buf->size / 2)) {
62 memmove(buf->d, buf->d + buf->off, buf->size - buf->off);
63 buf->size -= buf->off;
64 buf->off = 0;
119 sshbuf_fromb(struct sshbuf *buf)
123 if (sshbuf_check_sanity(buf) != 0)
125 if ((ret = sshbuf_from(sshbuf_ptr(buf), sshbuf_len(buf))) == NULL)
127 if (sshbuf_set_parent(ret, buf) != 0) {
148 sshbuf_free(struct sshbuf *buf)
152 if (buf == NULL)
160 if (sshbuf_check_sanity(buf) != 0)
166 if (buf->parent != NULL) {
167 sshbuf_free(buf->parent);
168 buf->parent = NULL;
175 buf->refcount--;
176 if (buf->refcount > 0)
178 dont_free = buf->dont_free;
179 if (!buf->readonly) {
180 bzero(buf->d, buf->alloc);
181 free(buf->d);
183 bzero(buf, sizeof(*buf));
185 free(buf);
189 sshbuf_reset(struct sshbuf *buf)
193 if (buf->readonly || buf->refcount > 1) {
195 buf->off = buf->size;
198 if (sshbuf_check_sanity(buf) == 0)
199 bzero(buf->d, buf->alloc);
200 buf->off = buf->size = 0;
201 if (buf->alloc != SSHBUF_SIZE_INIT) {
202 if ((d = realloc(buf->d, SSHBUF_SIZE_INIT)) != NULL) {
203 buf->cd = buf->d = d;
204 buf->alloc = SSHBUF_SIZE_INIT;
210 sshbuf_max_size(const struct sshbuf *buf)
212 return buf->max_size;
216 sshbuf_alloc(const struct sshbuf *buf)
218 return buf->alloc;
222 sshbuf_parent(const struct sshbuf *buf)
224 return buf->parent;
228 sshbuf_refcount(const struct sshbuf *buf)
230 return buf->refcount;
234 sshbuf_set_max_size(struct sshbuf *buf, size_t max_size)
240 SSHBUF_DBG(("set max buf = %p len = %zu", buf, max_size));
241 if ((r = sshbuf_check_sanity(buf)) != 0)
243 if (max_size == buf->max_size)
245 if (buf->readonly || buf->refcount > 1)
250 sshbuf_maybe_pack(buf, max_size < buf->size);
251 if (max_size < buf->alloc && max_size > buf->size) {
252 if (buf->size < SSHBUF_SIZE_INIT)
255 rlen = roundup(buf->size, SSHBUF_SIZE_INC);
258 bzero(buf->d + buf->size, buf->alloc - buf->size);
260 if ((dp = realloc(buf->d, rlen)) == NULL)
262 buf->cd = buf->d = dp;
263 buf->alloc = rlen;
266 if (max_size < buf->alloc)
268 buf->max_size = max_size;
273 sshbuf_len(const struct sshbuf *buf)
275 if (sshbuf_check_sanity(buf) != 0)
277 return buf->size - buf->off;
281 sshbuf_avail(const struct sshbuf *buf)
283 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1)
285 return buf->max_size - (buf->size - buf->off);
289 sshbuf_ptr(const struct sshbuf *buf)
291 if (sshbuf_check_sanity(buf) != 0)
293 return buf->cd + buf->off;
297 sshbuf_mutable_ptr(const struct sshbuf *buf)
299 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1)
301 return buf->d + buf->off;
305 sshbuf_check_reserve(const struct sshbuf *buf, size_t len)
309 if ((r = sshbuf_check_sanity(buf)) != 0)
311 if (buf->readonly || buf->refcount > 1)
315 if (len > buf->max_size || buf->max_size - len < buf->size - buf->off)
321 sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp)
330 SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len));
331 if ((r = sshbuf_check_reserve(buf, len)) != 0)
335 * then pack the buffer, zeroing buf->off.
337 sshbuf_maybe_pack(buf, buf->size + len > buf->max_size);
339 if (len + buf->size > buf->alloc) {
344 need = len + buf->size - buf->alloc;
345 rlen = roundup(buf->alloc + need, SSHBUF_SIZE_INC);
347 if (rlen > buf->max_size)
348 rlen = buf->alloc + need;
350 if ((dp = realloc(buf->d, rlen)) == NULL) {
356 buf->alloc = rlen;
357 buf->cd = buf->d = dp;
358 if ((r = sshbuf_check_reserve(buf, len)) < 0) {
365 dp = buf->d + buf->size;
366 buf->size += len;
374 sshbuf_consume(struct sshbuf *buf, size_t len)
379 if ((r = sshbuf_check_sanity(buf)) != 0)
383 if (len > sshbuf_len(buf))
385 buf->off += len;
391 sshbuf_consume_end(struct sshbuf *buf, size_t len)
396 if ((r = sshbuf_check_sanity(buf)) != 0)
400 if (len > sshbuf_len(buf))
402 buf->size -= len;