]>
Commit | Line | Data |
---|---|---|
552cc11b | 1 | /* |
6c20579a | 2 | * Copyright (C) 2008 Tobias Brunner |
552cc11b MW |
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 | * $Id$ | |
17 | */ | |
18 | ||
19 | #include "utils.h" | |
20 | ||
6c20579a | 21 | #include <sys/stat.h> |
552cc11b | 22 | #include <string.h> |
552cc11b | 23 | #include <stdio.h> |
6c20579a TB |
24 | #include <unistd.h> |
25 | #include <dirent.h> | |
f464d750 | 26 | #include <time.h> |
552cc11b MW |
27 | |
28 | #include <enum.h> | |
6c20579a | 29 | #include <debug.h> |
552cc11b MW |
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 dest[], u_int8_t src[], size_t n) | |
63 | { | |
4fd233a7 MW |
64 | int i = 0, m; |
65 | ||
66 | m = n - sizeof(long); | |
67 | while (i < m) | |
68 | { | |
69 | *(long*)(dest + i) ^= *(long*)(src + i); | |
70 | i += sizeof(long); | |
71 | } | |
72 | while (i < n) | |
552cc11b MW |
73 | { |
74 | dest[i] ^= src[i]; | |
4fd233a7 | 75 | i++; |
552cc11b MW |
76 | } |
77 | } | |
78 | ||
81736d7d TB |
79 | /** |
80 | * Described in header. | |
81 | */ | |
82 | void *memstr(const void *haystack, const char *needle, size_t n) | |
83 | { | |
84 | unsigned const char *pos = haystack; | |
85 | size_t l = strlen(needle); | |
86 | for (; n >= l; ++pos, --n) | |
87 | { | |
88 | if (memeq(pos, needle, l)) | |
89 | { | |
90 | return (void*)pos; | |
91 | } | |
92 | } | |
93 | return NULL; | |
94 | } | |
95 | ||
6c20579a TB |
96 | /** |
97 | * Described in header. | |
98 | */ | |
99 | bool mkdir_p(const char *path, mode_t mode) | |
100 | { | |
101 | size_t len; | |
102 | char *pos, full[PATH_MAX]; | |
103 | pos = full; | |
104 | if (!path || *path == '\0') | |
105 | { | |
106 | return TRUE; | |
107 | } | |
108 | len = snprintf(full, sizeof(full)-1, "%s", path); | |
109 | if (len < 0 || len >= sizeof(full)-1) | |
110 | { | |
111 | DBG1("path string %s too long", path); | |
112 | return FALSE; | |
113 | } | |
114 | /* ensure that the path ends with a '/' */ | |
115 | if (full[len-1] != '/') | |
116 | { | |
117 | full[len++] = '/'; | |
118 | full[len] = '\0'; | |
119 | } | |
120 | /* skip '/' at the beginning */ | |
121 | while (*pos == '/') | |
122 | { | |
123 | pos++; | |
124 | } | |
125 | while ((pos = strchr(pos, '/'))) | |
126 | { | |
127 | *pos = '\0'; | |
128 | if (access(full, F_OK) < 0) | |
129 | { | |
130 | if (mkdir(full, mode) < 0) | |
131 | { | |
132 | DBG1("failed to create directory %s", full); | |
133 | return FALSE; | |
134 | } | |
135 | } | |
136 | *pos = '/'; | |
137 | pos++; | |
138 | } | |
139 | return TRUE; | |
140 | } | |
141 | ||
081ae2eb MW |
142 | /** |
143 | * return null | |
144 | */ | |
145 | void *return_null() | |
146 | { | |
147 | return NULL; | |
148 | } | |
149 | ||
233b853d MW |
150 | /** |
151 | * nop operation | |
152 | */ | |
153 | void nop() | |
154 | { | |
155 | } | |
156 | ||
efd0fe21 MW |
157 | #ifndef HAVE_GCC_ATOMIC_OPERATIONS |
158 | #include <pthread.h> | |
159 | ||
552cc11b | 160 | /** |
efd0fe21 | 161 | * We use a single mutex for all refcount variables. |
552cc11b MW |
162 | */ |
163 | static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER; | |
164 | ||
165 | /** | |
efd0fe21 | 166 | * Increase refcount |
552cc11b MW |
167 | */ |
168 | void ref_get(refcount_t *ref) | |
169 | { | |
170 | pthread_mutex_lock(&ref_mutex); | |
171 | (*ref)++; | |
172 | pthread_mutex_unlock(&ref_mutex); | |
173 | } | |
174 | ||
175 | /** | |
efd0fe21 | 176 | * Decrease refcount |
552cc11b MW |
177 | */ |
178 | bool ref_put(refcount_t *ref) | |
179 | { | |
180 | bool more_refs; | |
181 | ||
182 | pthread_mutex_lock(&ref_mutex); | |
183 | more_refs = --(*ref); | |
184 | pthread_mutex_unlock(&ref_mutex); | |
185 | return !more_refs; | |
186 | } | |
efd0fe21 | 187 | #endif /* HAVE_GCC_ATOMIC_OPERATIONS */ |
552cc11b MW |
188 | |
189 | /** | |
190 | * output handler in printf() for time_t | |
191 | */ | |
192 | static int time_print(FILE *stream, const struct printf_info *info, | |
193 | const void *const *args) | |
194 | { | |
195 | static const char* months[] = { | |
196 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
197 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
198 | }; | |
199 | time_t *time = *((time_t**)(args[0])); | |
200 | bool utc = TRUE; | |
201 | struct tm t; | |
202 | ||
203 | if (info->alt) | |
204 | { | |
205 | utc = *((bool*)(args[1])); | |
206 | } | |
207 | if (time == UNDEFINED_TIME) | |
208 | { | |
209 | return fprintf(stream, "--- -- --:--:--%s----", | |
210 | info->alt ? " UTC " : " "); | |
211 | } | |
212 | if (utc) | |
213 | { | |
214 | gmtime_r(time, &t); | |
215 | } | |
216 | else | |
217 | { | |
218 | localtime_r(time, &t); | |
219 | } | |
220 | return fprintf(stream, "%s %02d %02d:%02d:%02d%s%04d", | |
221 | months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min, | |
222 | t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900); | |
223 | } | |
224 | ||
225 | /** | |
226 | * arginfo handler for printf() time | |
227 | */ | |
228 | static int time_arginfo(const struct printf_info *info, size_t n, int *argtypes) | |
229 | { | |
230 | if (info->alt) | |
231 | { | |
232 | if (n > 1) | |
233 | { | |
234 | argtypes[0] = PA_POINTER; | |
235 | argtypes[1] = PA_INT; | |
236 | } | |
237 | return 2; | |
238 | } | |
239 | ||
240 | if (n > 0) | |
241 | { | |
242 | argtypes[0] = PA_POINTER; | |
243 | } | |
244 | return 1; | |
245 | } | |
246 | ||
247 | /** | |
248 | * output handler in printf() for time deltas | |
249 | */ | |
250 | static int time_delta_print(FILE *stream, const struct printf_info *info, | |
251 | const void *const *args) | |
252 | { | |
253 | char* unit = "second"; | |
254 | time_t *arg1, *arg2; | |
255 | time_t delta; | |
256 | ||
257 | arg1 = *((time_t**)(args[0])); | |
258 | if (info->alt) | |
259 | { | |
260 | arg2 = *((time_t**)(args[1])); | |
261 | delta = abs(*arg1 - *arg2); | |
262 | } | |
263 | else | |
264 | { | |
265 | delta = *arg1; | |
266 | } | |
267 | ||
268 | if (delta > 2 * 60 * 60 * 24) | |
269 | { | |
270 | delta /= 60 * 60 * 24; | |
271 | unit = "day"; | |
272 | } | |
273 | else if (delta > 2 * 60 * 60) | |
274 | { | |
275 | delta /= 60 * 60; | |
276 | unit = "hour"; | |
277 | } | |
278 | else if (delta > 2 * 60) | |
279 | { | |
280 | delta /= 60; | |
281 | unit = "minute"; | |
282 | } | |
283 | return fprintf(stream, "%d %s%s", delta, unit, (delta == 1)? "":"s"); | |
284 | } | |
285 | ||
286 | /** | |
287 | * arginfo handler for printf() time deltas | |
288 | */ | |
289 | int time_delta_arginfo(const struct printf_info *info, size_t n, int *argtypes) | |
290 | { | |
291 | if (info->alt) | |
292 | { | |
293 | if (n > 1) | |
294 | { | |
295 | argtypes[0] = PA_POINTER; | |
296 | argtypes[1] = PA_POINTER; | |
297 | } | |
298 | return 2; | |
299 | } | |
300 | ||
301 | if (n > 0) | |
302 | { | |
303 | argtypes[0] = PA_POINTER; | |
304 | } | |
305 | return 1; | |
306 | } | |
307 | ||
308 | /** | |
309 | * Number of bytes per line to dump raw data | |
310 | */ | |
311 | #define BYTES_PER_LINE 16 | |
312 | ||
313 | static char hexdig_upper[] = "0123456789ABCDEF"; | |
314 | ||
315 | /** | |
316 | * output handler in printf() for mem ranges | |
317 | */ | |
318 | static int mem_print(FILE *stream, const struct printf_info *info, | |
319 | const void *const *args) | |
320 | { | |
321 | char *bytes = *((void**)(args[0])); | |
322 | int len = *((size_t*)(args[1])); | |
323 | ||
324 | char buffer[BYTES_PER_LINE * 3]; | |
325 | char ascii_buffer[BYTES_PER_LINE + 1]; | |
326 | char *buffer_pos = buffer; | |
327 | char *bytes_pos = bytes; | |
328 | char *bytes_roof = bytes + len; | |
329 | int line_start = 0; | |
330 | int i = 0; | |
331 | int written = 0; | |
332 | ||
333 | written += fprintf(stream, "=> %d bytes @ %p", len, bytes); | |
334 | ||
335 | while (bytes_pos < bytes_roof) | |
336 | { | |
337 | *buffer_pos++ = hexdig_upper[(*bytes_pos >> 4) & 0xF]; | |
338 | *buffer_pos++ = hexdig_upper[ *bytes_pos & 0xF]; | |
339 | ||
340 | ascii_buffer[i++] = | |
341 | (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.'; | |
342 | ||
343 | if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE) | |
344 | { | |
345 | int padding = 3 * (BYTES_PER_LINE - i); | |
346 | int written; | |
347 | ||
348 | while (padding--) | |
349 | { | |
350 | *buffer_pos++ = ' '; | |
351 | } | |
352 | *buffer_pos++ = '\0'; | |
353 | ascii_buffer[i] = '\0'; | |
354 | ||
355 | written += fprintf(stream, "\n%4d: %s %s", | |
356 | line_start, buffer, ascii_buffer); | |
357 | ||
358 | ||
359 | buffer_pos = buffer; | |
360 | line_start += BYTES_PER_LINE; | |
361 | i = 0; | |
362 | } | |
363 | else | |
364 | { | |
365 | *buffer_pos++ = ' '; | |
366 | } | |
367 | } | |
368 | return written; | |
369 | } | |
370 | ||
371 | /** | |
372 | * arginfo handler for printf() mem ranges | |
373 | */ | |
374 | int mem_arginfo(const struct printf_info *info, size_t n, int *argtypes) | |
375 | { | |
376 | if (n > 1) | |
377 | { | |
378 | argtypes[0] = PA_POINTER; | |
379 | argtypes[1] = PA_INT; | |
380 | } | |
381 | return 2; | |
382 | } | |
383 | ||
384 | /** | |
385 | * return printf hook functions for a time | |
386 | */ | |
387 | printf_hook_functions_t time_get_printf_hooks() | |
388 | { | |
389 | printf_hook_functions_t hooks = {time_print, time_arginfo}; | |
390 | ||
391 | return hooks; | |
392 | } | |
393 | ||
394 | /** | |
395 | * return printf hook functions for a time delta | |
396 | */ | |
397 | printf_hook_functions_t time_delta_get_printf_hooks() | |
398 | { | |
399 | printf_hook_functions_t hooks = {time_delta_print, time_delta_arginfo}; | |
400 | ||
401 | return hooks; | |
402 | } | |
403 | ||
404 | /** | |
405 | * return printf hook functions for mem ranges | |
406 | */ | |
407 | printf_hook_functions_t mem_get_printf_hooks() | |
408 | { | |
409 | printf_hook_functions_t hooks = {mem_print, mem_arginfo}; | |
410 | ||
411 | return hooks; | |
412 | } | |
413 |