1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/* $Id$
*
* Lasso - A free implementation of the Liberty Alliance specifications.
*
* Copyright (C) 2004-2007 Entr'ouvert
* http://lasso.entrouvert.org
*
* Authors: See AUTHORS file in top-level directory.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __LASSO_UTILS_H__
#define __LASSO_UTILS_H__
#include <glib.h>
#include "debug.h"
#ifdef LASSO_DEBUG
#ifdef __GNUC__
#define lasso_check_type_equality(a,b) \
{ \
enum { TYPE_MISMATCH = (1 / __builtin_types_compatible_p(typeof(a), typeof(b))) }; \
}
#else
#define lasso_check_type_equality(a,b)
#endif
#else
#define lasso_check_type_equality(a,b)
#endif
#ifdef __GNUC__
#define lasso_check_type_equality2(a,b,c) \
{ \
enum { TYPE_MISMATCH = (1 / (__builtin_types_compatible_p(typeof(a), typeof(b))+__builtin_types_compatible_p(typeof(a), typeof(c)))) }; \
}
#else
#define lasso_check_type_equality2(a,b,c)
#endif
#define lasso_private_data(object) ((object)->private_data)
/* Freeing */
#define lasso_release(dest) \
{ \
if (dest) { \
g_free(dest); dest = NULL; \
} \
}
#define lasso_release_full(dest, free_function) \
{ \
if (dest) { \
free_function(dest); dest = NULL; \
} \
}
#define lasso_release_full2(dest, free_function, type) \
{ \
lasso_check_type_equality(dest, type); \
if (dest) { \
free_function(dest); dest = NULL; \
} \
}
#define lasso_release_gobject(dest) \
{ \
if (G_IS_OBJECT(dest) || dest == NULL) { \
lasso_release_full(dest, g_object_unref); \
} else { \
g_critical("Trying to unref a non GObject pointer dest=%s", #dest); \
} \
}
#define lasso_release_string(dest) \
lasso_release_full(dest, g_free)
#define lasso_release_list(dest) \
lasso_release_full2(dest, g_list_free, GList*)
#define lasso_release_list_of_full(dest, free_function) \
{ \
if (dest) { \
g_list_foreach(dest, (GFunc)free_function, NULL); \
lasso_release_list(dest); \
} \
}
#define lasso_release_list_of_strings(dest) \
lasso_release_list_of_full(dest, g_free)
#define lasso_release_list_of_gobjects(dest) \
lasso_release_list_of_full(dest, g_object_unref)
#define lasso_release_list_of_xml_node(dest) \
lasso_release_list_of_full(dest, xmlFreeNode)
#define lasso_release_xml_node(node) \
lasso_release_full2(node, xmlFreeNode, xmlNodePtr)
#define lasso_release_doc(doc) \
lasso_release_full2(doc, xmlFreeDoc, xmlDocPtr)
#define lasso_release_xml_string(dest) \
lasso_release_full2(dest, xmlFree, xmlChar*)
#define lasso_release_encrypt_context(dest) \
lasso_release_full2(dest, xmlSecEncCtxDestroy, xmlSecEncCtxPtr)
#define lasso_release_signature_context(dest) \
lasso_release_full2(dest, xmlSecDSigCtxDestroy,xmlSecDSigCtxPtr)
#define lasso_release_key_manager(dest) \
lasso_release_full2(dest, xmlSecKeysMngrDestroy, xmlSecKeysMngrPtr)
#define lasso_release_output_buffer(dest) \
lasso_release_full2(dest, xmlOutputBufferClose, xmlOutputBufferPtr)
#define lasso_release_xpath_object(dest) \
lasso_release_full2(dest, xmlXPathFreeObject, xmlXPathObjectPtr)
#define lasso_release_xpath_context(dest) \
lasso_release_full2(dest, xmlXPathFreeContext, xmlXPathContextPtr)
#define lasso_release_xpath_job(xpathObject, xpathContext, xmlDocument) \
lasso_release_xpath_object(xpathObject); \
lasso_release_xpath_context(xpathContext); \
lasso_release_doc(xmlDocument)
#define lasso_release_sec_key(dest) \
lasso_release_full2(dest, xmlSecKeyDestroy, xmlSecKeyPtr)
/* Assignment and list appending */
#define lasso_assign_string(dest,src) \
{ \
char *__tmp = g_strdup(src);\
lasso_release_string(dest); \
dest = __tmp; \
}
#define lasso_assign_xml_string(dest,src) \
{ \
xmlChar *__tmp = g_strdup(src); \
lasso_release_xml_string(dest); \
dest = __tmp; \
}
#define lasso_assign_new_string(dest,src) \
{ \
char *__tmp = src; \
if (dest != __tmp) \
lasso_release_string(dest); \
dest = __tmp; \
}
#define lasso_assign_gobject(dest,src) \
{ \
GObject *__tmp = G_OBJECT(src); \
if (__tmp) \
g_object_ref(__tmp); \
lasso_release_gobject(dest); \
dest = (void*)(__tmp); \
}
#define lasso_assign_new_gobject(dest,src) \
{ \
GObject *__tmp = G_OBJECT(src); \
if (dest != (void*)__tmp) \
lasso_release_gobject(dest); \
dest = (void*)(__tmp); \
}
#define lasso_assign_xml_node(dest,src) \
{ \
xmlNode *__tmp = (src); \
lasso_check_type_equality(dest, src); \
if (dest) \
xmlFreeNode(dest); \
dest = xmlCopyNode(__tmp, 1); \
}
#define lasso_assign_new_list_of_gobjects(dest, src) \
{ \
GList *__tmp = (src); \
lasso_release_gobject_list(dest); \
dest = __tmp; \
}
#define lasso_assign_list_of_gobjects(dest, src) \
{ \
GList *__tmp = (src); \
lasso_release_list_of_gobjects(dest); \
dest = g_list_copy(__tmp); \
for (;__tmp != NULL; __tmp = g_list_next(__tmp)) { \
if (G_IS_OBJECT(__tmp->data)) { \
g_object_ref(__tmp->data); \
} \
} \
}
#define lasso_assign_list_of_strings(dest, src) \
{ \
GList *__tmp = src; \
GList *__iter_dest; \
lasso_release_list_of_strings(dest); \
dest = g_list_copy(__tmp); \
for (__iter_dest = dest ; __iter_dest != NULL ; __iter_dest = g_list_next(__iter_dest)) { \
__iter_dest->data = g_strdup(__iter_dest->data); \
} \
}
#define lasso_assign_new_sec_key(dest, src) \
{ \
xmlSecKey *__tmp = (src); \
if (dest) \
lasso_release_sec_key(dest); \
dest = __tmp; \
}
#define lasso_assign_sec_key(dest, src) \
{ \
xmlSecKey *__tmp = xmlSecKeyDup(src); \
if (dest) \
lasso_release_sec_key(dest); \
dest = __tmp; \
}
/* List appending */
#define lasso_list_add(dest, src) \
{ \
lasso_check_type_equality((src), void*); \
dest = g_list_append(dest, (src)); \
}
#define lasso_list_add_non_null(dest, src) \
{ \
void *__tmp_non_null_src = (src); \
if (__tmp_non_null_src != NULL) { \
dest = g_list_append(dest, __tmp_non_null_src); \
} else { \
g_critical("Adding a NULL value to a non-NULL content list: dest=%s src=%s", #dest, #src); \
} \
}
#define lasso_list_add_string(dest, src) \
{ \
lasso_list_add_non_null(dest, g_strdup(src));\
}
#define lasso_list_add_new_string(dest, src) \
{ \
gchar *__tmp = src; \
lasso_list_add_non_null(dest, __tmp); \
}
#define lasso_list_add_xml_string(dest, src) \
{\
xmlChar *__tmp_src = (src);\
lasso_list_add_non_null(dest, (void*)g_strdup((char*)__tmp_src));\
}
#define lasso_list_add_gobject(dest, src) \
{ \
void *__tmp_src = (src); \
if (G_IS_OBJECT(__tmp_src)) { \
dest = g_list_append(dest, g_object_ref(__tmp_src)); \
} else { \
g_critical("Trying to add to a GList* a non GObject pointer dest=%s src=%s", #dest, #src); \
} \
}
#define lasso_list_add_new_gobject(dest, src) \
{ \
void *__tmp_src = (src); \
if (G_IS_OBJECT(__tmp_src)) { \
dest = g_list_append(dest, __tmp_src); \
} else { \
g_critical("Trying to add to a GList* a non GObject pointer dest=%s src=%s", #dest, #src); \
} \
}
#define lasso_list_add_xml_node(dest, src) \
{ \
xmlNode *__tmp_src = src; \
lasso_list_add_non_null(dest, __tmp_src); \
}
/* Pointer ownership transfer */
#define lasso_transfer_full(dest, src, kind) \
{\
lasso_release_##kind((dest)); \
lasso_check_type_equality(dest, src); \
(dest) = (void*)(src); \
(src) = NULL; \
}
#define lasso_transfer_xpath_object(dest, src) \
lasso_transfer_full(dest, src, xpath_object)
#define lasso_transfer_string(dest, src) \
lasso_transfer_full(dest, src, string)
#define lasso_transfer_gobject(dest, src) \
lasso_transfer_full(dest, src, gobject)
/* Node extraction */
#define lasso_extract_node_or_fail(to, from, kind, error) \
{\
void *__tmp = (from); \
if (LASSO_IS_##kind(__tmp)) { \
to = LASSO_##kind(__tmp); \
} else { \
rc = error; \
goto cleanup; \
}\
}
/* Bad param handling */
#define lasso_return_val_if_invalid_param(kind, name, val) \
g_return_val_if_fail(LASSO_IS_##kind(name), val)
#define lasso_bad_param(kind, name) \
lasso_return_val_if_invalid_param(kind, name, \
LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
#define lasso_null_param(name) \
g_return_val_if_fail(name != NULL, LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
/**
* The following macros are made to create some formalism for function's cleanup code.
*
* The exit label should be called 'cleanup'. And for functions returning an integer error code, the
* error code should be named 'rc' and 'return rc;' should be the last statement of the function.
*/
/**
* goto_cleanup_with_rc:
* @rc_value: integer return value
*
* This macro jump to the 'cleanup' label and set the return value to @rc_value.
*
*/
#define goto_cleanup_with_rc(rc_value) \
{\
rc = (rc_value); \
goto cleanup; \
}
/**
* goto_cleanup_if_fail:
* @condition: a boolean condition
*
* Jump to the 'cleanup' label if the @condition is FALSE.
*
*/
#define goto_cleanup_if_fail(condition) \
{\
if (! (condition) ) {\
goto cleanup; \
} \
}
/**
* goto_cleanup_if_fail_with_rc:
* @condition: a boolean condition
* @rc_value: integer return value
*
* Jump to the 'cleanup' label if the @condition is FALSE and set the return value to
* @rc_value.
*
*/
#define goto_cleanup_if_fail_with_rc(condition, rc_value) \
{\
if (! (condition) ) {\
rc = (rc_value); \
goto cleanup; \
} \
}
/**
* goto_cleanup_if_fail_with_rc_with_warning:
* @condition: a boolean condition
* @rc_value: integer return value
*
* Jump to the 'cleanup' label if the @condition is FALSE and set the return value to
* @rc_value. Also emit a warning, showing the condition and the return value.
*
*/
#define goto_cleanup_if_fail_with_rc_with_warning(condition, rc_value) \
{\
if (! (condition) ) {\
g_warning("%s failed, returning %s", __STRING(condition), __STRING(rc_value));\
rc = (rc_value); \
goto cleanup; \
} \
}
#define lasso_mem_debug(who, what, where) \
{ \
if (lasso_flag_memory_debug) \
fprintf(stderr, " freeing %s/%s (at %p)\n", who, what, (void*)where); \
}
#define lasso_foreach(_iter, _list) \
for (_iter = (_list); _iter; _iter = g_list_next(_iter))
/* Declare type of element in a container */
#ifndef OFTYPE
#define OFTYPE(x)
#endif
/* Get a printable extract for error messages */
char* lasso_safe_prefix_string(const char *str, gsize length);
#endif /* __LASSO_UTILS_H__ */
|