Coverage Report

Created: 2023-11-19 07:36

/src/fluent-bit/src/record_accessor/flb_ra_parser.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*  Fluent Bit
4
 *  ==========
5
 *  Copyright (C) 2015-2022 The Fluent Bit Authors
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License");
8
 *  you may not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *      http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS,
15
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 */
19
20
#include <fluent-bit/flb_info.h>
21
#include <fluent-bit/flb_slist.h>
22
#include <fluent-bit/flb_mem.h>
23
#include <fluent-bit/flb_sds.h>
24
#include <fluent-bit/flb_log.h>
25
#include <fluent-bit/record_accessor/flb_ra_parser.h>
26
27
#include "ra_parser.h"
28
#include "ra_lex.h"
29
30
int flb_ra_parser_subkey_count(struct flb_ra_parser *rp)
31
0
{
32
0
    if (rp == NULL || rp->key == NULL) {
33
0
        return -1;
34
0
    }
35
0
    else if (rp->type != FLB_RA_PARSER_KEYMAP) {
36
0
        return 0;
37
0
    }
38
0
    else if(rp->key->subkeys == NULL) {
39
0
        return -1;
40
0
    }
41
42
0
    return mk_list_size(rp->key->subkeys);
43
0
}
44
45
void flb_ra_parser_dump(struct flb_ra_parser *rp)
46
294k
{
47
294k
    struct mk_list *head;
48
294k
    struct flb_ra_key *key;
49
294k
    struct flb_ra_subentry *entry;
50
51
294k
    key = rp->key;
52
294k
    if (rp->type == FLB_RA_PARSER_STRING) {
53
146k
        printf("type       : STRING\n");
54
146k
        printf("string     : '%s'\n", key->name);
55
146k
    }
56
294k
    if (rp->type == FLB_RA_PARSER_REGEX_ID) {
57
1.47k
        printf("type       : REGEX_ID\n");
58
1.47k
        printf("integer    : '%i'\n", rp->id);
59
1.47k
    }
60
294k
    if (rp->type == FLB_RA_PARSER_TAG) {
61
311
        printf("type       : TAG\n");
62
311
    }
63
294k
    if (rp->type == FLB_RA_PARSER_TAG_PART) {
64
652
        printf("type       : TAG[%i]\n", rp->id);
65
652
    }
66
293k
    else if (rp->type == FLB_RA_PARSER_KEYMAP) {
67
145k
        printf("type       : KEYMAP\n");
68
145k
        if (rp->key) {
69
144k
            printf("key name   : %s\n", key->name);
70
144k
            mk_list_foreach(head, key->subkeys) {
71
57.6k
                entry = mk_list_entry(head, struct flb_ra_subentry, _head);
72
57.6k
                if (entry->type == FLB_RA_PARSER_STRING) {
73
5.30k
                    printf(" - subkey  : %s\n", entry->str);
74
5.30k
                }
75
52.3k
                else if (entry->type == FLB_RA_PARSER_ARRAY_ID) {
76
52.3k
                    printf(" - array id: %i\n", entry->array_id);
77
52.3k
                }
78
57.6k
            }
79
144k
        }
80
145k
    }
81
294k
}
82
83
static void ra_parser_subentry_destroy_all(struct mk_list *list)
84
328k
{
85
328k
    struct mk_list *tmp;
86
328k
    struct mk_list *head;
87
328k
    struct flb_ra_subentry *entry;
88
89
328k
    mk_list_foreach_safe(head, tmp, list) {
90
80.6k
        entry = mk_list_entry(head, struct flb_ra_subentry, _head);
91
80.6k
        mk_list_del(&entry->_head);
92
80.6k
        if (entry->type == FLB_RA_PARSER_STRING) {
93
6.16k
            flb_sds_destroy(entry->str);
94
6.16k
        }
95
80.6k
        flb_free(entry);
96
80.6k
    }
97
328k
}
98
99
int flb_ra_parser_subentry_add_string(struct flb_ra_parser *rp, char *key)
100
6.67k
{
101
6.67k
    struct flb_ra_subentry *entry;
102
103
6.67k
    entry = flb_malloc(sizeof(struct flb_ra_subentry));
104
6.67k
    if (!entry) {
105
214
        flb_errno();
106
214
        return -1;
107
214
    }
108
109
6.46k
    entry->type = FLB_RA_PARSER_STRING;
110
6.46k
    entry->str = flb_sds_create(key);
111
6.46k
    if (!entry->str) {
112
299
        flb_errno();
113
299
        flb_free(entry);
114
299
        return -1;
115
299
    }
116
6.16k
    mk_list_add(&entry->_head, rp->slist);
117
118
6.16k
    return 0;
119
6.46k
}
120
121
int flb_ra_parser_subentry_add_array_id(struct flb_ra_parser *rp, int id)
122
74.6k
{
123
74.6k
    struct flb_ra_subentry *entry;
124
125
74.6k
    entry = flb_malloc(sizeof(struct flb_ra_subentry));
126
74.6k
    if (!entry) {
127
196
        flb_errno();
128
196
        return -1;
129
196
    }
130
131
74.4k
    entry->type = FLB_RA_PARSER_ARRAY_ID;
132
74.4k
    entry->array_id = id;
133
74.4k
    mk_list_add(&entry->_head, rp->slist);
134
135
74.4k
    return 0;
136
74.6k
}
137
138
139
struct flb_ra_key *flb_ra_parser_key_add(struct flb_ra_parser *rp, char *key)
140
161k
{
141
161k
    struct flb_ra_key *k;
142
143
161k
    k = flb_malloc(sizeof(struct flb_ra_key));
144
161k
    if (!k) {
145
230
        flb_errno();
146
230
        return NULL;
147
230
    }
148
149
160k
    k->name = flb_sds_create(key);
150
160k
    if (!k->name) {
151
540
        flb_errno();
152
540
        flb_free(k);
153
540
        return NULL;
154
540
    }
155
160k
    k->subkeys = NULL;
156
157
160k
    return k;
158
160k
}
159
160
struct flb_ra_array *flb_ra_parser_array_add(struct flb_ra_parser *rp, int index)
161
0
{
162
0
    struct flb_ra_array *arr;
163
164
0
    if (index < 0) {
165
0
        return NULL;
166
0
    }
167
168
0
    arr = flb_malloc(sizeof(struct flb_ra_array));
169
0
    if (!arr) {
170
0
        flb_errno();
171
0
        return NULL;
172
0
    }
173
174
0
    arr->index = index;
175
0
    arr->subkeys = NULL;
176
177
0
    return arr;
178
0
}
179
180
struct flb_ra_key *flb_ra_parser_string_add(struct flb_ra_parser *rp,
181
                                            char *str, int len)
