Wireshark 5.1.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
glib-compat.h
Go to the documentation of this file.
1
12#ifndef GLIB_COMPAT_H
13#define GLIB_COMPAT_H
14
15#include "ws_symbol_export.h"
16#include "ws_attributes.h"
17
18#include <glib.h>
19#include <string.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif /* __cplusplus */
24
25#if !GLIB_CHECK_VERSION(2, 57, 1)
26
36static inline gboolean
37g_hash_table_steal_extended (GHashTable *hash_table,
38 gconstpointer lookup_key,
39 gpointer *stolen_key,
40 gpointer *stolen_value)
41{
42 gpointer key, value;
43 if (g_hash_table_lookup_extended (hash_table, lookup_key, &key, &value))
44 {
45 stolen_key = &key;
46 stolen_value = &value;
47
48 g_hash_table_steal (hash_table, key);
49
50 return TRUE;
51 } else {
52 if (stolen_key != NULL)
53 *stolen_key = NULL;
54 if (stolen_value != NULL)
55 *stolen_value = NULL;
56 }
57
58 return FALSE;
59}
60
87static inline gchar *
88g_canonicalize_filename (const gchar *filename,
89 const gchar *relative_to)
90{
91 gchar *canon, *input, *output, *after_root, *output_start;
92
93 g_return_val_if_fail (relative_to == NULL || g_path_is_absolute (relative_to), NULL);
94
95 if (!g_path_is_absolute (filename))
96 {
97 gchar *cwd_allocated = NULL;
98 const gchar *cwd;
99
100 if (relative_to != NULL)
101 cwd = relative_to;
102 else
103 cwd = cwd_allocated = g_get_current_dir ();
104
105 canon = g_build_filename (cwd, filename, NULL);
106 g_free (cwd_allocated);
107 }
108 else
109 {
110 canon = g_strdup (filename);
111 }
112
113 after_root = (char *)g_path_skip_root (canon);
114
115 if (after_root == NULL)
116 {
117 /* This shouldn't really happen, as g_get_current_dir() should
118 return an absolute pathname, but bug 573843 shows this is
119 not always happening */
120 g_free (canon);
121 return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
122 }
123
124 /* Find the first dir separator and use the canonical dir separator. */
125 for (output = after_root - 1;
126 (output >= canon) && G_IS_DIR_SEPARATOR (*output);
127 output--)
128 *output = G_DIR_SEPARATOR;
129
130 /* 1 to re-increment after the final decrement above (so that output >= canon),
131 * and 1 to skip the first `/`. There might not be a first `/` if
132 * the @canon is a Windows `//server/share` style path with no
133 * trailing directories. @after_root will be '\0' in that case. */
134 output++;
135 if (*output == G_DIR_SEPARATOR)
136 output++;
137
138 /* POSIX allows double slashes at the start to mean something special
139 * (as does windows too). So, "//" != "/", but more than two slashes
140 * is treated as "/".
141 */
142 if (after_root - output == 1)
143 output++;
144
145 input = after_root;
146 output_start = output;
147 while (*input)
148 {
149 /* input points to the next non-separator to be processed. */
150 /* output points to the next location to write to. */
151 g_assert (input > canon && G_IS_DIR_SEPARATOR (input[-1]));
152 g_assert (output > canon && G_IS_DIR_SEPARATOR (output[-1]));
153 g_assert (input >= output);
154
155 /* Ignore repeated dir separators. */
156 while (G_IS_DIR_SEPARATOR (input[0]))
157 input++;
158
159 /* Ignore single dot directory components. */
160 if (input[0] == '.' && (input[1] == 0 || G_IS_DIR_SEPARATOR (input[1])))
161 {
162 if (input[1] == 0)
163 break;
164 input += 2;
165 }
166 /* Remove double-dot directory components along with the preceding
167 * path component. */
168 else if (input[0] == '.' && input[1] == '.' &&
169 (input[2] == 0 || G_IS_DIR_SEPARATOR (input[2])))
170 {
171 if (output > output_start)
172 {
173 do
174 {
175 output--;
176 }
177 while (!G_IS_DIR_SEPARATOR (output[-1]) && output > output_start);
178 }
179 if (input[2] == 0)
180 break;
181 input += 3;
182 }
183 /* Copy the input to the output until the next separator,
184 * while converting it to canonical separator */
185 else
186 {
187 while (*input && !G_IS_DIR_SEPARATOR (*input))
188 *output++ = *input++;
189 if (input[0] == 0)
190 break;
191 input++;
192 *output++ = G_DIR_SEPARATOR;
193 }
194
195 }
196
197 /* Remove a potentially trailing dir separator */
198 if (output > output_start && G_IS_DIR_SEPARATOR (output[-1]))
199 output--;
200
201 *output = '\0';
202
203 return canon;
204}
205
206#endif
207
208#if !GLIB_CHECK_VERSION(2, 60, 0)
209
210#define g_queue_clear_full queue_clear_full
211
221static inline void
222queue_clear_full (GQueue * queue, GDestroyNotify free_func)
223{
224 gpointer data;
225
226 while ((data = g_queue_pop_head (queue)) != NULL)
227 free_func (data);
228}
229
230#endif
231
232#if !GLIB_CHECK_VERSION(2, 61, 2)
233
234typedef volatile gint gatomicrefcount;
235
236typedef struct _GRealArray GRealArray;
241 guint8 *data;
242 guint len;
243 guint alloc;
244 guint elt_size;
246 guint clear;
247 gatomicrefcount ref_count;
248 GDestroyNotify clear_func;
249};
250
264static inline gboolean
265g_array_binary_search (GArray *array,
266 const void * target,
267 GCompareFunc compare_func,
268 guint *out_match_index)
269{
270 gboolean result = FALSE;
271 GRealArray *_array = (GRealArray *) array;
272 guint left, middle, right;
273 gint val;
274
275 g_return_val_if_fail (_array != NULL, FALSE);
276 g_return_val_if_fail (compare_func != NULL, FALSE);
277
278 if (G_LIKELY(_array->len))
279 {
280 left = 0;
281 right = _array->len - 1;
282
283 while (left <= right)
284 {
285 middle = left + (right - left) / 2;
286
287 val = compare_func (_array->data + (_array->elt_size * middle), target);
288 if (val == 0)
289 {
290 result = TRUE;
291 break;
292 }
293 else if (val < 0)
294 left = middle + 1;
295 else if (/* val > 0 && */ middle > 0)
296 right = middle - 1;
297 else
298 break; /* element not found */
299 }
300 }
301
302 if (result && out_match_index != NULL)
303 *out_match_index = middle;
304
305 return result;
306}
307#endif
308
309#if !GLIB_CHECK_VERSION(2, 64, 0)
310typedef struct _GRealPtrArray GRealPtrArray;
311
312
317 gpointer *pdata;
318 guint len;
319 guint alloc;
320 gatomicrefcount ref_count;
322 GDestroyNotify element_free_func;
323};
324
336static inline gpointer
337g_array_steal (GArray *array,
338 gsize *len)
339{
340 GRealArray *rarray;
341 gpointer segment;
342
343 g_return_val_if_fail (array != NULL, NULL);
344
345 rarray = (GRealArray *) array;
346 segment = (gpointer) rarray->data;
347
348 if (len != NULL)
349 *len = rarray->len;
350
351 rarray->data = NULL;
352 rarray->len = 0;
353 rarray->alloc = 0;
354 return segment;
355}
356
368static inline gpointer *
369g_ptr_array_steal (GPtrArray *array,
370 gsize *len)
371{
372 GRealPtrArray *rarray;
373 gpointer *segment;
374
375 g_return_val_if_fail (array != NULL, NULL);
376
377 rarray = (GRealPtrArray *) array;
378 segment = (gpointer *) rarray->pdata;
379
380 if (len != NULL)
381 *len = rarray->len;
382
383 rarray->pdata = NULL;
384 rarray->len = 0;
385 rarray->alloc = 0;
386 return segment;
387}
388
399static inline guint8 *g_byte_array_steal (GByteArray *array,
400 gsize *len)
401{
402 return (guint8 *) g_array_steal ((GArray *) array, len);
403}
404#endif
405
406#if !GLIB_CHECK_VERSION(2, 68, 0)
407
418static inline void *
419g_memdup2(const void *mem, size_t byte_size)
420{
421 void * new_mem;
422
423 if (mem && byte_size != 0) {
424 new_mem = g_malloc(byte_size);
425 memcpy(new_mem, mem, byte_size);
426 }
427 else
428 new_mem = NULL;
429
430 return new_mem;
431}
432#endif
433
434#if !GLIB_CHECK_VERSION(2, 74, 0)
435#ifndef G_REGEX_DEFAULT
436#define G_REGEX_DEFAULT ((GRegexCompileFlags)0)
437#endif
438#endif
439
440#if !GLIB_CHECK_VERSION(2, 74, 0)
441#ifndef G_REGEX_MATCH_DEFAULT
442#define G_REGEX_MATCH_DEFAULT ((GRegexMatchFlags)0)
443#endif
444#endif
445
446#if !GLIB_CHECK_VERSION(2, 88, 0)
447#ifndef G_NSEC_PER_SEC
448#define G_NSEC_PER_SEC 1000000000
449#endif
450#endif
451
452#ifdef __cplusplus
453}
454#endif /* __cplusplus */
455
456#endif /* GLIB_COMPAT_H */
Internal implementation of GArray, holding the backing buffer and allocation metadata.
Definition glib-compat.h:240
guint len
Definition glib-compat.h:242
guint elt_size
Definition glib-compat.h:244
GDestroyNotify clear_func
Definition glib-compat.h:248
guint alloc
Definition glib-compat.h:243
guint zero_terminated
Definition glib-compat.h:245
gatomicrefcount ref_count
Definition glib-compat.h:247
guint8 * data
Definition glib-compat.h:241
guint clear
Definition glib-compat.h:246
Internal implementation of GPtrArray, holding the backing pointer buffer and allocation metadata.
Definition glib-compat.h:316
guint len
Definition glib-compat.h:318
GDestroyNotify element_free_func
Definition glib-compat.h:322
guint alloc
Definition glib-compat.h:319
gatomicrefcount ref_count
Definition glib-compat.h:320
gpointer * pdata
Definition glib-compat.h:317
guint8 null_terminated
Definition glib-compat.h:321
Definition tvbuff_lz77huff.c:32
Represents a single decoded TCP segment extracted from a captured frame for graph analysis.
Definition tap-tcp-stream.h:56
int(* compare_func)(const void *a, const void *b)
Function pointer type for comparing two values.
Definition wmem_tree-int.h:106