1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-02 03:52:49 -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

@ -1550,33 +1550,33 @@ strbuf *ppk_save_sb(ssh2_userkey *key, const char *passphrase,
}
strbuf *out = strbuf_new_nm();
strbuf_catf(out, "PuTTY-User-Key-File-%u: %s\n",
params.fmt_version, ssh_key_ssh_id(key->key));
strbuf_catf(out, "Encryption: %s\n", cipherstr);
strbuf_catf(out, "Comment: %s\n", key->comment);
strbuf_catf(out, "Public-Lines: %d\n", base64_lines(pub_blob->len));
put_fmt(out, "PuTTY-User-Key-File-%u: %s\n",
params.fmt_version, ssh_key_ssh_id(key->key));
put_fmt(out, "Encryption: %s\n", cipherstr);
put_fmt(out, "Comment: %s\n", key->comment);
put_fmt(out, "Public-Lines: %d\n", base64_lines(pub_blob->len));
base64_encode_s(BinarySink_UPCAST(out), pub_blob->u, pub_blob->len, 64);
if (params.fmt_version == 3 && ciphertype->keylen != 0) {
strbuf_catf(out, "Key-Derivation: %s\n",
params.argon2_flavour == Argon2d ? "Argon2d" :
params.argon2_flavour == Argon2i ? "Argon2i" : "Argon2id");
strbuf_catf(out, "Argon2-Memory: %"PRIu32"\n", params.argon2_mem);
put_fmt(out, "Key-Derivation: %s\n",
params.argon2_flavour == Argon2d ? "Argon2d" :
params.argon2_flavour == Argon2i ? "Argon2i" : "Argon2id");
put_fmt(out, "Argon2-Memory: %"PRIu32"\n", params.argon2_mem);
assert(!params.argon2_passes_auto);
strbuf_catf(out, "Argon2-Passes: %"PRIu32"\n", params.argon2_passes);
strbuf_catf(out, "Argon2-Parallelism: %"PRIu32"\n",
params.argon2_parallelism);
strbuf_catf(out, "Argon2-Salt: ");
put_fmt(out, "Argon2-Passes: %"PRIu32"\n", params.argon2_passes);
put_fmt(out, "Argon2-Parallelism: %"PRIu32"\n",
params.argon2_parallelism);
put_fmt(out, "Argon2-Salt: ");
for (size_t i = 0; i < passphrase_salt->len; i++)
strbuf_catf(out, "%02x", passphrase_salt->u[i]);
strbuf_catf(out, "\n");
put_fmt(out, "%02x", passphrase_salt->u[i]);
put_fmt(out, "\n");
}
strbuf_catf(out, "Private-Lines: %d\n", base64_lines(priv_encrypted_len));
put_fmt(out, "Private-Lines: %d\n", base64_lines(priv_encrypted_len));
base64_encode_s(BinarySink_UPCAST(out),
priv_blob_encrypted, priv_encrypted_len, 64);
strbuf_catf(out, "Private-MAC: ");
put_fmt(out, "Private-MAC: ");
for (i = 0; i < macalg->len; i++)
strbuf_catf(out, "%02x", priv_mac[i]);
strbuf_catf(out, "\n");
put_fmt(out, "%02x", priv_mac[i]);
put_fmt(out, "\n");
strbuf_free(cipher_mac_keys_blob);
strbuf_free(passphrase_salt);
@ -1740,7 +1740,7 @@ static void ssh2_fingerprint_blob_md5(ptrlen blob, strbuf *sb)
hash_simple(&ssh_md5, blob, digest);
for (unsigned i = 0; i < 16; i++)
strbuf_catf(sb, "%02x%s", digest[i], i==15 ? "" : ":");
put_fmt(sb, "%02x%s", digest[i], i==15 ? "" : ":");
}
static void ssh2_fingerprint_blob_sha256(ptrlen blob, strbuf *sb)
@ -1778,9 +1778,9 @@ char *ssh2_fingerprint_blob(ptrlen blob, FingerprintType fptype)
const ssh_keyalg *alg = find_pubkey_alg_len(algname);
if (alg) {
int bits = ssh_key_public_bits(alg, blob);
strbuf_catf(sb, "%.*s %d ", PTRLEN_PRINTF(algname), bits);
put_fmt(sb, "%.*s %d ", PTRLEN_PRINTF(algname), bits);
} else {
strbuf_catf(sb, "%.*s ", PTRLEN_PRINTF(algname));
put_fmt(sb, "%.*s ", PTRLEN_PRINTF(algname));
}
}