Lines Matching refs:buf
35 buf->size, buf->alloc, buf->off, buf->max_size); \
55 sshbuf_check_sanity(const struct sshbuf *buf) in sshbuf_check_sanity() argument
58 if (__predict_false(buf == NULL || in sshbuf_check_sanity()
59 (!buf->readonly && buf->d != buf->cd) || in sshbuf_check_sanity()
60 buf->refcount < 1 || buf->refcount > SSHBUF_REFS_MAX || in sshbuf_check_sanity()
61 buf->cd == NULL || in sshbuf_check_sanity()
62 buf->max_size > SSHBUF_SIZE_MAX || in sshbuf_check_sanity()
63 buf->alloc > buf->max_size || in sshbuf_check_sanity()
64 buf->size > buf->alloc || in sshbuf_check_sanity()
65 buf->off > buf->size)) { in sshbuf_check_sanity()
76 sshbuf_maybe_pack(struct sshbuf *buf, int force) in sshbuf_maybe_pack() argument
80 if (buf->off == 0 || buf->readonly || buf->refcount > 1) in sshbuf_maybe_pack()
83 (buf->off >= SSHBUF_PACK_MIN && buf->off >= buf->size / 2)) { in sshbuf_maybe_pack()
84 memmove(buf->d, buf->d + buf->off, buf->size - buf->off); in sshbuf_maybe_pack()
85 buf->size -= buf->off; in sshbuf_maybe_pack()
86 buf->off = 0; in sshbuf_maybe_pack()
143 sshbuf_fromb(struct sshbuf *buf) in sshbuf_fromb() argument
147 if (sshbuf_check_sanity(buf) != 0) in sshbuf_fromb()
149 if ((ret = sshbuf_from(sshbuf_ptr(buf), sshbuf_len(buf))) == NULL) in sshbuf_fromb()
151 if (sshbuf_set_parent(ret, buf) != 0) { in sshbuf_fromb()
159 sshbuf_free(struct sshbuf *buf) in sshbuf_free() argument
161 if (buf == NULL) in sshbuf_free()
169 if (sshbuf_check_sanity(buf) != 0) in sshbuf_free()
177 buf->refcount--; in sshbuf_free()
178 if (buf->refcount > 0) in sshbuf_free()
185 sshbuf_free(buf->parent); in sshbuf_free()
186 buf->parent = NULL; in sshbuf_free()
188 if (!buf->readonly) { in sshbuf_free()
189 explicit_bzero(buf->d, buf->alloc); in sshbuf_free()
190 free(buf->d); in sshbuf_free()
192 freezero(buf, sizeof(*buf)); in sshbuf_free()
196 sshbuf_reset(struct sshbuf *buf) in sshbuf_reset() argument
200 if (buf->readonly || buf->refcount > 1) { in sshbuf_reset()
202 buf->off = buf->size; in sshbuf_reset()
205 if (sshbuf_check_sanity(buf) != 0) in sshbuf_reset()
207 buf->off = buf->size = 0; in sshbuf_reset()
208 if (buf->alloc != SSHBUF_SIZE_INIT) { in sshbuf_reset()
209 if ((d = recallocarray(buf->d, buf->alloc, SSHBUF_SIZE_INIT, in sshbuf_reset()
211 buf->cd = buf->d = d; in sshbuf_reset()
212 buf->alloc = SSHBUF_SIZE_INIT; in sshbuf_reset()
215 explicit_bzero(buf->d, buf->alloc); in sshbuf_reset()
219 sshbuf_max_size(const struct sshbuf *buf) in sshbuf_max_size() argument
221 return buf->max_size; in sshbuf_max_size()
225 sshbuf_alloc(const struct sshbuf *buf) in sshbuf_alloc() argument
227 return buf->alloc; in sshbuf_alloc()
231 sshbuf_parent(const struct sshbuf *buf) in sshbuf_parent() argument
233 return buf->parent; in sshbuf_parent()
237 sshbuf_refcount(const struct sshbuf *buf) in sshbuf_refcount() argument
239 return buf->refcount; in sshbuf_refcount()
243 sshbuf_set_max_size(struct sshbuf *buf, size_t max_size) in sshbuf_set_max_size() argument
249 SSHBUF_DBG(("set max buf = %p len = %zu", buf, max_size)); in sshbuf_set_max_size()
250 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_set_max_size()
252 if (max_size == buf->max_size) in sshbuf_set_max_size()
254 if (buf->readonly || buf->refcount > 1) in sshbuf_set_max_size()
259 sshbuf_maybe_pack(buf, max_size < buf->size); in sshbuf_set_max_size()
260 if (max_size < buf->alloc && max_size > buf->size) { in sshbuf_set_max_size()
261 if (buf->size < SSHBUF_SIZE_INIT) in sshbuf_set_max_size()
264 rlen = ROUNDUP(buf->size, SSHBUF_SIZE_INC); in sshbuf_set_max_size()
268 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL) in sshbuf_set_max_size()
270 buf->cd = buf->d = dp; in sshbuf_set_max_size()
271 buf->alloc = rlen; in sshbuf_set_max_size()
274 if (max_size < buf->alloc) in sshbuf_set_max_size()
276 buf->max_size = max_size; in sshbuf_set_max_size()
281 sshbuf_len(const struct sshbuf *buf) in sshbuf_len() argument
283 if (sshbuf_check_sanity(buf) != 0) in sshbuf_len()
285 return buf->size - buf->off; in sshbuf_len()
289 sshbuf_avail(const struct sshbuf *buf) in sshbuf_avail() argument
291 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1) in sshbuf_avail()
293 return buf->max_size - (buf->size - buf->off); in sshbuf_avail()
297 sshbuf_ptr(const struct sshbuf *buf) in sshbuf_ptr() argument
299 if (sshbuf_check_sanity(buf) != 0) in sshbuf_ptr()
301 return buf->cd + buf->off; in sshbuf_ptr()
305 sshbuf_mutable_ptr(const struct sshbuf *buf) in sshbuf_mutable_ptr() argument
307 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1) in sshbuf_mutable_ptr()
309 return buf->d + buf->off; in sshbuf_mutable_ptr()
313 sshbuf_check_reserve(const struct sshbuf *buf, size_t len) in sshbuf_check_reserve() argument
317 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_check_reserve()
319 if (buf->readonly || buf->refcount > 1) in sshbuf_check_reserve()
323 if (len > buf->max_size || buf->max_size - len < buf->size - buf->off) in sshbuf_check_reserve()
329 sshbuf_allocate(struct sshbuf *buf, size_t len) in sshbuf_allocate() argument
335 SSHBUF_DBG(("allocate buf = %p len = %zu", buf, len)); in sshbuf_allocate()
336 if ((r = sshbuf_check_reserve(buf, len)) != 0) in sshbuf_allocate()
342 sshbuf_maybe_pack(buf, buf->size + len > buf->max_size); in sshbuf_allocate()
344 if (len + buf->size <= buf->alloc) in sshbuf_allocate()
351 need = len + buf->size - buf->alloc; in sshbuf_allocate()
352 rlen = ROUNDUP(buf->alloc + need, SSHBUF_SIZE_INC); in sshbuf_allocate()
354 if (rlen > buf->max_size) in sshbuf_allocate()
355 rlen = buf->alloc + need; in sshbuf_allocate()
357 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL) { in sshbuf_allocate()
361 buf->alloc = rlen; in sshbuf_allocate()
362 buf->cd = buf->d = dp; in sshbuf_allocate()
363 if ((r = sshbuf_check_reserve(buf, len)) < 0) { in sshbuf_allocate()
372 sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp) in sshbuf_reserve() argument
380 SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len)); in sshbuf_reserve()
381 if ((r = sshbuf_allocate(buf, len)) != 0) in sshbuf_reserve()
384 dp = buf->d + buf->size; in sshbuf_reserve()
385 buf->size += len; in sshbuf_reserve()
392 sshbuf_consume(struct sshbuf *buf, size_t len) in sshbuf_consume() argument
397 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_consume()
401 if (len > sshbuf_len(buf)) in sshbuf_consume()
403 buf->off += len; in sshbuf_consume()
405 if (buf->off == buf->size) in sshbuf_consume()
406 buf->off = buf->size = 0; in sshbuf_consume()
412 sshbuf_consume_end(struct sshbuf *buf, size_t len) in sshbuf_consume_end() argument
417 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_consume_end()
421 if (len > sshbuf_len(buf)) in sshbuf_consume_end()
423 buf->size -= len; in sshbuf_consume_end()