1 #include "print_binary.h" 2 #include <linux/log2.h> 3 #include "sane_ctype.h" 4 5 void print_binary(unsigned char *data, size_t len, 6 size_t bytes_per_line, print_binary_t printer, 7 void *extra) 8 { 9 size_t i, j, mask; 10 11 if (!printer) 12 return; 13 14 bytes_per_line = roundup_pow_of_two(bytes_per_line); 15 mask = bytes_per_line - 1; 16 17 printer(BINARY_PRINT_DATA_BEGIN, 0, extra); 18 for (i = 0; i < len; i++) { 19 if ((i & mask) == 0) { 20 printer(BINARY_PRINT_LINE_BEGIN, -1, extra); 21 printer(BINARY_PRINT_ADDR, i, extra); 22 } 23 24 printer(BINARY_PRINT_NUM_DATA, data[i], extra); 25 26 if (((i & mask) == mask) || i == len - 1) { 27 for (j = 0; j < mask-(i & mask); j++) 28 printer(BINARY_PRINT_NUM_PAD, -1, extra); 29 30 printer(BINARY_PRINT_SEP, i, extra); 31 for (j = i & ~mask; j <= i; j++) 32 printer(BINARY_PRINT_CHAR_DATA, data[j], extra); 33 for (j = 0; j < mask-(i & mask); j++) 34 printer(BINARY_PRINT_CHAR_PAD, i, extra); 35 printer(BINARY_PRINT_LINE_END, -1, extra); 36 } 37 } 38 printer(BINARY_PRINT_DATA_END, -1, extra); 39 } 40 41 int is_printable_array(char *p, unsigned int len) 42 { 43 unsigned int i; 44 45 if (!p || !len || p[len - 1] != 0) 46 return 0; 47 48 len--; 49 50 for (i = 0; i < len; i++) { 51 if (!isprint(p[i]) && !isspace(p[i])) 52 return 0; 53 } 54 return 1; 55 } 56