diff options
author | Gregor Beck <gbeck@sernet.de> | 2011-03-17 10:20:30 +0100 |
---|---|---|
committer | Michael Adam <obnox@samba.org> | 2011-04-04 15:57:36 +0200 |
commit | 9272614d9dad501ad95ac76b40f53e433a4878bc (patch) | |
tree | 190dd74aa1d9b7bc96b099de634a689b3ca57e86 /source3 | |
parent | 340f20ddb01da08b6a6ad11143768ffd69b7944e (diff) | |
download | samba-9272614d9dad501ad95ac76b40f53e433a4878bc.tar.gz samba-9272614d9dad501ad95ac76b40f53e433a4878bc.tar.bz2 samba-9272614d9dad501ad95ac76b40f53e433a4878bc.zip |
s3: add function cbuf_print_quoted
Diffstat (limited to 'source3')
-rw-r--r-- | source3/lib/cbuf.c | 33 | ||||
-rw-r--r-- | source3/lib/cbuf.h | 17 |
2 files changed, 49 insertions, 1 deletions
diff --git a/source3/lib/cbuf.c b/source3/lib/cbuf.c index 16dd52c292..42353f8f64 100644 --- a/source3/lib/cbuf.c +++ b/source3/lib/cbuf.c @@ -286,3 +286,36 @@ int cbuf_print_quoted_string(cbuf* ost, const char* s) s++; } } + + +int cbuf_print_quoted(cbuf* ost, const char* s, size_t len) +{ + int n = 1; + int ret; + cbuf_reserve(ost, len+2); + + cbuf_putc(ost,'"'); + + while(len--) { + switch (*s) { + case '"': + case '\\': + ret = cbuf_printf(ost, "\\%c", *s); + break; + default: + if (isprint(*s) && ((*s == ' ') || !isspace(*s))) { + ret = cbuf_putc(ost, *s); + } else { + ret = cbuf_printf(ost, "\\%02x", *s); + } + } + s++; + if (ret == -1) { + return -1; + } + n += ret; + } + ret = cbuf_putc(ost,'"'); + + return (ret == -1) ? -1 : (n + ret); +} diff --git a/source3/lib/cbuf.h b/source3/lib/cbuf.h index ffb52d7b9a..90318ec7a9 100644 --- a/source3/lib/cbuf.h +++ b/source3/lib/cbuf.h @@ -225,11 +225,26 @@ char* cbuf_gets(cbuf* b, size_t idx); * @see srprs_quoted_string * * @param[out] ost outstream - * @param[in] s string + * @param[in] s '\0' terminated string of printable characters. * * @return numner of bytes written, -1 on error */ int cbuf_print_quoted_string(cbuf* ost, const char* s); +/** + * Print quoted string to stream. + * Escapes nonprintable characters. + * + * @todo check for ssputc failure + * @see srprs_quoted + * + * @param[out] ost outstream + * @param[in] s string of bytes + * @param[in] len number of bytes + * + * @return numner of bytes written, -1 on error + */ +int cbuf_print_quoted(cbuf* ost, const char* s, size_t len); + #endif /*__CBUF_H*/ |