From 0edd10925d6aeef2d283928f257df68c6f97b306 Mon Sep 17 00:00:00 2001 From: Dragan Dosen Date: Fri, 12 Feb 2016 13:23:02 +0100 Subject: [PATCH] MINOR: standard: add function "escape_chunk" This function tries to prefix all characters tagged in the with the character. The specified contains the input to be escaped. --- include/common/standard.h | 11 +++++++++++ src/standard.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/common/standard.h b/include/common/standard.h index 3e40fd509..a66317171 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -380,6 +380,17 @@ char *encode_chunk(char *start, char *stop, const char escape, const fd_set *map, const struct chunk *chunk); +/* + * Tries to prefix characters tagged in the with the + * character. contains the input to be escaped. The result will be + * stored between (included) and (excluded). The function + * will always try to terminate the resulting string with a '\0' before + * , and will return its position if the conversion completes. + */ +char *escape_chunk(char *start, char *stop, + const char escape, const fd_set *map, + const struct chunk *chunk); + /* Check a string for using it in a CSV output format. If the string contains * one of the following four char <">, <,>, CR or LF, the string is diff --git a/src/standard.c b/src/standard.c index 67c82c730..f4da01bc2 100644 --- a/src/standard.c +++ b/src/standard.c @@ -1439,6 +1439,38 @@ char *encode_chunk(char *start, char *stop, return start; } +/* + * Tries to prefix characters tagged in the with the + * character. contains the input to be escaped. The result will be + * stored between (included) and (excluded). The function + * will always try to terminate the resulting string with a '\0' before + * , and will return its position if the conversion completes. + */ +char *escape_chunk(char *start, char *stop, + const char escape, const fd_set *map, + const struct chunk *chunk) +{ + char *str = chunk->str; + char *end = chunk->str + chunk->len; + + if (start < stop) { + stop--; /* reserve one byte for the final '\0' */ + while (start < stop && str < end) { + if (!FD_ISSET((unsigned char)(*str), map)) + *start++ = *str; + else { + if (start + 2 >= stop) + break; + *start++ = escape; + *start++ = *str; + } + str++; + } + *start = '\0'; + } + return start; +} + /* Check a string for using it in a CSV output format. If the string contains * one of the following four char <">, <,>, CR or LF, the string is * encapsulated between <"> and the <"> are escaped by a <""> sequence.