gchar *tmp, *tp;
gint state = 0, l, next_state = 100, err_state = 100, t_state;
gboolean valid_folding = FALSE;
+ guint nlines_count[RSPAMD_TASK_NEWLINES_MAX];
p = in;
end = p + len;
c = p;
+ memset (nlines_count, 0, sizeof (nlines_count));
while (p < end) {
/* FSM for processing headers */
p++;
}
else if (*p == '\n' || *p == '\r') {
+
+ if (*p == '\n') {
+ nlines_count[RSPAMD_TASK_NEWLINES_LF] ++;
+ }
+ else if (*(p + 1) == '\n') {
+ nlines_count[RSPAMD_TASK_NEWLINES_CRLF] ++;
+ }
+ else {
+ nlines_count[RSPAMD_TASK_NEWLINES_CR] ++;
+ }
+
/* Process folding */
state = 99;
l = p - c;
case 3:
if (*p == '\r' || *p == '\n') {
/* Hold folding */
+ if (*p == '\n') {
+ nlines_count[RSPAMD_TASK_NEWLINES_LF] ++;
+ }
+ else if (*(p + 1) == '\n') {
+ nlines_count[RSPAMD_TASK_NEWLINES_CRLF] ++;
+ }
+ else {
+ nlines_count[RSPAMD_TASK_NEWLINES_CR] ++;
+ }
state = 99;
next_state = 3;
err_state = 4;
if (*p == '\r') {
if (*(p + 1) == '\n') {
+ nlines_count[RSPAMD_TASK_NEWLINES_CRLF] ++;
p++;
}
p++;
state = next_state;
}
else if (*p == '\n') {
+ nlines_count[RSPAMD_TASK_NEWLINES_LF] ++;
+
if (*(p + 1) == '\r') {
p++;
}
break;
}
}
+
+ guint max_cnt = 0;
+ gint sel = 0;
+
+ for (gint i = 0; i < RSPAMD_TASK_NEWLINES_MAX; i ++) {
+ if (nlines_count[i] > max_cnt) {
+ max_cnt = nlines_count[i];
+ sel = i;
+ }
+ }
+
+ task->nlines_type = sel;
}
static void
static gchar *
rspamd_encode_base64_common (const guchar *in, gsize inlen, gint str_len,
- gsize *outlen, gboolean fold)
+ gsize *outlen, gboolean fold, enum rspamd_newlines_type how)
{
+#define ADD_SPLIT do { \
+ if (how == RSPAMD_TASK_NEWLINES_CR || how == RSPAMD_TASK_NEWLINES_CRLF) *o++ = '\r'; \
+ if (how == RSPAMD_TASK_NEWLINES_LF || how == RSPAMD_TASK_NEWLINES_CRLF) *o++ = '\n'; \
+ if (fold) *o++ = '\t'; \
+} while (0)
#define CHECK_SPLIT \
do { if (str_len > 0 && cols >= str_len) { \
- *o++ = '\r'; \
- *o++ = '\n'; \
- if (fold) *o++ = '\t'; \
- cols = 0; \
+ ADD_SPLIT; \
+ cols = 0; \
} } \
while (0)
if (str_len > 0) {
g_assert (str_len > 8);
- allocated_len += (allocated_len / str_len + 1) * (fold ? 3 : 2) + 1;
+ if (fold) {
+ switch (how) {
+ case RSPAMD_TASK_NEWLINES_CR:
+ case RSPAMD_TASK_NEWLINES_LF:
+ allocated_len += (allocated_len / str_len + 1) * 2 + 1;
+ break;
+ default:
+ allocated_len += (allocated_len / str_len + 1) * 3 + 1;
+ break;
+ }
+ }
+ else {
+ switch (how) {
+ case RSPAMD_TASK_NEWLINES_CR:
+ case RSPAMD_TASK_NEWLINES_LF:
+ allocated_len += (allocated_len / str_len + 1) * 1 + 1;
+ break;
+ default:
+ allocated_len += (allocated_len / str_len + 1) * 2 + 1;
+ break;
+ }
+ }
}
out = g_malloc (allocated_len);
cols --;
}
- *o++ = '\r';
- *o++ = '\n';
- if (fold) {
- *o ++ = '\t';
- }
+ ADD_SPLIT;
/* Remaining bytes */
while (shift >= 16) {
rspamd_encode_base64 (const guchar *in, gsize inlen, gint str_len,
gsize *outlen)
{
- return rspamd_encode_base64_common (in, inlen, str_len, outlen, FALSE);
+ return rspamd_encode_base64_common (in, inlen, str_len, outlen, FALSE,
+ RSPAMD_TASK_NEWLINES_CRLF);
}
gchar *
rspamd_encode_base64_fold (const guchar *in, gsize inlen, gint str_len,
- gsize *outlen)
+ gsize *outlen, enum rspamd_newlines_type how)
{
- return rspamd_encode_base64_common (in, inlen, str_len, outlen, TRUE);
+ return rspamd_encode_base64_common (in, inlen, str_len, outlen, TRUE, how);
}
gsize
GString *
rspamd_header_value_fold (const gchar *name,
const gchar *value,
- guint fold_max)
+ guint fold_max,
+ enum rspamd_newlines_type how)
{
GString *res;
const guint default_fold_max = 76;
c = p;
state = read_quoted;
}
- else if (*p == '\r') {
+ else if (*p == '\r' || *p == '\n') {
/* Reset line length */
cur_len = 0;
/* Here, we have token start at 'c' and token end at 'p' */
if (fold_type == fold_after) {
g_string_append_len (res, c, p - c);
- g_string_append_len (res, "\r\n\t", 3);
+
+ switch (how) {
+ case RSPAMD_TASK_NEWLINES_LF:
+ g_string_append_len (res, "\n\t", 2);
+ break;
+ case RSPAMD_TASK_NEWLINES_CR:
+ g_string_append_len (res, "\r\t", 2);
+ break;
+ case RSPAMD_TASK_NEWLINES_CRLF:
+ default:
+ g_string_append_len (res, "\r\n\t", 3);
+ break;
+ }
/* Skip space if needed */
if (g_ascii_isspace (*p)) {
c ++;
}
- g_string_append_len (res, "\r\n\t", 3);
+ switch (how) {
+ case RSPAMD_TASK_NEWLINES_LF:
+ g_string_append_len (res, "\n\t", 2);
+ break;
+ case RSPAMD_TASK_NEWLINES_CR:
+ g_string_append_len (res, "\r\t", 2);
+ break;
+ case RSPAMD_TASK_NEWLINES_CRLF:
+ default:
+ g_string_append_len (res, "\r\n\t", 3);
+ break;
+ }
+
g_string_append_len (res, c, p - c);
}
if (g_ascii_isspace (*c)) {
c ++;
}
- g_string_append_len (res, "\r\n\t", 3);
+ switch (how) {
+ case RSPAMD_TASK_NEWLINES_LF:
+ g_string_append_len (res, "\n\t", 2);
+ break;
+ case RSPAMD_TASK_NEWLINES_CR:
+ g_string_append_len (res, "\r\t", 2);
+ break;
+ case RSPAMD_TASK_NEWLINES_CRLF:
+ default:
+ g_string_append_len (res, "\r\n\t", 3);
+ break;
+ }
g_string_append_len (res, c, p - c);
}
else {
#include "ucl.h"
#include "fstring.h"
+
+enum rspamd_newlines_type {
+ RSPAMD_TASK_NEWLINES_CR,
+ RSPAMD_TASK_NEWLINES_LF,
+ RSPAMD_TASK_NEWLINES_CRLF,
+ RSPAMD_TASK_NEWLINES_MAX
+};
+
/**
* Compare two memory regions of size `l` using case insensitive matching
*/
* @return freshly allocated base64 encoded value or NULL if input is invalid
*/
gchar * rspamd_encode_base64_fold (const guchar *in, gsize inlen, gint str_len,
- gsize *outlen);
+ gsize *outlen, enum rspamd_newlines_type how);
/**
* Decode URL encoded string in-place and return new length of a string, src and dst are NULL terminated
*/
GString *rspamd_header_value_fold (const gchar *name,
const gchar *value,
- guint fold_max);
+ guint fold_max,
+ enum rspamd_newlines_type how);
/**
* Search for a substring `srch` in the text `in` using Karp-Rabin algorithm