blob: 7f778a527205c9c29e108acb8680ab540ff76880 [file] [log] [blame]
Linus Walleijb9256fd2006-02-15 09:40:43 +00001/**
2 * \file util.c
3 *
4 * This file contains generic utility functions such as can be
5 * used for debugging for example.
Linus Walleij2f45d222007-02-02 22:47:39 +00006 *
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.
Linus Walleijb9256fd2006-02-15 09:40:43 +000023 */
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>
Richard Low8d97bad2010-09-12 17:31:12 +000036#include <string.h>
Marcus Meissner8a783322015-03-29 14:44:14 +020037#include "config.h"
Linus Walleijb9256fd2006-02-15 09:40:43 +000038#include "libmtp.h"
39#include "util.h"
40
Andrés G. Aragoneses524e42c2015-03-31 02:35:21 +020041
42/**
Andrés G. Aragoneses7c321e12015-06-17 20:06:08 +020043 * This prints to stdout info about device being UNKNOWN, its
44 * ids, and libmtp's version number.
Andrés G. Aragoneses524e42c2015-03-31 02:35:21 +020045 *
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 */
50void device_unknown(const int dev_number, const int id_vendor, const int id_product)
51{
52 // This device is unknown to the developers
Andrés G. Aragoneses7c321e12015-06-17 20:06:08 +020053 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN in libmtp v%s.\n",
Andrés G. Aragoneses524e42c2015-03-31 02:35:21 +020054 dev_number,
55 id_vendor,
Andrés G. Aragoneses7c321e12015-06-17 20:06:08 +020056 id_product,
57 LIBMTP_VERSION_STRING);
Andrés G. Aragoneses524e42c2015-03-31 02:35:21 +020058 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
Linus Walleijb9256fd2006-02-15 09:40:43 +000067/**
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 */
76void data_dump (FILE *f, void *buf, uint32_t n)
77{
78 unsigned char *bp = (unsigned char *) buf;
79 uint32_t i;
Linus Walleijdd1f3882014-08-26 22:16:51 +020080
Linus Walleijb9256fd2006-02-15 09:40:43 +000081 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 */
100void 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;
Linus Walleijdd1f3882014-08-26 22:16:51 +0200106
Linus Walleijb9256fd2006-02-15 09:40:43 +0000107 lc = 0;
108 while (remain) {
Linus Walleijca498592006-11-07 16:01:24 +0000109 fprintf(f, "\t%04x:", dump_boundry-0x10);
Linus Walleijdd1f3882014-08-26 22:16:51 +0200110
Linus Walleijb9256fd2006-02-15 09:40:43 +0000111 ln = ( remain > 16 ) ? 16 : remain;
Linus Walleijdd1f3882014-08-26 22:16:51 +0200112
Linus Walleijb9256fd2006-02-15 09:40:43 +0000113 for (i = 0; i < ln; i++) {
114 if ( ! (i%2) ) fprintf(f, " ");
115 fprintf(f, "%02x", bp[16*lc+i]);
116 }
Linus Walleijdd1f3882014-08-26 22:16:51 +0200117
Linus Walleijb9256fd2006-02-15 09:40:43 +0000118 if ( ln < 16 ) {
119 int width = ((16-ln)/2)*5 + (2*(ln%2));
120 fprintf(f, "%*.*s", width, width, "");
121 }
Linus Walleijdd1f3882014-08-26 22:16:51 +0200122
Linus Walleijb9256fd2006-02-15 09:40:43 +0000123 fprintf(f, "\t");
124 for (i = 0; i < ln; i++) {
125 unsigned char ch= bp[16*lc+i];
Linus Walleijdd1f3882014-08-26 22:16:51 +0200126 fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ?
Linus Walleijb9256fd2006-02-15 09:40:43 +0000127 ch : '.');
128 }
129 fprintf(f, "\n");
Linus Walleijdd1f3882014-08-26 22:16:51 +0200130
Linus Walleijb9256fd2006-02-15 09:40:43 +0000131 lc++;
132 remain -= ln;
133 dump_boundry += ln;
134 }
135}
Richard Low8d97bad2010-09-12 17:31:12 +0000136
137#ifndef HAVE_STRNDUP
138char *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