4 * This file contains generic utility functions such as can be
5 * used for debugging for example.
7 * Copyright (C) 2005-2007 Linus Walleij <triad@df.lth.se>
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.
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.
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.
25 /* MSVC does not have these */
42 * This dumps out a number of bytes to a textual, hexadecimal
45 * @param f the file to dump to (e.g. stdout or stderr)
46 * @param buf a pointer to the buffer containing the bytes to
47 * be dumped out in hex
48 * @param n the number of bytes to dump from this buffer
50 void data_dump (FILE *f, void *buf, uint32_t n)
52 unsigned char *bp = (unsigned char *) buf;
55 for (i = 0; i < n; i++) {
56 fprintf(f, "%02x ", *bp);
63 * This dumps out a number of bytes to a textual, hexadecimal
64 * dump, and also prints out the string ASCII representation
65 * for each line of bytes. It will also print the memory address
66 * offset from a certain boundry.
68 * @param f the file to dump to (e.g. stdout or stderr)
69 * @param buf a pointer to the buffer containing the bytes to
70 * be dumped out in hex
71 * @param n the number of bytes to dump from this buffer
72 * @param dump_boundry the address offset to start at (usually 0)
74 void data_dump_ascii (FILE *f, void *buf, uint32_t n, uint32_t dump_boundry)
79 unsigned char *bp = (unsigned char *) buf;
83 fprintf(f, "\t%04x:", dump_boundry-0x10);
85 ln = ( remain > 16 ) ? 16 : remain;
87 for (i = 0; i < ln; i++) {
88 if ( ! (i%2) ) fprintf(f, " ");
89 fprintf(f, "%02x", bp[16*lc+i]);
93 int width = ((16-ln)/2)*5 + (2*(ln%2));
94 fprintf(f, "%*.*s", width, width, "");
98 for (i = 0; i < ln; i++) {
99 unsigned char ch= bp[16*lc+i];
100 fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ?
112 char *strndup (const char *s, size_t n)
114 size_t len = strlen (s);