1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 03:22:48 -05:00

Use the BinarySink system for conf serialisation.

Now instead of iterating through conf twice in separate functions,
once to count up the size of the serialised data and once to write it
out, I just go through once and dump it all in a strbuf.

(Of course, I could still do a two-pass count-then-allocate approach
easily enough in this system; nothing would stop me writing a
BinarySink implementation that didn't actually store any data and just
counted its size, and then I could choose at each call site whether I
preferred to do it that way.)
This commit is contained in:
Simon Tatham
2018-05-24 10:48:20 +01:00
parent 81a04c4fe6
commit a990738aca
7 changed files with 54 additions and 119 deletions

View File

@ -2107,10 +2107,13 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
* config structure.
*/
SECURITY_ATTRIBUTES sa;
strbuf *serbuf;
void *p;
int size;
size = conf_serialised_size(conf);
serbuf = strbuf_new();
conf_serialise(BinarySink_UPCAST(serbuf), conf);
size = serbuf->len;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
@ -2122,10 +2125,12 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
if (filemap && filemap != INVALID_HANDLE_VALUE) {
p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, size);
if (p) {
conf_serialise(conf, p);
memcpy(p, serbuf->s, size);
UnmapViewOfFile(p);
}
}
strbuf_free(serbuf);
inherit_handles = TRUE;
cl = dupprintf("putty %s&%p:%u", argprefix,
filemap, (unsigned)size);

View File

@ -50,14 +50,9 @@ void filename_free(Filename *fn)
sfree(fn);
}
int filename_serialise(const Filename *f, void *vdata)
void filename_serialise(BinarySink *bs, const Filename *f)
{
char *data = (char *)vdata;
int len = strlen(f->path) + 1; /* include trailing NUL */
if (data) {
strcpy(data, f->path);
}
return len;
put_asciz(bs, f->path);
}
Filename *filename_deserialise(void *vdata, int maxsize, int *used)
{
@ -559,17 +554,12 @@ void fontspec_free(FontSpec *f)
sfree(f->name);
sfree(f);
}
int fontspec_serialise(FontSpec *f, void *vdata)
void fontspec_serialise(BinarySink *bs, FontSpec *f)
{
char *data = (char *)vdata;
int len = strlen(f->name) + 1; /* include trailing NUL */
if (data) {
strcpy(data, f->name);
PUT_32BIT_MSB_FIRST(data + len, f->isbold);
PUT_32BIT_MSB_FIRST(data + len + 4, f->height);
PUT_32BIT_MSB_FIRST(data + len + 8, f->charset);
}
return len + 12; /* also include three 4-byte ints */
put_asciz(bs, f->name);
put_uint32(bs, f->isbold);
put_uint32(bs, f->height);
put_uint32(bs, f->charset);
}
FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
{