| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2013-2024 Pablo Neira Ayuso <pablo@netfilter.org> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 (or any | ||
| 6 | * later) as published by the Free Software Foundation. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <nft.h> | ||
| 10 | |||
| 11 | #include <ctype.h> | ||
| 12 | #include <stdio.h> | ||
| 13 | #include <stdlib.h> | ||
| 14 | #include <stdint.h> | ||
| 15 | #include <stdbool.h> | ||
| 16 | #include <string.h> | ||
| 17 | #include <utils.h> | ||
| 18 | |||
| 19 | #include "list.h" | ||
| 20 | #include "parser.h" | ||
| 21 | #include "erec.h" | ||
| 22 | |||
| 23 | struct str_buf { | ||
| 24 | uint8_t *str; | ||
| 25 | uint32_t len; | ||
| 26 | uint32_t size; | ||
| 27 | }; | ||
| 28 | |||
| 29 | #define STR_BUF_LEN 128 | ||
| 30 | |||
| 31 | 46 | static struct str_buf *str_buf_alloc(void) | |
| 32 | { | ||
| 33 | struct str_buf *buf; | ||
| 34 | |||
| 35 | 46 | buf = xzalloc(sizeof(*buf)); | |
| 36 | 46 | buf->str = xzalloc_array(1, STR_BUF_LEN); | |
| 37 | 46 | buf->size = STR_BUF_LEN; | |
| 38 | |||
| 39 | 46 | return buf; | |
| 40 | } | ||
| 41 | |||
| 42 | 50 | static int str_buf_add(struct str_buf *buf, const char *str, uint32_t len) | |
| 43 | { | ||
| 44 | uint8_t *tmp; | ||
| 45 | |||
| 46 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 50 times.
|
50 | if (len + buf->len > buf->size) { |
| 47 | ✗ | buf->size = (len + buf->len) * 2; | |
| 48 | ✗ | tmp = xrealloc(buf->str, buf->size); | |
| 49 | ✗ | buf->str = tmp; | |
| 50 | } | ||
| 51 | |||
| 52 | 50 | memcpy(&buf->str[buf->len], str, len); | |
| 53 | 50 | buf->len += len; | |
| 54 | |||
| 55 | 50 | return 0; | |
| 56 | } | ||
| 57 | |||
| 58 | struct str_chunk { | ||
| 59 | struct list_head list; | ||
| 60 | char *str; | ||
| 61 | uint32_t len; | ||
| 62 | bool is_sym; | ||
| 63 | }; | ||
| 64 | |||
| 65 | 50 | static void add_str_chunk(const char *x, int from, int to, struct list_head *list, bool is_sym) | |
| 66 | { | ||
| 67 | struct str_chunk *chunk; | ||
| 68 | 50 | int len = to - from; | |
| 69 | |||
| 70 | 50 | chunk = xzalloc_array(1, sizeof(*chunk)); | |
| 71 | 50 | chunk->str = xzalloc_array(1, len + 1); | |
| 72 | 50 | chunk->is_sym = is_sym; | |
| 73 | 50 | chunk->len = len; | |
| 74 | 50 | memcpy(chunk->str, &x[from], len); | |
| 75 | |||
| 76 | 50 | list_add_tail(&chunk->list, list); | |
| 77 | 50 | } | |
| 78 | |||
| 79 | 50 | static void free_str_chunk(struct str_chunk *chunk) | |
| 80 | { | ||
| 81 | 50 | free(chunk->str); | |
| 82 | 50 | free(chunk); | |
| 83 | 50 | } | |
| 84 | |||
| 85 | 46 | const char *str_preprocess(struct parser_state *state, struct location *loc, | |
| 86 | struct scope *scope, const char *x, | ||
| 87 | struct error_record **erec) | ||
| 88 | { | ||
| 89 | struct str_chunk *chunk, *next; | ||
| 90 | struct str_buf *buf; | ||
| 91 | const char *str; | ||
| 92 | int i, j, start; | ||
| 93 | 46 | LIST_HEAD(list); | |
| 94 | |||
| 95 | 46 | start = 0; | |
| 96 | 46 | i = 0; | |
| 97 | while (1) { | ||
| 98 |
2/2✓ Branch 0 (3→4) taken 46 times.
✓ Branch 1 (3→5) taken 520 times.
|
566 | if (x[i] == '\0') { |
| 99 | 46 | i++; | |
| 100 | 46 | break; | |
| 101 | } | ||
| 102 | |||
| 103 |
2/2✓ Branch 0 (5→6) taken 518 times.
✓ Branch 1 (5→7) taken 2 times.
|
520 | if (x[i] != '$') { |
| 104 | 518 | i++; | |
| 105 | 518 | continue; | |
| 106 | } | ||
| 107 | |||
| 108 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 2 times.
|
2 | if (isdigit(x[++i])) |
| 109 | ✗ | continue; | |
| 110 | |||
| 111 | 2 | j = i; | |
| 112 | while (1) { | ||
| 113 |
2/2✓ Branch 0 (10→11) taken 2 times.
✓ Branch 1 (10→13) taken 7 times.
|
9 | if (isalpha(x[i]) || |
| 114 |
1/2✓ Branch 0 (11→12) taken 2 times.
✗ Branch 1 (11→13) not taken.
|
2 | isdigit(x[i]) || |
| 115 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 2 times.
|
2 | x[i] == '_') { |
| 116 | 7 | i++; | |
| 117 | 7 | continue; | |
| 118 | } | ||
| 119 | 2 | break; | |
| 120 | } | ||
| 121 | 2 | add_str_chunk(x, start, j-1, &list, false); | |
| 122 | 2 | add_str_chunk(x, j, i, &list, true); | |
| 123 | 2 | start = i; | |
| 124 | } | ||
| 125 |
1/2✓ Branch 0 (20→21) taken 46 times.
✗ Branch 1 (20→22) not taken.
|
46 | if (start != i) |
| 126 | 46 | add_str_chunk(x, start, i, &list, false); | |
| 127 | |||
| 128 | 46 | buf = str_buf_alloc(); | |
| 129 | |||
| 130 |
2/2✓ Branch 0 (39→24) taken 50 times.
✓ Branch 1 (39→40) taken 46 times.
|
96 | list_for_each_entry_safe(chunk, next, &list, list) { |
| 131 |
2/2✓ Branch 0 (24→25) taken 2 times.
✓ Branch 1 (24→35) taken 48 times.
|
50 | if (chunk->is_sym) { |
| 132 | struct symbol *sym; | ||
| 133 | |||
| 134 | 2 | sym = symbol_lookup(scope, chunk->str); | |
| 135 |
1/2✗ Branch 0 (26→27) not taken.
✓ Branch 1 (26→34) taken 2 times.
|
2 | if (!sym) { |
| 136 | ✗ | sym = symbol_lookup_fuzzy(scope, chunk->str); | |
| 137 | ✗ | if (sym) { | |
| 138 | ✗ | *erec = error(loc, "unknown identifier '%s'; " | |
| 139 | "did you mean identifier '%s'?", | ||
| 140 | chunk->str, sym->identifier); | ||
| 141 | } else { | ||
| 142 | ✗ | *erec = error(loc, "unknown identifier '%s'", | |
| 143 | chunk->str); | ||
| 144 | } | ||
| 145 | ✗ | goto err; | |
| 146 | } | ||
| 147 | 2 | str_buf_add(buf, sym->expr->identifier, | |
| 148 | 2 | strlen(sym->expr->identifier)); | |
| 149 | } else { | ||
| 150 | 48 | str_buf_add(buf, chunk->str, chunk->len); | |
| 151 | } | ||
| 152 | 50 | list_del(&chunk->list); | |
| 153 | 50 | free_str_chunk(chunk); | |
| 154 | } | ||
| 155 | |||
| 156 | 46 | str = (char *)buf->str; | |
| 157 | |||
| 158 | 46 | free(buf); | |
| 159 | |||
| 160 | 46 | return (char *)str; | |
| 161 | ✗ | err: | |
| 162 | ✗ | list_for_each_entry_safe(chunk, next, &list, list) { | |
| 163 | ✗ | list_del(&chunk->list); | |
| 164 | ✗ | free_str_chunk(chunk); | |
| 165 | } | ||
| 166 | ✗ | free(buf->str); | |
| 167 | ✗ | free(buf); | |
| 168 | |||
| 169 | ✗ | return NULL; | |
| 170 | } | ||
| 171 |