182
0
{
183
0
    struct flb_ra_key *k;
184
185
0
    k = flb_malloc(sizeof(struct flb_ra_key));
186
0
    if (!k) {
187
0
        flb_errno();
188
0
        return NULL;
189
0
    }
190
191
0
    k->name = flb_sds_create_len(str, len);
192
0
    if (!k->name) {
193
0
        flb_errno();
194
0
        flb_free(k);
195
0
        return NULL;
196
0
    }
197
0
    k->subkeys = NULL;
198
199
0
    return k;
200
0
}
201
202
static struct flb_ra_parser *flb_ra_parser_create()
203
328k
{
204
328k
    struct flb_ra_parser *rp;
205
206
328k
    rp = flb_calloc(1, sizeof(struct flb_ra_parser));
207
328k
    if (!rp) {
208
5
        flb_errno();
209
5
        return NULL;
210
5
    }
211
328k
    rp->type = -1;
212
328k
    rp->key = NULL;
213
328k
    rp->slist = flb_malloc(sizeof(struct mk_list));
214
328k
    if (!rp->slist) {
215
5
        flb_errno();
216
5
        flb_free(rp);
217
5
        return NULL;
218
5
    }
219
328k
    mk_list_init(rp->slist);
220
221
328k
    return rp;
222
328k
}
223
224
struct flb_ra_parser *flb_ra_parser_string_create(char *str, int len)
225
163k
{
226
163k
    struct flb_ra_parser *rp;
227
228
163k
    rp = flb_ra_parser_create();
229
163k
    if (!rp) {
230
5
        flb_error("[record accessor] could not create string context");
231
5
        return NULL;
232
5
    }
233
234
163k
    rp->type = FLB_RA_PARSER_STRING;
235
163k
    rp->key = flb_malloc(sizeof(struct flb_ra_key));
236
163k
    if (!rp->key) {
237
2
        flb_errno();
238
2
        flb_ra_parser_destroy(rp);
239
2
        return NULL;
240
2
    }
241
163k
    rp->key->subkeys = NULL;
242
163k
    rp->key->name = flb_sds_create_len(str, len);
243
163k
    if (!rp->key->name) {
244
2
        flb_ra_parser_destroy(rp);
245
2
        return NULL;
246
2
    }
247
248
163k
    return rp;
249
163k
}
250
251
struct flb_ra_parser *flb_ra_parser_regex_id_create(int id)
252
1.84k
{
253
1.84k
    struct flb_ra_parser *rp;
254
255
1.84k
    rp = flb_ra_parser_create();
256
1.84k
    if (!rp) {
257
1
        flb_error("[record accessor] could not create string context");
258
1
        return NULL;
259
1
    }
260
261
1.84k
    rp->type = FLB_RA_PARSER_REGEX_ID;
262
1.84k
    rp->id = id;
263
1.84k
    return rp;
264
1.84k
}
265
266
struct flb_ra_parser *flb_ra_parser_tag_create()
267
451
{
268
451
    struct flb_ra_parser *rp;
269
270
451
    rp = flb_ra_parser_create();
271
451
    if (!rp) {
272
1
        flb_error("[record accessor] could not create tag context");
273
1
        return NULL;
274
1
    }
275
276
450
    rp->type = FLB_RA_PARSER_TAG;
277
450
    return rp;
278
451
}
279
280
struct flb_ra_parser *flb_ra_parser_tag_part_create(int id)
281
1.18k
{
282
1.18k
    struct flb_ra_parser *rp;
283
284
1.18k
    rp = flb_ra_parser_create();
285
1.18k
    if (!rp) {
286
1
        flb_error("[record accessor] could not create tag context");
287
1
        return NULL;
288
1
    }
289
290
1.18k
    rp->type = FLB_RA_PARSER_TAG_PART;
291
1.18k
    rp->id = id;
292
293
1.18k
    return rp;
294
1.18k
}
295
296
struct flb_ra_parser *flb_ra_parser_meta_create(char *str, int len)
297
161k
{
298
161k
    int ret;
299
161k
    yyscan_t scanner;
300
161k
    YY_BUFFER_STATE buf;
301
161k
    flb_sds_t s;
302
161k
    struct flb_ra_parser *rp;
303
161k
    struct flb_ra_key *key;
304
305
161k
    rp = flb_ra_parser_create();
306
161k
    if (!rp) {
307
2
        flb_error("[record accessor] could not create meta context");
308
2
        return NULL;
309
2
    }
310
311
    /* Temporal buffer of string with fixed length */
312
161k
    s = flb_sds_create_len(str, len);
313
161k
    if (!s) {
314
1
        flb_errno();
315
1
        flb_ra_parser_destroy(rp);
316
1
        return NULL;
317
1
    }
318
319
    /* Flex/Bison work */
320
161k
    flb_ra_lex_init(&scanner);
321
161k
    buf = flb_ra__scan_string(s, scanner);
322
323
161k
    ret = flb_ra_parse(rp, s, scanner);
324
325
    /* release resources */
326
161k
    flb_sds_destroy(s);
327
161k
    flb_ra__delete_buffer(buf, scanner);
328
161k
    flb_ra_lex_destroy(scanner);
329
330
    /* Finish structure mapping */
331
161k
    if (rp->type == FLB_RA_PARSER_KEYMAP) {
332
161k
        if (rp->key) {
333
160k
            key = rp->key;
334
160k
            key->subkeys = rp->slist;
335
160k
            rp->slist = NULL;
336
160k
        }
337
161k
    }
338
339
161k
    if (ret != 0) {
340
218
        flb_ra_parser_destroy(rp);
341
218
        return NULL;
342
218
    }
343
344
161k
    return rp;
345
161k
}
346
347
void flb_ra_parser_destroy(struct flb_ra_parser *rp)
348
328k
{
349
328k
    struct flb_ra_key *key;
350
351
328k
    key = rp->key;
352
328k
    if (key) {
353
324k
        flb_sds_destroy(key->name);
354
324k
        if (key->subkeys) {
355
160k
            ra_parser_subentry_destroy_all(key->subkeys);
356
160k
            flb_free(key->subkeys);
357
160k
        }
358
324k
        flb_free(rp->key);
359
324k
    }
360
328k
    if (rp->slist) {
361
168k
        ra_parser_subentry_destroy_all(rp->slist);
362
168k
        flb_free(rp->slist);
363
168k
    }
364
328k
    flb_free(rp);
365
328k
}