]>
Commit | Line | Data |
---|---|---|
552cc11b | 1 | /* |
d25ce370 | 2 | * Copyright (C) 2008-2009 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 | /** | |
d25ce370 | 190 | * Described in header. |
552cc11b | 191 | */ |
d25ce370 TB |
192 | int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, |
193 | const void *const *args) | |
552cc11b MW |
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])); | |
d25ce370 | 200 | bool utc = *((bool*)(args[1]));; |
552cc11b MW |
201 | struct tm t; |
202 | ||
552cc11b MW |
203 | if (time == UNDEFINED_TIME) |
204 | { | |
d25ce370 TB |
205 | return print_in_hook(dst, len, "--- -- --:--:--%s----", |
206 | utc ? " UTC " : " "); | |
552cc11b MW |
207 | } |
208 | if (utc) | |
209 | { | |
210 | gmtime_r(time, &t); | |
211 | } | |
212 | else | |
213 | { | |
214 | localtime_r(time, &t); | |
215 | } | |
d25ce370 TB |
216 | return print_in_hook(dst, len, "%s %02d %02d:%02d:%02d%s%04d", |
217 | months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min, | |
218 | t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900); | |
552cc11b MW |
219 | } |
220 | ||
221 | /** | |
d25ce370 | 222 | * Described in header. |
552cc11b | 223 | */ |
d25ce370 TB |
224 | int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, |
225 | const void *const *args) | |
552cc11b MW |
226 | { |
227 | char* unit = "second"; | |
d25ce370 TB |
228 | time_t *arg1 = *((time_t**)(args[0])); |
229 | time_t *arg2 = *((time_t**)(args[1])); | |
230 | time_t delta = abs(*arg1 - *arg2); | |
552cc11b | 231 | |
552cc11b MW |
232 | if (delta > 2 * 60 * 60 * 24) |
233 | { | |
234 | delta /= 60 * 60 * 24; | |
235 | unit = "day"; | |
236 | } | |
237 | else if (delta > 2 * 60 * 60) | |
238 | { | |
239 | delta /= 60 * 60; | |
240 | unit = "hour"; | |
241 | } | |
242 | else if (delta > 2 * 60) | |
243 | { | |
244 | delta /= 60; | |
245 | unit = "minute"; | |
246 | } | |
d25ce370 | 247 | return print_in_hook(dst, len, "%d %s%s", delta, unit, (delta == 1)? "":"s"); |
552cc11b MW |
248 | } |
249 | ||
250 | /** | |
251 | * Number of bytes per line to dump raw data | |
252 | */ | |
253 | #define BYTES_PER_LINE 16 | |
254 | ||
255 | static char hexdig_upper[] = "0123456789ABCDEF"; | |
256 | ||
257 | /** | |
d25ce370 | 258 | * Described in header. |
552cc11b | 259 | */ |
d25ce370 TB |
260 | int mem_printf_hook(char *dst, size_t dstlen, |
261 | printf_hook_spec_t *spec, const void *const *args) | |
552cc11b MW |
262 | { |
263 | char *bytes = *((void**)(args[0])); | |
264 | int len = *((size_t*)(args[1])); | |
265 | ||
266 | char buffer[BYTES_PER_LINE * 3]; | |
267 | char ascii_buffer[BYTES_PER_LINE + 1]; | |
268 | char *buffer_pos = buffer; | |
269 | char *bytes_pos = bytes; | |
270 | char *bytes_roof = bytes + len; | |
271 | int line_start = 0; | |
272 | int i = 0; | |
273 | int written = 0; | |
274 | ||
d25ce370 | 275 | written += print_in_hook(dst, dstlen, "=> %d bytes @ %p", len, bytes); |
552cc11b MW |
276 | |
277 | while (bytes_pos < bytes_roof) | |
278 | { | |
279 | *buffer_pos++ = hexdig_upper[(*bytes_pos >> 4) & 0xF]; | |
280 | *buffer_pos++ = hexdig_upper[ *bytes_pos & 0xF]; | |
281 | ||
282 | ascii_buffer[i++] = | |
283 | (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.'; | |
284 | ||
285 | if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE) | |
286 | { | |
287 | int padding = 3 * (BYTES_PER_LINE - i); | |
552cc11b MW |
288 | |
289 | while (padding--) | |
290 | { | |
291 | *buffer_pos++ = ' '; | |
292 | } | |
293 | *buffer_pos++ = '\0'; | |
294 | ascii_buffer[i] = '\0'; | |
295 | ||
d25ce370 TB |
296 | written += print_in_hook(dst, dstlen, "\n%4d: %s %s", |
297 | line_start, buffer, ascii_buffer); | |
552cc11b MW |
298 | |
299 | buffer_pos = buffer; | |
300 | line_start += BYTES_PER_LINE; | |
301 | i = 0; | |
302 | } | |
303 | else | |
304 | { | |
305 | *buffer_pos++ = ' '; | |
306 | } | |
307 | } | |
308 | return written; | |
309 | } |