/[pdpsoft]/trunk/grid-mw-security/ees/src/aos/aos.c
ViewVC logotype

Contents of /trunk/grid-mw-security/ees/src/aos/aos.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 473 - (show annotations) (download) (as text)
Wed Jun 24 14:25:36 2009 UTC (12 years, 6 months ago) by aramv
File MIME type: text/x-chdr
File size: 5197 byte(s)
Added test code for l[4~oading a PEM file into the AOS.
1 #include "aos.h"
2
3 static arguments args_list;
4 static int _is_initialized;
5 int args_size;
6
7 int AOS_is_initialized(){
8 return _is_initialized;
9 }
10
11 int buffer_size(arg_type argtype){
12 switch (argtype){
13 case TYPE_NONE :
14 return 0;
15 case TYPE_SCHAR :
16 return sizeof(int);
17 case TYPE_UCHAR :
18 return sizeof(unsigned int);
19 case TYPE_SHORT :
20 return sizeof(int);
21 case TYPE_USHORT :
22 return sizeof(unsigned int);
23 case TYPE_INT :
24 return sizeof(int);
25 case TYPE_UINT :
26 return sizeof(unsigned int);
27 case TYPE_LONGINT :
28 return sizeof(long int);
29 case TYPE_ULONGINT :
30 return sizeof(unsigned long int);
31 #ifdef HAVE_LONG_LONG_INT
32 case TYPE_LONGLONGINT :
33 return sizeof(long long int);
34 case TYPE_ULONGLONGINT :
35 return sizeof(unsigned long long int);
36 #endif
37 case TYPE_FLOAT :
38 return sizeof(double);
39 case TYPE_DOUBLE :
40 return sizeof(double);
41 #ifdef HAVE_LONG_DOUBLE
42 case TYPE_LONGDOUBLE :
43 return sizeof(long double);
44 #endif
45 case TYPE_CHAR :
46 return sizeof(int);
47 #ifdef HAVE_WINT_T
48 case TYPE_WIDE_CHAR :
49 return sizeof(wint_t);
50 #endif
51 case TYPE_STRING :
52 return sizeof(const char*);
53 #ifdef HAVE_WCHAR_T
54 case TYPE_WIDE_STRING :
55 return sizeof(const wchar_t*);
56 #endif
57 case TYPE_POINTER :
58 return sizeof(void*);
59 case TYPE_COUNT_SCHAR_POINTER :
60 return sizeof(signed char*);
61 case TYPE_COUNT_SHORT_POINTER :
62 return sizeof(short *);
63 case TYPE_COUNT_INT_POINTER :
64 return sizeof(int*);
65 case TYPE_COUNT_LONGINT_POINTER :
66 return sizeof(long int*);
67 #ifdef HAVE_LONG_LONG_INT
68 case TYPE_COUNT_LONGLONGINT_POINTER :
69 return sizeof(long long int*);
70 #endif
71 default:
72 return 0;
73 }
74 }
75
76 int AOS_set_int(const char* label, int value){
77 int* stack_value = &value;
78 return AOS_set(label, stack_value, TYPE_INT, 0, NULL);
79 }
80
81 int AOS_set_string(const char* label, char* value){
82 return AOS_set(label, value, TYPE_STRING, strlen(value)+1, NULL);
83 }
84
85 int AOS_set_pem(const char* label, char* value){
86 return AOS_set(label, value, TYPE_PEM, strlen(value)+1, NULL);
87 }
88
89 /* uses memcpy to pass by value */
90 int AOS_set(const char *label, void *value, arg_type type, int arg_bufsize, argument_t * parent){
91 argument_t* args_new = NULL;
92 int buf_size;
93 buf_size = arg_bufsize;
94
95 if(!AOS_is_initialized()){
96 ng_log(LOG_ERR, "AOS is not yet initialized. Aborting.\n");
97 return 1;
98 }
99
100 /* allocate an extra argument in the list */
101 if((args_new = realloc(args_list.arg, (sizeof(argument_t) * (args_list.count + 1)))) == NULL){
102 ng_log(LOG_ERR, "Failed to realloc a new argument on the list. %s\n", strerror(errno));
103 return 1;
104 } else {
105 memset(&(args_new[args_list.count]), 0, sizeof(argument_t));
106 }
107 /*memset(args_list.arg[args_list.count].data, 0, buf_size);*/
108
109 if(args_new){
110 args_list.arg = args_new;
111 printf("Realloc succeeded. Argument lists is now %lu elements.\n", (unsigned long) (args_list.count + 1));
112 } else {
113 ng_log("args_list.arg not set: %s", strerror(errno));
114 return 1;
115 }
116
117 if(buf_size == 0) {
118 args_list.arg[args_list.count].data = value;
119 args_list.arg[args_list.count].needs_free = 0;
120 printf("1 pointer allocated at %p. No need to free!\n", args_list.arg[args_list.count].data);
121 } else {
122 args_list.arg[args_list.count].data = calloc(1, buf_size);
123 printf("Storing bytes: %i\n", buf_size);
124 memcpy(args_list.arg[args_list.count].data, value, buf_size);
125 args_list.arg[args_list.count].needs_free = 1;
126 printf("%i bytes allocated at %p. (index %lu) Remember to free it or DIE!\n", buf_size, args_list.arg[args_list.count].data, (unsigned long) args_list.count);
127 }
128
129 args_list.arg[args_list.count].type = type;
130 args_list.arg[args_list.count].label = label;
131 args_list.arg[args_list.count].parent= parent;
132
133 args_list.count++;
134 return 0;
135 }
136
137 void* AOS_get_value_by_label(char *label){
138 unsigned int i;
139 for(i = 0; i < args_list.count; i++){
140 if(args_list.arg[i].label == label){
141 return args_list.arg[i].data;
142 }
143 }
144 return NULL;
145 }
146
147 argument_t * AOS_get_argument_by_label(char *label){
148 unsigned int i;
149 for(i = 0; i < args_list.count; i++){
150 if(args_list.arg[i].label == label){
151 return &args_list.arg[i];
152 }
153 }
154 return NULL;
155 }
156
157 /* frees every copied byte */
158 void AOS_free_argslist(void) {
159 unsigned int i;
160 for(i = 0; i < args_list.count; i++){
161 if(args_list.arg[i].needs_free){
162 printf("Element %i points to %p\n", i, args_list.arg[i].data);
163 printf("Freeing arg %i\n",i);
164 free(args_list.arg[i].data);
165 }
166 }
167 args_list.count = 0;
168 }
169
170 int AOS_Init (void)
171 {
172 /*args_size = 0;*/
173 /* arguments args_list; */
174
175 args_list.count = 0;
176 /*printf("Allocating %i bytes for arguments list\n", args_list.count);*/
177 args_list.arg = NULL;
178 /*if (!args_list.arg)*/
179 /*return 1;*/
180
181 /*memset(args_list.arg, 0, args_size);*/
182
183 _is_initialized = 1;
184
185 return 0;
186 }
187
188
189 int AOS_Term (void)
190 {
191 AOS_free_argslist();
192
193 printf("Freeing list structure\n");
194 /* frees argument list */
195 free(args_list.arg);
196
197 return 0;
198 }

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28