Merge tag 'upstream/1.40' into tizen.
[platform/upstream/connman.git] / src / shared / util.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <string.h>
32
33 #include "src/shared/util.h"
34
35 void util_debug(util_debug_func_t function, void *user_data,
36                                                 const char *format, ...)
37 {
38         char str[78];
39         va_list ap;
40
41         if (!function || !format)
42                 return;
43
44         va_start(ap, format);
45         vsnprintf(str, sizeof(str), format, ap);
46         va_end(ap);
47
48         function(str, user_data);
49 }
50
51 void util_hexdump(const char dir, const unsigned char *buf, size_t len,
52                                 util_debug_func_t function, void *user_data)
53 {
54         static const char hexdigits[] = "0123456789abcdef";
55         char str[68];
56         size_t i;
57
58         if (!function || !len)
59                 return;
60
61         str[0] = dir;
62
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] : '.';
68
69                 if ((i + 1) % 16 == 0) {
70                         str[49] = ' ';
71                         str[50] = ' ';
72                         str[67] = '\0';
73                         function(str, user_data);
74                         str[0] = ' ';
75                 }
76         }
77
78         if (i % 16 > 0) {
79                 size_t j;
80                 for (j = (i % 16); j < 16; j++) {
81                         str[(j * 3) + 1] = ' ';
82                         str[(j * 3) + 2] = ' ';
83                         str[(j * 3) + 3] = ' ';
84                         str[j + 51] = ' ';
85                 }
86                 str[49] = ' ';
87                 str[50] = ' ';
88                 str[67] = '\0';
89                 function(str, user_data);
90         }
91 }
92
93 void util_iso8601_to_timeval(char *str, struct timeval *time)
94 {
95         struct tm tm;
96         time_t t;
97         char *p;
98
99         p = strptime(str, "%FT%T", &tm);
100         if (!p)
101                 return;
102
103         if (*p != 'Z') {
104                 /* backwards compatibility */
105                 if (*p != '.' || p[strlen(p) - 1] != 'Z')
106                         return;
107         }
108
109         t = mktime(&tm);
110         if (t < 0)
111                 return;
112
113         time->tv_sec = t;
114         time->tv_usec = 0;
115 }
116
117 char *util_timeval_to_iso8601(struct timeval *time)
118 {
119         char buf[255];
120         struct tm tm;
121         time_t t;
122
123         t = time->tv_sec;
124         if (!localtime_r(&t, &tm))
125                 return NULL;
126         if (!strftime(buf, sizeof(buf), "%FT%TZ", &tm))
127                 return NULL;
128
129         return g_strdup(buf);
130 }