Bug Summary

File:builds/wireshark/wireshark/epan/color_filters.c
Warning:line 1122, column 13
Read function called when stream is in EOF state. Function has no effect

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-30-100354-3660-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 bool_Bool ok = color_filters_get_own(err_msg, add_cb, app_env_var_prefix, !include_parent);
532 if (!include_parent) {
533 return ok;
534 }
535
536 /* The rules of the parent profiles are used also if the own file can't
537 * be read, whose error is the one reported */
538 char *inherit_err = NULL((void*)0);
539 bool_Bool inherit_ok = color_filters_add_inherited(color_filter_list, NULL((void*)0), ok ? err_msg : &inherit_err,
540 color_filters_add_inherited_cb, app_env_var_prefix);
541 g_free(inherit_err)(__builtin_object_size ((inherit_err), 0) != ((size_t) - 1)) ?
g_free_sized (inherit_err, __builtin_object_size ((inherit_err
), 0)) : (g_free) (inherit_err)
;
542 return ok && inherit_ok;
543}
544
545/* Initialize the filter structures (reading from file) for general running, including app startup */
546bool_Bool
547color_filters_init(char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix)
548{
549 /* delete all currently existing filters */
550 color_filter_list_delete(&color_filter_list);
551
552 /* now try to construct the filters list */
553 return color_filters_get(err_msg, add_cb, app_env_var_prefix);
554}
555
556bool_Bool
557color_filters_reload(char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix)
558{
559 /* "move" old entries to the deleted list
560 * we must keep them until the dissection no longer needs them */
561 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
562 color_filter_list = NULL((void*)0);
563
564 /* now try to construct the filters list */
565 return color_filters_get(err_msg, add_cb, app_env_var_prefix);
566}
567
568void
569color_filters_cleanup(void)
570{
571 /* delete the previously deleted filters */
572 color_filter_list_delete(&color_filter_deleted_list);
573}
574
575typedef struct _color_clone
576{
577 void *user_data;
578 color_filter_add_cb_func add_cb;
579} color_clone_t;
580
581static void
582color_filters_clone_cb(void *filter_arg, void *user_data)
583{
584 color_clone_t* clone_data = (color_clone_t*)user_data;
585 color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg);
586
587 clone_data->add_cb (new_colorf, clone_data->user_data);
588}
589
590void
591color_filters_clone(void *user_data, color_filter_add_cb_func add_cb)
592{
593 color_clone_t clone_data;
594
595 clone_data.user_data = user_data;
596 clone_data.add_cb = add_cb;
597 g_slist_foreach(color_filter_list, color_filters_clone_cb, &clone_data);
598}
599
600/* A rule takes part in matching if it compiled and is enabled. A disabled
601 * rule takes part only when asked to, and never a disabled conversation
602 * rule: unused conversation slots are compiled as "frame" and would match
603 * every packet. */
604static bool_Bool
605color_filter_in_play(const color_filter_t *colorf, bool_Bool include_disabled)
606{
607 if (colorf->c_colorfilter == NULL((void*)0))
608 return false0;
609 if (colorf->enabled)
610 return true1;
611 return include_disabled && !color_filter_is_tmp(colorf);
612}
613
614/* Compiles a filter without reporting errors. Disabled rules are compiled
615 * this way so that they can be matched while disabled rules are shown; one
616 * that does not compile simply never matches. */
617static dfilter_t *
618color_filter_compile_quiet(const char *text)
619{
620 dfilter_t *df = NULL((void*)0);
621 df_error_t *df_err = NULL((void*)0);
622
623 if (!dfilter_compile(text, &df, &df_err)dfilter_compile_full(text, &df, &df_err, (1U <<
1)|(1U << 2), __func__)
) {
624 df_error_free(&df_err);
625 }
626 return df;
627}
628
629/* Compiles the filter of a rule read from a file. An enabled rule that does
630 * not compile is reported and disabled. A disabled rule is compiled quietly
631 * when compile_disabled is set (rules read into the list used for matching)
632 * and not at all otherwise (rules read for editing). */
633static dfilter_t *
634color_filter_compile_read(const char *name, const char *text, bool_Bool *enabled,
635 bool_Bool compile_disabled, const char *path,
636 const char *file_kind)
637{
638 dfilter_t *df = NULL((void*)0);
639 df_error_t *df_err = NULL((void*)0);
640
641 if (!*enabled) {
642 return compile_disabled ? color_filter_compile_quiet(text) : NULL((void*)0);
643 }
644 if (!dfilter_compile(text, &df, &df_err)dfilter_compile_full(text, &df, &df_err, (1U <<
1)|(1U << 2), __func__)
) {
645 report_warning("Disabling color filter: Could not compile \"%s\" in %s \"%s\".\n%s",
646 name, file_kind, path, df_err->msg);
647 df_error_free(&df_err);
648 *enabled = false0;
649 }
650 return df;
651}
652
653static void
654color_filter_compile_cb(void *filter_arg, void *err)
655{
656 color_filter_t *colorf = (color_filter_t *)filter_arg;
657 char **err_msg = (char**)err;
658 df_error_t *df_err = NULL((void*)0);
659
660 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"
, 660, __func__, "assertion failed: %s", "colorf->c_colorfilter == ((void*)0)"
); } while (0)
;
661
662 /* Disabled conversation rules are never used. */
663 if (!colorf->enabled) {
664 if (!color_filter_is_tmp(colorf)) {
665 colorf->c_colorfilter = color_filter_compile_quiet(colorf->filter_text);
666 }
667 return;
668 }
669
670 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__
)
) {
671 *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)
672 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)
;
673 df_error_free(&df_err);
674 /* this filter was compilable before, so this should never happen */
675 /* except if the OK button of the parent window has been clicked */
676 /* so don't use ws_assert_not_reached() but check the filters again */
677 }
678}
679
680static void
681color_filter_validate_cb(void *filter_arg, void *err)
682{
683 color_filter_t *colorf = (color_filter_t *)filter_arg;
684 char **err_msg = (char**)err;
685 df_error_t *df_err = NULL((void*)0);
686
687 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"
, 687, __func__, "assertion failed: %s", "colorf->c_colorfilter == ((void*)0)"
); } while (0)
;
688
689 /* If the filter is disabled it doesn't matter if it compiles or not. */
690 if (!colorf->enabled) return;
691
692 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__
)
) {
693 *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)
694 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)
;
695 df_error_free(&df_err);
696
697 /* Disable the color filter in the list of color filters. */
698 colorf->enabled = false0;
699 }
700
701 /* XXX: What if the color filter tests "frame.coloring_rule.name" or
702 * "frame.coloring_rule.string"?
703 */
704}
705
706/* apply changes from the edit list */
707bool_Bool
708color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl, char** err_msg)
709{
710 bool_Bool ret = true1;
711
712 *err_msg = NULL((void*)0);
713
714 /* "move" old entries to the deleted list
715 * we must keep them until the dissection no longer needs them */
716 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
717 color_filter_list = NULL((void*)0);
718
719 /* clone all list entries from tmp/edit to normal list */
720 color_filter_list_delete(&color_filter_valid_list);
721 color_filter_valid_list = color_filter_list_clone(tmp_cfl);
722 color_filter_valid_list = g_slist_concat(color_filter_valid_list,
723 color_filter_list_clone(edit_cfl) );
724
725 /* compile all filter */
726 g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, err_msg);
727 if (*err_msg != NULL((void*)0)) {
728 ret = false0;
729 }
730
731 /* clone all list entries from tmp/edit to normal list */
732 color_filter_list = color_filter_list_clone(color_filter_valid_list);
733
734 /* compile all filter */
735 g_slist_foreach(color_filter_list, color_filter_compile_cb, err_msg);
736 if (*err_msg != NULL((void*)0)) {
737 ret = false0;
738 }
739
740 return ret;
741}
742
743bool_Bool
744color_filters_used(void)
745{
746 return color_filter_list != NULL((void*)0) && filters_enabled;
747}
748
749bool_Bool
750tmp_color_filters_used(void)
751{
752 return tmp_colors_set;
753}
754
755/* prepare the epan_dissect_t for the filter */
756static void
757prime_edt(void *data, void *user_data)
758{
759 color_filter_t *colorf = (color_filter_t *)data;
760 epan_dissect_t *edt = (epan_dissect_t *)user_data;
761
762 if (color_filter_in_play(colorf, prefs.gui_coloring_rules_include_disabled))
763 epan_dissect_prime_with_dfilter(edt, colorf->c_colorfilter);
764}
765
766/* Prime the epan_dissect_t with all the compiler
767 * color filters in 'color_filter_list'. */
768void
769color_filters_prime_edt(epan_dissect_t *edt)
770{
771 if (color_filters_used())
772 g_slist_foreach(color_filter_list, prime_edt, edt);
773}
774
775static int
776find_hfid(const void *data, const void *user_data)
777{
778 color_filter_t *colorf = (color_filter_t *)data;
779 int hfid = GPOINTER_TO_INT(user_data)((gint) (glong) (user_data));
780
781 if (color_filter_in_play(colorf, prefs.gui_coloring_rules_include_disabled)) {
782 if (dfilter_interested_in_field(colorf->c_colorfilter, hfid)) {
783 return 0;
784 }
785 }
786 return -1;
787}
788
789bool_Bool
790color_filters_use_hfid(int hfid)
791{
792 GSList *item = NULL((void*)0);
793 if (color_filters_used())
794 item = g_slist_find_custom(color_filter_list, GINT_TO_POINTER(hfid)((gpointer) (glong) (hfid)), find_hfid);
795 return (item != NULL((void*)0));
796}
797
798static int
799find_proto(const void *data, const void *user_data)
800{
801 color_filter_t *colorf = (color_filter_t *)data;
802 int proto_id = GPOINTER_TO_INT(user_data)((gint) (glong) (user_data));
803
804 if (color_filter_in_play(colorf, prefs.gui_coloring_rules_include_disabled)) {
805 if (dfilter_interested_in_proto(colorf->c_colorfilter, proto_id)) {
806 return 0;
807 }
808 }
809 return -1;
810}
811
812bool_Bool
813color_filters_use_proto(int proto_id)
814{
815 GSList *item = NULL((void*)0);
816 if (color_filters_used())
817 item = g_slist_find_custom(color_filter_list, GINT_TO_POINTER(proto_id)((gpointer) (glong) (proto_id)), find_proto);
818 return (item != NULL((void*)0));
819}
820
821const color_filter_t *
822color_filters_colorize_packet_all(epan_dissect_t *edt,
823 wmem_allocator_t *scope, wmem_list_t **matches, bool_Bool include_disabled)
824{
825 const color_filter_t *first_match = NULL((void*)0);
826
827 if (matches) {
828 *matches = NULL((void*)0);
829 }
830
831 /* If we have color filters, collect ALL matching ones. */
832 if ((edt->tree != NULL((void*)0)) && (color_filters_used())) {
833 for (GSList *curr = color_filter_list; curr != NULL((void*)0); curr = g_slist_next(curr)((curr) ? (((GSList *)(curr))->next) : ((void*)0))) {
834 color_filter_t *colorf = (color_filter_t *)curr->data;
835 if (color_filter_in_play(colorf, include_disabled) &&
836 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
837
838 if (matches) {
839 if (*matches == NULL((void*)0)) {
840 *matches = wmem_list_new(scope);
841 }
842 wmem_list_append(*matches, colorf);
843 }
844
845 /* A disabled rule never colors the packet */
846 if (!first_match && colorf->enabled) {
847 first_match = colorf; /* Backward compatibility */
848 }
849 }
850 }
851 }
852
853 return first_match;
854}
855
856/* Parse a "#rrggbb" or "rrggbb" string into a color_t. Returns false (and
857 * leaves *color untouched) if str is NULL or not a valid hex triplet. */
858static bool_Bool
859hex_str_to_color(const char *str, color_t *color)
860{
861 unsigned long cval;
862
863 if (str == NULL((void*)0)) {
864 return false0;
865 }
866 if (str[0] == '#') {
867 str++;
868 }
869 if (strlen(str) != 6 || strspn(str, "0123456789abcdefABCDEF") != 6) {
870 return false0;
871 }
872
873 cval = strtoul(str, NULL((void*)0), 16);
874 color->red = RED_COMPONENT(cval)(uint16_t) (((((cval) >> 16) & 0xff) * 65535 / 255)
)
;
875 color->green = GREEN_COMPONENT(cval)(uint16_t) (((((cval) >> 8) & 0xff) * 65535 / 255));
876 color->blue = BLUE_COMPONENT(cval)(uint16_t) ( (((cval) & 0xff) * 65535 / 255));
877 return true1;
878}
879
880static int
881read_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)
882{
883 long size;
884 char *buf;
885 size_t nread;
886 int num_tokens;
887 jsmntok_t *tokens;
888 int array_len;
889 jsmntok_t *element;
890
891 if (fseek(f, 0, SEEK_END2) == -1) {
892 return errno(*__errno_location ());
893 }
894 size = ftell(f);
895 if (size < 0) {
896 return errno(*__errno_location ());
897 }
898 if (fseek(f, 0, SEEK_SET0) == -1) {
899 return errno(*__errno_location ());
900 }
901 if (size == 0) {
902 return 0;
903 }
904
905 buf = (char *)g_malloc(size + 1);
906 nread = fread(buf, 1, size, f);
907 if (ferror(f)) {
908 int ret = errno(*__errno_location ());
909 g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized
(buf, __builtin_object_size ((buf), 0)) : (g_free) (buf)
;
910 return ret;
911 }
912 buf[nread] = '\0';
913
914 if (!json_strip_jsonc_comments(buf)) {
915 report_warning("Error parsing coloring rules file\n\"%s\": unterminated block comment.", path);
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 0;
918 }
919
920 num_tokens = json_parse(buf, NULL((void*)0), 0);
921 if (num_tokens == 0) {
922 /* Only comments and whitespace: a valid, empty rule list. */
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 if (num_tokens < 0) {
927 /*
928 * Not JSON at all. For the personal and global rule files that is
929 * an error the user must see, since none of the file's rules are
930 * used. An imported file may be in the legacy format, which never
931 * parses as JSON, so it just yields no rules here.
932 */
933 if (require_json) {
934 report_warning("Error parsing coloring rules file\n\"%s\": invalid JSON.", path);
935 }
936 g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized
(buf, __builtin_object_size ((buf), 0)) : (g_free) (buf)
;
937 return 0;
938 }
939
940 tokens = g_new(jsmntok_t, num_tokens)((jsmntok_t *) g_malloc_n ((num_tokens), sizeof (jsmntok_t)));
941 if (json_parse(buf, tokens, num_tokens) <= 0 || tokens[0].type != JSMN_ARRAY) {
942 report_warning("Error parsing coloring rules file\n\"%s\": invalid JSON.", path);
943 g_free(tokens)(__builtin_object_size ((tokens), 0) != ((size_t) - 1)) ? g_free_sized
(tokens, __builtin_object_size ((tokens), 0)) : (g_free) (tokens
)
;
944 g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized
(buf, __builtin_object_size ((buf), 0)) : (g_free) (buf)
;
945 return 0;
946 }
947
948 array_len = json_get_array_len(&tokens[0]);
949 element = json_get_array_index(&tokens[0], 0);
950
951 for (int i = 0; i < array_len; i++, element = json_get_next_object(element)) {
952 char *name, *filter_str, *fg_str, *bg_str;
953 bool_Bool enabled = true1;
954 color_t fg_color = { 0, 0, 0 }, bg_color = { 0, 0, 0 };
955 dfilter_t *temp_dfilter = NULL((void*)0);
956 const char *filter_exp;
957 color_filter_t *colorf;
958 jsmntok_t *name_tok, *properties_tok;
959
960 if (element->type != JSMN_OBJECT || element->size != 1) {
961 continue;
962 }
963
964 /* "<filter name>": { ... } */
965 name_tok = element + 1;
966 properties_tok = name_tok + 1;
967 if (name_tok->type != JSMN_STRING || properties_tok->type != JSMN_OBJECT) {
968 continue;
969 }
970
971 name = g_strndup(&buf[name_tok->start], (gsize)(name_tok->end - name_tok->start));
972 if (!json_decode_string_inplace(name)) {
973 g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized
(name, __builtin_object_size ((name), 0)) : (g_free) (name)
;
974 continue;
975 }
976
977 filter_str = json_get_string(buf, properties_tok, "filter");
978 fg_str = json_get_string(buf, properties_tok, "foreground");
979 bg_str = json_get_string(buf, properties_tok, "background");
980 json_get_boolean(buf, properties_tok, "enabled", &enabled);
981
982 hex_str_to_color(fg_str, &fg_color);
983 hex_str_to_color(bg_str, &bg_color);
984
985 filter_exp = filter_str ? filter_str : "";
986
987 temp_dfilter = color_filter_compile_read(name, filter_exp, &enabled,
988 user_data == &color_filter_list,
989 path, "coloring rules file");
990
991 colorf = color_filter_new(name, filter_exp, &bg_color, &fg_color, enabled);
992 g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized
(name, __builtin_object_size ((name), 0)) : (g_free) (name)
;
993
994 if (user_data == &color_filter_list) {
995 GSList **cfl = (GSList **)user_data;
996
997 /* internal call */
998 colorf->c_colorfilter = temp_dfilter;
999 *cfl = g_slist_append(*cfl, colorf);
1000 } else {
1001 /* external call */
1002 /* just editing, don't need the compiled filter, unless
1003 * the caller adds the rule to the list used for matching */
1004 if (keep_compiled) {
1005 colorf->c_colorfilter = temp_dfilter;
1006 } else {
1007 dfilter_free(temp_dfilter);
1008 }
1009 add_cb(colorf, user_data);
1010 }
1011 }
1012
1013 g_free(tokens)(__builtin_object_size ((tokens), 0) != ((size_t) - 1)) ? g_free_sized
(tokens, __builtin_object_size ((tokens), 0)) : (g_free) (tokens
)
;
1014 g_free(buf)(__builtin_object_size ((buf), 0) != ((size_t) - 1)) ? g_free_sized
(buf, __builtin_object_size ((buf), 0)) : (g_free) (buf)
;
1015 return 0;
1016}
1017
1018/* Read legacy (@-delimited) filters from the given file */
1019/* XXX At what point do we drop support? */
1020/* XXX Would it make more sense to use GStrings here instead of reallocing
1021 our buffers? */
1022static int
1023read_legacy_filters_file(const char *path, FILE *f, void *user_data, color_filter_add_cb_func add_cb, bool_Bool keep_compiled)
1024{
1025#define INIT_BUF_SIZE128 128
1026 char *name;
1027 char *filter_exp;
1028 uint32_t name_len = INIT_BUF_SIZE128;
1029 uint32_t filter_exp_len = INIT_BUF_SIZE128;
1030 uint32_t i = 0;
1031 int c;
1032 uint16_t fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
1033 bool_Bool disabled = false0;
1034 bool_Bool skip_end_of_line = false0;
1035 int ret = 0;
1036
1037 name = (char *)g_malloc(name_len + 1);
1038 filter_exp = (char *)g_malloc(filter_exp_len + 1);
1039
1040 while (1) {
3
Loop condition is true. Entering loop body
37
Loop condition is true. Entering loop body
1041
1042 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
1043 do {
41
Loop condition is false. Exiting loop
1044 c = ws_getc_unlockedgetc_unlocked(f);
1045 } while (c != EOF(-1) && c != '\n');
39
Assuming the condition is true
40
Assuming the condition is false
1046 if (c == EOF(-1))
42
Taking false branch
1047 break;
1048 disabled = false0;
1049 skip_end_of_line = false0;
1050 }
1051
1052 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 1058
43
Assuming the condition is true
44
Loop condition is false. Execution continues on line 1058
1053 if (c == '\n') {
1054 continue;
1055 }
1056 }
1057
1058 if (c == EOF(-1))
6
Taking false branch
45
Taking false branch
1059 break;
1060
1061 if (c == '!') {
7
Assuming the condition is false
8
Taking false branch
46
Assuming the condition is false
47
Taking false branch
1062 disabled = true1;
1063 continue;
1064 }
1065
1066 /* skip # comments and invalid lines */
1067 if (c != '@') {
9
Assuming the condition is false
10
Taking false branch
48
Assuming the condition is false
49
Taking false branch
1068 skip_end_of_line = true1;
1069 continue;
1070 }
1071
1072 /* we get the @ delimiter.
1073 * Format is:
1074 * @name@filter expression@[background r,g,b][foreground r,g,b]
1075 */
1076
1077 /* retrieve name */
1078 i = 0;
1079 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
1080 c = ws_getc_unlockedgetc_unlocked(f);
1081 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
1082 break;
20
Execution continues on line 1090
59
Execution continues on line 1090
1083 if (i
14.1
'i' is < 'name_len'
53.1
'i' is < 'name_len'
>= name_len) {
15
Taking false branch
54
Taking false branch
1084 /* buffer isn't long enough; double its length.*/
1085 name_len *= 2;
1086 name = (char *)g_realloc(name, name_len + 1);
1087 }
1088 name[i++] = c;
1089 }
1090 name[i] = '\0';
1091
1092 if (c == EOF(-1)) {
21
Taking false branch
60
Taking false branch
1093 break;
1094 } 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
1095 skip_end_of_line = true1;
1096 continue;
1097 }
1098
1099 /* retrieve filter expression */
1100 i = 0;
1101 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
1102 c = ws_getc_unlockedgetc_unlocked(f);
1103 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
1104 break;
32
Execution continues on line 1112
71
Execution continues on line 1112
1105 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
1106 /* buffer isn't long enough; double its length.*/
1107 filter_exp_len *= 2;
1108 filter_exp = (char *)g_realloc(filter_exp, filter_exp_len + 1);
1109 }
1110 filter_exp[i++] = c;
1111 }
1112 filter_exp[i] = '\0';
1113
1114 if (c == EOF(-1)) {
33
Taking false branch
72
Taking false branch
1115 break;
1116 } 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
1117 skip_end_of_line = true1;
1118 continue;
1119 }
1120
1121 /* retrieve background and foreground colors */
1122 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
35
Assuming stream reaches end-of-file here
36
Taking false branch
74
Read function called when stream is in EOF state. Function has no effect
1123 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
1124
1125 /* we got a complete color filter */
1126
1127 color_t bg_color, fg_color;
1128 color_filter_t *colorf;
1129 dfilter_t *temp_dfilter = NULL((void*)0);
1130 bool_Bool enabled = !disabled;
1131
1132 temp_dfilter = color_filter_compile_read(name, filter_exp, &enabled,
1133 user_data == &color_filter_list,
1134 path, "colorfilters file");
1135
1136 fg_color.red = fg_r;
1137 fg_color.green = fg_g;
1138 fg_color.blue = fg_b;
1139
1140 bg_color.red = bg_r;
1141 bg_color.green = bg_g;
1142 bg_color.blue = bg_b;
1143
1144 colorf = color_filter_new(name, filter_exp, &bg_color,
1145 &fg_color, enabled);
1146 if(user_data == &color_filter_list) {
1147 GSList **cfl = (GSList **)user_data;
1148
1149 /* internal call */
1150 colorf->c_colorfilter = temp_dfilter;
1151 *cfl = g_slist_append(*cfl, colorf);
1152 } else {
1153 /* external call */
1154 /* just editing, don't need the compiled filter, unless
1155 * the caller adds the rule to the list used for matching */
1156 if (keep_compiled) {
1157 colorf->c_colorfilter = temp_dfilter;
1158 } else {
1159 dfilter_free(temp_dfilter);
1160 }
1161 add_cb(colorf, user_data);
1162 }
1163 } /* if sscanf */
1164
1165 skip_end_of_line = true1;
1166 }
1167
1168 if (ferror(f))
1169 ret = errno(*__errno_location ());
1170
1171 g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized
(name, __builtin_object_size ((name), 0)) : (g_free) (name)
;
1172 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)
;
1173 return ret;
1174}
1175
1176/* read filters from the filter file */
1177bool_Bool
1178color_filters_read_globals(void *user_data, char** err_msg, color_filter_add_cb_func add_cb, const char* app_env_var_prefix)
1179{
1180 char *path;
1181 FILE *f;
1182 int ret;
1183
1184 /*
1185 * Try to get the global filters.
1186 *
1187 * Get the path for the file that would have the global filters, and
1188 * try to open it.
1189 */
1190 path = get_datafile_path(COLORING_RULES_FILE_NAME"coloring_rules.jsonc", app_env_var_prefix);
1191 if ((f = ws_fopenfopen(path, "r")) == NULL((void*)0)) {
1192 if (errno(*__errno_location ()) != ENOENT2) {
1193 /* Error trying to open the file; give up. */
1194 *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 ())))
1195 g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open global coloring rules file\n\"%s\": %s."
, path, g_strerror((*__errno_location ())))
;
1196 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1197 return false0;
1198 }
1199
1200 /*
1201 * There is no global rules file; treat that as equivalent to
1202 * that file existing bug being empty, and say we succeeded.
1203 */
1204 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1205 return true1;
1206 }
1207
1208 /* Don't try to read the legacy colorfilters file, unlike the personal config. */
1209
1210 ret = read_filters_file(path, f, user_data, add_cb, true1, false0);
1211 fclose(f);
1212 if (ret != 0) {
1213 *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 ())))
1214 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Error reading global coloring rules file\n\"%s\": %s."
, path, g_strerror((*__errno_location ())))
;
1215 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1216 return false0;
1217 }
1218
1219 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1220 return true1;
1221}
1222
1223/* read filters from some other filter file (import) */
1224bool_Bool
1225color_filters_import(const char *path, void *user_data, char **err_msg, color_filter_add_cb_func add_cb)
1226{
1227 FILE *f;
1228 int ret;
1229
1230 if ((f = ws_fopenfopen(path, "r")) == NULL((void*)0)) {
1
Taking false branch
1231 *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 ())))
1232 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open filter file\n%s\nfor reading: %s."
, path, g_strerror((*__errno_location ())))
;
1233 return false0;
1234 }
1235
1236 /*
1237 * The file being imported might be in the legacy @-delimited
1238 * format or the newer JSONC format. Try the legacy format first,
1239 * then the JSONC format. Each parser simply finds no filters if
1240 * the file isn't in the expected format.
1241 */
1242 ret = read_legacy_filters_file(path, f, user_data, add_cb, false0);
2
Calling 'read_legacy_filters_file'
1243 if (ret != 0) {
1244 *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 ())))
1245 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Error reading filter file\n\"%s\": %s."
, path, g_strerror((*__errno_location ())))
;
1246 fclose(f);
1247 return false0;
1248 }
1249
1250 /* read_filters_file() seeks to the start of the file. */
1251 ret = read_filters_file(path, f, user_data, add_cb, false0, false0);
1252 fclose(f);
1253 if (ret != 0) {
1254 *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 ())))
1255 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Error reading filter file\n\"%s\": %s."
, path, g_strerror((*__errno_location ())))
;
1256 return false0;
1257 }
1258
1259 return true1;
1260}
1261
1262struct write_filter_data
1263{
1264 FILE *f;
1265 bool_Bool only_selected;
1266};
1267
1268/* save a single filter, in the legacy @-delimited format */
1269static void
1270write_legacy_filter(void *filter_arg, void *data_arg)
1271{
1272 struct write_filter_data *data = (struct write_filter_data *)data_arg;
1273 color_filter_t *colorf = (color_filter_t *)filter_arg;
1274 FILE *f = data->f;
1275
1276 if ( (!data->only_selected) &&
1277 (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX"___conversation_color_filter___")==NULL((void*)0)) ) {
1278 fprintf(f,"%s@%s@%s@[%u,%u,%u][%u,%u,%u]\n",
1279 colorf->enabled ? "" : "!",
1280 colorf->filter_name,
1281 colorf->filter_text,
1282 colorf->bg_color.red,
1283 colorf->bg_color.green,
1284 colorf->bg_color.blue,
1285 colorf->fg_color.red,
1286 colorf->fg_color.green,
1287 colorf->fg_color.blue);
1288 }
1289}
1290
1291/* save filters in a filter file, in the legacy @-delimited format */
1292static bool_Bool
1293write_legacy_filters_file(GSList *cfl, FILE *f, bool_Bool only_selected, const char* app_name)
1294{
1295 struct write_filter_data data;
1296
1297 data.f = f;
1298 data.only_selected = only_selected;
1299
1300 fprintf(f,"# This file was created by %s. Edit with care.\n", app_name);
1301 g_slist_foreach(cfl, write_legacy_filter, &data);
1302 return true1;
1303}
1304
1305struct write_filter_jsonc_data
1306{
1307 json_dumper *dumper;
1308 bool_Bool only_selected;
1309};
1310
1311/* save a single filter, in JSONC format */
1312static void
1313write_jsonc_filter(void *filter_arg, void *data_arg)
1314{
1315 struct write_filter_jsonc_data *data = (struct write_filter_jsonc_data *)data_arg;
1316 color_filter_t *colorf = (color_filter_t *)filter_arg;
1317 json_dumper *dumper = data->dumper;
1318 char *fg_str, *bg_str;
1319
1320 if ( (!data->only_selected) &&
1321 (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX"___conversation_color_filter___")==NULL((void*)0)) ) {
1322 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))
;
1323 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))
;
1324
1325 json_dumper_begin_object(dumper);
1326 json_dumper_set_member_name(dumper, colorf->filter_name);
1327 json_dumper_begin_object(dumper);
1328 json_dumper_set_member_name_const(dumper, "filter")json_dumper_set_member_name_noesc(dumper, "filter", sizeof("filter"
) - 1)
;
1329 json_dumper_value_string(dumper, colorf->filter_text);
1330 json_dumper_set_member_name_const(dumper, "foreground")json_dumper_set_member_name_noesc(dumper, "foreground", sizeof
("foreground") - 1)
;
1331 json_dumper_value_string(dumper, fg_str);
1332 json_dumper_set_member_name_const(dumper, "background")json_dumper_set_member_name_noesc(dumper, "background", sizeof
("background") - 1)
;
1333 json_dumper_value_string(dumper, bg_str);
1334 json_dumper_set_member_name_const(dumper, "enabled")json_dumper_set_member_name_noesc(dumper, "enabled", sizeof("enabled"
) - 1)
;
1335 json_dumper_value_anyf(dumper, "%s", colorf->enabled ? "true" : "false");
1336 json_dumper_end_object(dumper);
1337 json_dumper_end_object(dumper);
1338
1339 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
)
;
1340 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
)
;
1341 }
1342}
1343
1344/* save filters in a coloring rules file, in JSONC format */
1345static bool_Bool
1346write_filters_file(GSList *cfl, FILE *f, bool_Bool only_selected, const char* app_name)
1347{
1348 struct write_filter_jsonc_data data;
1349 json_dumper dumper = {
1350 .output_file = f,
1351 .flags = JSON_DUMPER_FLAGS_PRETTY_PRINT(1 << 0),
1352 };
1353
1354 data.dumper = &dumper;
1355 data.only_selected = only_selected;
1356
1357 fprintf(f, "// This file was created by %s. Edit with care.\n", app_name);
1358
1359 json_dumper_begin_array(&dumper);
1360 g_slist_foreach(cfl, write_jsonc_filter, &data);
1361 json_dumper_end_array(&dumper);
1362
1363 return json_dumper_finish(&dumper);
1364}
1365
1366/* save filters in users filter file(s) */
1367bool_Bool
1368color_filters_write(GSList *cfl_all, const char* app_name, const char* app_env_var_prefix, char** err_msg)
1369{
1370 char *pf_dir_path;
1371 char *path;
1372 FILE *f;
1373 GSList *cfl = NULL((void*)0);
1374
1375 /* Rules inherited from parent profiles are not written to this profile */
1376 for (GSList *item = cfl_all; item; item = g_slist_next(item)((item) ? (((GSList *)(item))->next) : ((void*)0))) {
1377 color_filter_t *colorf = (color_filter_t *)item->data;
1378 if (colorf->source_profile == NULL((void*)0))
1379 cfl = g_slist_prepend(cfl, colorf);
1380 }
1381 cfl = g_slist_reverse(cfl);
1382
1383 /* Create the directory that holds personal configuration files,
1384 if necessary. */
1385 if (create_persconffile_dir(app_env_var_prefix, &pf_dir_path) == -1) {
1386 *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 ())))
1387 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 ())))
;
1388 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)
;
1389 g_slist_free(cfl);
1390 return false0;
1391 }
1392
1393 /*
1394 * Keep an existing legacy colorfilters file current for older versions,
1395 * which read only that file. Never create it.
1396 */
1397 path = get_persconffile_path(COLORFILTERS_FILE_NAME"colorfilters", true1, app_env_var_prefix);
1398 if (g_file_test(path, G_FILE_TEST_EXISTS)) {
1399 if ((f = ws_fopenfopen(path, "w+")) == NULL((void*)0)) {
1400 *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 ())))
1401 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s."
, path, g_strerror((*__errno_location ())))
;
1402 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1403 g_slist_free(cfl);
1404 return false0;
1405 }
1406 write_legacy_filters_file(cfl, f, false0, app_name);
1407 fclose(f);
1408 }
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
1411 path = get_persconffile_path(COLORING_RULES_FILE_NAME"coloring_rules.jsonc", true1, app_env_var_prefix);
1412 if ((f = ws_fopenfopen(path, "w+")) == NULL((void*)0)) {
1413 *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 ())))
1414 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s."
, path, g_strerror((*__errno_location ())))
;
1415 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1416 g_slist_free(cfl);
1417 return false0;
1418 }
1419 write_filters_file(cfl, f, false0, app_name);
1420 fclose(f);
1421 g_free(path)(__builtin_object_size ((path), 0) != ((size_t) - 1)) ? g_free_sized
(path, __builtin_object_size ((path), 0)) : (g_free) (path)
;
1422 g_slist_free(cfl);
1423 return true1;
1424}
1425
1426/* Save filters in the specified file (export) */
1427bool_Bool
1428color_filters_export(const char *path, GSList *cfl, bool_Bool only_marked, const char* app_name, char** err_msg)
1429{
1430 FILE *f;
1431
1432 if ((f = ws_fopenfopen(path, "w+")) == NULL((void*)0)) {
1433 *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 ())))
1434 path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open\n%s\nfor writing: %s."
, path, g_strerror((*__errno_location ())))
;
1435 return false0;
1436 }
1437 write_filters_file(cfl, f, only_marked, app_name);
1438 fclose(f);
1439 return true1;
1440}
1441
1442/*
1443 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1444 *
1445 * Local variables:
1446 * c-basic-offset: 4
1447 * tab-width: 8
1448 * indent-tabs-mode: nil
1449 * End:
1450 *
1451 * vi: set shiftwidth=4 tabstop=8 expandtab:
1452 * :indentSize=4:tabSize=8:noTabs=true:
1453 */