File: advanced.re

package info (click to toggle)
re2c 4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,512 kB
  • sloc: cpp: 34,160; ml: 8,494; sh: 5,311; makefile: 1,014; haskell: 611; python: 431; ansic: 234; javascript: 113
file content (252 lines) | stat: -rw-r--r-- 7,194 bytes parent folder | download | duplicates (2)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// re2c $INPUT -o $OUTPUT -cf --loop-switch -Wno-nondeterministic-tags

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*!types:re2c*/

struct mtag {
    struct mtag* prev;
    int          dist;
};

struct mtagpool {
    struct mtag* head;
    struct mtag* next;
    struct mtag* last;
};

enum con_status {
    CON_STATUS_WAITING,
    CON_STATUS_DONE,
    CON_STATUS_END,
    CON_STATUS_ERROR
};

#define CON_STATE_SIZE  (4096-32)
typedef struct con_state {
    unsigned char*  yycursor;
    unsigned char*  yymarker;
    unsigned char*  token;
    unsigned char*  yylimit;
    int             yycond;
    int             yystate;
    int             yyaccept;
    struct mtagpool mtp;
    /*!stags:re2c format = "\tunsigned char*\t\t@@;\n"; */
    /*!mtags:re2c format = "\tstruct mtag\t\t\t\t*@@;\n"; */
    size_t          bufsize;
    unsigned char*  buffer;
    unsigned char   static_buf[];
} con_state;
#define CON_READ_BUF_LEN  (CON_STATE_SIZE - sizeof(struct con_state) - 1) // -1: ensure a sentinel at the end of buf

static void mtagpool_init(struct mtagpool* mtp)
{
    static const unsigned size = 1024 * 1024;
    mtp->head = (struct mtag*)malloc(size * sizeof(struct mtag));
    mtp->next = mtp->head;
    mtp->last = mtp->head + size;
}

static void mtagpool_free(struct mtagpool* mtp)
{
    free(mtp->head);
    mtp->head = mtp->next = mtp->last = NULL;
}

static struct mtag* mtagpool_next(struct mtagpool* mtp)
{
    unsigned     size;
    struct mtag* head;

    if (mtp->next < mtp->last) return mtp->next++;

    size = mtp->last - mtp->head;
    head = (struct mtag*)malloc(2 * size * sizeof(struct mtag));
    memcpy(head, mtp->head, size * sizeof(struct mtag));
    free(mtp->head);
    mtp->head = head;
    mtp->next = head + size;
    mtp->last = head + size * 2;

    return mtp->next++;
}

static void mtag(struct mtag** pmt, const unsigned char* b, const unsigned char* t, struct mtagpool* mtp)
{
    struct mtag* mt = mtagpool_next(mtp);
    mt->prev = *pmt;
    mt->dist = t - b;
    *pmt = mt;
}

static enum con_status parse_con_req(struct con_state* c)
{
    unsigned int        yych = 0;
    const unsigned char *l1, *l2;
    struct mtag         *f1, *f2, *p1, *p2, *p3, *p4;

