1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Marshalling macros put_dataz and put_datalit.

When I wanted to append an ordinary C string to a BinarySink, without
any prefix length field or suffix terminator, I was using the idiom

  put_datapl(bs, ptrlen_from_asciz(string));

but I've finally decided that's too cumbersome, and it deserves a
shorter name. put_dataz(bs, string) now does the same thing - in fact
it's a macro expanding to exactly the above.

While I'm at it, I've also added put_datalit(), which is the same
except that it expects a C string literal (and will enforce that at
compile time, via PTRLEN_LITERAL which it calls in turn). You can use
that where possible to avoid the run-time cost of the strlen.
This commit is contained in:
Simon Tatham 2021-11-19 14:33:23 +00:00
parent be8d3974ff
commit cc6d3591ad
7 changed files with 11 additions and 7 deletions

View File

@ -141,6 +141,10 @@ struct BinarySink {
BinarySink_put_data(BinarySink_UPCAST(bs), val, len) BinarySink_put_data(BinarySink_UPCAST(bs), val, len)
#define put_datapl(bs, pl) \ #define put_datapl(bs, pl) \
BinarySink_put_datapl(BinarySink_UPCAST(bs), pl) BinarySink_put_datapl(BinarySink_UPCAST(bs), pl)
#define put_dataz(bs, val) \
BinarySink_put_datapl(BinarySink_UPCAST(bs), ptrlen_from_asciz(val))
#define put_datalit(bs, val) \
BinarySink_put_datapl(BinarySink_UPCAST(bs), PTRLEN_LITERAL(val))
/* Emit printf-formatted data, with no terminator. */ /* Emit printf-formatted data, with no terminator. */
#define put_fmt(bs, ...) \ #define put_fmt(bs, ...) \

View File

@ -774,7 +774,7 @@ int main(int argc, char **argv)
while (argc > 0) { while (argc > 0) {
if (cmdbuf->len > 0) if (cmdbuf->len > 0)
put_byte(cmdbuf, ' '); /* add space separator */ put_byte(cmdbuf, ' '); /* add space separator */
put_datapl(cmdbuf, ptrlen_from_asciz(p)); put_dataz(cmdbuf, p);
if (--argc > 0) if (--argc > 0)
p = *++argv; p = *++argv;
} }

View File

@ -557,7 +557,7 @@ bool enum_settings_next(settings_e *handle, strbuf *out)
fullpath = strbuf_new(); fullpath = strbuf_new();
char *sessiondir = make_filename(INDEX_SESSIONDIR, NULL); char *sessiondir = make_filename(INDEX_SESSIONDIR, NULL);
put_datapl(fullpath, ptrlen_from_asciz(sessiondir)); put_dataz(fullpath, sessiondir);
sfree(sessiondir); sfree(sessiondir);
put_byte(fullpath, '/'); put_byte(fullpath, '/');
@ -565,7 +565,7 @@ bool enum_settings_next(settings_e *handle, strbuf *out)
while ( (de = readdir(handle->dp)) != NULL ) { while ( (de = readdir(handle->dp)) != NULL ) {
strbuf_shrink_to(fullpath, baselen); strbuf_shrink_to(fullpath, baselen);
put_datapl(fullpath, ptrlen_from_asciz(de->d_name)); put_dataz(fullpath, de->d_name);
if (stat(fullpath->s, &st) < 0 || !S_ISREG(st.st_mode)) if (stat(fullpath->s, &st) < 0 || !S_ISREG(st.st_mode))
continue; /* try another one */ continue; /* try another one */

View File

@ -11,7 +11,7 @@ void seat_antispoof_msg(InteractionReadySeat iseat, const char *msg)
* generated by the client, then we can just use the message * generated by the client, then we can just use the message
* unmodified as an unspoofable header. * unmodified as an unspoofable header.
*/ */
put_datapl(sb, ptrlen_from_asciz(msg)); put_dataz(sb, msg);
} else if (*msg) { } else if (*msg) {
/* /*
* Otherwise, add enough padding around it that the server * Otherwise, add enough padding around it that the server

View File

@ -34,7 +34,7 @@ void add_prompt(prompts_t *p, char *promptstr, bool echo)
void prompt_set_result(prompt_t *pr, const char *newstr) void prompt_set_result(prompt_t *pr, const char *newstr)
{ {
strbuf_clear(pr->result); strbuf_clear(pr->result);
put_datapl(pr->result, ptrlen_from_asciz(newstr)); put_dataz(pr->result, newstr);
} }
const char *prompt_get_result_ref(prompt_t *pr) const char *prompt_get_result_ref(prompt_t *pr)

View File

@ -889,7 +889,7 @@ static INT_PTR CALLBACK HostKeyDialogProc(HWND hwnd, UINT msg,
for (size_t i = 0; ctx->keywords[i]; i++) { for (size_t i = 0; ctx->keywords[i]; i++) {
if (strstartswith(p, ctx->keywords[i])) { if (strstartswith(p, ctx->keywords[i])) {
p += strlen(ctx->keywords[i]); p += strlen(ctx->keywords[i]);
put_datapl(sb, ptrlen_from_asciz(ctx->values[i])); put_dataz(sb, ctx->values[i]);
goto matched; goto matched;
} }
} }

View File

@ -366,7 +366,7 @@ int main(int argc, char **argv)
while (argc > 0) { while (argc > 0) {
if (cmdbuf->len > 0) if (cmdbuf->len > 0)
put_byte(cmdbuf, ' '); /* add space separator */ put_byte(cmdbuf, ' '); /* add space separator */
put_datapl(cmdbuf, ptrlen_from_asciz(p)); put_dataz(cmdbuf, p);
if (--argc > 0) if (--argc > 0)
p = *++argv; p = *++argv;
} }