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

Generalise strbuf_catf() into put_fmt().

marshal.h now provides a macro put_fmt() which allows you to write
arbitrary printf-formatted data to an arbitrary BinarySink.

We already had this facility for strbufs in particular, in the form of
strbuf_catf(). That was able to take advantage of knowing the inner
structure of a strbuf to minimise memory allocation (it would snprintf
directly into the strbuf's existing buffer if possible). For a general
black-box BinarySink we can't do that, so instead we dupvprintf into a
temporary buffer.

For consistency, I've removed strbuf_catf, and converted all uses of
it into the new put_fmt - and I've also added an extra vtable method
in the BinarySink API, so that put_fmt can still use strbuf_catf's
more efficient memory management when talking to a strbuf, and fall
back to the simpler strategy when that's not available.
This commit is contained in:
Simon Tatham
2021-11-19 10:23:32 +00:00
parent efee4e0eae
commit be8d3974ff
24 changed files with 217 additions and 193 deletions

View File

@ -335,7 +335,7 @@ static void keylist_update_callback(
switch (key->ssh_version) {
case 1: {
strbuf_catf(listentry, "ssh1\t%s\t%s", fingerprint, comment);
put_fmt(listentry, "ssh1\t%s\t%s", fingerprint, comment);
/*
* Replace the space in the fingerprint (between bit count and
@ -390,15 +390,15 @@ static void keylist_update_callback(
put_byte(listentry, c);
}
strbuf_catf(listentry, "\t%s", comment);
put_fmt(listentry, "\t%s", comment);
break;
}
}
if (ext_flags & LIST_EXTENDED_FLAG_HAS_NO_CLEARTEXT_KEY) {
strbuf_catf(listentry, "\t(encrypted)");
put_fmt(listentry, "\t(encrypted)");
} else if (ext_flags & LIST_EXTENDED_FLAG_HAS_ENCRYPTED_KEY_FILE) {
strbuf_catf(listentry, "\t(re-encryptable)");
put_fmt(listentry, "\t(re-encryptable)");
/* At least one key can be re-encrypted */
ctx->enable_reencrypt_controls = true;

View File

@ -318,7 +318,7 @@ void enum_settings_finish(settings_e *e)
static void hostkey_regname(strbuf *sb, const char *hostname,
int port, const char *keytype)
{
strbuf_catf(sb, "%s@%d:", keytype, port);
put_fmt(sb, "%s@%d:", keytype, port);
escape_registry_key(hostname, sb);
}

View File

@ -514,7 +514,7 @@ int main(int argc, char **argv)
strbuf *cmdline = strbuf_new();
char *p;
strbuf_catf(cmdline, "%s -splat ", argv[0]);
put_fmt(cmdline, "%s -splat ", argv[0]);
printf(" {\"");
size_t args_start = cmdline->len;
for (p = argv[2]; *p; p++) {

View File

@ -4946,7 +4946,7 @@ static void wintw_clip_write(
get_unitab(CP_ACP, unitab, 0);
strbuf_catf(
put_fmt(
rtf, "{\\rtf1\\ansi\\deff0{\\fonttbl\\f0\\fmodern %s;}\\f0\\fs%d",
font->name, font->height*2);
@ -5034,16 +5034,16 @@ static void wintw_clip_write(
for (i = 0; i < OSC4_NCOLOURS; i++) {
if (palette[i] != 0) {
const PALETTEENTRY *pe = &logpal->palPalEntry[i];
strbuf_catf(rtf, "\\red%d\\green%d\\blue%d;",
pe->peRed, pe->peGreen, pe->peBlue);
put_fmt(rtf, "\\red%d\\green%d\\blue%d;",
pe->peRed, pe->peGreen, pe->peBlue);
}
}
if (rgbtree) {
rgbindex *rgbp;
for (i = 0; (rgbp = index234(rgbtree, i)) != NULL; i++)
strbuf_catf(rtf, "\\red%d\\green%d\\blue%d;",
GetRValue(rgbp->ref), GetGValue(rgbp->ref),
GetBValue(rgbp->ref));
put_fmt(rtf, "\\red%d\\green%d\\blue%d;",
GetRValue(rgbp->ref), GetGValue(rgbp->ref),
GetBValue(rgbp->ref));
}
put_datapl(rtf, PTRLEN_LITERAL("}"));
}
@ -5162,13 +5162,13 @@ static void wintw_clip_write(
lastfgcolour = fgcolour;
lastfg = fg;
if (fg == -1) {
strbuf_catf(rtf, "\\cf%d ",
(fgcolour >= 0) ? palette[fgcolour] : 0);
put_fmt(rtf, "\\cf%d ",
(fgcolour >= 0) ? palette[fgcolour] : 0);
} else {
rgbindex rgb, *rgbp;
rgb.ref = fg;
if ((rgbp = find234(rgbtree, &rgb, NULL)) != NULL)
strbuf_catf(rtf, "\\cf%d ", rgbp->index);
put_fmt(rtf, "\\cf%d ", rgbp->index);
}
}
@ -5176,13 +5176,13 @@ static void wintw_clip_write(
lastbgcolour = bgcolour;
lastbg = bg;
if (bg == -1)
strbuf_catf(rtf, "\\highlight%d ",
(bgcolour >= 0) ? palette[bgcolour] : 0);
put_fmt(rtf, "\\highlight%d ",
(bgcolour >= 0) ? palette[bgcolour] : 0);
else {
rgbindex rgb, *rgbp;
rgb.ref = bg;
if ((rgbp = find234(rgbtree, &rgb, NULL)) != NULL)
strbuf_catf(rtf, "\\highlight%d ", rgbp->index);
put_fmt(rtf, "\\highlight%d ", rgbp->index);
}
}
@ -5243,7 +5243,7 @@ static void wintw_clip_write(
} else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A) {
put_datapl(rtf, PTRLEN_LITERAL("\\par\r\n"));
} else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20) {
strbuf_catf(rtf, "\\'%02x", tdata[tindex+i]);
put_fmt(rtf, "\\'%02x", tdata[tindex+i]);
} else {
put_byte(rtf, tdata[tindex+i]);
}