Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file util.c |
| 3 | * |
| 4 | * This file contains generic utility functions such as can be |
| 5 | * used for debugging for example. |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 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. |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 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> |
Richard Low | 8d97bad | 2010-09-12 17:31:12 +0000 | [diff] [blame] | 36 | #include <string.h> |
Marcus Meissner | 8a78332 | 2015-03-29 14:44:14 +0200 | [diff] [blame] | 37 | #include "config.h" |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 38 | #include "libmtp.h" |
| 39 | #include "util.h" |
| 40 | |
Andrés G. Aragoneses | 524e42c | 2015-03-31 02:35:21 +0200 | [diff] [blame^] | 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 | */ |
| 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.\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 Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 66 | /** |
| 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 | */ |
| 75 | void data_dump (FILE *f, void *buf, uint32_t n) |
| 76 | { |
| 77 | unsigned char *bp = (unsigned char *) buf; |
| 78 | uint32_t i; |
Linus Walleij | dd1f388 | 2014-08-26 22:16:51 +0200 | [diff] [blame] | 79 | |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 80 | 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 | */ |
| 99 | void 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 Walleij | dd1f388 | 2014-08-26 22:16:51 +0200 | [diff] [blame] | 105 | |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 106 | lc = 0; |
| 107 | while (remain) { |
Linus Walleij | ca49859 | 2006-11-07 16:01:24 +0000 | [diff] [blame] | 108 | fprintf(f, "\t%04x:", dump_boundry-0x10); |
Linus Walleij | dd1f388 | 2014-08-26 22:16:51 +0200 | [diff] [blame] | 109 | |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 110 | ln = ( remain > 16 ) ? 16 : remain; |
Linus Walleij | dd1f388 | 2014-08-26 22:16:51 +0200 | [diff] [blame] | 111 | |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 112 | for (i = 0; i < ln; i++) { |
| 113 | if ( ! (i%2) ) fprintf(f, " "); |
| 114 | fprintf(f, "%02x", bp[16*lc+i]); |
| 115 | } |
Linus Walleij | dd1f388 | 2014-08-26 22:16:51 +0200 | [diff] [blame] | 116 | |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 117 | if ( ln < 16 ) { |
| 118 | int width = ((16-ln)/2)*5 + (2*(ln%2)); |
| 119 | fprintf(f, "%*.*s", width, width, ""); |
| 120 | } |
Linus Walleij | dd1f388 | 2014-08-26 22:16:51 +0200 | [diff] [blame] | 121 | |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 122 | fprintf(f, "\t"); |
| 123 | for (i = 0; i < ln; i++) { |
| 124 | unsigned char ch= bp[16*lc+i]; |
Linus Walleij | dd1f388 | 2014-08-26 22:16:51 +0200 | [diff] [blame] | 125 | fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ? |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 126 | ch : '.'); |
| 127 | } |
| 128 | fprintf(f, "\n"); |
Linus Walleij | dd1f388 | 2014-08-26 22:16:51 +0200 | [diff] [blame] | 129 | |
Linus Walleij | b9256fd | 2006-02-15 09:40:43 +0000 | [diff] [blame] | 130 | lc++; |
| 131 | remain -= ln; |
| 132 | dump_boundry += ln; |
| 133 | } |
| 134 | } |
Richard Low | 8d97bad | 2010-09-12 17:31:12 +0000 | [diff] [blame] | 135 | |
| 136 | #ifndef HAVE_STRNDUP |
| 137 | char *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 | |