    /*!re2c
        re2c:api               = record;
        re2c:eof               = 0;
        re2c:flags:tags        = 1;
        re2c:variable:yyrecord = "c";
        re2c:define:YYCTYPE    = "unsigned char";
        re2c:define:YYFILL     = "return CON_STATUS_WAITING;";
        re2c:define:YYMTAGP    = "mtag(&@@, c->token, c->yycursor, &c->mtp);";
        re2c:define:YYMTAGN    = "mtag(&@@, c->token, NULL, &c->mtp);";

        crlf  = '\r\n';
        sp    = ' ';
        htab  = '\t';
        ows   = (sp | htab)*;
        digit = [0-9];
        alpha = [a-zA-Z];
        vchar = [\x1f-\x7e];
        tchar = [-!#$%&'*+.^_`|~] | digit | alpha;

        obs_fold            = #f1 crlf (sp | htab)+ #f2;
        obs_text            = [\x80-\xff];
        field_name          = tchar+;
        field_vchar         = vchar | obs_text;
        field_content       = field_vchar ((sp | htab)+ field_vchar)?;
        field_value_folded  = (field_content* obs_fold field_content*)+;
        header_field_folded = field_value_folded ows;
        token               = tchar+;
        qdtext
            = htab
            | sp
            | [\x21-\x5B\x5D-\x7E] \ '"'
            | obs_text;
        quoted_pair         = '\\' ( htab | sp | vchar | obs_text );
        quoted_string       = '"' ( qdtext | quoted_pair )* '"';
        parameter           = #p1 token #p2 '=' #p3 ( token | quoted_string ) #p4;
        media_type          = @l1 token '/' token @l2 ( ows ';' ows parameter )*;


        <media_type> media_type ows crlf {
            struct mtag*    pname_start = p1;
            struct mtag*    pname_end   = p2;
            struct mtag*    pval_start  = p3;
            struct mtag*    pval_end    = p4;

            printf("media type: %.*s\n", (int)(l2-l1), l1);

            while (0 && pname_start) {
                printf("\t(%.*s) = (%.*s)\n",
                    pname_end->dist - pname_start->dist, c->token + pname_start->dist,
                    pval_end->dist - pval_start->dist, c->token + pval_start->dist);

                pname_start = pname_start->prev;
                pname_end = pname_end->prev;
                pval_start = pval_start->prev;
                pval_end = pval_end->prev;
            }

            return CON_STATUS_DONE;
        }

        <header> header_field_folded crlf {
            struct mtag*    fold_start  = f1;
            struct mtag*    fold_end    = f2;

            while (fold_start) {
                memset(c->token + fold_start->dist, ' ', fold_end->dist - fold_start->dist);
                fold_start  = fold_start->prev;
                fold_end    = fold_end->prev;
            }

            return CON_STATUS_DONE;
        }

        <*> $ { return CON_STATUS_END; }
        <*> * { return CON_STATUS_ERROR; }
    */
}

int feed(struct con_state* c, const unsigned char* chunk, size_t len)
{
    const size_t shift = c->token - c->buffer;
    const size_t free = c->bufsize - (c->yylimit - c->token);

    if (free < len) {
        fprintf(stderr, "Token too long for receive buffer: %ld\n", c->bufsize);
        return 1;
    }

    if (shift) {
        memmove(c->buffer, c->token, c->bufsize - shift);
        c->yylimit -= shift;
        c->yycursor -= shift;
        c->yymarker -= shift;
        c->token -= shift;
        /*!stags:re2c format = "\t\t\tif (c->@@) c->@@ -= shift;\n"; */
    }

    memcpy(c->yylimit, chunk, len);

    c->yylimit += len;
    c->yylimit[0] = 0;  // Append sentinel

    return 0;
}

int main(int argc, char** argv)
{
    int rc = 0;
    int i;
    struct con_state* c = NULL;
    static const char* chunks[] = {
        "ap",
        "plication/j",
        "son;",
        " charset=\"",
        "utf\\\"-8\"\r",
        "\n",
        "",
        NULL
    };

    c = (con_state*)malloc(CON_STATE_SIZE);
    c->buffer = c->static_buf;
    c->yycursor = c->yymarker = c->token = c->yylimit = c->buffer + CON_READ_BUF_LEN;
    c->yylimit[0] = 0; // sentinel
    c->yystate = -1;
    c->yyaccept = -1;
    c->bufsize = CON_READ_BUF_LEN;
    /*!stags:re2c format = "\tc->@@ = 0;\n"; */
    /*!mtags:re2c format = "\tc->@@ = NULL;\n"; */
    mtagpool_init(&c->mtp);

    for (i=0;;) {
        switch (parse_con_req(c)) {
            case CON_STATUS_WAITING:
                printf("waiting\n");
                rc = feed(c, (const unsigned char*)chunks[i], strlen(chunks[i]));
                ++i;
                if (rc) goto finally;
                break;
            case CON_STATUS_DONE:
                printf("done\n");
                break;
            case CON_STATUS_END:
                printf("end\n");
                goto finally;
            case CON_STATUS_ERROR:
                printf("error\n");
                rc = 1;
                goto finally;
        }
    }

finally:
    if (c) {
        mtagpool_free(&c->mtp);
        free(c);
        c = NULL;
    }
    if (rc) fprintf(stderr, "Error exit: %d\n", rc);
    return rc;
}