fixed two bugs in the time_t and chunk_ct print functions
[strongswan.git] / src / libstrongswan / types.c
1 /**
2 * @file types.c
3 *
4 * @brief Generic types.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <string.h>
25 #include <time.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <pthread.h>
29 #include <printf.h>
30
31 #include "types.h"
32
33 ENUM(status_names, SUCCESS, DESTROY_ME,
34 "SUCCESS",
35 "FAILED",
36 "OUT_OF_RES",
37 "ALREADY_DONE",
38 "NOT_SUPPORTED",
39 "INVALID_ARG",
40 "NOT_FOUND",
41 "PARSE_ERROR",
42 "VERIFY_ERROR",
43 "INVALID_STATE",
44 "DESTROY_ME",
45 );
46
47
48 /**
49 * Empty chunk.
50 */
51 chunk_t CHUNK_INITIALIZER = { NULL, 0 };
52
53 /**
54 * Described in header.
55 */
56 chunk_t chunk_clone(chunk_t chunk)
57 {
58 chunk_t clone = CHUNK_INITIALIZER;
59
60 if (chunk.ptr && chunk.len > 0)
61 {
62 clone.ptr = malloc(chunk.len);
63 clone.len = chunk.len;
64 memcpy(clone.ptr, chunk.ptr, chunk.len);
65 }
66
67 return clone;
68 }
69
70 /**
71 * Decribed in header.
72 */
73 chunk_t chunk_cat(const char* mode, ...)
74 {
75 chunk_t construct;
76 va_list chunks;
77 u_char *pos;
78 int i;
79 int count = strlen(mode);
80
81 /* sum up lengths of individual chunks */
82 va_start(chunks, mode);
83 construct.len = 0;
84 for (i = 0; i < count; i++)
85 {
86 chunk_t ch = va_arg(chunks, chunk_t);
87 construct.len += ch.len;
88 }
89 va_end(chunks);
90
91 /* allocate needed memory for construct */
92 construct.ptr = malloc(construct.len);
93 pos = construct.ptr;
94
95 /* copy or move the chunks */
96 va_start(chunks, mode);
97 for (i = 0; i < count; i++)
98 {
99 chunk_t ch = va_arg(chunks, chunk_t);
100 switch (*mode++)
101 {
102 case 'm':
103 memcpy(pos, ch.ptr, ch.len);
104 pos += ch.len;
105 free(ch.ptr);
106 break;
107 case 'c':
108 default:
109 memcpy(pos, ch.ptr, ch.len);
110 pos += ch.len;
111 }
112 }
113 va_end(chunks);
114
115 return construct;
116 }
117
118 /**
119 * Described in header.
120 */
121 void chunk_free(chunk_t *chunk)
122 {
123 free(chunk->ptr);
124 chunk->ptr = NULL;
125 chunk->len = 0;
126 }
127
128 /**
129 * Described in header.
130 */
131 chunk_t chunk_alloc(size_t bytes)
132 {
133 chunk_t new_chunk;
134 new_chunk.ptr = malloc(bytes);
135 new_chunk.len = bytes;
136 return new_chunk;
137 }
138
139 /**
140 * Described in header.
141 */
142 bool chunk_equals(chunk_t a, chunk_t b)
143 {
144 return a.ptr != NULL && b.ptr != NULL &&
145 a.len == b.len && memeq(a.ptr, b.ptr, a.len);
146 }
147
148 /**
149 * Described in header.
150 */
151 bool chunk_equals_or_null(chunk_t a, chunk_t b)
152 {
153 if (a.ptr == NULL || b.ptr == NULL)
154 return TRUE;
155 return a.len == b.len && memeq(a.ptr, b.ptr, a.len);
156 }
157
158 /**
159 * Described in header.
160 */
161 void *clalloc(void * pointer, size_t size)
162 {
163 void *data;
164 data = malloc(size);
165
166 memcpy(data, pointer,size);
167
168 return (data);
169 }
170
171 /**
172 * We use a single mutex for all refcount variables. This
173 * is not optimal for performance, but the critical section
174 * is not that long...
175 * TODO: Consider to include a mutex in each refcount_t variable.
176 */
177 static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
178
179 /**
180 * Described in header.
181 *
182 * TODO: May be implemented with atomic CPU instructions
183 * instead of a mutex.
184 */
185 void ref_get(refcount_t *ref)
186 {
187 pthread_mutex_lock(&ref_mutex);
188 (*ref)++;
189 pthread_mutex_unlock(&ref_mutex);
190 }
191
192 /**
193 * Described in header.
194 *
195 * TODO: May be implemented with atomic CPU instructions
196 * instead of a mutex.
197 */
198 bool ref_put(refcount_t *ref)
199 {
200 bool more_refs;
201
202 pthread_mutex_lock(&ref_mutex);
203 more_refs = --(*ref);
204 pthread_mutex_unlock(&ref_mutex);
205 return !more_refs;
206 }
207
208 /**
209 * Number of bytes per line to dump raw data
210 */
211 #define BYTES_PER_LINE 16
212
213 /**
214 * output handler in printf() for byte ranges
215 */
216 static int print_bytes(FILE *stream, const struct printf_info *info,
217 const void *const *args)
218 {
219 char *bytes = *((void**)(args[0]));
220 int len = *((size_t*)(args[1]));
221
222 char buffer[BYTES_PER_LINE * 3];
223 char ascii_buffer[BYTES_PER_LINE + 1];
224 char *buffer_pos = buffer;
225 char *bytes_pos = bytes;
226 char *bytes_roof = bytes + len;
227 int line_start = 0;
228 int i = 0;
229 int written = 0;
230
231 written += fprintf(stream, "=> %d bytes @ %p", len, bytes);
232
233 while (bytes_pos < bytes_roof)
234 {
235 static char hexdig[] = "0123456789ABCDEF";
236
237 *buffer_pos++ = hexdig[(*bytes_pos >> 4) & 0xF];
238 *buffer_pos++ = hexdig[ *bytes_pos & 0xF];
239
240 ascii_buffer[i++] =
241 (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.';
242
243 if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE)
244 {
245 int padding = 3 * (BYTES_PER_LINE - i);
246 int written;
247
248 while (padding--)
249 {
250 *buffer_pos++ = ' ';
251 }
252 *buffer_pos++ = '\0';
253 ascii_buffer[i] = '\0';
254
255 written += fprintf(stream, "\n%4d: %s %s",
256 line_start, buffer, ascii_buffer);
257
258
259 buffer_pos = buffer;
260 line_start += BYTES_PER_LINE;
261 i = 0;
262 }
263 else
264 {
265 *buffer_pos++ = ' ';
266 }
267 }
268 return written;
269 }
270
271 /**
272 * output handler in printf() for chunks
273 */
274 static int print_chunk(FILE *stream, const struct printf_info *info,
275 const void *const *args)
276 {
277 chunk_t *chunk = *((chunk_t**)(args[0]));
278 bool first = TRUE;
279 chunk_t copy = *chunk;
280 int written = 0;
281
282 if (!info->alt)
283 {
284 const void *new_args[] = {&chunk->ptr, &chunk->len};
285 return print_bytes(stream, info, new_args);
286 }
287
288 while (copy.len > 0)
289 {
290 if (first)
291 {
292 first = FALSE;
293 }
294 else
295 {
296 written += fprintf(stream, ":");
297 }
298 written += fprintf(stream, "%02x", *copy.ptr++);
299 copy.len--;
300 }
301 return written;
302 }
303
304 /**
305 * output handler in printf() for time_t
306 */
307 static int print_time(FILE *stream, const struct printf_info *info,
308 const void *const *args)
309 {
310 static const char* months[] = {
311 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
312 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
313 };
314 time_t time = *((time_t*)(args[0]));
315 bool utc = TRUE;
316 struct tm t;
317
318 if (info->alt)
319 {
320 utc = *((bool*)(args[1]));
321 }
322 if (time == UNDEFINED_TIME)
323 {
324 return fprintf(stream, "--- -- --:--:--%s----",
325 info->alt ? " UTC " : " ");
326 }
327 if (utc)
328 {
329 gmtime_r(&time, &t);
330 }
331 else
332 {
333 localtime_r(&time, &t);
334 }
335 return fprintf(stream, "%s %02d %02d:%02d:%02d%s%04d",
336 months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
337 t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
338 }
339
340 /**
341 * output handler in printf() for time deltas
342 */
343 static int print_time_delta(FILE *stream, const struct printf_info *info,
344 const void *const *args)
345 {
346 time_t start = *((time_t*)(args[0]));
347 time_t end = *((time_t*)(args[1]));
348 u_int delta = abs(end - start);
349 char* unit = "second";
350
351 if (delta > 2 * 60 * 60 * 24)
352 {
353 delta /= 60 * 60 * 24;
354 unit = "days";
355 }
356 else if (delta > 2 * 60 * 60)
357 {
358 delta /= 60 * 60;
359 unit = "hours";
360 }
361 else if (delta > 2 * 60)
362 {
363 delta /= 60;
364 unit = "minutes";
365 }
366 return fprintf(stream, "%d %s", delta, unit);
367 }
368
369 /**
370 * arginfo handler in printf() for byte ranges
371 */
372 static int print_bytes_arginfo(const struct printf_info *info, size_t n, int *argtypes)
373 {
374 if (n > 1)
375 {
376 argtypes[0] = PA_POINTER;
377 argtypes[1] = PA_INT;
378 }
379 return 2;
380 }
381
382 /**
383 * arginfo handler in printf() for time deltas
384 */
385 static int print_time_delta_arginfo(const struct printf_info *info, size_t n, int *argtypes)
386 {
387 if (n > 1)
388 {
389 argtypes[0] = PA_INT;
390 argtypes[1] = PA_INT;
391 }
392 return 2;
393 }
394
395 /**
396 * arginfo handler in printf() for time_t
397 */
398 static int print_time_arginfo(const struct printf_info *info, size_t n, int *argtypes)
399 {
400 if (info->alt)
401 {
402 if (n > 1)
403 {
404 argtypes[0] = PA_INT;
405 argtypes[1] = PA_INT;
406 }
407 return 2;
408 }
409
410 if (n > 0)
411 {
412 argtypes[0] = PA_INT;
413 }
414 return 1;
415 }
416
417 /**
418 * arginfo handler in printf() for chunks
419 */
420 static int print_chunk_arginfo(const struct printf_info *info, size_t n, int *argtypes)
421 {
422 if (n > 0)
423 {
424 argtypes[0] = PA_POINTER;
425 }
426 return 1;
427 }
428
429 /**
430 * register printf() handlers for time_t
431 */
432 static void __attribute__ ((constructor))print_register()
433 {
434 register_printf_function(CHUNK_PRINTF_SPEC, print_chunk, print_chunk_arginfo);
435 register_printf_function(BYTES_PRINTF_SPEC, print_bytes, print_bytes_arginfo);
436 register_printf_function(TIME_PRINTF_SPEC, print_time, print_time_arginfo);
437 register_printf_function(TIME_DELTA_PRINTF_SPEC, print_time_delta, print_time_delta_arginfo);
438 }