| File: | builds/wireshark/wireshark/epan/color_filters.c |
| Warning: | line 1129, column 13 File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* color_filters.c | ||||
| 2 | * Routines for color filters | ||||
| 3 | * | ||||
| 4 | * Wireshark - Network traffic analyzer | ||||
| 5 | * By Gerald Combs <[email protected]> | ||||
| 6 | * Copyright 1998 Gerald Combs | ||||
| 7 | * | ||||
| 8 | * SPDX-License-Identifier: GPL-2.0-or-later | ||||
| 9 | */ | ||||
| 10 | /* | ||||
| 11 | * Updated 1 Dec 10 jjm | ||||
| 12 | */ | ||||
| 13 | |||||
| 14 | #include "config.h" | ||||
| 15 | #define WS_LOG_DOMAIN"Epan" LOG_DOMAIN_EPAN"Epan" | ||||
| 16 | |||||
| 17 | #include <glib.h> | ||||
| 18 | |||||
| 19 | #include <errno(*__errno_location ()).h> | ||||
| 20 | #include <stdio.h> | ||||
| 21 | #include <stdlib.h> | ||||
| 22 | #include <string.h> | ||||
| 23 | |||||
| 24 | #include <wsutil/filesystem.h> | ||||
| 25 | #include <wsutil/file_util.h> | ||||
| 26 | #include <wsutil/report_message.h> | ||||
| 27 | #include <wsutil/wslog.h> | ||||
| 28 | #include <wsutil/ws_assert.h> | ||||
| 29 | #include <wsutil/wmem/wmem_list.h> | ||||
| 30 | #include <wsutil/wsjson.h> | ||||
| 31 | #include <wsutil/json_dumper.h> | ||||
| 32 | |||||
| 33 | #include <epan/packet.h> | ||||
| 34 | #include "color_filters.h" | ||||
| 35 | #include "file.h" | ||||
| 36 | #include <epan/dfilter/dfilter.h> | ||||
| 37 | #include <epan/prefs.h> | ||||
| 38 | #include <epan/epan_dissect.h> | ||||
| 39 | |||||
| 40 | /* | ||||
| 41 | * The coloring_rules.jsonc file is formatted as JSONC (jsonc.org). It | ||||
| 42 | * must contain an array of named objects with the following key:value | ||||
| 43 | * pairs: | ||||
| 44 | * [ | ||||
| 45 | * { "Filter name": { | ||||
| 46 | * "filter": "<display filter string>", | ||||
| 47 | * "foreground": "#rrggbb or rrggbb", | ||||
| 48 | * "background": "#rrggbb or rrggbb", | ||||
| 49 | * "enabled": "<boolean value>", | ||||
| 50 | * } | ||||
| 51 | * } | ||||
| 52 | * ] | ||||
| 53 | * | ||||
| 54 | * Each line in the legacy colorfilters file has the following format: | ||||
| 55 | * | ||||
| 56 | * @<filter name>@<filter string>@[<background>][<foreground>] | ||||
| 57 | * Background and foreground colors are 16-bit comma-separated RGB | ||||
| 58 | * triplets. Colors are 16 bits because that's what GdkColor used. | ||||
| 59 | * We might want to use a more standard, copy+paste-able color scheme | ||||
| 60 | * such as #RRGGBB instead. | ||||
| 61 | */ | ||||
| 62 | |||||
| 63 | static int read_filters_file(const char *path, FILE *f, void *user_data, color_filter_add_cb_func add_cb, bool_Bool require_json, bool_Bool keep_compiled); | ||||
| 64 | static int read_legacy_filters_file(const char *path, FILE *f, void *user_data, color_filter_add_cb_func add_cb, bool_Bool keep_compiled); | ||||
| 65 | static struct epan_dfilter *color_filter_compile_read(const char *name, const char *text, bool_Bool *enabled, | ||||
| 66 | bool_Bool compile_disabled, const char *path, | ||||
| 67 | const char *file_kind); | ||||
| 68 | |||||
| 69 | /* the currently active filters */ | ||||
| 70 | static GSList *color_filter_list; | ||||
| 71 | |||||
| 72 | /* keep "old" deleted filters in this list until | ||||
| 73 | * the dissection no longer needs them (e.g. file is closed) */ | ||||
| 74 | static GSList *color_filter_deleted_list; | ||||
| 75 | static GSList *color_filter_valid_list; | ||||
| 76 | |||||
| 77 | /* Color Filters can en-/disabled. */ | ||||
| 78 | static bool_Bool filters_enabled = true1; | ||||
| 79 | |||||
| 80 | /* Remember if there are temporary coloring filters set to | ||||
| 81 | * add sensitivity to the "Reset Coloring 1-10" menu item | ||||
| 82 | */ | ||||
| 83 | static bool_Bool tmp_colors_set; | ||||
| 84 | |||||
| 85 | /* Create a new filter */ | ||||
| 86 | color_filter_t * | ||||
| 87 | color_filter_new(const char *name, /* The name of the filter to create */ | ||||
| 88 | const char *filter_string, /* The string representing the filter */ | ||||
| 89 | color_t *bg_color, /* The background color */ | ||||
| 90 | color_t *fg_color, /* The foreground color */ | ||||
| 91 | bool_Bool enabled) /* Is the filter enabled? */ | ||||
| 92 | { | ||||
| 93 | color_filter_t *colorf; | ||||
| 94 | |||||
| 95 | colorf = g_new0(color_filter_t, 1)((color_filter_t *) g_malloc0_n ((1), sizeof (color_filter_t) )); | ||||
| 96 | colorf->filter_name = g_strdup(name)g_strdup_inline (name); | ||||
| 97 | colorf->filter_text = g_strdup(filter_string)g_strdup_inline (filter_string); | ||||
| 98 | colorf->bg_color = *bg_color; | ||||
| 99 | colorf->fg_color = *fg_color; | ||||
| 100 | colorf->enabled = enabled; | ||||
| 101 | return colorf; | ||||
| 102 | } | ||||
| 103 | |||||
| 104 | /* Add ten empty (temporary) colorfilters for easy coloring */ | ||||
| 105 | static void | ||||
| 106 | color_filters_add_tmp(GSList **cfl) | ||||
| 107 | { | ||||
| 108 | char *name = NULL((void*)0); | ||||
| 109 | uint32_t i; | ||||
| 110 | char** bg_colors; | ||||
| 111 | char** fg_colors; | ||||
| 112 | unsigned long cval; | ||||
| 113 | color_t bg_color, fg_color; | ||||
| 114 | color_filter_t *colorf; | ||||
| 115 | |||||
| 116 | ws_assert(strlen(prefs.gui_colorized_fg)==69)do { if ((1) && !(strlen(prefs.gui_colorized_fg)==69) ) ws_log_fatal_full("Epan", LOG_LEVEL_ERROR, "epan/color_filters.c" , 116, __func__, "assertion failed: %s", "strlen(prefs.gui_colorized_fg)==69" ); } while (0); | ||||
| 117 | ws_assert(strlen(prefs.gui_colorized_bg)==69)do { if ((1) && !(strlen(prefs.gui_colorized_bg)==69) ) ws_log_fatal_full("Epan", LOG_LEVEL_ERROR, "epan/color_filters.c" , 117, __func__, "assertion failed: %s", "strlen(prefs.gui_colorized_bg)==69" ); } while (0); | ||||
| 118 | fg_colors = g_strsplit(prefs.gui_colorized_fg, ",", -1); | ||||
| 119 | bg_colors = g_strsplit(prefs.gui_colorized_bg, ",", -1); | ||||
| 120 | |||||
| 121 | for ( i=1 ; i<=10 ; i++ ) { | ||||
| 122 | name = ws_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i)wmem_strdup_printf(((void*)0), "%s%02d","___conversation_color_filter___" ,i); | ||||
| 123 | |||||
| 124 | /* retrieve background and foreground colors */ | ||||
| 125 | cval = strtoul(fg_colors[i-1], NULL((void*)0), 16); | ||||
| 126 | fg_color.red = RED_COMPONENT(cval)(uint16_t) (((((cval) >> 16) & 0xff) * 65535 / 255) ); | ||||
| 127 | fg_color.green = GREEN_COMPONENT(cval)(uint16_t) (((((cval) >> 8) & 0xff) * 65535 / 255)); | ||||
| 128 | fg_color.blue = BLUE_COMPONENT(cval)(uint16_t) ( (((cval) & 0xff) * 65535 / 255)); | ||||
| 129 | cval = strtoul(bg_colors[i-1], NULL((void*)0), 16); | ||||
| 130 | bg_color.red = RED_COMPONENT(cval)(uint16_t) (((((cval) >> 16) & 0xff) * 65535 / 255) ); | ||||
| 131 | bg_color.green = GREEN_COMPONENT(cval)(uint16_t) (((((cval) >> 8) & 0xff) * 65535 / 255)); | ||||
| 132 | bg_color.blue = BLUE_COMPONENT(cval)(uint16_t) ( (((cval) & 0xff) * 65535 / 255)); | ||||
| 133 | colorf = color_filter_new(name, NULL((void*)0), &bg_color, &fg_color, false0); | ||||
| 134 | colorf->filter_text = g_strdup("frame")g_strdup_inline ("frame"); | ||||
| 135 | *cfl = g_slist_append(*cfl, colorf); | ||||
| 136 | |||||
| 137 | g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized (name, __builtin_object_size ((name), 0)) : (g_free) (name); | ||||
| 138 | } | ||||
| 139 | |||||
| 140 | g_strfreev(fg_colors); | ||||
| 141 | g_strfreev(bg_colors); | ||||
| 142 | } | ||||
| 143 | |||||
| 144 | static int | ||||
| 145 | color_filters_find_by_name_cb(const void *arg1, const void *arg2) | ||||
| 146 | { | ||||
| 147 | const color_filter_t *colorf = (const color_filter_t *)arg1; | ||||
| 148 | const char *name = (const char *)arg2; | ||||
| 149 | |||||
| 150 | return strcmp(colorf->filter_name, name); | ||||
| 151 | } | ||||
| 152 | |||||
| 153 | /* Get the filter of a temporary color filter */ | ||||
| 154 | char* | ||||
| 155 | color_filters_get_tmp(uint8_t filt_nr) | ||||
| 156 | { | ||||
| 157 | char* name = NULL((void*)0); | ||||
| 158 | char* filter = NULL((void*)0); | ||||
| 159 | GSList* cfl; | ||||
| 160 | color_filter_t* colorf; | ||||
| 161 | /* Only perform a lookup if the supplied filter number is in the expected range */ | ||||
| 162 | if (filt_nr < 1 || filt_nr > 10) | ||||
| 163 | return NULL((void*)0); | ||||
| 164 | |||||
| 165 | name = ws_strdup_printf("%s%02d", CONVERSATION_COLOR_PREFIX, filt_nr)wmem_strdup_printf(((void*)0), "%s%02d", "___conversation_color_filter___" , filt_nr); | ||||
| 166 | cfl = g_slist_find_custom(color_filter_list, name, color_filters_find_by_name_cb); | ||||
| 167 | colorf = (color_filter_t*)cfl->data; | ||||
| 168 | |||||
| 169 | if (colorf->enabled) | ||||
| 170 | filter = g_strdup(colorf->filter_text)g_strdup_inline (colorf->filter_text); | ||||
| 171 | |||||
| 172 | g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized (name, __builtin_object_size ((name), 0)) : (g_free) (name); | ||||
| 173 | |||||
| 174 | return filter; | ||||
| 175 | } | ||||
| 176 | |||||
| 177 | /* Set the filter off a temporary colorfilters and enable it */ | ||||
| 178 | bool_Bool | ||||
| 179 | color_filters_set_tmp(uint8_t filt_nr, const char *filter, bool_Bool enabled, char **err_msg) | ||||
| 180 | { | ||||
| 181 | char *name = NULL((void*)0); | ||||
| 182 | const char *tmpfilter = NULL((void*)0); | ||||
| 183 | GSList *cfl; | ||||
| 184 | color_filter_t *colorf; | ||||
| 185 | dfilter_t *compiled_filter; | ||||
| 186 | uint8_t i; | ||||
| 187 | df_error_t *df_err = NULL((void*)0); | ||||
| 188 | /* Go through the temporary filters and look for the same filter string. | ||||
| 189 | * If found, clear it so that a filter can be "moved" up and down the list | ||||
| 190 | */ | ||||
| 191 | for ( i=1 ; i<=10 ; i++ ) { | ||||
| 192 | /* If we need to reset the temporary filter (filter==NULL), don't look | ||||
| 193 | * for other rules with the same filter string | ||||
| 194 | */ | ||||
| 195 | if( i!=filt_nr && filter==NULL((void*)0) ) | ||||
| 196 | continue; | ||||
| 197 | |||||
| 198 | name = ws_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i)wmem_strdup_printf(((void*)0), "%s%02d","___conversation_color_filter___" ,i); | ||||
| 199 | cfl = g_slist_find_custom(color_filter_list, name, color_filters_find_by_name_cb); | ||||
| 200 | colorf = (color_filter_t *)cfl->data; | ||||
| 201 | |||||
| 202 | /* Only change the filter rule if this is the rule to change or if | ||||
| 203 | * a matching filter string has been found | ||||
| 204 | */ | ||||
| 205 | if(colorf && ( i == filt_nr || filter == NULL((void*)0) || !strcmp(filter, colorf->filter_text) ) ) { | ||||
| 206 | /* set filter string to "frame" if we are resetting the rules | ||||
| 207 | * or if we found a matching filter string which need to be cleared | ||||
| 208 | */ | ||||
| 209 | tmpfilter = ( (filter==NULL((void*)0)) || (i!=filt_nr) ) ? "frame" : filter; | ||||
| 210 | if (!dfilter_compile(tmpfilter, &compiled_filter, &df_err)dfilter_compile_full(tmpfilter, &compiled_filter, &df_err , (1U << 1)|(1U << 2), __func__)) { | ||||
| 211 | *err_msg = ws_strdup_printf( "Could not compile color filter name: \"%s\" text: \"%s\".\n%s", name, filter, df_err->msg)wmem_strdup_printf(((void*)0), "Could not compile color filter name: \"%s\" text: \"%s\".\n%s" , name, filter, df_err->msg); | ||||
| 212 | df_error_free(&df_err); | ||||
| 213 | g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized (name, __builtin_object_size ((name), 0)) : (g_free) (name); | ||||
| 214 | return false0; | ||||
| 215 | } else { | ||||
| 216 | g_free(colorf->filter_text)(__builtin_object_size ((colorf->filter_text), 0) != ((size_t ) - 1)) ? g_free_sized (colorf->filter_text, __builtin_object_size ((colorf->filter_text), 0)) : (g_free) (colorf->filter_text ); | ||||
| 217 | dfilter_free(colorf->c_colorfilter); | ||||
| 218 | colorf->filter_text = g_strdup(tmpfilter)g_strdup_inline (tmpfilter); | ||||
| 219 | colorf->c_colorfilter = compiled_filter; | ||||
| 220 | colorf->enabled = ((i!=filt_nr) ? false0 : enabled); | ||||
| 221 | /* Remember that there are now temporary coloring filters set */ | ||||
| 222 | if( filter ) | ||||
| 223 | tmp_colors_set = true1; | ||||
| 224 | } | ||||
| 225 | } | ||||
| 226 | g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized (name, __builtin_object_size ((name), 0)) : (g_free) (name); | ||||
| 227 | } | ||||
| 228 | return true1; | ||||
| 229 | } | ||||
| 230 | |||||
| 231 | const color_filter_t * | ||||
| 232 | color_filters_tmp_color(uint8_t filter_num) { | ||||
| 233 | char *name; | ||||
| 234 | color_filter_t *colorf = NULL((void*)0); | ||||
| 235 | GSList *cfl; | ||||
| 236 | |||||
| 237 | name = ws_strdup_printf("%s%02d", CONVERSATION_COLOR_PREFIX, filter_num)wmem_strdup_printf(((void*)0), "%s%02d", "___conversation_color_filter___" , filter_num); | ||||
| 238 | cfl = g_slist_find_custom(color_filter_list, name, color_filters_find_by_name_cb); | ||||
| 239 | if (cfl) { | ||||
| 240 | colorf = (color_filter_t *)cfl->data; | ||||
| 241 | } | ||||
| 242 | g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized (name, __builtin_object_size ((name), 0)) : (g_free) (name); | ||||
| 243 | |||||
| 244 | return colorf; | ||||
| 245 | } | ||||
| 246 | |||||
| 247 | /* Reset the temporary colorfilters */ | ||||
| 248 | bool_Bool | ||||
| 249 | color_filters_reset_tmp(char **err_msg) | ||||
| 250 | { | ||||
| 251 | uint8_t i; | ||||
| 252 | |||||
| 253 | for ( i=1 ; i<=10 ; i++ ) { | ||||
| 254 | if (!color_filters_set_tmp(i, NULL((void*)0), false0, err_msg)) | ||||
| 255 | return false0; | ||||
| 256 | } | ||||
| 257 | /* Remember that there are now *no* temporary coloring filters set */ | ||||
| 258 | tmp_colors_set = false0; | ||||
| 259 | return true1; | ||||
| 260 | } | ||||
| 261 | |||||
| 262 | /* delete the specified filter */ | ||||
| 263 | void | ||||
| 264 | color_filter_delete(color_filter_t *colorf) | ||||
| 265 | { | ||||
| 266 | g_free(colorf->filter_name)(__builtin_object_size ((colorf->filter_name), 0) != ((size_t ) - 1)) ? g_free_sized (colorf->filter_name, __builtin_object_size ((colorf->filter_name), 0)) : (g_free) (colorf->filter_name ); | ||||
| 267 | g_free(colorf->filter_text)(__builtin_object_size ((colorf->filter_text), 0) != ((size_t ) - 1)) ? g_free_sized (colorf->filter_text, __builtin_object_size ((colorf->filter_text), 0)) : (g_free) (colorf->filter_text ); | ||||
| 268 | g_free(colorf->source_profile)(__builtin_object_size ((colorf->source_profile), 0) != (( size_t) - 1)) ? g_free_sized (colorf->source_profile, __builtin_object_size ((colorf->source_profile), 0)) : (g_free) (colorf->source_profile ); | ||||
| 269 | dfilter_free(colorf->c_colorfilter); | ||||
| 270 | g_free(colorf)(__builtin_object_size ((colorf), 0) != ((size_t) - 1)) ? g_free_sized (colorf, __builtin_object_size ((colorf), 0)) : (g_free) (colorf ); | ||||
| 271 | } | ||||
| 272 | |||||
| 273 | /* delete the specified filter (called from g_slist_foreach) */ | ||||
| 274 | static void | ||||
| 275 | color_filter_delete_cb(void *filter_arg) | ||||
| 276 | { | ||||
| 277 | color_filter_t *colorf = (color_filter_t *)filter_arg; | ||||
| 278 | |||||
| 279 | color_filter_delete(colorf); | ||||
| 280 | } | ||||
| 281 | |||||
| 282 | /* delete the specified list */ | ||||
| 283 | void | ||||
| 284 | color_filter_list_delete(GSList **cfl) | ||||
| 285 | { | ||||
| 286 | g_slist_free_full(*cfl, color_filter_delete_cb); | ||||
| 287 | *cfl = NULL((void*)0); | ||||
| 288 | } | ||||
| 289 | |||||
| 290 | /* clone a single list entries from normal to edit list */ | ||||
| 291 | static color_filter_t * | ||||
| 292 | color_filter_clone(color_filter_t *colorf) | ||||
| 293 | { | ||||
| 294 | color_filter_t *new_colorf; | ||||
| 295 | |||||
| 296 | new_colorf = g_new(color_filter_t, 1)((color_filter_t *) g_malloc_n ((1), sizeof (color_filter_t)) ); | ||||
| 297 | new_colorf->filter_name = g_strdup(colorf->filter_name)g_strdup_inline (colorf->filter_name); | ||||
| 298 | new_colorf->filter_text = g_strdup(colorf->filter_text)g_strdup_inline (colorf->filter_text); | ||||
| 299 | new_colorf->bg_color = colorf->bg_color; | ||||
| 300 | new_colorf->fg_color = colorf->fg_color; | ||||
| 301 | new_colorf->enabled = colorf->enabled; | ||||
| 302 | new_colorf->source_profile = g_strdup(colorf->source_profile)g_strdup_inline (colorf->source_profile); | ||||
| 303 | new_colorf->c_colorfilter = NULL((void*)0); | ||||
| 304 | |||||
| 305 | return new_colorf; | ||||
| 306 | } | ||||
| 307 | |||||
| 308 | static void | ||||
| 309 | color_filter_list_clone_cb(void *filter_arg, void *cfl_arg) | ||||
| 310 | { | ||||
| 311 | GSList **cfl = (GSList **)cfl_arg; | ||||
| 312 | color_filter_t *new_colorf; | ||||
| 313 | |||||
| 314 | new_colorf = color_filter_clone((color_filter_t *)filter_arg); | ||||
| 315 | *cfl = g_slist_append(*cfl, new_colorf); | ||||
| 316 | } | ||||
| 317 | |||||
| 318 | /* clone the specified list */ | ||||
| 319 | static GSList * | ||||
| 320 | color_filter_list_clone(GSList *cfl) | ||||
| 321 | { | ||||
| 322 | GSList *new_list = NULL((void*)0); | ||||
| 323 | |||||
| 324 | g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list); | ||||
| 325 | |||||
| 326 | return new_list; | ||||
| 327 | } | ||||
| 328 | |||||
| 329 | /* | ||||
| 330 | * Read the personal rules of the current profile into the list used for | ||||
| 331 | * matching. Without a personal file, the global rules are read if | ||||
| 332 | * fallback_to_globals is set. | ||||
| 333 | */ | ||||
| 334 | static bool_Bool | ||||
| 335 | color_filters_get_own(char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix, | ||||
| 336 | bool_Bool read_own, bool_Bool fallback_to_globals) | ||||
| 337 | { | ||||
| 338 | char *path; | ||||
| 339 | char *legacy_path; | ||||
| 340 | FILE *f; | ||||
| 341 | int ret; | ||||
| 342 | |||||
| 343 | /* | ||||
| 344 | * Resolve both personal paths up front, although at most one file is | ||||
| 345 | * read. get_persconffile_path() registers each name as a profile file, | ||||
| 346 | * which makes profile copy and the Default profile reset include it. | ||||
| 347 | * Resolving the legacy name only when it is read would let a reset | ||||
| 348 | * remove coloring_rules.jsonc but keep a stale colorfilters, whose | ||||
| 349 | * rules would then come back on the next start. | ||||
| 350 | */ | ||||
| 351 | path = get_persconffile_path(COLORING_RULES_FILE_NAME"coloring_rules.jsonc", true1, app_env_var_prefix); | ||||
| 352 | legacy_path = get_persconffile_path(COLORFILTERS_FILE_NAME"colorfilters", true1, app_env_var_prefix); | ||||
| 353 | |||||
| 354 | if (!read_own) { | ||||
| 355 | /* Only entries from the parent profiles are used */ | ||||
| 356 | g_free(legacy_path)(__builtin_object_size ((legacy_path), 0) != ((size_t) - 1)) ? g_free_sized (legacy_path, __builtin_object_size ((legacy_path ), 0)) : (g_free) (legacy_path); | ||||
| 357 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 358 | return true1; | ||||
| 359 | } | ||||
| 360 | |||||
| 361 | /* | ||||
| 362 | * If the personal coloring_rules.jsonc exists, it is the user's rule | ||||
| 363 | * list and the only file read. A legacy colorfilters file next to it | ||||
| 364 | * is an older copy kept for older versions; reading it as well would | ||||
| 365 | * bring back rules the user has since deleted or changed. | ||||
| 366 | */ | ||||
| 367 | if ((f = ws_fopenfopen(path, "r")) != NULL((void*)0)) { | ||||
| 368 | ret = read_filters_file(path, f, &color_filter_list, add_cb, true1, false0); | ||||
| 369 | fclose(f); | ||||
| 370 | if (ret != 0) { | ||||
| 371 | *err_msg = ws_strdup_printf("Error reading coloring rules file\n\"%s\": %s.",wmem_strdup_printf(((void*)0), "Error reading coloring rules file\n\"%s\": %s." , path, g_strerror(ret)) | ||||
| 372 | path, g_strerror(ret))wmem_strdup_printf(((void*)0), "Error reading coloring rules file\n\"%s\": %s." , path, g_strerror(ret)); | ||||
| 373 | g_free(legacy_path)(__builtin_object_size ((legacy_path), 0) != ((size_t) - 1)) ? g_free_sized (legacy_path, __builtin_object_size ((legacy_path ), 0)) : (g_free) (legacy_path); | ||||
| 374 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 375 | return false0; | ||||
| 376 | } | ||||
| 377 | g_free(legacy_path)(__builtin_object_size ((legacy_path), 0) != ((size_t) - 1)) ? g_free_sized (legacy_path, __builtin_object_size ((legacy_path ), 0)) : (g_free) (legacy_path); | ||||
| 378 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 379 | return true1; | ||||
| 380 | } | ||||
| 381 | if (errno(*__errno_location ()) != ENOENT2) { | ||||
| 382 | *err_msg = ws_strdup_printf("Could not open coloring rules file\n\"%s\": %s.", path,wmem_strdup_printf(((void*)0), "Could not open coloring rules file\n\"%s\": %s." , path, g_strerror((*__errno_location ()))) | ||||
| 383 | g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open coloring rules file\n\"%s\": %s." , path, g_strerror((*__errno_location ()))); | ||||
| 384 | g_free(legacy_path)(__builtin_object_size ((legacy_path), 0) != ((size_t) - 1)) ? g_free_sized (legacy_path, __builtin_object_size ((legacy_path ), 0)) : (g_free) (legacy_path); | ||||
| 385 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 386 | return false0; | ||||
| 387 | } | ||||
| 388 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 389 | |||||
| 390 | /* | ||||
| 391 | * No personal coloring_rules.jsonc. A legacy colorfilters file from a | ||||
| 392 | * version before the JSONC format is the complete personal rule list, | ||||
| 393 | * so the global rules are not added to it. The next save writes it | ||||
| 394 | * out as coloring_rules.jsonc. | ||||
| 395 | */ | ||||
| 396 | if ((f = ws_fopenfopen(legacy_path, "r")) != NULL((void*)0)) { | ||||
| 397 | ret = read_legacy_filters_file(legacy_path, f, &color_filter_list, add_cb, false0); | ||||
| 398 | fclose(f); | ||||
| 399 | if (ret != 0) { | ||||
| 400 | *err_msg = ws_strdup_printf("Error reading filter file\n\"%s\": %s.",wmem_strdup_printf(((void*)0), "Error reading filter file\n\"%s\": %s." , legacy_path, g_strerror(ret)) | ||||
| 401 | legacy_path, g_strerror(ret))wmem_strdup_printf(((void*)0), "Error reading filter file\n\"%s\": %s." , legacy_path, g_strerror(ret)); | ||||
| 402 | g_free(legacy_path)(__builtin_object_size ((legacy_path), 0) != ((size_t) - 1)) ? g_free_sized (legacy_path, __builtin_object_size ((legacy_path ), 0)) : (g_free) (legacy_path); | ||||
| 403 | return false0; | ||||
| 404 | } | ||||
| 405 | g_free(legacy_path)(__builtin_object_size ((legacy_path), 0) != ((size_t) - 1)) ? g_free_sized (legacy_path, __builtin_object_size ((legacy_path ), 0)) : (g_free) (legacy_path); | ||||
| 406 | return true1; | ||||
| 407 | } | ||||
| 408 | if (errno(*__errno_location ()) != ENOENT2) { | ||||
| 409 | *err_msg = ws_strdup_printf("Could not open filter file\n\"%s\": %s.", legacy_path,wmem_strdup_printf(((void*)0), "Could not open filter file\n\"%s\": %s." , legacy_path, g_strerror((*__errno_location ()))) | ||||
| 410 | g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open filter file\n\"%s\": %s." , legacy_path, g_strerror((*__errno_location ()))); | ||||
| 411 | g_free(legacy_path)(__builtin_object_size ((legacy_path), 0) != ((size_t) - 1)) ? g_free_sized (legacy_path, __builtin_object_size ((legacy_path ), 0)) : (g_free) (legacy_path); | ||||
| 412 | return false0; | ||||
| 413 | } | ||||
| 414 | g_free(legacy_path)(__builtin_object_size ((legacy_path), 0) != ((size_t) - 1)) ? g_free_sized (legacy_path, __builtin_object_size ((legacy_path ), 0)) : (g_free) (legacy_path); | ||||
| 415 | |||||
| 416 | if (!fallback_to_globals) { | ||||
| 417 | /* Rules are inherited from the parent profiles instead */ | ||||
| 418 | return true1; | ||||
| 419 | } | ||||
| 420 | |||||
| 421 | /* They don't have any personal rules; try to read the global rules */ | ||||
| 422 | return color_filters_read_globals(&color_filter_list, err_msg, add_cb, app_env_var_prefix); | ||||
| 423 | } | ||||
| 424 | |||||
| 425 | /* Collect rules read from a parent profile, in file order */ | ||||
| 426 | static void | ||||
| 427 | color_filters_collect_cb(color_filter_t *colorf, void *user_data) | ||||
| 428 | { | ||||
| 429 | GSList **cfl = (GSList **)user_data; | ||||
| 430 | |||||
| 431 | *cfl = g_slist_append(*cfl, colorf); | ||||
| 432 | } | ||||
| 433 | |||||
| 434 | typedef struct { | ||||
| 435 | color_filter_add_cb_func add_cb; /* called for each inherited rule */ | ||||
| 436 | void *user_data; | ||||
| 437 | bool_Bool keep_compiled; /* the rules are used for matching, so they are compiled when read */ | ||||
| 438 | } color_filters_layer_read_t; | ||||
| 439 | |||||
| 440 | /* | ||||
| 441 | * Read the rules of one parent profile. A rule is skipped if it is a | ||||
| 442 | * conversation rule, if its name was seen in a nearer profile, | ||||
| 443 | * or if it is disabled; a disabled rule still hides the same rule from | ||||
| 444 | * profiles further up. | ||||
| 445 | */ | ||||
| 446 | static bool_Bool | ||||
| 447 | color_filters_read_layer(const profile_inherit_layer_t *layer, const char *path, const char *filename, | ||||
| 448 | profile_inherit_seen_t *seen, void *user_data, char **err) | ||||
| 449 | { | ||||
| 450 | color_filters_layer_read_t *layer_read = (color_filters_layer_read_t *)user_data; | ||||
| 451 | GSList *layer_list = NULL((void*)0); | ||||
| 452 | FILE *f; | ||||
| 453 | int ret; | ||||
| 454 | |||||
| 455 | if ((f = ws_fopenfopen(path, "r")) == NULL((void*)0)) { | ||||
| 456 | *err = ws_strdup_printf("Could not open coloring rules file\n\"%s\": %s.", path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open coloring rules file\n\"%s\": %s." , path, g_strerror((*__errno_location ()))); | ||||
| 457 | return false0; | ||||
| 458 | } | ||||
| 459 | if (strcmp(filename, COLORFILTERS_FILE_NAME"colorfilters") == 0) { | ||||
| 460 | ret = read_legacy_filters_file(path, f, &layer_list, color_filters_collect_cb, layer_read->keep_compiled); | ||||
| 461 | } else { | ||||
| 462 | ret = read_filters_file(path, f, &layer_list, color_filters_collect_cb, true1, layer_read->keep_compiled); | ||||
| 463 | } | ||||
| 464 | fclose(f); | ||||
| 465 | if (ret != 0) { | ||||
| 466 | *err = ws_strdup_printf("Error reading coloring rules file\n\"%s\": %s.", path, g_strerror(ret))wmem_strdup_printf(((void*)0), "Error reading coloring rules file\n\"%s\": %s." , path, g_strerror(ret)); | ||||
| 467 | color_filter_list_delete(&layer_list); | ||||
| 468 | return false0; | ||||
| 469 | } | ||||
| 470 | |||||
| 471 | for (GSList *item = layer_list; item; item = g_slist_next(item)((item) ? (((GSList *)(item))->next) : ((void*)0))) { | ||||
| 472 | color_filter_t *colorf = (color_filter_t *)item->data; | ||||
| 473 | bool_Bool keep = !color_filter_is_tmp(colorf) && | ||||
| 474 | !profile_inherit_seen_check(seen, colorf->filter_name) && | ||||
| 475 | colorf->enabled; | ||||
| 476 | |||||
| 477 | if (!keep) { | ||||
| 478 | color_filter_delete(colorf); | ||||
| 479 | continue; | ||||
| 480 | } | ||||
| 481 | g_free(colorf->source_profile)(__builtin_object_size ((colorf->source_profile), 0) != (( size_t) - 1)) ? g_free_sized (colorf->source_profile, __builtin_object_size ((colorf->source_profile), 0)) : (g_free) (colorf->source_profile ); | ||||
| 482 | colorf->source_profile = g_strdup(layer->source)g_strdup_inline (layer->source); | ||||
| 483 | layer_read->add_cb(colorf, layer_read->user_data); | ||||
| 484 | } | ||||
| 485 | g_slist_free(layer_list); | ||||
| 486 | return true1; | ||||
| 487 | } | ||||
| 488 | |||||
| 489 | /* Add an inherited rule, compiled when read, to the list used for matching */ | ||||
| 490 | static void | ||||
| 491 | color_filters_add_inherited_cb(color_filter_t *colorf, void *user_data _U___attribute__((unused))) | ||||
| 492 | { | ||||
| 493 | color_filter_list = g_slist_append(color_filter_list, colorf); | ||||
| 494 | } | ||||
| 495 | |||||
| 496 | /* | ||||
| 497 | * Read the inherited rules for a mode. Rules duplicating a rule in own_list, | ||||
| 498 | * which may be NULL, are not inherited. | ||||
| 499 | */ | ||||
| 500 | static bool_Bool | ||||
| 501 | color_filters_add_inherited(profile_inherit_e mode, GSList *own_list, void *user_data, char **err_msg, | ||||
| 502 | color_filter_add_cb_func add_cb, const char *app_env_var_prefix) | ||||
| 503 | { | ||||
| 504 | profile_inherit_seen_t *seen = profile_inherit_seen_new(); | ||||
| 505 | /* The rules read for matching are compiled once, when read */ | ||||
| 506 | color_filters_layer_read_t layer_read = { add_cb, user_data, add_cb == color_filters_add_inherited_cb }; | ||||
| 507 | |||||
| 508 | /* The profile's own rules come first, and win over duplicates from parents */ | ||||
| 509 | for (GSList *item = own_list; item; item = g_slist_next(item)((item) ? (((GSList *)(item))->next) : ((void*)0))) { | ||||
| 510 | color_filter_t *colorf = (color_filter_t *)item->data; | ||||
| 511 | if (!color_filter_is_tmp(colorf)) | ||||
| 512 | profile_inherit_seen_check(seen, colorf->filter_name); | ||||
| 513 | } | ||||
| 514 | profile_inherit_seen_next(seen); | ||||
| 515 | |||||
| 516 | bool_Bool ret = profile_inherit_read_layers(app_env_var_prefix, COLORING_RULES_FILE_NAME"coloring_rules.jsonc", COLORFILTERS_FILE_NAME"colorfilters", true1, | ||||
| 517 | mode, seen, color_filters_read_layer, &layer_read, err_msg); | ||||
| 518 | |||||
| 519 | profile_inherit_seen_free(seen); | ||||
| 520 | return ret; | ||||
| 521 | } | ||||
| 522 | |||||
| 523 | bool_Bool | ||||
| 524 | color_filters_read_inherited(profile_inherit_e mode, void *user_data, char** err_msg, | ||||
| 525 | color_filter_add_cb_func add_cb, const char* app_env_var_prefix) | ||||
| 526 | { | ||||
| 527 | return color_filters_add_inherited(mode, NULL((void*)0), user_data, err_msg, add_cb, app_env_var_prefix); | ||||
| 528 | } | ||||
| 529 | |||||
| 530 | static bool_Bool | ||||
| 531 | color_filters_get(char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix) | ||||
| 532 | { | ||||
| 533 | profile_inherit_e mode = profile_get_inherit(app_env_var_prefix, NULL((void*)0), COLORING_RULES_FILE_NAME"coloring_rules.jsonc"); | ||||
| 534 | |||||
| 535 | /* start the list with the temporary colorizing rules */ | ||||
| 536 | color_filters_add_tmp(&color_filter_list); | ||||
| 537 | |||||
| 538 | if (!color_filters_get_own(err_msg, add_cb, app_env_var_prefix, | ||||
| 539 | mode != PROFILE_INHERIT_PARENT_ONLY, | ||||
| 540 | mode == PROFILE_INHERIT_PROFILE_ONLY)) { | ||||
| 541 | return false0; | ||||
| 542 | } | ||||
| 543 | |||||
| 544 | if (mode == PROFILE_INHERIT_PROFILE_ONLY) { | ||||
| 545 | return true1; | ||||
| 546 | } | ||||
| 547 | |||||
| 548 | return color_filters_add_inherited(mode, color_filter_list, NULL((void*)0), err_msg, | ||||
| 549 | color_filters_add_inherited_cb, app_env_var_prefix); | ||||
| 550 | } | ||||
| 551 | |||||
| 552 | /* Initialize the filter structures (reading from file) for general running, including app startup */ | ||||
| 553 | bool_Bool | ||||
| 554 | color_filters_init(char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix) | ||||
| 555 | { | ||||
| 556 | /* delete all currently existing filters */ | ||||
| 557 | color_filter_list_delete(&color_filter_list); | ||||
| 558 | |||||
| 559 | /* now try to construct the filters list */ | ||||
| 560 | return color_filters_get(err_msg, add_cb, app_env_var_prefix); | ||||
| 561 | } | ||||
| 562 | |||||
| 563 | bool_Bool | ||||
| 564 | color_filters_reload(char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix) | ||||
| 565 | { | ||||
| 566 | /* "move" old entries to the deleted list | ||||
| 567 | * we must keep them until the dissection no longer needs them */ | ||||
| 568 | color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list); | ||||
| 569 | color_filter_list = NULL((void*)0); | ||||
| 570 | |||||
| 571 | /* now try to construct the filters list */ | ||||
| 572 | return color_filters_get(err_msg, add_cb, app_env_var_prefix); | ||||
| 573 | } | ||||
| 574 | |||||
| 575 | void | ||||
| 576 | color_filters_cleanup(void) | ||||
| 577 | { | ||||
| 578 | /* delete the previously deleted filters */ | ||||
| 579 | color_filter_list_delete(&color_filter_deleted_list); | ||||
| 580 | } | ||||
| 581 | |||||
| 582 | typedef struct _color_clone | ||||
| 583 | { | ||||
| 584 | void *user_data; | ||||
| 585 | color_filter_add_cb_func add_cb; | ||||
| 586 | } color_clone_t; | ||||
| 587 | |||||
| 588 | static void | ||||
| 589 | color_filters_clone_cb(void *filter_arg, void *user_data) | ||||
| 590 | { | ||||
| 591 | color_clone_t* clone_data = (color_clone_t*)user_data; | ||||
| 592 | color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg); | ||||
| 593 | |||||
| 594 | clone_data->add_cb (new_colorf, clone_data->user_data); | ||||
| 595 | } | ||||
| 596 | |||||
| 597 | void | ||||
| 598 | color_filters_clone(void *user_data, color_filter_add_cb_func add_cb) | ||||
| 599 | { | ||||
| 600 | color_clone_t clone_data; | ||||
| 601 | |||||
| 602 | clone_data.user_data = user_data; | ||||
| 603 | clone_data.add_cb = add_cb; | ||||
| 604 | g_slist_foreach(color_filter_list, color_filters_clone_cb, &clone_data); | ||||
| 605 | } | ||||
| 606 | |||||
| 607 | /* A rule takes part in matching if it compiled and is enabled. A disabled | ||||
| 608 | * rule takes part only when asked to, and never a disabled conversation | ||||
| 609 | * rule: unused conversation slots are compiled as "frame" and would match | ||||
| 610 | * every packet. */ | ||||
| 611 | static bool_Bool | ||||
| 612 | color_filter_in_play(const color_filter_t *colorf, bool_Bool include_disabled) | ||||
| 613 | { | ||||
| 614 | if (colorf->c_colorfilter == NULL((void*)0)) | ||||
| 615 | return false0; | ||||
| 616 | if (colorf->enabled) | ||||
| 617 | return true1; | ||||
| 618 | return include_disabled && !color_filter_is_tmp(colorf); | ||||
| 619 | } | ||||
| 620 | |||||
| 621 | /* Compiles a filter without reporting errors. Disabled rules are compiled | ||||
| 622 | * this way so that they can be matched while disabled rules are shown; one | ||||
| 623 | * that does not compile simply never matches. */ | ||||
| 624 | static dfilter_t * | ||||
| 625 | color_filter_compile_quiet(const char *text) | ||||
| 626 | { | ||||
| 627 | dfilter_t *df = NULL((void*)0); | ||||
| 628 | df_error_t *df_err = NULL((void*)0); | ||||
| 629 | |||||
| 630 | if (!dfilter_compile(text, &df, &df_err)dfilter_compile_full(text, &df, &df_err, (1U << 1)|(1U << 2), __func__)) { | ||||
| 631 | df_error_free(&df_err); | ||||
| 632 | } | ||||
| 633 | return df; | ||||
| 634 | } | ||||
| 635 | |||||
| 636 | /* Compiles the filter of a rule read from a file. An enabled rule that does | ||||
| 637 | * not compile is reported and disabled. A disabled rule is compiled quietly | ||||
| 638 | * when compile_disabled is set (rules read into the list used for matching) | ||||
| 639 | * and not at all otherwise (rules read for editing). */ | ||||
| 640 | static dfilter_t * | ||||
| 641 | color_filter_compile_read(const char *name, const char *text, bool_Bool *enabled, | ||||
| 642 | bool_Bool compile_disabled, const char *path, | ||||
| 643 | const char *file_kind) | ||||
| 644 | { | ||||
| 645 | dfilter_t *df = NULL((void*)0); | ||||
| 646 | df_error_t *df_err = NULL((void*)0); | ||||
| 647 | |||||
| 648 | if (!*enabled) { | ||||
| 649 | return compile_disabled ? color_filter_compile_quiet(text) : NULL((void*)0); | ||||
| 650 | } | ||||
| 651 | if (!dfilter_compile(text, &df, &df_err)dfilter_compile_full(text, &df, &df_err, (1U << 1)|(1U << 2), __func__)) { | ||||
| 652 | report_warning("Disabling color filter: Could not compile \"%s\" in %s \"%s\".\n%s", | ||||
| 653 | name, file_kind, path, df_err->msg); | ||||
| 654 | df_error_free(&df_err); | ||||
| 655 | *enabled = false0; | ||||
| 656 | } | ||||
| 657 | return df; | ||||
| 658 | } | ||||
| 659 | |||||
| 660 | static void | ||||
| 661 | color_filter_compile_cb(void *filter_arg, void *err) | ||||
| 662 | { | ||||
| 663 | color_filter_t *colorf = (color_filter_t *)filter_arg; | ||||
| 664 | char **err_msg = (char**)err; | ||||
| 665 | df_error_t *df_err = NULL((void*)0); | ||||
| 666 | |||||
| 667 | ws_assert(colorf->c_colorfilter == NULL)do { if ((1) && !(colorf->c_colorfilter == ((void* )0))) ws_log_fatal_full("Epan", LOG_LEVEL_ERROR, "epan/color_filters.c" , 667, __func__, "assertion failed: %s", "colorf->c_colorfilter == ((void*)0)" ); } while (0); | ||||
| 668 | |||||
| 669 | /* Disabled conversation rules are never used. */ | ||||
| 670 | if (!colorf->enabled) { | ||||
| 671 | if (!color_filter_is_tmp(colorf)) { | ||||
| 672 | colorf->c_colorfilter = color_filter_compile_quiet(colorf->filter_text); | ||||
| 673 | } | ||||
| 674 | return; | ||||
| 675 | } | ||||
| 676 | |||||
| 677 | if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter, &df_err)dfilter_compile_full(colorf->filter_text, &colorf-> c_colorfilter, &df_err, (1U << 1)|(1U << 2), __func__ )) { | ||||
| 678 | *err_msg = ws_strdup_printf("Could not compile color filter name: \"%s\" text: \"%s\".\n%s",wmem_strdup_printf(((void*)0), "Could not compile color filter name: \"%s\" text: \"%s\".\n%s" , colorf->filter_name, colorf->filter_text, df_err-> msg) | ||||
| 679 | colorf->filter_name, colorf->filter_text, df_err->msg)wmem_strdup_printf(((void*)0), "Could not compile color filter name: \"%s\" text: \"%s\".\n%s" , colorf->filter_name, colorf->filter_text, df_err-> msg); | ||||
| 680 | df_error_free(&df_err); | ||||
| 681 | /* this filter was compilable before, so this should never happen */ | ||||
| 682 | /* except if the OK button of the parent window has been clicked */ | ||||
| 683 | /* so don't use ws_assert_not_reached() but check the filters again */ | ||||
| 684 | } | ||||
| 685 | } | ||||
| 686 | |||||
| 687 | static void | ||||
| 688 | color_filter_validate_cb(void *filter_arg, void *err) | ||||
| 689 | { | ||||
| 690 | color_filter_t *colorf = (color_filter_t *)filter_arg; | ||||
| 691 | char **err_msg = (char**)err; | ||||
| 692 | df_error_t *df_err = NULL((void*)0); | ||||
| 693 | |||||
| 694 | ws_assert(colorf->c_colorfilter == NULL)do { if ((1) && !(colorf->c_colorfilter == ((void* )0))) ws_log_fatal_full("Epan", LOG_LEVEL_ERROR, "epan/color_filters.c" , 694, __func__, "assertion failed: %s", "colorf->c_colorfilter == ((void*)0)" ); } while (0); | ||||
| 695 | |||||
| 696 | /* If the filter is disabled it doesn't matter if it compiles or not. */ | ||||
| 697 | if (!colorf->enabled) return; | ||||
| 698 | |||||
| 699 | if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter, &df_err)dfilter_compile_full(colorf->filter_text, &colorf-> c_colorfilter, &df_err, (1U << 1)|(1U << 2), __func__ )) { | ||||
| 700 | *err_msg = ws_strdup_printf("Disabling color filter name: \"%s\" filter: \"%s\".\n%s",wmem_strdup_printf(((void*)0), "Disabling color filter name: \"%s\" filter: \"%s\".\n%s" , colorf->filter_name, colorf->filter_text, df_err-> msg) | ||||
| 701 | colorf->filter_name, colorf->filter_text, df_err->msg)wmem_strdup_printf(((void*)0), "Disabling color filter name: \"%s\" filter: \"%s\".\n%s" , colorf->filter_name, colorf->filter_text, df_err-> msg); | ||||
| 702 | df_error_free(&df_err); | ||||
| 703 | |||||
| 704 | /* Disable the color filter in the list of color filters. */ | ||||
| 705 | colorf->enabled = false0; | ||||
| 706 | } | ||||
| 707 | |||||
| 708 | /* XXX: What if the color filter tests "frame.coloring_rule.name" or | ||||
| 709 | * "frame.coloring_rule.string"? | ||||
| 710 | */ | ||||
| 711 | } | ||||
| 712 | |||||
| 713 | /* apply changes from the edit list */ | ||||
| 714 | bool_Bool | ||||
| 715 | color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl, char** err_msg) | ||||
| 716 | { | ||||
| 717 | bool_Bool ret = true1; | ||||
| 718 | |||||
| 719 | *err_msg = NULL((void*)0); | ||||
| 720 | |||||
| 721 | /* "move" old entries to the deleted list | ||||
| 722 | * we must keep them until the dissection no longer needs them */ | ||||
| 723 | color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list); | ||||
| 724 | color_filter_list = NULL((void*)0); | ||||
| 725 | |||||
| 726 | /* clone all list entries from tmp/edit to normal list */ | ||||
| 727 | color_filter_list_delete(&color_filter_valid_list); | ||||
| 728 | color_filter_valid_list = color_filter_list_clone(tmp_cfl); | ||||
| 729 | color_filter_valid_list = g_slist_concat(color_filter_valid_list, | ||||
| 730 | color_filter_list_clone(edit_cfl) ); | ||||
| 731 | |||||
| 732 | /* compile all filter */ | ||||
| 733 | g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, err_msg); | ||||
| 734 | if (*err_msg != NULL((void*)0)) { | ||||
| 735 | ret = false0; | ||||
| 736 | } | ||||
| 737 | |||||
| 738 | /* clone all list entries from tmp/edit to normal list */ | ||||
| 739 | color_filter_list = color_filter_list_clone(color_filter_valid_list); | ||||
| 740 | |||||
| 741 | /* compile all filter */ | ||||
| 742 | g_slist_foreach(color_filter_list, color_filter_compile_cb, err_msg); | ||||
| 743 | if (*err_msg != NULL((void*)0)) { | ||||
| 744 | ret = false0; | ||||
| 745 | } | ||||
| 746 | |||||
| 747 | return ret; | ||||
| 748 | } | ||||
| 749 | |||||
| 750 | bool_Bool | ||||
| 751 | color_filters_used(void) | ||||
| 752 | { | ||||
| 753 | return color_filter_list != NULL((void*)0) && filters_enabled; | ||||
| 754 | } | ||||
| 755 | |||||
| 756 | bool_Bool | ||||
| 757 | tmp_color_filters_used(void) | ||||
| 758 | { | ||||
| 759 | return tmp_colors_set; | ||||
| 760 | } | ||||
| 761 | |||||
| 762 | /* prepare the epan_dissect_t for the filter */ | ||||
| 763 | static void | ||||
| 764 | prime_edt(void *data, void *user_data) | ||||
| 765 | { | ||||
| 766 | color_filter_t *colorf = (color_filter_t *)data; | ||||
| 767 | epan_dissect_t *edt = (epan_dissect_t *)user_data; | ||||
| 768 | |||||
| 769 | if (color_filter_in_play(colorf, prefs.gui_coloring_rules_include_disabled)) | ||||
| 770 | epan_dissect_prime_with_dfilter(edt, colorf->c_colorfilter); | ||||
| 771 | } | ||||
| 772 | |||||
| 773 | /* Prime the epan_dissect_t with all the compiler | ||||
| 774 | * color filters in 'color_filter_list'. */ | ||||
| 775 | void | ||||
| 776 | color_filters_prime_edt(epan_dissect_t *edt) | ||||
| 777 | { | ||||
| 778 | if (color_filters_used()) | ||||
| 779 | g_slist_foreach(color_filter_list, prime_edt, edt); | ||||
| 780 | } | ||||
| 781 | |||||
| 782 | static int | ||||
| 783 | find_hfid(const void *data, const void *user_data) | ||||
| 784 | { | ||||
| 785 | color_filter_t *colorf = (color_filter_t *)data; | ||||
| 786 | int hfid = GPOINTER_TO_INT(user_data)((gint) (glong) (user_data)); | ||||
| 787 | |||||
| 788 | if (color_filter_in_play(colorf, prefs.gui_coloring_rules_include_disabled)) { | ||||
| 789 | if (dfilter_interested_in_field(colorf->c_colorfilter, hfid)) { | ||||
| 790 | return 0; | ||||
| 791 | } | ||||
| 792 | } | ||||
| 793 | return -1; | ||||
| 794 | } | ||||
| 795 | |||||
| 796 | bool_Bool | ||||
| 797 | color_filters_use_hfid(int hfid) | ||||
| 798 | { | ||||
| 799 | GSList *item = NULL((void*)0); | ||||
| 800 | if (color_filters_used()) | ||||
| 801 | item = g_slist_find_custom(color_filter_list, GINT_TO_POINTER(hfid)((gpointer) (glong) (hfid)), find_hfid); | ||||
| 802 | return (item != NULL((void*)0)); | ||||
| 803 | } | ||||
| 804 | |||||
| 805 | static int | ||||
| 806 | find_proto(const void *data, const void *user_data) | ||||
| 807 | { | ||||
| 808 | color_filter_t *colorf = (color_filter_t *)data; | ||||
| 809 | int proto_id = GPOINTER_TO_INT(user_data)((gint) (glong) (user_data)); | ||||
| 810 | |||||
| 811 | if (color_filter_in_play(colorf, prefs.gui_coloring_rules_include_disabled)) { | ||||
| 812 | if (dfilter_interested_in_proto(colorf->c_colorfilter, proto_id)) { | ||||
| 813 | return 0; | ||||
| 814 | } | ||||
| 815 | } | ||||
| 816 | return -1; | ||||
| 817 | } | ||||
| 818 | |||||
| 819 | bool_Bool | ||||
| 820 | color_filters_use_proto(int proto_id) | ||||
| 821 | { | ||||
| 822 | GSList *item = NULL((void*)0); | ||||
| 823 | if (color_filters_used()) | ||||
| 824 | item = g_slist_find_custom(color_filter_list, GINT_TO_POINTER(proto_id)((gpointer) (glong) (proto_id)), find_proto); | ||||
| 825 | return (item != NULL((void*)0)); | ||||
| 826 | } | ||||
| 827 | |||||
| 828 | const color_filter_t * | ||||
| 829 | color_filters_colorize_packet_all(epan_dissect_t *edt, | ||||
| 830 | wmem_allocator_t *scope, wmem_list_t **matches, bool_Bool include_disabled) | ||||
| 831 | { | ||||
| 832 | const color_filter_t *first_match = NULL((void*)0); | ||||
| 833 | |||||
| 834 | if (matches) { | ||||
| 835 | *matches = NULL((void*)0); | ||||
| 836 | } | ||||
| 837 | |||||
| 838 | /* If we have color filters, collect ALL matching ones. */ | ||||
| 839 | if ((edt->tree != NULL((void*)0)) && (color_filters_used())) { | ||||
| 840 | for (GSList *curr = color_filter_list; curr != NULL((void*)0); curr = g_slist_next(curr)((curr) ? (((GSList *)(curr))->next) : ((void*)0))) { | ||||
| 841 | color_filter_t *colorf = (color_filter_t *)curr->data; | ||||
| 842 | if (color_filter_in_play(colorf, include_disabled) && | ||||
| 843 | dfilter_apply_edt(colorf->c_colorfilter, edt)) { | ||||
| 844 | |||||
| 845 | if (matches) { | ||||
| 846 | if (*matches == NULL((void*)0)) { | ||||
| 847 | *matches = wmem_list_new(scope); | ||||
| 848 | } | ||||
| 849 | wmem_list_append(*matches, colorf); | ||||
| 850 | } | ||||
| 851 | |||||
| 852 | /* A disabled rule never colors the packet */ | ||||
| 853 | if (!first_match && colorf->enabled) { | ||||
| 854 | first_match = colorf; /* Backward compatibility */ | ||||
| 855 | } | ||||
| 856 | } | ||||
| 857 | } | ||||
| 858 | } | ||||
| 859 | |||||
| 860 | return first_match; | ||||
| 861 | } | ||||
| 862 | |||||
| 863 | /* Parse a "#rrggbb" or "rrggbb" string into a color_t. Returns false (and | ||||
| 864 | * leaves *color untouched) if str is NULL or not a valid hex triplet. */ | ||||
| 865 | static bool_Bool | ||||
| 866 | hex_str_to_color(const char *str, color_t *color) | ||||
| 867 | { | ||||
| 868 | unsigned long cval; | ||||
| 869 | |||||
| 870 | if (str == NULL((void*)0)) { | ||||
| 871 | return false0; | ||||
| 872 | } | ||||
| 873 | if (str[0] == '#') { | ||||
| 874 | str++; | ||||
| 875 | } | ||||
| 876 | if (strlen(str) != 6 || strspn(str, "0123456789abcdefABCDEF") != 6) { | ||||
| 877 | return false0; | ||||
| 878 | } | ||||
| 879 | |||||
| 880 | cval = strtoul(str, NULL((void*)0), 16); | ||||
| 881 | color->red = RED_COMPONENT(cval)(uint16_t) (((((cval) >> 16) & 0xff) * 65535 / 255) ); | ||||
| 882 | color->green = GREEN_COMPONENT(cval)(uint16_t) (((((cval) >> 8) & 0xff) * 65535 / 255)); | ||||
| 883 | color->blue = BLUE_COMPONENT(cval)(uint16_t) ( (((cval) & 0xff) * 65535 / 255)); | ||||
| 884 | return true1; | ||||
| 885 | } | ||||
| 886 | |||||
| 887 | static int | ||||
| 888 | read_filters_file(const char *path, FILE *f, void *user_data, color_filter_add_cb_func add_cb, bool_Bool require_json, bool_Bool keep_compiled) | ||||
| 889 | { | ||||
| 890 | long size; | ||||
| 891 | char *buf; | ||||
| 892 | size_t nread; | ||||
| 893 | int num_tokens; | ||||
| 894 | jsmntok_t *tokens; | ||||
| 895 | int array_len; | ||||
| 896 | jsmntok_t *element; | ||||
| 897 | |||||
| 898 | if (fseek(f, 0, SEEK_END2) == -1) { | ||||
| 899 | return errno(*__errno_location ()); | ||||
| 900 | } | ||||
| 901 | size = ftell(f); | ||||
| 902 | if (size < 0) { | ||||
| 903 | return errno(*__errno_location ()); | ||||
| 904 | } | ||||
| 905 | if (fseek(f, 0, SEEK_SET0) == -1) { | ||||
| 906 | return errno(*__errno_location ()); | ||||
| 907 | } | ||||
| 908 | if (size == 0) { | ||||
| 909 | return 0; | ||||
| 910 | } | ||||
| 911 | |||||
| 912 | buf = (char *)g_malloc(size + 1); | ||||
| 913 | nread = fread(buf, 1, size, f); | ||||
| 914 | if (ferror(f)) { | ||||
| 915 | int ret = errno(*__errno_location ()); | ||||
| 916 | g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized (buf, __builtin_object_size ((buf), 0)) : (g_free) (buf); | ||||
| 917 | return ret; | ||||
| 918 | } | ||||
| 919 | buf[nread] = '\0'; | ||||
| 920 | |||||
| 921 | if (!json_strip_jsonc_comments(buf)) { | ||||
| 922 | report_warning("Error parsing coloring rules file\n\"%s\": unterminated block comment.", path); | ||||
| 923 | g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized (buf, __builtin_object_size ((buf), 0)) : (g_free) (buf); | ||||
| 924 | return 0; | ||||
| 925 | } | ||||
| 926 | |||||
| 927 | num_tokens = json_parse(buf, NULL((void*)0), 0); | ||||
| 928 | if (num_tokens == 0) { | ||||
| 929 | /* Only comments and whitespace: a valid, empty rule list. */ | ||||
| 930 | g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized (buf, __builtin_object_size ((buf), 0)) : (g_free) (buf); | ||||
| 931 | return 0; | ||||
| 932 | } | ||||
| 933 | if (num_tokens < 0) { | ||||
| 934 | /* | ||||
| 935 | * Not JSON at all. For the personal and global rule files that is | ||||
| 936 | * an error the user must see, since none of the file's rules are | ||||
| 937 | * used. An imported file may be in the legacy format, which never | ||||
| 938 | * parses as JSON, so it just yields no rules here. | ||||
| 939 | */ | ||||
| 940 | if (require_json) { | ||||
| 941 | report_warning("Error parsing coloring rules file\n\"%s\": invalid JSON.", path); | ||||
| 942 | } | ||||
| 943 | g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized (buf, __builtin_object_size ((buf), 0)) : (g_free) (buf); | ||||
| 944 | return 0; | ||||
| 945 | } | ||||
| 946 | |||||
| 947 | tokens = g_new(jsmntok_t, num_tokens)((jsmntok_t *) g_malloc_n ((num_tokens), sizeof (jsmntok_t))); | ||||
| 948 | if (json_parse(buf, tokens, num_tokens) <= 0 || tokens[0].type != JSMN_ARRAY) { | ||||
| 949 | report_warning("Error parsing coloring rules file\n\"%s\": invalid JSON.", path); | ||||
| 950 | g_free(tokens)(__builtin_object_size ((tokens), 0) != ((size_t) - 1)) ? g_free_sized (tokens, __builtin_object_size ((tokens), 0)) : (g_free) (tokens ); | ||||
| 951 | g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized (buf, __builtin_object_size ((buf), 0)) : (g_free) (buf); | ||||
| 952 | return 0; | ||||
| 953 | } | ||||
| 954 | |||||
| 955 | array_len = json_get_array_len(&tokens[0]); | ||||
| 956 | element = json_get_array_index(&tokens[0], 0); | ||||
| 957 | |||||
| 958 | for (int i = 0; i < array_len; i++, element = json_get_next_object(element)) { | ||||
| 959 | char *name, *filter_str, *fg_str, *bg_str; | ||||
| 960 | bool_Bool enabled = true1; | ||||
| 961 | color_t fg_color = { 0, 0, 0 }, bg_color = { 0, 0, 0 }; | ||||
| 962 | dfilter_t *temp_dfilter = NULL((void*)0); | ||||
| 963 | const char *filter_exp; | ||||
| 964 | color_filter_t *colorf; | ||||
| 965 | jsmntok_t *name_tok, *properties_tok; | ||||
| 966 | |||||
| 967 | if (element->type != JSMN_OBJECT || element->size != 1) { | ||||
| 968 | continue; | ||||
| 969 | } | ||||
| 970 | |||||
| 971 | /* "<filter name>": { ... } */ | ||||
| 972 | name_tok = element + 1; | ||||
| 973 | properties_tok = name_tok + 1; | ||||
| 974 | if (name_tok->type != JSMN_STRING || properties_tok->type != JSMN_OBJECT) { | ||||
| 975 | continue; | ||||
| 976 | } | ||||
| 977 | |||||
| 978 | name = g_strndup(&buf[name_tok->start], (gsize)(name_tok->end - name_tok->start)); | ||||
| 979 | if (!json_decode_string_inplace(name)) { | ||||
| 980 | g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized (name, __builtin_object_size ((name), 0)) : (g_free) (name); | ||||
| 981 | continue; | ||||
| 982 | } | ||||
| 983 | |||||
| 984 | filter_str = json_get_string(buf, properties_tok, "filter"); | ||||
| 985 | fg_str = json_get_string(buf, properties_tok, "foreground"); | ||||
| 986 | bg_str = json_get_string(buf, properties_tok, "background"); | ||||
| 987 | json_get_boolean(buf, properties_tok, "enabled", &enabled); | ||||
| 988 | |||||
| 989 | hex_str_to_color(fg_str, &fg_color); | ||||
| 990 | hex_str_to_color(bg_str, &bg_color); | ||||
| 991 | |||||
| 992 | filter_exp = filter_str ? filter_str : ""; | ||||
| 993 | |||||
| 994 | temp_dfilter = color_filter_compile_read(name, filter_exp, &enabled, | ||||
| 995 | user_data == &color_filter_list, | ||||
| 996 | path, "coloring rules file"); | ||||
| 997 | |||||
| 998 | colorf = color_filter_new(name, filter_exp, &bg_color, &fg_color, enabled); | ||||
| 999 | g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized (name, __builtin_object_size ((name), 0)) : (g_free) (name); | ||||
| 1000 | |||||
| 1001 | if (user_data == &color_filter_list) { | ||||
| 1002 | GSList **cfl = (GSList **)user_data; | ||||
| 1003 | |||||
| 1004 | /* internal call */ | ||||
| 1005 | colorf->c_colorfilter = temp_dfilter; | ||||
| 1006 | *cfl = g_slist_append(*cfl, colorf); | ||||
| 1007 | } else { | ||||
| 1008 | /* external call */ | ||||
| 1009 | /* just editing, don't need the compiled filter, unless | ||||
| 1010 | * the caller adds the rule to the list used for matching */ | ||||
| 1011 | if (keep_compiled) { | ||||
| 1012 | colorf->c_colorfilter = temp_dfilter; | ||||
| 1013 | } else { | ||||
| 1014 | dfilter_free(temp_dfilter); | ||||
| 1015 | } | ||||
| 1016 | add_cb(colorf, user_data); | ||||
| 1017 | } | ||||
| 1018 | } | ||||
| 1019 | |||||
| 1020 | g_free(tokens)(__builtin_object_size ((tokens), 0) != ((size_t) - 1)) ? g_free_sized (tokens, __builtin_object_size ((tokens), 0)) : (g_free) (tokens ); | ||||
| 1021 | g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized (buf, __builtin_object_size ((buf), 0)) : (g_free) (buf); | ||||
| 1022 | return 0; | ||||
| 1023 | } | ||||
| 1024 | |||||
| 1025 | /* Read legacy (@-delimited) filters from the given file */ | ||||
| 1026 | /* XXX At what point do we drop support? */ | ||||
| 1027 | /* XXX Would it make more sense to use GStrings here instead of reallocing | ||||
| 1028 | our buffers? */ | ||||
| 1029 | static int | ||||
| 1030 | read_legacy_filters_file(const char *path, FILE *f, void *user_data, color_filter_add_cb_func add_cb, bool_Bool keep_compiled) | ||||
| 1031 | { | ||||
| 1032 | #define INIT_BUF_SIZE128 128 | ||||
| 1033 | char *name; | ||||
| 1034 | char *filter_exp; | ||||
| 1035 | uint32_t name_len = INIT_BUF_SIZE128; | ||||
| 1036 | uint32_t filter_exp_len = INIT_BUF_SIZE128; | ||||
| 1037 | uint32_t i = 0; | ||||
| 1038 | int c; | ||||
| 1039 | uint16_t fg_r, fg_g, fg_b, bg_r, bg_g, bg_b; | ||||
| 1040 | bool_Bool disabled = false0; | ||||
| 1041 | bool_Bool skip_end_of_line = false0; | ||||
| 1042 | int ret = 0; | ||||
| 1043 | |||||
| 1044 | name = (char *)g_malloc(name_len + 1); | ||||
| 1045 | filter_exp = (char *)g_malloc(filter_exp_len + 1); | ||||
| 1046 | |||||
| 1047 | while (1) { | ||||
| 1048 | |||||
| 1049 | if (skip_end_of_line
| ||||
| 1050 | do { | ||||
| 1051 | c = ws_getc_unlockedgetc_unlocked(f); | ||||
| 1052 | } while (c != EOF(-1) && c != '\n'); | ||||
| 1053 | if (c == EOF(-1)) | ||||
| 1054 | break; | ||||
| 1055 | disabled = false0; | ||||
| 1056 | skip_end_of_line = false0; | ||||
| 1057 | } | ||||
| 1058 | |||||
| 1059 | while ((c = ws_getc_unlockedgetc_unlocked(f)) != EOF(-1) && g_ascii_isspace(c)((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0)) { | ||||
| 1060 | if (c == '\n') { | ||||
| 1061 | continue; | ||||
| 1062 | } | ||||
| 1063 | } | ||||
| 1064 | |||||
| 1065 | if (c == EOF(-1)) | ||||
| 1066 | break; | ||||
| 1067 | |||||
| 1068 | if (c == '!') { | ||||
| 1069 | disabled = true1; | ||||
| 1070 | continue; | ||||
| 1071 | } | ||||
| 1072 | |||||
| 1073 | /* skip # comments and invalid lines */ | ||||
| 1074 | if (c != '@') { | ||||
| 1075 | skip_end_of_line = true1; | ||||
| 1076 | continue; | ||||
| 1077 | } | ||||
| 1078 | |||||
| 1079 | /* we get the @ delimiter. | ||||
| 1080 | * Format is: | ||||
| 1081 | * @name@filter expression@[background r,g,b][foreground r,g,b] | ||||
| 1082 | */ | ||||
| 1083 | |||||
| 1084 | /* retrieve name */ | ||||
| 1085 | i = 0; | ||||
| 1086 | while (1) { | ||||
| 1087 | c = ws_getc_unlockedgetc_unlocked(f); | ||||
| 1088 | if (c == EOF(-1) || c == '@') | ||||
| 1089 | break; | ||||
| 1090 | if (i
| ||||
| 1091 | /* buffer isn't long enough; double its length.*/ | ||||
| 1092 | name_len *= 2; | ||||
| 1093 | name = (char *)g_realloc(name, name_len + 1); | ||||
| 1094 | } | ||||
| 1095 | name[i++] = c; | ||||
| 1096 | } | ||||
| 1097 | name[i] = '\0'; | ||||
| 1098 | |||||
| 1099 | if (c == EOF(-1)) { | ||||
| 1100 | break; | ||||
| 1101 | } else if (i
| ||||
| 1102 | skip_end_of_line = true1; | ||||
| 1103 | continue; | ||||
| 1104 | } | ||||
| 1105 | |||||
| 1106 | /* retrieve filter expression */ | ||||
| 1107 | i = 0; | ||||
| 1108 | while (1) { | ||||
| 1109 | c = ws_getc_unlockedgetc_unlocked(f); | ||||
| 1110 | if (c == EOF(-1) || c == '@') | ||||
| 1111 | break; | ||||
| 1112 | if (i
| ||||
| 1113 | /* buffer isn't long enough; double its length.*/ | ||||
| 1114 | filter_exp_len *= 2; | ||||
| 1115 | filter_exp = (char *)g_realloc(filter_exp, filter_exp_len + 1); | ||||
| 1116 | } | ||||
| 1117 | filter_exp[i++] = c; | ||||
| 1118 | } | ||||
| 1119 | filter_exp[i] = '\0'; | ||||
| 1120 | |||||
| 1121 | if (c == EOF(-1)) { | ||||
| 1122 | break; | ||||
| 1123 | } else if (i
| ||||
| 1124 | skip_end_of_line = true1; | ||||
| 1125 | continue; | ||||
| 1126 | } | ||||
| 1127 | |||||
| 1128 | /* retrieve background and foreground colors */ | ||||
| 1129 | if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]", | ||||
| |||||
| 1130 | &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) { | ||||
| 1131 | |||||
| 1132 | /* we got a complete color filter */ | ||||
| 1133 | |||||
| 1134 | color_t bg_color, fg_color; | ||||
| 1135 | color_filter_t *colorf; | ||||
| 1136 | dfilter_t *temp_dfilter = NULL((void*)0); | ||||
| 1137 | bool_Bool enabled = !disabled; | ||||
| 1138 | |||||
| 1139 | temp_dfilter = color_filter_compile_read(name, filter_exp, &enabled, | ||||
| 1140 | user_data == &color_filter_list, | ||||
| 1141 | path, "colorfilters file"); | ||||
| 1142 | |||||
| 1143 | fg_color.red = fg_r; | ||||
| 1144 | fg_color.green = fg_g; | ||||
| 1145 | fg_color.blue = fg_b; | ||||
| 1146 | |||||
| 1147 | bg_color.red = bg_r; | ||||
| 1148 | bg_color.green = bg_g; | ||||
| 1149 | bg_color.blue = bg_b; | ||||
| 1150 | |||||
| 1151 | colorf = color_filter_new(name, filter_exp, &bg_color, | ||||
| 1152 | &fg_color, enabled); | ||||
| 1153 | if(user_data == &color_filter_list) { | ||||
| 1154 | GSList **cfl = (GSList **)user_data; | ||||
| 1155 | |||||
| 1156 | /* internal call */ | ||||
| 1157 | colorf->c_colorfilter = temp_dfilter; | ||||
| 1158 | *cfl = g_slist_append(*cfl, colorf); | ||||
| 1159 | } else { | ||||
| 1160 | /* external call */ | ||||
| 1161 | /* just editing, don't need the compiled filter, unless | ||||
| 1162 | * the caller adds the rule to the list used for matching */ | ||||
| 1163 | if (keep_compiled) { | ||||
| 1164 | colorf->c_colorfilter = temp_dfilter; | ||||
| 1165 | } else { | ||||
| 1166 | dfilter_free(temp_dfilter); | ||||
| 1167 | } | ||||
| 1168 | add_cb(colorf, user_data); | ||||
| 1169 | } | ||||
| 1170 | } /* if sscanf */ | ||||
| 1171 | |||||
| 1172 | skip_end_of_line = true1; | ||||
| 1173 | } | ||||
| 1174 | |||||
| 1175 | if (ferror(f)) | ||||
| 1176 | ret = errno(*__errno_location ()); | ||||
| 1177 | |||||
| 1178 | g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized (name, __builtin_object_size ((name), 0)) : (g_free) (name); | ||||
| 1179 | g_free(filter_exp)(__builtin_object_size ((filter_exp), 0) != ((size_t) - 1)) ? g_free_sized (filter_exp, __builtin_object_size ((filter_exp ), 0)) : (g_free) (filter_exp); | ||||
| 1180 | return ret; | ||||
| 1181 | } | ||||
| 1182 | |||||
| 1183 | /* read filters from the filter file */ | ||||
| 1184 | bool_Bool | ||||
| 1185 | color_filters_read_globals(void *user_data, char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix) | ||||
| 1186 | { | ||||
| 1187 | char *path; | ||||
| 1188 | FILE *f; | ||||
| 1189 | int ret; | ||||
| 1190 | |||||
| 1191 | /* | ||||
| 1192 | * Try to get the global filters. | ||||
| 1193 | * | ||||
| 1194 | * Get the path for the file that would have the global filters, and | ||||
| 1195 | * try to open it. | ||||
| 1196 | */ | ||||
| 1197 | path = get_datafile_path(COLORING_RULES_FILE_NAME"coloring_rules.jsonc", app_env_var_prefix); | ||||
| 1198 | if ((f = ws_fopenfopen(path, "r")) == NULL((void*)0)) { | ||||
| 1199 | if (errno(*__errno_location ()) != ENOENT2) { | ||||
| 1200 | /* Error trying to open the file; give up. */ | ||||
| 1201 | *err_msg = ws_strdup_printf("Could not open global coloring rules file\n\"%s\": %s.", path,wmem_strdup_printf(((void*)0), "Could not open global coloring rules file\n\"%s\": %s." , path, g_strerror((*__errno_location ()))) | ||||
| 1202 | g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open global coloring rules file\n\"%s\": %s." , path, g_strerror((*__errno_location ()))); | ||||
| 1203 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 1204 | return false0; | ||||
| 1205 | } | ||||
| 1206 | |||||
| 1207 | /* | ||||
| 1208 | * There is no global rules file; treat that as equivalent to | ||||
| 1209 | * that file existing bug being empty, and say we succeeded. | ||||
| 1210 | */ | ||||
| 1211 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 1212 | return true1; | ||||
| 1213 | } | ||||
| 1214 | |||||
| 1215 | /* Don't try to read the legacy colorfilters file, unlike the personal config. */ | ||||
| 1216 | |||||
| 1217 | ret = read_filters_file(path, f, user_data, add_cb, true1, false0); | ||||
| 1218 | fclose(f); | ||||
| 1219 | if (ret != 0) { | ||||
| 1220 | *err_msg = ws_strdup_printf("Error reading global coloring rules file\n\"%s\": %s.",wmem_strdup_printf(((void*)0), "Error reading global coloring rules file\n\"%s\": %s." , path, g_strerror((*__errno_location ()))) | ||||
| 1221 | path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Error reading global coloring rules file\n\"%s\": %s." , path, g_strerror((*__errno_location ()))); | ||||
| 1222 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 1223 | return false0; | ||||
| 1224 | } | ||||
| 1225 | |||||
| 1226 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 1227 | return true1; | ||||
| 1228 | } | ||||
| 1229 | |||||
| 1230 | /* read filters from some other filter file (import) */ | ||||
| 1231 | bool_Bool | ||||
| 1232 | color_filters_import(const char *path, void *user_data, char **err_msg, color_filter_add_cb_func add_cb) | ||||
| 1233 | { | ||||
| 1234 | FILE *f; | ||||
| 1235 | int ret; | ||||
| 1236 | |||||
| 1237 | if ((f = ws_fopenfopen(path, "r")) == NULL((void*)0)) { | ||||
| |||||
| 1238 | *err_msg = ws_strdup_printf("Could not open filter file\n%s\nfor reading: %s.",wmem_strdup_printf(((void*)0), "Could not open filter file\n%s\nfor reading: %s." , path, g_strerror((*__errno_location ()))) | ||||
| 1239 | path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open filter file\n%s\nfor reading: %s." , path, g_strerror((*__errno_location ()))); | ||||
| 1240 | return false0; | ||||
| 1241 | } | ||||
| 1242 | |||||
| 1243 | /* | ||||
| 1244 | * The file being imported might be in the legacy @-delimited | ||||
| 1245 | * format or the newer JSONC format. Try the legacy format first, | ||||
| 1246 | * then the JSONC format. Each parser simply finds no filters if | ||||
| 1247 | * the file isn't in the expected format. | ||||
| 1248 | */ | ||||
| 1249 | ret = read_legacy_filters_file(path, f, user_data, add_cb, false0); | ||||
| 1250 | if (ret != 0) { | ||||
| 1251 | *err_msg = ws_strdup_printf("Error reading filter file\n\"%s\": %s.",wmem_strdup_printf(((void*)0), "Error reading filter file\n\"%s\": %s." , path, g_strerror((*__errno_location ()))) | ||||
| 1252 | path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Error reading filter file\n\"%s\": %s." , path, g_strerror((*__errno_location ()))); | ||||
| 1253 | fclose(f); | ||||
| 1254 | return false0; | ||||
| 1255 | } | ||||
| 1256 | |||||
| 1257 | /* read_filters_file() seeks to the start of the file. */ | ||||
| 1258 | ret = read_filters_file(path, f, user_data, add_cb, false0, false0); | ||||
| 1259 | fclose(f); | ||||
| 1260 | if (ret != 0) { | ||||
| 1261 | *err_msg = ws_strdup_printf("Error reading filter file\n\"%s\": %s.",wmem_strdup_printf(((void*)0), "Error reading filter file\n\"%s\": %s." , path, g_strerror((*__errno_location ()))) | ||||
| 1262 | path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Error reading filter file\n\"%s\": %s." , path, g_strerror((*__errno_location ()))); | ||||
| 1263 | return false0; | ||||
| 1264 | } | ||||
| 1265 | |||||
| 1266 | return true1; | ||||
| 1267 | } | ||||
| 1268 | |||||
| 1269 | struct write_filter_data | ||||
| 1270 | { | ||||
| 1271 | FILE *f; | ||||
| 1272 | bool_Bool only_selected; | ||||
| 1273 | }; | ||||
| 1274 | |||||
| 1275 | /* save a single filter, in the legacy @-delimited format */ | ||||
| 1276 | static void | ||||
| 1277 | write_legacy_filter(void *filter_arg, void *data_arg) | ||||
| 1278 | { | ||||
| 1279 | struct write_filter_data *data = (struct write_filter_data *)data_arg; | ||||
| 1280 | color_filter_t *colorf = (color_filter_t *)filter_arg; | ||||
| 1281 | FILE *f = data->f; | ||||
| 1282 | |||||
| 1283 | if ( (!data->only_selected) && | ||||
| 1284 | (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX"___conversation_color_filter___")==NULL((void*)0)) ) { | ||||
| 1285 | fprintf(f,"%s@%s@%s@[%u,%u,%u][%u,%u,%u]\n", | ||||
| 1286 | colorf->enabled ? "" : "!", | ||||
| 1287 | colorf->filter_name, | ||||
| 1288 | colorf->filter_text, | ||||
| 1289 | colorf->bg_color.red, | ||||
| 1290 | colorf->bg_color.green, | ||||
| 1291 | colorf->bg_color.blue, | ||||
| 1292 | colorf->fg_color.red, | ||||
| 1293 | colorf->fg_color.green, | ||||
| 1294 | colorf->fg_color.blue); | ||||
| 1295 | } | ||||
| 1296 | } | ||||
| 1297 | |||||
| 1298 | /* save filters in a filter file, in the legacy @-delimited format */ | ||||
| 1299 | static bool_Bool | ||||
| 1300 | write_legacy_filters_file(GSList *cfl, FILE *f, bool_Bool only_selected, const char* app_name) | ||||
| 1301 | { | ||||
| 1302 | struct write_filter_data data; | ||||
| 1303 | |||||
| 1304 | data.f = f; | ||||
| 1305 | data.only_selected = only_selected; | ||||
| 1306 | |||||
| 1307 | fprintf(f,"# This file was created by %s. Edit with care.\n", app_name); | ||||
| 1308 | g_slist_foreach(cfl, write_legacy_filter, &data); | ||||
| 1309 | return true1; | ||||
| 1310 | } | ||||
| 1311 | |||||
| 1312 | struct write_filter_jsonc_data | ||||
| 1313 | { | ||||
| 1314 | json_dumper *dumper; | ||||
| 1315 | bool_Bool only_selected; | ||||
| 1316 | }; | ||||
| 1317 | |||||
| 1318 | /* save a single filter, in JSONC format */ | ||||
| 1319 | static void | ||||
| 1320 | write_jsonc_filter(void *filter_arg, void *data_arg) | ||||
| 1321 | { | ||||
| 1322 | struct write_filter_jsonc_data *data = (struct write_filter_jsonc_data *)data_arg; | ||||
| 1323 | color_filter_t *colorf = (color_filter_t *)filter_arg; | ||||
| 1324 | json_dumper *dumper = data->dumper; | ||||
| 1325 | char *fg_str, *bg_str; | ||||
| 1326 | |||||
| 1327 | if ( (!data->only_selected) && | ||||
| 1328 | (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX"___conversation_color_filter___")==NULL((void*)0)) ) { | ||||
| 1329 | fg_str = ws_strdup_printf("#%06x", color_t_to_rgb(&colorf->fg_color))wmem_strdup_printf(((void*)0), "#%06x", color_t_to_rgb(&colorf ->fg_color)); | ||||
| 1330 | bg_str = ws_strdup_printf("#%06x", color_t_to_rgb(&colorf->bg_color))wmem_strdup_printf(((void*)0), "#%06x", color_t_to_rgb(&colorf ->bg_color)); | ||||
| 1331 | |||||
| 1332 | json_dumper_begin_object(dumper); | ||||
| 1333 | json_dumper_set_member_name(dumper, colorf->filter_name); | ||||
| 1334 | json_dumper_begin_object(dumper); | ||||
| 1335 | json_dumper_set_member_name_const(dumper, "filter")json_dumper_set_member_name_noesc(dumper, "filter", sizeof("filter" ) - 1); | ||||
| 1336 | json_dumper_value_string(dumper, colorf->filter_text); | ||||
| 1337 | json_dumper_set_member_name_const(dumper, "foreground")json_dumper_set_member_name_noesc(dumper, "foreground", sizeof ("foreground") - 1); | ||||
| 1338 | json_dumper_value_string(dumper, fg_str); | ||||
| 1339 | json_dumper_set_member_name_const(dumper, "background")json_dumper_set_member_name_noesc(dumper, "background", sizeof ("background") - 1); | ||||
| 1340 | json_dumper_value_string(dumper, bg_str); | ||||
| 1341 | json_dumper_set_member_name_const(dumper, "enabled")json_dumper_set_member_name_noesc(dumper, "enabled", sizeof("enabled" ) - 1); | ||||
| 1342 | json_dumper_value_anyf(dumper, "%s", colorf->enabled ? "true" : "false"); | ||||
| 1343 | json_dumper_end_object(dumper); | ||||
| 1344 | json_dumper_end_object(dumper); | ||||
| 1345 | |||||
| 1346 | g_free(fg_str)(__builtin_object_size ((fg_str), 0) != ((size_t) - 1)) ? g_free_sized (fg_str, __builtin_object_size ((fg_str), 0)) : (g_free) (fg_str ); | ||||
| 1347 | g_free(bg_str)(__builtin_object_size ((bg_str), 0) != ((size_t) - 1)) ? g_free_sized (bg_str, __builtin_object_size ((bg_str), 0)) : (g_free) (bg_str ); | ||||
| 1348 | } | ||||
| 1349 | } | ||||
| 1350 | |||||
| 1351 | /* save filters in a coloring rules file, in JSONC format */ | ||||
| 1352 | static bool_Bool | ||||
| 1353 | write_filters_file(GSList *cfl, FILE *f, bool_Bool only_selected, const char* app_name) | ||||
| 1354 | { | ||||
| 1355 | struct write_filter_jsonc_data data; | ||||
| 1356 | json_dumper dumper = { | ||||
| 1357 | .output_file = f, | ||||
| 1358 | .flags = JSON_DUMPER_FLAGS_PRETTY_PRINT(1 << 0), | ||||
| 1359 | }; | ||||
| 1360 | |||||
| 1361 | data.dumper = &dumper; | ||||
| 1362 | data.only_selected = only_selected; | ||||
| 1363 | |||||
| 1364 | fprintf(f, "// This file was created by %s. Edit with care.\n", app_name); | ||||
| 1365 | |||||
| 1366 | json_dumper_begin_array(&dumper); | ||||
| 1367 | g_slist_foreach(cfl, write_jsonc_filter, &data); | ||||
| 1368 | json_dumper_end_array(&dumper); | ||||
| 1369 | |||||
| 1370 | return json_dumper_finish(&dumper); | ||||
| 1371 | } | ||||
| 1372 | |||||
| 1373 | /* save filters in users filter file(s) */ | ||||
| 1374 | bool_Bool | ||||
| 1375 | color_filters_write(GSList *cfl_all, const char* app_name, const char* app_env_var_prefix, char** err_msg) | ||||
| 1376 | { | ||||
| 1377 | char *pf_dir_path; | ||||
| 1378 | char *path; | ||||
| 1379 | FILE *f; | ||||
| 1380 | GSList *cfl = NULL((void*)0); | ||||
| 1381 | |||||
| 1382 | /* Rules inherited from parent profiles are not written to this profile */ | ||||
| 1383 | for (GSList *item = cfl_all; item; item = g_slist_next(item)((item) ? (((GSList *)(item))->next) : ((void*)0))) { | ||||
| 1384 | color_filter_t *colorf = (color_filter_t *)item->data; | ||||
| 1385 | if (colorf->source_profile == NULL((void*)0)) | ||||
| 1386 | cfl = g_slist_prepend(cfl, colorf); | ||||
| 1387 | } | ||||
| 1388 | cfl = g_slist_reverse(cfl); | ||||
| 1389 | |||||
| 1390 | /* Create the directory that holds personal configuration files, | ||||
| 1391 | if necessary. */ | ||||
| 1392 | if (create_persconffile_dir(app_env_var_prefix, &pf_dir_path) == -1) { | ||||
| 1393 | *err_msg = ws_strdup_printf("Can't create directory\n\"%s\"\nfor color files: %s.",wmem_strdup_printf(((void*)0), "Can't create directory\n\"%s\"\nfor color files: %s." , pf_dir_path, g_strerror((*__errno_location ()))) | ||||
| 1394 | pf_dir_path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Can't create directory\n\"%s\"\nfor color files: %s." , pf_dir_path, g_strerror((*__errno_location ()))); | ||||
| 1395 | g_free(pf_dir_path)(__builtin_object_size ((pf_dir_path), 0) != ((size_t) - 1)) ? g_free_sized (pf_dir_path, __builtin_object_size ((pf_dir_path ), 0)) : (g_free) (pf_dir_path); | ||||
| 1396 | g_slist_free(cfl); | ||||
| 1397 | return false0; | ||||
| 1398 | } | ||||
| 1399 | |||||
| 1400 | /* | ||||
| 1401 | * Keep an existing legacy colorfilters file current for older versions, | ||||
| 1402 | * which read only that file. Never create it. | ||||
| 1403 | */ | ||||
| 1404 | path = get_persconffile_path(COLORFILTERS_FILE_NAME"colorfilters", true1, app_env_var_prefix); | ||||
| 1405 | if (g_file_test(path, G_FILE_TEST_EXISTS)) { | ||||
| 1406 | if ((f = ws_fopenfopen(path, "w+")) == NULL((void*)0)) { | ||||
| 1407 | *err_msg = ws_strdup_printf("Could not open\n%s\nfor writing: %s.",wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s." , path, g_strerror((*__errno_location ()))) | ||||
| 1408 | path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s." , path, g_strerror((*__errno_location ()))); | ||||
| 1409 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 1410 | g_slist_free(cfl); | ||||
| 1411 | return false0; | ||||
| 1412 | } | ||||
| 1413 | write_legacy_filters_file(cfl, f, false0, app_name); | ||||
| 1414 | fclose(f); | ||||
| 1415 | } | ||||
| 1416 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 1417 | |||||
| 1418 | path = get_persconffile_path(COLORING_RULES_FILE_NAME"coloring_rules.jsonc", true1, app_env_var_prefix); | ||||
| 1419 | if ((f = ws_fopenfopen(path, "w+")) == NULL((void*)0)) { | ||||
| 1420 | *err_msg = ws_strdup_printf("Could not open\n%s\nfor writing: %s.",wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s." , path, g_strerror((*__errno_location ()))) | ||||
| 1421 | path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s." , path, g_strerror((*__errno_location ()))); | ||||
| 1422 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 1423 | g_slist_free(cfl); | ||||
| 1424 | return false0; | ||||
| 1425 | } | ||||
| 1426 | write_filters_file(cfl, f, false0, app_name); | ||||
| 1427 | fclose(f); | ||||
| 1428 | g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized (path, __builtin_object_size ((path), 0)) : (g_free) (path); | ||||
| 1429 | g_slist_free(cfl); | ||||
| 1430 | return true1; | ||||
| 1431 | } | ||||
| 1432 | |||||
| 1433 | /* Save filters in the specified file (export) */ | ||||
| 1434 | bool_Bool | ||||
| 1435 | color_filters_export(const char *path, GSList *cfl, bool_Bool only_marked, const char* app_name, char** err_msg) | ||||
| 1436 | { | ||||
| 1437 | FILE *f; | ||||
| 1438 | |||||
| 1439 | if ((f = ws_fopenfopen(path, "w+")) == NULL((void*)0)) { | ||||
| 1440 | *err_msg = ws_strdup_printf("Could not open\n%s\nfor writing: %s.",wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s." , path, g_strerror((*__errno_location ()))) | ||||
| 1441 | path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s." , path, g_strerror((*__errno_location ()))); | ||||
| 1442 | return false0; | ||||
| 1443 | } | ||||
| 1444 | write_filters_file(cfl, f, only_marked, app_name); | ||||
| 1445 | fclose(f); | ||||
| 1446 | return true1; | ||||
| 1447 | } | ||||
| 1448 | |||||
| 1449 | /* | ||||
| 1450 | * Editor modelines - https://www.wireshark.org/tools/modelines.html | ||||
| 1451 | * | ||||
| 1452 | * Local variables: | ||||
| 1453 | * c-basic-offset: 4 | ||||
| 1454 | * tab-width: 8 | ||||
| 1455 | * indent-tabs-mode: nil | ||||
| 1456 | * End: | ||||
| 1457 | * | ||||
| 1458 | * vi: set shiftwidth=4 tabstop=8 expandtab: | ||||
| 1459 | * :indentSize=4:tabSize=8:noTabs=true: | ||||
| 1460 | */ |