summaryrefslogtreecommitdiffstats
path: root/src/ragel/newlines_strip.rl
blob: d5de198a529fbd47bbd1e511146a9d4387926143 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
%%{
  machine newlines_strip;

  action Double_CRLF {
    if (!crlf_added && p > c) {
      (*newlines_count)++;
      g_byte_array_append (data, (const guint8 *)" ", 1);
      c = p;
    }

    crlf_added = TRUE;
    c = p;
  }

  action WSP {
    g_byte_array_append (data, (const guint8 *)" ", 1);
    c = p;
  }

  action Text_Start {
    crlf_added = FALSE;
    c = p;
  }

  action Text_End {
    if (p > c) {
      g_byte_array_append (data, (const guint8 *)c, p - c);
      last_c = *(p - 1);
    }

    c = p;
  }

  action Line_CRLF {
    if (!crlf_added) {
      if (is_html || g_ascii_ispunct (last_c)) {
         g_byte_array_append (data, (const guint8 *)" ", 1);
         crlf_added = TRUE;
      }
    }

    (*newlines_count)++;
    g_ptr_array_add (newlines, (((gpointer) (goffset) (data->len))));
    c = p;
  }


  WSP   = " " | "\t" | "\v";
  CRLF  = ("\r" . "\n") | ( "\r" ) | ("\n");
  DOUBLE_CRLF = (CRLF <: (WSP* CRLF)+) %Double_CRLF;
  ANY_CRLF = CRLF | DOUBLE_CRLF;
  LINE = (([^\r\n]+) >Text_Start %Text_End);
  TEXT  = ANY_CRLF* . (LINE <: ANY_CRLF %Line_CRLF)+ | LINE | ANY_CRLF %Line_CRLF;

  main := TEXT;
}%%

#include <glib.h>

%% write data;

void
rspamd_strip_newlines_parse (const gchar *begin, const gchar *pe,
    GByteArray *data, gboolean is_html, guint *newlines_count,
    GPtrArray *newlines)
{
  const gchar *c, *p, *eof;
  gint last_c = -1;
  gint cs = 0;
  gboolean crlf_added = FALSE;

  c = begin;
  p = begin;
  eof = pe;

  %% write init;
  %% write exec;

  if (p > c) {
     g_byte_array_append (data, (const guint8 *)c, p - c);
  }
}