62
62
void memxor(u_int8_t dest[], u_int8_t src[], size_t n)
65
for (i = 0; i < n; i++)
69
*(long*)(dest + i) ^= *(long*)(src + i);
80
* Described in header.
82
void *memstr(const void *haystack, const char *needle, size_t n)
84
unsigned const char *pos = haystack;
85
size_t l = strlen(needle);
86
for (; n >= l; ++pos, --n)
88
if (memeq(pos, needle, l))
157
#ifndef HAVE_GCC_ATOMIC_OPERATIONS
133
* We use a single mutex for all refcount variables. This
134
* is not optimal for performance, but the critical section
135
* is not that long...
136
* TODO: Consider to include a mutex in each refcount_t variable.
161
* We use a single mutex for all refcount variables.
138
163
static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
141
* Described in header.
143
* TODO: May be implemented with atomic CPU instructions
144
* instead of a mutex.
146
168
void ref_get(refcount_t *ref)
165
184
pthread_mutex_unlock(&ref_mutex);
166
185
return !more_refs;
187
#endif /* HAVE_GCC_ATOMIC_OPERATIONS */
170
* output handler in printf() for time_t
190
* Described in header.
172
static int time_print(FILE *stream, const struct printf_info *info,
173
const void *const *args)
192
int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
193
const void *const *args)
175
195
static const char* months[] = {
176
196
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
177
197
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
179
199
time_t *time = *((time_t**)(args[0]));
200
bool utc = *((bool*)(args[1]));;
185
utc = *((bool*)(args[1]));
187
203
if (time == UNDEFINED_TIME)
189
return fprintf(stream, "--- -- --:--:--%s----",
190
info->alt ? " UTC " : " ");
205
return print_in_hook(dst, len, "--- -- --:--:--%s----",
206
utc ? " UTC " : " ");
198
214
localtime_r(time, &t);
200
return fprintf(stream, "%s %02d %02d:%02d:%02d%s%04d",
201
months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
202
t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
206
* arginfo handler for printf() time
208
static int time_arginfo(const struct printf_info *info, size_t n, int *argtypes)
214
argtypes[0] = PA_POINTER;
215
argtypes[1] = PA_INT;
222
argtypes[0] = PA_POINTER;
228
* output handler in printf() for time deltas
230
static int time_delta_print(FILE *stream, const struct printf_info *info,
231
const void *const *args)
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);
222
* Described in header.
224
int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
225
const void *const *args)
233
227
char* unit = "second";
228
time_t *arg1 = *((time_t**)(args[0]));
229
time_t *arg2 = *((time_t**)(args[1]));
230
time_t delta = abs(*arg1 - *arg2);
237
arg1 = *((time_t**)(args[0]));
240
arg2 = *((time_t**)(args[1]));
241
delta = abs(*arg1 - *arg2);
248
232
if (delta > 2 * 60 * 60 * 24)
250
234
delta /= 60 * 60 * 24;
263
return fprintf(stream, "%d %s%s", delta, unit, (delta == 1)? "":"s");
267
* arginfo handler for printf() time deltas
269
int time_delta_arginfo(const struct printf_info *info, size_t n, int *argtypes)
275
argtypes[0] = PA_POINTER;
276
argtypes[1] = PA_POINTER;
283
argtypes[0] = PA_POINTER;
247
return print_in_hook(dst, len, "%d %s%s", delta, unit, (delta == 1)? "":"s");
293
255
static char hexdig_upper[] = "0123456789ABCDEF";
296
* output handler in printf() for mem ranges
258
* Described in header.
298
static int mem_print(FILE *stream, const struct printf_info *info,
299
const void *const *args)
260
int mem_printf_hook(char *dst, size_t dstlen,
261
printf_hook_spec_t *spec, const void *const *args)
301
263
char *bytes = *((void**)(args[0]));
302
264
int len = *((size_t*)(args[1]));
313
written += fprintf(stream, "=> %d bytes @ %p", len, bytes);
275
written += print_in_hook(dst, dstlen, "=> %d bytes @ %p", len, bytes);
315
277
while (bytes_pos < bytes_roof)
332
293
*buffer_pos++ = '\0';
333
294
ascii_buffer[i] = '\0';
335
written += fprintf(stream, "\n%4d: %s %s",
336
line_start, buffer, ascii_buffer);
296
written += print_in_hook(dst, dstlen, "\n%4d: %s %s",
297
line_start, buffer, ascii_buffer);
339
299
buffer_pos = buffer;
340
300
line_start += BYTES_PER_LINE;
352
* arginfo handler for printf() mem ranges
354
int mem_arginfo(const struct printf_info *info, size_t n, int *argtypes)
358
argtypes[0] = PA_POINTER;
359
argtypes[1] = PA_INT;
365
* return printf hook functions for a time
367
printf_hook_functions_t time_get_printf_hooks()
369
printf_hook_functions_t hooks = {time_print, time_arginfo};
375
* return printf hook functions for a time delta
377
printf_hook_functions_t time_delta_get_printf_hooks()
379
printf_hook_functions_t hooks = {time_delta_print, time_delta_arginfo};
385
* return printf hook functions for mem ranges
387
printf_hook_functions_t mem_get_printf_hooks()
389
printf_hook_functions_t hooks = {mem_print, mem_arginfo};