5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "src/shared/util.h"
35 void util_debug(util_debug_func_t function, void *user_data,
36 const char *format, ...)
41 if (!function || !format)
45 vsnprintf(str, sizeof(str), format, ap);
48 function(str, user_data);
51 void util_hexdump(const char dir, const unsigned char *buf, size_t len,
52 util_debug_func_t function, void *user_data)
54 static const char hexdigits[] = "0123456789abcdef";
58 if (!function || !len)
63 for (i = 0; i < len; i++) {
64 str[((i % 16) * 3) + 1] = ' ';
65 str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4];
66 str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf];
67 str[(i % 16) + 51] = isprint(buf[i]) ? buf[i] : '.';
69 if ((i + 1) % 16 == 0) {
73 function(str, user_data);
80 for (j = (i % 16); j < 16; j++) {
81 str[(j * 3) + 1] = ' ';
82 str[(j * 3) + 2] = ' ';
83 str[(j * 3) + 3] = ' ';
89 function(str, user_data);
93 void util_iso8601_to_timeval(char *str, struct timeval *time)
99 p = strptime(str, "%FT%T", &tm);
104 /* backwards compatibility */
105 if (*p != '.' || p[strlen(p) - 1] != 'Z')
117 char *util_timeval_to_iso8601(struct timeval *time)
124 if (!localtime_r(&t, &tm))
126 if (!strftime(buf, sizeof(buf), "%FT%TZ", &tm))
129 return g_strdup(buf);