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

@ -18,7 +18,7 @@ void seat_antispoof_msg(InteractionReadySeat iseat, const char *msg)
* wouldn't be able to mimic it within our line-length
* constraint.
*/
strbuf_catf(sb, "-- %s ", msg);
put_fmt(sb, "-- %s ", msg);
while (sb->len < 78)
put_byte(sb, '-');
}

View File

@ -10,26 +10,25 @@ char *buildinfo(const char *newline)
{
strbuf *buf = strbuf_new();
strbuf_catf(buf, "Build platform: %d-bit %s",
(int)(CHAR_BIT * sizeof(void *)),
BUILDINFO_PLATFORM);
put_fmt(buf, "Build platform: %d-bit %s",
(int)(CHAR_BIT * sizeof(void *)), BUILDINFO_PLATFORM);
#ifdef __clang_version__
#define FOUND_COMPILER
strbuf_catf(buf, "%sCompiler: clang %s", newline, __clang_version__);
put_fmt(buf, "%sCompiler: clang %s", newline, __clang_version__);
#elif defined __GNUC__ && defined __VERSION__
#define FOUND_COMPILER
strbuf_catf(buf, "%sCompiler: gcc %s", newline, __VERSION__);
put_fmt(buf, "%sCompiler: gcc %s", newline, __VERSION__);
#endif
#if defined _MSC_VER
#ifndef FOUND_COMPILER
#define FOUND_COMPILER
strbuf_catf(buf, "%sCompiler: ", newline);
put_fmt(buf, "%sCompiler: ", newline);
#else
strbuf_catf(buf, ", emulating ");
put_fmt(buf, ", emulating ");
#endif
strbuf_catf(buf, "Visual Studio");
put_fmt(buf, "Visual Studio");
#if 0
/*
@ -51,69 +50,69 @@ char *buildinfo(const char *newline)
* 19.28.29500.* and going up. Hence, 19 28 29500 is what we
* compare _MSC_FULL_VER against above.
*/
strbuf_catf(buf, " 2019 (16.9)");
put_fmt(buf, " 2019 (16.9)");
#elif _MSC_VER == 1928
strbuf_catf(buf, " 2019 (16.8)");
put_fmt(buf, " 2019 (16.8)");
#elif _MSC_VER == 1927
strbuf_catf(buf, " 2019 (16.7)");
put_fmt(buf, " 2019 (16.7)");
#elif _MSC_VER == 1926
strbuf_catf(buf, " 2019 (16.6)");
put_fmt(buf, " 2019 (16.6)");
#elif _MSC_VER == 1925
strbuf_catf(buf, " 2019 (16.5)");
put_fmt(buf, " 2019 (16.5)");
#elif _MSC_VER == 1924
strbuf_catf(buf, " 2019 (16.4)");
put_fmt(buf, " 2019 (16.4)");
#elif _MSC_VER == 1923
strbuf_catf(buf, " 2019 (16.3)");
put_fmt(buf, " 2019 (16.3)");
#elif _MSC_VER == 1922
strbuf_catf(buf, " 2019 (16.2)");
put_fmt(buf, " 2019 (16.2)");
#elif _MSC_VER == 1921
strbuf_catf(buf, " 2019 (16.1)");
put_fmt(buf, " 2019 (16.1)");
#elif _MSC_VER == 1920
strbuf_catf(buf, " 2019 (16.0)");
put_fmt(buf, " 2019 (16.0)");
#elif _MSC_VER == 1916
strbuf_catf(buf, " 2017 version 15.9");
put_fmt(buf, " 2017 version 15.9");
#elif _MSC_VER == 1915
strbuf_catf(buf, " 2017 version 15.8");
put_fmt(buf, " 2017 version 15.8");
#elif _MSC_VER == 1914
strbuf_catf(buf, " 2017 version 15.7");
put_fmt(buf, " 2017 version 15.7");
#elif _MSC_VER == 1913
strbuf_catf(buf, " 2017 version 15.6");
put_fmt(buf, " 2017 version 15.6");
#elif _MSC_VER == 1912
strbuf_catf(buf, " 2017 version 15.5");
put_fmt(buf, " 2017 version 15.5");
#elif _MSC_VER == 1911
strbuf_catf(buf, " 2017 version 15.3");
put_fmt(buf, " 2017 version 15.3");
#elif _MSC_VER == 1910
strbuf_catf(buf, " 2017 RTW (15.0)");
put_fmt(buf, " 2017 RTW (15.0)");
#elif _MSC_VER == 1900
strbuf_catf(buf, " 2015 (14.0)");
put_fmt(buf, " 2015 (14.0)");
#elif _MSC_VER == 1800
strbuf_catf(buf, " 2013 (12.0)");
put_fmt(buf, " 2013 (12.0)");
#elif _MSC_VER == 1700
strbuf_catf(buf, " 2012 (11.0)");
put_fmt(buf, " 2012 (11.0)");
#elif _MSC_VER == 1600
strbuf_catf(buf, " 2010 (10.0)");
put_fmt(buf, " 2010 (10.0)");
#elif _MSC_VER == 1500
strbuf_catf(buf, " 2008 (9.0)");
put_fmt(buf, " 2008 (9.0)");
#elif _MSC_VER == 1400
strbuf_catf(buf, " 2005 (8.0)");
put_fmt(buf, " 2005 (8.0)");
#elif _MSC_VER == 1310
strbuf_catf(buf, " .NET 2003 (7.1)");
put_fmt(buf, " .NET 2003 (7.1)");
#elif _MSC_VER == 1300
strbuf_catf(buf, " .NET 2002 (7.0)");
put_fmt(buf, " .NET 2002 (7.0)");
#elif _MSC_VER == 1200
strbuf_catf(buf, " 6.0");
put_fmt(buf, " 6.0");
#else
strbuf_catf(buf, ", unrecognised version");
put_fmt(buf, ", unrecognised version");
#endif
strbuf_catf(buf, ", _MSC_VER=%d", (int)_MSC_VER);
put_fmt(buf, ", _MSC_VER=%d", (int)_MSC_VER);
#endif
#ifdef BUILDINFO_GTK
{
char *gtk_buildinfo = buildinfo_gtk_version();
if (gtk_buildinfo) {
strbuf_catf(buf, "%sCompiled against GTK version %s",
newline, gtk_buildinfo);
put_fmt(buf, "%sCompiled against GTK version %s",
newline, gtk_buildinfo);
sfree(gtk_buildinfo);
}
}
@ -122,34 +121,34 @@ char *buildinfo(const char *newline)
{
int echm = has_embedded_chm();
if (echm >= 0)
strbuf_catf(buf, "%sEmbedded HTML Help file: %s", newline,
echm ? "yes" : "no");
put_fmt(buf, "%sEmbedded HTML Help file: %s", newline,
echm ? "yes" : "no");
}
#endif
#if defined _WINDOWS && defined MINEFIELD
strbuf_catf(buf, "%sBuild option: MINEFIELD", newline);
put_fmt(buf, "%sBuild option: MINEFIELD", newline);
#endif
#ifdef NO_IPV6
strbuf_catf(buf, "%sBuild option: NO_IPV6", newline);
put_fmt(buf, "%sBuild option: NO_IPV6", newline);
#endif
#ifdef NO_GSSAPI
strbuf_catf(buf, "%sBuild option: NO_GSSAPI", newline);
put_fmt(buf, "%sBuild option: NO_GSSAPI", newline);
#endif
#ifdef STATIC_GSSAPI
strbuf_catf(buf, "%sBuild option: STATIC_GSSAPI", newline);
put_fmt(buf, "%sBuild option: STATIC_GSSAPI", newline);
#endif
#ifdef UNPROTECT
strbuf_catf(buf, "%sBuild option: UNPROTECT", newline);
put_fmt(buf, "%sBuild option: UNPROTECT", newline);
#endif
#ifdef FUZZING
strbuf_catf(buf, "%sBuild option: FUZZING", newline);
put_fmt(buf, "%sBuild option: FUZZING", newline);
#endif
#ifdef DEBUG
strbuf_catf(buf, "%sBuild option: DEBUG", newline);
put_fmt(buf, "%sBuild option: DEBUG", newline);
#endif
strbuf_catf(buf, "%sSource commit: %s", newline, commitid);
put_fmt(buf, "%sSource commit: %s", newline, commitid);
return strbuf_to_str(buf);
}

