upstream: Add sshbuf_consume_upto_child(), to similify particular

parsing patterns using parent/child buffer; ok markus@

OpenBSD-Commit-ID: c11ed27907751f2a16c1283313e77f88617e4852
This commit is contained in:
djm@openbsd.org
2025-12-29 23:52:09 +00:00
committed by Damien Miller
parent 6eafc52a41
commit 55b6b16974
2 changed files with 40 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: sshbuf.c,v 1.23 2024/08/14 15:42:18 tobias Exp $ */
/* $OpenBSD: sshbuf.c,v 1.24 2025/12/29 23:52:09 djm Exp $ */
/*
* Copyright (c) 2011 Damien Miller
*
@@ -425,3 +425,23 @@ sshbuf_consume_end(struct sshbuf *buf, size_t len)
return 0;
}
int
sshbuf_consume_upto_child(struct sshbuf *buf, const struct sshbuf *child)
{
int r;
if ((r = sshbuf_check_sanity(buf)) != 0 ||
(r = sshbuf_check_sanity(child)) != 0)
return r;
/* This function is only used for parent/child buffers */
if (child->parent != buf)
return SSH_ERR_INVALID_ARGUMENT;
/* Nonsensical if the parent has advanced past the child */
if (sshbuf_len(child) > sshbuf_len(buf))
return SSH_ERR_INVALID_ARGUMENT;
/* More paranoia, shouldn't happen */
if (child->cd < buf->cd)
return SSH_ERR_INTERNAL_ERROR;
/* Advance */
return sshbuf_consume(buf, sshbuf_len(buf) - sshbuf_len(child));
}

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: sshbuf.h,v 1.33 2025/11/21 01:29:06 djm Exp $ */
/* $OpenBSD: sshbuf.h,v 1.34 2025/12/29 23:52:09 djm Exp $ */
/*
* Copyright (c) 2011 Damien Miller
*
@@ -143,6 +143,24 @@ int sshbuf_consume(struct sshbuf *buf, size_t len);
*/
int sshbuf_consume_end(struct sshbuf *buf, size_t len);
/*
* Consume data from a parent buffer up to that of a child buffer (i.e.
* one created by sshbuf_fromb()).
*
* Intended to be used in a pattern like:
*
* b = sshbuf_fromb(parent);
* sshbuf_get_string(b, &foo, &foostr);
* sshbuf_get_u32(b, &bar);
* sshbuf_consume_upto_child(parent, b);
*
* After which, both "b" and "parent" will point to the same data.
*
* "child" must be a direct child of "buf" (i.e. neither an unrelated buffer
* nor a grandchild) which has consumed data past that of "buf".
*/
int sshbuf_consume_upto_child(struct sshbuf *buf, const struct sshbuf *child);
/* Extract or deposit some bytes */
int sshbuf_get(struct sshbuf *buf, void *v, size_t len);
int sshbuf_put(struct sshbuf *buf, const void *v, size_t len);