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

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