Code Sync up from tizen_2.4
[platform/upstream/libmtp.git] / src / util.c
1 /**
2  * \file util.c
3  *
4  * This file contains generic utility functions such as can be
5  * used for debugging for example.
6  *
7  * Copyright (C) 2005-2007 Linus Walleij <triad@df.lth.se>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /* MSVC does not have these */
26 #ifndef _MSC_VER
27 #include <sys/time.h>
28 #include <unistd.h>
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #include "config.h"
38 #include "libmtp.h"
39 #include "util.h"
40
41 /**
42  * This dumps out a number of bytes to a textual, hexadecimal
43  * dump.
44  *
45  * @param f the file to dump to (e.g. stdout or stderr)
46  * @param buf a pointer to the buffer containing the bytes to
47  *            be dumped out in hex
48  * @param n the number of bytes to dump from this buffer
49  */
50 void data_dump (FILE *f, void *buf, uint32_t n)
51 {
52   unsigned char *bp = (unsigned char *) buf;
53   uint32_t i;
54
55   for (i = 0; i < n; i++) {
56     fprintf(f, "%02x ", *bp);
57     bp++;
58   }
59   fprintf(f, "\n");
60 }
61
62 /**
63  * This dumps out a number of bytes to a textual, hexadecimal
64  * dump, and also prints out the string ASCII representation
65  * for each line of bytes. It will also print the memory address
66  * offset from a certain boundry.
67  *
68  * @param f the file to dump to (e.g. stdout or stderr)
69  * @param buf a pointer to the buffer containing the bytes to
70  *            be dumped out in hex
71  * @param n the number of bytes to dump from this buffer
72  * @param dump_boundry the address offset to start at (usually 0)
73  */
74 void data_dump_ascii (FILE *f, void *buf, uint32_t n, uint32_t dump_boundry)
75 {
76   uint32_t remain = n;
77   uint32_t ln, lc;
78   int i;
79   unsigned char *bp = (unsigned char *) buf;
80
81   lc = 0;
82   while (remain) {
83     fprintf(f, "\t%04x:", dump_boundry-0x10);
84
85     ln = ( remain > 16 ) ? 16 : remain;
86
87     for (i = 0; i < ln; i++) {
88       if ( ! (i%2) ) fprintf(f, " ");
89       fprintf(f, "%02x", bp[16*lc+i]);
90     }
91
92     if ( ln < 16 ) {
93       int width = ((16-ln)/2)*5 + (2*(ln%2));
94       fprintf(f, "%*.*s", width, width, "");
95     }
96
97     fprintf(f, "\t");
98     for (i = 0; i < ln; i++) {
99       unsigned char ch= bp[16*lc+i];
100       fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ?
101               ch : '.');
102     }
103     fprintf(f, "\n");
104
105     lc++;
106     remain -= ln;
107     dump_boundry += ln;
108   }
109 }
110
111 #ifndef HAVE_STRNDUP
112 char *strndup (const char *s, size_t n)
113 {
114   size_t len = strlen (s);
115   char *ret;
116
117   if (len <= n)
118     return strdup (s);
119
120   ret = malloc(n + 1);
121   strncpy(ret, s, n);
122   ret[n] = '\0';
123   return ret;
124 }
125 #endif
126