1 /** 2 * \file util.c 3 * 4 * This file contains generic utility functions such as can be 5 * used for debugging for example. 6 * 7 * Copyright (C) 2005-2007 Linus Walleij <triad (at) 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. 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> 36 #include "libmtp.h" 37 #include "util.h" 38 39 /** 40 * This dumps out a number of bytes to a textual, hexadecimal 41 * dump. 42 * 43 * @param f the file to dump to (e.g. stdout or stderr) 44 * @param buf a pointer to the buffer containing the bytes to 45 * be dumped out in hex 46 * @param n the number of bytes to dump from this buffer 47 */ 48 void data_dump (FILE *f, void *buf, uint32_t n) 49 { 50 unsigned char *bp = (unsigned char *) buf; 51 uint32_t i; 52 53 for (i = 0; i < n; i++) { 54 fprintf(f, "%02x ", *bp); 55 bp++; 56 } 57 fprintf(f, "\n"); 58 } 59 60 /** 61 * This dumps out a number of bytes to a textual, hexadecimal 62 * dump, and also prints out the string ASCII representation 63 * for each line of bytes. It will also print the memory address 64 * offset from a certain boundry. 65 * 66 * @param f the file to dump to (e.g. stdout or stderr) 67 * @param buf a pointer to the buffer containing the bytes to 68 * be dumped out in hex 69 * @param n the number of bytes to dump from this buffer 70 * @param dump_boundry the address offset to start at (usually 0) 71 */ 72 void data_dump_ascii (FILE *f, void *buf, uint32_t n, uint32_t dump_boundry) 73 { 74 uint32_t remain = n; 75 uint32_t ln, lc; 76 int i; 77 unsigned char *bp = (unsigned char *) buf; 78 79 lc = 0; 80 while (remain) { 81 fprintf(f, "\t%04x:", dump_boundry-0x10); 82 83 ln = ( remain > 16 ) ? 16 : remain; 84 85 for (i = 0; i < ln; i++) { 86 if ( ! (i%2) ) fprintf(f, " "); 87 fprintf(f, "%02x", bp[16*lc+i]); 88 } 89 90 if ( ln < 16 ) { 91 int width = ((16-ln)/2)*5 + (2*(ln%2)); 92 fprintf(f, "%*.*s", width, width, ""); 93 } 94 95 fprintf(f, "\t"); 96 for (i = 0; i < ln; i++) { 97 unsigned char ch= bp[16*lc+i]; 98 fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ? 99 ch : '.'); 100 } 101 fprintf(f, "\n"); 102 103 lc++; 104 remain -= ln; 105 dump_boundry += ln; 106 } 107 } 108