diff --git a/misc.h b/misc.h index 5c8f982e..84643936 100644 --- a/misc.h +++ b/misc.h @@ -123,6 +123,8 @@ ptrlen bufchain_prefix(bufchain *ch); void bufchain_consume(bufchain *ch, size_t len); void bufchain_fetch(bufchain *ch, void *data, size_t len); void bufchain_fetch_consume(bufchain *ch, void *data, size_t len); +bool bufchain_try_consume(bufchain *ch, size_t len); +bool bufchain_try_fetch(bufchain *ch, void *data, size_t len); bool bufchain_try_fetch_consume(bufchain *ch, void *data, size_t len); size_t bufchain_fetch_consume_up_to(bufchain *ch, void *data, size_t len); void bufchain_set_callback_inner( diff --git a/utils/bufchain.c b/utils/bufchain.c index 60ebc2be..9b02c65f 100644 --- a/utils/bufchain.c +++ b/utils/bufchain.c @@ -153,6 +153,26 @@ void bufchain_fetch_consume(bufchain *ch, void *data, size_t len) bufchain_consume(ch, len); } +bool bufchain_try_fetch(bufchain *ch, void *data, size_t len) +{ + if (ch->buffersize >= len) { + bufchain_fetch(ch, data, len); + return true; + } else { + return false; + } +} + +bool bufchain_try_consume(bufchain *ch, size_t len) +{ + if (ch->buffersize >= len) { + bufchain_consume(ch, len); + return true; + } else { + return false; + } +} + bool bufchain_try_fetch_consume(bufchain *ch, void *data, size_t len) { if (ch->buffersize >= len) {