The introduced SHA1_NOFINAL hasher was not sufficient for EAP-AKA,
[strongswan.git] / src / libstrongswan / enum.c
1 /*
2 * Copyright (C) 2006 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include <stddef.h>
19 #include <stdio.h>
20
21 #include "enum.h"
22
23 #include <printf_hook.h>
24
25 /**
26 * get the name of an enum value in a enum_name_t list
27 */
28 static char *enum_name(enum_name_t *e, int val)
29 {
30 do
31 {
32 if (val >= e->first && val <= e->last)
33 {
34 return e->names[val - e->first];
35 }
36 }
37 while ((e = e->next));
38 return NULL;
39 }
40
41 /**
42 * output handler in printf() for enum names
43 */
44 static int print(FILE *stream, const struct printf_info *info,
45 const void *const *args)
46 {
47 enum_name_t *ed = *((enum_name_t**)(args[0]));
48 int val = *((int*)(args[1]));
49
50 char *name = enum_name(ed, val);
51
52 if (name == NULL)
53 {
54 return fprintf(stream, "(%d)", val);
55 }
56 else
57 {
58 return fprintf(stream, "%s", name);
59 }
60 }
61
62 /**
63 * arginfo handler for printf() hook
64 */
65 static int arginfo(const struct printf_info *info, size_t n, int *argtypes)
66 {
67 if (n > 1)
68 {
69 argtypes[0] = PA_POINTER;
70 argtypes[1] = PA_INT;
71 }
72 return 2;
73 }
74
75 /**
76 * return printf hook functions
77 */
78 printf_hook_functions_t enum_get_printf_hooks()
79 {
80 printf_hook_functions_t hooks = {print, arginfo};
81
82 return hooks;
83 }
84