Lines Matching refs:buf
32 sshbuf_check_sanity(const struct sshbuf *buf) in sshbuf_check_sanity() argument
35 if (__predict_false(buf == NULL || in sshbuf_check_sanity()
36 (!buf->readonly && buf->d != buf->cd) || in sshbuf_check_sanity()
37 buf->refcount < 1 || buf->refcount > SSHBUF_REFS_MAX || in sshbuf_check_sanity()
38 buf->cd == NULL || in sshbuf_check_sanity()
39 (buf->dont_free && (buf->readonly || buf->parent != NULL)) || in sshbuf_check_sanity()
40 buf->max_size > SSHBUF_SIZE_MAX || in sshbuf_check_sanity()
41 buf->alloc > buf->max_size || in sshbuf_check_sanity()
42 buf->size > buf->alloc || in sshbuf_check_sanity()
43 buf->off > buf->size)) { in sshbuf_check_sanity()
54 sshbuf_maybe_pack(struct sshbuf *buf, int force) in sshbuf_maybe_pack() argument
58 if (buf->off == 0 || buf->readonly || buf->refcount > 1) in sshbuf_maybe_pack()
61 (buf->off >= SSHBUF_PACK_MIN && buf->off >= buf->size / 2)) { in sshbuf_maybe_pack()
62 memmove(buf->d, buf->d + buf->off, buf->size - buf->off); in sshbuf_maybe_pack()
63 buf->size -= buf->off; in sshbuf_maybe_pack()
64 buf->off = 0; in sshbuf_maybe_pack()
119 sshbuf_fromb(struct sshbuf *buf) in sshbuf_fromb() argument
123 if (sshbuf_check_sanity(buf) != 0) in sshbuf_fromb()
125 if ((ret = sshbuf_from(sshbuf_ptr(buf), sshbuf_len(buf))) == NULL) in sshbuf_fromb()
127 if (sshbuf_set_parent(ret, buf) != 0) { in sshbuf_fromb()
148 sshbuf_free(struct sshbuf *buf) in sshbuf_free() argument
152 if (buf == NULL) in sshbuf_free()
160 if (sshbuf_check_sanity(buf) != 0) in sshbuf_free()
166 sshbuf_free(buf->parent); in sshbuf_free()
167 buf->parent = NULL; in sshbuf_free()
173 buf->refcount--; in sshbuf_free()
174 if (buf->refcount > 0) in sshbuf_free()
176 dont_free = buf->dont_free; in sshbuf_free()
177 if (!buf->readonly) { in sshbuf_free()
178 explicit_bzero(buf->d, buf->alloc); in sshbuf_free()
179 free(buf->d); in sshbuf_free()
181 explicit_bzero(buf, sizeof(*buf)); in sshbuf_free()
183 free(buf); in sshbuf_free()
187 sshbuf_reset(struct sshbuf *buf) in sshbuf_reset() argument
191 if (buf->readonly || buf->refcount > 1) { in sshbuf_reset()
193 buf->off = buf->size; in sshbuf_reset()
196 if (sshbuf_check_sanity(buf) == 0) in sshbuf_reset()
197 explicit_bzero(buf->d, buf->alloc); in sshbuf_reset()
198 buf->off = buf->size = 0; in sshbuf_reset()
199 if (buf->alloc != SSHBUF_SIZE_INIT) { in sshbuf_reset()
200 if ((d = realloc(buf->d, SSHBUF_SIZE_INIT)) != NULL) { in sshbuf_reset()
201 buf->cd = buf->d = d; in sshbuf_reset()
202 buf->alloc = SSHBUF_SIZE_INIT; in sshbuf_reset()
208 sshbuf_max_size(const struct sshbuf *buf) in sshbuf_max_size() argument
210 return buf->max_size; in sshbuf_max_size()
214 sshbuf_alloc(const struct sshbuf *buf) in sshbuf_alloc() argument
216 return buf->alloc; in sshbuf_alloc()
220 sshbuf_parent(const struct sshbuf *buf) in sshbuf_parent() argument
222 return buf->parent; in sshbuf_parent()
226 sshbuf_refcount(const struct sshbuf *buf) in sshbuf_refcount() argument
228 return buf->refcount; in sshbuf_refcount()
232 sshbuf_set_max_size(struct sshbuf *buf, size_t max_size) in sshbuf_set_max_size() argument
238 SSHBUF_DBG(("set max buf = %p len = %zu", buf, max_size)); in sshbuf_set_max_size()
239 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_set_max_size()
241 if (max_size == buf->max_size) in sshbuf_set_max_size()
243 if (buf->readonly || buf->refcount > 1) in sshbuf_set_max_size()
248 sshbuf_maybe_pack(buf, max_size < buf->size); in sshbuf_set_max_size()
249 if (max_size < buf->alloc && max_size > buf->size) { in sshbuf_set_max_size()
250 if (buf->size < SSHBUF_SIZE_INIT) in sshbuf_set_max_size()
253 rlen = ROUNDUP(buf->size, SSHBUF_SIZE_INC); in sshbuf_set_max_size()
256 explicit_bzero(buf->d + buf->size, buf->alloc - buf->size); in sshbuf_set_max_size()
258 if ((dp = realloc(buf->d, rlen)) == NULL) in sshbuf_set_max_size()
260 buf->cd = buf->d = dp; in sshbuf_set_max_size()
261 buf->alloc = rlen; in sshbuf_set_max_size()
264 if (max_size < buf->alloc) in sshbuf_set_max_size()
266 buf->max_size = max_size; in sshbuf_set_max_size()
271 sshbuf_len(const struct sshbuf *buf) in sshbuf_len() argument
273 if (sshbuf_check_sanity(buf) != 0) in sshbuf_len()
275 return buf->size - buf->off; in sshbuf_len()
279 sshbuf_avail(const struct sshbuf *buf) in sshbuf_avail() argument
281 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1) in sshbuf_avail()
283 return buf->max_size - (buf->size - buf->off); in sshbuf_avail()
287 sshbuf_ptr(const struct sshbuf *buf) in sshbuf_ptr() argument
289 if (sshbuf_check_sanity(buf) != 0) in sshbuf_ptr()
291 return buf->cd + buf->off; in sshbuf_ptr()
295 sshbuf_mutable_ptr(const struct sshbuf *buf) in sshbuf_mutable_ptr() argument
297 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1) in sshbuf_mutable_ptr()
299 return buf->d + buf->off; in sshbuf_mutable_ptr()
303 sshbuf_check_reserve(const struct sshbuf *buf, size_t len) in sshbuf_check_reserve() argument
307 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_check_reserve()
309 if (buf->readonly || buf->refcount > 1) in sshbuf_check_reserve()
313 if (len > buf->max_size || buf->max_size - len < buf->size - buf->off) in sshbuf_check_reserve()
319 sshbuf_allocate(struct sshbuf *buf, size_t len) in sshbuf_allocate() argument
325 SSHBUF_DBG(("allocate buf = %p len = %zu", buf, len)); in sshbuf_allocate()
326 if ((r = sshbuf_check_reserve(buf, len)) != 0) in sshbuf_allocate()
332 sshbuf_maybe_pack(buf, buf->size + len > buf->max_size); in sshbuf_allocate()
334 if (len + buf->size <= buf->alloc) in sshbuf_allocate()
341 need = len + buf->size - buf->alloc; in sshbuf_allocate()
342 rlen = ROUNDUP(buf->alloc + need, SSHBUF_SIZE_INC); in sshbuf_allocate()
344 if (rlen > buf->max_size) in sshbuf_allocate()
345 rlen = buf->alloc + need; in sshbuf_allocate()
347 if ((dp = realloc(buf->d, rlen)) == NULL) { in sshbuf_allocate()
351 buf->alloc = rlen; in sshbuf_allocate()
352 buf->cd = buf->d = dp; in sshbuf_allocate()
353 if ((r = sshbuf_check_reserve(buf, len)) < 0) { in sshbuf_allocate()
362 sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp) in sshbuf_reserve() argument
370 SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len)); in sshbuf_reserve()
371 if ((r = sshbuf_allocate(buf, len)) != 0) in sshbuf_reserve()
374 dp = buf->d + buf->size; in sshbuf_reserve()
375 buf->size += len; in sshbuf_reserve()
382 sshbuf_consume(struct sshbuf *buf, size_t len) in sshbuf_consume() argument
387 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_consume()
391 if (len > sshbuf_len(buf)) in sshbuf_consume()
393 buf->off += len; in sshbuf_consume()
399 sshbuf_consume_end(struct sshbuf *buf, size_t len) in sshbuf_consume_end() argument
404 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_consume_end()
408 if (len > sshbuf_len(buf)) in sshbuf_consume_end()
410 buf->size -= len; in sshbuf_consume_end()