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->max_size > SSHBUF_SIZE_MAX || in sshbuf_check_sanity()
40 buf->alloc > buf->max_size || in sshbuf_check_sanity()
41 buf->size > buf->alloc || in sshbuf_check_sanity()
42 buf->off > buf->size)) { in sshbuf_check_sanity()
53 sshbuf_maybe_pack(struct sshbuf *buf, int force) in sshbuf_maybe_pack() argument
57 if (buf->off == 0 || buf->readonly || buf->refcount > 1) in sshbuf_maybe_pack()
60 (buf->off >= SSHBUF_PACK_MIN && buf->off >= buf->size / 2)) { in sshbuf_maybe_pack()
61 memmove(buf->d, buf->d + buf->off, buf->size - buf->off); in sshbuf_maybe_pack()
62 buf->size -= buf->off; in sshbuf_maybe_pack()
63 buf->off = 0; in sshbuf_maybe_pack()
118 sshbuf_fromb(struct sshbuf *buf) in sshbuf_fromb() argument
122 if (sshbuf_check_sanity(buf) != 0) in sshbuf_fromb()
124 if ((ret = sshbuf_from(sshbuf_ptr(buf), sshbuf_len(buf))) == NULL) in sshbuf_fromb()
126 if (sshbuf_set_parent(ret, buf) != 0) { in sshbuf_fromb()
134 sshbuf_free(struct sshbuf *buf) in sshbuf_free() argument
136 if (buf == NULL) in sshbuf_free()
144 if (sshbuf_check_sanity(buf) != 0) in sshbuf_free()
152 buf->refcount--; in sshbuf_free()
153 if (buf->refcount > 0) in sshbuf_free()
160 sshbuf_free(buf->parent); in sshbuf_free()
161 buf->parent = NULL; in sshbuf_free()
163 if (!buf->readonly) { in sshbuf_free()
164 explicit_bzero(buf->d, buf->alloc); in sshbuf_free()
165 free(buf->d); in sshbuf_free()
167 freezero(buf, sizeof(*buf)); in sshbuf_free()
171 sshbuf_reset(struct sshbuf *buf) in sshbuf_reset() argument
175 if (buf->readonly || buf->refcount > 1) { in sshbuf_reset()
177 buf->off = buf->size; in sshbuf_reset()
180 (void) sshbuf_check_sanity(buf); in sshbuf_reset()
181 buf->off = buf->size = 0; in sshbuf_reset()
182 if (buf->alloc != SSHBUF_SIZE_INIT) { in sshbuf_reset()
183 if ((d = recallocarray(buf->d, buf->alloc, SSHBUF_SIZE_INIT, in sshbuf_reset()
185 buf->cd = buf->d = d; in sshbuf_reset()
186 buf->alloc = SSHBUF_SIZE_INIT; in sshbuf_reset()
189 explicit_bzero(buf->d, SSHBUF_SIZE_INIT); in sshbuf_reset()
193 sshbuf_max_size(const struct sshbuf *buf) in sshbuf_max_size() argument
195 return buf->max_size; in sshbuf_max_size()
199 sshbuf_alloc(const struct sshbuf *buf) in sshbuf_alloc() argument
201 return buf->alloc; in sshbuf_alloc()
205 sshbuf_parent(const struct sshbuf *buf) in sshbuf_parent() argument
207 return buf->parent; in sshbuf_parent()
211 sshbuf_refcount(const struct sshbuf *buf) in sshbuf_refcount() argument
213 return buf->refcount; in sshbuf_refcount()
217 sshbuf_set_max_size(struct sshbuf *buf, size_t max_size) in sshbuf_set_max_size() argument
223 SSHBUF_DBG(("set max buf = %p len = %zu", buf, max_size)); in sshbuf_set_max_size()
224 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_set_max_size()
226 if (max_size == buf->max_size) in sshbuf_set_max_size()
228 if (buf->readonly || buf->refcount > 1) in sshbuf_set_max_size()
233 sshbuf_maybe_pack(buf, max_size < buf->size); in sshbuf_set_max_size()
234 if (max_size < buf->alloc && max_size > buf->size) { in sshbuf_set_max_size()
235 if (buf->size < SSHBUF_SIZE_INIT) in sshbuf_set_max_size()
238 rlen = ROUNDUP(buf->size, SSHBUF_SIZE_INC); in sshbuf_set_max_size()
242 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL) in sshbuf_set_max_size()
244 buf->cd = buf->d = dp; in sshbuf_set_max_size()
245 buf->alloc = rlen; in sshbuf_set_max_size()
248 if (max_size < buf->alloc) in sshbuf_set_max_size()
250 buf->max_size = max_size; in sshbuf_set_max_size()
255 sshbuf_len(const struct sshbuf *buf) in sshbuf_len() argument
257 if (sshbuf_check_sanity(buf) != 0) in sshbuf_len()
259 return buf->size - buf->off; in sshbuf_len()
263 sshbuf_avail(const struct sshbuf *buf) in sshbuf_avail() argument
265 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1) in sshbuf_avail()
267 return buf->max_size - (buf->size - buf->off); in sshbuf_avail()
271 sshbuf_ptr(const struct sshbuf *buf) in sshbuf_ptr() argument
273 if (sshbuf_check_sanity(buf) != 0) in sshbuf_ptr()
275 return buf->cd + buf->off; in sshbuf_ptr()
279 sshbuf_mutable_ptr(const struct sshbuf *buf) in sshbuf_mutable_ptr() argument
281 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1) in sshbuf_mutable_ptr()
283 return buf->d + buf->off; in sshbuf_mutable_ptr()
287 sshbuf_check_reserve(const struct sshbuf *buf, size_t len) in sshbuf_check_reserve() argument
291 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_check_reserve()
293 if (buf->readonly || buf->refcount > 1) in sshbuf_check_reserve()
297 if (len > buf->max_size || buf->max_size - len < buf->size - buf->off) in sshbuf_check_reserve()
303 sshbuf_allocate(struct sshbuf *buf, size_t len) in sshbuf_allocate() argument
309 SSHBUF_DBG(("allocate buf = %p len = %zu", buf, len)); in sshbuf_allocate()
310 if ((r = sshbuf_check_reserve(buf, len)) != 0) in sshbuf_allocate()
316 sshbuf_maybe_pack(buf, buf->size + len > buf->max_size); in sshbuf_allocate()
318 if (len + buf->size <= buf->alloc) in sshbuf_allocate()
325 need = len + buf->size - buf->alloc; in sshbuf_allocate()
326 rlen = ROUNDUP(buf->alloc + need, SSHBUF_SIZE_INC); in sshbuf_allocate()
328 if (rlen > buf->max_size) in sshbuf_allocate()
329 rlen = buf->alloc + need; in sshbuf_allocate()
331 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL) { in sshbuf_allocate()
335 buf->alloc = rlen; in sshbuf_allocate()
336 buf->cd = buf->d = dp; in sshbuf_allocate()
337 if ((r = sshbuf_check_reserve(buf, len)) < 0) { in sshbuf_allocate()
346 sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp) in sshbuf_reserve() argument
354 SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len)); in sshbuf_reserve()
355 if ((r = sshbuf_allocate(buf, len)) != 0) in sshbuf_reserve()
358 dp = buf->d + buf->size; in sshbuf_reserve()
359 buf->size += len; in sshbuf_reserve()
366 sshbuf_consume(struct sshbuf *buf, size_t len) in sshbuf_consume() argument
371 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_consume()
375 if (len > sshbuf_len(buf)) in sshbuf_consume()
377 buf->off += len; in sshbuf_consume()
379 if (buf->off == buf->size) in sshbuf_consume()
380 buf->off = buf->size = 0; in sshbuf_consume()
386 sshbuf_consume_end(struct sshbuf *buf, size_t len) in sshbuf_consume_end() argument
391 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_consume_end()
395 if (len > sshbuf_len(buf)) in sshbuf_consume_end()
397 buf->size -= len; in sshbuf_consume_end()