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