cast pointers to uintptr_t for alignement check
[strongswan.git] / src / libstrongswan / utils.c
1 /*
2 * Copyright (C) 2008-2009 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "utils.h"
18
19 #include <sys/stat.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <stdint.h>
24 #include <limits.h>
25 #include <dirent.h>
26 #include <time.h>
27
28 #include <enum.h>
29 #include <debug.h>
30
31 ENUM(status_names, SUCCESS, DESTROY_ME,
32 "SUCCESS",
33 "FAILED",
34 "OUT_OF_RES",
35 "ALREADY_DONE",
36 "NOT_SUPPORTED",
37 "INVALID_ARG",
38 "NOT_FOUND",
39 "PARSE_ERROR",
40 "VERIFY_ERROR",
41 "INVALID_STATE",
42 "DESTROY_ME",
43 "NEED_MORE",
44 );
45
46 /**
47 * Described in header.
48 */
49 void *clalloc(void * pointer, size_t size)
50 {
51 void *data;
52 data = malloc(size);
53
54 memcpy(data, pointer, size);
55
56 return (data);
57 }
58
59 /**
60 * Described in header.
61 */
62 void memxor(u_int8_t dst[], u_int8_t src[], size_t n)
63 {
64 int m, i;
65
66 /* byte wise XOR until dst aligned */
67 for (i = 0; (uintptr_t)&dst[i] % sizeof(long); i++)
68 {
69 dst[i] ^= src[i];
70 }
71 /* try to use words if src shares an aligment with dst */
72 switch (((uintptr_t)&src[i] % sizeof(long)))
73 {
74 case 0:
75 for (m = n - sizeof(long); i <= m; i += sizeof(long))
76 {
77 *(long*)&dst[i] ^= *(long*)&src[i];
78 }
79 break;
80 case sizeof(int):
81 for (m = n - sizeof(int); i <= m; i += sizeof(int))
82 {
83 *(int*)&dst[i] ^= *(int*)&src[i];
84 }
85 break;
86 case sizeof(short):
87 for (m = n - sizeof(short); i <= m; i += sizeof(short))
88 {
89 *(short*)&dst[i] ^= *(short*)&src[i];
90 }
91 break;
92 default:
93 break;
94 }
95 /* byte wise XOR of the rest */
96 for (; i < n; i++)
97 {
98 dst[i] ^= src[i];
99 }
100 }
101
102 /**
103 * Described in header.
104 */
105 void *memstr(const void *haystack, const char *needle, size_t n)
106 {
107 unsigned const char *pos = haystack;
108 size_t l = strlen(needle);
109 for (; n >= l; ++pos, --n)
110 {
111 if (memeq(pos, needle, l))
112 {
113 return (void*)pos;
114 }
115 }
116 return NULL;
117 }
118
119 /**
120 * Described in header.
121 */
122 bool mkdir_p(const char *path, mode_t mode)
123 {
124 size_t len;
125 char *pos, full[PATH_MAX];
126 pos = full;
127 if (!path || *path == '\0')
128 {
129 return TRUE;
130 }
131 len = snprintf(full, sizeof(full)-1, "%s", path);
132 if (len < 0 || len >= sizeof(full)-1)
133 {
134 DBG1("path string %s too long", path);
135 return FALSE;
136 }
137 /* ensure that the path ends with a '/' */
138 if (full[len-1] != '/')
139 {
140 full[len++] = '/';
141 full[len] = '\0';
142 }
143 /* skip '/' at the beginning */
144 while (*pos == '/')
145 {
146 pos++;
147 }
148 while ((pos = strchr(pos, '/')))
149 {
150 *pos = '\0';
151 if (access(full, F_OK) < 0)
152 {
153 if (mkdir(full, mode) < 0)
154 {
155 DBG1("failed to create directory %s", full);
156 return FALSE;
157 }
158 }
159 *pos = '/';
160 pos++;
161 }
162 return TRUE;
163 }
164
165 /**
166 * return null
167 */
168 void *return_null()
169 {
170 return NULL;
171 }
172
173 /**
174 * returns TRUE
175 */
176 bool return_true()
177 {
178 return TRUE;
179 }
180
181 /**
182 * returns FALSE
183 */
184 bool return_false()
185 {
186 return FALSE;
187 }
188
189 /**
190 * nop operation
191 */
192 void nop()
193 {
194 }
195
196 #ifndef HAVE_GCC_ATOMIC_OPERATIONS
197 #include <pthread.h>
198
199 /**
200 * We use a single mutex for all refcount variables.
201 */
202 static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
203
204 /**
205 * Increase refcount
206 */
207 void ref_get(refcount_t *ref)
208 {
209 pthread_mutex_lock(&ref_mutex);
210 (*ref)++;
211 pthread_mutex_unlock(&ref_mutex);
212 }
213
214 /**
215 * Decrease refcount
216 */
217 bool ref_put(refcount_t *ref)
218 {
219 bool more_refs;
220
221 pthread_mutex_lock(&ref_mutex);
222 more_refs = --(*ref);
223 pthread_mutex_unlock(&ref_mutex);
224 return !more_refs;
225 }
226 #endif /* HAVE_GCC_ATOMIC_OPERATIONS */
227
228 /**
229 * Described in header.
230 */
231 int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
232 const void *const *args)
233 {
234 static const char* months[] = {
235 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
236 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
237 };
238 time_t *time = *((time_t**)(args[0]));
239 bool utc = *((bool*)(args[1]));;
240 struct tm t;
241
242 if (time == UNDEFINED_TIME)
243 {
244 return print_in_hook(dst, len, "--- -- --:--:--%s----",
245 utc ? " UTC " : " ");
246 }
247 if (utc)
248 {
249 gmtime_r(time, &t);
250 }
251 else
252 {
253 localtime_r(time, &t);
254 }
255 return print_in_hook(dst, len, "%s %02d %02d:%02d:%02d%s%04d",
256 months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
257 t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
258 }
259
260 /**
261 * Described in header.
262 */
263 int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
264 const void *const *args)
265 {
266 char* unit = "second";
267 time_t *arg1 = *((time_t**)(args[0]));
268 time_t *arg2 = *((time_t**)(args[1]));
269 time_t delta = abs(*arg1 - *arg2);
270
271 if (delta > 2 * 60 * 60 * 24)
272 {
273 delta /= 60 * 60 * 24;
274 unit = "day";
275 }
276 else if (delta > 2 * 60 * 60)
277 {
278 delta /= 60 * 60;
279 unit = "hour";
280 }
281 else if (delta > 2 * 60)
282 {
283 delta /= 60;
284 unit = "minute";
285 }
286 return print_in_hook(dst, len, "%d %s%s", delta, unit, (delta == 1)? "":"s");
287 }
288
289 /**
290 * Number of bytes per line to dump raw data
291 */
292 #define BYTES_PER_LINE 16
293
294 static char hexdig_upper[] = "0123456789ABCDEF";
295
296 /**
297 * Described in header.
298 */
299 int mem_printf_hook(char *dst, size_t dstlen,
300 printf_hook_spec_t *spec, const void *const *args)
301 {
302 char *bytes = *((void**)(args[0]));
303 int len = *((size_t*)(args[1]));
304
305 char buffer[BYTES_PER_LINE * 3];
306 char ascii_buffer[BYTES_PER_LINE + 1];
307 char *buffer_pos = buffer;
308 char *bytes_pos = bytes;
309 char *bytes_roof = bytes + len;
310 int line_start = 0;
311 int i = 0;
312 int written = 0;
313
314 written += print_in_hook(dst, dstlen, "=> %d bytes @ %p", len, bytes);
315
316 while (bytes_pos < bytes_roof)
317 {
318 *buffer_pos++ = hexdig_upper[(*bytes_pos >> 4) & 0xF];
319 *buffer_pos++ = hexdig_upper[ *bytes_pos & 0xF];
320
321 ascii_buffer[i++] =
322 (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.';
323
324 if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE)
325 {
326 int padding = 3 * (BYTES_PER_LINE - i);
327
328 while (padding--)
329 {
330 *buffer_pos++ = ' ';
331 }
332 *buffer_pos++ = '\0';
333 ascii_buffer[i] = '\0';
334
335 written += print_in_hook(dst, dstlen, "\n%4d: %s %s",
336 line_start, buffer, ascii_buffer);
337
338 buffer_pos = buffer;
339 line_start += BYTES_PER_LINE;
340 i = 0;
341 }
342 else
343 {
344 *buffer_pos++ = ' ';
345 }
346 }
347 return written;
348 }