blob: 56e2d8233697d5ea5d7976a4f998cc3ff1526934 [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/**
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 */
50void 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.\n",
54 dev_number,
55 id_vendor,
56 id_product);
57 LIBMTP_ERROR("Please report this VID/PID and the device model to the "
58 "libmtp development team\n");
59 /*
60 * Trying to get iManufacturer or iProduct from the device at this
61 * point would require opening a device handle, that we don't want
62 * to do right now. (Takes time for no good enough reason.)
63 */
64}
65
Linus Walleijb9256fd2006-02-15 09:40:43 +000066/**
67 * This dumps out a number of bytes to a textual, hexadecimal
68 * dump.
69 *
70 * @param f the file to dump to (e.g. stdout or stderr)
71 * @param buf a pointer to the buffer containing the bytes to
72 * be dumped out in hex
73 * @param n the number of bytes to dump from this buffer
74 */
75void data_dump (FILE *f, void *buf, uint32_t n)
76{
77 unsigned char *bp = (unsigned char *) buf;
78 uint32_t i;
Linus Walleijdd1f3882014-08-26 22:16:51 +020079
Linus Walleijb9256fd2006-02-15 09:40:43 +000080 for (i = 0; i < n; i++) {
81 fprintf(f, "%02x ", *bp);
82 bp++;
83 }
84 fprintf(f, "\n");
85}
86
87/**
88 * This dumps out a number of bytes to a textual, hexadecimal
89 * dump, and also prints out the string ASCII representation
90 * for each line of bytes. It will also print the memory address
91 * offset from a certain boundry.
92 *
93 * @param f the file to dump to (e.g. stdout or stderr)
94 * @param buf a pointer to the buffer containing the bytes to
95 * be dumped out in hex
96 * @param n the number of bytes to dump from this buffer
97 * @param dump_boundry the address offset to start at (usually 0)
98 */
99void data_dump_ascii (FILE *f, void *buf, uint32_t n, uint32_t dump_boundry)
100{
101 uint32_t remain = n;
102 uint32_t ln, lc;
103 int i;
104 unsigned char *bp = (unsigned char *) buf;
Linus Walleijdd1f3882014-08-26 22:16:51 +0200105
Linus Walleijb9256fd2006-02-15 09:40:43 +0000106 lc = 0;
107 while (remain) {
Linus Walleijca498592006-11-07 16:01:24 +0000108 fprintf(f, "\t%04x:", dump_boundry-0x10);
Linus Walleijdd1f3882014-08-26 22:16:51 +0200109
Linus Walleijb9256fd2006-02-15 09:40:43 +0000110 ln = ( remain > 16 ) ? 16 : remain;
Linus Walleijdd1f3882014-08-26 22:16:51 +0200111
Linus Walleijb9256fd2006-02-15 09:40:43 +0000112 for (i = 0; i < ln; i++) {
113 if ( ! (i%2) ) fprintf(f, " ");
114 fprintf(f, "%02x", bp[16*lc+i]);
115 }
Linus Walleijdd1f3882014-08-26 22:16:51 +0200116
Linus Walleijb9256fd2006-02-15 09:40:43 +0000117 if ( ln < 16 ) {
118 int width = ((16-ln)/2)*5 + (2*(ln%2));
119 fprintf(f, "%*.*s", width, width, "");
120 }
Linus Walleijdd1f3882014-08-26 22:16:51 +0200121
Linus Walleijb9256fd2006-02-15 09:40:43 +0000122 fprintf(f, "\t");
123 for (i = 0; i < ln; i++) {
124 unsigned char ch= bp[16*lc+i];
Linus Walleijdd1f3882014-08-26 22:16:51 +0200125 fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ?
Linus Walleijb9256fd2006-02-15 09:40:43 +0000126 ch : '.');
127 }
128 fprintf(f, "\n");
Linus Walleijdd1f3882014-08-26 22:16:51 +0200129
Linus Walleijb9256fd2006-02-15 09:40:43 +0000130 lc++;
131 remain -= ln;
132 dump_boundry += ln;
133 }
134}
Richard Low8d97bad2010-09-12 17:31:12 +0000135
136#ifndef HAVE_STRNDUP
137char *strndup (const char *s, size_t n)
138{
139 size_t len = strlen (s);
140 char *ret;
141
142 if (len <= n)
143 return strdup (s);
144
145 ret = malloc(n + 1);
146 strncpy(ret, s, n);
147 ret[n] = '\0';
148 return ret;
149}
150#endif
151