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