From d51b3d7eb63bf72a85950d008109201c0608e9dc Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Mon, 6 Jan 2020 19:55:41 +0000 Subject: [PATCH] Add BinarySource_REWIND_TO. A partial cherry-pick of commit 32d61d7c48b7cf0eb7445fb410e339bbcadd1d61 omitting the parts that aren't needed on this branch. --- marshal.c | 11 +++++++++++ marshal.h | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/marshal.c b/marshal.c index a9b1074a..1b35b455 100644 --- a/marshal.c +++ b/marshal.c @@ -235,6 +235,17 @@ ptrlen BinarySource_get_pstring(BinarySource *src) return make_ptrlen(consume(len), len); } +void BinarySource_REWIND_TO__(BinarySource *src, size_t pos) +{ + if (pos <= src->len) { + src->pos = pos; + src->err = BSE_NO_ERROR; /* clear any existing error */ + } else { + src->pos = src->len; + src->err = BSE_OUT_OF_DATA; /* new error if we rewind out of range */ + } +} + static void stdio_sink_write(BinarySink *bs, const void *data, size_t len) { stdio_sink *sink = BinarySink_DOWNCAST(bs, stdio_sink); diff --git a/marshal.h b/marshal.h index bc020a8d..5cbc0797 100644 --- a/marshal.h +++ b/marshal.h @@ -255,6 +255,10 @@ static inline void BinarySource_INIT__(BinarySource *src, ptrlen data) (object)->binarysource_) #define BinarySource_COPIED(obj) \ ((obj)->binarysource_->binarysource_ = (obj)->binarysource_) +#define BinarySource_REWIND_TO(src, pos) \ + BinarySource_REWIND_TO__((src)->binarysource_, pos) +#define BinarySource_REWIND(src) \ + BinarySource_REWIND_TO__((src)->binarysource_, 0) #define get_data(src, len) \ BinarySource_get_data(BinarySource_UPCAST(src), len) @@ -305,6 +309,8 @@ ptrlen BinarySource_get_pstring(BinarySource *); mp_int *BinarySource_get_mp_ssh1(BinarySource *src); mp_int *BinarySource_get_mp_ssh2(BinarySource *src); +void BinarySource_REWIND_TO__(BinarySource *src, size_t pos); + /* * A couple of useful standard BinarySink implementations, which live * as sensibly here as anywhere else: one that makes a BinarySink