Lines Matching defs:buf

33 		    buf->size, buf->alloc, buf->off, buf->max_size); \
43 size_t off; /* First available byte is buf->d + buf->off */
44 size_t size; /* Last byte is buf->d + buf->size - 1 */
46 size_t alloc; /* Total bytes allocated to buf->d */
53 sshbuf_check_sanity(const struct sshbuf *buf)
56 if (__predict_false(buf == NULL ||
57 (!buf->readonly && buf->d != buf->cd) ||
58 buf->parent == buf ||
59 buf->refcount < 1 || buf->refcount > SSHBUF_REFS_MAX ||
60 buf->cd == NULL ||
61 buf->max_size > SSHBUF_SIZE_MAX ||
62 buf->alloc > buf->max_size ||
63 buf->size > buf->alloc ||
64 buf->off > buf->size)) {
75 sshbuf_maybe_pack(struct sshbuf *buf, int force)
79 if (buf->off == 0 || buf->readonly || buf->refcount > 1)
82 (buf->off >= SSHBUF_PACK_MIN && buf->off >= buf->size / 2)) {
83 memmove(buf->d, buf->d + buf->off, buf->size - buf->off);
84 buf->size -= buf->off;
85 buf->off = 0;
143 sshbuf_fromb(struct sshbuf *buf)
147 if (sshbuf_check_sanity(buf) != 0)
149 if ((ret = sshbuf_from(sshbuf_ptr(buf), sshbuf_len(buf))) == NULL)
151 if (sshbuf_set_parent(ret, buf) != 0) {
159 sshbuf_free(struct sshbuf *buf)
161 if (buf == NULL)
169 if (sshbuf_check_sanity(buf) != 0)
177 buf->refcount--;
178 if (buf->refcount > 0)
185 sshbuf_free(buf->parent);
186 buf->parent = NULL;
188 if (!buf->readonly)
189 freezero(buf->d, buf->alloc);
190 freezero(buf, sizeof(*buf));
194 sshbuf_reset(struct sshbuf *buf)
198 if (buf->readonly || buf->refcount > 1) {
200 buf->off = buf->size;
203 if (sshbuf_check_sanity(buf) != 0)
205 buf->off = buf->size = 0;
206 if (buf->alloc != SSHBUF_SIZE_INIT) {
207 if ((d = recallocarray(buf->d, buf->alloc, SSHBUF_SIZE_INIT,
209 buf->cd = buf->d = d;
210 buf->alloc = SSHBUF_SIZE_INIT;
213 explicit_bzero(buf->d, buf->alloc);
217 sshbuf_max_size(const struct sshbuf *buf)
219 return buf->max_size;
223 sshbuf_alloc(const struct sshbuf *buf)
225 return buf->alloc;
229 sshbuf_parent(const struct sshbuf *buf)
231 return buf->parent;
235 sshbuf_refcount(const struct sshbuf *buf)
237 return buf->refcount;
241 sshbuf_set_max_size(struct sshbuf *buf, size_t max_size)
247 SSHBUF_DBG(("set max buf = %p len = %zu", buf, max_size));
248 if ((r = sshbuf_check_sanity(buf)) != 0)
250 if (max_size == buf->max_size)
252 if (buf->readonly || buf->refcount > 1)
257 sshbuf_maybe_pack(buf, max_size < buf->size);
258 if (max_size < buf->alloc && max_size > buf->size) {
259 if (buf->size < SSHBUF_SIZE_INIT)
262 rlen = ROUNDUP(buf->size, SSHBUF_SIZE_INC);
266 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL)
268 buf->cd = buf->d = dp;
269 buf->alloc = rlen;
272 if (max_size < buf->alloc)
274 buf->max_size = max_size;
279 sshbuf_len(const struct sshbuf *buf)
281 if (sshbuf_check_sanity(buf) != 0)
283 return buf->size - buf->off;
287 sshbuf_avail(const struct sshbuf *buf)
289 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1)
291 return buf->max_size - (buf->size - buf->off);
295 sshbuf_ptr(const struct sshbuf *buf)
297 if (sshbuf_check_sanity(buf) != 0)
299 return buf->cd + buf->off;
303 sshbuf_mutable_ptr(const struct sshbuf *buf)
305 if (sshbuf_check_sanity(buf) != 0 || buf->readonly || buf->refcount > 1)
307 return buf->d + buf->off;
311 sshbuf_check_reserve(const struct sshbuf *buf, size_t len)
315 if ((r = sshbuf_check_sanity(buf)) != 0)
317 if (buf->readonly || buf->refcount > 1)
321 if (len > buf->max_size || buf->max_size - len < buf->size - buf->off)
327 sshbuf_allocate(struct sshbuf *buf, size_t len)
333 SSHBUF_DBG(("allocate buf = %p len = %zu", buf, len));
334 if ((r = sshbuf_check_reserve(buf, len)) != 0)
338 * then pack the buffer, zeroing buf->off.
340 sshbuf_maybe_pack(buf, buf->size + len > buf->max_size);
342 if (len + buf->size <= buf->alloc)
349 need = len + buf->size - buf->alloc;
350 rlen = ROUNDUP(buf->alloc + need, SSHBUF_SIZE_INC);
352 if (rlen > buf->max_size)
353 rlen = buf->alloc + need;
355 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL) {
359 buf->alloc = rlen;
360 buf->cd = buf->d = dp;
361 if ((r = sshbuf_check_reserve(buf, len)) < 0) {
370 sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp)
378 SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len));
379 if ((r = sshbuf_allocate(buf, len)) != 0)
382 dp = buf->d + buf->size;
383 buf->size += len;
390 sshbuf_consume(struct sshbuf *buf, size_t len)
395 if ((r = sshbuf_check_sanity(buf)) != 0)
399 if (len > sshbuf_len(buf))
401 buf->off += len;
403 if (buf->off == buf->size)
404 buf->off = buf->size = 0;
410 sshbuf_consume_end(struct sshbuf *buf, size_t len)
415 if ((r = sshbuf_check_sanity(buf)) != 0)
419 if (len > sshbuf_len(buf))
421 buf->size -= len;