/ hexdump.c
hexdump.c
1 /* 2 * hexdump implementation without depenecies to *printf() 3 * output is equal to 'hexdump -C' 4 * should be compatible to 64bit architectures 5 * 6 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de> 7 * 8 * This program is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 #define hex_print(p) applog(LOG_DEBUG, "%s", p) 23 24 static char nibble[] = { 25 '0', '1', '2', '3', '4', '5', '6', '7', 26 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; 27 28 #define BYTES_PER_LINE 0x10 29 30 static void hexdump(const uint8_t *p, unsigned int len) 31 { 32 unsigned int i, addr; 33 unsigned int wordlen = sizeof(unsigned int); 34 unsigned char v, line[BYTES_PER_LINE * 5]; 35 36 for (addr = 0; addr < len; addr += BYTES_PER_LINE) { 37 /* clear line */ 38 for (i = 0; i < sizeof(line); i++) { 39 if (i == wordlen * 2 + 52 || 40 i == wordlen * 2 + 69) { 41 line[i] = '|'; 42 continue; 43 } 44 45 if (i == wordlen * 2 + 70) { 46 line[i] = '\0'; 47 continue; 48 } 49 50 line[i] = ' '; 51 } 52 53 /* print address */ 54 for (i = 0; i < wordlen * 2; i++) { 55 v = addr >> ((wordlen * 2 - i - 1) * 4); 56 line[i] = nibble[v & 0xf]; 57 } 58 59 /* dump content */ 60 for (i = 0; i < BYTES_PER_LINE; i++) { 61 int pos = (wordlen * 2) + 3 + (i / 8); 62 63 if (addr + i >= len) 64 break; 65 66 v = p[addr + i]; 67 line[pos + (i * 3) + 0] = nibble[v >> 4]; 68 line[pos + (i * 3) + 1] = nibble[v & 0xf]; 69 70 /* character printable? */ 71 line[(wordlen * 2) + 53 + i] = 72 (v >= ' ' && v <= '~') ? v : '.'; 73 } 74 75 hex_print(line); 76 } 77 }