1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 11:32: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

@ -684,11 +684,11 @@ static char *ecc_cache_str_shared(
strbuf *sb = strbuf_new();
if (curve_name)
strbuf_catf(sb, "%s,", curve_name);
put_fmt(sb, "%s,", curve_name);
char *hx = mp_get_hex(x);
char *hy = mp_get_hex(y);
strbuf_catf(sb, "0x%s,0x%s", hx, hy);
put_fmt(sb, "0x%s,0x%s", hx, hy);
sfree(hx);
sfree(hy);

View File

@ -42,20 +42,20 @@ static ssh2_mac *hmac_new(const ssh2_macalg *alg, ssh_cipher *cipher)
ctx->digest = snewn(ctx->hashalg->hlen, uint8_t);
ctx->text_name = strbuf_new();
strbuf_catf(ctx->text_name, "HMAC-%s%s",
ctx->hashalg->text_basename, extra->suffix);
put_fmt(ctx->text_name, "HMAC-%s%s",
ctx->hashalg->text_basename, extra->suffix);
if (extra->annotation || ctx->hashalg->annotation) {
strbuf_catf(ctx->text_name, " (");
put_fmt(ctx->text_name, " (");
const char *sep = "";
if (extra->annotation) {
strbuf_catf(ctx->text_name, "%s%s", sep, extra->annotation);
put_fmt(ctx->text_name, "%s%s", sep, extra->annotation);
sep = ", ";
}
if (ctx->hashalg->annotation) {
strbuf_catf(ctx->text_name, "%s%s", sep, ctx->hashalg->annotation);
put_fmt(ctx->text_name, "%s%s", sep, ctx->hashalg->annotation);
sep = ", ";
}
strbuf_catf(ctx->text_name, ")");
put_fmt(ctx->text_name, ")");
}
ctx->mac.vt = alg;

View File

@ -311,11 +311,11 @@ char *rsa_ssh1_fingerprint(RSAKey *key)
ssh_hash_final(hash, digest);
out = strbuf_new();
strbuf_catf(out, "%"SIZEu" ", mp_get_nbits(key->modulus));
put_fmt(out, "%"SIZEu" ", mp_get_nbits(key->modulus));
for (i = 0; i < 16; i++)
strbuf_catf(out, "%s%02x", i ? ":" : "", digest[i]);
put_fmt(out, "%s%02x", i ? ":" : "", digest[i]);
if (key->comment)
strbuf_catf(out, " %s", key->comment);
put_fmt(out, " %s", key->comment);
return strbuf_to_str(out);
}