From d5c143e5be0fb4beefe6a2da1f0f181bf4468883 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Tue, 16 Oct 2012 15:58:19 +0200 Subject: Moved enum_name_t to utils folder --- src/libhydra/attributes/attributes.h | 2 +- src/libsimaka/simaka_message.h | 2 +- src/libstrongswan/Android.mk | 4 +- src/libstrongswan/Makefile.am | 8 +- src/libstrongswan/crypto/transform.h | 2 +- src/libstrongswan/debug.h | 2 +- src/libstrongswan/enum.c | 81 ------------- src/libstrongswan/enum.h | 136 ---------------------- src/libstrongswan/plugins/pgp/pgp_builder.c | 2 +- src/libstrongswan/plugins/pkcs11/pkcs11_library.h | 2 +- src/libstrongswan/utils.c | 1 - src/libstrongswan/utils.h | 2 +- src/libstrongswan/utils/enum.c | 81 +++++++++++++ src/libstrongswan/utils/enum.h | 136 ++++++++++++++++++++++ 14 files changed, 230 insertions(+), 231 deletions(-) delete mode 100644 src/libstrongswan/enum.c delete mode 100644 src/libstrongswan/enum.h create mode 100644 src/libstrongswan/utils/enum.c create mode 100644 src/libstrongswan/utils/enum.h diff --git a/src/libhydra/attributes/attributes.h b/src/libhydra/attributes/attributes.h index 8ff774b64..c3c37cfc4 100644 --- a/src/libhydra/attributes/attributes.h +++ b/src/libhydra/attributes/attributes.h @@ -24,7 +24,7 @@ typedef enum configuration_attribute_type_t configuration_attribute_type_t; -#include +#include /** * Type of the attribute, as in IKEv2 RFC 3.15.1 or IKEv1 ModeConfig. diff --git a/src/libsimaka/simaka_message.h b/src/libsimaka/simaka_message.h index 209067c70..32c39a348 100644 --- a/src/libsimaka/simaka_message.h +++ b/src/libsimaka/simaka_message.h @@ -26,7 +26,7 @@ #ifndef SIMAKA_MESSAGE_H_ #define SIMAKA_MESSAGE_H_ -#include +#include #include #include "simaka_crypto.h" diff --git a/src/libstrongswan/Android.mk b/src/libstrongswan/Android.mk index 7308e3fee..3d01b2ada 100644 --- a/src/libstrongswan/Android.mk +++ b/src/libstrongswan/Android.mk @@ -3,7 +3,7 @@ include $(CLEAR_VARS) # copy-n-paste from Makefile.am LOCAL_SRC_FILES := \ -library.c debug.c enum.c settings.c \ +library.c debug.c settings.c \ asn1/asn1.c asn1/asn1_parser.c asn1/oid.c bio/bio_reader.c bio/bio_writer.c \ collections/blocking_queue.c collections/enumerator.c collections/hashtable.c \ collections/linked_list.c \ @@ -30,7 +30,7 @@ pen/pen.c plugins/plugin_loader.c plugins/plugin_feature.c processing/jobs/job.c processing/jobs/callback_job.c processing/processor.c processing/scheduler.c \ selectors/traffic_selector.c threading/thread.c threading/thread_value.c \ threading/mutex.c threading/semaphore.c threading/rwlock.c threading/spinlock.c \ -utils.c utils/chunk.c utils/identification.c utils/lexparser.c \ +utils.c utils/chunk.c utils/enum.c utils/identification.c utils/lexparser.c \ utils/optionsfrom.c utils/capabilities.c utils/backtrace.c utils/printf_hook.c # adding the plugin source files diff --git a/src/libstrongswan/Makefile.am b/src/libstrongswan/Makefile.am index 07d04b03e..153f6c9d4 100644 --- a/src/libstrongswan/Makefile.am +++ b/src/libstrongswan/Makefile.am @@ -1,7 +1,7 @@ ipseclib_LTLIBRARIES = libstrongswan.la libstrongswan_la_SOURCES = \ -library.c debug.c enum.c settings.c \ +library.c debug.c settings.c \ asn1/asn1.c asn1/asn1_parser.c asn1/oid.c bio/bio_reader.c bio/bio_writer.c \ collections/blocking_queue.c collections/enumerator.c collections/hashtable.c \ collections/linked_list.c \ @@ -28,13 +28,13 @@ pen/pen.c plugins/plugin_loader.c plugins/plugin_feature.c processing/jobs/job.c processing/jobs/callback_job.c processing/processor.c processing/scheduler.c \ selectors/traffic_selector.c threading/thread.c threading/thread_value.c \ threading/mutex.c threading/semaphore.c threading/rwlock.c threading/spinlock.c \ -utils.c utils/chunk.c utils/identification.c utils/lexparser.c \ +utils.c utils/chunk.c utils/enum.c utils/identification.c utils/lexparser.c \ utils/optionsfrom.c utils/capabilities.c utils/backtrace.c utils/printf_hook.c if USE_DEV_HEADERS strongswan_includedir = ${dev_headers} nobase_strongswan_include_HEADERS = \ -library.h debug.h enum.h settings.h \ +library.h debug.h settings.h \ asn1/asn1.h asn1/asn1_parser.h asn1/oid.h bio/bio_reader.h bio/bio_writer.h \ collections/blocking_queue.h collections/enumerator.h collections/hashtable.h \ collections/linked_list.h \ @@ -68,7 +68,7 @@ processing/scheduler.h selectors/traffic_selector.h \ threading/thread.h threading/thread_value.h \ threading/mutex.h threading/condvar.h threading/spinlock.h threading/semaphore.h \ threading/rwlock.h threading/rwlock_condvar.h threading/lock_profiler.h \ -utils.h utils/chunk.h utils/identification.h utils/lexparser.h \ +utils.h utils/chunk.h utils/enum.h utils/identification.h utils/lexparser.h \ utils/optionsfrom.h utils/capabilities.h utils/backtrace.h \ utils/leak_detective.h utils/printf_hook.h utils/integrity_checker.h endif diff --git a/src/libstrongswan/crypto/transform.h b/src/libstrongswan/crypto/transform.h index 311df068f..4a98f81e9 100644 --- a/src/libstrongswan/crypto/transform.h +++ b/src/libstrongswan/crypto/transform.h @@ -23,7 +23,7 @@ typedef enum transform_type_t transform_type_t; -#include +#include /** * Type of a transform, as in IKEv2 RFC 3.3.2. diff --git a/src/libstrongswan/debug.h b/src/libstrongswan/debug.h index ff4b4a1e9..337dfd92f 100644 --- a/src/libstrongswan/debug.h +++ b/src/libstrongswan/debug.h @@ -26,7 +26,7 @@ typedef enum level_t level_t; #include -#include "enum.h" +#include "utils/enum.h" /** * Debug message group. diff --git a/src/libstrongswan/enum.c b/src/libstrongswan/enum.c deleted file mode 100644 index 9b3c4d566..000000000 --- a/src/libstrongswan/enum.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2006 Martin Willi - * Hochschule fuer Technik Rapperswil - * - * 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. See . - * - * 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. - */ - -#include -#include - -#include - -#include "enum.h" - -/** - * See header. - */ -char *enum_to_name(enum_name_t *e, int val) -{ - do - { - if (val >= e->first && val <= e->last) - { - return e->names[val - e->first]; - } - } - while ((e = e->next)); - return NULL; -} - -/** - * See header. - */ -int enum_from_name(enum_name_t *e, char *name) -{ - do - { - int i, count = e->last - e->first + 1; - - for (i = 0; i < count; i++) - { - if (strcaseeq(name, e->names[i])) - { - return e->first + i; - } - } - } - while ((e = e->next)); - return -1; -} - -/** - * Described in header. - */ -int enum_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, - const void *const *args) -{ - enum_name_t *ed = *((enum_name_t**)(args[0])); - int val = *((int*)(args[1])); - char *name, buf[32]; - - name = enum_to_name(ed, val); - if (name == NULL) - { - snprintf(buf, sizeof(buf), "(%d)", val); - name = buf; - } - if (spec->minus) - { - return print_in_hook(data, "%-*s", spec->width, name); - } - return print_in_hook(data, "%*s", spec->width, name); -} diff --git a/src/libstrongswan/enum.h b/src/libstrongswan/enum.h deleted file mode 100644 index 0ed606813..000000000 --- a/src/libstrongswan/enum.h +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (C) 2009 Tobias Brunner - * Copyright (C) 2006-2008 Martin Willi - * Hochschule fuer Technik Rapperswil - * - * 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. See . - * - * 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. - */ - -/** - * @defgroup enum enum - * @{ @ingroup libstrongswan - */ - -#ifndef ENUM_H_ -#define ENUM_H_ - -#include "utils/printf_hook.h" - -typedef struct enum_name_t enum_name_t; - -/** - * Struct to store names for enums. - * - * To print the string representation of enumeration values, the strings - * are stored in these structures. Every enum_name contains a range - * of strings, multiple ranges are linked together. - * Use the convenience macros to define these linked ranges. - * - * For a single range, use: - * @code - ENUM(name, first, last, string1, string2, ...) - @endcode - * For multiple linked ranges, use: - * @code - ENUM_BEGIN(name, first, last, string1, string2, ...) - ENUM_NEXT(name, first, last, last_from_previous, string3, ...) - ENUM_NEXT(name, first, last, last_from_previous, string4, ...) - ENUM_END(name, last_from_previous) - @endcode - * The ENUM and the ENUM_END define a enum_name_t pointer with the name supplied - * in "name". - * - * Resolving of enum names is done using a printf hook. A printf fromat - * character %N is replaced by the enum string. Printf needs two arguments to - * resolve a %N, the enum_name_t* (the defined name in ENUM_BEGIN) followed - * by the numerical enum value. - */ -struct enum_name_t { - /** value of the first enum string */ - int first; - /** value of the last enum string */ - int last; - /** next enum_name_t in list */ - enum_name_t *next; - /** array of strings containing names from first to last */ - char *names[]; -}; - -/** - * Begin a new enum_name list. - * - * @param name name of the enum_name list - * @param first enum value of the first enum string - * @param last enum value of the last enum string - * @param ... a list of strings - */ -#define ENUM_BEGIN(name, first, last, ...) static enum_name_t name##last = {first, last, NULL, { __VA_ARGS__ }} - -/** - * Continue a enum name list startetd with ENUM_BEGIN. - * - * @param name name of the enum_name list - * @param first enum value of the first enum string - * @param last enum value of the last enum string - * @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT - * @param ... a list of strings - */ -#define ENUM_NEXT(name, first, last, prev, ...) static enum_name_t name##last = {first, last, &name##prev, { __VA_ARGS__ }} - -/** - * Complete enum name list started with ENUM_BEGIN. - * - * @param name name of the enum_name list - * @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT - */ -#define ENUM_END(name, prev) enum_name_t *name = &name##prev; - -/** - * Define a enum name with only one range. - * - * This is a convenience macro to use when a enum_name list contains only - * one range, and is equal as defining ENUM_BEGIN followed by ENUM_END. - * - * @param name name of the enum_name list - * @param first enum value of the first enum string - * @param last enum value of the last enum string - * @param ... a list of strings - */ -#define ENUM(name, first, last, ...) ENUM_BEGIN(name, first, last, __VA_ARGS__); ENUM_END(name, last) - -/** - * Convert a enum value to its string representation. - * - * @param e enum names for this enum value - * @param val enum value to get string for - * @return string for enum, NULL if not found - */ -char *enum_to_name(enum_name_t *e, int val); - -/** - * Convert a enum string back to its enum value. - * - * @param e enum names for this enum value - * @param name name to get enum value for - * @return enum value, -1 if not found - */ -int enum_from_name(enum_name_t *e, char *name); - -/** - * printf hook function for enum_names_t. - * - * Arguments are: - * enum_names_t *names, int value - */ -int enum_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, - const void *const *args); - -#endif /** ENUM_H_ @}*/ diff --git a/src/libstrongswan/plugins/pgp/pgp_builder.c b/src/libstrongswan/plugins/pgp/pgp_builder.c index 361157742..f358dc764 100644 --- a/src/libstrongswan/plugins/pgp/pgp_builder.c +++ b/src/libstrongswan/plugins/pgp/pgp_builder.c @@ -17,7 +17,7 @@ #include "pgp_builder.h" #include "pgp_utils.h" -#include +#include #include #include diff --git a/src/libstrongswan/plugins/pkcs11/pkcs11_library.h b/src/libstrongswan/plugins/pkcs11/pkcs11_library.h index f225b9b8d..abd99ed5f 100644 --- a/src/libstrongswan/plugins/pkcs11/pkcs11_library.h +++ b/src/libstrongswan/plugins/pkcs11/pkcs11_library.h @@ -29,7 +29,7 @@ typedef struct pkcs11_library_t pkcs11_library_t; #include "pkcs11.h" -#include +#include #include #include diff --git a/src/libstrongswan/utils.c b/src/libstrongswan/utils.c index db55b7673..4e13a4c51 100644 --- a/src/libstrongswan/utils.c +++ b/src/libstrongswan/utils.c @@ -27,7 +27,6 @@ #include #include -#include "enum.h" #include "debug.h" #include "collections/enumerator.h" diff --git a/src/libstrongswan/utils.h b/src/libstrongswan/utils.h index 4e2065f63..1da71af7b 100644 --- a/src/libstrongswan/utils.h +++ b/src/libstrongswan/utils.h @@ -29,7 +29,7 @@ #include #include -#include "enum.h" +#include "utils/enum.h" /** * strongSwan program return codes diff --git a/src/libstrongswan/utils/enum.c b/src/libstrongswan/utils/enum.c new file mode 100644 index 000000000..9b3c4d566 --- /dev/null +++ b/src/libstrongswan/utils/enum.c @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2006 Martin Willi + * Hochschule fuer Technik Rapperswil + * + * 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. See . + * + * 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. + */ + +#include +#include + +#include + +#include "enum.h" + +/** + * See header. + */ +char *enum_to_name(enum_name_t *e, int val) +{ + do + { + if (val >= e->first && val <= e->last) + { + return e->names[val - e->first]; + } + } + while ((e = e->next)); + return NULL; +} + +/** + * See header. + */ +int enum_from_name(enum_name_t *e, char *name) +{ + do + { + int i, count = e->last - e->first + 1; + + for (i = 0; i < count; i++) + { + if (strcaseeq(name, e->names[i])) + { + return e->first + i; + } + } + } + while ((e = e->next)); + return -1; +} + +/** + * Described in header. + */ +int enum_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, + const void *const *args) +{ + enum_name_t *ed = *((enum_name_t**)(args[0])); + int val = *((int*)(args[1])); + char *name, buf[32]; + + name = enum_to_name(ed, val); + if (name == NULL) + { + snprintf(buf, sizeof(buf), "(%d)", val); + name = buf; + } + if (spec->minus) + { + return print_in_hook(data, "%-*s", spec->width, name); + } + return print_in_hook(data, "%*s", spec->width, name); +} diff --git a/src/libstrongswan/utils/enum.h b/src/libstrongswan/utils/enum.h new file mode 100644 index 000000000..df8dbf8c1 --- /dev/null +++ b/src/libstrongswan/utils/enum.h @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2009 Tobias Brunner + * Copyright (C) 2006-2008 Martin Willi + * Hochschule fuer Technik Rapperswil + * + * 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. See . + * + * 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. + */ + +/** + * @defgroup enum enum + * @{ @ingroup utils + */ + +#ifndef ENUM_H_ +#define ENUM_H_ + +#include "printf_hook.h" + +typedef struct enum_name_t enum_name_t; + +/** + * Struct to store names for enums. + * + * To print the string representation of enumeration values, the strings + * are stored in these structures. Every enum_name contains a range + * of strings, multiple ranges are linked together. + * Use the convenience macros to define these linked ranges. + * + * For a single range, use: + * @code + ENUM(name, first, last, string1, string2, ...) + @endcode + * For multiple linked ranges, use: + * @code + ENUM_BEGIN(name, first, last, string1, string2, ...) + ENUM_NEXT(name, first, last, last_from_previous, string3, ...) + ENUM_NEXT(name, first, last, last_from_previous, string4, ...) + ENUM_END(name, last_from_previous) + @endcode + * The ENUM and the ENUM_END define a enum_name_t pointer with the name supplied + * in "name". + * + * Resolving of enum names is done using a printf hook. A printf fromat + * character %N is replaced by the enum string. Printf needs two arguments to + * resolve a %N, the enum_name_t* (the defined name in ENUM_BEGIN) followed + * by the numerical enum value. + */ +struct enum_name_t { + /** value of the first enum string */ + int first; + /** value of the last enum string */ + int last; + /** next enum_name_t in list */ + enum_name_t *next; + /** array of strings containing names from first to last */ + char *names[]; +}; + +/** + * Begin a new enum_name list. + * + * @param name name of the enum_name list + * @param first enum value of the first enum string + * @param last enum value of the last enum string + * @param ... a list of strings + */ +#define ENUM_BEGIN(name, first, last, ...) static enum_name_t name##last = {first, last, NULL, { __VA_ARGS__ }} + +/** + * Continue a enum name list startetd with ENUM_BEGIN. + * + * @param name name of the enum_name list + * @param first enum value of the first enum string + * @param last enum value of the last enum string + * @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT + * @param ... a list of strings + */ +#define ENUM_NEXT(name, first, last, prev, ...) static enum_name_t name##last = {first, last, &name##prev, { __VA_ARGS__ }} + +/** + * Complete enum name list started with ENUM_BEGIN. + * + * @param name name of the enum_name list + * @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT + */ +#define ENUM_END(name, prev) enum_name_t *name = &name##prev; + +/** + * Define a enum name with only one range. + * + * This is a convenience macro to use when a enum_name list contains only + * one range, and is equal as defining ENUM_BEGIN followed by ENUM_END. + * + * @param name name of the enum_name list + * @param first enum value of the first enum string + * @param last enum value of the last enum string + * @param ... a list of strings + */ +#define ENUM(name, first, last, ...) ENUM_BEGIN(name, first, last, __VA_ARGS__); ENUM_END(name, last) + +/** + * Convert a enum value to its string representation. + * + * @param e enum names for this enum value + * @param val enum value to get string for + * @return string for enum, NULL if not found + */ +char *enum_to_name(enum_name_t *e, int val); + +/** + * Convert a enum string back to its enum value. + * + * @param e enum names for this enum value + * @param name name to get enum value for + * @return enum value, -1 if not found + */ +int enum_from_name(enum_name_t *e, char *name); + +/** + * printf hook function for enum_names_t. + * + * Arguments are: + * enum_names_t *names, int value + */ +int enum_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, + const void *const *args); + +#endif /** ENUM_H_ @}*/ -- cgit v1.2.3