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()
150 sshbuf_free(buf->parent); in sshbuf_free()
151 buf->parent = NULL; in sshbuf_free()
157 buf->refcount--; in sshbuf_free()
158 if (buf->refcount > 0) in sshbuf_free()
160 if (!buf->readonly) { in sshbuf_free()
161 explicit_bzero(buf->d, buf->alloc); in sshbuf_free()
162 free(buf->d); in sshbuf_free()
164 explicit_bzero(buf, sizeof(*buf)); in sshbuf_free()
165 free(buf); in sshbuf_free()
169 sshbuf_reset(struct sshbuf *buf) in sshbuf_reset() argument
173 if (buf->readonly || buf->refcount > 1) { in sshbuf_reset()
175 buf->off = buf->size; in sshbuf_reset()
178 (void) sshbuf_check_sanity(buf); in sshbuf_reset()
179 buf->off = buf->size = 0; in sshbuf_reset()
180 if (buf->alloc != SSHBUF_SIZE_INIT) { in sshbuf_reset()
181 if ((d = recallocarray(buf->d, buf->alloc, SSHBUF_SIZE_INIT, in sshbuf_reset()
183 buf->cd = buf->d = d; in sshbuf_reset()
184 buf->alloc = SSHBUF_SIZE_INIT; in sshbuf_reset()
187 explicit_bzero(buf->d, SSHBUF_SIZE_INIT); in sshbuf_reset()
191 sshbuf_max_size(const struct sshbuf *buf) in sshbuf_max_size() argument
193 return buf->max_size; in sshbuf_max_size()
197 sshbuf_alloc(const struct sshbuf *buf) in sshbuf_alloc() argument
199 return buf->alloc; in sshbuf_alloc()
203 sshbuf_parent(const struct sshbuf *buf) in sshbuf_parent() argument
205 return buf->parent; in sshbuf_parent()
209 sshbuf_refcount(const struct sshbuf *buf) in sshbuf_refcount() argument
211 return buf->refcount; in sshbuf_refcount()
215 sshbuf_set_max_size(struct sshbuf *buf, size_t max_size) in sshbuf_set_max_size() argument
221 SSHBUF_DBG(("set max buf = %p len = %zu", buf, max_size)); in sshbuf_set_max_size()
222 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_set_max_size()
224 if (max_size == buf->max_size) in sshbuf_set_max_size()
226 if (buf->readonly || buf->refcount > 1) in sshbuf_set_max_size()
231 sshbuf_maybe_pack(buf, max_size < buf->size); in sshbuf_set_max_size()
232 if (max_size < buf->alloc && max_size > buf->size) { in sshbuf_set_max_size()
233 if (buf->size < SSHBUF_SIZE_INIT) in sshbuf_set_max_size()
236 rlen = ROUNDUP(buf->size, SSHBUF_SIZE_INC); in sshbuf_set_max_size()
240 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL) in sshbuf_set_max_size()
242 buf->cd = buf->d = dp; in sshbuf_set_max_size()
243 buf->alloc = rlen; in sshbuf_set_max_size()
246 if (max_size < buf->alloc) in sshbuf_set_max_size()
248 buf->max_size = max_size; in sshbuf_set_max_size()
253 sshbuf_len(const struct sshbuf *buf) in sshbuf_len() argument
255 if (sshbuf_check_sanity(buf) != 0) in sshbuf_len()
257 return buf->size - buf->off; in sshbuf_len()
261 sshbuf_avail(const struct sshbuf *buf) in sshbuf_avail() argument
263 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1) in sshbuf_avail()
265 return buf->max_size - (buf->size - buf->off); in sshbuf_avail()
269 sshbuf_ptr(const struct sshbuf *buf) in sshbuf_ptr() argument
271 if (sshbuf_check_sanity(buf) != 0) in sshbuf_ptr()
273 return buf->cd + buf->off; in sshbuf_ptr()
277 sshbuf_mutable_ptr(const struct sshbuf *buf) in sshbuf_mutable_ptr() argument
279 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1) in sshbuf_mutable_ptr()
281 return buf->d + buf->off; in sshbuf_mutable_ptr()
285 sshbuf_check_reserve(const struct sshbuf *buf, size_t len) in sshbuf_check_reserve() argument
289 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_check_reserve()
291 if (buf->readonly || buf->refcount > 1) in sshbuf_check_reserve()
295 if (len > buf->max_size || buf->max_size - len < buf->size - buf->off) in sshbuf_check_reserve()
301 sshbuf_allocate(struct sshbuf *buf, size_t len) in sshbuf_allocate() argument
307 SSHBUF_DBG(("allocate buf = %p len = %zu", buf, len)); in sshbuf_allocate()
308 if ((r = sshbuf_check_reserve(buf, len)) != 0) in sshbuf_allocate()
314 sshbuf_maybe_pack(buf, buf->size + len > buf->max_size); in sshbuf_allocate()
316 if (len + buf->size <= buf->alloc) in sshbuf_allocate()
323 need = len + buf->size - buf->alloc; in sshbuf_allocate()
324 rlen = ROUNDUP(buf->alloc + need, SSHBUF_SIZE_INC); in sshbuf_allocate()
326 if (rlen > buf->max_size) in sshbuf_allocate()
327 rlen = buf->alloc + need; in sshbuf_allocate()
329 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL) { in sshbuf_allocate()
333 buf->alloc = rlen; in sshbuf_allocate()
334 buf->cd = buf->d = dp; in sshbuf_allocate()
335 if ((r = sshbuf_check_reserve(buf, len)) < 0) { in sshbuf_allocate()
344 sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp) in sshbuf_reserve() argument
352 SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len)); in sshbuf_reserve()
353 if ((r = sshbuf_allocate(buf, len)) != 0) in sshbuf_reserve()
356 dp = buf->d + buf->size; in sshbuf_reserve()
357 buf->size += len; in sshbuf_reserve()
364 sshbuf_consume(struct sshbuf *buf, size_t len) in sshbuf_consume() argument
369 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_consume()
373 if (len > sshbuf_len(buf)) in sshbuf_consume()
375 buf->off += len; in sshbuf_consume()
377 if (buf->off == buf->size) in sshbuf_consume()
378 buf->off = buf->size = 0; in sshbuf_consume()
384 sshbuf_consume_end(struct sshbuf *buf, size_t len) in sshbuf_consume_end() argument
389 if ((r = sshbuf_check_sanity(buf)) != 0) in sshbuf_consume_end()
393 if (len > sshbuf_len(buf)) in sshbuf_consume_end()
395 buf->size -= len; in sshbuf_consume_end()