Imported Upstream version 1.38
[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
32 #include "src/shared/util.h"
33
34 void util_debug(util_debug_func_t function, void *user_data,
35                                                 const char *format, ...)
36 {
37         char str[78];
38         va_list ap;
39
40         if (!function || !format)
41                 return;
42
43         va_start(ap, format);
44         vsnprintf(str, sizeof(str), format, ap);
45         va_end(ap);
46
47         function(str, user_data);
48 }
49
50 void util_hexdump(const char dir, const unsigned char *buf, size_t len,
51                                 util_debug_func_t function, void *user_data)
52 {
53         static const char hexdigits[] = "0123456789abcdef";
54         char str[68];
55         size_t i;
56
57         if (!function || !len)
58                 return;
59
60         str[0] = dir;
61
62         for (i = 0; i < len; i++) {
63                 str[((i % 16) * 3) + 1] = ' ';
64                 str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4];
65                 str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf];
66                 str[(i % 16) + 51] = isprint(buf[i]) ? buf[i] : '.';
67
68                 if ((i + 1) % 16 == 0) {
69                         str[49] = ' ';
70                         str[50] = ' ';
71                         str[67] = '\0';
72                         function(str, user_data);
73                         str[0] = ' ';
74                 }
75         }
76
77         if (i % 16 > 0) {
78                 size_t j;
79                 for (j = (i % 16); j < 16; j++) {
80                         str[(j * 3) + 1] = ' ';
81                         str[(j * 3) + 2] = ' ';
82                         str[(j * 3) + 3] = ' ';
83                         str[j + 51] = ' ';
84                 }
85                 str[49] = ' ';
86                 str[50] = ' ';
87                 str[67] = '\0';
88                 function(str, user_data);
89         }
90 }
91
92 void util_iso8601_to_timeval(char *str, struct timeval *time)
93 {
94         struct tm tm;
95         time_t t;
96         char *p;
97
98         p = strptime(str, "%FT%T", &tm);
99         if (!p)
100                 return;
101
102         if (*p != 'Z') {
103                 /* backwards compatibility */
104                 if (*p != '.' || p[strlen(p) - 1] != 'Z')
105                         return;
106         }
107
108         t = mktime(&tm);
109         if (t < 0)
110                 return;
111
112         time->tv_sec = t;
113         time->tv_usec = 0;
114 }
115
116 char *util_timeval_to_iso8601(struct timeval *time)
117 {
118         char buf[255];
119         struct tm tm;
120         time_t t;
121
122         t = time->tv_sec;
123         if (!localtime_r(&t, &tm))
124                 return NULL;
125         if (!strftime(buf, sizeof(buf), "%FT%TZ", &tm))
126                 return NULL;
127
128         return g_strdup(buf);
129 }