1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-06-30 19:12: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

@ -3615,12 +3615,12 @@ int gtk_seat_confirm_ssh_host_key(
strbuf *sb = strbuf_new();
if (fingerprints[SSH_FPTYPE_SHA256])
strbuf_catf(sb, "SHA256 fingerprint: %s\n",
fingerprints[SSH_FPTYPE_SHA256]);
put_fmt(sb, "SHA256 fingerprint: %s\n",
fingerprints[SSH_FPTYPE_SHA256]);
if (fingerprints[SSH_FPTYPE_MD5])
strbuf_catf(sb, "MD5 fingerprint: %s\n",
fingerprints[SSH_FPTYPE_MD5]);
strbuf_catf(sb, "Full text of host's public key:");
put_fmt(sb, "MD5 fingerprint: %s\n",
fingerprints[SSH_FPTYPE_MD5]);
put_fmt(sb, "Full text of host's public key:");
/* We have to manually wrap the public key, or else the GtkLabel
* will resize itself to accommodate the longest word, which will
* lead to a hilariously wide message box. */
@ -3943,12 +3943,12 @@ static void eventlog_list_handler(union control *ctrl, dlgparam *dp,
strbuf_clear(es->seldata);
for (i = 0; i < es->ninitial; i++) {
if (dlg_listbox_issel(ctrl, dp, i))
strbuf_catf(es->seldata, "%s\n", es->events_initial[i]);
put_fmt(es->seldata, "%s\n", es->events_initial[i]);
}
for (i = 0; i < es->ncircular; i++) {
if (dlg_listbox_issel(ctrl, dp, es->ninitial + i)) {
int j = (es->circular_first + i) % LOGEVENT_CIRCULAR_MAX;
strbuf_catf(es->seldata, "%s\n", es->events_circular[j]);
put_fmt(es->seldata, "%s\n", es->events_circular[j]);
}
}

View File

@ -232,9 +232,9 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_fami
ret->superfamily = IP;
if (ret->ais->ai_canonname != NULL)
strbuf_catf(realhost, "%s", ret->ais->ai_canonname);
put_fmt(realhost, "%s", ret->ais->ai_canonname);
else
strbuf_catf(realhost, "%s", host);
put_fmt(realhost, "%s", host);
#else
if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) {
/*
@ -258,7 +258,7 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_fami
}
/* This way we are always sure the h->h_name is valid :) */
strbuf_clear(realhost);
strbuf_catf(realhost, "%s", h->h_name);
put_fmt(realhost, "%s", h->h_name);
for (n = 0; h->h_addr_list[n]; n++);
ret->addresses = snewn(n, unsigned long);
ret->naddresses = n;
@ -273,7 +273,7 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_fami
*/
ret->superfamily = IP;
strbuf_clear(realhost);
strbuf_catf(realhost, "%s", host);
put_fmt(realhost, "%s", host);
ret->addresses = snew(unsigned long);
ret->naddresses = 1;
ret->addresses[0] = ntohl(a);

View File

@ -170,8 +170,8 @@ static char *format_sockaddr(const void *addr, int family)
const uint32_t *addrwords = (const uint32_t *)a->sin6_addr.s6_addr;
for (int i = 0; i < 4; i++)
strbuf_catf(sb, "%08X", addrwords[i]);
strbuf_catf(sb, ":%04X", ntohs(a->sin6_port));
put_fmt(sb, "%08X", addrwords[i]);
put_fmt(sb, ":%04X", ntohs(a->sin6_port));
return strbuf_to_str(sb);
} else {

View File

@ -173,7 +173,7 @@ static char *make_filename(int index, const char *subname)
if (index == INDEX_SESSION) {
strbuf *sb = strbuf_new();
tmp = make_filename(INDEX_SESSIONDIR, NULL);
strbuf_catf(sb, "%s/", tmp);
put_fmt(sb, "%s/", tmp);
sfree(tmp);
make_session_filename(subname, sb);
return strbuf_to_str(sb);

View File

@ -1904,19 +1904,19 @@ static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
/* Weight: normal, then lighter, then bolder */
if (weight <= PANGO_WEIGHT_NORMAL)
weight = PANGO_WEIGHT_NORMAL - weight;
strbuf_catf(buf, "%4d", weight);
put_fmt(buf, "%4d", weight);
strbuf_catf(buf, " %2d",
pango_font_description_get_style(desc));
put_fmt(buf, " %2d",
pango_font_description_get_style(desc));
int stretch = pango_font_description_get_stretch(desc);
/* Stretch: closer to normal sorts earlier */
stretch = 2 * abs(PANGO_STRETCH_NORMAL - stretch) +
(stretch < PANGO_STRETCH_NORMAL);
strbuf_catf(buf, " %2d", stretch);
put_fmt(buf, " %2d", stretch);
strbuf_catf(buf, " %2d",
pango_font_description_get_variant(desc));
put_fmt(buf, " %2d",
pango_font_description_get_variant(desc));
stylekey = strbuf_to_str(buf);
}