1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00: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

1
defs.h
View File

@ -91,6 +91,7 @@ typedef struct BinarySink BinarySink;
typedef struct BinarySource BinarySource;
typedef struct stdio_sink stdio_sink;
typedef struct bufchain_sink bufchain_sink;
typedef struct buffer_sink buffer_sink;
typedef struct handle_sink handle_sink;
typedef struct IdempotentCallback IdempotentCallback;

View File

@ -353,7 +353,14 @@ struct bufchain_sink {
bufchain *ch;
BinarySink_IMPLEMENTATION;
};
struct buffer_sink {
char *out;
size_t space;
bool overflowed;
BinarySink_IMPLEMENTATION;
};
void stdio_sink_init(stdio_sink *sink, FILE *fp);
void bufchain_sink_init(bufchain_sink *sink, bufchain *ch);
void buffer_sink_init(buffer_sink *sink, void *buffer, size_t len);
#endif /* PUTTY_MARSHAL_H */

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);
}