]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: log: add lf_rawtext{_len}() functions
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 10 Apr 2024 10:07:26 +0000 (12:07 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Fri, 26 Apr 2024 16:39:31 +0000 (18:39 +0200)
similar to lf_text_{len}, except that quoting and mandatory options are
ignored. Use this to print the input string without any modification (
except for encoding logic).

src/log.c

index 8639b18b7caa987422cb641f89c0aceadc429873..7debd27759ed4ac1fe1f77799c663e68778e5d76 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -1831,21 +1831,14 @@ static char *lf_encode_chunk(char *start, char *stop,
 }
 
 /*
- * Write a string in the log string
- * Take cares of quote and escape options
+ * Write a raw string in the log string
+ * Take care of escape option
  *
  * Return the address of the \0 character, or NULL on error
  */
-char *lf_text_len(char *dst, const char *src, size_t len, size_t size, const struct logformat_node *node)
+static inline char *_lf_text_len(char *dst, const char *src,
+                                 size_t len, size_t size, const struct logformat_node *node)
 {
-       if (size < 2)
-               return NULL;
-
-       if (node->options & LOG_OPT_QUOTE) {
-               *(dst++) = '"';
-               size--;
-       }
-
        if (src && len) {
                /* escape_string and strlcpy2 will both try to add terminating NULL-byte
                 * to dst
@@ -1862,33 +1855,95 @@ char *lf_text_len(char *dst, const char *src, size_t len, size_t size, const str
                        if (++len > size)
                                len = size;
                        len = strlcpy2(dst, src, len);
+                       if (len == 0)
+                               return NULL;
                }
-
-               size -= len;
                dst += len;
+               size -= len;
        }
-       else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
-               if (size < 2)
-                       return NULL;
-               *(dst++) = '-';
-               size -= 1;
-       }
 
-       if (node->options & LOG_OPT_QUOTE) {
-               if (size < 2)
+       if (size < 1)
+               return NULL;
+       *dst = '\0';
+
+       return dst;
+}
+
+/*
+ * Quote a string, then leverage _lf_text_len() to write it
+ */
+static inline char *_lf_quotetext_len(char *dst, const char *src,
+                                      size_t len, size_t size, const struct logformat_node *node)
+{
+       if (size < 2)
+               return NULL;
+
+       *(dst++) = '"';
+       size--;
+
+       if (src && len) {
+               char *ret;
+
+               ret = _lf_text_len(dst, src, len, size, node);
+               if (ret == NULL)
                        return NULL;
-               *(dst++) = '"';
+               size -= (ret - dst);
+               dst += (ret - dst);
        }
 
+       if (size < 2)
+               return NULL;
+       *(dst++) = '"';
+
        *dst = '\0';
        return dst;
 }
 
+/*
+ * Write a string in the log string
+ * Take care of quote, mandatory and escape options
+ *
+ * Return the address of the \0 character, or NULL on error
+ */
+char *lf_text_len(char *dst, const char *src, size_t len, size_t size, const struct logformat_node *node)
+{
+       if ((node->options & LOG_OPT_QUOTE))
+               return _lf_quotetext_len(dst, src, len, size, node);
+       else if (src && len)
+               return _lf_text_len(dst, src, len, size, node);
+
+       if (size < 2)
+               return NULL;
+
+       if ((node->options & LOG_OPT_MANDATORY))
+               return _lf_text_len(dst, "-", 1, size, node);
+
+       *dst = '\0';
+
+       return dst;
+}
+
+/*
+ * Same as lf_text_len() except that it ignores mandatory and quoting options.
+ * Quoting is only performed when strictly required by the encoding method.
+ */
+char *lf_rawtext_len(char *dst, const char *src, size_t len, size_t size, const struct logformat_node *node)
+{
+       return _lf_text_len(dst, src, len, size, node);
+}
+
+/* lf_text_len() helper when <src> is null-byte terminated */
 static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
 {
        return lf_text_len(dst, src, size, size, node);
 }
 
+/* lf_rawtext_len() helper when <src> is null-byte terminated */
+static inline char *lf_rawtext(char *dst, const char *src, size_t size, const struct logformat_node *node)
+{
+       return lf_rawtext_len(dst, src, size, size, node);
+}
+
 /*
  * Write a IP address to the log string
  * +X option write in hexadecimal notation, most significant byte on the left