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 /**
43 * This prints to stdout info about device being UNKNOWN, its
44 * ids, and libmtp's version number.
45 *
46 * @param dev_number the device number
47 * @param id_vendor vendor ID from the usb_device_desc struct
48 * @param id_product product ID from the usb_device_desc struct
49 */
device_unknown(const int dev_number,const int id_vendor,const int id_product)50 void device_unknown(const int dev_number, const int id_vendor, const int id_product)
51 {
52 // This device is unknown to the developers
53 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN in libmtp v%s.\n",
54 dev_number,
55 id_vendor,
56 id_product,
57 LIBMTP_VERSION_STRING);
58 LIBMTP_ERROR("Please report this VID/PID and the device model to the "
59 "libmtp development team\n");
60 /*
61 * Trying to get iManufacturer or iProduct from the device at this
62 * point would require opening a device handle, that we don't want
63 * to do right now. (Takes time for no good enough reason.)
64 */
65 }
66
67 /**
68 * This dumps out a number of bytes to a textual, hexadecimal
69 * dump.
70 *
71 * @param f the file to dump to (e.g. stdout or stderr)
72 * @param buf a pointer to the buffer containing the bytes to
73 * be dumped out in hex
74 * @param n the number of bytes to dump from this buffer
75 */
data_dump(FILE * f,void * buf,uint32_t n)76 void data_dump (FILE *f, void *buf, uint32_t n)
77 {
78 unsigned char *bp = (unsigned char *) buf;
79 uint32_t i;
80
81 for (i = 0; i < n; i++) {
82 fprintf(f, "%02x ", *bp);
83 bp++;
84 }
85 fprintf(f, "\n");
86 }
87
88 /**
89 * This dumps out a number of bytes to a textual, hexadecimal
90 * dump, and also prints out the string ASCII representation
91 * for each line of bytes. It will also print the memory address
92 * offset from a certain boundry.
93 *
94 * @param f the file to dump to (e.g. stdout or stderr)
95 * @param buf a pointer to the buffer containing the bytes to
96 * be dumped out in hex
97 * @param n the number of bytes to dump from this buffer
98 * @param dump_boundry the address offset to start at (usually 0)
99 */
data_dump_ascii(FILE * f,void * buf,uint32_t n,uint32_t dump_boundry)100 void data_dump_ascii (FILE *f, void *buf, uint32_t n, uint32_t dump_boundry)
101 {
102 uint32_t remain = n;
103 uint32_t ln, lc;
104 int i;
105 unsigned char *bp = (unsigned char *) buf;
106
107 lc = 0;
108 while (remain) {
109 fprintf(f, "\t%04x:", dump_boundry-0x10);
110
111 ln = ( remain > 16 ) ? 16 : remain;
112
113 for (i = 0; i < ln; i++) {
114 if ( ! (i%2) ) fprintf(f, " ");
115 fprintf(f, "%02x", bp[16*lc+i]);
116 }
117
118 if ( ln < 16 ) {
119 int width = ((16-ln)/2)*5 + (2*(ln%2));
120 fprintf(f, "%*.*s", width, width, "");
121 }
122
123 fprintf(f, "\t");
124 for (i = 0; i < ln; i++) {
125 unsigned char ch= bp[16*lc+i];
126 fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ?
127 ch : '.');
128 }
129 fprintf(f, "\n");
130
131 lc++;
132 remain -= ln;
133 dump_boundry += ln;
134 }
135 }
136
137 #ifndef HAVE_STRNDUP
strndup(const char * s,size_t n)138 char *strndup (const char *s, size_t n)
139 {
140 size_t len = strlen (s);
141 char *ret;
142
143 if (len <= n)
144 return strdup (s);
145
146 ret = malloc(n + 1);
147 strncpy(ret, s, n);
148 ret[n] = '\0';
149 return ret;
150 }
151 #endif
152
153