| 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" | |
| 38 #include "libmtp.h" | 37 #include "libmtp.h" |
| 39 #include "util.h" | 38 #include "util.h" |
| 40 | 39 |
| 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 | |
| 67 /** | 40 /** |
| 68 * This dumps out a number of bytes to a textual, hexadecimal | 41 * This dumps out a number of bytes to a textual, hexadecimal |
| 69 * dump. | 42 * dump. |
| 70 * | 43 * |
| 71 * @param f the file to dump to (e.g. stdout or stderr) | 44 * @param f the file to dump to (e.g. stdout or stderr) |
| 72 * @param buf a pointer to the buffer containing the bytes to | 45 * @param buf a pointer to the buffer containing the bytes to |
| 73 * be dumped out in hex | 46 * be dumped out in hex |
| 74 * @param n the number of bytes to dump from this buffer | 47 * @param n the number of bytes to dump from this buffer |
| 75 */ | 48 */ |
| 76 void data_dump (FILE *f, void *buf, uint32_t n) | 49 void data_dump (FILE *f, void *buf, uint32_t n) |
| 77 { | 50 { |
| 78 unsigned char *bp = (unsigned char *) buf; | 51 unsigned char *bp = (unsigned char *) buf; |
| 79 uint32_t i; | 52 uint32_t i; |
| 80 | 53 |
| 81 for (i = 0; i < n; i++) { | 54 for (i = 0; i < n; i++) { |
| 82 fprintf(f, "%02x ", *bp); | 55 fprintf(f, "%02x ", *bp); |
| 83 bp++; | 56 bp++; |
| 84 } | 57 } |
| 85 fprintf(f, "\n"); | 58 fprintf(f, "\n"); |
| 86 } | 59 } |
| 87 | 60 |
| 88 /** | 61 /** |
| 89 * This dumps out a number of bytes to a textual, hexadecimal | 62 * This dumps out a number of bytes to a textual, hexadecimal |
| 90 * dump, and also prints out the string ASCII representation | 63 * dump, and also prints out the string ASCII representation |
| 91 * for each line of bytes. It will also print the memory address | 64 * for each line of bytes. It will also print the memory address |
| 92 * offset from a certain boundry. | 65 * offset from a certain boundry. |
| 93 * | 66 * |
| 94 * @param f the file to dump to (e.g. stdout or stderr) | 67 * @param f the file to dump to (e.g. stdout or stderr) |
| 95 * @param buf a pointer to the buffer containing the bytes to | 68 * @param buf a pointer to the buffer containing the bytes to |
| 96 * be dumped out in hex | 69 * be dumped out in hex |
| 97 * @param n the number of bytes to dump from this buffer | 70 * @param n the number of bytes to dump from this buffer |
| 98 * @param dump_boundry the address offset to start at (usually 0) | 71 * @param dump_boundry the address offset to start at (usually 0) |
| 99 */ | 72 */ |
| 100 void data_dump_ascii (FILE *f, void *buf, uint32_t n, uint32_t dump_boundry) | 73 void data_dump_ascii (FILE *f, void *buf, uint32_t n, uint32_t dump_boundry) |
| 101 { | 74 { |
| 102 uint32_t remain = n; | 75 uint32_t remain = n; |
| 103 uint32_t ln, lc; | 76 uint32_t ln, lc; |
| 104 int i; | 77 int i; |
| 105 unsigned char *bp = (unsigned char *) buf; | 78 unsigned char *bp = (unsigned char *) buf; |
| 106 | 79 |
| 107 lc = 0; | 80 lc = 0; |
| 108 while (remain) { | 81 while (remain) { |
| 109 fprintf(f, "\t%04x:", dump_boundry-0x10); | 82 fprintf(f, "\t%04x:", dump_boundry-0x10); |
| 110 | 83 |
| 111 ln = ( remain > 16 ) ? 16 : remain; | 84 ln = ( remain > 16 ) ? 16 : remain; |
| 112 | 85 |
| 113 for (i = 0; i < ln; i++) { | 86 for (i = 0; i < ln; i++) { |
| 114 if ( ! (i%2) ) fprintf(f, " "); | 87 if ( ! (i%2) ) fprintf(f, " "); |
| 115 fprintf(f, "%02x", bp[16*lc+i]); | 88 fprintf(f, "%02x", bp[16*lc+i]); |
| 116 } | 89 } |
| 117 | 90 |
| 118 if ( ln < 16 ) { | 91 if ( ln < 16 ) { |
| 119 int width = ((16-ln)/2)*5 + (2*(ln%2)); | 92 int width = ((16-ln)/2)*5 + (2*(ln%2)); |
| 120 fprintf(f, "%*.*s", width, width, ""); | 93 fprintf(f, "%*.*s", width, width, ""); |
| 121 } | 94 } |
| 122 | 95 |
| 123 fprintf(f, "\t"); | 96 fprintf(f, "\t"); |
| 124 for (i = 0; i < ln; i++) { | 97 for (i = 0; i < ln; i++) { |
| 125 unsigned char ch= bp[16*lc+i]; | 98 unsigned char ch= bp[16*lc+i]; |
| 126 fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ? | 99 fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ? |
| 127 ch : '.'); | 100 ch : '.'); |
| 128 } | 101 } |
| 129 fprintf(f, "\n"); | 102 fprintf(f, "\n"); |
| 130 | 103 |
| 131 lc++; | 104 lc++; |
| 132 remain -= ln; | 105 remain -= ln; |
| 133 dump_boundry += ln; | 106 dump_boundry += ln; |
| 134 } | 107 } |
| 135 } | 108 } |
| 136 | 109 |
| 137 #ifndef HAVE_STRNDUP | 110 #ifndef HAVE_STRNDUP |
| 138 char *strndup (const char *s, size_t n) | 111 char *strndup (const char *s, size_t n) |
| 139 { | 112 { |
| 140 size_t len = strlen (s); | 113 size_t len = strlen (s); |
| 141 char *ret; | 114 char *ret; |
| 142 | 115 |
| 143 if (len <= n) | 116 if (len <= n) |
| 144 return strdup (s); | 117 return strdup (s); |
| 145 | 118 |
| 146 ret = malloc(n + 1); | 119 ret = malloc(n + 1); |
| 147 strncpy(ret, s, n); | 120 strncpy(ret, s, n); |
| 148 ret[n] = '\0'; | 121 ret[n] = '\0'; |
| 149 return ret; | 122 return ret; |
| 150 } | 123 } |
| 151 #endif | 124 #endif |
| 152 | 125 |
| OLD | NEW |