1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-06-30 19:12:48 -05:00

Implement a BinarySink writing to a fixed-size buffer.

This is one of marshal.c's small collection of handy BinarySink
adapters to existing kinds of thing, alongside stdio_sink and
bufchain_sink. It writes into a fixed-size buffer, discarding all
writes after the buffer fills up, and sets a flag to let you know if
it overflowed.

There was one of these in Windows Pageant a while back, under the name
'struct PageantReply' (introduced in commit b6cbad89fc, removed
again in 98538caa39 when the named-pipe revamp made it
unnecessary). This is the same idea but centralised for reusability.
This commit is contained in:
Simon Tatham
2022-11-09 18:53:34 +00:00
parent c8ba48be43
commit 991e22c9bb
3 changed files with 28 additions and 0 deletions

View File

@ -336,3 +336,23 @@ void bufchain_sink_init(bufchain_sink *sink, bufchain *ch)
sink->ch = ch;
BinarySink_INIT(sink, bufchain_sink_write);
}
static void buffer_sink_write(BinarySink *bs, const void *data, size_t len)
{
buffer_sink *sink = BinarySink_DOWNCAST(bs, buffer_sink);
if (len > sink->space) {
len = sink->space;
sink->overflowed = true;
}
memcpy(sink->out, data, len);
sink->space -= len;
sink->out += len;
}
void buffer_sink_init(buffer_sink *sink, void *buffer, size_t len)
{
sink->out = buffer;
sink->space = len;
sink->overflowed = false;
BinarySink_INIT(sink, buffer_sink_write);
}