Coverage Report

Created: 2024-07-23 07:12

/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-2024 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
272k
{
47
272k
    struct mk_list *head;
48
272k
    struct flb_ra_key *key;
49
272k
    struct flb_ra_subentry *entry;
50
51
272k
    key = rp->key;
52
272k
    if (rp->type == FLB_RA_PARSER_STRING) {
53
135k
        printf("type       : STRING\n");
54
135k
        printf("string     : '%s'\n", key->name);
55
135k
    }
56
272k
    if (rp->type == FLB_RA_PARSER_REGEX_ID) {
57
1.94k
        printf("type       : REGEX_ID\n");
58
1.94k
        printf("integer    : '%i'\n", rp->id);
59
1.94k
    }
60
272k
    if (rp->type == FLB_RA_PARSER_TAG) {
61
311
        printf("type       : TAG\n");
62
311
    }
63
272k
    if (rp->type == FLB_RA_PARSER_TAG_PART) {
64
643
        printf("type       : TAG[%i]\n", rp->id);
65
643
    }
66
272k
    else if (rp->type == FLB_RA_PARSER_KEYMAP) {
67
133k
        printf("type       : KEYMAP\n");
68
133k
        if (rp->key) {
69
133k
            printf("key name   : %s\n", key->name);
70
133k
            mk_list_foreach(head, key->subkeys) {
71
58.7k
                entry = mk_list_entry(head, struct flb_ra_subentry, _head);
72
58.7k
                if (entry->type == FLB_RA_PARSER_STRING) {
73
6.13k
                    printf(" - subkey  : %s\n", entry->str);
74
6.13k
                }
75
52.6k
                else if (entry->type == FLB_RA_PARSER_ARRAY_ID) {
76
52.6k
                    printf(" - array id: %i\n", entry->array_id);
77
52.6k
                }
78
58.7k
            }
79
133k
        }
80
133k
    }
81
272k
}
82
83
static void ra_parser_subentry_destroy_all(struct mk_list *list)
84
355k
{
85
355k
    struct mk_list *tmp;
86
355k
    struct mk_list *head;
87
355k
    struct flb_ra_subentry *entry;
88
89
355k
    mk_list_foreach_safe(head, tmp, list) {
90
61.3k
        entry = mk_list_entry(head, struct flb_ra_subentry, _head);
91
61.3k
        mk_list_del(&entry->_head);
92
61.3k
        if (entry->type == FLB_RA_PARSER_STRING) {
93
7.22k
            flb_sds_destroy(entry->str);
94
7.22k
        }
95
61.3k
        flb_free(entry);
96
61.3k
    }
97
355k
}
98
99
int flb_ra_parser_subentry_add_string(struct flb_ra_parser *rp, char *key)
100
7.64k
{
101
7.64k
    struct flb_ra_subentry *entry;
102
103
7.64k
    entry = flb_malloc(sizeof(struct flb_ra_subentry));
104
7.64k
    if (!entry) {
105
217
        flb_errno();
106
217
        return -1;
107
217
    }
108
109
7.42k
    entry->type = FLB_RA_PARSER_STRING;
110
7.42k
    entry->str = flb_sds_create(key);
111
7.42k
    if (!entry->str) {
112
195
        flb_errno();
113
195
        flb_free(entry);
114
195
        return -1;
115
195
    }
116
7.22k
    mk_list_add(&entry->_head, rp->slist);
117
118
7.22k
    return 0;
119
7.42k
}
120
121
int flb_ra_parser_subentry_add_array_id(struct flb_ra_parser *rp, int id)
122
54.2k
{
123
54.2k
    struct flb_ra_subentry *entry;
124
125
54.2k
    entry = flb_malloc(sizeof(struct flb_ra_subentry));
126
54.2k
    if (!entry) {
127
211
        flb_errno();
128
211
        return -1;
129
211
    }
130
131
54.0k
    entry->type = FLB_RA_PARSER_ARRAY_ID;
132
54.0k
    entry->array_id = id;
133
54.0k
    mk_list_add(&entry->_head, rp->slist);
134
135
54.0k
    return 0;
136
54.2k
}
137
138
139
struct flb_ra_key *flb_ra_parser_key_add(struct flb_ra_parser *rp, char *key)
140
174k
{
141
174k
    struct flb_ra_key *k;
142
143
174k
    k = flb_malloc(sizeof(struct flb_ra_key));
144
174k
    if (!k) {
145
234
        flb_errno();
146
234
        return NULL;
147
234
    }
148
149
173k
    k->name = flb_sds_create(key);
150
173k
    if (!k->name) {
151
733
        flb_errno();
152
733
        flb_free(k);
153
733
        return NULL;
154
733
    }
155
173k
    k->subkeys = NULL;
156
157
173k
    return k;
158
173k
}
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
355k
{
204
355k
    struct flb_ra_parser *rp;
205
206
355k
    rp = flb_calloc(1, sizeof(struct flb_ra_parser));
207
355k
    if (!rp) {
208
6
        flb_errno();
209
6
        return NULL;
210
6
    }
211
355k
    rp->type = -1;
212
355k
    rp->key = NULL;
213
355k
    rp->slist = flb_malloc(sizeof(struct mk_list));
214
355k
    if (!rp->slist) {
215
6
        flb_errno();
216
6
        flb_free(rp);
217
6
        return NULL;
218
6
    }
219
355k
    mk_list_init(rp->slist);
220
221
355k
    return rp;
222
355k
}
223
224
struct flb_ra_parser *flb_ra_parser_string_create(char *str, int len)
225
176k
{
226
176k
    struct flb_ra_parser *rp;
227
228
176k
    rp = flb_ra_parser_create();
229
176k
    if (!rp) {
230
7
        flb_error("[record accessor] could not create string context");
231
7
        return NULL;
232
7
    }
233
234
176k
    rp->type = FLB_RA_PARSER_STRING;
235
176k
    rp->key = flb_malloc(sizeof(struct flb_ra_key));
236
176k
    if (!rp->key) {
237
2
        flb_errno();
238
2
        flb_ra_parser_destroy(rp);
239
2
        return NULL;
240
2
    }
241
176k
    rp->key->subkeys = NULL;
242
176k
    rp->key->name = flb_sds_create_len(str, len);
243
176k
    if (!rp->key->name) {
244
4
        flb_ra_parser_destroy(rp);
245
4
        return NULL;
246
4
    }
247
248
176k
    return rp;
249
176k
}
250
251
struct flb_ra_parser *flb_ra_parser_regex_id_create(int id)
252
2.33k
{
253
2.33k
    struct flb_ra_parser *rp;
254
255
2.33k
    rp = flb_ra_parser_create();
256
2.33k
    if (!rp) {
257
1
        flb_error("[record accessor] could not create string context");
258
1
        return NULL;
259
1
    }
260
261
2.33k
    rp->type = FLB_RA_PARSER_REGEX_ID;
262
2.33k
    rp->id = id;
263
2.33k
    return rp;
264
2.33k
}
265
266
struct flb_ra_parser *flb_ra_parser_tag_create()
267
614
{
268
614
    struct flb_ra_parser *rp;
269
270
614
    rp = flb_ra_parser_create();
271
614
    if (!rp) {
272
1
        flb_error("[record accessor] could not create tag context");
273
1
        return NULL;
274
1
    }
275
276
613
    rp->type = FLB_RA_PARSER_TAG;
277
613
    return rp;
278
614
}
279
280
struct flb_ra_parser *flb_ra_parser_tag_part_create(int id)
281
1.24k
{
282
1.24k
    struct flb_ra_parser *rp;
283
284
1.24k
    rp = flb_ra_parser_create();
285
1.24k
    if (!rp) {
286
2
        flb_error("[record accessor] could not create tag context");
287
2
        return NULL;
288
2
    }
289
290
1.24k
    rp->type = FLB_RA_PARSER_TAG_PART;
291
1.24k
    rp->id = id;
292
293
1.24k
    return rp;
294
1.24k
}
295
296
struct flb_ra_parser *flb_ra_parser_meta_create(char *str, int len)
297
174k
{
298
174k
    int ret;
299
174k
    yyscan_t scanner;
300
174k
    YY_BUFFER_STATE buf;
301
174k
    flb_sds_t s;
302
174k
    struct flb_ra_parser *rp;
303
174k
    struct flb_ra_key *key;
304
305
174k
    rp = flb_ra_parser_create();
306
174k
    if (!rp) {
307
1
        flb_error("[record accessor] could not create meta context");
308
1
        return NULL;
309
1
    }
310
311
    /* Temporal buffer of string with fixed length */
312
174k
    s = flb_sds_create_len(str, len);
313
174k
    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
174k
    flb_ra_lex_init(&scanner);
321
174k
    buf = flb_ra__scan_string(s, scanner);
322
323
174k
    ret = flb_ra_parse(rp, s, scanner);
324
325
    /* release resources */
326
174k
    flb_sds_destroy(s);
327
174k
    flb_ra__delete_buffer(buf, scanner);
328
174k
    flb_ra_lex_destroy(scanner);
329
330
    /* Finish structure mapping */
331
174k
    if (rp->type == FLB_RA_PARSER_KEYMAP) {
332
174k
        if (rp->key) {
333
173k
            key = rp->key;
334
173k
            key->subkeys = rp->slist;
335
173k
            rp->slist = NULL;
336
173k
        }
337
174k
    }
338
339
174k
    if (ret != 0) {
340
250
        flb_ra_parser_destroy(rp);
341
250
        return NULL;
342
250
    }
343
344
173k
    return rp;
345
174k
}
346
347
void flb_ra_parser_destroy(struct flb_ra_parser *rp)
348
355k
{
349
355k
    struct flb_ra_key *key;
350
351
355k
    key = rp->key;
352
355k
    if (key) {
353
349k
        flb_sds_destroy(key->name);
354
349k
        if (key->subkeys) {
355
173k
            ra_parser_subentry_destroy_all(key->subkeys);
356
173k
            flb_free(key->subkeys);
357
173k
        }
358
349k
        flb_free(rp->key);
359
349k
    }
360
355k
    if (rp->slist) {
361
182k
        ra_parser_subentry_destroy_all(rp->slist);
362
182k
        flb_free(rp->slist);
363
182k
    }
364
355k
    flb_free(rp);
365
355k
}