From 9d96c3eb02121156002e75fd516b47dcb4cc59cb Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 18 May 2018 07:22:57 +0100 Subject: [PATCH] bufchain: new combined fetch + consume functions. bufchain_fetch_consume is a one-stop function that moves a given number of bytes out of the head of a bufchain into an output buffer, removing them from the bufchain in the process. That function will fail an assertion (just like bufchain_fetch) if the bufchain doesn't actually _have_ at least that many bytes to read, so I also provide bufchain_try_fetch_consume which will return a success or failure status. Nothing uses these functions yet, but they will. --- misc.c | 16 ++++++++++++++++ misc.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/misc.c b/misc.c index fb32da5d..08198004 100644 --- a/misc.c +++ b/misc.c @@ -751,6 +751,22 @@ void bufchain_fetch(bufchain *ch, void *data, int len) } } +void bufchain_fetch_consume(bufchain *ch, void *data, int len) +{ + bufchain_fetch(ch, data, len); + bufchain_consume(ch, len); +} + +int bufchain_try_fetch_consume(bufchain *ch, void *data, int len) +{ + if (ch->buffersize >= len) { + bufchain_fetch_consume(ch, data, len); + return TRUE; + } else { + return FALSE; + } +} + /* ---------------------------------------------------------------------- * My own versions of malloc, realloc and free. Because I want * malloc and realloc to bomb out and exit the program if they run diff --git a/misc.h b/misc.h index 32e7bc32..d2efdca7 100644 --- a/misc.h +++ b/misc.h @@ -82,6 +82,8 @@ void bufchain_add(bufchain *ch, const void *data, int len); void bufchain_prefix(bufchain *ch, void **data, int *len); void bufchain_consume(bufchain *ch, int len); void bufchain_fetch(bufchain *ch, void *data, int len); +void bufchain_fetch_consume(bufchain *ch, void *data, int len); +int bufchain_try_fetch_consume(bufchain *ch, void *data, int len); int validate_manual_hostkey(char *key);