2 * Copyright (C) 2008-2011 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
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>.
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
32 ENUM(status_names
, SUCCESS
, NEED_MORE
,
48 * Described in header.
50 void *clalloc(void * pointer
, size_t size
)
55 memcpy(data
, pointer
, size
);
61 * Described in header.
63 void memxor(u_int8_t dst
[], u_int8_t src
[], size_t n
)
67 /* byte wise XOR until dst aligned */
68 for (i
= 0; (uintptr_t)&dst
[i
] % sizeof(long) && i
< n
; i
++)
72 /* try to use words if src shares an aligment with dst */
73 switch (((uintptr_t)&src
[i
] % sizeof(long)))
76 for (m
= n
- sizeof(long); i
<= m
; i
+= sizeof(long))
78 *(long*)&dst
[i
] ^= *(long*)&src
[i
];
82 for (m
= n
- sizeof(int); i
<= m
; i
+= sizeof(int))
84 *(int*)&dst
[i
] ^= *(int*)&src
[i
];
88 for (m
= n
- sizeof(short); i
<= m
; i
+= sizeof(short))
90 *(short*)&dst
[i
] ^= *(short*)&src
[i
];
96 /* byte wise XOR of the rest */
104 * Described in header.
106 void memwipe_noinline(void *ptr
, size_t n
)
108 memwipe_inline(ptr
, n
);
112 * Described in header.
114 void *memstr(const void *haystack
, const char *needle
, size_t n
)
116 unsigned const char *pos
= haystack
;
117 size_t l
= strlen(needle
);
118 for (; n
>= l
; ++pos
, --n
)
120 if (memeq(pos
, needle
, l
))
129 * Described in header.
131 char* translate(char *str
, const char *from
, const char *to
)
134 if (strlen(from
) != strlen(to
))
141 if ((match
= strchr(from
, *pos
)) != NULL
)
143 *pos
= to
[match
- from
];
151 * Described in header.
153 bool mkdir_p(const char *path
, mode_t mode
)
156 char *pos
, full
[PATH_MAX
];
158 if (!path
|| *path
== '\0')
162 len
= snprintf(full
, sizeof(full
)-1, "%s", path
);
163 if (len
< 0 || len
>= sizeof(full
)-1)
165 DBG1(DBG_LIB
, "path string %s too long", path
);
168 /* ensure that the path ends with a '/' */
169 if (full
[len
-1] != '/')
174 /* skip '/' at the beginning */
179 while ((pos
= strchr(pos
, '/')))
182 if (access(full
, F_OK
) < 0)
184 if (mkdir(full
, mode
) < 0)
186 DBG1(DBG_LIB
, "failed to create directory %s", full
);
196 #ifndef HAVE_CLOSEFROM
198 * Described in header.
200 void closefrom(int lowfd
)
203 maxfd
= (int)sysconf(_SC_OPEN_MAX
);
208 for (fd
= lowfd
; fd
< maxfd
; fd
++)
213 #endif /* HAVE_CLOSEFROM */
216 * Return monotonic time
218 time_t time_monotonic(timeval_t
*tv
)
220 #if defined(HAVE_CLOCK_GETTIME) && \
221 (defined(HAVE_CONDATTR_CLOCK_MONOTONIC) || \
222 defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC))
223 /* as we use time_monotonic() for condvar operations, we use the
224 * monotonic time source only if it is also supported by pthread. */
227 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0)
231 tv
->tv_sec
= ts
.tv_sec
;
232 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
236 #endif /* HAVE_CLOCK_GETTIME && (...) */
237 /* Fallback to non-monotonic timestamps:
238 * On MAC OS X, creating monotonic timestamps is rather difficult. We
239 * could use mach_absolute_time() and catch sleep/wakeup notifications.
240 * We stick to the simpler (non-monotonic) gettimeofday() for now.
241 * But keep in mind: we need the same time source here as in condvar! */
246 if (gettimeofday(tv
, NULL
) != 0)
247 { /* should actually never fail if passed pointers are valid */
280 status_t
return_failed()
292 #ifndef HAVE_GCC_ATOMIC_OPERATIONS
296 * We use a single mutex for all refcount variables.
298 static pthread_mutex_t ref_mutex
= PTHREAD_MUTEX_INITIALIZER
;
303 void ref_get(refcount_t
*ref
)
305 pthread_mutex_lock(&ref_mutex
);
307 pthread_mutex_unlock(&ref_mutex
);
313 bool ref_put(refcount_t
*ref
)
317 pthread_mutex_lock(&ref_mutex
);
318 more_refs
= --(*ref
) > 0;
319 pthread_mutex_unlock(&ref_mutex
);
322 #endif /* HAVE_GCC_ATOMIC_OPERATIONS */
325 * Described in header.
327 int time_printf_hook(char *dst
, size_t len
, printf_hook_spec_t
*spec
,
328 const void *const *args
)
330 static const char* months
[] = {
331 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
332 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
334 time_t *time
= *((time_t**)(args
[0]));
335 bool utc
= *((bool*)(args
[1]));;
338 if (time
== UNDEFINED_TIME
)
340 return print_in_hook(dst
, len
, "--- -- --:--:--%s----",
341 utc ?
" UTC " : " ");
349 localtime_r(time
, &t
);
351 return print_in_hook(dst
, len
, "%s %02d %02d:%02d:%02d%s%04d",
352 months
[t
.tm_mon
], t
.tm_mday
, t
.tm_hour
, t
.tm_min
,
353 t
.tm_sec
, utc ?
" UTC " : " ", t
.tm_year
+ 1900);
357 * Described in header.
359 int time_delta_printf_hook(char *dst
, size_t len
, printf_hook_spec_t
*spec
,
360 const void *const *args
)
362 char* unit
= "second";
363 time_t *arg1
= *((time_t**)(args
[0]));
364 time_t *arg2
= *((time_t**)(args
[1]));
365 u_int64_t delta
= llabs(*arg1
- *arg2
);
367 if (delta
> 2 * 60 * 60 * 24)
369 delta
/= 60 * 60 * 24;
372 else if (delta
> 2 * 60 * 60)
377 else if (delta
> 2 * 60)
382 return print_in_hook(dst
, len
, "%" PRIu64
" %s%s", delta
, unit
,
383 (delta
== 1) ?
"" : "s");
387 * Number of bytes per line to dump raw data
389 #define BYTES_PER_LINE 16
391 static char hexdig_upper
[] = "0123456789ABCDEF";
394 * Described in header.
396 int mem_printf_hook(char *dst
, size_t dstlen
,
397 printf_hook_spec_t
*spec
, const void *const *args
)
399 char *bytes
= *((void**)(args
[0]));
400 int len
= *((size_t*)(args
[1]));
402 char buffer
[BYTES_PER_LINE
* 3];
403 char ascii_buffer
[BYTES_PER_LINE
+ 1];
404 char *buffer_pos
= buffer
;
405 char *bytes_pos
= bytes
;
406 char *bytes_roof
= bytes
+ len
;
411 written
+= print_in_hook(dst
, dstlen
, "=> %d bytes @ %p", len
, bytes
);
413 while (bytes_pos
< bytes_roof
)
415 *buffer_pos
++ = hexdig_upper
[(*bytes_pos
>> 4) & 0xF];
416 *buffer_pos
++ = hexdig_upper
[ *bytes_pos
& 0xF];
419 (*bytes_pos
> 31 && *bytes_pos
< 127) ?
*bytes_pos
: '.';
421 if (++bytes_pos
== bytes_roof
|| i
== BYTES_PER_LINE
)
423 int padding
= 3 * (BYTES_PER_LINE
- i
);
429 *buffer_pos
++ = '\0';
430 ascii_buffer
[i
] = '\0';
432 written
+= print_in_hook(dst
, dstlen
, "\n%4d: %s %s",
433 line_start
, buffer
, ascii_buffer
);
436 line_start
+= BYTES_PER_LINE
;