View File

@ -1,4 +1,5 @@
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
@ -99,6 +100,25 @@ bool BinarySink_put_pstring(BinarySink *bs, const char *str)
return true;
}
void BinarySink_put_fmtv(BinarySink *bs, const char *fmt, va_list ap)
{
if (bs->writefmtv) {
bs->writefmtv(bs, fmt, ap);
} else {
char *str = dupvprintf(fmt, ap);
bs->write(bs, str, strlen(str));
burnstr(str);
}
}
void BinarySink_put_fmt(BinarySink *bs, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
BinarySink_put_fmtv(bs, fmt, ap);
va_end(ap);
}
/* ---------------------------------------------------------------------- */
static bool BinarySource_data_avail(BinarySource *src, size_t wanted)

View File

@ -60,10 +60,21 @@ static void strbuf_BinarySink_write(
memcpy(strbuf_append(buf_o, len), data, len);
}
static void strbuf_BinarySink_writefmtv(
BinarySink *bs, const char *fmt, va_list ap)
{
strbuf *buf_o = BinarySink_DOWNCAST(bs, strbuf);
struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
STRBUF_SET_PTR(buf, dupvprintf_inner(buf->visible.s, buf->visible.len,
&buf->size, fmt, ap));
buf->visible.len += strlen(buf->visible.s + buf->visible.len);
}
static strbuf *strbuf_new_general(bool nm)
{
struct strbuf_impl *buf = snew(struct strbuf_impl);
BinarySink_INIT(&buf->visible, strbuf_BinarySink_write);
buf->visible.binarysink_->writefmtv = strbuf_BinarySink_writefmtv;
buf->visible.len = 0;
buf->size = 512;
buf->nm = nm;
@ -89,21 +100,6 @@ char *strbuf_to_str(strbuf *buf_o)
sfree(buf);
return ret;
}
void strbuf_catfv(strbuf *buf_o, const char *fmt, va_list ap)
{
struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
STRBUF_SET_PTR(buf, dupvprintf_inner(buf->visible.s, buf->visible.len,
&buf->size, fmt, ap));
buf->visible.len += strlen(buf->visible.s + buf->visible.len);
}
void strbuf_catf(strbuf *buf_o, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
strbuf_catfv(buf_o, fmt, ap);
va_end(ap);
}
strbuf *strbuf_new_for_agent_query(void)
{
strbuf *buf = strbuf_new();