Bug Summary

File:builds/wireshark/wireshark/epan/color_filters.c
Warning:line 1120, column 13
File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name color_filters.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /builds/wireshark/wireshark/epan -isystem /builds/wireshark/wireshark/build/epan -isystem /usr/include/mit-krb5 -isystem /usr/include/lua5.5 -isystem /usr/include/libxml2 -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D REX_API=extern -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -D epan_EXPORTS -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/wiretap -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-11-28-100325-3662-1 -x c /builds/wireshark/wireshark/epan/color_filters.c
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
63static 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);
64static int read_legacy_filters_file(const char *path, FILE *f, void *user_data, color_filter_add_cb_func add_cb, bool_Bool keep_compiled);
65static 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 */
70static 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) */
74static GSList *color_filter_deleted_list;
75static GSList *color_filter_valid_list;
76
77/* Color Filters can en-/disabled. */
78static 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 */
83static bool_Bool tmp_colors_set;
84
85/* Create a new filter */
86color_filter_t *
87color_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 */
105static void
106color_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
144static int
145color_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 */
154char*
155color_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 */
178bool_Bool
179color_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
231const color_filter_t *
232color_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 */
248bool_Bool
249color_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 */
263void
264color_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) */
274static void
275color_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 */
283void
284color_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 */
291static color_filter_t *
292color_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
308static void
309color_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 */
319static GSList *
320color_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 */
334static bool_Bool
335color_filters_get_own(char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix,
336 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 /*
355 * If the personal coloring_rules.jsonc exists, it is the user's rule
356 * list and the only file read. A legacy colorfilters file next to it
357 * is an older copy kept for older versions; reading it as well would
358 * bring back rules the user has since deleted or changed.
359 */
360 if ((f = ws_fopenfopen(path, "r")) != NULL((void*)0)) {
361 ret = read_filters_file(path, f, &color_filter_list, add_cb, true1, false0);
362 fclose(f);
363 if (ret != 0) {
364 *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))
365 path, g_strerror(ret))wmem_strdup_printf(((void*)0), "Error reading coloring rules file\n\"%s\": %s."
, path, g_strerror(ret))
;
366 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)
;
367 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
368 return false0;
369 }
370 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)
;
371 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
372 return true1;
373 }
374 if (errno(*__errno_location ()) != ENOENT2) {
375 *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 ())))
376 g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open coloring rules file\n\"%s\": %s."
, path, g_strerror((*__errno_location ())))
;
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 false0;
380 }
381 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
382
383 /*
384 * No personal coloring_rules.jsonc. A legacy colorfilters file from a
385 * version before the JSONC format is the complete personal rule list,
386 * so the global rules are not added to it. The next save writes it
387 * out as coloring_rules.jsonc.
388 */
389 if ((f = ws_fopenfopen(legacy_path, "r")) != NULL((void*)0)) {
390 ret = read_legacy_filters_file(legacy_path, f, &color_filter_list, add_cb, false0);
391 fclose(f);
392 if (ret != 0) {
393 *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))
394 legacy_path, g_strerror(ret))wmem_strdup_printf(((void*)0), "Error reading filter file\n\"%s\": %s."
, legacy_path, g_strerror(ret))
;
395 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)
;
396 return false0;
397 }
398 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)
;
399 return true1;
400 }
401 if (errno(*__errno_location ()) != ENOENT2) {
402 *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 ())))
403 g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open filter file\n\"%s\": %s."
, legacy_path, g_strerror((*__errno_location ())))
;
404 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)
;
405 return false0;
406 }
407 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)
;
408
409 if (!fallback_to_globals) {
410 /* Rules are inherited from the parent profiles instead */
411 return true1;
412 }
413
414 /* They don't have any personal rules; try to read the global rules */
415 return color_filters_read_globals(&color_filter_list, err_msg, add_cb, app_env_var_prefix);
416}
417
418/* Collect rules read from a parent profile, in file order */
419static void
420color_filters_collect_cb(color_filter_t *colorf, void *user_data)
421{
422 GSList **cfl = (GSList **)user_data;
423
424 *cfl = g_slist_append(*cfl, colorf);
425}
426
427typedef struct {
428 color_filter_add_cb_func add_cb; /* called for each inherited rule */
429 void *user_data;
430 bool_Bool keep_compiled; /* the rules are used for matching, so they are compiled when read */
431} color_filters_layer_read_t;
432
433/*
434 * Read the rules of one parent profile. A rule is skipped if it is a
435 * conversation rule, if its name or filter was seen in a nearer profile,
436 * or if it is disabled; a disabled rule still hides the same rule from
437 * profiles further up.
438 */
439static bool_Bool
440color_filters_read_layer(const profile_inherit_layer_t *layer, const char *path, const char *filename,
441 profile_inherit_seen_t *seen, void *user_data, char **err)
442{
443 color_filters_layer_read_t *layer_read = (color_filters_layer_read_t *)user_data;
444 GSList *layer_list = NULL((void*)0);
445 FILE *f;
446 int ret;
447
448 if ((f = ws_fopenfopen(path, "r")) == NULL((void*)0)) {
449 *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 ())))
;
450 return false0;
451 }
452 if (strcmp(filename, COLORFILTERS_FILE_NAME"colorfilters") == 0) {
453 ret = read_legacy_filters_file(path, f, &layer_list, color_filters_collect_cb, layer_read->keep_compiled);
454 } else {
455 ret = read_filters_file(path, f, &layer_list, color_filters_collect_cb, true1, layer_read->keep_compiled);
456 }
457 fclose(f);
458 if (ret != 0) {
459 *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))
;
460 color_filter_list_delete(&layer_list);
461 return false0;
462 }
463
464 for (GSList *item = layer_list; item; item = g_slist_next(item)((item) ? (((GSList *)(item))->next) : ((void*)0))) {
465 color_filter_t *colorf = (color_filter_t *)item->data;
466 bool_Bool keep = !color_filter_is_tmp(colorf) &&
467 !profile_inherit_seen_check(seen, colorf->filter_name, colorf->filter_text) &&
468 colorf->enabled;
469
470 if (!keep) {
471 color_filter_delete(colorf);
472 continue;
473 }
474 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
)
;
475 colorf->source_profile = g_strdup(layer->source)g_strdup_inline (layer->source);
476 layer_read->add_cb(colorf, layer_read->user_data);
477 }
478 g_slist_free(layer_list);
479 return true1;
480}
481
482/* Add an inherited rule, compiled when read, to the list used for matching */
483static void
484color_filters_add_inherited_cb(color_filter_t *colorf, void *user_data _U___attribute__((unused)))
485{
486 color_filter_list = g_slist_append(color_filter_list, colorf);
487}
488
489/*
490 * Read the inherited rules. Rules duplicating a rule in own_list, which may
491 * be NULL, are not inherited.
492 */
493static bool_Bool
494color_filters_add_inherited(GSList *own_list, void *user_data, char **err_msg,
495 color_filter_add_cb_func add_cb, const char *app_env_var_prefix)
496{
497 profile_inherit_seen_t *seen = profile_inherit_seen_new();
498 /* The rules read for matching are compiled once, when read */
499 color_filters_layer_read_t layer_read = { add_cb, user_data, add_cb == color_filters_add_inherited_cb };
500
501 /* The profile's own rules come first, and win over duplicates from parents */
502 for (GSList *item = own_list; item; item = g_slist_next(item)((item) ? (((GSList *)(item))->next) : ((void*)0))) {
503 color_filter_t *colorf = (color_filter_t *)item->data;
504 if (!color_filter_is_tmp(colorf))
505 profile_inherit_seen_check(seen, colorf->filter_name, colorf->filter_text);
506 }
507 profile_inherit_seen_next(seen);
508
509 bool_Bool ret = profile_inherit_read_layers(app_env_var_prefix, COLORING_RULES_FILE_NAME"coloring_rules.jsonc", COLORFILTERS_FILE_NAME"colorfilters", true1,
510 true1, seen, color_filters_read_layer, &layer_read, err_msg);
511
512 profile_inherit_seen_free(seen);
513 return ret;
514}
515
516bool_Bool
517color_filters_read_inherited(void *user_data, char** err_msg,
518 color_filter_add_cb_func add_cb, const char* app_env_var_prefix)
519{
520 return color_filters_add_inherited(NULL((void*)0), user_data, err_msg, add_cb, app_env_var_prefix);
521}
522
523static bool_Bool
524color_filters_get(char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix)
525{
526 bool_Bool include_parent = profile_get_include_parent(app_env_var_prefix, NULL((void*)0), COLORING_RULES_FILE_NAME"coloring_rules.jsonc");
527
528 /* start the list with the temporary colorizing rules */
529 color_filters_add_tmp(&color_filter_list);
530
531 if (!color_filters_get_own(err_msg, add_cb, app_env_var_prefix, !include_parent)) {
532 return false0;
533 }
534
535 if (!include_parent) {
536 return true1;
537 }
538
539 return color_filters_add_inherited(color_filter_list, NULL((void*)0), err_msg,
540 color_filters_add_inherited_cb, app_env_var_prefix);
541}
542
543/* Initialize the filter structures (reading from file) for general running, including app startup */
544bool_Bool
545color_filters_init(char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix)
546{
547 /* delete all currently existing filters */
548 color_filter_list_delete(&color_filter_list);
549
550 /* now try to construct the filters list */
551 return color_filters_get(err_msg, add_cb, app_env_var_prefix);
552}
553
554bool_Bool
555color_filters_reload(char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix)
556{
557 /* "move" old entries to the deleted list
558 * we must keep them until the dissection no longer needs them */
559 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
560 color_filter_list = NULL((void*)0);
561
562 /* now try to construct the filters list */
563 return color_filters_get(err_msg, add_cb, app_env_var_prefix);
564}
565
566void
567color_filters_cleanup(void)
568{
569 /* delete the previously deleted filters */
570 color_filter_list_delete(&color_filter_deleted_list);
571}
572
573typedef struct _color_clone
574{
575 void *user_data;
576 color_filter_add_cb_func add_cb;
577} color_clone_t;
578
579static void
580color_filters_clone_cb(void *filter_arg, void *user_data)
581{
582 color_clone_t* clone_data = (color_clone_t*)user_data;
583 color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg);
584
585 clone_data->add_cb (new_colorf, clone_data->user_data);
586}
587
588void
589color_filters_clone(void *user_data, color_filter_add_cb_func add_cb)
590{
591 color_clone_t clone_data;
592
593 clone_data.user_data = user_data;
594 clone_data.add_cb = add_cb;
595 g_slist_foreach(color_filter_list, color_filters_clone_cb, &clone_data);
596}
597
598/* A rule takes part in matching if it compiled and is enabled. A disabled
599 * rule takes part only when asked to, and never a disabled conversation
600 * rule: unused conversation slots are compiled as "frame" and would match
601 * every packet. */
602static bool_Bool
603color_filter_in_play(const color_filter_t *colorf, bool_Bool include_disabled)
604{
605 if (colorf->c_colorfilter == NULL((void*)0))
606 return false0;
607 if (colorf->enabled)
608 return true1;
609 return include_disabled && !color_filter_is_tmp(colorf);
610}
611
612/* Compiles a filter without reporting errors. Disabled rules are compiled
613 * this way so that they can be matched while disabled rules are shown; one
614 * that does not compile simply never matches. */
615static dfilter_t *
616color_filter_compile_quiet(const char *text)
617{
618 dfilter_t *df = NULL((void*)0);
619 df_error_t *df_err = NULL((void*)0);
620
621 if (!dfilter_compile(text, &df, &df_err)dfilter_compile_full(text, &df, &df_err, (1U <<
1)|(1U << 2), __func__)
) {
622 df_error_free(&df_err);
623 }
624 return df;
625}
626
627/* Compiles the filter of a rule read from a file. An enabled rule that does
628 * not compile is reported and disabled. A disabled rule is compiled quietly
629 * when compile_disabled is set (rules read into the list used for matching)
630 * and not at all otherwise (rules read for editing). */
631static dfilter_t *
632color_filter_compile_read(const char *name, const char *text, bool_Bool *enabled,
633 bool_Bool compile_disabled, const char *path,
634 const char *file_kind)
635{
636 dfilter_t *df = NULL((void*)0);
637 df_error_t *df_err = NULL((void*)0);
638
639 if (!*enabled) {
640 return compile_disabled ? color_filter_compile_quiet(text) : NULL((void*)0);
641 }
642 if (!dfilter_compile(text, &df, &df_err)dfilter_compile_full(text, &df, &df_err, (1U <<
1)|(1U << 2), __func__)
) {
643 report_warning("Disabling color filter: Could not compile \"%s\" in %s \"%s\".\n%s",
644 name, file_kind, path, df_err->msg);
645 df_error_free(&df_err);
646 *enabled = false0;
647 }
648 return df;
649}
650
651static void
652color_filter_compile_cb(void *filter_arg, void *err)
653{
654 color_filter_t *colorf = (color_filter_t *)filter_arg;
655 char **err_msg = (char**)err;
656 df_error_t *df_err = NULL((void*)0);
657
658 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"
, 658, __func__, "assertion failed: %s", "colorf->c_colorfilter == ((void*)0)"
); } while (0)
;
659
660 /* Disabled conversation rules are never used. */
661 if (!colorf->enabled) {
662 if (!color_filter_is_tmp(colorf)) {
663 colorf->c_colorfilter = color_filter_compile_quiet(colorf->filter_text);
664 }
665 return;
666 }
667
668 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__
)
) {
669 *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)
670 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)
;
671 df_error_free(&df_err);
672 /* this filter was compilable before, so this should never happen */
673 /* except if the OK button of the parent window has been clicked */
674 /* so don't use ws_assert_not_reached() but check the filters again */
675 }
676}
677
678static void
679color_filter_validate_cb(void *filter_arg, void *err)
680{
681 color_filter_t *colorf = (color_filter_t *)filter_arg;
682 char **err_msg = (char**)err;
683 df_error_t *df_err = NULL((void*)0);
684
685 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"
, 685, __func__, "assertion failed: %s", "colorf->c_colorfilter == ((void*)0)"
); } while (0)
;
686
687 /* If the filter is disabled it doesn't matter if it compiles or not. */
688 if (!colorf->enabled) return;
689
690 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__
)
) {
691 *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)
692 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)
;
693 df_error_free(&df_err);
694
695 /* Disable the color filter in the list of color filters. */
696 colorf->enabled = false0;
697 }
698
699 /* XXX: What if the color filter tests "frame.coloring_rule.name" or
700 * "frame.coloring_rule.string"?
701 */
702}
703
704/* apply changes from the edit list */
705bool_Bool
706color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl, char** err_msg)
707{
708 bool_Bool ret = true1;
709
710 *err_msg = NULL((void*)0);
711
712 /* "move" old entries to the deleted list
713 * we must keep them until the dissection no longer needs them */
714 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
715 color_filter_list = NULL((void*)0);
716
717 /* clone all list entries from tmp/edit to normal list */
718 color_filter_list_delete(&color_filter_valid_list);
719 color_filter_valid_list = color_filter_list_clone(tmp_cfl);
720 color_filter_valid_list = g_slist_concat(color_filter_valid_list,
721 color_filter_list_clone(edit_cfl) );
722
723 /* compile all filter */
724 g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, err_msg);
725 if (*err_msg != NULL((void*)0)) {
726 ret = false0;
727 }
728
729 /* clone all list entries from tmp/edit to normal list */
730 color_filter_list = color_filter_list_clone(color_filter_valid_list);
731
732 /* compile all filter */
733 g_slist_foreach(color_filter_list, color_filter_compile_cb, err_msg);
734 if (*err_msg != NULL((void*)0)) {
735 ret = false0;
736 }
737
738 return ret;
739}
740
741bool_Bool
742color_filters_used(void)
743{
744 return color_filter_list != NULL((void*)0) && filters_enabled;
745}
746
747bool_Bool
748tmp_color_filters_used(void)
749{
750 return tmp_colors_set;
751}
752
753/* prepare the epan_dissect_t for the filter */
754static void
755prime_edt(void *data, void *user_data)
756{
757 color_filter_t *colorf = (color_filter_t *)data;
758 epan_dissect_t *edt = (epan_dissect_t *)user_data;
759
760 if (color_filter_in_play(colorf, prefs.gui_coloring_rules_include_disabled))
761 epan_dissect_prime_with_dfilter(edt, colorf->c_colorfilter);
762}
763
764/* Prime the epan_dissect_t with all the compiler
765 * color filters in 'color_filter_list'. */
766void
767color_filters_prime_edt(epan_dissect_t *edt)
768{
769 if (color_filters_used())
770 g_slist_foreach(color_filter_list, prime_edt, edt);
771}
772
773static int
774find_hfid(const void *data, const void *user_data)
775{
776 color_filter_t *colorf = (color_filter_t *)data;
777 int hfid = GPOINTER_TO_INT(user_data)((gint) (glong) (user_data));
778
779 if (color_filter_in_play(colorf, prefs.gui_coloring_rules_include_disabled)) {
780 if (dfilter_interested_in_field(colorf->c_colorfilter, hfid)) {
781 return 0;
782 }
783 }
784 return -1;
785}
786
787bool_Bool
788color_filters_use_hfid(int hfid)
789{
790 GSList *item = NULL((void*)0);
791 if (color_filters_used())
792 item = g_slist_find_custom(color_filter_list, GINT_TO_POINTER(hfid)((gpointer) (glong) (hfid)), find_hfid);
793 return (item != NULL((void*)0));
794}
795
796static int
797find_proto(const void *data, const void *user_data)
798{
799 color_filter_t *colorf = (color_filter_t *)data;
800 int proto_id = GPOINTER_TO_INT(user_data)((gint) (glong) (user_data));
801
802 if (color_filter_in_play(colorf, prefs.gui_coloring_rules_include_disabled)) {
803 if (dfilter_interested_in_proto(colorf->c_colorfilter, proto_id)) {
804 return 0;
805 }
806 }
807 return -1;
808}
809
810bool_Bool
811color_filters_use_proto(int proto_id)
812{
813 GSList *item = NULL((void*)0);
814 if (color_filters_used())
815 item = g_slist_find_custom(color_filter_list, GINT_TO_POINTER(proto_id)((gpointer) (glong) (proto_id)), find_proto);
816 return (item != NULL((void*)0));
817}
818
819const color_filter_t *
820color_filters_colorize_packet_all(epan_dissect_t *edt,
821 wmem_allocator_t *scope, wmem_list_t **matches, bool_Bool include_disabled)
822{
823 const color_filter_t *first_match = NULL((void*)0);
824
825 if (matches) {
826 *matches = NULL((void*)0);
827 }
828
829 /* If we have color filters, collect ALL matching ones. */
830 if ((edt->tree != NULL((void*)0)) && (color_filters_used())) {
831 for (GSList *curr = color_filter_list; curr != NULL((void*)0); curr = g_slist_next(curr)((curr) ? (((GSList *)(curr))->next) : ((void*)0))) {
832 color_filter_t *colorf = (color_filter_t *)curr->data;
833 if (color_filter_in_play(colorf, include_disabled) &&
834 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
835
836 if (matches) {
837 if (*matches == NULL((void*)0)) {
838 *matches = wmem_list_new(scope);
839 }
840 wmem_list_append(*matches, colorf);
841 }
842
843 /* A disabled rule never colors the packet */
844 if (!first_match && colorf->enabled) {
845 first_match = colorf; /* Backward compatibility */
846 }
847 }
848 }
849 }
850
851 return first_match;
852}
853
854/* Parse a "#rrggbb" or "rrggbb" string into a color_t. Returns false (and
855 * leaves *color untouched) if str is NULL or not a valid hex triplet. */
856static bool_Bool
857hex_str_to_color(const char *str, color_t *color)
858{
859 unsigned long cval;
860
861 if (str == NULL((void*)0)) {
862 return false0;
863 }
864 if (str[0] == '#') {
865 str++;
866 }
867 if (strlen(str) != 6 || strspn(str, "0123456789abcdefABCDEF") != 6) {
868 return false0;
869 }
870
871 cval = strtoul(str, NULL((void*)0), 16);
872 color->red = RED_COMPONENT(cval)(uint16_t) (((((cval) >> 16) & 0xff) * 65535 / 255)
)
;
873 color->green = GREEN_COMPONENT(cval)(uint16_t) (((((cval) >> 8) & 0xff) * 65535 / 255));
874 color->blue = BLUE_COMPONENT(cval)(uint16_t) ( (((cval) & 0xff) * 65535 / 255));
875 return true1;
876}
877
878static int
879read_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)
880{
881 long size;
882 char *buf;
883 size_t nread;
884 int num_tokens;
885 jsmntok_t *tokens;
886 int array_len;
887 jsmntok_t *element;
888
889 if (fseek(f, 0, SEEK_END2) == -1) {
890 return errno(*__errno_location ());
891 }
892 size = ftell(f);
893 if (size < 0) {
894 return errno(*__errno_location ());
895 }
896 if (fseek(f, 0, SEEK_SET0) == -1) {
897 return errno(*__errno_location ());
898 }
899 if (size == 0) {
900 return 0;
901 }
902
903 buf = (char *)g_malloc(size + 1);
904 nread = fread(buf, 1, size, f);
905 if (ferror(f)) {
906 int ret = errno(*__errno_location ());
907 g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized
(buf, __builtin_object_size ((buf), 0)) : (g_free) (buf)
;
908 return ret;
909 }
910 buf[nread] = '\0';
911
912 if (!json_strip_jsonc_comments(buf)) {
913 report_warning("Error parsing coloring rules file\n\"%s\": unterminated block comment.", path);
914 g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized
(buf, __builtin_object_size ((buf), 0)) : (g_free) (buf)
;
915 return 0;
916 }
917
918 num_tokens = json_parse(buf, NULL((void*)0), 0);
919 if (num_tokens == 0) {
920 /* Only comments and whitespace: a valid, empty rule list. */
921 g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized
(buf, __builtin_object_size ((buf), 0)) : (g_free) (buf)
;
922 return 0;
923 }
924 if (num_tokens < 0) {
925 /*
926 * Not JSON at all. For the personal and global rule files that is
927 * an error the user must see, since none of the file's rules are
928 * used. An imported file may be in the legacy format, which never
929 * parses as JSON, so it just yields no rules here.
930 */
931 if (require_json) {
932 report_warning("Error parsing coloring rules file\n\"%s\": invalid JSON.", path);
933 }
934 g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized
(buf, __builtin_object_size ((buf), 0)) : (g_free) (buf)
;
935 return 0;
936 }
937
938 tokens = g_new(jsmntok_t, num_tokens)((jsmntok_t *) g_malloc_n ((num_tokens), sizeof (jsmntok_t)));
939 if (json_parse(buf, tokens, num_tokens) <= 0 || tokens[0].type != JSMN_ARRAY) {
940 report_warning("Error parsing coloring rules file\n\"%s\": invalid JSON.", path);
941 g_free(tokens)(__builtin_object_size ((tokens), 0) != ((size_t) - 1)) ? g_free_sized
(tokens, __builtin_object_size ((tokens), 0)) : (g_free) (tokens
)
;
942 g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized
(buf, __builtin_object_size ((buf), 0)) : (g_free) (buf)
;
943 return 0;
944 }
945
946 array_len = json_get_array_len(&tokens[0]);
947 element = json_get_array_index(&tokens[0], 0);
948
949 for (int i = 0; i < array_len; i++, element = json_get_next_object(element)) {
950 char *name, *filter_str, *fg_str, *bg_str;
951 bool_Bool enabled = true1;
952 color_t fg_color = { 0, 0, 0 }, bg_color = { 0, 0, 0 };
953 dfilter_t *temp_dfilter = NULL((void*)0);
954 const char *filter_exp;
955 color_filter_t *colorf;
956 jsmntok_t *name_tok, *properties_tok;
957
958 if (element->type != JSMN_OBJECT || element->size != 1) {
959 continue;
960 }
961
962 /* "<filter name>": { ... } */
963 name_tok = element + 1;
964 properties_tok = name_tok + 1;
965 if (name_tok->type != JSMN_STRING || properties_tok->type != JSMN_OBJECT) {
966 continue;
967 }
968
969 name = g_strndup(&buf[name_tok->start], (gsize)(name_tok->end - name_tok->start));
970 if (!json_decode_string_inplace(name)) {
971 g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized
(name, __builtin_object_size ((name), 0)) : (g_free) (name)
;
972 continue;
973 }
974
975 filter_str = json_get_string(buf, properties_tok, "filter");
976 fg_str = json_get_string(buf, properties_tok, "foreground");
977 bg_str = json_get_string(buf, properties_tok, "background");
978 json_get_boolean(buf, properties_tok, "enabled", &enabled);
979
980 hex_str_to_color(fg_str, &fg_color);
981 hex_str_to_color(bg_str, &bg_color);
982
983 filter_exp = filter_str ? filter_str : "";
984
985 temp_dfilter = color_filter_compile_read(name, filter_exp, &enabled,
986 user_data == &color_filter_list,
987 path, "coloring rules file");
988
989 colorf = color_filter_new(name, filter_exp, &bg_color, &fg_color, enabled);
990 g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized
(name, __builtin_object_size ((name), 0)) : (g_free) (name)
;
991
992 if (user_data == &color_filter_list) {
993 GSList **cfl = (GSList **)user_data;
994
995 /* internal call */
996 colorf->c_colorfilter = temp_dfilter;
997 *cfl = g_slist_append(*cfl, colorf);
998 } else {
999 /* external call */
1000 /* just editing, don't need the compiled filter, unless
1001 * the caller adds the rule to the list used for matching */
1002 if (keep_compiled) {
1003 colorf->c_colorfilter = temp_dfilter;
1004 } else {
1005 dfilter_free(temp_dfilter);
1006 }
1007 add_cb(colorf, user_data);
1008 }
1009 }
1010
1011 g_free(tokens)(__builtin_object_size ((tokens), 0) != ((size_t) - 1)) ? g_free_sized
(tokens, __builtin_object_size ((tokens), 0)) : (g_free) (tokens
)
;
1012 g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized
(buf, __builtin_object_size ((buf), 0)) : (g_free) (buf)
;
1013 return 0;
1014}
1015
1016/* Read legacy (@-delimited) filters from the given file */
1017/* XXX At what point do we drop support? */
1018/* XXX Would it make more sense to use GStrings here instead of reallocing
1019 our buffers? */
1020static int
1021read_legacy_filters_file(const char *path, FILE *f, void *user_data, color_filter_add_cb_func add_cb, bool_Bool keep_compiled)
1022{
1023#define INIT_BUF_SIZE128 128
1024 char *name;
1025 char *filter_exp;
1026 uint32_t name_len = INIT_BUF_SIZE128;
1027 uint32_t filter_exp_len = INIT_BUF_SIZE128;
1028 uint32_t i = 0;
1029 int c;
1030 uint16_t fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
1031 bool_Bool disabled = false0;
1032 bool_Bool skip_end_of_line = false0;
1033 int ret = 0;
1034
1035 name = (char *)g_malloc(name_len + 1);
1036 filter_exp = (char *)g_malloc(filter_exp_len + 1);
1037
1038 while (1) {
3
Loop condition is true. Entering loop body
37
Loop condition is true. Entering loop body
1039
1040 if (skip_end_of_line
3.1
'skip_end_of_line' is false
37.1
'skip_end_of_line' is true
) {
38
Taking true branch
1041 do {
41
Loop condition is false. Exiting loop
1042 c = ws_getc_unlockedgetc_unlocked(f);
1043 } while (c != EOF(-1) && c != '\n');
39
Assuming the condition is true
40
Assuming the condition is false
1044 if (c == EOF(-1))
42
Taking false branch
1045 break;
1046 disabled = false0;
1047 skip_end_of_line = false0;
1048 }
1049
1050 while ((c = ws_getc_unlockedgetc_unlocked(f)) != EOF(-1) && g_ascii_isspace(c)((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0)) {
4
Assuming the condition is true
5
Loop condition is false. Execution continues on line 1056
43
Assuming the condition is true
44
Loop condition is false. Execution continues on line 1056
1051 if (c == '\n') {
1052 continue;
1053 }
1054 }
1055
1056 if (c == EOF(-1))
6
Taking false branch
45
Taking false branch
1057 break;
1058
1059 if (c == '!') {
7
Assuming the condition is false
8
Taking false branch
46
Assuming the condition is false
47
Taking false branch
1060 disabled = true1;
1061 continue;
1062 }
1063
1064 /* skip # comments and invalid lines */
1065 if (c != '@') {
9
Assuming the condition is false
10
Taking false branch
48
Assuming the condition is false
49
Taking false branch
1066 skip_end_of_line = true1;
1067 continue;
1068 }
1069
1070 /* we get the @ delimiter.
1071 * Format is:
1072 * @name@filter expression@[background r,g,b][foreground r,g,b]
1073 */
1074
1075 /* retrieve name */
1076 i = 0;
1077 while (1) {
11
Loop condition is true. Entering loop body
16
Loop condition is true. Entering loop body
50
Loop condition is true. Entering loop body
55
Loop condition is true. Entering loop body
1078 c = ws_getc_unlockedgetc_unlocked(f);
1079 if (c == EOF(-1) || c == '@')
12
Assuming the condition is false
13
Assuming the condition is false
14
Taking false branch
17
Assuming the condition is false
18
Assuming the condition is true
19
Taking true branch
51
Assuming the condition is false
52
Assuming the condition is false
53
Taking false branch
56
Assuming the condition is false
57
Assuming the condition is true
58
Taking true branch
1080 break;
20
Execution continues on line 1088
59
Execution continues on line 1088
1081 if (i
14.1
'i' is < 'name_len'
53.1
'i' is < 'name_len'
>= name_len) {
15
Taking false branch
54
Taking false branch
1082 /* buffer isn't long enough; double its length.*/
1083 name_len *= 2;
1084 name = (char *)g_realloc(name, name_len + 1);
1085 }
1086 name[i++] = c;
1087 }
1088 name[i] = '\0';
1089
1090 if (c == EOF(-1)) {
21
Taking false branch
60
Taking false branch
1091 break;
1092 } else if (i
21.1
'i' is not equal to 0
60.1
'i' is not equal to 0
== 0) {
22
Taking false branch
61
Taking false branch
1093 skip_end_of_line = true1;
1094 continue;
1095 }
1096
1097 /* retrieve filter expression */
1098 i = 0;
1099 while (1) {
23
Loop condition is true. Entering loop body
28
Loop condition is true. Entering loop body
62
Loop condition is true. Entering loop body
67
Loop condition is true. Entering loop body
1100 c = ws_getc_unlockedgetc_unlocked(f);
1101 if (c == EOF(-1) || c == '@')
24
Assuming the condition is false
25
Assuming the condition is false
26
Taking false branch
29
Assuming the condition is false
30
Assuming the condition is true
31
Taking true branch
63
Assuming the condition is false
64
Assuming the condition is false
65
Taking false branch
68
Assuming the condition is false
69
Assuming the condition is true
70
Taking true branch
1102 break;
32
Execution continues on line 1110
71
Execution continues on line 1110
1103 if (i
26.1
'i' is < 'filter_exp_len'
65.1
'i' is < 'filter_exp_len'
>= filter_exp_len) {
27
Taking false branch
66
Taking false branch
1104 /* buffer isn't long enough; double its length.*/
1105 filter_exp_len *= 2;
1106 filter_exp = (char *)g_realloc(filter_exp, filter_exp_len + 1);
1107 }
1108 filter_exp[i++] = c;
1109 }
1110 filter_exp[i] = '\0';
1111
1112 if (c == EOF(-1)) {
33
Taking false branch
72
Taking false branch
1113 break;
1114 } else if (i
33.1
'i' is not equal to 0
72.1
'i' is not equal to 0
== 0) {
34
Taking false branch
73
Taking false branch
1115 skip_end_of_line = true1;
1116 continue;
1117 }
1118
1119 /* retrieve background and foreground colors */
1120 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
35
Assuming this stream operation fails
36
Taking false branch
74
File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior
1121 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
1122
1123 /* we got a complete color filter */
1124
1125 color_t bg_color, fg_color;
1126 color_filter_t *colorf;
1127 dfilter_t *temp_dfilter = NULL((void*)0);
1128 bool_Bool enabled = !disabled;
1129
1130 temp_dfilter = color_filter_compile_read(name, filter_exp, &enabled,
1131 user_data == &color_filter_list,
1132 path, "colorfilters file");
1133
1134 fg_color.red = fg_r;
1135 fg_color.green = fg_g;
1136 fg_color.blue = fg_b;
1137
1138 bg_color.red = bg_r;
1139 bg_color.green = bg_g;
1140 bg_color.blue = bg_b;
1141
1142 colorf = color_filter_new(name, filter_exp, &bg_color,
1143 &fg_color, enabled);
1144 if(user_data == &color_filter_list) {
1145 GSList **cfl = (GSList **)user_data;
1146
1147 /* internal call */
1148 colorf->c_colorfilter = temp_dfilter;
1149 *cfl = g_slist_append(*cfl, colorf);
1150 } else {
1151 /* external call */
1152 /* just editing, don't need the compiled filter, unless
1153 * the caller adds the rule to the list used for matching */
1154 if (keep_compiled) {
1155 colorf->c_colorfilter = temp_dfilter;
1156 } else {
1157 dfilter_free(temp_dfilter);
1158 }
1159 add_cb(colorf, user_data);
1160 }
1161 } /* if sscanf */
1162
1163 skip_end_of_line = true1;
1164 }
1165
1166 if (ferror(f))
1167 ret = errno(*__errno_location ());
1168
1169 g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized
(name, __builtin_object_size ((name), 0)) : (g_free) (name)
;
1170 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)
;
1171 return ret;
1172}
1173
1174/* read filters from the filter file */
1175bool_Bool
1176color_filters_read_globals(void *user_data, char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix)
1177{
1178 char *path;
1179 FILE *f;
1180 int ret;
1181
1182 /*
1183 * Try to get the global filters.
1184 *
1185 * Get the path for the file that would have the global filters, and
1186 * try to open it.
1187 */
1188 path = get_datafile_path(COLORING_RULES_FILE_NAME"coloring_rules.jsonc", app_env_var_prefix);
1189 if ((f = ws_fopenfopen(path, "r")) == NULL((void*)0)) {
1190 if (errno(*__errno_location ()) != ENOENT2) {
1191 /* Error trying to open the file; give up. */
1192 *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 ())))
1193 g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open global coloring rules file\n\"%s\": %s."
, path, g_strerror((*__errno_location ())))
;
1194 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1195 return false0;
1196 }
1197
1198 /*
1199 * There is no global rules file; treat that as equivalent to
1200 * that file existing bug being empty, and say we succeeded.
1201 */
1202 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1203 return true1;
1204 }
1205
1206 /* Don't try to read the legacy colorfilters file, unlike the personal config. */
1207
1208 ret = read_filters_file(path, f, user_data, add_cb, true1, false0);
1209 fclose(f);
1210 if (ret != 0) {
1211 *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 ())))
1212 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Error reading global coloring rules file\n\"%s\": %s."
, path, g_strerror((*__errno_location ())))
;
1213 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1214 return false0;
1215 }
1216
1217 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1218 return true1;
1219}
1220
1221/* read filters from some other filter file (import) */
1222bool_Bool
1223color_filters_import(const char *path, void *user_data, char **err_msg, color_filter_add_cb_func add_cb)
1224{
1225 FILE *f;
1226 int ret;
1227
1228 if ((f = ws_fopenfopen(path, "r")) == NULL((void*)0)) {
1
Taking false branch
1229 *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 ())))
1230 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open filter file\n%s\nfor reading: %s."
, path, g_strerror((*__errno_location ())))
;
1231 return false0;
1232 }
1233
1234 /*
1235 * The file being imported might be in the legacy @-delimited
1236 * format or the newer JSONC format. Try the legacy format first,
1237 * then the JSONC format. Each parser simply finds no filters if
1238 * the file isn't in the expected format.
1239 */
1240 ret = read_legacy_filters_file(path, f, user_data, add_cb, false0);
2
Calling 'read_legacy_filters_file'
1241 if (ret != 0) {
1242 *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 ())))
1243 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Error reading filter file\n\"%s\": %s."
, path, g_strerror((*__errno_location ())))
;
1244 fclose(f);
1245 return false0;
1246 }
1247
1248 /* read_filters_file() seeks to the start of the file. */
1249 ret = read_filters_file(path, f, user_data, add_cb, false0, false0);
1250 fclose(f);
1251 if (ret != 0) {
1252 *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 ())))
1253 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Error reading filter file\n\"%s\": %s."
, path, g_strerror((*__errno_location ())))
;
1254 return false0;
1255 }
1256
1257 return true1;
1258}
1259
1260struct write_filter_data
1261{
1262 FILE *f;
1263 bool_Bool only_selected;
1264};
1265
1266/* save a single filter, in the legacy @-delimited format */
1267static void
1268write_legacy_filter(void *filter_arg, void *data_arg)
1269{
1270 struct write_filter_data *data = (struct write_filter_data *)data_arg;
1271 color_filter_t *colorf = (color_filter_t *)filter_arg;
1272 FILE *f = data->f;
1273
1274 if ( (!data->only_selected) &&
1275 (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX"___conversation_color_filter___")==NULL((void*)0)) ) {
1276 fprintf(f,"%s@%s@%s@[%u,%u,%u][%u,%u,%u]\n",
1277 colorf->enabled ? "" : "!",
1278 colorf->filter_name,
1279 colorf->filter_text,
1280 colorf->bg_color.red,
1281 colorf->bg_color.green,
1282 colorf->bg_color.blue,
1283 colorf->fg_color.red,
1284 colorf->fg_color.green,
1285 colorf->fg_color.blue);
1286 }
1287}
1288
1289/* save filters in a filter file, in the legacy @-delimited format */
1290static bool_Bool
1291write_legacy_filters_file(GSList *cfl, FILE *f, bool_Bool only_selected, const char* app_name)
1292{
1293 struct write_filter_data data;
1294
1295 data.f = f;
1296 data.only_selected = only_selected;
1297
1298 fprintf(f,"# This file was created by %s. Edit with care.\n", app_name);
1299 g_slist_foreach(cfl, write_legacy_filter, &data);
1300 return true1;
1301}
1302
1303struct write_filter_jsonc_data
1304{
1305 json_dumper *dumper;
1306 bool_Bool only_selected;
1307};
1308
1309/* save a single filter, in JSONC format */
1310static void
1311write_jsonc_filter(void *filter_arg, void *data_arg)
1312{
1313 struct write_filter_jsonc_data *data = (struct write_filter_jsonc_data *)data_arg;
1314 color_filter_t *colorf = (color_filter_t *)filter_arg;
1315 json_dumper *dumper = data->dumper;
1316 char *fg_str, *bg_str;
1317
1318 if ( (!data->only_selected) &&
1319 (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX"___conversation_color_filter___")==NULL((void*)0)) ) {
1320 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))
;
1321 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))
;
1322
1323 json_dumper_begin_object(dumper);
1324 json_dumper_set_member_name(dumper, colorf->filter_name);
1325 json_dumper_begin_object(dumper);
1326 json_dumper_set_member_name_const(dumper, "filter")json_dumper_set_member_name_noesc(dumper, "filter", sizeof("filter"
) - 1)
;
1327 json_dumper_value_string(dumper, colorf->filter_text);
1328 json_dumper_set_member_name_const(dumper, "foreground")json_dumper_set_member_name_noesc(dumper, "foreground", sizeof
("foreground") - 1)
;
1329 json_dumper_value_string(dumper, fg_str);
1330 json_dumper_set_member_name_const(dumper, "background")json_dumper_set_member_name_noesc(dumper, "background", sizeof
("background") - 1)
;
1331 json_dumper_value_string(dumper, bg_str);
1332 json_dumper_set_member_name_const(dumper, "enabled")json_dumper_set_member_name_noesc(dumper, "enabled", sizeof("enabled"
) - 1)
;
1333 json_dumper_value_anyf(dumper, "%s", colorf->enabled ? "true" : "false");
1334 json_dumper_end_object(dumper);
1335 json_dumper_end_object(dumper);
1336
1337 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
)
;
1338 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
)
;
1339 }
1340}
1341
1342/* save filters in a coloring rules file, in JSONC format */
1343static bool_Bool
1344write_filters_file(GSList *cfl, FILE *f, bool_Bool only_selected, const char* app_name)
1345{
1346 struct write_filter_jsonc_data data;
1347 json_dumper dumper = {
1348 .output_file = f,
1349 .flags = JSON_DUMPER_FLAGS_PRETTY_PRINT(1 << 0),
1350 };
1351
1352 data.dumper = &dumper;
1353 data.only_selected = only_selected;
1354
1355 fprintf(f, "// This file was created by %s. Edit with care.\n", app_name);
1356
1357 json_dumper_begin_array(&dumper);
1358 g_slist_foreach(cfl, write_jsonc_filter, &data);
1359 json_dumper_end_array(&dumper);
1360
1361 return json_dumper_finish(&dumper);
1362}
1363
1364/* save filters in users filter file(s) */
1365bool_Bool
1366color_filters_write(GSList *cfl_all, const char* app_name, const char* app_env_var_prefix, char** err_msg)
1367{
1368 char *pf_dir_path;
1369 char *path;
1370 FILE *f;
1371 GSList *cfl = NULL((void*)0);
1372
1373 /* Rules inherited from parent profiles are not written to this profile */
1374 for (GSList *item = cfl_all; item; item = g_slist_next(item)((item) ? (((GSList *)(item))->next) : ((void*)0))) {
1375 color_filter_t *colorf = (color_filter_t *)item->data;
1376 if (colorf->source_profile == NULL((void*)0))
1377 cfl = g_slist_prepend(cfl, colorf);
1378 }
1379 cfl = g_slist_reverse(cfl);
1380
1381 /* Create the directory that holds personal configuration files,
1382 if necessary. */
1383 if (create_persconffile_dir(app_env_var_prefix, &pf_dir_path) == -1) {
1384 *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 ())))
1385 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 ())))
;
1386 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)
;
1387 g_slist_free(cfl);
1388 return false0;
1389 }
1390
1391 /*
1392 * Keep an existing legacy colorfilters file current for older versions,
1393 * which read only that file. Never create it.
1394 */
1395 path = get_persconffile_path(COLORFILTERS_FILE_NAME"colorfilters", true1, app_env_var_prefix);
1396 if (g_file_test(path, G_FILE_TEST_EXISTS)) {
1397 if ((f = ws_fopenfopen(path, "w+")) == NULL((void*)0)) {
1398 *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 ())))
1399 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s."
, path, g_strerror((*__errno_location ())))
;
1400 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1401 g_slist_free(cfl);
1402 return false0;
1403 }
1404 write_legacy_filters_file(cfl, f, false0, app_name);
1405 fclose(f);
1406 }
1407 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1408
1409 path = get_persconffile_path(COLORING_RULES_FILE_NAME"coloring_rules.jsonc", true1, app_env_var_prefix);
1410 if ((f = ws_fopenfopen(path, "w+")) == NULL((void*)0)) {
1411 *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 ())))
1412 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s."
, path, g_strerror((*__errno_location ())))
;
1413 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1414 g_slist_free(cfl);
1415 return false0;
1416 }
1417 write_filters_file(cfl, f, false0, app_name);
1418 fclose(f);
1419 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1420 g_slist_free(cfl);
1421 return true1;
1422}
1423
1424/* Save filters in the specified file (export) */
1425bool_Bool
1426color_filters_export(const char *path, GSList *cfl, bool_Bool only_marked, const char* app_name, char** err_msg)
1427{
1428 FILE *f;
1429
1430 if ((f = ws_fopenfopen(path, "w+")) == NULL((void*)0)) {
1431 *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 ())))
1432 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s."
, path, g_strerror((*__errno_location ())))
;
1433 return false0;
1434 }
1435 write_filters_file(cfl, f, only_marked, app_name);
1436 fclose(f);
1437 return true1;
1438}
1439
1440/*
1441 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1442 *
1443 * Local variables:
1444 * c-basic-offset: 4
1445 * tab-width: 8
1446 * indent-tabs-mode: nil
1447 * End:
1448 *
1449 * vi: set shiftwidth=4 tabstop=8 expandtab:
1450 * :indentSize=4:tabSize=8:noTabs=true:
1451 */