From a4956ccba0ae6254b897eb49e0db03999d002151 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Mon, 5 Aug 2024 00:10:38 +0900 Subject: [PATCH] in_forward: Use extracted function for processing concatenated gzip Signed-off-by: Hiroshi Hatake --- plugins/in_forward/fw_prot.c | 60 +++++------------------------------- 1 file changed, 8 insertions(+), 52 deletions(-) diff --git a/plugins/in_forward/fw_prot.c b/plugins/in_forward/fw_prot.c index 5f8fa6feb0c..82d37244a5b 100644 --- a/plugins/in_forward/fw_prot.c +++ b/plugins/in_forward/fw_prot.c @@ -1200,55 +1200,6 @@ int fw_prot_secure_forward_handshake(struct flb_input_instance *ins, return -1; } -static size_t gzip_concatenated_count(const char *data, size_t len) -{ - int i; - size_t count = 0; - const uint8_t *p; - - p = (const uint8_t *) data; - - /* search other gzip starting bits and method. */ - for (i = 2; i < len && - i + 2 <= len; i++) { - if (p[i] == 0x1F && p[i+1] == 0x8B && p[i+2] == 8) { - count++; - } - } - - return count; -} - -static size_t gzip_concatenated_borders(const char *data, size_t len, size_t **out_borders, size_t border_count) -{ - int i; - size_t count = 0; - const uint8_t *p; - size_t *borders = NULL; - - p = (const uint8_t *) data; - borders = (size_t *) flb_calloc(1, sizeof(size_t) * (border_count + 1)); - if (borders == NULL) { - flb_errno(); - return -1; - } - - /* search other gzip starting bits and method. */ - for (i = 2; i < len && - i + 2 <= len; i++) { - if (p[i] == 0x1F && p[i+1] == 0x8B && p[i+2] == 8) { - borders[count] = i; - count++; - } - } - /* The length of the last border refers to the original length. */ - borders[border_count] = len; - - *out_borders = borders; - - return count; -} - int fw_prot_process(struct flb_input_instance *ins, struct fw_conn *conn) { int ret; @@ -1543,11 +1494,16 @@ int fw_prot_process(struct flb_input_instance *ins, struct fw_conn *conn) size_t *gzip_borders = NULL; const size_t original_len = len; - gzip_payloads_count = gzip_concatenated_count(data, len); + gzip_payloads_count = flb_gzip_count(data, len, NULL, 0); flb_plg_debug(ctx->ins, "concatenated gzip payload count is %zd", gzip_payloads_count); if (gzip_payloads_count > 0) { - if (gzip_concatenated_borders(data, len, &gzip_borders, gzip_payloads_count) < 0) { + gzip_borders = (size_t *)flb_calloc(1, sizeof(size_t) * (gzip_payloads_count + 1)); + if (gzip_borders == NULL) { + flb_errno(); + return -1; + } + if (flb_gzip_count(data, len, &gzip_borders, gzip_payloads_count) < 0) { flb_plg_error(ctx->ins, "failed to traverse boundaries of concatenated gzip payloads"); return -1; @@ -1618,7 +1574,7 @@ int fw_prot_process(struct flb_input_instance *ins, struct fw_conn *conn) /* a valid payload of gzip is larger than 18 bytes. */ if (gzip_payloads_count > 0) { if ((gzip_payloads_count - loop) > 0 && - (original_len - gzip_borders[loop]) > 18) { + (original_len - gzip_borders[loop]) >= 18) { len = original_len - gzip_borders[loop]; flb_plg_debug(ctx->ins, "left unconsumed %zd byte(s)", len); prev_pos = gzip_borders[loop];