4 * This file contains generic utility functions such as can be
5 * used for debugging for example.
7 * Copyright (C) 2005-2007 Linus Walleij <triad@df.lth.se>
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.
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.
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.
25 /* MSVC does not have these */
43 * This prints to stdout info about device being UNKNOWN, its
44 * ids, and libmtp's version number.
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
50 void device_unknown(const int dev_number, const int id_vendor, const int id_product)
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",
57 LIBMTP_VERSION_STRING);
58 LIBMTP_ERROR("Please report this VID/PID and the device model to the "
59 "libmtp development team\n");
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.)
68 * This dumps out a number of bytes to a textual, hexadecimal
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
76 void data_dump (FILE *f, void *buf, uint32_t n)
78 unsigned char *bp = (unsigned char *) buf;
81 for (i = 0; i < n; i++) {
82 fprintf(f, "%02x ", *bp);
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.
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)
100 void data_dump_ascii (FILE *f, void *buf, uint32_t n, uint32_t dump_boundry)
105 unsigned char *bp = (unsigned char *) buf;
109 fprintf(f, "\t%04x:", dump_boundry-0x10);
111 ln = ( remain > 16 ) ? 16 : remain;
113 for (i = 0; i < ln; i++) {
114 if ( ! (i%2) ) fprintf(f, " ");
115 fprintf(f, "%02x", bp[16*lc+i]);
119 int width = ((16-ln)/2)*5 + (2*(ln%2));
120 fprintf(f, "%*.*s", width, width, "");
124 for (i = 0; i < ln; i++) {
125 unsigned char ch= bp[16*lc+i];
126 fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ?
138 char *strndup (const char *s, size_t n)
140 size_t len = strlen (s);