| OLD | NEW |
| 1 /** | 1 /** |
| 2 * \file util.c | 2 * \file util.c |
| 3 * | 3 * |
| 4 * This file contains generic utility functions such as can be | 4 * This file contains generic utility functions such as can be |
| 5 * used for debugging for example. | 5 * used for debugging for example. |
| 6 * | 6 * |
| 7 * Copyright (C) 2005-2007 Linus Walleij <triad@df.lth.se> | 7 * Copyright (C) 2005-2007 Linus Walleij <triad@df.lth.se> |
| 8 * | 8 * |
| 9 * This library is free software; you can redistribute it and/or | 9 * This library is free software; you can redistribute it and/or |
| 10 * modify it under the terms of the GNU Lesser General Public | 10 * modify it under the terms of the GNU Lesser General Public |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #include <sys/time.h> | 27 #include <sys/time.h> |
| 28 #include <unistd.h> | 28 #include <unistd.h> |
| 29 #endif | 29 #endif |
| 30 | 30 |
| 31 #include <stdio.h> | 31 #include <stdio.h> |
| 32 #include <stdlib.h> | 32 #include <stdlib.h> |
| 33 #include <errno.h> | 33 #include <errno.h> |
| 34 #include <sys/stat.h> | 34 #include <sys/stat.h> |
| 35 #include <fcntl.h> | 35 #include <fcntl.h> |
| 36 #include <string.h> | 36 #include <string.h> |
| 37 #include "config.h" |
| 37 #include "libmtp.h" | 38 #include "libmtp.h" |
| 38 #include "util.h" | 39 #include "util.h" |
| 39 | 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 */ |
| 50 void device_unknown(const int dev_number, const int id_vendor, const int id_prod
uct) |
| 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 |
| 40 /** | 67 /** |
| 41 * This dumps out a number of bytes to a textual, hexadecimal | 68 * This dumps out a number of bytes to a textual, hexadecimal |
| 42 * dump. | 69 * dump. |
| 43 * | 70 * |
| 44 * @param f the file to dump to (e.g. stdout or stderr) | 71 * @param f the file to dump to (e.g. stdout or stderr) |
| 45 * @param buf a pointer to the buffer containing the bytes to | 72 * @param buf a pointer to the buffer containing the bytes to |
| 46 * be dumped out in hex | 73 * be dumped out in hex |
| 47 * @param n the number of bytes to dump from this buffer | 74 * @param n the number of bytes to dump from this buffer |
| 48 */ | 75 */ |
| 49 void data_dump (FILE *f, void *buf, uint32_t n) | 76 void data_dump (FILE *f, void *buf, uint32_t n) |
| 50 { | 77 { |
| 51 unsigned char *bp = (unsigned char *) buf; | 78 unsigned char *bp = (unsigned char *) buf; |
| 52 uint32_t i; | 79 uint32_t i; |
| 53 | 80 |
| 54 for (i = 0; i < n; i++) { | 81 for (i = 0; i < n; i++) { |
| 55 fprintf(f, "%02x ", *bp); | 82 fprintf(f, "%02x ", *bp); |
| 56 bp++; | 83 bp++; |
| 57 } | 84 } |
| 58 fprintf(f, "\n"); | 85 fprintf(f, "\n"); |
| 59 } | 86 } |
| 60 | 87 |
| 61 /** | 88 /** |
| 62 * This dumps out a number of bytes to a textual, hexadecimal | 89 * This dumps out a number of bytes to a textual, hexadecimal |
| 63 * dump, and also prints out the string ASCII representation | 90 * dump, and also prints out the string ASCII representation |
| 64 * for each line of bytes. It will also print the memory address | 91 * for each line of bytes. It will also print the memory address |
| 65 * offset from a certain boundry. | 92 * offset from a certain boundry. |
| 66 * | 93 * |
| 67 * @param f the file to dump to (e.g. stdout or stderr) | 94 * @param f the file to dump to (e.g. stdout or stderr) |
| 68 * @param buf a pointer to the buffer containing the bytes to | 95 * @param buf a pointer to the buffer containing the bytes to |
| 69 * be dumped out in hex | 96 * be dumped out in hex |
| 70 * @param n the number of bytes to dump from this buffer | 97 * @param n the number of bytes to dump from this buffer |
| 71 * @param dump_boundry the address offset to start at (usually 0) | 98 * @param dump_boundry the address offset to start at (usually 0) |
| 72 */ | 99 */ |
| 73 void 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) |
| 74 { | 101 { |
| 75 uint32_t remain = n; | 102 uint32_t remain = n; |
| 76 uint32_t ln, lc; | 103 uint32_t ln, lc; |
| 77 int i; | 104 int i; |
| 78 unsigned char *bp = (unsigned char *) buf; | 105 unsigned char *bp = (unsigned char *) buf; |
| 79 | 106 |
| 80 lc = 0; | 107 lc = 0; |
| 81 while (remain) { | 108 while (remain) { |
| 82 fprintf(f, "\t%04x:", dump_boundry-0x10); | 109 fprintf(f, "\t%04x:", dump_boundry-0x10); |
| 83 | 110 |
| 84 ln = ( remain > 16 ) ? 16 : remain; | 111 ln = ( remain > 16 ) ? 16 : remain; |
| 85 | 112 |
| 86 for (i = 0; i < ln; i++) { | 113 for (i = 0; i < ln; i++) { |
| 87 if ( ! (i%2) ) fprintf(f, " "); | 114 if ( ! (i%2) ) fprintf(f, " "); |
| 88 fprintf(f, "%02x", bp[16*lc+i]); | 115 fprintf(f, "%02x", bp[16*lc+i]); |
| 89 } | 116 } |
| 90 | 117 |
| 91 if ( ln < 16 ) { | 118 if ( ln < 16 ) { |
| 92 int width = ((16-ln)/2)*5 + (2*(ln%2)); | 119 int width = ((16-ln)/2)*5 + (2*(ln%2)); |
| 93 fprintf(f, "%*.*s", width, width, ""); | 120 fprintf(f, "%*.*s", width, width, ""); |
| 94 } | 121 } |
| 95 | 122 |
| 96 fprintf(f, "\t"); | 123 fprintf(f, "\t"); |
| 97 for (i = 0; i < ln; i++) { | 124 for (i = 0; i < ln; i++) { |
| 98 unsigned char ch= bp[16*lc+i]; | 125 unsigned char ch= bp[16*lc+i]; |
| 99 fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ? | 126 fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ? |
| 100 ch : '.'); | 127 ch : '.'); |
| 101 } | 128 } |
| 102 fprintf(f, "\n"); | 129 fprintf(f, "\n"); |
| 103 | 130 |
| 104 lc++; | 131 lc++; |
| 105 remain -= ln; | 132 remain -= ln; |
| 106 dump_boundry += ln; | 133 dump_boundry += ln; |
| 107 } | 134 } |
| 108 } | 135 } |
| 109 | 136 |
| 110 #ifndef HAVE_STRNDUP | 137 #ifndef HAVE_STRNDUP |
| 111 char *strndup (const char *s, size_t n) | 138 char *strndup (const char *s, size_t n) |
| 112 { | 139 { |
| 113 size_t len = strlen (s); | 140 size_t len = strlen (s); |
| 114 char *ret; | 141 char *ret; |
| 115 | 142 |
| 116 if (len <= n) | 143 if (len <= n) |
| 117 return strdup (s); | 144 return strdup (s); |
| 118 | 145 |
| 119 ret = malloc(n + 1); | 146 ret = malloc(n + 1); |
| 120 strncpy(ret, s, n); | 147 strncpy(ret, s, n); |
| 121 ret[n] = '\0'; | 148 ret[n] = '\0'; |
| 122 return ret; | 149 return ret; |
| 123 } | 150 } |
| 124 #endif | 151 #endif |
| 125 | 152 |
| OLD | NEW |