diff --git a/dix/rpcbuf.c b/dix/rpcbuf.c index de9dfcd560..1eb3a768da 100644 --- a/dix/rpcbuf.c +++ b/dix/rpcbuf.c @@ -93,6 +93,23 @@ Bool x_rpcbuf_write_string_pad(struct x_rpcbuf *rpcbuf, const char *str) return TRUE; } +Bool x_rpcbuf_write_string_0t_pad(struct x_rpcbuf *rpcbuf, const char *str) +{ + if (!str) + return x_rpcbuf_write_CARD32(rpcbuf, 0); + + size_t slen = strlen(str); + if (!slen) + return x_rpcbuf_write_CARD32(rpcbuf, 0); + + char *reserved = x_rpcbuf_reserve(rpcbuf, pad_to_int32(slen+1)); + if (!reserved) + return FALSE; + + memcpy(reserved, str, slen+1); + return TRUE; +} + Bool x_rpcbuf_write_CARD8(struct x_rpcbuf *rpcbuf, CARD8 value) { CARD8 *reserved = x_rpcbuf_reserve(rpcbuf, sizeof(value)); diff --git a/dix/rpcbuf_priv.h b/dix/rpcbuf_priv.h index f853f66f61..e509f5e613 100644 --- a/dix/rpcbuf_priv.h +++ b/dix/rpcbuf_priv.h @@ -96,6 +96,19 @@ void *x_rpcbuf_reserve(struct x_rpcbuf *rpcbuf, size_t needed) Bool x_rpcbuf_write_string_pad(struct x_rpcbuf *rpcbuf, const char *str) _X_ATTRIBUTE_NONNULL_ARG(1); +/* + * write a plain C string with terminating 0 to rpc buffer and pad it. + * + * allocate a region for the string (padded to 32bits) and copy in the string. + * if given string is NULL or zero-size, only a (CARD32)0 is written. + * + * @param rpcbuf pointer to struct x_rpcbuf to operate on + * @param needed string to plain C string + * @return TRUE on success, FALSE on allocation failure + */ +Bool x_rpcbuf_write_string_0t_pad(struct x_rpcbuf *rpcbuf, const char *str) + _X_ATTRIBUTE_NONNULL_ARG(1); + /* * write binary data to rpc buffer and pad it. *